[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,814
    Likes Received:
    628
    Best Answers:
    1
    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:
    1. function onManaChange(creature, attacker, manaChange, origin)
    2.    -- check if attacker is Player, 'creature' is always Player, because we 'registerEvent' only for players
    3.     if attacker:isPlayer() then
    4.         -- change manaChange to 75% of start value
    5.         manaChange = (manaChange * 75) / 100
    6.     end
    7.     return true
    Example, some PvP arena (teleport to temple when HP goes under 0) and loot protection for low levels:
    PHP:
    1. --// OLD SCRIPT FOR TFS 0.3.6
    2. local Arena = { frompos = {x=1173,y=1390,z=7}, topos = {x=1250,y=1422,z=7} }
    3. local TemploArenas = {x=1429, y=1242, z=7, stackpos=1}
    4.  
    5. function onStatsChange(cid, attacker, type, combat, value)
    6.     if isInRange(getCreaturePosition(cid), Arena.frompos, Arena.topos) then
    7.         if type == 1 then
    8.             if getCreatureHealth(cid) <= value then
    9.                 if isPlayer(cid) then
    10.                     doSendMagicEffect(TemploArenas, 10)
    11.                     doTeleportThing(cid, TemploArenas, true)
    12.                     doCreatureAddHealth(cid, getCreatureMaxHealth(cid), true)
    13.                     if isPlayer(attacker) then
    14.                         doPlayerSetPzLocked(attacker, true)
    15.                     end
    16.                 end
    17.                 return false
    18.             end
    19.         end
    20.     end
    21.     if isPlayer(cid) and type == 1 and getCreatureHealth(cid) <= value then
    22.         if getPlayerLevel(cid) <= 100 then
    23.             if (getCreatureSkullType(cid) ~= SKULL_RED) and (getCreatureSkullType(cid) ~= SKULL_BLACK) then
    24.                 doCreatureSetDropLoot(cid, false)
    25.             end
    26.         end
    27.     end
    28.     return true
    29.  
    30. --// NEW SCRIPT FOR TFS 1.1
    31.  
    32. local Arena = { frompos = {x=1173,y=1390,z=7}, topos = {x=1250,y=1422,z=7} }
    33. local TemploArenas = Position(1429, 1242, 7) --// config as Position object
    34.  
    35. --// hit on mana can't kill player, only hit on HP, so we don't need onManaChange event
    36. function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    37.     local player = Player(creature)
    38.     if player then --// not nil = it is a player, not monster/npc
    39.         if isInRange(player:getPosition(), Arena.frompos, Arena.topos) then
    40.             if primaryDamage < 0 then --// it is attack, not healing
    41.                 --// if primary is under 0, secondary is also under 0
    42.                 local totalDamage = -(primaryDamage + secondaryDamage) --// sum damage and change it to positive number
    43.                 if player:getHealth() <= totalDamage then
    44.                     --// doSendMagicEffect(TemploArenas, 10)
    45.                     TemploArenas:sendMagicEffect(10)
    46.                     --// doTeleportThing(cid, TemploArenas, true)
    47.                     player:teleportTo(TemploArenas, true)
    48.                     --// doCreatureAddHealth(cid, getCreatureMaxHealth(cid), true)
    49.                     player:addHealth(player:getMaxHealth() - player:getHealth())
    50.                     local attackerPlayer = Player(attacker)
    51.                     if attackerPlayer then
    52.                         --//doPlayerSetPzLocked(attacker, true
    53.                         --// WTF, there is no function to set PZ lock on TFS 1.1 (and 1.2)!
    54.                         --// Bug reported: https://github.com/otland/forgottenserver/issues/1874
    55.                     end
    56.                     --// return false - old code to block dmg
    57.                     primaryDamage = 0
    58.                     secondaryDamage = 0
    59.                 end
    60.             end
    61.         end
    62.  
    63.         if primaryDamage < 0 then --// it is attack, not healing
    64.             local totalDamage = -(primaryDamage + secondaryDamage) --// sum damage and change it to positive number
    65.             if player:getHealth() <= totalDamage then
    66.                 if player:getLevel() <= 100 then
    67.                     if player:getSkull() ~= SKULL_RED) and player:getSkull() ~= SKULL_BLACK then
    68.                         --// doCreatureSetDropLoot(cid, false)
    69.                         player:setDropLoot(false)
    70.                     end
    71.                 end
    72.             end
    73.         end
    74.     end
    75.  
    76.     return true
    77.  
     
  2. HeberPcL

    HeberPcL [PowerOT.com.br]

    Joined:
    Aug 21, 2007
    Messages:
    1,275
    Likes Received:
    38
    Best Answers:
    0
    Realy helpful.

    Thanks!
     

Share This Page

Loading...