Anonymous

Module:Monsters: Difference between revisions

From Melvor Idle
getChestDrops: Refactor to avoid modifying item object; Other optimizations
No edit summary
(getChestDrops: Refactor to avoid modifying item object; Other optimizations)
Line 713: Line 713:
--Sort the loot table by weight in descending order
--Sort the loot table by weight in descending order
table.sort(monster.lootTable, function(a, b) return a[2] > b[2] end)
table.sort(monster.lootTable, function(a, b) return a[2] > b[2] end)
for i, row in Shared.skpairs(monster.lootTable) do
for i, row in ipairs(monster.lootTable) do
local thisItem = Items.getItemByID(row[1])
local thisItem = Items.getItemByID(row[1])
Line 817: Line 817:
end
end


for i, row in Shared.skpairs(monster.lootTable) do
for i, row in ipairs(monster.lootTable) do
local thisItem = Items.getItemByID(row[1])
local thisItem = Items.getItemByID(row[1])
Line 867: Line 867:
local dropChance = 0
local dropChance = 0
local dropWt = 0
local dropWt = 0
for i, row in Shared.skpairs(monster.lootTable) do
for i, row in ipairs(monster.lootTable) do
local thisItem = Items.getItemByID(row[1])
totalWt = totalWt + row[2]
totalWt = totalWt + row[2]
if item['id'] == thisItem['id'] then
if item.id == row[1] then
dropWt = row[2]
dropWt = row[2]
end
end
Line 886: Line 885:
return "ERROR: No item named "..ChestName..' found[[Category:Pages with script errors]]'
return "ERROR: No item named "..ChestName..' found[[Category:Pages with script errors]]'
end
end
local result = ''
local result = ''


Line 905: Line 903:


--Sort the loot table by weight in descending order
--Sort the loot table by weight in descending order
local chestDrops, dropIdx = {}, 0
local hasQty = type(chest.dropQty) == 'table'
for i, row in pairs(chest.dropTable) do
for i, row in pairs(chest.dropTable) do
if chest.dropQty ~= nil then
local qty = hasQty and chest.dropQty[i] or 1
table.insert(row, chest.dropQty[i])
dropIdx = dropIdx + 1
else
chestDrops[dropIdx] = {row[1], row[2], qty}
table.insert(row, 1)
end
end
end
table.sort(chest.dropTable, function(a, b) return a[2] > b[2] end)
table.sort(chestDrops, function(a, b) return a[2] > b[2] end)
for i, row in Shared.skpairs(chest.dropTable) do
for i, row in ipairs(chestDrops) do
local thisItem = Items.getItemByID(row[1])
local thisItem = Items.getItemByID(row[1])
local qty = row[3]
local qty = row[3]