• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Factoring code

LaloHao

Member
Joined
Sep 14, 2008
Messages
73
Reaction score
14
Location
Mexico
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:

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)

Capturadepantalla_2015-08-13_11-57-44.png


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 :)
 
To be able to reduce even more:

Capturadepantalla_2015-08-15_14-08-58.png


Setting formula
Code:
local min = (level / 5) + (maglevel * 4.5) + 35
local max = (level / 5) + (maglevel * 7.3) + 55

Analyzing the formula function we can see that there are two
operations involved.
(+) (*) sum and multiplication

(Since division is just the inverse of multiplication)

We can also represent (level/5) as level*(1/5)
and this give us the ability to map the function easily.

The function below returns {level / 5, maglevel * 4.5, 35}
Code:
mapn(function(x,y) return x*y end, {1/5, 4*5, 35}, {level, maglevel, 1})

All that is left is to sum the values and we're done
Code:
function onGetFormulaValues(player, level, maglevel)
   local param = {level, maglevel, 1}
   local min = sum(mapn(function(x,y) return x*y end, min, param))
   local max = sum(mapn(function(x,y) return x*y end, max, param))
   return -min, -max
end

data/spells/lib/spells.lua now looks like this
Code:
ice = {COMBAT_ICEDAMAGE, CONST_ME_ICEAREA, CONST_ANI_ICE}
energy = {COMBAT_ENERGYDAMAGE, CONST_ME_ENERGYAREA, CONST_ANI_ENERGY}

PARAM_LIST = {COMBAT_PARAM_TYPE, COMBAT_PARAM_EFFECT, COMBAT_PARAM_DISTANCEEFFECT}

function setCombatElement(combat, element)
   mapn(function(x,y) return setCombatParam(combat,x,y) end, PARAM_LIST, element)
end

function setCombatFormula(combat, min, max)
   function onGetFormulaValues(player, level, maglevel)
      local param = {level, maglevel, 1}
      local min = sum(mapn(function(x,y) return x*y end, min, param))
      local max = sum(mapn(function(x,y) return x*y end, max, param))
      return -min, -max
   end
   setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
end

function newCombat(element, min, max)
   local combat = Combat()
   setCombatElement(combat, element)
   setCombatFormula(combat, min, max)
   return combat
end

The so called sum function
Code:
function sum (lst)
   local result = 0
   for i = 1, #lst do
      result = result + lst[i]
   end
   return result
end

Im happy indeed
 
Back
Top