Module:Monsters: Difference between revisions

From Melvor Idle
m (typing iz hard)
(added functions to get average GP for all monster drops and then made a table of that)
Line 136: Line 136:
end
end


function p.getMonsterCombatLevel(frame)
function p._getMonsterCombatLevel(monster)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)
 
  if monster == nil then
    return "ERROR: No monster with that name found"
  end
 
   local base = 0.25 * (monster.defenceLevel + monster.hitpoints)
   local base = 0.25 * (monster.defenceLevel + monster.hitpoints)
   local melee = 0.325 * (monster.attackLevel + monster.strengthLevel)
   local melee = 0.325 * (monster.attackLevel + monster.strengthLevel)
Line 155: Line 148:
     return math.floor(base + magic)
     return math.floor(base + magic)
   end
   end
end
function p.getMonsterCombatLevel(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)
  if monster == nil then
    return "ERROR: No monster with that name found"
  end
  return p._getMonsterCombatLevel(monster)
end
end


Line 214: Line 218:
   local dunCount = 0
   local dunCount = 0
   local nonDunCount = 0
   local nonDunCount = 0
   for i, area in Shared.skpairs(areaList) do
   for i, area in Shared.skpairs(areaList) do
    mw.log(area.type)
     if area.type == 'dungeon' then
     if area.type == 'dungeon' then
       dunCount = dunCount + 1
       dunCount = dunCount + 1
Line 764: Line 770:
     end
     end
   end
   end
  return result
end
function p._getMonsterAverageGP(monster)
  local result = ''
  local totalGP = 0
  if monster.bones ~= nil then
    local bones = Items.getItemByID(monster.bones)
    --Show the bones only if either the monster shows up outside of dungeons _or_ the monster drops shards
    if not p._isDungeonOnlyMonster(monster) or Shared.contains(bones.name, 'Shard') then
      totalGP = totalGP + bones.sellsFor
    end
  end
  --Likewise, seeing the loot table is tied to the monster appearing outside of dungeons
  if not p._isDungeonOnlyMonster(monster) then
    local lootChance = monster.lootChance ~= nil and monster.lootChance or 100
    local lootValue = 0
    local avgGp = 0
    if monster.dropCoins ~= nil and monster.dropCoins[2] > 1 then
      avgGp = (monster.dropCoins[1] + monster.dropCoins[2] - 1) / 2
    end
    totalGP = totalGP + avgGp
    local multiDrop = Shared.tableCount(monster.lootTable) > 1
    local totalWt = 0
    for i, row in pairs(monster.lootTable) do
      totalWt = totalWt + row[2]
    end
    for i, row in Shared.skpairs(monster.lootTable) do
      local thisItem = Items.getItemByID(row[1])
      local maxQty = row[3]
      local itemPrice = thisItem.sellsFor ~= nil and thisItem.sellsFor or 0
      --Getting the drop chance
      local dropChance = (row[2] / totalWt * lootChance)
      --Adding to the average loot value based on price & dropchance
      lootValue = lootValue + (dropChance * 0.01 * itemPrice * ((1 + maxQty) / 2))
    end
   
    totalGP = totalGP + lootValue
  end
  return Shared.round(totalGP, 2, 2)
end
function p.getMonsterAverageGP(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)
  if monster == nil then
    return "ERROR: No monster with that name found"
  end
 
  return p._getMonsterAverageGP(monster)
end
function p.getMonsterEVTable(frame)
  local result = '{| class="wikitable sortable"'
  result = result..'\r\n!Monster!!Combat Level!!Average GP'
  for i, monsterTemp in Shared.skpairs(MonsterData.Monsters) do
    local monster = Shared.clone(monsterTemp)
    monster.id = i - 1
    if not p._isDungeonOnlyMonster(monster) then
      local monsterGP = p._getMonsterAverageGP(monster)
      local combatLevel = p._getMonsterCombatLevel(monster, 'Combat Level')
      result = result..'\r\n|-\r\n|[['..monster.name..']]||'..combatLevel..'||'..monsterGP
    end
  end
  result = result..'\r\n|}'
   return result
   return result
end
end


return p
return p

