From b97c5908ec9c580366734821b1f025d45e4f095d Mon Sep 17 00:00:00 2001 From: teknomunk Date: Tue, 9 Apr 2024 08:14:30 +0000 Subject: [PATCH] Fix undefined global warning, move player off to the side of a cart when dismounting so trains don't get pushed apart when getting out --- mods/ENTITIES/mcl_minecarts/carts.lua | 16 +++++++++++++++- mods/ENTITIES/mcl_minecarts/carts/minecart.lua | 6 +++++- mods/ENTITIES/mcl_minecarts/init.lua | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/carts.lua b/mods/ENTITIES/mcl_minecarts/carts.lua index 36c6d30c2..b608e1708 100644 --- a/mods/ENTITIES/mcl_minecarts/carts.lua +++ b/mods/ENTITIES/mcl_minecarts/carts.lua @@ -21,9 +21,10 @@ assert(handle_cart_enter) -- Constants local max_step_distance = 0.5 local MINECART_MAX_HP = 4 -local PASSENGER_ATTACH_POSITION = vector.new(0, -1.75, 0) local function detach_driver(self) + local staticdata = self._staticdata + if not self._driver then return end @@ -45,9 +46,22 @@ local function detach_driver(self) -- Detatch the player object from the minecart local player = minetest.get_player_by_name(driver_name) if player then + local dir = staticdata.dir or vector.new(1,0,0) + local cart_pos = mod.get_cart_position(staticdata) or self.object:get_pos() + local new_pos = vector.offset(cart_pos, -dir.z, 0, dir.x) player:set_detach() + print("placing player at "..tostring(new_pos).." from cart at "..tostring(cart_pos)..", old_pos="..tostring(player:get_pos()).."dir="..tostring(dir)) + + -- There needs to be a delay here or the player's position won't update + minetest.after(0.1,function(driver_name,new_pos) + local player = minetest.get_player_by_name(driver_name) + player:moveto(new_pos, false) + end, driver_name, new_pos) + player:set_eye_offset(vector.new(0,0,0),vector.new(0,0,0)) mcl_player.player_set_animation(player, "stand" , 30) + else + print("No player object found for "..driver_name) end end diff --git a/mods/ENTITIES/mcl_minecarts/carts/minecart.lua b/mods/ENTITIES/mcl_minecarts/carts/minecart.lua index b711155b3..bf3fce514 100644 --- a/mods/ENTITIES/mcl_minecarts/carts/minecart.lua +++ b/mods/ENTITIES/mcl_minecarts/carts/minecart.lua @@ -1,6 +1,10 @@ local modname = minetest.get_current_modname() local S = minetest.get_translator(modname) local mcl_log = mcl_util.make_mcl_logger("mcl_logging_minecarts", "Minecarts") +local mod = mcl_minecarts + +-- Imports +local PASSENGER_ATTACH_POSITION = mod.PASSENGER_ATTACH_POSITION local function activate_normal_minecart(self) detach_driver(self) @@ -12,7 +16,7 @@ local function activate_normal_minecart(self) end end -mcl_minecarts.register_minecart({ +mod.register_minecart({ itemstring = "mcl_minecarts:minecart", craft = { output = "mcl_minecarts:minecart", diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index 55f348544..6a6f93318 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -10,6 +10,7 @@ mod.check_float_time = 15 mod.FRICTION = 0.4 mod.MAX_TRAIN_LENGTH = 4 mod.CART_BLOCK_SIZE = 64 +mod.PASSENGER_ATTACH_POSITION = vector.new(0, -1.75, 0) for _,filename in pairs({"storage","functions","rails","train","carts"}) do dofile(modpath.."/"..filename..".lua")