How Traffic Works in Cities: Skylines

Quite often artificial intelligence is in control of so many elements of our favourite video games, but we seldom take notice and take it for granted. An AI tool or system can be running in plain sight, providing a critical element of the game that players are largely unaware of until it starts to go wrong. Let’s take a look at a fantastic example of this found in the popular city-building simulator Cities: Skylines. In this article, we’ll take a look at the traffic and navigation systems that run in every city, how citizens go about their day-to-day routine and how developers Colossal Order sought to address the traffic jams that can easily emerge.

AI and Games #54: How Traffic Works in Cities Skylines (YouTube)

About the Problem

Released in 2015 and published by Paradox Interactive, Cities: Skylines is an open-ended city-building simulator where players are responsible for many aspects of modern urban planning. This includes establishing city zoning, utilities such as water and electricity, establishing zones for residential, commercial and industrial growth, public services such as schools, fire services and law enforcement, all the taxation that is required to pay for it all and – critically for this episode – road placement and establishing transportation services within the region. Colossal Order, having achieved success with the Cities in Motion series had sought to expand into a full city building title, a sentiment that at that time Paradox Interactive did not share. However, 2013 saw a gap emerge in the market courtesy of the latest entry in the beloved SimCity franchise from Electronic Arts. The game came under intense criticism at release with players posting screenshots and videos of cities not behaving as expected. And while the issues raised were numerous, there are two critical points we are exploring in this piece.

Sim City had issues with pathfinding and citizen behaviour.

First, the traffic simulation in SimCity was highly problematic. Traffic would often bottleneck, creating massive jams that seemingly never resolved themselves. Meanwhile, should the play attempt to rework junctions to enable better traffic flow or add public transportation this often proved to be more a hindrance than a help. Secondly, the citizens in SimCity did not behave as expected. While it was anticipated that the sims would live their pre-determined lives – working jobs and living happily in their homes – in reality, they lacked any individuality. Sims would leave the house in the morning, find the nearest job available in the city and then after the working day, they didn’t necessarily go back to the house they left that morning. Instead, they simply go to the nearest vacant property. While this is undesirable it seems slightly innocuous, until you realise how big an impact this had on the already troubled traffic systems as well as the actual behaviour and economy of the cities themselves.

While SimCity sought to address this through numerous patches, it never really coalesced at a solution that players were happy with. However, Cities: Skylines when released satisfied many of the features that players sought from its competitor. And as we’ll see in a moment, it not only tracks the passage of every vehicle through your city’s road system, but your citizens will go about their daily business as one would expect. That said, it still suffers from traffic congestion issues.

Now before we get into the intricacies of this problem and how it is tackled in Cities: Skylines, it’s important to acknowledge that this isn’t just an issue that plagues video games, but the real world too. Traffic management systems in the real world are highly complex and incredibly expensive to research and develop. These systems have to manage signalling, traffic flow and congestion, tracking and responding to accidents and other incidents on the road such as spillages, damage to the road or faults emerging in signalling systems and CCTV cameras that used to provide oversight. There are efforts to employ artificial intelligence to handle the complexities of real-world traffic management, but it is far from a solved problem at this time. Hence, it’s understandable that as city-building simulation games seek to become more and more realistic, the challenges that developers face will rise accordingly.

How Citizens Work

First let’s talk about the citizens, how do they work and what impact are they going to have on the traffic network. Each citizen in Cities: Skylines has information that is used as part of the city simulation. This includes:

  • Their name.
  • What level of education they have.
  • Their residential address.
  • Their place of employment or education if they have one.
  • Their overall level of happiness.
  • Current activity, including any destinations they are currently headed towards if they’re travelling around the map.
  • Their health.
  • Their wealth.
  • References to familial relationships and what other citizens are related to them.

