Gabriel Gambetta is Tech Lead/Manager of the SpatialOS Community team at Improbable.
Welcome to the first entry in the developer diary of AP3X (pronounced “Apex”), a game world we’re building internally to test and showcase SpatialOS features.
This is written by game developers, and with game developers in mind. Most of the concepts will be familiar, but some are specific to SpatialOS. For a high-level introduction read this article, and for the technical details you can jump straight into the technical documentation.
Like all of our demos, the full source code for AP3X is available here.
AP3X will feature massive battles, fully functioning ecosystems, PC and mobile clients, and much more. This will be developed incrementally over the next months, and every iteration will be publicly available.
This first iteration of AP3X is set in a flat, barren world. You can’t grow potatoes in its slightly reflective surface, whose only feature is the occasional energy crystal reminiscent of a cactus (affectionately known as “cactals”).
Several factions of soldiers have landed on the planet, and set up bases that look like giant water tanks. They only venture out of their bases to collect cactals and use their energy to power the bases; but they will attack soldiers from rival factions as soon as they spot them.
When the simulation starts, it is empty. The first step is to add the bases, which act as repositories for the collected cactals. Then, we add the soldiers. Each of the 8 factions starts with 250 soldiers, for a total of 2000 soldiers competing for resources. All this is done here.
Finally, the game world is seeded with cactals, following a pattern that is pleasant to the eye and to the developer. Each cactal is an entity, but they don't do much on their own.
We want to create a world that is in conflict, even when no players are connected. Having an interesting AI for the soldiers is critical for this; it makes it possible to have a massive battle happening at all times.
Soldiers have just one motivation: to collect the cactals. They will wander until they detect a cactal, and then beeline to collect it. When a soldier has collected three, she will return to her team's base. Of course, if while collecting cactals a soldier should detect a soldier from another team she will open fire to defend her team’s access to the resource. All of these behaviours are orchestrated by a state machine.
So far the “player” can only interact with the world as a spectator, flying around and watching an epic AI battle for the precious cactals.
Whatever you build on SpatialOS is multiplayer by default, so from the get go you have support for any number of spectators. But surely we can do better!
Each soldier is an entity, and is independently simulated; the game worlds built on SpatialOS don’t treat players or game clients in a special way. So it is quite simple to let any player take control of any soldier! We just need to disable the AI, make the client authoritative on the player controls, and change the camera to render in first-person.
This is all we need to turn this AI-driven simulation into a massive FPS battle. Fifteen or so of us connected to AP3X and had a ton of fun shooting at each other.
Interestingly, we didn’t have to do any other changes to the code. For example, a soldier controlled by a human player can collect cactals by walking over them, just like AI soldiers do. This highlights the fact that the only difference between players and AI soldiers is where the inputs are coming from (a game client, or a snippet of code), and are otherwise identical from the point of view of the simulated world.
SpatialOS imposes no hard limits on the number of concurrent players, so if every soldier was controlled by a human, we end up with a two thousand player FPS battle!
Asking two thousand people to connect for testing purposes would be impractical, so we added “dummy players”. Unlike the AI-controlled soldiers, which would be considered “bots”, these dummy players connect to the simulation from the outside, just like a real game client would, and send synthetic player inputs, just like a real game client would.
From the point of view of the game, they’re completely indistinguishable from real players, and use just as much compute and networking resources. We use these dummy players internally to stress test our tech, so it’s in our best interest to make sure they are as realistic as possible in this sense.
But in short, we connected 2000 players to the game world, and verified that SpatialOS could handle the load. Here’s what the Inspector looked like after a few minutes of shooting and resource collecting:
This first iteration of AP3X sets the scene for a massive battle: two thousand players competing for resources in a barren world. SpatialOS makes it really easy to create a world that “works” by itself, but it makes equally easy to turn this simulation into an FPS that can support an impressive number of human players simultaneously.
The full source code for this demo is available here.