summaryrefslogtreecommitdiff
path: root/Tyard/nodes/(-4027,12,-2731).lua
blob: 91a72499f767dffb3766453475d5fd11aeb383ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- Classification track
local this_dir = false
-- this_dir == true for north end, false for south end
if F.yard_active() then
	if atc_arrow then -- loco is at working end
		F.remove_rc({"TY_PICKUP"})
		if F.has_rc("TY_ARRIVE") and F.dir() == this_dir then --first pass, prep train for working
			F.remove_rc({"TY_AROUND"})
			if not F.get_rc_safe():match("TY_LOCOS_%d+") or not F.get_rc_safe():match("TY_WAGONS_%d+") then
				local full_length = train_length()
				split_off_locomotive("A0B0")
				F.add_rc({"TY_HEADSHUNT"}) -- train will end up going through headshunt no matter what. there shouldn't be any trains entering that pass straight through.
				if train_length() == full_length then --train is either only here for collection or there's no FC's in the wagons. can't differentiate
					local lane = F.get_rc_safe():match("TY_COLLECT_(%S+)") or ""
					F.add_rc({"TY_LAST_CLASS","TY_CLASS_"..lane}) -- direct train to correct lane for collection
				else --identify and save loco:wagon ratio, then bounce back from headshunt. it's easier than trying to reconnect
					F.add_rc({"TY_LOCOS_"..train_length(), "TY_WAGONS_"..(full_length-train_length()),"TY_PICKUP"})
				end
				return -- train has departed for headshunt
			end
			if F.has_rc("TY_RTS") then
				F.dir(not F.dir())
				F.remove_rc({"TY_RTS"})
				F.add_rc({"TY_HEADSHUNT","TY_AROUND"}) -- send loco around to the other end
				split_off_locomotive("A0B0")
				atc_set_ars_disable(false)
				return -- train has departed for RTS loop
			end
			F.remove_rc({"TY_ARRIVE"})
			step_fc()
		end
		if this_dir == F.dir() then --train has bounced and is ready to classify wagon(s)
			-- local pre_split = train_length()
			local lane = split_at_fc("A0B0",5) -- where to classify this rake
			local locos = tonumber(F.get_rc_safe():match("TY_LOCOS_(%d+)")) or 1 --saved loco count
			local wagons = tonumber(F.get_rc_safe():match("TY_WAGONS_(%d+)")) or 0 -- wagon_count from last classification split
			local this_rake = train_length() - locos-- subtract these wagons from the overall wagon count
			if this_rake == wagons then -- mark for last_classification
				F.add_rc({"TY_LAST_CLASS"})
			end
			if lane ~= "" then
				F.add_rc({"TY_CLASS_"..lane})
			end
			for v in F.get_rc_safe():gmatch("(TY_WAGONS_%d+)") do
				F.remove_rc({v})
			end
			F.add_rc({"TY_HEADSHUNT","TY_WAGONS_"..wagons-this_rake})
			atc_set_ars_disable(false)
			set_autocouple()
		else --bounce train back towards working end
			atc_set_ars_disable(true)
			atc_send("S0WRD1S3")
			unset_autocouple()
		end
	else -- train entering from the far end. set autocouple so it pushes all the way through to the bounce
		set_autocouple()
	end
end