So i'm really lazy and i hate typing over and over the same thing (why
wouldnt anyone?). I set myself a goal to try and reduce everything
that is not worth more than 5 seconds of your precious time.
[Note that this is nowhere to be a complete solution (yet).]
When i say table, set, list, array they
will be refering all to the same object (at least in this post). So if
i were to talk about an empty set or empty array/table/list it means:
or
Theres some functions that could aid (coolaid yeah) in certain
situations and i will talk about them in here.
First let's talk about map, it is a function that applies a
given function to each element of a list. You could see it as y = f(x).
It returns a list whose elements are the mapped elements.
Now you will be asking, what's this nonsense all about? So let's get
practical
place_monster.lua
This gives you the ability to execute command /m rat;spider;demon
Moving on
mapn is like y = f(x,y). (If this needs further explanation
please do tell me so i can expand this)
data/spells/lib/spells.lua
So far so good; with a little more time i think it can be reduced
quite significatively.
map, mapn
Ideas are truly appreciated, of course code implementations too
wouldnt anyone?). I set myself a goal to try and reduce everything
that is not worth more than 5 seconds of your precious time.
[Note that this is nowhere to be a complete solution (yet).]
When i say table, set, list, array they
will be refering all to the same object (at least in this post). So if
i were to talk about an empty set or empty array/table/list it means:
Code:
{}
or
Code:
list = {}
Theres some functions that could aid (coolaid yeah) in certain
situations and i will talk about them in here.
First let's talk about map, it is a function that applies a
given function to each element of a list. You could see it as y = f(x).
It returns a list whose elements are the mapped elements.
Code:
map(function, list)
Now you will be asking, what's this nonsense all about? So let's get
practical
place_monster.lua
Code:
function onSay(player, words, param)
local orig = player:getPosition()
local creatures = param:split(";")
local summon = function(c) return doSummonCreature(c, orig) end
local check = function(s)
if s ~= false then
local monster = Monster(s)
monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
orig:sendMagicEffect(CONST_ME_MAGIC_RED)
else
player:sendCancelMessage("There is not enough room.")
orig:sendMagicEffect(CONST_ME_POFF)
end
end
creatures = map(summon, creatures)
map(check, creatures)
return false
end
This gives you the ability to execute command /m rat;spider;demon
Moving on
Code:
mapn(function, list, list)
mapn is like y = f(x,y). (If this needs further explanation
please do tell me so i can expand this)
data/spells/lib/spells.lua
Code:
energy = {COMBAT_ENERGYDAMAGE, CONST_ME_ENERGYAREA, CONST_ANI_ENERGY}
PARAM_LIST = {COMBAT_PARAM_TYPE, COMBAT_PARAM_EFFECT, COMBAT_PARAM_DISTANCEEFFECT}
function newCombat(element)
local combat = Combat()
mapn(function(x,y) return setCombatParam(combat,x,y) end, PARAM_LIST, element)
return combat
end
So far so good; with a little more time i think it can be reduced
quite significatively.
map, mapn
Code:
function map(func, array)
local new_array = {}
for i,v in ipairs(array) do
new_array[i] = func(v)
end
return new_array
end
function mapn(func, ...)
local new_array = {}
local i=1
local arg = table.pack(...)
local arg_length = #arg
while true do
local arg_list = map(function(arr) return arr[i] end, arg)
if #arg_list < arg_length then return new_array end
new_array[i] = func(table.unpack(arg_list))
i = i+1
end
end
Ideas are truly appreciated, of course code implementations too