
Constants:
Braking=1, DoMin=4, DoFull=5, CalCount=9, EndCal=10, Free=0, WaitClear=1, Discovered=2,
Stopped=3, StopClear=4, Stopping=5, GetTrain=6, GetCal=7, Booked=8, Calibrated=9,
CalFwd=10, CalRev=11, Departing=12, Occupied=13, Arriving=14, Expected=15, ArrOcc=13,
Moving=12, Left=0, Right=1, ChosenLeft=2, ChosenRight=3, Undef=4, Fwd=0, Rev=1,
Goods=1, Local=3, Passenger=7, Express=15,

Controls:
GROUP: Tracks (rs, rc)=(L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16, L18, L19, L20, L21, P1, P2)
GROUP: SPDT (rs, rc1, rc2)=(L2)
GROUP: switch (rcA, rcB)=(L17)
GROUP: Points (TurnA, TurnB)=(TO4, TO5, TI2, TI4, TI6, TI7, TI8, TI11, TI12, TI13, TI14, TI15)
{ rc<section>: Cab relay. 0=preferred cab, 1=secondary cab }
{ rs<section>: Stop relay. 0=running, 1=Stopped }
{ re<section>: Emergency relay. 0=Stopped. 1=Safety zone enabled }
{ Stack: CTIout 32/32 }
DirRelay, TurnTO1, TurnCO1, TurnCO2, spare, spare, spare, spare,
spare, spare, rcL9, rsL9, rcL8, rsL8, rcL7, rsL7,
rcL6, rsL6, spare, rcL5, rsL5, TurnTO3, rcL4, rsL4,
rcL3, rsL3, TurnTI3, TurnTO2, rc1L2, rc2L2, rsL2, TurnTI1,
{ Stack: MergOdds 2/8 }
PwrTI2, PwrTI8, spare, spare, spare, spare, spare, spare,
{ Stack: MergOut 28/32 }
rcL13, rcL14, rcL15, rcL16, rsL13, rsL14, rsL15, rsL16,
rcAL17, rcL10, rcL11, rcL12, rcBL17, rsL10, rsL11, rsL12,
rcL21, rcL20, rcL19, rcL18, rsL21, rsL20, rsL19, rsL18,
spare, spare, rcP1, rcP2, spare, spare, rsP1, rsP2,
{ Stack: B3out 6/32 }
spare, spare, TurnATI6, TurnBTI6, TurnATI7, TurnBTI7, spare, spare,
spare, spare, spare, spare, spare, spare, spare, spare,
spare, spare, spare, spare, spare, spare, spare, spare,
spare, spare, spare, spare, spare, spare, spare, spare,
{ Stack: B1out 12/16 }
TurnATI8, TurnBTI8, TurnATI11, TurnBTI11, TurnATI12, TurnBTI12, TurnATI13, TurnBTI13,
TurnATI14, TurnBTI14, TurnATI15, TurnBTI15, spare, spare, spare, spare,
{ Stack: B4out 6/8 }
TurnATI4, TurnBTI4, TurnATO4, TurnBTO4, TurnATO5, TurnBTO5, spare, spare,
{ Stack: B5out 2/8 }
TurnATI2, TurnBTI2, spare, spare, spare, spare, spare, spare,

Sensors:
GROUP: Tracks (Cur, Opt, OptO)=(L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16, L17, L18, L19, L20, L21, P1, P2)
{ Cur<section>: Current detector }
{ Opt<section>: Optical detector starts braking, marks entry }
{ OptO<section>: Optical detector stops train, marks exit }
{ Stack: CTIin 32/32 }
OptL16*, OptOL16*, spare, spare, spare, spare, spare, spare,
OptL11*, OptOL11*, CurL9#, OptL9~*, CurL8#, spare, OptOL8~*, CurL7#,
OptL7~*, spare, CurL6#, spare, spare, CurL5#, spare, spare,
CurL4#, OptL4~*, OptOL4~*, CurL3#, CurL2#, OptL2*, OptOL2*, OptL17~*,
{ Stack: MergIn 14/16 }
CurL16#~, CurL15#~, CurL14#~, CurL13#~, CurL12#~, CurL11#~, CurL10#~, CurL17#~,
CurL18#~, CurL19#~, CurL20#~, CurL21#~, CurP2#~, CurP1#~, spare, spare,
{ Stack: B3in 10/32 }
OptL12~*, OptOL12~*, OptOL7~*, OptL6~*, OptOL18~*, OptL19~*, OptOL19~*, OptOL20~*,
OptL21~*, OptOL21~*, spare, spare, spare, spare, spare, spare,
spare, spare, spare, spare, spare, spare, spare, spare,
spare, spare, spare, spare, spare, spare, spare, spare,
{ Stack: B1in 7/16 }
OptL10~*, OptOL10~*, OptOL9~*, OptL8~*, OptP2~*, OptOP2*, OptOP1~*, spare,
spare, spare, spare, spare, spare, spare, spare, spare,
{ Stack: B4in 8/8 }
OptOL14~*, OptL13~*, OptOL13~*, OptOL6~*, OptL5~*, OptOL5~*, OptL18~*, OptL20~*,
{ Stack: B5in 8/8 }
OptL15~*, OptOL15~*, OptL14~*, spare, spare, OptL3~*, OptOL3~*, OptOL17~*,

SmartCabs:
cab[8], ct1, ct2, ct3, ct4, ct5, ct6,

Qkeys:
Stop, Pause, Turnout, Discover, Mark, Clear,

Variables:
GROUP: Tracks (s, t, p, mxtt, wt, len, tcal, Rand)=(L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16, L17, L18, L19, L20, L21, P1, P2)
GROUP: Turnouts (s, np, TReq, TSet, lp, rp, ncn, TBook)=(TO1, TO2, TO3, TO4, TO5)
GROUP: Turnins (s, t, TReq, TSet, psp, lp, rp, TBook)=(TI1, TI2, TI3, TI4, TI6, TI7, TI8, TI11, TI12, TI13, TI14, TI15)
GROUP: Crossover (as, bs, TReq, TSet, TBook)=(CO1, CO2)
GROUP: Odds (s, t, tt)=(D2, X1, X2, J14)
GROUP: Trains (cab, t, len, tdir, sp, osp, ncab, col)=(t[])
GROUP: Speeds (m, f, mm, mf, om, of, omm, omf)=(st[])
GROUP: Throttles (t)=(c[])
GROUP: Calibration (Train, Time, Len, Spd, FS, TT, Red, Inc)=(Cal)
GROUP: TargetSpeeds (express, passenger, local, goods, crawl)=(FS)
GROUP: Networks (s, t, In_, Out_, Pri_, Time_)=(N1, N2)
GROUP: N1in (Tt_, np, Route, ncn)=(N1L12, N1L19, N1L21)
GROUP: N1out (s, t, psp, Route)=(N1_L11)
GROUP: N2in (Tt_, np, Route, ncn)=(N2L2, N2L3, N2L4, N2P1, N2P2, N2L10, N2L15, N2L16)
GROUP: N2out (s, t, psp, Route)=(N2_P2, N2_L2, N2_L9, N2_L14, N2_L15, N2_L16, N2_L17, N2_P1)
STRUCT (s, t, p, wt)=(L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16, L17, L18, L19, L20, L21, P1, P2, FreeSect)
STRUCT (s, t)=(X2)
STRUCT (s, TReq, TSet)=(TI1, TI2, TI3, TI4, TI6, TI7, TI8, TI11, TI12, TI13, TI14, TI15, TO1, TO2, TO3, TO4, TO5, CO1, CO2)
STRUCT (s)=(D2, X1, J14, CO1a, CO1b, CO2a, CO2b)
STRUCT (s, t)=(N1, N2)
STRUCT (s, t)=(N1_L11)
STRUCT (s, t)=(N2_P2, N2_L2, N2_L9, N2_L14, N2_L15, N2_L16, N2_L17, N2_P1)
CalTrain, CalTime, CalLen, CalSpd, CalFS, CalTT, CalRed, CalInc,
                                               { Variables used by train recalibration }
FSexpress, FSpassenger, FSlocal, FSgoods, FScrawl               { Desired train speeds }
tc[8]                                                        { Train controlled by cab }
cabt[6], ncabt[6]                                     { Cab, and new cab used by train }
BookNo, Avail                                                { Turnout booking globals }
In_N1, Out_N1, Time_N1, Pri_N1                                            { Network N1 }
In_N2, Out_N2, Time_N2, Pri_N2                                            { Network N2 }
TBookTO1                                                              { Net switch TO1 }
TBookCO1                                                              { Net switch CO1 }
TBookCO2                                                              { Net switch CO2 }
RandL15, tcalL15                                                    { Long section L15 }
sX2, ttX2                                                                { Crossing X2 }
tcalL9                                                               { Long section L9 }
pspTI4, lpTI4, rpTI4                                                      { TurnIn TI4 }
npTO3, lpTO3, rpTO3, TBookTO3                                            { TurnOut TO3 }
mxttL4, RandL4                                                       { Long section L4 }
mxttL3, RandL3                                                       { Long section L3 }
TBookTI3                                                              { Net switch TI3 }
TBookTO2                                                              { Net switch TO2 }
TBookTI1                                                              { Net switch TI1 }
pspTI2, lpTI2, rpTI2                                                      { TurnIn TI2 }
npTO4, ncnTO4, lpTO4, rpTO4                                              { TurnOut TO4 }
npTO5, ncnTO5, lpTO5, rpTO5                                              { TurnOut TO5 }
RandL20                                                             { Long section L20 }
RouteN1L12, Tt_N1L12, npN1L12                                        { Net input N1L12 }
RouteN1L19, Tt_N1L19, npN1L19                                        { Net input N1L19 }
RouteN1L21, Tt_N1L21, npN1L21                                        { Net input N1L21 }
RouteN1_L11, pspN1_L11                                             { Net output N1_L11 }
RouteN2P2, Tt_N2P2, npN2P2                                            { Net input N2P2 }
RouteN2L2, Tt_N2L2, npN2L2, ncnN2L2                                   { Net input N2L2 }
RouteN2L3, Tt_N2L3, npN2L3                                            { Net input N2L3 }
RouteN2L4, Tt_N2L4, npN2L4                                            { Net input N2L4 }
RouteN2L10, Tt_N2L10, npN2L10                                        { Net input N2L10 }
RouteN2L15, Tt_N2L15, npN2L15, ncnN2L15                              { Net input N2L15 }
RouteN2L16, Tt_N2L16, npN2L16                                        { Net input N2L16 }
RouteN2P1, Tt_N2P1, npN2P1                                            { Net input N2P1 }
RouteN2_P2, pspN2_P2                                                { Net output N2_P2 }
RouteN2_L2, pspN2_L2                                                { Net output N2_L2 }
RouteN2_L9, pspN2_L9                                                { Net output N2_L9 }
RouteN2_L14, pspN2_L14                                             { Net output N2_L14 }
RouteN2_L15, pspN2_L15                                             { Net output N2_L15 }
RouteN2_L16, pspN2_L16                                             { Net output N2_L16 }
RouteN2_L17, pspN2_L17                                             { Net output N2_L17 }
RouteN2_P1, pspN2_P1                                                { Net output N2_P1 }
spt[6], ospt[6]                                    { current & previous speed of train }
tdirt[6]                                                         { Direction for train }
mst[6], omst[6]                                         { Minimum speed for each train }
mmst[6], ommst[6]                                                { Measured slow speed }
fst[6], ofst[6]                                            { Full speed for each train }
mfst[6], omfst[6]                                                { Measured full speed }
lent[6], tt[6]                                           { Length & type of each train }
colt0, colt[6], coltx                                              { Colours of trains }
initialised, Rand, FreeTrain, TrainTrack, c, d, upmom, downmom        { Misc variables }

Actions:

{ WHEN Stop button pressed DO Stop at next valid location }
{ WHEN Stop released DO Resume }
{ WHEN Pause button pressed DO Brake all trains }
{ WHEN Pause released DO resume }
{ WHEN Turnout=left, paused DO set all turnouts left }
{ WHEN Turnout=right, paused DO set all turnouts right }
{ WHEN Turnout=left, not paused DO random to left }
{ WHEN Turnout=right, not paused DO random to right }
{ WHEN Discover button pressed DO look for new trains }
{ WHEN Mark button pressed DO Clear error message }
WHEN Stop=LEFT DO initialised=4                                                 {WHEN 1}
WHEN Stop=RIGHT DO initialised=2                                                {WHEN 2}
WHEN Pause=LEFT DO initialised=5                                                {WHEN 3}
WHEN Pause=RIGHT DO initialised=2                                               {WHEN 4}
WHEN Turnout=LEFT, initialised=5 DO { Set all turnouts left }                   {WHEN 5}
	TI11.Treq=0, TI12.Treq=0, TI13.Treq=0, TI14.Treq=0, TI15.Treq=0, TO1.Treq=0,
	TO2.Treq=0, CO1.Treq=0, TI1.Treq=0, CO2.Treq=0, TI3.Treq=0, TI6.Treq=0, TI7.Treq=0,
	TI8.Treq=0,
WHEN Turnout=RIGHT, initialised=5 DO { Set all turnouts right }                 {WHEN 6}
	TO3.Treq=1, TO4.Treq=1, TO5.Treq=1, TI2.Treq=1, TI4.Treq=1, TI11.Treq=1, TI12.Treq=1,
	TI13.Treq=1, TI14.Treq=1, TI15.Treq=1, TO1.Treq=1, TO2.Treq=1, CO1.Treq=1, TI1.Treq=1,
	CO2.Treq=1, TI3.Treq=1, TI6.Treq=1, TI7.Treq=1, TI8.Treq=1,
WHEN Turnout=LEFT, initialised=2 DO Rand=65535                                  {WHEN 7}
WHEN Turnout=RIGHT, initialised=2 DO Rand=0                                     {WHEN 8}
WHEN Discover=LEFT DO initialised=1, wait 0.1, initialised=2                    {WHEN 9}
WHEN Mark=LEFT DO $Erase Message(1,3,3)                                         {WHEN 10}

{ WHEN Main initialisation }

WHEN initialised=0                                                              {WHEN 11}
 DO	FSexpress=80, FSpassenger=60, FSlocal=50, FSgoods=40, FScrawl=20
	upmom=7, downmom=4, $Draw Message(6,1,3)="@upmom", $Draw Message(6,2,3)="@downmom",
	Avail=1, BookNo=1                                      { Ready for first turnout move }
	$Color Block(3,9,1)=0, $Color Block(2,11,1)=0, $Color Block(3,15,1)=0,
	$Color Block(6,18,1)=0, $Color Block(7,18,1)=0, $Color Block(13,18,1)=0,
	$Color Block(15,13,1)=0, $Color Block(15,7,1)=0, $Color Block(10,3,1)=0,
	$Color Block(5,3,1)=0, $Color Block(2,10,1)=0, $Color Block(2,3,1)=0,
	$Color Block(12,2,1)=0, $Color Block(16,8,1)=0, $Color Block(16,15,1)=0,
	$Color Block(16,16,1)=0, $Color Block(13,19,1)=0, $Color Block(12,20,1)=0,
	$Color Block(12,19,1)=0, $Color Block(4,19,1)=0, $Color Block(2,15,1)=0,
	$Color Block(3,19,1)=0, $Color Block(6,12,1)=0, $Color Block(7,12,1)=0,
	$Color Block(1,1,1)=0, $Color Block(12,12,1)=0, $Color Block(13,11,1)=0,
	$Color Block(14,10,1)=0, $Color Block(14,7,1)=0, $Color Block(13,10,1)=0,
	$Color Block(13,7,1)=0, $Color Block(12,4,1)=0, $Color Block(11,3,1)=0,
	$Color Block(3,8,1)=0, $Color Block(1,1,1)=0, $Color Block(1,1,1)=0,
	$Color Block(1,1,1)=0, $Color Block(1,1,1)=0, $Color Block(1,1,1)=0,
	$Color Block(4,7,1)=0, $Color Block(1,1,1)=0, $Color Block(3,14,1)=0,
	$Color Block(1,1,1)=0,
	L17.p=4,
	TO1.Tset=Undef, TO1.TReq=Undef, CO1.Tset=Undef, CO1.TReq=Undef,
	CO2.Tset=Undef, CO2.TReq=Undef, TI4.Tset=Undef, TI4.TReq=Undef,
	pspTI4=&FreeSect, lpTI4=&FreeSect, rpTI4=&FreeSect, TO3.Tset=Undef, TO3.TReq=Undef,
	npTO3=&FreeSect, lpTO3=&FreeSect, rpTO3=&FreeSect, TI3.Tset=Undef, TI3.TReq=Undef,
	TO2.Tset=Undef, TO2.TReq=Undef, TI1.Tset=Undef, TI1.TReq=Undef,
	TI2.Tset=Undef, TI2.TReq=Undef, pspTI2=&FreeSect, lpTI2=&FreeSect, rpTI2=&FreeSect,
	TO4.Tset=Undef, TO4.TReq=Undef, npTO4=&FreeSect, lpTO4=&FreeSect, rpTO4=&FreeSect,
	TO5.Tset=Undef, TO5.TReq=Undef, npTO5=&FreeSect, lpTO5=&FreeSect, rpTO5=&FreeSect,
	TI6.Tset=Undef, TI6.TReq=Undef, TI7.Tset=Undef, TI7.TReq=Undef,
	TI8.Tset=Undef, TI8.TReq=Undef, TI11.Tset=Undef, TI11.TReq=Undef,
	TI12.Tset=Undef, TI12.TReq=Undef, TI13.Tset=Undef, TI13.TReq=Undef,
	TI14.Tset=Undef, TI14.TReq=Undef, TI15.Tset=Undef, TI15.TReq=Undef, npN1L12=&FreeSect,
	npN1L19=&FreeSect, npN1L21=&FreeSect, pspN1_L11=&FreeSect, npN2P2=&FreeSect,
	npN2L2=&FreeSect, npN2L3=&FreeSect, npN2L4=&FreeSect, npN2L10=&FreeSect,
	npN2L15=&FreeSect, npN2L16=&FreeSect, npN2P1=&FreeSect, pspN2_P2=&FreeSect,
	pspN2_L2=&FreeSect, pspN2_L9=&FreeSect, pspN2_L14=&FreeSect, pspN2_L15=&FreeSect,
	pspN2_L16=&FreeSect, pspN2_L17=&FreeSect, pspN2_P1=&FreeSect,
	colt0=$RGB_0, colt[1]=$RGB_4080, colt[2]=$RGB_FF, colt[3]=$RGB_80FF,
	colt[4]=$RGB_FFFF, colt[5]=$RGB_FF00, colt[6]=$RGB_FF0000, coltx=$RGB_C0C0C0,
	$Erase Message(8,2,2), $Erase Message(10,2,2), lent[1]=999,
	$Erase Message(8,3,2), $Erase Message(10,3,2), lent[2]=999,
	$Erase Message(8,4,2), $Erase Message(10,4,2), lent[3]=999,
	$Erase Message(8,5,2), $Erase Message(10,5,2), lent[4]=999,
	$Erase Message(8,6,2), $Erase Message(10,6,2), lent[5]=999,
	$Erase Message(8,7,2), $Erase Message(10,7,2), lent[6]=999,
	cab[1]=10, cab[2]=10, cab[3]=10, cab[4]=10, cab[5]=10, cab[6]=10, cab[7]=10,
	cab[8]=10,
	wait 1, initialised=1, wait 0.5
	L17.p=0,
	cab[1]=0, cab[2]=0, cab[3]=0, cab[4]=0, cab[5]=0, cab[6]=0, cab[7]=0, cab[8]=0,
	initialised=2
{ For each section with sensor, allocate a train if current is drawn }


{ Allocate train numbers to newly discovered trains }

{ WHEN initialising, current detected DO Stopped }
{ WHEN initialising, opticals detected DO WaitClear }
{ WHEN Stopped & time to go DO GetTrain }
{ WHEN GetTrain & Train=0 DO Grab FreeTrain }
{ WHEN GetTrain & Train=0 & FreeTrain Grabbed DO set Train }
{ WHEN GetTrain & Train<>0 & Next free DO GetCal }
{ WHEN GetCal & Calibrated & cab<>0 DO Calibrated }
{ WHEN Calibrated & next ready DO Occupied }
{ WHEN Calibrated & timeout DO Release cab, Stopped }

WHEN L16.s=Free, initialised=1, CurL16=TRUE                                     {WHEN 12}
 DO	L16.s=Stopped, L16.wt=$TIME, L16.wt=3+
WHEN L16.s=Free, initialised=1, CurL16=FALSE, OptL16=TRUE                       {WHEN 13}
 OR  initialised=1, L16.s=Free, CurL16=FALSE, OptOL16=TRUE
 DO	L16.s=WaitClear
WHEN L16.s=Stopped, $TIME>=L16.wt, initialised<>4 DO L16.s=GetTrain             {WHEN 14}
WHEN L16.s=GetTrain, L16.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=5        {WHEN 15}
WHEN L16.s=GetTrain, L16.t=0, FreeTrain<>0, TrainTrack=5                        {WHEN 16}
 DO	L16.t=FreeTrain, FreeTrain=0, spt[L16.t]=Occupied, TrainTrack=0
WHEN L16.s=GetTrain, L16.t<>0, *npN2L16.s=Free                                  {WHEN 17}
 DO	L16.s=GetCal, L16.wt=mst[L16.t]
	$Color Track(3,15,1)=colt[L16.t]
WHEN L16.s=GetCal, L16.wt<>0, L16.p<>0                                          {WHEN 18}
 DO	L16.s=Calibrated, L16.wt=$SESSION, L16.wt=10+
WHEN L16.s=Calibrated, L16.t=*npN2L16.t                                         {WHEN 19}
 DO	wait 0.7, L16.s=Occupied, L16.wt=$SESSION, L16.wt=10+
	$Color Block(3,15,1)=colt[L16.t]
WHEN L16.s=Calibrated, $SESSION>L16.wt                                          {WHEN 20}
 DO	spt[L16.t]=Stopped, L16.p=0, L16.s=Stopped, L16.wt=$TIME, L16.wt=5+
WHEN L15.s=Free, initialised=1, CurL15=TRUE                                     {WHEN 21}
 DO	L15.s=Stopped, L15.wt=$TIME, L15.wt=3+
WHEN L15.s=Free, initialised=1, CurL15=FALSE, OptL15=TRUE                       {WHEN 22}
 OR  initialised=1, L15.s=Free, CurL15=FALSE, OptOL15=TRUE
 DO	L15.s=WaitClear
