amazeing 0.9.0

Amazeing is a maze generator/solver application with simulation/visualization.
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
use crate::cli::{AmazeingContext, Colors, ContextType};
use crate::maze::{BLOCK, Maze, Node, NodeFactory, OPEN, Rank, UnitShape};
use crate::render::helper::{current_micros, is_point_in_triangle};
use crate::render::unit::{
    HexagonRectangleUnitShapeFactory, HexagonUnitShapeFactory, OctagonSquareUnitShapeFactory, OctagonUnitShapeFactory,
    RhombusUnitShapeFactory, SquareUnitShapeFactory, TriangleUnitShapeFactory, UnitShapeFactory,
};
use crate::render::{BORDER, MARGIN};
use crate::util::IsDivisible;
use macroquad::prelude::{Color, Mesh, Vertex, clear_background, draw_line, draw_mesh, vec2, vec3};

// ── constants ─────────────────────────────────────────────────────────────────

/// macroquad's internal geometry batcher (quad_gl) rejects calls that exceed
/// these limits and prints "geometry() exceeded max drawcall size, clamping".
/// We stay a little below the hard limits to leave headroom.
///
/// Limits from macroquad 0.4 `src/quad_gl.rs`:
///   MAX_VERTICES = 10_000
///   MAX_INDICES  =  5_000
const CHUNK_MAX_VERTS: usize = 9_900;
const CHUNK_MAX_INDICES: usize = 4_950;

// ── internal data types ───────────────────────────────────────────────────────

/// Location of a single cell's vertices inside the flat `scene_chunks` array.
#[derive(Clone, Copy)]
struct CellLocation {
    /// Index into `scene_chunks`.
    chunk: usize,
    /// First vertex index inside `scene_chunks[chunk].vertices`.
    vertex_start: usize,
    /// Number of vertices belonging to this cell.
    vertex_count: usize,
}

/// Polygon vertex positions used only for hit-testing.
/// No colour data is stored here; all colour lives in `scene_chunks`.
struct CellHitData {
    positions: Vec<(f32, f32)>,
}

// ── MazeScene ─────────────────────────────────────────────────────────────────

pub(crate) struct MazeScene {
    pub(crate) context: AmazeingContext,
    pub(crate) maze: Maze,
    pub(crate) colors: Colors,
    pub(crate) wh: (u32, u32),
    pub(crate) bound: Option<Mesh>,
    pub(crate) rows: usize,
    pub(crate) cols: usize,

    /// Flat rendering geometry.
    /// All cell polygons are merged into a small number of large Mesh objects
    /// (typically 3-7 for a 175×285 maze) so that `draw()` issues only that
    /// many GPU draw calls instead of one per cell (up to ~50 000).
    scene_chunks: Vec<Mesh>,

    /// For each cell: which chunk it lives in and its vertex range.
    cell_locations: Vec<Vec<CellLocation>>,

    /// Lightweight per-cell geometry used only for click detection.
    cell_hitdata: Vec<Vec<CellHitData>>,
}

// ── construction helpers ──────────────────────────────────────────────────────

#[inline]
fn cell_color(cell: i8, colors: &Colors) -> Color {
    match cell {
        OPEN => colors.color_open,
        BLOCK => colors.color_block,
        _ => colors.color_bg,
    }
}

