Module:Township: Difference between revisions

From Melvor Idle
(getSeasonTable: Initial implementation)
(getBiomeTable: Initial implementation; Re-arrange some functions & consistency of function naming with other modules)
Line 9: Line 9:
p.Township = Township
p.Township = Township


-- Gets a Township building by ID, e.g. melvorF:Hunters_Cabin
function p._getBuildingByID(id)
-- Check for the special statue case
if id == 'melvorF:Statues' then
local building = Shared.clone(GameData.getEntityByID(Township.buildings, id))
building.name = 'Statue of Worship'
return building
else
return GameData.getEntityByID(Township.buildings, id)
end
end
-- Given a building, find the building's downgrade
function p._getBuildingDowngrade(building)
if building.upgradesFrom ~= nil then
return p._getBuildingByID(building.upgradesFrom)
end
return nil
end
-- Returns a sorted list of all Township buildings
function p._sortedBuildings(keepUnsorted)
local ku = true
if keepUnsorted ~= nil then
ku = keepUnsorted
end
return GameData.sortByOrderTable(Township.buildings, Township.buildingDisplayOrder, ku)
end
-- Gets the Township level and population requirements for a tier
-- Returns {population=X, level=X}
function p._getTierRequirements(tier)
return Township.populationForTier[tier]
end
-- Generates a table of all seasons, their type/requirements, and modifiers
function p.getSeasonTable(frame)
function p.getSeasonTable(frame)
-- Manual data specifying the worship requirement for those
-- Manual data specifying the worship requirement for those rare seasons
-- rare seasons
local seasonReqs = {
local seasonReqs = {
["Nightfall"] = Icons.Icon({'Township%23Worship', 'Bane Worship', img='Statue of Bane', type='building'}),
["Nightfall"] = Icons.Icon({'Township%23Worship', 'Bane Worship', img='Statue of Bane', type='building'}),
Line 40: Line 75:


return table.concat(resultPart)
return table.concat(resultPart)
end
-- Generates a table listing all biomes and their associated requirements
function p.getBiomeTable(frame)
local resultPart = {}
table.insert(resultPart, '{| class="wikitable sortable stickyHeaders"')
table.insert(resultPart, '\n|- class="headerRow-0"')
table.insert(resultPart, '\n!rowspan="2" colspan="2"| Biome\n!colspan="2"| Requirements')
table.insert(resultPart, '\n|- class="headerRow-1"')
table.insert(resultPart, '\n! ' .. Icons.Icon({'Township', 'Level', type='skill', nolink=true}))
table.insert(resultPart, '\n! ' .. Icons.Icon({'Township%23Population', 'Population', img='Population', type='township'}))
for i, biome in ipairs(Township.biomes) do
local reqs = p._getTierRequirements(biome.tier)
table.insert(resultPart, '\n|-\n|class="table-img"| ' .. Icons.Icon({biome.name, type='biome', size=50, nolink=true, notext=true}))
table.insert(resultPart, '\n| ' .. biome.name)
table.insert(resultPart, '\n|style="text-align:right"| ' .. reqs.level)
table.insert(resultPart, '\n|style="text-align:right" data-sort-value="' .. reqs.population .. '"| ' .. Shared.formatnum(reqs.population))
end
table.insert(resultPart, '\n|}')
return table.concat(resultPart)
end
-- Generates a table showing which buildings can be built in which biomes
-- Skips upgraded buildings
function p.getBuildingBiomeTable(frame)
-- Setup the table
local ret = {}
table.insert(ret, '{| class="wikitable sortable stickyHeaders" style="text-align:center"')
table.insert(ret, '\n|- class="headerRow-0"')
table.insert(ret, '\n! Building')
-- Generate the table header, one column per biome
for _, biome in ipairs(Township.biomes) do
table.insert(ret, '\n! ' .. Icons.Icon({biome.name, type='biome', notext=true, nolink=true}) .. '<br/>' .. biome.name)
end
for _, _building in ipairs(p._sortedBuildings(false)) do
-- Fix melvorF:Statues
local building = p._getBuildingByID(_building.id)
-- Skip upgraded buildings
if p._getBuildingDowngrade(building) == nil then
-- Populate the biome habitability data
local buildingBiomes = {}
-- Set all valid biomes to true
for _, biomeid in ipairs(building.biomes) do
buildingBiomes[biomeid] = true
end
-- Build the row
table.insert(ret, '\n|-')
table.insert(ret, '\n!data-sort-value="' .. building.name .. '" style="text-align:left"| ' .. Icons.Icon({building.name, type='building'}))
for _, biome in ipairs(Township.biomes) do
if buildingBiomes[biome.id] then
-- Buildable
table.insert(ret, '\n|class="table-positive"| ✓')
else
-- Invalid biome
table.insert(ret, '\n|style="border:0px"|')
end
end
end
end
table.insert(ret, '\n|}')
return table.concat(ret)
end
end


Line 105: Line 207:
return resources
return resources
end
-- Returns a sorted list of all Township buildings
function p._SortedBuildings()
return GameData.sortByOrderTable(Township.buildings, Township.buildingDisplayOrder)
end
end


Line 316: Line 413:
function p._GetResourceSkill(id)
function p._GetResourceSkill(id)
return resource_skill[id].skill
return resource_skill[id].skill
end
-- Gets a Township building by ID, e.g. melvorF:Hunters_Cabin
function p._GetBuildingByID(id)
-- Check for the special statue case
if id == 'melvorF:Statues' then
local building = Shared.clone(GameData.getEntityByID(Township.buildings, id))
building.name = 'Statue of Worship'
return building
else
return GameData.getEntityByID(Township.buildings, id)
end
end
end


Line 344: Line 429:
return GameData.getEntityByName(Township.buildings, name)
return GameData.getEntityByName(Township.buildings, name)
end
end
end
-- Gets the Township level and population requirements for a tier
-- Returns {population=X, level=X}
function p._GetTierRequirements(tier)
return Township.populationForTier[tier]
end
end


-- Returns a string containing the Township level and population requirements for a tier
-- Returns a string containing the Township level and population requirements for a tier
function p._GetTierText(tierlevel)
function p._GetTierText(tierlevel)
local tier = p._GetTierRequirements(tierlevel)
local tier = p._getTierRequirements(tierlevel)
return Icons._SkillReq('Township', tier.level, false)..'<br>'..Icons.Icon({'Population', type='township', notext=true})..'&nbsp;'..tier.population
return Icons._SkillReq('Township', tier.level, false)..'<br>'..Icons.Icon({'Population', type='township', notext=true})..'&nbsp;'..tier.population
end
end
Line 381: Line 460:
-- Upgrades From
-- Upgrades From
table.insert(ret, '\r\n|-\r\n| <b>Base Cost:</b>')
table.insert(ret, '\r\n|-\r\n| <b>Base Cost:</b>')
local upgradesFrom = p._GetBuildingDowngrade(building)
local upgradesFrom = p._getBuildingDowngrade(building)
if upgradesFrom ~= nil then
if upgradesFrom ~= nil then
table.insert(ret, '<br>'..Icons.Icon({upgradesFrom.name, type='building'}))
table.insert(ret, '<br>'..Icons.Icon({upgradesFrom.name, type='building'}))
Line 500: Line 579:
if #upgradesTo > 0 then
if #upgradesTo > 0 then
return upgradesTo[1]
return upgradesTo[1]
end
return nil
end
-- Given a building, find the building's downgrade
function p._GetBuildingDowngrade(building)
if building.upgradesFrom ~= nil then
return p._GetBuildingByID(building.upgradesFrom)
end
end
return nil
return nil
Line 557: Line 628:
local baseBuilding = building
local baseBuilding = building
while true do
while true do
local previousBuilding = p._GetBuildingDowngrade(baseBuilding)
local previousBuilding = p._getBuildingDowngrade(baseBuilding)
if previousBuilding ~= nil then
if previousBuilding ~= nil then
baseBuilding = previousBuilding
baseBuilding = previousBuilding
Line 679: Line 750:
return table.concat(ret)
return table.concat(ret)
--]=]]
--]=]]
end
-- Generates a table showing which buildings can be built in which biomes
-- Skips upgraded buildings
function p.GetBuildingBiomeTable()
-- Setup the table
local ret = {}
table.insert(ret, '{| class="wikitable sortable" style="text-align:center"')
table.insert(ret, '\n!Building')
-- Make a biomeModifiers table that will keep track of the bonus of each building
-- At the same time, make the output table header
local biomesMaster = {}
for _, biome in ipairs(Township.biomes) do
table.insert(ret, '\n!'..Icons.Icon({biome.name, type='biome', notext=true, nolink=true})..'<br>'..biome.name)
biomesMaster[biome.id] = false
end
for _, _building in ipairs(p._SortedBuildings()) do
-- Fix melvorF:Statues
local building = p._GetBuildingByID(_building.id)
-- Skip upgraded buildings
local downgrade = p._GetBuildingDowngrade(building)
if downgrade == nil then
-- Let's populate the biome habitability data
local buildingBiomes = Shared.clone(biomesMaster)
-- Set all valid biomes to 0
for _, biomeid in ipairs(building.biomes) do
buildingBiomes[biomeid] = true
end
-- Let's build the row
table.insert(ret, '\n|-')
table.insert(ret, '\n!data-sort-value="'..building.name..'" style="text-align:left"|'..Icons.Icon({building.name, type='building'}))
for _, biome in ipairs(Township.biomes) do
if buildingBiomes[biome.id] then
-- Buildable
table.insert(ret, '\n|class="table-positive"|✓')
else
-- Invalid biome
table.insert(ret, '\n|style="border:0px"|')
end
end
end
end
table.insert(ret, '\n|}')
return table.concat(ret)
end
end