Build a prosperous settlement in a unique simulation of a feudal economy - each family runs their shop, owns their goods, and trades with other villagers in a free market. You will manage the land, build houses, organise events, set taxes, and balance supply and demand while engaging in diplomacy!
In the early days of Lords and Villeins pre-production, I observed that most city-builders tend to simplify their economy into a few abstract numbers. The settlement just has 100 piles of wood "somewhere" and people can spend them instantly at an infinite distance. Only games like Banished, Dwarf Fortress, or Rimworld would dare to put them in a physical world, but even then all resources were universally shared and not really owned by anyone.
In this, I saw a potential for a different kind of city-building game. One where resources are not only physically present in the world but also privately owned. One where people can not simply take what is not theirs without consequences - including the player. And so the defining direction of Lords and Villeins was set and our team embarked on a journey of solving what turned out to be an incredibly difficult problem.
[img]{STEAM_CLAN_IMAGE}/37633948/90e9f4a96303e8b064ff8d70a5725eac115795a7.png[/img]
Before we start designing any kind of simulation, it is very useful to define, what kind of behavior we are looking for. Is the goal to be realistic to draw upon observations of macro and microeconomics? If not, in what ways does it differ from reality? We had to ask ourselves this question nearly every day of development so I am not going to be able to uncover everything here, but a few things stood out above all.
[b]The goal of our economy is maximizing the profit of every entity.[/b] This is similar to what AI simulations sometimes call the utility function, but it is important to realize that making more money in this game is not guaranteed to maximize "happiness" (which is what utility functions are typically aiming for). In fact, in many cases, it goes against the survivalist intuition to prioritize immediate profits. While there is no right or wrong approach, this was a direction we chose to go for since survival can be rectified by players more easily than profits.
[b]Our economy is fully logical.[/b] This is a key distinction from real-life economies that often display phenomenons stemming from shortcomings of human psychology. Supply and demand are calculated exactly and precisely, with perfect information and without personal bias - something that is impossible in the real world. This should be seen as a feature, rather than a bug.
[b]Logical does not mean perfect.[/b] Players rightfully point out many scenarios when villagers are "dumb" and engage in actions that seemingly cause them to lose money or even die. Achieving a perfect simulation would require an incredible level of intelligence that is reaching the scope of highly advanced AI research centers, let alone for a team with one and a half of programmers. At the end of the day, we are making a game and we would like to reach a point where we can consider it "finished". In some places, we rather try to give players tools to manage the behavior of villagers so they can try to minimize the negative impacts of the imperfect AI.
Now that we have the goals more clearly defined, let's unpack what our simulation is doing. We can distinguish between three main elements of the economy: the villager, their family, and the market. On each level, we have a number of systems that together simulate the economy as a whole.
[img]{STEAM_CLAN_IMAGE}/37633948/cf7006cc3f8d15e0670e54a7deb7570ddd044942.png[/img]
A villager is the source of labor and thus their most direct impact on the economy comes from two places - their needs and productivity/efficiency. Concerning their needs is rather simple. They need some food, clothing, and tools, they need to get paid if they are employed in the demesne and occasionally they spend money at the inn or in the theater. All we have to do is aggregate those values for every individual and take them into account on a higher level (family economics).
On the other hand, their productivity is a different beast. The AI that runs their brain must be able to recognize a variety of contexts and make minute-to-minute decisions that lead to the most optimal outcome of their day. For this, we have developed a three-fold system: the priority solver, the goal planner, and the execution routine.
[b]The priority solver[/b] evaluates the priority value for every possible goal that a given villager could possibly try to achieve. From simple goals like reducing hunger or gaining energy to complex goals like producing a material, shopping on the market, managing storage, or cleaning the house. Most goals can be quickly discarded as impossible (i.e. there is no need to plan for visiting a graveyard when none of the family members are buried there). The rest gets assigned a priority number. This number is typically fixed to reflect its importance relative to other goals but can be dynamically changed in certain contexts (i.e. producing resources becomes all the less important the more people are already producing it. Or cleaning the house becomes far more important if the villager is assigned the role of a household cleaner by a different layer of the AI).
[b]The goal planner[/b] takes the latest snapshot of the priority queue and attempts to solve plans - a series of steps to achieve the goal in the game world - for each goal from the highest priority to the lowest. The planning process can test for specific conditions of each step and it is also responsible for solving what we call the resource flow - a mathematical set of transactions that validate if the villager has access to enough resources to execute the whole plan, and where would they come from. Once a planner finds a viable plan for any goal, it gives the plan to the execution routine. From there on, it only evaluates plans for goals, that have a higher priority than the one currently executed, and if it manages to find a plan for any of those, it may force the villager to interrupt their current plan and change their focus. To make sure that this happens only in very necessary scenarios, we are greatly increasing the priority of goals that are currently executed so that only the highly reactive or immediately serious goals have the capability to override them.
[b]The execution routine[/b] deals with the specifics of the real world. It calls the pathfinder to calculate a path to all targets that need to be reached ([url=https://store.steampowered.com/news/app/1287530/view/3657540278448387075]see our previous pathfinding article about that here[/url]). It handles the reservation of resources and locations so they are not blocked or taken by other villagers before they can reach them (in essence secretly communicating with others over an infinite distance) and it handles the execution of specific tasks along the way.
[img]{STEAM_CLAN_IMAGE}/37633948/1f6400e74f7996e009c2369450538008983c10d2.png[/img]
In terms of the economy simulation, a family is equivalent to a business. For every villager in it, we aggregate their consumption and production for each day. From there, we add some abstract information and predictions - given the history of their production, the size of the family, and current priorities, we try to predict how much of each resource they can produce the next day and generate additional demand for an appropriate amount of material that they will need to produce them. Here we also evaluate the availability of construction materials and generate orders for construction services by carpenters and architects.
So a family unit is largely just an aggregate for demand of material and consumer goods. More importantly, though, it is on this level where we determine, how much of the current stock of each family will be offered on sale. This is done by fitting a statistical model with a history of produce and sales for the most recent 60 days. The outcome is a predictor that dictates, how much of a current stock, after accounting for their taxes and other existing needs, should be offered on sale. This way, the AI has the ability to anticipate lows and highs across the varying seasons and maintain higher stocks in preparation for seasons with lower produce without oversupplying the market and plummeting resource prices.
This model starts with a generalized dataset. Over time as the game collects real data, it adapts and optimizes its decision to the existing situation. A similar approach is also used for generating demand for material. We start with a base assumption and adjust it based on a monitored history of the past few days.
The AI always plays a game of catch-up. Tuning the parameters too much can make it overreact to small fluctuations. Making it less sensitive can make it stubborn and ignore significant outliers. This relationship is a large source of its inefficiencies and no matter how well we tune it, they will always be present. But assuming a stable environment, it should lead to optimal behavior.
All families also aim to slightly oversupply the market to create some reserves for a so-called ad-hoc demand - that is a demand generated dynamically by villagers during the day, which was not accounted for in the morning. The ideal outcome is to offer a certain amount of resources so that on average, they are only left with a small percentage at the end of the day remaining unsold.
The family level is also where the player has the biggest impact, as their warehouse can be a significant source of supply thus creating an indirect market force. The taxation policies can also limit the supply provided by others and since the addition of automated trading policies, the player can also generate their own demand on the local market.
[img]{STEAM_CLAN_IMAGE}/37633948/ec00e705dd774e7cd918eea609773796ced57e38.png[/img]
The level of the market is concerned with two main things - market prices and the import and export economy.
Pricing is a very simplified simulation of supply and demand with no inflation. For every resource, we are setting a so-called standard price. This is the price the game always starts with. Then for every day, we aggregate the supply of all families for a given resource, and we monitor the percentage that was sold.
Remember that the ideal outcome here is that every resource is slightly oversupplied. We look at the real percentage and depending on how close we are to the ideal scenario, we conclude that the resource is either balanced, oversupplied (large % of offered resources remains unsold), or under-supplied (when all or nearly all offered resources were sold).
If it is oversupplied, we will slightly reduce its price up to a certain minimum. If it is under-supplied, we will increase its price. And if it is balanced, we will move the price closer back to its standard price (so increase or decrease depending on where it currently sits). This means there is never inflation, and the dynamic prices only act so far as market stagnation and disproportions in supply are forcing them to. As long as this is maintained and the prices remain close to their standard values, all families should (in theory) be able to produce all resources and generate profit on every produced item.
The reason why that only works in theory is because there is one major problem - it is practically impossible to achieve perfect balance on the hundreds of resources that the game generates, in a population of hundreds of people.
Even more so, if a family is too overwhelmed with labor and is unable to reach storefronts on time, the price can go down even if the demand practically exists. However, since an overwhelmed family that is unable to spend their money is a demand without a meaningful impact, reducing their price to attract caravans that can spend their money is a correct behavior.
Caravans give us the ability to buy nearly infinite amounts of resources and provide (far more limited amounts of) supplies that the settlement can not reliably generate. In some cases, the reliance on caravans can be quite high and the amount of supplies they would bring to keep the economy balanced would cross far beyond a comfortable level of immersion, so we keep their impact somewhat limited. To a large extent, players can adapt to this situation and find ways to grow their economies in more stable ways, especially with the addition of favor that alleviates the randomness of generated professions. However, it still leaves us with a problem of cash flow.
[img]{STEAM_CLAN_IMAGE}/37633948/5a7489eb822a13484f429e7e3ae85ad3b47326d8.png[/img]
What happens, if caravans consistently supply far more, than they buy from the market? Eventually, all gold disappears from the economy and villagers will lose the ability to trade. In fact, we do not even need the caravans for this to happen. Certain professions are naturally far more universal than others. Farmers, hunters, foragers, innkeepers, and builders tend to be largely profitable because their services are so fundamental. And over time, their pockets grow rich, and the wealth gap in our little society is slowly but surely increasing.
We could keep increasing the complexity of the simulation and introduce inflation to alleviate this problem further, but this is the point where we decided to opt out and leave the problem to balancing and player input. On the balancing side, we try to improve the bottlenecks of the production chain and the pricing gaps between higher-level resources to make it easier for other professions to generate disproportionate profits with lower volumes of sales.
On the side of the player input, we give options to utilize high rents on households, the embargo on caravan trade, and find ways to circulate the money back into the economy. This is arguably where we still have some work to do and we are currently testing a new balancing patch, which went through significant efforts to improve these values, but even after years of trying, we are inevitably facing the conclusion that this is always going to happen, and we have to embrace it.
[img]{STEAM_CLAN_IMAGE}/37633948/e0b275ad4abbc98402de98f539d7f9610fe618cf.png[/img]
I tried to keep it simple and abstract enough so that you can get a general understanding of the systems that drive the economy of Lords and Villeins. Even though it ends up still being quite a long article, I hope you found it insightful and enjoyable to read.
As a small side note, this simulation also made Lords and Villeins highly historically inaccurate. Feudal economies were far more close knit, intertwined with exchange trades, publicly rented utilities and so on. Trying to keep it accurate would explode the scope of our game beyond anything we could do, so the decision was made to also abandon the historical accuracy in those aspects and simply deliver an interesting game while still keeping its medieval theme and inspiration in the production processes of goods.
I would love to hear your thoughts! What parts of the economy simulation do you find most enjoyable? Which parts tend to be frustrating or repetitive? There is always room to grow, but as you can see, with so many moving parts, our work would never be done.
I truly believe that this approach to a city-builder is very unique, but perhaps also quite niche. Over the years, I have learned that certain problems will never go away and while I do my best to please as many of our players as possible, this is the game we set out to do and I remain committed to its vision, despite its shortcomings. I simply hope that after years of improvements, what we arrived at can satisfy your cravings for a decent economy simulation and get you excited to discover the endless fun in the city-building sandbox of Lords and Villeins.
Truth be told, I was not fully aware, that its weaknesses are also its biggest strengths, and in the past, we were a little bit shy in marketing Lords and Villeins as such. Rather than highlighting its economy, we focused on the society and medieval aspect of the game and maybe we created the wrong expectation. You might have noticed that our store description is now quite different, as we learned from this mistake and tried to change our approach. I hope this article serves to celebrate the long journey we have had.
Thank you for staying with us and supporting us along the way. And stick around, because more exciting news is coming!
Michal
Honestly Games