--couple.lua--Handles coupling and discoupling of trains, and defines the coupling entities--Rework June 2021 - some functions from trainlogic.lua have been moved here-- COUPLING ---- During coupling rework, the behavior of coupling was changed to make automation easier. It is now as follows:-- Coupling is only ever initiated when a train is standing somewhere (not moving) and another train drives onto one of its ends-- with a speed greater than 0-- "stationary" train is the one standing there - in old code called "train2"-- "initiating" train is the one that approached it and bumped into it - typically an engine - in old code called "train1"-- When the initiating train has autocouple set, trains are immediately coupled-- When not, a couple entity is spawned and coupling commences on click-- Coupling MUST preserve the train ID of the initiating train, so it is done like this:-- index of initiating train is set so that it matches the front pos of stationary train-- remove stationary train-- wagons of stationary train are inserted at the beginning of initiating train (considers direction of stat_train and inserts reverse if required)-- train.couple_* contain references to ObjectRefs of couple objects, which contain all relevant information-- These objectRefs will delete themselves once the couples no longer match (see below)-- Get current translatorlocal S = advtrains.translate
advtrains.coupler_types = {}function advtrains.register_coupler_type(code, name)
advtrains.coupler_types[code] = name
end-- Register some default couplers
advtrains.register_coupler_type("chain",S("Buffer and Chain Coupler"))
advtrains.register_coupler_type("scharfenberg",S("Scharfenberg Coupler"))for _, name in pairs {"couple","decouple"}dolocal t = {}local functionreg(f)table.insert(t, f)endlocal functioncb(...)for _, f inipairs(t)dof(...)endend
advtrains["te_registered_on_".. name] = t
advtrains["te_register_on_".. name] = reg
advtrains["te_run_callbacks_on_".. name] = cb
endlocal functioncreate_couple_entity(pos, train1, t1_is_front, train2, t2_is_front)local id1 = train1.id
local id2 = train2.id
-- delete previous couple entitiesif t1_is_front thenif train1.cpl_front then train1.cpl_front:remove()endelseif train1.cpl_back then train1.cpl_back:remove()endendif t2_is_front thenif train2.cpl_front then train2.cpl_front:remove()endelseif train2.cpl_back then train2.cpl_back:remove()endendlocal obj=minetest.add_entity(pos,"advtrains:couple")if not obj thenerror("Failed creating couple object!")return endlocal le=obj:get_luaentity()
le.train_id_1=id1
le.t1_is_front=t1_is_front
le.train_id_2=id2
le.t2_is_front=t2_is_front
--atdebug("created couple between",train1.id,train2.id,t2_is_front)if t1_is_front then
train1.cpl_front = obj
else
train2.cpl_back = obj
endif t2_is_front then
train2.cpl_front = obj
else
train2.cpl_back = obj
endend-- Old static couple checking. Never used for autocouple, only used for standing trains if train did not approachlocal CPL_CHK_DST = -1local CPL_ZONE =2function advtrains.train_check_couples(train)--atdebug("rechecking couples")if train.cpl_front thenif not train.cpl_front:get_yaw()then-- objectref is no longer valid. reset.
train.cpl_front =nilendendif not train.cpl_front then-- recheck front couplelocal 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 inpairs(front_trains)dolocal other_train = advtrains.trains[tid]if not advtrains.train_ensure_init(tid, other_train)thenatwarn("Train",tid,"is not initialized! Couldn't check couples!")returnend--atdebug(train.id,"front: ",idx,"on",tid,atround(other_train.index),atround(other_train.end_index))if other_train.velocity ==0thenif idx>=other_train.index and idx<=other_train.index + CPL_ZONE thencreate_couple_entity(pos, train,true, other_train,true)breakendif idx<=other_train.end_index and idx>=other_train.end_index - CPL_ZONE thencreate_couple_entity(pos, train,true, other_train,false)breakendendendendendif train.cpl_back thenif not train.cpl_back:get_yaw()then-- objectref is no longer valid. reset.
train.cpl_back =nilendendif not train.cpl_back then-- recheck back couplelocal 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 inpairs(back_trains)dolocal other_train = advtrains.trains[tid]if not advtrains.train_ensure_init(tid, other_train)thenatwarn("Train",tid,"is not initialized! Couldn't check couples!")returnend--atdebug(train.id,"back: ",idx,"on",tid,atround(other_train.index),atround(other_train.end_index))if other_train.velocity ==0thenif idx>=other_train.index and idx<=other_train.index + CPL_ZONE thencreate_couple_entity(pos, train,false, other_train,true)breakendif idx<=other_train.end_index and idx>=other_train.end_index - CPL_ZONE thencreate_couple_entity(pos, train,false, other_train,false)breakendendendendendend-- Deletes couple entities from the trainfunction advtrains.couple_invalidate(train)if train.cpl_back then
train.cpl_back:remove()
train.cpl_back =nilendif train.cpl_front then
train.cpl_front:remove()
train.cpl_front =nilend
train.couples_up_to_date =nilend-- Called from train_step_b() when the current train (init_train) just stopped at one of the end indices of another train (stat_train)-- Depending on autocouple, either couples immediately or spawns a couple entityfunction advtrains.couple_initiate_with(init_train, stat_train, stat_is_front)--atdebug("Couple init autocouple=",init_train.autocouple,"atc_w_acpl=",init_train.atc_wait_autocouple)if init_train.autocouple or init_train.atc_wait_autocouple thenlocal cplmatch, msg = advtrains.check_matching_coupler_types(init_train,true, stat_train, stat_is_front)if cplmatch then
advtrains.couple_trains(init_train,false, stat_train, stat_is_front)-- clear atc couple waiting blocker
init_train.atc_wait_autocouple =nilreturnendend-- get here if either autocouple is not on or couples dont matchlocal pos = advtrains.path_get_interpolated(init_train, init_train.index)create_couple_entity(pos, init_train,true, stat_train, stat_is_front)-- clear ATC command on collision
advtrains.atc.train_reset_command(init_train)end-- check if the player has permission for the first/last wagon of the trainlocal functioncheck_twagon_owner(train, b_first, pname)local wtp = b_first and1or#train.trainparts
local wid = train.trainparts[wtp]local wdata = advtrains.wagons[wid]if wdata thenreturn advtrains.check_driving_couple_protection(pname, wdata.owner, wdata.whitelist)endreturn falseend-- Perform coupling, but check if the player is authorized to couplefunction advtrains.safe_couple_trains(train1, t1_is_front, train2, t2_is_front, pname)if pname and not minetest.check_player_privs(pname,"train_operator")then
minetest.chat_send_player(pname,S("You are not allowed to couple trains without the train_operator privilege."))return falseendlocal wck_t1, wck_t2
if pname then
wck_t1 =check_twagon_owner(train1, t1_is_front, pname)
wck_t2 =check_twagon_owner(train2, t2_is_front, pname)endif(wck_t1 or wck_t2)or not pname thenlocal cplmatch, msg = advtrains.check_matching_coupler_types(train1, t1_is_front, train2, t2_is_front)if cplmatch then
advtrains.couple_trains(train1,not t1_is_front, train2, t2_is_front)else
minetest.chat_send_player(pname, msg)endendend-- Actually performs the train coupling. Always retains train ID of train1function advtrains.couple_trains(init_train, invert_init_train, stat_train, stat_train_opposite)--atdebug("Couple trains init=",init_train.id,"initinv=",invert_init_train,"stat=",stat_train.id,"statreverse=",stat_train_opposite)if not advtrains.train_ensure_init(init_train.id, init_train)thenatwarn("Coupling: initiating train",init_train.id,"is not initialized! Operation aborted!")returnendif not advtrains.train_ensure_init(stat_train.id, stat_train)thenatwarn("Coupling: stationary train",stat_train.id,"is not initialized! Operation aborted!")returnend-- only used with the couple entityif invert_init_train then
advtrains.invert_train(init_train.id)endlocal itp = init_train.trainparts
local init_wagoncnt = #itp
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 couplinglocal tot_len = init_wagoncnt + stat_wagoncnt
if tot_len > advtrains.TRAIN_MAX_WAGONS thenatwarn(S("Cannot couple @1 and @2 - train would have length @3 which is above the limit of @4", stat_train.id, init_train.id, tot_len, advtrains.TRAIN_MAX_WAGONS))returnend
advtrains.te_run_callbacks_on_couple(init_train, stat_train)if stat_train_opposite then-- insert wagons in inverse order and set their wagon_flipped statefor i=1,stat_wagoncnt dotable.insert(itp,1, stp[i])local wdata = advtrains.wagons[stp[i]]
if wdata then
wdata.wagon_flipped =not wdata.wagon_flipped
elseatwarn("While coupling, wagon",stp[i],"of stationary train",stat_train.id,"not found!")endendelse--insert wagons in normal orderfor i=stat_wagoncnt,1,-1dotable.insert(itp,1, stp[i])endend-- TODO: migrate some of the properties from stat_train to init_train?
advtrains.remove_train(stat_train.id)-- Set train index forward
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 trueend-- Couple types matching check-- returns: true, nil if OK-- false, errmsg if there is an errorfunction advtrains.check_matching_coupler_types(t1, t1_front, t2, t2_front)-- 1. get wagonslocal t1_wid
if t1_front then
t1_wid = t1.trainparts[1]else
t1_wid = t1.trainparts[#t1.trainparts]endlocal t2_wid
if t2_front then
t2_wid = t2.trainparts[1]else
t2_wid = t2.trainparts[#t2.trainparts]end--atdebug("CMCT: t1_wid",t1_wid,"t2_wid",t2_wid,"")if not t1_wid or not t2_wid thenreturn false,"Unable to retrieve wagons from train"--note: no translation needed, case should not occurendlocal t1_wagon = advtrains.wagons[t1_wid]local t2_wagon = advtrains.wagons[t2_wid]if not t1_wagon or not t2_wagon thenreturn false,"At least one of wagons "..t1_wagon.." or "..t2_wagon.." does not exist"--note: no translation needed, case should not occurend-- these calls do not fail, they may return placeholder - doesn't matterlocal _,t1_wpro = advtrains.get_wagon_prototype(t1_wagon)local _,t2_wpro = advtrains.get_wagon_prototype(t2_wagon)-- get correct couplers table (front/back)local t1_cplt
if not t1_front ==not t1_wagon.wagon_flipped then--fancy XOR
t1_cplt = t1_wpro.coupler_types_back
else
t1_cplt = t1_wpro.coupler_types_front
endlocal t2_cplt
if not t2_front ==not t2_wagon.wagon_flipped then--fancy XOR
t2_cplt = t2_wpro.coupler_types_back
else
t2_cplt = t2_wpro.coupler_types_front
end--atdebug("CMCT: t1",t1_cplt,"t2",t2_cplt,"")-- if at least one of the trains has no couplers table, it always couples (fallback behavior and mode for universal shunters)if minetest.settings:get_bool("advtrains_universal_couplers",false)or not t1_cplt or not t2_cplt thenreturn trueend-- have common coupler?for typ,_ inpairs(t1_cplt)doif t2_cplt[typ]then--atdebug("CMCT: Matching type",typ)return trueendend--no match, give user an infolocal t1_cplhr, t2_cplhr = {},{}for typ,_ inpairs(t1_cplt)dotable.insert(t1_cplhr, advtrains.coupler_types[typ]or typ)endif#t1_cplhr==0then t1_cplhr[1]=S("<No coupler>")endfor typ,_ inpairs(t2_cplt)dotable.insert(t2_cplhr, advtrains.coupler_types[typ]or typ)endif#t2_cplhr==0then t2_cplhr[1]=S("<No coupler>")endreturn false,S("Can not couple: The couplers of the trains do not match (@1 and @2).",table.concat(t1_cplhr,","),table.concat(t2_cplhr,","))end-- DECOUPLING --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 wagonslocal train_id = train.id
local fc =falselocal ind =0for i =1, #train.trainparts dolocal w_id = train.trainparts[i]local data = advtrains.wagons[w_id]if length_limit and i > length_limit then
ind = i
breakendif data thenlocal wfc = advtrains.get_cur_fc(data)if wfc ~=""or count_empty thenif fc thenif fc ~= wfc then
ind = i
breakendelse
fc = wfc
endendendendif ind >0thenreturn advtrains.split_train_at_index(train, ind), fc
endif fc thenreturn nil, fc
endendfunction advtrains.train_step_fc(train)for i=1,#train.trainparts dolocal w_id = train.trainparts[i]local data = advtrains.wagons[w_id]if data then
advtrains.step_fc(data)endendend-- split_train_at_index() is in trainlogic.lua because it needs access to two local functionsfunction advtrains.split_train_at_wagon(wagon_id)--get trainlocal data = advtrains.wagons[wagon_id]
advtrains.split_train_at_index(advtrains.trains[data.train_id], data.pos_in_trainparts)end-- COUPLE ENTITIES --local couple_max_dist=3
minetest.register_entity("advtrains:discouple", {
visual="sprite",
textures = {"advtrains_discouple.png"},
collisionbox = {-0.3,-0.3,-0.3,0.3,0.3,0.3},
visual_size = {x=0.7, y=0.7},
initial_sprite_basepos = {x=0, y=0},
is_discouple=true,
static_save =false,
on_activate=function(self, staticdata)if staticdata=="DISCOUPLE"then--couple entities have no right to exist further...atprint("Discouple loaded from staticdata, destroying")
self.object:remove()returnend
self.object:set_armor_groups({immortal=1})end,
get_staticdata=function()return"DISCOUPLE"end,
on_punch=function(self, player)local pname = player:get_player_name()if pname and pname~=""and self.wagon thenif advtrains.safe_decouple_wagon(self.wagon.id, pname)then
self.object:remove()endendend,
on_step=function(self, dtime)if not self.wagon then
self.object:remove()returnend--get_yaw seems to be a reliable method to check if an object is loaded...if it returns nil, it is not.if not self.wagon.object:get_yaw()then
self.object:remove()returnendif not self.wagon:train()or self.wagon:train().velocity >0then
self.object:remove()returnendend,})-- advtrains:couple-- Couple entity
minetest.register_entity("advtrains:couple", {
visual="sprite",
textures = {"advtrains_couple.png"},
collisionbox = {-0.3,-0.3,-0.3,0.3,0.3,0.3},
visual_size = {x=0.7, y=0.7},
initial_sprite_basepos = {x=0, y=0},
is_couple=true,
static_save =false,
on_activate=function(self, staticdata)if staticdata=="COUPLE"then--couple entities have no right to exist further...--atdebug("Couple loaded from staticdata, destroying")
self.object:remove()returnend
self.object:set_armor_groups({immmortal=1})end,
get_staticdata=function(self)return"COUPLE"end,
on_rightclick=function(self, clicker)if not self.train_id_1 or not self.train_id_2 then return endlocal pname=clicker
iftype(clicker)~="string"then pname=clicker:get_player_name()endlocal train1=advtrains.trains[self.train_id_1]local train2=advtrains.trains[self.train_id_2]
advtrains.safe_couple_trains(train1, self.t1_is_front, train2, self.t2_is_front, pname)
self.object:remove()end,
on_step=function(self, dtime)if advtrains.wagon_outside_range(self.object:get_pos())then--atdebug("Couple Removing outside range")
self.object:remove()returnendif not self.train_id_1 or not self.train_id_2 then--atdebug("Couple Removing ids missing")
self.object:remove()returnendlocal train1=advtrains.trains[self.train_id_1]local train2=advtrains.trains[self.train_id_2]if not train1 or not train2 then--atdebug("Couple Removing trains missing")
self.object:remove()returnendif self.position_set and train1.velocity>0or train2.velocity>0then--atdebug("Couple: train is moving, destroying")
self.object:remove()returnendif not self.position_set thenlocal tp1
if self.t1_is_front then
tp1=advtrains.path_get_interpolated(train1, train1.index)else
tp1=advtrains.path_get_interpolated(train1, train1.end_index)endlocal tp2
if self.t2_is_front then
tp2=advtrains.path_get_interpolated(train2, train2.index)else
tp2=advtrains.path_get_interpolated(train2, train2.end_index)endlocal pos_median=advtrains.pos_median(tp1, tp2)if not vector.equals(pos_median, self.object:get_pos())then
self.object:set_pos(pos_median)end
self.position_set=trueendend,})