From 8c26b29ca6b21da6364b23b8a343eb1790360455 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sun, 7 Aug 2022 23:46:16 -0600 Subject: [PATCH] Get wires connecting Unforunately this has exposed my circuit analysis is incomplete --- assets/maps/wired.ldtk | 17 ++++++++--------- src/game.zig | 23 ++++++++++++++++++++++- src/world.zig | 41 ++++++++++++++++++++++++++++------------- tools/LDtkImport.zig | 24 +++++++++++++++++------- 4 files changed, 75 insertions(+), 30 deletions(-) diff --git a/assets/maps/wired.ldtk b/assets/maps/wired.ldtk index c49e232..ac81734 100644 --- a/assets/maps/wired.ldtk +++ b/assets/maps/wired.ldtk @@ -3528,10 +3528,10 @@ "optionalRules": [], "intGridCsv": [ 0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,5,1,0,0,0,0,0,0,0,0,0,0,1,0,0, - 0,0,0,2,1,0,0,1,1,1,1,2,0,0,0,2,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,1, + 0,0,0,2,1,0,0,1,2,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,1, 1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0, 0,0,8,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, - 1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,2,0,0,0,0,0,0,0, + 1,0,0,0,0,0,0,1,1,1,1,1,6,1,1,1,1,1,1,0,1,0,0,0,0,0,0,2,0,0,0,0,0,0,0, 0,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0, 0,0,1,1,1,1,1,1,1,9,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0, @@ -3563,7 +3563,6 @@ { "px": [0,136], "src": [40,48], "f": 0, "t": 101, "d": [38,340] }, { "px": [40,152], "src": [40,48], "f": 0, "t": 101, "d": [38,385] }, { "px": [80,24], "src": [56,48], "f": 0, "t": 103, "d": [42,70] }, - { "px": [96,56], "src": [56,48], "f": 0, "t": 103, "d": [42,152] }, { "px": [96,24], "src": [88,48], "f": 0, "t": 107, "d": [43,72] }, { "px": [16,0], "src": [48,48], "f": 0, "t": 102, "d": [44,2] }, { "px": [24,0], "src": [48,48], "f": 0, "t": 102, "d": [44,3] }, @@ -3575,9 +3574,6 @@ { "px": [72,0], "src": [48,48], "f": 0, "t": 102, "d": [44,9] }, { "px": [80,0], "src": [48,48], "f": 0, "t": 102, "d": [44,10] }, { "px": [88,0], "src": [48,48], "f": 0, "t": 102, "d": [44,11] }, - { "px": [24,16], "src": [48,48], "f": 0, "t": 102, "d": [44,43] }, - { "px": [32,16], "src": [48,48], "f": 0, "t": 102, "d": [44,44] }, - { "px": [40,16], "src": [48,48], "f": 0, "t": 102, "d": [44,45] }, { "px": [64,24], "src": [48,48], "f": 0, "t": 102, "d": [44,68] }, { "px": [72,24], "src": [48,48], "f": 0, "t": 102, "d": [44,69] }, { "px": [88,24], "src": [48,48], "f": 0, "t": 102, "d": [44,71] }, @@ -3633,12 +3629,13 @@ { "px": [80,16], "src": [24,8], "f": 0, "t": 19, "d": [56,50] }, { "px": [96,72], "src": [24,8], "f": 0, "t": 19, "d": [56,192] }, { "px": [144,8], "src": [16,8], "f": 0, "t": 18, "d": [59,38] }, - { "px": [48,16], "src": [8,8], "f": 0, "t": 17, "d": [58,46] }, + { "px": [24,16], "src": [8,8], "f": 0, "t": 17, "d": [58,43] }, { "px": [56,64], "src": [0,8], "f": 0, "t": 16, "d": [57,167] }, { "px": [144,64], "src": [0,8], "f": 0, "t": 16, "d": [57,178] }, { "px": [40,128], "src": [96,8], "f": 0, "t": 28, "d": [78,325] }, { "px": [72,128], "src": [96,8], "f": 0, "t": 28, "d": [78,329] }, { "px": [0,8], "src": [48,48], "f": 0, "t": 102, "d": [68,20] }, + { "px": [96,56], "src": [32,8], "f": 0, "t": 20, "d": [82,152] }, { "px": [56,120], "src": [32,8], "f": 0, "t": 20, "d": [82,307] } ], "seed": 1371187, @@ -3666,7 +3663,7 @@ "optionalRules": [], "intGridCsv": [ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,3,3,1,1,1, + 0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,3,3,3,3,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,0,0,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0, @@ -3796,11 +3793,13 @@ { "px": [8,136], "src": [16,56], "f": 0, "t": 114, "d": [20,341] }, { "px": [56,32], "src": [8,56], "f": 0, "t": 113, "d": [23,87] }, { "px": [88,120], "src": [8,56], "f": 0, "t": 113, "d": [23,311] }, - { "px": [40,24], "src": [8,16], "f": 0, "t": 33, "d": [125,65] }, { "px": [40,56], "src": [8,16], "f": 0, "t": 33, "d": [125,145] }, { "px": [40,88], "src": [8,16], "f": 0, "t": 33, "d": [125,225] }, { "px": [32,40], "src": [24,16], "f": 0, "t": 35, "d": [126,104] }, { "px": [32,72], "src": [24,16], "f": 0, "t": 35, "d": [126,184] }, + { "px": [24,24], "src": [16,16], "f": 0, "t": 34, "d": [127,63] }, + { "px": [32,24], "src": [16,16], "f": 0, "t": 34, "d": [127,64] }, + { "px": [40,24], "src": [16,16], "f": 0, "t": 34, "d": [127,65] }, { "px": [48,24], "src": [16,16], "f": 0, "t": 34, "d": [127,66] }, { "px": [24,40], "src": [16,16], "f": 0, "t": 34, "d": [127,103] }, { "px": [48,56], "src": [16,16], "f": 0, "t": 34, "d": [127,146] }, diff --git a/src/game.zig b/src/game.zig index f87b180..2c8eee8 100644 --- a/src/game.zig +++ b/src/game.zig @@ -677,7 +677,11 @@ fn updateCircuit() !void { @intCast(i16, cellEnd[1]), }).addC(topleft); - db.connectPlugs(p1, p2); + w4.tracef("p1 %d, %d \t p2 %d, %d", p1.val[0], p1.val[1], p2.val[0], p2.val[1]); + + db.connectPlugs(p1, p2) catch { + w4.tracef("connect plugs error"); + }; } // Simulate circuit @@ -708,6 +712,23 @@ fn updateCircuit() !void { } db.updateCircuit(); + + for (db.circuit_info) |node, n| { + switch (node.kind) { + .Conduit => |Conduit| w4.tracef("[%d]: Conduit [%d, %d]", n, Conduit[0], Conduit[1]), + .And => |And| w4.tracef("[%d]: And [%d, %d]", n, And[0], And[1]), + .Xor => |Xor| w4.tracef("[%d]: Xor [%d, %d]", n, Xor[0], Xor[1]), + .Source => w4.tracef("[%d]: Source", n), + .Plug => |Plug| { + const plug = Plug orelse std.math.maxInt(world.NodeID); + w4.tracef("[%d]: Plug [%d]", n, plug); + }, + .Jack => |Jack| w4.tracef("[%d]: Jack [%d]", n, Jack), + .Switch => |Switch| w4.tracef("[%d]: Switch [%s]", n, &@tagName(Switch)), + .Join => |Join| w4.tracef("[%d]: Join [%d]", n, Join), + .Outlet => |Outlet| w4.tracef("[%d]: Outlet [%d]", n, Outlet), + } + } } fn wirePhysicsProcess(dt: f32, wire: *Wire) !void { diff --git a/src/world.zig b/src/world.zig index e0f0968..c0296f0 100644 --- a/src/world.zig +++ b/src/world.zig @@ -635,22 +635,24 @@ pub const Database = struct { return null; } - pub fn connectPlugs(db: *Database, p1: Coord, p2: Coord) void { - var opt1: ?usize = null; - var opt2: ?usize = null; + fn getNodeID(db: *Database, coord: Coord) ?NodeID { for (db.circuit_info) |node, i| { - if (!p1.eq(node.coord) or !p2.eq(node.coord)) continue; - if (p1.eq(node.coord)) opt1 = i else opt2 = i; + if (!coord.eq(node.coord)) continue; + return @intCast(NodeID, i); } - var plug1 = db.circuit_info[opt1 orelse return]; - var plug2 = db.circuit_info[opt2 orelse return]; + return null; + } - if (plug1.energized and !plug2.energized) { - plug2.energized = true; - plug2.kind.Plug = @intCast(NodeID, opt1.?); - } else if (plug2.energized and !plug1.energized) { - plug1.energized = true; - plug1.kind.Plug = @intCast(NodeID, opt2.?); + pub fn connectPlugs(db: *Database, p1: Coord, p2: Coord) !void { + const p1id = db.getNodeID(p1) orelse return; + const p2id = db.getNodeID(p2) orelse return; + + if (db.circuit_info[p1id].kind == .Jack and db.circuit_info[p2id].kind == .Plug) { + db.circuit_info[p2id].kind.Plug = p1id; + } else if (db.circuit_info[p2id].kind == .Jack and db.circuit_info[p1id].kind == .Plug) { + db.circuit_info[p1id].kind.Plug = p2id; + } else if (db.circuit_info[p2id].kind == .Jack and db.circuit_info[p1id].kind == .Plug) { + return error.Unimplemented; } } @@ -695,6 +697,9 @@ pub const Database = struct { db.circuit_info[i].energized = false; } }, + .Jack => |Jack| { + db.circuit_info[i].energized = db.circuit_info[Jack].energized; + }, .Switch => |state| { // TODO Rework switch to make sense db.circuit_info[i].energized = false; @@ -763,6 +768,7 @@ const NodeEnum = enum(u4) { Source, Conduit, Plug, + Jack, Switch, Join, Outlet, @@ -780,6 +786,8 @@ pub const NodeKind = union(NodeEnum) { /// This node represents a physical plug in the game world. The /// NodeID points to another plug, if connected Plug: ?NodeID, + /// If a plug is connected to a circuit fragment with a source, it is a jack. + Jack: NodeID, /// A switch can be in one of five states, though only /// two apply to any one switch. /// Vertical = Off or Top/Bottom, depending on flow @@ -822,6 +830,9 @@ pub const NodeKind = union(NodeEnum) { kind = .{ .Plug = plug }; } }, + .Jack => { + kind = .{ .Jack = try reader.readInt(NodeID, .Little) }; + }, .Switch => { kind = .{ .Switch = @intToEnum(SwitchEnum, try reader.readInt(NodeID, .Little)), @@ -857,6 +868,9 @@ pub const NodeKind = union(NodeEnum) { const plug = Plug orelse std.math.maxInt(NodeID); try writer.writeInt(NodeID, plug, .Little); }, + .Jack => |Jack| { + try writer.writeInt(NodeID, Jack, .Little); + }, .Switch => |Switch| { try writer.writeInt(NodeID, @enumToInt(Switch), .Little); }, @@ -879,6 +893,7 @@ pub const NodeKind = union(NodeEnum) { .Xor => |Xor| std.fmt.format(writer, "{s} [{}, {}]", .{ name, Xor[0], Xor[1] }), .Source => std.fmt.format(writer, "{s}", .{name}), .Plug => |Plug| std.fmt.format(writer, "{s} [{?}]", .{ name, Plug }), + .Jack => |Jack| std.fmt.format(writer, "{s} [{}]", .{ name, Jack }), .Switch => |Switch| std.fmt.format(writer, "{s} [{s}]", .{ name, @tagName(Switch) }), .Join => |Join| std.fmt.format(writer, "{s} [{}]", .{ name, Join }), .Outlet => |Outlet| std.fmt.format(writer, "{s} [{}]", .{ name, Outlet }), diff --git a/tools/LDtkImport.zig b/tools/LDtkImport.zig index c221522..cca85a7 100644 --- a/tools/LDtkImport.zig +++ b/tools/LDtkImport.zig @@ -371,13 +371,23 @@ pub fn buildCircuit(alloc: std.mem.Allocator, levels: []world.Level) !std.ArrayL // TODO }, .Plug => { - // These have already been added, so just continue the - // search - next_node = @intCast(world.NodeID, nodes.items.len); - try nodes.append(.{ - .kind = .{ .Plug = null }, - .coord = coord, - }); + if (last_node == std.math.maxInt(world.NodeID)) { + next_node = @intCast(world.NodeID, nodes.items.len); + try nodes.append(.{ + .kind = .{ .Plug = null }, + .coord = coord, + }); + } else { + // This plug is connected directly to a circuit fragment + // with a source, so we are at the end of the search along + // this conduit path. Make the plug a jack instead, so it + // looks at the last node instead of being null + try nodes.append(.{ + .kind = .{ .Jack = last_node }, + .coord = coord, + }); + continue; + } }, .Outlet => { next_node = @intCast(world.NodeID, nodes.items.len);