diff --git a/src/extract.zig b/src/extract.zig index 57b87d7..1533d62 100644 --- a/src/extract.zig +++ b/src/extract.zig @@ -113,8 +113,7 @@ pub fn extractLevel(opt: Options) !void { for (autotiles) |autotile_opt, i| { if (autotile_opt) |autotile| { - const li = autotile.to_u4(); - const tile = tileset.lookup[li]; + const tile = tileset.find(autotile); map.tiles[i] = tile; } } @@ -177,12 +176,11 @@ pub fn extractLevel(opt: Options) !void { 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], + .Conduit, .Source, .Join => opt.conduit.find(autotile), + .Switch_On => opt.switch_on.find(autotile), + .Switch_Off => opt.switch_off.find(autotile), + .Plug => opt.plug.find(autotile), .And => 60, .Xor => 62, else => 0, diff --git a/src/rewrite.zig b/src/rewrite.zig index 01a0163..4aa8c4c 100644 --- a/src/rewrite.zig +++ b/src/rewrite.zig @@ -44,7 +44,7 @@ pub fn start() !void { .level = level, .map = &map, .circuit = &circuit, - .tileset = world.AutoTileset{ .lookup = .{ + .tileset = world.AutoTileset.initFull(&.{ 35, // Island 51, // North 52, // West @@ -61,8 +61,8 @@ pub fn start() !void { 37, // South-East-North 22, // South-East-West 0, // South-East-West-North - }}, - .conduit = world.AutoTileset{ .lookup = .{ + }), + .conduit = world.AutoTileset.initFull(&.{ 42, // Island 58, // North 59, // West @@ -79,61 +79,23 @@ pub fn start() !void { 75, // South-East-North 72, // South-East-West 43, // South-East-West-North - } }, - .plug = world.AutoTileset{ .lookup = .{ - 02, // Island + }), + .plug = world.AutoTileset.initCardinal(&.{ 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 + }, 2), + .switch_off = world.AutoTileset.initSwitches(&.{ 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 + }, 2), + .switch_on = world.AutoTileset.initSwitches(&.{ 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 - } }, + }, 2), }); } diff --git a/src/world.zig b/src/world.zig index e9543ee..c8d98bd 100644 --- a/src/world.zig +++ b/src/world.zig @@ -96,7 +96,7 @@ pub const Level = struct { tiles: []TileStore, pub fn init(header: LevelHeader, buf: []TileStore) Level { - return Level { + return Level{ .world_x = header.world_x, .world_y = header.world_y, .width = header.width, @@ -122,5 +122,58 @@ pub const AutoTile = packed struct { }; pub const AutoTileset = struct { - lookup: [16]u8, + lookup: []const u8, + kind: enum { + Cardinal, + Switches, + Full, + }, + default: u8, + + pub fn initFull(table: []const u8) AutoTileset { + std.debug.assert(table.len == 16); + return AutoTileset{ + .lookup = table, + .kind = .Full, + .default = 0, + }; + } + + pub fn initCardinal(table: []const u8, default: u8) AutoTileset { + std.debug.assert(table.len == 4); + return AutoTileset{ + .lookup = table, + .kind = .Cardinal, + .default = default, + }; + } + + pub fn initSwitches(table: []const u8, default: u8) AutoTileset { + std.debug.assert(table.len == 3); + return AutoTileset{ + .lookup = table, + .kind = .Switches, + .default = default, + }; + } + + pub fn find(tileset: AutoTileset, autotile: AutoTile) u8 { + const autoint = autotile.to_u4(); + switch (tileset.kind) { + .Full => return tileset.lookup[autoint], + .Cardinal => switch (autoint) { + 0b0001 => return tileset.lookup[0], + 0b0010 => return tileset.lookup[1], + 0b0100 => return tileset.lookup[2], + 0b1000 => return tileset.lookup[3], + else => return tileset.default, + }, + .Switches => switch (autoint) { + 0b1001 => return tileset.lookup[0], + 0b1011 => return tileset.lookup[1], + 0b1101 => return tileset.lookup[2], + else => return tileset.default, + }, + } + } };