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)) return vector.add(data.connected_at, vector.multiply(data.dir or vector.zero(), data.distance or 0))
end 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 -- Imports
local train_length = mod.train_length local train_length = mod.train_length
local update_train = mod.update_train local update_train = mod.update_train
local reverse_train = mod.reverse_train
local link_cart_ahead = mod.link_cart_ahead local link_cart_ahead = mod.link_cart_ahead
local update_cart_orientation = mod.update_cart_orientation local update_cart_orientation = mod.update_cart_orientation
local get_cart_data = mod.get_cart_data local get_cart_data = mod.get_cart_data
@ -248,14 +249,12 @@ local function calculate_acceleration(self, staticdata)
end end
local function reverse_direction(self, staticdata) 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 if staticdata.behind or staticdata.ahead then
local next_dir = -staticdata.dir reverse_train(self)
staticdata.connected_at = staticdata.connected_at + staticdata.dir return
staticdata.distance = 1 - (staticdata.distance or 0) end
-- recalculate direction mod.reverse_cart_direction(staticdata)
local next_dir,_ = mcl_minecarts:get_rail_direction(staticdata.connected_at, next_dir, nil, nil, staticdata.railtype)
staticdata.dir = next_dir
end end
local function do_movement_step(self, dtime) 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 staticdata.ahead = ca_staticdata.uuid
ca_staticdata.behind = staticdata.uuid ca_staticdata.behind = staticdata.uuid
end 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