Module:Township: Difference between revisions

Resource icons and stats are now accessed via Icons.Icon. Added GetBuildingTable
No edit summary
(Resource icons and stats are now accessed via Icons.Icon. Added GetBuildingTable)
Line 2: Line 2:
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local GameData = require('Module:GameData')
local GameData = require('Module:GameData')
local Constants = require('Module:Constants')




local resource_data = {
 
['melvorF:GP'] = {icon = {'Coins'}, skill = nil},
local Township = GameData.getSkillData('melvorD:Township')
['melvorF:Food'] = {icon = {'Raw Beef', type='item'}, skill = 'melvorD:Cooking'},
['melvorF:Wood'] = {icon = {'Wood', type='resource'}, skill = 'melvorD:Woodcutting'},
['melvorF:Stone'] = {icon = {'Stone', type='resource'}, skill = 'melvorD:Mining'},
['melvorF:Ore'] = {icon = {'Iron', type='rock'}, skill = 'melvorD:Mining'},
['melvorF:Coal'] = {icon = {'Coal', type='resource'}, skill = 'melvorD:Mining'},
['melvorF:Bar'] = {icon = {'Iron Bar', type='item'}, skill = 'melvorD:Smithing'},
['melvorF:Herbs'] = {icon = {'Garum Herb', type='item'}, skill = 'melvorD:Farming'},
['melvorF:Rune_Essence'] = {icon = {'Rune Essence', type='item'}, skill = 'melvorD:Mining'},
['melvorF:Leather'] = {icon = {'Leather', type='item'}, skill = nil},
['melvorF:Potions'] = {icon = {'Potion', type='resource'}, skill = 'melvorD:Herblore'},
['melvorF:Planks'] = {icon = {'Planks', type='resource'}, skill = 'melvorD:Woodcutting'},
['melvorF:Clothing'] = {icon = {'Leather Body', type='item'}, skill = 'melvorD:Crafting'}
}


local p = {}
local p = {}
Line 91: Line 79:
-- Get a sorted list of all the resources
-- Get a sorted list of all the resources
local Township = GameData.getSkillData('melvorD:Township')
local resources = GameData.sortByOrderTable(Township.resources, Township.resourceDisplayOrder[1].ids)
local resources = GameData.sortByOrderTable(Township.resources, Township.resourceDisplayOrder[1].ids)
resources = Shared.clone(resources)
resources = Shared.clone(resources)
Line 273: Line 260:
table.insert(ret_resource, '\r\n|style="text-align:center" data-sort-value="' .. item.toTownship .. '"|'..Icons.Icon({item.name, type='item', notext=true})..' '..Shared.formatnum(item.toTownship))
table.insert(ret_resource, '\r\n|style="text-align:center" data-sort-value="' .. item.toTownship .. '"|'..Icons.Icon({item.name, type='item', notext=true})..' '..Shared.formatnum(item.toTownship))
-- Take From
-- Take From
table.insert(ret_resource, '\r\n|style="text-align:center" data-sort-value="' .. item.fromTownship .. '"|'..Icons.Icon(p._GetResourceIcon(resource.id))..' '..Shared.formatnum(item.fromTownship))
table.insert(ret_resource, '\r\n|style="text-align:center" data-sort-value="' .. item.fromTownship .. '"|'..Icons.Icon({resource.name, type='resource', notext=true})..' '..Shared.formatnum(item.fromTownship))
-- Value
-- Value
table.insert(ret_resource, '\r\n|style="text-align:center" data-sort-value="' .. item.sellsFor .. '"|'..Icons.GP(item.sellsFor))
table.insert(ret_resource, '\r\n|style="text-align:center" data-sort-value="' .. item.sellsFor .. '"|'..Icons.GP(item.sellsFor))
Line 292: Line 279:
end
end
return table.concat(ret)
return table.concat(ret)
end
-- Gets the icon of a resource
-- e.g. Icons.Icon(_GetResourceIcon('melvorF:Bar'))
function p._GetResourceIcon(id)
local resource_icon = Shared.clone(resource_data[id].icon)
resource_icon.notext = true
resource_icon.nolink = true
return resource_icon
end
end


