Module:CombatAreas: Difference between revisions

Update for v1.3
(Added requirements code and made it work with slayer stuff)
(Update for v1.3)
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
--NOTE: Some tables are in Module:CombatAreas/AreaTables to prevent loop from referencing Monsters
--NOTE: Some tables are in Module:CombatAreas/AreaTables to prevent loop from referencing Monsters
local p = {}
local p = {}
local AreaData = mw.loadData('Module:CombatAreas/data')


local Constants = require('Module:Constants')
local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local GameData = require('Module:GameData')
local Common = require('Module:Common')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Items = require('Module:Items')


function processArea(area, index, type)
local areaMap = {
  local result = Shared.clone(area)
["combat"] = 'combatAreas',
  result.id = index - 1
["dungeon"] = 'dungeons',
  if result.name == nil then
["slayer"] = 'slayerAreas',
    result.name = result.areaName
["depth"] = 'abyssDepths'
  end
}
  result.type = type
  return result
end


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


  for i, area in pairs(AreaData.slayerAreas) do
function p.getAreaByID(id, areaType)
    if area.name == name then
for aType, areaKey in pairs(areaMap) do
      return processArea(area, i, 'slayer')
if areaType == nil or areaType == aType then
    end
local area = GameData.getEntityByID(areaKey, id)
  end
if area ~= nil then
 
return area
  for i, area in pairs(AreaData.dungeons) do
end
    if area.name == name then
end
      return processArea(area, i, 'dungeon')
end
    end
  end
 
  return nil
end
end


function p.getAreaByID(type, id)
function p.getAreaFilterType(name, type)
  if type == 'dungeon' then type = 'dungeons'
local areaType = areaMap[type]
  elseif type == 'combat' then type = 'combatAreas'
if areaType ~= nil then
  elseif type == 'slayer' then type = 'slayerAreas' end
return GameData.getEntityByName(areaType, name)
  return processArea(AreaData[type][id + 1], id + 1)
end
end
end


function p.getAreaFilterType(type, name)
function p.getAreas(checkFunc)
  local areaName = nil
local resultArray = nil
  if type == 'dungeon' then areas = AreaData.dungeons
  elseif type == 'combat' then areas = AreaData.combatAreas
  elseif type == 'slayer' then areas = AreaData.slayerAreas
  else return nil end


  for i, area in pairs(areas) do
for i, areaType in pairs(areaMap) do
    if type == 'dungeon' then areaName = area.name
local areas = GameData.getEntities(areaType, checkFunc)
    else areaName = area.areaName end
if resultArray == nil then
resultArray = areas
else
for k, area in ipairs(areas) do
table.insert(resultArray, area)
end
end
end
if resultArray == nil then
resultArray = {}
end


    if areaName == name then
return resultArray
      return processArea(area, i, type)
end
    end
  end


  return nil
--Returns the expansion icon for the area if it has one
function p.getExpansionIcon(frame)
local areaName = frame.args ~= nil and frame.args[1] or frame
local area = p.getArea(areaName)
if area == nil then
return Shared.printError('No area named "' .. areaName .. '" exists in the data module')
end
return Icons.getExpansionIcon(area.id)
end
end


function p.getAreas(checkFunc)
function p._getAreaRequirements(area)
  local resultArray = {}
local resultArray = {}
if area.entryRequirements ~= nil then
local reqText = Common.getRequirementString(area.entryRequirements)
if reqText ~= nil then
table.insert(resultArray, reqText)
end
end


  for i, area in Shared.skpairs(AreaData.combatAreas) do
if area.unlockRequirement ~= nil then
    local temp = processArea(area, i, 'combat')
-- Avoid repeating the same requirements twice, can happen for some dungeons e.g. Impending Darkness
    if checkFunc(temp) then
if area.entryRequirements == nil or mw.dumpObject(area.unlockRequirement) ~= mw.dumpObject(area.entryRequirements) then
      table.insert(resultArray, temp)
local reqText = Common.getRequirementString(area.unlockRequirement)
    end
if reqText ~= nil then
  end
table.insert(resultArray, reqText)
  for i, area in Shared.skpairs(AreaData.slayerAreas) do
end
    local temp = processArea(area, i, 'slayer')
end
    if checkFunc(temp) then
end
      table.insert(resultArray, temp)
    end
  end
  for i, area in Shared.skpairs(AreaData.dungeons) do
    local temp = processArea(area, i, 'dungeon')
    if checkFunc(temp) then
      table.insert(resultArray, temp)
    end
  end


  return resultArray
return table.concat(resultArray, '<br/>')
end
end


function p._getAreaRequirements(area)
function p.getAreaRequirementsForBox(frame)
  local result = ''
--Returns infobox formatting for requirements, or returns nothing if there are none.
  local resultArray = {}
local areaName = frame.args ~= nil and frame.args[1] or frame
  local addReqsToArray = function(reqArray, requirements)
local area = p.getArea(areaName)
      for i, reqDetails in Shared.skpairs(requirements) do
if area == nil then
        mw.log(reqDetails.type)
return Shared.printError('No area named "' .. areaName .. '" exists in the data module')
        if reqDetails.type == 'Level' then
