572
edits
No edit summary |
(Pull out functions from GetBuildingTable into helper functions) |
||
Line 84: | Line 84: | ||
return resources | return resources | ||
end | end | ||
p.resources = p._ResourcesData() | |||
-- Returns a list of all the Township resources along with the Trader's trade ratios | -- Returns a list of all the Township resources along with the Trader's trade ratios | ||
function p._TraderData() | function p._TraderData() | ||
-- Get the list of resources | -- Get the list of resources. We get a copy instead of directly using p.resources because we are going to modify the table | ||
local resources = p._ResourcesData() | local resources = p._ResourcesData() | ||
Line 316: | Line 317: | ||
return Township.populationForTier[tier] | return Township.populationForTier[tier] | ||
end | end | ||
-- Returns a string containing the Township level and population requirements for a tier | |||
function p._GetTierText(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})..' '..tier.population..'</li></ul>' | |||
end | |||
-- Gets a building and prepares all the relevant stats for the building | -- Gets a building and prepares all the relevant stats for the building | ||
Line 321: | Line 329: | ||
local name = frame.args ~= nil and frame.args[1] or frame | local name = frame.args ~= nil and frame.args[1] or frame | ||
local building = Shared.clone(p._GetBuildingByName(name)) | local building = Shared.clone(p._GetBuildingByName(name)) | ||
local ret = {} | local ret = {} | ||
Line 335: | Line 342: | ||
table.insert(ret, '\r\n|-\r\n| <b>Type:</b> '..building.type) | table.insert(ret, '\r\n|-\r\n| <b>Type:</b> '..building.type) | ||
-- Tier | -- Tier | ||
local tier = p. | local tier = p._GetTierText(building.tier) | ||
table.insert(ret, | table.insert(ret, tier) | ||
-- 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) | |||
if upgradesFrom ~= nil then | |||
table.insert(ret, '<br>'..Icons.Icon({ | table.insert(ret, '<br>'..Icons.Icon({upgradesFrom.name, type='building'})..'<br>') | ||
end | end | ||
-- Cost | -- Cost | ||
local cost = | local cost = p._GetBuildingBaseCost(building) | ||
table.insert(ret, cost) | |||
table.insert(ret, | |||
-- Upgrades To | -- Upgrades To | ||
local | local upgradesTo = p._GetBuildingIDUpgrade(building.id) | ||
if upgradesTo ~= nil then | |||
table.insert(ret, '\r\n|-\r\n| <b>Upgrades To:</b>') | |||
table.insert(ret, '<br>'..Icons.Icon({upgradesTo.name, type='building'})) | |||
if | local upgrade_cost = p._GetBuildingBaseCost(upgradesTo) | ||
table.insert(ret, '\r\n|-\r\n| <b>Upgrades To:</b><br>'..Icons.Icon({upgradesTo | table.insert(ret, '<br>'..upgrade_cost) | ||
local upgrade_cost = | |||
table.insert(ret, '<br>'.. | |||
end | end | ||
Line 466: | Line 462: | ||
end | end | ||
return nil | return nil | ||
end | |||
-- Given a building id, find the next building upgrade | |||
function p._GetBuildingIDUpgrade(buildingid) | |||
local function checkFunc(entity) | |||
return entity.upgradesFrom ~= nil and entity.upgradesFrom == buildingid | |||
end | |||
local upgradesTo = GameData.getEntities(Township.buildings, checkFunc) | |||
if #upgradesTo > 0 then | |||
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 | |||
return nil | |||
end | |||
-- Given a building, find the base resource cost | |||
function p._GetBuildingBaseCost(building, _join) | |||
local join = _join ~= nil and _join or ', ' | |||
local cost = {} | |||
for _, resource in ipairs(upgradesTo.cost) do | |||
local resource_data = p._GetResourceByID(resource.id) | |||
table.insert(upgrade_cost, Icons.Icon({resource_data.name, type='resource', notext=true})..' '..resource.quantity) | |||
end | |||
table.insert(ret, '<br>'..table.concat(upgrade_cost, join)) | |||
end | |||
-- Gets a resource from id | |||
function p._GetResourceByID(id) | |||
return GameData.getEntityByID(p.resources, id) | |||
end | end | ||
return p | return p |
edits