Scrap Galaxy is a local build-and-destroy deathmatch game for up to 4 players. It is a 2D top-down shooter taking place out in space in a galactic junkyard. The AI in Scrap Galaxy faces two problems, first one is how to build a decent spaceship from a pile of random parts, where we do not know the next parts beforehand. Second problem is how to pilot a spaceship made out of a lot of random parts. This article will cover the first problem, how to build a spaceship from random parts.

Posted by CatalopeGames on Nov 30th, 2017

Hi,

Jesper from Catalope Games here again. This time I will tell something about the AI in Scrap Galaxy. Scrap Galaxy is a local build-and-destroy deathmatch game for up to 4 players. It is a 2D top-down shooter taking place out in space in a galactic junkyard. In this setting, the players are trying to survive by constructing spaceships. They can use these ships to fight against each other or they can fight together against the AI. You can also play it with bots instead of real players.

The AI in Scrap Galaxy faces two problems, first one is how to build a decent spaceship from a pile of random parts, where we do not know the next parts beforehand. Second problem is how to pilot a spaceship made out of a lot of random parts with weapons sticking out at all directions, enemies trying to shoot you down and space junk we can collide with. This article will cover the first problem, how to build a spaceship from random parts!

First we need to define what we mean by "decent spaceship". We want to make an AI that builds ships which are.

Sound - do not shoot itself apart. So do not build weapons facing already placed components nor place components in places so they'll get hit.

- do not shoot itself apart. So do not build weapons facing already placed components nor place components in places so they'll get hit. Functional as in the ship can fly decently and has weapons. A balance of components.

as in the ship can fly decently and has weapons. A balance of components. Aesthetic - a spaceship does not look like a pile of junk taped together but has symmetry.

- a spaceship does not look like a pile of junk taped together but has symmetry. And lastly not deterministic. Given the same pool of components, which is the case in some game modes, it should not build the same spaceship over and over again.

How I solved this is like an local optimization problem. For each build step it searches after the best part and the best position to place it in. Here are the different parameters it can optimize over. When the AI constructs a new ship these are randomized within certain ranges. This also gives the feature of being able to have AIs with different playstyles - one likes fast ships while another one prefers slow large ones.





public enum SymmetryType { X_AXIS, BOTH_AXIS, Y_AXIS, BEND, } public float RandomPart; public float Symmetry; public SymmetryType PreferedSymmetry; public float Speed; public float Damage; public float Range; public float Defence; public float Size; public float[] Direction;

But this does not solve the problem completely, as the AI needs take into account what parts have already been placed on the ship. Otherwise an AI with a high liking of Damage would always place weapons until the entire ship is covered in nothing else, without any engines or other components whatsoever.

That is why the utility value of a component is scaled via a diminishing return function. This functions says that the more components of a certain type, for example weapons, you have the lesser you gain from having yet another weapon on your ship.

utilityValue = DiminishingReturn(weapon.DPS, ShipData.TotalDPS) * BuildStrategy.Damage; // .... private float DiminishingReturn(float value, float shipValue) { float pow = 0.5f ; return Mathf.Pow(value + shipValue, pow) - Mathf.Pow(shipValue, pow); }