/// Build flat scene chunks, per-cell location table, and hit-test data
/// in a single pass over the maze.
fn build_scene(
    maze: &Maze,
    shape_factory: &dyn UnitShapeFactory,
    colors: &Colors,
) -> (Vec<Mesh>, Vec<Vec<CellLocation>>, Vec<Vec<CellHitData>>) {
    let (rows, cols) = (maze.rows(), maze.cols());

    let mut chunks: Vec<Mesh> = vec![Mesh { vertices: Vec::new(), indices: Vec::new(), texture: None }];
    let mut locations: Vec<Vec<CellLocation>> = Vec::with_capacity(rows);
    let mut hitdata: Vec<Vec<CellHitData>> = Vec::with_capacity(rows);

    for r in 0..rows {
        let mut row_locs = Vec::with_capacity(cols);
        let mut row_hits = Vec::with_capacity(cols);

        for c in 0..cols {
            let color = cell_color(maze.data[r][c], colors);
            let mesh = shape_factory.shape(r, c, rows, cols, color);

            // Collect vertex positions (read-only; used only for hit testing).
            let positions = mesh.vertices.iter().map(|v| (v.position.x, v.position.y)).collect();
            row_hits.push(CellHitData { positions });

            // Pack this cell into the current chunk, starting a new chunk when
            // either the vertex count or index count would exceed macroquad's
            // internal geometry batcher limits.
            let vcount = mesh.vertices.len();
            let icount = mesh.indices.len();
            {
                let last = chunks.last().unwrap();
                if last.vertices.len() + vcount > CHUNK_MAX_VERTS
                    || last.indices.len() + icount > CHUNK_MAX_INDICES
                {
                    chunks.push(Mesh { vertices: Vec::new(), indices: Vec::new(), texture: None });
                }
            }

            let chunk_idx = chunks.len() - 1;
            let chunk = chunks.last_mut().unwrap();
            let vstart = chunk.vertices.len();
            let base = vstart as u16;

            chunk.vertices.extend_from_slice(&mesh.vertices);
            for &idx in &mesh.indices {
                chunk.indices.push(base + idx);
            }

            row_locs.push(CellLocation { chunk: chunk_idx, vertex_start: vstart, vertex_count: vcount });
        }

        locations.push(row_locs);
        hitdata.push(row_hits);
    }

    (chunks, locations, hitdata)
}

// ── MazeScene impl ───────────────────────────────────────────────────────────

impl MazeScene {
    fn new_from(maze: &Maze, context: AmazeingContext, colors: &Colors, shape_factory: Box<dyn UnitShapeFactory>) -> Self {
        let wh = shape_factory.wh_for(maze.rows(), maze.cols());
        let (scene_chunks, cell_locations, cell_hitdata) = build_scene(maze, shape_factory.as_ref(), colors);

        let mut scene = Self {
            context,
            maze: maze.clone(),
            colors: colors.clone(),
            wh,
            bound: None,
            rows: maze.rows(),
            cols: maze.cols(),
            scene_chunks,
            cell_locations,
            cell_hitdata,
        };

        if scene.context.context_type == ContextType::Create || scene.context.show_perimeter {
            scene.set_bound();
        }

        scene
    }

    pub(crate) fn new_from_maze(maze: &Maze, context: &AmazeingContext, colors: &Colors) -> Self {
        let shape_factory = MazeScene::shape_factory(maze.unit_shape, context.zoom);
        MazeScene::new_from(maze, context.clone(), colors, shape_factory)
    }

    pub(crate) fn new_from_dimension(unit_shape: UnitShape, context: &AmazeingContext, colors: &Colors) -> Self {
        let shape_factory = MazeScene::shape_factory(unit_shape, context.zoom);
        let (m_rows, m_cols) = MazeScene::adjust_dimension(unit_shape, context.rows, context.cols);
        MazeScene::new_from(&Maze::new_void(unit_shape, m_rows, m_cols), context.clone(), colors, shape_factory)
    }

    pub(crate) fn w(&self) -> u32 { self.wh.0 }
    pub(crate) fn h(&self) -> u32 { self.wh.1 }

    /// Bulk-refresh all cell colours from the current maze state.
    /// Called after a generation/load that replaced the entire maze.
    pub(crate) fn update(&mut self) {
        let color_open = self.colors.color_open;
        let color_block = self.colors.color_block;
        let color_bg = self.colors.color_bg;

        for r in 0..self.rows {
            for c in 0..self.cols {
                let color = match self.maze.data[r][c] {
                    OPEN => color_open,
                    BLOCK => color_block,
                    _ => color_bg,
                };
                let loc = self.cell_locations[r][c];
                let col: [u8; 4] = color.into();
                let start = loc.vertex_start;
                let end = start + loc.vertex_count;
                for v in &mut self.scene_chunks[loc.chunk].vertices[start..end] {
                    v.color = col;
                }
            }
        }
    }

