amaze 0.4.1

A maze generator
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use crate::dungeon::{DungeonGrid, TileType};
use crate::grid_coord_2d::{GetCoordinateBounds2D, GridCoord2D};

const EXPAND_CHUNK: isize = 16;

pub struct DynDungeonGrid {
    inner: DungeonGrid,
    origin_x: isize,
    origin_y: isize,
    min_floor_x: isize,
    min_floor_y: isize,
    max_floor_x: isize,
    max_floor_y: isize,
    exit_world: Option<(isize, isize)>,
}

impl DynDungeonGrid {
    pub fn new(initial_width: usize, initial_height: usize) -> Self {
        let half_w = (initial_width as isize) / 2;
        let half_h = (initial_height as isize) / 2;

        Self {
            inner: DungeonGrid::new(initial_width, initial_height),
            origin_x: -half_w,
            origin_y: -half_h,
            min_floor_x: isize::MAX,
            min_floor_y: isize::MAX,
            max_floor_x: isize::MIN,
            max_floor_y: isize::MIN,
            exit_world: None,
        }
    }

    pub fn world_to_grid(&self, wx: isize, wy: isize) -> Option<GridCoord2D> {
        let gx = wx.checked_sub(self.origin_x)?;
        let gy = wy.checked_sub(self.origin_y)?;
        if gx >= 0
            && gx < self.inner.width() as isize
            && gy >= 0
            && gy < self.inner.height() as isize
        {
            Some(GridCoord2D::new(gx as usize, gy as usize))
        } else {
            None
        }
    }

    pub fn grid_to_world(&self, coord: GridCoord2D) -> (isize, isize) {
        (
            coord.x as isize + self.origin_x,
            coord.y as isize + self.origin_y,
        )
    }

    pub fn update_floor_bounds(&mut self, wx: isize, wy: isize) {
        if wx < self.min_floor_x {
            self.min_floor_x = wx;
        }
        if wx > self.max_floor_x {
            self.max_floor_x = wx;
        }
        if wy < self.min_floor_y {
            self.min_floor_y = wy;
        }
        if wy > self.max_floor_y {
            self.max_floor_y = wy;
        }
    }

    pub fn ensure_bounds(&mut self, min_x: isize, min_y: isize, max_x: isize, max_y: isize) {
        let needs_left = min_x < self.origin_x;
        let needs_right = max_x >= self.origin_x + self.inner.width() as isize;
        let needs_top = min_y < self.origin_y;
        let needs_bottom = max_y >= self.origin_y + self.inner.height() as isize;

        if !needs_left && !needs_right && !needs_top && !needs_bottom {
            return;
        }

        let current_width = self.inner.width() as isize;
        let current_height = self.inner.height() as isize;

        let expand_left = if needs_left {
            ((self.origin_x - min_x + EXPAND_CHUNK - 1) / EXPAND_CHUNK) * EXPAND_CHUNK
        } else {
            0
        };

        let expand_right = if needs_right {
            ((max_x - (self.origin_x + current_width) + EXPAND_CHUNK) / EXPAND_CHUNK) * EXPAND_CHUNK
        } else {
            0
        };

        let expand_top = if needs_top {
            ((self.origin_y - min_y + EXPAND_CHUNK - 1) / EXPAND_CHUNK) * EXPAND_CHUNK
        } else {
            0
        };

        let expand_bottom = if needs_bottom {
            ((max_y - (self.origin_y + current_height) + EXPAND_CHUNK) / EXPAND_CHUNK)
                * EXPAND_CHUNK
        } else {
            0
        };

        let new_width = (current_width + expand_left + expand_right) as usize;
        let new_height = (current_height + expand_top + expand_bottom) as usize;

        let mut new_grid = DungeonGrid::new(new_width, new_height);

        // Copy existing tiles
        for old_y in 0..current_height {
            for old_x in 0..current_width {
                let old_coord = GridCoord2D::new(old_x as usize, old_y as usize);
                if let Some(tile) = self.inner.get(old_coord) {
                    let new_x = (old_x + expand_left) as usize;
                    let new_y = (old_y + expand_top) as usize;
                    let new_coord = GridCoord2D::new(new_x, new_y);
                    new_grid.set(new_coord, tile);
                }
            }
        }

        // Update exit position in grid coordinates
        if let Some(exit_coord) = self.inner.exit() {
            let new_exit_x = (exit_coord.x as isize + expand_left) as usize;
            let new_exit_y = (exit_coord.y as isize + expand_top) as usize;
            new_grid.set_exit(GridCoord2D::new(new_exit_x, new_exit_y));
        }

        self.origin_x -= expand_left;
        self.origin_y -= expand_top;
        self.inner = new_grid;
    }

