From 4c51c52d05ac5b344cb6e89561ce9652f3edf1bc Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Fri, 5 Aug 2022 18:34:08 -0600 Subject: [PATCH] Remove the other sources of off-by-one --- src/circuit.zig | 4 ++-- src/extract.zig | 4 ++-- src/game.zig | 20 ++++++-------------- src/map.zig | 20 +++++--------------- src/world.zig | 29 +++++++++++++++++++++++------ tools/LDtkImport.zig | 2 +- 6 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/circuit.zig b/src/circuit.zig index 2fed73a..1c32225 100644 --- a/src/circuit.zig +++ b/src/circuit.zig @@ -123,12 +123,12 @@ fn get_plugs(tile: u8) Plugs { pub fn get_cell(this: @This(), cell: Cell) ?u8 { const i = this.indexOf(cell) orelse return null; - return if (this.map[i] != 0) this.map[i] - 1 else null; + return if (this.map[i] != 0) this.map[i] else null; } pub fn set_cell(this: *@This(), cell: Cell, tile: u8) void { const i = this.indexOf(cell) orelse return; - this.map[i] = tile + 1; + this.map[i] = tile; this.levels[i] = 0; } diff --git a/src/extract.zig b/src/extract.zig index 4d41445..a62307a 100644 --- a/src/extract.zig +++ b/src/extract.zig @@ -188,8 +188,8 @@ pub fn extractLevel(opt: Options) !void { .Switch_On => opt.switch_on.find(autotile), .Switch_Off => opt.switch_off.find(autotile), .Plug => opt.plug.find(autotile), - .And => 21, - .Xor => 23, + .And => world.Tiles.LogicAnd, + .Xor => world.Tiles.LogicXor, else => 0, }; circuit.map[i] = tile; diff --git a/src/game.zig b/src/game.zig index fac0278..1ade13e 100644 --- a/src/game.zig +++ b/src/game.zig @@ -228,19 +228,11 @@ pub fn start() !void { .level = level, .map = &map, .circuit = &circuit, - .tileset = world.AutoTileset.initOffsetFull(113), - .conduit = world.AutoTileset.initOffsetFull(97), - .plug = world.AutoTileset.initOffsetCardinal(17), - .switch_off = world.AutoTileset.initSwitches(&.{ - 29, // South-North - 25, // South-West-North - 27, // South-East-North - }, 2), - .switch_on = world.AutoTileset.initSwitches(&.{ - 30, // South-North - 26, // South-West-North - 28, // South-East-West - }, 2), + .tileset = world.Tiles.Walls, + .conduit = world.Tiles.Conduit, + .plug = world.Tiles.Plugs, + .switch_off = world.Tiles.SwitchesOff, + .switch_on = world.Tiles.SwitchesOn, }); var entity_buf = try alloc.alloc(world.Entity, level.entity_count); @@ -627,7 +619,7 @@ fn updateCircuit() !void { var i: usize = 0; while (level.getDoor(i)) |door| : (i += 1) { const tile: u8 = if (door.kind == .Door) world.Tiles.Door else world.Tiles.Trapdoor; - try map.set_cell(.{door.x, door.y}, tile); + try map.set_cell(.{ door.x, door.y }, tile); } // Remove doors that have been unlocked diff --git a/src/map.zig b/src/map.zig index 2ea1c5c..b7cc772 100644 --- a/src/map.zig +++ b/src/map.zig @@ -82,9 +82,8 @@ pub fn draw(this: @This(), offset: Vec2) void { while (x < width) : (x += 1) { const cell = Vec2{ @intCast(i32, x), @intCast(i32, y) } + offset; const pos = Vec2{ @intCast(i32, x), @intCast(i32, y) } * tile_size; - const tilePlus = this.get_cell(cell) orelse continue; - if (tilePlus == 0) continue; - const tile = tilePlus - 1; + const tile = this.get_cell(cell) orelse continue; + if (tile == world.Tiles.Empty) continue; const t = Vec2{ @intCast(i32, (tile % tilemap_width) * tile_width), @intCast(i32, (tile / tilemap_width) * tile_width), @@ -134,14 +133,14 @@ pub fn collide(this: @This(), body: BodyInfo) CollisionInfo { const bottom = @floatToInt(i32, bot_right[1]); const foot = body.rect.pos[1] + body.rect.size[1]; - if (isOneWay(tile)) { + if (world.Tiles.is_oneway(tile)) { if (!body.is_passing and a == bottom and body.last[1] <= body.next[1] and foot < tiley + 2) { collisions.append(util.AABB{ .pos = Vec2f{ tilex, tiley }, .size = tile_sizef, }); } - } else if (isSolid(tile)) { + } else if (world.Tiles.is_solid(tile)) { collisions.append(util.AABB{ .pos = Vec2f{ tilex, tiley }, .size = tile_sizef, @@ -171,14 +170,6 @@ pub const CollisionInfo = struct { } }; -pub fn isSolid(tile: u8) bool { - return if (tile == 0) false else world.Tiles.is_solid(tile - 1); -} - -pub fn isOneWay(tile: u8) bool { - return if (tile == 0) false else world.Tiles.is_oneway(tile - 1); -} - // Debug functions pub fn trace(this: @This()) void { @@ -190,8 +181,7 @@ pub fn trace(this: @This()) void { } pub fn traceDraw(this: @This()) void { - for (this.tiles) |tilePlus, i| { - const tile = tilePlus - 1; + for (this.tiles) |tile, i| { const t = Vec2{ @intCast(i32, (tile % tilemap_width) * tile_width), @intCast(i32, (tile / tilemap_width) * tile_width), diff --git a/src/world.zig b/src/world.zig index 0900473..a00441c 100644 --- a/src/world.zig +++ b/src/world.zig @@ -22,15 +22,18 @@ pub const Tiles = struct { // Switches pub const SwitchTeeWestOff = 24; pub const SwitchTeeWestOn = 25; + pub const SwitchTeeEastOff = 26; pub const SwitchTeeEastOn = 27; + pub const SwitchVerticalOff = 28; pub const SwitchVerticalOn = 29; + pub const SwitchHorizontalOff = 30; pub const SwitchHorizontalOn = 31; pub fn is_switch(tile: u8) bool { - return tile >= 24 and tile <= 31; + return tile >= SwitchTeeWestOff and tile <= SwitchHorizontalOn; } // Plugs, sorted by autotile order @@ -51,8 +54,8 @@ pub const Tiles = struct { return tile >= 21 and tile <= 24; } - pub const ConduitCross = 97; - pub const ConduitSingle = 113; + pub const ConduitCross = 96; + pub const ConduitSingle = 112; pub fn is_conduit(tile: u8) bool { return tile >= ConduitCross and tile <= ConduitSingle; @@ -62,15 +65,15 @@ pub const Tiles = struct { return is_plug(tile) or is_conduit(tile) or is_switch(tile) or is_logic(tile); } - pub const WallSingle = 113; + pub const WallSingle = 112; pub const WallSurrounded = 127; pub fn is_wall(tile: u8) bool { return tile >= WallSingle and tile <= WallSurrounded; } - pub const Door = 3; - pub const Trapdoor = 4; + pub const Door = 2; + pub const Trapdoor = 3; pub fn is_door(tile: u8) bool { return tile == 3 or tile == 4; @@ -89,6 +92,20 @@ pub const Tiles = struct { } pub const Empty = 0; + + pub const Walls = AutoTileset.initOffsetFull(WallSingle); + pub const Conduit = AutoTileset.initOffsetFull(ConduitCross); + pub const Plugs = AutoTileset.initOffsetCardinal(PlugNorth); + pub const SwitchesOff = AutoTileset.initSwitches(&.{ + SwitchVerticalOff, // South-North + SwitchTeeWestOff, // South-West-North + SwitchTeeEastOff, // South-East-North + }, 2); + pub const SwitchesOn = AutoTileset.initSwitches(&.{ + SwitchVerticalOn, // South-North + SwitchTeeWestOn, // South-West-North + SwitchTeeEastOn, // South-East-North + }, 2); }; pub const TileData = union(enum) { diff --git a/tools/LDtkImport.zig b/tools/LDtkImport.zig index 1c7517c..a234bcb 100644 --- a/tools/LDtkImport.zig +++ b/tools/LDtkImport.zig @@ -180,7 +180,7 @@ fn make(step: *std.build.Step) !void { const i = @intCast(usize, x + y * width); const sx = @divExact(autotile.src[0], collision.__gridSize); const sy = @divExact(autotile.src[1], collision.__gridSize); - const t = sx + sy * 16 + 1; + const t = sx + sy * 16; tiles[i] = world.TileData{ .tile = @intCast(u7, t) }; }