How Townscaper Works: A Story Four Games in the Making

April 7, 2022
None
,


'AI and Games' is a crowdfunded YouTube series that explores research and applications of artificial intelligence in video games. You can support this work by visiting my Patreon page.

Townscaper is a small game, with big ambitions. A city builder in which players can simply add or remove a block from the game world. With this limited toolset, you can craft everything from idyllic seaside towns to a horizon spanning metropolis. A procedural generation engine running under the hood caters to the unique topographies players establish and refines the world with each new action taken. But how is it capable of expanding, and rebuilding the world with such consistency? While also introducing fun and novel ideas for players to discover on their own.

For this case study we’re going to explain the different level generation systems that power Townscaper and the AI principles they derive from. Plus I sat down with the game’s creator Oskar Stalberg to gain a stronger understanding of how it all works and his aspirations in designing the game. In order to truly understand Townscaper, we’ll be taking a journey through Stalberg’s career and his previous work. Because the secrets to the game’s success are hiding in plain sight, in everything else that he has built to date.

Townscaper Explained

First released on PC and Mac in June of 2020, Townscaper is, by Stalberg’s own admission, more of a toy than a game. It does not have any explicitly defined objectives and instead, you are left to craft towns based on your own design principles. Players have a very limited number of interactions: you can decide where a block is added or removed, and you can customise the colour of the blocks you place. That’s it. Everything else that you see, is achieved courtesy of the game’s procedural generation system. This creates what we would typically consider to be a mixed-initiative AI system: meaning that the player gives input to where the game needs to expand the structure, and the system assesses the current shape, identifies what prebuilt chunks of architecture are valid for that space and then injects them into the game world. Despite this lack of direct control, the system is highly flexible and can handle a myriad of different scenarios that players can come up with, resulting in a variety of fun, interesting and aesthetically varied settlements.

And it’s this simplicity in gameplay combined with the game’s bright, abstract yet detailed aesthetic, that has led to Townscaper transcending boundaries: developing a huge following across social media that has captured the imaginations of many who don’t typically play videogames. Plus if you’re a game developer on Twitter, you’re bound to have seen one of Stalberg’s development tweets on your timeline. Either for Townscaper, his previous game Bad North or his more recent projects.

The secret to Townscaper’s success is one that many aspiring (and current) game developers should reflect upon. The game is effectively the fourth entry in a series of works that capitalises on Stalberg’s interest and growing expertise in procedural generation and technical art, with each project building upon the previous one. And to successfully explain the tricks employed in Townscaper, we need to go back and visit each of them in turn.

“I feel like I’ve kind of been working on the same thing for the past ten years or so. But it’s also sort of fun because instead of turning it into one huge epic thing, I’m making a small thing at a time. Release that and then make a slightly bigger thing with some new expansions, release that. So it’s a pretty good way of working: sort of releasing the intermediate steps of your project because you learn a lot from getting into the shipping mindset and wrapping things up.”

Oskar Stalberg

So with this in mind, we start our journey in what is arguably an unexpected and unlikely place. The game that inspired Townscaper and everything that preceded it: Tom Clancy’s The Division.

Brick Block

“When I made BrickBlock and a couple of other, like, procedural toys. Those were not commercial projects, but just finishing them and putting them online for other people to use was a really good way of getting into the shipping mindset, making sure, you know, I do some UI and maybe some sounds and stuff like that.

When you get into the shipping mindset, you start to sort of see your things from sort of another person’s eyes. You start thinking about your thing as if you came to it the first time. It’s very hard to do that when you when you’re not in that mindset, when you’re just expanding and expanding. But then when you start to think about, “Oh, I actually have to show this to other people”, and then I need to fix these couple of things, and I can actually cut out these couple of things that I was planning to do because another person won’t know that those things are missing.”

Oskar Stalberg

Stalberg graduated in 2012 from The Game Assembly, a school specialising in game development based in Malmo: arguably the biggest hub of game development studios in Sweden. Not long after, he landed a job at Ubisoft’s Massive Entertainment, creators of Tom Clancy’s The Division, working as a Technical Artist. The Division takes place in downtown Manhattan, an area comprised largely of tall skyscrapers and ominous-looking buildings. While Stalberg wasn’t involved in this part of the production – his work was actually on the very cool looking holographic map – he watched his colleague’s work on the city building team with great interest.

“They needed a lot of buildings so they made some tools to help make the [them]. And then I started thinking about how I would approach making the building tools. I wanted to think about it because there’s the standard way which I think is sort of how they [Massive] did that as well. You make a polygonal shape and then you extrude outwards and then you just duplicate modules along the walls.

