Example simulation visualization
This project is a Rust implementation of a parallel virtual ecosystem simulation featuring:
- Multiple being types (Herbivores, Carnivores, Omnivores) with distinct behaviors
- Genetic traits that evolve over generations
- Food sources that spawn dynamically
- Parallel computation using Rayon for efficient multi-core utilization
- Real-time visualization using Piston graphics
- Dedicated stats display area with comprehensive metrics
- Built and tested on Fedora 40 Cinnamon with Ryzen R9 5950X
- More modular code base
- 40% performance increase (avg. therad usage utilization dropped from 7% - 9% to 5% - 7%)
- Added FPS counter to stats panel
- Optimized predator hunting logic
- Cannibalism Prevention: Beings will no longer target others of their own type
- Type-Specific Interactions:
- Herbivores only interact with food
- Carnivores only hunt herbivores and omnivores
- Omnivores have balanced hunting/foraging behavior
- Size-Based Hunting: Predators only target beings smaller than themselves
- Dedicated stats panel above simulation area
- Real-time population counts by type
- Food availability tracking
- Thread utilization metrics
- Average energy levels
- Birth/death counters
- Clean separation between stats and simulation areas
- Optimized rendering pipeline
- Consistent color coding:
- Blue: Herbivores
- Red: Carnivores
- Orange: Omnivores
- Green: Food sources
Type | Color | Behavior | Diet | Special Traits |
---|---|---|---|---|
Herbivore | Blue | Passive | Plants | High perception for food |
Carnivore | Red | Aggressive | Other beings | Fast movement, high attack |
Omnivore | Orange | Adaptive | Mixed | Balanced abilities |
-
Inheritable Traits:
- Speed (Movement capability)
- Size (Affects hunting ability)
- Reproduction Rate
- Perception (Detection range)
-
Mutation: Small random variations in offspring
-
Natural Selection: Successful traits propagate through generations
-
Ecosystem Dynamics:
- Energy-based lifecycle (consumption, metabolism, reproduction)
- Age-based mortality
- Population limits
-
Parallel Processing:
- Multi-core being updates using Rayon
- Thread-safe food management
- Configurable thread count
- By default utilizing all of CPU threads and cores regardless of their number
Example of all my 32 CPU threads used evenly (Ryzen 5950X)
The header displays real-time information:
Pop: 120/220 (H:80 C:20 O:20) | Food: 450 | Threads: 8 | FPS: 4200.7
-
Pop: Total beings from set limit (with counts by type)
-
Food: Available food sources
-
Threads: Active worker threads
-
FPS: Total FPS of simulation running
- Rust 1.60+
- Cargo
- TrueType font file (for stats display, optional)
-
Clone the repository:
git clone git@github.com:Peter-L-SVK/simple-life.git cd simple-life
-
Add a font file in case of not included (optional):
mkdir assets cp /path/to/your/font.ttf assets/FiraSans-Regular.ttf
-
Build and run in same directory as Cargo.toml:
cargo run --release
Here's a complete guide to getting the simulation running on Windows systems:
-
Install Rust:
- Download the Rust installer
- Run the installer and follow the prompts
- Ensure you select the option to add Rust to your PATH
-
Install Visual Studio Build Tools (required for Rust on Windows):
- Download Build Tools for Visual Studio
- Install with these components:
- "Desktop development with C++"
- Windows 10/11 SDK
-
Open Command Prompt:
- Press
Win + R
, typecmd
, and press Enter
- Press
-
Clone and run the project:
git clone git@github.com:Peter-L-SVK/simple-life.git cd simple-life cargo run --release
For better performance and Linux-like environment:
-
Install WSL:
wsl --install
Restart your computer when prompted
-
Install Rust in WSL:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
Run the simulation:
git clone git@github.com:Peter-L-SVK/simple-life.git cd simple-life cargo run --release
-
Linker errors:
- Ensure Visual Studio Build Tools are installed
- Run in Visual Studio Developer Command Prompt if needed
-
Font issues:
mkdir assets copy C:\Windows\Fonts\arial.ttf assets\FiraSans-Regular.ttf
-
Performance problems:
- Add this to
Cargo.toml
:[profile.release] lto = true codegen-units = 1
- Add this to
-
Disable console window (for release builds): Add this to
Cargo.toml
:[profile.release] strip = true
-
Window scaling: If the window appears too small, modify:
const WINDOW_SIZE: f64 = 1200.0; // Increase from 800.0
-
High DPI support: Add early in
main()
:use winit::dpi::LogicalSize; window.set_inner_size(LogicalSize::new(WINDOW_SIZE, WINDOW_SIZE));
To build a standalone .exe
:
cargo build --release
The executable will be at:
target\release\virtual-ecosystem.exe
You can distribute this file along with any required assets (like the font file).
Modify these constants in main.rs
to adjust simulation parameters:
const WINDOW_SIZE: f64 = 800.0; // Simulation window size
const BASE_BEING_SIZE: f64 = 10.0; // Base size for beings
const MAX_BEINGS: usize = 260; // Maximum population
const MAX_FOOD: usize = 990; // Maximum food items
const FOOD_SPAWN_RATE: f64 = 0.99; // Food spawn probability
const ENERGY_DECAY: f32 = 0.000001; // Energy loss rate
- ESC: Exit simulation
- Window close: Exit simulation
The simulation automatically scales to use available CPU cores. For best performance:
- Use
--release
flag for optimized builds - Larger populations (>100 beings) benefit most from parallelization
- Stats display shows active thread count
To modify being behaviors, adjust the update methods in:
update_herbivore()
update_carnivore()
update_omnivore()
MIT License - See LICENSE for details.