199 lines
5.9 KiB
Zig
199 lines
5.9 KiB
Zig
const std = @import("std");
|
|
const zm = @import("zmath");
|
|
const enet = @import("c.zig").enet;
|
|
const bufzilla = @import("bufzilla");
|
|
|
|
const shared = @import("shared");
|
|
|
|
const chunk = @import("chunk.zig");
|
|
const server = @import("server.zig");
|
|
|
|
const dt : f32 = 1.0 / 200.0;
|
|
var t : f32 = 0;
|
|
var gt : f32 = 0;
|
|
var accumulator : f32 = 0;
|
|
var k : f32 = 1.0;
|
|
var frame : i32 = 0;
|
|
|
|
var send_accumulator : f32 = 0;
|
|
|
|
var dbg_allocator = std.heap.DebugAllocator(.{}){};
|
|
|
|
pub fn main() !void {
|
|
|
|
const allocator = dbg_allocator.allocator();
|
|
defer {
|
|
const deinit_status = dbg_allocator.deinit();
|
|
if (deinit_status == .leak) {
|
|
@panic("LEAKED!");
|
|
}
|
|
}
|
|
|
|
var stdout = std.fs.File.stdout();
|
|
_ = try stdout.write("Hello, Server!\n");
|
|
|
|
if (enet.enet_initialize() != 0) {
|
|
std.log.info("Failed to load ENet", .{});
|
|
return;
|
|
}
|
|
defer enet.enet_deinitialize();
|
|
|
|
const address = enet.ENetAddress{ .host = enet.ENET_HOST_ANY, .port = shared.protocol.SERVER_PORT };
|
|
const host = enet.enet_host_create(&address, 32, 2, 0, 0);
|
|
if (host == null) {
|
|
@panic("host is null");
|
|
}
|
|
defer enet.enet_host_destroy(host);
|
|
|
|
var the_chunk = try shared.chunk.initChunk(allocator);
|
|
defer shared.chunk.deinitChunk(&the_chunk, allocator);
|
|
|
|
try chunk.spawn(
|
|
allocator,
|
|
host,
|
|
&the_chunk,
|
|
shared.entity.Soldier,
|
|
.{
|
|
.hp = 10,
|
|
.pos = zm.f32x4(1, 0, 0, 0),
|
|
.vel = zm.f32x4(0, 0, 0, 0),
|
|
}
|
|
);
|
|
|
|
var old_time = std.time.nanoTimestamp();
|
|
|
|
while (true) {
|
|
const new_time = std.time.nanoTimestamp();
|
|
var frame_time : f32 = @as(f32, @floatFromInt(new_time - old_time)) / 1_000_000_000.0;
|
|
old_time = new_time;
|
|
|
|
if (frame_time > 0.25)
|
|
frame_time = 0.25;
|
|
|
|
t += frame_time;
|
|
accumulator += frame_time * k;
|
|
send_accumulator += frame_time;
|
|
|
|
var event = enet.ENetEvent{};
|
|
while (enet.enet_host_service(host, &event, 0) > 0) {
|
|
switch (event.type) {
|
|
enet.ENET_EVENT_TYPE_CONNECT => {
|
|
try on_connect(allocator, host, event.peer, &the_chunk);
|
|
},
|
|
enet.ENET_EVENT_TYPE_RECEIVE => {
|
|
std.log.info("receive", .{});
|
|
},
|
|
enet.ENET_EVENT_TYPE_DISCONNECT => {
|
|
std.log.info("disconnect", .{});
|
|
},
|
|
else => {},
|
|
}
|
|
}
|
|
|
|
// const connection = try tcp_server.accept();
|
|
// defer connection.stream.close();
|
|
|
|
// var recv_buffer: [4096]u8 = undefined;
|
|
// var reader = connection.stream.reader(&recv_buffer);
|
|
|
|
// const line = try reader.interface().takeDelimiterExclusive('\n');
|
|
// std.log.info("Received: {s}", .{line});
|
|
|
|
// var send_buffer: [4096]u8 = undefined;
|
|
// var writer = connection.stream.writer(&send_buffer);
|
|
// const w = &writer.interface;
|
|
// try shared.protocol.sendHello(w, .{ .msg = "Hello from server!" });
|
|
|
|
// try chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
|
|
// .pos = zm.f32x4(1, 1, 0, 0),
|
|
// .hp = 20,
|
|
// }, w);
|
|
|
|
// try chunk.spawn(&the_chunk, shared.entity.Projectile, allocator, .{
|
|
// .pos = zm.f32x4(0, 0, 0, 0),
|
|
// .vel = zm.f32x4(0.2, 0, 0, 0),
|
|
// }, w);
|
|
|
|
while (accumulator > dt * k) {
|
|
shared.chunk.updateChunk(&the_chunk);
|
|
|
|
accumulator -= dt * k;
|
|
gt += dt * k;
|
|
}
|
|
|
|
if (send_accumulator > 1) {
|
|
// try chunk.broadcastChanges(&the_chunk, host);
|
|
|
|
send_accumulator = 0;
|
|
}
|
|
|
|
frame += 1;
|
|
}
|
|
}
|
|
|
|
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 writer = bufzilla.Writer.init(&aw.writer);
|
|
|
|
const fields = @typeInfo(shared.chunk.Chunk).@"struct".fields;
|
|
|
|
inline for (fields) |field| {
|
|
const list = &@field(the_chunk, field.name);
|
|
|
|
const itemsType = @FieldType(field.type, "items");
|
|
const T = std.meta.Child(itemsType);
|
|
|
|
for (list.items) |entity| {
|
|
|
|
const msg = shared.protocol.makeSpawnMessage(T, entity);
|
|
|
|
std.log.info("{}", .{entity});
|
|
|
|
try writer.writeAny(msg);
|
|
}
|
|
}
|
|
|
|
const encoded = aw.written();
|
|
|
|
std.log.info("{d} bytes: {s}", .{encoded.len, encoded});
|
|
|
|
var buffer2: [4096]u8 = undefined;
|
|
var fixed2 = std.io.Writer.fixed(&buffer2);
|
|
|
|
var inspector = bufzilla.Inspect(.{}).init(encoded, &fixed2, .{});
|
|
try inspector.inspect();
|
|
|
|
std.log.info("{s}\n", .{fixed2.buffered()});
|
|
|
|
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.", .{});
|
|
}
|
|
}
|
|
|
|
//fn handle_connection(connection: std.net.Server.Connection) !void {}
|
|
|
|
// test "simple test" {
|
|
// const gpa = std.testing.allocator;
|
|
// var list: std.ArrayList(i32) = .empty;
|
|
// defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
|
|
// try list.append(gpa, 42);
|
|
// try std.testing.expectEqual(@as(i32, 42), list.pop());
|
|
// }
|
|
|
|
// test "fuzz example" {
|
|
// const Context = struct {
|
|
// fn testOne(context: @This(), input: []const u8) anyerror!void {
|
|
// _ = context;
|
|
// // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
|
|
// try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
|
|
// }
|
|
// };
|
|
// try std.testing.fuzz(Context{}, Context.testOne, .{});
|
|
// }
|