572
edits
(Resource icons and stats are now accessed via Icons.Icon. Added GetBuildingTable) |
(Refactoring) |
||
Line 5: | Line 5: | ||
local p = {} | |||
local Township = GameData.getSkillData('melvorD:Township') | local Township = GameData.getSkillData('melvorD:Township') | ||
p.Township = Township | |||
-- Returns the namespace name (eventually we should use an icon) | -- Returns the namespace name (eventually we should use an icon) | ||
Line 87: | Line 87: | ||
-- 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 184: | Line 184: | ||
return resources | return resources | ||
end | end | ||
p.resources = p._TraderData() | |||
-- Builds the table of trader items | -- Builds the table of trader items | ||
function p.GetTraderTable(frame) | function p.GetTraderTable(frame) | ||
-- Get the resources data with associated trader data | -- Get the resources data with associated trader data | ||
-- Build the text | -- Build the text | ||
local ret = {} | local ret = {} | ||
for _, resource in ipairs(resources) do | for _, resource in ipairs(p.resources) do | ||
if #resource.itemConversions ~= 0 then -- Skips GP | if #resource.itemConversions ~= 0 then -- Skips GP | ||
local ret_resource = {} | local ret_resource = {} | ||
Line 316: | Line 316: | ||
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 328: | ||
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 341: | ||
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, '\r\n|-\r\n| | table.insert(ret, '\r\n|-\r\n| '..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'})) | ||
end | end | ||
-- Cost | -- Cost | ||
local cost = | local cost = p._GetBuildingBaseCost(building) | ||
table.insert(ret, '<br>'..cost) | |||
-- 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 | ||
-- Fixed benefits | -- Fixed benefits | ||
local benefits = | local benefits = p._GetBuildingBenefits(building) | ||
if benefits ~= nil then | |||
table.insert(ret, '\r\n|-\r\n| <b>Provides:</b> '..benefits) | |||
if | |||
table.insert(ret, '\r\n|-\r\n| <b>Provides:</b> '.. | |||
end | end | ||
Line 428: | Line 411: | ||
end | end | ||
-- Gets a string displaying the base production of a building | -- 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) | local production = Shared.clone(building.provides.resources) | ||
if #production == 0 then | if #production == 0 then | ||
Line 444: | Line 426: | ||
-- 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) | ||
local resource_data = | local resource_data = p._GetResourceByID(resource.id) | ||
table.insert(retProduction, '<span style="color:green">'..Icons.Icon({resource_data.name, type='resource', notext=true})..' +'..production..'</span>') | table.insert(retProduction, '<span style="color:green">'..Icons.Icon({resource_data.name, type='resource', notext=true})..' +'..production..'</span>') | ||
if resource_data.requires ~= nil and #resource_data.requires > 0 then | if resource_data.requires ~= nil and #resource_data.requires > 0 then | ||
for _, required_resource in ipairs(resource_data.requires) do | for _, required_resource in ipairs(resource_data.requires) do | ||
local demand = production*required_resource.quantity*100 | local demand = production*required_resource.quantity*100 | ||
local required_resource_data = | local required_resource_data = p._GetResourceByID(required_resource.id) | ||
table.insert(retProduction, '<span style="color:red">'..Icons.Icon({required_resource_data.name, type='resource', notext=true})..' -'..demand..'</span>') | table.insert(retProduction, '<span style="color:red">'..Icons.Icon({required_resource_data.name, type='resource', notext=true})..' -'..demand..'</span>') | ||
end | end | ||
Line 459: | Line 441: | ||
end | end | ||
-- | -- Gets a string displaying the building's benefits, or nil if no benefits | ||
p.GameData = | function p._GetBuildingBenefits(building) | ||
p. | local benefits = {} | ||
p. | local stats = { | ||
population = 'Population', | |||
happiness = 'Happiness', | |||
education = 'Education', | |||
storage = 'Storage', | |||
deadStorage = 'Dead Storage' | |||
} | |||
for key, stat in pairs(stats) do | |||
if building.provides[key] ~= nil and building.provides[key] ~= 0 then | |||
table.insert(benefits, Icons.Icon({stat, type='township', notext=true})..' '..building.provides[key]) | |||
end | |||
end | |||
if #benefits > 0 then | |||
return table.concat(benefits, ', ') | |||
end | |||
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(building.cost) do | |||
local resource_data = p._GetResourceByID(resource.id) | |||
table.insert(cost, Icons.Icon({resource_data.name, type='resource', notext=true})..' '..resource.quantity) | |||
end | |||
return table.concat(cost, join) | |||
end | |||
-- Gets a resource from id | |||
function p._GetResourceByID(id) | |||
return GameData.getEntityByID(p.resources, id) | |||
end | |||
return p | return p |
edits