Anonymous

Module:Monsters: Difference between revisions

From Melvor Idle
no edit summary
No edit summary
No edit summary
Line 762: Line 762:
end
end


-- Find drop chance of specified item from specified monster.
-- Usage: |Monster Name|Item Name|numFormat=(perc/frac)
function p.getItemDropChance(frame)
function p.getItemDropChance(frame)
   local MonsterName = frame.args ~= nil and frame.args[1] or frame
   local args = frame.args ~= nil and frame.args or frame
   local ItemName = frame.args ~= nil and frame.args[2] or frame
  local MonsterName = args[1]
   local ItemName = args[2]
  local numFormat = args.numFormat ~= nil and args.numFormat ~= ''
 
   local monster = p.getMonster(MonsterName)
   local monster = p.getMonster(MonsterName)
   local item = Items.getItem(ItemName)
   local item = Items.getItem(ItemName)
Line 775: Line 780:
   end
   end
    
    
  local dropChance = nil
   if not p._isDungeonOnlyMonster(monster) then
   if not p._isDungeonOnlyMonster(monster) then
     local lootChance = monster.lootChance ~= nil and monster.lootChance or 100
     local lootChance = monster.lootChance ~= nil and monster.lootChance or 100
   
 
local totalWt = 0
local totalWt = 0
    for i, row in pairs(monster.lootTable) do
    for i, row in pairs(monster.lootTable) do
      totalWt = totalWt + row[2]
      totalWt = totalWt + row[2]
    end
    end
   
 
  for i, row in Shared.skpairs(monster.lootTable) do
  for i, row in Shared.skpairs(monster.lootTable) do
      local thisItem = Items.getItemByID(row[1])
      local thisItem = Items.getItemByID(row[1])
      if item == thisItem then
     
      if item['id'] == thisItem['id'] then
    local dropChance = (row[2] / totalWt * lootChance)
    local dropChance = (row[2] / totalWt * lootChance)
    end
    end
  end
  end
end
end
if numFormat == 'perc' or nil then
  return dropChance
return Shared.round(dropChance, 2, 2)
elseif numFormat == 'frac' then
return Shared.fraction(dropChance, 100)
end
end
end


318

edits