• 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 single stackable item with jewel

freaked1

Active Member
Joined
Jan 30, 2008
Messages
486
Solutions
5
Reaction score
29
Location
Sweden
Hey, I'm unsure wether this belongs in Requests or Support.
I have the upgrade with jewel script for my TFS 1.1 server.
What I need help with is to make single spears upgradable. If I enchant a stack now all the spears become upgraded but I only want 1 to become enchanted.
I posted the original script and not my messed up attempts.

This is the part I'm struggling with.
Code:
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)

Whole script.
action/scripts/upgrading.lua
Code:
local conf = {
   ["level"] = {
   -- [item_level] = {successPercent= CHANCE TO UPGRADE ITEM, downgradeLevel = ITEM GETS THIS LEVEL IF UPGRADE FAILS}
     [1] = {successPercent = 95, downgradeLevel = 0},
     [2] = {successPercent = 90, downgradeLevel = 1},
     [3] = {successPercent = 85, downgradeLevel = 2},
     [4] = {successPercent = 80, downgradeLevel = 2},
     [5] = {successPercent = 75, downgradeLevel = 4},
     [6] = {successPercent = 70, downgradeLevel = 4},
     [7] = {successPercent = 65, downgradeLevel = 6},
     [8] = {successPercent = 60, downgradeLevel = 6},
     [9] = {successPercent = 55, downgradeLevel = 6}
   },

   ["upgrade"] = { -- how many percent attributes are increased?
     attack = 5, -- attack %
     defense = 4, -- defense %
     extraDefense = 6, -- extra defense %
     armor = 4, -- armor %
     hitChance = 4, -- 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(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))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_HITCHANCE, upgrading.upValue(it:getHitChance(), nLevel, conf["upgrade"].hitChance))
  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

actions.xml
Code:
 <action itemid="9970" script="upgrading.lua"/>
 
This says if the weapon type is greater than 0, WEAPON_NONE is equal to 0 which is equal to fists if this is not true the or comparison operator jumps over to the next condition and asks to get the item attribute of itemEx its ITEM_ATTRIBUTE_ARMOR if it is greater 0.
The requirement of the and comparison operator requires both conditions are met in order to be considered true whereas or only requires one condition be true.
The thing here is the 2 conditions (it:getWeaponType() > 0 or getItemAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR) > 0) are 1 condition of the and comparison operator, everything within parenthesis is executed 1st so the above will be evaluated 1st and then based on its outcome the and operator will compare the 2nd condition but only if the 1st condition is true, if it is false then it will immediately return false for the entire condition.
Code:
if((it:getWeaponType() > 0 or getItemAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR) > 0) and not isItemStackable(itemEx.itemid))then
To simplify this concept I'll use a simple example.
Code:
local a = 5
local b = 6
if (a > b or a < b) and a == (b - 1) then
The result of the above condition would look something like this.
Code:
local a = 5
local b = 6
if (5 > 6 or 5 < 6) and 5 == (6 - 1) then
or
Code:
local a = 5
local b = 6
if (false or true) and true then
-- the code will now execute
 
Back
Top