disconnectPlug now disconnects the plugs

master
Louis Pearson 2022-08-09 21:47:05 -06:00
parent d3450e4323
commit cc5867424a
3 changed files with 53 additions and 36 deletions

View File

@ -299,7 +299,7 @@ pub fn clear(this: *@This()) void {
pub fn reset(this: *@This()) void { pub fn reset(this: *@This()) void {
this.clear(); this.clear();
// Resizing to zero should always work // Resizing to zero should always work
this.sources.resize(0) catch unreachable; this.sources.reset();
} }
const w4 = @import("wasm4.zig"); const w4 = @import("wasm4.zig");

View File

@ -697,17 +697,19 @@ fn manipulationProcess(pos: *Pos, control: *Control) !void {
switch (i.details) { switch (i.details) {
.wire => |wire| { .wire => |wire| {
control.grabbing = .{ .id = wire.id, .which = wire.which }; control.grabbing = .{ .id = wire.id, .which = wire.which };
wires.slice()[wire.id].nodes.slice()[wire.which].pos = pos.pos + Vec2f{ 0, -4 }; var wireStruct = wires.slice()[wire.id];
wires.slice()[wire.id].nodes.slice()[wire.which].pinned = false; const wireSlice = wires.slice()[wire.id].nodes.slice();
const local32 = vec2ftovec2(wires.slice()[wire.id].nodes.slice()[wire.which].pos / @splat(2, @as(f32, 8))); wireSlice[wire.which].pos = pos.pos + Vec2f{ 0, -4 };
const x = level.world_x * 20 + @intCast(i16, local32[0]); wireSlice[wire.which].pinned = false;
const y = level.world_y * 20 + @intCast(i16, local32[1]); const coord1 = Coord.fromVec2(util.world2cell(wireStruct.begin().pos));
db.disconnectPlug(Coord.init(.{ x, y })); const coord2 = Coord.fromVec2(util.world2cell(wireStruct.end().pos));
db.disconnectPlug(level, coord1, coord2);
try updateCircuit(); try updateCircuit();
}, },
.plug => |plug| { .plug => |plug| {
wires.slice()[plug.wireID].nodes.slice()[plug.which].pos = vec2tovec2f(indicator.?.pos); const wireSlice = wires.slice()[plug.wireID].nodes.slice();
wires.slice()[plug.wireID].nodes.slice()[plug.which].pinned = true; wireSlice[plug.which].pos = vec2tovec2f(indicator.?.pos);
wireSlice[plug.which].pinned = true;
control.grabbing = null; control.grabbing = null;
try updateCircuit(); try updateCircuit();
}, },
@ -742,11 +744,7 @@ fn updateCircuit() !void {
circuit.bridge(.{ cellBegin, cellEnd }, wireID); circuit.bridge(.{ cellBegin, cellEnd }, wireID);
const topleft = Coord.fromWorld(level.world_x, level.world_y); db.connectPlugs(level, cellBegin, cellEnd) catch {
const globalBegin = cellBegin.addC(topleft);
const globalEnd = cellEnd.addC(topleft);
db.connectPlugs(globalBegin, globalEnd) catch {
w4.tracef("connect plugs error"); w4.tracef("connect plugs error");
}; };
} }
@ -756,15 +754,18 @@ fn updateCircuit() !void {
// Simulate circuit // Simulate circuit
_ = try circuit.fill(frame_alloc, db, level); _ = try circuit.fill(frame_alloc, db, level);
w4.tracef("[updateCircuit] circuit filled"); w4.tracef("[updateCircuit] circuit filled");
// for (circuit.nodes) |node, i| {
// w4.tracef("%d: %d", i, node);
// }
// Energize wires // Energize wires
{ {
var i: usize = 0; for (wires.slice()) |*wire| {
while (circuit.enabledBridge(i)) |wireID| : (i += 1) { const begin = wire.begin();
wires.slice()[wireID].enabled = true; const end = wire.end();
const coord1 = Coord.fromVec2(util.world2cell(begin.pos));
const coord2 = Coord.fromVec2(util.world2cell(end.pos));
const energized1 = if (db.getLevelNodeID(level, coord1)) |node| db.circuit_info[node].energized else false;
const energized2 = if (db.getLevelNodeID(level, coord2)) |node| db.circuit_info[node].energized else false;
if ((energized1 and begin.pinned) or
(energized2 and end.pinned)) wire.enabled = true;
} }
} }
@ -783,22 +784,29 @@ fn updateCircuit() !void {
} }
} }
w4.tracef("[updateCircuit] end");
printCircuit();
}
fn printCircuit() void {
for (db.circuit_info) |node, n| { for (db.circuit_info) |node, n| {
const e = @boolToInt(node.energized); const e = @boolToInt(node.energized);
const x = node.coord.val[0];
const y = node.coord.val[1];
switch (node.kind) { switch (node.kind) {
.Conduit => |Conduit| w4.tracef("[%d]: Conduit [%d, %d] <%d>", n, Conduit[0], Conduit[1], e), .Conduit => |Conduit| w4.tracef("[%d]: Conduit (%d, %d) [%d, %d] <%d>", n, x, y, Conduit[0], Conduit[1], e),
.And => |And| w4.tracef("[%d]: And [%d, %d] <%d>", n, And[0], And[1], e), .And => |And| w4.tracef("[%d]: And (%d, %d) [%d, %d] <%d>", n, x, y, And[0], And[1], e),
.Xor => |Xor| w4.tracef("[%d]: Xor [%d, %d] <%d>", n, Xor[0], Xor[1], e), .Xor => |Xor| w4.tracef("[%d]: Xor (%d, %d) [%d, %d] <%d>", n, x, y, Xor[0], Xor[1], e),
.Source => w4.tracef("[%d]: Source (%d, %d)", n, node.coord.val[0], node.coord.val[1]), .Source => w4.tracef("[%d]: Source (%d, %d) (%d, %d)", n, x, y, node.coord.val[0], node.coord.val[1]),
.Socket => |Socket| { .Socket => |Socket| {
const socket = Socket orelse std.math.maxInt(world.NodeID); const socket = Socket orelse std.math.maxInt(world.NodeID);
w4.tracef("[%d]: Socket [%d] <%d>", n, socket, e); w4.tracef("[%d]: Socket (%d, %d) [%d] <%d>", n, x, y, socket, e);
}, },
.Plug => |Plug| w4.tracef("[%d]: Plug [%d] <%d>", n, Plug, e), .Plug => |Plug| w4.tracef("[%d]: Plug (%d, %d) [%d] <%d>", n, x, y, Plug, e),
.Switch => |Switch| w4.tracef("[%d]: Switch %d [%d] <%d>", n, Switch.state, Switch.source, e), .Switch => |Switch| w4.tracef("[%d]: Switch (%d, %d) %d [%d] <%d>", n, x, y, Switch.state, Switch.source, e),
.SwitchOutlet => |Switch| w4.tracef("[%d]: SwitchOutlet %d [%d] <%d>", n, Switch.which, Switch.source, e), .SwitchOutlet => |Switch| w4.tracef("[%d]: SwitchOutlet (%d, %d) %d [%d] <%d>", n, x, y, Switch.which, Switch.source, e),
.Join => |Join| w4.tracef("[%d]: Join [%d] <%d>", n, Join, e), .Join => |Join| w4.tracef("[%d]: Join (%d, %d) [%d] <%d>", n, x, y, Join, e),
.Outlet => |Outlet| w4.tracef("[%d]: Outlet [%d] <%d>", n, Outlet, e), .Outlet => |Outlet| w4.tracef("[%d]: Outlet (%d, %d) [%d] <%d>", n, x, y, Outlet, e),
} }
} }
} }

