From 388e63da7ea9ff95f834a717b3366e2891c764c4 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Tue, 23 Apr 2024 16:34:39 +0000 Subject: [PATCH] Stop carts from reversing when they stop, make stopped carts try to start moving in the direction the player is facing --- mods/ENTITIES/mcl_minecarts/carts.lua | 2 ++ mods/ENTITIES/mcl_minecarts/movement.lua | 28 +++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/carts.lua b/mods/ENTITIES/mcl_minecarts/carts.lua index 6e4b9a528..ed19690d7 100644 --- a/mods/ENTITIES/mcl_minecarts/carts.lua +++ b/mods/ENTITIES/mcl_minecarts/carts.lua @@ -21,6 +21,7 @@ assert(handle_cart_enter) -- Constants local max_step_distance = 0.5 local MINECART_MAX_HP = 4 +local TWO_OVER_PI = 2 / math.pi local function detach_driver(self) local staticdata = self._staticdata @@ -244,6 +245,7 @@ function DEFAULT_CART_DEF:on_step(dtime) local controls = {} if ctrl.up then controls.forward = now_time end if ctrl.down then controls.brake = now_time end + controls.look = math.round(player:get_look_horizontal() * TWO_OVER_PI) % 4 staticdata.controls = controls end diff --git a/mods/ENTITIES/mcl_minecarts/movement.lua b/mods/ENTITIES/mcl_minecarts/movement.lua index 2499bddc4..e4e682508 100644 --- a/mods/ENTITIES/mcl_minecarts/movement.lua +++ b/mods/ENTITIES/mcl_minecarts/movement.lua @@ -38,6 +38,16 @@ local function try_detach_minecart(staticdata) end end +local function reverse_direction(staticdata) + if staticdata.behind or staticdata.ahead then + reverse_train(staticdata) + return + end + + mod.reverse_cart_direction(staticdata) +end + + --[[ Array of hooks { {u,v,w}, name } Actual position is pos + u * dir + v * right + w * up @@ -248,6 +258,13 @@ local function calculate_acceleration(staticdata) local time_active = minetest.get_gametime() - 0.25 if (ctrl.forward or 0) > time_active then + if staticdata.velocity == 0 then + local look_dir = minetest.facedir_to_dir(ctrl.look or 0) + local dot = vector.dot(staticdata.dir, look_dir) + if dot < 0 then + reverse_direction(staticdata) + end + end acceleration = 4 elseif (ctrl.brake or 0) > time_active then acceleration = -1.5 @@ -270,15 +287,6 @@ local function calculate_acceleration(staticdata) return acceleration end -local function reverse_direction(staticdata) - if staticdata.behind or staticdata.ahead then - reverse_train(staticdata) - return - end - - mod.reverse_cart_direction(staticdata) -end - local function do_movement_step(staticdata, dtime) if not staticdata.connected_at then return 0 end @@ -405,7 +413,7 @@ local function do_movement_step(staticdata, dtime) -- Update cart direction staticdata.dir = next_dir - elseif stops_in_block and v_1 < (FRICTION/5) and a <= 0 then + elseif stops_in_block and v_1 < (FRICTION/5) and a <= 0 and staticdata.dir.y > 0 then -- Handle direction flip due to gravity if DEBUG then mcl_debug("Gravity flipped direction") end