Code clean-up

master
Louis Pearson 2022-01-22 14:46:37 -07:00
parent 555c200033
commit 3644d40849
3 changed files with 40 additions and 53 deletions

View File

@ -4,7 +4,7 @@ const util = @import("util.zig");
const Vec2 = util.Vec2;
const Cell = util.Cell;
fn is_circuit(tile: u8) bool {
pub fn is_circuit(tile: u8) bool {
return is_plug(tile) or is_conduit(tile) or is_switch(tile);
}
@ -12,7 +12,7 @@ pub fn is_plug(tile: u8) bool {
return (tile >= 149 and tile <= 153) or tile == 147;
}
fn is_conduit(tile: u8) bool {
pub fn is_conduit(tile: u8) bool {
return (tile >= 128 and tile < 132) or
(tile >= 144 and tile < 148) or
(tile >= 160 and tile < 164) or
@ -23,15 +23,24 @@ pub fn is_switch(tile: u8) bool {
return tile >= 134 and tile < 136;
}
fn toggle_switch(tile: u8) u8 {
pub fn toggle_switch(tile: u8) u8 {
return if (tile == 134) 135 else 134;
}
const Side = enum(u2) { up, right, down, left };
fn side(s: Side) u2 {
pub fn side(s: Side) u2 {
return @enumToInt(s);
}
pub fn dir(s: Side) Cell {
return switch (s) {
.up => util.Dir.up,
.down => util.Dir.down,
.left => util.Dir.left,
.right => util.Dir.right,
};
}
const Current = [4]bool;
/// Returns sides that can conduct current
fn get_inputs(tile: u8) Current {
@ -83,31 +92,6 @@ fn get_plugs(tile: u8) Plugs {
};
}
const Signals = [4]bool;
/// Returns sides where a signal may be sent
fn get_signals(tile: u8) Signals {
return switch (tile) {
// Ends
160 => .{ true, true, false, true },
161 => .{ true, false, true, true },
162 => .{ false, true, true, true },
163 => .{ true, true, true, false },
// Switches
134 => .{ true, false, true, false },
135 => .{ true, false, true, false },
else => .{ false, false, false, false },
};
}
fn dir(s: Side) Cell {
return switch (s) {
.up => Vec2{ 0, -1 },
.down => Vec2{ 0, 1 },
.left => Vec2{ -1, 0 },
.right => Vec2{ 1, 0 },
};
}
pub fn get_cell(this: @This(), c: Cell) ?u8 {
if (c[0] < 0 or c[0] > 19 or c[1] > 19 or c[1] < 0) return null;
const i = @intCast(usize, @mod(c[0], 20) + (c[1] * 20));
@ -202,34 +186,26 @@ pub fn toggle(this: *@This(), cell: Cell) void {
}
}
const Queue = struct {
data: std.BoundedArray(Cell, MAXCELLS),
pub fn init() @This() {
return @This(){
.data = std.BoundedArray(Cell, MAXCELLS).init(0) catch unreachable,
};
pub fn clear(this: *@This()) void {
for (this.cells) |*cell| {
cell.enabled = false;
}
pub fn insert(this: *@This(), c: Cell) void {
this.data.insert(0, c) catch unreachable;
}
pub fn remove(this: *@This()) ?Cell {
return this.data.popOrNull();
}
};
this.bridges.resize(0) catch unreachable;
}
const w4 = @import("wasm4.zig");
// Returns number of cells filled
pub fn fill(this: *@This(), rootRaw: Cell) usize {
const Queue = util.Queue(Cell, MAXCELLS);
var count: usize = 0;
const root = rootRaw - this.offset;
var visited = std.StaticBitSet(MAXCELLS).initEmpty();
var visited: [MAXCELLS]bool = [_]bool{false} ** MAXCELLS;
var q = Queue.init();
q.insert(root);
while (q.remove()) |cell| {
const index = this.indexOf(cell) orelse continue;
const tile = this.get_cell(cell) orelse continue;
if (visited.isSet(index)) continue;
visited.set(index);
if (visited[index]) continue;
visited[index] = true;
this.enable(cell);
count += 1;
for (get_inputs(tile)) |conductor, i| {
@ -253,10 +229,3 @@ pub fn fill(this: *@This(), rootRaw: Cell) usize {
}
return count;
}
pub fn clear(this: *@This()) void {
for (this.cells) |*cell| {
cell.enabled = false;
}
this.bridges.resize(0) catch unreachable;
}

View File

@ -1,6 +1,7 @@
const std = @import("std");
const w4 = @import("wasm4.zig");
// Adapted from https://gist.github.com/YuxiUx/c3a8787209e32fc29fb48e8454f0009c
const midiNote = [_]u16{
8, 9, 9, 10, 10, 11, 12, 12, 13, 14, 15,
15, 16, 17, 18, 19, 21, 22, 23, 24, 26, 28,

View File

@ -63,3 +63,20 @@ pub const AABB = struct {
return @This(){ .pos = this.pos + vec2f, .size = this.size };
}
};
pub fn Queue(comptime T: type, len: usize) type {
return struct {
data: std.BoundedArray(T, len),
pub fn init() @This() {
return @This(){
.data = std.BoundedArray(T, len).init(0) catch unreachable,
};
}
pub fn insert(this: *@This(), t: T) void {
this.data.insert(0, t) catch unreachable;
}
pub fn remove(this: *@This()) ?Cell {
return this.data.popOrNull();
}
};
}