Module:Monsters: Difference between revisions

getMonsterDrops: Fix logic for excluding zero weighted drops, and only include table of drops when loot table isn't empty
(getMonsterDrops: Exclude loot table entries with zero weights)
(getMonsterDrops: Fix logic for excluding zero weighted drops, and only include table of drops when loot table isn't empty)
Line 1,074: Line 1,074:
end
end


local multiDrop = Shared.tableCount(monster.lootTable) > 1
local lootTable = GameData.getEntities(monster.lootTable,
local totalWt = 0
function(loot)
for i, row in ipairs(monster.lootTable) do
mw.logObject(loot)
totalWt = totalWt + row.weight
return lootChance > 0 and loot.weight ~= nil and loot.weight > 0
end
end)
result = result..'\r\n{|class="wikitable sortable" id="itemdrops"'
result = result..'\r\n!Item!!Qty'
if not Shared.tableIsEmpty(lootTable) then
result = result..'!!Price!!colspan="2"|Chance'
local multiDrop = Shared.tableCount(lootTable) > 1
 
local totalWt = 0
--Sort the loot table by weight in descending order
for i, row in ipairs(monster.lootTable) do
local lootTable = Shared.shallowClone(monster.lootTable)
totalWt = totalWt + row.weight
table.sort(lootTable, function(a, b)
end
if a.weight == b.weight then
result = result..'\r\n{|class="wikitable sortable" id="itemdrops"'
local aItem, bItem = Items.getItemByID(a.itemID), Items.getItemByID(b.itemID)
result = result..'\r\n!Item!!Qty'
if aItem ~= nil and bItem ~= nil then
result = result..'!!Price!!colspan="2"|Chance'
return aItem.name < bItem.name
--Sort the loot table by weight in descending order
table.sort(lootTable, function(a, b)
if a.weight == b.weight then
local aItem, bItem = Items.getItemByID(a.itemID), Items.getItemByID(b.itemID)
if aItem ~= nil and bItem ~= nil then
return aItem.name < bItem.name
else
return a.itemID < b.itemID
end
else
else
return a.itemID < b.itemID
return a.weight > b.weight
end
end
else
end)
return a.weight > b.weight
for i, row in ipairs(lootTable) do
end
end)
for i, row in ipairs(lootTable) do
if row.weight > 0 then
local thisItem = Items.getItemByID(row.itemID)
local thisItem = Items.getItemByID(row.itemID)
currencyID = thisItem.sellsForCurrency or 'melvorD:GP'
currencyID = thisItem.sellsForCurrency or 'melvorD:GP'
Line 1,144: Line 1,149:
lootValue = lootValue + (dropChance * 0.01 * itemPrice * ((row.minQuantity + row.maxQuantity) / 2))
lootValue = lootValue + (dropChance * 0.01 * itemPrice * ((row.minQuantity + row.maxQuantity) / 2))
end
end
end
if multiDrop then
if multiDrop then
result = result..'\r\n|-class="sortbottom" \r\n!colspan="3"|Total:'
result = result..'\r\n|-class="sortbottom" \r\n!colspan="3"|Total:'
if lootChance < 100 then
if lootChance < 100 then
result = result..'\r\n|style="text-align:right"|'..Num.fraction(lootChance, 100)..'||'
result = result..'\r\n|style="text-align:right"|'..Num.fraction(lootChance, 100)..'||'
else
else
result = result..'\r\n|colspan="2" '
result = result..'\r\n|colspan="2" '
end
result = result..'style="text-align:right"|'..Num.round(lootChance, 2, 2)..'%'
end
end
result = result..'style="text-align:right"|'..Num.round(lootChance, 2, 2)..'%'
result = result..'\r\n|}'
result = result..'\r\nThe loot dropped by the average kill is worth '.. Icons._Currency(currencyID, Num.round(lootValue, 2, 0)).." if sold."
end
end
result = result..'\r\n|}'
result = result..'\r\nThe loot dropped by the average kill is worth '.. Icons._Currency(currencyID, Num.round(lootValue, 2, 0)).." if sold."
if avgGp > 0 then
if avgGp > 0 then
result = result.."<br/>Including "..(currencyID == 'melvorD:GP' and 'GP' or 'AP')
result = result.."<br/>Including "..(currencyID == 'melvorD:GP' and 'GP' or 'AP')