    pub fn set_world(&mut self, wx: isize, wy: isize, tile: TileType) {
        if let Some(coord) = self.world_to_grid(wx, wy) {
            let was_floor = self.inner.is_floor(coord);
            self.inner.set(coord, tile);

            if tile.is_passable() && !was_floor {
                self.update_floor_bounds(wx, wy);
            }
        }
    }

    pub fn get_world(&self, wx: isize, wy: isize) -> Option<TileType> {
        self.world_to_grid(wx, wy)
            .and_then(|coord| self.inner.get(coord))
    }

    pub fn is_floor_world(&self, wx: isize, wy: isize) -> bool {
        self.world_to_grid(wx, wy)
            .is_some_and(|coord| self.inner.is_floor(coord))
    }

    pub fn set_exit_world(&mut self, wx: isize, wy: isize) {
        self.exit_world = Some((wx, wy));
        if let Some(coord) = self.world_to_grid(wx, wy) {
            self.inner.set_exit(coord);
        }
    }

    pub fn finalize(self, padding: usize) -> DungeonGrid {
        let padding = padding as isize;

        let content_min_x = if self.min_floor_x == isize::MAX {
            0
        } else {
            self.min_floor_x - padding
        };
        let content_min_y = if self.min_floor_y == isize::MAX {
            0
        } else {
            self.min_floor_y - padding
        };
        let content_max_x = if self.max_floor_x == isize::MIN {
            0
        } else {
            self.max_floor_x + padding
        };
        let content_max_y = if self.max_floor_y == isize::MIN {
            0
        } else {
            self.max_floor_y + padding
        };

        let clamped_min_x = content_min_x.max(self.origin_x);
        let clamped_min_y = content_min_y.max(self.origin_y);
        let clamped_max_x = content_max_x.min(self.origin_x + self.inner.width() as isize - 1);
        let clamped_max_y = content_max_y.min(self.origin_y + self.inner.height() as isize - 1);

        let final_width = (clamped_max_x - clamped_min_x + 1).max(1) as usize;
        let final_height = (clamped_max_y - clamped_min_y + 1).max(1) as usize;

        let mut final_grid = DungeonGrid::new(final_width, final_height);

        for old_y in 0..self.inner.height() as isize {
            for old_x in 0..self.inner.width() as isize {
                let world_x = old_x + self.origin_x;
                let world_y = old_y + self.origin_y;

                if world_x < clamped_min_x
                    || world_x > clamped_max_x
                    || world_y < clamped_min_y
                    || world_y > clamped_max_y
                {
                    continue;
                }

                let old_coord = GridCoord2D::new(old_x as usize, old_y as usize);
                let new_x = (world_x - clamped_min_x) as usize;
                let new_y = (world_y - clamped_min_y) as usize;
                let new_coord = GridCoord2D::new(new_x, new_y);

                if let Some(tile) = self.inner.get(old_coord) {
                    final_grid.set(new_coord, tile);
                }
            }
        }

        // Adjust exit position
        if let Some((exit_wx, exit_wy)) = self.exit_world {
            let exit_x = (exit_wx - clamped_min_x)
                .max(0)
                .min(final_width as isize - 1) as usize;
            let exit_y = (exit_wy - clamped_min_y)
                .max(0)
                .min(final_height as isize - 1) as usize;
            final_grid.set_exit(GridCoord2D::new(exit_x, exit_y));
        } else if let Some(exit_coord) = self.inner.exit() {
            let exit_wx = exit_coord.x as isize + self.origin_x;
            let exit_wy = exit_coord.y as isize + self.origin_y;
            let exit_x = (exit_wx - clamped_min_x)
                .max(0)
                .min(final_width as isize - 1) as usize;
            let exit_y = (exit_wy - clamped_min_y)
                .max(0)
                .min(final_height as isize - 1) as usize;
            final_grid.set_exit(GridCoord2D::new(exit_x, exit_y));
        }

        // Place walls around any newly exposed edges
        final_grid.place_walls();

        // Recompute edge masks
        final_grid.compute_edge_masks();

        final_grid
    }

