Data Driven City Life AI

How to design and implement a system which makes city lifelike

I have been following the problems CD Project Red is having with Cyberpunk 2077. I noticed that one of the problems is AI related, namely police officers, who seem to appear in a very illogical manner, even in locations where it would be highly improbable to encounter them. Also, their response time to crime events (done by player) is something that any police force would be envious. In normal life police arrive at crime location after a long period of time, if at all, and response time varies depending on priorities. But the problem does not limit itself only to police officers - at the launch time the whole city was kind of bland. The buildings are there and some NPCs, but it does not feel realistic nor full of life, as a city of that size should. And I do not accept any of those excuses that "we tried hard" or "we have never done anything like this". If a studio has a history like CDPR, they simply are not acceptable explanations.

Instead of developing a game with dynamic, living city, it seems that CDPR has designed and old-fashioned linear game with detailed quests, but the city is designed around and on top of them, almost as an afterthought. Basically it is the quests that run the city. Later they have tried to add dynamic features to city, but obviously these "last minute additions" leads to problems, because it is like building on mud - eventually it will start to slide.

There seems to be no overall structure which binds all the details and elements together. Events may seem illogical, NPCs do not seem to be doing anything worthwhile, city does not seem to response reasonably to player's action… in short, there is no feeling of real, living city, and that was something which CDPR talked about when marketing their CP 2077 project.

This gave me an idea to write about how I would design a City AI, which handles events and everyday life of large simulated city. Of course I do not claim that my approach in this matter could be better than something from the bright minds of a multiple award winning AAA game studio. After all, I am just a inexperienced and unsuccessful dabbler, while they have a legion of talented game developers with several decades of experience publishing flawless and bug-free games.

Advantages of Well-planned City AI

In a big project it is important that the groundwork is done properly. When groundwork is solid, then it is easier to build on top of that, add new pieces, and even change them - as long as the groundwork holds. We should start from ground up, and leave the decorative details, storylines and adventure plots for later. It seems that the design in CP 2007 has proceeded just the opposite - in wrong order. I cannot understand why, because there surely was no lack of source material. I have been a game master in tabletop Cyberpunk since the first edition, and second edition has plethora of sourcebooks published.

As a veteran of designing adventures for RPGs I find the city setting to be one of the most enjoyable settings to design narrative content. It offers a plethora of opportunities just by itself, and more you design the environment, even more ideas and possibilities will literally flood into your mind, just waiting to be used. Best thing about a city is that you cannot design it thoroughly, you will always have plenty of room to add new ideas during the game. Players do not see this, because their perspective to the city is limited. However, they will always get a feeling of real city, where there are lots of individual people living their lives, and also lots of adventure possibilities.

It does not matter whether the game itself is quest oriented or not, the city should be like a living city, because it is a very important part of game immersion. If the city seems illogical and shallow, it does not matter how detailed the quests are - actually, the deep details in quests just emphasize it how empty the game is outside the quests. If there had been a deep design of living Night City, implemented as game mechanics, then it would have easy to design quests of top of that - even in such way, that if player sidesteps from a quest, he just faces normal life in the city, but it still seems immersive and living.

In my opinion it is best to start with designing the world, or city in this case. You design its layout, prominent personalities, organisations, companies, power groups, laws etc. But most importantly you should also design how that world or city lives. What kind of events happen there, daily cycles etc. You start the design with general things, and then step by step add more details.

As an experienced AI  and simulation designer and programmer I know there is an excellent, easy and quite compact way to implement this as a Data Driven Citylife AI, which controls what kind of things player will encounter. This doesn't have to very complex, because in a single player game we only have to simulate a small part of the city in detail - namely the part that player is currently observing and its surroundings.

There is no need to simulate everything all the time, as long as the city is designed to follow logical and consistent rules that generate believable and immersive situations wherever player goes. This makes all AI functions easier to implement because the life and events in the city have been generalized as logical, consistent rules and data. Same applies to narrative storylines, adventures and quests. They can exploit the City AI to make the narrative more immersive and reactive. This is not rocket science but just common sense storytelling system, and that is why I don't understand how an experienced studio with lots of designers and developers can even fail in a task as simple as this.

Idea of System

In order to accomplish a light simulation of city life, first we have to define several concepts, which will be used for data. Although the core mechanism is rather simple, there will be lots of data, since this kind of AI is totally data driven. It is the data which defines the behavior of whole city, its areas, and its inhabitants. This kind of system works in any RPG setting, both in computer games and tabletop games. When fleshed out, this system could work easily in Cyberpunk RED or any scifi setting.

As a programmer I see this City System as a master AI which controls NPC AI, traffic system, media broadcasts and creates dynamic procedural content like characters and small events. It is an independent module, but it has to be able to work in unison with mission/quest system. Neither of them should totally override the other one, both systems should exist and work all the time, but depending on the situation the mission system might be mostly passive, or actively put events and NPC into play.

Thus it is very clear that we should design the city system first. When we have defined what normally happens in the city, it is easier to integrate narrative missions and adventures without destroying the flow of normal city events. Actually, this way we have the advantage of mixing the normal events with special adventure events, and get more flexibility in the narrative flow.

