Module:Township: Difference between revisions

Add land cost functions
(Refactoring)
(Add land cost functions)
Line 320: Line 320:
function p._GetTierText(tierlevel)
function p._GetTierText(tierlevel)
local tier = p._GetTierRequirements(tierlevel)
local tier = p._GetTierRequirements(tierlevel)
return '<b>Tier '..tierlevel..'</b><ul><li>'..Icons._SkillReq('Township', tier.level, false)..'</li><li>'..Icons.Icon({'Population', type='township', notext=true})..'&nbsp;'..tier.population..'</li></ul>'
return Icons._SkillReq('Township', tier.level, false)..'<br>'..Icons.Icon({'Population', type='township', notext=true})..'&nbsp;'..tier.population
end
end
Line 342: Line 342:
-- Tier
-- Tier
local tier = p._GetTierText(building.tier)
local tier = p._GetTierText(building.tier)
table.insert(ret, '\r\n|-\r\n| '..tier)
table.insert(ret, '\r\n|-\r\n| <b>Requirements:</b><br>'..tier)


-- Upgrades From
-- Upgrades From
Line 453: Line 453:
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
if building.provides[key] ~= nil and building.provides[key] ~= 0 then
table.insert(benefits, Icons.Icon({stat, type='township', notext=true})..'&nbsp;'..building.provides[key])
local quantity = building.provides[key]
if quantity < 0 then
quantity = '<span style="color:red">'..quantity..'</span>'
else
quantity = Shared.numStrWithSign(quantity)
end
table.insert(benefits, Icons.Icon({stat, type='township', notext=true})..'&nbsp;'..quantity)
end
end
end
end
Line 496: Line 502:
function p._GetResourceByID(id)
function p._GetResourceByID(id)
return GameData.getEntityByID(p.resources, id)
return GameData.getEntityByID(p.resources, id)
end
-- Gets text for only the biomes that have a modifier for a building
function p._GetBiomeModifiers(building)
local biomeRet = {}
for _, biome in ipairs(building.biomeModifiers) do
local biomename = GameData.getEntityByID(Township.biomes, biome.biomeID).name
local color = biome.value < 0 and 'red' or 'green'
local biome_value = Shared.numStrWithSign(biome.value)
table.insert(biomeRet, '<li style="color:'..color..'">'..biomename..' ('..biome_value..'%)</li>')
end
if #biomeRet == 0 then
return nil
end
return '<ul>'..table.concat(biomeRet)..'</ul>'
end
-- Returns an upgrade table of a building
function p.GetBuildingUpgradeTable(frame)
local buildingname = frame.args ~= nil and frame.args[1] or frame
local building = p._GetBuildingByName(buildingname)
-- Let's find the base building
local baseBuilding = building
while true do
local previousBuilding = p._GetBuildingDowngrade(baseBuilding)
if previousBuilding ~= nil then
baseBuilding = previousBuilding
else
break
end
end
-- Let's make a list of all the buildings
-- Return empty string if there is only 1 building in the upgrade chain (i.e. no upgrades/downgrades)
local buildingList = {}
local _curBuilding = baseBuilding
while true do
table.insert(buildingList, _curBuilding)
_curBuilding = p._GetBuildingIDUpgrade(_curBuilding.id)
if _curBuilding == nil then
break
end
end
if #buildingList == 1 then
return ''
end
local ret = {}
table.insert(ret, '\r\n== Upgrade Chart ==')
table.insert(ret, '\r\n{| class="wikitable"')
-- Name
table.insert(ret, '\r\n|- style="text-align:center" \r\n! Name')
for _, building in ipairs(buildingList) do
table.insert(ret, '\r\n!'..Icons.Icon({building.name, type='building'}))
end
-- Tier
table.insert(ret, '\r\n|-\r\n! Requirements')
for _, building in ipairs(buildingList) do
local tier = p._GetTierText(building.tier)
table.insert(ret, '\r\n|'..tier)
end
-- Cost
table.insert(ret, '\r\n|-\r\n! Cost')
for _, building in ipairs(buildingList) do
local cost = p._GetBuildingBaseCost(building)
table.insert(ret, '\r\n|'..cost)
end
-- Optional params
-- Generate a row
-- textFunc: returns nil if no data for a building, or else returns a string
local function BuildOptionalRow(header, textFunc)
local texts = {}
local hasTexts = false
for _, building in ipairs(buildingList) do
local text = textFunc(building)
hasTexts = hasTexts == true or text ~= nil
texts = texts ~= nil and texts or ''
table.insert(texts, text)
end
if hasTexts == true then
texts = table.concat(texts, '\r\n|')
table.insert(ret, header..texts)
end
end
BuildOptionalRow('\r\n|-\r\n! Benefits\r\n|', p._GetBuildingBenefits)
BuildOptionalRow('\r\n|-\r\n! Base Production per '..Icons.Icon({'Workers', type='township', notext=true})..'\r\n|', p._GetBuildingBaseProduction)
BuildOptionalRow('\r\n|-\r\n! Biome Production Modifiers\r\n|', p._GetBiomeModifiers)
-- End
table.insert(ret, '\r\n|}')
return table.concat(ret)
end
local FREE_LAND = Township.sectionSize
-- Gets the cost of the current price of land
-- Taken from township.js -> Township.getNextSectionCost
function p.GetLandCost(nthland)
-- First FREE_LAND plots of land are free
if nthland <= FREE_LAND then
return 0
end
return math.floor(15^(0.0100661358978*(nthland/32) + (nthland/32)^0.42))
end
-- Gets the cost to buy land until you have X amount of available land
-- Currently the max is 2048 land
function p.GetCumulativeLandCost(totalLand)
local cost = 0
while totalLand > FREE_LAND do
cost = cost + p.GetLandCost(totalLand)
totalLand = totalLand - 1
end
return cost
end
-- Returns a table showing the land cost of a town
function p.GetLandCostTable()
local ret = {}
table.insert(ret, '\r\n{| class="wikitable"')
table.insert(ret, '\r\n|- style="text-align:center" \r\n! Total Land \r\n! Single Land Cost \r\n! Total Cost')
for i=FREE_LAND,Township.maxTownSize,FREE_LAND do
table.insert(ret, '\r\n|-\r\n|'..i..'\r\n|'..Icons.GP(p.GetLandCost(i))..'\r\n|'..Icons.GP(p.GetCumulativeLandCost(i)))
end
table.insert(ret, '\r\n|}')
return table.concat(ret)
end
end


return p
return p
572

edits