Revision as of 17:02, 26 February 2021

Data is pulled from Module:GameData/data


local p = {}

local Constants = mw.loadData('Module:Constants/data')
local MonsterData = mw.loadData('Module:Monsters/data')

local Areas = require('Module:CombatAreas')
local Magic = require('Module:Magic')
local Shared = require('Module:Shared')
local Icons = require('Module:Icons')
local Items = require('Module:Items')

function p.getMonster(name)
  local result = nil
  if name == 'Spider (lv. 51)' or name == 'Spider' then
    return p.getMonsterByID(50)
  elseif name == 'Spider (lv. 52)' or name == 'Spider2' then
    return p.getMonsterByID(51)
  end

  for i, monster in pairs(MonsterData.Monsters) do
    if(monster.name == name) then
      result = Shared.clone(monster)
      --Make sure every monster has an ID, and account for the 1-based indexing of Lua
      result.id = i - 1
    end
  end
  return result
end

function p.getMonsterByID(ID)
  local result = Shared.clone(MonsterData.Monsters[ID + 1])
  result.id = ID
  return result
end

function p.getSpecialAttack(name)
  local result = nil

  for i, attack in pairs(MonsterData.SpecialAttacks) do
    if(attack.name == name) then
      result = Shared.clone(attack)
      --Make sure every attack has an ID, and account for the 1-based indexing of Lua
      result.id = i - 1
    end
  end
  return result
end

function p.getSpecialAttackByID(ID)
  return MonsterData.SpecialAttacks[ID + 1]
end

function p.getPassive(name)
  local result = nil

  for i, passive in pairs(MonsterData.Passives) do
    if passive.name == name then
      result = Shared.clone(passive)
      --Make sure every passive has an ID, and account for the 1-based indexing of Lua
      result.id = i - 1
    end
  end
  return result
end

function p.getPassiveByID(ID)
  return MonsterData.Passives[ID + 1]
end

