Lua Function [TFS 1.3] Item abilities via Lua

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,419
Reaction score
1,997
SQL needed to execute in db:
SQL:
ALTER TABLE `player_items` ADD `abilities` BLOB NOT NULL;
ALTER TABLE `player_depotitems` ADD `abilities` BLOB NOT NULL;
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:
ITEM_ABILITY_HEALTHGAIN
ITEM_ABILITY_HEALTHTICKS
ITEM_ABILITY_MANAGAIN
ITEM_ABILITY_MANATICKS
ITEM_ABILITY_CONDITIONSUPPRESSIONS
ITEM_ABILITY_MAXHITPOINTS
ITEM_ABILITY_MAXMANAPOINTS
ITEM_ABILITY_MAGICPOINTS
ITEM_ABILITY_MAXHITPOINTSPERCENT
ITEM_ABILITY_MAXMANAPOINTSPERCENT
ITEM_ABILITY_MAGICPOINTSPERCENT
ITEM_ABILITY_SKILLFIST
ITEM_ABILITY_SKILLCLUB
ITEM_ABILITY_SKILLSWORD
ITEM_ABILITY_SKILLAXE
ITEM_ABILITY_SKILLDISTANCE
ITEM_ABILITY_SKILLSHIELD
ITEM_ABILITY_SKILLFISHING
ITEM_ABILITY_CRITICALHITCHANCE
ITEM_ABILITY_CRITICALHITAMOUNT
ITEM_ABILITY_LIFELEECHCHANCE
ITEM_ABILITY_LIFELEECHAMOUNT
ITEM_ABILITY_MANALEECHCHANCE
ITEM_ABILITY_MANALEECHAMOUNT
ITEM_ABILITY_SPEED
ITEM_ABILITY_ABSORBPHYSICAL
ITEM_ABILITY_ABSORBENERGY
ITEM_ABILITY_ABSORBEARTH
ITEM_ABILITY_ABSORBFIRE
ITEM_ABILITY_ABSORBWATER
ITEM_ABILITY_ABSORBICE
ITEM_ABILITY_ABSORBHOLY
ITEM_ABILITY_ABSORBDEATH
ITEM_ABILITY_FIELDABSORBPHYSICAL
ITEM_ABILITY_FIELDABSORBENERGY
ITEM_ABILITY_FIELDABSORBEARTH
ITEM_ABILITY_FIELDABSORBFIRE
ITEM_ABILITY_FIELDABSORBWATER
ITEM_ABILITY_FIELDABSORBICE
ITEM_ABILITY_FIELDABSORBHOLY
ITEM_ABILITY_FIELDABSORBDEATH
ITEM_ABILITY_ELEMENTTYPE
ITEM_ABILITY_ELEMENTDAMAGE
ITEM_ABILITY_MANASHIELD
ITEM_ABILITY_INVISIBLE
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)
 

ScorpionOT

uber n00b
Joined
Mar 28, 2015
Messages
238
Reaction score
42
wooow vulcan thanxx see you relly can help ppll in this community!
 

Peonso

Godly Member
Joined
Jan 14, 2008
Messages
1,493
Reaction score
981
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 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.
 
OP
Stigma

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,419
Reaction score
1,997
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.
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
 

Togu

Active Member
Joined
Jun 22, 2018
Messages
244
Reaction score
97
Location
Brazil
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?
 
OP
Stigma

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,419
Reaction score
1,997
So, it's in this link, right? Vulcanx/forgottenserver

Can I trust in the green-lighted lines and just copy and paste it?
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

Active Member
Joined
Jun 22, 2018
Messages
244
Reaction score
97
Location
Brazil
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 :)
 

Togu

Active Member
Joined
Jun 22, 2018
Messages
244
Reaction score
97
Location
Brazil
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:
XML:
<movevent event="Equip" itemid="2643" slot="feet" function="onEquipItem" script="healthregen.lua" />
Lua:
function Item.setHealthRegen(self, amount, ticks)
   self:setAbility(ITEM_ABILITY_HEALTHGAIN, amount)
   self:setAbility(ITEM_ABILITY_HEALTHTICKS, ticks)
   self:setAbility(ITEM_ABILITY_REGENERATION, 1)
end

function Item.removeHealthRegen(self)
   self:removeAbility(ITEM_ABILITY_HEALTHGAIN)
   self:removeAbility(ITEM_ABILITY_HEALTHTICKS)
   self:removeAbility(ITEM_ABILITY_REGENERATION)
end

local item = player:getSlotItem(CONST_SLOT_RING)
item:setHealthRegen(50, 1000) -- regen 50 health every second

-- later
item:removeHealthRegen() -- regen will no longer work
 

Togu

Active Member
Joined
Jun 22, 2018
Messages
244
Reaction score
97
Location
Brazil
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.
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.
 
OP
Stigma

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,419
Reaction score
1,997
Most probably items stored in houses would lost theirs attributes
did you actually test or confirm? or are you just guessing

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.
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
XML:
<action itemid="8300" script="crit.lua" />
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not target:isItem() then
        return false
    end
    local position = (toPosition.x ~= 65535 and toPosition or player:getPostion())
    target:setAbility(ITEM_ABILITY_CRITICALCHANCE, math.random(1, 100))
    target:setAbility(ITEM_ABILITY_CRITICALDAMAGE, math.random(1, 100))
    position:sendMagicEffect(CONST_ME_MAGIC_RED)
    item:remove()
    return true
end
 
OP
Stigma

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,419
Reaction score
1,997
i can add attributes for defense in this script ?
[TFS 1.2/1.3] Free scripting service
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
Lua:
local meleeSkills = {ITEM_ABILITY_SKILLFIST, ITEM_ABILITY_SKILLCLUB, ITEM_ABILITY_SKILLSWORD, ITEM_ABILITY_SKILLAXE}
for _, abilityKey in ipairs(meleeSkills) do
    item:setAbility(meleeSkills, abilityKey == ITEM_ABILITY_SKILLAXE and 45 or 30)
end
 
OP
Stigma

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,419
Reaction score
1,997
100% guess
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
 
Top