From d3b4a579c60cb6cb16e920e98ba79448bf5a9b8b Mon Sep 17 00:00:00 2001 From: teknomunk Date: Thu, 12 Sep 2024 08:28:34 -0500 Subject: [PATCH] Calculate acceleration of trains based on average of acceleration for all carts in the train, make velocity not change on slopes and 45 degree track --- mods/ENTITIES/mcl_minecarts/movement.lua | 19 +++++++++++++++++-- mods/ENTITIES/mcl_minecarts/train.lua | 4 +++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/movement.lua b/mods/ENTITIES/mcl_minecarts/movement.lua index b60fbfe25..e0fb8f22f 100644 --- a/mods/ENTITIES/mcl_minecarts/movement.lua +++ b/mods/ENTITIES/mcl_minecarts/movement.lua @@ -305,8 +305,23 @@ local function do_movement_step(staticdata, dtime) -- Calculate timestep remaiing in this block local x_0 = staticdata.distance or 0 local remaining_in_block = 1 - x_0 - local a = calculate_acceleration(staticdata) + + -- Calculate acceleration local v_0 = staticdata.velocity + local a = 0 + if staticdata.ahead or staticdata.behind then + -- Calculate acceleration of the entire train + local count = 0 + for cart in mod.train_cars(staticdata) do + count = count + 1 + if cart.behind then + a = a + calculate_acceleration(cart) + end + end + a = a / count + else + a = calculate_acceleration(staticdata) + end -- Repel minecarts local away = direction_away_from_players(staticdata) @@ -370,7 +385,7 @@ local function do_movement_step(staticdata, dtime) end -- Calculate x_1 - local x_1 = x_0 + timestep * v_0 + 0.5 * a * timestep * timestep + local x_1 = x_0 + (timestep * v_0 + 0.5 * a * timestep * timestep) / vector.length(staticdata.dir) -- Update position and velocity of the minecart staticdata.velocity = v_1 diff --git a/mods/ENTITIES/mcl_minecarts/train.lua b/mods/ENTITIES/mcl_minecarts/train.lua index aab26fcc9..c21cbb0eb 100644 --- a/mods/ENTITIES/mcl_minecarts/train.lua +++ b/mods/ENTITIES/mcl_minecarts/train.lua @@ -20,7 +20,7 @@ local function find_back(start) end -- Iterate across all the cars in a train -local function train_cars(staticdata) +function mod.train_cars(staticdata) assert(staticdata) local back = find_back(staticdata) @@ -38,6 +38,8 @@ local function train_cars(staticdata) return ret end end +local train_cars = mod.train_cars + function mod.train_length(cart) local count = 0 for cart in train_cars(cart) do