From 482eb7bebdfdcf099fddcdaaaa4234921744e5b9 Mon Sep 17 00:00:00 2001 From: theFox6 Date: Tue, 3 Mar 2020 17:16:35 +0100 Subject: [PATCH] fixed varous item vanishing bugs --- LICENSE | 2 +- README.md | 18 ++++++++----- modules/network/init.lua | 16 ++++++----- modules/network/network.lua | 51 +++++++++++++++++++++++------------- modules/storage/api.lua | 40 ++++++++++++++++++++-------- modules/storage/drive.lua | 11 ++++---- modules/storage/terminal.lua | 13 ++++++--- 7 files changed, 100 insertions(+), 51 deletions(-) diff --git a/LICENSE b/LICENSE index 0b3ce07..21d8a34 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ MIT License -Copyright (c) 2017 Elijah Duffy +Copyright (c) 2019-2020 theFox6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell diff --git a/README.md b/README.md index b980410..69fd6db 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,16 @@ MicroExpansion - ME [microexpansion] ====================================== * **Licence:** Code: MIT (see LICENSE), Media: CC-BY-SA 3.0 -* [Github Repository](https://github.com/octacian/microexpansion) -* **Downloads:** - * [Master (Unstable)](https://github.com/octacian/microexpansion/archive/master.zip) - * ...or browse the code on [GitHub](https://github.com/octacian/microexpansion) -When you really get into a survival world, you typically end up with a lot of items, like a ton of items. Sometimes literally. But with that huge amount of resources, comes something annoying and typically unwanted: chests. Well, of course. You have to have chests to store items, but no biggie, it's just chests. Then your storage room starts to grow, soon you have 25 chests, then 50, then 100. Management gets kinda hard. MicroExpansion, is the solution. +When you really get into a survival world, you typically end up with a lot of items, like a ton of items. +Sometimes literally. +But with that huge amount of resources, comes something annoying and typically unwanted: chests. +Well, of course. You have to have chests to store items, but no biggie, it's just chests. +Then your storage room starts to grow, soon you have 25 chests, then 50, then 100. +When management gets too hard. MicroExpansion, is the solution. -Remade by theFox, original mod by octacian. -Originally inspired by Applied Energistics 2 for Minecraft, MicroExpansion introduces many new nodes and items giving the player simpler and more compact ways to store thousands of items inside of a single ME drive. +Forked and continued by theFox6 from the original mod by octacian. +[Original Repository](https://github.com/octacian/microexpansion) +Original license: MIT +This mod was inspired by Applied Energistics 2 for Minecraft +MicroExpansion introduces many new nodes and items giving the player simpler and more compact ways to store thousands of items inside of a single ME drive. diff --git a/modules/network/init.lua b/modules/network/init.lua index 0693b9d..c907796 100644 --- a/modules/network/init.lua +++ b/modules/network/init.lua @@ -3,11 +3,7 @@ me.networks = {} local networks = me.networks local path = microexpansion.get_module_path("network") -function me.insert_item(stack, inv, listname) - if me.settings.huge_stacks == false then - inv:add_item(listname, stack) - return - end +local function split_stack_values(stack) local stack_name local stack_count if type(stack) == "string" then @@ -22,6 +18,14 @@ function me.insert_item(stack, inv, listname) stack_name = stack:get_name() stack_count = stack:get_count() end + return stack_name, stack_count +end + +function me.insert_item(stack, inv, listname) + if me.settings.huge_stacks == false then + return inv:add_item(listname, stack) + end + local stack_name,stack_count = split_stack_values(stack) local found = false for i = 0, inv:get_size(listname) do local inside = inv:get_stack(listname, i) @@ -40,7 +44,7 @@ function me.insert_item(stack, inv, listname) end end if not found then - inv:add_item(listname, stack) + return inv:add_item(listname, stack) end end diff --git a/modules/network/network.lua b/modules/network/network.lua index 783a696..98766ed 100644 --- a/modules/network/network.lua +++ b/modules/network/network.lua @@ -7,7 +7,8 @@ local network = { power_load = 0, power_storage = 0 } -microexpansion.network = network +local me = microexpansion +me.network = network --- construct a new network -- @function [parent=#network] new @@ -30,7 +31,7 @@ function network.can_connect(np) if np.name then nn = np.name else - local node = microexpansion.get_node(np) + local node = me.get_node(np) nn = node.name end end @@ -55,7 +56,7 @@ function network.adjacent_connected_nodes(pos, include_ctrl) local nodes = {} for _,apos in pairs(adjacent) do - local napos = microexpansion.get_node(apos) + local napos = me.get_node(apos) local nn = napos.name if network.can_connect(nn) then if include_ctrl == false then @@ -123,7 +124,7 @@ local function get_drive_capacity(pos) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() for i = 1, inv:get_size("main") do - cap = cap + microexpansion.get_cell_size(inv:get_stack("main", i):get_name()) + cap = cap + me.get_cell_size(inv:get_stack("main", i):get_name()) end return cap end @@ -133,8 +134,8 @@ end -- @return #number the total number of items that can be stored in the network function network:get_item_capacity() local cap = 0 - for npos in microexpansion.connected_nodes(self.controller_pos) do - if microexpansion.get_node(npos).name == "microexpansion:drive" then + for npos in me.connected_nodes(self.controller_pos) do + if me.get_node(npos).name == "microexpansion:drive" then cap = cap + get_drive_capacity(npos) end end @@ -142,7 +143,22 @@ function network:get_item_capacity() return cap end -function network:add_storage_slots(count,listname) +local function remove_slots(inv,ln,target,csize) + for i = target, csize do + local s = inv:get_stack(ln,i) + if not s:is_empty() then + inv:set_stack(ln, i, "") + me.insert_item(s, inv, ln) + end + end + --perhaps allow list removal + if target < 0 then + target = 1 + end + inv:set_size(ln, target) +end + +function network:set_storage_space(count,listname) local c = count or 1 local ln = listname or "main" local inv = self:get_inventory() @@ -167,18 +183,17 @@ function network:add_storage_slots(count,listname) end end local needed = c - space - needed = needed + csize - --TODO allow list removal - if needed == 0 then - needed = 1 + if needed > 0 then + needed = needed + csize + inv:set_size(ln, needed) + elseif needed < 0 then + needed = needed + csize + remove_slots(inv,ln,needed,csize) end - inv:set_size(ln, needed) end ---FIXME: add size removal function because items are removed when size decreases - function network:update() - self:add_storage_slots(true) + self:set_storage_space(true) end function network:get_inventory_name() @@ -217,14 +232,14 @@ local function create_inventory(net) end, on_put = function(inv, listname, _, stack) inv:remove_item(listname, stack) - microexpansion.insert_item(stack, inv, listname) - net:add_storage_slots(true) + me.insert_item(stack, inv, listname) + net:set_storage_space(true) end, allow_take = function(_, _, _, stack) return math.min(stack:get_count(),stack:get_stack_max()) end, on_take = function() - net:add_storage_slots(true) + net:set_storage_space(true) end }) end diff --git a/modules/storage/api.lua b/modules/storage/api.lua index a9eb74b..dd9fcb4 100644 --- a/modules/storage/api.lua +++ b/modules/storage/api.lua @@ -51,18 +51,36 @@ function microexpansion.int_to_pagenum(int) end -- [function] Move items from inv to inv -function microexpansion.move_inv(inv1, inv2) - local finv, tinv = inv1.inv, inv2.inv - local fname, tname = inv1.name, inv2.name +function microexpansion.move_inv(inv1, inv2, max) + if max <= 0 then return end + local finv, tinv = inv1.inv, inv2.inv + local fname, tname = inv1.name, inv2.name + local huge = inv2.huge + local inserted = 0 - --FIXME only as many as allowed in a drive - for _,v in ipairs(finv:get_list(fname) or {}) do - if tinv and tinv:room_for_item(tname, v) then - local leftover = tinv:add_item( tname, v ) - finv:remove_item(fname, v) - if leftover and not(leftover:is_empty()) then - finv:add_item(fname, v) - end + for _,v in ipairs(finv:get_list(fname) or {}) do + local left = max-inserted + if left <= 0 then + break; + end + if not v:is_empty() then + if v:get_count() > left then + v = v:peek_item(left) + end + if tinv and tinv:room_for_item(tname, v) then + if huge then + microexpansion.insert_item(v, tinv, tname) + finv:remove_item(fname, v) + else + local leftover = tinv:add_item(tname, v) + finv:remove_item(fname, v) + if leftover and not(leftover:is_empty()) then + minetest.log("warning","leftover items when transfering inventory") + finv:add_item(fname, leftover) + end + end + inserted = inserted + v:get_count() + end end end end diff --git a/modules/storage/drive.lua b/modules/storage/drive.lua index 64104df..953f8b1 100644 --- a/modules/storage/drive.lua +++ b/modules/storage/drive.lua @@ -80,7 +80,7 @@ microexpansion.register_node("drive", { me.update_connected_machines(pos) return end - network:add_storage_slots(#items) + network:set_storage_space(#items) for _,s in pairs(items) do me.insert_item(s, ctrl_inv, "main") end @@ -114,10 +114,10 @@ microexpansion.register_node("drive", { if stack_name ~= "" then local item_count = stack_inside:get_count() while item_count ~= 0 and cell_idx ~= nil do + --print(("stack to store: %s %i"):format(stack_name,item_count)) if size < items_in_cell_count + item_count then local space = size - items_in_cell_count - item_count = item_count - space - table.insert(cell_items,stack_name.." "..space) + table.insert(cell_items,("%s %i"):format(stack_name,space)) items_in_cell_count = items_in_cell_count + space own_inv:set_stack("main", cell_idx, write_to_cell(cells[cell_idx],cell_items,items_in_cell_count)) @@ -125,13 +125,14 @@ microexpansion.register_node("drive", { size = microexpansion.get_cell_size(cells[cell_idx]:get_name()) items_in_cell_count = 0 cell_items = {} + item_count = item_count - space if cell_idx == nil then --there may be other drives within the network minetest.log("info","too many items to store in drive") end else items_in_cell_count = items_in_cell_count + item_count - table.insert(cell_items,stack_inside:to_string()) + table.insert(cell_items, ("%s %i"):format(stack_name,item_count)) item_count = 0 end end @@ -165,7 +166,7 @@ microexpansion.register_node("drive", { --this returns 99 (max count) even if it removes more ctrl_inv:remove_item("main", ostack) end - print(stack:to_string()) + --print(stack:to_string()) network:update() me.update_connected_machines(pos) diff --git a/modules/storage/terminal.lua b/modules/storage/terminal.lua index 15b24cf..fd6f0bc 100644 --- a/modules/storage/terminal.lua +++ b/modules/storage/terminal.lua @@ -191,9 +191,16 @@ microexpansion.register_node("term", { meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max)) elseif fields.tochest then local pinv = minetest.get_inventory({type="player", name=sender:get_player_name()}) - net:add_storage_slots(pinv:get_size("main")) - microexpansion.move_inv({ inv=pinv, name="main" }, { inv=ctrl_inv, name="main" }) - net:add_storage_slots(true) + net:set_storage_space(pinv:get_size("main")) + local space = net:get_item_capacity() + local contents = ctrl_inv:get_list("main") or {} + for _,s in pairs(contents) do + if not s:is_empty() then + space = space - s:get_count() + end + end + microexpansion.move_inv({ inv=pinv, name="main" }, { inv=ctrl_inv, name="main",huge=true }, space) + net:set_storage_space(true) end end, })