WHEN L15.s=Stopped, $TIME>=L15.wt, initialised<>4 DO L15.s=GetTrain             {WHEN 23}
WHEN L15.s=GetTrain, L15.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=7        {WHEN 24}
WHEN L15.s=GetTrain, L15.t=0, FreeTrain<>0, TrainTrack=7                        {WHEN 25}
 DO	L15.t=FreeTrain, FreeTrain=0, spt[L15.t]=Occupied, TrainTrack=0
WHEN L15.s=GetTrain, L15.t<>0, *npN2L15.s=Free                                  {WHEN 26}
 DO	L15.s=GetCal, L15.wt=mst[L15.t]
	$Color Track(7,18,1)=colt[L15.t]
WHEN L15.s=GetCal, L15.wt<>0, L15.p<>0                                          {WHEN 27}
 DO	L15.s=Calibrated, L15.wt=$SESSION, L15.wt=10+
WHEN L15.s=Calibrated, L15.t=*npN2L15.t                                         {WHEN 28}
 DO	wait 0.7, L15.s=Occupied, L15.wt=$SESSION, L15.wt=10+
	$Color Block(7,18,1)=colt[L15.t]
WHEN L15.s=Calibrated, $SESSION>L15.wt                                          {WHEN 29}
 DO	spt[L15.t]=Stopped, L15.p=0, L15.s=Stopped, L15.wt=$TIME, L15.wt=5+
WHEN L14.s=Free, initialised=1, CurL14=TRUE                                     {WHEN 30}
 DO	L14.s=Stopped, L14.wt=$TIME, L14.wt=3+
WHEN L14.s=Free, initialised=1, CurL14=FALSE, OptL14=TRUE                       {WHEN 31}
 OR  initialised=1, L14.s=Free, CurL14=FALSE, OptOL14=TRUE
 DO	L14.s=WaitClear
WHEN L14.s=Stopped, $TIME>=L14.wt, initialised<>4 DO L14.s=GetTrain             {WHEN 32}
WHEN L14.s=GetTrain, L14.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=8        {WHEN 33}
WHEN L14.s=GetTrain, L14.t=0, FreeTrain<>0, TrainTrack=8                        {WHEN 34}
 DO	L14.t=FreeTrain, FreeTrain=0, spt[L14.t]=Occupied, TrainTrack=0
WHEN L14.s=GetTrain, L14.t<>0, X2.s=Free DO L14.s=GetCal, L14.wt=mst[L14.t]     {WHEN 35}
	$Color Track(13,18,1)=colt[L14.t]
WHEN L14.s=GetCal, L14.wt<>0, L14.p<>0                                          {WHEN 36}
 DO	L14.s=Calibrated, L14.wt=$SESSION, L14.wt=10+
WHEN L14.s=Calibrated, L14.t=X2.t                                               {WHEN 37}
 DO	L14.s=Occupied, L14.wt=$SESSION, L14.wt=10+
	$Color Block(13,18,1)=colt[L14.t]
WHEN L14.s=Calibrated, $SESSION>L14.wt                                          {WHEN 38}
 DO	spt[L14.t]=Stopped, L14.p=0, L14.s=Stopped, L14.wt=$TIME, L14.wt=5+
WHEN L13.s=Free, initialised=1, CurL13=TRUE                                     {WHEN 39}
 DO	L13.s=Stopped, L13.wt=$TIME, L13.wt=3+
WHEN L13.s=Free, initialised=1, CurL13=FALSE, OptL13=TRUE                       {WHEN 40}
 OR  initialised=1, L13.s=Free, CurL13=FALSE, OptOL13=TRUE
 DO	L13.s=WaitClear
WHEN L13.s=Stopped, $TIME>=L13.wt, initialised<>4 DO L13.s=GetTrain             {WHEN 41}
WHEN L13.s=GetTrain, L13.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=10       {WHEN 42}
WHEN L13.s=GetTrain, L13.t=0, FreeTrain<>0, TrainTrack=10                       {WHEN 43}
 DO	L13.t=FreeTrain, FreeTrain=0, spt[L13.t]=Occupied, TrainTrack=0
WHEN L13.s=GetTrain, L13.t<>0, L12.s=Free DO L13.s=GetCal, L13.wt=mst[L13.t]    {WHEN 44}
	$Color Track(15,13,1)=colt[L13.t]
WHEN L13.s=GetCal, L13.wt<>0, L13.p<>0                                          {WHEN 45}
 DO	L13.s=Calibrated, L13.wt=$SESSION, L13.wt=10+
WHEN L13.s=Calibrated, L13.t=L12.t                                              {WHEN 46}
 DO	L13.s=Occupied, L13.wt=$SESSION, L13.wt=10+
	$Color Block(15,13,1)=colt[L13.t]
WHEN L13.s=Calibrated, $SESSION>L13.wt                                          {WHEN 47}
 DO	spt[L13.t]=Stopped, L13.p=0, L13.s=Stopped, L13.wt=$TIME, L13.wt=5+
WHEN L12.s=Free, initialised=1, CurL12=TRUE                                     {WHEN 48}
 DO	L12.s=Stopped, L12.wt=$TIME, L12.wt=3+
WHEN L12.s=Free, initialised=1, CurL12=FALSE, OptL12=TRUE                       {WHEN 49}
 OR  initialised=1, L12.s=Free, CurL12=FALSE, OptOL12=TRUE
 DO	L12.s=WaitClear
WHEN L12.s=Stopped, $TIME>=L12.wt, initialised<>4 DO L12.s=GetTrain             {WHEN 50}
WHEN L12.s=GetTrain, L12.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=11       {WHEN 51}
WHEN L12.s=GetTrain, L12.t=0, FreeTrain<>0, TrainTrack=11                       {WHEN 52}
 DO	L12.t=FreeTrain, FreeTrain=0, spt[L12.t]=Occupied, TrainTrack=0
WHEN L12.s=GetTrain, L12.t<>0, *npN1L12.s=Free                                  {WHEN 53}
 DO	L12.s=GetCal, L12.wt=mst[L12.t]
	$Color Track(15,7,1)=colt[L12.t]
WHEN L12.s=GetCal, L12.wt<>0, L12.p<>0                                          {WHEN 54}
 DO	L12.s=Calibrated, L12.wt=$SESSION, L12.wt=10+
WHEN L12.s=Calibrated, L12.t=*npN1L12.t                                         {WHEN 55}
 DO	wait 0.7, L12.s=Occupied, L12.wt=$SESSION, L12.wt=10+
	$Color Block(15,7,1)=colt[L12.t]
WHEN L12.s=Calibrated, $SESSION>L12.wt                                          {WHEN 56}
 DO	spt[L12.t]=Stopped, L12.p=0, L12.s=Stopped, L12.wt=$TIME, L12.wt=5+
WHEN L11.s=Free, initialised=1, CurL11=TRUE                                     {WHEN 57}
 DO	L11.s=Stopped, L11.wt=$TIME, L11.wt=3+
WHEN L11.s=Free, initialised=1, CurL11=FALSE, OptL11=TRUE                       {WHEN 58}
 OR  initialised=1, L11.s=Free, CurL11=FALSE, OptOL11=TRUE
 DO	L11.s=WaitClear
WHEN L11.s=Stopped, $TIME>=L11.wt, initialised<>4 DO L11.s=GetTrain             {WHEN 59}
WHEN L11.s=GetTrain, L11.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=12       {WHEN 60}
WHEN L11.s=GetTrain, L11.t=0, FreeTrain<>0, TrainTrack=12                       {WHEN 61}
 DO	L11.t=FreeTrain, FreeTrain=0, spt[L11.t]=Occupied, TrainTrack=0
WHEN L11.s=GetTrain, L11.t<>0, L10.s=Free DO L11.s=GetCal, L11.wt=mst[L11.t]    {WHEN 62}
	$Color Track(10,3,1)=colt[L11.t]
WHEN L11.s=GetCal, L11.wt<>0, L11.p<>0                                          {WHEN 63}
 DO	L11.s=Calibrated, L11.wt=$SESSION, L11.wt=10+
WHEN L11.s=Calibrated, L11.t=L10.t                                              {WHEN 64}
 DO	L11.s=Occupied, L11.wt=$SESSION, L11.wt=10+
	$Color Block(10,3,1)=colt[L11.t]
WHEN L11.s=Calibrated, $SESSION>L11.wt                                          {WHEN 65}
 DO	spt[L11.t]=Stopped, L11.p=0, L11.s=Stopped, L11.wt=$TIME, L11.wt=5+
WHEN L10.s=Free, initialised=1, CurL10=TRUE                                     {WHEN 66}
 DO	L10.s=Stopped, L10.wt=$TIME, L10.wt=3+
WHEN L10.s=Free, initialised=1, CurL10=FALSE, OptL10=TRUE                       {WHEN 67}
 OR  initialised=1, L10.s=Free, CurL10=FALSE, OptOL10=TRUE
 DO	L10.s=WaitClear
WHEN L10.s=Stopped, $TIME>=L10.wt, initialised<>4 DO L10.s=GetTrain             {WHEN 68}
WHEN L10.s=GetTrain, L10.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=13       {WHEN 69}
WHEN L10.s=GetTrain, L10.t=0, FreeTrain<>0, TrainTrack=13                       {WHEN 70}
 DO	L10.t=FreeTrain, FreeTrain=0, spt[L10.t]=Occupied, TrainTrack=0
WHEN L10.s=GetTrain, L10.t<>0, *npN2L10.s=Free                                  {WHEN 71}
 DO	L10.s=GetCal, L10.wt=mst[L10.t]
	$Color Track(5,3,1)=colt[L10.t]
WHEN L10.s=GetCal, L10.wt<>0, L10.p<>0                                          {WHEN 72}
 DO	L10.s=Calibrated, L10.wt=$SESSION, L10.wt=10+
WHEN L10.s=Calibrated, L10.t=*npN2L10.t                                         {WHEN 73}
 DO	wait 0.7, L10.s=Occupied, L10.wt=$SESSION, L10.wt=10+
	$Color Block(5,3,1)=colt[L10.t]
WHEN L10.s=Calibrated, $SESSION>L10.wt                                          {WHEN 74}
 DO	spt[L10.t]=Stopped, L10.p=0, L10.s=Stopped, L10.wt=$TIME, L10.wt=5+
WHEN L9.s=Free, initialised=1, CurL9=TRUE                                       {WHEN 75}
 DO	L9.s=Stopped, L9.wt=$TIME, L9.wt=3+
WHEN L9.s=Free, initialised=1, CurL9=FALSE, OptL9=TRUE                          {WHEN 76}
 OR  initialised=1, L9.s=Free, CurL9=FALSE, OptOL9=TRUE
 DO	L9.s=WaitClear
WHEN L9.s=Stopped, $TIME>=L9.wt, initialised<>4 DO L9.s=GetTrain                {WHEN 77}
WHEN L9.s=GetTrain, L9.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=14         {WHEN 78}
WHEN L9.s=GetTrain, L9.t=0, FreeTrain<>0, TrainTrack=14                         {WHEN 79}
 DO	L9.t=FreeTrain, FreeTrain=0, spt[L9.t]=Occupied, TrainTrack=0
WHEN L9.s=GetTrain, L9.t<>0, L8.s=Free DO L9.s=GetCal, L9.wt=mst[L9.t]          {WHEN 80}
	$Color Track(2,10,1)=colt[L9.t]
WHEN L9.s=GetCal, L9.wt<>0, L9.p<>0                                             {WHEN 81}
 DO	L9.s=Calibrated, L9.wt=$SESSION, L9.wt=10+
WHEN L9.s=Calibrated, L9.t=L8.t DO L9.s=Occupied, L9.wt=$SESSION, L9.wt=10+     {WHEN 82}
	$Color Block(2,10,1)=colt[L9.t]
WHEN L9.s=Calibrated, $SESSION>L9.wt                                            {WHEN 83}
 DO	spt[L9.t]=Stopped, L9.p=0, L9.s=Stopped, L9.wt=$TIME, L9.wt=5+
WHEN L8.s=Free, initialised=1, CurL8=TRUE                                       {WHEN 84}
 DO	L8.s=Stopped, L8.wt=$TIME, L8.wt=3+
WHEN L8.s=Free, initialised=1, CurL8=FALSE, OptL8=TRUE                          {WHEN 85}
 OR  initialised=1, L8.s=Free, CurL8=FALSE, OptOL8=TRUE
 DO	L8.s=WaitClear
WHEN L8.s=Stopped, $TIME>=L8.wt, initialised<>4 DO L8.s=GetTrain                {WHEN 86}
WHEN L8.s=GetTrain, L8.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=15         {WHEN 87}
WHEN L8.s=GetTrain, L8.t=0, FreeTrain<>0, TrainTrack=15                         {WHEN 88}
 DO	L8.t=FreeTrain, FreeTrain=0, spt[L8.t]=Occupied, TrainTrack=0
WHEN L8.s=GetTrain, L8.t<>0, L7.s=Free DO L8.s=GetCal, L8.wt=mst[L8.t]          {WHEN 89}
	$Color Track(2,3,1)=colt[L8.t]
WHEN L8.s=GetCal, L8.wt<>0, L8.p<>0                                             {WHEN 90}
 DO	L8.s=Calibrated, L8.wt=$SESSION, L8.wt=10+
WHEN L8.s=Calibrated, L8.t=L7.t DO L8.s=Occupied, L8.wt=$SESSION, L8.wt=10+     {WHEN 91}
	$Color Block(2,3,1)=colt[L8.t]
WHEN L8.s=Calibrated, $SESSION>L8.wt                                            {WHEN 92}
 DO	spt[L8.t]=Stopped, L8.p=0, L8.s=Stopped, L8.wt=$TIME, L8.wt=5+
WHEN L7.s=Free, initialised=1, CurL7=TRUE                                       {WHEN 93}
 DO	L7.s=Stopped, L7.wt=$TIME, L7.wt=3+
WHEN L7.s=Free, initialised=1, CurL7=FALSE, OptL7=TRUE                          {WHEN 94}
 OR  initialised=1, L7.s=Free, CurL7=FALSE, OptOL7=TRUE
 DO	L7.s=WaitClear
WHEN L7.s=Stopped, $TIME>=L7.wt, initialised<>4 DO L7.s=GetTrain                {WHEN 95}
WHEN L7.s=GetTrain, L7.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=16         {WHEN 96}
WHEN L7.s=GetTrain, L7.t=0, FreeTrain<>0, TrainTrack=16                         {WHEN 97}
 DO	L7.t=FreeTrain, FreeTrain=0, spt[L7.t]=Occupied, TrainTrack=0
WHEN L7.s=GetTrain, L7.t<>0, L6.s=Free DO L7.s=GetCal, L7.wt=mst[L7.t]          {WHEN 98}
	$Color Track(12,2,1)=colt[L7.t]
WHEN L7.s=GetCal, L7.wt<>0, L7.p<>0                                             {WHEN 99}
 DO	L7.s=Calibrated, L7.wt=$SESSION, L7.wt=10+
WHEN L7.s=Calibrated, L7.t=L6.t DO L7.s=Occupied, L7.wt=$SESSION, L7.wt=10+     {WHEN 100}
	$Color Block(12,2,1)=colt[L7.t]
WHEN L7.s=Calibrated, $SESSION>L7.wt                                            {WHEN 101}
 DO	spt[L7.t]=Stopped, L7.p=0, L7.s=Stopped, L7.wt=$TIME, L7.wt=5+
WHEN L6.s=Free, initialised=1, CurL6=TRUE                                       {WHEN 102}
 DO	L6.s=Stopped, L6.wt=$TIME, L6.wt=3+
WHEN L6.s=Free, initialised=1, CurL6=FALSE, OptL6=TRUE                          {WHEN 103}
 OR  initialised=1, L6.s=Free, CurL6=FALSE, OptOL6=TRUE
 DO	L6.s=WaitClear
WHEN L6.s=Stopped, $TIME>=L6.wt, initialised<>4 DO L6.s=GetTrain                {WHEN 104}
WHEN L6.s=GetTrain, L6.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=17         {WHEN 105}
WHEN L6.s=GetTrain, L6.t=0, FreeTrain<>0, TrainTrack=17                         {WHEN 106}
 DO	L6.t=FreeTrain, FreeTrain=0, spt[L6.t]=Occupied, TrainTrack=0
WHEN L6.s=GetTrain, L6.t<>0, *lpTI4.s=Free DO L6.s=GetCal, L6.wt=mst[L6.t]      {WHEN 107}
	$Color Track(16,8,1)=colt[L6.t]
WHEN L6.s=GetCal, L6.wt<>0, L6.p<>0                                             {WHEN 108}
 DO	L6.s=Calibrated, L6.wt=$SESSION, L6.wt=10+
WHEN L6.s=Calibrated, L6.t=*lpTI4.t                                             {WHEN 109}
 DO	wait 0.7, L6.s=Occupied, L6.wt=$SESSION, L6.wt=10+
	$Color Block(16,8,1)=colt[L6.t]
WHEN L6.s=Calibrated, $SESSION>L6.wt                                            {WHEN 110}
 DO	spt[L6.t]=Stopped, L6.p=0, L6.s=Stopped, L6.wt=$TIME, L6.wt=5+
WHEN L5.s=Free, initialised=1, CurL5=TRUE                                       {WHEN 111}
 DO	L5.s=Stopped, L5.wt=$TIME, L5.wt=3+
WHEN L5.s=Free, initialised=1, CurL5=FALSE, OptL5=TRUE                          {WHEN 112}
 OR  initialised=1, L5.s=Free, CurL5=FALSE, OptOL5=TRUE
 DO	L5.s=WaitClear
WHEN L5.s=Stopped, $TIME>=L5.wt, initialised<>4 DO L5.s=GetTrain                {WHEN 113}
WHEN L5.s=GetTrain, L5.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=19         {WHEN 114}
WHEN L5.s=GetTrain, L5.t=0, FreeTrain<>0, TrainTrack=19                         {WHEN 115}
 DO	L5.t=FreeTrain, FreeTrain=0, spt[L5.t]=Occupied, TrainTrack=0
WHEN L5.s=GetTrain, L5.t<>0, *npTO3.s=Free DO L5.s=GetCal, L5.wt=mst[L5.t]      {WHEN 116}
	$Color Track(16,16,1)=colt[L5.t]
WHEN L5.s=GetCal, L5.wt<>0, L5.p<>0                                             {WHEN 117}
 DO	L5.s=Calibrated, L5.wt=$SESSION, L5.wt=10+
WHEN L5.s=Calibrated, L5.t=*npTO3.t                                             {WHEN 118}
 DO	wait 0.7, L5.s=Occupied, L5.wt=$SESSION, L5.wt=10+
	$Color Block(16,16,1)=colt[L5.t]
WHEN L5.s=Calibrated, $SESSION>L5.wt                                            {WHEN 119}
 DO	spt[L5.t]=Stopped, L5.p=0, L5.s=Stopped, L5.wt=$TIME, L5.wt=5+
WHEN L4.s=Free, initialised=1, CurL4=TRUE                                       {WHEN 120}
 DO	L4.s=Stopped, L4.wt=$TIME, L4.wt=3+
WHEN L4.s=Free, initialised=1, CurL4=FALSE, OptL4=TRUE                          {WHEN 121}
 OR  initialised=1, L4.s=Free, CurL4=FALSE, OptOL4=TRUE
 DO	L4.s=WaitClear
WHEN L4.s=Stopped, $TIME>=L4.wt, initialised<>4, tt[L4.t]>=mxttL4               {WHEN 122}
 DO	L4.s=GetTrain
WHEN L4.s=GetTrain, L4.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=21         {WHEN 123}
WHEN L4.s=GetTrain, L4.t=0, FreeTrain<>0, TrainTrack=21                         {WHEN 124}
 DO	L4.t=FreeTrain, FreeTrain=0, spt[L4.t]=Occupied, TrainTrack=0
WHEN L4.s=GetTrain, L4.t<>0, *npN2L4.s=Free DO L4.s=GetCal, L4.wt=mst[L4.t]     {WHEN 125}
	$Color Track(12,20,1)=colt[L4.t]
WHEN L4.s=GetCal, L4.wt<>0, L4.p<>0                                             {WHEN 126}
 DO	L4.s=Calibrated, L4.wt=$SESSION, L4.wt=10+
WHEN L4.s=Calibrated, L4.t=*npN2L4.t                                            {WHEN 127}
 DO	wait 0.7, L4.s=Occupied, L4.wt=$SESSION, L4.wt=10+
	$Color Block(12,20,1)=colt[L4.t]
WHEN L4.s=Calibrated, $SESSION>L4.wt                                            {WHEN 128}
 DO	spt[L4.t]=Stopped, L4.p=0, L4.s=Stopped, L4.wt=$TIME, L4.wt=5+
WHEN L3.s=Free, initialised=1, CurL3=TRUE                                       {WHEN 129}
 DO	L3.s=Stopped, L3.wt=$TIME, L3.wt=3+
WHEN L3.s=Free, initialised=1, CurL3=FALSE, OptL3=TRUE                          {WHEN 130}
 OR  initialised=1, L3.s=Free, CurL3=FALSE, OptOL3=TRUE
 DO	L3.s=WaitClear
WHEN L3.s=Stopped, $TIME>=L3.wt, initialised<>4, tt[L3.t]>=mxttL3               {WHEN 131}
 DO	L3.s=GetTrain
WHEN L3.s=GetTrain, L3.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=22         {WHEN 132}
WHEN L3.s=GetTrain, L3.t=0, FreeTrain<>0, TrainTrack=22                         {WHEN 133}
 DO	L3.t=FreeTrain, FreeTrain=0, spt[L3.t]=Occupied, TrainTrack=0
WHEN L3.s=GetTrain, L3.t<>0, *npN2L3.s=Free DO L3.s=GetCal, L3.wt=mst[L3.t]     {WHEN 134}
	$Color Track(12,19,1)=colt[L3.t]
WHEN L3.s=GetCal, L3.wt<>0, L3.p<>0                                             {WHEN 135}
 DO	L3.s=Calibrated, L3.wt=$SESSION, L3.wt=10+
WHEN L3.s=Calibrated, L3.t=*npN2L3.t                                            {WHEN 136}
 DO	wait 0.7, L3.s=Occupied, L3.wt=$SESSION, L3.wt=10+
	$Color Block(12,19,1)=colt[L3.t]