    pub fn inner(&self) -> &DungeonGrid {
        &self.inner
    }

    pub fn inner_mut(&mut self) -> &mut DungeonGrid {
        &mut self.inner
    }
}

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

    #[test]
    fn test_new_grid_origin_at_center() {
        let grid = DynDungeonGrid::new(32, 32);
        assert_eq!(grid.origin_x, -16);
        assert_eq!(grid.origin_y, -16);
        assert_eq!(grid.inner.width(), 32);
        assert_eq!(grid.inner.height(), 32);
    }

    #[test]
    fn test_world_to_grid_conversion() {
        let grid = DynDungeonGrid::new(32, 32);

        // World (0, 0) should map to grid (16, 16)
        let coord = grid.world_to_grid(0, 0).unwrap();
        assert_eq!(coord.x, 16);
        assert_eq!(coord.y, 16);

        // World (-16, -16) should map to grid (0, 0)
        let coord = grid.world_to_grid(-16, -16).unwrap();
        assert_eq!(coord.x, 0);
        assert_eq!(coord.y, 0);

        // World (15, 15) should map to grid (31, 31)
        let coord = grid.world_to_grid(15, 15).unwrap();
        assert_eq!(coord.x, 31);
        assert_eq!(coord.y, 31);
    }

    #[test]
    fn test_grid_to_world_conversion() {
        let grid = DynDungeonGrid::new(32, 32);

        let (wx, wy) = grid.grid_to_world(GridCoord2D::new(16, 16));
        assert_eq!(wx, 0);
        assert_eq!(wy, 0);

        let (wx, wy) = grid.grid_to_world(GridCoord2D::new(0, 0));
        assert_eq!(wx, -16);
        assert_eq!(wy, -16);
    }

    #[test]
    fn test_set_world_and_get_world() {
        let mut grid = DynDungeonGrid::new(32, 32);
        grid.set_world(0, 0, TileType::Floor);
        grid.update_floor_bounds(0, 0);

        assert_eq!(grid.get_world(0, 0), Some(TileType::Floor));
        assert!(grid.is_floor_world(0, 0));
        assert!(!grid.is_floor_world(1, 0));
    }

    #[test]
    fn test_expand_right() {
        let mut grid = DynDungeonGrid::new(32, 32);
        // Initial right boundary is at world x = 15
        assert_eq!(grid.origin_x + grid.inner.width() as isize, 16);

        // Request a tile beyond the right boundary
        grid.ensure_bounds(0, 0, 20, 0);
        assert!(grid.inner.width() >= 37); // 32 + at least 5 to reach 20
        assert_eq!(grid.get_world(20, 0), Some(TileType::Empty));
    }

    #[test]
    fn test_expand_left() {
        let mut grid = DynDungeonGrid::new(32, 32);
        // Initial left boundary is at world x = -16

        // Request a tile beyond the left boundary
        grid.ensure_bounds(-20, 0, 0, 0);
        assert!(grid.inner.width() >= 36); // 32 + at least 4 to reach -20
        assert_eq!(grid.get_world(-20, 0), Some(TileType::Empty));
    }

    #[test]
    fn test_expand_top() {
        let mut grid = DynDungeonGrid::new(32, 32);

        grid.ensure_bounds(0, -20, 0, 0);
        assert!(grid.inner.height() >= 36);
        assert_eq!(grid.get_world(0, -20), Some(TileType::Empty));
    }

    #[test]
    fn test_expand_bottom() {
        let mut grid = DynDungeonGrid::new(32, 32);

        grid.ensure_bounds(0, 0, 0, 20);
        assert!(grid.inner.height() >= 37);
        assert_eq!(grid.get_world(0, 20), Some(TileType::Empty));
    }

    #[test]
    fn test_expand_all_directions() {
        let mut grid = DynDungeonGrid::new(32, 32);

        grid.ensure_bounds(-20, -20, 20, 20);
        assert!(grid.inner.width() >= 41);
        assert!(grid.inner.height() >= 41);
        assert_eq!(grid.get_world(-20, -20), Some(TileType::Empty));
        assert_eq!(grid.get_world(20, 20), Some(TileType::Empty));
    }

    #[test]
    fn test_finalize_trims_correctly() {
        let mut grid = DynDungeonGrid::new(32, 32);

        // Place floor tiles in a small region
        for x in -5..=5 {
            for y in -3..=3 {
                grid.ensure_bounds(x, y, x, y);
                grid.set_world(x, y, TileType::Floor);
            }
        }

        let final_grid = grid.finalize(0);
        assert_eq!(final_grid.width(), 11);
        assert_eq!(final_grid.height(), 7);
    }

    #[test]
    fn test_finalize_preserves_exit() {
        let mut grid = DynDungeonGrid::new(32, 32);

        for x in -5..=5 {
            grid.ensure_bounds(x, 0, x, 0);
            grid.set_world(x, 0, TileType::Floor);
        }
        grid.set_exit_world(5, 0);

        let final_grid = grid.finalize(0);
        let exit = final_grid.exit().unwrap();

        // Exit should be at (10, 3) in the trimmed grid
        // The content goes from x=-5 to x=5, so width=11, and exit at world x=5 maps to grid x=10
        assert_eq!(exit.x, 10);
    }

    #[test]
    fn test_finalize_with_padding() {
        let mut grid = DynDungeonGrid::new(32, 32);

        for x in -3..=3 {
            for y in -2..=2 {
                grid.ensure_bounds(x, y, x, y);
                grid.set_world(x, y, TileType::Floor);
            }
        }

        let final_grid = grid.finalize(2);
        assert_eq!(final_grid.width(), 11); // 7 + 2*2 = 11
        assert_eq!(final_grid.height(), 9); // 5 + 2*2 = 9
    }

    #[test]
    fn test_finalize_recomputes_walls() {
        let mut grid = DynDungeonGrid::new(32, 32);

        // Place a single floor tile at origin
        grid.ensure_bounds(0, 0, 0, 0);
        grid.set_world(0, 0, TileType::Floor);

        // Finalize with padding to have room for walls
        let final_grid = grid.finalize(1);

        // Grid should be 3x3 (from -1 to 1 with padding)
        assert_eq!(final_grid.width(), 3);
        assert_eq!(final_grid.height(), 3);

        // Center should be floor
        let center = GridCoord2D::new(1, 1);
        assert_eq!(final_grid.get(center), Some(TileType::Floor));

        // All 4 neighbors should be walls
        assert_eq!(final_grid.get(GridCoord2D::new(0, 1)), Some(TileType::Wall));
        assert_eq!(final_grid.get(GridCoord2D::new(2, 1)), Some(TileType::Wall));
        assert_eq!(final_grid.get(GridCoord2D::new(1, 0)), Some(TileType::Wall));
        assert_eq!(final_grid.get(GridCoord2D::new(1, 2)), Some(TileType::Wall));
    }

    #[test]
    fn test_expand_preserves_existing_tiles() {
        let mut grid = DynDungeonGrid::new(32, 32);

        // Place floor at center
        grid.set_world(0, 0, TileType::Floor);
        grid.update_floor_bounds(0, 0);

        // Expand in all directions
        grid.ensure_bounds(-20, -20, 20, 20);

        // Original floor should still be there
        assert_eq!(grid.get_world(0, 0), Some(TileType::Floor));
        assert!(grid.is_floor_world(0, 0));
    }

    #[test]
    fn test_chunk_expansion_amortization() {
        let mut grid = DynDungeonGrid::new(32, 32);

        // Request just beyond current boundary
        let initial_width = grid.inner.width();
        grid.ensure_bounds(0, 0, 16, 0); // Right at boundary + 1

        // Should have expanded by EXPAND_CHUNK (16), not by 1
        assert_eq!(grid.inner.width(), initial_width + EXPAND_CHUNK as usize);
    }
}