local EXP_PER_LEVEL = 1000
local MAX_LEVEL = 10
local ATTACK_PER_LEVEL = 2
local EXP_GAIN_MIN = 1
local EXP_GAIN_MAX = 5
local ELEMENTS = {"fire", "ice", "earth", "death"}
local function updateWeaponDescription(item, level, exp)
local percent = math.floor((exp / EXP_PER_LEVEL) * 100)
item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION,
string.format("Weapon Level: +%d\nEXP: %d/%d (%d%%)", level, exp, EXP_PER_LEVEL, percent))
end
local function applyWeaponBonuses(item, level)
item:setAttack(item:getAttack() + ATTACK_PER_LEVEL)
if level == 5 then
item:setAttribute(ITEM_ATTRIBUTE_NAME, item:getName() .. " [Power+5]")
elseif level == 10 then
local element = ELEMENTS[math.random(#ELEMENTS)]
item:setAttribute(ITEM_ATTRIBUTE_NAME, item:getName() .. " [" .. element .. "]")
local holder = item:getHolder()
if holder and holder:isPlayer() then
Game.broadcastMessage(string.format("Gracz %s ulepszył broń do poziomu +10!", holder:getName()), MESSAGE_STATUS_WARNING)
end
end
end
local weaponExpEvent = CreatureEvent("WeaponExpGain")
function weaponExpEvent.onStatsChange(creature, attacker, changetype, combat, value)
if not attacker or not attacker:isPlayer() then return true end
if changetype ~= STATSCHANGE_HEALTHLOSS then return true end
local weapon = attacker:getSlotItem(CONST_SLOT_RIGHT)
if not weapon or not weapon:isWeapon() then return true end
local level = weapon:getCustomAttribute("weaponLevel") or 0
local exp = weapon:getCustomAttribute("weaponExp") or 0
if level >= MAX_LEVEL then return true end
local gain = math.random(EXP_GAIN_MIN, EXP_GAIN_MAX)
exp = exp + gain
if exp >= EXP_PER_LEVEL then
exp = exp - EXP_PER_LEVEL
level = level + 1
weapon:setCustomAttribute("weaponLevel", level)
weapon:setCustomAttribute("weaponExp", exp)
updateWeaponDescription(weapon, level, exp)
applyWeaponBonuses(weapon, level)
else
weapon:setCustomAttribute("weaponExp", exp)
updateWeaponDescription(weapon, level, exp)
end
return true
end
weaponExpEvent:register()
local weaponClickAction = Action()
function weaponClickAction.onUse(player, item, fromPos, target, toPos, isHotkey)
if not item:isWeapon() then return false end
local level = item:getCustomAttribute("weaponLevel") or 0
local exp = item:getCustomAttribute("weaponExp") or 0
local percent = (exp / EXP_PER_LEVEL) * 100
player:sendTextMessage(MESSAGE_INFO_DESCR,
string.format("Twoja broń ma %d/%d EXP (%.1f%%) - Poziom: +%d", exp, EXP_PER_LEVEL, percent, level)
)
return true
end
weaponClickAction:id(2398, 2400, 2403, 2406, 7407, 2423, 2430, 7384, 7404)
weaponClickAction:register()