From 797c21a80b42512803880a80292aaba2d6e81e3a Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Mon, 8 Aug 2022 23:07:40 -0600 Subject: [PATCH] Save wires, fix deletion --- src/game.zig | 64 ++++++++++++++++++++++++++++++++++++--------------- src/world.zig | 33 +++++++++++++++++--------- 2 files changed, 67 insertions(+), 30 deletions(-) diff --git a/src/game.zig b/src/game.zig index 17ad90f..db6ccfa 100644 --- a/src/game.zig +++ b/src/game.zig @@ -232,7 +232,7 @@ fn loadLevel(lvl: usize) !void { const wire = try world.Wire.getEnds(wireSlice); const coord0 = wire[0].coord.subC(levelc); const coord1 = wire[1].coord.subC(levelc); - w4.tracef("---- Wire (%d, %d), (%d, %d)", coord0.val[0], coord0.val[1], coord1.val[0], coord1.val[1]); + w4.tracef("---- Wire [%d, %d] (%d, %d), (%d, %d)", wireArr[0], wireArr[1], coord0.val[0], coord0.val[1], coord1.val[0], coord1.val[1]); const p1 = util.vec2ToVec2f(coord0.toVec2() * tile_size + Vec2{ 4, 4 }); const p2 = util.vec2ToVec2f(coord1.toVec2() * tile_size + Vec2{ 4, 4 }); @@ -293,6 +293,32 @@ fn loadLevel(lvl: usize) !void { } fn moveLevel(direction: enum { L, R, U, D }) !void { + // Save wires back into database + const levelc = world.Coordinate.fromWorld(level.world_x, level.world_y); + while (wires.popOrNull()) |*w| { + const aStart = w.begin().pinned; + const aEnd = w.begin().pinned; + const divby = @splat(2, @as(f32, 8)); + const wstart = world.Coordinate.fromVec2f(w.begin().pos / divby).addC(levelc); + const offset = w.end().pos - w.begin().pos; + const end = world.Coordinate.fromVec2f(offset / divby).toOffset(); + var wire: [3]world.Wire = undefined; + if (aStart) { + wire[0] = .{.BeginPinned = wstart}; + } else { + wire[0] = .{.Begin = wstart}; + } + + if (aEnd) { + wire[1] = .{.PointPinned = end}; + } else { + wire[1] = .{.Point = end}; + } + + wire[2] = .End; + db.addWire(&wire); + } + // TODO: Figure out the more principled way for checking boundaries var velocity = player.pos.getVelocity(); switch (direction) { @@ -745,24 +771,24 @@ fn updateCircuit() !void { try db.updateCircuit(frame_alloc); - for (db.circuit_info) |node, n| { - const e = @boolToInt(node.energized); - switch (node.kind) { - .Conduit => |Conduit| w4.tracef("[%d]: Conduit [%d, %d] <%d>", n, Conduit[0], Conduit[1], e), - .And => |And| w4.tracef("[%d]: And [%d, %d] <%d>", n, And[0], And[1], e), - .Xor => |Xor| w4.tracef("[%d]: Xor [%d, %d] <%d>", n, Xor[0], Xor[1], e), - .Source => w4.tracef("[%d]: Source", n), - .Socket => |Socket| { - const socket = Socket orelse std.math.maxInt(world.NodeID); - w4.tracef("[%d]: Socket [%d] <%d>", n, socket, e); - }, - .Plug => |Plug| w4.tracef("[%d]: Plug [%d] <%d>", n, Plug, e), - .Switch => |Switch| w4.tracef("[%d]: Switch %d [%d] <%d>", n, Switch.state, Switch.source, e), - .SwitchOutlet => |Switch| w4.tracef("[%d]: SwitchOutlet %d [%d] <%d>", n, Switch.which, Switch.source, e), - .Join => |Join| w4.tracef("[%d]: Join [%d] <%d>", n, Join, e), - .Outlet => |Outlet| w4.tracef("[%d]: Outlet [%d] <%d>", n, Outlet, e), - } - } + // for (db.circuit_info) |node, n| { + // const e = @boolToInt(node.energized); + // switch (node.kind) { + // .Conduit => |Conduit| w4.tracef("[%d]: Conduit [%d, %d] <%d>", n, Conduit[0], Conduit[1], e), + // .And => |And| w4.tracef("[%d]: And [%d, %d] <%d>", n, And[0], And[1], e), + // .Xor => |Xor| w4.tracef("[%d]: Xor [%d, %d] <%d>", n, Xor[0], Xor[1], e), + // .Source => w4.tracef("[%d]: Source", n), + // .Socket => |Socket| { + // const socket = Socket orelse std.math.maxInt(world.NodeID); + // w4.tracef("[%d]: Socket [%d] <%d>", n, socket, e); + // }, + // .Plug => |Plug| w4.tracef("[%d]: Plug [%d] <%d>", n, Plug, e), + // .Switch => |Switch| w4.tracef("[%d]: Switch %d [%d] <%d>", n, Switch.state, Switch.source, e), + // .SwitchOutlet => |Switch| w4.tracef("[%d]: SwitchOutlet %d [%d] <%d>", n, Switch.which, Switch.source, e), + // .Join => |Join| w4.tracef("[%d]: Join [%d] <%d>", n, Join, e), + // .Outlet => |Outlet| w4.tracef("[%d]: Outlet [%d] <%d>", n, Outlet, e), + // } + // } } fn wirePhysicsProcess(dt: f32, wire: *Wire) !void { diff --git a/src/world.zig b/src/world.zig index 5a1f05a..7ec043e 100644 --- a/src/world.zig +++ b/src/world.zig @@ -228,6 +228,10 @@ pub const Coordinate = struct { return .{ .val = .{ @intCast(i16, vec[0]), @intCast(i16, vec[1]) } }; } + pub fn fromVec2f(vec: @Vector(2, f32)) Coordinate { + return fromVec2(.{ @floatToInt(i32, vec[0]), @floatToInt(i32, vec[1]) }); + } + pub fn toLevelTopLeft(coord: Coordinate) Coordinate { const worldc = coord.toWorld(); return .{ .val = .{ @@ -489,30 +493,21 @@ pub const Wire = union(enum) { pub fn getEnds(wires: []Wire) ![2]EndData { std.debug.assert(wires[0] == .Begin or wires[0] == .BeginPinned); var ends: [2]EndData = undefined; - const w4 = @import("wasm4.zig"); for (wires) |wire| { switch (wire) { .Begin => |coord| { ends[0] = .{ .coord = coord, .anchored = false }; ends[1] = ends[0]; - w4.tracef("[getEnds] Begin (%d, %d)", coord.val[0], coord.val[1]); }, .BeginPinned => |coord| { ends[0] = .{ .coord = coord, .anchored = true }; ends[1] = ends[0]; - w4.tracef("[getEnds] BeginPinned (%d, %d)", coord.val[0], coord.val[1]); }, .Point => |offset| { ends[1] = .{ .coord = ends[1].coord.addOffset(offset), .anchored = false }; - const o1 = @intCast(i32, offset[0]); - const o2 = @intCast(i32, offset[1]); - w4.tracef("[getEnds] Point (%d, %d)", o1, o2); }, .PointPinned => |offset| { ends[1] = .{ .coord = ends[1].coord.addOffset(offset), .anchored = true }; - const o1 = @intCast(i32, offset[0]); - const o2 = @intCast(i32, offset[1]); - w4.tracef("[getEnds] Point Pinned (%d, %d)", o1, o2); }, .End => { return ends; @@ -859,8 +854,16 @@ pub const Database = struct { /// Remove a wire slice from the wires array. Invalidates handles returned from findWire pub fn deleteWire(database: *Database, wire: [2]usize) void { + const w4 = @import("wasm4.zig"); const wire_size = wire[1] - wire[0]; - std.mem.rotate(Wire, database.wires, wire_size); + if (wire[0] + wire_size == database.wire_count) { + database.wire_count -= wire_size; + return; + } + const wires_end = database.wires[wire[0] + wire_size .. database.wire_count]; + const new_end = database.wires[wire[0] .. database.wire_count - wire_size]; + w4.tracef("%d, %d, %d", wire_size, wires_end.len, new_end.len); + std.mem.copy(Wire, new_end, wires_end); database.wire_count -= wire_size; } @@ -869,6 +872,14 @@ pub const Database = struct { return database.wires[wire[0]..wire[1]]; } + /// Retrieve the slice of the wire from findWire + pub fn addWire(database: *Database, wire: []Wire) void { + const start = database.wire_count; + const end = start + wire.len; + std.mem.copy(Wire, database.wires[start..end], wire); + database.wire_count += wire.len; + } + /// Find a wire within the limits of a given level pub fn findWire(database: *Database, level: Level, num: usize) ?[2]usize { const nw = Coord.fromWorld(level.world_x, level.world_y); @@ -891,7 +902,7 @@ pub const Database = struct { .End => { if (node_begin) |node| { if (wire_count == num) { - return [2]usize{node, i + 1}; + return [2]usize{ node, i + 1 }; } wire_count += 1; node_begin = null;