And that’s very good for most things. Most realistic houses anyway. But I thought, okay, but what if houses have slightly weirder shapes? Like one of the it’s not just sort of 2.1 dimensional, but like wall goes like this and then up and like that. Like, how do you account for those kind of shapes? And because a kind of a key idea in most of my procedural work and sort of artistic work in general, my key philosophy or approach that I have is that what’s important is what happens when things change. So what happens when one material meets another material? What happens like at the corner of a house or the corner of the wall on the roof? And things like that. Because like when you just have the same brick wall, that’s not that interesting.

You should have a nice brick texture, but probably a subtle one. But what happens when the shapes change? And that’s like if you wanted to make a house with a wall that goes like flat then up and then flat again, like that little corner; you would want to artistically comment on the thing that happened there.”

Oskar Stalberg

This led to a small but significant question that would drive the creation of Townscaper: how to build urban structures that respect the local topography. The answer was a small prototype called Brick Block: in which players can place blocks within a fixed 5x5x7 grid that allow for small, yet fun looking urban environments to be constructed. As you can see, Brick Block glues together pieces of apartment buildings together in ways that make sense.

To make this work, Stahlberg built a tool that is markedly similar to what is commonly known as Marching Cubes: an algorithm built for the purposes of generating a polygonal mesh within a specified three-dimensional volume. By discretising a grid space into a series of points, you can create a polygonal mesh using select points within that 3D space. Technical complexities aside, what this means is that by selecting several points in space you can draw a 3D shape. In Brick Block the game only allows you to build a new unit provided the cell selected connects to polygons already constructed in neighbouring cells. This prevents rogue blocks from floating in space, but can still allow for structures that would make a civil engineer break out in a sweat. However, the key thing, is that gives players a lot of freedom to make interesting and fun structures.

The origins of Townscaper are clearly evident here, but it was shelved for the time being. While Stalberg struggled with how to take this idea and turn it into a fully-fledged game, the big technical issue was that Brick Block structures quickly become repetitive. Hence in the demo you can play online, a conscious decision was made to keep the grid very small. Hence there was a new question to tackle: how could you approach the same idea on a larger grid while enabling more diversity in the generated output?

Bad North & Wave Function Collapse

Stalberg left Massive Entertainment in 2016, not long before the release of Tom Clancy’s The Division and after a stint at the Malmo studio of Monument Valley developer UsTwo Games, transitioned into indie development. Working alongside colleagues Richard Meredith and Martin Kvale, this led to the creation of the micro RTS combat game Bad North, published by Raw Fury and released in 2018.

In Bad North players are presented with the challenge of defending small islands with village structures that can be attacked by Viking invaders. To enable the roguelike elements of the game, the game procedurally generates each island prior to its appearing on the game world map. But unlike Brick Block, which needs only create something aesthetically pleasing, Bad North had the added challenge of satisfying a myriad of gameplay functions. Each island needs to provide sufficient real estate for the movement of armies, large coastlines that will enable all of the invading forces in that level to beach successfully. And critically, the ability for player and enemy AI characters to be able to move from one area of the island to another successfully.

This time around, the resulting solution needed to provide a lot of variety, while ensuring logical consistency. And it’s with this in mind, Stalberg drew inspiration from Wave Function Collapse – an algorithm that can be used to decide what objects can be placed next to each other based on the rules provided to it. Wave Function Collapse is what in AI terms we call a constraint solver. Constraints Solvers are a class of AI algorithms designed to solve problems in which variables can be assigned a myriad of possible values, but there are rules on what values can be assigned, and those rules can have a huge chain reaction on the final solution. A simple, yet very effective example of these constraint satisfaction problems, is a game of Sudoku. In Sudoku, each cell can only have one value between 1 and 9, and the rules of the game add constraints that tell us that no number can appear twice in the same row, the same column and in the defined 9×9 subsquares. The goal of an AI Sudoku player is then to ensure that every new digit added to the grid satisfies those constraints, but also doesn’t prevent other cells from having any valid numbers left to assign themselves.

So what’s this got to do with Bad North? The algorithm Wave Function Collapse, developed by Maxim Gumin in 2016, is designed to create large 2D graphics using the same principle. The idea is that upon reading a smaller image, it learns what are the valid rules for generating new images of its own, of any size. The original image highlights adjacency constraints, meaning that for any pixel in the graphic, we can quickly determine what valid pixels can be placed next to it. Stalberg was inspired by this idea and sought to build a 3

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>