serialization getting better maybe??

This commit is contained in:
vfs committed 2026-01-10 11:59:08 +01:00
1 parent da8589a700
commit b686052636
7 files changed
+131 -88

No files matched your search

+25 -9
View File
@@ -1,8 +1,13 @@
const std = @import("std");
const znet = @import("znet");
const bufzilla = @import("bufzilla");
const shared = @import("shared");
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;
server.next_entity_id += 1;
@@ -17,14 +22,25 @@ pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.
}
// serialize entity
const messaged = entity.to_message();
var buffer: [64]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
try T.encode(entity, fbs.writer());
var fixed = std.io.Writer.fixed(&buffer);
var writer = bufzilla.Writer.init(&fixed);
try writer.writeAny(messaged);
try shared.protocol.write_message(
w,
.spawn_entity,
fbs.getWritten(),
);
try w.flush();
const encoded = fixed.buffered();
std.log.info("\nsending the boy ( {s} )\n", .{encoded});
var iterator = server.host.iterPeers();
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);
}
}
}
+6 -6
View File
@@ -24,7 +24,7 @@ pub fn main() !void {
try znet.init();
defer znet.deinit();
const host = try znet.Host.init(.{
server.host = try znet.Host.init(.{
.addr = try .init(.{
.ip = .any,
.port = .{ .uint = 5000 },
@@ -44,7 +44,7 @@ pub fn main() !void {
defer shared.chunk.deinitChunk(&the_chunk, allocator);
while (true) {
while (try host.service(500)) |event| switch (event) {
while (try server.host.service(500)) |event| switch (event) {
.connect => |data| {
_ = data;
// std.log.info("{}", .{data.peer});
@@ -73,10 +73,10 @@ pub fn main() !void {
// const w = &writer.interface;
// try shared.protocol.sendHello(w, .{ .msg = "Hello from server!" });
// try chunk.spawn(&the_chunk, shared.entity.Player, allocator, .{
// .pos = zm.f32x4(1, 1, 0, 0),
// .hp = 10,
// }, w);
try chunk.spawn(&the_chunk, shared.entity.Elf, allocator, .{
.pos = zm.f32x4(1, 1, 0, 0),
.hp = 10,
});
// try chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
// .pos = zm.f32x4(1, 1, 0, 0),
+5 -1
View File
@@ -1,3 +1,7 @@
const znet = @import("znet");
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;
+7 -45
View File
@@ -2,8 +2,8 @@ const std = @import("std");
const zm = @import("zmath");
const protocol = @import("protocol.zig");
pub const entity_id = u64;
pub const INVALID_ENTITY_ID: entity_id = 0;
pub const id = u64;
pub const INVALID_ID: id = 0;
pub const EntityKinds = .{
Elf,
@@ -12,7 +12,7 @@ pub const EntityKinds = .{
};
pub const Elf = struct {
id: entity_id = INVALID_ENTITY_ID,
id: id = INVALID_ID,
pos: zm.Vec,
hp: i32,
@@ -20,43 +20,19 @@ pub const Elf = struct {
self.pos += zm.f32x4(1, 1, 0, 0);
}
pub fn encode(self: Elf, 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) !Elf {
return .{
.id = try r.readInt(u64, .little),
.pos = try protocol.readVec4(r),
.vel = try protocol.readVec4(r),
};
pub fn to_message(self: *Elf) protocol.Elf_v1 {
return protocol.Elf_v1.init(self);
}
};
pub const Monster = struct {
id: entity_id = INVALID_ENTITY_ID,
id: id = INVALID_ID,
pos: zm.Vec,
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 {
id: entity_id = INVALID_ENTITY_ID,
id: id = INVALID_ID,
pos: zm.Vec,
vel: zm.Vec,
@@ -64,18 +40,4 @@ pub const Projectile = struct {
self.pos = self.pos + self.vel;
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),
};
}
};
+69 -14
View File
@@ -1,6 +1,8 @@
const std = @import("std");
const entity = @import("entity.zig");
const zm = @import("zmath");
const bufzilla = @import("bufzilla");
const entity = @import("entity.zig");
pub const SERVER_PORT: u16 = 1337;
@@ -11,7 +13,7 @@ pub const MessageType = enum(u8) {
pub const SpawnEntity = struct {
id: entity.entity_id,
kind: EntityKind,
kind: entity.EntityKind,
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) {
Player,
Monster,
Projectile,
pub const Elf_v1 = struct {
id: entity.id = entity.INVALID_ID,
pos: zm.Vec,
hp: i32,
pub fn init(elf: *entity.Elf) Elf_v1 {
return .{
.id = elf.id,
.pos = elf.pos,
.hp = elf.hp,
};
}
pub fn encode(self: Elf_v1, w: bufzilla.Writer) !void {
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 Hello = struct {
msg: []const u8,
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 fn sendHello(writer: *std.io.Writer, hello: Hello) !void {
try writer.print("{s}\n", .{hello.msg});
try writer.flush();
}
pub const Projectile_v1 = struct {
id: entity.id = entity.INVALID_ID,
pos: zm.Vec,
vel: zm.Vec,
fn writeVec4(w: *std.Io.Writer, v: zm.Vec4) !void {
const a = zm.vecToArray(v);
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| {
try w.writeFloat(f32, f, .little);
}