end
          for j, lvlDetails in Shared.skpairs(reqDetails.levels) do
            local skill = Constants.getSkillName(lvlDetails.skill)
            table.insert(reqArray, Icons._SkillReq(skill, lvlDetails.level))
          end
        elseif reqDetails.type == 'SlayerItem' then
          local item = Items.getItemByID(reqDetails.itemID)
          table.insert(reqArray, Icons.Icon({item.name, type='item'})..' Equipped')
        elseif reqDetails.type == 'Dungeon' then
          for j, dungDetails in Shared.skpairs(reqDetails.dungeons) do
            local dung = p.getAreaByID('dungeon', dungDetails.dungeonID)
            if dungDetails.count > 1 then
              table.insert(reqArray, dungDetails.count..'x '..Icons.Icon({dung.name, type='dungeon'})..' Completions')
            else
              table.insert(reqArray, Icons.Icon({dung.name, type='dungeon'})..' Completed')
            end
          end
        end
      end
    end
 
  if area.entryRequirements ~= nil then
    addReqsToArray(resultArray, area.entryRequirements)
  end
 
  if area.unlockRequirement ~= nil then
    addReqsToArray(resultArray, { area.unlockRequirement })
  end


  result = table.concat(resultArray, '<br/>')
local reqs = p._getAreaRequirements(area)
  return result
if reqs ~= '' then
reqs = "|-\r\n|'''Requirements:'''\r\n"..reqs
end
return reqs
end
end


function p._getAreaStat(area, statName)
function p._getAreaStat(area, statName)
  if statName == 'requirements' then
if statName == 'requirements' then
    return p._getAreaRequirements(area)
return p._getAreaRequirements(area)
  elseif statName == 'areaEffectDesc' then
elseif statName == 'areaEffectDesc' then
    if area.areaEffect ~= nil and area.areaEffect then
if area.areaEffect ~= nil then
      return area.areaEffectDescription
local descText, subIdx = string.gsub(area.areaEffectDescription, '${effectValue}', area.areaEffect.magnitude or 0)
    else
return descText
      return 'None'
else
    end
return 'None'
  elseif statName == 'difficulty' then
end
    local result = Constants.getDifficultyString(area.difficulty[1])
elseif statName == 'difficulty' then
    if area.difficulty[2] ~= nil then
local result = Constants.getDifficultyString(area.difficulty[1])
      result = result..' - '..Constants.getDifficultyString(area.difficulty[2])
if area.difficulty[2] ~= nil then
    end
result = result..' - '..Constants.getDifficultyString(area.difficulty[2])
    return result
end
  end
return result
end


  return area[statName]
return area[statName]
end
end


function p.getAreaStat(frame)
function p.getAreaStat(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame[1]
local areaName = frame.args ~= nil and frame.args[1] or frame[1]
  local statName = frame.args ~= nil and frame.args[2] or frame[2]
local statName = frame.args ~= nil and frame.args[2] or frame[2]
  local area = p.getArea(areaName)
local area = p.getArea(areaName)
  if area == nil then
if area == nil then
    return "ERROR: Could not find an area named "..areaName
return Shared.printError('No area named "' .. areaName .. '" exists in the data module')
  end
end


  return p._getAreaStat(area, statName)
return p._getAreaStat(area, statName)
end
end


function p.getMonsterAreas(monsterID)
function p._isMonsterInArea(monster, area)
  local areaArray = {}
return (
  --There are three types of areas but the lists are pretty short so looping all of them isn't a real issue
Shared.contains(area.monsterIDs, monster.id)
  for i, area in pairs(AreaData.combatAreas) do
-- Check for Lair of the Spider Queen random spiders
    if Shared.contains(area.monsters, monsterID) then
or (
      table.insert(areaArray, processArea(area, i, 'combat'))
Shared.contains(area.monsterIDs, 'melvorTotH:RandomSpiderLair')
    end
and Shared.contains(GameData.rawData.spiderLairMonsters, monster.id)
  end
)
 
)
  for i, area in pairs(AreaData.slayerAreas) do
end
    if Shared.contains(area.monsters, monsterID) then
      table.insert(areaArray, processArea(area, i, 'slayer'))
    end
  end


  --Hill Giants specifically ignore dungeons to prevent the issue with Into the Mist incorrectly being listed.
function p._getMonsterAreas(monster)
  if monsterID ~= 1 then
-- Special handling for Lair of the Spider Queen, which has a random list of enemies
    for i, area in pairs(AreaData.dungeons) do
local randomSpiderCheck = Shared.contains(GameData.rawData.spiderLairMonsters, monster.id)
      if Shared.contains(area.monsters, monsterID) then
return p.getAreas(
        table.insert(areaArray, processArea(area, i, 'dungeon'))
function(area)
      end
return p._isMonsterInArea(monster, area)
    end
end)
  end
  return areaArray
end
end


function p.getDungeonRequirements(frame)
function p.getDungeonRequirements(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame
local areaName = frame.args ~= nil and frame.args[1] or frame
  local area = p.getArea(areaName)
local area = p.getArea(areaName)
  if area == nil then
if area == nil then
    return "ERROR: Could not find an area named "..areaName
return Shared.printError('No area named "' .. areaName .. '" exists in the data module')
  end
end


  local result = p._getAreaStat(area, 'requirements')
local result = p._getAreaStat(area, 'requirements')
  if result ~= '' then
if result ~= '' then
    result = "\r\n|-\r\n|'''Requirements:'''<br/>"..result
result = "\r\n|-\r\n|'''Requirements:'''<br/>"..result
  end
end
  return result
return result
end
end


return p
return p