WHEN L3.s=Calibrated, $SESSION>L3.wt                                            {WHEN 137}
 DO	spt[L3.t]=Stopped, L3.p=0, L3.s=Stopped, L3.wt=$TIME, L3.wt=5+
WHEN L2.s=Free, initialised=1, CurL2=TRUE                                       {WHEN 138}
 DO	L2.s=Stopped, L2.wt=$TIME, L2.wt=3+
WHEN L2.s=Free, initialised=1, CurL2=FALSE, OptL2=TRUE                          {WHEN 139}
 OR  initialised=1, L2.s=Free, CurL2=FALSE, OptOL2=TRUE
 DO	L2.s=WaitClear
WHEN L2.s=Stopped, $TIME>=L2.wt, initialised<>4 DO L2.s=GetTrain                {WHEN 140}
WHEN L2.s=GetTrain, L2.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=25         {WHEN 141}
WHEN L2.s=GetTrain, L2.t=0, FreeTrain<>0, TrainTrack=25                         {WHEN 142}
 DO	L2.t=FreeTrain, FreeTrain=0, spt[L2.t]=Occupied, TrainTrack=0
WHEN L2.s=GetTrain, L2.t<>0, *npN2L2.s=Free DO L2.s=GetCal, L2.wt=mst[L2.t]     {WHEN 143}
	$Color Track(3,19,1)=colt[L2.t]
WHEN L2.s=GetCal, L2.wt<>0, L2.p<>0                                             {WHEN 144}
 DO	L2.s=Calibrated, L2.wt=$SESSION, L2.wt=10+
WHEN L2.s=Calibrated, L2.t=*npN2L2.t                                            {WHEN 145}
 DO	wait 0.7, L2.s=Occupied, L2.wt=$SESSION, L2.wt=10+
	$Color Block(3,19,1)=colt[L2.t]
WHEN L2.s=Calibrated, $SESSION>L2.wt                                            {WHEN 146}
 DO	spt[L2.t]=Stopped, L2.p=0, L2.s=Stopped, L2.wt=$TIME, L2.wt=5+
WHEN L17.s=Free, initialised=1, CurL17=TRUE                                     {WHEN 147}
 DO	L17.s=Stopped, L17.wt=$TIME, L17.wt=3+
WHEN L17.s=Free, initialised=1, CurL17=FALSE, OptL17=TRUE                       {WHEN 148}
 OR  initialised=1, L17.s=Free, CurL17=FALSE, OptOL17=TRUE
 DO	L17.s=WaitClear
WHEN L17.s=Stopped, $TIME>=L17.wt, initialised<>4 DO L17.s=GetTrain             {WHEN 149}
WHEN L17.s=GetTrain, L17.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=27       {WHEN 150}
WHEN L17.s=GetTrain, L17.t=0, FreeTrain<>0, TrainTrack=27                       {WHEN 151}
 DO	L17.t=FreeTrain, FreeTrain=0, spt[L17.t]=Occupied, TrainTrack=0
WHEN L17.s=GetTrain, L17.t<>0, *lpTI2.s=Free DO L17.s=GetCal, L17.wt=mst[L17.t] {WHEN 152}
	$Color Track(7,12,1)=colt[L17.t]
WHEN L17.s=GetCal, L17.wt<>0, L17.p<>0                                          {WHEN 153}
 DO	L17.s=Calibrated, L17.wt=$SESSION, L17.wt=10+
WHEN L17.s=Calibrated, L17.t=*lpTI2.t                                           {WHEN 154}
 DO	wait 0.7, L17.s=Occupied, L17.wt=$SESSION, L17.wt=10+
	$Color Block(7,12,1)=colt[L17.t]
WHEN L17.s=Calibrated, $SESSION>L17.wt                                          {WHEN 155}
 DO	spt[L17.t]=Stopped, L17.p=0, L17.s=Stopped, L17.wt=$TIME, L17.wt=5+
WHEN L18.s=Free, initialised=1, CurL18=TRUE                                     {WHEN 156}
 DO	L18.s=Stopped, L18.wt=$TIME, L18.wt=3+
WHEN L18.s=Free, initialised=1, CurL18=FALSE, OptL18=TRUE                       {WHEN 157}
 OR  initialised=1, L18.s=Free, CurL18=FALSE, OptOL18=TRUE
 DO	L18.s=WaitClear
WHEN L18.s=Stopped, $TIME>=L18.wt, initialised<>4 DO L18.s=GetTrain             {WHEN 158}
WHEN L18.s=GetTrain, L18.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=31       {WHEN 159}
WHEN L18.s=GetTrain, L18.t=0, FreeTrain<>0, TrainTrack=31                       {WHEN 160}
 DO	L18.t=FreeTrain, FreeTrain=0, spt[L18.t]=Occupied, TrainTrack=0
WHEN L18.s=GetTrain, L18.t<>0, L19.s=Free DO L18.s=GetCal, L18.wt=mst[L18.t]    {WHEN 161}
	$Color Track(14,10,1)=colt[L18.t]
WHEN L18.s=GetCal, L18.wt<>0, L18.p<>0                                          {WHEN 162}
 DO	L18.s=Calibrated, L18.wt=$SESSION, L18.wt=10+
WHEN L18.s=Calibrated, L18.t=L19.t                                              {WHEN 163}
 DO	L18.s=Occupied, L18.wt=$SESSION, L18.wt=10+
	$Color Block(14,10,1)=colt[L18.t]
WHEN L18.s=Calibrated, $SESSION>L18.wt                                          {WHEN 164}
 DO	spt[L18.t]=Stopped, L18.p=0, L18.s=Stopped, L18.wt=$TIME, L18.wt=5+
WHEN L19.s=Free, initialised=1, CurL19=TRUE                                     {WHEN 165}
 DO	L19.s=Stopped, L19.wt=$TIME, L19.wt=3+
WHEN L19.s=Free, initialised=1, CurL19=FALSE, OptL19=TRUE                       {WHEN 166}
 OR  initialised=1, L19.s=Free, CurL19=FALSE, OptOL19=TRUE
 DO	L19.s=WaitClear
WHEN L19.s=Stopped, $TIME>=L19.wt, initialised<>4 DO L19.s=GetTrain             {WHEN 167}
WHEN L19.s=GetTrain, L19.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=32       {WHEN 168}
WHEN L19.s=GetTrain, L19.t=0, FreeTrain<>0, TrainTrack=32                       {WHEN 169}
 DO	L19.t=FreeTrain, FreeTrain=0, spt[L19.t]=Occupied, TrainTrack=0
WHEN L19.s=GetTrain, L19.t<>0, *npN1L19.s=Free                                  {WHEN 170}
 DO	L19.s=GetCal, L19.wt=mst[L19.t]
	$Color Track(14,7,1)=colt[L19.t]
WHEN L19.s=GetCal, L19.wt<>0, L19.p<>0                                          {WHEN 171}
 DO	L19.s=Calibrated, L19.wt=$SESSION, L19.wt=10+
WHEN L19.s=Calibrated, L19.t=*npN1L19.t                                         {WHEN 172}
 DO	wait 0.7, L19.s=Occupied, L19.wt=$SESSION, L19.wt=10+
	$Color Block(14,7,1)=colt[L19.t]
WHEN L19.s=Calibrated, $SESSION>L19.wt                                          {WHEN 173}
 DO	spt[L19.t]=Stopped, L19.p=0, L19.s=Stopped, L19.wt=$TIME, L19.wt=5+
WHEN L20.s=Free, initialised=1, CurL20=TRUE                                     {WHEN 174}
 DO	L20.s=Stopped, L20.wt=$TIME, L20.wt=3+
WHEN L20.s=Free, initialised=1, CurL20=FALSE, OptL20=TRUE                       {WHEN 175}
 OR  initialised=1, L20.s=Free, CurL20=FALSE, OptOL20=TRUE
 DO	L20.s=WaitClear
WHEN L20.s=Stopped, $TIME>=L20.wt, initialised<>4 DO L20.s=GetTrain             {WHEN 176}
WHEN L20.s=GetTrain, L20.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=33       {WHEN 177}
WHEN L20.s=GetTrain, L20.t=0, FreeTrain<>0, TrainTrack=33                       {WHEN 178}
 DO	L20.t=FreeTrain, FreeTrain=0, spt[L20.t]=Occupied, TrainTrack=0
WHEN L20.s=GetTrain, L20.t<>0, L21.s=Free DO L20.s=GetCal, L20.wt=mst[L20.t]    {WHEN 179}
	$Color Track(13,10,1)=colt[L20.t]
WHEN L20.s=GetCal, L20.wt<>0, L20.p<>0                                          {WHEN 180}
 DO	L20.s=Calibrated, L20.wt=$SESSION, L20.wt=10+
WHEN L20.s=Calibrated, L20.t=L21.t                                              {WHEN 181}
 DO	L20.s=Occupied, L20.wt=$SESSION, L20.wt=10+
	$Color Block(13,10,1)=colt[L20.t]
WHEN L20.s=Calibrated, $SESSION>L20.wt                                          {WHEN 182}
 DO	spt[L20.t]=Stopped, L20.p=0, L20.s=Stopped, L20.wt=$TIME, L20.wt=5+
WHEN L21.s=Free, initialised=1, CurL21=TRUE                                     {WHEN 183}
 DO	L21.s=Stopped, L21.wt=$TIME, L21.wt=3+
WHEN L21.s=Free, initialised=1, CurL21=FALSE, OptL21=TRUE                       {WHEN 184}
 OR  initialised=1, L21.s=Free, CurL21=FALSE, OptOL21=TRUE
 DO	L21.s=WaitClear
WHEN L21.s=Stopped, $TIME>=L21.wt, initialised<>4 DO L21.s=GetTrain             {WHEN 185}
WHEN L21.s=GetTrain, L21.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=35       {WHEN 186}
WHEN L21.s=GetTrain, L21.t=0, FreeTrain<>0, TrainTrack=35                       {WHEN 187}
 DO	L21.t=FreeTrain, FreeTrain=0, spt[L21.t]=Occupied, TrainTrack=0
WHEN L21.s=GetTrain, L21.t<>0, *npN1L21.s=Free                                  {WHEN 188}
 DO	L21.s=GetCal, L21.wt=mst[L21.t]
	$Color Track(13,7,1)=colt[L21.t]
WHEN L21.s=GetCal, L21.wt<>0, L21.p<>0                                          {WHEN 189}
 DO	L21.s=Calibrated, L21.wt=$SESSION, L21.wt=10+
WHEN L21.s=Calibrated, L21.t=*npN1L21.t                                         {WHEN 190}
 DO	wait 0.7, L21.s=Occupied, L21.wt=$SESSION, L21.wt=10+
	$Color Block(13,7,1)=colt[L21.t]
WHEN L21.s=Calibrated, $SESSION>L21.wt                                          {WHEN 191}
 DO	spt[L21.t]=Stopped, L21.p=0, L21.s=Stopped, L21.wt=$TIME, L21.wt=5+
WHEN P2.s=Free, initialised=1, CurP2=TRUE                                       {WHEN 192}
 DO	P2.s=Stopped, P2.wt=$TIME, P2.wt=3+
WHEN P2.s=Free, initialised=1, CurP2=FALSE, OptP2=TRUE                          {WHEN 193}
 OR  initialised=1, P2.s=Free, CurP2=FALSE, OptOP2=TRUE
 DO	P2.s=WaitClear
WHEN P2.s=Stopped, $TIME>=P2.wt, initialised<>4 DO P2.s=GetTrain                {WHEN 194}
WHEN P2.s=GetTrain, P2.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=44         {WHEN 195}
WHEN P2.s=GetTrain, P2.t=0, FreeTrain<>0, TrainTrack=44                         {WHEN 196}
 DO	P2.t=FreeTrain, FreeTrain=0, spt[P2.t]=Occupied, TrainTrack=0
WHEN P2.s=GetTrain, P2.t<>0, *npN2P2.s=Free DO P2.s=GetCal, P2.wt=mst[P2.t]     {WHEN 197}
	$Color Track(4,7,1)=colt[P2.t]
WHEN P2.s=GetCal, P2.wt<>0, P2.p<>0                                             {WHEN 198}
 DO	P2.s=Calibrated, P2.wt=$SESSION, P2.wt=10+
WHEN P2.s=Calibrated, P2.t=*npN2P2.t                                            {WHEN 199}
 DO	wait 0.7, P2.s=Occupied, P2.wt=$SESSION, P2.wt=10+
	$Color Block(4,7,1)=colt[P2.t]
WHEN P2.s=Calibrated, $SESSION>P2.wt                                            {WHEN 200}
 DO	spt[P2.t]=Stopped, P2.p=0, P2.s=Stopped, P2.wt=$TIME, P2.wt=5+
WHEN initialised=1 DO P1.s=Stopped, P1.wt=$TIME, P1.wt=99999+                   {WHEN 201}
WHEN P1.s=Stopped, $TIME>=P1.wt, initialised<>4 DO P1.s=GetTrain                {WHEN 202}
WHEN P1.s=GetTrain, P1.t=0, FreeTrain<>0, TrainTrack=0 DO TrainTrack=45         {WHEN 203}
WHEN P1.s=GetTrain, P1.t=0, FreeTrain<>0, TrainTrack=45                         {WHEN 204}
 DO	P1.t=FreeTrain, FreeTrain=0, spt[P1.t]=Occupied, TrainTrack=0
WHEN P1.s=GetTrain, P1.t<>0, *npN2P1.s=Free DO P1.s=GetCal, P1.wt=mst[P1.t]     {WHEN 205}
	$Color Track(1,1,1)=colt[P1.t]
WHEN P1.s=GetCal, P1.wt<>0, P1.p<>0                                             {WHEN 206}
 DO	P1.s=Calibrated, P1.wt=$SESSION, P1.wt=10+
WHEN P1.s=Calibrated, P1.t=*npN2P1.t                                            {WHEN 207}
 DO	wait 0.7, P1.s=Occupied, P1.wt=$SESSION, P1.wt=10+
	$Color Block(1,1,1)=colt[P1.t]
WHEN P1.s=Calibrated, $SESSION>P1.wt                                            {WHEN 208}
 DO	spt[P1.t]=Stopped, P1.p=0, P1.s=Stopped, P1.wt=$TIME, P1.wt=5+

{ Per-train display management code }
{ WHEN Goods button pressed DO Type=goods }
{ WHEN Local button pressed DO Type=local }
{ WHEN Passenger button pressed DO Type=passenger }
{ WHEN Express button pressed DO Type=express }
{ WHEN Train is free DO Clear board }
{ WHEN Train is occupied DO Set colour on board }
{ WHEN Min speed down pressed DO reduce min speed }
{ WHEN Min speed up pressed DO increase min speed }
{ WHEN Min speed changes DO Update screen }
{ WHEN Measured min speed changes DO Update screen }
{ WHEN Full speed down pressed DO reduce full speed }
{ WHEN Full speed up pressed DO increase full speed }
{ WHEN Full speed changes DO Update screen }
{ WHEN Measured full speed changes DO Update screen }

WHEN $Left_Mouse=(4,2,2)                                                        {WHEN 209}
 DO	tt[1]=Goods, $Erase Message(4,2,2), $Draw Message(4,2,2)=" Goods"
WHEN $Left_Mouse=(5,2,2)                                                        {WHEN 210}
 DO	tt[1]=Local, $Erase Message(4,2,2), $Draw Message(4,2,2)=" Local"
WHEN $Left_Mouse=(6,2,2)                                                        {WHEN 211}
 DO	tt[1]=Passenger, $Erase Message(4,2,2), $Draw Message(4,2,2)=" Passenger"
WHEN $Left_Mouse=(7,2,2)                                                        {WHEN 212}
 DO	tt[1]=Express, $Erase Message(4,2,2), $Draw Message(4,2,2)=" Express"
WHEN initialised=2, spt[1]=Free                                                 {WHEN 213}
 DO	$Color Track(2, 2, 2)=coltx, $Color Track(3, 2, 2)=coltx, $Erase Message(4, 2, 2)
WHEN spt[1]<>Free DO $Color Track(2, 2, 2)=0, $Color Track(3, 2, 2)=colt[1]     {WHEN 214}
WHILE $Left_Mouse=(8,2,2), mst[1]>1 DO mst[1]=2-                                {WHEN 215}
WHILE $Right_Mouse=(8,2,2), mst[1]<fst[1] DO mst[1]=2+                          {WHEN 216}
WHILE mst[1]<>omst[1]                                                            {WHEN 217}
 DO	$Erase Message(8,2,2), omst[1]=mst[1], $Draw Message(8,2,2)="@omst[1]"
WHILE mmst[1]<>ommst[1]                                                          {WHEN 218}
 DO	$Erase Message(9,2,2), ommst[1]=mmst[1], $Draw Message(9,2,2)="@ommst[1]"
WHILE $Left_Mouse=(10,2,2), fst[1]>mst[1] DO fst[1]=2-                          {WHEN 219}
WHILE $Right_Mouse=(10,2,2), fst[1]<99 DO fst[1]=2+                             {WHEN 220}
WHILE fst[1]<>ofst[1]                                                            {WHEN 221}
 DO	$Erase Message(10,2,2), ofst[1]=fst[1], $Draw Message(10,2,2)="@ofst[1]"
WHILE mfst[1]<>omfst[1]                                                          {WHEN 222}
 DO	$Erase Message(11,2,2), omfst[1]=mfst[1], $Draw Message(11,2,2)="@omfst[1]"
WHEN $Left_Mouse=(4,3,2)                                                        {WHEN 223}
 DO	tt[2]=Goods, $Erase Message(4,3,2), $Draw Message(4,3,2)=" Goods"
WHEN $Left_Mouse=(5,3,2)                                                        {WHEN 224}
 DO	tt[2]=Local, $Erase Message(4,3,2), $Draw Message(4,3,2)=" Local"
WHEN $Left_Mouse=(6,3,2)                                                        {WHEN 225}
 DO	tt[2]=Passenger, $Erase Message(4,3,2), $Draw Message(4,3,2)=" Passenger"
WHEN $Left_Mouse=(7,3,2)                                                        {WHEN 226}
 DO	tt[2]=Express, $Erase Message(4,3,2), $Draw Message(4,3,2)=" Express"
WHEN initialised=2, spt[2]=Free                                                 {WHEN 227}
 DO	$Color Track(2, 3, 2)=coltx, $Color Track(3, 3, 2)=coltx, $Erase Message(4, 3, 2)
WHEN spt[2]<>Free DO $Color Track(2, 3, 2)=0, $Color Track(3, 3, 2)=colt[2]     {WHEN 228}
WHILE $Left_Mouse=(8,3,2), mst[2]>1 DO mst[2]=2-                                {WHEN 229}
WHILE $Right_Mouse=(8,3,2), mst[2]<fst[2] DO mst[2]=2+                          {WHEN 230}
WHILE mst[2]<>omst[2]                                                            {WHEN 231}
 DO	$Erase Message(8,3,2), omst[2]=mst[2], $Draw Message(8,3,2)="@omst[2]"
WHILE mmst[2]<>ommst[2]                                                          {WHEN 232}
 DO	$Erase Message(9,3,2), ommst[2]=mmst[2], $Draw Message(9,3,2)="@ommst[2]"
WHILE $Left_Mouse=(10,3,2), fst[2]>mst[2] DO fst[2]=2-                          {WHEN 233}
WHILE $Right_Mouse=(10,3,2), fst[2]<99 DO fst[2]=2+                             {WHEN 234}
WHILE fst[2]<>ofst[2]                                                            {WHEN 235}
 DO	$Erase Message(10,3,2), ofst[2]=fst[2], $Draw Message(10,3,2)="@ofst[2]"
WHILE mfst[2]<>omfst[2]                                                          {WHEN 236}
 DO	$Erase Message(11,3,2), omfst[2]=mfst[2], $Draw Message(11,3,2)="@omfst[2]"
WHEN $Left_Mouse=(4,4,2)                                                        {WHEN 237}
 DO	tt[3]=Goods, $Erase Message(4,4,2), $Draw Message(4,4,2)=" Goods"
WHEN $Left_Mouse=(5,4,2)                                                        {WHEN 238}
 DO	tt[3]=Local, $Erase Message(4,4,2), $Draw Message(4,4,2)=" Local"
WHEN $Left_Mouse=(6,4,2)                                                        {WHEN 239}
 DO	tt[3]=Passenger, $Erase Message(4,4,2), $Draw Message(4,4,2)=" Passenger"
WHEN $Left_Mouse=(7,4,2)                                                        {WHEN 240}
 DO	tt[3]=Express, $Erase Message(4,4,2), $Draw Message(4,4,2)=" Express"
WHEN initialised=2, spt[3]=Free                                                 {WHEN 241}
 DO	$Color Track(2, 4, 2)=coltx, $Color Track(3, 4, 2)=coltx, $Erase Message(4, 4, 2)
WHEN spt[3]<>Free DO $Color Track(2, 4, 2)=0, $Color Track(3, 4, 2)=colt[3]     {WHEN 242}
WHILE $Left_Mouse=(8,4,2), mst[3]>1 DO mst[3]=2-                                {WHEN 243}
WHILE $Right_Mouse=(8,4,2), mst[3]<fst[3] DO mst[3]=2+                          {WHEN 244}
WHILE mst[3]<>omst[3]                                                            {WHEN 245}
 DO	$Erase Message(8,4,2), omst[3]=mst[3], $Draw Message(8,4,2)="@omst[3]"
WHILE mmst[3]<>ommst[3]                                                          {WHEN 246}
 DO	$Erase Message(9,4,2), ommst[3]=mmst[3], $Draw Message(9,4,2)="@ommst[3]"
WHILE $Left_Mouse=(10,4,2), fst[3]>mst[3] DO fst[3]=2-                          {WHEN 247}
WHILE $Right_Mouse=(10,4,2), fst[3]<99 DO fst[3]=2+                             {WHEN 248}
WHILE fst[3]<>ofst[3]                                                            {WHEN 249}
 DO	$Erase Message(10,4,2), ofst[3]=fst[3], $Draw Message(10,4,2)="@ofst[3]"
WHILE mfst[3]<>omfst[3]                                                          {WHEN 250}
 DO	$Erase Message(11,4,2), omfst[3]=mfst[3], $Draw Message(11,4,2)="@omfst[3]"
WHEN $Left_Mouse=(4,5,2)                                                        {WHEN 251}
 DO	tt[4]=Goods, $Erase Message(4,5,2), $Draw Message(4,5,2)=" Goods"
