add maximum length parameter to split_at_fc and split_off_locomotive

master
Gabriel Pérez-Cerezo 2020-08-21 15:58:12 +02:00
parent 36d8c8b716
commit b23d346ace
3 changed files with 30 additions and 8 deletions

View File

@ -892,16 +892,21 @@ function advtrains.spawn_wagons(train_id)
end
end
function advtrains.split_train_at_fc(train, count_empty)
function advtrains.split_train_at_fc(train, count_empty, length_limit)
-- splits train at first different current FC by convention,
-- locomotives have empty FC so are ignored
-- count_empty is used to split off locomotives
-- length_limit limits the length of the first train to length_limit wagons
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 length_limit and i > length_limit then
ind = i
break
end
if data then
local wfc = advtrains.get_cur_fc(data)
if wfc ~= "" or count_empty then

View File

@ -203,21 +203,38 @@ set_rc(routingcode)
split_at_index(index, command)
Splits the train at the specified index, into a train with index-1 wagons and a second train starting with the index-th wagon.
command specifies an atc command to be sent to the second train after decoupling.
split_at_fc(command)
split_at_fc(command, len)
Splits the train in such a way that all cars with non-empty
current FC of the first part of the train have the same FC. The
command specified is sent to the rear part, as with
split_at_index. It returns the fc of the cars of the first part.
The optional argument len specifies the maximum length for the
first part of the train. Say, we have len=3, and the train has ""
"" "foo" "foo" "foo" "bar", then the first train part will be ""
"" "foo".
Example : Train has current FCs "" "" "foo" "bar" "foo"
Result: first train: "" "" "foo"; second train: "bar" "foo"
The command returns "foo" in this case
split_off_locomotive(command)
split_off_locomotive(command, len)
Splits off the locomotives at the front of the train, which are
identified by an empty FC. command specifies the command to be
executed by the rear half of the train.
The optional argument len specifies the maximum length for the
first part of the train. Say, we have len=3, and the train has ""
"" "foo" "foo" "foo" "bar", then the first train part will be ""
"" "foo".
step_fc()
Steps the FCs of all train cars forward
Steps the FCs of all train cars forward. FCs are composed of codes
separated by exclamation marks (!), for instance
"foo!bar!baz". Each wagon has a current FC, indicating its next
destination. Stepping the freight code forward, selects the next
code after the !. If the end of the string is reached, then the
first code is selected, except if the string ends with a question
mark, then the order is reversed.
train_length()
returns the number of cars the train is composed of
set_autocouple()

View File

@ -66,19 +66,19 @@ function r.fire_event(pos, evtdata)
end
return false
end,
split_at_fc = function(cmd)
split_at_fc = function(cmd, len)
assertt(cmd, "string")
if not train_id then return false end
local new_id, fc = advtrains.split_train_at_fc(train)
local new_id, fc = advtrains.split_train_at_fc(train, false, len)
if new_id then
minetest.after(1,advtrains.atc.train_set_command,advtrains.trains[new_id], cmd, atc_arrow)
end
return fc or ""
end,
split_off_locomotive = function(cmd)
split_off_locomotive = function(cmd, len)
assertt(cmd, "string")
if not train_id then return false end
local new_id, fc = advtrains.split_train_at_fc(train, true)
local new_id, fc = advtrains.split_train_at_fc(train, true, len)
if new_id then
minetest.after(1,advtrains.atc.train_set_command,advtrains.trains[new_id], cmd, atc_arrow)
end