• 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 Very large script, how to reduce?

anderkrox

New Member
Joined
May 14, 2020
Messages
21
Reaction score
1
I created this rune script for my server, but there will be several others similar to this script.
I would like to know how to compress this script, that is, how to reduce its size to make it more practical.

How to shorten this script?

Lua:
--[[
Nome da runa: Esfera Flamejante
Descrição: Um feitiço que condensa a energia de fogo numa pequena esfera e a dispara contra um inimigo. Se a esfera atingir o inimigo, esta provoca uma enorme explosão.

Tier 1:
187% de ataque de feitiço como dano de feitiço.
[Efeito quando atingido]
-Queimado durante 5 segundos (máx. 3 acumulações, não removível)
-Quando reprimido, aumento do dano e de esmagamento +50%
]]

EVASION__LEVEL = 159951001
PRECISION__LEVEL = 159951002
ESFERA__FLAMEJANTE__COOLDOWN = 159951003
ESFERA__FLAMEJANTE__TIER = 159951004

function getEvasionLevel(creature)
    return creature.getStorageValue(EVASION__LEVEL)
end

function getPrecisionLevel(creature)
    return creature.getStorageValue(PRECISION__LEVEL)
end

local config = {
    failMsg = "miss",
    damageX = 38,
    manaCost = 285,
    cooldown = 12,
    storCooldown = ESFERA__FLAMEJANTE__COOLDOWN,
    tierRune = ESFERA__FLAMEJANTE__TIER,
}

local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_EXPLOSIONAREA)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_EXPLOSION)
combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat:setArea(createCombatArea(AREA_CROSS1X1))

function onGetFormulaValues(player, level, magicLevel)
    if player:getStorageValue(config.tierRune) <= 1 then
        local min = ((magicLevel*1.87)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 2 then
        local min = ((magicLevel*1.91)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 3 then
        local min = ((magicLevel*1.95)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 4 then
        local min = ((magicLevel*1.99)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 5 then
        local min = ((magicLevel*2.03)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 6 then
        local min = ((magicLevel*2.07)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 7 then
        local min = ((magicLevel*2.11)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 8 then
        local min = ((magicLevel*2.15)*config.damageX)
        local max = min
        return -min, -max
    elseif player:getStorageValue(config.tierRune) == 9 then
        local min = ((magicLevel*2.19)*config.damageX)
        local max = min
        return -min, -max
    else
        local min = ((magicLevel*2.23)*config.damageX)
        local max = min
        return -min, -max
    end
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onCastSpell(creature, variant, isHotkey)
    if player:getStorageValue(config.storCooldown) <= 0 then
        player:removeMana(config.manaCost)
        player:setStorageValue(config.storCooldown, 1)
        addEvent(setStorageValue, (config.cooldown*1000), player:getId(), config.storCooldown, 0)

        if creature:isPlayer() == true then
            if creature:getEvasionLevel() < player.getPrecisionLevel() then
                if math.random(1, 100) <= 99 then
                    return combat:execute(creature, variant)
                else
                    creature:say(config.failMsg, TALKTYPE_MONSTER_SAY)
                end
            else
                if math.random(1, 100) <= 24 then
                    return combat:execute(creature, variant)
                else
                    creature:say(config.failMsg, TALKTYPE_MONSTER_SAY)
                end
            end
        else
            return combat:execute(creature, variant)
        end
        
    else
        player:sendCancelMessage("You are exhausted.")
    end
end
 
I would start reducing the if-elses in onGetFormulaValues. You can use a lookup table to get its value.

Lua:
local formulaLookupTable = {
    1.87, 1.91, 1.95, 1.99, 2.03, 2.07, 2.11, 2.15, 2.19, 2.23
}


function onGetFormulaValues(player, level, magicLevel)
    local storageTierRune = player:getStorageValue(config.tierRune)

    if storageTierRune <= 1 then
        storageTierRune = 1
    elseif storageTierRune >= 10 then
        storageTierRune = 10
    end

    local formulaMultiplier = formulaLookupTable[storageTierRune]

    local min = ((magicLevel*formulaMultiplier)*config.damageX)
    local max = min

    return -min, -max
end


You can use more dynamic way using the #formulaLookupTable instead of hard-coded 10, like:

Code:
    if storageTierRune <= 1 then
        storageTierRune = 1
    elseif storageTierRune >= #formulaLookupTable then
        storageTierRune = #formulaLookupTable
    end

This way you can add more multipliers and just change the formulaLookupTable

I didn´t check the other part of the code, maybe there is something to work on there too.
PS: I didn´t test the code above. use it to get the idea behind it.
 
Back
Top