WHEN $Left_Mouse=(5,5,2)                                                        {WHEN 252}
 DO	tt[4]=Local, $Erase Message(4,5,2), $Draw Message(4,5,2)=" Local"
WHEN $Left_Mouse=(6,5,2)                                                        {WHEN 253}
 DO	tt[4]=Passenger, $Erase Message(4,5,2), $Draw Message(4,5,2)=" Passenger"
WHEN $Left_Mouse=(7,5,2)                                                        {WHEN 254}
 DO	tt[4]=Express, $Erase Message(4,5,2), $Draw Message(4,5,2)=" Express"
WHEN initialised=2, spt[4]=Free                                                 {WHEN 255}
 DO	$Color Track(2, 5, 2)=coltx, $Color Track(3, 5, 2)=coltx, $Erase Message(4, 5, 2)
WHEN spt[4]<>Free DO $Color Track(2, 5, 2)=0, $Color Track(3, 5, 2)=colt[4]     {WHEN 256}
WHILE $Left_Mouse=(8,5,2), mst[4]>1 DO mst[4]=2-                                {WHEN 257}
WHILE $Right_Mouse=(8,5,2), mst[4]<fst[4] DO mst[4]=2+                          {WHEN 258}
WHILE mst[4]<>omst[4]                                                            {WHEN 259}
 DO	$Erase Message(8,5,2), omst[4]=mst[4], $Draw Message(8,5,2)="@omst[4]"
WHILE mmst[4]<>ommst[4]                                                          {WHEN 260}
 DO	$Erase Message(9,5,2), ommst[4]=mmst[4], $Draw Message(9,5,2)="@ommst[4]"
WHILE $Left_Mouse=(10,5,2), fst[4]>mst[4] DO fst[4]=2-                          {WHEN 261}
WHILE $Right_Mouse=(10,5,2), fst[4]<99 DO fst[4]=2+                             {WHEN 262}
WHILE fst[4]<>ofst[4]                                                            {WHEN 263}
 DO	$Erase Message(10,5,2), ofst[4]=fst[4], $Draw Message(10,5,2)="@ofst[4]"
WHILE mfst[4]<>omfst[4]                                                          {WHEN 264}
 DO	$Erase Message(11,5,2), omfst[4]=mfst[4], $Draw Message(11,5,2)="@omfst[4]"
WHEN $Left_Mouse=(4,6,2)                                                        {WHEN 265}
 DO	tt[5]=Goods, $Erase Message(4,6,2), $Draw Message(4,6,2)=" Goods"
WHEN $Left_Mouse=(5,6,2)                                                        {WHEN 266}
 DO	tt[5]=Local, $Erase Message(4,6,2), $Draw Message(4,6,2)=" Local"
WHEN $Left_Mouse=(6,6,2)                                                        {WHEN 267}
 DO	tt[5]=Passenger, $Erase Message(4,6,2), $Draw Message(4,6,2)=" Passenger"
WHEN $Left_Mouse=(7,6,2)                                                        {WHEN 268}
 DO	tt[5]=Express, $Erase Message(4,6,2), $Draw Message(4,6,2)=" Express"
WHEN initialised=2, spt[5]=Free                                                 {WHEN 269}
 DO	$Color Track(2, 6, 2)=coltx, $Color Track(3, 6, 2)=coltx, $Erase Message(4, 6, 2)
WHEN spt[5]<>Free DO $Color Track(2, 6, 2)=0, $Color Track(3, 6, 2)=colt[5]     {WHEN 270}
WHILE $Left_Mouse=(8,6,2), mst[5]>1 DO mst[5]=2-                                {WHEN 271}
WHILE $Right_Mouse=(8,6,2), mst[5]<fst[5] DO mst[5]=2+                          {WHEN 272}
WHILE mst[5]<>omst[5]                                                            {WHEN 273}
 DO	$Erase Message(8,6,2), omst[5]=mst[5], $Draw Message(8,6,2)="@omst[5]"
WHILE mmst[5]<>ommst[5]                                                          {WHEN 274}
 DO	$Erase Message(9,6,2), ommst[5]=mmst[5], $Draw Message(9,6,2)="@ommst[5]"
WHILE $Left_Mouse=(10,6,2), fst[5]>mst[5] DO fst[5]=2-                          {WHEN 275}
WHILE $Right_Mouse=(10,6,2), fst[5]<99 DO fst[5]=2+                             {WHEN 276}
WHILE fst[5]<>ofst[5]                                                            {WHEN 277}
 DO	$Erase Message(10,6,2), ofst[5]=fst[5], $Draw Message(10,6,2)="@ofst[5]"
WHILE mfst[5]<>omfst[5]                                                          {WHEN 278}
 DO	$Erase Message(11,6,2), omfst[5]=mfst[5], $Draw Message(11,6,2)="@omfst[5]"
WHEN $Left_Mouse=(4,7,2)                                                        {WHEN 279}
 DO	tt[6]=Goods, $Erase Message(4,7,2), $Draw Message(4,7,2)=" Goods"
WHEN $Left_Mouse=(5,7,2)                                                        {WHEN 280}
 DO	tt[6]=Local, $Erase Message(4,7,2), $Draw Message(4,7,2)=" Local"
WHEN $Left_Mouse=(6,7,2)                                                        {WHEN 281}
 DO	tt[6]=Passenger, $Erase Message(4,7,2), $Draw Message(4,7,2)=" Passenger"
WHEN $Left_Mouse=(7,7,2)                                                        {WHEN 282}
 DO	tt[6]=Express, $Erase Message(4,7,2), $Draw Message(4,7,2)=" Express"
WHEN initialised=2, spt[6]=Free                                                 {WHEN 283}
 DO	$Color Track(2, 7, 2)=coltx, $Color Track(3, 7, 2)=coltx, $Erase Message(4, 7, 2)
WHEN spt[6]<>Free DO $Color Track(2, 7, 2)=0, $Color Track(3, 7, 2)=colt[6]     {WHEN 284}
WHILE $Left_Mouse=(8,7,2), mst[6]>1 DO mst[6]=2-                                {WHEN 285}
WHILE $Right_Mouse=(8,7,2), mst[6]<fst[6] DO mst[6]=2+                          {WHEN 286}
WHILE mst[6]<>omst[6]                                                            {WHEN 287}
 DO	$Erase Message(8,7,2), omst[6]=mst[6], $Draw Message(8,7,2)="@omst[6]"
WHILE mmst[6]<>ommst[6]                                                          {WHEN 288}
 DO	$Erase Message(9,7,2), ommst[6]=mmst[6], $Draw Message(9,7,2)="@ommst[6]"
WHILE $Left_Mouse=(10,7,2), fst[6]>mst[6] DO fst[6]=2-                          {WHEN 289}
WHILE $Right_Mouse=(10,7,2), fst[6]<99 DO fst[6]=2+                             {WHEN 290}
WHILE fst[6]<>ofst[6]                                                            {WHEN 291}
 DO	$Erase Message(10,7,2), ofst[6]=fst[6], $Draw Message(10,7,2)="@ofst[6]"
WHILE mfst[6]<>omfst[6]                                                          {WHEN 292}
 DO	$Erase Message(11,7,2), omfst[6]=mfst[6], $Draw Message(11,7,2)="@omfst[6]"
{ WHEN Up momentum down pressed DO reduce & display }
{ WHEN Up momentum up pressed DO increase & display }
{ WHEN Down momentum down pressed DO reduce & display }
{ WHEN Down momentum up pressed DO increase & display }
WHILE $Left_Mouse=(6,1,3), upmom>0                                               {WHEN 293}
 DO	$Erase Message(6,1,3), upmom=-, $Draw Message(6,1,3)="@upmom"
WHILE $Right_Mouse=(6,1,3), upmom<8                                              {WHEN 294}
 DO	$Erase Message(6,1,3), upmom=+, $Draw Message(6,1,3)="@upmom"
WHILE $Left_Mouse=(6,2,3), downmom>0                                             {WHEN 295}
 DO	$Erase Message(6,2,3), downmom=-, $Draw Message(6,2,3)="@downmom"
WHILE $Right_Mouse=(6,2,3), downmom<8                                            {WHEN 296}
 DO	$Erase Message(6,2,3), downmom=+, $Draw Message(6,2,3)="@downmom"

{ WHEN train is free DO advertise self }

WHEN spt[1]=Free, FreeTrain=0 DO FreeTrain=1                                    {WHEN 297}
WHEN spt[1]<>Free, spt[2]=Free, FreeTrain=0 DO FreeTrain=2                      {WHEN 298}
WHEN spt[1]<>Free, spt[2]<>0, spt[3]=Free, FreeTrain=0 DO FreeTrain=3           {WHEN 299}
WHEN spt[1]<>Free, spt[2]<>0, spt[3]<>0, spt[4]=Free, FreeTrain=0               {WHEN 300}
 DO	FreeTrain=4
WHEN spt[1]<>Free, spt[2]<>0, spt[3]<>0, spt[4]<>0, spt[5]=Free, FreeTrain=0    {WHEN 301}
 DO	FreeTrain=5
WHEN spt[1]<>Free, spt[2]<>0, spt[3]<>0, spt[4]<>0, spt[5]<>0, spt[6]=Free, FreeTrain=0
 DO	FreeTrain=6                                                             {WHEN 302}

{ Per-train operational code }

{ WHEN TrainDir=Fwd DO cab forward }
{ WHEN TrainDir=Rev DO cab reverse }
{ WHEN Cab=Free or stopped DO speed 0, forward, brake off }
{ WHEN Cab=Brake & not changing OR Pause DO Brake_ON }
{ WHEN Cab=Brake & changing DO Brake_ON both cabs }
{ WHEN Cab=MinSpeed & not changing DO Speed=mst, brake off, downmom }
{ WHEN Cab=MinSpeed & changing DO Speed=mst, brake off, downmom on old cab, new=mst }
{ WHEN Cab=FullSpeed & not changing DO Speed=fst, brake off, upmom }
{ WHEN Cab=FullSpeed from stopped DO Speed=mst...fst, brake off, upmom }
{ WHEN cab switch time DO copy settings and switchover }
{ WHEN Cab=CalCount, timeout expired DO increase speed, reset timeout }
{ WHEN Cab=EndCal DO set mst=speed, fst=mst+5, Brake }

WHEN tdirt[1]=Fwd, cabt[1]<>0 DO c=cabt[1], cab[c]=(FORWARD), ct1=(FORWARD)     {WHEN 303}
WHEN tdirt[1]=Rev, cabt[1]<>0 DO c=cabt[1], cab[c]=(REVERSE), ct1=(REVERSE)     {WHEN 304}
WHEN spt[1]=Free, ncabt[1]=0, cabt[1]<>0                                        {WHEN 305}
 OR  spt[1]=Stopped, ncabt[1]=0, cabt[1]<>0
 DO	c=cabt[1], cab[c]=0(BRAKE_OFF, MOMENTUM_0)
	ct1=cab[c], tdirt[1]=Fwd, ncabt[1]=0
	tc[c]=0, cabt[1]=0, ospt[1]=spt[1]
WHEN spt[1]=Braking, spt[1]<>ospt[1], cabt[1]<>0, ncabt[1]=0 OR initialised=5   {WHEN 306}
 DO	c=cabt[1], cab[c]=0(BRAKE_ON, MOMENTUM_1),
	ospt[1]=spt[1], ct1=cab[c]
WHEN spt[1]=Braking, spt[1]<>ospt[1], cabt[1]<>0, ncabt[1]<>0                   {WHEN 307}
 DO	c=cabt[1], cab[c]=0(BRAKE_ON, MOMENTUM_1), ct1=cab[c],
	d=ncabt[1], cab[d]=cab[c], ospt[1]=spt[1]
WHEN spt[1]=DoMin, spt[1]<>ospt[1], cabt[1]<>0, ncabt[1]=0                      {WHEN 308}
 DO	c=cabt[1], cab[c]=mst[1](BRAKE_OFF), d=&cab[c],d=3+,*d=downmom, ospt[1]=spt[1]
	ct1=cab[c]
WHEN spt[1]=DoMin, spt[1]<>ospt[1], cabt[1]<>0, ncabt[1]<>0, initialised=2      {WHEN 309}
 DO	c=cabt[1], d=&cab[c], *d=mst[1],d=3+,*d=downmom, d=ncabt[1], cab[d]=cab[c],
	ospt[1]=spt[1], ct1=cab[c]
WHEN spt[1]=DoFull, ospt[1]>=DoMin, ospt[1]<=DoFull, cabt[1]<>0, ncabt[1]=0, initialised=2{WHEN 310}
 DO	c=cabt[1], cab[c]=fst[1](BRAKE_OFF), d=&cab[c],d=3+,*d=upmom, ospt[1]=spt[1]
	ct1=cab[c], spt[1]=Booked
WHEN spt[1]=DoFull, ospt[1]<>DoMin, ospt[1]<>DoFull, cabt[1]<>0, ncabt[1]=0, initialised=2
 DO	c=cabt[1], cab[c]=mst[1](BRAKE_OFF, MOMENTUM_1),                        {WHEN 311}
	ct1=cab[c], wait 1
	c=cabt[1], d=&cab[c], *d=fst[1], d=3+,*d=upmom
	ospt[1]=spt[1], ct1=cab[c], spt[1]=Booked
WHEN ncabt[1]<>0 DO c=cabt[1], d=ncabt[1], cab[d]=cab[c],                       {WHEN 312}
	wait 0.2, d=ncabt[1], tc[d]=1, wait 0.2
	c=cabt[1], tc[c]=99, cabt[1]=ncabt[1], ncabt[1]=0
WHILE spt[1]=CalCount, cabt[1]<>99                                               {WHEN 313}
 DO	c=cabt[1], cab[c]=2+(BRAKE_OFF), ct1=cab[c], wait 1
WHEN spt[1]=EndCal                                                              {WHEN 314}
 DO	c=cabt[1], mst[1]=cab[c], fst[1]=mst[1], fst[1]=10+, spt[1]=Braking
WHEN tdirt[2]=Fwd, cabt[2]<>0 DO c=cabt[2], cab[c]=(FORWARD), ct2=(FORWARD)     {WHEN 315}
WHEN tdirt[2]=Rev, cabt[2]<>0 DO c=cabt[2], cab[c]=(REVERSE), ct2=(REVERSE)     {WHEN 316}
WHEN spt[2]=Free, ncabt[2]=0, cabt[2]<>0                                        {WHEN 317}
 OR  spt[2]=Stopped, ncabt[2]=0, cabt[2]<>0
 DO	c=cabt[2], cab[c]=0(BRAKE_OFF, MOMENTUM_0)
	ct2=cab[c], tdirt[2]=Fwd, ncabt[2]=0
	tc[c]=0, cabt[2]=0, ospt[2]=spt[2]
WHEN spt[2]=Braking, spt[2]<>ospt[2], cabt[2]<>0, ncabt[2]=0 OR initialised=5   {WHEN 318}
 DO	c=cabt[2], cab[c]=0(BRAKE_ON, MOMENTUM_1),
	ospt[2]=spt[2], ct2=cab[c]
WHEN spt[2]=Braking, spt[2]<>ospt[2], cabt[2]<>0, ncabt[2]<>0                   {WHEN 319}
 DO	c=cabt[2], cab[c]=0(BRAKE_ON, MOMENTUM_1), ct2=cab[c],
	d=ncabt[2], cab[d]=cab[c], ospt[2]=spt[2]
WHEN spt[2]=DoMin, spt[2]<>ospt[2], cabt[2]<>0, ncabt[2]=0                      {WHEN 320}
 DO	c=cabt[2], cab[c]=mst[2](BRAKE_OFF), d=&cab[c],d=3+,*d=downmom, ospt[2]=spt[2]
	ct2=cab[c]
WHEN spt[2]=DoMin, spt[2]<>ospt[2], cabt[2]<>0, ncabt[2]<>0, initialised=2      {WHEN 321}
 DO	c=cabt[2], d=&cab[c], *d=mst[2],d=3+,*d=downmom, d=ncabt[2], cab[d]=cab[c],
	ospt[2]=spt[2], ct2=cab[c]
WHEN spt[2]=DoFull, ospt[2]>=DoMin, ospt[2]<=DoFull, cabt[2]<>0, ncabt[2]=0, initialised=2{WHEN 322}
 DO	c=cabt[2], cab[c]=fst[2](BRAKE_OFF), d=&cab[c],d=3+,*d=upmom, ospt[2]=spt[2]
	ct2=cab[c], spt[2]=Booked
WHEN spt[2]=DoFull, ospt[2]<>DoMin, ospt[2]<>DoFull, cabt[2]<>0, ncabt[2]=0, initialised=2
 DO	c=cabt[2], cab[c]=mst[2](BRAKE_OFF, MOMENTUM_1),                        {WHEN 323}
	ct2=cab[c], wait 1
	c=cabt[2], d=&cab[c], *d=fst[2], d=3+,*d=upmom
	ospt[2]=spt[2], ct2=cab[c], spt[2]=Booked
WHEN ncabt[2]<>0 DO c=cabt[2], d=ncabt[2], cab[d]=cab[c],                       {WHEN 324}
	wait 0.2, d=ncabt[2], tc[d]=2, wait 0.2
	c=cabt[2], tc[c]=99, cabt[2]=ncabt[2], ncabt[2]=0
WHILE spt[2]=CalCount, cabt[2]<>99                                               {WHEN 325}
 DO	c=cabt[2], cab[c]=2+(BRAKE_OFF), ct2=cab[c], wait 1
WHEN spt[2]=EndCal                                                              {WHEN 326}
 DO	c=cabt[2], mst[2]=cab[c], fst[2]=mst[2], fst[2]=10+, spt[2]=Braking
WHEN tdirt[3]=Fwd, cabt[3]<>0 DO c=cabt[3], cab[c]=(FORWARD), ct3=(FORWARD)     {WHEN 327}
WHEN tdirt[3]=Rev, cabt[3]<>0 DO c=cabt[3], cab[c]=(REVERSE), ct3=(REVERSE)     {WHEN 328}
WHEN spt[3]=Free, ncabt[3]=0, cabt[3]<>0                                        {WHEN 329}
 OR  spt[3]=Stopped, ncabt[3]=0, cabt[3]<>0
 DO	c=cabt[3], cab[c]=0(BRAKE_OFF, MOMENTUM_0)
	ct3=cab[c], tdirt[3]=Fwd, ncabt[3]=0
	tc[c]=0, cabt[3]=0, ospt[3]=spt[3]
WHEN spt[3]=Braking, spt[3]<>ospt[3], cabt[3]<>0, ncabt[3]=0 OR initialised=5   {WHEN 330}
 DO	c=cabt[3], cab[c]=0(BRAKE_ON, MOMENTUM_1),
	ospt[3]=spt[3], ct3=cab[c]
WHEN spt[3]=Braking, spt[3]<>ospt[3], cabt[3]<>0, ncabt[3]<>0                   {WHEN 331}
 DO	c=cabt[3], cab[c]=0(BRAKE_ON, MOMENTUM_1), ct3=cab[c],
	d=ncabt[3], cab[d]=cab[c], ospt[3]=spt[3]
WHEN spt[3]=DoMin, spt[3]<>ospt[3], cabt[3]<>0, ncabt[3]=0                      {WHEN 332}
 DO	c=cabt[3], cab[c]=mst[3](BRAKE_OFF), d=&cab[c],d=3+,*d=downmom, ospt[3]=spt[3]
	ct3=cab[c]
WHEN spt[3]=DoMin, spt[3]<>ospt[3], cabt[3]<>0, ncabt[3]<>0, initialised=2      {WHEN 333}
 DO	c=cabt[3], d=&cab[c], *d=mst[3],d=3+,*d=downmom, d=ncabt[3], cab[d]=cab[c],
	ospt[3]=spt[3], ct3=cab[c]
WHEN spt[3]=DoFull, ospt[3]>=DoMin, ospt[3]<=DoFull, cabt[3]<>0, ncabt[3]=0, initialised=2{WHEN 334}
 DO	c=cabt[3], cab[c]=fst[3](BRAKE_OFF), d=&cab[c],d=3+,*d=upmom, ospt[3]=spt[3]
	ct3=cab[c], spt[3]=Booked
WHEN spt[3]=DoFull, ospt[3]<>DoMin, ospt[3]<>DoFull, cabt[3]<>0, ncabt[3]=0, initialised=2
 DO	c=cabt[3], cab[c]=mst[3](BRAKE_OFF, MOMENTUM_1),                        {WHEN 335}
	ct3=cab[c], wait 1
	c=cabt[3], d=&cab[c], *d=fst[3], d=3+,*d=upmom
	ospt[3]=spt[3], ct3=cab[c], spt[3]=Booked
WHEN ncabt[3]<>0 DO c=cabt[3], d=ncabt[3], cab[d]=cab[c],                       {WHEN 336}
	wait 0.2, d=ncabt[3], tc[d]=3, wait 0.2
	c=cabt[3], tc[c]=99, cabt[3]=ncabt[3], ncabt[3]=0
WHILE spt[3]=CalCount, cabt[3]<>99                                               {WHEN 337}
 DO	c=cabt[3], cab[c]=2+(BRAKE_OFF), ct3=cab[c], wait 1
WHEN spt[3]=EndCal                                                              {WHEN 338}
 DO	c=cabt[3], mst[3]=cab[c], fst[3]=mst[3], fst[3]=10+, spt[3]=Braking
WHEN tdirt[4]=Fwd, cabt[4]<>0 DO c=cabt[4], cab[c]=(FORWARD), ct4=(FORWARD)     {WHEN 339}
WHEN tdirt[4]=Rev, cabt[4]<>0 DO c=cabt[4], cab[c]=(REVERSE), ct4=(REVERSE)     {WHEN 340}
WHEN spt[4]=Free, ncabt[4]=0, cabt[4]<>0                                        {WHEN 341}
 OR  spt[4]=Stopped, ncabt[4]=0, cabt[4]<>0
 DO	c=cabt[4], cab[c]=0(BRAKE_OFF, MOMENTUM_0)
	ct4=cab[c], tdirt[4]=Fwd, ncabt[4]=0
	tc[c]=0, cabt[4]=0, ospt[4]=spt[4]
WHEN spt[4]=Braking, spt[4]<>ospt[4], cabt[4]<>0, ncabt[4]=0 OR initialised=5   {WHEN 342}
 DO	c=cabt[4], cab[c]=0(BRAKE_ON, MOMENTUM_1),
	ospt[4]=spt[4], ct4=cab[c]
WHEN spt[4]=Braking, spt[4]<>ospt[4], cabt[4]<>0, ncabt[4]<>0                   {WHEN 343}
 DO	c=cabt[4], cab[c]=0(BRAKE_ON, MOMENTUM_1), ct4=cab[c],
	d=ncabt[4], cab[d]=cab[c], ospt[4]=spt[4]
WHEN spt[4]=DoMin, spt[4]<>ospt[4], cabt[4]<>0, ncabt[4]=0                      {WHEN 344}
 DO	c=cabt[4], cab[c]=mst[4](BRAKE_OFF), d=&cab[c],d=3+,*d=downmom, ospt[4]=spt[4]
	ct4=cab[c]
WHEN spt[4]=DoMin, spt[4]<>ospt[4], cabt[4]<>0, ncabt[4]<>0, initialised=2      {WHEN 345}
 DO	c=cabt[4], d=&cab[c], *d=mst[4],d=3+,*d=downmom, d=ncabt[4], cab[d]=cab[c],
	ospt[4]=spt[4], ct4=cab[c]
WHEN spt[4]=DoFull, ospt[4]>=DoMin, ospt[4]<=DoFull, cabt[4]<>0, ncabt[4]=0, initialised=2{WHEN 346}
 DO	c=cabt[4], cab[c]=fst[4](BRAKE_OFF), d=&cab[c],d=3+,*d=upmom, ospt[4]=spt[4]
	ct4=cab[c], spt[4]=Booked
WHEN spt[4]=DoFull, ospt[4]<>DoMin, ospt[4]<>DoFull, cabt[4]<>0, ncabt[4]=0, initialised=2
 DO	c=cabt[4], cab[c]=mst[4](BRAKE_OFF, MOMENTUM_1),                        {WHEN 347}
	ct4=cab[c], wait 1
	c=cabt[4], d=&cab[c], *d=fst[4], d=3+,*d=upmom
	ospt[4]=spt[4], ct4=cab[c], spt[4]=Booked
WHEN ncabt[4]<>0 DO c=cabt[4], d=ncabt[4], cab[d]=cab[c],                       {WHEN 348}
	wait 0.2, d=ncabt[4], tc[d]=4, wait 0.2
	c=cabt[4], tc[c]=99, cabt[4]=ncabt[4], ncabt[4]=0
WHILE spt[4]=CalCount, cabt[4]<>99                                               {WHEN 349}
 DO	c=cabt[4], cab[c]=2+(BRAKE_OFF), ct4=cab[c], wait 1
WHEN spt[4]=EndCal                                                              {WHEN 350}
 DO	c=cabt[4], mst[4]=cab[c], fst[4]=mst[4], fst[4]=10+, spt[4]=Braking
WHEN tdirt[5]=Fwd, cabt[5]<>0 DO c=cabt[5], cab[c]=(FORWARD), ct5=(FORWARD)     {WHEN 351}
WHEN tdirt[5]=Rev, cabt[5]<>0 DO c=cabt[5], cab[c]=(REVERSE), ct5=(REVERSE)     {WHEN 352}
WHEN spt[5]=Free, ncabt[5]=0, cabt[5]<>0                                        {WHEN 353}
 OR  spt[5]=Stopped, ncabt[5]=0, cabt[5]<>0
 DO	c=cabt[5], cab[c]=0(BRAKE_OFF, MOMENTUM_0)
	ct5=cab[c], tdirt[5]=Fwd, ncabt[5]=0
	tc[c]=0, cabt[5]=0, ospt[5]=spt[5]
WHEN spt[5]=Braking, spt[5]<>ospt[5], cabt[5]<>0, ncabt[5]=0 OR initialised=5   {WHEN 354}
 DO	c=cabt[5], cab[c]=0(BRAKE_ON, MOMENTUM_1),
	ospt[5]=spt[5], ct5=cab[c]
WHEN spt[5]=Braking, spt[5]<>ospt[5], cabt[5]<>0, ncabt[5]<>0                   {WHEN 355}
 DO	c=cabt[5], cab[c]=0(BRAKE_ON, MOMENTUM_1), ct5=cab[c],
	d=ncabt[5], cab[d]=cab[c], ospt[5]=spt[5]
WHEN spt[5]=DoMin, spt[5]<>ospt[5], cabt[5]<>0, ncabt[5]=0                      {WHEN 356}
 DO	c=cabt[5], cab[c]=mst[5](BRAKE_OFF), d=&cab[c],d=3+,*d=downmom, ospt[5]=spt[5]
	ct5=cab[c]
WHEN spt[5]=DoMin, spt[5]<>ospt[5], cabt[5]<>0, ncabt[5]<>0, initialised=2      {WHEN 357}
 DO	c=cabt[5], d=&cab[c], *d=mst[5],d=3+,*d=downmom, d=ncabt[5], cab[d]=cab[c],
	ospt[5]=spt[5], ct5=cab[c]
WHEN spt[5]=DoFull, ospt[5]>=DoMin, ospt[5]<=DoFull, cabt[5]<>0, ncabt[5]=0, initialised=2{WHEN 358}
 DO	c=cabt[5], cab[c]=fst[5](BRAKE_OFF), d=&cab[c],d=3+,*d=upmom, ospt[5]=spt[5]
	ct5=cab[c], spt[5]=Booked
WHEN spt[5]=DoFull, ospt[5]<>DoMin, ospt[5]<>DoFull, cabt[5]<>0, ncabt[5]=0, initialised=2
 DO	c=cabt[5], cab[c]=mst[5](BRAKE_OFF, MOMENTUM_1),                        {WHEN 359}
	ct5=cab[c], wait 1
	c=cabt[5], d=&cab[c], *d=fst[5], d=3+,*d=upmom
	ospt[5]=spt[5], ct5=cab[c], spt[5]=Booked
WHEN ncabt[5]<>0 DO c=cabt[5], d=ncabt[5], cab[d]=cab[c],                       {WHEN 360}
	wait 0.2, d=ncabt[5], tc[d]=5, wait 0.2
	c=cabt[5], tc[c]=99, cabt[5]=ncabt[5], ncabt[5]=0
WHILE spt[5]=CalCount, cabt[5]<>99                                               {WHEN 361}
 DO	c=cabt[5], cab[c]=2+(BRAKE_OFF), ct5=cab[c], wait 1
WHEN spt[5]=EndCal                                                              {WHEN 362}
 DO	c=cabt[5], mst[5]=cab[c], fst[5]=mst[5], fst[5]=10+, spt[5]=Braking
WHEN tdirt[6]=Fwd, cabt[6]<>0 DO c=cabt[6], cab[c]=(FORWARD), ct6=(FORWARD)     {WHEN 363}
WHEN tdirt[6]=Rev, cabt[6]<>0 DO c=cabt[6], cab[c]=(REVERSE), ct6=(REVERSE)     {WHEN 364}
WHEN spt[6]=Free, ncabt[6]=0, cabt[6]<>0                                        {WHEN 365}
 OR  spt[6]=Stopped, ncabt[6]=0, cabt[6]<>0
 DO	c=cabt[6], cab[c]=0(BRAKE_OFF, MOMENTUM_0)
	ct6=cab[c], tdirt[6]=Fwd, ncabt[6]=0
	tc[c]=0, cabt[6]=0, ospt[6]=spt[6]
WHEN spt[6]=Braking, spt[6]<>ospt[6], cabt[6]<>0, ncabt[6]=0 OR initialised=5   {WHEN 366}
 DO	c=cabt[6], cab[c]=0(BRAKE_ON, MOMENTUM_1),
	ospt[6]=spt[6], ct6=cab[c]
WHEN spt[6]=Braking, spt[6]<>ospt[6], cabt[6]<>0, ncabt[6]<>0                   {WHEN 367}
 DO	c=cabt[6], cab[c]=0(BRAKE_ON, MOMENTUM_1), ct6=cab[c],
	d=ncabt[6], cab[d]=cab[c], ospt[6]=spt[6]
WHEN spt[6]=DoMin, spt[6]<>ospt[6], cabt[6]<>0, ncabt[6]=0                      {WHEN 368}
 DO	c=cabt[6], cab[c]=mst[6](BRAKE_OFF), d=&cab[c],d=3+,*d=downmom, ospt[6]=spt[6]
	ct6=cab[c]
WHEN spt[6]=DoMin, spt[6]<>ospt[6], cabt[6]<>0, ncabt[6]<>0, initialised=2      {WHEN 369}
 DO	c=cabt[6], d=&cab[c], *d=mst[6],d=3+,*d=downmom, d=ncabt[6], cab[d]=cab[c],
	ospt[6]=spt[6], ct6=cab[c]
WHEN spt[6]=DoFull, ospt[6]>=DoMin, ospt[6]<=DoFull, cabt[6]<>0, ncabt[6]=0, initialised=2{WHEN 370}
 DO	c=cabt[6], cab[c]=fst[6](BRAKE_OFF), d=&cab[c],d=3+,*d=upmom, ospt[6]=spt[6]
	ct6=cab[c], spt[6]=Booked
WHEN spt[6]=DoFull, ospt[6]<>DoMin, ospt[6]<>DoFull, cabt[6]<>0, ncabt[6]=0, initialised=2
 DO	c=cabt[6], cab[c]=mst[6](BRAKE_OFF, MOMENTUM_1),                        {WHEN 371}
	ct6=cab[c], wait 1
	c=cabt[6], d=&cab[c], *d=fst[6], d=3+,*d=upmom
	ospt[6]=spt[6], ct6=cab[c], spt[6]=Booked
WHEN ncabt[6]<>0 DO c=cabt[6], d=ncabt[6], cab[d]=cab[c],                       {WHEN 372}
	wait 0.2, d=ncabt[6], tc[d]=6, wait 0.2
	c=cabt[6], tc[c]=99, cabt[6]=ncabt[6], ncabt[6]=0
WHILE spt[6]=CalCount, cabt[6]<>99                                               {WHEN 373}
 DO	c=cabt[6], cab[c]=2+(BRAKE_OFF), ct6=cab[c], wait 1
WHEN spt[6]=EndCal                                                              {WHEN 374}
 DO	c=cabt[6], mst[6]=cab[c], fst[6]=mst[6], fst[6]=10+, spt[6]=Braking

{ Cab handling code }

{ WHEN cab released DO wait and mark free }

WHEN tc[1]=99 DO cab[1]=0 (MOMENTUM_0), wait 0.3, tc[1]=0                       {WHEN 375}
WHEN tc[2]=99 DO cab[2]=0 (MOMENTUM_0), wait 0.3, tc[2]=0                       {WHEN 376}
WHEN tc[3]=99 DO cab[3]=0 (MOMENTUM_0), wait 0.3, tc[3]=0                       {WHEN 377}
WHEN tc[4]=99 DO cab[4]=0 (MOMENTUM_0), wait 0.3, tc[4]=0                       {WHEN 378}
WHEN tc[5]=99 DO cab[5]=0 (MOMENTUM_0), wait 0.3, tc[5]=0                       {WHEN 379}
WHEN tc[6]=99 DO cab[6]=0 (MOMENTUM_0), wait 0.3, tc[6]=0                       {WHEN 380}
WHEN tc[7]=99 DO cab[7]=0 (MOMENTUM_0), wait 0.3, tc[7]=0                       {WHEN 381}
WHEN tc[8]=99 DO cab[8]=0 (MOMENTUM_0), wait 0.3, tc[8]=0                       {WHEN 382}

{ Train recalibration code - 1 copy shared for all trains on all sections }

{ WHEN CalTrain DO Calculate speed }
{ WHEN Full Speed calculated DO Update speed for train display }
{ WHEN Min Speed calculated DO Update speed for train display }
{ WHEN Express going too fast DO Calculate reduction }
{ WHEN Express going too slow DO Calculate increase }
{ WHEN Passenger going too fast DO Calculate reduction }
{ WHEN Passenger going too slow DO Calculate increase }
{ WHEN Local going too fast DO Calculate reduction }
{ WHEN Local going too slow DO Calculate increase }
{ WHEN Goods going too fast DO Calculate reduction }
{ WHEN Goods going too slow DO Calculate increase }
{ WHEN Slow train going too fast DO Calculate reduction }
{ WHEN Slow train going too slow DO Calculate increase }
{ WHEN Full speed increase needed DO Calculate and implement change }
{ WHEN Full speed reduction needed DO Calculate and implement change }
{ WHEN Min speed increase needed DO Calculate and implement change }
{ WHEN Min speed reduction needed DO Calculate and implement change }
{ WHEN Speed calculated, no change needed DO Clear variables }

WHEN CalTrain<>0, CalTime<>0, CalLen<>0                                         {WHEN 383}
 DO	CalSpd=CalLen, CalSpd=100*, CalSpd=CalTime/
	CalFS=ospt[CalTrain], CalTT=tt[CalTrain]
WHEN CalTrain<>0, CalSpd<>0, CalFS=DoFull DO mfst[CalTrain]=CalSpd              {WHEN 384}
WHEN CalTrain<>0, CalSpd<>0, CalFS=DoMin DO mmst[CalTrain]=CalSpd               {WHEN 385}
WHEN CalTT=Express, CalFS=DoFull, CalSpd>FSexpress                              {WHEN 386}
 DO	CalRed=CalSpd, CalRed=FSexpress-, CalRed=100*, CalRed=CalSpd/
WHEN CalTT=Express, CalFS=DoFull, CalSpd<FSexpress                              {WHEN 387}
 DO	CalInc=FSexpress, CalInc=CalSpd-, CalInc=100*, CalInc=CalSpd/
WHEN CalTT=Passenger, CalFS=DoFull, CalSpd>FSpassenger                          {WHEN 388}
 DO	CalRed=CalSpd, CalRed=FSpassenger-, CalRed=100*, CalRed=CalSpd/
WHEN CalTT=Passenger, CalFS=DoFull, CalSpd<FSpassenger                          {WHEN 389}
 DO	CalInc=FSpassenger, CalInc=CalSpd-, CalInc=100*, CalInc=CalSpd/
WHEN CalTT=Local, CalFS=DoFull, CalSpd>FSlocal                                  {WHEN 390}
 DO	CalRed=CalSpd, CalRed=FSlocal-, CalRed=100*, CalRed=CalSpd/
WHEN CalTT=Local, CalFS=DoFull, CalSpd<FSlocal                                  {WHEN 391}
 DO	CalInc=FSlocal, CalInc=CalSpd-, CalInc=100*, CalInc=CalSpd/
WHEN CalTT=Goods, CalFS=DoFull, CalSpd>FSgoods                                  {WHEN 392}
 DO	CalRed=CalSpd, CalRed=FSgoods-, CalRed=100*, CalRed=CalSpd/
WHEN CalTT=Goods, CalFS=DoFull, CalSpd<FSgoods                                  {WHEN 393}
 DO	CalInc=FSgoods, CalInc=CalSpd-, CalInc=100*, CalInc=CalSpd/
WHEN CalFS=DoMin, CalSpd>FScrawl                                                {WHEN 394}
 DO	CalRed=CalSpd, CalRed=FScrawl-, CalRed=100*, CalRed=CalSpd/
WHEN CalFS=DoMin, CalSpd<FScrawl                                                {WHEN 395}
 DO	CalInc=FScrawl, CalInc=CalSpd-, CalInc=100*, CalInc=CalSpd/
WHEN CalInc<>0, CalTrain<>0, CalFS=DoFull                                       {WHEN 396}
 DO	CalInc=Max 25, $Draw Message(1,4,3)="Increase speed train @CalTrain by @CalInc%"
	c=mst[CalTrain], c=fst[CalTrain]-, c=CalInc%, c=Max 20, fst[CalTrain]=c-, CalInc=0
WHEN CalRed<>0, CalTrain<>0, CalFS=DoFull                                       {WHEN 397}
 DO	CalRed=Max 25, $Draw Message(1,4,3)="Reduce speed train @CalTrain by @CalRed%"
	c=mst[CalTrain], c=fst[CalTrain]-, c=CalRed%, c=Max 20, fst[CalTrain]=c+, CalRed=0
WHEN CalInc<>0, CalTrain<>0, CalFS=DoMin                                        {WHEN 398}
 DO	$Erase Message(1,4,3), $Draw Message(1,4,3)="Increase speed train @CalTrain by @CalInc%"
WHEN CalRed<>0, CalTrain<>0, CalFS=DoMin                                        {WHEN 399}
 DO	$Erase Message(1,4,3), $Draw Message(1,4,3)="Reduce speed train @CalTrain by @CalRed%"
WHEN CalTrain<>0, CalSpd<>0                                                     {WHEN 400}
 DO	wait 0.1, CalTrain=0, CalTT=0, CalSpd=0, CalTime=0, CalLen=0

{ Initial starting from stopped state }

{ WHEN GetCal & NOT cal & OptO DO CalRev }
{ WHEN GetCal & NOT cal & NOT OptO DO CalFwd }
{ WHEN CalFwd & OptO OR CalRev & !OptO DO Cab=EndCal, GetCal }

WHEN L16.s=GetCal, L16.wt=0, L16.p<>0, OptOL16=TRUE                             {WHEN 401}
 DO	L16.s=CalRev, tdirt[L16.t]=Rev, spt[L16.t]=CalCount
WHEN L16.s=GetCal, L16.wt=0, L16.p<>0, OptOL16=FALSE                            {WHEN 402}
 DO	L16.s=CalFwd, tdirt[L16.t]=Fwd, spt[L16.t]=CalCount
WHEN L16.s=CalFwd, OptOL16=TRUE OR L16.s=CalRev, OptOL16=FALSE                  {WHEN 403}
 DO	L16.s=GetCal, spt[L16.t]=EndCal, L16.wt=1, tdirt[L16.t]=Fwd
WHEN L15.s=GetCal, L15.wt=0, L15.p<>0, OptOL15=TRUE                             {WHEN 404}
 DO	L15.s=CalRev, tdirt[L15.t]=Rev, spt[L15.t]=CalCount
WHEN L15.s=GetCal, L15.wt=0, L15.p<>0, OptOL15=FALSE                            {WHEN 405}
 DO	L15.s=CalFwd, tdirt[L15.t]=Fwd, spt[L15.t]=CalCount
WHEN L15.s=CalFwd, OptOL15=TRUE OR L15.s=CalRev, OptOL15=FALSE                  {WHEN 406}
 DO	L15.s=GetCal, spt[L15.t]=EndCal, L15.wt=1, tdirt[L15.t]=Fwd
WHEN L14.s=GetCal, L14.wt=0, L14.p<>0, OptOL14=TRUE                             {WHEN 407}
 DO	L14.s=CalRev, tdirt[L14.t]=Rev, spt[L14.t]=CalCount
WHEN L14.s=GetCal, L14.wt=0, L14.p<>0, OptOL14=FALSE                            {WHEN 408}
 DO	L14.s=CalFwd, tdirt[L14.t]=Fwd, spt[L14.t]=CalCount
WHEN L14.s=CalFwd, OptOL14=TRUE OR L14.s=CalRev, OptOL14=FALSE                  {WHEN 409}
 DO	L14.s=GetCal, spt[L14.t]=EndCal, L14.wt=1, tdirt[L14.t]=Fwd
WHEN L13.s=GetCal, L13.wt=0, L13.p<>0, OptOL13=TRUE                             {WHEN 410}
 DO	L13.s=CalRev, tdirt[L13.t]=Rev, spt[L13.t]=CalCount
WHEN L13.s=GetCal, L13.wt=0, L13.p<>0, OptOL13=FALSE                            {WHEN 411}
 DO	L13.s=CalFwd, tdirt[L13.t]=Fwd, spt[L13.t]=CalCount
WHEN L13.s=CalFwd, OptOL13=TRUE OR L13.s=CalRev, OptOL13=FALSE                  {WHEN 412}
 DO	L13.s=GetCal, spt[L13.t]=EndCal, L13.wt=1, tdirt[L13.t]=Fwd
WHEN L12.s=GetCal, L12.wt=0, L12.p<>0, OptOL12=TRUE                             {WHEN 413}
 DO	L12.s=CalRev, tdirt[L12.t]=Rev, spt[L12.t]=CalCount
WHEN L12.s=GetCal, L12.wt=0, L12.p<>0, OptOL12=FALSE                            {WHEN 414}
 DO	L12.s=CalFwd, tdirt[L12.t]=Fwd, spt[L12.t]=CalCount
WHEN L12.s=CalFwd, OptOL12=TRUE OR L12.s=CalRev, OptOL12=FALSE                  {WHEN 415}
 DO	L12.s=GetCal, spt[L12.t]=EndCal, L12.wt=1, tdirt[L12.t]=Fwd
WHEN L11.s=GetCal, L11.wt=0, L11.p<>0, OptOL11=TRUE                             {WHEN 416}
 DO	L11.s=CalRev, tdirt[L11.t]=Rev, spt[L11.t]=CalCount
WHEN L11.s=GetCal, L11.wt=0, L11.p<>0, OptOL11=FALSE                            {WHEN 417}
 DO	L11.s=CalFwd, tdirt[L11.t]=Fwd, spt[L11.t]=CalCount
WHEN L11.s=CalFwd, OptOL11=TRUE OR L11.s=CalRev, OptOL11=FALSE                  {WHEN 418}
 DO	L11.s=GetCal, spt[L11.t]=EndCal, L11.wt=1, tdirt[L11.t]=Fwd
WHEN L10.s=GetCal, L10.wt=0, L10.p<>0, OptOL10=TRUE                             {WHEN 419}
 DO	L10.s=CalRev, tdirt[L10.t]=Rev, spt[L10.t]=CalCount
WHEN L10.s=GetCal, L10.wt=0, L10.p<>0, OptOL10=FALSE                            {WHEN 420}
 DO	L10.s=CalFwd, tdirt[L10.t]=Fwd, spt[L10.t]=CalCount
WHEN L10.s=CalFwd, OptOL10=TRUE OR L10.s=CalRev, OptOL10=FALSE                  {WHEN 421}
 DO	L10.s=GetCal, spt[L10.t]=EndCal, L10.wt=1, tdirt[L10.t]=Fwd
WHEN L9.s=GetCal, L9.wt=0, L9.p<>0, OptOL9=TRUE                                 {WHEN 422}
 DO	L9.s=CalRev, tdirt[L9.t]=Rev, spt[L9.t]=CalCount
