Game controller [jumpdrive] compat (#64)

This commit is contained in:
Luke aka SwissalpS 2024-12-31 19:43:11 +01:00 committed by GitHub
parent 4763cb9805
commit 422dfdb2cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 14 deletions

View File

@ -1,5 +1,6 @@
unused_args = false unused_args = false
max_line_length = 300 -- TODO: fix line lengths max_line_length = 300 -- TODO: fix line lengths
--std = "luanti+max"
globals = { globals = {
"minetest", "minetest",
@ -9,7 +10,9 @@ globals = {
read_globals = { read_globals = {
-- Builtin -- Builtin
"table.copy", "core",
table = {fields = {"copy"}},
"vector", "vector",
"ItemStack", "ItemStack",
"DIR_DELIM", "DIR_DELIM",

View File

@ -14,6 +14,12 @@ local players_on_controller = {}
local last_seen_inputs = {} 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 function removeEntity(pos)
local entitiesNearby = minetest.get_objects_inside_radius(pos,0.5) local entitiesNearby = minetest.get_objects_inside_radius(pos,0.5)
for _,i in pairs(entitiesNearby) do for _,i in pairs(entitiesNearby) do
@ -26,25 +32,26 @@ end
local function process_inputs(pos) local function process_inputs(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local hash = minetest.hash_node_position(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 if minetest.get_node(pos).name ~= "digistuff:controller_programmed" then
local player = minetest.get_player_by_name(players_on_controller[hash])
if player then if player then
player:set_physics_override({speed = 1,jump = 1,}) player:set_physics_override({speed = 1,jump = 1,})
player:set_pos(vector.add(pos,vector.new(0.25,0,0.25))) 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 end
last_seen_inputs[players_on_controller[hash]] = nil last_seen_inputs[name] = nil
players_on_controller[hash] = nil players_on_controller[hash] = nil
return return
end end
local name = players_on_controller[hash]
local player = minetest.get_player_by_name(name)
if not player then if not player then
digilines.receptor_send(pos,digiline_rules,meta:get_string("channel"),"player_left") 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)") minetest.get_meta(pos):set_string("infotext","Digilines Game Controller Ready\n(right-click to use)")
players_on_controller[hash] = nil players_on_controller[hash] = nil
return return
end end
local inputs = player:get_player_control() local inputs = player:get_player_control()
inputs.pitch = player:get_look_vertical() inputs.pitch = player:get_look_vertical()
inputs.yaw = player:get_look_horizontal() inputs.yaw = player:get_look_horizontal()
@ -71,18 +78,22 @@ end
local function release_player(pos) local function release_player(pos)
local hash = minetest.hash_node_position(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]
if player and player:get_properties()._is_gamecontroller then local player = minetest.get_player_by_name(name)
if player then
local parent = player:get_attach() local parent = player:get_attach()
if parent then local lua_entity = parent and parent:get_luaentity()
player:set_detach() if lua_entity and lua_entity._is_gamecontroller then
-- Remove also detaches
parent:remove()
end 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 end
-- Shouldn't find any more entities now that above code is fixed
removeEntity(pos) removeEntity(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("infotext","Digilines Game Controller Ready\n(right-click to use)") 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 players_on_controller[hash] = nil
digilines.receptor_send(pos,digiline_rules,meta:get_string("channel"),"player_left") digilines.receptor_send(pos,digiline_rules,meta:get_string("channel"),"player_left")
end end
@ -92,7 +103,8 @@ local function trap_player(pos,player)
local oldname = players_on_controller[hash] local oldname = players_on_controller[hash]
local newname = player:get_player_name() local newname = player:get_player_name()
if oldname and minetest.get_player_by_name(oldname) then 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 return
else else
players_on_controller[hash] = newname players_on_controller[hash] = newname
@ -185,6 +197,36 @@ minetest.register_node("digistuff:controller_programmed", {
toggle_trap_player(pos,clicker) toggle_trap_player(pos,clicker)
end end
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 player = core.get_player_by_name(name)
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
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 = { digiline = {
receptor = {}, receptor = {},
wire = { wire = {
@ -211,8 +253,8 @@ minetest.register_entity("digistuff:controller_entity",{
physical = false, physical = false,
collisionbox = {0,0,0,0,0,0,}, collisionbox = {0,0,0,0,0,0,},
textures = {"digistuff_transparent.png",}, textures = {"digistuff_transparent.png",},
_is_gamecontroller = true,
}, },
_is_gamecontroller = true,
}) })
local acc_dtime = 0 local acc_dtime = 0