Skip to content

Commit

Permalink
Remove the function inheritance pattern from the item drop manager.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
IntegratedQuantum committed Jan 1, 2025
1 parent de2e4ae commit e718a4f
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/itemdrop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager

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 {
self.* = ItemDropManager {
.allocator = allocator,
Expand All @@ -77,7 +74,6 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
.world = world,
.gravity = gravity,
.airDragFactor = gravity/maxSpeed,
.internalAdd = &defaultInternalAdd,
};
self.list.resize(self.allocator.allocator, maxCapacity) catch unreachable;
}
Expand Down Expand Up @@ -248,7 +244,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
while(self.changeQueue.dequeue()) |data| {
switch(data) {
.add => |addData| {
self.internalAdd(self, addData[0], addData[1]);
self.internalAdd(addData[0], addData[1]);
},
.remove => |index| {
self.internalRemove(index);
Expand All @@ -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_;
if(self.world == null) {
ClientItemDropManager.clientSideInternalAdd(self, i, drop);
}
drop.reverseIndex = @intCast(self.size);
self.list.set(i, drop);
if(self.world != null) {
Expand Down Expand Up @@ -410,7 +409,6 @@ pub const ClientItemDropManager = struct { // MARK: ClientItemDropManager
.lastTime = @as(i16, @truncate(std.time.milliTimestamp())) -% settings.entityLookback,
};
self.super.init(allocator, null, world.gravity);
self.super.internalAdd = &overrideInternalAdd;
self.interpolation.init(
@ptrCast(self.super.list.items(.pos).ptr),
@ptrCast(self.super.list.items(.vel).ptr)
Expand Down Expand Up @@ -455,18 +453,15 @@ pub const ClientItemDropManager = struct { // MARK: ClientItemDropManager
self.lastTime = time;
}

fn overrideInternalAdd(super: *ItemDropManager, i: u16, drop: ItemDrop) void {
{
mutex.lock();
defer mutex.unlock();
for(&instance.?.interpolation.lastVel) |*lastVel| {
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastVel))[i] = Vec3d{0, 0, 0};
}
for(&instance.?.interpolation.lastPos) |*lastPos| {
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastPos))[i] = drop.pos;
}
fn clientSideInternalAdd(_: *ItemDropManager, i: u16, drop: ItemDrop) void {
mutex.lock();
defer mutex.unlock();
for(&instance.?.interpolation.lastVel) |*lastVel| {
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastVel))[i] = Vec3d{0, 0, 0};
}
for(&instance.?.interpolation.lastPos) |*lastPos| {
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastPos))[i] = drop.pos;
}
super.defaultInternalAdd(i, drop);
}

pub fn remove(self: *ClientItemDropManager, i: u16) void {
Expand Down

0 comments on commit e718a4f

Please sign in to comment.