City Divided

Our assumption is that the actual city, its map, layout, general atmosphere and history is already available. What follows is just a system, which makes it easier to handle its intelligent simulation. It is like a set of AI behavior rules and data, that will instantly provide description of what is happening around player, wherever he may be. Before we are there, we have to break the city into pieces.

First you divide whole area into sections and subsections. Sections are large areas with similar activity, like industry, commercial, business, office, administrative, cheap housing, moderate housing, expensive housing, suburban, development, energy. These areas can be of any shape and size. They do not have to be of same size. Usually they would be the same as named city sections in real cities.

Each section is divided into arbitrary number of subsections which should be of similar size in all sections. These subsections are most important units in this city simulation, since they are used to track players movements, and simulate what happens around the player based on the data of each subsection. Since they are roughly the same size throughout the city, they make it easier to estimate distance and travel time calculations.

Time Units

Anyone who has lived in a city knows that the life you encounter there is not always similar. There are rush hours and quiet times. Some areas are quiet during the day, but full of activity during night, and vice versa. So we need appropriately descriptive time units - something that is general and not too accurate.

For most situations following units are sufficient

  • Day: Normal day, Weekend, Holiday

  • Time: Night, Dawn,Early Morning, Morning, Noon, Afternoon, Late afternoon, Evening

Those units and values give us a way to differentiate the behavior of the city as a data when we combine it with city sections and subsection. If necessary, we can also define certain special dates. But we need even more definitions than this.

Encounter Groups and Individuals

Now we should decide what player can encounter in this little world. People with different occupations, vendors, officials, police, gangs etc. One group should be General citizen, a catch-all group for ordinary citizens of area. We can go into more detail, and divide this group into several groups according to social status and income like

  • Poor Citizen

  • Worker

  • Middle-class

  • Teens

  • Children

  • Salaryman

  • Specialist

  • Executive

and what ever kind of basic citizens may live in this world. These are meant to be used as kind of window dressing. They do not need any special behavior, they are just going to work or home, shopping or just passing time. They are like those extras used in filming, people who make the scene look real. Outwards these NPCs look natural and any special NPCs will easily blend in the crowd, thus avoiding the usual problem where some character is so obviously "important clue for the mission", perhaps even highlighted. As a player I do not want clues offered on silver plate, I want to find them myself and use my brains for it. I do not see any point of playing a game where everything is already solved for me, and I just have to click on the spot pointed to me.

Now we should decide what special groups are relevant in our game. In a real city there would be hundreds of different occupations, so in this example I am only going through some of them. In modern, cyberpunk or science fiction environment we can imagine that at least following classes could be relevant; meaning that they can affect the choices player has, or provide random encounters.

  • Police (uniform, plain clothes, undercover)

  • Gangs

  • Organized criminals (bodyguards, henchmen, leader)

  • Urchins (pickpockets, muggers, bullies, hustlers, beggars)

  • Performers

  • Reporter

  • Fixer

  • Prostitute

  • Corporate person

  • Business person

  • Maintenance

  • Cleaning (window washer, carbage collector)

  • Courier

  • Celebrity

This list is not exhaustive, of course, these are just examples. In real game I would add much more, to cover all possibilities. Also, I would add several options to many classes. For example, a corporate person could be either some low ranking office clerk, or a high ranking department boss etc. Police officer could be a rookie or veteran, perhaps he is corrupt etc. Performers could be just annoying, or extremely good, drawing enthusiastic crowds.

If I need more background on any of these characters, I would create it with a character background generator I have made for this kind of situations. It creates a background history with events, family and relatives, profession, skills, possessions etc. Once created these characters may be stored and used again, later. Seeing a same NPC in another event would strengthen the illusion of a real world, even if it were just a random NPC.

Probability of Being There at Certain Time

When we make a  selection of classes like we did above, we should keep in mind that in an average, normal city most of the people are just average workers, trying to make their living in there. Not everyone is some kind of high level criminal or pumped up violent gang member. However, in some locations or area almost everyone could be a gang member of an affiliate.

As in any city anywhere there are totally different areas in the terms of demographics - meaning that you may usually encounter certain groups of people in one area, but rarely in some other area. These differences can be expressed as probabilities.

At this point we can define for each NPC class a probability to appear in a subsection of the city for certain day and time unit. Of course we have to define these probabilities for each every subsection, every day unit and every time unit, and their combination. At the same time we also define other values like how many persons there are representing that class, are they in groups, their outlook and mood.

  • Appearing probability

  • Quantity expressed as a function with minimum and maximum

  • Grouping as single individual, pairs, small groups, large groups etc.

  • Outlook as expression like stylish, shabby, poor, expensive, fashionable etc.

  • Mood as expression like busy, lazy, angry, joyful, relaxed, edgy etc.

Probablity of Doing Something

