Skip to content
8 changes: 3 additions & 5 deletions src/client/Entity.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ rot: Vec3f = undefined,

id: u32,
name: []const u8,
playerIndex: ?usize, // TODO extract into own component #2760

pub fn init(self: *@This(), zon: ZonElement, allocator: NeverFailingAllocator) !void {
self.* = @This(){
.id = zon.get(u32, "id", std.math.maxInt(u32)),
.width = zon.get(f64, "width", 1),
.height = zon.get(f64, "height", 1),
.name = allocator.dupe(u8, zon.get([]const u8, "name", "")),
.playerIndex = zon.get(?usize, "playerIndex", null),
};
self._interpolationPos = [_]f64{
self.pos[0],
Expand Down Expand Up @@ -80,14 +78,14 @@ pub fn update(self: *@This(), time: i16, lastTime: i16) void {
}

pub fn format(self: *const @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
if (main.settings.showPlayerIndexWithName and self.playerIndex != null) {
if (main.settings.showPlayerIndexWithName and main.entity.components.@"cubyz:player".client.get(self.id) != null) {
try self.formatWithPlayerIndex(writer);
} else {
try writer.print("{s}", .{self.name});
}
}

pub fn formatWithPlayerIndex(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void {
std.debug.assert(self.playerIndex != null);
try writer.print("{s}@{d}", .{self.name, self.playerIndex.?});
std.debug.assert(main.entity.components.@"cubyz:player".client.get(self.id) != null);
try writer.print("{s}@{d}", .{self.name, main.entity.components.@"cubyz:player".client.get(self.id).?.playerIndex});
}
1 change: 1 addition & 0 deletions src/entityComponent/_list.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const @"cubyz:_template" = @import("_template.zig");
pub const @"cubyz:bag" = @import("bag.zig");
pub const @"cubyz:model" = @import("model.zig");
pub const @"cubyz:player" = @import("player.zig");
97 changes: 97 additions & 0 deletions src/entityComponent/player.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const std = @import("std");

const main = @import("main");
const chunk = main.chunk;
const game = main.game;
const graphics = main.graphics;
const c = graphics.c;
const ZonElement = main.ZonElement;
const renderer = main.renderer;
const settings = main.settings;
const utils = main.utils;
const vec = main.vec;
const Mat4f = vec.Mat4f;
const Vec3d = vec.Vec3d;
const Vec3f = vec.Vec3f;
const Vec4f = vec.Vec4f;
const NeverFailingAllocator = main.heap.NeverFailingAllocator;

const BinaryReader = main.utils.BinaryReader;

pub var entityComponentID: main.entity.EntityComponentId = undefined;
pub const entityComponentVersion = 0;

// ############################# Client only stuff ################################
pub const client = struct {
const Component = struct {
playerIndex: u32,
};
pub var components: main.utils.SparseSet(Component, main.entity.Entity) = .{};

pub fn init() void {}
pub fn deinit() void {
components.deinit(main.globalAllocator);
}
pub fn clear() void {
components.clear();
}
pub fn load(entity: u32, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != 0)
return error.InvalidComponentVersion;
const playerIndex = reader.readVarInt(u32) catch return error.UnreadableComponentData;

const ptr = components.get(@enumFromInt(entity)) orelse components.add(main.globalAllocator, @enumFromInt(entity));
ptr.* = Component{
.playerIndex = playerIndex,
};
}
pub fn unload(entity: u32) void {
components.remove(@enumFromInt(entity)) catch {};
}
pub fn get(entity: u32) ?*Component {
return components.get(@enumFromInt(entity));
}
};

// ############################# Server only stuff ################################

pub const server = struct {
pub const Component = struct {
playerIndex: u32, // model
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
writer.writeVarInt(u32, self.playerIndex);
if (audience == .disk)
return .discard;
return .save;
}
};
var components: main.utils.SparseSet(Component, main.entity.Entity) = undefined;
pub fn init() void {
components = .{};
}
pub fn deinit() void {
components.deinit(main.globalAllocator);
}
pub fn loadFromData(entity: u32, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != 0)
return error.InvalidComponentVersion;
const playerIndex = reader.readVarInt(u32) catch return error.UnreadableComponentData;

load(entity, playerIndex);
}
pub fn load(entity: u32, playerIndex: u32) void {
put(entity, Component{
.playerIndex = playerIndex,
});
}
pub fn unload(entity: u32) void {
components.remove(@enumFromInt(entity)) catch {};
}
pub fn put(entity: u32, renderComponent: Component) void {
const ptr = components.get(@enumFromInt(entity)) orelse components.add(main.globalAllocator, @enumFromInt(entity));
ptr.* = renderComponent;
}
pub fn get(entity: u32) ?*Component {
return components.get(@enumFromInt(entity));
}
Comment thread
tillpp marked this conversation as resolved.
Resolved
};
4 changes: 2 additions & 2 deletions src/gui/windows/players.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ pub fn onOpen() void {
}

for (main.client.entity_manager.entities.items()) |ent| {
if (ent.playerIndex == null) continue;
const playerComponent = main.entity.components.@"cubyz:player".client.get(ent.id) orelse continue;
const row = HorizontalList.init();

const string = std.fmt.allocPrint(main.stackAllocator.allocator, "{f}", .{std.fmt.alt(ent, .formatWithPlayerIndex)}) catch unreachable;
defer main.stackAllocator.free(string);
row.add(Label.init(.{0, 0}, 200, string, .left));
row.add(Button.initText(.{0, 0}, 100, "Kick", .initWithInt(kickByPlayerIndex, ent.playerIndex.?)));
row.add(Button.initText(.{0, 0}, 100, "Kick", .initWithInt(kickByPlayerIndex, playerComponent.playerIndex)));
list.add(row);
}
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/server/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ pub const User = struct { // MARK: User

self.interpolation.init(@ptrCast(&self.player().pos), @ptrCast(&self.player().vel));
self.loadUnloadChunks();

main.entity.components.@"cubyz:player".server.load(self.id, @truncate(self.playerIndex));
}

fn simArrIndex(x: i32) usize {
Expand Down Expand Up @@ -786,8 +788,6 @@ pub fn connectInternal(user: *User) void {
defer zonArray.deinit(main.stackAllocator);

const entityZon = user.player().save(main.stackAllocator, .playerNearby);
entityZon.put("name", user.name);
Comment thread
tillpp marked this conversation as resolved.
Resolved
entityZon.put("playerIndex", user.playerIndex);
zonArray.array.append(entityZon);
const data = zonArray.toStringEfficient(main.stackAllocator, &.{});
defer main.stackAllocator.free(data);
Expand All @@ -800,8 +800,6 @@ pub fn connectInternal(user: *User) void {
defer zonArray.deinit(main.stackAllocator);
for (userList) |other| {
const entityZon = other.player().save(main.stackAllocator, .playerNearby);
entityZon.put("name", other.name);
entityZon.put("playerIndex", other.playerIndex);
zonArray.array.append(entityZon);
}
const data = zonArray.toStringEfficient(main.stackAllocator, &.{});
Expand Down
7 changes: 7 additions & 0 deletions src/server/world.zig
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,13 @@ pub const ServerWorld = struct { // MARK: ServerWorld
}
const player = user.player();
const loadingError = player.loadFrom(user.id, playerData.getChild("entity"), .server);

// override the name for players.
if (player.name) |name| {
main.globalAllocator.free(name);
}
player.name = main.globalAllocator.dupe(u8, user.name);

if (playerData == .null) {
player.pos = @floatFromInt(self.spawn);

Expand Down
Loading