master
Louis Pearson 2022-01-22 23:10:28 -07:00
parent d8f624a838
commit 3b467a4173
7 changed files with 114 additions and 76 deletions

File diff suppressed because one or more lines are too long

View File

@ -122,8 +122,8 @@
27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
75,26,26,26,26,26,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
27,0,0,0,0,0,0,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
75,26,26,26,26,26,72,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
27,0,0,0,0,0,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
40,26,46,0,47,25,29,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,27,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

File diff suppressed because one or more lines are too long

View File

@ -94,7 +94,7 @@ fn get_plugs(tile: u8) Plugs {
}
pub fn get_cell(this: @This(), cell: Cell) ?u8 {
if (this.cell_map.getConstCell(cell)) |c| return c.tile;
if (this.cell_map.get_const(cell)) |c| return c.tile;
const c = cell;
if (c[0] < 0 or c[0] > this.map_size[0] or c[1] > this.map_size[1] or c[1] < 0) return null;
const i = @intCast(usize, @mod(c[0], this.map_size[0]) + (c[1] * this.map_size[1]));
@ -102,89 +102,52 @@ pub fn get_cell(this: @This(), cell: Cell) ?u8 {
}
pub fn set_cell(this: *@This(), cell: Cell, tile: u8) void {
var cellstate = CellState{ .cell = cell, .tile = tile };
this.cell_map.setCell(cellstate);
var cellstate = CellState{ .tile = tile };
this.cell_map.set(cell, cellstate);
}
const MAXCELLS = 400;
const MAXBRIDGES = 10;
const MAXSOURCES = 10;
const MAXDOORS = 10;
const CellState = struct { cell: Cell, enabled: bool = false, tile: u8 };
const CellMap = struct {
data: std.BoundedArray(CellState, MAXCELLS),
pub fn init() @This() {
return @This(){ .data = std.BoundedArray(CellState, MAXCELLS).init(0) catch unreachable };
}
pub fn getCell(this: *@This(), cell: Cell) ?*CellState {
for (this.data.slice()) |*cellstate| {
if (@reduce(.And, cellstate.cell == cell)) {
return cellstate;
}
}
return null;
}
pub fn getConstCell(this: @This(), cell: Cell) ?CellState {
for (this.data.constSlice()) |cellstate| {
if (@reduce(.And, cellstate.cell == cell)) {
return cellstate;
}
}
return null;
}
pub fn setCell(this: *@This(), newcell: CellState) void {
for (this.data.slice()) |*cellstate| {
if (@reduce(.And, cellstate.cell == newcell.cell)) {
cellstate.* = newcell;
return;
}
}
this.data.append(newcell) catch unreachable;
}
pub fn is_enabled(this: @This(), cell: Cell) bool {
if (this.getConstCell(cell)) |cellstate| {
return cellstate.enabled;
}
return false;
}
};
const CellState = struct { enabled: bool = false, tile: u8 };
const CellMap = util.Map(Cell, CellState, MAXCELLS);
const BridgeState = struct { cells: [2]Cell, id: usize, enabled: bool };
const DoorState = struct { cell: Cell, enabled: bool };
map: []const u8,
map_size: Vec2,
cell_map: CellMap,
bridges: std.BoundedArray(BridgeState, MAXBRIDGES),
sources: std.BoundedArray(Cell, MAXSOURCES),
doors: std.BoundedArray(DoorState, MAXDOORS),
pub fn init() @This() {
pub fn init(map: []const u8, map_size: Vec2) @This() {
var this = @This(){
.map = &assets.conduit,
.map_size = assets.conduit_size,
.map = map,
.map_size = map_size,
.cell_map = CellMap.init(),
.bridges = std.BoundedArray(BridgeState, MAXBRIDGES).init(0) catch unreachable,
.sources = std.BoundedArray(Cell, MAXSOURCES).init(0) catch unreachable,
.doors = std.BoundedArray(DoorState, MAXDOORS).init(0) catch unreachable,
};
return this;
}
pub fn load(this: *@This(), map: []const u8, map_size: Vec2) void {
this.map = map;
this.map_size = map_size;
}
pub fn indexOf(this: @This(), cell: Cell) ?usize {
if (cell[0] < 0 or cell[0] >= this.map_size[0] or cell[1] >= this.map_size[1] or cell[1] < 0) return null;
return @intCast(usize, @mod(cell[0], this.map_size[0]) + (cell[1] * this.map_size[1]));
}
pub fn enable(this: *@This(), cell: Cell) void {
if (this.cell_map.getCell(cell)) |c| {
if (this.cell_map.get(cell)) |c| {
c.enabled = true;
return;
}
const t = this.get_cell(cell) orelse unreachable;
this.cell_map.setCell(.{ .cell = cell, .tile = t, .enabled = true });
this.cell_map.set(cell, .{ .tile = t, .enabled = true });
}
pub fn bridge(this: *@This(), cells: [2]Cell, bridgeID: usize) void {
@ -201,6 +164,12 @@ pub fn addSource(this: *@This(), cell: Cell) void {
}
}
pub fn addDoor(this: *@This(), cell: Cell) void {
if (this.indexOf(cell)) |_| {
this.doors.append(.{ .cell = cell, .enabled = false }) catch unreachable;
}
}
pub fn enabledBridges(this: @This()) std.BoundedArray(usize, MAXBRIDGES) {
var items = std.BoundedArray(usize, MAXBRIDGES).init(0) catch unreachable;
for (this.bridges.constSlice()) |b| {
@ -209,8 +178,19 @@ pub fn enabledBridges(this: @This()) std.BoundedArray(usize, MAXBRIDGES) {
return items;
}
pub fn enabledDoors(this: @This()) std.BoundedArray(Cell, MAXDOORS) {
var items = std.BoundedArray(Cell, MAXDOORS).init(0) catch unreachable;
for (this.doors.constSlice()) |d| {
if (d.enabled) items.append(d.cell) catch unreachable;
}
return items;
}
pub fn isEnabled(this: @This(), cell: Cell) bool {
return this.cell_map.is_enabled(cell);
if (this.cell_map.get_const(cell)) |c| {
return c.enabled;
}
return false;
}
pub fn toggle(this: *@This(), c: Cell) void {
@ -218,14 +198,13 @@ pub fn toggle(this: *@This(), c: Cell) void {
if (this.get_cell(cell)) |tile| {
if (is_switch(tile)) {
const toggled = toggle_switch(tile);
w4.tracef("%d, %d: %d to %d", cell[0], cell[1], tile, toggled);
this.set_cell(cell, toggled);
}
}
}
pub fn clear(this: *@This()) void {
for (this.cell_map.data.slice()) |*cell| {
for (this.cell_map.values.slice()) |*cell| {
cell.enabled = false;
}
this.bridges.resize(0) catch unreachable;
@ -250,7 +229,16 @@ pub fn fill(this: *@This()) usize {
const index = this.indexOf(cell) orelse continue;
const hasVisited = std.mem.containsAtLeast(usize, visited.slice(), 1, &.{index});
if (hasVisited) continue;
const tile = this.get_cell(cell) orelse continue;
const tile = this.get_cell(cell) orelse {
for (this.doors.slice()) |*d| {
if (cell[0] > 9 and cell[1] > 50)
w4.tracef("%d, %d %d, %d", cell[0], cell[1], d.cell[0], d.cell[1]);
if (@reduce(.And, d.cell == cell)) {
d.enabled = true;
}
}
continue;
};
visited.append(index) catch unreachable;
this.enable(cell);
count += 1;

View File

@ -187,12 +187,10 @@ fn showErr(msg: []const u8) noreturn {
export fn start() void {
particles = ParticleSystem.init();
circuit = Circuit.init();
map = Map.init(fba.allocator()) catch showErr("Init map");
circuit = Circuit.init(&assets.conduit, assets.conduit_size);
map = Map.init(&assets.solid, assets.solid_size);
camera = @divTrunc(assets.spawn, @splat(2, @as(i32, 20))) * @splat(2, @as(i32, 20));
circuit.load(&assets.conduit, assets.conduit_size);
map.load(&assets.solid, assets.solid_size);
const tile_size = Vec2{ 8, 8 };
const offset = Vec2{ 4, 8 };
@ -231,6 +229,10 @@ export fn start() void {
circuit.addSource(source);
}
for (assets.doors) |door| {
circuit.addDoor(door);
}
updateCircuit();
}
@ -433,6 +435,11 @@ fn updateCircuit() void {
const cellEnd = util.world2cell(end.pos);
if (circuit.isEnabled(cellBegin) or circuit.isEnabled(cellEnd)) wire.enabled = true;
}
const enabledDoors = circuit.enabledDoors();
for (enabledDoors.constSlice()) |door| {
w4.tracef("here");
map.set_cell(door, 0);
}
}
fn wirePhysicsProcess(dt: f32, wire: *Wire) void {

View File

@ -19,26 +19,32 @@ const tilemap_width = 16;
const tilemap_height = 16;
const tilemap_stride = 128;
alloc: std.mem.Allocator,
const UpdateMap = util.Map(Cell, u8, MAXCELLS);
tiles: []const u8,
tile_updates: UpdateMap,
map_size: Vec2,
pub fn init(alloc: std.mem.Allocator) !@This() {
pub fn init(map: []const u8, map_size: Vec2) @This() {
var this = @This(){
.alloc = alloc,
.tiles = &assets.solid,
.map_size = assets.solid_size,
.tiles = map,
.tile_updates = UpdateMap.init(),
.map_size = map_size,
};
return this;
}
pub fn load(this: *@This(), map: []const u8, map_size: Vec2) void {
this.tiles = map;
this.map_size = map_size;
pub fn set_cell(this: *@This(), cell: Cell, tile: u8) void {
this.tile_updates.set(cell, tile);
}
pub fn deinit(this: @This()) void {
this.alloc.free(this.tiles);
pub fn get_cell(this: @This(), cell: Cell) ?u8 {
if (this.tile_updates.get_const(cell)) |tile| return tile;
const x = cell[0];
const y = cell[1];
if (x < 0 or x > this.map_size[0] or y < 0 or y > this.map_size[1]) return null;
const i = x + y * this.map_size[0];
return this.tiles[@intCast(u32, i)];
}
pub fn draw(this: @This(), offset: Vec2) void {
@ -47,9 +53,9 @@ pub fn draw(this: @This(), offset: Vec2) void {
while (y < height) : (y += 1) {
var x: usize = 0;
while (x < width) : (x += 1) {
const cell = Vec2{ @intCast(i32, x), @intCast(i32, y) } + offset;
const pos = Vec2{ @intCast(i32, x), @intCast(i32, y) } * tile_size;
const a = (@intCast(usize, offset[0]) + x) + (@intCast(usize, offset[1]) + y) * @intCast(usize, this.map_size[0]);
const tilePlus = this.tiles[a];
const tilePlus = this.get_cell(cell) orelse continue;
if (tilePlus == 0) continue;
const tile = tilePlus - 1;
const t = Vec2{
@ -100,7 +106,7 @@ pub fn collide(this: @This(), rect: util.AABB) std.BoundedArray(util.AABB, 9) {
}
pub fn isSolid(this: @This(), cell: Cell) bool {
if (this.getTile(cell[0], cell[1])) |tile| {
if (this.get_cell(cell)) |tile| {
return tile != 0 and tile != 1;
}
return true;

View File

@ -73,3 +73,40 @@ pub fn Queue(comptime T: type, len: usize) type {
}
};
}
pub fn Map(comptime K: type, comptime V: type, len: usize) type {
return struct {
keys: std.BoundedArray(K, len),
values: std.BoundedArray(V, len),
pub fn init() @This() {
return @This(){
.keys = std.BoundedArray(K, len).init(0) catch unreachable,
.values = std.BoundedArray(V, len).init(0) catch unreachable,
};
}
pub fn get(this: *@This(), key: K) ?*V {
for (this.keys.slice()) |k, i| {
if (@reduce(.And, key == k)) {
return &this.values.slice()[i];
}
}
return null;
}
pub fn get_const(this: @This(), key: Cell) ?V {
for (this.keys.constSlice()) |k, i| {
if (@reduce(.And, key == k)) {
return this.values.constSlice()[i];
}
}
return null;
}
pub fn set(this: *@This(), key: K, new: V) void {
if (this.get(key)) |v| {
v.* = new;
} else {
this.keys.append(key) catch unreachable;
this.values.append(new) catch unreachable;
}
}
};
}