Skip to main content

cuqueclicker_lib/game/tree/
coord.rs

1//! Lot coordinates on the infinite tree canvas.
2
3use serde::{Deserialize, Serialize};
4
5/// Identifies a lot in the infinite procedural tree. The pair `(x, y)` is
6/// the **stable id** of a node — saves store only owned lot coords, and the
7/// generator regenerates every other property from `(TREE_SEED, x, y)`.
8///
9/// Origin `(0, 0)` is the player's starting lot — the Index Finger homeland.
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub struct TreeCoord {
12    pub x: i32,
13    pub y: i32,
14}
15
16impl TreeCoord {
17    pub const ORIGIN: TreeCoord = TreeCoord { x: 0, y: 0 };
18
19    pub const fn new(x: i32, y: i32) -> Self {
20        Self { x, y }
21    }
22
23    /// Manhattan distance from origin, saturating so `(i32::MIN, i32::MIN)`
24    /// returns `u32::MAX` instead of overflowing the sum (each
25    /// `unsigned_abs` is up to `2^31`; the plain sum overflows at
26    /// `i32::MIN + i32::MIN`, panicking in debug builds).
27    pub fn manhattan(self) -> u32 {
28        self.x.unsigned_abs().saturating_add(self.y.unsigned_abs())
29    }
30}