WHEN L9.s=GetCal, L9.wt=0, L9.p<>0, OptOL9=FALSE                                {WHEN 423}
 DO	L9.s=CalFwd, tdirt[L9.t]=Fwd, spt[L9.t]=CalCount
WHEN L9.s=CalFwd, OptOL9=TRUE OR L9.s=CalRev, OptOL9=FALSE                      {WHEN 424}
 DO	L9.s=GetCal, spt[L9.t]=EndCal, L9.wt=1, tdirt[L9.t]=Fwd
WHEN L8.s=GetCal, L8.wt=0, L8.p<>0, OptOL8=TRUE                                 {WHEN 425}
 DO	L8.s=CalRev, tdirt[L8.t]=Rev, spt[L8.t]=CalCount
WHEN L8.s=GetCal, L8.wt=0, L8.p<>0, OptOL8=FALSE                                {WHEN 426}
 DO	L8.s=CalFwd, tdirt[L8.t]=Fwd, spt[L8.t]=CalCount
WHEN L8.s=CalFwd, OptOL8=TRUE OR L8.s=CalRev, OptOL8=FALSE                      {WHEN 427}
 DO	L8.s=GetCal, spt[L8.t]=EndCal, L8.wt=1, tdirt[L8.t]=Fwd
WHEN L7.s=GetCal, L7.wt=0, L7.p<>0, OptOL7=TRUE                                 {WHEN 428}
 DO	L7.s=CalRev, tdirt[L7.t]=Rev, spt[L7.t]=CalCount
WHEN L7.s=GetCal, L7.wt=0, L7.p<>0, OptOL7=FALSE                                {WHEN 429}
 DO	L7.s=CalFwd, tdirt[L7.t]=Fwd, spt[L7.t]=CalCount
WHEN L7.s=CalFwd, OptOL7=TRUE OR L7.s=CalRev, OptOL7=FALSE                      {WHEN 430}
 DO	L7.s=GetCal, spt[L7.t]=EndCal, L7.wt=1, tdirt[L7.t]=Fwd
WHEN L6.s=GetCal, L6.wt=0, L6.p<>0, OptOL6=TRUE                                 {WHEN 431}
 DO	L6.s=CalRev, tdirt[L6.t]=Rev, spt[L6.t]=CalCount
WHEN L6.s=GetCal, L6.wt=0, L6.p<>0, OptOL6=FALSE                                {WHEN 432}
 DO	L6.s=CalFwd, tdirt[L6.t]=Fwd, spt[L6.t]=CalCount
WHEN L6.s=CalFwd, OptOL6=TRUE OR L6.s=CalRev, OptOL6=FALSE                      {WHEN 433}
 DO	L6.s=GetCal, spt[L6.t]=EndCal, L6.wt=1, tdirt[L6.t]=Fwd
WHEN L5.s=GetCal, L5.wt=0, L5.p<>0, OptOL5=TRUE                                 {WHEN 434}
 DO	L5.s=CalRev, tdirt[L5.t]=Rev, spt[L5.t]=CalCount
WHEN L5.s=GetCal, L5.wt=0, L5.p<>0, OptOL5=FALSE                                {WHEN 435}
 DO	L5.s=CalFwd, tdirt[L5.t]=Fwd, spt[L5.t]=CalCount
WHEN L5.s=CalFwd, OptOL5=TRUE OR L5.s=CalRev, OptOL5=FALSE                      {WHEN 436}
 DO	L5.s=GetCal, spt[L5.t]=EndCal, L5.wt=1, tdirt[L5.t]=Fwd
WHEN L4.s=GetCal, L4.wt=0, L4.p<>0, OptOL4=TRUE                                 {WHEN 437}
 DO	L4.s=CalRev, tdirt[L4.t]=Rev, spt[L4.t]=CalCount
WHEN L4.s=GetCal, L4.wt=0, L4.p<>0, OptOL4=FALSE                                {WHEN 438}
 DO	L4.s=CalFwd, tdirt[L4.t]=Fwd, spt[L4.t]=CalCount
WHEN L4.s=CalFwd, OptOL4=TRUE OR L4.s=CalRev, OptOL4=FALSE                      {WHEN 439}
 DO	L4.s=GetCal, spt[L4.t]=EndCal, L4.wt=1, tdirt[L4.t]=Fwd
WHEN L3.s=GetCal, L3.wt=0, L3.p<>0, OptOL3=TRUE                                 {WHEN 440}
 DO	L3.s=CalRev, tdirt[L3.t]=Rev, spt[L3.t]=CalCount
WHEN L3.s=GetCal, L3.wt=0, L3.p<>0, OptOL3=FALSE                                {WHEN 441}
 DO	L3.s=CalFwd, tdirt[L3.t]=Fwd, spt[L3.t]=CalCount
WHEN L3.s=CalFwd, OptOL3=TRUE OR L3.s=CalRev, OptOL3=FALSE                      {WHEN 442}
 DO	L3.s=GetCal, spt[L3.t]=EndCal, L3.wt=1, tdirt[L3.t]=Fwd
WHEN L2.s=GetCal, L2.wt=0, L2.p<>0, OptOL2=TRUE                                 {WHEN 443}
 DO	L2.s=CalRev, tdirt[L2.t]=Rev, spt[L2.t]=CalCount
WHEN L2.s=GetCal, L2.wt=0, L2.p<>0, OptOL2=FALSE                                {WHEN 444}
 DO	L2.s=CalFwd, tdirt[L2.t]=Fwd, spt[L2.t]=CalCount
WHEN L2.s=CalFwd, OptOL2=TRUE OR L2.s=CalRev, OptOL2=FALSE                      {WHEN 445}
 DO	L2.s=GetCal, spt[L2.t]=EndCal, L2.wt=1, tdirt[L2.t]=Fwd
WHEN L17.s=GetCal, L17.wt=0, L17.p<>0, OptOL17=TRUE                             {WHEN 446}
 DO	L17.s=CalRev, tdirt[L17.t]=Rev, spt[L17.t]=CalCount
WHEN L17.s=GetCal, L17.wt=0, L17.p<>0, OptOL17=FALSE                            {WHEN 447}
 DO	L17.s=CalFwd, tdirt[L17.t]=Fwd, spt[L17.t]=CalCount
WHEN L17.s=CalFwd, OptOL17=TRUE OR L17.s=CalRev, OptOL17=FALSE                  {WHEN 448}
 DO	L17.s=GetCal, spt[L17.t]=EndCal, L17.wt=1, tdirt[L17.t]=Fwd
WHEN L18.s=GetCal, L18.wt=0, L18.p<>0, OptOL18=TRUE                             {WHEN 449}
 DO	L18.s=CalRev, tdirt[L18.t]=Rev, spt[L18.t]=CalCount
WHEN L18.s=GetCal, L18.wt=0, L18.p<>0, OptOL18=FALSE                            {WHEN 450}
 DO	L18.s=CalFwd, tdirt[L18.t]=Fwd, spt[L18.t]=CalCount
WHEN L18.s=CalFwd, OptOL18=TRUE OR L18.s=CalRev, OptOL18=FALSE                  {WHEN 451}
 DO	L18.s=GetCal, spt[L18.t]=EndCal, L18.wt=1, tdirt[L18.t]=Fwd
WHEN L19.s=GetCal, L19.wt=0, L19.p<>0, OptOL19=TRUE                             {WHEN 452}
 DO	L19.s=CalRev, tdirt[L19.t]=Rev, spt[L19.t]=CalCount
WHEN L19.s=GetCal, L19.wt=0, L19.p<>0, OptOL19=FALSE                            {WHEN 453}
 DO	L19.s=CalFwd, tdirt[L19.t]=Fwd, spt[L19.t]=CalCount
WHEN L19.s=CalFwd, OptOL19=TRUE OR L19.s=CalRev, OptOL19=FALSE                  {WHEN 454}
 DO	L19.s=GetCal, spt[L19.t]=EndCal, L19.wt=1, tdirt[L19.t]=Fwd
WHEN L20.s=GetCal, L20.wt=0, L20.p<>0, OptOL20=TRUE                             {WHEN 455}
 DO	L20.s=CalRev, tdirt[L20.t]=Rev, spt[L20.t]=CalCount
WHEN L20.s=GetCal, L20.wt=0, L20.p<>0, OptOL20=FALSE                            {WHEN 456}
 DO	L20.s=CalFwd, tdirt[L20.t]=Fwd, spt[L20.t]=CalCount
WHEN L20.s=CalFwd, OptOL20=TRUE OR L20.s=CalRev, OptOL20=FALSE                  {WHEN 457}
 DO	L20.s=GetCal, spt[L20.t]=EndCal, L20.wt=1, tdirt[L20.t]=Fwd
WHEN L21.s=GetCal, L21.wt=0, L21.p<>0, OptOL21=TRUE                             {WHEN 458}
 DO	L21.s=CalRev, tdirt[L21.t]=Rev, spt[L21.t]=CalCount
WHEN L21.s=GetCal, L21.wt=0, L21.p<>0, OptOL21=FALSE                            {WHEN 459}
 DO	L21.s=CalFwd, tdirt[L21.t]=Fwd, spt[L21.t]=CalCount
WHEN L21.s=CalFwd, OptOL21=TRUE OR L21.s=CalRev, OptOL21=FALSE                  {WHEN 460}
 DO	L21.s=GetCal, spt[L21.t]=EndCal, L21.wt=1, tdirt[L21.t]=Fwd
WHEN P2.s=GetCal, P2.wt=0, P2.p<>0, OptOP2=TRUE                                 {WHEN 461}
 DO	P2.s=CalRev, tdirt[P2.t]=Fwd, spt[P2.t]=CalCount
WHEN P2.s=GetCal, P2.wt=0, P2.p<>0, OptOP2=FALSE                                {WHEN 462}
 DO	P2.s=CalFwd, tdirt[P2.t]=Rev, spt[P2.t]=CalCount
WHEN P2.s=CalFwd, OptOP2=TRUE OR P2.s=CalRev, OptOP2=FALSE                      {WHEN 463}
 DO	P2.s=GetCal, spt[P2.t]=EndCal, P2.wt=1, tdirt[P2.t]=Fwd
WHEN P1.s=GetCal, P1.wt=0, P1.p<>0, OptOP1=TRUE                                 {WHEN 464}
 DO	P1.s=CalRev, tdirt[P1.t]=Fwd, spt[P1.t]=CalCount
WHEN P1.s=GetCal, P1.wt=0, P1.p<>0, OptOP1=FALSE                                {WHEN 465}
 DO	P1.s=CalFwd, tdirt[P1.t]=Rev, spt[P1.t]=CalCount
WHEN P1.s=CalFwd, OptOP1=TRUE OR P1.s=CalRev, OptOP1=FALSE                      {WHEN 466}
 DO	P1.s=GetCal, spt[P1.t]=EndCal, P1.wt=1, tdirt[P1.t]=Fwd

{ Cab allocation, power routing code for powered sections }

{ WHEN GetCal & no power & first free DO grab first }
{ WHEN no power & first grabbed DO power from first }
{ WHEN GetCal & no power & second free DO grab second }
{ WHEN no power & second grabbed DO power from second }
{ WHEN GetCal & no power & third free DO grab third }
{ WHEN no power & third grabbed DO power from third }

WHEN L16.s=GetCal, L16.t<>0, L16.p=0, tc[2]<>L16.t, tc[1]=0 DO tc[1]=L16.t      {WHEN 467}
WHEN L16.s=GetCal, L16.p=0, tc[1]=L16.t                                         {WHEN 468}
 DO	L16.p=1, cabt[L16.t]=1, spt[L16.t]=Braking
WHEN L16.s=GetCal, L16.t<>0, L16.p=0, tc[1]<>0, tc[1]<>L16.t, tc[2]=0           {WHEN 469}
 DO	tc[2]=L16.t
WHEN L16.s=GetCal, L16.p=0, tc[2]=L16.t                                         {WHEN 470}
 DO	L16.p=2, cabt[L16.t]=2, spt[L16.t]=Braking
WHEN L15.s=GetCal, L15.t<>0, L15.p=0, ncnN2L15<>6, ncnN2L15<>2, tc[1]=0         {WHEN 471}
 DO	tc[1]=L15.t
WHEN L15.s=GetCal, L15.p=0, tc[1]=L15.t                                         {WHEN 472}
 DO	L15.p=1, cabt[L15.t]=1, spt[L15.t]=Braking
WHEN L15.s=GetCal, L15.t<>0, L15.p=0, ncnN2L15=6, tc[6]=0 DO tc[6]=L15.t        {WHEN 473}
WHEN L15.s=GetCal, L15.p=0, tc[6]=L15.t                                         {WHEN 474}
 DO	L15.p=6, cabt[L15.t]=6, spt[L15.t]=Braking
WHEN L15.s=GetCal, L15.t<>0, L15.p=0, ncnN2L15=2, tc[2]=0 DO tc[2]=L15.t        {WHEN 475}
WHEN L15.s=GetCal, L15.p=0, tc[2]=L15.t                                         {WHEN 476}
 DO	L15.p=2, cabt[L15.t]=2, spt[L15.t]=Braking
WHEN L14.s=GetCal, L14.t<>0, L14.p=0, tc[1]<>L14.t, tc[2]=0 DO tc[2]=L14.t      {WHEN 477}
WHEN L14.s=GetCal, L14.p=0, tc[2]=L14.t                                         {WHEN 478}
 DO	L14.p=2, cabt[L14.t]=2, spt[L14.t]=Braking
WHEN L14.s=GetCal, L14.t<>0, L14.p=0, tc[2]<>0, tc[2]<>L14.t, tc[1]=0           {WHEN 479}
 DO	tc[1]=L14.t
WHEN L14.s=GetCal, L14.p=0, tc[1]=L14.t                                         {WHEN 480}
 DO	L14.p=1, cabt[L14.t]=1, spt[L14.t]=Braking
WHEN L13.s=GetCal, L13.t<>0, L13.p=0, tc[1]<>L13.t, tc[2]=0 DO tc[2]=L13.t      {WHEN 481}
WHEN L13.s=GetCal, L13.p=0, tc[2]=L13.t                                         {WHEN 482}
 DO	L13.p=2, cabt[L13.t]=2, spt[L13.t]=Braking
WHEN L13.s=GetCal, L13.t<>0, L13.p=0, tc[2]<>0, tc[2]<>L13.t, tc[1]=0           {WHEN 483}
 DO	tc[1]=L13.t
WHEN L13.s=GetCal, L13.p=0, tc[1]=L13.t                                         {WHEN 484}
 DO	L13.p=1, cabt[L13.t]=1, spt[L13.t]=Braking
WHEN L12.s=GetCal, L12.t<>0, L12.p=0, tc[3]<>L12.t, tc[2]=0 DO tc[2]=L12.t      {WHEN 485}
WHEN L12.s=GetCal, L12.p=0, tc[2]=L12.t                                         {WHEN 486}
 DO	L12.p=2, cabt[L12.t]=2, spt[L12.t]=Braking
WHEN L12.s=GetCal, L12.t<>0, L12.p=0, tc[2]<>0, tc[2]<>L12.t, tc[3]=0           {WHEN 487}
 DO	tc[3]=L12.t
WHEN L12.s=GetCal, L12.p=0, tc[3]=L12.t                                         {WHEN 488}
 DO	L12.p=3, cabt[L12.t]=3, spt[L12.t]=Braking
WHEN L11.s=GetCal, L11.t<>0, L11.p=0, tc[3]<>L11.t, tc[2]<>L11.t, tc[4]=0       {WHEN 489}
 DO	tc[4]=L11.t
WHEN L11.s=GetCal, L11.p=0, tc[4]=L11.t                                         {WHEN 490}
 DO	L11.p=4, cabt[L11.t]=4, spt[L11.t]=Braking
WHEN L11.s=GetCal, L11.t<>0, L11.p=0, tc[4]<>0, tc[4]<>L11.t, tc[2]<>L11.t, tc[3]=0
 DO	tc[3]=L11.t                                                             {WHEN 491}
WHEN L11.s=GetCal, L11.p=0, tc[3]=L11.t                                         {WHEN 492}
 DO	L11.p=3, cabt[L11.t]=3, spt[L11.t]=Braking
WHEN L11.s=GetCal, L11.t<>0, L11.p=0, tc[4]<>0, tc[4]<>L11.t, tc[3]<>0, tc[3]<>L11.t, tc[2]=0
 DO	tc[2]=L11.t                                                             {WHEN 493}
WHEN L11.s=GetCal, L11.p=0, tc[2]=L11.t                                         {WHEN 494}
 DO	L11.p=2, cabt[L11.t]=2, spt[L11.t]=Braking
WHEN L10.s=GetCal, L10.t<>0, L10.p=0, tc[4]=0 DO tc[4]=L10.t                    {WHEN 495}
WHEN L10.s=GetCal, L10.p=0, tc[4]=L10.t                                         {WHEN 496}
 DO	L10.p=4, cabt[L10.t]=4, spt[L10.t]=Braking
WHEN L9.s=GetCal, L9.t<>0, L9.p=0, tc[5]<>L9.t, tc[6]=0 DO tc[6]=L9.t           {WHEN 497}
WHEN L9.s=GetCal, L9.p=0, tc[6]=L9.t DO L9.p=6, cabt[L9.t]=6, spt[L9.t]=Braking {WHEN 498}
WHEN L9.s=GetCal, L9.t<>0, L9.p=0, tc[6]<>0, tc[6]<>L9.t, tc[5]=0 DO tc[5]=L9.t {WHEN 499}
WHEN L9.s=GetCal, L9.p=0, tc[5]=L9.t DO L9.p=5, cabt[L9.t]=5, spt[L9.t]=Braking {WHEN 500}
WHEN L8.s=GetCal, L8.t<>0, L8.p=0, tc[6]=0 DO tc[6]=L8.t                        {WHEN 501}
WHEN L8.s=GetCal, L8.p=0, tc[6]=L8.t DO L8.p=6, cabt[L8.t]=6, spt[L8.t]=Braking {WHEN 502}
WHEN L7.s=GetCal, L7.t<>0, L7.p=0, tc[6]<>L7.t, tc[7]=0 DO tc[7]=L7.t           {WHEN 503}
WHEN L7.s=GetCal, L7.p=0, tc[7]=L7.t DO L7.p=7, cabt[L7.t]=7, spt[L7.t]=Braking {WHEN 504}
WHEN L7.s=GetCal, L7.t<>0, L7.p=0, tc[7]<>0, tc[7]<>L7.t, tc[6]=0 DO tc[6]=L7.t {WHEN 505}
WHEN L7.s=GetCal, L7.p=0, tc[6]=L7.t DO L7.p=6, cabt[L7.t]=6, spt[L7.t]=Braking {WHEN 506}
WHEN L6.s=GetCal, L6.t<>0, L6.p=0, tc[7]=0 DO tc[7]=L6.t                        {WHEN 507}
WHEN L6.s=GetCal, L6.p=0, tc[7]=L6.t DO L6.p=7, cabt[L6.t]=7, spt[L6.t]=Braking {WHEN 508}
WHEN L5.s=GetCal, L5.t<>0, L5.p=0, tc[7]<>L5.t, tc[5]=0                         {WHEN 509}
 OR  L5.s=GetCal, L5.t<>0, L5.p=99, tc[7]<>L5.t, tc[5]=0
 DO	tc[5]=L5.t
WHEN L5.s=GetCal, L5.p=0, tc[5]=L5.t OR L5.s=GetCal, L5.p=99, tc[5]=L5.t        {WHEN 510}
 DO	L5.p=5, cabt[L5.t]=5, spt[L5.t]=Braking
WHEN L5.s=GetCal, L5.t<>0, L5.p=0, tc[5]<>0, tc[5]<>L5.t, tc[7]=0               {WHEN 511}
 OR  L5.s=GetCal, L5.t<>0, L5.p=99, tc[5]<>0, tc[5]<>L5.t, tc[7]=0
 DO	tc[7]=L5.t
WHEN L5.s=GetCal, L5.p=0, tc[7]=L5.t OR L5.s=GetCal, L5.p=99, tc[7]=L5.t        {WHEN 512}
 DO	L5.p=7, cabt[L5.t]=7, spt[L5.t]=Braking
WHEN L4.s=GetCal, L4.t<>0, L4.p=0, tc[5]=0 DO tc[5]=L4.t                        {WHEN 513}
WHEN L4.s=GetCal, L4.p=0, tc[5]=L4.t DO L4.p=5, cabt[L4.t]=5, spt[L4.t]=Braking {WHEN 514}
WHEN L3.s=GetCal, L3.t<>0, L3.p=0, tc[5]=0 DO tc[5]=L3.t                        {WHEN 515}
WHEN L3.s=GetCal, L3.p=0, tc[5]=L3.t DO L3.p=5, cabt[L3.t]=5, spt[L3.t]=Braking {WHEN 516}
WHEN L2.s=GetCal, L2.t<>0, L2.p=0, ncnN2L2<>5, ncnN2L2<>4, tc[6]=0              {WHEN 517}
 DO	tc[6]=L2.t
WHEN L2.s=GetCal, L2.p=0, tc[6]=L2.t DO L2.p=6, cabt[L2.t]=6, spt[L2.t]=Braking {WHEN 518}
WHEN L2.s=GetCal, L2.t<>0, L2.p=0, ncnN2L2=5, tc[5]=0 DO tc[5]=L2.t             {WHEN 519}
WHEN L2.s=GetCal, L2.p=0, tc[5]=L2.t DO L2.p=5, cabt[L2.t]=5, spt[L2.t]=Braking {WHEN 520}
WHEN L2.s=GetCal, L2.t<>0, L2.p=0, ncnN2L2=4, tc[4]=0 DO tc[4]=L2.t             {WHEN 521}
WHEN L2.s=GetCal, L2.p=0, tc[4]=L2.t DO L2.p=4, cabt[L2.t]=4, spt[L2.t]=Braking {WHEN 522}
WHEN L17.s=GetCal, L17.t<>0, L17.p=0, ncnTO4<>5, tc[4]=0 DO tc[4]=L17.t         {WHEN 523}
WHEN L17.s=GetCal, L17.p=0, tc[4]=L17.t                                         {WHEN 524}
 DO	L17.p=4, cabt[L17.t]=4, spt[L17.t]=Braking
WHEN L17.s=GetCal, L17.t<>0, L17.p=0, ncnTO4=5, tc[5]=0 DO tc[5]=L17.t          {WHEN 525}
WHEN L17.s=GetCal, L17.p=0, tc[5]=L17.t                                         {WHEN 526}
 DO	L17.p=5, cabt[L17.t]=5, spt[L17.t]=Braking