    pub(crate) fn set_bound(&mut self) {
        let (x_min, x_max) = (MARGIN - BORDER, self.w() as f32 - MARGIN + BORDER);
        let (y_min, y_max) = (MARGIN - BORDER, self.h() as f32 - MARGIN + BORDER);
        self.bound = Some(Mesh {
            vertices: vec![
                Vertex::new2(vec3(x_min, y_min, 0.), vec2(0., 0.), self.colors.color_perimeter),
                Vertex::new2(vec3(x_max, y_min, 0.), vec2(0., 0.), self.colors.color_perimeter),
                Vertex::new2(vec3(x_max, y_max, 0.), vec2(0., 0.), self.colors.color_perimeter),
                Vertex::new2(vec3(x_min, y_max, 0.), vec2(0., 0.), self.colors.color_perimeter),
            ],
            indices: vec![0, 1, 2, 0, 2, 3],
            texture: None,
        });

        if self.context.context_type == ContextType::Create {
            // Determine which cells fall inside the bound before mutating colours
            // (avoids a simultaneous immutable+mutable borrow of self).
            let in_bound: Vec<(usize, usize)> = {
                let bound = self.bound.as_ref().unwrap();
                let mut result = Vec::new();
                for r in 0..self.rows {
                    for c in 0..self.cols {
                        if Self::cell_in_bound(bound, &self.cell_hitdata[r][c]) {
                            result.push((r, c));
                        }
                    }
                }
                result
            };

            let node_factory = NodeFactory::new(self.rows, self.cols);
            let color_block = self.colors.color_block;
            for (r, c) in in_bound {
                let node = node_factory.at(r, c).unwrap();
                self.update_color(node, color_block);
                self.maze[node] = BLOCK;
            }
        }
    }

    pub(crate) fn clear_and_draw(&self) {
        clear_background(self.colors.color_bg);
        self.draw();
        self.draw_bound();
    }

    /// Draw the entire scene.  Issues one `draw_mesh` call per chunk —
    /// typically 3-7 calls regardless of maze size.
    pub(crate) fn draw(&self) {
        self.scene_chunks.iter().for_each(draw_mesh);
    }

    pub(crate) fn draw_bound(&self) {
        if !self.context.show_perimeter {
            return;
        }
        if let Some(bound) = &self.bound {
            let verts = &bound.vertices;
            let n = verts.len();
            for i in 0..n {
                let next = (i + 1) % n;
                draw_line(
                    verts[i].position.x,
                    verts[i].position.y,
                    verts[next].position.x,
                    verts[next].position.y,
                    1.,
                    self.colors.color_perimeter,
                );
            }
        }
    }

    pub(crate) fn clicked_on(&self, (x, y): (f32, f32)) -> Option<Node> {
        for (row_idx, row) in self.cell_hitdata.iter().enumerate() {
            for (col_idx, hit) in row.iter().enumerate() {
                if Self::is_point_in_hit(hit, x, y) {
                    return NodeFactory::new(self.rows, self.cols).at(row_idx, col_idx);
                }
            }
        }
        None
    }

    pub(crate) fn update_node(&mut self, node: Node, value: i8, color: Color) {
        self.update_color(node, color);
        self.maze[node] = value;
    }

    /// Update vertex colours for a single cell in the appropriate scene chunk.
    /// O(vertices_per_cell) — typically 3-8 vertex writes.
    pub(crate) fn update_color(&mut self, node: Node, color: Color) {
        let loc = self.cell_locations[node.row][node.col];
        let c: [u8; 4] = color.into();
        let start = loc.vertex_start;
        let end = start + loc.vertex_count;
        for v in &mut self.scene_chunks[loc.chunk].vertices[start..end] {
            v.color = c;
        }
    }

    #[allow(dead_code)]
    pub(crate) fn display_void(&mut self, node: Node) {
        self.update_color(node, self.colors.color_bg)
    }

    pub(crate) fn display_block(&mut self, node: Node) {
        self.update_color(node, self.colors.color_block)
    }

    pub(crate) fn display_open(&mut self, node: Node) {
        self.update_color(node, self.colors.color_open)
    }

