Module:CombatAreas: Difference between revisions

From Melvor Idle
(Added p.getMonsterAreas)
(Some tweaks to make handling areas in other code easier)
Line 7: Line 7:
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Monsters = require('Module:Monsters')
local Monsters = require('Module:Monsters')
function processArea(area, index)
  local result = Shared.clone(area)
  result.id = index - 1
  if result.name == nil then
    result.name = result.areaName
  end
  if Shared.contains(result.type, 'Dungeon') then
    result.type = 'dungeon'
  end
  return result
end


function p.getArea(name)
function p.getArea(name)
Line 14: Line 26:
   for i, area in pairs(AreaData.combatAreas) do
   for i, area in pairs(AreaData.combatAreas) do
     if area.areaName == name then
     if area.areaName == name then
       result = Shared.clone(area)
       return processArea(area, i)
      result.id = i - 1
      return result
     end
     end
   end
   end
Line 22: Line 32:
   for i, area in pairs(AreaData.slayerAreas) do
   for i, area in pairs(AreaData.slayerAreas) do
     if area.areaName == name then
     if area.areaName == name then
       result = Shared.clone(area)
       return processArea(area, i)
      result.id = i - 1
      return result
     end
     end
   end
   end
Line 30: Line 38:
   for i, area in pairs(AreaData.dungeons) do
   for i, area in pairs(AreaData.dungeons) do
     if area.name == name then
     if area.name == name then
       result = Shared.clone(area)
       return processArea(area, i)
      result.type = 'dungeon'
      result.id = i - 1
      return result
     end
     end
   end
   end
Line 45: Line 50:
   for i, area in pairs(AreaData.combatAreas) do
   for i, area in pairs(AreaData.combatAreas) do
     if Shared.contains(area.monsters, monsterID) then
     if Shared.contains(area.monsters, monsterID) then
      result = Shared.clone(area)
       table.insert(areaArray, processArea(area, i))
      result.id = i - 1
       table.insert(areaArray, result)
     end
     end
   end
   end
Line 53: Line 56:
   for i, area in pairs(AreaData.slayerAreas) do
   for i, area in pairs(AreaData.slayerAreas) do
     if Shared.contains(area.monsters, monsterID) then
     if Shared.contains(area.monsters, monsterID) then
      result = Shared.clone(area)
       table.insert(areaArray, processArea(area, i))
      result.id = i - 1
       table.insert(areaArray, result)
     end
     end
   end
   end
Line 61: Line 62:
   for i, area in pairs(AreaData.dungeons) do
   for i, area in pairs(AreaData.dungeons) do
     if Shared.contains(area.monsters, monsterID) then
     if Shared.contains(area.monsters, monsterID) then
      result = Shared.clone(area)
       table.insert(areaArray, processArea(area, i))
      result.type = 'dungeon'
      result.id = i - 1
       table.insert(areaArray, result)
     end
     end
   end
   end

Revision as of 19:28, 7 October 2020

Data is pulled from Module:GameData/data

NOTE: Some tables are in Module:CombatAreas/AreaTables to prevent loop from referencing Monsters


local p = {}

local Constants = mw.loadData('Module:Constants/data')
local AreaData = mw.loadData('Module:CombatAreas/data')

local Shared = require('Module:Shared')
local Icons = require('Module:Icons')
local Monsters = require('Module:Monsters')

function processArea(area, index)
  local result = Shared.clone(area)
  result.id = index - 1
  if result.name == nil then
    result.name = result.areaName
  end
  if Shared.contains(result.type, 'Dungeon') then
    result.type = 'dungeon'
  end
  return result
end

function p.getArea(name)
  local result = nil
  mw.log(name)
  --There are three types of areas but the lists are pretty short so looping all of them isn't a real issue
  for i, area in pairs(AreaData.combatAreas) do
    if area.areaName == name then
      return processArea(area, i)
    end
  end

  for i, area in pairs(AreaData.slayerAreas) do
    if area.areaName == name then
      return processArea(area, i)
    end
  end

  for i, area in pairs(AreaData.dungeons) do
    if area.name == name then
      return processArea(area, i)
    end
  end

  return nil
end

function p.getMonsterAreas(monsterID)
  local areaArray = {}
  --There are three types of areas but the lists are pretty short so looping all of them isn't a real issue
  for i, area in pairs(AreaData.combatAreas) do
    if Shared.contains(area.monsters, monsterID) then
      table.insert(areaArray, processArea(area, i))
    end
  end

  for i, area in pairs(AreaData.slayerAreas) do
    if Shared.contains(area.monsters, monsterID) then
      table.insert(areaArray, processArea(area, i))
    end
  end

  for i, area in pairs(AreaData.dungeons) do
    if Shared.contains(area.monsters, monsterID) then
      table.insert(areaArray, processArea(area, i))
    end
  end
  return areaArray
end

function p.getAreaMonsterTable(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame
  local area = p.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 = Monsters.getMonsterByID(monsterID)
    tableTxt = tableTxt..'\r\n|-\r\n|'..Icons.Icon({monster.name, type='monster'})
    tableTxt = tableTxt..'||'..Monsters.getMonsterCombatLevel(monster.name)
    tableTxt = tableTxt..'||'..Shared.formatnum(Monsters.getMonsterHP(monster.name))
    tableTxt = tableTxt..'||'..Shared.formatnum(Monsters.getMonsterMaxHit(monster.name))
    tableTxt = tableTxt..'||'..Monsters.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 = p.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 = Monsters.getMonsterByID(monsterID)
      local name = monster.name
      if monsterID == 51 then name = 'Spider2' end
      tableTxt = tableTxt..'\r\n|-\r\n|'..Icons.Icon({name, type='monster'})
      tableTxt = tableTxt..'||'..Monsters.getMonsterCombatLevel(name)
      tableTxt = tableTxt..'||'..Shared.formatnum(Monsters.getMonsterHP(name))
      tableTxt = tableTxt..'||'..Shared.formatnum(Monsters.getMonsterMaxHit(name))
      tableTxt = tableTxt..'||'..Monsters.getMonsterStyleIcon({name, nolink='true'})
      tableTxt = tableTxt..'||'..monsterCounts[monsterID]
      table.insert(usedMonsters, monsterID)
    end
  end
  tableTxt = tableTxt..'\r\n|}'
  return tableTxt
end

return p