tibia-client/modules/game_skills/skills.lua

274 lines
8.1 KiB
Lua

skillsWindow = nil
skillsButton = nil
function init()
connect(LocalPlayer, {
onExperienceChange = onExperienceChange,
onLevelChange = onLevelChange,
onHealthChange = onHealthChange,
onManaChange = onManaChange,
onSoulChange = onSoulChange,
onFreeCapacityChange = onFreeCapacityChange,
onStaminaChange = onStaminaChange,
onOfflineTrainingChange = onOfflineTrainingChange,
onRegenerationChange = onRegenerationChange,
onSpeedChange = onSpeedChange,
onBaseSpeedChange = onBaseSpeedChange,
onMagicLevelChange = onMagicLevelChange,
onBaseMagicLevelChange = onBaseMagicLevelChange,
onSkillChange = onSkillChange,
onBaseSkillChange = onBaseSkillChange
})
connect(g_game, {
onGameStart = update,
onGameEnd = update
})
skillsWindow = g_ui.loadUI('skills.otui', modules.game_interface.getRightPanel())
skillsButton = TopMenu.addRightGameToggleButton('skillsButton', tr('Skills') .. ' (Ctrl+S)', 'skills.png', toggle)
skillsButton:setOn(true)
g_keyboard.bindKeyDown('Ctrl+S', toggle)
refresh()
end
function terminate()
disconnect(LocalPlayer, {
onExperienceChange = onExperienceChange,
onLevelChange = onLevelChange,
onHealthChange = onHealthChange,
onManaChange = onManaChange,
onSoulChange = onSoulChange,
onFreeCapacityChange = onFreeCapacityChange,
onStaminaChange = onStaminaChange,
onOfflineTrainingChange = onOfflineTrainingChange,
onRegenerationChange = onRegenerationChange,
onSpeedChange = onSpeedChange,
onBaseSpeedChange = onBaseSpeedChange,
onMagicLevelChange = onMagicLevelChange,
onBaseMagicLevelChange = onBaseMagicLevelChange,
onSkillChange = onSkillChange,
onBaseSkillChange = onBaseSkillChange
})
disconnect(g_game, {
onGameStart = update,
onGameEnd = update
})
g_keyboard.unbindKeyDown('Ctrl+S')
skillsButton:destroy()
skillsWindow:destroy()
end
function setSkillBase(id, value, baseValue)
if baseValue < 1 or value < 1 then
return
end
local skill = skillsWindow:recursiveGetChildById(id)
local widget = skill:getChildById('value')
if value > baseValue then
widget:setColor('#008b00')
skill:setTooltip(baseValue .. ' + ' .. (value - baseValue))
elseif value < baseValue then
widget:setColor('#008b00')
skill:setTooltip(baseValue .. ' - ' .. (value - baseValue))
else
widget:setColor('#bbbbbb')
skill:removeTooltip()
end
end
function setSkillValue(id, value)
local skill = skillsWindow:recursiveGetChildById(id)
local widget = skill:getChildById('value')
widget:setText(value)
end
function setSkillColor(id, value)
local skill = skillsWindow:recursiveGetChildById(id)
local widget = skill:getChildById('value')
widget:setColor(value)
end
function setSkillTooltip(id, value)
local skill = skillsWindow:recursiveGetChildById(id)
local widget = skill:getChildById('value')
widget:setTooltip(value)
end
function setSkillPercent(id, percent, tooltip)
local skill = skillsWindow:recursiveGetChildById(id)
local widget = skill:getChildById('percent')
widget:setPercent(percent)
if tooltip then
widget:setTooltip(tooltip)
end
end
function update()
local offlineTraining = skillsWindow:recursiveGetChildById('offlineTraining')
if not g_game.getFeature(GameOfflineTrainingTime) then
offlineTraining:hide()
else
offlineTraining:show()
end
local regenerationTime = skillsWindow:recursiveGetChildById('regenerationTime')
if not g_game.getFeature(GamePlayerRegenerationTime) then
regenerationTime:hide()
else
regenerationTime:show()
end
end
function refresh()
local player = g_game.getLocalPlayer()
if not player then return end
onExperienceChange(player, player:getExperience())
onLevelChange(player, player:getLevel(), player:getLevelPercent())
onHealthChange(player, player:getHealth(), player:getMaxHealth())
onManaChange(player, player:getMana(), player:getMaxMana())
onSoulChange(player, player:getSoul())
onFreeCapacityChange(player, player:getFreeCapacity())
onStaminaChange(player, player:getStamina())
onMagicLevelChange(player, player:getMagicLevel(), player:getMagicLevelPercent())
onOfflineTrainingChange(player, player:getOfflineTrainingTime())
onRegenerationChange(player, player:getRegenerationTime())
onSpeedChange(player, player:getSpeed())
onBaseSpeedChange(player, player:getBaseSpeed())
onBaseMagicLevelChange(player, player:getBaseMagicLevel())
for i=0,6 do
onSkillChange(player, i, player:getSkillLevel(i), player:getSkillLevelPercent(i))
onBaseSkillChange(player, i, player:getSkillBaseLevel(i))
end
end
function toggle()
if skillsButton:isOn() then
skillsWindow:close()
skillsButton:setOn(false)
else
skillsWindow:open()
skillsButton:setOn(true)
end
end
function onMiniWindowClose()
skillsButton:setOn(false)
end
function onSkillButtonClick(button)
local percentBar = button:getChildById('percent')
if percentBar then
percentBar:setVisible(not percentBar:isVisible())
if percentBar:isVisible() then
button:setHeight(21)
else
button:setHeight(21 - 6)
end
end
end
function onExperienceChange(localPlayer, value)
setSkillValue('experience', tr(value))
end
function onLevelChange(localPlayer, value, percent)
setSkillValue('level', tr(value))
setSkillPercent('level', percent, tr('You have %s percent to go', 100 - percent))
end
function onHealthChange(localPlayer, health, maxHealth)
setSkillValue('health', tr(health))
end
function onManaChange(localPlayer, mana, maxMana)
setSkillValue('mana', tr(mana))
end
function onSoulChange(localPlayer, soul)
setSkillValue('soul', soul)
end
function onFreeCapacityChange(localPlayer, freeCapacity)
setSkillValue('capacity', freeCapacity)
end
function onStaminaChange(localPlayer, stamina)
local hours = math.floor(stamina / 60)
local minutes = stamina % 60
if minutes < 10 then
minutes = '0' .. minutes
end
local percent = 100 * stamina / (42 * 60) -- max is 42 hours
setSkillValue('stamina', hours .. ":" .. minutes)
setSkillPercent('stamina', percent, tr('You have %s percent', percent))
end
function onOfflineTrainingChange(localPlayer, offlineTrainingTime)
local hours = math.floor(offlineTrainingTime / 60)
local minutes = offlineTrainingTime % 60
if minutes < 10 then
minutes = '0' .. minutes
end
local percent = 100 * offlineTrainingTime / (12 * 60) -- max is 12 hours
setSkillValue('offlineTraining', hours .. ":" .. minutes)
setSkillPercent('offlineTraining', percent, tr('You have %s percent', percent))
end
function onRegenerationChange(localPlayer, regenerationTime)
local hours = math.floor(regenerationTime / 60)
local minutes = regenerationTime % 60
if minutes < 10 then
minutes = '0' .. minutes
end
setSkillValue('regenerationTime', hours .. ":" .. minutes)
end
function onSpeedChange(localPlayer, speed)
setSkillValue('speed', speed)
setSkillBase('speed', speed, localPlayer:getBaseSpeed())
end
function onBaseSpeedChange(localPlayer, baseSpeed)
local speed = localPlayer:getSpeed()
onSpeedChange(localPlayer, baseSpeed)
setSkillBase('speed', speed, baseSpeed)
end
function onMagicLevelChange(localPlayer, magiclevel, percent)
setSkillValue('magiclevel', magiclevel)
setSkillPercent('magiclevel', percent, tr('You have %s percent to go', 100 - percent))
setSkillBase('magiclevel', magiclevel, localPlayer:getBaseMagicLevel())
end
function onBaseMagicLevelChange(localPlayer, baseMagicLevel)
local magiclevel = localPlayer:getMagicLevel()
onMagicLevelChange(localPlayer, magiclevel, localPlayer:getMagicLevelPercent())
setSkillBase('magiclevel', magiclevel, baseMagicLevel)
end
function onSkillChange(localPlayer, id, level, percent)
setSkillValue('skillId' .. id, level)
setSkillPercent('skillId' .. id, percent, tr('You have %s percent to go', 100 - percent))
setSkillBase('skillId'..id, level, localPlayer:getSkillBaseLevel(id))
end
function onBaseSkillChange(localPlayer, id, baseLevel)
local level = localPlayer:getSkillLevel(id)
onSkillChange(localPlayer, id, level, localPlayer:getSkillLevelPercent(id))
setSkillBase('skillId'..id, level, baseLevel)
end