From 36d8c8b7162b3b6ba6cc0a7f98b0dc295024ecd8 Mon Sep 17 00:00:00 2001 From: Gabriel PĂ©rez-Cerezo Date: Tue, 28 Jul 2020 20:39:35 +0200 Subject: Add command to split off locomotive --- advtrains/trainlogic.lua | 8 +++++--- advtrains_luaautomation/README.txt | 12 ++++++++---- advtrains_luaautomation/atc_rail.lua | 13 +++++++++---- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 2c2e247..c39cc7a 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -892,8 +892,10 @@ function advtrains.spawn_wagons(train_id) end end -function advtrains.split_train_at_fc(train) - -- splits train at first different current FC +function advtrains.split_train_at_fc(train, count_empty) + -- splits train at first different current FC by convention, + -- locomotives have empty FC so are ignored + -- count_empty is used to split off locomotives local train_id = train.id local fc = false local ind = 0 @@ -902,7 +904,7 @@ function advtrains.split_train_at_fc(train) local data = advtrains.wagons[w_id] if data then local wfc = advtrains.get_cur_fc(data) - if wfc ~= "" then + if wfc ~= "" or count_empty then if fc then if fc ~= wfc then ind = i diff --git a/advtrains_luaautomation/README.txt b/advtrains_luaautomation/README.txt index bf7ac52..a495a8d 100644 --- a/advtrains_luaautomation/README.txt +++ b/advtrains_luaautomation/README.txt @@ -206,12 +206,16 @@ split_at_index(index, command) split_at_fc(command) 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. + command specified is sent to the rear part, as with + split_at_index. It returns the fc of the cars of the first part. - Example: Train has current FCs "" "" "foo" "bar" "boo" "foo" - Result: first train: "" "" "foo"; second train: "bar" "boo" "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) + 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. step_fc() Steps the FCs of all train cars forward train_length() diff --git a/advtrains_luaautomation/atc_rail.lua b/advtrains_luaautomation/atc_rail.lua index 88203ae..758b8f9 100644 --- a/advtrains_luaautomation/atc_rail.lua +++ b/advtrains_luaautomation/atc_rail.lua @@ -73,10 +73,15 @@ function r.fire_event(pos, evtdata) if new_id then minetest.after(1,advtrains.atc.train_set_command,advtrains.trains[new_id], cmd, atc_arrow) end - if fc == "" then - return nil - end - return fc + return fc or "" + end, + split_off_locomotive = function(cmd) + assertt(cmd, "string") + if not train_id then return false end + local new_id, fc = advtrains.split_train_at_fc(train, true) + if new_id then + minetest.after(1,advtrains.atc.train_set_command,advtrains.trains[new_id], cmd, atc_arrow) + end end, train_length = function () if not train_id then return false end -- cgit v1.2.3