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

TFS 1.X+ How to optimize

ZeroSkyer

Member
Joined
May 17, 2019
Messages
62
Reaction score
9
Location
EEUU
I made these scripts, but they cause a lot of lag. Thanks in advance<3

Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker and attacker:isPlayer() then
        if attacker:getStorageValue(Storage.dromePotions.fireAmplification) > os.time() then
            if primaryType == COMBAT_FIREDAMAGE then
                primaryDamage = primaryDamage * 1.08
            end
            if secondaryType == COMBAT_FIREDAMAGE then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
        if attacker:getStorageValue(Storage.dromePotions.iceAmplification) > os.time() then
            if primaryType == COMBAT_ICEDAMAGE then
                primaryDamage = primaryDamage * 1.08
            end
            if secondaryType == COMBAT_ICEDAMAGE then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
        if attacker:getStorageValue(Storage.dromePotions.earthAmplification) > os.time() then
            if primaryType == COMBAT_EARTHDAMAGE then
                primaryDamage = primaryDamage * 1.08
            end
            if secondaryType == COMBAT_EARTHDAMAGE then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
        if attacker:getStorageValue(Storage.dromePotions.energyAmplification) > os.time() then
            if primaryType == COMBAT_ENERGYDAMAGE then
                primaryDamage = primaryDamage * 1.08
            end
            if secondaryType == COMBAT_ENERGYDAMAGE then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
        if attacker:getStorageValue(Storage.dromePotions.holyAmplification) > os.time() then
            if primaryType == COMBAT_HOLYDAMAGE then
                primaryDamage = primaryDamage * 1.08
            end
            if secondaryType == COMBAT_HOLYDAMAGE then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
        if attacker:getStorageValue(Storage.dromePotions.deathAmplification) > os.time() then
            if primaryType == COMBAT_DEATHDAMAGE then
                primaryDamage = primaryDamage * 1.08
            end
            if secondaryType == COMBAT_DEATHDAMAGE then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
        if attacker:getStorageValue(Storage.dromePotions.physicalAmplification) > os.time() then
            if primaryType == COMBAT_PHYSICALDAMAGE then
                primaryDamage = primaryDamage * 1.08
            end
            if secondaryType == COMBAT_PHYSICALDAMAGE then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if creature and creature:isPlayer() then
        if creature:getStorageValue(Storage.dromePotions.fireResilience) > os.time() then
            if primaryType == COMBAT_FIREDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_FIREDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.iceResilience) > os.time() then
            if primaryType == COMBAT_ICEDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_ICEDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.earthResilience) > os.time() then
            if primaryType == COMBAT_EARTHDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_EARTHDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.energyResilience) > os.time() then
            if primaryType == COMBAT_ENERGYDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_ENERGYDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.holyResilience) > os.time() then
            if primaryType == COMBAT_HOLYDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_HOLYDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.deathResilience) > os.time() then
            if primaryType == COMBAT_DEATHDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_DEATHDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.physicalResilience) > os.time() then
            if primaryType == COMBAT_PHYSICALDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_PHYSICALDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

function onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if creature and creature:isPlayer() then
        if creature:getStorageValue(Storage.dromePotions.fireResilience) > os.time() then
            if primaryType == COMBAT_FIREDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_FIREDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.iceResilience) > os.time() then
            if primaryType == COMBAT_ICEDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_ICEDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.earthResilience) > os.time() then
            if primaryType == COMBAT_EARTHDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_EARTHDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.energyResilience) > os.time() then
            if primaryType == COMBAT_ENERGYDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_ENERGYDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.holyResilience) > os.time() then
            if primaryType == COMBAT_HOLYDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_HOLYDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.deathResilience) > os.time() then
            if primaryType == COMBAT_DEATHDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_DEATHDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
        if creature:getStorageValue(Storage.dromePotions.physicalResilience) > os.time() then
            if primaryType == COMBAT_PHYSICALDAMAGE then
                primaryDamage = primaryDamage * 0.92
            end
            if secondaryType == COMBAT_PHYSICALDAMAGE then
                secondaryDamage = secondaryDamage * 0.92
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
Lua:
local amps = {
    {Storage.dromePotions.fireAmplification, COMBAT_FIREDAMAGE}
    {Storage.dromePotions.iceAmplification, COMBAT_ICEDAMAGE}
    {Storage.dromePotions.earthAmplification, COMBAT_EARTHDAMAGE}
    {Storage.dromePotions.energyAmplification, COMBAT_ENERGYDAMAGE}
    {Storage.dromePotions.holyAmplification, COMBAT_HOLYDAMAGE}
    {Storage.dromePotions.deathAmplification, COMBAT_DEATHDAMAGE}
    {Storage.dromePotions.physicalAmplification, COMBAT_PHYSICALDAMAGE}
}
local _PlayerGetStorageValue = Player.getStorageValue
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker and attacker:isPlayer() then
        local now = os.time()
        for k, v in pairs(amps) do
            local ampStorage, combatType = v[1], v[2]
            if _PlayerGetStorageValue(attacker, ampStorage) > now then
                if primaryType == combatType then
                    primaryDamage = primaryDamage * 1.08
                end
                if secondaryType == combatType then
                    secondaryDamage = secondaryDamage * 1.08
                end
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
this is all i can really think of if you REALLY need to squeeze performance, but i'm extremely out of practice with lua (~1.5 years), let alone TFS
1. create a variable for the current time so os.time() isn't called, and the time access from the os table as well
2. same thing for Player.getStorageValue to reduce time that lua takes to pull the value (getStorageValue) from the Player table
(3?). you could probably just break/return early when you first buff the damage, 99.9999% of damage in every server is 1 type of damage only, unless your server is just wacky and you have multiple types of damage written in your spells and/or weapons etc.. if you return or break early you reduce the average time the function takes to execute since you no longer have to check for every single type of damage, it'll just simply find the first damage that should be buffed then reduce it. if you do it this way, i'd recommend reversing the if statements so that you check the damage type before calling _PlayerGetStorageValue, since then on average each iteration won't contain a function call which should squeeze extra nanoseconds of time for you :)
the code i provided should work for your other scripts as well, just copy the layout/storages/etc & apply the little performance hacks i explained
hope it helps a little, or someone else can be of more help. i'm too unsure of more performance hacks or a better way to logically write this (other than rewriting your little system here into a source edit, which i'd prefer anyways for a system like this)
it's also untested as i haven't had a test server in forever, hope it works lmao
 
Here's a slightly different version, that checks the type of damage directly, instead of looping through all the damage types.

Lua:
local damageTypeStorages = {
    [1] = {storage = Storage.dromePotions.physicalAmplification},
    [2] = {storage = Storage.dromePotions.energyAmplification},
    [4] = {storage = Storage.dromePotions.earthAmplification},
    [8] = {storage = Storage.dromePotions.fireAmplification},    
    [512] = {storage = Storage.dromePotions.iceAmplification},
    [1024] = {storage = Storage.dromePotions.holyAmplification},
    [2048] = {storage = Storage.dromePotions.deathAmplification}
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if creature and creature:isPlayer() then
        local currentTime = os.time()
        local damageIndex = damageTypeStorages[primaryType]
        if damageIndex then
            if creature:getStorageValue(damageIndex.storage) > currentTime then
                primaryDamage = primaryDamage * 1.08
            end
        end
        damageIndex = damageTypeStorages[secondaryType]
        if damageIndex then
            if creature:getStorageValue(damageIndex.storage) > currentTime then
                secondaryDamage = secondaryDamage * 1.08
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
Here's a slightly different version, that checks the type of damage directly, instead of looping through all the damage types.
Loop or not, most important optimization to that script is to first check combat type [1 INT comparision], then storage value [much more operations].
 
Back
Top