-- Gets the associated skill of a resource by id
-- Gets the associated skill of a resource by id
local resource_skill = {
['melvorF:GP'] = {skill = nil},
['melvorF:Food'] = {skill = 'melvorD:Cooking'},
['melvorF:Wood'] = {skill = 'melvorD:Woodcutting'},
['melvorF:Stone'] = {skill = 'melvorD:Mining'},
['melvorF:Ore'] = {skill = 'melvorD:Mining'},
['melvorF:Coal'] = {skill = 'melvorD:Mining'},
['melvorF:Bar'] = {skill = 'melvorD:Smithing'},
['melvorF:Herbs'] = {skill = 'melvorD:Farming'},
['melvorF:Rune_Essence'] = {skill = 'melvorD:Mining'},
['melvorF:Leather'] = {skill = nil},
['melvorF:Potions'] = {skill = 'melvorD:Herblore'},
['melvorF:Planks'] = {skill = 'melvorD:Woodcutting'},
['melvorF:Clothing'] = {skill = 'melvorD:Crafting'}
}
function p._GetResourceSkill(id)
function p._GetResourceSkill(id)
return resource_data[id].skill
return resource_skill[id].skill
end
end


-- Gets a Township building by ID, e.g. melvorF:Hunters_Cabin
-- Gets a Township building by ID, e.g. melvorF:Hunters_Cabin
function p._GetBuildingByID(id)
function p._GetBuildingByID(id)
GameData.getEntityByID(GameData.getSkillData('melvorD:Township').buildings, id)
return GameData.getEntityByID(Township.buildings, id)
end
 
-- Gets a Township building by name, e.g. Hunters Cabin
function p._GetBuildingByName(name)
return GameData.getEntityByName(Township.buildings, name)
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


-- Gets a building and prepares all the relevant stats for the building
-- Gets a building and prepares all the relevant stats for the building
function p._GetBuildingProfile(id)
function p.GetBuildingTable(frame)
local building = p._GetBuildingByID(id)
local name = frame.args ~= nil and frame.args[1] or frame
local building = Shared.clone(p._GetBuildingByName(name))
local resources = p._ResourcesData()
local ret = {}
 
-- Header
table.insert(ret, '\r\n{| class="wikitable infobox"')
-- Name
table.insert(ret, '\r\n|-\r\n!'..building.name)
-- Icon
table.insert(ret, '\r\n|-\r\n|style="text-align:center"|'..Icons.Icon({building.name, type='building', size='250', notext=true}))
-- ID
table.insert(ret, '\r\n|-\r\n| <b>Building ID:</b> '..building.id)
-- Type
table.insert(ret, '\r\n|-\r\n| <b>Type:</b> '..building.type)
-- Tier
local tier = p._GetTierRequirements(building.tier)
table.insert(ret, '\r\n|-\r\n| <b>Tier '..building.tier..'</b><ul><li>'..Icons._SkillReq('Township', tier.level, false)..'</li><li>'..Icons.Icon({'Population', type='township', notext=true})..'&nbsp;'..tier.population..'</li></ul>')
-- Upgrades From
table.insert(ret, '\r\n|-\r\n| <b>Base Cost:</b>')
if building.upgradesFrom ~= nil then
local upgradesFromBuilding = p._GetBuildingByID(building.upgradesFrom)
table.insert(ret, '<br>'..Icons.Icon({upgradesFromBuilding.name, type='building'})..'<br>')
end
-- Cost
local cost = {}
for _, resource in ipairs(building.cost) do
local resource_data = GameData.getEntityByID(resources, resource.id)
table.insert(cost, Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;'..resource.quantity)
end
table.insert(ret, table.concat(cost, ', '))
-- Upgrades To
local function checkFunc(entity)
return entity.upgradesFrom ~= nil and entity.upgradesFrom == building.id
end
local upgradesTo = GameData.getEntities(Township.buildings, checkFunc)
if #upgradesTo > 0 then
table.insert(ret, '\r\n|-\r\n| <b>Upgrades To:</b><br>'..Icons.Icon({upgradesTo[1].name, type='building'}))
-- Cost
local upgrade_cost = {}
for _, resource in ipairs(upgradesTo[1].cost) do
local resource_data = GameData.getEntityByID(resources, resource.id)
table.insert(upgrade_cost, Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;'..resource.quantity)
end
table.insert(ret, '<br>'..table.concat(upgrade_cost, ', '))
end
 
-- Fixed benefits
local benefits = {}
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})..'&nbsp;'..building.provides[key])
end
end
if #benefits > 0 then
table.insert(ret, '\r\n|-\r\n| <b>Provides:</b> '..table.concat(benefits, ', '))
end
 
