[LUA] How to migrate scripts from TFS 0.x to TFS 1.x

Discussion in 'Programming & Scripting' started by Gesior.pl, Aug 12, 2016.

  1. Gesior.pl

    Gesior.pl Mega Noob&LOL 2012

    Joined:
    Sep 18, 2007
    Messages:
    1,794
    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(creatureattackermanaChangeorigin)
       -- 
    check if attacker is Player'creature' is always Playerbecause we 'registerEvent' only for players
        
    if attacker:isPlayer() then
            
    -- change manaChange to 75of 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=1429y=1242z=7stackpos=1}

    function 
    onStatsChange(cidattackertypecombatvalue)
        if 
    isInRange(getCreaturePosition(cid), Arena.fromposArena.toposthen
            
    if type == 1 then
                
    if getCreatureHealth(cid) <= value then
                    
    if isPlayer(cidthen
                        doSendMagicEffect
    (TemploArenas10)
                        
    doTeleportThing(cidTemploArenastrue)
                        
    doCreatureAddHealth(cidgetCreatureMaxHealth(cid), true)
                        if 
    isPlayer(attackerthen
                            doPlayerSetPzLocked
    (attackertrue)
                        
    end
                    end
                    
    return false
                end
            end
        end
        
    if isPlayer(cid) and type == and getCreatureHealth(cid) <= value then
            
    if getPlayerLevel(cid) <= 100 then
                
    if (getCreatureSkullType(cid) ~= SKULL_RED) and (getCreatureSkullType(cid) ~= SKULL_BLACKthen
                    doCreatureSetDropLoot
    (cidfalse)
                
    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(142912427) --// config as Position object

    --// hit on mana can't kill player, only hit on HP, so we don't need onManaChange event
    function onHealthChange(creatureattackerprimaryDamageprimaryTypesecondaryDamagesecondaryTypeorigin)
        
    local player Player(creature)
        if 
    player then --// not nil = it is a player, not monster/npc
            
    if isInRange(player:getPosition(), Arena.fromposArena.toposthen
                
    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(TemploArenastrue)
                        --
    // 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
     
  2. HeberPcL

    HeberPcL [PowerOT.com.br] Premium User

    Joined:
    Aug 21, 2007
    Messages:
    1,271
    Realy helpful.

    Thanks!
     

Share This Page