Skip to content

Commit

Permalink
Whoops, behavior ordering needs to be deterministic.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Sep 19, 2023
1 parent 1b096ed commit 2079af2
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions all-is-cubes/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use core::any::TypeId;
use core::fmt::{self, Debug};

use downcast_rs::{impl_downcast, Downcast};
use hashbrown::HashMap as HbHashMap;

use crate::time::Tick;
use crate::transaction::{self, Merge as _, Transaction};
Expand Down Expand Up @@ -109,19 +108,26 @@ pub enum Then {
///
#[doc = include_str!("save/serde-warning.md")]
pub struct BehaviorSet<H: BehaviorHost> {
members: HbHashMap<Key, BehaviorSetEntry<H>>,
/// Note that this map is deterministically ordered, so any incidental things
/// depending on ordering, such as [`Self::query()`] will be deterministic.
/// (Transaction merges would prevent nondeterministic gameplay outcomes, but
/// it still wouldn't be ideal.)
members: BTreeMap<Key, BehaviorSetEntry<H>>,
}

impl<H: BehaviorHost> BehaviorSet<H> {
/// Constructs an empty [`BehaviorSet`].
pub fn new() -> Self {
BehaviorSet {
members: HbHashMap::new(),
members: BTreeMap::new(),
}
}

/// Find behaviors of a specified type.
///
/// The behaviors will be returned in a deterministic order. In the current
/// implementation, that order is the order in which they were added.
///
/// TODO: Allow querying by attachment details (spatial, etc)
pub fn query<T: Behavior<H>>(&self) -> impl Iterator<Item = QueryItem<'_, H, T>> + '_ {
self.query_any(Some(TypeId::of::<T>())).map(
Expand All @@ -137,6 +143,9 @@ impl<H: BehaviorHost> BehaviorSet<H> {

/// Find behaviors by filter criteria. All `None`s mean “anything”.
///
/// The behaviors will be returned in a deterministic order. In the current
/// implementation, that order is the order in which they were added.
///
/// TODO: Allow querying by attachment details (spatial, etc)
pub fn query_any<'a>(
&'a self,
Expand Down

0 comments on commit 2079af2

Please sign in to comment.