Commuter train timetable


setnormstep(128)         				{set speed steps to 128}
securitytime(0)                			{set time out to i min}
t.a := curtimeval              			{get the current system time}
label('start')                 				{main start point}   
i.a :=300                     				{I.A is used to transfer values} 
                               					{to sub routines}
gosub('waittime')            			{call wait time routine to allow}
                               					{passangers to get on/off}
setdir                         				{change direction}
label('checkroutclear')        {			check the route is clear}
if ser(6,6) or ser(6,11) then goto('checkroutclear')
setrou('sto',set)              			{set up route to leave the station}
i.a := 10                      				{wait another 10 secs}
gosub('waittime')
setaimstep(35)                 			{set speed to leave the station at}                         
waitfor(ser(6,10))             			{wait until the train has left the station}
i.e:=5                         				{pass sensor number to routine}
i.f:=14                       				{second part of sensor number}
i.a:=704                     			 	{this signal number}      
i.b:=705                       				{next signal number}
gosub('nextblock')
i.e:=6                                  				{ pass info as above}
i.f:=15
i.a:=705
i.b:=701
gosub('nextblock')
i.e:=6                                  				{pass info as above}
i.f:=7
i.a:=701
i.b:=703
gosub('nextblock')
i.h :=5                        				{set the number of laps on the mainline}  
i.g:=0                         				{set completed counter to 0}
label('trainondownmain')       			{loop for down main}
waitfor(ser(5,12))             			{make sure we are on the main loop} 
if not setrou('mn1',set) then setrou('mn1',set) { make sure we are not going around the rev loop}
i.e:=7                                  				{pass block info}
i.f:=9
i.a:= 703
i.b:= 806
gosub('nextblock')
i.e:=6
i.f:=4
i.a:= 806
i.b:= 707
gosub('nextblock')
i.e:=6
i.f:=12
i.a:= 707
i.b:= 706
gosub('nextblock')
i.e:=6
i.f:=13
i.a:= 706
i.b:= 702
gosub('nextblock')
i.e:=5
i.f:=11
i.a:= 702
i.b:= 703
gosub('nextblock')
inc(i.g)                       				{ add one to the times around the main line counter}
if i.g < i.h then goto('trainondownmain') 	{ if number of laps achived then go to the rev loop}
setrou('rev',set)               			{set reverse loop on}
setaimstep(50)                 			{slow the train down}
i.e:=7                         				{block stuff for the rev loop}
i.f:=10
i.a:=808
i.b:=805 
gosub('nextblock')            			{hidden siding block}
i.e:=5
i.f:=4
i.a :=805
i.b:=807
gosub('nextblock')
if not setrou('mn2',set) then setrou('mn2',set) {dont go up to station}
setrou('hi4',set)              			{set hidden pass by}
gosub('nextblock')
i.h :=4                        				{reset vars for 4 laps}
i.g := 0
i.i := 0                       				{set flag not been to hidden}
label('trainonupmain')         			{loop for up main}
i.e:=5                         				{block stuff for up main}
i.f:=8
i.a:=807
i.b:=708
gosub('nextblock')   
i.e:=6
i.f:=16
i.a:=708
i.b:=709
gosub('nextblock')
i.e:=6
i.f:=16
i.a:=709
i.b:=801
gosub('nextblock')
if not setrou('hi4',set) then setrou('hi4',set) { make sure we go thru the hidden siding pass track}
i.e:=5
i.f:=4
i.a:=801
i.b:=805
gosub('nextblock')
inc(i.g)                                				{inc the lap counter}
if i.g < i.h then goto('trainonupmain') 		{check number of laps}
waitfor(ser(5,15))
setrou('hi3',set)                       			{set hidden siding to 3}
i.a:=801
i.b:=804
i.e:=5
i.f:=16
gosub('nextblock')
waitfor(not ser(5,16))
setaimstep(0)                           			{stop in hidden siding}
setrou('hi4',set)                       			{set  points to thru track}
i.a := 300                              			{ time to stop in the sidings for x secs}
i.i := 1                                				{set flag been in hidden}
gosub('waittime')                       			{wait in hidden sidings}
setrou('hi3',set)                        			{set the route to hidden siding 3}
setrou('stu',set)			  	{ set the up to station route}
i.a:=804
i.b:=807
i.e:=5
i.f:=8
gosub('nextblock')
setrou('hi4',set)                     			{ set the hidden back to pass thru}
if not setrou('stu',set) then setrou('stu',set) 	{make sure up route is set}
i.a:=710
i.b :=711
i.e:=6
i.f:=14
gosub('nextblock')
i.a:=711
i.b :=711
i.e:=6
i.f:=3
gosub('nextblock')
if not setrou('pl4',set) then setrou('pl4',set)
gosub('stationentrance')
waitfor(ser(7,5))
setaimstep(30)
waitfor(ser(7,6))
setaimstep(0)
goto('start')
halt
sub('waittime')                     			 {wait for x seconds as carried by i.a}
t.a:=curtimeval
label('waitloop')
i.G := timediff(curtimeval,t.a)
if timediff(curtimeval,t.a) < i.a then goto('waitloop')
return
i.c := 80
sub('nextblock')
if getswi(i.a) = red then goto('signalatred')
if getswi(i.b) = green  then setaimstep(65) else setaimstep(35)   {check the next sig before speeding up} 
if i.a = i.b then setaimstep(35)
waitfor(ser(i.e,i.f))                                                                     			{ wait for block}
if ser(5,8) then if not setrou('mn1',set) then setrou('mn1',set)        	{clear rev loop}
label('blockreturn')
return
sub('stationentrance')
if getswi(711) = red then setaimstep(35)
waitfor(ser(6,5))
if getswi(711) =red then setaimstep(0)
label('waitroute')
if not setrou('pl4',set) then setrou('pl4',set)           			{set platform 4}
if getswi(711) = red then goto('waitroute')
setaimstep(35)
return
label('signalatred')             {signal at red loop}
label('loopred')                   { wait till close to signal b efore stopping} 
if getswi(i.a) = green then goto('redcleared') { red cleared}
if not ser(i.e,i.f) then goto('loopred')
label('redslowing')               {stop a signal at red}
setaimstep(0)
if getswi(i.a) = green then goto('redcleared')
label('waitred')                  { wait at the red }
if getswi(i.a) = RED then goto('waitred')
t.a:=curtimeval
label('reddelay')                { wait 5 secs before moving off after signal clears}
if timediff(curtimeval,t.a) < 5 then goto('reddelay')
label('redcleared')
if getswi(i.b) = green then setaimstep(65) else setaimstep(35){ if next sig ok go at normal speed else go slow}
goto('blockreturn') 						{go back to the block routine}