const std = @import("std"); const hex = @import("hex.zig"); const tst = std.testing; // Generate hexagon-shaped map with given radius (N rings around center) // Uses axial coords: |q|<=N, |r|<=N, |q+r|<=N pub fn generateHexWorld(comptime N: i32, outer_radius: f32, allocator: std.mem.Allocator) !std.ArrayListUnmanaged(hex.Cell) { var cells = std.ArrayListUnmanaged(hex.Cell){}; var q: i32 = -N; while (q <= N) : (q += 1) { const r1 = @max(-N, -q - N); const r2 = @min(N, -q + N); var r: i32 = r1; while (r <= r2) : (r += 1) { try cells.append(allocator, hex.new_hex(outer_radius, hex.Axial.init(q, r))); } } return cells; } // Template-based generation using axial coords // Template indexed by [r][q] where r,q range from -N to +N // Template size is (2*N+1) x (2*N+1), center is at [N][N] pub fn generateFromTemplate(comptime N: i32, template: *const [2 * N + 1][2 * N + 1]u1, outer_radius: f32, allocator: std.mem.Allocator) !std.ArrayListUnmanaged(hex.Cell) { var cells = std.ArrayListUnmanaged(hex.Cell){}; var q: i32 = -N; while (q <= N) : (q += 1) { var r: i32 = -N; while (r <= N) : (r += 1) { const ti_r: usize = @intCast(r + N); const ti_q: usize = @intCast(q + N); if (template[ti_r][ti_q] == 1) { try cells.append(allocator, hex.new_hex(outer_radius, hex.Axial.init(q, r))); } } } return cells; } // Old template-based generation (offset coords) pub fn generateWorld(comptime H: usize, comptime W: usize, template: *const [H][W]u1, outer_radius: f32) [H][W]?hex.Cell { var grid: [H][W]?hex.Cell = undefined; for (0..H) |row| { for (0..W) |col| { if (template[row][col] == 1) { const offset = hex.Offset{ .col = @intCast(col), .row = @intCast(row) }; grid[row][col] = hex.new_hex(outer_radius, offset.toAxial()); } else { grid[row][col] = null; } } } return grid; } test "generateWorld creates grid from template" { const template = [3][3]u1{ .{ 1, 1, 0 }, .{ 1, 0, 1 }, .{ 0, 1, 1 }, }; const grid = generateWorld(3, 3, &template, 50.0); try tst.expect(grid[0][0] != null); try tst.expect(grid[0][2] == null); try tst.expect(grid[1][1] == null); }