From 159ef79ad316fcc5c5ec7de414466ef753fcc882 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Tue, 24 Dec 2024 23:47:24 +0100 Subject: [PATCH 1/9] make use of variables and don't shaddow them --- controller.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/controller.lua b/controller.lua index 35f384a..367735d 100644 --- a/controller.lua +++ b/controller.lua @@ -26,25 +26,26 @@ end local function process_inputs(pos) local meta = minetest.get_meta(pos) local hash = minetest.hash_node_position(pos) + local name = players_on_controller[hash] + local player = minetest.get_player_by_name(name) if minetest.get_node(pos).name ~= "digistuff:controller_programmed" then - local player = minetest.get_player_by_name(players_on_controller[hash]) if player then player:set_physics_override({speed = 1,jump = 1,}) player:set_pos(vector.add(pos,vector.new(0.25,0,0.25))) - minetest.chat_send_player(players_on_controller[hash],"You are now free to move.") + minetest.chat_send_player(name, "You are now free to move.") end - last_seen_inputs[players_on_controller[hash]] = nil + last_seen_inputs[name] = nil players_on_controller[hash] = nil return end - local name = players_on_controller[hash] - local player = minetest.get_player_by_name(name) + if not player then digilines.receptor_send(pos,digiline_rules,meta:get_string("channel"),"player_left") minetest.get_meta(pos):set_string("infotext","Digilines Game Controller Ready\n(right-click to use)") players_on_controller[hash] = nil return end + local inputs = player:get_player_control() inputs.pitch = player:get_look_vertical() inputs.yaw = player:get_look_horizontal() @@ -92,7 +93,8 @@ local function trap_player(pos,player) local oldname = players_on_controller[hash] local newname = player:get_player_name() if oldname and minetest.get_player_by_name(oldname) then - minetest.chat_send_player(player:get_player_name(),"Controller is already occupied by "..oldname) + minetest.chat_send_player(newname, + "Controller is already occupied by " .. oldname) return else players_on_controller[hash] = newname From aba0cc3eeae8ce0cf3b249497ddc9de93feff11f Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Tue, 24 Dec 2024 23:58:54 +0100 Subject: [PATCH 2/9] simpler to read when using variables --- controller.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/controller.lua b/controller.lua index 367735d..b6edd9b 100644 --- a/controller.lua +++ b/controller.lua @@ -72,18 +72,19 @@ end local function release_player(pos) local hash = minetest.hash_node_position(pos) - local player = minetest.get_player_by_name(players_on_controller[hash]) + local name = players_on_controller[hash] + local player = minetest.get_player_by_name(name) if player and player:get_properties()._is_gamecontroller then local parent = player:get_attach() if parent then player:set_detach() end - minetest.chat_send_player(players_on_controller[hash],"You are now free to move.") + minetest.chat_send_player(name, "You are now free to move.") end removeEntity(pos) local meta = minetest.get_meta(pos) meta:set_string("infotext","Digilines Game Controller Ready\n(right-click to use)") - last_seen_inputs[players_on_controller[hash]] = nil + last_seen_inputs[name] = nil players_on_controller[hash] = nil digilines.receptor_send(pos,digiline_rules,meta:get_string("channel"),"player_left") end From 3e2a3410e6255497a1fc2e7b2d9b4d82fb18f96e Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 25 Dec 2024 00:55:19 +0100 Subject: [PATCH 3/9] update luacheck to std = "luanti+max" --- .luacheckrc | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 13fa8a9..45ff7f0 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,19 +1,13 @@ unused_args = false max_line_length = 300 -- TODO: fix line lengths +std = "luanti+max" globals = { - "minetest", "digistuff", "digilines", } read_globals = { - -- Builtin - "table.copy", - "vector", - "ItemStack", - "DIR_DELIM", - -- Mod Deps "default", "mesecon", From 448bfdcbcc32b8fda41463ede41b2a5e401985bd Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 25 Dec 2024 00:58:09 +0100 Subject: [PATCH 4/9] fix never reached code --- controller.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/controller.lua b/controller.lua index b6edd9b..e3aa2ea 100644 --- a/controller.lua +++ b/controller.lua @@ -14,6 +14,12 @@ local players_on_controller = {} local last_seen_inputs = {} +-- TODO: can we remove this function now? +-- This does still clean up stray entities from crashes +-- or possibly from older versions? +-- Disconnecting from the game while attached also leaves stray +-- entities. Maybe we better handle those instead of using this +-- somewhat expensive search every time some player detaches. local function removeEntity(pos) local entitiesNearby = minetest.get_objects_inside_radius(pos,0.5) for _,i in pairs(entitiesNearby) do @@ -74,13 +80,16 @@ local function release_player(pos) local hash = minetest.hash_node_position(pos) local name = players_on_controller[hash] local player = minetest.get_player_by_name(name) - if player and player:get_properties()._is_gamecontroller then + if player then local parent = player:get_attach() - if parent then - player:set_detach() + local lua_entity = parent and parent:get_luaentity() + if lua_entity and lua_entity._is_gamecontroller then + -- Remove also detaches + parent:remove() end minetest.chat_send_player(name, "You are now free to move.") end + -- Shouldn't find any more entities now that above code is fixed removeEntity(pos) local meta = minetest.get_meta(pos) meta:set_string("infotext","Digilines Game Controller Ready\n(right-click to use)") @@ -214,8 +223,8 @@ minetest.register_entity("digistuff:controller_entity",{ physical = false, collisionbox = {0,0,0,0,0,0,}, textures = {"digistuff_transparent.png",}, - _is_gamecontroller = true, }, + _is_gamecontroller = true, }) local acc_dtime = 0 From e6a2fda39ed94551bda0f1b404afd5c8518ccb8d Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 25 Dec 2024 02:52:43 +0100 Subject: [PATCH 5/9] Add [jumpdrive] support --- controller.lua | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/controller.lua b/controller.lua index e3aa2ea..2c38bde 100644 --- a/controller.lua +++ b/controller.lua @@ -197,6 +197,43 @@ minetest.register_node("digistuff:controller_programmed", { toggle_trap_player(pos,clicker) end end, + on_movenode = function(from_pos, to_pos) + local hashed_from_pos = core.hash_node_position(from_pos) + local hashed_to_pos = core.hash_node_position(to_pos) + local name = players_on_controller[hashed_from_pos] + if not name then + -- No player attached to this controller. + return + end + + local cleanup = false + local player = core.get_player_by_name(name) + if not player then + -- Player has logged off -> cleanup + cleanup = true + end + local parent = player and player:get_attach() + local lua_entity = parent and parent:get_luaentity() + if not (lua_entity and lua_entity._is_gamecontroller) then + -- Player is not attached or failed to get lua entity + -- or player is now attached to some other entity -> cleanup + cleanup = true + end + if cleanup then + removeEntity(from_pos) + players_on_controller[hashed_from_pos] = nil + last_seen_inputs[name] = nil + return + end + + -- Move entity to new location -> player moves along + -- Jumpdrive will then also attempt to move player and + -- delete entity at from_pos. + parent:set_pos(to_pos) + -- Update cache to new position + players_on_controller[hashed_to_pos] = name + players_on_controller[hashed_from_pos] = nil + end, digiline = { receptor = {}, wire = { From b6cf1979acc35c5bae14eb72c38832ca79a623cb Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 25 Dec 2024 02:57:19 +0100 Subject: [PATCH 6/9] compact jd-compat code a bit --- controller.lua | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/controller.lua b/controller.lua index 2c38bde..bc2eab2 100644 --- a/controller.lua +++ b/controller.lua @@ -206,20 +206,13 @@ minetest.register_node("digistuff:controller_programmed", { return end - local cleanup = false local player = core.get_player_by_name(name) - if not player then - -- Player has logged off -> cleanup - cleanup = true - end local parent = player and player:get_attach() local lua_entity = parent and parent:get_luaentity() if not (lua_entity and lua_entity._is_gamecontroller) then + -- Player has logged off -> cleanup -- Player is not attached or failed to get lua entity -- or player is now attached to some other entity -> cleanup - cleanup = true - end - if cleanup then removeEntity(from_pos) players_on_controller[hashed_from_pos] = nil last_seen_inputs[name] = nil From 031f5b066143ffb491cf2bd326f7e8a741e5876a Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 25 Dec 2024 09:07:55 +0100 Subject: [PATCH 7/9] tweaking .luacheckrc --- .luacheckrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.luacheckrc b/.luacheckrc index 45ff7f0..cc3546c 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,6 +1,6 @@ unused_args = false max_line_length = 300 -- TODO: fix line lengths -std = "luanti+max" +std = "minetest+max" globals = { "digistuff", From 5e59e2ade5a7c07622c52852827115fb070c922c Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 25 Dec 2024 13:22:45 +0100 Subject: [PATCH 8/9] tweaking luacheckrc --- .luacheckrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.luacheckrc b/.luacheckrc index cc3546c..45ff7f0 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,6 +1,6 @@ unused_args = false max_line_length = 300 -- TODO: fix line lengths -std = "minetest+max" +std = "luanti+max" globals = { "digistuff", From b0c5cfe918242e65cf6326d95895abbde5f20a08 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 25 Dec 2024 15:45:27 +0100 Subject: [PATCH 9/9] revert .luacheckrc for now until github has updated all images and starts supporting the luanti standard --- .luacheckrc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.luacheckrc b/.luacheckrc index 45ff7f0..30d25c6 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,13 +1,22 @@ unused_args = false max_line_length = 300 -- TODO: fix line lengths -std = "luanti+max" +--std = "luanti+max" globals = { + "minetest", "digistuff", "digilines", } read_globals = { + -- Builtin + "core", + table = {fields = {"copy"}}, + + "vector", + "ItemStack", + "DIR_DELIM", + -- Mod Deps "default", "mesecon",