pinch-points 0.8.0

A fast, kid-friendly crab-routing game: route streams of crabs into your sandcastle before time runs out
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! The brush: what the editor paints, the key that loads each one, and the
//! edits a press makes to the sand under the cursor.

use crate::sim::MAX_PLAYERS;
use crate::sim::{Board, CrabKind, Direction, Handedness, Spawner, TileKind};
use bevy::prelude::*;

pub(super) const SPAWNER_PERIOD: u32 = 60;

/// What the brush paints. One name for each thing the sand can hold, so the
/// palette, the key that selects it and the edit it makes are three views
/// of one list rather than three lists that have to agree.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Brush {
    /// Bare sand, which is also the eraser: the only way to clear a tile
    /// without knowing what is already on it.
    #[default]
    Sand,
    Rock,
    Castle,
    Hole,
    Log,
    Weed,
    Pool,
    Crab,
    Gull,
}

impl Brush {
    pub const ALL: [Brush; 9] = [
        Brush::Sand,
        Brush::Rock,
        Brush::Castle,
        Brush::Hole,
        Brush::Log,
        Brush::Weed,
        Brush::Pool,
        Brush::Crab,
        Brush::Gull,
    ];

    /// The key that loads this brush, which is also the shortcut that
    /// paints it outright. Kept as they were where they could be, so a
    /// hand that already knows the editor does not have to learn it again.
    ///
    /// None of them may be W, A, S or D: those walk the cursor, and a key
    /// that does both walks the cursor *and* changes the brush. Weed was on
    /// W, so moving up armed the kelp; `no_brush_takes_a_movement_key`
    /// holds the line.
    pub fn key(self) -> KeyCode {
        match self {
            Brush::Sand => KeyCode::KeyX,
            Brush::Rock => KeyCode::KeyR,
            Brush::Castle => KeyCode::KeyC,
            Brush::Hole => KeyCode::KeyH,
            Brush::Log => KeyCode::KeyL,
            Brush::Weed => KeyCode::KeyE,
            Brush::Pool => KeyCode::KeyP,
            Brush::Crab => KeyCode::KeyB,
            Brush::Gull => KeyCode::KeyG,
        }
    }

    /// The letter the palette shows beside the brush: the one on the key
    /// that loads it, always. The two lists have disagreed, Weed moving off
    /// W to E while the palette went on saying W, so
    /// `letters_name_their_keys` holds them together.
    pub fn letter(self) -> &'static str {
        match self {
            Brush::Sand => "X",
            Brush::Rock => "R",
            Brush::Castle => "C",
            Brush::Hole => "H",
            Brush::Log => "L",
            Brush::Weed => "E",
            Brush::Pool => "P",
            Brush::Crab => "B",
            Brush::Gull => "G",
        }
    }

    pub fn label(self, tr: &crate::app::i18n::Tr) -> &'static str {
        tr.ed_brushes[Brush::ALL.iter().position(|b| *b == self).unwrap_or(0)]
    }

    pub fn icon(self, art: &crate::app::art::Art) -> Handle<Image> {
        match self {
            Brush::Sand => art.sand_a.clone(),
            Brush::Rock => art.rock.clone(),
            Brush::Castle => art.castle.clone(),
            Brush::Hole => art.hole.clone(),
            Brush::Log => art.log.clone(),
            Brush::Weed => art.kelp.clone(),
            Brush::Pool => art.pool.clone(),
            Brush::Crab => art.crab.clone(),
            Brush::Gull => art.gull.clone(),
        }
    }
}

/// Step the castle on a tile through every seat and then away again,
/// so one key paints every owner. Anything else on the tile is replaced
/// (and its creatures swept) on the way in.
pub(super) fn cycle_castle(board: &mut Board, x: u8, y: u8) {
    let kind = match board.tile_at(x, y) {
        TileKind::Castle(owner) if usize::from(owner) + 1 < MAX_PLAYERS => {
            TileKind::Castle(owner + 1)
        }
        TileKind::Castle(_) => TileKind::Empty,
        TileKind::Empty
        | TileKind::Rock
        | TileKind::Spawner(_)
        | TileKind::Turnstile { .. }
        | TileKind::Kelp
        | TileKind::Pool => {
            board.remove_crabs_at(x, y);
            board.remove_gulls_at(x, y);
            TileKind::Castle(0)
        }
    };
    board.set_tile(x, y, kind);
}