function p.getMonsterStat(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame[1]
  local StatName = frame.args ~= nil and frame.args[2] or frame[2]
  local monster = p.getMonster(MonsterName)
  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  if StatName == 'HP' then
    return p.getMonsterHP(MonsterName)
  elseif StatName == 'maxHit' then
    return p.getMonsterMaxHit(MonsterName)
  elseif StatName == 'accuracyRating' then
    return p.getMonsterAR(MonsterName)
  elseif StatName == 'meleeEvasionRating' then
    return p.getMonsterER({MonsterName, 'Melee'})
  elseif StatName == 'rangedEvasionRating' then
    return p.getMonsterER({MonsterName, 'Ranged'})
  elseif StatName == 'magicEvasionRating' then
    return p.getMonsterER({MonsterName, 'Magic'})
  end

  return monster[StatName]
end

function p.getMonsterStyleIcon(frame)
  local args = frame.args ~= nil and frame.args or frame
  local MonsterName = args[1]
  local notext = args.notext
  local nolink = args.nolink
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  local iconText = ''
  if  monster.attackType == Constants.attackType.Melee then
    iconText = Icons.Icon({'Melee', notext=notext, nolink=nolink})
  elseif monster.attackType == Constants.attackType.Ranged then
    iconText = Icons.Icon({'Ranged', type='skill', notext=notext, nolink=nolink})
  else
    iconText = Icons.Icon({'Magic', type='skill', notext=notext, nolink=nolink})
  end

  return iconText
end

function p.getMonsterHP(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)
  if monster ~= nil then
    return monster.hitpoints * 10
  else
    return "ERROR: No monster with that name found"
  end
end

function p.getMonsterAttackSpeed(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)
  if monster ~= nil then
    return monster.attackSpeed / 1000
  else
    return "ERROR: No monster with that name found"
  end
end

function p._getMonsterCombatLevel(monster)
  local base = 0.25 * (monster.defenceLevel + monster.hitpoints)
  local melee = 0.325 * (monster.attackLevel + monster.strengthLevel)
  local range = 0.325 * (1.5 * monster.rangedLevel)
  local magic = 0.325 * (1.5 * monster.magicLevel)
  if melee > range and melee > magic then
    return math.floor(base + melee)
  elseif range > magic then
    return math.floor(base + range)
  else
    return math.floor(base + magic)
  end
end

function p.getMonsterCombatLevel(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  return p._getMonsterCombatLevel(monster)
end

function p.getMonsterAR(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end
  
  local effAttLvl = 0
  local attBonus = 0
  if monster.attackType == Constants.attackType.Melee then
    effAttLvl = monster.attackLevel + 9
    attBonus = monster.attackBonus + 64
  elseif monster.attackType == Constants.attackType.Ranged then
    effAttLvl = monster.rangedLevel + 9
    attBonus = monster.attackBonusRanged + 64
  elseif monster.attackType == Constants.attackType.Magic then
    effAttLvl = monster.magicLevel + 9
    attBonus = monster.attackBonusMagic + 64
  else
    return "ERROR: This monster has an invalid attack type somehow"
  end

  return effAttLvl * attBonus
end

function p.getMonsterER(frame)
  local args = frame.args ~= nil and frame.args or frame
  local MonsterName = args[1]
  local style = args[2]
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end
  
  local effDefLvl = 0
  local defBonus = 0
  if style == "Melee" then
    effDefLvl = monster.defenceLevel + 9
    defBonus = monster.defenceBonus + 64
  elseif style == "Ranged" then
    effDefLvl = monster.defenceLevel + 9
    defBonus = monster.defenceBonusRanged + 64
  elseif style == "Magic" then
    effDefLvl = math.floor(monster.magicLevel * 0.7 + monster.defenceLevel * 0.3) + 9
    defBonus = monster.defenceBonusMagic + 64
  else
    return "ERROR: Must choose Melee, Ranged, or Magic"
  end
  return effDefLvl * defBonus
end

function p._isDungeonOnlyMonster(monster)
  local areaList = Areas.getMonsterAreas(monster.id)
  local dunCount = 0
  local nonDunCount = 0

  for i, area in Shared.skpairs(areaList) do
    mw.log(area.type)
    if area.type == 'dungeon' then
      dunCount = dunCount + 1
    else
      nonDunCount = nonDunCount + 1
    end
  end
  return dunCount > 0 and nonDunCount == 0
end

function p.isDungeonOnlyMonster(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with name "..monsterName.." found"
  end

  return p._isDungeonOnlyMonster(monster)
end

function p._getMonsterAreas(monster)
  local result = ''
  local areaList = Areas.getMonsterAreas(monster.id)
  for i, area in pairs(areaList) do
    if i > 1 then result = result..'<br/>' end
    result = result..Icons.Icon({area.name, type = area.type})
  end
  return result
end

function p.getMonsterAreas(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with name "..monsterName.." found"
  end

  return p._getMonsterAreas(monster)
end

function p.getMonsterMaxHit(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  local normalChance = 100
  local specialMaxHit = 0
  local normalMaxHit = p.getMonsterBaseMaxHit(frame)
  if monster.hasSpecialAttack then
    for i, specID in pairs(monster.specialAttackID) do
      local specAttack = p.getSpecialAttackByID(specID)
      if monster.overrideSpecialChances ~= nil then
        normalChance = normalChance - monster.overrideSpecialChances[i]
      else
        normalChance = normalChance - specAttack.chance
      end
      local thisMax = 0
      if specAttack.setDamage ~= nil then
        thisMax = specAttack.setDamage * 10
      else
        thisMax = normalMaxHit
      end
      if specAttack.stunDamageMultiplier ~= nil and specAttack.stunDamageMultiplier > 1 then
        thisMax = thisMax * specAttack.stunDamageMultiplier
      end
      if specAttack.sleepDamageMultiplier ~= nil and specAttack.sleepDamageMultiplier > 1 then
        thisMax = thisMax * specAttack.sleepDamageMultiplier
      end
      if thisMax > specialMaxHit then specialMaxHit = thisMax end
    end
  end
  --Ensure that if the monster never does a normal attack, the normal max hit is irrelevant
  if normalChance == 0 then normalMaxHit = 0 end
  return math.max(specialMaxHit, normalMaxHit)
end

function p.getMonsterBaseMaxHit(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end
  
  local effStrLvl = 0
  local strBonus = 0
  if monster.attackType == Constants.attackType.Melee then
    effStrLvl = monster.strengthLevel + 9
    strBonus = monster.strengthBonus
  elseif monster.attackType == Constants.attackType.Ranged then
    effStrLvl = monster.rangedLevel + 9
    strBonus = monster.strengthBonusRanged
  elseif monster.attackType == Constants.attackType.Magic then
    local mSpell = nil
    if monster.selectedSpell ~= nil then mSpell = Magic.getSpellByID('Spells', monster.selectedSpell) end
    if mSpell == nil then
      return math.floor(10 * (monster.setMaxHit + (monster.setMaxHit * monster.damageBonusMagic / 100)))
    else
      return math.floor(10 * (mSpell.maxHit + (mSpell.maxHit * monster.damageBonusMagic / 100)))
    end
  else
    return "ERROR: This monster has an invalid attack type somehow"
  end

  --Should only get here for Melee/Ranged, which use functionally the same damage formula
  return math.floor(10 * (1.3 + (effStrLvl/10) + (strBonus / 80) + ((effStrLvl * strBonus) / 640)))
end

function p.getMonsterAttacks(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  local result = ''

  local iconText = ''
  local typeText = ''
  if  monster.attackType == Constants.attackType.Melee then
    iconText = Icons.Icon({'Melee', notext=true})
    typeText = 'Melee'
  elseif monster.attackType == Constants.attackType.Ranged then
    iconText = Icons.Icon({'Ranged', type='skill', notext=true})
    typeText = 'Ranged'
  else
    iconText = Icons.Icon({'Magic', type='skill', notext=true})
    typeText = 'Magic'
  end

  local normalAttackChance = 100
  if monster.hasSpecialAttack then
    for i, specID in pairs(monster.specialAttackID) do
      local specAttack = p.getSpecialAttackByID(specID)
      local attChance = 0
      if monster.overrideSpecialChances ~= nil then
        attChance = monster.overrideSpecialChances[i]
      else
        attChance = specAttack.chance
      end
      normalAttackChance = normalAttackChance - attChance

      result = result..'\r\n* '..attChance..'% '..iconText..' '..specAttack.name..'\r\n** '..specAttack.description
    end
  end
  if normalAttackChance == 100 then
    result = iconText..'1 - '..p.getMonsterBaseMaxHit(frame)..' '..typeText..' Damage'
  elseif normalAttackChance > 0 then
    result = '* '..normalAttackChance..'% '..iconText..'1-'..p.getMonsterBaseMaxHit(frame)..' '..typeText..' Damage'..result
  end
  return result
end

function p.getMonsterPassives(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  local result = ''

  if monster.hasPassive then
    result = result .. '===Passives==='
    for i, passiveID in pairs(monster.passiveID) do
      local passive = p.getPassiveByID(passiveID)
      local passiveChance = 0
      if passive.chance ~= nil then
        passiveChance = passive.chance
      end

      result = result .. '\r\n* ' .. Shared.round(passiveChance, 2, 0) .. '% ' .. passive.name .. '\r\n** ' .. passive.description
    end
  end
  return result
end

function p.getMonsterCategories(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  local result = '[[Category:Monsters]]'
  
  if monster.attackType == Constants.attackType.Melee then
    result = result..'[[Category:Melee Monsters]]'
  elseif monster.attackType == Constants.attackType.Ranged then
    result = result..'[[Category:Ranged Monsters]]'
  elseif monster.attackType == Constants.attackType.Magic then
    result = result..'[[Category:Magic Monsters]]'
  end

  if monster.hasSpecialAttack then
    result = result..'[[Category:Monsters with Special Attacks]]'
  end
  
  if monster.isBoss then
    result = result..'[[Category:Bosses]]'
  end

  return result
end

function p.getMonsterDrops(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end

  local result = ''

  if monster.bones ~= nil then
    local bones = Items.getItemByID(monster.bones)
    --Show the bones only if either the monster shows up outside of dungeons _or_ the monster drops shards
    if not p._isDungeonOnlyMonster(monster) or Shared.contains(bones.name, 'Shard') then
      result = result.."'''Always Drops:'''"
      result = result..'\r\n{|class="wikitable"'
      result = result..'\r\n!Item !! Qty'
      result = result..'\r\n|-\r\n|'..Icons.Icon({bones.name, type='item'})
      result = result..'||'..(monster.boneQty ~= nil and monster.boneQty or 1)..'\r\n'..'|}'
    end
  end

  --Likewise, seeing the loot table is tied to the monster appearing outside of dungeons
  if not p._isDungeonOnlyMonster(monster) then
    local lootChance = monster.lootChance ~= nil and monster.lootChance or 100
    local lootValue = 0

    result = result.."'''Loot:'''"
    local avgGp = 0

    if monster.dropCoins ~= nil and monster.dropCoins[2] > 1 then
      avgGp = (monster.dropCoins[1] + monster.dropCoins[2] - 1) / 2
      local gpTxt = Icons.GP(monster.dropCoins[1], monster.dropCoins[2] - 1)
      result = result.."\r\nIn addition to loot, the monster will also drop "..gpTxt
    end

    local multiDrop = Shared.tableCount(monster.lootTable) > 1
    local totalWt = 0
    for i, row in pairs(monster.lootTable) do
      totalWt = totalWt + row[2]
    end
    result = result..'\r\n{|class="wikitable sortable"'
    result = result..'\r\n!Item!!Qty'
    result = result..'!!Price!!colspan="2"|Chance'

    --Sort the loot table by weight in descending order
    table.sort(monster.lootTable, function(a, b) return a[2] > b[2] end)
    for i, row in Shared.skpairs(monster.lootTable) do
      local thisItem = Items.getItemByID(row[1])
      local maxQty = row[3]
      result = result..'\r\n|-\r\n|'..Icons.Icon({thisItem.name, type='item'})
      result = result..'||style="text-align:right" data-sort-value="'..maxQty..'"|'

      if maxQty > 1 then 
        result = result.. '1 - ' 
      end
      result = result..Shared.formatnum(row[3])

      --Adding price columns
      local itemPrice = thisItem.sellsFor ~= nil and thisItem.sellsFor or 0
      if itemPrice == 0 or maxQty == 1 then
        result = result..'||'..Icons.GP(itemPrice)
      else
        result = result..'||'..Icons.GP(itemPrice, itemPrice * maxQty)
      end

      --Getting the drop chance
      local dropChance = (row[2] / totalWt * lootChance)
      if dropChance ~= 100 then
        --Show fraction as long as it isn't going to be 1/1
        result = result..'||style="text-align:right" data-sort-value="'..row[2]..'"'
        result = result..'|'..Shared.fraction(row[2] * lootChance, totalWt * 100)
        result = result..'||'
      else
        result = result..'||colspan="2" data-sort-value="'..row[2]..'"'
      end
      result = result..'style="text-align:right"|'..Shared.round(dropChance, 2, 2)..'%'

      --Adding to the average loot value based on price & dropchance
      lootValue = lootValue + (dropChance * 0.01 * itemPrice * ((1 + maxQty) / 2))
    end
    if multiDrop then
      result = result..'\r\n|-class="sortbottom" \r\n!colspan="3"|Total:'
      if lootChance < 100 then
        result = result..'\r\n|style="text-align:right"|'..Shared.fraction(lootChance, 100)..'||'
      else
        result = result..'\r\n|colspan="2" '
      end
      result = result..'style="text-align:right"|'..lootChance..'.00%'
    end
    result = result..'\r\n|}'
    result = result..'\r\nThe loot dropped by the average kill is worth '..Icons.GP(Shared.round(lootValue, 2, 0)).." if sold"
    result = result..'<br/>Including GP, the average kill is worth '..Icons.GP(Shared.round(avgGp + lootValue, 2, 0))
  end

  --If no other drops, make sure to at least say so.
  if result == '' then result = 'None' end
  return result
end

function p.getChestDrops(frame)
  local ChestName = frame.args ~= nil and frame.args[1] or frame
  local chest = Items.getItem(ChestName)

  if chest == nil then
    return "ERROR: No item named "..ChestName..' found'
  end

  local result = ''

  if chest.dropTable == nil then
    return "ERROR: "..ChestName.." does not have a drop table"
  else
    local lootChance = 100
    local lootValue = 0

    local multiDrop = Shared.tableCount(chest.dropTable) > 1
    local totalWt = 0
    for i, row in pairs(chest.dropTable) do
      totalWt = totalWt + row[2]
    end
    result = result..'\r\n{|class="wikitable sortable"'
    result = result..'\r\n!Item!!Qty'
    result = result..'!!colspan="2"|Chance!!Price'

    --Sort the loot table by weight in descending order
    for i, row in pairs(chest.dropTable) do
      if chest.dropQty ~= nil then
        table.insert(row, chest.dropQty[i])
      else
        table.insert(row, 1)
      end
    end
    table.sort(chest.dropTable, function(a, b) return a[2] > b[2] end)
    for i, row in Shared.skpairs(chest.dropTable) do
      local thisItem = Items.getItemByID(row[1])
      local qty = row[3]
      result = result..'\r\n|-\r\n|'..Icons.Icon({thisItem.name, type='item'})
      result = result..'||style="text-align:right" data-sort-value="'..qty..'"|'

      if qty > 1 then 
        result = result.. '1 - ' 
      end
      result = result..Shared.formatnum(qty)

      local dropChance = (row[2] / totalWt) * 100
      result = result..'||style="text-align:right" data-sort-value="'..row[2]..'"'
      result = result..'|'..Shared.fraction(row[2], totalWt)

      result = result..'||style="text-align:right"|'..Shared.round(dropChance, 2, 2)..'%'

      result = result..'||style="text-align:left" data-sort-value="'..thisItem.sellsFor..'"'
      if qty > 1 then
         result = result..'|'..Icons.GP(thisItem.sellsFor, thisItem.sellsFor * qty)
      else
        result = result..'|'..Icons.GP(thisItem.sellsFor)
      end
      lootValue = lootValue + (dropChance * 0.01 * thisItem.sellsFor * ((1 + qty)/ 2))
    end
    result = result..'\r\n|}'
    result = result..'\r\nThe average value of the contents of one chest is '..Icons.GP(Shared.round(lootValue, 2, 0))
  end

  return result
end

function p.getAreaMonsterTable(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame
  local area = Areas.getArea(areaName)
  if area == nil then
    return "ERROR: Could not find an area named "..areaName
  end

  if area.type == 'dungeon' then
    return p.getDungeonMonsterTable(frame)
  end

  local tableTxt = '{| class="wikitable sortable"'
  tableTxt = tableTxt..'\r\n! Name !! Combat Level !! Hitpoints !! Max Hit !! [[Combat Triangle|Combat Style]]'
  for i, monsterID in pairs(area.monsters) do
    local monster = p.getMonsterByID(monsterID)
    tableTxt = tableTxt..'\r\n|-\r\n|'..Icons.Icon({monster.name, type='monster'})
    tableTxt = tableTxt..'||'..p.getMonsterCombatLevel(monster.name)
    tableTxt = tableTxt..'||'..Shared.formatnum(p.getMonsterHP(monster.name))
    tableTxt = tableTxt..'||'..Shared.formatnum(p.getMonsterMaxHit(monster.name))
    tableTxt = tableTxt..'||'..p.getMonsterStyleIcon({monster.name, nolink='true'})
  end
  tableTxt = tableTxt..'\r\n|}'
  return tableTxt
end

function p.getDungeonMonsterTable(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame
  local area = Areas.getArea(areaName)
  if area == nil then
    return "ERROR: Could not find a dungeon named "..areaName
  end

  --For Dungeons, go through and count how many of each monster are in the dungeon first
  local monsterCounts = {}
  for i, monsterID in pairs(area.monsters) do
    if monsterCounts[monsterID] == nil then 
      monsterCounts[monsterID] = 1
    else
      monsterCounts[monsterID] = monsterCounts[monsterID] + 1
    end
  end

  local usedMonsters = {}

  local tableTxt = '{| class="wikitable sortable"'
  tableTxt = tableTxt..'\r\n! Name !! Combat Level !! Hitpoints !! Max Hit !! [[Combat Triangle|Combat Style]] !! Count'
  for i, monsterID in pairs(area.monsters) do
    if not Shared.contains(usedMonsters, monsterID) then
      local monster = p.getMonsterByID(monsterID)
      local name = monster.name
      if monsterID == 51 then name = 'Spider2' end
      if monsterID ~= 1 then
        tableTxt = tableTxt..'\r\n|-\r\n|'..Icons.Icon({name, type='monster'})
        tableTxt = tableTxt..'||'..p.getMonsterCombatLevel(name)
        tableTxt = tableTxt..'||'..Shared.formatnum(p.getMonsterHP(name))
        tableTxt = tableTxt..'||'..Shared.formatnum(p.getMonsterMaxHit(name))
        tableTxt = tableTxt..'||'..p.getMonsterStyleIcon({name, nolink='true'})
        tableTxt = tableTxt..'||'..monsterCounts[monsterID]
      else
        --Special handling for Into the Mist
        tableTxt = tableTxt..'\r\n|-\r\n|'..Icons.Icon({'Into the Mist', 'Afflicted Monster', nolink=true, img='Question'})
        tableTxt = tableTxt..'||data-sort-value="0"|'..Icons.Icon({'Into the Mist', notext=true, nolink=true, img='Question'})
        tableTxt = tableTxt..'||data-sort-value="0"|'..Icons.Icon({'Into the Mist', notext=true, nolink=true, img='Question'})
        tableTxt = tableTxt..'||data-sort-value="0"|'..Icons.Icon({'Into the Mist', notext=true, nolink=true, img='Question'})
        tableTxt = tableTxt..'||data-sort-value="0"|'..Icons.Icon({'Into the Mist', notext=true, nolink=true, img='Question'})
        tableTxt = tableTxt..'||'..monsterCounts[monsterID]
      end
      table.insert(usedMonsters, monsterID)
    end
  end
  tableTxt = tableTxt..'\r\n|}'
  return tableTxt
end

function p._getAreaMonsterList(area)
  local monsterList = {}
  for i, monsterID in pairs(area.monsters) do
    local monster = p.getMonsterByID(monsterID)
    table.insert(monsterList, Icons.Icon({monster.name, type='monster'}))
  end
  return table.concat(monsterList, '<br/>')
end

function p._getDungeonMonsterList(area)
  local monsterList = {}
  local lastMonster = nil
  local lastID = -1
  local count = 0
  for i, monsterID in Shared.skpairs(area.monsters) do
    if monsterID ~= lastID then
      local monster = p.getMonsterByID(monsterID)
      if lastMonster ~= nil then
        if lastID == 1 then
          --Special handling for Afflicted Monsters
          table.insert(monsterList, Icons.Icon({'Affliction', 'Afflicted Monster', img='Question', qty=count}))
        else
          local name = lastMonster.name
          if lastMonster.id == 51 then name = 'Spider2' end
          table.insert(monsterList, Icons.Icon({name, type='monster', qty=count}))
        end
      end
      lastMonster = monster
      lastID = monster.id
      count = 1
    else
      count = count + 1
    end
    --Make sure the final monster in the dungeon gets counted
    if i == Shared.tableCount(area.monsters) then
      local name = lastMonster.name
      table.insert(monsterList, Icons.Icon({lastMonster.name, type='monster', qty=count}))
    end
  end
  return table.concat(monsterList, '<br/>')
end

function p.getAreaMonsterList(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame
  local area = Areas.getArea(areaName)
  if area == nil then
    return "ERROR: Could not find an area named "..areaName
  end

  if area.type == 'dungeon' then
    return p._getDungeonMonsterList(area)
  else
    return p._getAreaMonsterList(area)
  end
end

function p._getDungeonRewards(area)
  local bossMonster = p.getMonsterByID(area.monsters[Shared.tableCount(area.monsters)])
  local gpMin = bossMonster.dropCoins[1]
  local gpMax = bossMonster.dropCoins[2]
  local chestID = bossMonster.lootTable[1][1]
  local chestQty = bossMonster.lootTable[1][3]
  local theChest = Items.getItemByID(chestID)
  local result = '* '..Icons.GP(gpMin, gpMax)
  result = result..'\r\n* '..Icons.Icon({theChest.name, type='item', qty=chestQty})
  if area.name == 'Volcanic Cave' then
    result = result..'\r\n* '..Icons.Icon({'Fire Cape', type='item', qty=1})
  elseif area.name == 'Infernal Stronghold' then
    result = result..'\r\n* '..Icons.Icon({'Infernal Cape', type='item', qty=1})
  end
  return result
end

function p.getDungeonRewards(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame
  local area = Areas.getArea(areaName)
  if area == nil then
    return "ERROR: Could not find an area named "..areaName
  end

  if area.type == 'dungeon' then
    return p._getDungeonRewards(area)
  else
    return "ERROR: "..areaName.." is not a dungeon"
  end
end

function p.getFoxyTable(frame)
  local result = 'Monster,Min GP,Max GP,Average GP'
  for i, monster in Shared.skpairs(MonsterData.Monsters) do
    if not p._isDungeonOnlyMonster(monster) then
      if monster.dropCoins ~= nil and monster.dropCoins[2] > 1 then
       local avgGp = (monster.dropCoins[1] + monster.dropCoins[2] - 1) / 2
       result = result..'<br/>'..monster.name..','..monster.dropCoins[1]..','..(monster.dropCoins[2]-1)..','..avgGp
      end
    end
  end
  return result
end

function p._getMonsterAverageGP(monster)
  local result = ''
  local totalGP = 0

  if monster.bones ~= nil then
    local bones = Items.getItemByID(monster.bones)
    --Show the bones only if either the monster shows up outside of dungeons _or_ the monster drops shards
    if not p._isDungeonOnlyMonster(monster) or Shared.contains(bones.name, 'Shard') then
      totalGP = totalGP + bones.sellsFor
    end
  end

  --Likewise, seeing the loot table is tied to the monster appearing outside of dungeons
  if not p._isDungeonOnlyMonster(monster) then
    local lootChance = monster.lootChance ~= nil and monster.lootChance or 100
    local lootValue = 0

    local avgGp = 0

    if monster.dropCoins ~= nil and monster.dropCoins[2] > 1 then
      avgGp = (monster.dropCoins[1] + monster.dropCoins[2] - 1) / 2
    end

    totalGP = totalGP + avgGp

    local multiDrop = Shared.tableCount(monster.lootTable) > 1
    local totalWt = 0
    for i, row in pairs(monster.lootTable) do
      totalWt = totalWt + row[2]
    end

    for i, row in Shared.skpairs(monster.lootTable) do
      local thisItem = Items.getItemByID(row[1])
      local maxQty = row[3]

      local itemPrice = thisItem.sellsFor ~= nil and thisItem.sellsFor or 0

      --Getting the drop chance
      local dropChance = (row[2] / totalWt * lootChance)

      --Adding to the average loot value based on price & dropchance
      lootValue = lootValue + (dropChance * 0.01 * itemPrice * ((1 + maxQty) / 2))
    end
    
    totalGP = totalGP + lootValue
  end

  return Shared.round(totalGP, 2, 2)
end

function p.getMonsterAverageGP(frame)
  local MonsterName = frame.args ~= nil and frame.args[1] or frame
  local monster = p.getMonster(MonsterName)

  if monster == nil then
    return "ERROR: No monster with that name found"
  end
  
  return p._getMonsterAverageGP(monster)
end

function p.getMonsterEVTable(frame)
  local result = '{| class="wikitable sortable"'
  result = result..'\r\n!Monster!!Combat Level!!Average GP'
  for i, monsterTemp in Shared.skpairs(MonsterData.Monsters) do
    local monster = Shared.clone(monsterTemp)
    monster.id = i - 1
    if not p._isDungeonOnlyMonster(monster) then
      local monsterGP = p._getMonsterAverageGP(monster)
      local combatLevel = p._getMonsterCombatLevel(monster, 'Combat Level')
      result = result..'\r\n|-\r\n|[['..monster.name..']]||'..combatLevel..'||'..monsterGP
    end
  end
  result = result..'\r\n|}'
  return result
end

return p