serialization getting better maybe??

This commit is contained in:
Vicente Ferrari Smith 2026-01-10 11:59:08 +01:00
parent da8589a700
commit b686052636
7 changed files with 131 additions and 88 deletions

View File

@ -10,15 +10,6 @@ pub fn build(b: *std.Build) void {
.install_subdir = "assets", .install_subdir = "assets",
}); });
const raylib_dep = b.dependency("raylib_zig", .{
.target = target,
.optimize = optimize,
});
const raylib = raylib_dep.module("raylib"); // main raylib module
const raygui = raylib_dep.module("raygui"); // raygui module
const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library
// const sdl3 = b.dependency("sdl3", .{ // const sdl3 = b.dependency("sdl3", .{
// .target = target, // .target = target,
// .optimize = .Debug, // .optimize = .Debug,
@ -96,17 +87,28 @@ pub fn build(b: *std.Build) void {
}); });
const znet_mod = znet_dep.module("znet"); const znet_mod = znet_dep.module("znet");
const znet_artifact = znet_dep.artifact("znet"); const znet_artifact = znet_dep.artifact("znet");
client.root_module.addImport("znet", znet_mod); client.root_module.addImport("znet", znet_mod);
client.linkLibrary(znet_artifact); client.linkLibrary(znet_artifact);
server.root_module.addImport("znet", znet_mod);
server.linkLibrary(znet_artifact);
const bufzilla = b.dependency("bufzilla", .{});
client.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
server.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
const raylib_dep = b.dependency("raylib_zig", .{
.target = target,
.optimize = optimize,
});
const raylib = raylib_dep.module("raylib"); // main raylib module
const raygui = raylib_dep.module("raygui"); // raygui module
const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library
client.linkLibrary(raylib_artifact); client.linkLibrary(raylib_artifact);
client.root_module.addImport("raylib", raylib); client.root_module.addImport("raylib", raylib);
client.root_module.addImport("raygui", raygui); client.root_module.addImport("raygui", raygui);
server.root_module.addImport("znet", znet_mod);
server.linkLibrary(znet_artifact);
b.installArtifact(client); b.installArtifact(client);
b.installArtifact(server); b.installArtifact(server);

View File

@ -48,6 +48,10 @@
.url = "git+https://github.com/connellr023/znet#cb11fb0c4a2b668128c436fbbccd111223c74898", .url = "git+https://github.com/connellr023/znet#cb11fb0c4a2b668128c436fbbccd111223c74898",
.hash = "znet-0.0.0-PGDNtD9RAAChe8Ky4dhWhS2XH77-xyf1X8HcDBwpM3kA", .hash = "znet-0.0.0-PGDNtD9RAAChe8Ky4dhWhS2XH77-xyf1X8HcDBwpM3kA",
}, },
.bufzilla = .{
.url = "https://github.com/theseyan/bufzilla/archive/refs/tags/v0.3.2.tar.gz",
.hash = "bufzilla-0.3.0-gU6dXi67AQAg3WIFrNQ0iafrvexj3iBwVcczrVzrN3Ir",
},
}, },
.paths = .{ .paths = .{
"build.zig", "build.zig",

View File

@ -1,8 +1,13 @@
const std = @import("std"); const std = @import("std");
const znet = @import("znet");
const bufzilla = @import("bufzilla");
const shared = @import("shared"); const shared = @import("shared");
const server = @import("server.zig"); const server = @import("server.zig");
pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.Allocator, value: T, w: *std.Io.Writer) !void { pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.Allocator, value: T) !void {
std.debug.assert(value.id == shared.entity.INVALID_ID);
const id = server.next_entity_id; const id = server.next_entity_id;
server.next_entity_id += 1; server.next_entity_id += 1;
@ -17,14 +22,25 @@ pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.
} }
// serialize entity // serialize entity
const messaged = entity.to_message();
var buffer: [64]u8 = undefined; var buffer: [64]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer); var fixed = std.io.Writer.fixed(&buffer);
try T.encode(entity, fbs.writer()); var writer = bufzilla.Writer.init(&fixed);
try writer.writeAny(messaged);
try shared.protocol.write_message( const encoded = fixed.buffered();
w,
.spawn_entity, std.log.info("\nsending the boy ( {s} )\n", .{encoded});
fbs.getWritten(),
); var iterator = server.host.iterPeers();
try w.flush();
std.log.info("iterator length?? ({})", .{iterator.peers.len});
while (iterator.next()) |peer| {
if (peer.state() == .connected) {
std.log.info("peer?? ({})", .{peer});
const packet = try znet.Packet.init(encoded, 0, .reliable);
try peer.send(packet);
}
}
} }

