Partially fix circuits and wires

master
Louis Pearson 2022-08-04 22:16:33 -06:00
parent dd26d1f093
commit e8ebe43de6
6 changed files with 95 additions and 117 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 654 B

After

Width:  |  Height:  |  Size: 655 B

File diff suppressed because one or more lines are too long

View File

@ -10,31 +10,37 @@ pub fn is_circuit(tile: u8) bool {
} }
pub fn is_plug(tile: u8) bool { pub fn is_plug(tile: u8) bool {
return (tile >= 43 and tile <= 46) or tile == 41; return tile >= 17 and tile <= 20;
} }
pub fn is_conduit(tile: u8) bool { pub fn is_conduit(tile: u8) bool {
return (tile >= 23 and tile <= 26) or return tile >= 97 and tile <= 112;
(tile >= 39 and tile <= 42) or
(tile >= 55 and tile <= 58) or
(tile >= 71 and tile <= 74);
} }
pub fn is_switch(tile: u8) bool { pub fn is_switch(tile: u8) bool {
return tile >= 27 and tile <= 30; return tile >= 25 and tile <= 32;
} }
pub fn is_logic(tile: u8) bool { pub fn is_logic(tile: u8) bool {
return tile >= 59 and tile <= 62; return tile >= 21 and tile <= 24;
} }
pub fn toggle_switch(tile: u8) u8 { pub fn toggle_switch(tile: u8) u8 {
return switch (tile) { return switch (tile) {
// Tee west
25 => 26,
26 => 25,
// Tee east
27 => 28, 27 => 28,
28 => 27, 28 => 27,
// Vertical
29 => 30, 29 => 30,
30 => 29, 30 => 29,
else => unreachable, // Horizontal
31 => 32,
32 => 21,
// Not a switch, pass tile through
else => tile,
}; };
} }
@ -68,79 +74,47 @@ const Current = [4]bool;
/// Returns sides that can conduct current /// Returns sides that can conduct current
fn get_inputs(tile: u8) Current { fn get_inputs(tile: u8) Current {
return switch (tile) { return switch (tile) {
// Corners // Conduit recieves from every side
23 => .{ false, true, true, false }, 17...20,
24 => .{ false, false, true, true }, 43...47,
39 => .{ true, true, false, false }, 97...113,
40 => .{ true, false, false, true }, => .{ true, true, true, true },
// Straight // Switch_On
25 => .{ false, true, false, true }, 25,
26 => .{ true, false, true, false }, 27,
// Cross 30,
41 => .{ false, false, false, false }, => .{ true, false, true, false },
42 => .{ true, true, true, true }, // Switch_Off
// Ends 26,
55 => .{ false, false, true, false }, 28,
56 => .{ false, true, false, false }, => .{ false, false, true, true },
57 => .{ true, false, false, false }, // And, Xor
58 => .{ false, false, false, true }, 21,
// Tees 23,
71 => .{ false, true, true, true }, => .{ false, true, false, true },
72 => .{ true, true, false, true }, // Not
73 => .{ true, false, true, true }, 22 => .{ false, false, true, false },
74 => .{ true, true, true, false },
// Plugs
43 => .{ false, false, true, false },
44 => .{ true, false, false, false },
45 => .{ false, false, false, true },
46 => .{ false, true, false, false },
// Switch
27, 28, 29, 30 => .{ true, false, true, false },
// Logic
59 => .{ false, true, false, true },
60 => .{ false, false, true, false },
61 => .{ false, true, false, true },
else => .{ false, false, false, false }, else => .{ false, false, false, false },
}; };
} }
fn get_outputs(tile: u8) Current { fn get_outputs(tile: u8) Current {
return switch (tile) { return switch (tile) {
// Corners // Conduit goes out every side
23 => .{ false, true, true, false }, 17...21,
24 => .{ false, false, true, true }, 43...47,
39 => .{ true, true, false, false }, 97...113,
40 => .{ true, false, false, true }, => .{ true, true, true, true },
// Straight // Switches
25 => .{ false, true, false, true }, // Tee west
25 => .{ false, false, true, true },
26 => .{ true, false, true, false }, 26 => .{ true, false, true, false },
// Cross // Tee east
41 => .{ false, false, false, false }, 27 => .{ false, true, true, false },
42 => .{ true, true, true, true }, 28 => .{ true, false, true, false },
// Ends // Vertical
55 => .{ false, false, true, false }, 29 => .{ false, false, true, false },
56 => .{ false, true, false, false }, 30 => .{ true, false, true, false },
57 => .{ true, false, false, false },
58 => .{ false, false, false, true },
// Tees
71 => .{ false, true, true, true },
72 => .{ true, true, false, true },
73 => .{ true, false, true, true },
74 => .{ true, true, true, false },
// Plugs
43 => .{ false, false, true, false },
44 => .{ true, false, false, false },
45 => .{ false, false, false, true },
46 => .{ false, true, false, false },
// Switch
27, 29 => .{ true, false, true, false },
28 => .{ false, false, false, true },
30 => .{ false, true, false, false },
// Logic
// Calculated in fill
// 59 => .{ false, false, false, false },
// 60 => .{ true, false, false, false },
// 61 => .{ true, false, false, false },
else => .{ false, false, false, false }, else => .{ false, false, false, false },
}; };
} }
@ -149,9 +123,9 @@ const Logic = union(enum) { Not, And, Xor };
fn get_logic(tile: u8) ?Logic { fn get_logic(tile: u8) ?Logic {
return switch (tile) { return switch (tile) {
59 => .And, 21 => .And,
60 => .Not, 22 => .Not,
61 => .Xor, 23 => .Xor,
else => null, else => null,
}; };
} }
@ -160,13 +134,10 @@ const Plugs = [4]bool;
/// Returns sides where wires may be plugged /// Returns sides where wires may be plugged
fn get_plugs(tile: u8) Plugs { fn get_plugs(tile: u8) Plugs {
return switch (tile) { return switch (tile) {
// Plugs 17 => .{ false, false, true, false },
43 => .{ true, false, false, false }, 18 => .{ false, false, false, true },
44 => .{ false, false, true, false }, 19 => .{ false, true, false, false },
45 => .{ false, true, false, false }, 20 => .{ true, false, false, false },
46 => .{ false, false, false, true },
// Cross
41 => .{ true, true, true, true },
else => .{ false, false, false, false }, else => .{ false, false, false, false },
}; };
} }

