-
-
Notifications
You must be signed in to change notification settings - Fork 221
Sort button attempt 2 #3194
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
base: master
Are you sure you want to change the base?
Sort button attempt 2 #3194
Changes from all commits
7a91563
1bc6472
bb2ff56
96e1ccd
9cc3310
befe81c
cc53269
88510ea
2c6e489
d505cba
4721852
d5f8e9a
1fa83ee
8e5d0e1
fe8e1bb
7e399ef
b2dd74d
398bfed
1484c50
d8823ff
2d56b5a
61c286e
44a6365
f4adb1e
6884130
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ const Item = main.items.Item; | |
| const ItemStack = main.items.ItemStack; | ||
| const ProceduralItem = main.items.ProceduralItem; | ||
| const utils = main.utils; | ||
| const Tag = main.Tag; | ||
| const BinaryWriter = utils.BinaryWriter; | ||
| const BinaryReader = utils.BinaryReader; | ||
| const NeverFailingAllocator = main.heap.NeverFailingAllocator; | ||
|
|
@@ -531,6 +532,67 @@ pub const ClientInventory = struct { // MARK: ClientInventory | |
| main.sync.client.executeCommand(.{.craftProceduralItem = .init(destinations, workbenchInv)}); | ||
| } | ||
|
|
||
| pub fn sortItems(source: ClientInventory, ignoredSlotCount: usize) void { | ||
| compressItems(source); | ||
| const ctx: SortContext = .{.inv = source}; | ||
| std.sort.insertionContext(ignoredSlotCount, source.super._items.len, ctx); | ||
| } | ||
|
|
||
| pub fn compressItems(source: ClientInventory) void { | ||
| for (source.super._items, 0..) |invStack, slot| { | ||
| for (source.super._items, 0..) |checkedInvStack, checkedSlot| { | ||
| if (checkedSlot < slot) continue; | ||
| if (std.meta.eql(invStack.item, checkedInvStack.item)) { | ||
| main.sync.client.executeCommand(.{.deposit = .{.dest = .{.inv = source.super, .slot = @intCast(checkedSlot)}, .source = .{.inv = source.super, .slot = @intCast(slot)}, .amount = checkedInvStack.amount}}); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const SortContext = struct { | ||
| inv: ClientInventory, | ||
|
|
||
| pub fn lessThan(ctx: @This(), a: usize, b: usize) bool { | ||
| const itemA: Item = ctx.inv.getItem(a); | ||
| const itemB: Item = ctx.inv.getItem(b); | ||
|
|
||
| if (itemA == .null) return false; | ||
| if (itemB == .null) return true; | ||
| if ((itemA != .proceduralItem) and (itemB == .proceduralItem)) return false; | ||
| if ((itemA == .proceduralItem) and (itemB != .proceduralItem)) return true; | ||
|
|
||
| const itemATags = getTagsFromItem(itemA); | ||
| const itemBTags = getTagsFromItem(itemB); | ||
|
|
||
| for (0..@min(itemATags.len, itemBTags.len)) |i| { | ||
| if (itemATags[i] == itemBTags[i]) continue; | ||
| return std.mem.lessThan(u8, itemATags[i].getName(), itemBTags[i].getName()); | ||
| } | ||
| if (itemATags.len != itemBTags.len) return itemATags.len < itemBTags.len; | ||
|
Comment on lines
+567
to
+571
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is probably not the best thing (I know it is from me but it was at that time made up on the spot). We probably need to enforce that the tags are sorted so that for example when ItemA has a,b tags and ItemB has c,a,b It gets compared with a,b and a,b,c. Either by sorting them when we load the tags or here. Or some other way.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely on load.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I do this I think it would cause read order issues if I make it check if the first tag of ItemA exists in ItemB because they could have different orders you could create tags that are used in nothing but sorting so that all flowers are sorted together under the category of cuttable
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We didn't mean that. We meant that the tags should be already sorted so what you at the start said doesn't need to be done. This sorting algorithm could then also be changed by a user if they want to. pub fn loadTagsFromZon(_allocator: main.heap.NeverFailingAllocator, zon: main.ZonElement) []Tag {
const result = _allocator.alloc(Tag, zon.toSlice().len);
for (zon.toSlice(), 0..) |tagZon, i| {
result[i] = Tag.find(tagZon.as([]const u8) orelse blk: {
std.log.err("Tag array field {s} has incorrect type, expected string", .{@tagName(tagZon)});
break :blk "incorrect";
});
}
+ // some kind of sorting before returning the result
return result;
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t understand what you mean by sorting the tags
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see all get sorted together A B C
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yes exactly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the sorting of the tags is also clientside. So it can also be changed. |
||
| if ((itemA == .proceduralItem) and (itemB == .proceduralItem)) { | ||
| std.log.debug("checking durability", .{}); | ||
| return (itemA.proceduralItem.durability > itemB.proceduralItem.durability); | ||
| } | ||
|
|
||
| return std.mem.lessThan(u8, itemA.id().?, itemB.id().?); | ||
|
Crepestrom marked this conversation as resolved.
|
||
| } | ||
|
|
||
| pub fn swap(ctx: @This(), a: usize, b: usize) void { | ||
| main.sync.client.executeCommand(.{.depositOrSwap = .{ | ||
| .dest = .{.inv = ctx.inv.super, .slot = @intCast(a)}, | ||
| .source = .{.inv = ctx.inv.super, .slot = @intCast(b)}, | ||
| }}); | ||
| } | ||
| }; | ||
|
|
||
| pub fn getTagsFromItem(givenItem: Item) []const Tag { | ||
| switch (givenItem) { | ||
| .null => return &[_]Tag{}, | ||
| .proceduralItem => return givenItem.proceduralItem.type.tags(), | ||
| .baseItem => return givenItem.baseItem.tags(), | ||
| } | ||
| } | ||
|
Comment on lines
+588
to
+594
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be in the |
||
|
|
||
| pub fn placeBlock(self: ClientInventory, slot: u32) void { | ||
| std.debug.assert(self.type == .serverShared); | ||
| main.renderer.MeshSelection.placeBlock(self, slot); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.