2012-07-24 07:30:08 +02:00
|
|
|
battleWindow = nil
|
|
|
|
battleButton = nil
|
|
|
|
battlePanel = nil
|
2013-07-02 22:43:52 +02:00
|
|
|
filterPanel = nil
|
|
|
|
toggleFilterButton = nil
|
2012-07-24 07:30:08 +02:00
|
|
|
lastBattleButtonSwitched = nil
|
|
|
|
battleButtonsByCreaturesList = {}
|
2013-07-02 22:43:52 +02:00
|
|
|
creatureAgeList = {}
|
2012-02-09 06:27:29 +01:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
mouseWidget = nil
|
2012-02-09 06:27:29 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
sortTypeBox = nil
|
|
|
|
sortOrderBox = nil
|
2012-07-24 07:30:08 +02:00
|
|
|
hidePlayersButton = nil
|
|
|
|
hideNPCsButton = nil
|
|
|
|
hideMonstersButton = nil
|
|
|
|
hideSkullsButton = nil
|
|
|
|
hidePartyButton = nil
|
2012-02-09 06:27:29 +01:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function init()
|
2013-01-18 23:39:11 +01:00
|
|
|
g_ui.importStyle('battlebutton')
|
|
|
|
battleButton = modules.client_topmenu.addRightGameToggleButton('battleButton', tr('Battle') .. ' (Ctrl+B)', '/images/topbuttons/battle', toggle)
|
2012-03-28 16:10:21 +02:00
|
|
|
battleButton:setOn(true)
|
2013-01-18 23:39:11 +01:00
|
|
|
battleWindow = g_ui.loadUI('battle', modules.game_interface.getRightPanel())
|
2012-07-24 07:30:08 +02:00
|
|
|
g_keyboard.bindKeyDown('Ctrl+B', toggle)
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-08-18 07:04:01 +02:00
|
|
|
-- this disables scrollbar auto hiding
|
|
|
|
local scrollbar = battleWindow:getChildById('miniwindowScrollBar')
|
|
|
|
scrollbar:mergeStyle({ ['$!on'] = { }})
|
|
|
|
|
2012-03-28 16:10:21 +02:00
|
|
|
battlePanel = battleWindow:recursiveGetChildById('battlePanel')
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
filterPanel = battleWindow:recursiveGetChildById('filterPanel')
|
|
|
|
toggleFilterButton = battleWindow:recursiveGetChildById('toggleFilterButton')
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
if isHidingFilters() then
|
|
|
|
hideFilterPanel()
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
sortTypeBox = battleWindow:recursiveGetChildById('sortTypeBox')
|
|
|
|
sortOrderBox = battleWindow:recursiveGetChildById('sortOrderBox')
|
2012-03-28 16:10:21 +02:00
|
|
|
hidePlayersButton = battleWindow:recursiveGetChildById('hidePlayers')
|
|
|
|
hideNPCsButton = battleWindow:recursiveGetChildById('hideNPCs')
|
|
|
|
hideMonstersButton = battleWindow:recursiveGetChildById('hideMonsters')
|
|
|
|
hideSkullsButton = battleWindow:recursiveGetChildById('hideSkulls')
|
|
|
|
hidePartyButton = battleWindow:recursiveGetChildById('hideParty')
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
mouseWidget = g_ui.createWidget('UIButton')
|
2012-02-09 06:27:29 +01:00
|
|
|
mouseWidget:setVisible(false)
|
2012-02-20 03:27:08 +01:00
|
|
|
mouseWidget:setFocusable(false)
|
2013-01-06 16:04:49 +01:00
|
|
|
mouseWidget.cancelNextRelease = false
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-08-24 10:18:15 +02:00
|
|
|
battleWindow:setContentMinimumHeight(80)
|
2012-08-21 01:56:16 +02:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
sortTypeBox:addOption('Name', 'name')
|
|
|
|
sortTypeBox:addOption('Distance', 'distance')
|
|
|
|
sortTypeBox:addOption('Age', 'age')
|
|
|
|
sortTypeBox:addOption('Health', 'health')
|
|
|
|
sortTypeBox:setCurrentOptionByData(getSortType())
|
|
|
|
sortTypeBox.onOptionChange = onChangeSortType
|
|
|
|
|
|
|
|
sortOrderBox:addOption('Asc.', 'asc')
|
|
|
|
sortOrderBox:addOption('Desc.', 'desc')
|
|
|
|
sortOrderBox:setCurrentOptionByData(getSortOrder())
|
|
|
|
sortOrderBox.onOptionChange = onChangeSortOrder
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-08-25 18:05:33 +02:00
|
|
|
connect(Creature, {
|
|
|
|
onSkullChange = updateCreatureSkull,
|
|
|
|
onEmblemChange = updateCreatureEmblem,
|
2012-10-05 21:13:50 +02:00
|
|
|
onOutfitChange = onCreatureOutfitChange,
|
2012-08-25 18:05:33 +02:00
|
|
|
onHealthPercentChange = onCreatureHealthPercentChange,
|
|
|
|
onPositionChange = onCreaturePositionChange,
|
|
|
|
onAppear = onCreatureAppear,
|
|
|
|
onDisappear = onCreatureDisappear
|
|
|
|
})
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
connect(LocalPlayer, {
|
|
|
|
onPositionChange = onCreaturePositionChange
|
|
|
|
})
|
2012-08-25 18:05:33 +02:00
|
|
|
|
|
|
|
connect(g_game, {
|
|
|
|
onAttackingCreatureChange = onAttack,
|
|
|
|
onFollowingCreatureChange = onFollow,
|
|
|
|
onGameEnd = removeAllCreatures
|
|
|
|
})
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-08-08 21:17:16 +02:00
|
|
|
checkCreatures()
|
2012-08-21 23:40:47 +02:00
|
|
|
battleWindow:setup()
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function terminate()
|
2012-06-26 00:13:30 +02:00
|
|
|
g_keyboard.unbindKeyDown('Ctrl+B')
|
2012-02-09 06:27:29 +01:00
|
|
|
battleButtonsByCreaturesList = {}
|
|
|
|
battleButton:destroy()
|
|
|
|
battleWindow:destroy()
|
2012-04-27 06:54:14 +02:00
|
|
|
mouseWidget:destroy()
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2014-01-18 15:09:26 +01:00
|
|
|
disconnect(Creature, {
|
2012-08-25 18:05:33 +02:00
|
|
|
onSkullChange = updateCreatureSkull,
|
|
|
|
onEmblemChange = updateCreatureEmblem,
|
2012-10-05 21:13:50 +02:00
|
|
|
onOutfitChange = onCreatureOutfitChange,
|
2012-08-25 18:05:33 +02:00
|
|
|
onHealthPercentChange = onCreatureHealthPercentChange,
|
|
|
|
onPositionChange = onCreaturePositionChange,
|
|
|
|
onAppear = onCreatureAppear,
|
|
|
|
onDisappear = onCreatureDisappear
|
|
|
|
})
|
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
disconnect(LocalPlayer, {
|
|
|
|
onPositionChange = onCreaturePositionChange
|
|
|
|
})
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-08-25 18:05:33 +02:00
|
|
|
disconnect(g_game, {
|
|
|
|
onAttackingCreatureChange = onAttack,
|
|
|
|
onFollowingCreatureChange = onFollow,
|
|
|
|
onGameEnd = removeAllCreatures
|
|
|
|
})
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function toggle()
|
2012-06-21 21:31:22 +02:00
|
|
|
if battleButton:isOn() then
|
|
|
|
battleWindow:close()
|
|
|
|
battleButton:setOn(false)
|
|
|
|
else
|
|
|
|
battleWindow:open()
|
|
|
|
battleButton:setOn(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onMiniWindowClose()
|
2012-06-21 21:31:22 +02:00
|
|
|
battleButton:setOn(false)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
function getSortType()
|
|
|
|
local settings = g_settings.getNode('BattleList')
|
|
|
|
if not settings then
|
|
|
|
return 'name'
|
|
|
|
end
|
|
|
|
return settings['sortType']
|
|
|
|
end
|
|
|
|
|
|
|
|
function setSortType(state)
|
|
|
|
settings = {}
|
|
|
|
settings['sortType'] = state
|
|
|
|
g_settings.mergeNode('BattleList', settings)
|
|
|
|
|
|
|
|
checkCreatures()
|
|
|
|
end
|
|
|
|
|
|
|
|
function getSortOrder()
|
|
|
|
local settings = g_settings.getNode('BattleList')
|
|
|
|
if not settings then
|
|
|
|
return 'asc'
|
|
|
|
end
|
|
|
|
return settings['sortOrder']
|
|
|
|
end
|
|
|
|
|
|
|
|
function setSortOrder(state)
|
|
|
|
settings = {}
|
|
|
|
settings['sortOrder'] = state
|
|
|
|
g_settings.mergeNode('BattleList', settings)
|
|
|
|
|
|
|
|
checkCreatures()
|
|
|
|
end
|
|
|
|
|
|
|
|
function isSortAsc()
|
|
|
|
return getSortOrder() == 'asc'
|
|
|
|
end
|
|
|
|
|
|
|
|
function isSortDesc()
|
|
|
|
return getSortOrder() == 'desc'
|
|
|
|
end
|
|
|
|
|
|
|
|
function isHidingFilters()
|
|
|
|
local settings = g_settings.getNode('BattleList')
|
|
|
|
if not settings then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return settings['hidingFilters']
|
|
|
|
end
|
|
|
|
|
|
|
|
function setHidingFilters(state)
|
|
|
|
settings = {}
|
|
|
|
settings['hidingFilters'] = state
|
|
|
|
g_settings.mergeNode('BattleList', settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
function hideFilterPanel()
|
|
|
|
filterPanel.originalHeight = filterPanel:getHeight()
|
|
|
|
filterPanel:setHeight(0)
|
|
|
|
toggleFilterButton:getParent():setMarginTop(0)
|
|
|
|
toggleFilterButton:setImageClip(torect("0 0 21 12"))
|
|
|
|
setHidingFilters(true)
|
|
|
|
filterPanel:setVisible(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
function showFilterPanel()
|
|
|
|
toggleFilterButton:getParent():setMarginTop(5)
|
|
|
|
filterPanel:setHeight(filterPanel.originalHeight)
|
|
|
|
toggleFilterButton:setImageClip(torect("21 0 21 12"))
|
|
|
|
setHidingFilters(false)
|
|
|
|
filterPanel:setVisible(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
function toggleFilterPanel()
|
|
|
|
if filterPanel:isVisible() then
|
|
|
|
hideFilterPanel()
|
|
|
|
else
|
|
|
|
showFilterPanel()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function onChangeSortType(comboBox, option)
|
|
|
|
setSortType(option:lower())
|
|
|
|
end
|
|
|
|
|
|
|
|
function onChangeSortOrder(comboBox, option)
|
2014-05-29 03:16:47 +02:00
|
|
|
-- Replace dot in option name
|
|
|
|
setSortOrder(option:lower():gsub('[.]', ''))
|
2013-07-02 22:43:52 +02:00
|
|
|
end
|
|
|
|
|
2012-08-03 07:05:09 +02:00
|
|
|
function checkCreatures()
|
2013-01-21 22:36:53 +01:00
|
|
|
removeAllCreatures()
|
|
|
|
|
2014-05-29 03:16:47 +02:00
|
|
|
if not g_game.isOnline() then
|
|
|
|
return
|
2013-01-21 22:36:53 +01:00
|
|
|
end
|
|
|
|
|
2014-05-29 03:16:47 +02:00
|
|
|
local player = g_game.getLocalPlayer()
|
|
|
|
local spectators = g_map.getSpectators(player:getPosition(), false)
|
|
|
|
for _, creature in ipairs(spectators) do
|
|
|
|
if doCreatureFitFilters(creature) then
|
|
|
|
addCreature(creature)
|
|
|
|
end
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function doCreatureFitFilters(creature)
|
2014-05-29 03:16:47 +02:00
|
|
|
if creature:isLocalPlayer() then
|
2012-08-03 07:05:09 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local pos = creature:getPosition()
|
|
|
|
if not pos then return false end
|
|
|
|
|
2014-05-29 03:16:47 +02:00
|
|
|
local localPlayer = g_game.getLocalPlayer()
|
2013-01-21 22:36:53 +01:00
|
|
|
if pos.z ~= localPlayer:getPosition().z or not creature:canBeSeen() then return false end
|
2012-10-05 21:13:50 +02:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
local hidePlayers = hidePlayersButton:isChecked()
|
|
|
|
local hideNPCs = hideNPCsButton:isChecked()
|
|
|
|
local hideMonsters = hideMonstersButton:isChecked()
|
|
|
|
local hideSkulls = hideSkullsButton:isChecked()
|
|
|
|
local hideParty = hidePartyButton:isChecked()
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-07-29 05:34:40 +02:00
|
|
|
if hidePlayers and creature:isPlayer() then
|
2012-02-09 06:27:29 +01:00
|
|
|
return false
|
2012-07-29 05:34:40 +02:00
|
|
|
elseif hideNPCs and creature:isNpc() then
|
2012-02-09 06:27:29 +01:00
|
|
|
return false
|
2012-07-29 05:34:40 +02:00
|
|
|
elseif hideMonsters and creature:isMonster() then
|
2012-02-09 06:27:29 +01:00
|
|
|
return false
|
2012-07-29 05:34:40 +02:00
|
|
|
elseif hideSkulls and creature:isPlayer() and creature:getSkull() == SkullNone then
|
2012-02-09 06:27:29 +01:00
|
|
|
return false
|
|
|
|
elseif hideParty and creature:getShield() > ShieldWhiteBlue then
|
|
|
|
return false
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-08-08 21:17:16 +02:00
|
|
|
function onCreatureHealthPercentChange(creature, health)
|
|
|
|
local battleButton = battleButtonsByCreaturesList[creature:getId()]
|
|
|
|
if battleButton then
|
2013-07-02 22:43:52 +02:00
|
|
|
if getSortType() == 'health' then
|
|
|
|
removeCreature(creature)
|
|
|
|
addCreature(creature)
|
|
|
|
return
|
|
|
|
end
|
2012-08-25 18:05:33 +02:00
|
|
|
battleButton:setLifeBarPercent(creature:getHealthPercent())
|
2012-08-08 21:17:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
local function getDistanceBetween(p1, p2)
|
|
|
|
return math.max(math.abs(p1.x - p2.x), math.abs(p1.y - p2.y))
|
|
|
|
end
|
|
|
|
|
2012-08-03 07:05:09 +02:00
|
|
|
function onCreaturePositionChange(creature, newPos, oldPos)
|
|
|
|
if creature:isLocalPlayer() then
|
2013-01-21 22:36:53 +01:00
|
|
|
if oldPos and newPos and newPos.z ~= oldPos.z then
|
|
|
|
checkCreatures()
|
|
|
|
else
|
2013-07-02 22:43:52 +02:00
|
|
|
-- Distance will change when moving, recalculate and move to correct index
|
|
|
|
if getSortType() == 'distance' then
|
|
|
|
local distanceList = {}
|
2014-05-29 03:16:47 +02:00
|
|
|
for _, battleButton in pairs(battleButtonsByCreaturesList) do
|
|
|
|
table.insert(distanceList, {distance = getDistanceBetween(newPos, battleButton.creature:getPosition()), widget = battleButton})
|
2013-07-02 22:43:52 +02:00
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
if isSortAsc() then
|
|
|
|
table.sort(distanceList, function(a, b) return a.distance < b.distance end)
|
|
|
|
else
|
2014-01-18 15:09:26 +01:00
|
|
|
table.sort(distanceList, function(a, b) return a.distance > b.distance end)
|
2013-07-02 22:43:52 +02:00
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
for i = 1, #distanceList do
|
|
|
|
battlePanel:moveChildToIndex(distanceList[i].widget, i)
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
end
|
2013-07-02 22:43:52 +02:00
|
|
|
|
2014-05-29 03:16:47 +02:00
|
|
|
for _, battleButton in pairs(battleButtonsByCreaturesList) do
|
|
|
|
addCreature(battleButton.creature)
|
2013-01-21 22:36:53 +01:00
|
|
|
end
|
|
|
|
end
|
2012-08-03 07:05:09 +02:00
|
|
|
else
|
|
|
|
local has = hasCreature(creature)
|
|
|
|
local fit = doCreatureFitFilters(creature)
|
|
|
|
if has and not fit then
|
|
|
|
removeCreature(creature)
|
2013-01-21 22:36:53 +01:00
|
|
|
elseif fit then
|
2013-07-02 22:43:52 +02:00
|
|
|
if has and getSortType() == 'distance' then
|
|
|
|
removeCreature(creature)
|
|
|
|
end
|
2012-08-03 07:05:09 +02:00
|
|
|
addCreature(creature)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
2012-08-03 07:05:09 +02:00
|
|
|
end
|
|
|
|
|
2012-10-05 21:13:50 +02:00
|
|
|
function onCreatureOutfitChange(creature, outfit, oldOutfit)
|
2013-02-04 19:33:34 +01:00
|
|
|
if doCreatureFitFilters(creature) then
|
|
|
|
addCreature(creature)
|
|
|
|
else
|
|
|
|
removeCreature(creature)
|
2012-10-05 21:13:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-08 21:17:16 +02:00
|
|
|
function onCreatureAppear(creature)
|
2014-05-29 03:16:47 +02:00
|
|
|
if creature:isLocalPlayer() then
|
|
|
|
addEvent(function()
|
|
|
|
updateStaticSquare()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2012-10-05 21:13:50 +02:00
|
|
|
if doCreatureFitFilters(creature) then
|
2012-08-08 21:17:16 +02:00
|
|
|
addCreature(creature)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function onCreatureDisappear(creature)
|
|
|
|
removeCreature(creature)
|
|
|
|
end
|
|
|
|
|
2012-08-03 07:05:09 +02:00
|
|
|
function hasCreature(creature)
|
|
|
|
return battleButtonsByCreaturesList[creature:getId()] ~= nil
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function addCreature(creature)
|
2012-02-09 06:27:29 +01:00
|
|
|
local creatureId = creature:getId()
|
2012-08-25 18:05:33 +02:00
|
|
|
local battleButton = battleButtonsByCreaturesList[creatureId]
|
2013-01-10 18:25:32 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
-- Register when creature is added to battlelist for the first time
|
|
|
|
if not creatureAgeList[creatureId] then
|
|
|
|
creatureAgeList[creatureId] = os.time()
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-08-03 07:05:09 +02:00
|
|
|
if not battleButton then
|
2013-07-02 22:43:52 +02:00
|
|
|
battleButton = g_ui.createWidget('BattleButton')
|
2012-08-25 18:05:33 +02:00
|
|
|
battleButton:setup(creature)
|
|
|
|
|
2012-08-20 23:58:43 +02:00
|
|
|
battleButton.onHoverChange = onBattleButtonHoverChange
|
2013-01-20 17:09:14 +01:00
|
|
|
battleButton.onMouseRelease = onBattleButtonMouseRelease
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
battleButtonsByCreaturesList[creatureId] = battleButton
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-08-03 07:58:48 +02:00
|
|
|
if creature == g_game.getAttackingCreature() then
|
|
|
|
onAttack(creature)
|
|
|
|
end
|
|
|
|
|
|
|
|
if creature == g_game.getFollowingCreature() then
|
|
|
|
onFollow(creature)
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
local inserted = false
|
|
|
|
local nameLower = creature:getName():lower()
|
|
|
|
local healthPercent = creature:getHealthPercent()
|
|
|
|
local playerPosition = g_game.getLocalPlayer():getPosition()
|
|
|
|
local distance = getDistanceBetween(playerPosition, creature:getPosition())
|
|
|
|
local age = creatureAgeList[creatureId]
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
local childCount = battlePanel:getChildCount()
|
|
|
|
for i = 1, childCount do
|
|
|
|
local child = battlePanel:getChildByIndex(i)
|
|
|
|
local childName = child:getCreature():getName():lower()
|
|
|
|
local equal = false
|
|
|
|
if getSortType() == 'age' then
|
|
|
|
local childAge = creatureAgeList[child:getCreature():getId()]
|
|
|
|
if (age < childAge and isSortAsc()) or (age > childAge and isSortDesc()) then
|
|
|
|
battlePanel:insertChild(i, battleButton)
|
|
|
|
inserted = true
|
|
|
|
break
|
|
|
|
elseif age == childAge then
|
|
|
|
equal = true
|
|
|
|
end
|
|
|
|
elseif getSortType() == 'distance' then
|
|
|
|
local childDistance = getDistanceBetween(child:getCreature():getPosition(), playerPosition)
|
|
|
|
if (distance < childDistance and isSortAsc()) or (distance > childDistance and isSortDesc()) then
|
|
|
|
battlePanel:insertChild(i, battleButton)
|
|
|
|
inserted = true
|
|
|
|
break
|
|
|
|
elseif childDistance == distance then
|
|
|
|
equal = true
|
|
|
|
end
|
|
|
|
elseif getSortType() == 'health' then
|
|
|
|
local childHealth = child:getCreature():getHealthPercent()
|
|
|
|
if (healthPercent < childHealth and isSortAsc()) or (healthPercent > childHealth and isSortDesc()) then
|
|
|
|
battlePanel:insertChild(i, battleButton)
|
|
|
|
inserted = true
|
|
|
|
break
|
|
|
|
elseif healthPercent == childHealth then
|
|
|
|
equal = true
|
2014-01-18 15:09:26 +01:00
|
|
|
end
|
2013-07-02 22:43:52 +02:00
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
-- If any other sort type is selected and values are equal, sort it by name also
|
|
|
|
if getSortType() == 'name' or equal then
|
|
|
|
local length = math.min(childName:len(), nameLower:len())
|
|
|
|
for j=1,length do
|
|
|
|
if (nameLower:byte(j) < childName:byte(j) and isSortAsc()) or (nameLower:byte(j) > childName:byte(j) and isSortDesc()) then
|
|
|
|
battlePanel:insertChild(i, battleButton)
|
|
|
|
inserted = true
|
|
|
|
break
|
|
|
|
elseif (nameLower:byte(j) > childName:byte(j) and isSortAsc()) or (nameLower:byte(j) < childName:byte(j) and isSortDesc()) then
|
|
|
|
break
|
2014-01-18 15:09:26 +01:00
|
|
|
elseif j == nameLower:len() and isSortAsc() then
|
2013-07-02 22:43:52 +02:00
|
|
|
battlePanel:insertChild(i, battleButton)
|
|
|
|
inserted = true
|
|
|
|
elseif j == childName:len() and isSortDesc() then
|
|
|
|
battlePanel:insertChild(i, battleButton)
|
|
|
|
inserted = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if inserted then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-07-02 22:43:52 +02:00
|
|
|
-- Insert at the end if no other place is found
|
|
|
|
if not inserted then
|
|
|
|
battlePanel:insertChild(childCount + 1, battleButton)
|
|
|
|
end
|
2012-08-03 07:05:09 +02:00
|
|
|
else
|
2012-08-25 18:05:33 +02:00
|
|
|
battleButton:setLifeBarPercent(creature:getHealthPercent())
|
2012-08-08 21:17:16 +02:00
|
|
|
end
|
2013-01-21 22:36:53 +01:00
|
|
|
|
|
|
|
local localPlayer = g_game.getLocalPlayer()
|
|
|
|
battleButton:setVisible(localPlayer:hasSight(creature:getPosition()) and creature:canBeSeen())
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
2013-01-20 17:09:14 +01:00
|
|
|
function removeAllCreatures()
|
2013-07-02 22:43:52 +02:00
|
|
|
creatureAgeList = {}
|
2014-05-29 03:16:47 +02:00
|
|
|
for _, battleButton in pairs(battleButtonsByCreaturesList) do
|
|
|
|
removeCreature(battleButton.creature)
|
2013-01-20 17:09:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function removeCreature(creature)
|
|
|
|
if hasCreature(creature) then
|
|
|
|
local creatureId = creature:getId()
|
|
|
|
|
|
|
|
if lastBattleButtonSwitched == battleButtonsByCreaturesList[creatureId] then
|
|
|
|
lastBattleButtonSwitched = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
battleButtonsByCreaturesList[creatureId].creature:hideStaticSquare()
|
|
|
|
battleButtonsByCreaturesList[creatureId]:destroy()
|
|
|
|
battleButtonsByCreaturesList[creatureId] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function onBattleButtonMouseRelease(self, mousePosition, mouseButton)
|
2013-01-06 16:04:49 +01:00
|
|
|
if mouseWidget.cancelNextRelease then
|
|
|
|
mouseWidget.cancelNextRelease = false
|
|
|
|
return false
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
if ((g_mouse.isPressed(MouseLeftButton) and mouseButton == MouseRightButton)
|
2013-01-06 16:04:49 +01:00
|
|
|
or (g_mouse.isPressed(MouseRightButton) and mouseButton == MouseLeftButton)) then
|
|
|
|
mouseWidget.cancelNextRelease = true
|
|
|
|
g_game.look(self.creature)
|
|
|
|
return true
|
2013-01-25 20:30:40 +01:00
|
|
|
elseif mouseButton == MouseLeftButton and g_keyboard.isShiftPressed() then
|
|
|
|
g_game.look(self.creature)
|
|
|
|
return true
|
2013-01-07 18:17:01 +01:00
|
|
|
elseif mouseButton == MouseRightButton and not g_mouse.isPressed(MouseLeftButton) then
|
2012-07-24 07:30:08 +02:00
|
|
|
modules.game_interface.createThingMenu(mousePosition, nil, nil, self.creature)
|
2012-02-09 08:26:52 +01:00
|
|
|
return true
|
2013-01-07 18:17:01 +01:00
|
|
|
elseif mouseButton == MouseLeftButton and not g_mouse.isPressed(MouseRightButton) then
|
2013-01-06 16:04:49 +01:00
|
|
|
if self.isTarget then
|
|
|
|
g_game.cancelAttack()
|
2012-02-09 08:26:52 +01:00
|
|
|
else
|
2013-01-06 16:04:49 +01:00
|
|
|
g_game.attack(self.creature)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
2012-02-09 08:26:52 +01:00
|
|
|
return true
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
2013-01-06 16:04:49 +01:00
|
|
|
return false
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
2014-05-29 03:16:47 +02:00
|
|
|
function onBattleButtonHoverChange(battleButton, hovered)
|
|
|
|
if battleButton.isBattleButton then
|
|
|
|
battleButton.isHovered = hovered
|
|
|
|
updateBattleButton(battleButton)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onAttack(creature)
|
2012-02-20 03:27:08 +01:00
|
|
|
local battleButton = creature and battleButtonsByCreaturesList[creature:getId()] or lastBattleButtonSwitched
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButton then
|
|
|
|
battleButton.isTarget = creature and true or false
|
2012-08-25 18:05:33 +02:00
|
|
|
updateBattleButton(battleButton)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onFollow(creature)
|
2012-02-20 03:27:08 +01:00
|
|
|
local battleButton = creature and battleButtonsByCreaturesList[creature:getId()] or lastBattleButtonSwitched
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButton then
|
|
|
|
battleButton.isFollowed = creature and true or false
|
2012-08-25 18:05:33 +02:00
|
|
|
updateBattleButton(battleButton)
|
2012-02-20 03:27:08 +01:00
|
|
|
end
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
2014-05-29 03:16:47 +02:00
|
|
|
function updateStaticSquare()
|
|
|
|
for _, battleButton in pairs(battleButtonsByCreaturesList) do
|
|
|
|
if battleButton.isTarget then
|
|
|
|
battleButton:update()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-25 18:05:33 +02:00
|
|
|
function updateCreatureSkull(creature, skullId)
|
|
|
|
local battleButton = battleButtonsByCreaturesList[creature:getId()]
|
|
|
|
if battleButton then
|
|
|
|
battleButton:updateSkull(skullId)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
2012-08-25 18:05:33 +02:00
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-08-25 18:05:33 +02:00
|
|
|
function updateCreatureEmblem(creature, emblemId)
|
|
|
|
local battleButton = battleButtonsByCreaturesList[creature:getId()]
|
|
|
|
if battleButton then
|
|
|
|
battleButton:updateSkull(emblemId)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
2012-08-25 18:05:33 +02:00
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-08-25 18:05:33 +02:00
|
|
|
function updateBattleButton(battleButton)
|
|
|
|
battleButton:update()
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButton.isTarget or battleButton.isFollowed then
|
2012-07-09 13:37:47 +02:00
|
|
|
-- set new last battle button switched
|
2012-02-09 06:27:29 +01:00
|
|
|
if lastBattleButtonSwitched and lastBattleButtonSwitched ~= battleButton then
|
|
|
|
lastBattleButtonSwitched.isTarget = false
|
|
|
|
lastBattleButtonSwitched.isFollowed = false
|
2012-08-25 18:05:33 +02:00
|
|
|
updateBattleButton(lastBattleButtonSwitched)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
lastBattleButtonSwitched = battleButton
|
|
|
|
end
|
|
|
|
end
|