View File

@ -132,6 +132,8 @@ pub fn extractLevel(opt: Options) !void {
const y = @divTrunc(i, width); const y = @divTrunc(i, width);
const stride = width; const stride = width;
if (circuit_map[i] == .Source) circuit.addSource(.{@intCast(i32, x), @intCast(i32, y)});
if (circuit_map[i] == .None) { if (circuit_map[i] == .None) {
autotiles[i] = null; autotiles[i] = null;
continue; continue;
@ -186,8 +188,8 @@ pub fn extractLevel(opt: Options) !void {
.Switch_On => opt.switch_on.find(autotile), .Switch_On => opt.switch_on.find(autotile),
.Switch_Off => opt.switch_off.find(autotile), .Switch_Off => opt.switch_off.find(autotile),
.Plug => opt.plug.find(autotile), .Plug => opt.plug.find(autotile),
.And => 60, .And => 21,
.Xor => 62, .Xor => 23,
else => 0, else => 0,
}; };
circuit.map[i] = tile; circuit.map[i] = tile;

View File

@ -268,16 +268,19 @@ pub fn start() !void {
_ = try wires.resize(0); _ = try wires.resize(0);
var a: usize = 0; var a: usize = 0;
while (level.getWire(a)) |wire| : (a += 1) { while (level.getWire(a)) |wire| : (a += 1) {
const p1 = util.vec2ToVec2f(Vec2{ wire[0].x, wire[0].y } * tile_size + Vec2{ 4, 4 });
const p2 = util.vec2ToVec2f(Vec2{ wire[1].x, wire[1].y } * tile_size + Vec2{ 4, 4 });
var w = try wires.addOne(); var w = try wires.addOne();
_ = try w.nodes.resize(0); _ = try w.nodes.resize(0);
// const divisions = wire.divisions; // const divisions = wire.divisions;
const divisions = 10; const divisions = 10;
var i: usize = 0; var i: usize = 0;
while (i <= divisions) : (i += 1) { while (i <= divisions) : (i += 1) {
try w.nodes.append(Pos.init(Vec2f{ 0, 0 })); try w.nodes.append(Pos.init(p1));
} }
w.begin().pos = util.vec2ToVec2f(Vec2{ wire[0].x, wire[0].y } * tile_size); w.begin().pos = p1;
w.end().pos = util.vec2ToVec2f(Vec2{ wire[1].x, wire[1].y } * tile_size); w.end().pos = p2;
w.begin().pinned = wire[0].kind == world.EntityKind.WireAnchor; w.begin().pinned = wire[0].kind == world.EntityKind.WireAnchor;
w.end().pinned = wire[1].kind == world.EntityKind.WireEndAnchor; w.end().pinned = wire[1].kind == world.EntityKind.WireEndAnchor;

View File

@ -95,37 +95,39 @@ fn make(step: *std.build.Step) !void {
}; };
try entity_array.append(world_entity); try entity_array.append(world_entity);
} else { } else {
const p1_x: i16 = @intCast(i16, entity.__grid[0]);
const p1_y: i16 = @intCast(i16, entity.__grid[1]);
var anchor1 = false;
var anchor2 = false;
var p2_x: i16 = p1_x;
var p2_y: i16 = p1_y;
for (entity.fieldInstances) |field| {
if (std.mem.eql(u8, field.__identifier, "Anchor")) {
const anchors = field.__value.Array.items;
anchor1 = anchors[0].Bool;
anchor2 = anchors[1].Bool;
} else if (std.mem.eql(u8, field.__identifier, "Point")) {
const end = field.__value.Array.items.len - 1;
const endpoint = field.__value.Array.items[end];
const x = endpoint.Object.get("cx").?;
const y = endpoint.Object.get("cy").?;
p2_x = @intCast(i16, x.Integer);
p2_y = @intCast(i16, y.Integer);
}
}
const wire_begin = world.Entity{ const wire_begin = world.Entity{
.kind = .WireNode, .kind = if (anchor1) .WireAnchor else .WireNode,
.x = @intCast(i16, entity.__grid[0]), .x = p1_x,
.y = @intCast(i16, entity.__grid[1]), .y = p1_y,
}; };
try entity_array.append(wire_begin); try entity_array.append(wire_begin);
for (entity.fieldInstances) |field| {
if (std.mem.eql(u8, field.__identifier, "Point")) {
const end = field.__value.Array.items.len - 1;
const endpoint = field.__value.Array.items[end];
// const jstr = switch (endpoint) {
// .Array => "Array",
// .Object => "Object",
// .Integer => "Integer",
// else => "Other",
// };
// std.log.warn("{s}", .{jstr});
// std.log.warn("{}", .{endpoint.Integer});
// endpoint.dump();
const x = endpoint.Object.get("cx").?;
const y = endpoint.Object.get("cy").?;
const wire_end = world.Entity{ const wire_end = world.Entity{
.kind = .WireEndNode, .kind = if (anchor2) .WireEndAnchor else .WireEndNode,
.x = @intCast(i16, x.Integer), .x = p2_x,
.y = @intCast(i16, y.Integer), .y = p2_y,
}; };
try entity_array.append(wire_end); try entity_array.append(wire_end);
break;
}
}
} }
} }
} }