unbanished_game/behaviour/
loiter_order.rs1use super::activities::MapRect;
12use std::cell::RefCell;
13use std::collections::VecDeque;
14
15thread_local! {
16 static PICKS: RefCell<Option<VecDeque<MapRect>>> = const { RefCell::new(None) };
17}
18
19pub fn set(picks: impl IntoIterator<Item = MapRect>) {
21 PICKS.with(|p| *p.borrow_mut() = Some(picks.into_iter().collect()));
22}
23
24pub(crate) fn place(k: usize, rects: impl Iterator<Item = Option<MapRect>>) -> usize {
27 let Some(want) = PICKS.with(|p| p.borrow_mut().as_mut().and_then(VecDeque::pop_front)) else {
28 return k;
29 };
30 let mut rects = rects;
31 rects.position(|r| r == Some(want)).unwrap_or_else(|| {
32 eprintln!("no object near the citizen has the original's pick {want:?}");
33 k
34 })
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40
41 #[test]
43 fn picks_take_the_member_with_their_rectangle() {
44 let rects = [Some((1, 1, 2, 2)), None, Some((5, 5, 1, 1))];
45 assert_eq!(place(0, rects.iter().copied()), 0);
46 set([(5, 5, 1, 1), (9, 9, 1, 1)]);
47 assert_eq!(place(0, rects.iter().copied()), 2);
48 assert_eq!(place(1, rects.iter().copied()), 1, "no member has it");
49 assert_eq!(place(1, rects.iter().copied()), 1, "the picks ran out");
50 }
51}