• 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!

Lua Reduce lua code

pasiak12

Well-Known Member
Joined
Jun 7, 2009
Messages
261
Solutions
13
Reaction score
71
Hi!

I would like to reduce my lua code. There are a lot of repeated functions but with another variables. I'd like to use there some table initialization like in c++, but in LUA.

Example spell:

Code:
local combat1 = Combat()
combat1:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat1:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat1:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat1:setParameter(COMBAT_PARAM_USECHARGES, true)
combat1:setArea(createCombatArea(AREA_g1))


local combat2 = Combat()
combat2:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat2:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat2:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat2:setParameter(COMBAT_PARAM_USECHARGES, true)
combat2:setArea(createCombatArea(AREA_g2))

local combat3 = Combat()
combat3:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat3:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat3:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat3:setParameter(COMBAT_PARAM_USECHARGES, true)
combat3:setArea(createCombatArea(AREA_g3))

local combat4 = Combat()
combat4:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat4:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat4:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat4:setParameter(COMBAT_PARAM_USECHARGES, true)
combat4:setArea(createCombatArea(AREA_g4))

local combat5 = Combat()
combat5:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat5:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat5:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat5:setParameter(COMBAT_PARAM_USECHARGES, true)
combat5:setArea(createCombatArea(AREA_g5))

local combat6 = Combat()
combat6:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat6:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat6:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat6:setParameter(COMBAT_PARAM_USECHARGES, true)
combat6:setArea(createCombatArea(AREA_g6))

local combat7 = Combat()
combat7:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat7:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat7:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat7:setParameter(COMBAT_PARAM_USECHARGES, true)
combat7:setArea(createCombatArea(AREA_g7))

local combat8 = Combat()
combat8:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat8:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
combat8:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat8:setParameter(COMBAT_PARAM_USECHARGES, true)
combat8:setArea(createCombatArea(AREA_g8))

function onGetFormulaValues1(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end

function onGetFormulaValues2(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end

function onGetFormulaValues3(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end
function onGetFormulaValues4(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end
function onGetFormulaValues5(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end
function onGetFormulaValues6(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end
function onGetFormulaValues7(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end
function onGetFormulaValues8(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end

combat1:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues1")
combat2:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues2")
combat3:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues3")
combat4:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues4")
combat5:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues5")
combat6:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues6")
combat7:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues7")
combat8:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues8")


function onCastSpell(creature, variant)

    mana20 = creature:getMaxMana() * 20/100

    if creature:getMana() >= mana20 then
        creature:addMana(- mana20 )
    else
        creature:getPlayer():sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Not enough mana points")
        return
    end


    local i =0;
    local mul = 100;
   

    addEvent(doCombat, i*mul, creature.uid, combat1, variant)

    i=i+1
    addEvent(doCombat, i*mul, creature.uid, combat2, variant)
   
    i=i+1
    addEvent(doCombat, i*mul, creature.uid, combat3, variant)
   
    i=i+1
    addEvent(doCombat, i*mul, creature.uid, combat4, variant)
   
    i=i+1
    addEvent(doCombat, i*mul, creature.uid, combat5, variant)
   
    i=i+1
    addEvent(doCombat, i*mul, creature.uid, combat6, variant)
   
    i=i+1
    addEvent(doCombat, i*mul, creature.uid, combat7, variant)
   
    i=i+1
    addEvent(doCombat, i*mul, creature.uid, combat8, variant)
   
    return
end

For example in C++ language the solution for declaring all the combats would be like this:

PSEUDOCODE:
Code:
local combat[8];
local AREA[8] = {AREA_g1, AREA_g2, AREA_g3, AREA_g4, AREA_g5, AREA_g6, AREA_g7, AREA_g8}
for(int i =0; i<0; i++)
{
    combat[i]:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
    combat[i]:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
    combat[i]:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
    combat[i]:setParameter(COMBAT_PARAM_USECHARGES, true)
    combat[i]:setArea(createCombatArea(AREA[i])
}

And another question:

I tried to set formula values once for all combats because the damage is the same for every tick, but the
Code:
function onGetFormulaValues(player, skill, attack, factor)
    local min = 10
    local max = 20
    return -min, -max
end


combat1:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat2:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat3:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat4:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat5:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat6:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat7:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat8:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")

worked only for 1 combat (1 combat did damage and the rest didnt)
 
Solution
nevermind i didnt think
Code:
for i = 1, 8 do
    local combat = Combat()
    combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
    combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
    combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
    combat:setParameter(COMBAT_PARAM_USECHARGES, true)
    combat:setArea(createCombatArea(areas[i]))
    _G['onGetFormulaValues'..i] = function() return -10, -20 end
    combat:setCallback(CALLBACK_PARAM_SKILLVALUE, 'onGetFormulaValues'..i)
    combats[#combats+1] = combat
end
im not sure but i think you can get away with assigning i to the end
Code:
local areas = {AREA_g1, AREA_g2, AREA_g3, AREA_g4, AREA_g5, AREA_g6, AREA_g7, AREA_g8}

local combats = {}
for i = 1, 8 do
    local callback = math.random(999999999)
    local combat = Combat()
    combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
    combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
    combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
    combat:setParameter(COMBAT_PARAM_USECHARGES, true)
    combat:setArea(createCombatArea(areas[i]))
    _G['onGetFormulaValues'..callback] = function() return -10, -20 end
    combat:setCallback(CALLBACK_PARAM_SKILLVALUE, 'onGetFormulaValues'..callback)
    combats[#combats+1] = combat
end
 
Thanks for response!

What's the purpose of this line?
Code:
 local callback = math.random(999999999)
 
Thanks for response!

What's the purpose of this line?
Code:
 local callback = math.random(999999999)
I'm as interested as you are. :p
(my idiot view of the line..)
main directory, get formula values, and randomize them?.. make them called function().. and return real values.

I have no idea what it's for. :c

Xeraphus, Halp.
 
nevermind i didnt think
Code:
for i = 1, 8 do
    local combat = Combat()
    combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
    combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
    combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
    combat:setParameter(COMBAT_PARAM_USECHARGES, true)
    combat:setArea(createCombatArea(areas[i]))
    _G['onGetFormulaValues'..i] = function() return -10, -20 end
    combat:setCallback(CALLBACK_PARAM_SKILLVALUE, 'onGetFormulaValues'..i)
    combats[#combats+1] = combat
end
im not sure but i think you can get away with assigning i to the end
 
Solution
Back
Top