View File

@ -24,7 +24,7 @@ pub fn main() !void {
try znet.init(); try znet.init();
defer znet.deinit(); defer znet.deinit();
const host = try znet.Host.init(.{ server.host = try znet.Host.init(.{
.addr = try .init(.{ .addr = try .init(.{
.ip = .any, .ip = .any,
.port = .{ .uint = 5000 }, .port = .{ .uint = 5000 },
@ -44,7 +44,7 @@ pub fn main() !void {
defer shared.chunk.deinitChunk(&the_chunk, allocator); defer shared.chunk.deinitChunk(&the_chunk, allocator);
while (true) { while (true) {
while (try host.service(500)) |event| switch (event) { while (try server.host.service(500)) |event| switch (event) {
.connect => |data| { .connect => |data| {
_ = data; _ = data;
// std.log.info("{}", .{data.peer}); // std.log.info("{}", .{data.peer});
@ -73,10 +73,10 @@ pub fn main() !void {
// const w = &writer.interface; // const w = &writer.interface;
// try shared.protocol.sendHello(w, .{ .msg = "Hello from server!" }); // try shared.protocol.sendHello(w, .{ .msg = "Hello from server!" });
// try chunk.spawn(&the_chunk, shared.entity.Player, allocator, .{ try chunk.spawn(&the_chunk, shared.entity.Elf, allocator, .{
// .pos = zm.f32x4(1, 1, 0, 0), .pos = zm.f32x4(1, 1, 0, 0),
// .hp = 10, .hp = 10,
// }, w); });
// try chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{ // try chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
// .pos = zm.f32x4(1, 1, 0, 0), // .pos = zm.f32x4(1, 1, 0, 0),

View File

@ -1,3 +1,7 @@
const znet = @import("znet");
const shared = @import("shared"); const shared = @import("shared");
pub var next_entity_id: shared.entity.entity_id = 1; pub var host: znet.Host = undefined;
pub var next_entity_id: shared.entity.id = 1;

View File

@ -2,8 +2,8 @@ const std = @import("std");
const zm = @import("zmath"); const zm = @import("zmath");
const protocol = @import("protocol.zig"); const protocol = @import("protocol.zig");
pub const entity_id = u64; pub const id = u64;
pub const INVALID_ENTITY_ID: entity_id = 0; pub const INVALID_ID: id = 0;
pub const EntityKinds = .{ pub const EntityKinds = .{
Elf, Elf,
@ -12,7 +12,7 @@ pub const EntityKinds = .{
}; };
pub const Elf = struct { pub const Elf = struct {
id: entity_id = INVALID_ENTITY_ID, id: id = INVALID_ID,
pos: zm.Vec, pos: zm.Vec,
hp: i32, hp: i32,
@ -20,43 +20,19 @@ pub const Elf = struct {
self.pos += zm.f32x4(1, 1, 0, 0); self.pos += zm.f32x4(1, 1, 0, 0);
} }
pub fn encode(self: Elf, w: *std.Io.Writer) !void { pub fn to_message(self: *Elf) protocol.Elf_v1 {
try w.writeInt(u64, self.id, .little); return protocol.Elf_v1.init(self);
try protocol.writeVec4(w, self.pos);
try protocol.writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Elf {
return .{
.id = try r.readInt(u64, .little),
.pos = try protocol.readVec4(r),
.vel = try protocol.readVec4(r),
};
} }
}; };
pub const Monster = struct { pub const Monster = struct {
id: entity_id = INVALID_ENTITY_ID, id: id = INVALID_ID,
pos: zm.Vec, pos: zm.Vec,
hp: i32, hp: i32,
pub fn encode(self: Monster, w: *std.Io.Writer) !void {
try w.writeInt(u64, self.id, .little);
try protocol.writeVec4(w, self.pos);
try protocol.writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Monster {
return .{
.id = try r.readInt(u64, .little),
.pos = try protocol.readVec4(r),
.vel = try protocol.readVec4(r),
};
}
}; };
pub const Projectile = struct { pub const Projectile = struct {
id: entity_id = INVALID_ENTITY_ID, id: id = INVALID_ID,
pos: zm.Vec, pos: zm.Vec,
vel: zm.Vec, vel: zm.Vec,
@ -64,18 +40,4 @@ pub const Projectile = struct {
self.pos = self.pos + self.vel; self.pos = self.pos + self.vel;
std.log.info("pos=({})", .{self.pos}); std.log.info("pos=({})", .{self.pos});
} }
pub fn encode(self: Projectile, w: *std.Io.Writer) !void {
try w.writeInt(u64, self.id, .little);
try protocol.writeVec4(w, self.pos);
try protocol.writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Projectile {
return .{
.id = try r.readInt(u64, .little),
.pos = try protocol.readVec4(r),
.vel = try protocol.readVec4(r),
};
}
}; };

View File

@ -1,6 +1,8 @@
const std = @import("std"); const std = @import("std");
const entity = @import("entity.zig");
const zm = @import("zmath"); const zm = @import("zmath");
const bufzilla = @import("bufzilla");
const entity = @import("entity.zig");
pub const SERVER_PORT: u16 = 1337; pub const SERVER_PORT: u16 = 1337;
@ -11,7 +13,7 @@ pub const MessageType = enum(u8) {
pub const SpawnEntity = struct { pub const SpawnEntity = struct {
id: entity.entity_id, id: entity.entity_id,
kind: EntityKind, kind: entity.EntityKind,
payload: []const u8, payload: []const u8,
}; };
@ -39,23 +41,76 @@ pub fn read_message(reader: *std.Io.Reader, allocator: std.mem.Allocator) !struc
}; };
} }
pub const EntityKind = enum(u8) { pub const Elf_v1 = struct {
Player, id: entity.id = entity.INVALID_ID,
Monster, pos: zm.Vec,
Projectile, hp: i32,
};
pub const Hello = struct { pub fn init(elf: *entity.Elf) Elf_v1 {
msg: []const u8, return .{
.id = elf.id,
.pos = elf.pos,
.hp = elf.hp,
}; };
pub fn sendHello(writer: *std.io.Writer, hello: Hello) !void {
try writer.print("{s}\n", .{hello.msg});
try writer.flush();
} }
fn writeVec4(w: *std.Io.Writer, v: zm.Vec4) !void { pub fn encode(self: Elf_v1, w: bufzilla.Writer) !void {
const a = zm.vecToArray(v); try w.write(self.id, entity.id);
//try w.write(self.pos, zm.Vec);
try w.write(self.hp, i32);
}
pub fn decode(r: *std.Io.Reader) !Elf_v1 {
return .{
.id = try r.readInt(u64, .little),
.pos = try readVec4(r),
.vel = try readVec4(r),
};
}
};
pub const Monster_v1 = struct {
id: entity.id = entity.INVALID_ID,
pos: zm.Vec,
hp: i32,
pub fn encode(self: Monster_v1, w: *std.Io.Writer) !void {
try w.writeInt(u64, self.id, .little);
try writeVec4(w, self.pos);
try writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Monster_v1 {
return .{
.id = try r.readInt(u64, .little),
.pos = try readVec4(r),
.vel = try readVec4(r),
};
}
};
pub const Projectile_v1 = struct {
id: entity.id = entity.INVALID_ID,
pos: zm.Vec,
vel: zm.Vec,
pub fn encode(self: Projectile_v1, w: *std.Io.Writer) !void {
try w.writeInt(u64, self.id, .little);
try writeVec4(w, self.pos);
try writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Projectile_v1 {
return .{
.id = try r.readInt(u64, .little),
.pos = try readVec4(r),
.vel = try readVec4(r),
};
}
};
fn writeVec4(w: *std.Io.Writer, v: zm.Vec) !void {
const a = zm.vecToArr4(v);
inline for (a) |f| { inline for (a) |f| {
try w.writeFloat(f32, f, .little); try w.writeFloat(f32, f, .little);
} }