    #[allow(dead_code)]
    pub(crate) fn display_visiting(&mut self, node: Node) {
        self.update_color(node, self.colors.color_visiting)
    }

    pub(crate) fn display_visiting_gradient(&mut self, node: Node, rank: &Rank) {
        self.update_color(node, *self.colors.shed_color(rank))
    }

    pub(crate) fn display_path(&mut self, node: Node) {
        self.update_color(node, self.colors.color_path)
    }

    pub(crate) fn display_source(&mut self, node: Node) {
        self.update_color(node, self.colors.color_source)
    }

    pub(crate) fn display_destination(&mut self, node: Node) {
        self.update_color(node, self.colors.color_destination)
    }

    pub(crate) fn display_traversed(&mut self, node: Node) {
        self.update_color(node, self.colors.color_traversed)
    }

    /// Sleep until the next frame deadline using microsecond precision.
    /// Allows accurate pacing at any FPS value including values above 300.
    pub(crate) fn delay_till_next_frame(&self, frame_start_us: u128) {
        let elapsed_us = (current_micros() - frame_start_us) as f64;
        let target_us = 1_000_000.0 / self.context.fps as f64;
        if elapsed_us < target_us {
            let sleep_us = (target_us - elapsed_us) as u64;
            if sleep_us > 0 {
                std::thread::sleep(std::time::Duration::from_micros(sleep_us));
            }
        }
    }

    // ── private helpers ───────────────────────────────────────────────────────

    fn shape_factory(unit_shape: UnitShape, zoom: f32) -> Box<dyn UnitShapeFactory> {
        match unit_shape {
            UnitShape::Triangle => Box::new(TriangleUnitShapeFactory::new(zoom)),
            UnitShape::Square => Box::new(SquareUnitShapeFactory::new(zoom)),
            UnitShape::Rhombus => Box::new(RhombusUnitShapeFactory::new(zoom)),
            UnitShape::Hexagon => Box::new(HexagonUnitShapeFactory::new(zoom)),
            UnitShape::HexagonRectangle => Box::new(HexagonRectangleUnitShapeFactory::new(zoom)),
            UnitShape::Octagon => Box::new(OctagonUnitShapeFactory::new(zoom)),
            UnitShape::OctagonSquare => Box::new(OctagonSquareUnitShapeFactory::new(zoom)),
        }
    }

    /// Triangle-fan hit test for a convex polygon stored in `hit.positions`.
    fn is_point_in_hit(hit: &CellHitData, x: f32, y: f32) -> bool {
        let pos = &hit.positions;
        let n = pos.len();
        if n < 3 {
            return false;
        }
        for i in 1..n - 1 {
            if is_point_in_triangle((x, y), pos[0], pos[i], pos[i + 1]) {
                return true;
            }
        }
        false
    }

    /// Returns `true` when the centroid of `hit` lies inside the bound mesh.
    fn cell_in_bound(bound: &Mesh, hit: &CellHitData) -> bool {
        let n = hit.positions.len();
        if n == 0 {
            return false;
        }
        let (sx, sy) = hit.positions.iter().fold((0.0f32, 0.0f32), |acc, &(x, y)| (acc.0 + x, acc.1 + y));
        let center = (sx / n as f32, sy / n as f32);

        let bv = &bound.vertices;
        let bi = &bound.indices;
        for i in (0..bi.len()).step_by(3) {
            if i + 2 < bi.len() {
                let v1 = (bv[bi[i] as usize].position.x, bv[bi[i] as usize].position.y);
                let v2 = (bv[bi[i + 1] as usize].position.x, bv[bi[i + 1] as usize].position.y);
                let v3 = (bv[bi[i + 2] as usize].position.x, bv[bi[i + 2] as usize].position.y);
                if is_point_in_triangle(center, v1, v2, v3) {
                    return true;
                }
            }
        }
        false
    }

    fn adjust_dimension(unit_shape: UnitShape, rows: usize, cols: usize) -> (usize, usize) {
        match unit_shape {
            UnitShape::Triangle | UnitShape::HexagonRectangle | UnitShape::OctagonSquare => {
                ((rows * 2).odd_floor(), cols)
            }
            _ => (rows, cols),
        }
    }
}