Lua Function [TFS 1.3] Item abilities via Lua

Discussion in 'C++ Codes' started by Vulcan_, Aug 13, 2018.

Tags:
  1. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,095
    Likes Received:
    1,892
    Best Answers:
    325
    SQL needed to execute in db:
    Code (SQL):
    1. ALTER TABLE `player_items` ADD `abilities` BLOB NOT NULL;
    2. ALTER TABLE `player_depotitems` ADD `abilities` BLOB NOT NULL;
    3. ALTER TABLE `player_inboxitems` ADD `abilities` BLOB NOT NULL;
    C++ Code: Vulcanx/forgottenserver

    New Lua functions:
    • item:setAbility(key, value)
    • item:getAbility(key)
    • item:removeAbility(key)
    Enums (keys):
    Code (Text):
    1. ITEM_ABILITY_HEALTHGAIN
    2. ITEM_ABILITY_HEALTHTICKS
    3. ITEM_ABILITY_MANAGAIN
    4. ITEM_ABILITY_MANATICKS
    5. ITEM_ABILITY_CONDITIONSUPPRESSIONS
    6. ITEM_ABILITY_MAXHITPOINTS
    7. ITEM_ABILITY_MAXMANAPOINTS
    8. ITEM_ABILITY_MAGICPOINTS
    9. ITEM_ABILITY_MAXHITPOINTSPERCENT
    10. ITEM_ABILITY_MAXMANAPOINTSPERCENT
    11. ITEM_ABILITY_MAGICPOINTSPERCENT
    12. ITEM_ABILITY_SKILLFIST
    13. ITEM_ABILITY_SKILLCLUB
    14. ITEM_ABILITY_SKILLSWORD
    15. ITEM_ABILITY_SKILLAXE
    16. ITEM_ABILITY_SKILLDISTANCE
    17. ITEM_ABILITY_SKILLSHIELD
    18. ITEM_ABILITY_SKILLFISHING
    19. ITEM_ABILITY_CRITICALHITCHANCE
    20. ITEM_ABILITY_CRITICALHITAMOUNT
    21. ITEM_ABILITY_LIFELEECHCHANCE
    22. ITEM_ABILITY_LIFELEECHAMOUNT
    23. ITEM_ABILITY_MANALEECHCHANCE
    24. ITEM_ABILITY_MANALEECHAMOUNT
    25. ITEM_ABILITY_SPEED
    26. ITEM_ABILITY_ABSORBPHYSICAL
    27. ITEM_ABILITY_ABSORBENERGY
    28. ITEM_ABILITY_ABSORBEARTH
    29. ITEM_ABILITY_ABSORBFIRE
    30. ITEM_ABILITY_ABSORBWATER
    31. ITEM_ABILITY_ABSORBICE
    32. ITEM_ABILITY_ABSORBHOLY
    33. ITEM_ABILITY_ABSORBDEATH
    34. ITEM_ABILITY_FIELDABSORBPHYSICAL
    35. ITEM_ABILITY_FIELDABSORBENERGY
    36. ITEM_ABILITY_FIELDABSORBEARTH
    37. ITEM_ABILITY_FIELDABSORBFIRE
    38. ITEM_ABILITY_FIELDABSORBWATER
    39. ITEM_ABILITY_FIELDABSORBICE
    40. ITEM_ABILITY_FIELDABSORBHOLY
    41. ITEM_ABILITY_FIELDABSORBDEATH
    42. ITEM_ABILITY_ELEMENTTYPE
    43. ITEM_ABILITY_ELEMENTDAMAGE
    44. ITEM_ABILITY_MANASHIELD
    45. ITEM_ABILITY_INVISIBLE
    46. ITEM_ABILITY_REGENERATION
    Examples:
    Notes:
    • For regeneration to work (HEALTHGAIN, MANAGAIN) you need to also set ITEM_ABILITY_REGENERATION to 1.
    • ITEM_ABILITY_REGENERATION, ITEM_ABILITY_MANASHIELD, and ITEM_ABILITY_INVISIBLE should only be set to 1 to enable the ability.
    • If you want to "set an ability to 0" aka item:setAbility(key, 0) it won't work. Use item:removeAbility(key) instead.
    • Setting an ability while an item is equipped will not automatically update it and apply the stats. For it to update, you must de-equip and re-equip the item.
    • For now, this only works with TFS 1.3. If you use 1.2 and still want to use this code you can apply this commit to your sources for critical and leech skills
    • ITEM_ABILITY_ELEMENTTYPE and ITEM_ABILITY_ELEMENTDAMAGE must be paired together to work properly
    • Items that are not loaded through movements.xml will not register abilities
    • Weapons that are loaded through weapons.xml will not register abilities
    This is basically just item attributes but for abilities (abilities are only loaded through items.xml and before this you were not able to modify them through Lua and create unique items without using workarounds with movement scripts + conditions.
    I figured it was done enough for me to "release" it after a week (although I still have more planned for this, there are a lot of things I need to add, fix, and optimize)
    I didn't thoroughly test everything (especially item descriptions), so if you find a bug let me know on this thread and I will fix it.
    Be sure to keep up to date with this branch to get the latest bugfixes with this code. (further commits will have the prefix Update so you know what to look for)
     
    HalfAway, margoh, Digital and 21 others like this.
  2. pepsiman

    pepsiman ---

    Joined:
    Nov 17, 2017
    Messages:
    67
    Likes Received:
    22
    Best Answers:
    4
    awesome release! thanks for sharing
     
  3. ScorpionOT

    ScorpionOT uber n00b

    Joined:
    Mar 28, 2015
    Messages:
    217
    Likes Received:
    38
    Best Answers:
    6
    wooow vulcan thanxx see you relly can help ppll in this community!
     
  4. ckripz

    ckripz Member

    Joined:
    May 30, 2013
    Messages:
    54
    Likes Received:
    6
    Best Answers:
    0
    excelente
     
  5. Raggaer

    Raggaer Godly Member

    Joined:
    Jul 25, 2012
    Messages:
    1,535
    Likes Received:
    886
    Best Answers:
    8
    Finally BRs contribute to the community
     
    pepsiman, Peonso and eldera like this.
  6. Peonso

    Peonso 1337

    Joined:
    Jan 14, 2008
    Messages:
    1,399
    Likes Received:
    902
    Best Answers:
    20
    I hate you, it took me 7 month to gather the courage to dig in this task, and 5 days after I end my system with tons of workarounds that barely works you release this jewel. What I have and woul be cool for you to add is bonus % damage for elemental damage, like 32% fire damage item that raises all damage from you fires sources.
     
    Vulcan_ likes this.
  7. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,095
    Likes Received:
    1,892
    Best Answers:
    325
    lol barely took me less than a week
    the current goal is to have the core abilities that are already in tfs working 100% properly bug-free with this system before i add custom things to it
     
  8. Togu

    Togu Member

    Joined:
    Jun 22, 2018
    Messages:
    135
    Likes Received:
    19
    Best Answers:
    0
    I don't know if I understood the tutorial... Couldn't find the step-by-step source code replacement, is this on GitHub? How do I see that?
     
  9. Togu

    Togu Member

    Joined:
    Jun 22, 2018
    Messages:
    135
    Likes Received:
    19
    Best Answers:
    0
    So, it's in this link, right? Vulcanx/forgottenserver

    Can I trust in the green-lighted lines and just copy and paste it?
     
  10. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,095
    Likes Received:
    1,892
    Best Answers:
    325
    the green lines are what you're supposed to add, any red lines are what you're supposed to delete
    right click the view button and open in new tab, find the lines with the change and copy every change
    if you try to copy without viewing the actual file and copying you'll copy the + or - signs which will give you errors
     
    Togu likes this.
  11. Togu

    Togu Member

    Joined:
    Jun 22, 2018
    Messages:
    135
    Likes Received:
    19
    Best Answers:
    0
    Didn't know this GitHub feature, I can say I'm a noob in GitHub...

    But thanks, I'm gonna try it, this is exactly what I need on my server :)
     
  12. kito2

    kito2 https://mtibia.online

    Joined:
    Mar 9, 2009
    Messages:
    3,675
    Likes Received:
    194
    Best Answers:
    1
    Dude u rock!!
     
    Vulcan_ likes this.
  13. _M4G0_

    _M4G0_ Active Member

    Joined:
    Feb 6, 2016
    Messages:
    379
    Likes Received:
    62
    Best Answers:
    12
  14. Togu

    Togu Member

    Joined:
    Jun 22, 2018
    Messages:
    135
    Likes Received:
    19
    Best Answers:
    0
    Compiled without any error or warnings, but how can I use these examples? Do I use as a lib? Put in movements/scripts? What do I change in items.xml or movements.xml? Could you give a complete example, pleeeaseee?

    Didn't work:
    Code (XML):
    1. <movevent event="Equip" itemid="2643" slot="feet" function="onEquipItem" script="healthregen.lua" />
    Code (Lua):
    1. function Item.setHealthRegen(self, amount, ticks)
    2.    self:setAbility(ITEM_ABILITY_HEALTHGAIN, amount)
    3.    self:setAbility(ITEM_ABILITY_HEALTHTICKS, ticks)
    4.    self:setAbility(ITEM_ABILITY_REGENERATION, 1)
    5. end
    6.  
    7. function Item.removeHealthRegen(self)
    8.    self:removeAbility(ITEM_ABILITY_HEALTHGAIN)
    9.    self:removeAbility(ITEM_ABILITY_HEALTHTICKS)
    10.    self:removeAbility(ITEM_ABILITY_REGENERATION)
    11. end
    12.  
    13. local item = player:getSlotItem(CONST_SLOT_RING)
    14. item:setHealthRegen(50, 1000) -- regen 50 health every second
    15.  
    16. -- later
    17. item:removeHealthRegen() -- regen will no longer work
     
  15. Togu

    Togu Member

    Joined:
    Jun 22, 2018
    Messages:
    135
    Likes Received:
    19
    Best Answers:
    0
    Didn't understand that, please don't make us read all changes to understand, give us more information so we can test your incredible job!
    We can set abilities during the game? Isn't it loaded when the server opens? I was thinking in something like add critical chance permanently in a weapon or increase health regen in boots.
     
  16. QuaS

    QuaS Cykotitan Pwned =/

    Joined:
    Sep 11, 2008
    Messages:
    833
    Likes Received:
    27
    Best Answers:
    0
    Most probably items stored in houses would lost theirs attributes
     
  17. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,095
    Likes Received:
    1,892
    Best Answers:
    325
    did you actually test or confirm? or are you just guessing

    abilities are loaded primarily through items.xml, but if you already set an ability on an item through lua it will prioritize that instead of the loaded ability from items.xml
    here's a full example for adding critical chance permanently to a weapon using a flawless ice crystal as an upgrader
    Code (XML):
    1. <action itemid="8300" script="crit.lua" />
    Code (Lua):
    1. function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    2.     if not target:isItem() then
    3.         return false
    4.     end
    5.     local position = (toPosition.x ~= 65535 and toPosition or player:getPostion())
    6.     target:setAbility(ITEM_ABILITY_CRITICALCHANCE, math.random(1, 100))
    7.     target:setAbility(ITEM_ABILITY_CRITICALDAMAGE, math.random(1, 100))
    8.     position:sendMagicEffect(CONST_ME_MAGIC_RED)
    9.     item:remove()
    10.     return true
    11. end
     
    Togu likes this.
  18. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,095
    Likes Received:
    1,892
    Best Answers:
    325
    that script works differently and it's the reason why i made something like this in the first place, so people wouldn't have to use scripts like that as a workaround to create unique items
    in my example for that script i used +30 melee and +15 axe skill increase with onEquip with conditions
    with this system you can just execute this on an item and you won't need to use a movement script to reap its benefits
    Code (Lua):
    1. local meleeSkills = {ITEM_ABILITY_SKILLFIST, ITEM_ABILITY_SKILLCLUB, ITEM_ABILITY_SKILLSWORD, ITEM_ABILITY_SKILLAXE}
    2. for _, abilityKey in ipairs(meleeSkills) do
    3.     item:setAbility(meleeSkills, abilityKey == ITEM_ABILITY_SKILLAXE and 45 or 30)
    4. end
     
    _M4G0_ likes this.
  19. QuaS

    QuaS Cykotitan Pwned =/

    Joined:
    Sep 11, 2008
    Messages:
    833
    Likes Received:
    27
    Best Answers:
    0
    100% guess
     
    Vulcan_ likes this.
  20. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,095
    Likes Received:
    1,892
    Best Answers:
    325
    well good guess, tested it and you're right
    i'll work on a fix and add a commit to the branch and update this thread when it's fixed
     

Share This Page

Loading...