153 lines
4.5 KiB
Zig
153 lines
4.5 KiB
Zig
const std = @import("std");
|
|
const zm = @import("zmath");
|
|
const enet = @import("c.zig").enet;
|
|
|
|
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 => {
|
|
std.log.info("connect", .{});
|
|
},
|
|
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 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, .{});
|
|
// }
|