• 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!

TFS 1.X+ A little fix on weapon upgrade

ralke

(҂ ͠❛ ෴ ͡❛)ᕤ
Joined
Dec 17, 2011
Messages
1,470
Solutions
27
Reaction score
844
Location
Santiago - Chile
GitHub
ralke23
Twitch
ralke23
Hi, I like this script but the only problem is that upgrade wands too... How can I block wands or other item types from being updated? Im also getting thiserror sometimes when upgrading.. Im using tfs 1.3 revscriptsys

Script:

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 = 1, -- attack %
     defense = 1, -- defense %
     extraDefense = 1, -- extra defense %
     armor = 1, -- armor %
   }
}



-- // 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(cid, item, fromPosition, itemEx, toPosition)
   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_EXTRADEFENSE, upgrading.upValue(it:getExtraDefense(), nLevel, conf["upgrade"].extraDefense))
  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

Console error:
error script.png
 
Solution
An easy fix but not the best one is to just create a table with all the ids you want to block and return false if the item ID is in there.
The best way is to check the weapon type and if it is wand, return false.

I can't test right now this, but for the second one it should look something among these lines:
Lua:
if it:getWeaponType() == WEAPON_WAND then
    return false
end
This line of code should be after line 50. I am not sure if the check is done in this way, because I have never done this type of check and can't check it myself right now.


About the error, I am not able to read the image you linked. The quality is terrible.
An easy fix but not the best one is to just create a table with all the ids you want to block and return false if the item ID is in there.
The best way is to check the weapon type and if it is wand, return false.

I can't test right now this, but for the second one it should look something among these lines:
Lua:
if it:getWeaponType() == WEAPON_WAND then
    return false
end
This line of code should be after line 50. I am not sure if the check is done in this way, because I have never done this type of check and can't check it myself right now.


About the error, I am not able to read the image you linked. The quality is terrible.
 
Solution
About the error, I am not able to read the image you linked. The quality is terrible.
KEfpxAa.png
 
Amazing, its working ^^ thanks
Post automatically merged:

@zhendekeaikuma A last question, it is possible to raise magic level from spellbooks too? Excluding armors and other equipment from magic level upgrading
Post automatically merged:

@zhendekeaikuma Sorry I did test again and the error keeps happening when I try to upgrade spellbooks

error script 2.png
Post automatically merged:

Also getting this error when upgrading yalahari mask

yalahari mask.png
 
Last edited:
Bump...

