Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt config #19

Merged
merged 4 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: "rustfmt"
- name: Check Formatting
run: cargo fmt --all -- --check

Expand Down
6 changes: 6 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
reorder_imports = true
format_code_in_doc_comments = true
normalize_doc_attributes = true
wrap_comments = true
format_strings = true
imports_granularity = "Crate"
14 changes: 10 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

* Added rustfmt config (#19)

## 0.8.0

* Added automatic `CellMap` update on `Cell` component removal
Expand All @@ -17,7 +19,8 @@ by default
* `bevy` 0.10
* (**BREAKING**) `SimulationBatch` is now a unit struct
* (**BREAKING**) Renamed `CellularAutomatonPlugin::new` to `with_time_step`
* Temporarily disabled batching for the `auto-coloring` systems, until this [bug](https://github.com/bevyengine/bevy/pull/8029) is adressed
* Temporarily disabled batching for the `auto-coloring` systems, until this
[bug](https://github.com/bevyengine/bevy/pull/8029) is adressed

## 0.6.0

Expand Down Expand Up @@ -67,7 +70,8 @@ by default
## 0.2.1

* `CellMap`:
* Using a `bevy::utils::Hashmap` instead of the standard library one, which is slightly faster
* Using a `bevy::utils::Hashmap` instead of the standard library one, which
is slightly faster
* Cells system:
* Removed reference counting `Arc` from the `RwLock` for the batched query iterations
* Using batched queries for the coloring systems (`auto-coloring` feature)
Expand All @@ -81,7 +85,8 @@ by default
### Changed (**BREAKING CHANGES**)

* `3D` feature gate is no longer enabled by default
* Complete rework of the `auto-coloring` features, these changes are not detailed as this feature is provided for example purposes.
* Complete rework of the `auto-coloring` features, these changes are not detailed
as this feature is provided for example purposes.

Renamed:

Expand All @@ -100,7 +105,8 @@ Renamed:
* `ImmigrationCellState` for the immigration game (bi color)
* `RainbowCellState` for the rainbow game (gray scale)

The `CellularAutomatonPlugin` now takes an additional `BATCH_SIZE: usize` const parameter defining the new query batch size for better parallel execution.
The `CellularAutomatonPlugin` now takes an additional `BATCH_SIZE: usize` const
parameter defining the new query batch size for better parallel execution.
The system handling cells and states now uses parallel querying with this new parameter

New examples are added.
Expand Down
158 changes: 85 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,91 +10,103 @@

<!-- cargo-sync-readme start -->

`bevy_life` is a generic plugin for [cellular automaton](https://en.wikipedia.org/wiki/Cellular_automaton).
From the classic 2D [Conway’s game of life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) to [`WireWorld`](https://en.wikipedia.org/wiki/Wireworld) and 3D rules, the plugin is completely generic and dynamic.

See:
- [Game of life variations](https://cs.stanford.edu/people/eroberts/courses/soco/projects/2008-09/modeling-natural-systems/gameOfLife2.html)
- [`Wireworld` implementation](https://www.quinapalus.com/wi-index.html) (see this lib's [implementation](https://github.com/ManevilleF/wireworld-rs))

## Bevy versions

The `main` branch follows the released version of `bevy` but I provide the [`bevy-main`](https://github.com/ManevilleF/bevy_life/tree/feat/bevy-main) branch
to follow the `main` branch of `bevy`

| `bevy_life` | `bevy` |
|---------------|-----------|
| 0.3.x | 0.6.x |
| 0.4.x | 0.7.x |
| 0.5.x | 0.8.x |
| 0.6.x | 0.9.x |
| 0.7.x | 0.10.x |
| 0.8.x | 0.11.x |

## How to use

Add a `CellularAutomatonPlugin` to your bevy app:

A `CellularAutomatonPlugin<C, S>` has two generic types:
- `C` -> Any type implementing `Cell`, defining the coordinate system
- `S` -> Any type implementing `CellState`, defining the simulation rules.

You may add as many generic `CellularAutomatonPlugin` as wished, the lib provides some implementations like:
- `GameOfLife2dPlugin`
- `GameOfLife3dPlugin`
- `ImmigrationGame2dPlugin`
- `ImmigrationGame3dPlugin`
- `RainbowGame2dPlugin`
- `RainbowGame3dPlugin`
- `WireWorld2dPlugin`
- `WireWorld3dPlugin`
- `CyclicColors2dPlugin`
- `CyclicColors3dPlugin`

Then you may use bevy as usual and add `impl Cell` and `impl CellState` components to the entities.
The lib provides some implementations like `MooreCell2d` or `MooreCell3d` for cells and `ConwayCellState`, `WireWorldCellState`, etc for states.

You may implement your own *cells* (coordinate system) and *states* (rules) as you want, the cellular automaton system is completely dynamic and generic.

For more information you may look at some examples:
`bevy_life` is a generic plugin for [cellular automaton](https://en.wikipedia.org/wiki/Cellular_automaton).
From the classic 2D [Conway’s game of life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) to [`WireWorld`](https://en.wikipedia.org/wiki/Wireworld) and 3D rules, the plugin is completely generic and dynamic.

See:
- [Game of life variations](https://cs.stanford.edu/people/eroberts/courses/soco/projects/2008-09/modeling-natural-systems/gameOfLife2.html)
- [`Wireworld` implementation](https://www.quinapalus.com/wi-index.html) (see
this lib's [implementation](https://github.com/ManevilleF/wireworld-rs))

## Bevy versions

The `main` branch follows the released version of `bevy` but I provide the [`bevy-main`](https://github.com/ManevilleF/bevy_life/tree/feat/bevy-main) branch
to follow the `main` branch of `bevy`

| `bevy_life` | `bevy` |
|---------------|-----------|
| 0.3.x | 0.6.x |
| 0.4.x | 0.7.x |
| 0.5.x | 0.8.x |
| 0.6.x | 0.9.x |
| 0.7.x | 0.10.x |
| 0.8.x | 0.11.x |

## How to use

Add a `CellularAutomatonPlugin` to your bevy app:

A `CellularAutomatonPlugin<C, S>` has two generic types:
- `C` -> Any type implementing `Cell`, defining the coordinate system
- `S` -> Any type implementing `CellState`, defining the simulation rules.

You may add as many generic `CellularAutomatonPlugin` as wished, the lib
provides some implementations like:
- `GameOfLife2dPlugin`
- `GameOfLife3dPlugin`
- `ImmigrationGame2dPlugin`
- `ImmigrationGame3dPlugin`
- `RainbowGame2dPlugin`
- `RainbowGame3dPlugin`
- `WireWorld2dPlugin`
- `WireWorld3dPlugin`
- `CyclicColors2dPlugin`
- `CyclicColors3dPlugin`

Then you may use bevy as usual and add `impl Cell` and `impl CellState`
components to the entities. The lib provides some implementations like
`MooreCell2d` or `MooreCell3d` for cells and `ConwayCellState`,
`WireWorldCellState`, etc for states.

You may implement your own *cells* (coordinate system) and *states* (rules)
as you want, the cellular automaton system is completely dynamic and
generic.

For more information you may look at some examples:
- The [Classic examples](./examples) showcase the provided implementations
- the [Rock Paper Scissor](./examples/2d_rock_paper_scissor.rs) defines custom rules.
- the [Rock Paper Scissor](./examples/2d_rock_paper_scissor.rs) defines
custom rules.
- the [wireworld](https://github.com/ManevilleF/wireworld-rs) repository

### Pausing
### Pausing

Inserting a `SimulationPause` resource will pause the simulation, removing it wil resume the it.
Inserting a `SimulationPause` resource will pause the simulation, removing
it wil resume the it.

### Parallel execution and batching
### Parallel execution and batching

Inserting a `SimulationBatch` resource will allow parallel computation of cells with custom batch sizes.
Inserting a `SimulationBatch` resource will allow parallel computation of
cells with custom batch sizes.

## Cargo Features
## Cargo Features

No feature is required for the plugin to work and the main traits `Cell` and `CellState` are always available.
But you may enable the following features
No feature is required for the plugin to work and the main traits `Cell` and
`CellState` are always available. But you may enable the following features

- `2D` (enabled by default): Enables 2D types like:
- `MooreCell2d` (square cell with 8 neighbors)
- `NeumannCell2d` (square cell with 4 neighbors)
- `HexagonCell2d` (hexagon cell with 6 neighbors)
- plugin presets: `GameOfLife2dPlugin`, `ImmigrationGame2dPlugin`, `RainbowGame2dPlugin`, `WireWorld2dPlugin`, `CyclicAutomaton2dPlugin`
- `3D`: Enables 3D types like:
- `MooreCell3d` (cube cell with 26 neighbors)
- `NeumannCell3d` (cube cell with 6 neighbors)
- plugin presets: `GameOfLife3dPlugin`, `ImmigrationGame3dPlugin`, `RainbowGame3dPlugin`, `WireWorld3dPlugin`, `CyclicAutomaton3dPlugin`
- `auto-coloring` (Example or debug purpose):
- Enables `CellStateMaterials` resource to contain material handles
- The `CellState` type now requires to build a `CellStateMaterials`
- All `CellState` components with materials will be colored according to their type.
- `2D` (enabled by default): Enables 2D types like:
- `MooreCell2d` (square cell with 8 neighbors)
- `NeumannCell2d` (square cell with 4 neighbors)
- `HexagonCell2d` (hexagon cell with 6 neighbors)
- plugin presets: `GameOfLife2dPlugin`, `ImmigrationGame2dPlugin`,
`RainbowGame2dPlugin`, `WireWorld2dPlugin`, `CyclicAutomaton2dPlugin`
- `3D`: Enables 3D types like:
- `MooreCell3d` (cube cell with 26 neighbors)
- `NeumannCell3d` (cube cell with 6 neighbors)
- plugin presets: `GameOfLife3dPlugin`, `ImmigrationGame3dPlugin`,
`RainbowGame3dPlugin`, `WireWorld3dPlugin`, `CyclicAutomaton3dPlugin`
- `auto-coloring` (Example or debug purpose):
- Enables `CellStateMaterials` resource to contain material handles
- The `CellState` type now requires to build a `CellStateMaterials`
- All `CellState` components with materials will be colored according to
their type.

## Disclaimer
## Disclaimer

This is probably not the fastest rust implementation of a cellular automaton in rust.
For example, using Gosper's [`HashLife`](https://www.drdobbs.com/jvm/an-algorithm-for-compressing-space-and-t/184406478) a classic game of life could be much faster.

This library aim is to be generic and dynamic, so that you can integrate cellular automata to any project in bevy, with any rules, in 2D or 3D.
This is probably not the fastest rust implementation of a cellular automaton
in rust. For example, using Gosper's [`HashLife`](https://www.drdobbs.com/jvm/an-algorithm-for-compressing-space-and-t/184406478) a classic game of life could be much faster.

This library aim is to be generic and dynamic, so that you can integrate
cellular automata to any project in bevy, with any rules, in 2D or 3D.

<!-- cargo-sync-readme end -->

Expand Down
3 changes: 2 additions & 1 deletion src/components/cell/hexagon_2d_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const NEIGHBOR_COORDINATES: [IVec3; 6] = [
IVec3::new(-1, 1, 0),
];

/// Hexagonal 2D cell. It has 6 neighbors and uses `IVec3` coordinates (Cubic coordinates).
/// Hexagonal 2D cell. It has 6 neighbors and uses `IVec3` coordinates (Cubic
/// coordinates).
///
/// ````ascii
/// X
Expand Down
7 changes: 3 additions & 4 deletions src/components/cell/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bevy::prelude::Component;
use std::fmt::Debug;
use std::hash::Hash;
use std::{fmt::Debug, hash::Hash};
#[cfg(feature = "2D")]
pub use {hexagon_2d_cell::*, moore_2d_cell::*, neumann_2d_cell::*};
#[cfg(feature = "3D")]
Expand All @@ -17,8 +16,8 @@ mod neumann_2d_cell;
#[cfg(feature = "3D")]
mod neumann_3d_cell;

/// Trait defining a Cell, every cell type (2d, 3d, hexagonal, etc) must implement this trait
/// and define an associated `Coordinates` type
/// Trait defining a Cell, every cell type (2d, 3d, hexagonal, etc) must
/// implement this trait and define an associated `Coordinates` type
pub trait Cell: Clone + Component {
/// Associated coordinates type
type Coordinates: Clone + Debug + Send + Sync + Eq + Hash;
Expand Down
3 changes: 2 additions & 1 deletion src/components/cell/neumann_3d_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const NEIGHBOR_COORDINATES: [IVec3; 6] = [
IVec3::new(0, 0, 1),
];

/// [Neumann] Classic cube 3D cell, it has 6 neighbors and uses `IVec3` coordinates
/// [Neumann] Classic cube 3D cell, it has 6 neighbors and uses `IVec3`
/// coordinates
///
/// [Neumann]: https://en.wikipedia.org/wiki/Von_Neumann_neighborhood
#[derive(Debug, Clone, Component, Reflect)]
Expand Down
31 changes: 18 additions & 13 deletions src/components/cell_state/conway_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ use bevy::prelude::{Component, Reflect};
use bevy::render::color::Color;
use std::ops::{Deref, DerefMut};

/// Classic cellular automation state and rules following Conway's game of life classic **2333** rules:
/// Classic cellular automation state and rules following Conway's game of life
/// classic **2333** rules:
///
/// - Any live cell with fewer than two live neighbours dies, as if by underpopulation.
/// - Any live cell with two or three live neighbours lives on to the next generation.
/// - Any live cell with more than three live neighbours dies, as if by overpopulation.
/// - Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
/// - Any live cell with fewer than two live neighbours dies, as if by
/// underpopulation.
/// - Any live cell with two or three live neighbours lives on to the next
/// generation.
/// - Any live cell with more than three live neighbours dies, as if by
/// overpopulation.
/// - Any dead cell with exactly three live neighbours becomes a live cell, as
/// if by reproduction.
///
/// A dead cell is `false`, a live cell is `true`
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Component, Reflect)]
Expand Down Expand Up @@ -61,7 +66,7 @@ mod tests {
let cell_state = ConwayCellState(true);

// 4 alive neighbors
let neighbors = vec![
let neighbors = [
false.into(),
true.into(),
false.into(),
Expand All @@ -75,7 +80,7 @@ mod tests {
let new_state = cell_state.new_cell_state(neighbors.iter());
assert!(!new_state.0);
// 8 alive neighbors
let neighbors = vec![
let neighbors = [
true.into(),
true.into(),
true.into(),
Expand All @@ -95,7 +100,7 @@ mod tests {
let cell_state = ConwayCellState(true);

// 3 alive neighbors
let neighbors = vec![
let neighbors = [
false.into(),
true.into(),
false.into(),
Expand All @@ -110,7 +115,7 @@ mod tests {
assert!(new_state.0);

// 2 alive neighbors
let neighbors = vec![
let neighbors = [
false.into(),
true.into(),
false.into(),
Expand All @@ -126,7 +131,7 @@ mod tests {

// 2 alive neighbors but "off"
let cell_state = ConwayCellState(false);
let neighbors = vec![
let neighbors = [
false.into(),
true.into(),
false.into(),
Expand All @@ -146,7 +151,7 @@ mod tests {
let cell_state = ConwayCellState(false);

// 3 alive neighbors
let neighbors = vec![
let neighbors = [
false.into(),
true.into(),
false.into(),
Expand All @@ -166,7 +171,7 @@ mod tests {
let cell_state = ConwayCellState(true);

// 1 alive neighbors
let neighbors = vec![
let neighbors = [
false.into(),
false.into(),
false.into(),
Expand All @@ -181,7 +186,7 @@ mod tests {
assert!(!new_state.0);

// 0 alive neighbors
let neighbors = vec![
let neighbors = [
false.into(),
false.into(),
false.into(),
Expand Down
Loading