summaryrefslogtreecommitdiff
path: root/src/sim/cell/cell.rs
blob: 2a16d0c71b02631a1ca26e926b88ad35443457db (plain)
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
use crate::{config::SETTLED_THRESOHLD, sim::cell::materials::MaterialId};

#[derive(Clone, Copy)]
pub struct Cell {
    pub material: MaterialId,
    pub flags: u8,
    pub data: u16,
}

impl Cell {
    const FLAG_PARITY: u8 = 0b0000_0001;
    const FLAG_ENTITY: u8 = 0b0000_0010;
    const MASK_SETTLED: u8 = 0b0001_1100;

    #[inline]
    pub fn parity(self) -> u8 {
        self.flags & Self::FLAG_PARITY
    }
    #[inline]
    pub fn flip_parity(&mut self) {
        self.flags ^= Self::FLAG_PARITY;
    }
    #[inline]
    pub fn match_parity(&mut self, seqno: u64) {
        let parity = seqno % 2 != 0;
        if parity {
            self.flags |= Self::FLAG_PARITY;
        } else {
            self.flags = self.flags & !Self::FLAG_PARITY
        }
    }

    #[inline]
    pub fn entity(self) -> bool {
        (self.flags & Self::FLAG_ENTITY) == Self::FLAG_ENTITY
    }
    #[inline]
    pub fn set_entity(&mut self, rb: bool) {
        if rb {
            self.flags |= Self::FLAG_ENTITY
        } else {
            self.flags = self.flags & !Self::FLAG_ENTITY
        }
    }

    #[inline]
    pub fn settled(self) -> u8 {
        (self.flags & Self::MASK_SETTLED) >> 2
    }
    #[inline]
    fn set_settled(&mut self, settled: u8) {
        debug_assert!(settled <= 7);
        self.flags = (settled << 2) | (self.flags & !Self::MASK_SETTLED);
    }
    #[inline]
    pub fn reset_settled(&mut self) {
        self.flags &= !Self::MASK_SETTLED;
    }

    #[inline]
    pub fn increment_settled(&mut self) {
        let s = self.settled();
        if s < SETTLED_THRESOHLD {
            self.set_settled(s + 1);
        }
    }
}

impl Cell {
    pub fn void() -> Cell {
        Cell {
            material: MaterialId::Void,
            flags: 0,
            data: 0,
        }
    }
    pub fn from_material(material: MaterialId) -> Cell {
        Cell {
            material,
            flags: 0,
            data: 0,
        }
    }
}