• 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] How to migrate scripts from TFS 0.x to TFS 1.x

Gesior.pl

Mega Noob&LOL 2012
Senator
Joined
Sep 18, 2007
Messages
2,966
Solutions
99
Reaction score
3,383
Location
Poland
GitHub
gesior
LEARN BY EXAMPLE!
In this thread I will post script that I migrated from old TFS to TFS 1.1+. They are not some ready-to-run system, so I can't post them in Resources, but I'm sure that many people can learn how to write 'new LUA scripts' from it.

1. onStatsChange(cid, attacker, type, combat, value) event in 1.x is splited into 2 events:

onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)

onManaChange(creature, attacker, manaChange, origin)

In old script you had to 'return false' to block damage, in new you need to set damage values to 0 - LUA variables are linked to C++ variables! WoW! :)
Linked variables in TFS 1.1+ are:
primaryDamage
primaryType
secondaryDamage
secondaryType
manaChange

If you modify value of any of them, it will change in TFS C++ calculations! Now you can not only block damage, but also reduce it by percent or by value.

Example, reduce mana damage [and healing, potions?] by 25%:
PHP:
function onManaChange(creature, attacker, manaChange, origin)
   -- check if attacker is Player, 'creature' is always Player, because we 'registerEvent' only for players
    if attacker:isPlayer() then
        -- change manaChange to 75% of start value
        manaChange = (manaChange * 75) / 100
    end
    return true
end

Example, some PvP arena (teleport to temple when HP goes under 0) and loot protection for low levels:
PHP:
--// OLD SCRIPT FOR TFS 0.3.6
local Arena = { frompos = {x=1173,y=1390,z=7}, topos = {x=1250,y=1422,z=7} }
local TemploArenas = {x=1429, y=1242, z=7, stackpos=1}

function onStatsChange(cid, attacker, type, combat, value)
    if isInRange(getCreaturePosition(cid), Arena.frompos, Arena.topos) then
        if type == 1 then
            if getCreatureHealth(cid) <= value then
                if isPlayer(cid) then
                    doSendMagicEffect(TemploArenas, 10)
                    doTeleportThing(cid, TemploArenas, true)
                    doCreatureAddHealth(cid, getCreatureMaxHealth(cid), true)
                    if isPlayer(attacker) then
                        doPlayerSetPzLocked(attacker, true)
                    end
                end
                return false
            end
        end
    end
    if isPlayer(cid) and type == 1 and getCreatureHealth(cid) <= value then
        if getPlayerLevel(cid) <= 100 then
            if (getCreatureSkullType(cid) ~= SKULL_RED) and (getCreatureSkullType(cid) ~= SKULL_BLACK) then
                doCreatureSetDropLoot(cid, false)
            end
        end
    end
    return true
end

--// NEW SCRIPT FOR TFS 1.1

local Arena = { frompos = {x=1173,y=1390,z=7}, topos = {x=1250,y=1422,z=7} }
local TemploArenas = Position(1429, 1242, 7) --// config as Position object

--// hit on mana can't kill player, only hit on HP, so we don't need onManaChange event
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local player = Player(creature)
    if player then --// not nil = it is a player, not monster/npc
        if isInRange(player:getPosition(), Arena.frompos, Arena.topos) then
            if primaryDamage < 0 then --// it is attack, not healing
                --// if primary is under 0, secondary is also under 0
                local totalDamage = -(primaryDamage + secondaryDamage) --// sum damage and change it to positive number
                if player:getHealth() <= totalDamage then
                    --// doSendMagicEffect(TemploArenas, 10)
                    TemploArenas:sendMagicEffect(10)
                    --// doTeleportThing(cid, TemploArenas, true)
                    player:teleportTo(TemploArenas, true)
                    --// doCreatureAddHealth(cid, getCreatureMaxHealth(cid), true)
                    player:addHealth(player:getMaxHealth() - player:getHealth())
                    local attackerPlayer = Player(attacker)
                    if attackerPlayer then
                        --//doPlayerSetPzLocked(attacker, true
                        --// WTF, there is no function to set PZ lock on TFS 1.1 (and 1.2)!
                        --// Bug reported: https://github.com/otland/forgottenserver/issues/1874
                    end
                    --// return false - old code to block dmg
                    primaryDamage = 0
                    secondaryDamage = 0
                end
            end
        end

        if primaryDamage < 0 then --// it is attack, not healing
            local totalDamage = -(primaryDamage + secondaryDamage) --// sum damage and change it to positive number
            if player:getHealth() <= totalDamage then
                if player:getLevel() <= 100 then
                    if player:getSkull() ~= SKULL_RED) and player:getSkull() ~= SKULL_BLACK then
                        --// doCreatureSetDropLoot(cid, false)
                        player:setDropLoot(false)
                    end
                end
            end
        end
    end

    return true
end
 
Back
Top