-- Production
local production = p._GetBuildingBaseProduction(building)
if production ~= nil then
table.insert(ret, '\r\n|-\r\n| <b>Base Production per '..Icons.Icon({'Workers', type='township', notext=true})..':</b><br>')
table.insert(ret, production)
end
 
-- Modifiers
if building.modifiers ~= nil and not Shared.tableIsEmpty(building.modifiers) then
table.insert(ret, '\r\n|-\r\n| <b>Modifiers:</b>\r\n'..Constants.getModifiersText(building.modifiers, true))
end
-- Biomes
table.insert(ret, '\r\n|-\r\n| <b>Biomes:</b><br><ul>')
for _, biomeid in ipairs(building.biomes) do
local biomename = GameData.getEntityByID(Township.biomes, biomeid).name
-- Optional hidden bonus/penalty for building
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, '<li style="color:'..color..'"><b>'..biomename..' ('..modifier_value..'%)</b></li>')
else
table.insert(ret, '<li>'..biomename..'</li>')
end
end
-- End
table.insert(ret, '\r\n|}')
return table.concat(ret)
end
 
-- Given a resource id, return the job id
-- e.g. melvorF:Bar -> melvorF:Blacksmith
function p._GetJobFromResource(resource_id)
local job = GameData.getEntityByProperty(Township.jobs, 'produces', resource_id)
return job.id
end
 
-- Gets a string displaying the base production of a building
function p._GetBuildingBaseProduction(building)
local production = Shared.clone(building.provides.resources)
local resources = p._ResourcesData()
local resources = p._ResourcesData()
if #production == 0 then
return nil
end
local retResources = {}
for _, resource in ipairs(production) do
local retProduction = {}
local job = p._GetJobFromResource(resource.id)
local workers = GameData.getEntityByID(building.provides.workers, job).quantity
-- Sourced from township.js -> Township.computeTownResourceGain()
local production = resource.quantity*100*(Township.tickLength/10)
local resource_data = GameData.getEntityByID(resources, resource.id)
table.insert(retProduction, '<span style="color:green">'..Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;+'..production..'</span>')
if resource_data.requires ~= nil and #resource_data.requires > 0 then
for _, required_resource in ipairs(resource_data.requires) do
local demand = production*required_resource.quantity*100
local required_resource_data = GameData.getEntityByID(resources, resource.id)
table.insert(retProduction, '<span style="color:red">'..Icons.Icon({required_resource_data.name, type='resource', notext=true})..'&nbsp;-'..demand..'</span>')
end
end
retProduction = table.concat(retProduction, ', ')..'/t ('..Icons.Icon({'Workers', type='township', notext=true})..'&nbsp;'..workers..')'
table.insert(retResources, retProduction)
end
return table.concat(retResources, '<br>')
end
end
-- Testing
p.GameData = GameData
p.Icons = Icons
p.Township = Township


return p
return p
572

edits