From: Ian Jackson Date: Sun, 7 Mar 2021 11:00:43 +0000 (+0000) Subject: hidden: implement permutation X-Git-Tag: otter-0.4.0~235 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=b26bd7ccd8fe54a221bca451cf22c4e3f853b517;p=otter.git hidden: implement permutation Signed-off-by: Ian Jackson --- diff --git a/src/hidden.rs b/src/hidden.rs index 4a6b1e5c..d64a9803 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -131,8 +131,8 @@ mod vpid { #[derive(Clone,Debug,Default,Serialize,Deserialize)] pub struct PerPlayerIdMap { - f: SecondarySlotMap, - r: DenseSlotMap, + pub(in super) f: SecondarySlotMap, + pub(in super) r: DenseSlotMap, } impl PerPlayerIdMap { @@ -190,6 +190,11 @@ mod vpid { type NR = NotchRecord; impl NotchRecord { + fn piece(&self) -> Option { match self { + &NR::Piece(p) => Some(p), + &NR::Free(_) => None, + }} + fn unwrap_free(&self) -> NotchPtr { match self { &NR::Free(ptr) => ptr, _ => panic!(), @@ -385,6 +390,53 @@ mod vpid { // We choose a single permutation of the pieces in the // Occultation::notches. + let permu = { + let mut permu: Vec<_> = + occ.notches.table.iter() + .filter_map(NR::piece) + .collect(); + + let mut rng = thread_rng(); + permu.shuffle(&mut rng); + permu + }; + + let new_notches = { + let mut permus = permu.iter(); + let new_notches = occ.notches.table.iter().map(|nr| match nr { + &f@ NR::Free(_) => f, + NR::Piece(p) => NR::Piece(*permus.next().unwrap()), + }) + .collect::>(); + permus.next().map(|x| panic!("{:?}", x)); + new_notches + }; + + for gpl in gplayers.values_mut() { + + // xxx don't do this for Visible, check occk + + let mut fwd_updates = vec![]; + for (old, new) in izip!(&occ.notches.table, &new_notches) { + if_chain! { + if let Some((old, new)) = zip_eq(old.piece(), new.piece()).next(); + if let Some(vpid) = gpl.idmap.fwd(old); + then { + fwd_updates.push((new, vpid)); + if let Some(e) = gpl.idmap.r.get_mut(vpid) { + *e = new; + } + } + } + } + + for (new, vpid) in fwd_updates { + gpl.idmap.f.insert(new, vpid); + } + + } + occ.notches.table = new_notches; + dbgc!(&occ); } } diff --git a/src/prelude.rs b/src/prelude.rs index 7baa8932..53c054d4 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -63,7 +63,7 @@ pub use flexi_logger::LogSpecification; pub use fs2::FileExt; pub use if_chain::if_chain; pub use index_vec::{define_index_type, index_vec, IndexSlice, IndexVec}; -pub use itertools::{izip, EitherOrBoth, Itertools}; +pub use itertools::{izip, zip_eq, EitherOrBoth, Itertools}; pub use lazy_static::lazy_static; pub use log::{debug, error, info, trace, warn}; pub use log::{log, log_enabled}; @@ -77,6 +77,7 @@ pub use percent_encoding::NON_ALPHANUMERIC; pub use rand::distributions::Alphanumeric; pub use rand::thread_rng; pub use rand::Rng; +pub use rand::prelude::SliceRandom; pub use regex::Regex; pub use serde::ser::SerializeTuple; pub use serde::{de::DeserializeOwned, Deserialize, Serialize};