From 60c6a5df8d68d33d560b58c6004e70164691ed79 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Thu, 4 Aug 2022 03:17:23 -0600 Subject: [PATCH] Auto-tile circuits --- src/extract.zig | 79 +++++++++++++++++++++++++++++++++++++++++++++++-- src/rewrite.zig | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 3 deletions(-) diff --git a/src/extract.zig b/src/extract.zig index cdfc819..57b87d7 100644 --- a/src/extract.zig +++ b/src/extract.zig @@ -14,6 +14,10 @@ pub const Options = struct { alloc: std.mem.Allocator, level: world.Level, tileset: world.AutoTileset, + conduit: world.AutoTileset, + plug: world.AutoTileset, + switch_on: world.AutoTileset, + switch_off: world.AutoTileset, }; fn is_solid(tile: u7) bool { @@ -55,9 +59,10 @@ pub fn extractLevel(opt: Options) !void { var autotiles = try alloc.alloc(?AutoTile, size); defer alloc.free(autotiles); + // Auto generate walls { var i: usize = 0; - while (i < level.tiles.len) : (i += 1) { + while (i < size) : (i += 1) { const x = @mod(i, width); const y = @divTrunc(i, width); const stride = width; @@ -111,10 +116,78 @@ pub fn extractLevel(opt: Options) !void { const li = autotile.to_u4(); const tile = tileset.lookup[li]; map.tiles[i] = tile; - } else { + } + } + + // Auto generate circuit + // Re-use autotiles to save memory + { + var i: usize = 0; + while (i < size) : (i += 1) { const x = @mod(i, width); const y = @divTrunc(i, width); - w4.trace("{}, {}: {}", .{ x, y, map.tiles[i] }); + const stride = width; + + if (circuit_map[i] == .None) { + autotiles[i] = null; + continue; + } + + const out_of_bounds = switch (circuit_map[i]) { + .Join, .Source => true, + else => false, + }; + var north = false; + var south = false; + var west = false; + var east = false; + + // Check horizontal neighbors + if (x == 0) { + west = out_of_bounds; + east = circuit_map[i + 1] != .None; + } else if (x == width - 1) { + west = circuit_map[i - 1] != .None; + east = out_of_bounds; + } else { + west = circuit_map[i - 1] != .None; + east = circuit_map[i + 1] != .None; + } + + // Check vertical neighbours + if (y == 0) { + north = out_of_bounds; + south = circuit_map[i + stride] != .None; + } else if (y == height - 1) { + north = circuit_map[i - stride] != .None; + south = out_of_bounds; + } else { + north = circuit_map[i - stride] != .None; + south = circuit_map[i + stride] != .None; + } + + autotiles[i] = AutoTile{ + .North = north, + .South = south, + .West = west, + .East = east, + }; + } + } + + for (autotiles) |autotile_opt, i| { + if (autotile_opt) |autotile| { + const li = autotile.to_u4(); + const tile = switch (circuit_map[i]) { + .Conduit, .Source, .Join => opt.conduit.lookup[li], + .Switch_On => opt.switch_on.lookup[li], + .Switch_Off => opt.switch_off.lookup[li], + .Plug => opt.plug.lookup[li], + .And => 60, + .Xor => 62, + else => 0, + }; + circuit.map[i] = tile; } } } diff --git a/src/rewrite.zig b/src/rewrite.zig index fc60eb2..01a0163 100644 --- a/src/rewrite.zig +++ b/src/rewrite.zig @@ -62,6 +62,78 @@ pub fn start() !void { 22, // South-East-West 0, // South-East-West-North }}, + .conduit = world.AutoTileset{ .lookup = .{ + 42, // Island + 58, // North + 59, // West + 41, // West-North + 57, // East + 40, // East-North + 26, // East-West + 73, // East-West-North + 56, // South + 27, // South-North + 25, // South-West + 74, // South-West-North + 24, // South-East + 75, // South-East-North + 72, // South-East-West + 43, // South-East-West-North + } }, + .plug = world.AutoTileset{ .lookup = .{ + 02, // Island + 45, // North + 46, // West + 02, // West-North + 47, // East + 02, // East-North + 02, // East-West + 02, // East-West-North + 44, // South + 02, // South-North + 02, // South-West + 02, // South-West-North + 02, // South-East + 02, // South-East-North + 02, // South-East-West + 02, // South-East-West-North + } }, + .switch_off = world.AutoTileset{ .lookup = .{ + 02, // Island + 02, // North + 02, // West + 02, // West-North + 02, // East + 02, // East-North + 02, // East-West + 02, // East-West-North + 02, // South + 32, // South-North + 02, // South-West + 29, // South-West-North + 02, // South-East + 31, // South-East-North + 02, // South-East-West + 02, // South-East-West-North + } }, + .switch_on = world.AutoTileset{ .lookup = .{ + 02, // Island + 02, // North + 02, // West + 02, // West-North + 02, // East + 02, // East-North + 02, // East-West + 02, // East-West-North + 02, // South + 48, // South-North + 02, // South-West + 28, // South-West-North + 02, // South-East + 02, // South-East-North + 30, // South-East-West + 02, // South-East-West-North + } }, }); }