Skip to content

nikhilcodes777/lifu

Repository files navigation

Lifu 🍀

A real-time 2D ecosystem simulation where autonomous agents — herbivores, omnivores, and carnivores — compete for food, water, and survival across four seasons. Every creature is driven by a small neural network brain that it inherits (and mutates) from its parent, so the population slowly evolves over time without any hand-written rules for individual behaviour.

Read the full simulation report


Screenshots

Spring Summer
Spring screenshot Summer screenshot
Autumn Winter
Autumn screenshot Winter screenshot

What it does

  • Neural-network-controlled agents — each creature has a tiny 6-8-5 feedforward brain. Inputs are proximity to food, predators, and water plus internal energy/thirst/health levels. Outputs drive steering, fleeing, and social behaviour.
  • Four seasons that cycle automatically and affect plant growth rates, metabolism, and thirst drain.
  • Disease spread — one creature starts infected; the infection transmits on contact with a probability proportional to proximity and time. Infected individuals recover or die.
  • Emergent evolution — on reproduction the brain weights and physical traits (speed, size, field of view) are mutated slightly. Over many generations faster prey, sharper predators, and better foragers accumulate.
  • Real-time statistics panel — live population graph, mortality breakdown, and averaged trait stats per dietary group.
  • Video recording — pass --record output.mp4 to pipe every rendered frame through ffmpeg and save a compressed H.264 file automatically.

Dependencies

Dependency Version tested Purpose
raylib 5.x Window, rendering, input
g++ / clang++ C++17 Compiler
ffmpeg (optional) 6.x Video recording only

Install on Ubuntu/Debian:

sudo apt install libraylib-dev ffmpeg

On macOS with Homebrew:

brew install raylib ffmpeg

Building

make

The binary is placed at ./lifu.

To clean build artefacts:

make clean

Running

./lifu

Record a full season cycle to a video file:

./lifu --record output.mp4

Controls

Key Action
1 1× speed
2 2× speed
3 5× speed
4 10× speed
5 20× speed
Q Quit

Implementation details

Spatial grid

The world is divided into fixed 50 × 50 px cells. Every update tick, all living agents and plants index themselves into the grid. Perception queries for each agent then only iterate over the 3×3 neighbourhood of cells around the agent's position, keeping the per-frame cost close to O(n) rather than O(n²) even with hundreds of agents.

Neural network

Each Brain is a plain 3-layer network (no backprop, no gradient descent):

inputs  [6]  →  hidden  [8]  (LeakyReLU)  →  outputs  [5]  (Sigmoid)

Inputs:

  1. Normalised proximity to nearest food source
  2. Normalised proximity to nearest threat
  3. Normalised proximity to nearest water
  4. Current energy ratio
  5. Current thirst ratio
  6. Current health ratio

Outputs control: food-seeking drive, flee drive, water-seeking drive, social flocking drive, random-walk noise intensity.

Evolution / mutation

On reproduction the parent pays an energy cost and produces one offspring. The child inherits all trait values but each weight and bias has a 12 % chance of being perturbed by ±0.2, and continuous physical traits (speed, size, FOV, max age) are scaled by a factor drawn uniformly from [0.85, 1.15]. There is no crossover — this is a purely mutation-based evolutionary scheme.

Seasons

A season lasts 70 simulated seconds and cycles Spring → Summer → Autumn → Winter. Each season applies multipliers to:

  • Flora spawn rate (×2.0 spring down to ×0.35 winter)
  • Metabolic cost per tick (×0.85 spring up to ×1.5 winter)
  • Thirst drain rate (×0.7 spring, ×1.6 summer peak)

The background tile colours and plant colours are also changed to reflect the season visually.

Disease

One herbivore is flagged as infected at startup. Infected agents:

  • Lose 2.5 HP/s
  • Move at 65 % of their normal speed
  • Transmit the infection to any agent within size + other.size + 3 px at a rate of 0.007 probability per frame (at 60 fps this is roughly 0.42 %/frame)
  • Recover automatically after 20 s of infection, gaining a 18 s immunity window

Video recording

Recording is done by opening an ffmpeg subprocess via popen and piping raw RGBA frames directly from raylib's LoadImageFromScreen() on every rendered frame. The recording stops automatically once a full four-season cycle has elapsed.


Inspiration


License

MIT License. See LICENSE for the full text.

About

An evolution simulator with inbuilt screen capture written in c++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages