-
Notifications
You must be signed in to change notification settings - Fork 188
DepositToAny slice for the destination #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
739e2e6
add8af4
fbd21a0
c8204c6
b3a1dee
f15167a
dfb73e6
ea634f9
0787846
b4a7023
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1670,16 +1670,21 @@ pub const Command = struct { // MARK: Command | |
| }; | ||
|
|
||
| const DepositToAny = struct { // MARK: DepositToAny | ||
| dest: Inventory, | ||
| destinations: []Inventory, | ||
| owned: bool = false, | ||
| source: InventoryAndSlot, | ||
| amount: u16, | ||
|
|
||
| fn run(self: DepositToAny, ctx: Context) error{serverFailure}!void { | ||
| if(self.dest.type == .creative) return; | ||
| if(self.dest.type == .crafting) return; | ||
| if(self.dest.type == .workbench) return; | ||
| defer if(self.owned) main.stackAllocator.free(self.destinations); | ||
|
||
| if(self.destinations.len == 0) return; | ||
|
||
| for(self.destinations) |dest| { | ||
| if(dest.type == .creative) return; | ||
| if(dest.type == .crafting) return; | ||
| if(dest.type == .workbench) return; | ||
| } | ||
| if(self.source.inv.type == .crafting) { | ||
| ctx.cmd.tryCraftingTo(ctx.allocator, self.dest, self.source, ctx.side, ctx.user); | ||
| ctx.cmd.tryCraftingTo(ctx.allocator, self.destinations[0], self.source, ctx.side, ctx.user); | ||
IntegratedQuantum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| const sourceStack = self.source.ref(); | ||
|
|
@@ -1688,41 +1693,65 @@ pub const Command = struct { // MARK: Command | |
|
|
||
| var remainingAmount = self.amount; | ||
| var selectedEmptySlot: ?u32 = null; | ||
| for(self.dest._items, 0..) |*destStack, destSlot| { | ||
| if(destStack.item == .null and selectedEmptySlot == null) { | ||
| selectedEmptySlot = @intCast(destSlot); | ||
| var selectedEmptyInv: u8 = 0; | ||
|
||
| var selectedEmptyInvHasItem = false; | ||
| outer: for(self.destinations, 0..) |dest, destInv| { | ||
| var emptySlot: ?u32 = null; | ||
| var hasItem = false; | ||
| for(dest._items, 0..) |*destStack, destSlot| { | ||
| if(destStack.item == .null and emptySlot == null) { | ||
| emptySlot = @intCast(destSlot); | ||
| } | ||
| if(std.meta.eql(destStack.item, sourceStack.item)) { | ||
| hasItem = true; | ||
| const amount = @min(sourceStack.item.stackSize() - destStack.amount, remainingAmount); | ||
| if(amount == 0) continue; | ||
| ctx.execute(.{.move = .{ | ||
| .dest = .{.inv = dest, .slot = @intCast(destSlot)}, | ||
| .source = self.source, | ||
| .amount = amount, | ||
| }}); | ||
| remainingAmount -= amount; | ||
| if(remainingAmount == 0) break :outer; | ||
| } | ||
| } | ||
| if(std.meta.eql(destStack.item, sourceStack.item)) { | ||
| const amount = @min(sourceStack.item.stackSize() - destStack.amount, remainingAmount); | ||
| if(amount == 0) continue; | ||
| ctx.execute(.{.move = .{ | ||
| .dest = .{.inv = self.dest, .slot = @intCast(destSlot)}, | ||
| .source = self.source, | ||
| .amount = amount, | ||
| }}); | ||
| remainingAmount -= amount; | ||
| if(remainingAmount == 0) break; | ||
| if(emptySlot != null and (selectedEmptySlot == null or (hasItem and !selectedEmptyInvHasItem))) { | ||
| selectedEmptySlot = emptySlot; | ||
| selectedEmptyInv = @intCast(destInv); | ||
| selectedEmptyInvHasItem = hasItem; | ||
|
||
| } | ||
| } | ||
| if(remainingAmount > 0 and selectedEmptySlot != null) { | ||
| ctx.execute(.{.move = .{ | ||
| .dest = .{.inv = self.dest, .slot = selectedEmptySlot.?}, | ||
| .dest = .{.inv = self.destinations[selectedEmptyInv], .slot = selectedEmptySlot.?}, | ||
| .source = self.source, | ||
| .amount = remainingAmount, | ||
| }}); | ||
| } | ||
| } | ||
|
|
||
| fn serialize(self: DepositToAny, writer: *utils.BinaryWriter) void { | ||
| writer.writeEnum(InventoryId, self.dest.id); | ||
| writer.writeInt(u8, @intCast(self.destinations.len)); | ||
|
||
| for(self.destinations) |dest| { | ||
| writer.writeEnum(InventoryId, dest.id); | ||
| } | ||
| self.source.write(writer); | ||
| writer.writeInt(u16, self.amount); | ||
| } | ||
|
|
||
| fn deserialize(reader: *utils.BinaryReader, side: Side, user: ?*main.server.User) !DepositToAny { | ||
| const destId = try reader.readEnum(InventoryId); | ||
| const destinationsSize = try reader.readInt(u8); | ||
| var destinations = main.stackAllocator.alloc(Inventory, destinationsSize); | ||
| errdefer main.stackAllocator.free(destinations); | ||
|
|
||
| for(destinations) |*dest| { | ||
| const invId = try reader.readEnum(InventoryId); | ||
| dest.* = Sync.getInventory(invId, side, user) orelse return error.InventoryNotFound; | ||
| } | ||
|
|
||
| return .{ | ||
| .dest = Sync.getInventory(destId, side, user) orelse return error.InventoryNotFound, | ||
| .destinations = destinations[0..], | ||
| .owned = true, | ||
| .source = try InventoryAndSlot.read(reader, side, user), | ||
| .amount = try reader.readInt(u16), | ||
| }; | ||
|
|
@@ -2178,8 +2207,8 @@ pub fn depositOrDrop(dest: Inventory, source: Inventory) void { | |
| Sync.ClientSide.executeCommand(.{.depositOrDrop = .{.dest = dest, .source = source, .dropLocation = undefined}}); | ||
| } | ||
|
|
||
| pub fn depositToAny(source: Inventory, sourceSlot: u32, dest: Inventory, amount: u16) void { | ||
| Sync.ClientSide.executeCommand(.{.depositToAny = .{.dest = dest, .source = .{.inv = source, .slot = sourceSlot}, .amount = amount}}); | ||
| pub fn depositToAny(source: Inventory, sourceSlot: u32, destinations: []Inventory, amount: u16) void { | ||
| Sync.ClientSide.executeCommand(.{.depositToAny = .{.destinations = destinations, .source = .{.inv = source, .slot = sourceSlot}, .amount = amount}}); | ||
| } | ||
|
|
||
| pub fn dropStack(source: Inventory, sourceSlot: u32) void { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.