/// Put `brush` on the tile under the cursor.
///
/// Every brush replaces whatever was there, rather than toggling it off
/// again: a palette that paints on the first press and erases on the second
/// is a palette that cannot be dragged across a row. `Sand` is the eraser,
/// and it is the only way to clear a tile without first knowing what is on
/// it.
pub(super) fn paint(board: &mut Board, x: u8, y: u8, brush: Brush) {
    // Two of them cycle rather than replace, because they carry a value the
    // player has to be able to reach: which seat a castle belongs to, and
    // which way a hole faces.
    match brush {
        Brush::Castle => return cycle_castle(board, x, y),
        Brush::Hole => {
            let kind = match board.tile_at(x, y) {
                TileKind::Spawner(s) => match next_dir(s.dir) {
                    Some(dir) => TileKind::Spawner(Spawner {
                        dir,
                        period: SPAWNER_PERIOD,
                    }),
                    None => TileKind::Empty,
                },
                TileKind::Empty
                | TileKind::Rock
                | TileKind::Castle(_)
                | TileKind::Turnstile { .. }
                | TileKind::Kelp
                | TileKind::Pool => {
                    board.remove_crabs_at(x, y);
                    board.remove_gulls_at(x, y);
                    TileKind::Spawner(Spawner {
                        dir: Direction::Right,
                        period: SPAWNER_PERIOD,
                    })
                }
            };
            return board.set_tile(x, y, kind);
        }
        Brush::Crab => return cycle_crab(board, x, y),
        Brush::Gull => {
            let tile = board.index_of(x, y);
            if board.gulls().iter().any(|g| g.tile == tile) {
                board.remove_gulls_at(x, y);
            } else if board.gull_may_stand(x, y) {
                board.spawn_gull(x, y, Direction::Right);
            }
            return;
        }
        Brush::Sand | Brush::Rock | Brush::Log | Brush::Weed | Brush::Pool => {}
    }
    // The rest are plain ground. Anything standing on the tile goes with
    // it, since a crab inside a rock is not a thing the sim has a rule for.
    board.remove_crabs_at(x, y);
    board.remove_gulls_at(x, y);
    board.set_tile(
        x,
        y,
        match brush {
            Brush::Rock => TileKind::Rock,
            Brush::Log => TileKind::Turnstile { next_right: true },
            Brush::Weed => TileKind::Kelp,
            Brush::Pool => TileKind::Pool,
            // The four that return above never reach here; sand is the
            // eraser and clears the tile.
            Brush::Sand | Brush::Castle | Brush::Hole | Brush::Crab | Brush::Gull => {
                TileKind::Empty
            }
        },
    );
}

fn next_dir(dir: Direction) -> Option<Direction> {
    match dir {
        Direction::Right => Some(Direction::Down),
        Direction::Down => Some(Direction::Left),
        Direction::Left => Some(Direction::Up),
        Direction::Up => None,
    }
}

/// Cycle the crab on a tile through kind/handedness combinations, ending at
/// none. Spawned crabs wall-resolve, so the cycle keys off identity fields.
fn cycle_crab(board: &mut Board, x: u8, y: u8) {
    const CYCLE: [(CrabKind, Handedness); 12] = [
        (CrabKind::Common, Handedness::Left),
        (CrabKind::Common, Handedness::Right),
        (CrabKind::Juvenile, Handedness::Left),
        (CrabKind::Juvenile, Handedness::Right),
        (CrabKind::Giant, Handedness::Left),
        (CrabKind::Giant, Handedness::Right),
        (CrabKind::Molting, Handedness::Left),
        (CrabKind::Molting, Handedness::Right),
        (CrabKind::Golden, Handedness::Left),
        (CrabKind::Golden, Handedness::Right),
        (CrabKind::Sparkling, Handedness::Left),
        (CrabKind::Sparkling, Handedness::Right),
    ];
    if board.tile_at(x, y) != TileKind::Empty {
        return; // crabs start on open sand only
    }
    let tile = board.index_of(x, y);
    let current = board
        .crabs()
        .iter()
        .find(|c| c.tile == tile)
        .map(|c| (c.kind, c.handed));
    board.remove_crabs_at(x, y);
    let next = match current {
        None => Some(CYCLE[0]),
        Some(cur) => CYCLE
            .iter()
            .position(|&e| e == cur)
            .and_then(|i| CYCLE.get(i + 1))
            .copied(),
    };
    if let Some((kind, handed)) = next {
        board.spawn_crab(x, y, Direction::Right, handed, kind);
    }
}

