diff --git a/README.md b/README.md index 109ba1c..b6c8a31 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,62 @@ -# DS_SortInventory -Don't starve mod : Sort Inventory by Brody & Whismerhill - -Features: -- Sorts inventory by weapons, equipment, food, etc when you press z. -- Changes sort method (explained in the v1.1 features) when you press Ctrl+z. -- Toggles whether the first 1-2 slots get sorted when you press Shift+z. -- Food is sorted by hungervalue. -- Multiple stacks of items that aren't equipment or food are set next to eachother. -- Works with RoG. -- Works with items from other mods, and variable inventory/backpack sizes! - -NOTE: If anyone has a logical sorting method that is not implemented and you think it'll be a good addition to the mod, let me know in the comments - -v1.1 Features: -- Now works with backpacks -- Works with other mods (RPG HUD, and several others which introduce new items have been tested)! -- Ctrl+z now toggles the sorting method -- -- 1: Weapons, tools, equips, food, others -- -- 2: Weapons, tools, equips, food, others at end of inventory(or in backpack) -- -- 3: Weapons, tools, equips, others, food at end of inventory -- -- 4: Weapons, tools, equips to left of equipslots. Others, food from left to right. -- Shift+z toggles whether the first 1-2 items are also sorted -- -- default: all items are sorted -- -- 1 held: 1st item of inventory will not be sorted -- -- 2 held: 1st 2 items of inventory will not be sorted -- Settings are not persistent through reloading of game, or changing settings! - -v1.3 Features: -- Now with configuration options for -- -- the default sorting method when loading savegame (shortcuts still work for changing options ingame) -- -- whether or not the backpack is sorted when loading a savegame -- -- custom keybinds between A-Z & Fx keys - -Bugs: -- Keybind reset when controls or options are modified. -x Modicon crashed mod page - Fixed. Thanks to KARAS for help! - -Future updates: --Sort chests +# DS_SortInventory +Don't starve mod : Sort Inventory by Brody & Whismerhill + +Features: +- Sorts inventory by weapons, equipment, food, etc when you press z. (keybind configurable) +- Changes sort method when you press Ctrl+z. +- Toggles whether the first 1-2 slots get sorted when you press Shift+z. +- Food is sorted by hungervalue. +- Multiple stacks of items that aren't equipment or food are set next to eachother. +- compatible with RoG & SW +- Works with items from other mods, and variable inventory/backpack sizes! + + +Sorting Methods (CTRL + Z or chosen keybind) +Note: in the configuration menu, there is no space to write all this, so only the first letter were kept in an approximate of how it's gonna appear. +- --1: Weapons, lights, tools, books, equips, food, others +- --2: Weapons, lights, tools, books, equips, food, others at end of inventory(or in backpack) +- --3: Weapons, lights, tools, books, equips, others, food at end of inventory +- --4: Weapons, lights, tools, books, equips to left of equipslots. Others, food from left to right. +- --5: CUSTOM SORTING + +Skip first slots of inventory: +- --default: none +- --1 held: 1st item of inventory will not be sorted +- --2 held: 1st 2 items of inventory will not be sorted + +CUSTOM SORTING DOCUMENTATION +in the mod option menu +- 1 : Begin by enabling "Custom sorting order" (Yes) +- 2 : Choose whether or not "Sorting Order" is set to "5 Custom" +this is the default sorting order which you can change with CTRL+Z or keybind +- 3 : Choose the order you want for your objects with the "First group of items" to "Last group of items" +IT IS VERY IMPORTANT THAT THERE'S ONLY 1 OF EACH HERE otherwise custom sorting will be disabled. +- 4 : Choose whether or not the objects in a group are in reversed order. Please note that most objects are not sorted +inside their group except foods +- 5 : Choose "how to sort" +- -- "basic" just puts items as if you just picked them up in the sorting order. +- -- "place at end" puts items at the end of your inventory (inside the backpack if available) +- -- "end + no backpack" puts items at the end of your inventory but will skip the backpack +- -- "place at start" puts items at the start ???? + + + +----------------------------------------------------------- +-------------------------CHANGELOG------------------------- +----------------------------------------------------------- +v1.4 +- custom sorting, even more configuration options +v1.3 +- configurable keybinds +v1.1 +- Now works with backpacks +- Works with other mods (RPG HUD, and several others which introduce new items have been tested)! +- Ctrl+z now toggles the sorting method +- Shift+z toggles whether the first 1-2 items are also sorted + +Bugs: +- Keybind reset when controls or options are modified. +x Modicon crashed mod page - Fixed. Thanks to KARAS for help! + +Future updates: +- Sort chests diff --git a/modinfo.lua b/modinfo.lua index c1ad9e9..900ec0e 100644 --- a/modinfo.lua +++ b/modinfo.lua @@ -1,7 +1,7 @@ name = "Sort Inventory" description = "Sorts inventory by weapons, equipment, food, etc when you press z. Ctrl+z changes the sorting method. Shift+z toggles whether the first 1-2 slots are sorted. (See Workshop page for v1.2 updates!)" author = "Brody & Whismerhill" -version = "1.3" +version = "1.4" forumthread = "" api_version = 6 icon_atlas = "modicon.xml" @@ -30,10 +30,11 @@ configuration_options = label = "Sorting Order", options = { - {description = "1: ", data = 1}, --Dump everything in inventory in an intuitive order - {description = "2: ", data = 2}, --Stick others at end, starting in the pack if present - {description = "3: ", data = 3}, --Place all food into backpack if possible. Do food last - {description = "4: ", data = 4}, --keep weapons and tools just left of equipslots, then from left to right: others, food + {description = "1: wltbefo ", data = 1}, --Dump everything in inventory in an intuitive order + {description = "2: wltbef..o", data = 2}, --Stick others at end, starting in the pack if present + {description = "3: wltbeo..f", data = 3}, --Place all food into backpack if possible. Do food last + {description = "4: of.ebtlw", data = 4}, --keep weapons and tools just left of equipslots, then from left to right: others, food + {description = "5: Custom", data = 5}, }, default = 1, hover = "Controls the default order for autosorting (changeable with keybind)", @@ -130,4 +131,273 @@ configuration_options = }, default = 112, }, + { + name = "CustomSortConf", + label = "Custom sorting order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Placement1Conf", + label = "First group of items", + options = + { + {description = "Weapons", data = "w"}, + {description = "Lights", data = "l"}, + {description = "Tools", data = "t"}, + {description = "Books", data = "b"}, + {description = "Equips", data = "e"}, + {description = "Foods", data = "f"}, + {description = "Others", data = "o"}, + }, + default = "w", + }, + { + name = "Placement2Conf", + label = "Second group of items", + options = + { + {description = "Weapons", data = "w"}, + {description = "Lights", data = "l"}, + {description = "Tools", data = "t"}, + {description = "Books", data = "b"}, + {description = "Equips", data = "e"}, + {description = "Foods", data = "f"}, + {description = "Others", data = "o"}, + }, + default = "l", + }, + { + name = "Placement3Conf", + label = "Third group of items", + options = + { + {description = "Weapons", data = "w"}, + {description = "Lights", data = "l"}, + {description = "Tools", data = "t"}, + {description = "Books", data = "b"}, + {description = "Equips", data = "e"}, + {description = "Foods", data = "f"}, + {description = "Others", data = "o"}, + }, + default = "t", + }, + { + name = "Placement4Conf", + label = "Fourth group of items", + options = + { + {description = "Weapons", data = "w"}, + {description = "Lights", data = "l"}, + {description = "Tools", data = "t"}, + {description = "Books", data = "b"}, + {description = "Equips", data = "e"}, + {description = "Foods", data = "f"}, + {description = "Others", data = "o"}, + }, + default = "b", + }, + { + name = "Placement5Conf", + label = "Fifth group of items", + options = + { + {description = "Weapons", data = "w"}, + {description = "Lights", data = "l"}, + {description = "Tools", data = "t"}, + {description = "Books", data = "b"}, + {description = "Equips", data = "e"}, + {description = "Foods", data = "f"}, + {description = "Others", data = "o"}, + }, + default = "e", + }, + { + name = "Placement6Conf", + label = "Sixth group of items", + options = + { + {description = "Weapons", data = "w"}, + {description = "Lights", data = "l"}, + {description = "Tools", data = "t"}, + {description = "Books", data = "b"}, + {description = "Equips", data = "e"}, + {description = "Foods", data = "f"}, + {description = "Others", data = "o"}, + }, + default = "f", + }, + { + name = "Placement7Conf", + label = "Last group of items", + options = + { + {description = "Weapons", data = "w"}, + {description = "Lights", data = "l"}, + {description = "Tools", data = "t"}, + {description = "Books", data = "b"}, + {description = "Equips", data = "e"}, + {description = "Foods", data = "f"}, + {description = "Others", data = "o"}, + }, + default = "o", + }, + { + name = "Reverse1Conf", + label = "First: Reverse order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Reverse2Conf", + label = "Second: Reverse order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Reverse3Conf", + label = "Third: Reverse order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Reverse4Conf", + label = "Fourth: Reverse order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Reverse5Conf", + label = "Fifth: Reverse order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Reverse6Conf", + label = "Sixth: Reverse order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Reverse7Conf", + label = "Last: Reverse order", + options = + { + {description = "Yes", data = true}, + {description = "No", data = false}, + }, + default = false, + }, + { + name = "Order1Conf", + label = "First: how to sort", + options = + { + {description = "basic", data = 1}, + {description = "place at end", data = 2}, + {description = "end + no backpack", data = 3}, + {description = "place at start", data = 4}, + }, + default = 1, + }, + { + name = "Order2Conf", + label = "Second: how to sort", + options = + { + {description = "basic", data = 1}, + {description = "place at end", data = 2}, + {description = "end + no backpack", data = 3}, + {description = "place at start", data = 4}, + }, + default = 1, + }, + { + name = "Order3Conf", + label = "Third: how to sort", + options = + { + {description = "basic", data = 1}, + {description = "place at end", data = 2}, + {description = "end + no backpack", data = 3}, + {description = "place at start", data = 4}, + }, + default = 1, + }, + { + name = "Order4Conf", + label = "Fourth: how to sort", + options = + { + {description = "basic", data = 1}, + {description = "place at end", data = 2}, + {description = "end + no backpack", data = 3}, + {description = "place at start", data = 4}, + }, + default = 1, + }, + { + name = "Order5Conf", + label = "Fifth: how to sort", + options = + { + {description = "basic", data = 1}, + {description = "place at end", data = 2}, + {description = "end + no backpack", data = 3}, + {description = "place at start", data = 4}, + }, + default = 1, + }, + { + name = "Order6Conf", + label = "Sixth: how to sort", + options = + { + {description = "basic", data = 1}, + {description = "place at end", data = 2}, + {description = "end + no backpack", data = 3}, + {description = "place at start", data = 4}, + }, + default = 1, + }, + { + name = "Order7Conf", + label = "Last: how to sort", + options = + { + {description = "basic", data = 1}, + {description = "place at end", data = 2}, + {description = "end + no backpack", data = 3}, + {description = "place at start", data = 4}, + }, + default = 1, + }, } diff --git a/modmain.lua b/modmain.lua index f1e0508..946a100 100644 --- a/modmain.lua +++ b/modmain.lua @@ -1,5 +1,6 @@ --- version 1.3 of sort_inventory +-- version 1.4 of sort_inventory -- sorts into weapons, tools, equips, foods, and others +local RECIPETABS = GLOBAL.RECIPETABS -- Set this value to false if you don't want the backpack to be sorted. (now a config option) local sort_backpack = GetModConfigData("BackpackSortConf") -- true @@ -9,10 +10,43 @@ local open_chest = nil local KEY_SORT = GetModConfigData("Key_SortConf") local KEY_BACKPACK = GetModConfigData("Key_BackpackConf") +-- for custom sorting option +local sort_custom = GetModConfigData("CustomSortConf") +local places = { GetModConfigData("Placement1Conf"), GetModConfigData("Placement2Conf"), GetModConfigData("Placement3Conf"), + GetModConfigData("Placement4Conf"), GetModConfigData("Placement5Conf"), GetModConfigData("Placement6Conf"), + GetModConfigData("Placement7Conf") } +local reverses = { GetModConfigData("Reverse1Conf"), GetModConfigData("Reverse2Conf"), GetModConfigData("Reverse3Conf"), + GetModConfigData("Reverse4Conf"), GetModConfigData("Reverse5Conf"), GetModConfigData("Reverse6Conf"), + GetModConfigData("Reverse7Conf") } +local orders = { GetModConfigData("Order1Conf"), GetModConfigData("Order2Conf"), GetModConfigData("Order3Conf"), + GetModConfigData("Order4Conf"), GetModConfigData("Order5Conf"), GetModConfigData("Order6Conf"), + GetModConfigData("Order7Conf") } + +-- this function checks that the player didn't use duplicate entries for the custom sorting + +local checkduplicate = function() + print("checkingforduplicates") + local cache = {} + for k, v in pairs(places) do + if(cache[v]) then + sort_custom = false + --error("Duplicate Entries found in Placements for Custom Sorting") + print("ERROR Duplicate Entries found in Groups for Custom Sorting") + return 1 + end + cache[v] = true + end + print("no duplicates") + return 5 +end + +-- check no duplicate value or cancel custom sorting silently (for now) +if sort_method == 5 then sort_method = checkduplicate() end + -- Combines 2 stacks of the same item -- returns false if the 2nd item disappears -combine_stacks = function(item1, item2) +local combine_stacks = function(item1, item2) if not item1.components.stackable then return end local size1 = item1.components.stackable.stacksize local size2 = item2.components.stackable.stacksize @@ -52,7 +86,7 @@ combine_stacks = function(item1, item2) return false end -average = function(value1, amount1, value2, amount2) +local average = function(value1, amount1, value2, amount2) local total1 = value1*amount1 local total2 = value2*amount2 local total = total1 + total2 @@ -61,7 +95,7 @@ end -- Requires either the backpack or the inventory to have an empty slot -place_item_at_end = function(item) +local place_item_at_end = function(item) local player = GLOBAL.GetPlayer() local backpack = player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.PACK) or player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BACK) or player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY) if not sort_backpack then backpack = nil end @@ -92,7 +126,7 @@ place_item_at_end = function(item) end end -place_item_at_end_no_backpack = function(item) +local place_item_at_end_no_backpack = function(item) local player = GLOBAL.GetPlayer() local MAXSLOTS = player.components.inventory.maxslots local backpack = nil @@ -107,7 +141,7 @@ place_item_at_end_no_backpack = function(item) end -- insert item at first free index -place_item_at_start = function(item) +local place_item_at_start = function(item) local player = GLOBAL.GetPlayer() local backpack = player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.PACK) or player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BACK) or player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY) if not sort_backpack then backpack = nil end @@ -136,7 +170,7 @@ place_item_at_start = function(item) end -- Reverse the order of a table -reverse_table = function(t) +local reverse_table = function(t) local new_table = {} local i = #t --length of original table for k,v in ipairs(t) do @@ -146,7 +180,7 @@ reverse_table = function(t) return(new_table) end -quick_stack = function(chest, player_inventory) +local quick_stack = function(chest, player_inventory) for k,v in pairs(chest.slots) do if v and v.components and v.components.stackable then for k1,v1 in pairs(player_inventory.itemslots) do @@ -200,7 +234,7 @@ quick_stack = function(chest, player_inventory) end end -quick_stack_backpack = function(chest, backpack) +local quick_stack_backpack = function(chest, backpack) for k,v in pairs(chest.slots) do if v and v.components and v.components.stackable then for k1,v1 in pairs(backpack.slots) do @@ -277,7 +311,7 @@ local final_sort = function(player,index,object,reverse,mode) end -- Main sort inventory function -sort_inv = function() +local sort_inv = function() local player = GLOBAL.GetPlayer() local backpack = player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.PACK) or player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BACK) or player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY) if not sort_backpack then backpack = nil end @@ -296,9 +330,12 @@ sort_inv = function() local temp_inventory = {} local weapons = {} local tools = {} + local books = {} --added books & lights categories + local lights ={} local equips = {} local foods = {} local others = {} + local recipes = GLOBAL.GetAllRecipes() -- First see if there's a chest and use quick_stack if open_chest then @@ -320,9 +357,22 @@ sort_inv = function() --backpack exists so start taking items from there item = backpack.components.container.slots[i-MAXSLOTS] end - -- equips - if item then - if (item["components"]["equippable"]) then -- includes weapons, tools, and other equippable items + if item and item.prefab then + -- print(item.prefab) + -- if recipes[item.prefab] then + -- print(recipes[item.prefab].tab) + -- end + if (item["components"]["book"]) then --books have component book but it wasn't working (they are not equippable !) + local new_item_index = #books+1 + for n = 1, #books do + o = books[n] + if o["prefab"] == item["prefab"] then + new_item_index = n+1 -- stick the identical item in the next spot + end + end + table.insert(books, new_item_index, item) + -- equips + elseif (item["components"]["equippable"]) then -- includes weapons, tools, and other equippable items if (item["components"]["tool"]) then local new_item_index = #tools+1 for n = 1, #tools do @@ -332,7 +382,16 @@ sort_inv = function() end end table.insert(tools, new_item_index, item) - elseif (item["components"]["weapon"]) then + elseif (item["Light"] or recipes[item.prefab].tab == RECIPETABS.LIGHT) then --or item.prefab == "torch" or item.prefab == "molehat") then --lights + local new_item_index = #lights+1 --item["fire"] doesn't exist until equipped. + for n = 1, #lights do + o = lights[n] + if o["prefab"] == item["prefab"] then + new_item_index = n+1 -- stick the identical item in the next spot + end + end + table.insert(lights, new_item_index, item) + elseif (item["components"]["weapon"]) then local new_item_index = #weapons+1 for n = 1, #weapons do o = weapons[n] @@ -341,7 +400,7 @@ sort_inv = function() end end table.insert(weapons, new_item_index, item) - elseif (item["components"]["equippable"]) then + else --if (item["components"]["equippable"]) then --already part of the earlier check !! local new_item_index = #equips+1 for n = 1, #equips do o = equips[n] @@ -403,21 +462,18 @@ sort_inv = function() -- Method 2: Stick others at end, starting in the pack if present if method == 2 then index = final_sort(player,index,weapons,false,1) --final_sort 1 is giveitem, 2 is placeatend, 3 is nobackpack, 4 is placeatstart + index = final_sort(player,index,lights,false,1) index = final_sort(player,index,tools,false,1) + index = final_sort(player,index,books,false,1) index = final_sort(player,index,equips,false,1) index = final_sort(player,index,foods,false,1) index = final_sort(player,index,others,true,2) -- true means reverse table - -- Method 1: Dump everything in inventory in an intuitive order - elseif method == 1 then - index = final_sort(player,index,weapons,false,1) - index = final_sort(player,index,tools,false,1) - index = final_sort(player,index,equips,false,1) - index = final_sort(player,index,foods,false,1) - index = final_sort(player,index,others,false,1) -- Sort method 3: Place all food into backpack if possible. Do food last elseif method == 3 then index = final_sort(player,index,weapons,false,1) + index = final_sort(player,index,lights,false,1) index = final_sort(player,index,tools,false,1) + index = final_sort(player,index,books,false,1) index = final_sort(player,index,equips,false,1) index = final_sort(player,index,others,false,1) index = final_sort(player,index,foods,true,2) @@ -425,14 +481,39 @@ sort_inv = function() -- others, food elseif method == 4 then index = final_sort(player,index,weapons,true,3) + index = final_sort(player,index,lights,true,3) index = final_sort(player,index,tools,true,3) + index = final_sort(player,index,books,true,3) index = final_sort(player,index,equips,true,3) index = final_sort(player,index,others,false,4) index = final_sort(player,index,foods,false,4) + --custom sorting options + elseif method == 5 then + for i,v in ipairs(places) do --for i=1,#places do + if v == "w" then v = weapons + elseif v == "t" then v = tools + elseif v == "l" then v = lights + elseif v == "b" then v = books + elseif v == "e" then v = equips + elseif v == "f" then v = foods + elseif v == "o" then v = others + end + index = final_sort(player,index,v,reverses[i],orders[i]) + end + + -- Method 1: Dump everything in inventory in an intuitive order + elseif method == 1 then + index = final_sort(player,index,weapons,false,1) + index = final_sort(player,index,lights,false,1) + index = final_sort(player,index,tools,false,1) + index = final_sort(player,index,books,false,1) + index = final_sort(player,index,equips,false,1) + index = final_sort(player,index,foods,false,1) + index = final_sort(player,index,others,false,1) end end -toggle_method = function() +local toggle_method = function() if sort_method == 1 then sort_method = 2 print("Sorting method 2") @@ -442,13 +523,16 @@ toggle_method = function() elseif sort_method == 3 then sort_method = 4 print("Sorting method 4") + elseif sort_method == 4 and sort_custom then + sort_method = checkduplicate() + print("Sorting method Custom") else sort_method = 1 print("Sorting method 1") end end -toggle_hold = function() +local toggle_hold = function() if lowest_sort_index == 1 then lowest_sort_index = 2 print("Left-most slot shall remain unsorted")