[−][src]Struct conway::universe::Universe
Represents a wrapping universe in Conway's game of life.
Methods
impl Universe
[src]
pub fn get_cell_state(
&mut self,
col: usize,
row: usize,
opt_player_id: Option<usize>
) -> CellState
[src]
&mut self,
col: usize,
row: usize,
opt_player_id: Option<usize>
) -> CellState
pub fn set_unchecked(&mut self, col: usize, row: usize, new_state: CellState)
[src]
Sets the state of a cell in the latest generation, with minimal checking. It doesn't
support setting CellState::Fog
.
Panics
Panics if an attempt is made to set an unknown cell.
pub fn set(
&mut self,
col: usize,
row: usize,
new_state: CellState,
player_id: usize
)
[src]
&mut self,
col: usize,
row: usize,
new_state: CellState,
player_id: usize
)
Checked set - check for:
- current cell state (can't change wall)
- player writable region
- fog
- if current cell is alive, player_id matches player_id argument
If any of the above checks fail, do nothing.
Panics
Panic if player_id inside CellState does not match player_id argument.
pub fn toggle_unchecked(
&mut self,
col: usize,
row: usize,
opt_player_id: Option<usize>
) -> CellState
[src]
&mut self,
col: usize,
row: usize,
opt_player_id: Option<usize>
) -> CellState
Switches any non-dead state to CellState::Dead. Switches CellState::Dead to CellState::Alive(opt_player_id) and clears fog for that player, if any.
This operation works in three steps:
- Toggle alive/dead cell in the current generation state cell grid
- Clear all players' cell
- If general cell transitioned Dead->Alive, then set requested player's cell
The new value of the cell is returned.
pub fn toggle(
&mut self,
col: usize,
row: usize,
player_id: usize
) -> ConwayResult<CellState>
[src]
&mut self,
col: usize,
row: usize,
player_id: usize
) -> ConwayResult<CellState>
Checked toggle - switch between CellState::Alive and CellState::Dead.
Errors
It is an error to toggle outside player's writable area, or to toggle a wall or an unknown cell.
pub fn new(
width: usize,
height: usize,
is_server: bool,
history: usize,
num_players: usize,
player_writable: Vec<Region>,
fog_radius: usize
) -> ConwayResult<Universe>
[src]
width: usize,
height: usize,
is_server: bool,
history: usize,
num_players: usize,
player_writable: Vec<Region>,
fog_radius: usize
) -> ConwayResult<Universe>
Instantiate a new blank universe with the given width and height, in cells. The universe is at generation 1.
Note: it is easier to use BigBang
to build a Universe
, as that has default values
that can be overridden as needed.
pub fn latest_gen(&self) -> usize
[src]
Get the latest generation number (1-based).
pub fn next(&mut self) -> usize
[src]
Compute the next generation. Returns the new latest generation number.
pub fn each_non_dead(
&self,
region: Region,
visibility: Option<usize>,
callback: &mut dyn FnMut(usize, usize, CellState)
)
[src]
&self,
region: Region,
visibility: Option<usize>,
callback: &mut dyn FnMut(usize, usize, CellState)
)
Iterate over every non-dead cell in the universe for the current generation. region
is
the rectangular area used for restricting results. visibility
is an optional player_id;
if specified, causes cells not visible to the player to be passed as CellState::Fog
to the
callback.
Callback receives (col
, row
, cell_state
).
Panics
Does numerous consistency checks on the bitmaps, and panics if inconsistencies are found.
pub fn each_non_dead_full(
&self,
visibility: Option<usize>,
callback: &mut dyn FnMut(usize, usize, CellState)
)
[src]
&self,
visibility: Option<usize>,
callback: &mut dyn FnMut(usize, usize, CellState)
)
Iterate over every non-dead cell in the universe for the current generation.
visibility
is an optional player_id, allowing filtering based on fog.
Callback receives (col, row, cell_state).
pub fn region(&self) -> Region
[src]
Get a Region of the same size as the universe.
pub fn copy_from_bit_grid(
&mut self,
src: &BitGrid,
dst_region: Region,
opt_player_id: Option<usize>
)
[src]
&mut self,
src: &BitGrid,
dst_region: Region,
opt_player_id: Option<usize>
)
Copies from src
BitGrid to this GenState as the player specified by opt_player_id
,
unless opt_player_id
is None
.
This function is similar to GenState::copy_from_bit_grid
except that 1) when a player_id
is specified, the specified player's writable region is used, and 2) the latest generation
is written to.
Panics if opt_player_id
is Some(player_id)
and player_id
is out of range.
pub fn apply(
&mut self,
diff: &GenStateDiff,
visibility: Option<usize>
) -> ConwayResult<Option<usize>>
[src]
&mut self,
diff: &GenStateDiff,
visibility: Option<usize>
) -> ConwayResult<Option<usize>>
Apply diff
to our Universe
. Generally this is executed by the client in response to an
update from the server.
Arguments
diff
: reference to aGenStateDiff
containing a base generation number, a new generation number, and a pattern showing how to make the new generation out of the base.visibility
: an optionalplayer_id
indicating the player to apply this for. IfNone
, the pattern must not have any fog.
Return values
Ok(Some(new_gen))
if the update was applied.Ok(None)
if the update is valid but was not applied because either:- the generation to be applied is already present,
- there is already a greater generation present, or
- the base generation of this diff (that is,
diff.gen0
) could not be found. A base generation of 0 is a special case -- it is always found.
Err(InvalidData{reason: msg})
if the update is invalid, either because:- the difference between
diff.gen0
anddiff.gen1
is too large. Since the server knows the client's buffer size, this should not happen. In this case, no updates are made to theUniverse
. A base generation of 0 is a special case -- the difference is never too large. - the RLE pattern is invalid. NOTE: in this case, the pattern is only partially written and all other updates (e.g., increasing the generation count) are made as if it were valid.
- the difference between
Panics
This will panic if:
gen0
is not less thangen1
.visibility
is out of range.
pub fn diff(
&self,
gen0: usize,
gen1: usize,
visibility: Option<usize>
) -> Option<GenStateDiff>
[src]
&self,
gen0: usize,
gen1: usize,
visibility: Option<usize>
) -> Option<GenStateDiff>
If it's possible to generate a diff between the GenStates specified by gen0
and gen1
, do
so. Otherwise, return None
.
The optional visibility
specifies the player this is viewed as.
Panics
- Panics if
gen0
>=gen1
. - Panics if
visibility
is out of range.
Trait Implementations
impl CharGrid for Universe
[src]
fn is_valid(ch: char) -> bool
[src]
fn write_at_position(
&mut self,
_col: usize,
_row: usize,
_ch: char,
_visibility: Option<usize>
)
[src]
&mut self,
_col: usize,
_row: usize,
_ch: char,
_visibility: Option<usize>
)
fn width(&self) -> usize
[src]
Return width in cells.
fn height(&self) -> usize
[src]
Return height in cells.
fn get_run(
&self,
col: usize,
row: usize,
visibility: Option<usize>
) -> (usize, char)
[src]
&self,
col: usize,
row: usize,
visibility: Option<usize>
) -> (usize, char)
fn to_pattern(&self, visibility: Option<usize>) -> Pattern
[src]
Returns a Pattern that describes this CharGrid
as viewed by specified player if visibility.is_some()
, or a fog-less view if visibility.is_none()
. Read more
impl Display for Universe
[src]
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,