1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020-2021 Ian Jackson and contributors to Otter
// SPDX-License-Identifier: AGPL-3.0-or-later
// There is NO WARRANTY.

use super::*; // we are otter::updates::movehist

pub const LENS: &[usize] = &[ 0, 1, 3, 10 ]; // want option with no name
pub const LEN_MAX: usize = 10;
pub const LEN_DEF_I: usize = 1;
pub const MIN_DIST: f64 = 7.5;

#[test]
fn movehist_lens() {
  assert_eq!(
    LENS.iter().max(),
    Some(&LEN_MAX),
  );
  assert!( LENS.get(LEN_DEF_I).is_some() );
  assert_eq!( LENS.iter().cloned().fold(None, |b, i| {
    let i = Some(i);
    assert!(i > b);
    i
  }),
              Some(LEN_MAX) );
}

#[derive(Debug,Copy,Clone,Serialize,Deserialize)]
pub struct Posx { // usual variable: posx
  pub pos: Pos,
  pub angle: CompassAngle,
  pub facehint: Option<FaceId>,
}

#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Ent {
  pub held: PlayerId,
  pub posx: OldNew<Posx>,
  pub diff: DiffToShow,
}

#[derive(Debug,Copy,Clone,Serialize,Deserialize)]
pub enum DiffToShow {
  Moved { d: f64 },
}

#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct PlHist {
  pub hist: VecDeque<Ent>,
}

// ---------- non-public structs ----------

#[derive(Debug,Clone,Serialize,Deserialize,Default)]
pub struct PlHeld {
  held: SparseSecondaryMap<VisiblePieceId, PlHistLast>,
}

#[derive(Debug,Copy,Clone,Serialize,Deserialize)]
struct PlHistLast {
  held: PlayerId,
  posx: Posx,
}

impl Default for PlHist {
  fn default() -> PlHist { PlHist {
    hist: VecDeque::with_capacity(LEN_MAX),
  } }
}

impl Posx {
  fn differs_significantly(&self, other: &Posx)
                           -> Option<DiffToShow> {
    use DiffToShow as D;

    match (|| Ok::<_,CoordinateOverflow> ({
      (self.pos - other.pos)?.len()?
    }))() {
      Ok(d) if d >= MIN_DIST as f64 => return Some(D::Moved{ d }),
      _ => {},
    }

    None
  }
}

impl PlHist {
  pub fn clear(&mut self) { self.hist.clear() }
}

// We're track this on behalf of the client, based on the updates
// we are sending.  That means we don't ahve to worry about
// occultation, etc. etc.
pub fn peek_prep_update(gs: &mut GameState, peek: &PreparedUpdateEntry)
                        -> Option<PreparedUpdateEntry> {
  if_let!{ PUE::Piece(PUE_P { ops,.. }) = peek; else return None; }

  let mut pu = SecondarySlotMap::new();
  for (player, &PreparedPieceUpdate { ref op, piece,.. }) in ops { if_chain! {
    if let Some(ns) = op.new_state();
    if let Some(gpl) = wants!( gs.players.get_mut(player), ?player);
    if let Some(mut ent) = wants!( gpl.moveheld.held.entry(piece), ?piece);
    let &PreparedPieceState { pos, angle, facehint, .. } = ns;
    let new_posx = Posx { pos, angle, facehint };

    then {
      if let slotmap::sparse_secondary::Entry::Occupied(ref mut oe) = ent {
        let last = oe.get();
        if ns.held == Some(last.held) { continue }

        if let Some(diff) = new_posx.differs_significantly(&last.posx) {
          // Generate an update
          let histent = Ent {
            held: last.held,
            posx: OldNew::from([last.posx, new_posx]),
            diff,
          };
          if gpl.movehist.hist.len() == LEN_MAX {
            gpl.movehist.hist.pop_front();
          }
          gpl.movehist.hist.push_back(histent.clone());
          pu.insert(player, histent);
        }
      }

      if let Some(held) = ns.held {
        ent.insert(PlHistLast { held: held, posx: new_posx });
      } else {
        ent.remove();
      }
    }
  } }

  if pu.is_empty() { return None }

  Some(PUE::MoveHistEnt(pu))
}