diff --git a/src/server/main.zig b/src/server/main.zig index 9225665..eb60a89 100644 --- a/src/server/main.zig +++ b/src/server/main.zig @@ -132,10 +132,39 @@ pub fn main() !void { } fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.ENetPeer, the_chunk: *shared.chunk.Chunk) !void { - var aw = try std.io.Writer.Allocating.initCapacity(allocator, 128); - defer aw.deinit(); + var aw = try std.ArrayList(u8).initCapacity(allocator, 128); + defer aw.deinit(allocator); - // var writer = bufzilla.Writer.init(&aw.writer); + _ = peer; + + // var w = bufzilla.Writer.init(&aw.writer); + + var writer = shared.bits.BitWriter.init(&aw); + + // Write 3 bits (101) + try writer.write(allocator, @as(u3, 0x5)); + // Write 1 bit (true = 1) + try writer.write(allocator, true); + // Write 4 bits (0000) + try writer.write(allocator, @as(u3, 0x1)); + + try writer.write(allocator, @as(u4, 0xE)); + + try writer.write(allocator, @as(u32, 0xFFFFFFFA)); + + var reader = shared.bits.BitReader.init(writer.written()); + + const _1 : u3 = reader.read(u3); + const _2 : bool = reader.read(bool); + const _3 : u3 = reader.read(u3); + const _4 : u4 = reader.read(u4); + const _5 : u32 = reader.read(u32); + + std.log.info("_1: {}", .{_1}); + std.log.info("_1: {}", .{_2}); + std.log.info("_1: {}", .{_3}); + std.log.info("_1: {}", .{_4}); + std.log.info("_1: {}", .{_5}); const fields = @typeInfo(shared.chunk.Chunk).@"struct".fields; @@ -149,13 +178,15 @@ fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.EN // const msg = shared.protocol.makeSpawnMessage(T, entity); + // w.write(entity); + std.log.info("{}", .{entity}); // try writer.writeAny(msg); } } - const encoded = aw.written(); + // const encoded = aw.written(); // std.log.info("{d} bytes: {s}", .{encoded.len, encoded}); @@ -167,13 +198,13 @@ fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.EN // std.log.info("{s}\n", .{fixed2.buffered()}); - const packet = enet.enet_packet_create(encoded.ptr, encoded.len, enet.ENET_PACKET_FLAG_RELIABLE); + // const packet = enet.enet_packet_create(encoded.ptr, encoded.len, enet.ENET_PACKET_FLAG_RELIABLE); // enet.enet_host_broadcast(host, 0, packet); _ = host; - if (enet.enet_peer_send(peer, 0, packet) != 0) { - std.log.err("Could not send packet to peer.", .{}); - } + // if (enet.enet_peer_send(peer, 0, packet) != 0) { + // std.log.err("Could not send packet to peer.", .{}); + // } } //fn handle_connection(connection: std.net.Server.Connection) !void {} diff --git a/src/shared/protocol.zig b/src/shared/protocol.zig index eb2be6f..6ee0d79 100644 --- a/src/shared/protocol.zig +++ b/src/shared/protocol.zig @@ -3,6 +3,7 @@ const zm = @import("zmath"); // const bufzilla = @import("bufzilla"); const entity = @import("entity.zig"); +const bits = @import("bits.zig"); pub const SERVER_PORT: u16 = 1337; diff --git a/src/shared/shared.zig b/src/shared/shared.zig index 9951dfb..d74da14 100644 --- a/src/shared/shared.zig +++ b/src/shared/shared.zig @@ -1,4 +1,5 @@ -pub const entity = @import("entity.zig"); -pub const chunk = @import("chunk.zig"); -pub const misc = @import("misc.zig"); +pub const entity = @import("entity.zig"); +pub const chunk = @import("chunk.zig"); +pub const misc = @import("misc.zig"); pub const protocol = @import("protocol.zig"); +pub const bits = @import("bits.zig"); diff --git a/vendor/enet/build.zig.zon b/vendor/enet/build.zig.zon index 3cde035..b277217 100644 --- a/vendor/enet/build.zig.zon +++ b/vendor/enet/build.zig.zon @@ -1,5 +1,6 @@ .{ .name = .enet, + .fingerprint = 0x201714c0933cd1fc, .version = "0.1.0", .minimum_zig_version = "0.15.2", .paths = .{