• 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 Upgrade system - TFS 1.2 Nostalrius

PuszekLDZ

https://tibia74.eu
Joined
Jan 2, 2020
Messages
428
Solutions
2
Reaction score
109
Location
Lodz
Twitch
puszekgamer
Hi
Im trying to add a upgrade system (attack, armor, defence)

Im working on a script from @zbizu

I got upgrade.lua:
LUA:
local conf = {
   ["level"] = {
   -- [item_level] = {successPercent= CHANCE TO UPGRADE ITEM, downgradeLevel = ITEM GETS THIS LEVEL IF UPGRADE FAILS}
     [1] = {successPercent = 85, downgradeLevel = 0},
     [2] = {successPercent = 80, downgradeLevel = 1},
     [3] = {successPercent = 75, downgradeLevel = 2},
     [4] = {successPercent = 70, downgradeLevel = 3},
     [5] = {successPercent = 65, downgradeLevel = 4},
     [6] = {successPercent = 60, downgradeLevel = 5},
     [7] = {successPercent = 55, downgradeLevel = 0},
     [8] = {successPercent = 50, downgradeLevel = 0},
     [9] = {successPercent = 45, downgradeLevel = 0}
   },

   ["upgrade"] = { -- how many percent attributes are rised?
     attack = 5, -- attack %
     defense = 5, -- defense %
     extraDefense = 10, -- extra defense %
     armor = 5, -- armor %
     hitChance = 5, -- hit chance %
   }
}



-- // do not touch // --
-- Upgrading system by Azi [Ersiu] --
-- Edited for TFS 1.1 by Zbizu --

local upgrading = {
  upValue = function (value, level, percent)
  if value < 0 then return 0 end
     if level == 0 then return value end
     local nVal = value
     for i = 1, level do
       nVal = nVal + (math.ceil((nVal/100*percent)))
     end
  return nVal > 0 and nVal or value
  end,

  getLevel = function (item)
  local name = Item(item):getName():split('+')
     if (#name == 1) then
       return 0
     end
 
     return math.abs(name[2])
  end,
}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    cid = player:getId()
    itemEx = target
   local it = ItemType(itemEx.itemid)
  if((it:getWeaponType() > 0 or getItemAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR) > 0) and not isItemStackable(itemEx.itemid))then
  local level = upgrading.getLevel(itemEx.uid)
  if(level < #conf["level"])then
  local nLevel = (conf["level"][(level+1)].successPercent >= math.random(1,100)) and (level+1) or conf["level"][level].downgradeLevel
  if(nLevel > level)then
  doSendMagicEffect(toPosition, CONST_ME_MAGIC_GREEN)
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Upgrade to level " .. nLevel .. " successful!")
  else
  doSendMagicEffect(toPosition, CONST_ME_BLOCKHIT)
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Upgrade failed. Your " .. it:getName() .. " is now on level " .. nLevel .. "")
  end
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_NAME, it:getName()..((nLevel>0) and "+"..nLevel or ""))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_ATTACK,  upgrading.upValue(it:getAttack(), nLevel, conf["upgrade"].attack))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_DEFENSE, upgrading.upValue(it:getDefense(), nLevel, conf["upgrade"].defense))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR, upgrading.upValue(it:getArmor(), nLevel, conf["upgrade"].armor))
  doRemoveItem(item.uid, 1)
  else
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your " .. it:getName() .. " is on max level alredy.")
  end
  else
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You cannot upgrade this item.")
  end
end

added do global.lua
LUA:
function getItemAttribute(uid, key)
   local i = ItemType(Item(uid):getId())
   local string_attributes = {
     [ITEM_ATTRIBUTE_NAME] = i:getName(),
     [ITEM_ATTRIBUTE_ARTICLE] = i:getArticle(),
     [ITEM_ATTRIBUTE_PLURALNAME] = i:getPluralName(),
     ["name"] = i:getName(),
     ["article"] = i:getArticle(),
     ["pluralname"] = i:getPluralName()
   }

   local numeric_attributes = {
     [ITEM_ATTRIBUTE_WEIGHT] = i:getWeight(),
     [ITEM_ATTRIBUTE_ATTACK] = i:getAttack(),
     [ITEM_ATTRIBUTE_DEFENSE] = i:getDefense(),
     [ITEM_ATTRIBUTE_EXTRADEFENSE] = i:getExtraDefense(),
     [ITEM_ATTRIBUTE_ARMOR] = i:getArmor(),
     [ITEM_ATTRIBUTE_HITCHANCE] = i:getHitChance(),
     [ITEM_ATTRIBUTE_SHOOTRANGE] = i:getShootRange(),
     ["weight"] = i:getWeight(),
     ["attack"] = i:getAttack(),
     ["defense"] = i:getDefense(),
     ["extradefense"] = i:getExtraDefense(),
     ["armor"] = i:getArmor(),
     ["hitchance"] = i:getHitChance(),
     ["shootrange"] = i:getShootRange()
   }
  
   local attr = Item(uid):getAttribute(key)
   if tonumber(attr) then
     if numeric_attributes[key] then
       return attr ~= 0 and attr or numeric_attributes[key]
     end
   else
     if string_attributes[key] then
       if attr == "" then
         return string_attributes[key]
       end
     end
   end
return attr
end

function doItemSetAttribute(uid, key, value)
   return Item(uid):setAttribute(key, value)
end

function doItemEraseAttribute(uid, key)
   return Item(uid):removeAttribute(key)
end

The effect is that i got "+1" on item name, but stats are the same (atk and def)... i dosnt add atk and def..

before adding
20:46 You see a sword (Atk:14 Def:12).
and after adding
20:46 You see a sword+1 (Atk:14 Def:12).

Is it possible to do that? Maybe its not?
 
ok, i got some working script, but id add atk and def but it dont show in weapon...
if I upgrade sword (atk 14 def 12) to +1
i got sword +1 (atk 14 def 12) but indeed stats are 15/13 ...

it wont overwrite atk and def values - is there something to change in distro that it could overwrite those values ?
 
ok, i got some working script, but id add atk and def but it dont show in weapon...
if I upgrade sword (atk 14 def 12) to +1
i got sword +1 (atk 14 def 12) but indeed stats are 15/13 ...

it wont overwrite atk and def values - is there something to change in distro that it could overwrite those values ?
/attr attack,amount will change the attack of weapon ur standing in front of maybe have look how that works :) the thing u would do is /attr name,item:getName() .. "+1" and then attr attack,15 and it would be what ur looking for i guess
 
/attr attack,amount will change the attack of weapon ur standing in front of maybe have look how that works :) the thing u would do is /attr name,item:getName() .. "+1" and then attr attack,15 and it would be what ur looking for i guess
thanks for that, but i figure it out for myself the way it will work for my server :)
just done 3 different stones for upgrade weapon, shield and armor (boots, amulets etc)
and each one got a list of possible to upgrade, so 1 stone dosnt upgrade other item, like weapon stone dont add atk to shield etc :)
 
thanks for that, but i figure it out for myself the way it will work for my server :)
just done 3 different stones for upgrade weapon, shield and armor (boots, amulets etc)
and each one got a list of possible to upgrade, so 1 stone dosnt upgrade other item, like weapon stone dont add atk to shield etc :)
im just trying to point you in the right direction so u dont have to use zbizu systems since they seem to be barely working or are hard to grasp sometimes its better to figure out root ways of doing things and developing your own systems:)
 
this script is poorly written and will be 10 years old in one month
there are better ways to handle item upgrades as the engine itself has more possibilities now
I suggest finding a more recent code
 
Back
Top