It is not enough to determine what people are on the scene, we should also determine what they are doing. Just like the actual probability of presence may change during the day, their actions may change. In the morning workers are probably heading towards their work, while in the afternoon they may go home, or go to some bar, shopping etc.

  • Chilling

  • Eating/Drinking

  • Socializing (conversation, debate, arguing)

  • Idling

  • Commuting (going to work, going home)

  • Shopping

  • Demonstrators

  • Street Performers

  • Hustling

  • Selling

Player does not always know what all those NPCs are really doing, but they should have a logical basis, which affects the selection of necessary animations and general movement. For example commuting and shopping both may use similar animations, but the pace and routes of the movement are different. A person heading to work or home will walk faster and most direct route possible through a mall, while someone shopping there probably walks slower, stopping now and then to look at something etc.

Again, all these differences in the behavior will make the whole scene more natural. Otherwise it could look like a place, where dozens of robotic persons walk monotonously in a hurry, giving an artificial feeling.

Atmosphere and Attitude

Usually, most areas have some kind of atmosphere, a first impression of what kind of area it is. This could also be an attitude, like an animosity or disdain towards people who are not part of the people native to that area.

We should define two specific things - a general atmosphere, which is something that anyone visiting the area would kind of sense after being there for a while. But there is the other side of the coin too - how does the residents of that area react to visitor, what is their attitude towards them. This usually depends on the class of the visitor, his profession, outlook etc.

To make it easier to handle, we would store data only about attitudes that are not neutral. Thus we could describe an area to have attitudes like

  • Hostile

  • Suspicious

  • Aversive

  • Wellcoming

  • Friendly

These are expressed as probabilities again, which means that the attitude will be different. This attitude factor only affects the behavior of NPCs who are residents of that area, work there or otherwise consider it their domain (e.g. gangs).

Regular Routes

Although the real city seems quite unpredictable it still follows certain patterns and mechanisms, but the outcome of those mechanisms is usually unpredictable. Above we have already defined most of those mechanisms, but not all of them. Usually modern cities have certain highly predictable, regular elements which function almost like a clockwork.

These regular elements bring order into the chaos, and strengthen the illusion of real city. Usually these are things like bus lines, trains, subways, trash trucks, and delivery trucks, but they could include regular police patrol routes and guard patrols. In gamedev terms, we can simulate regular routes with splines, and even define timetables with varying accuracy. These routes may define how various regularly trafficking units move on their route.

This way it makes more sense how e.g. police patrol will respond to some event. They don't just teleport behind your back. They are somewhere along their patrol route, and may respond quickly, or after half an hour, if they are behind heavy traffic. Or they may not come at all, because you are middle of an area which is highly hostile towards police officers.

Also, we have already checked from our data (defined above) what kind NPCs there are present in that subsection where player is, and thus we know whether there are police officers present on this subsection, and neighbouring subsections etc. If there are, they may respond very quickly.

Together these randomized patterns and regular mechanisms give a good balance of predictable and unpredictable events - just like a real city life - and even unpredictable events follow a certain logic and pattern.

Quests, Missions and Adventures

When we have defined the behavior of the city as data, we already have a good understanding how it will work during play. Now it is much more easier to design quests, missions and adventures in such way that they really fit in, and work well with normal events of city.

More importantly, we know that there could be multitude of ways the city reacts to player, they may be small, so called random events, or large pre-scripted  missions and adventures. However, now we have the possibility to avoid railroading the player along linear storylines, and instead give more freedom, since we know that there is always something happening around the player. NPCs are not faceless characters anymore, they have behavior and player may not even understand that some encounters were not part of pre-planned adventure.

Player may encounter a fixer, who is desperate to raise some cash and would sell exceptional piece of hardware for a low price, or player may encounter instead some con-man who just tries to rob the player - or he may not encounter them now, but next day. If larger missions and adventures are appropriately divided into smaller sequential pieces, it is easy to blend all these different levels together, so that they appear as on seamless narrative.

Learning and Evolving City

We have to remember that this is a game simulation, and its primary purpose is entertainment. There may be storyline or ad hoc events which change the city, perhaps even in big scale. City may evolve gradually, changing the demographics and layout, depending on time scale. Sudden catastrophes, like blockwide fires, large accidents, or large riots can happen, and they affect not only on their location but sometimes on several surrounding subsections.

So, it is clear that this City AI has to be able to react both small and large events. Also, it must learn to react to anything player may do. This will be the hardest part to define, but not impossible. Basically, most of this is just changing the data according to events. It may also involve introducing new NPC types, that did not exist in the beginning. However, this is beyond the scope of this article, and I just mention it to point out the dynamical nature of city in a game world.

Fleshing out and Linking

All we have done here is just a design concept. This is the point where the real work would begin. We should program the City AI mechanisms and develop all data structures. We should test that and watch out if we have included all details we need. After that we should link all that to other pre-existing game mechanism like NPC characters and their AI, animations, background generation, minor events, major events, missions etc.

If everything works properly, we should have a city that really feels alive, where small encounters blend with major adventures, enhancing them. Of course it would get repetitive eventually - depending on amount of work we would put in it - but within the average playing time it would feel more realistic than without these mechanisms, so I think it is worth all the work.