/// Which brush would have drawn what is on this tile. Creatures win over
/// the ground they stand on, because that is what the player put there
/// last and what another press would take away.
pub(super) fn standing_on(board: &Board, x: u8, y: u8) -> Brush {
    let tile = board.index_of(x, y);
    if board.gulls().iter().any(|g| g.tile == tile) {
        return Brush::Gull;
    }
    if board.crabs().iter().any(|c| c.tile == tile) {
        return Brush::Crab;
    }
    match board.tile_at(x, y) {
        TileKind::Rock => Brush::Rock,
        TileKind::Castle(_) => Brush::Castle,
        TileKind::Spawner(_) => Brush::Hole,
        TileKind::Turnstile { .. } => Brush::Log,
        TileKind::Kelp => Brush::Weed,
        TileKind::Pool => Brush::Pool,
        TileKind::Empty => Brush::Sand,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The editor moves on WASD and paints with letters, so a brush on one
    /// of those four does two things at once. It also must not collide
    /// with the editor's own keys, which are just as invisible a clash.
    #[test]
    fn no_brush_takes_a_movement_key() {
        use bevy::prelude::KeyCode;
        let movement = [KeyCode::KeyW, KeyCode::KeyA, KeyCode::KeyS, KeyCode::KeyD];
        let controls = [
            KeyCode::KeyK,   // gull period
            KeyCode::KeyO,   // wrapping edges
            KeyCode::KeyV,   // validate
            KeyCode::Tab,    // next brush
            KeyCode::Space,  // paint
            KeyCode::Enter,  // playtest
            KeyCode::Escape, // leave
            KeyCode::F6,     // puzzle or beach
        ];
        for brush in Brush::ALL {
            let key = brush.key();
            assert!(!movement.contains(&key), "{brush:?} is on a movement key");
            assert!(!controls.contains(&key), "{brush:?} is on a control key");
        }
        // And no two brushes share one.
        let mut keys: Vec<_> = Brush::ALL
            .iter()
            .map(|b| format!("{:?}", b.key()))
            .collect();
        keys.sort();
        let before = keys.len();
        keys.dedup();
        assert_eq!(keys.len(), before, "two brushes on one key");
    }

    /// The palette names the key that loads each brush, so the letter it
    /// shows has to be the key's own: it once said "W" for kelp after the
    /// key had moved to E.
    #[test]
    fn letters_name_their_keys() {
        for brush in Brush::ALL {
            assert_eq!(
                format!("{:?}", brush.key()),
                format!("Key{}", brush.letter()),
                "{brush:?} shows a letter that is not its key"
            );
        }
    }

    fn sand() -> Board {
        Board::new(6, 5, 3)
    }

    /// The gull brush asks the sim where a bird may stand rather than
    /// keeping its own list: it once refused rock and let kelp through,
    /// which `spawn_gull` meets with a panic.
    #[test]
    fn the_gull_key_refuses_rock_and_kelp() {
        let mut board = sand();
        board.set_tile(1, 1, TileKind::Rock);
        board.set_tile(2, 2, TileKind::Kelp);
        paint(&mut board, 1, 1, Brush::Gull);
        paint(&mut board, 2, 2, Brush::Gull);
        assert!(board.gulls().is_empty(), "nowhere for a gull to stand");
        paint(&mut board, 3, 3, Brush::Gull);
        assert_eq!(board.gulls().len(), 1, "sand takes one");
    }

    /// One key paints every seat's castle and then clears the tile, and a
    /// castle never lands on top of a creature.
    #[test]
    fn the_castle_key_walks_the_seats_then_clears() {
        let mut board = sand();
        board.spawn_crab(2, 2, Direction::Right, Handedness::Left, CrabKind::Common);
        for owner in 0..MAX_PLAYERS as u8 {
            cycle_castle(&mut board, 2, 2);
            assert_eq!(board.tile_at(2, 2), TileKind::Castle(owner));
        }
        assert!(board.crabs().is_empty(), "the crab was swept aside");
        cycle_castle(&mut board, 2, 2);
        assert_eq!(board.tile_at(2, 2), TileKind::Empty, "and round to nothing");
    }

    /// A brush replaces what is on the tile, and `Sand` is what takes it
    /// away.
    ///
    /// Toggling, so that pressing the same key twice bares the tile, reads
    /// well with one key per terrain and badly with a palette, where
    /// dragging a brush along a row rubs out every other tile. `Sand` is
    /// the eraser instead.
    #[test]
    fn a_brush_replaces_and_sand_erases() {
        let mut board = sand();
        paint(&mut board, 1, 1, Brush::Weed);
        assert_eq!(board.tile_at(1, 1), TileKind::Kelp);
        paint(&mut board, 1, 1, Brush::Weed);
        assert_eq!(board.tile_at(1, 1), TileKind::Kelp, "and again is the same");
        paint(&mut board, 1, 1, Brush::Pool);
        assert_eq!(board.tile_at(1, 1), TileKind::Pool, "another replaces it");
        paint(&mut board, 1, 1, Brush::Sand);
        assert_eq!(board.tile_at(1, 1), TileKind::Empty, "and sand clears it");

        // Whatever was standing there goes with the ground: a crab inside a
        // rock is not a thing the sim has a rule for.
        board.spawn_crab(3, 3, Direction::Right, Handedness::Left, CrabKind::Common);
        paint(&mut board, 3, 3, Brush::Rock);
        assert_eq!(board.tile_at(3, 3), TileKind::Rock);
        assert!(board.crabs().is_empty());
    }

    /// Every brush has its own letter, and the palette, the keys and the
    /// labels are all the one list.
    #[test]
    fn the_palette_is_one_list() {
        use std::collections::HashSet;
        let keys: HashSet<KeyCode> = Brush::ALL.iter().map(|b| b.key()).collect();
        assert_eq!(keys.len(), Brush::ALL.len(), "two brushes share a key");
        let letters: HashSet<&str> = Brush::ALL.iter().map(|b| b.letter()).collect();
        assert_eq!(
            letters.len(),
            Brush::ALL.len(),
            "two brushes share a letter"
        );
        assert_eq!(
            crate::app::i18n::EN.ed_brushes.len(),
            Brush::ALL.len(),
            "the palette and its labels have drifted apart"
        );
    }

    /// The spawner key turns its hole through the four directions and then
    /// removes it, so the whole cycle is reachable from one key.
    #[test]
    fn spawner_directions_run_out_after_four() {
        let dirs: Vec<Direction> =
            std::iter::successors(Some(Direction::Right), |d| next_dir(*d)).collect();
        assert_eq!(
            dirs,
            [
                Direction::Right,
                Direction::Down,
                Direction::Left,
                Direction::Up
            ]
        );
        assert_eq!(next_dir(Direction::Up), None, "the cycle ends, and removes");
    }

    /// The crab key walks every kind in both handednesses and then leaves
    /// the tile bare; crabs refuse to stand on anything but open sand.
    #[test]
    fn the_crab_key_cycles_twelve_then_clears() {
        let mut board = sand();
        let mut seen = Vec::new();
        for _ in 0..12 {
            cycle_crab(&mut board, 3, 3);
            let crab = board.crabs().first().copied().expect("a crab is standing");
            seen.push((crab.kind, crab.handed));
        }
        seen.dedup();
        assert_eq!(seen.len(), 12, "every kind, both hands: {seen:?}");
        cycle_crab(&mut board, 3, 3);
        assert!(board.crabs().is_empty(), "the thirteenth press clears it");

        // Not on a rock.
        board.set_tile(4, 3, TileKind::Rock);
        cycle_crab(&mut board, 4, 3);
        assert!(board.crabs().is_empty(), "crabs start on open sand only");
    }
}