Anonymous

Module:Items: Difference between revisions

From Melvor Idle
3,656 bytes added ,  24 September 2020
Added p.getItemLootSourceTable
(Tweaks to the getEquipmentTable code)
(Added p.getItemLootSourceTable)
Line 208: Line 208:
   local killStr = ''
   local killStr = ''
   for i, monster in pairs(MonsterData.Monsters) do
   for i, monster in pairs(MonsterData.Monsters) do
     if monster.lootTable ~= nil then
    local isDrop = false
     if monster.bones == item.id then
      isDrop = true
    elseif monster.lootTable ~= nil then
       for j, loot in pairs(monster.lootTable) do
       for j, loot in pairs(monster.lootTable) do
         if loot[1] == item.id then
         if loot[1] == item.id then
           if string.len(killStr) > 0 then
           isDrop = true
            killStr = killStr..','..Icons.Icon({monster.name, type="monster", notext="true"})
          else
            killStr = killStr..'Killing: '..Icons.Icon({monster.name, type="monster", notext="true"})
          end
         end
         end
      end
    end
    if isDrop then
      if string.len(killStr) > 0 then
        killStr = killStr..','..Icons.Icon({monster.name, type="monster", notext="true"})
      else
        killStr = killStr..'Killing: '..Icons.Icon({monster.name, type="monster", notext="true"})
       end
       end
     end
     end
Line 368: Line 374:


function p.getItemSources(frame)
function p.getItemSources(frame)
   local itemName = frame.args ~= nil and frame[1] or frame
   local itemName = frame.args ~= nil and frame.args[1] or frame
   local item = p.getItem(itemName)
   local item = p.getItem(itemName)
   if item == nil then
   if item == nil then
Line 375: Line 381:


   return p._getItemSources(item)
   return p._getItemSources(item)
end
function p._getItemLootSourceTable(item)
  local result = '{| class="wikitable sortable stickyHeader"'
  result = result..'\r\n|- class="headerRow-0"'
  result = result..'\r\n!Source!!Source Type!!Quantity!!Chance'
  --Set up function for adding rows
  local buildRow = function(source, type, qty, chance)
    local rowTxt = '\r\n|-'
    rowTxt = rowTxt..'\r\n|style ="text-align: left;"|'..source
    rowTxt = rowTxt..'\r\n|style ="text-align: left;"|'..type
    rowTxt = rowTxt..'\r\n|style ="text-align: right;" data-sort-value:"'..qty..'"|1'
    if qty > 1 then rowTxt = rowTxt..' - '..qty end
    rowTxt = rowTxt..'\r\n|style ="text-align: right;"|'..Shared.round(chance, 2, 2)..'%'
    return rowTxt
  end
  local dropRows = {}
 
  --Alright, time to go through a few ways to get the item
  --First up: Can we kill somebody and take theirs?
  for i, monster in pairs(MonsterData.Monsters) do
    local qty = 1
    local chance = 0
    local wt = 0
    local totalWt = 0
    if monster.bones == item.id then
      qty = monster.boneQty ~= nil and monster.boneQty or 1
      chance = 100
    elseif monster.lootTable ~= nil then
      for j, loot in pairs(monster.lootTable) do
        totalWt = totalWt + loot[2]
        if loot[1] == item.id then
          wt = loot[2]
          qty = loot[3]
        end
      end
      if wt > 0 then
        local lootChance = monster.lootChance ~= nil and monster.lootChance or 100
        chance = ((wt * lootChance) / (totalWt * 100)) * 100
      end
    end
    if chance > 0 then
      local sourceTxt = Icons.Icon({monster.name, type='monster'})
      table.insert(dropRows, {source = sourceTxt, type = '[[Monster]]', qty = qty, chance = chance})
    end
  end
  --Next: Can we find it by rummaging around in another item?
  for i, item2 in pairs(ItemData) do
    if item2.dropTable ~= nil then
      local qty = 1
      local chance = 0
      local wt = 0
      local totalWt = 0
      for j, loot in pairs(item2.dropTable) do
        totalWt = totalWt + loot[2]
        if loot[1] == item.id then
          wt = loot[2]
          if item2.dropQty ~= nil then qty = item2.dropQty[j] end
        end
      end
      if wt > 0 then
        chance = (wt / totalWt) * 100
        local sourceTxt = Icons.Icon({item2.name, type='item'})
        table.insert(dropRows, {source = sourceTxt, type = '[[Chest]]', qty = qty, chance = chance})
      end
    end
  end
  --Finally, let's try just stealing it
  local thiefType = Icons.Icon({"Thieving", type='skill'})
  for i, npc in pairs(SkillData.Thieving) do
    local qty = 1
    local chance = 0
    local wt = 0
    local totalWt = 0
    if npc.lootTable ~= nil then
      for j, loot in pairs(npc.lootTable) do
        totalWt = totalWt + loot[2]
        if loot[1] == item.id then
          wt = loot[2]
        end
      end
      if wt > 0 then
        chance = (wt / totalWt) * 75
        local sourceTxt = Icons.Icon({npc.name, type='npc'})
        table.insert(dropRows, {source = sourceTxt, type = thiefType, qty = qty, chance = chance})
      end
    end
  end
 
  for i, data in pairs(dropRows) do
    result = result..buildRow(data.source, data.type, data.qty, data.chance)
  end
  result = result..'\r\n|}'
  return result
end
function p.getItemLootSourceTable(frame)
  local itemName = frame.args ~= nil and frame.args[1] or frame
  local item = p.getItem(itemName)
  if item == nil then
    return "ERROR: No item named "..itemName.." exists in the data module"
  end
  return p._getItemLootSourceTable(item)
end
end