diff --git a/src/Inventory.zig b/src/Inventory.zig index d459231c..4f5d53c3 100644 --- a/src/Inventory.zig +++ b/src/Inventory.zig @@ -930,7 +930,7 @@ pub const Command = struct { // MARK: Command if(_items[0].item != null) { if(side == .server) { const direction = vec.rotateZ(vec.rotateX(Vec3f{0, 1, 0}, -user.?.player.rot[0]), -user.?.player.rot[2]); - main.server.world.?.drop(_items[0], user.?.player.pos, direction, 20); + main.server.world.?.drop(_items[0].clone(), user.?.player.pos, direction, 20); } } return; @@ -940,7 +940,7 @@ pub const Command = struct { // MARK: Command } if(side == .server) { const direction = vec.rotateZ(vec.rotateX(Vec3f{0, 1, 0}, -user.?.player.rot[0]), -user.?.player.rot[2]); - main.server.world.?.drop(self.source.ref().*, user.?.player.pos, direction, 20); + main.server.world.?.drop(self.source.ref().clone(), user.?.player.pos, direction, 20); } cmd.executeBaseOperation(allocator, .{.delete = .{ .source = self.source, @@ -1053,7 +1053,7 @@ pub const Command = struct { // MARK: Command } if(side == .server) { const direction = vec.rotateZ(vec.rotateX(Vec3f{0, 1, 0}, -user.?.player.rot[0]), -user.?.player.rot[2]); - main.server.world.?.drop(sourceStack.*, user.?.player.pos, direction, 20); + main.server.world.?.drop(sourceStack.clone(), user.?.player.pos, direction, 20); } cmd.executeBaseOperation(allocator, .{.delete = .{ .source = .{.inv = self.source, .slot = @intCast(sourceSlot)}, diff --git a/src/items.zig b/src/items.zig index c7027fa0..51d27019 100644 --- a/src/items.zig +++ b/src/items.zig @@ -925,6 +925,33 @@ pub const Tool = struct { // MARK: Tool main.globalAllocator.destroy(self); } + pub fn clone(self: *const Tool) *Tool { + const result = main.globalAllocator.create(Tool); + result.* = .{ + .craftingGrid = self.craftingGrid, + .materialGrid = self.materialGrid, + .tooltip = null, + .image = graphics.Image.init(main.globalAllocator, self.image.width, self.image.height), + .texture = null, + .seed = self.seed, + .pickaxePower = self.pickaxePower, + .axePower = self.axePower, + .shovelPower = self.shovelPower, + .damage = self.damage, + .durability = self.durability, + .maxDurability = self.maxDurability, + .swingTime = self.swingTime, + .mass = self.mass, + .handlePosition = self.handlePosition, + .inertiaHandle = self.inertiaHandle, + .centerOfMass = self.centerOfMass, + .inertiaCenterOfMass = self.inertiaCenterOfMass, + }; + @memcpy(result.image.imageData, self.image.imageData); + return result; + + } + pub fn initFromCraftingGrid(craftingGrid: [25]?*const BaseItem, seed: u32) *Tool { const self = init(); self.seed = seed; @@ -1047,6 +1074,15 @@ pub const Item = union(enum) { // MARK: Item } } + pub fn clone(self: Item) Item { + switch(self) { + .baseItem => return self, + .tool => |tool| { + return .{.tool = tool.clone()}; + } + } + } + pub fn stackSize(self: Item) u16 { switch(self) { .baseItem => |_baseItem| { @@ -1128,6 +1164,14 @@ pub const ItemStack = struct { // MARK: ItemStack } } + pub fn clone(self: *const ItemStack) ItemStack { + const item = self.item orelse return .{}; + return .{ + .item = item.clone(), + .amount = self.amount, + }; + } + pub fn empty(self: *const ItemStack) bool { return self.amount == 0; }