Food, reproduction, and life counters
Food
After predators were added, we ventured into adding an additional layer to the food chain: fish food. While this change ultimately required relatively few lines of code to implement, there were a lot of small tweaks and bugfixes that needed to be made.
In order to utilize the predator system, food was added as new school of fish. That is to say, every piece of food is actually a boid, with all the abilities and steering forces of any other fish, just with zero movement speed. Apparently there were several parts, both in how the boids spawn and in how they interact, that originally didn't account for having "fishes" that can't move, but once these were hunted down, they were relatively easy to fix.
For simplicity's sake, the model for food is just a light green sphere.
Reproduction
At this point, both fish and food will both be slowly picked off by their respective predators until the only remaining feature in the simulation are the sharks, so the next step is of course to allow the fish to reproduce. Originally this was done by giving each boid a boolean variable, which was originally set to false, but would change to true when something is eaten. When two fishes (or two predators) who both have this variable set to true are close enough to each other a new boid will spawn at their location. Later, this boolean variable was removed, and this system instead became governed by the life counter described below. The biggest challenge of this part was to write the new functions for spawning new fish during the simulation, since up until this point boids were only spawned at initialization.
Food can, of course, neither move nor eat, and doesn't reproduce this way. Instead, in order to ensure that food won't run out, food doesn't "die" like other boid, but are instead relocated to a random spot in the simulation upon death.
Life counters
Finally, to add some spice to the behavior of the boids, we added life counters. Each boid is initialized with a counter that starts at 1000, and each time the boid is updated the counter decreases by one. Eating resets the counter, reproducing decreases it by a few hundred. Further, to get the boids to move around more, and not just congregate on food and then get stuck in a cycle of eating and reproducing, reproduction is only allowed during a certain window, i.e. not immediately after eating, nor if the counter has gone down too much. The boids also only act predatory if the counter is low enough.
Originally, the boids were supposed to die if the counter got low enough, but since this rarely happened due to the abundance of food, and since this seemed to cause some weird errors at initialization, this was removed.
Kommentarer
Skicka en kommentar