diff --git a/src/client/Entity.zig b/src/client/Entity.zig index 49d719ff83..f826c4cca6 100644 --- a/src/client/Entity.zig +++ b/src/client/Entity.zig @@ -30,7 +30,6 @@ 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(){ @@ -38,7 +37,6 @@ pub fn init(self: *@This(), zon: ZonElement, allocator: NeverFailingAllocator) ! .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], @@ -80,7 +78,7 @@ 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}); @@ -88,6 +86,6 @@ pub fn format(self: *const @This(), writer: *std.Io.Writer) std.Io.Writer.Error! } 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}); } diff --git a/src/entityComponent/_list.zig b/src/entityComponent/_list.zig index b5c8620745..3959d8d62a 100644 --- a/src/entityComponent/_list.zig +++ b/src/entityComponent/_list.zig @@ -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"); diff --git a/src/entityComponent/player.zig b/src/entityComponent/player.zig new file mode 100644 index 0000000000..2d0c94b036 --- /dev/null +++ b/src/entityComponent/player.zig @@ -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)); + } +}; diff --git a/src/gui/windows/players.zig b/src/gui/windows/players.zig index 7bd71c1b16..3d9a411267 100644 --- a/src/gui/windows/players.zig +++ b/src/gui/windows/players.zig @@ -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 { diff --git a/src/server/server.zig b/src/server/server.zig index 9df4ea9501..3c6a19cd12 100644 --- a/src/server/server.zig +++ b/src/server/server.zig @@ -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 { @@ -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); - entityZon.put("playerIndex", user.playerIndex); zonArray.array.append(entityZon); const data = zonArray.toStringEfficient(main.stackAllocator, &.{}); defer main.stackAllocator.free(data); @@ -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, &.{}); diff --git a/src/server/world.zig b/src/server/world.zig index 8c96a8c094..2b175ae7dc 100644 --- a/src/server/world.zig +++ b/src/server/world.zig @@ -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);