pub struct Pattern {
pub chance: f32,
pub priority: f32,
pub before: CellGrid,
pub after: CellGrid,
}Expand description
A pattern consists both of a grid of cells to search for and a grid of cells to replace it with.
The before pattern may contain wildcards * to match any character.
The after pattern may contain wildcards * to not mutate that cell and simply keep its previous value.
Whenever a pattern matches, the attribute might randomly be discarded instead of being applied.
The chance attribute describes the likelihood of the pattern being applied without discard, i.e. any value over 1.0 means the pattern will always be applied when it matches.
If multiple patterns are applicable within a time step, the one with higher priority will always be applied first. Only if no cell concerning the second pattern has been mutated, the second pattern will apply also.
use cellumina::rule::Rule;
let rule = cellumina::rule::PatternRule::from_patterns(
&[
cellumina::rule::Pattern{
chance: 1.0,
priority: 1.0,
before: grid::grid![['X'][' ']],
after: grid::grid![[' ']['X']],
},
cellumina::rule::Pattern{
chance: 1.0,
priority: 0.5,
before: grid::grid![[' ', 'X']['X', ' ']],
after: grid::grid![['X', 'X'][' ', ' ']],
},
],
cellumina::rule::BoundaryBehaviour::Symbol('_'),
cellumina::rule::BoundaryBehaviour::Symbol('_'),
);
let mut grid = grid::grid![[' ', 'X']['X', ' '][' ', ' ']];
rule.transform(&mut grid);
assert_eq!(grid, grid::grid![[' ', ' '][' ', 'X']['X', ' ']]);
rule.transform(&mut grid);
assert_eq!(grid, grid::grid![[' ', ' '][' ', ' ']['X', 'X']]);
let rule2 = cellumina::rule::PatternRule::from(rule.to_string().as_str());
grid = grid::grid![[' ', 'X']['X', ' '][' ', ' ']];
rule2.transform(&mut grid);
assert_eq!(grid, grid::grid![[' ', ' '][' ', 'X']['X', ' ']]);
rule2.transform(&mut grid);
assert_eq!(grid, grid::grid![[' ', ' '][' ', ' ']['X', 'X']]);Fields§
§chance: f32The chance for the pattern to apply on a match.
priority: f32The priority of this pattern over others.
before: CellGridThe cell pattern to search for.
after: CellGridThe cell pattern it should be replaced with.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Pattern
impl<'de> Deserialize<'de> for Pattern
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<&str> for Pattern
use cellumina::rule::Rule;
let pattern = cellumina::rule::Pattern{
chance: 1.0,
priority: 1.0,
before: grid::grid![[' ', ' ', 'X'][' ', 'X', 'X']],
after: grid::grid![['*', '*', ' ']['X', '*', '*']],
};
let pattern2 = cellumina::rule::Pattern::from(pattern.to_string().as_str());
assert_eq!(pattern.chance, pattern2.chance);
assert_eq!(pattern.priority, pattern2.priority);
assert_eq!(pattern.before.rows(), pattern2.before.rows());
assert_eq!(pattern.before.cols(), pattern2.before.cols());
assert_eq!(pattern.after.rows(), pattern2.after.rows());
assert_eq!(pattern.after.cols(), pattern2.after.cols());
for (c1, c2) in pattern.before.iter().zip(pattern2.before.iter()) {
assert_eq!(*c1, *c2);
}
for (c1, c2) in pattern.after.iter().zip(pattern2.after.iter()) {
assert_eq!(*c1, *c2);
}
impl From<&str> for Pattern
use cellumina::rule::Rule;
let pattern = cellumina::rule::Pattern{
chance: 1.0,
priority: 1.0,
before: grid::grid![[' ', ' ', 'X'][' ', 'X', 'X']],
after: grid::grid![['*', '*', ' ']['X', '*', '*']],
};
let pattern2 = cellumina::rule::Pattern::from(pattern.to_string().as_str());
assert_eq!(pattern.chance, pattern2.chance);
assert_eq!(pattern.priority, pattern2.priority);
assert_eq!(pattern.before.rows(), pattern2.before.rows());
assert_eq!(pattern.before.cols(), pattern2.before.cols());
assert_eq!(pattern.after.rows(), pattern2.after.rows());
assert_eq!(pattern.after.cols(), pattern2.after.cols());
for (c1, c2) in pattern.before.iter().zip(pattern2.before.iter()) {
assert_eq!(*c1, *c2);
}
for (c1, c2) in pattern.after.iter().zip(pattern2.after.iter()) {
assert_eq!(*c1, *c2);
}Auto Trait Implementations§
impl Freeze for Pattern
impl RefUnwindSafe for Pattern
impl Send for Pattern
impl Sync for Pattern
impl Unpin for Pattern
impl UnwindSafe for Pattern
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().