From 59a0a3248e4bd7a2383810a711329f90f63e03ce Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Tue, 14 Apr 2020 18:46:50 +0200 Subject: [PATCH] ta4_chest and display improvements --- basic_machines/ta4_chest.lua | 68 +++++++++++++++++++----------------- doc/manual_DE.lua | 4 +-- icta_controller/display.lua | 31 +++++++++++----- manuals/manual_ta4_DE.md | 4 +-- 4 files changed, 59 insertions(+), 48 deletions(-) diff --git a/basic_machines/ta4_chest.lua b/basic_machines/ta4_chest.lua index 2ad9680..44e3ee6 100644 --- a/basic_machines/ta4_chest.lua +++ b/basic_machines/ta4_chest.lua @@ -27,6 +27,18 @@ local function gen_inv(nvm) end end +local function repair_inv(nvm) + nvm.inventory = nvm.inventory or {} + for i = 1,8 do + local item = nvm.inventory[i] + if not item or type(item) ~= "table" + or not item.name or type(item.name) ~= "string" + or not item.count or type(item.count) ~= "number" then + nvm.inventory[i] = {name = "", count = 0} + end + end +end + local function get_stack(nvm, idx) nvm.inventory = nvm.inventory or {} if nvm.inventory[idx] then @@ -91,24 +103,26 @@ local function get_stacksize(pos) end -- Sort the items into the nvm inventory --- If the nvm inventry is full, the items are stored in the main inventory --- If the main inventory is also full, false is returned -local function sort_in(pos, inv, nvm, stack) - if inv:is_empty("main") then -- the main inv is used for the case the nvm-inventory is full - for _,item in ipairs(nvm.inventory or {}) do - if item.name and (item.name == "" or item.name == stack:get_name()) then - local count = math.min(stack:get_count(), get_stacksize(pos) - item.count) - item.count = item.count + count - item.name = stack:get_name() - stack:set_count(stack:get_count() - count) - if stack:get_count() == 0 then - return true - end +local function sort_in(pos, nvm, stack) + local old_counts = {} + local orig_count = stack:get_count() + for idx,item in ipairs(nvm.inventory or {}) do + if item.name and (item.name == "" or item.name == stack:get_name()) then + local count = math.min(stack:get_count(), get_stacksize(pos) - item.count) + old_counts[idx] = item.count -- store old value + item.count = item.count + count + item.name = stack:get_name() + stack:set_count(stack:get_count() - count) + if stack:get_count() == 0 then + return true end end - inv:add_item("main", stack) - return true end + -- restore old values + for idx,cnt in pairs(old_counts) do + nvm.inventory[idx].count = cnt + end + stack:set_count(orig_count) return false end @@ -124,21 +138,10 @@ local function move_items_to_stack(item, stack, num) return stack end -local function get_item(pos, inv, nvm, item_name, count) +local function get_item(pos, nvm, item_name, count) local stack = {count = 0} nvm.inventory = nvm.inventory or {} - if not inv:is_empty("main") then - if item_name then - local taken = inv:remove_item("main", {name = item_name, count = count}) - if taken:get_count() > 0 then - return taken - end - else - return techage.get_items(pos, inv, "main", count) - end - end - if item_name then -- Take specified items from the chest for _,item in ipairs(nvm.inventory) do @@ -327,6 +330,8 @@ end local function on_rightclick(pos, node, clicker) if M(pos):get_int("disabled") ~= 1 then + local nvm = techage.get_nvm(pos) + repair_inv(nvm) M(pos):set_string("formspec", formspec(pos)) techage.set_activeformspec(pos, clicker) end @@ -482,8 +487,7 @@ minetest.register_node("techage:ta4_chest_dummy", { techage.register_node({"techage:ta4_chest"}, { on_pull_item = function(pos, in_dir, num, item_name) local nvm = techage.get_nvm(pos) - local inv = M(pos):get_inventory() - local res = get_item(pos, inv, nvm, item_name, num) + local res = get_item(pos, nvm, item_name, num) if techage.is_activeformspec(pos) then M(pos):set_string("formspec", formspec(pos)) end @@ -491,8 +495,7 @@ techage.register_node({"techage:ta4_chest"}, { end, on_push_item = function(pos, in_dir, stack) local nvm = techage.get_nvm(pos) - local inv = M(pos):get_inventory() - local res = sort_in(pos, inv, nvm, stack) + local res = sort_in(pos, nvm, stack) if techage.is_activeformspec(pos) then M(pos):set_string("formspec", formspec(pos)) end @@ -500,8 +503,7 @@ techage.register_node({"techage:ta4_chest"}, { end, on_unpull_item = function(pos, in_dir, stack) local nvm = techage.get_nvm(pos) - local inv = M(pos):get_inventory() - local res = sort_in(pos, inv, nvm, stack) + local res = sort_in(pos, nvm, stack) if techage.is_activeformspec(pos) then M(pos):set_string("formspec", formspec(pos)) end diff --git a/doc/manual_DE.lua b/doc/manual_DE.lua index 7b3c9b3..5b0fd39 100644 --- a/doc/manual_DE.lua +++ b/doc/manual_DE.lua @@ -1349,9 +1349,7 @@ techage.manual_DE.aText = { "\n", "Die TA4 8x2000 Kiste hat kein normales Inventar wir andere Kisten\\, sondern verfügt über 8 Speicher\\, wobei jeder Speicher bis zu 2000 Items einer Sorte aufnehmen kann. Über die orangefarbenen Taster können Items in den Speicher verschoben bzw. wieder heraus geholt werden. Die Kiste kann auch wie sonst üblich mit einem Schieber (TA2\\, TA3 oder TA4) gefüllt bzw. geleert werden.\n".. "\n".. - "Wird die Kiste mit einem Schieber gefüllt\\, so füllen sich alle Speicherplätze von links nach rechts. Sind alle 8 Speicher voll und können keine weiteren Items hinzugefügt werden\\, so werden diese Items im Ein-/Ausgabe-Inventar gespeichert. Die Kiste ist damit für den Schieberbetrieb vollständig gefüllt und weitere Items werden abgewiesen.\n".. - "\n".. - "Wird die Kiste über einen Schieber geleert\\, werden von Speicherplätzen Items entnommen.\n".. + "Wird die Kiste mit einem Schieber gefüllt\\, so füllen sich alle Speicherplätze von links nach rechts. Sind alle 8 Speicher voll und können keine weiteren Items hinzugefügt werden\\, so werden weitere Items werden abgewiesen.\n".. "\n".. "*Reihenfunktion*\n".. "\n".. diff --git a/icta_controller/display.lua b/icta_controller/display.lua index 8ace0af..5bf4018 100644 --- a/icta_controller/display.lua +++ b/icta_controller/display.lua @@ -16,11 +16,25 @@ local NUM_ROWS = 5 local RADIUS = 6 local Param2ToFacedir = {[0] = 0, 0, 3, 1, 2, 0} +local function lcdlib_bugfix(text_tbl) + if text_tbl and next(text_tbl) then + local t = {} + for _,txt in ipairs(text_tbl) do + if txt == "" then + t[#t+1] = " " + else + t[#t+1] = txt + end + end + return table.concat(t, "\n") + end + return "" +end + local function display_update(pos, objref) pos = vector.round(pos) local nvm = techage.get_nvm(pos) - nvm.text = nvm.text or {""} - local text = table.concat(nvm.text, "\n") + local text = lcdlib_bugfix(nvm.text) local texture = lcdlib.make_multiline_texture( "default", text, 70, 70, NUM_ROWS, "top", "#000") @@ -31,8 +45,7 @@ end local function display_updateXL(pos, objref) pos = vector.round(pos) local nvm = techage.get_nvm(pos) - nvm.text = nvm.text or {""} - local text = table.concat(nvm.text, "\n") + local text = lcdlib_bugfix(nvm.text) local texture = lcdlib.make_multiline_texture( "default", text, 126, 70, NUM_ROWS, "top", "#000") @@ -175,7 +188,7 @@ minetest.register_craft({ local function add_line(pos, payload, cycle_time) local nvm = techage.get_nvm(pos) local mem = techage.get_mem(pos) - nvm.text = nvm.text or {""} + nvm.text = nvm.text or {} mem.ticks = mem.ticks or 0 local str = tostring(payload) or "oops" @@ -192,7 +205,7 @@ end local function write_row(pos, payload, cycle_time) local nvm = techage.get_nvm(pos) local mem = techage.get_mem(pos) - nvm.text = nvm.text or {""} + nvm.text = nvm.text or {} mem.ticks = mem.ticks or 0 local str = tostring(payload.str) or "oops" local row = tonumber(payload.row) or 1 @@ -209,8 +222,8 @@ local function write_row(pos, payload, cycle_time) return end - while #nvm.text <= row do - table.insert(nvm.text, " ") + while #nvm.text < row do + table.insert(nvm.text, "") end nvm.text[row] = str end @@ -224,7 +237,7 @@ local function clear_screen(pos, cycle_time) mem.ticks = cycle_time end - nvm.text = {""} + nvm.text = {} end techage.register_node({"techage:ta4_display"}, { diff --git a/manuals/manual_ta4_DE.md b/manuals/manual_ta4_DE.md index c27f16e..30f7d79 100644 --- a/manuals/manual_ta4_DE.md +++ b/manuals/manual_ta4_DE.md @@ -520,9 +520,7 @@ Die Funktion entspricht der von TA3. Die Kiste kann aber mehr Inhalt aufnehmen. Die TA4 8x2000 Kiste hat kein normales Inventar wir andere Kisten, sondern verfügt über 8 Speicher, wobei jeder Speicher bis zu 2000 Items einer Sorte aufnehmen kann. Über die orangefarbenen Taster können Items in den Speicher verschoben bzw. wieder heraus geholt werden. Die Kiste kann auch wie sonst üblich mit einem Schieber (TA2, TA3 oder TA4) gefüllt bzw. geleert werden. -Wird die Kiste mit einem Schieber gefüllt, so füllen sich alle Speicherplätze von links nach rechts. Sind alle 8 Speicher voll und können keine weiteren Items hinzugefügt werden, so werden diese Items im Ein-/Ausgabe-Inventar gespeichert. Die Kiste ist damit für den Schieberbetrieb vollständig gefüllt und weitere Items werden abgewiesen. - -Wird die Kiste über einen Schieber geleert, werden von Speicherplätzen Items entnommen. +Wird die Kiste mit einem Schieber gefüllt, so füllen sich alle Speicherplätze von links nach rechts. Sind alle 8 Speicher voll und können keine weiteren Items hinzugefügt werden, so werden weitere Items werden abgewiesen. **Reihenfunktion**