Skip to content

About

A falling-sand style plant simulation in C++ and SDL2: plants grow cell by cell from light and water, with genomes you can evolve.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

Sand-forest-sim

A seed grows into a forest, outgrows its water supply and collapses

A single seed grows into a forest until it outgrows its water supply and collapses (2 minutes of simulation, sped up 6×). In some runs, parts of the forest survive and grow back.

The program itself is called Vertical Forest (verticalForest).

A falling-sand style plant simulation. A seed drops onto the ground, sprouts, and grows a plant made of individual cells (roots, stem, leaf nerves, leaf tissue). Each cell has its own food and water and passes them to its neighbours. Light, rain and soil moisture are simulated across the whole grid. How a plant grows is set by a genome per cell type. A built-in trainer evolves genomes over many headless runs.

How this was made

The simulation itself (the cells, the world, genomes and the game loop in cell, world and main) is my own code. I used AI (Claude) for the evolutionary trainer (trainer.{h,cpp}), the SDL2 helper PixelWindow.hpp, and for writing this README and the docs in docs/.

Quick start

cmake --preset linux
cmake --build --preset linux
./build-release/verticalForest

You need CMake ≥ 3.20, a C++17 compiler and SDL2 (development package).

Tip: the first seed doesn't always make it. It can run out of food or land in dry soil, so you may need to start the sim a few times before the forest takes off. Press + a few times to speed it up while you wait.

Running

Command What it does
verticalForest Run the sim with the built-in default genome
verticalForest pause Same, but start paused
verticalForest --genome <file.json> [pause] Run with a genome loaded from JSON
verticalForest train Headless evolutionary training, writes trained.json

Every interactive run saves the genome it started with to genomes-N.json. These files are gitignored.

Controls

Key / mouse Action
+ / - Double / halve the simulation speed (default 160 ticks/s)
P Pause / resume
L Toggle light-field view
M Toggle moisture view
R Toggle rain
F Toggle flood (heavy rain every tick)
D Toggle desert (no rain)
Left click Drop water
Right click Drop a seed

The window title shows the actual ticks/s, the per-tick update time and the current tick.

Documentation

  • Building: presets, Windows cross-compile, compile-time flags
  • Simulation: the world, the tick order, light, rain, moisture
  • Plant cells: how each cell type lives, grows, moves sap and dies
  • Genomes: genome fields, the JSON format, mutation and build cost
  • Training: how train evolves genomes, and how the fitness score works
  • User cells (WIP): the planned plugin API for user-programmed cells

Source layout

File Contents
src/main.cpp Entry point, CLI parsing, game loop and input
src/world.{h,cpp} Grid, light, rain, moisture, rendering
src/cell.{h,cpp} Cell types, plant behaviour, genomes and their JSON I/O
src/trainer.{h,cpp} Parallel evolutionary trainer
src/userCells.{h,cpp} Work in progress: loading user cell logic from a shared library
src/PixelWindow.hpp Small SDL2 helper that scales a pixel buffer to the window
src/rng.hpp Fast RNG
src/debug.h Compile-time feature switches
src/json.hpp Vendored nlohmann/json

License

MIT, see LICENSE. src/json.hpp is nlohmann/json, also MIT.

About

A falling-sand style plant simulation in C++ and SDL2: plants grow cell by cell from light and water, with genomes you can evolve.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages