aboutsummaryrefslogtreecommitdiff
path: root/advtrains/couple.lua
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains/couple.lua')
-rw-r--r--advtrains/couple.lua15
1 files changed, 13 insertions, 2 deletions
diff --git a/advtrains/couple.lua b/advtrains/couple.lua
index c421f61..b6a445e 100644
--- a/advtrains/couple.lua
+++ b/advtrains/couple.lua
@@ -79,8 +79,9 @@ function advtrains.train_check_couples(train)
end
if not train.cpl_front then
-- recheck front couple
- local front_trains, pos = advtrains.occ.get_occupations(train, atround(train.index) + CPL_CHK_DST)
+ local pos = advtrains.path_get(train, atround(train.index) + CPL_CHK_DST)
if advtrains.is_node_loaded(pos) then -- if the position is loaded...
+ local front_trains = advtrains.occ.reverse_lookup_sel(pos, "in_train")
for tid, idx in pairs(front_trains) do
local other_train = advtrains.trains[tid]
if not advtrains.train_ensure_init(tid, other_train) then
@@ -109,8 +110,9 @@ function advtrains.train_check_couples(train)
end
if not train.cpl_back then
-- recheck back couple
- local back_trains, pos = advtrains.occ.get_occupations(train, atround(train.end_index) - CPL_CHK_DST)
+ local pos = advtrains.path_get(train, atround(train.end_index) - CPL_CHK_DST)
if advtrains.is_node_loaded(pos) then -- if the position is loaded...
+ local back_trains = advtrains.occ.reverse_lookup_sel(pos, "in_train")
for tid, idx in pairs(back_trains) do
local other_train = advtrains.trains[tid]
if not advtrains.train_ensure_init(tid, other_train) then
@@ -225,6 +227,13 @@ function advtrains.couple_trains(init_train, invert_init_train, stat_train, stat
local stp = stat_train.trainparts
local stat_wagoncnt = #stp
local stat_trainlen = stat_train.trainlen -- save the train length of stat train, to be added to index
+
+ -- sanity check, prevent coupling if train would be longer than 20 after coupling
+ local tot_len = init_wagoncnt + stat_wagoncnt
+ if tot_len > advtrains.TRAIN_MAX_WAGONS then
+ atwarn("Cannot couple",stat_train.id,"and",init_train.id,"- train would have length",tot_len,"which is above the limit of",advtrains.TRAIN_MAX_WAGONS)
+ return
+ end
if stat_train_opposite then
-- insert wagons in inverse order and set their wagon_flipped state
@@ -252,6 +261,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