Module:CombatAreas: Difference between revisions

From Melvor Idle
(Use tabs instead of spaces for indentation; getMonsterAreas: Remove check for ID 1/Hill Giants - Game data no longer flags ITM afflicted monsters in this way)
(Update for v1.1)
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 Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local Items = require('Module:Items')
local Shop = require('Module:Shop')
local Shop = require('Module:Shop')


p.eventData = AreaData.event
local areaMap = {
 
    ["combat"] = 'combatAreas',
function processArea(area, index, type)
    ["dungeon"] = 'dungeons',
local result = Shared.clone(area)
    ["slayer"] = 'slayerAreas'
result.id = index - 1
}
if result.name == nil then
result.name = result.areaName
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 i, area in pairs(AreaData.combatAreas) do
    for k, areaType in pairs(areaMap) do
if area.name == name then
        local area = GameData.getEntityByName(areaType, name)
return processArea(area, i, 'combat')
        if area ~= nil then
end
            return area
end
        end
 
    end
for i, area in pairs(AreaData.slayerAreas) do
if area.name == name then
return processArea(area, i, 'slayer')
end
end
 
for i, area in pairs(AreaData.dungeons) do
if area.name == name then
return processArea(area, i, 'dungeon')
end
end
 
return nil
end
end


function p.getAreaByID(type, id)
function p.getAreaByID(id, 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.getEntityByID(areaType, id)
return processArea(AreaData[type][id + 1], id + 1)
    end
end
end


function p.getAreaFilterType(type, name)
function p.getAreaFilterType(name, type)
local areaName = nil
    local areaType = areaMap[type]
if type == 'dungeon' then areas = AreaData.dungeons
    if areaType ~= nil then
elseif type == 'combat' then areas = AreaData.combatAreas
        return GameData.getEntityByName(areaType, name)
elseif type == 'slayer' then areas = AreaData.slayerAreas
    end
else return nil end
 
for i, area in pairs(areas) do
if type == 'dungeon' then areaName = area.name
else areaName = area.areaName end
 
if areaName == name then
return processArea(area, i, type)
end
end
 
return nil
end
end


function p.getAreas(checkFunc)
function p.getAreas(checkFunc)
local resultArray = {}
local resultArray = nil


for i, area in Shared.skpairs(AreaData.combatAreas) do
    for i, areaType in pairs(areaMap) do
local temp = processArea(area, i, 'combat')
        local areas = GameData.getEntities(areaType, checkFunc)
if checkFunc(temp) then
        if resultArray == nil then
table.insert(resultArray, temp)
            resultArray = areas
end
        else
end
            for k, area in ipairs(areas) do
for i, area in Shared.skpairs(AreaData.slayerAreas) do
                table.insert(resultArray, area)
local temp = processArea(area, i, 'slayer')
            end
if checkFunc(temp) then
        end
table.insert(resultArray, temp)
    end
end
    if resultArray == nil then
end
        resultArray = {}
for i, area in Shared.skpairs(AreaData.dungeons) do
    end
local temp = processArea(area, i, 'dungeon')
if checkFunc(temp) then
table.insert(resultArray, temp)
end
end


return resultArray
    return resultArray
end
end


function p._getAreaRequirements(area)
function p._getAreaRequirements(area)
local result = ''
local resultArray = {}
local resultArray = {}
local addReqsToArray = function(reqArray, requirements)
local addReqsToArray = function(reqArray, requirements)
for i, reqDetails in Shared.skpairs(requirements) do
for i, reqDetails in ipairs(requirements) do
if reqDetails.type == 'Level' then
if reqDetails.type == 'SkillLevel' then
for j, lvlDetails in Shared.skpairs(reqDetails.levels) do
                local skillName = Constants.getSkillName(reqDetails.skillID)
local skill = Constants.getSkillName(lvlDetails.skill)
                if skillName ~= nil then
table.insert(reqArray, Icons._SkillReq(skill, lvlDetails.level))
                    table.insert(reqArray, Icons._SkillReq(skillName, reqDetails.level))
end
                end
elseif reqDetails.type == 'SlayerItem' then
elseif reqDetails.type == 'SlayerItem' then
local item = Items.getItemByID(reqDetails.itemID)
local item = Items.getItemByID(reqDetails.itemID)
table.insert(reqArray, Icons.Icon({item.name, type='item'})..' Equipped')
table.insert(reqArray, Icons.Icon({item.name, type='item'}) .. ' Equipped')
elseif reqDetails.type == 'Dungeon' then
elseif reqDetails.type == 'DungeonCompletion' then
for j, dungDetails in Shared.skpairs(reqDetails.dungeons) do
                local dung = p.getAreaByID(reqDetails.dungeonID, 'dungeon')
local dung = p.getAreaByID('dungeon', dungDetails.dungeonID)
                if dung ~= nil then
if dungDetails.count > 1 then
                    if reqDetails.count > 1 then
table.insert(reqArray, dungDetails.count..'x '..Icons.Icon({dung.name, type='dungeon'})..' Completions')
                        table.insert(reqArray, Shared.formatnum(reqDetails.count) .. 'x ' .. Icons.Icon({dung.name, type='dungeon'}) .. ' Completions')
else
                    else
table.insert(reqArray, Icons.Icon({dung.name, type='dungeon'})..' Completed')
                        table.insert(reqArray, Icons.Icon({dung.name, type='dungeon'}) .. ' Completed')
end
                    end
end
                end
elseif reqDetails.type == 'ShopPurchase' then
elseif reqDetails.type == 'ShopPurchase' then
local shopPurchase = Shop.processPurchase(reqDetails.category, reqDetails.id)
                local shopPurchase = GameData.getEntityByID('shopPurchases', reqDetails.purchaseID)
if shopPurchase ~= nil then
if shopPurchase ~= nil then
table.insert(reqArray, Shop._getPurchaseIcon({ shopPurchase }) .. ' Purchased')
table.insert(reqArray, Shop._getPurchaseIcon({ shopPurchase }) .. ' Purchased')
Line 135: Line 96:


if area.unlockRequirement ~= nil then
if area.unlockRequirement ~= nil then
-- Ensure this requirement isn't already part of the entry requirements
        addReqsToArray(resultArray, area.unlockRequirement)
local addReq = true
if area.entryRequirements ~= nil then
local unlockReqStr = mw.dumpObject(area.unlockRequirement)
for i, reqDetails in ipairs(area.entryRequirements) do
-- Using mw.dumpObject() as a lazy way to compare tables
if area.unlockRequirement.type == reqDetails.type and mw.dumpObject(reqDetails) == unlockReqStr then
addReq = false
break
end
end
end
if addReq then
addReqsToArray(resultArray, { area.unlockRequirement })
end
end
end


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


Line 160: Line 106:
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
local descText, subIdx = string.gsub(area.areaEffectDescription, '${effectValue}', area.areaEffectValue or 0)
local descText, subIdx = string.gsub(area.areaEffectDescription, '${effectValue}', area.areaEffect.magnitude or 0)
return descText
return descText
else
else
Line 189: Line 135:


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



Revision as of 15:51, 22 October 2022

Data is pulled from Module:GameData/data

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 Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local GameData = require('Module:GameData')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local Shop = require('Module:Shop')

local areaMap = {
    ["combat"] = 'combatAreas',
    ["dungeon"] = 'dungeons',
    ["slayer"] = 'slayerAreas'
}

function p.getArea(name)
	--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
        local area = GameData.getEntityByName(areaType, name)
        if area ~= nil then
            return area
        end
    end
end

function p.getAreaByID(id, type)
    local areaType = areaMap[type]
    if areaType ~= nil then
        return GameData.getEntityByID(areaType, id)
    end
end

function p.getAreaFilterType(name, type)
    local areaType = areaMap[type]
    if areaType ~= nil then
        return GameData.getEntityByName(areaType, name)
    end
end

function p.getAreas(checkFunc)
	local resultArray = nil

    for i, areaType in pairs(areaMap) do
        local areas = GameData.getEntities(areaType, checkFunc)
        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

    return resultArray
end

function p._getAreaRequirements(area)
	local resultArray = {}
	local addReqsToArray = function(reqArray, requirements)
		for i, reqDetails in ipairs(requirements) do
			if reqDetails.type == 'SkillLevel' then
                local skillName = Constants.getSkillName(reqDetails.skillID)
                if skillName ~= nil then
                    table.insert(reqArray, Icons._SkillReq(skillName, reqDetails.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 == 'DungeonCompletion' then
                local dung = p.getAreaByID(reqDetails.dungeonID, 'dungeon')
                if dung ~= nil then
                    if reqDetails.count > 1 then
                        table.insert(reqArray, Shared.formatnum(reqDetails.count) .. 'x ' .. Icons.Icon({dung.name, type='dungeon'}) .. ' Completions')
                    else
                        table.insert(reqArray, Icons.Icon({dung.name, type='dungeon'}) .. ' Completed')
                    end
                end
			elseif reqDetails.type == 'ShopPurchase' then
                local shopPurchase = GameData.getEntityByID('shopPurchases', reqDetails.purchaseID)
				if shopPurchase ~= nil then
					table.insert(reqArray, Shop._getPurchaseIcon({ shopPurchase }) .. ' Purchased')
				end
			else
				table.insert(reqArray, 'ERROR: Unknown requirement type ' .. (reqDetails.type or 'nil') .. '[[Category:Pages with script errors]]')
			end
		end
	end

	if area.entryRequirements ~= nil then
		addReqsToArray(resultArray, area.entryRequirements)
	end

	if area.unlockRequirement ~= nil then
        addReqsToArray(resultArray, area.unlockRequirement)
	end

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

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

	return area[statName]
end

function p.getAreaStat(frame)
	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 area = p.getArea(areaName)
	if area == nil then
		return "ERROR: Could not find an area named "..areaName
	end

	return p._getAreaStat(area, statName)
end

function p.getMonsterAreas(monsterID)
	return p.getAreas(
        function(area)
           return Shared.contains(area.monsterIDs, monsterID) 
        end)
end

function p.getDungeonRequirements(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

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

return p