Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/basalt/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .basalt},
.tags = .{.mineable, .stone, .basalt},
.blockHealth = 30,
.blockResistance = 1,
.drops = .{
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/chalk/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable},
.tags = .{.mineable, .stone, .chalk},
.blockHealth = 12,
.drops = .{
.{.items = .{.auto}},
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/ferrock/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .ferrock},
.tags = .{.mineable, .stone, .ferrock},
.blockHealth = 32.5,
.blockResistance = 1,
.drops = .{
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/glacite/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .glacite},
.tags = .{.mineable, .stone, .glacite},
.blockHealth = 25,
.blockResistance = 1,
.drops = .{
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/glass/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.mineable},
.tags = .{.mineable, .glass},
.blockHealth = 2.5,
.drops = .{
.{.items = .{.auto}},
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/glow_crystal/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.mineable},
.tags = .{.mineable, .glow_crystal},
.blockHealth = 5,
.drops = .{
.{.items = .{.auto}},
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/limestone/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .limestone},
.tags = .{.limestone, .stone, .mineable},
.blockHealth = 27.5,
.blockResistance = 1,
.drops = .{
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/marble/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .marble},
.tags = .{.mineable, .stone, .marble},
.blockHealth = 28,
.drops = .{
.{.items = .{.auto}},
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/pyrolite/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.mineable, .pyrolite},
.tags = .{.mineable, .stone, .pyrolite},
.blockHealth = 40,
.blockResistance = 25,
.drops = .{
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/sandstone/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .sandstone},
.tags = .{.mineable, .stone, .sandstone},
.blockHealth = 20,
.drops = .{
.{.items = .{.auto}},
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/slate/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .slate},
.tags = .{.mineable, .stone, .slate},
.blockHealth = 30,
.blockResistance = 1,
.drops = .{
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/terracotta/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.mineable},
.tags = .{.mineable, .terracotta},
.blockHealth = 20,
.blockResistance = 1,
.drops = .{
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/voidstone/_defaults.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.tags = .{.stone, .mineable, .voidStone},
.tags = .{.voidstone, .stone, .mineable},
.blockHealth = 60,
.blockResistance = 5,
.drops = .{
Expand Down
7 changes: 7 additions & 0 deletions src/gui/windows/inventory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ pub fn deinit() void {

var itemSlots: [20]*ItemSlot = undefined;

pub fn sortItems(target: main.items.Inventory.ClientInventory) void {
const convertedInv: main.items.Inventory.InventoryAndSlot = .{.inv = target.super, .slot = 0};
main.sync.client.executeCommand(.{.sortItems = .{.target = convertedInv}});
}

pub fn onOpen() void {
const list = VerticalList.init(.{padding, padding + 16}, 300, 0);
// Some miscellanious slots and buttons:
// TODO: armor slots, backpack slot + stack-based backpack inventory, other items maybe?
{
const row = HorizontalList.init();
const sortCallback: main.callbacks.SimpleCallback = .{.inner = @ptrCast(&sortItems), .data = &Player.inventory};
row.add(Button.initIcon(.{32, 0}, .{32, 32}, craftingIcon, true, sortCallback));
blk: {
row.add(GuiComponent.BagSlot.init(.{0, 0}, main.entity.components.@"cubyz:bag".client.getBag(main.game.Player.id) orelse break :blk));
}
Expand Down
155 changes: 155 additions & 0 deletions src/sync.zig
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ pub const Command = struct { // MARK: Command
takeFromPlayerBag = 17,
craftFrom = 13,
craftProceduralItem = 15,
sortItems = 18,
clear = 8,
updateBlock = 9,
addHealth = 10,
Expand All @@ -257,6 +258,7 @@ pub const Command = struct { // MARK: Command
takeFromPlayerBag: TakeFromPlayerBag,
craftFrom: CraftFrom,
craftProceduralItem: CraftProceduralItem,
sortItems: SortItems,
clear: Clear,
updateBlock: UpdateBlock,
addHealth: AddHealth,
Expand Down Expand Up @@ -1771,6 +1773,159 @@ pub const Command = struct { // MARK: Command
};
}
};

const SortItems = struct { // MARK: SortItems
target: InventoryAndSlot,

fn compressItems(self: SortItems, ctx: Context) void {
for (self.target.inv._items, 0..) |invStack, slot| {
if (invStack.item != .null) {
for (self.target.inv._items, 0..) |checkedInvStack, checkedSlot| {
if (checkedInvStack.item != .null) {
if (std.meta.eql(invStack.item, checkedInvStack.item)) {
if (self.target.ref().amount >= invStack.item.stackSize()) continue;
if (slot <= checkedSlot) continue;
const amount = @min(checkedInvStack.item.stackSize() - checkedInvStack.amount, invStack.amount);
const dest: InventoryAndSlot = .{.inv = self.target.inv, .slot = @intCast(checkedSlot)};
const source: InventoryAndSlot = .{.inv = self.target.inv, .slot = @intCast(slot)};
std.log.debug("Moving objects", .{});
std.log.debug("{}", .{invStack.item.stackSize() - invStack.amount});
ctx.execute(.{.move = .{
.dest = dest,
.source = source,
.amount = amount,
}});
}
}
}
}
}
}

fn sortProceduralItems(self: SortItems, ctx: Context) void {
for (self.target.inv._items, 0..) |invStack, slot| {
if (invStack.item == .proceduralItem) {
var dest: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
var source: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
for (self.target.inv._items, 0..) |checkedInvStack, checkedSlot| {
if (checkedInvStack.item != .proceduralItem) {
dest.slot = @intCast(checkedSlot);
source.slot = @intCast(slot);
break;
}
}
ctx.execute(.{.swap = .{
.dest = dest,
.source = source,
}});
}
}
for (self.target.inv._items, 0..) |invStack, slot| {
if (invStack.item != .proceduralItem) break;
var dest: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
var source: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
var foundSimilarTag = false;
if (invStack.item.proceduralItem.type.tags().len == 0) continue;
for (self.target.inv._items, 0..) |checkedInvStack, checkedSlot| {
if ((foundSimilarTag)) {
if (checkedInvStack.item == .proceduralItem) {
if (checkedInvStack.item.proceduralItem.hasTag(invStack.item.proceduralItem.type.tags()[0])) continue;
} else {
continue;
}
dest.slot = @intCast(checkedSlot);
source.slot = @intCast(slot);
break;
}
if (checkedInvStack.item == .proceduralItem) {
if (checkedInvStack.item.proceduralItem.type.tags().len == 0) continue;
std.log.debug("{}", .{(invStack.item.proceduralItem.type.tags()[0])});
if (checkedInvStack.item.proceduralItem.hasTag(invStack.item.proceduralItem.type.tags()[0])) foundSimilarTag = true;
}
}
ctx.execute(.{.swap = .{
.dest = dest,
.source = source,
}});
}
}

fn sortBaseItems(self: SortItems, ctx: Context) void {
for (self.target.inv._items, 0..) |invStack, slot| {
if (invStack.item == .baseItem) {
var dest: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
var source: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
for (self.target.inv._items, 0..) |checkedInvStack, checkedSlot| {
if (checkedInvStack.item == .proceduralItem) continue;
if (checkedInvStack.item != .baseItem) {
dest.slot = @intCast(checkedSlot);
source.slot = @intCast(slot);
break;
}
}
ctx.execute(.{.swap = .{
.dest = dest,
.source = source,
}});
}
}
for (self.target.inv._items, 0..) |invStack, slot| {
if (invStack.item == .proceduralItem) continue;
if (invStack.item != .baseItem) break;
std.log.debug("trying sort", .{});
var dest: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
var source: InventoryAndSlot = .{.inv = self.target.inv, .slot = 0};
var foundSimilarTag = false;
std.log.debug("trying sort 2 {}", .{invStack.item});
std.log.debug("trying sort 2 {}", .{invStack.item.baseItem.tags()});
if (invStack.item.baseItem.tags().len == 0) continue;
std.log.debug("trying sort 3", .{});
for (self.target.inv._items, 0..) |checkedInvStack, checkedSlot| {
if ((foundSimilarTag)) {
if (checkedInvStack.item == .baseItem) {
if (checkedInvStack.item.baseItem.hasTag(invStack.item.baseItem.tags()[0])) continue;
} else {
continue;
}
std.log.debug("found a proper swap for the items", .{});
dest.slot = @intCast(checkedSlot);
source.slot = @intCast(slot);
break;
}
if (checkedInvStack.item == .baseItem) {
if (checkedInvStack.item.baseItem.tags().len == 0) continue;
std.log.debug("{}", .{(invStack.item.baseItem.tags()[0])});
if (checkedInvStack.item.baseItem.hasTag(invStack.item.baseItem.tags()[0])) foundSimilarTag = true;
}
}
std.log.debug("swapping {} {}", .{source.slot, dest.slot});
ctx.execute(.{.swap = .{
.dest = dest,
.source = source,
}});
}
}

fn run(self: SortItems, ctx: Context) error{serverFailure}!void {
std.log.debug("Running Sort", .{});
// first compresses items before sorting
// we sort out procedural tools second then generally try to put items with similar tags together
// then we sort normal items
compressItems(self, ctx);
sortProceduralItems(self, ctx);
sortBaseItems(self, ctx);
}

fn serialize(self: SortItems, writer: *BinaryWriter) void {
self.target.write(writer);
}

fn deserialize(reader: *BinaryReader, side: Side, user: ?*main.server.User) !SortItems {
return .{
.target = try InventoryAndSlot.read(reader, side, user),
};
}
};
};

pub threadlocal var threadContext: ThreadContext = .other;
Expand Down
Loading