View File

@ -766,9 +766,9 @@ pub const Database = struct {
return db.getNodeID(coord.addC(levelc)); return db.getNodeID(coord.addC(levelc));
} }
pub fn connectPlugs(db: *Database, p1: Coord, p2: Coord) !void { pub fn connectPlugs(db: *Database, level: Level, p1: Coord, p2: Coord) !void {
const p1id = db.getNodeID(p1) orelse return; const p1id = db.getLevelNodeID(level, p1) orelse return;
const p2id = db.getNodeID(p2) orelse return; const p2id = db.getLevelNodeID(level, p2) orelse return;
if (db.circuit_info[p1id].kind == .Plug and db.circuit_info[p2id].kind == .Socket) { if (db.circuit_info[p1id].kind == .Plug and db.circuit_info[p2id].kind == .Socket) {
db.circuit_info[p2id].kind.Socket = p1id; db.circuit_info[p2id].kind.Socket = p1id;
@ -779,10 +779,19 @@ pub const Database = struct {
} }
} }
pub fn disconnectPlug(db: *Database, plug: Coord) void { pub fn disconnectPlug(db: *Database, level: Level, plug1: Coord, plug2: Coord) void {
const levelc = Coord.fromWorld(level.world_x, level.world_y);
const coord1 = plug1.addC(levelc);
const coord2 = plug2.addC(levelc);
var found: usize = 0;
for (db.circuit_info) |node, i| { for (db.circuit_info) |node, i| {
if (!plug.eq(node.coord)) continue; if (!coord1.eq(node.coord) and !coord2.eq(node.coord)) continue;
db.circuit_info[i].energized = false; found += 1;
if (db.circuit_info[i].kind == .Socket) {
db.circuit_info[i].kind.Socket = null;
db.circuit_info[i].energized = false;
break;
}
} }
} }