autoLootConf = {
modalWindow = {
mwID = 4999,
title = "toggle auto-loot items",
name = "auto-loot configuration",
infoText = {
"You can toggle on and off up to 6 different items",
"New items must be in your bag if you want to see them in your auto-loot config list",
},
},
saveSV = {40000, 40001, 40002, 40003, 40004, 40005, 40006, 40007, 40008, 40009, 40010, 40011},
}
function autoLoot_startUp()
local configKeys = {"modalWindow", "saveSV"}
local mwKeys = {"mwID", "title", "name", "infoText"}
local errorMsg = "autoLootConf."
local mwT = autoLootConf.modalWindow
local saveT = autoLootConf.saveSV
local function missingError(missingKey, errorMsg) print("ERROR - missing value in "..errorMsg..missingKey) end
for key, v in pairs(autoLootConf) do
if not isInArray(configKeys, key) then print("ERROR - unknown key ["..key.."] in "..errorMsg) end
end
if not saveT then return missingError("saveSV", errorMsg) end
if not mwT then return missingError("modalWindow", errorMsg) end
if not mwT.mwID then return missingError("mwID", errorMsg) end
if not mwT.title then return missingError("title", errorMsg) end
if not mwT.name then return missingError("name", errorMsg) end
if not mwT.infoText then mwT.infoText = {} end
if type(mwT.infoText) ~= "table" then mwT.infoText = {mwT.infoText} end
if type(saveT) ~= "table" then autoLootConf.saveSV = {saveT} end
end
function autoLoot_configMW(player)
local mwT = autoLootConf.modalWindow
local window = ModalWindow(mwT.mwID, mwT.name, MWTitle(mwT.title))
local itemList = autoLoot_getItemList(player)
local activatedItems = autoLoot_getAutoLootItem(player)
local function toggleState(itemName)
for _, itemT in ipairs(activatedItems) do
if itemT.itemName == itemName then return "AUTO-LOOT ON" end
end
return "OFF"
end
for i, itemT in pairs(itemList) do
local itemName = itemT.itemName
window:addChoice(i, itemName.." - "..toggleState(itemName))
end
player:registerEvent("autoLootMW")
window:addButton(100, "toggle")
window:addButton(101, "close")
window:addButton(102, "info")
window:setDefaultEnterButton(100)
window:setDefaultEscapeButton(101)
window:sendToPlayer(player)
end
function autoLoot_handleMW(player, mwID, buttonID, choiceID)
if mwID ~= autoLootConf.modalWindow.mwID then return end
if buttonID == 101 then return end
if buttonID == 102 then
for _, msg in ipairs(autoLootConf.modalWindow.infoText) do player:sendTextMessage(ORANGE, msg) end
return autoLoot_configMW(player)
end
local itemList = autoLoot_getItemList(player)
local itemT = itemList[choiceID]
if not itemT then return end
for _, sv in ipairs(autoLootConf.saveSV) do
local sv_itemID = player:getStorageValue(sv)
if sv_itemID == itemT.itemID then
player:setStorageValue(sv, -1)
return autoLoot_configMW(player)
end
end
for _, sv in ipairs(autoLootConf.saveSV) do
local sv_itemID = player:getStorageValue(sv)
if sv_itemID < 0 then
player:setStorageValue(sv, itemT.itemID)
return autoLoot_configMW(player)
end
end
player:sendTextMessage(GREEN, "You can only auto-loot up to "..tableCount(autoLootConf.saveSV).." different items")
player:sendTextMessage(BLUE, "You can only auto-loot up to "..tableCount(autoLootConf.saveSV).." different items")
return autoLoot_configMW(player)
end
local function getAutoLootPlayers(damageMap)
local playerT = {}
for creatureID, damT in pairs(damageMap) do
if hasAutoLoot(creatureID) then playerT[creatureID] = damT.total end
end
return playerT
end
function autoLoot_onLogin(player)
player:registerEvent("autoLoot_onKill")
end
function autoLoot_onKill(killer, target)
if not target or not target:isMonster() then return end
target:registerEvent("autoLoot_onDeath")
end
function autoLoot_onDeath(creature, corpse)
if not corpse then return end
if not corpse or type(corpse) == "table" then return end
if type(corpse) == "table" then
return
end
local itemID = corpse:getId()
local decayID = ItemType(itemID):getDecayId()
addEvent(autoLoot_shareItems, 100, creature:getDamageMap(), itemID, corpse:getPosition(), decayID)
end
function autoLoot_shareItems(damageMap, itemID, pos, decayID)
local tileItems = Tile(pos):getItems()
local corpse
for _, item in ipairs(tileItems) do
if item:getId() == itemID then corpse = item break end
end
if not corpse then
for _, item in ipairs(tileItems) do
if item:getId() == decayID then corpse = item break end
end
end
if not corpse then return print("ERROR - missing corpse in autoLoot_shareItems()") end
if not corpse:isContainer() then return end
local items = corpse:getItems()
local playerT = getAutoLootPlayers(damageMap)
if tableCount(playerT) == 0 then return end
for _, item in ipairs(items) do
local itemID = item:getId()
local player
local damage = 0
for playerID, totalDamage in pairs(playerT) do
local items = autoLoot_getAutoLootItem(playerID)
for _, itemT in ipairs(items) do
if itemT.itemID == itemID then
if totalDamage > damage then
player = Player(playerID)
damage = totalDamage
end
break
end
end
end
if player then
local newItem = item:clone()
player:addItemEx(newItem)
item:remove()
end
end
end
function autoLoot_getItemList(player)
local itemList = autoLoot_getAutoLootItem(player)
local bagItems = player:getBag() and player:getBag():getItems() or {}
local newItemList = {}
local loopID = 0
for _, itemT in pairs(itemList) do
loopID = loopID + 1
newItemList[loopID] = itemT
end
local function addToList(itemID)
for _, itemT in ipairs(newItemList) do
if itemT.itemID == itemID then return end
end
return true
end
for _, item in ipairs(bagItems) do
local itemID = item:getId()
if addToList(itemID) then
loopID = loopID + 1
newItemList[loopID] = {
itemName = item:getName(),
itemID = itemID,
}
end
end
return newItemList
end
function autoLoot_getAutoLootItem(playerID)
local player = Player(playerID)
if not player then return end
local itemList = {}
local loopID = 0
for _, sv in ipairs(autoLootConf.saveSV) do
local itemID = player:getStorageValue(sv)
if itemID > 0 then
loopID = loopID + 1
itemList[loopID] = {
itemID = itemID,
itemName = ItemType(itemID):getName(),
}
end
end
return itemList
end
function hasAutoLoot(playerID)
local player = Player(playerID)
if not player then return end
for _, sv in ipairs(autoLootConf.saveSV) do
if player:getStorageValue(sv) > 0 then return true end
end
end