WHEN L18.s=GetCal, L18.t<>0, L18.p=0, tc[4]<>L18.t, tc[3]=0                     {WHEN 527}
 OR  L18.s=GetCal, L18.t<>0, L18.p=99, tc[4]<>L18.t, tc[3]=0
 DO	tc[3]=L18.t
WHEN L18.s=GetCal, L18.p=0, tc[3]=L18.t OR L18.s=GetCal, L18.p=99, tc[3]=L18.t  {WHEN 528}
 DO	L18.p=3, cabt[L18.t]=3, spt[L18.t]=Braking
WHEN L18.s=GetCal, L18.t<>0, L18.p=0, tc[3]<>0, tc[3]<>L18.t, tc[4]=0           {WHEN 529}
 OR  L18.s=GetCal, L18.t<>0, L18.p=99, tc[3]<>0, tc[3]<>L18.t, tc[4]=0
 DO	tc[4]=L18.t
WHEN L18.s=GetCal, L18.p=0, tc[4]=L18.t OR L18.s=GetCal, L18.p=99, tc[4]=L18.t  {WHEN 530}
 DO	L18.p=4, cabt[L18.t]=4, spt[L18.t]=Braking
WHEN L19.s=GetCal, L19.t<>0, L19.p=0, tc[4]<>L19.t, tc[3]=0 DO tc[3]=L19.t      {WHEN 531}
WHEN L19.s=GetCal, L19.p=0, tc[3]=L19.t                                         {WHEN 532}
 DO	L19.p=3, cabt[L19.t]=3, spt[L19.t]=Braking
WHEN L19.s=GetCal, L19.t<>0, L19.p=0, tc[3]<>0, tc[3]<>L19.t, tc[4]=0           {WHEN 533}
 DO	tc[4]=L19.t
WHEN L19.s=GetCal, L19.p=0, tc[4]=L19.t                                         {WHEN 534}
 DO	L19.p=4, cabt[L19.t]=4, spt[L19.t]=Braking
WHEN L20.s=GetCal, L20.t<>0, L20.p=0, tc[4]<>L20.t, tc[2]=0                     {WHEN 535}
 OR  L20.s=GetCal, L20.t<>0, L20.p=99, tc[4]<>L20.t, tc[2]=0
 DO	tc[2]=L20.t
WHEN L20.s=GetCal, L20.p=0, tc[2]=L20.t OR L20.s=GetCal, L20.p=99, tc[2]=L20.t  {WHEN 536}
 DO	L20.p=2, cabt[L20.t]=2, spt[L20.t]=Braking
WHEN L20.s=GetCal, L20.t<>0, L20.p=0, tc[2]<>0, tc[2]<>L20.t, tc[4]=0           {WHEN 537}
 OR  L20.s=GetCal, L20.t<>0, L20.p=99, tc[2]<>0, tc[2]<>L20.t, tc[4]=0
 DO	tc[4]=L20.t
WHEN L20.s=GetCal, L20.p=0, tc[4]=L20.t OR L20.s=GetCal, L20.p=99, tc[4]=L20.t  {WHEN 538}
 DO	L20.p=4, cabt[L20.t]=4, spt[L20.t]=Braking
WHEN L21.s=GetCal, L21.t<>0, L21.p=0, tc[4]<>L21.t, tc[2]=0 DO tc[2]=L21.t      {WHEN 539}
WHEN L21.s=GetCal, L21.p=0, tc[2]=L21.t                                         {WHEN 540}
 DO	L21.p=2, cabt[L21.t]=2, spt[L21.t]=Braking
WHEN L21.s=GetCal, L21.t<>0, L21.p=0, tc[2]<>0, tc[2]<>L21.t, tc[4]=0           {WHEN 541}
 DO	tc[4]=L21.t
WHEN L21.s=GetCal, L21.p=0, tc[4]=L21.t                                         {WHEN 542}
 DO	L21.p=4, cabt[L21.t]=4, spt[L21.t]=Braking
WHEN P2.s=GetCal, P2.t<>0, P2.p=0, tc[4]=0 DO tc[4]=P2.t                        {WHEN 543}
WHEN P2.s=GetCal, P2.p=0, tc[4]=P2.t DO P2.p=4, cabt[P2.t]=4, spt[P2.t]=Braking {WHEN 544}
WHEN P1.s=GetCal, P1.t<>0, P1.p=0, tc[5]<>P1.t, tc[8]<>P1.t, tc[4]=0            {WHEN 545}
 DO	tc[4]=P1.t
WHEN P1.s=GetCal, P1.p=0, tc[4]=P1.t DO P1.p=4, cabt[P1.t]=4, spt[P1.t]=Braking {WHEN 546}
WHEN P1.s=GetCal, P1.t<>0, P1.p=0, tc[4]<>0, tc[4]<>P1.t, tc[8]<>P1.t, tc[5]=0  {WHEN 547}
 DO	tc[5]=P1.t
WHEN P1.s=GetCal, P1.p=0, tc[5]=P1.t DO P1.p=5, cabt[P1.t]=5, spt[P1.t]=Braking {WHEN 548}
WHEN P1.s=GetCal, P1.t<>0, P1.p=0, tc[4]<>0, tc[4]<>P1.t, tc[5]<>0, tc[5]<>P1.t, tc[8]=0
 DO	tc[8]=P1.t                                                              {WHEN 549}
WHEN P1.s=GetCal, P1.p=0, tc[8]=P1.t DO P1.p=8, cabt[P1.t]=8, spt[P1.t]=Braking {WHEN 550}

{ Handle power switching relays }

{ WHEN Power=first DO Power relay = 0/1 }
{ WHEN Power=second DO Power relay = 1/1 }
{ WHEN Power=third DO Power relay = 1/0 }
{ WHEN Power=none DO Power relays = off }

WHEN L16.p=1 DO rcL16=0, rsL16=1                                                {WHEN 551}
WHEN L16.p=4 DO rcL16=1, rsL16=1                                                {WHEN 552}
WHEN L16.p=2 DO rcL16=1, rsL16=0                                                {WHEN 553}
WHEN L16.p=0 DO rcL16=0, rsL16=0                                                {WHEN 554}
WHEN L15.p=1 DO rcL15=0, rsL15=1                                                {WHEN 555}
WHEN L15.p=6 DO rcL15=1, rsL15=1                                                {WHEN 556}
WHEN L15.p=2 DO rcL15=1, rsL15=0                                                {WHEN 557}
WHEN L15.p=0 DO rcL15=0, rsL15=0                                                {WHEN 558}
WHEN L14.p=2 DO rcL14=0, rsL14=1                                                {WHEN 559}
WHEN L14.p=1 DO rcL14=1, rsL14=1                                                {WHEN 560}
WHEN L14.p=0 DO rcL14=0, rsL14=0                                                {WHEN 561}
WHEN L13.p=2 DO rcL13=0, rsL13=1                                                {WHEN 562}
WHEN L13.p=1 DO rcL13=1, rsL13=1                                                {WHEN 563}
WHEN L13.p=0 DO rcL13=0, rsL13=0                                                {WHEN 564}
WHEN L12.p=2 DO rcL12=0, rsL12=1                                                {WHEN 565}
WHEN L12.p=1 DO rcL12=1, rsL12=1                                                {WHEN 566}
WHEN L12.p=3 DO rcL12=1, rsL12=0                                                {WHEN 567}
WHEN L12.p=0 DO rcL12=0, rsL12=0                                                {WHEN 568}
WHEN L11.p=4 DO rcL11=0, rsL11=1                                                {WHEN 569}
WHEN L11.p=3 DO rcL11=1, rsL11=1                                                {WHEN 570}
WHEN L11.p=2 DO rcL11=1, rsL11=0                                                {WHEN 571}
WHEN L11.p=0 DO rcL11=0, rsL11=0                                                {WHEN 572}
WHEN L10.p=4 DO rcL10=0, rsL10=1                                                {WHEN 573}
WHEN L10.p=3 DO rcL10=1, rsL10=1                                                {WHEN 574}
WHEN L10.p=2 DO rcL10=1, rsL10=0                                                {WHEN 575}
WHEN L10.p=0 DO rcL10=0, rsL10=0                                                {WHEN 576}
WHEN L9.p=6 DO rcL9=0, rsL9=1                                                   {WHEN 577}
WHEN L9.p=5 DO rcL9=1, rsL9=1                                                   {WHEN 578}
WHEN L9.p=0 DO rcL9=0, rsL9=0                                                   {WHEN 579}
WHEN L8.p=6 DO rcL8=0, rsL8=1                                                   {WHEN 580}
WHEN L8.p=5 DO rcL8=1, rsL8=1                                                   {WHEN 581}
WHEN L8.p=0 DO rcL8=0, rsL8=0                                                   {WHEN 582}
WHEN L7.p=7 DO rcL7=0, rsL7=1                                                   {WHEN 583}
WHEN L7.p=6 DO rcL7=1, rsL7=1                                                   {WHEN 584}
WHEN L7.p=0 DO rcL7=0, rsL7=0                                                   {WHEN 585}
WHEN L6.p=7 DO rcL6=0, rsL6=1                                                   {WHEN 586}
WHEN L6.p=6 DO rcL6=1, rsL6=1                                                   {WHEN 587}
WHEN L6.p=0 DO rcL6=0, rsL6=0                                                   {WHEN 588}
WHEN L5.p=5 DO rcL5=0, rsL5=1                                                   {WHEN 589}
WHEN L5.p=7 DO rcL5=1, rsL5=1                                                   {WHEN 590}
WHEN L5.p=0 DO rcL5=0, rsL5=0                                                   {WHEN 591}
WHEN L4.p=5 DO rcL4=0, rsL4=1                                                   {WHEN 592}
WHEN L4.p=7 DO rcL4=1, rsL4=1                                                   {WHEN 593}
WHEN L4.p=0 DO rcL4=0, rsL4=0                                                   {WHEN 594}
WHEN L3.p=5 DO rcL3=0, rsL3=1                                                   {WHEN 595}
WHEN L3.p=7 DO rcL3=1, rsL3=1                                                   {WHEN 596}
WHEN L3.p=0 DO rcL3=0, rsL3=0                                                   {WHEN 597}
WHEN L2.p=6 DO rc1L2=0, rc2L2=0, rsL2=1                                         {WHEN 598}
WHEN L2.p=5 DO rc1L2=1, rc2L2=0, rsL2=1                                         {WHEN 599}
WHEN L2.p=4 DO rc1L2=0, rc2L2=1, rsL2=1                                         {WHEN 600}
WHEN L2.p=0 DO rc1L2=0, rc2L2=0, rsL2=0                                         {WHEN 601}
WHEN L17.p=4 DO rcAL17=0, rcBL17=1                                              {WHEN 602}
WHEN L17.p=5 DO rcAL17=1, rcBL17=0                                              {WHEN 603}
WHEN L17.p=0 DO rcAL17=0, rcBL17=0                                              {WHEN 604}
WHEN L18.p=3 DO rcL18=0, rsL18=1                                                {WHEN 605}
WHEN L18.p=4 DO rcL18=1, rsL18=1                                                {WHEN 606}
WHEN L18.p=0 DO rcL18=0, rsL18=0                                                {WHEN 607}
WHEN L19.p=3 DO rcL19=0, rsL19=1                                                {WHEN 608}
WHEN L19.p=4 DO rcL19=1, rsL19=1                                                {WHEN 609}
WHEN L19.p=0 DO rcL19=0, rsL19=0                                                {WHEN 610}
WHEN L20.p=2 DO rcL20=0, rsL20=1                                                {WHEN 611}
WHEN L20.p=4 DO rcL20=1, rsL20=1                                                {WHEN 612}
WHEN L20.p=0 DO rcL20=0, rsL20=0                                                {WHEN 613}
WHEN L21.p=2 DO rcL21=0, rsL21=1                                                {WHEN 614}
WHEN L21.p=4 DO rcL21=1, rsL21=1                                                {WHEN 615}
WHEN L21.p=0 DO rcL21=0, rsL21=0                                                {WHEN 616}
WHEN P2.p=4 DO rcP2=0, rsP2=1                                                   {WHEN 617}
WHEN P2.p=5 DO rcP2=1, rsP2=1                                                   {WHEN 618}
WHEN P2.p=8 DO rcP2=1, rsP2=0                                                   {WHEN 619}
WHEN P2.p=0 DO rcP2=0, rsP2=0                                                   {WHEN 620}
WHEN P1.p=4 DO rcP1=0, rsP1=1                                                   {WHEN 621}
WHEN P1.p=5 DO rcP1=1, rsP1=1                                                   {WHEN 622}
WHEN P1.p=8 DO rcP1=1, rsP1=0                                                   {WHEN 623}
WHEN P1.p=0 DO rcP1=0, rsP1=0                                                   {WHEN 624}

{ Beginning of main code }

{ Track sections: When free, previous occupied then become booked }

{ WHEN Free & prev>Booked & controller OK DO booked, t = prev.t }
{ WHEN Booked & prev Stopped DO WaitClear }
{ WHEN Free & Opt DO Error }
{ WHEN Departing & next<>booked & next<ArrOcc & next<>Stopping DO Error }
{ WHEN >=Booked & cab not valid DO Error }
{ WHEN > GetTrain & Train=0 DO Error }
{ WHEN Calibrating and OptO DO Recalibrate train }
{ WHEN Departing & next Occ|Dep OR Free & Guided power on DO WaitClear }
{ WHEN WaitClear & all sensors clear DO Free }

WHEN L16.s=Free, *pspN2_L16.s>Booked, *pspN2_L16.p<>0                           {WHEN 625}
 DO	L16.s=Booked, L16.t=*pspN2_L16.t, L16.p=*pspN2_L16.p
WHEN L16.s=Booked, *pspN2_L16.s<=Stopping                                       {WHEN 626}
 OR  L16.s=Arriving, *pspN2_L16.s<=Stopping
 DO	L16.s=WaitClear, L16.t=0, L16.p=0, $Color Block(5,18,1)=colt0
WHEN L16.s=Free, initialised=2, OptL16=TRUE                                     {WHEN 627}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L16"
	L16.s=WaitClear
WHEN L16.s=Departing, *npN2L16.s<>Booked, *npN2L16.s<Moving, *npN2L16.s<>Stopping, *npN2L16.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L16"         {WHEN 628}
WHEN L16.s>=Booked, L16.s<>GetCal, L16.p<>1, L16.p<>4, L16.p<>2                 {WHEN 629}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L16"
WHEN L16.s>GetTrain, L16.t=0                                                    {WHEN 630}
 DO	$Draw Message(1,3,3)="Train number 0 on section L16"
WHEN L16.s=Departing, *npN2L16.s>=Departing, *npN2L16.s<=Occupied               {WHEN 631}
 OR  L16.s=Departing, *npN2L16.s=StopClear
 DO	L16.s=WaitClear
WHEN L16.s=WaitClear, OptL16=FALSE, OptOL16=FALSE, CurL16=FALSE                 {WHEN 632}
 DO	L16.s=Free, L16.t=0, L16.p=0
	$Color Block(5,18,1)=colt0
WHEN L15.s=Free, *pspN2_L15.s>Booked, *pspN2_L15.p<>0                           {WHEN 633}
 DO	L15.s=Booked, L15.t=*pspN2_L15.t, L15.p=*pspN2_L15.p
WHEN L15.s=Booked, *pspN2_L15.s<=Stopping                                       {WHEN 634}
 OR  L15.s=Arriving, *pspN2_L15.s<=Stopping
 DO	L15.s=WaitClear, L15.t=0, L15.p=0, $Color Block(12,18,1)=colt0
WHEN L15.s=Free, initialised=2, OptL15=TRUE                                     {WHEN 635}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L15"
	L15.s=WaitClear
WHEN L15.s=Departing, *npN2L15.s<>Booked, *npN2L15.s<Moving, *npN2L15.s<>Stopping, *npN2L15.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L15"         {WHEN 636}
WHEN L15.s>=Booked, L15.s<>GetCal, L15.p<>1, L15.p<>6, L15.p<>2                 {WHEN 637}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L15"
WHEN L15.s>GetTrain, L15.t=0                                                    {WHEN 638}
 DO	$Draw Message(1,3,3)="Train number 0 on section L15"
WHEN tcalL15<>0, OptOL15=TRUE, CalTrain=0                                       {WHEN 639}
 DO	CalTrain=L15.t, CalTime=$SESSION, CalTime=tcalL15-, CalLen=31, tcalL15=0
WHEN L15.s=Departing, *npN2L15.s>=Departing, *npN2L15.s<=Occupied               {WHEN 640}
 OR  L15.s=Departing, *npN2L15.s=StopClear
 OR  L15.s=Free, ncnN2L15<>0
 DO	L15.s=WaitClear, L15.p=ncnN2L15
WHEN L15.s=WaitClear, OptL15=FALSE, OptOL15=FALSE, CurL15=FALSE, ncnN2L15=0     {WHEN 641}
 DO	L15.s=Free, L15.t=0, L15.p=0, tcalL15=0
	$Color Block(12,18,1)=colt0
WHEN L14.s=Free, *pspN2_L14.s>Booked, *pspN2_L14.p<>0                           {WHEN 642}
 DO	L14.s=Booked, L14.t=*pspN2_L14.t, L14.p=*pspN2_L14.p
WHEN L14.s=Booked, *pspN2_L14.s<=Stopping                                       {WHEN 643}
 OR  L14.s=Arriving, *pspN2_L14.s<=Stopping
 DO	L14.s=WaitClear, L14.t=0, L14.p=0, $Color Block(15,15,1)=colt0
WHEN L14.s=Free, initialised=2, OptL14=TRUE                                     {WHEN 644}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L14"
	L14.s=WaitClear
WHEN L14.s=Departing, X2.s<>Booked, X2.s<Moving, X2.s<>Stopping, X2.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L14"         {WHEN 645}
WHEN L14.s>=Booked, L14.s<>GetCal, L14.p<>2, L14.p<>1                           {WHEN 646}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L14"
WHEN L14.s>GetTrain, L14.t=0                                                    {WHEN 647}
 DO	$Draw Message(1,3,3)="Train number 0 on section L14"
WHEN L14.s=Departing, X2.s>=Departing, X2.s<=Occupied                           {WHEN 648}
 OR  L14.s=Departing, X2.s=StopClear
 DO	L14.s=WaitClear
WHEN L14.s=WaitClear, OptL14=FALSE, OptOL14=FALSE, CurL14=FALSE                 {WHEN 649}
 DO	L14.s=Free, L14.t=0, L14.p=0
	$Color Block(15,15,1)=colt0
WHEN L13.s=Free, L14.s>Booked, L14.p<>0                                         {WHEN 650}
 DO	L13.s=Booked, L13.t=L14.t, L13.p=L14.p
WHEN L13.s=Booked, L14.s<=Stopping OR L13.s=Arriving, L14.s<=Stopping           {WHEN 651}
 DO	L13.s=WaitClear, L13.t=0, L13.p=0, $Color Block(15,8,1)=colt0
WHEN L13.s=Free, initialised=2, OptL13=TRUE                                     {WHEN 652}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L13"
	L13.s=WaitClear
WHEN L13.s=Departing, L12.s<>Booked, L12.s<Moving, L12.s<>Stopping, L12.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L13"         {WHEN 653}
WHEN L13.s>=Booked, L13.s<>GetCal, L13.p<>2, L13.p<>1                           {WHEN 654}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L13"
WHEN L13.s>GetTrain, L13.t=0                                                    {WHEN 655}
 DO	$Draw Message(1,3,3)="Train number 0 on section L13"
WHEN L13.s=Departing, L12.s>=Departing, L12.s<=Occupied                         {WHEN 656}
 OR  L13.s=Departing, L12.s=StopClear
 DO	L13.s=WaitClear
WHEN L13.s=WaitClear, OptL13=FALSE, OptOL13=FALSE, CurL13=FALSE                 {WHEN 657}
 DO	L13.s=Free, L13.t=0, L13.p=0
	$Color Block(15,8,1)=colt0
WHEN L12.s=Free, L13.s>Booked, L13.p<>0                                         {WHEN 658}
 DO	L12.s=Booked, L12.t=L13.t, L12.p=L13.p
WHEN L12.s=Booked, L13.s<=Stopping OR L12.s=Arriving, L13.s<=Stopping           {WHEN 659}
 DO	L12.s=WaitClear, L12.t=0, L12.p=0, $Color Block(12,3,1)=colt0
WHEN L12.s=Free, initialised=2, OptL12=TRUE                                     {WHEN 660}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L12"
	L12.s=WaitClear
WHEN L12.s=Departing, *npN1L12.s<>Booked, *npN1L12.s<Moving, *npN1L12.s<>Stopping, *npN1L12.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L12"         {WHEN 661}
WHEN L12.s>=Booked, L12.s<>GetCal, L12.p<>2, L12.p<>1, L12.p<>3                 {WHEN 662}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L12"
WHEN L12.s>GetTrain, L12.t=0                                                    {WHEN 663}
 DO	$Draw Message(1,3,3)="Train number 0 on section L12"
WHEN L12.s=Departing, *npN1L12.s>=Departing, *npN1L12.s<=Occupied               {WHEN 664}
 OR  L12.s=Departing, *npN1L12.s=StopClear
 DO	L12.s=WaitClear
WHEN L12.s=WaitClear, OptL12=FALSE, OptOL12=FALSE, CurL12=FALSE                 {WHEN 665}
 DO	L12.s=Free, L12.t=0, L12.p=0
	$Color Block(12,3,1)=colt0
WHEN L11.s=Free, *pspN1_L11.s>Booked, *pspN1_L11.p<>0                           {WHEN 666}
 DO	L11.s=Booked, L11.t=*pspN1_L11.t, L11.p=*pspN1_L11.p
WHEN L11.s=Booked, *pspN1_L11.s<=Stopping                                       {WHEN 667}
 OR  L11.s=Arriving, *pspN1_L11.s<=Stopping
 DO	L11.s=WaitClear, L11.t=0, L11.p=0, $Color Block(6,3,1)=colt0
WHEN L11.s=Free, initialised=2, OptL11=TRUE                                     {WHEN 668}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L11"
	L11.s=WaitClear
