Implement train reversing

This commit is contained in:
teknomunk 2024-04-08 21:37:54 +00:00
parent a48c58a244
commit e59a89eb2a
3 changed files with 27 additions and 7 deletions

View file

@ -364,3 +364,15 @@ function mod.get_cart_position(cart_staticdata)
return vector.add(data.connected_at, vector.multiply(data.dir or vector.zero(), data.distance or 0))
end
function mod.reverse_cart_direction(staticdata)
-- Complete moving thru this block into the next, reverse direction, and put us back at the same position we were at
local next_dir = -staticdata.dir
staticdata.connected_at = staticdata.connected_at + staticdata.dir
staticdata.distance = 1 - (staticdata.distance or 0)
-- recalculate direction
local next_dir,_ = mod:get_rail_direction(staticdata.connected_at, next_dir, nil, nil, staticdata.railtype)
staticdata.dir = next_dir
end

View file

@ -11,6 +11,7 @@ local MAX_TRAIN_LENGTH = mod.MAX_TRAIN_LENGTH
-- Imports
local train_length = mod.train_length
local update_train = mod.update_train
local reverse_train = mod.reverse_train
local link_cart_ahead = mod.link_cart_ahead
local update_cart_orientation = mod.update_cart_orientation
local get_cart_data = mod.get_cart_data
@ -248,14 +249,12 @@ local function calculate_acceleration(self, staticdata)
end
local function reverse_direction(self, staticdata)
-- Complete moving thru this block into the next, reverse direction, and put us back at the same position we were at
local next_dir = -staticdata.dir
staticdata.connected_at = staticdata.connected_at + staticdata.dir
staticdata.distance = 1 - (staticdata.distance or 0)
if staticdata.behind or staticdata.ahead then
reverse_train(self)
return
end
-- recalculate direction
local next_dir,_ = mcl_minecarts:get_rail_direction(staticdata.connected_at, next_dir, nil, nil, staticdata.railtype)
staticdata.dir = next_dir
mod.reverse_cart_direction(staticdata)
end
local function do_movement_step(self, dtime)

View file

@ -114,3 +114,12 @@ function mod.link_cart_ahead(cart, cart_ahead)
staticdata.ahead = ca_staticdata.uuid
ca_staticdata.behind = staticdata.uuid
end
function mod.reverse_train(cart)
for c in train_cars(cart) do
mod.reverse_cart_direction(c)
c.behind,c.ahead = c.ahead,c.behind
end
end