Module:Township: Difference between revisions

2,140 bytes removed ,  13 April 2023
(Partially) update for v1.1.2, requires a far more thorough rework following Township changes
(Added p.GetWorshipTable)
((Partially) update for v1.1.2, requires a far more thorough rework following Township changes)
Line 1: Line 1:
local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local GameData = require('Module:GameData')
local GameData = require('Module:Sandbox/GameData')
local Constants = require('Module:Constants')
local Constants = require('Module:Constants')


Line 388: Line 388:
for _, biomeid in ipairs(building.biomes) do
for _, biomeid in ipairs(building.biomes) do
local biomename = GameData.getEntityByID(Township.biomes, biomeid).name
local biomename = GameData.getEntityByID(Township.biomes, biomeid).name
-- Optional hidden bonus/penalty for building
table.insert(ret, '<br>'..Icons.Icon({biomename, type='biome', notext=true, nolink=true})..' <span>'..biomename..'</span>')
local modifier = nil
if #building.biomeModifiers > 0 then
modifier = GameData.getEntityByProperty(building.biomeModifiers, 'biomeID', biomeid)
end
if modifier ~= nil then
local color = modifier.value < 0 and 'red' or 'green'
local modifier_value = Shared.numStrWithSign(modifier.value)
table.insert(ret, '<br>'..Icons.Icon({biomename, type='biome', notext=true, nolink=true})..' <span style="color:'..color..'"><b>'..biomename..' ('..modifier_value..'%)</b></span>')
else
table.insert(ret, '<br>'..Icons.Icon({biomename, type='biome', notext=true, nolink=true})..' <span>'..biomename..'</span>')
end
end
end
Line 416: Line 405:
-- Gets a string displaying the base production of a building, or nil if no production
-- Gets a string displaying the base production of a building, or nil if no production
function p._GetBuildingBaseProduction(building)
function p._GetBuildingBaseProduction(building)
local production = Shared.clone(building.provides.resources)
-- TODO Fix always using first biome
local production = Shared.clone(building.provides[1].resources)
if #production == 0 then
if #production == 0 then
Line 426: Line 416:
local retProduction = {}
local retProduction = {}
local job = p._GetJobFromResource(resource.id)
local job = p._GetJobFromResource(resource.id)
local workers = GameData.getEntityByID(building.provides.workers, job).quantity
-- TODO Fix always using first biome
local workers = GameData.getEntityByID(building.provides[1].workers, job).quantity
-- Sourced from township.js -> Township.computeTownResourceGain()
-- Sourced from township.js -> Township.computeTownResourceGain()
local production = resource.quantity*100*(Township.tickLength/10)
local production = resource.quantity*100*(Township.tickLength/10)
Line 457: Line 448:
}
}
for key, stat in pairs(stats) do
for key, stat in pairs(stats) do
if building.provides[key] ~= nil and building.provides[key] ~= 0 then
-- TODO Fix always using first biome
local quantity = building.provides[key]
if building.provides[1][key] ~= nil and building.provides[1][key] ~= 0 then
local quantity = building.provides[1][key]
if quantity < 0 then
if quantity < 0 then
quantity = '<span style="color:red">'..quantity..'</span>'
quantity = '<span style="color:red">'..quantity..'</span>'
Line 497: Line 489:
local join = _join ~= nil and _join or ', '
local join = _join ~= nil and _join or ', '
local cost = {}
local cost = {}
for _, resource in ipairs(building.cost) do
-- TODO Cost can vary by biome, properly handle this rather than
-- always taking costs for the first biome
for _, resource in ipairs(building.cost[1].cost) do
local resource_data = p._GetResourceByID(resource.id)
local resource_data = p._GetResourceByID(resource.id)
table.insert(cost, Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;'..resource.quantity)
table.insert(cost, Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;'..resource.quantity)
Line 511: Line 505:
-- Gets text for only the biomes that have a modifier for a building
-- Gets text for only the biomes that have a modifier for a building
function p._GetBiomeModifiers(building)
function p._GetBiomeModifiers(building)
-- TODO Concept of biome modifiers doesn't exist anymore
return ''
--[==[
local biomeRet = {}
local biomeRet = {}
for _, biome in ipairs(building.biomeModifiers) do
for _, biome in ipairs(building.biomeModifiers) do
Line 522: Line 519:
end
end
return table.concat(biomeRet, '<br>')
return table.concat(biomeRet, '<br>')
--]==]
end
end


Line 607: Line 605:
end
end


local FREE_LAND = Township.sectionSize
-- TODO Fix
local FREE_LAND = Township.sectionSize or 0
-- Gets the cost of the current price of land
-- Gets the cost of the current price of land
-- Taken from township.js -> Township.getNextSectionCost
-- Taken from township.js -> Township.getNextSectionCost
Line 642: Line 641:
-- Returns a table showing the land cost of a town
-- Returns a table showing the land cost of a town
function p.GetLandCostTable()
function p.GetLandCostTable()
-- TODO Fix
return ''
--[[=[
local ret = {}
local ret = {}
table.insert(ret, '\r\n{| class="wikitable"')
table.insert(ret, '\r\n{| class="wikitable"')
Line 650: Line 652:
table.insert(ret, '\r\n|}')
table.insert(ret, '\r\n|}')
return table.concat(ret)
return table.concat(ret)
--]=]]
end
end


Line 657: Line 660:
-- Setup the table
-- Setup the table
local ret = {}
local ret = {}
table.insert(ret, '\r\n{| class="wikitable sortable" style="text-align:center"')
table.insert(ret, '{| class="wikitable sortable" style="text-align:center"')
table.insert(ret, '\r\n!Building')
table.insert(ret, '\n!Building')
-- Make a biomeModifiers table that will keep track of the bonus of each building
-- Make a biomeModifiers table that will keep track of the bonus of each building
-- At the same time, make the output table header
-- At the same time, make the output table header
local biomeModifiersMaster = {}
local biomesMaster = {}
for _, biome in ipairs(Township.biomes) do
for _, biome in ipairs(Township.biomes) do
table.insert(ret, '\r\n!'..Icons.Icon({biome.name, type='biome', notext=true, nolink=true})..'<br>'..biome.name)
table.insert(ret, '\n!'..Icons.Icon({biome.name, type='biome', notext=true, nolink=true})..'<br>'..biome.name)
biomeModifiersMaster[biome.id] = false
biomesMaster[biome.id] = false
end
end
Line 675: Line 678:
if downgrade == nil then
if downgrade == nil then
-- Let's populate the biome habitability data
-- Let's populate the biome habitability data
local biomeModifiers = Shared.clone(biomeModifiersMaster)
local buildingBiomes = Shared.clone(biomesMaster)
-- Set all valid biomes to 0
-- Set all valid biomes to 0
for _, biomeid in ipairs(building.biomes) do
for _, biomeid in ipairs(building.biomes) do
biomeModifiers[biomeid] = 0
buildingBiomes[biomeid] = true
end
-- Then add the biome modifier values
for _, biomeModifier in ipairs(building.biomeModifiers) do
biomeModifiers[biomeModifier.biomeID] = biomeModifier.value
end
end
-- Let's build the row
-- Let's build the row
table.insert(ret, '\r\n|-')
table.insert(ret, '\n|-')
table.insert(ret, '\r\n!data-sort-value="'..building.name..'" style="text-align:left"|'..Icons.Icon({building.name, type='building'}))
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
for _, biome in ipairs(Township.biomes) do
local modifier = biomeModifiers[biome.id]
if buildingBiomes[biome.id] then
if modifier then
-- Buildable
if modifier == 0 then
table.insert(ret, '\n|class="table-positive"|')
-- Buildable but no bonuses
table.insert(ret, '\r\n|class="table-na"|+0%')
else
-- Bonus or penalty
local class = modifier < 0 and 'table-negative' or 'table-positive'
local modifier_value = Shared.numStrWithSign(modifier)
table.insert(ret, '\r\n|class="'..class..'"|<b>'..modifier_value..'%</b>')
end
else
else
-- Invalid biome
-- Invalid biome
table.insert(ret, '\r\n|style="border:0px"|')
table.insert(ret, '\n|style="border:0px"|')
end
end
end
end
end
end
end
end
table.insert(ret, '\r\n|}')
table.insert(ret, '\n|}')
return table.concat(ret)
end
 
 
-- Generates a table showing all the maps and the number of biomes
-- Skips upgraded buildings
function p.GetMapTable()
-- Setup the table
local ret = {}
table.insert(ret, '\r\n{| class="wikitable sortable" style="text-align:center"')
table.insert(ret, '\r\n!Map')
-- Make two table that will keep track of the max/min amount of land for each biome
-- At the same time, make the output table header
local biomeMax = {}
local biomeMin = {}
for _, biome in ipairs(Township.biomes) do
table.insert(ret, '\r\n!'..Icons.Icon({biome.name, type='biome', notext=true, nolink=true})..'<br>'..biome.name)
biomeMax[biome.id] = -1
biomeMin[biome.id] = Township.maxTownSize + 1
end
-- Find the min and max amount for each biome
for _, map in ipairs(Township.maps) do
for _, biome in ipairs(map.biomes) do
biomeMax[biome.biomeID] = math.max(biomeMax[biome.biomeID], biome.count)
biomeMin[biome.biomeID] = math.min(biomeMin[biome.biomeID], biome.count)
end
end
-- Draw all the map rows
for _, map in ipairs(Township.maps) do
table.insert(ret, '\r\n|-')
table.insert(ret, '\r\n!style="text-align:left"|'..map.name)
for _, biome in ipairs(map.biomes) do
-- Color the cell if min or max value
local max = biomeMax[biome.biomeID]
local min = biomeMin[biome.biomeID]
local count = biome.count
local class = count == max and 'table-positive' or count == min and 'table-negative' or ''
-- Insert cell
table.insert(ret, '\r\n|class="'..class..'"|'..count)
end
end
table.insert(ret, '\r\n|}')
return table.concat(ret)
return table.concat(ret)
end
end
Line 825: Line 770:
function p.GetTaskTable(frame)
function p.GetTaskTable(frame)
local category = frame.args ~= nil and frame.args[1] or frame
local category = frame.args ~= nil and frame.args[1] or frame
local categoryname = GameData.getEntityByID(Township.taskCategories, category).name
local categoryData = GameData.getEntityByID(Township.taskCategories, category)
if categoryData == nil then
return Shared.printError('Invalid task category specified: ' .. (tostring(category) or 'nil'))
end
local categoryname = categoryData.name
local taskcount = 0
local taskcount = 0
Line 957: Line 906:
table.insert(ret, GetCheckpointCell(0))
table.insert(ret, GetCheckpointCell(0))
for _, worship in ipairs(Township.worships) do
for _, worship in ipairs(Township.worships) do
mw.logObject(worship.isHidden == false)
if worship.isHidden == false then
if worship.isHidden == false then
table.insert(ret, '\r\n|'..Constants.getModifiersText(worship.modifiers))
table.insert(ret, '\r\n|'..Constants.getModifiersText(worship.modifiers))