h-population

I just came back from holiday yesterday, full of energy and inspiration. It was time to tackle the level generation.

ChiChiLand Adventure will use procedural generation for many of its elements, including zones within the levels. Within these zones, the entities (zone citizens) will be simulated based on simple sets of rules. In the garden, for example, I am planning to have some animals and some trees. Every x amount of time, each tree will have a chance to bear fruit. The fruit will fall down and roll in a random direction until stopping. Each animal in the zone will have a hunger meter – as they get hungry, they will seek out fruit and eat it. There will definitely be more behaviors, but that’s the basic logic.

To keep things interesting and unpredictable, I want the zones to spawn their population differently each time the game is played. That’s what I worked on today.

For the garden, I first define the population through a simple array. It looks something like this:

// Define population
population[0,0] = obj_gardenTree;
population[0,1] = 6;
population[1,0] = obj_gardenAnimal;
population[1,1] = 10;
population[2,0] = obj_player;
population[2,1] = 1;

As a result, the garden will randomly spawn 6 trees, 10 animals and 1 player.

I wanted the objects to be distributed around the whole zone but to avoid having to write heavy path-finding algorithms to make sure the player is not blocked from moving around. Rather than doing tons of position and collision checks, I settled on a simpler approach (inspired by the way Paris is divided into quadrants).

The zone (garden in this case) is defined by hand. I also define the size of the cell inside the zone. For now, cellSize = 3; meaning the garden zone will be partitioned into cells of 3×3 blocks, or 48x48px (each block is 16x16px in size – same as the sprites).

The script then calculates the total amount of cells available and randomly distributes the population among them.

// Assign citizens
for (var i=0; i

The citizen array has been previously filled with the value of -1 for each of its positions. Now it becomes populated by 0's, 1's and 2's, representing trees, animals and the player.

Once the array is constructed, I run the second loop to actually spawn the citizens. I could do all that in a single block of code, but legibility is a big concern for me at the moment, as I am still learning the ins-and-outs of the Game Maker Studio language.

// Spawn population
{
var k=0;
for (var i=0; i

The code above spawns the correct citizen inside each block, additionally randomizing which cell will it occupy.

As a result, I guarantee that within every 3x3 blocks, there will always be 2 cells of space and 1 cell of content/sprite. That is, of course, before the animals start walking, trees bearing fruit and all that.

The result can be seen below. Pressing Spacebar will re-spawn the garden, randomizing it from scratch. The trees don't yet have the art on them and are visualized by the block with letter "T" on it :)

Click to play

 

The system works, but is not yet quite as modular as I would hope it to be. The next step will include moving the functions out of the garden object and making them universal enough to be used by any zone I construct in the future. I will also need to construct a simple check that makes sure there are enough cells to accumulate the requested population. At the moment I do the check manually, but requesting too many citizens puts the script in an infinite loop as it continues looking for an empty cell forever.

Before I do that, however, I will probably work a bit on the sprites and background tiles. I am, after all, a visual person and I want things to be not just functional, but also to look good :)