From deab00aa073bca2e931874c7cec4fb1b33a167fd Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Thu, 11 Nov 2021 22:04:43 +0100 Subject: [PATCH] Improve move-controller, remove 'move' command --- basis/lib.lua | 26 +++++++++++++ doc/manual_DE.lua | 1 - doc/manual_EN.lua | 1 - logic/button_2x.lua | 27 +++++++++----- logic/button_4x.lua | 27 +++++++++----- logic/lib.lua | 2 +- logic/movecontroller.lua | 81 ++++++++++++++++++---------------------- manuals/manual_ta4_DE.md | 1 - manuals/manual_ta4_EN.md | 1 - 9 files changed, 100 insertions(+), 67 deletions(-) diff --git a/basis/lib.lua b/basis/lib.lua index 9d9a319..7905a85 100644 --- a/basis/lib.lua +++ b/basis/lib.lua @@ -27,6 +27,32 @@ local Input = { 20,21,22,23, -- 6 } +-- Input data for facedir_to_rotation +local PARAM2_TO_ROT = {[0] = + 1,39,35,47, + 49,38,32,48, + 17,14,56,8, + 2,50,12,28, + 4,20,10,52, + 3,7,11,15 +} + +local Rotations = {} + +for x = 0,3 do + for y = 0,3 do + for z = 0,3 do + Rotations[#Rotations + 1] = {x=x*math.pi/2, y=y*math.pi/2, z=z*math.pi/2} + end + end +end + +function techage.facedir_to_rotation(facedir) + local idx = PARAM2_TO_ROT[facedir] or 0 + return Rotations[idx] +end + + -- allowed for digging local RegisteredNodesToBeDug = {} diff --git a/doc/manual_DE.lua b/doc/manual_DE.lua index 7de46ff..6038fb7 100644 --- a/doc/manual_DE.lua +++ b/doc/manual_DE.lua @@ -1620,7 +1620,6 @@ techage.manual_DE.aText = { "\n".. " - 'a2b' Bewege Block von A nach B\n".. " - 'b2a' Bewege Block von B nach A\n".. - " - 'move' Bewege Block auf die andere Seite\n".. "\n".. "\n".. "\n", diff --git a/doc/manual_EN.lua b/doc/manual_EN.lua index d4aed2d..73d4bd3 100644 --- a/doc/manual_EN.lua +++ b/doc/manual_EN.lua @@ -1618,7 +1618,6 @@ techage.manual_EN.aText = { "\n".. " - 'a2b' Move block from A to B.\n".. " - 'b2a' Move block from B to A.\n".. - " - 'move' Move block to the other side\n".. "\n".. "\n".. "\n", diff --git a/logic/button_2x.lua b/logic/button_2x.lua index 2c5a18c..3d46904 100644 --- a/logic/button_2x.lua +++ b/logic/button_2x.lua @@ -143,6 +143,21 @@ end local lcd_box = {-8/16, -4/16, 7.75/16, 8/16, 4/16, 8/16} +local function can_access(pos, player) + local meta = M(pos) + local playername = player:get_player_name() + local access = meta:get_string("access") + local owner = meta:get_string("owner") + local protected = minetest.is_protected(pos, playername) + + if access == "private" and playername ~= owner then + return false + elseif access == "protected" and protected then + return false + end + return true +end + minetest.register_node("techage:ta4_button_2x", { description = S("TA4 2x Button"), inventory_image = 'techage_smartline_button_2x.png', @@ -177,17 +192,11 @@ minetest.register_node("techage:ta4_button_2x", { on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) if clicker and clicker:is_player() then - local playername = clicker:get_player_name() - if minetest.is_protected(pos, playername) then + -- Check access settings + if not can_access(pos, clicker) then return end - -- Check node settings in addition - local access = M(pos):get_string("access") - local owner = M(pos):get_string("owner") - if access == "private" and playername ~= owner then - return - end - + local num = get_button_num(pos, clicker, pointed_thing) if num then local typ = M(pos):get_string("type") diff --git a/logic/button_4x.lua b/logic/button_4x.lua index 56ef4ea..7377265 100644 --- a/logic/button_4x.lua +++ b/logic/button_4x.lua @@ -195,6 +195,21 @@ end local lcd_box = {-8/16, -8/16, 7.75/16, 8/16, 8/16, 8/16} +local function can_access(pos, player) + local meta = M(pos) + local playername = player:get_player_name() + local access = meta:get_string("access") + local owner = meta:get_string("owner") + local protected = minetest.is_protected(pos, playername) + + if access == "private" and playername ~= owner then + return false + elseif access == "protected" and protected then + return false + end + return true +end + minetest.register_node("techage:ta4_button_4x", { description = S("TA4 4x Button"), inventory_image = 'techage_smartline_button_4x.png', @@ -231,17 +246,11 @@ minetest.register_node("techage:ta4_button_4x", { on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) if clicker and clicker:is_player() then - local playername = clicker:get_player_name() - if minetest.is_protected(pos, playername) then + -- Check access settings + if not can_access(pos, clicker) then return end - -- Check node settings in addition - local access = M(pos):get_string("access") - local owner = M(pos):get_string("owner") - if access == "private" and playername ~= owner then - return - end - + local num = get_button_num(pos, clicker, pointed_thing) if num then local typ = M(pos):get_string("type") diff --git a/logic/lib.lua b/logic/lib.lua index 86a99fe..1d21911 100644 --- a/logic/lib.lua +++ b/logic/lib.lua @@ -102,4 +102,4 @@ function techage.logic.set_numbers(pos, numbers, player_name, descr) return true end return false -end \ No newline at end of file +end diff --git a/logic/movecontroller.lua b/logic/movecontroller.lua index 55939ee..72c1dbb 100644 --- a/logic/movecontroller.lua +++ b/logic/movecontroller.lua @@ -48,8 +48,10 @@ end -- determine exact position of attached entities local function obj_pos(obj) local _, _, pos = obj:get_attach() - pos = vector.divide(pos, 29) - return vector.add(obj:get_pos(), pos) + if pos then + pos = vector.divide(pos, 29) + return vector.add(obj:get_pos(), pos) + end end -- Check access conflicts with other mods @@ -77,9 +79,11 @@ end local function detach_player(player) local pos = obj_pos(player) - player:set_detach() - player:set_properties({visual_size = {x=1, y=1}}) - player:set_pos(pos) + if pos then + player:set_detach() + player:set_properties({visual_size = {x=1, y=1}}) + player:set_pos(pos) + end -- TODO: move to save position end @@ -153,14 +157,13 @@ local function entity_to_node(pos, obj) local self = obj:get_luaentity() if self then local name = self.item or "air" + local param2 = self.param2 or 0 local metadata = self.metadata or {} local rot = obj:get_rotation() detach_objects(pos, self) obj:remove() pos = vector.round(pos) - local dir = minetest.yaw_to_dir(rot.y or 0) - local param2 = minetest.dir_to_facedir(dir) or 0 local node = minetest.get_node(pos) local ndef1 = minetest.registered_nodes[name] local ndef2 = minetest.registered_nodes[node.name] @@ -184,7 +187,7 @@ local function entity_to_node(pos, obj) end end -local function node_to_entity(pos, handover) +local function node_to_entity(pos, handover, pos_2to1) local meta = M(pos) local node, metadata @@ -198,17 +201,18 @@ local function node_to_entity(pos, handover) metadata = meta:to_table() minetest.remove_node(pos) end - local dir = minetest.facedir_to_dir(node.param2) - local yaw = minetest.dir_to_yaw(dir) local obj = minetest.add_entity(pos, "techage:move_item") if obj then local self = obj:get_luaentity() - obj:set_rotation({x=0, y=yaw, z=0}) + local rot = techage.facedir_to_rotation(node.param2) + obj:set_rotation(rot) obj:set_properties({wield_item=node.name}) obj:set_armor_groups({immortal=1}) self.item = node.name + self.param2 = node.param2 self.metadata = metadata or {} self.handover = handover + self.pos_2to1 = pos_2to1 self.start_pos = table.copy(pos) return obj end @@ -247,10 +251,7 @@ local function handover_to(pos, self) local mem = techage.get_mem(info.pos) if not mem.entities_are_there then mem.entities_are_there = true - -- copy move direction - --print("techage.get_nvm(pos).pos_2to1", techage.get_nvm(pos).pos_2to1) - techage.get_nvm(info.pos).pos_2to1 = techage.get_nvm(pos).pos_2to1 - minetest.after(0.2, techage.send_single, "0", self.handover, "handover") + minetest.after(0.2, techage.send_single, "0", self.handover, "handover", self.pos_2to1) end return true end @@ -303,13 +304,15 @@ minetest.register_entity("techage:move_item", { local pos = obj:get_pos() local dist = vector.distance(pos, self.dest_pos) local speed = vector.length(obj:get_velocity()) + self.old_dist = self.old_dist or dist -- Landing - if dist < 0.05 then + if dist < 0.05 or dist > self.old_dist then obj:move_to(self.dest_pos, true) obj:set_acceleration({x=0, y=0, z=0}) obj:set_velocity({x=0, y=0, z=0}) self.dest_pos = nil + self.old_dist = nil if not self.handover or not handover_to(pos, self) then minetest.after(0.5, entity_to_node, pos, obj) end @@ -317,6 +320,8 @@ minetest.register_entity("techage:move_item", { return end + self.old_dist = dist + -- Braking or limit max speed if speed > (dist * 2) or speed > self.max_speed then local speed = math.min(speed, math.max(dist * 2, MIN_SPEED)) @@ -527,10 +532,9 @@ local function formspec(nvm, meta) "label[0.3,4.3;" .. status .. "]" end -local function move_node(pos, pos1, pos2, max_speed, handover, height) - local meta = M(pos) +local function move_node(pos, pos1, pos2, max_speed, handover, height, pos_2to1) local dir = determine_dir(pos1, pos2) - local obj = node_to_entity(pos1, handover) + local obj = node_to_entity(pos1, handover, pos_2to1) local self = obj:get_luaentity() self.players = {} self.entities = {} @@ -547,7 +551,7 @@ local function move_node(pos, pos1, pos2, max_speed, handover, height) end end -local function move_nodes(pos, lpos1, lpos2, handover) +local function move_nodes(pos, lpos1, lpos2, handover, pos_2to1) local meta = M(pos) local owner = meta:get_string("owner") local max_speed = meta:contains("max_speed") and meta:get_int("max_speed") or MAX_SPEED @@ -560,7 +564,7 @@ local function move_nodes(pos, lpos1, lpos2, handover) local pos2 = lpos2[idx] if not minetest.is_protected(pos1, owner) and not minetest.is_protected(pos2, owner) then if is_simple_node(pos1) and is_valid_dest(pos2) then - move_node(pos, pos1, pos2, max_speed, handover, height) + move_node(pos, pos1, pos2, max_speed, handover, height, pos_2to1) else if not is_simple_node(pos1) then meta:set_string("status", S("No valid node at the start position")) @@ -634,41 +638,37 @@ local function moveon_nodes(pos, lpos1, lpos2, handover) return true end -local function move_to_other_pos(pos) +local function move_to_other_pos(pos, pos_2to1) local meta = M(pos) local nvm = techage.get_nvm(pos) - if nvm.pos_2to1 then + if pos_2to1 then local lpos1 = nvm.lpos1 or {} local lpos2 = nvm.lpos2 or {} - nvm.pos_2to1 = false local handover = meta:contains("handoverA") and meta:get_string("handoverA") - return move_nodes(pos, lpos2, lpos1, handover) + return move_nodes(pos, lpos2, lpos1, handover, pos_2to1) else local lpos1 = nvm.lpos1 or {} local lpos2 = nvm.lpos2 or {} - nvm.pos_2to1 = true local handover = meta:contains("handoverB") and meta:get_string("handoverB") - return move_nodes(pos, lpos1, lpos2, handover) + return move_nodes(pos, lpos1, lpos2, handover, pos_2to1) end end -local function takeover(pos) +local function takeover(pos, pos_2to1) local meta = M(pos) local nvm = techage.get_nvm(pos) local mem = techage.get_mem(pos) mem.entities_are_there = nil - if nvm.pos_2to1 then + if pos_2to1 then local lpos1 = nvm.lpos1 or {} local lpos2 = nvm.lpos2 or {} - nvm.pos_2to1 = false local handover = meta:contains("handoverA") and meta:get_string("handoverA") return moveon_nodes(pos, lpos2, lpos1, handover) else local lpos1 = nvm.lpos1 or {} local lpos2 = nvm.lpos2 or {} - nvm.pos_2to1 = true local handover = meta:contains("handoverB") and meta:get_string("handoverB") return moveon_nodes(pos, lpos1, lpos2, handover) end @@ -733,15 +733,14 @@ minetest.register_node("techage:ta4_movecontroller", { elseif fields.moveAB then meta:set_string("status", "") nvm.pos_2to1 = false - if move_to_other_pos(pos) then + if move_to_other_pos(pos, false) then meta:set_string("formspec", formspec(nvm, meta)) local name = player:get_player_name() MarkedNodes[name] = nil end elseif fields.moveBA then meta:set_string("status", "") - nvm.pos_2to1 = true - if move_to_other_pos(pos) then + if move_to_other_pos(pos, true) then meta:set_string("formspec", formspec(nvm, meta)) local name = player:get_player_name() MarkedNodes[name] = nil @@ -763,24 +762,18 @@ minetest.register_node("techage:ta4_movecontroller", { sounds = default.node_sound_wood_defaults(), }) -local INFO = [[Commands: 'a2b', 'b2a', 'move']] +local INFO = [[Commands: 'a2b', 'b2a']] techage.register_node({"techage:ta4_movecontroller"}, { on_recv_message = function(pos, src, topic, payload) if topic == "info" then return INFO elseif topic == "a2b" then - local nvm = techage.get_nvm(pos) - nvm.pos_2to1 = false - return move_to_other_pos(pos) + return move_to_other_pos(pos, false) elseif topic == "b2a" then - local nvm = techage.get_nvm(pos) - nvm.pos_2to1 = true - return move_to_other_pos(pos) - elseif topic == "move" then - return move_to_other_pos(pos) + return move_to_other_pos(pos, true) elseif topic == "handover" then - return takeover(pos) + return takeover(pos, payload) end return false end, diff --git a/manuals/manual_ta4_DE.md b/manuals/manual_ta4_DE.md index f181706..40b8628 100644 --- a/manuals/manual_ta4_DE.md +++ b/manuals/manual_ta4_DE.md @@ -541,7 +541,6 @@ Der Move Controller unterstützt folgende techage Kommandos: - `a2b` Bewege Block von A nach B - `b2a` Bewege Block von B nach A -- `move` Bewege Block auf die andere Seite [ta4_movecontroller|image] diff --git a/manuals/manual_ta4_EN.md b/manuals/manual_ta4_EN.md index 93bf82d..4f0cf6c 100644 --- a/manuals/manual_ta4_EN.md +++ b/manuals/manual_ta4_EN.md @@ -535,7 +535,6 @@ The Move Controller supports the following techage commands: - `a2b` Move block from A to B. - `b2a` Move block from B to A. -- `move` Move block to the other side [ta4_movecontroller|image]