# Remake of Caesar III: a mathematical model of the city

I remember playing Caesar 3 for the first time, it is an amazingly smart and balanced game, it creates the feeling that the city lives its life after the mission is completed. You can spend hours observing the city and not interfere in its life: plebeians will run around the city in search of work, and patricians will complain about poor living conditions, traders, schoolchildren, boats, priests - this world freezes only in minutes of pauses, giving the player the opportunity think over the next step. Exploring the internal algorithms of the game, I can’t stop wondering how accurately the authors put together pieces of a mosaic called “balance”. During the time spent on restoring the original game code, enough material has been accumulated on the game macromechanics, which I want to share with the habrasociety.

#### Different approaches to modeling the bustle of the city

It all starts with the prestige of the earth, this is one of the layers of the city that the game operates to calculate migration parameters and the level of houses. In addition to the base layers, the relationship of which is shown below, each layer can be further divided into parts.

In the city, people live in houses, the number of people and the quality of housing varies with each level, at each level a number of parameters are prescribed, upon reaching which it goes to the next. As I have already said, the prestige of the land serves as a basic parameter, the food supply is another basic parameter: in a small city this may not be noticeable, but in large cities the remoteness of barns and warehouses greatly affects the placement of residential areas on the map. Despite the fact that the market negatively affects the surrounding territory and the priority of merchants is shifted towards the supply of food to the population, a situation often arises when, due to a lack of food, a shortage of other goods, especially the most valuable (for the development of houses) pottery and furniture. Each next level of the house requires more expensive services and resources.

At a certain stage of development, houses turn into villas and significantly improve the rating of the city, but reduce the number of workers in the city: patricians do not work, which in turn leads to a sharp decrease in the level of production in the city and the mass exodus of residents, and if at some point in the game skip these “population swings”, they often lead to a losing mission. You can invite new settlers to the city, but with the advent of huts, well-to-do residents begin to flee from the city and the prosperity rating decreases.

The base layers of the city may contain additional sublayers that are responsible for a specific parameter or the provided service. For example, health care can be divided into general (doctors and baths) and specialized, which is required by the patricians (clinic), but also replaces the functions of the general: for example, the clinic provides the services of surgeons and doctors, but the cost of maintenance is much higher than that of a doctor’s house. With entertainment, it’s the same, except that entertainment buildings require “students”: for the theater, actors are needed, for the coliseum, gladiators and lions. It seems to be a small detail, but such an unobtrusive micro-control greatly affects the layout of the city in the limited area of ​​buildings.

Having decided on the components of "urban life", you can make a mathematical model of the city. I note that the original game uses a probabilistic-statistical model of the city, which means that at certain points in time (game day, week, month and year) a picture of the city’s parameters is taken and based on these data and taking into account previous states, the probability of the occurrence of the event is calculated. A simplified description of the model is shown below. Red indicates the negative influence of one object on another, green positive. The positive side of the statistical model is that when calculating the probability of occurrence of an event, it is not necessary to take into account the parameters of an individual object, we consider the “average temperature in the hospital”, this significantly reduces the load in the calculations (I recall that the game was run on computers with RAM < 32Mb). Buildings, although they have their own characteristics, in most cases they are involved in calculating the averaged parameters, and not for calculating the situation for a particular building.

In the remake of the game from the very beginning, a dynamic model of the city was laid down, a similar one can be seen in the Tropico game, where each object of the game has a set of its own characteristics, which it uses to select the behavior of the object at a particular moment in time. The set of connections in this case is more complicated and depends on the specific object, for example, the parameters of a house depend on the number of people living, the number of people working in this house, the availability of products and goods, as well as the means to buy them on the market. In the picture you can see a simplified model for calculating the parameters of the house.

Since the calculation of the probability of occurrence of an event is transferred from the city level to the object level, additional relationships must be entered for the interaction between the objects, in the remake their role is performed by “service providers” (porters, attendant, market vendor, priest, schoolchildren, etc. .). Accordingly, the model of behavior of objects in the game changes, most of the interaction between which occurs through an intermediary.

Using a dynamic model greatly complicates the programming of the logic of individual objects, because it is necessary to take into account the impact and type of source, the positive side of such an organization is the flexibility to customize the behavior of both the source of the event and its processor. Any dynamic modeling is designed to evaluate non-stationary processes to answer the question: “What if?”. But what if: in the city there will come a famine with a large flow of settlers, how will an epidemic, fire or earthquake spread.

#### Differences from the original game

I was asked in PM many times whether I’m making a Caesar’s clone and where the decompiled sources went. It was the clone (the code that came after the reverse engineering) that I completed 90 percent, the project is going and the first mission is working. The sources were removed from the github at the request of the copyright holder. This is directly about Caesar’s clone, it is located in an even more “gray legal area”. As for the remake, CaesarIA is an independent open source game, an attempt to revive the game with more than 15 years of history from Impressions Games.

No, this is not Caesar, I use the ideas from this game, but all the code is written from scratch: logic, maps, search for the path and other things that you used to see in old Caesar, they are made with an eye on it, but they are different.
YesI use ripped resources for test builds, if you are tormented by the question of legality - you are free to choose a mode of working with original archives from an honestly purchased copy.

Having asked the hawkers for their opinions, I decided to try to legalize the remake in the way that I thought was available at the moment - to raise funds through crowdfunding, and in Russian “with a beat”. The choice fell on the Indiegogo.com site ( and yes, I know that many consider this site a dump for suspicious projects) Having talked with the artists on gamedev.ru and with my familiar designers, the average price for creating 3D models (and therefore textures) of buildings and people was determined: \$ 6k (102 building models + animation) for buildings, \$ 4k for people and surroundings. this will avoid claims of copyright holders and make it possible to distribute the game for both desktop computers and tablets running Android and iOS. At the moment, I have done everything that I planned with the game world by the fall of this year, but I am only one person and I hope that the help of other people will allow the game to find its own style. Now the project has reached a point where its further development is difficult without new textures, buildings, sounds and other materials.

What tasks are solved at the moment
This is a video of the second training mission, then the playing of game clips has not yet been done (this is a live gameplay as it is) the video is long, I just played the second mission:

1. Simulation of the city
1.1 fires and aging of buildings
1.2 all original buildings are implemented in the game, some functional needs to be improved
1.3 all layers of the city used in the original are implemented (training, health,
healthcare, taxes, etc.)
1.4 a tax collection system has been implemented based forums
2. gods and religion, the gods can be angry or happy
2.1 It is shortchanging the coverage area of the temples
3. The calculation of the distribution of water, goods, products and services
4. Made appearance disgruntled citizens and insurgents
5. It is easy to control AI Solda s, and its enemy
6. Planning and festivals
7. Request for the emperor and the trade between the towns
8. Animals and fishing
9. Birth, aging and death of citizens. Age groups
10. Dynamically updating paths at objects, avoiding obstacles
11. Story missions from the original (until the mission in Tingis)
12. Extraction of resources and production chains

Many other little things that had to be decided during the creation of the game