Remove the function inheritance pattern from the item drop manager.

My old java brain was responsible for this, and it has bothered me for a while.

A simple if can achieve the same with much less indirection.
This commit is contained in:
IntegratedQuantum 2025-01-01 21:13:19 +01:00
parent de2e4aedb4
commit e718a4fc43

View File

@ -64,9 +64,6 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
lastUpdates: ZonElement, lastUpdates: ZonElement,
// TODO: Get rid of this inheritance pattern.
internalAdd: *const fn(self: *ItemDropManager, i: u16, drop: ItemDrop) void,
pub fn init(self: *ItemDropManager, allocator: NeverFailingAllocator, world: ?*ServerWorld, gravity: f64) void { pub fn init(self: *ItemDropManager, allocator: NeverFailingAllocator, world: ?*ServerWorld, gravity: f64) void {
self.* = ItemDropManager { self.* = ItemDropManager {
.allocator = allocator, .allocator = allocator,
@ -77,7 +74,6 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
.world = world, .world = world,
.gravity = gravity, .gravity = gravity,
.airDragFactor = gravity/maxSpeed, .airDragFactor = gravity/maxSpeed,
.internalAdd = &defaultInternalAdd,
}; };
self.list.resize(self.allocator.allocator, maxCapacity) catch unreachable; self.list.resize(self.allocator.allocator, maxCapacity) catch unreachable;
} }
@ -248,7 +244,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
while(self.changeQueue.dequeue()) |data| { while(self.changeQueue.dequeue()) |data| {
switch(data) { switch(data) {
.add => |addData| { .add => |addData| {
self.internalAdd(self, addData[0], addData[1]); self.internalAdd(addData[0], addData[1]);
}, },
.remove => |index| { .remove => |index| {
self.internalRemove(index); self.internalRemove(index);
@ -257,8 +253,11 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
} }
} }
fn defaultInternalAdd(self: *ItemDropManager, i: u16, drop_: ItemDrop) void { fn internalAdd(self: *ItemDropManager, i: u16, drop_: ItemDrop) void {
var drop = drop_; var drop = drop_;
if(self.world == null) {
ClientItemDropManager.clientSideInternalAdd(self, i, drop);
}
drop.reverseIndex = @intCast(self.size); drop.reverseIndex = @intCast(self.size);
self.list.set(i, drop); self.list.set(i, drop);
if(self.world != null) { if(self.world != null) {
@ -410,7 +409,6 @@ pub const ClientItemDropManager = struct { // MARK: ClientItemDropManager
.lastTime = @as(i16, @truncate(std.time.milliTimestamp())) -% settings.entityLookback, .lastTime = @as(i16, @truncate(std.time.milliTimestamp())) -% settings.entityLookback,
}; };
self.super.init(allocator, null, world.gravity); self.super.init(allocator, null, world.gravity);
self.super.internalAdd = &overrideInternalAdd;
self.interpolation.init( self.interpolation.init(
@ptrCast(self.super.list.items(.pos).ptr), @ptrCast(self.super.list.items(.pos).ptr),
@ptrCast(self.super.list.items(.vel).ptr) @ptrCast(self.super.list.items(.vel).ptr)
@ -455,18 +453,15 @@ pub const ClientItemDropManager = struct { // MARK: ClientItemDropManager
self.lastTime = time; self.lastTime = time;
} }
fn overrideInternalAdd(super: *ItemDropManager, i: u16, drop: ItemDrop) void { fn clientSideInternalAdd(_: *ItemDropManager, i: u16, drop: ItemDrop) void {
{ mutex.lock();
mutex.lock(); defer mutex.unlock();
defer mutex.unlock(); for(&instance.?.interpolation.lastVel) |*lastVel| {
for(&instance.?.interpolation.lastVel) |*lastVel| { @as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastVel))[i] = Vec3d{0, 0, 0};
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastVel))[i] = Vec3d{0, 0, 0}; }
} for(&instance.?.interpolation.lastPos) |*lastPos| {
for(&instance.?.interpolation.lastPos) |*lastPos| { @as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastPos))[i] = drop.pos;
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastPos))[i] = drop.pos;
}
} }
super.defaultInternalAdd(i, drop);
} }
pub fn remove(self: *ClientItemDropManager, i: u16) void { pub fn remove(self: *ClientItemDropManager, i: u16) void {