diff options
Diffstat (limited to 'advtrains/trainlogic.lua')
-rw-r--r-- | advtrains/trainlogic.lua | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 00c04bf..35c3726 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 @@ -387,7 +392,7 @@ function advtrains.train_step_b(id, train, dtime) -- interlocking speed restriction elseif train.speed_restriction then --atprint("in train_step_b: applying interlocking speed restriction",train.speed_restriction) - sit_v_cap = train.speed_restriction + sit_v_cap = math.min(sit_v_cap or math.huge, train.speed_restriction) end --apply off-track handling: @@ -418,9 +423,11 @@ function advtrains.train_step_b(id, train, dtime) ctrl_lever = userc else if train.atc_command then - if (not train.atc_delay or train.atc_delay<=0) and not train.atc_wait_finish then + if (not train.atc_delay or train.atc_delay<=0) + and not train.atc_wait_finish + and not train.atc_wait_autocouple then advtrains.atc.execute_atc_command(id, train) - else + elseif train.atc_delay and train.atc_delay > 0 then train.atc_delay=train.atc_delay-dtime end elseif train.atc_delay then @@ -641,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 = { @@ -711,12 +719,15 @@ function advtrains.train_step_c(id, train, dtime) if train.ontrack_collision_info then train.velocity = 0 train.acceleration = 0 - advtrains.atc.train_reset_command(train) + --advtrains.atc.train_reset_command(train) will occur in couple_initiate_with if required local otrn = advtrains.trains[train.ontrack_collision_info.otid] if otrn.velocity == 0 then -- other train must be standing, else don't initiate coupling advtrains.couple_initiate_with(train, otrn, not train.ontrack_collision_info.same_dir) + else + -- other collision - stop any ATC control + advtrains.atc.train_reset_command(train) end train.ontrack_collision_info = nil @@ -1108,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 @@ -1130,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 @@ -1139,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 @@ -1153,6 +1168,7 @@ function advtrains.split_train_at_index(train, index) newtrain.line = train.line newtrain.routingcode = train.routingcode newtrain.speed_restriction = train.speed_restriction + newtrain.speed_restrictions_t = table.copy(train.speed_restrictions_t or {main=train.speed_restriction}) newtrain.is_shunt = train.is_shunt newtrain.points_split = advtrains.merge_tables(train.points_split) newtrain.autocouple = train.autocouple @@ -1195,10 +1211,10 @@ function advtrains.invert_train(train_id) -- If interlocking present, check whether this train is in a section and then set as shunt move after reversion if advtrains.interlocking and train.il_sections and #train.il_sections > 0 then train.is_shunt = true - train.speed_restriction = advtrains.SHUNT_SPEED_MAX + advtrains.speed.set_restriction(train, "main", advtrains.SHUNT_SPEED_MAX) else train.is_shunt = false - train.speed_restriction = nil + advtrains.speed.set_restriction(train, "main", -1) end end |