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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::sim::direction::Direction;
use crate::sim::pose::Pose;
/// Which claw is oversized, i.e. which side this crab tries first when its
/// forward path is blocked. The one-bit divergence from ChuChu Rocket that
/// turns herding puzzles into sorting puzzles (spec §2).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Handedness {
Left,
Right,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum CrabKind {
Common,
Juvenile,
Giant,
/// Banking one starts the 10-second lure that draws every loose crab to
/// the banking castle.
Molting,
/// The jackpot (ChuChu Rocket's gold mouse, re-shelled): vanishingly
/// rare, quick, and worth a small castle tier by itself.
Golden,
/// The "?" mouse, re-shelled: banking it spins the tide-event roulette
/// on boards where events are enabled.
Sparkling,
}
impl CrabKind {
pub const ALL: [CrabKind; 6] = [
CrabKind::Common,
CrabKind::Juvenile,
CrabKind::Giant,
CrabKind::Molting,
CrabKind::Golden,
CrabKind::Sparkling,
];
/// The level-format token; `from_token` is its inverse.
pub fn token(self) -> &'static str {
match self {
CrabKind::Common => "common",
CrabKind::Juvenile => "juvenile",
CrabKind::Giant => "giant",
CrabKind::Molting => "molting",
CrabKind::Golden => "golden",
CrabKind::Sparkling => "sparkling",
}
}
pub fn from_token(token: &str) -> Option<CrabKind> {
Self::ALL.iter().copied().find(|k| k.token() == token)
}
/// Walking speed in subunits per tick (one tile = 256 subunits, spec §4.2).
pub fn speed(self) -> u16 {
match self {
CrabKind::Common | CrabKind::Molting | CrabKind::Sparkling => 12,
CrabKind::Juvenile => 18, // 1.5×
CrabKind::Giant => 7, // 0.6× of 12 is 7.2; integer sim rounds down
CrabKind::Golden => 15, // 1.25×: catchable, but it makes you work
}
}
/// Score awarded when banked. Every kind banks differently: commons are
/// the bread and butter, juveniles pay a little extra for being hard to
/// route, molting crabs are prized (and start the lure), giants are the
/// jackpot worth protecting.
pub fn value(self) -> u32 {
match self {
CrabKind::Common => 1,
CrabKind::Juvenile => 2,
CrabKind::Molting => 5,
CrabKind::Giant => 10,
CrabKind::Golden => 50,
CrabKind::Sparkling => 1, // the event is the prize
}
}
pub(crate) fn id(self) -> u8 {
match self {
CrabKind::Common => 0,
CrabKind::Juvenile => 1,
CrabKind::Giant => 2,
CrabKind::Molting => 3,
CrabKind::Golden => 4,
CrabKind::Sparkling => 5,
}
}
}
/// One crab. Position is integer-only: the crab is `progress` subunits past the
/// centre of `tile`, moving toward the centre of the next tile in `dir`.
/// `prev` holds last tick's position for render-side interpolation (spec §7.4)
/// and is never read by the simulation itself.
#[derive(Clone, Copy, Debug)]
pub struct Crab {
/// Stable identity for the render layer's sprite mapping; unique within a
/// match, assigned by the `Board` at spawn.
pub id: u32,
pub tile: u16,
pub dir: Direction,
pub progress: u16,
pub prev: Pose,
pub handed: Handedness,
pub kind: CrabKind,
}
impl Crab {
/// What banking this crab does to its banker's score: `value`, the
/// worth the board gives its kind (`Board::crab_value`), or under a
/// Right Claws call twice that for a right claw and that much taken
/// away for a left one. The one statement of the rule, read by the
/// score itself and by everything that shows it.
pub fn bank_points(&self, value: u32, claw_call: bool) -> i32 {
let value = value as i32;
match (claw_call, self.handed) {
(false, _) => value,
(true, Handedness::Right) => value * 2,
(true, Handedness::Left) => -value,
}
}
/// The current position as one value, so a tick can save it into
/// `prev` in a single assignment.
pub fn pose(&self) -> Pose {
Pose {
tile: self.tile,
dir: self.dir,
progress: self.progress,
}
}
}
impl Handedness {
pub(crate) fn id(self) -> u8 {
match self {
Handedness::Left => 0,
Handedness::Right => 1,
}
}
/// The level-format token (`L`/`R`); `from_token` is its inverse.
pub fn token(self) -> &'static str {
match self {
Handedness::Left => "L",
Handedness::Right => "R",
}
}
pub fn from_token(token: &str) -> Option<Handedness> {
match token {
"L" => Some(Handedness::Left),
"R" => Some(Handedness::Right),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::{CrabKind, Handedness};
#[test]
fn tokens_round_trip_for_every_kind() {
for kind in CrabKind::ALL {
assert_eq!(CrabKind::from_token(kind.token()), Some(kind));
}
assert_eq!(CrabKind::from_token("hermit"), None);
for handed in [Handedness::Left, Handedness::Right] {
assert_eq!(Handedness::from_token(handed.token()), Some(handed));
}
}
#[test]
fn golden_crab_is_the_jackpot() {
assert_eq!(CrabKind::Golden.value(), 50);
assert!(CrabKind::Golden.speed() > CrabKind::Common.speed());
assert!(CrabKind::Golden.speed() < CrabKind::Juvenile.speed());
}
}