WHEN L11.s=Departing, L10.s<>Booked, L10.s<Moving, L10.s<>Stopping, L10.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L11"         {WHEN 669}
WHEN L11.s>=Booked, L11.s<>GetCal, L11.p<>4, L11.p<>3, L11.p<>2                 {WHEN 670}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L11"
WHEN L11.s>GetTrain, L11.t=0                                                    {WHEN 671}
 DO	$Draw Message(1,3,3)="Train number 0 on section L11"
WHEN L11.s=Departing, L10.s>=Departing, L10.s<=Occupied                         {WHEN 672}
 OR  L11.s=Departing, L10.s=StopClear
 DO	L11.s=WaitClear
WHEN L11.s=WaitClear, OptL11=FALSE, OptOL11=FALSE, CurL11=FALSE                 {WHEN 673}
 DO	L11.s=Free, L11.t=0, L11.p=0
	$Color Block(6,3,1)=colt0
WHEN L10.s=Free, L11.s>Booked, L11.p<>0                                         {WHEN 674}
 DO	L10.s=Booked, L10.t=L11.t, L10.p=L11.p
WHEN L10.s=Booked, L11.s<=Stopping OR L10.s=Arriving, L11.s<=Stopping           {WHEN 675}
 DO	L10.s=WaitClear, L10.t=0, L10.p=0, $Color Block(3,7,1)=colt0
WHEN L10.s=Free, initialised=2, OptL10=TRUE                                     {WHEN 676}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L10"
	L10.s=WaitClear
WHEN L10.s=Departing, *npN2L10.s<>Booked, *npN2L10.s<Moving, *npN2L10.s<>Stopping, *npN2L10.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L10"         {WHEN 677}
WHEN L10.s>=Booked, L10.s<>GetCal, L10.p<>4, L10.p<>3, L10.p<>2                 {WHEN 678}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L10"
WHEN L10.s>GetTrain, L10.t=0                                                    {WHEN 679}
 DO	$Draw Message(1,3,3)="Train number 0 on section L10"
WHEN L10.s=Departing, *npN2L10.s>=Departing, *npN2L10.s<=Occupied               {WHEN 680}
 OR  L10.s=Departing, *npN2L10.s=StopClear
 DO	L10.s=WaitClear
WHEN L10.s=WaitClear, OptL10=FALSE, OptOL10=FALSE, CurL10=FALSE                 {WHEN 681}
 DO	L10.s=Free, L10.t=0, L10.p=0
	$Color Block(3,7,1)=colt0
WHEN L9.s=Free, *pspN2_L9.s>Booked, *pspN2_L9.p<>0                              {WHEN 682}
 DO	L9.s=Booked, L9.t=*pspN2_L9.t, L9.p=*pspN2_L9.p
WHEN L9.s=Booked, *pspN2_L9.s<=Stopping OR L9.s=Arriving, *pspN2_L9.s<=Stopping {WHEN 683}
 DO	L9.s=WaitClear, L9.t=0, L9.p=0, $Color Block(2,4,1)=colt0
WHEN L9.s=Free, initialised=2, OptL9=TRUE                                       {WHEN 684}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L9"
	L9.s=WaitClear
WHEN L9.s=Departing, L8.s<>Booked, L8.s<Moving, L8.s<>Stopping, L8.s<>StopClear {WHEN 685}
 DO	$Draw Message(1,3,3)="Next not ready for departing section L9"
WHEN L9.s>=Booked, L9.s<>GetCal, L9.p<>6, L9.p<>5                               {WHEN 686}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L9"
WHEN L9.s>GetTrain, L9.t=0                                                      {WHEN 687}
 DO	$Draw Message(1,3,3)="Train number 0 on section L9"
WHEN tcalL9<>0, OptOL9=TRUE, CalTrain=0                                         {WHEN 688}
 DO	CalTrain=L9.t, CalTime=$SESSION, CalTime=tcalL9-, CalLen=50, tcalL9=0
WHEN L9.s=Departing, L8.s>=Departing, L8.s<=Occupied                            {WHEN 689}
 OR  L9.s=Departing, L8.s=StopClear
 DO	L9.s=WaitClear
WHEN L9.s=WaitClear, OptL9=FALSE, OptOL9=FALSE, CurL9=FALSE                     {WHEN 690}
 DO	L9.s=Free, L9.t=0, L9.p=0, tcalL9=0
	$Color Block(2,4,1)=colt0
WHEN L8.s=Free, L9.s>Booked, L9.p<>0                                            {WHEN 691}
 DO	L8.s=Booked, L8.t=L9.t, L8.p=L9.p
WHEN L8.s=Booked, L9.s<=Stopping OR L8.s=Arriving, L9.s<=Stopping               {WHEN 692}
 DO	L8.s=WaitClear, L8.t=0, L8.p=0, $Color Block(11,2,1)=colt0
WHEN L8.s=Free, initialised=2, OptL8=TRUE                                       {WHEN 693}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L8"
	L8.s=WaitClear
WHEN L8.s=Departing, L7.s<>Booked, L7.s<Moving, L7.s<>Stopping, L7.s<>StopClear {WHEN 694}
 DO	$Draw Message(1,3,3)="Next not ready for departing section L8"
WHEN L8.s>=Booked, L8.s<>GetCal, L8.p<>6, L8.p<>5                               {WHEN 695}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L8"
WHEN L8.s>GetTrain, L8.t=0                                                      {WHEN 696}
 DO	$Draw Message(1,3,3)="Train number 0 on section L8"
WHEN L8.s=Departing, L7.s>=Departing, L7.s<=Occupied                            {WHEN 697}
 OR  L8.s=Departing, L7.s=StopClear
 DO	L8.s=WaitClear
WHEN L8.s=WaitClear, OptL8=FALSE, OptOL8=FALSE, CurL8=FALSE                     {WHEN 698}
 DO	L8.s=Free, L8.t=0, L8.p=0
	$Color Block(11,2,1)=colt0
WHEN L7.s=Free, L8.s>Booked, L8.p<>0                                            {WHEN 699}
 DO	L7.s=Booked, L7.t=L8.t, L7.p=L8.p
WHEN L7.s=Booked, L8.s<=Stopping OR L7.s=Arriving, L8.s<=Stopping               {WHEN 700}
 DO	L7.s=WaitClear, L7.t=0, L7.p=0, $Color Block(16,7,1)=colt0
WHEN L7.s=Free, initialised=2, OptL7=TRUE                                       {WHEN 701}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L7"
	L7.s=WaitClear
WHEN L7.s=Departing, L6.s<>Booked, L6.s<Moving, L6.s<>Stopping, L6.s<>StopClear {WHEN 702}
 DO	$Draw Message(1,3,3)="Next not ready for departing section L7"
WHEN L7.s>=Booked, L7.s<>GetCal, L7.p<>7, L7.p<>6                               {WHEN 703}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L7"
WHEN L7.s>GetTrain, L7.t=0                                                      {WHEN 704}
 DO	$Draw Message(1,3,3)="Train number 0 on section L7"
WHEN L7.s=Departing, L6.s>=Departing, L6.s<=Occupied                            {WHEN 705}
 OR  L7.s=Departing, L6.s=StopClear
 DO	L7.s=WaitClear
WHEN L7.s=WaitClear, OptL7=FALSE, OptOL7=FALSE, CurL7=FALSE                     {WHEN 706}
 DO	L7.s=Free, L7.t=0, L7.p=0
	$Color Block(16,7,1)=colt0
WHEN L6.s=Free, L7.s>Booked, L7.p<>0                                            {WHEN 707}
 DO	L6.s=Booked, L6.t=L7.t, L6.p=L7.p
WHEN L6.s=Booked, L7.s<=Stopping OR L6.s=Arriving, L7.s<=Stopping               {WHEN 708}
 DO	L6.s=WaitClear, L6.t=0, L6.p=0, $Color Block(16,14,1)=colt0
WHEN L6.s=Free, initialised=2, OptL6=TRUE                                       {WHEN 709}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L6"
	L6.s=WaitClear
WHEN L6.s=Departing, *lpTI4.s<>Booked, *lpTI4.s<Moving, *lpTI4.s<>Stopping, *lpTI4.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L6"          {WHEN 710}
WHEN L6.s>=Booked, L6.s<>GetCal, L6.p<>7, L6.p<>6                               {WHEN 711}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L6"
WHEN L6.s>GetTrain, L6.t=0                                                      {WHEN 712}
 DO	$Draw Message(1,3,3)="Train number 0 on section L6"
WHEN L6.s=Departing, *lpTI4.s>=Departing, *lpTI4.s<=Occupied                    {WHEN 713}
 OR  L6.s=Departing, *lpTI4.s=StopClear
 DO	L6.s=WaitClear
WHEN L6.s=WaitClear, OptL6=FALSE, OptOL6=FALSE, CurL6=FALSE                     {WHEN 714}
 DO	L6.s=Free, L6.t=0, L6.p=0
	$Color Block(16,14,1)=colt0
WHEN L5.s=Free, *pspTI4.s>Booked, *pspTI4.p<>0                                  {WHEN 715}
 DO	L5.s=Booked, L5.t=*pspTI4.t, L5.p=*pspTI4.p
WHEN L5.s=Booked, *pspTI4.s<=Stopping OR L5.s=Arriving, *pspTI4.s<=Stopping     {WHEN 716}
 DO	L5.s=WaitClear, L5.t=0, L5.p=0, $Color Block(14,19,1)=colt0
WHEN L5.s=Free, initialised=2, OptL5=TRUE                                       {WHEN 717}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L5"
	L5.s=WaitClear
WHEN L5.s=Departing, *npTO3.s<>Booked, *npTO3.s<Moving, *npTO3.s<>Stopping, *npTO3.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L5"          {WHEN 718}
WHEN L5.s>=Booked, L5.s<>GetCal, L5.p<>5, L5.p<>7                               {WHEN 719}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L5"
WHEN L5.s>GetTrain, L5.t=0                                                      {WHEN 720}
 DO	$Draw Message(1,3,3)="Train number 0 on section L5"
WHEN L5.s=Departing, *npTO3.s>=Departing, *npTO3.s<=Occupied                    {WHEN 721}
 OR  L5.s=Departing, *npTO3.s=StopClear
 DO	L5.s=WaitClear
WHEN L5.s=WaitClear, OptL5=FALSE, OptOL5=FALSE, CurL5=FALSE                     {WHEN 722}
 DO	L5.s=Free, L5.t=0, L5.p=0
	$Color Block(14,19,1)=colt0
WHEN L4.s=Free, *lpTO3.s>Booked, L5.p<>0                                        {WHEN 723}
 DO	L4.s=Booked, L4.t=*lpTO3.t, L4.p=L5.p, mxttL4=0
WHEN L4.s=Booked, *lpTO3.s<=Stopping OR L4.s=Arriving, *lpTO3.s<=Stopping       {WHEN 724}
 DO	L4.s=WaitClear, L4.t=0, L4.p=0, $Color Block(5,20,1)=colt0
WHEN L4.s=Free, initialised=2, OptL4=TRUE                                       {WHEN 725}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L4"
	L4.s=WaitClear
WHEN L4.s=Departing, *npN2L4.s<>Booked, *npN2L4.s<Moving, *npN2L4.s<>Stopping, *npN2L4.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L4"          {WHEN 726}
WHEN L4.s>=Booked, L4.s<>GetCal, L4.p<>5, L4.p<>7                               {WHEN 727}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L4"
WHEN L4.s>GetTrain, L4.t=0                                                      {WHEN 728}
 DO	$Draw Message(1,3,3)="Train number 0 on section L4"
WHEN L4.s=Departing, *npN2L4.s>=Departing, *npN2L4.s<=Occupied                  {WHEN 729}
 OR  L4.s=Departing, *npN2L4.s=StopClear
 DO	L4.s=WaitClear
WHEN L4.s=WaitClear, OptL4=FALSE, OptOL4=FALSE, CurL4=FALSE                     {WHEN 730}
 DO	L4.s=Free, L4.t=0, L4.p=0
	$Color Block(5,20,1)=colt0
WHEN L3.s=Free, *rpTO3.s>Booked, L5.p<>0                                        {WHEN 731}
 DO	L3.s=Booked, L3.t=*rpTO3.t, L3.p=L5.p, mxttL3=0
WHEN L3.s=Booked, *rpTO3.s<=Stopping OR L3.s=Arriving, *rpTO3.s<=Stopping       {WHEN 732}
 DO	L3.s=WaitClear, L3.t=0, L3.p=0, $Color Block(6,19,1)=colt0
WHEN L3.s=Free, initialised=2, OptL3=TRUE                                       {WHEN 733}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L3"
	L3.s=WaitClear
WHEN L3.s=Departing, *npN2L3.s<>Booked, *npN2L3.s<Moving, *npN2L3.s<>Stopping, *npN2L3.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L3"          {WHEN 734}
WHEN L3.s>=Booked, L3.s<>GetCal, L3.p<>5, L3.p<>7                               {WHEN 735}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L3"
WHEN L3.s>GetTrain, L3.t=0                                                      {WHEN 736}
 DO	$Draw Message(1,3,3)="Train number 0 on section L3"
WHEN L3.s=Departing, *npN2L3.s>=Departing, *npN2L3.s<=Occupied                  {WHEN 737}
 OR  L3.s=Departing, *npN2L3.s=StopClear
 DO	L3.s=WaitClear
WHEN L3.s=WaitClear, OptL3=FALSE, OptOL3=FALSE, CurL3=FALSE                     {WHEN 738}
 DO	L3.s=Free, L3.t=0, L3.p=0
	$Color Block(6,19,1)=colt0
WHEN L2.s=Free, *pspN2_L2.s>Booked, *pspN2_L2.p<>0                              {WHEN 739}
 DO	L2.s=Booked, L2.t=*pspN2_L2.t, L2.p=*pspN2_L2.p
WHEN L2.s=Booked, *pspN2_L2.s<=Stopping OR L2.s=Arriving, *pspN2_L2.s<=Stopping {WHEN 740}
 DO	L2.s=WaitClear, L2.t=0, L2.p=0, $Color Block(2,16,1)=colt0
WHEN L2.s=Free, initialised=2, OptL2=TRUE                                       {WHEN 741}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L2"
	L2.s=WaitClear
WHEN L2.s=Departing, *npN2L2.s<>Booked, *npN2L2.s<Moving, *npN2L2.s<>Stopping, *npN2L2.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L2"          {WHEN 742}
WHEN L2.s>=Booked, L2.s<>GetCal, L2.p<>6, L2.p<>5, L2.p<>4                      {WHEN 743}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L2"
WHEN L2.s>GetTrain, L2.t=0                                                      {WHEN 744}
 DO	$Draw Message(1,3,3)="Train number 0 on section L2"
WHEN L2.s=Departing, *npN2L2.s>=Departing, *npN2L2.s<=Occupied                  {WHEN 745}
 OR  L2.s=Departing, *npN2L2.s=StopClear
 OR  L2.s=Free, ncnN2L2<>0
 DO	L2.s=WaitClear, L2.p=ncnN2L2
WHEN L2.s=WaitClear, OptL2=FALSE, OptOL2=FALSE, CurL2=FALSE, ncnN2L2=0          {WHEN 746}
 DO	L2.s=Free, L2.t=0, L2.p=0
	$Color Block(2,16,1)=colt0
WHEN L17.s=Free, *pspN2_L17.s>Booked, *pspN2_L17.p<>0                           {WHEN 747}
 DO	L17.s=Booked, L17.t=*pspN2_L17.t, L17.p=*pspN2_L17.p
WHEN L17.s=Booked, *pspN2_L17.s<=Stopping                                       {WHEN 748}
 OR  L17.s=Arriving, *pspN2_L17.s<=Stopping
 DO	L17.s=WaitClear, L17.t=0, L17.p=0, $Color Block(11,12,1)=colt0
WHEN L17.s=Free, initialised=2, OptL17=TRUE                                     {WHEN 749}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L17"
	L17.s=WaitClear
WHEN L17.s=Departing, *lpTI2.s<>Booked, *lpTI2.s<Moving, *lpTI2.s<>Stopping, *lpTI2.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L17"         {WHEN 750}
WHEN L17.s>=Booked, L17.s<>GetCal, L17.p<>4, L17.p<>5                           {WHEN 751}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L17"
WHEN L17.s>GetTrain, L17.t=0                                                    {WHEN 752}
 DO	$Draw Message(1,3,3)="Train number 0 on section L17"
WHEN L17.s=Departing, *lpTI2.s>=Departing, *lpTI2.s<=Occupied                   {WHEN 753}
 OR  L17.s=Departing, *lpTI2.s=StopClear
 DO	L17.s=WaitClear
WHEN L17.s=WaitClear, OptL17=FALSE, OptOL17=FALSE, CurL17=FALSE                 {WHEN 754}
 DO	L17.s=Free, L17.t=0, L17.p=0
	$Color Block(11,12,1)=colt0
WHEN L18.s=Free, *rpTO5.s>Booked, *pspTI2.p<>0                                  {WHEN 755}
 DO	L18.s=Booked, L18.t=*rpTO5.t, L18.p=*pspTI2.p
WHEN L18.s=Booked, *rpTO5.s<=Stopping OR L18.s=Arriving, *rpTO5.s<=Stopping     {WHEN 756}
 DO	L18.s=WaitClear, L18.t=0, L18.p=0, $Color Block(14,8,1)=colt0
WHEN L18.s=Free, initialised=2, OptL18=TRUE                                     {WHEN 757}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L18"
	L18.s=WaitClear
WHEN L18.s=Departing, L19.s<>Booked, L19.s<Moving, L19.s<>Stopping, L19.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L18"         {WHEN 758}
WHEN L18.s>=Booked, L18.s<>GetCal, L18.p<>3, L18.p<>4                           {WHEN 759}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L18"
WHEN L18.s>GetTrain, L18.t=0                                                    {WHEN 760}
 DO	$Draw Message(1,3,3)="Train number 0 on section L18"
WHEN L18.s=Departing, L19.s>=Departing, L19.s<=Occupied                         {WHEN 761}
 OR  L18.s=Departing, L19.s=StopClear
 DO	L18.s=WaitClear
WHEN L18.s=WaitClear, OptL18=FALSE, OptOL18=FALSE, CurL18=FALSE                 {WHEN 762}
 DO	L18.s=Free, L18.t=0, L18.p=0
	$Color Block(14,8,1)=colt0
WHEN L19.s=Free, L18.s>Booked, L18.p<>0                                         {WHEN 763}
 DO	L19.s=Booked, L19.t=L18.t, L19.p=L18.p
WHEN L19.s=Booked, L18.s<=Stopping OR L19.s=Arriving, L18.s<=Stopping           {WHEN 764}
 DO	L19.s=WaitClear, L19.t=0, L19.p=0, $Color Block(13,4,1)=colt0
WHEN L19.s=Free, initialised=2, OptL19=TRUE                                     {WHEN 765}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L19"
	L19.s=WaitClear
WHEN L19.s=Departing, *npN1L19.s<>Booked, *npN1L19.s<Moving, *npN1L19.s<>Stopping, *npN1L19.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L19"         {WHEN 766}
WHEN L19.s>=Booked, L19.s<>GetCal, L19.p<>3, L19.p<>4                           {WHEN 767}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L19"
WHEN L19.s>GetTrain, L19.t=0                                                    {WHEN 768}
 DO	$Draw Message(1,3,3)="Train number 0 on section L19"
WHEN L19.s=Departing, *npN1L19.s>=Departing, *npN1L19.s<=Occupied               {WHEN 769}
 OR  L19.s=Departing, *npN1L19.s=StopClear
 DO	L19.s=WaitClear
WHEN L19.s=WaitClear, OptL19=FALSE, OptOL19=FALSE, CurL19=FALSE                 {WHEN 770}
 DO	L19.s=Free, L19.t=0, L19.p=0
	$Color Block(13,4,1)=colt0
WHEN L20.s=Free, *lpTO5.s>Booked, *pspTI2.p<>0                                  {WHEN 771}
 DO	L20.s=Booked, L20.t=*lpTO5.t, L20.p=*pspTI2.p
WHEN L20.s=Booked, *lpTO5.s<=Stopping OR L20.s=Arriving, *lpTO5.s<=Stopping     {WHEN 772}
 DO	L20.s=WaitClear, L20.t=0, L20.p=0, $Color Block(13,9,1)=colt0
WHEN L20.s=Free, initialised=2, OptL20=TRUE                                     {WHEN 773}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L20"
	L20.s=WaitClear
WHEN L20.s=Departing, L21.s<>Booked, L21.s<Moving, L21.s<>Stopping, L21.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L20"         {WHEN 774}
WHEN L20.s>=Booked, L20.s<>GetCal, L20.p<>2, L20.p<>4                           {WHEN 775}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L20"
WHEN L20.s>GetTrain, L20.t=0                                                    {WHEN 776}
 DO	$Draw Message(1,3,3)="Train number 0 on section L20"
WHEN L20.s=Departing, L21.s>=Departing, L21.s<=Occupied                         {WHEN 777}
 OR  L20.s=Departing, L21.s=StopClear
 DO	L20.s=WaitClear
WHEN L20.s=WaitClear, OptL20=FALSE, OptOL20=FALSE, CurL20=FALSE                 {WHEN 778}
 DO	L20.s=Free, L20.t=0, L20.p=0
	$Color Block(13,9,1)=colt0
WHEN L21.s=Free, L20.s>Booked, L20.p<>0                                         {WHEN 779}
 DO	L21.s=Booked, L21.t=L20.t, L21.p=L20.p
WHEN L21.s=Booked, L20.s<=Stopping OR L21.s=Arriving, L20.s<=Stopping           {WHEN 780}
 DO	L21.s=WaitClear, L21.t=0, L21.p=0, $Color Block(13,5,1)=colt0
WHEN L21.s=Free, initialised=2, OptL21=TRUE                                     {WHEN 781}
 DO	$Draw Message(1,3,3)="Entry sensor triggered when free on section L21"
	L21.s=WaitClear
WHEN L21.s=Departing, *npN1L21.s<>Booked, *npN1L21.s<Moving, *npN1L21.s<>Stopping, *npN1L21.s<>StopClear
 DO	$Draw Message(1,3,3)="Next not ready for departing section L21"         {WHEN 782}
WHEN L21.s>=Booked, L21.s<>GetCal, L21.p<>2, L21.p<>4                           {WHEN 783}
 DO	$Draw Message(1,3,3)="Controller number invalid on section L21"
WHEN L21.s>GetTrain, L21.t=0                                                    {WHEN 784}
 DO	$Draw Message(1