This is how i have 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(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
 
Bump...

This is how i have 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(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

In your global.lua search for the line
Lua:
local attr = Item(item.uid):getAttribute(key)
Change it to
Lua:
local attr = Item(uid):getAttribute(key)

Also, if you want the message also appearing in case it tries to upgrade a wand, change your code in the upgrade action to
Lua:
  if it:getWeaponType() == WEAPON_WAND then
    return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You cannot upgrade this item.")
  end
 
I have a bug.

How to reproduce:

1 - try change on code:

Lua:
[1] = {successPercent = 85, downgradeLevel = 0},

for

Lua:
[1] = {successPercent = 0, downgradeLevel = 0},

2 - this will make it impossible to upgrade +0 items, right?

However, when if i try, his message appears.:

1584417853442.png

the expected result should just be that the item has not been upgraded.
 
I have a bug.

How to reproduce:

1 - try change on code:

Lua:
[1] = {successPercent = 85, downgradeLevel = 0},

for

Lua:
[1] = {successPercent = 0, downgradeLevel = 0},

2 - this will make it impossible to upgrade +0 items, right?

However, when if i try, his message appears.:

View attachment 43152

the expected result should just be that the item has not been upgraded.
The reason is this line:
Lua:
(conf["level"][level].downgradeLevel)
The fastest way to fix this besides rewriting the whole code (which would be what I would do, because this is a mess)
is to add this in your table in the beginning of your code:
Lua:
[0] = {downgradeLevel = 0},
This happens because it is trying to find what downgrade level it goes past 0, and it wasn't given in the table, because the code wasn't made to have 0% chance to level 1.

Your table should look something like this:
Lua:
local conf = {
  ["level"] = {
    -- [item_level] = {successPercent= CHANCE TO UPGRADE ITEM, downgradeLevel = ITEM GETS THIS LEVEL IF UPGRADE FAILS}
    [0] = {downgradeLevel = 0},
    [1] = {successPercent = 0, 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 = 1, -- attack %
    defense = 1, -- defense %
    extraDefense = 1, -- extra defense %
    armor = 1, -- armor %
  }
}
 
All errors from console has been solved, great work @zhendekeaikuma and thanks a lot! I set a best answer because main errors are all solved...

A last thing, as request., it is possible to do this? ^^
@zhendekeaikuma A last question, it is possible to raise magic level from spellbooks too? Excluding armors and other equipment from magic level upgrading

If is too much work to do in the same script, maybe a different one to do magic level attribute upgrader that only works for spellbooks? Will be very helpfull in order to balance paladin's and knight's weapons that has raised attack with mage equipment (that should have magic level upgrading in compensation)...

For example:

Lua:
   ["upgrade"] = { -- how many percent attributes are rised?
     attack = 1, --
     defense = 1, --
     extraDefense = 1, --
     armor = 1, --
     magic = 1,     --- new magic attribute

then on global.lua

Lua:
   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(),
     [ITEM_ATTRIBUTE_MAGIC] = i:getMagicLevel(),     ------  i dont know how its supposed to be written
     ["weight"] = i:getWeight(),
     ["attack"] = i:getAttack(),
     ["defense"] = i:getDefense(),
     ["extradefense"] = i:getExtraDefense(),
     ["armor"] = i:getArmor(),
     ["hitchance"] = i:getHitChance(),
     ["shootrange"] = i:getShootRange(),
     ["magiclevel"] = i:getMagicLevel()    ------- i dont know how its supposed to be written
   }

then edit script at

Lua:
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_MAGIC, upgrading.upValue(it:getMagicLevel(), nLevel, conf["upgrade"].magic))
---- this is only an idea of how it should be...

If you can help me with this I will really appreciate! I dont know how to raise magic only if the item at least has a +1 magic attribute, like it happen with every other item too...
 
Last edited:
All errors from console has been solved, great work @zhendekeaikuma and thanks a lot! I set a best answer because main errors are all solved...

A last thing, as request., it is possible to do this? ^^


If is too much work to do in the same script, maybe a different one to do magic level attribute upgrader that only works for spellbooks? Will be very helpfull in order to balance paladin's and knight's weapons that has raised attack with mage equipment (that should have magic level upgrading in compensation)...

For example:

Lua:
   ["upgrade"] = { -- how many percent attributes are rised?
     attack = 1, --
     defense = 1, --
     extraDefense = 1, --
     armor = 1, --
     magic = 1,     --- new magic attribute

then on global.lua

Lua:
   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(),
     [ITEM_ATTRIBUTE_MAGIC] = i:getMagicLevel(),     ------  i dont know how its supposed to be written
     ["weight"] = i:getWeight(),
     ["attack"] = i:getAttack(),
     ["defense"] = i:getDefense(),
     ["extradefense"] = i:getExtraDefense(),
     ["armor"] = i:getArmor(),
     ["hitchance"] = i:getHitChance(),
     ["shootrange"] = i:getShootRange(),
     ["magiclevel"] = i:getMagicLevel()    ------- i dont know how its supposed to be written
   }

then edit script at

Lua:
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_MAGIC, upgrading.upValue(it:getMagicLevel(), nLevel, conf["upgrade"].magic))
---- this is only an idea of how it should be...

If you can help me with this I will really appreciate! I dont know how to raise magic only if the item at least has a +1 magic attribute, like it happen with every other item too...
did u finished that? I want the same thing... e.e
 
did u finished that? I want the same thing... e.e

unfortunately nope, but i can tell you that the magic attribute needs to be to compiled because tfs 0.4 doesn't support i:getMagicLevel()
theres 2 ways to get it, first, change to tfs 1.3, and the second one, set new lua functions like is mentioned here or here

regards!
 
unfortunately nope, but i can tell you that the magic attribute needs to be to compiled because tfs 0.4 doesn't support i:getMagicLevel()
theres 2 ways to get it, first, change to tfs 1.3, and the second one, set new lua functions like is mentioned here or here

regards!
i'm using tfs 1.3... U can tell how i do that?
 
Back
Top