________________
Poradnik poniżej jest po prostu listą skryptów które mogą być zoptymalizowane po przez ich skrócenie. Nie będę tłumaczył działania każdego z kodów, bo zajęłoby to po prostu wieczność. Jeśli już wiesz czego szukasz to powinieneś wiedzieć jak to działa. Zaczynajmy.
Zamiast pisania tej instrukcji w ten sposób:
Kod PHP:
//zle
x = 7
if x < 10 then
x = 10
end
Kod PHP:
//dobrze
x = 7
x = math.max(x, 10)
Kod PHP:
//zle
x = 23
if x > 20 then
x = 20
end
Kod PHP:
//dobrze
x = 23
x = math.min(x, 20)
Kod PHP:
//zle
x = 23
if x < 10 then
x = 10
elseif x > 20 then
x = 20
end
Kod PHP:
//dobrze
x = 23
x = math.min(math.max(x, 10), 20)
Kod PHP:
//dobrze
x = 23
x = math.max(math.min(x, 20), 10)
Następna optymalizacja może nastąpić w tym skrypcie:
Kod PHP:
//zle
if x == 10 then
y = 100
b = 50
xybz = x + y + b + 35
elseif x == 20 then
y = 100
b = 50
xybz = x + y + b + 110
else
y = 30
b = 50
xybz = x + y + b + 20
end
Kod PHP:
//dobrze
y = 100
b = 50
if x == 10 then
z = 35
elseif x == 20 then
z = 110
else
y = 30
z = 20
end
xybz = x + y + b + z
Kolejnym błędnym kodem będzie to:
Kod PHP:
//zle
if x == 10 then
y = 100
b = 50
xybz = x + y + b + 35
elseif x == 20 then
y = 40
b = 78
xybz = x + y + b + 76
elseif x == 30 then
y = 80
b = 50
xybz = x + y + b + 45
elseif x == 40 then
y = 100
b = 666
xybz = x + y + b + 144
elseif x == 50 then
y = 33
b = 45
xybz = x + y + b + 12
Kod PHP:
//prawie dobrze
function numbers(x, y, b, z)
return (x + y + b + z)
end
if x == 10 then
xybz = numbers(x, 100, 50, 35)
elseif x == 20 then
xybz = numbers(x, 40, 78, 76)
elseif x == 30 then
xybz = numbers(x, 80, 50, 45)
elseif x == 40 then
xybz = numbers(x, 100, 666, 144)
elseif x == 50 then
xybz = numbers(x, 33, 45, 12)
else
xybz = numbers(x, 100, 100, 20)
end
Kod PHP:
//dobrze
results = {
[10] = {100, 50, 35},
[20] = {40, 78, 76},
[30] = {80, 50, 45},
[40] = {100, 666, 144},
[50] = {33, 45, 12},
default = {100, 100, 20}
}
result = results[x] or results.default
xybz = x + result[1] + result[2] + result[3]
A zamiast tego:
Kod PHP:
//zle
doSummonCreature("Rat", pos)
doSummonCreature("Rat", pos)
doSummonCreature("Rat", pos)
doSummonCreature("Troll", pos)
doSummonCreature("Troll", pos)
doSummonCreature("Orc", pos)
Kod PHP:
//dobrze
local summons = {["Rat"] = 3, ["Troll"] = 2, ["Orc"] = 1}
for k, v in pairs(summons) do
for i=1, v do
doSummonCreature(k, pos)
end
end
Tutaj kolejny za długi skrypt który będzie nam dawał addony:
Kod PHP:
//zle
sexAddons = {
{[136]=142, [147]=150, [155]=158, [252]=252},
{[128]=134, [143]=146, [151]=154, [251]=251}
}
local addons = sexAddons[getPlayerSex(cid)+1]
if (addons ~= nil) then
for k, v in pairs(addons) do
for i=k, v do
doPlayerAddOutfit(player, i, addonType[1])
end
end
end
Kod PHP:
//dobrze
numbers = {
[10] = 30,
[55] = 65,
[80] = 140
}
for start, stop in pairs(numbers) do
for number=start, stop do
print(number)
end
end
Kod PHP:
//zle
function doSomething(x)
if x == nil then
x = 1
end
return x
end
Kod PHP:
//dobrze
function doSomething(x)
return x or 1
end
Jeszcze większe skrócenie:
Kod PHP:
//dobrze
x = 5
y = 10
z = x or y or 5
Kolejny przykład który nigdy nie zwróci nil'a:
Kod PHP:
//dobrze bobrze
function sum(a, b)
return (a or 0) + (b or 0)
end
Kod PHP:
print(sum(5, 10))
print(sum(5, nil))
print(sum(nil, 10))
print(sum(5))
Kod:
15
5
10
5
Kod PHP:
//zle
if x == nil then
if y == nil then
print("Error, all values are nil!")
else
z = y
end
else
z = x
end
Kod PHP:
//prawie dobrze
z = x or y or print("Error, all values are nil")
Spójrz:
Kod PHP:
//dobrze
z = x or y or print("Error, all values are nil") or 0
Tutaj mamy kolejny przykład używania "or":
Kod PHP:
//zle
function onSay(cid, words, param)
param = tonumber(param)
vocations = {"Sorcerer", "Druid", "Paladin", "Knight", "Master Sorcerer", "Elder Druid", "Royal Paladin", "Elite Knight"}
if vocations[param] ~= nil then
vocation = vocations[param]
else
vocation = "Unknown"
end
end
Kod PHP:
//dobrze
function onSay(cid, words, param)
param = tonumber(param)
vocations = {"Sorcerer", "Druid", "Paladin", "Knight", "Master Sorcerer", "Elder Druid", "Royal Paladin", "Elite Knight"}
vocation = vocations[param] or "Unknown"
end
Kod PHP:
//zle
if x and y and z then
pos = {x=x, y=y, z=z}
end
Kod PHP:
//dobrze
(a and b) or (c and d) or e
Czyli to:
Kod PHP:
//dobrze
result = (a and b) or (c and d) or e
Kod PHP:
//zle
if a then
result = b
elseif c then
result = d
else
result = e
end
Kod PHP:
function position(x, y, z, stackpos)
if ((x and y and z) and type(tonumber(x..y..z)) == 'number') then
pos = {x=x, y=y, z=z}
if (stackpos and type(stackpos) == 'number') then
pos.stackpos = stackpos
end
return pos
end
return false
end
Kod PHP:
//zle
type(tonumber(x) == 'number' and type(tonumber(y) == 'number' and type(tonumber(z) == 'number'
Kod PHP:
//dobrze
type(tonumber(x..y..z)) == 'number'
Kod PHP:
x = "134"
y = "abc"
z = "7"
print(tonumber(x))
print(tonumber(y))
print(tonumber(z))
print(tonumber(x..y..z))
Kod:
134
nil
7
nil
Zamiast tego długiego skryptu:
Kod PHP:
//zle
if (x > z) then
r = x
else
r = z
end
Kod PHP:
//dobrze
r = (x > z and x) or z
Kolejny przykład:
Kod PHP:
//zle
if (type(x) == "number" and type(y) == "number") then
r = math.max(x, y)
else
r = 0
end
Kod PHP:
//dobrze
r = (type(x) == "number" and type(y) == "number" and math.max(x, y)) or 0
Kod PHP:
//zle
if (type(x) == "number" and type(y) == "number") then
r = math.max(x, y)
elseif (type(x) == "number") then
r = x
elseif (type(y) == "number") then
r = y
else
r = 0
end
Kod PHP:
//zle
r = (type(x) == "number" and type(y) == "number" and math.max(x, y)) or (type(x) == "number" and x) or (type(y) == "number" and y) or 0
Poradnik pisany z pomocą tutoriala z otland'u. Zabraniam jego kopiowania na inne strony bez mojej zgody, korzystam jeszcze z nicku 'Grimekk'.
Cześć.
Zakładki