---
title: "Random enemies generation and buffs management"
author: "Veselin Slavchev"
date: 2016-11-06
tags: ["game development", "procedural generation"]
description: "How my space invaders game generates a new balanced alien fleet every level and rewards combos with stacking buffs."
url: https://vsslavchev.com/blog/invade-yer-space/
---

# Random enemies generation and buffs management

**In short:** Every level the game generates a new, symmetric fleet of 8 by 20 aliens. Each alien's type is random, but nudged by how far the average so far is from the level's difficulty, so the fleet stays random but balanced. Kill combos, where each kill comes within 7 seconds of the last, unlock a ladder of heat-free buffs (shield, rockets, scatter, spears, laser, flakes) that make it easier to keep the combo going.

![Invade yer space gameplay screenshot](https://vsslavchev.com/img/projects/invade-yer-space-poster.jpg)

Invade yer space is a space invaders game I made in Java. You are Captain Manol, an old pirate who lost his fleet to the aliens, but managed to steal one of their ships. Now he goes on a journey to invade their space. Yarr.

After cleaning up the code, I want to write about the two parts I like the most: how the levels are generated and the combo buffs. Download to play from the [projects page](/projects/#invade-yer-space).

First a bit of context about the weapons. You have 4 of them and each one heats up as you shoot, and when it overheats it stops firing. So end up switching between weapons, letting one cool while you use another.

Ok, the levels. There are no hand-made levels in the game - every time you win a new alien fleet is generated. The fleet is 8 rows by 20 columns and it is stored as a string of digits, where each digit is the type of the alien in that spot. There are 8 types and the type decides the health of the alien and how it looks and shoots

I wanted the fleets to look like a formation and not like random noise, so only the left half of each row is generated. Then the row is appended to the map together with its mirror image:

```
public void generateNewEntitiesMap() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < maxRows; i++) {
        StringBuilder innerSb = new StringBuilder();
        for (int j = 0; j < maxColumns / 2; j++) {
            innerSb.append(generateAlienType(i, j));
        }
        sb.append(innerSb);
        sb.append(innerSb.reverse());
    }
    entitiesLevelMap = sb.toString();
}
```

Every fleet is symmetric this way, which already makes it look designed. It also means half the random numbers.

The interesting part is picking the type. What I did is take a random type and then correct it by the difference between the level difficulty and the average of all the aliens generated so far in this level:

The way it works is like a thermostat. If the first few aliens came out strong, the average goes above the difficulty and the next ones get pushed down. If they came out weak, the next ones get pushed up. The bounds check at the end just keeps the type between 1 and 8. The result is that locally the fleet is still random - you get clusters of tough aliens here and there - but the overall strength of the fleet follows the difficulty.

The difficulty goes up a little every time you win and a new map is generated, so the average alien slowly gets stronger.

There is also the classical escalation inside a level. Every time an alien dies all the remaining ones get a bit faster and start shooting a bit more often.

Now the buffs. The game counts combos - kills where each one comes within 7 seconds of the previous one. When the combo reaches certain sizes you get a buff. Each buff is given once per combo.

A buff itself is just a type and a countdown. Every tick the ship counts down all active buffs, removes the expired ones and then goes through the rest - the shield makes the ship invulnerable, the laser marks a column where aliens get destroyed and the weapon buffs try to fire. The weapon buffs are actually the same weapon class as the normal ones. Buffs also stack, so at a big combo you can have three or four of them going at once.

The reason this works well is that the buffs feed the combo. The rockets and the laser destroy aliens instantly with area damage, and every alien also has a small chance to explode when it dies and take its neighbours with it. That means more kills in a short time, which keeps the combo alive, which gets you to the next buff. When it goes well the screen just turns into explosions.

Your highest combo also goes into your score, so going for combos is the way to play, not just a bonus.

The game and the source are [on github](https://github.com/vs-slavchev/Invade-yer-space) and you can download a release [here](https://github.com/vs-slavchev/Invade-yer-space/releases).