Each of these features impacts the game in different ways. Levels of education influences what types of jobs they take on, the performance of these businesses and the value of residential areas. Meanwhile, happiness is shown to the player and gives a barometer for how well you’re performing, given there may be services these citizens require, such as healthcare or utilities that they feel is lacking or the job opportunities in the region aren’t up to scratch. But perhaps what’s most influential on citizens behaviour moving around the map is their age. While a citizen does have a numeric age, this isn’t exposed to players by default, but the key part is that they live their lives in five stages:

  • Children, where they start going to nearby schools if there is capacity.
  • Teens, who go to high schools if there is space.
  • Young Adults, who either attend university or get a job if they can.
  • Adults, who not only move out of their current home and into a new one if they can, but will also settle down and start their own families.
  • Seniors, where citizens retire and no longer go to work but still use city services and commercial businesses.

This has a huge impact on the transportation network, given kids are going to school, adults are either at university or in a job and pensioners are still active in their senior years. Some of these citizens will be in their own car while others use public transport. And this is a very difficult balancing act, given not only will your city grow if you’re doing your job right, but citizens age at an incredibly fast rate, with their average life expectancy of around six in-game years.

How Are Roads Built?

So given we have so many citizens moving around, how does the road system work? Under the hood, roads are modelled as a collection of nodes. Two nodes connected to one another is a segment, providing both start and endpoints. Your average road in Cities Skylines is actually multiple segments glued together, given a segment has a maximum size. Each node in a segment is known as a control point, this provides start and positions for roads, as well as the intersection with other roads creating junctions. Each segment of road stores information about itself, including what type of road it is, how many lanes it has and what types of lanes these are. This all become useful when a citizen wants to travel between locations.

Now this information extends to sidewalks too, which operate on a slightly different system, given they can be elevated or tunnelled to go over or under traffic and crosswalks are established when building them alongside roads to enable pedestrians to cross. All of this information comes into play when a citizen makes the decision to travel to a destination.

Building a City in Motion

Having citizens move around in Cities: Skylines is – by the developers own admission – the single greatest challenge faced during development, resonating with the issues that plagued Sim City. Given it is arguably the one system that any player who spends more than 10 minutes in the game will come to interact with, hence ensuring a basic level of functionality and avoiding some of the more gnarly edge cases was paramount. To that end, not only did Colossal Order rework this system several times over, but as we’ll see in a moment, some elements that players might want to see were actually removed because it only made the problem worse

When a citizen decides they want to travel, they use all of the available information about the cities transport infrastructure: including the roads, sidewalks, crossings, public transportation routes and available vehicles and will find the fastest possible route to their destination. Hence the node system within the roads and sidewalks is searched through to find a valid path. This actually factors in numerous elements based on the citizen involved and the local geography. If a citizen owns a car, they will attempt to drive it. If they don’t have a car, they’ll not only check for a valid path on sidewalks but also factor in whether there is any public transportation that can speed up that process. In some cases, there are strict rules on how this can operate: specific buildings such as hospitals and fire departments will enforce the use of ambulances and fire trucks respectively rather than a firefighter taking a bus with a bucket of water as your home turns to ash. Meanwhile, pedestrians cannot walk on highways and hence if they don’t have public transportation options and sufficient sidewalks to get them to their destinations, they’ll resign themselves to defeat. Colossal Order has never outright stated what they’re searching algorithm is. However, given the rich data laced throughout the road network, it would not be surprising to find a modified version of A* or Dijkstra’s that achieves the desired effect.

The resulting system is actually quite remarkable that a character can make this level of nuanced decision making, especially at this scale. Given a pedestrian can easily walk and take multiple forms of public transportation in order to get to their destination. The destination they want to visit can vary and while they will prioritise going to the primary destination for their life stage – such as school, college or work – they will also go to commercial units such as shops and restaurants, as well as clinics and hospitals.

When a citizen takes a car, it utilises not just the road network topology, but the additional constraints of the road type themselves against the vehicle. This includes the dedicated cycling or bus lanes, as well as the speed limits, the number of lanes and the current congestion level. Provided the system can generate a path through the road network, then the vehicle will begin to drive it. It can also still use the public transportation system. Hence it is possible for a citizen to drive to a metro station and then use that for the remainder of the journey.

