Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game controller [jumpdrive] compat #64

Merged
merged 9 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
unused_args = false
max_line_length = 300 -- TODO: fix line lengths
--std = "luanti+max"

globals = {
"minetest",
Expand All @@ -9,7 +10,9 @@ globals = {

read_globals = {
-- Builtin
"table.copy",
"core",
table = {fields = {"copy"}},

"vector",
"ItemStack",
"DIR_DELIM",
Expand Down
68 changes: 55 additions & 13 deletions controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,25 +32,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()
Expand All @@ -71,18 +78,22 @@ end

local function release_player(pos)
local hash = minetest.hash_node_position(pos)
local player = minetest.get_player_by_name(players_on_controller[hash])
if player and player:get_properties()._is_gamecontroller then
local name = players_on_controller[hash]
local player = minetest.get_player_by_name(name)
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(players_on_controller[hash],"You are now free to move.")
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)")
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
Expand All @@ -92,7 +103,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
Expand Down Expand Up @@ -185,6 +197,36 @@ 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 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 = {
receptor = {},
wire = {
Expand All @@ -211,8 +253,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
Expand Down
Loading