serialization getting better maybe??
This commit is contained in:
parent
da8589a700
commit
b686052636
28
build.zig
28
build.zig
@ -10,15 +10,6 @@ pub fn build(b: *std.Build) void {
|
||||
.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", .{
|
||||
// .target = target,
|
||||
// .optimize = .Debug,
|
||||
@ -96,17 +87,28 @@ pub fn build(b: *std.Build) void {
|
||||
});
|
||||
const znet_mod = znet_dep.module("znet");
|
||||
const znet_artifact = znet_dep.artifact("znet");
|
||||
|
||||
client.root_module.addImport("znet", znet_mod);
|
||||
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.root_module.addImport("raylib", raylib);
|
||||
client.root_module.addImport("raygui", raygui);
|
||||
|
||||
server.root_module.addImport("znet", znet_mod);
|
||||
server.linkLibrary(znet_artifact);
|
||||
|
||||
b.installArtifact(client);
|
||||
b.installArtifact(server);
|
||||
|
||||
|
||||
@ -48,6 +48,10 @@
|
||||
.url = "git+https://github.com/connellr023/znet#cb11fb0c4a2b668128c436fbbccd111223c74898",
|
||||
.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 = .{
|
||||
"build.zig",
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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),
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@ -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 const Hello = struct {
|
||||
msg: []const u8,
|
||||
pub fn init(elf: *entity.Elf) Elf_v1 {
|
||||
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 {
|
||||
const a = zm.vecToArray(v);
|
||||
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 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| {
|
||||
try w.writeFloat(f32, f, .little);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user