From 8a9f1389a5ee4c53b423b548bf8c7355adb7ec94 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Thu, 4 Nov 2021 20:14:20 +0100 Subject: On-Track Collision system: Fix train teleportation when using split_at_index() from LuaATC Issue was that train thought it would collide with the newly added back portion although it was already long past this train. Added additional check that current train index is not further than the collision point. --- advtrains/couple.lua | 2 ++ advtrains/trainlogic.lua | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'advtrains') diff --git a/advtrains/couple.lua b/advtrains/couple.lua index c421f61..3e6c432 100644 --- a/advtrains/couple.lua +++ b/advtrains/couple.lua @@ -252,6 +252,8 @@ function advtrains.couple_trains(init_train, invert_init_train, stat_train, stat init_train.index = advtrains.path_get_index_by_offset(init_train, init_train.index, stat_trainlen) advtrains.update_trainpart_properties(init_train.id) + advtrains.update_train_start_and_end(init_train) + advtrains.couple_invalidate(init_train) return true end diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 4650f9e..e7f2fd4 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -251,6 +251,11 @@ local callbacks_update, run_callbacks_update = mkcallback("update") local callbacks_create, run_callbacks_create = mkcallback("create") local callbacks_remove, run_callbacks_remove = mkcallback("remove") +-- required to call from couple.lua +function advtrains.update_train_start_and_end(train) + recalc_end_index(train) + run_callbacks_update(train.id, train) +end -- train_ensure_init: responsible for creating a state that we can work on, after one of the following events has happened: -- - the train's path got cleared @@ -643,7 +648,8 @@ function advtrains.train_step_b(id, train, dtime) if target_is_inside then local our_index = advtrains.path_project(otrn, ref_index, id) --atdebug("Backprojected our_index",our_index) - if our_index and our_index <= new_index_curr_tv then + if our_index and our_index <= new_index_curr_tv + and our_index >= train.index then --FIX: If train was already past the collision point in the previous step, there is no collision! Fixes bug with split_at_index -- ON_TRACK COLLISION IS HAPPENING -- the actual collision is handled in train_step_c, so set appropriate signal variables train.ontrack_collision_info = { @@ -1113,6 +1119,7 @@ end function advtrains.split_train_at_index(train, index) -- this function splits a train at index, creating a new train from the back part of the train. + --atdebug("split_train_at_index invoked on",train.id,"index",index) local train_id=train.id if index > #train.trainparts then @@ -1135,6 +1142,7 @@ function advtrains.split_train_at_index(train, index) local p_index=advtrains.path_get_index_by_offset(train, train.index, - data.pos_in_train + wagon.wagon_span) local pos, connid, frac = advtrains.path_getrestore(train, p_index) + --atdebug("new train position p_index",p_index,"pos",pos,"connid",connid,"frac",frac) local tp = {} for k,v in ipairs(train.trainparts) do if k >= index then @@ -1144,12 +1152,14 @@ function advtrains.split_train_at_index(train, index) end advtrains.update_trainpart_properties(train_id) recalc_end_index(train) + --atdebug("old train index",train.index,"end_index",train.end_index) run_callbacks_update(train_id, train) --create subtrain local newtrain_id=advtrains.create_new_train_at(pos, connid, frac, tp) local newtrain=advtrains.trains[newtrain_id] - + --atdebug("new train created with ID",newtrain_id,"index",newtrain.index,"end_index",newtrain.end_index) + newtrain.velocity=train.velocity -- copy various properties from the old to the new train newtrain.door_open = train.door_open -- cgit v1.2.3