In essence, that’s how Cities: Skylines works, but there are several edge cases revolving around traffic, the number of citizens in the city and much more. And we’re going to get into that now, but before I do there are two really important caveats. Firstly, not all citizens are rendered in the city at any point in time. There is a hard limit on how many citizens and vehicles can be simulated on the streets of your city at any given time. Now you might think this is just a rendering issue: in that the game is still simulating all citizens under the hood and you just don’t see it. No, no, no; Cities Skylines outright caps how many citizens can be travelling within the city at any given time. There is a separate system that monitors whether there are available slots for travelling citizens and then allows them to travel once there is space. While the movement of citizens is important for maintaining the economy and their own personal health and education, you the player are not penalised for a citizen being unable to travel due to this simulation cap. The system that oversees this ensures that players are not punished because a citizen is – due to computing limitations – unable to travel to their desired destination. The second caveat is that the traffic you see on the streets is a separate simulation system from the citizen management. When you watch traffic, it’s a collection of thousands of small physics objects that read information from the road network to figure out how to move through it.

So for the remainder of this episode, let’s look at two things: first how the traffic simulation systems work, and secondly, what happens when traffic jams start to occur in the game world.

Watching Traffic

Traffic is handled in a separate simulation system that is actively calculating how to follow the chosen path, the velocity that the vehicle should be going at and preventing collisions. While traffic looks like this smooth and constantly updating process, it’s only rendered that way. The traffic simulation is updated 4 times a second, and the rendering system takes two of these simulation updates to derive movement of the objects on the screen.

When traffic is simulated a vehicle knows the path that it’s been told to go on and they know where on a given road they can drive – hence they won’t drive the wrong way down a one way. Paths through the roads are calculated on a spline using the nodes of each road segment. This allows for tight curves and intersections to be handled as gracefully as possible.

These simulation steps allow for vehicles to calculate whether collisions are going to occur at intersections or from traffic slowing down. Vehicles do recognise traffic lights and will slow down and stop if the lights dictate they can’t travel into the intersection. Meanwhile, to minimise crashes, vehicles check ahead of themselves during the simulation for whether a collision between two vehicles is imminent. This annotates a segment of the road as liable to cause a crash, at which point the distance to the segment is used to decide which vehicle goes first and they move through it on a first-come-first-served basis, each adapting speed accordingly.

But of course despite all of this effort, there are still issues with traffic jams. In part this is to be expected if the player does not build the network sufficiently, but there are other issues that impact the system. So let’s wrap up by talking about what the simulation does and does not do in order to keep it manageable.

First of all, unless something happens that causes a given path to no longer be valid or has been extensively modified, a citizen or vehicle will not change their pre-calculated route. You may think there would be some opportunistic behaviour going on where a character may re-evaluate their options on the fly, but nope, only in the event something happens, that negates the original path. While in some larger segments of the road with multiple lanes that could spread traffic out, you’ll notice they don’t change lane again after moving into the one they originally decided upon – even if that would make sense to do so. Having vehicles change lanes midway through a segment of road was actually something that Colossal Order experimented with, only to later remove because during internal testing, it actually made everything worse.

When traffic builds up, you may see cars sitting idle at junctions and not moving forward, despite having room to do so and that’s often because their paths have determined further issues up ahead and as a result they opt to simply stand idle.

Last but not least, Cities Skylines has a fallback option for when traffic jams are overwhelming. If a vehicle has found itself in gridlock and unable to move, it will actually teleport back to its origin. And the previously mentioned worker system may well stop that citizen from trying to travel again and avoid punishing the player any further. This was a decision made by Colossal Order in order to make the transportation challenge more manageable, given even relatively small cities begin to be impacted by this issue. This decision has actually proven to be contentious among some players, who prefer the challenge it presents and use mods that actively remove the teleport feature.

Closing

Building a city simulation game is incredibly demanding, especially as the granularity and fidelity of the simulation increases. Cities: Skylines is an incredible effort from a team that – at launch – was comprised of only 14 people. But it shows the core tenets of their design from previous games could steer them through to making a game that is now the leading game of its kind on the market. With an entire generation of city planners still working away for hours on end perfecting those road networks, Colossal Order knows this is a problem they need to tackle again in future releases. The issue of the traffic is an ongoing one within the Cities: Skylines community – with some PC players using mods to teleportation or outright replace the pathfinding systems used in the game. Meanwhile, others are happy to leave it as it is and embrace the chaos.

References