aboutsummaryrefslogtreecommitdiff
path: root/advtrains/trainlogic.lua
diff options
context:
space:
mode:
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>2020-07-26 21:07:47 +0200
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>2020-07-26 23:30:07 +0200
commitc54e0b341a88349e1a4fc7d902fdd9d9e1a6040d (patch)
tree356497a3b91b0f2a201984923b0bcae2acebe3be /advtrains/trainlogic.lua
parentde8add139c44001700bd656f82c22d62480e6e8c (diff)
downloadadvtrains-c54e0b341a88349e1a4fc7d902fdd9d9e1a6040d.tar.gz
advtrains-c54e0b341a88349e1a4fc7d902fdd9d9e1a6040d.tar.bz2
advtrains-c54e0b341a88349e1a4fc7d902fdd9d9e1a6040d.zip
add split_train_at_fc luaatc command
This command splits the train at the second non-empty FC it encounters, taking with it the first few freight cars that all go to the same destination.
Diffstat (limited to 'advtrains/trainlogic.lua')
-rw-r--r--advtrains/trainlogic.lua35
1 files changed, 34 insertions, 1 deletions
diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua
index 3e57db2..1bda0f2 100644
--- a/advtrains/trainlogic.lua
+++ b/advtrains/trainlogic.lua
@@ -892,6 +892,36 @@ function advtrains.spawn_wagons(train_id)
end
end
+function advtrains.split_train_at_fc(train)
+ -- splits train at first different current FC
+ local train_id = train.id
+ local fc = false
+ local ind = 0
+ for i = 1, #train.trainparts do
+ local w_id = train.trainparts[i]
+ local data = advtrains.wagons[w_id]
+ if data then
+ local wfc = advtrains.get_cur_fc(data)
+ if wfc ~= "" then
+ if fc then
+ if fc ~= wfc then
+ ind = i
+ break
+ end
+ else
+ fc = wfc
+ end
+ end
+ end
+ end
+ if ind > 0 then
+ return advtrains.split_train_at_index(train, ind), fc
+ end
+ if fc then
+ return nil, fc
+ end
+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.
@@ -1080,7 +1110,10 @@ function advtrains.do_connect_trains(first_id, second_id, vel)
end
advtrains.remove_train(second_id)
-
+ if vel < 0 then
+ advtrains.invert_train(first_id)
+ vel = -vel
+ end
first.velocity= vel or 0
advtrains.update_trainpart_properties(first_id)