Economy: Production
Most strategy games feature an economic system that produces different resources for the player to spend on improving their circumstances.
This post looks at the mechanics used for the simple forms of producing resources. There are two main concepts at play:
- Resources: Gold, wood, ore, manpower, etc. Numbers that indicate how rich in some specific resource you are.
- Producer: Things that create resources, for example mines for resources or buildings that attract units.
Since this post is being written in the context of explaining the mechanics of Heroes of Might and Magic I, it will serve as a good example.
The kingdom overview in Heroes of Might and Magic shown below, gives a good overview of the production side of the economic system:
There are 7 resources, in 3 groups:
- Currency: Gold is the most common resource and is needed in every transaction. It is produced by castles (1000 per day) and towns (250 per day).
- Common: Wood and Ore are common resources. Produced by Lumber Mills and Ore Mines, which each produce 2 resources per day. Used in the construction of most buildings.
- Rare: Sulphur, Mercury, Crystal and Gems are rare resources. Their mines only produce 1 resource per day. Used in the construction of advanced buildings and for purchasing some of the the most advanced units.
There is one additional wrinkle introduced by Heroes of Might and Magic - it has the concept of creature dwellings that can be built inside your castles and towns. These dwellings produce additional creatures each week, which can then be recruited into the player’s armies. The screen shown below summarizes what creatures are available in a castle and how many are produced per week:
The economy in this game is quite simple, in the sense that simply owning a settlement or mine is enough for it to start producing resources.
ECS Design
Components
Resource
- amount: Integer
- type: Class
This component is used to track the balance of a type of resource owned by an entity.
Producer
- amount: Integer
- type: Class
The amount indicates how much of a resource is produced.
The type is type(GoldResource)
or type(PeasantResource)
or whatever specific resource types your game has.
Controllable
From Control - tells us who controls a mine.
TimedEvent
From the Time system. Use this to define how frequently a Producer produces.
EventTrigger
From the Time system. Indicates that a timed event has triggered - so produce now.
Entities
The player will have a GoldResource plus a similar component for each type of player-owned resource in the game.
Buildings like the castle, town or mines would have Controllable, Producer, TimedEvent (frequency=1) and GoldResource to produce gold every day.
Buildings like the peasant dwelling in a castle, would have Controllable (owner=building entity itself), Producer, TimedEvent (frequency=7) and PeasantResource to produce peasants every week.
Systems
ProductionSystem
This system is responsible for producing resources when enough time has passed for an EventTrigger to be received.
producers = world.get_entities_with_components(Controllable, Producer, EventTrigger)
for entity in producers:
if entity.controllable.owner is None:
continue
owner = entity.controllable.owner
producer = entity.producer
resource = find_resource(owner, producer.type)
if resource is None:
continue
resource.amount += producer.amount
world.remove_component(entity, EventTrigger)