From 0d1f1fde9ddfc12ea03e34f5be782ea45030520f Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 10 Nov 2024 07:36:02 -0600 Subject: [PATCH] Change cart punching to impulse on velocity instead of acceleration, make punch impulse larger --- mods/ENTITIES/mcl_minecarts/carts.lua | 15 ++++++--- mods/ENTITIES/mcl_minecarts/movement.lua | 41 +++++++++++++----------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/carts.lua b/mods/ENTITIES/mcl_minecarts/carts.lua index 39770ebc5..61cff7bb8 100644 --- a/mods/ENTITIES/mcl_minecarts/carts.lua +++ b/mods/ENTITIES/mcl_minecarts/carts.lua @@ -266,14 +266,21 @@ function DEFAULT_CART_DEF:on_punch(puncher, time_from_last_punch, tool_capabilit end local controls = staticdata.controls or {} - local impulse = vector.multiply(dir, damage * 20) - local accel = vector.dot(staticdata.dir, impulse) - if accel < 0 and staticdata.velocity == 0 then + dir.y = 0 + dir = vector.normalize(dir) + local impulse = vector.dot(staticdata.dir, vector.multiply(dir, damage * 4)) + minetest.log(dump({ + dir = dir, + dir_len = vector.length(dir), + damage = damage, + impulse = impulse, + })) + if impulse < 0 and staticdata.velocity == 0 then mod.reverse_direction(staticdata) + impulse = -impulse end controls.impulse = impulse - --print("uuid="..self._uuid..", controls="..dump(controls)) staticdata.controls = controls end function DEFAULT_CART_DEF:on_step(dtime) diff --git a/mods/ENTITIES/mcl_minecarts/movement.lua b/mods/ENTITIES/mcl_minecarts/movement.lua index f7f1fd817..44ab1c535 100644 --- a/mods/ENTITIES/mcl_minecarts/movement.lua +++ b/mods/ENTITIES/mcl_minecarts/movement.lua @@ -286,9 +286,6 @@ local function calculate_acceleration(staticdata) acceleration = 4 elseif (ctrl.brake or 0) > time_active then acceleration = -1.5 - elseif ctrl.impulse then - acceleration = vector.dot(staticdata.dir, ctrl.impulse) - ctrl.impulse = nil elseif (staticdata.fueltime or 0) > 0 and staticdata.velocity <= 4 then acceleration = 0.6 elseif staticdata.velocity >= ( node_def._max_acceleration_velocity or SPEED_MAX ) then @@ -320,8 +317,24 @@ local function do_movement_step(staticdata, dtime) local x_0 = staticdata.distance or 0 local remaining_in_block = 1 - x_0 - -- Calculate acceleration + -- Apply velocity impulse local v_0 = staticdata.velocity or 0 + local ctrl = staticdata.controls or {} + if ctrl.impulse then + local impulse = ctrl.impulse + ctrl.impulse = nil + + local old_v_0 = v_0 + local new_v_0 = v_0 + impulse + if new_v_0 > SPEED_MAX then + new_v_0 = SPEED_MAX + elseif new_v_0 < 0.025 then + new_v_0 = 0 + end + v_0 = new_v_0 + end + + -- Calculate acceleration local a = 0 if staticdata.ahead or staticdata.behind then -- Calculate acceleration of the entire train @@ -385,18 +398,17 @@ local function do_movement_step(staticdata, dtime) end -- Truncate timestep to prevent v_1 from being larger that speed_max - local v_max = SPEED_MAX - if (v_0 < v_max) and ( v_0 + a * timestep > v_max) then - timestep = ( v_max - v_0 ) / a + if (v_0 < SPEED_MAX) and ( v_0 + a * timestep > SPEED_MAX) then + timestep = ( SPEED_MAX - v_0 ) / a end -- Prevent infinite loops if timestep <= 0 then return 0 end - -- Calculate v_1 taking v_max into account + -- Calculate v_1 taking SPEED_MAX into account local v_1 = v_0 + a * timestep - if v_1 > v_max then - v_1 = v_max + if v_1 > SPEED_MAX then + v_1 = SPEED_MAX elseif v_1 < 0.025 then v_1 = 0 end @@ -496,15 +508,6 @@ end function submod.do_movement( staticdata, dtime ) assert(staticdata) - -- Allow the carts to be delay for the rest of the world to react before moving again - --[[ - if ( staticdata.delay or 0 ) > dtime then - staticdata.delay = staticdata.delay - dtime - return - else - staticdata.delay = 0 - end]] - -- Break long movements at block boundaries to make it -- it impossible to jump across gaps due to server lag -- causing large timesteps