# Segnale imperativo a tre vele quadre, seguito da qualsiasi segnale.
# Se aperto con Ctrl-click si apre con RGG invece di S se il binario
# è ingombro; da spento è apribile solo con Ctrl-click.
#
# Aspetti: R, S, G, Gx, V, GG, GV, GxVx, Gx/Vx, off (spento), fault (guasto),
#          RV, RG, RGx, RGG, RGV, RGxVx, RGx/Vx

# Main signal with 3 square heads, followed by whatever signal.
# When cleared with ctrl-click (white track) it shows RYY instead of S if
# the track is occupied; when unlit it can be cleared only with Ctrl-click.
#
# Aspects:  R, S, Y, Yx, G, YY, YG, YxGx, Yx/Gx, off, fault,
#           RG, RY, RYx, RYY, RYG, RYxGx, RYx/Gx

# P.Rosati  21/01/2021 (Versione 5.1)

Aspect: red
	IconE: NQ_rbb_E.xpm
	IconW: NQ_rbb_W.xpm
	IconN: NQ_rbb_N.xpm
	IconS: NQ_rbb_S.xpm
	Action: stop

Aspect: shunt
	IconE: NQ_rbb_E.xpm
	IconW: NQ_rbb_W.xpm
	IconN: NQ_rbb_N.xpm
	IconS: NQ_rbb_S.xpm
	Action: speedLimit 29

Aspect: green
	IconE: NQ_gbb_E.xpm
	IconW: NQ_gbb_W.xpm
	IconN: NQ_gbb_N.xpm
	IconS: NQ_gbb_S.xpm
	Action: proceed

Aspect: yellow
	IconE: NQ_ybb_E.xpm
	IconW: NQ_ybb_W.xpm
	IconN: NQ_ybb_N.xpm
	IconS: NQ_ybb_S.xpm
	Action: speedLimit 110

Aspect: short_yellow
	IconE: NQ_ybb_E.xpm
	IconW: NQ_ybb_W.xpm
	IconN: NQ_ybb_N.xpm
	IconS: NQ_ybb_S.xpm
	Action: speedLimit 110

Aspect: yellow_flashing
	IconE: NQ_ybb_E.xpm NQ_bbb_E.xpm
	IconW: NQ_ybb_W.xpm NQ_bbb_W.xpm
	IconN: NQ_ybb_N.xpm NQ_bbb_N.xpm
	IconS: NQ_ybb_S.xpm NQ_bbb_S.xpm
	Action: speedLimit 150

Aspect: yellow_yellow
	IconE: NQ_yyb_E.xpm
	IconW: NQ_yyb_W.xpm
	IconN: NQ_yyb_N.xpm
	IconS: NQ_yyb_S.xpm
	Action: speedLimit 50

Aspect: yellow_green
	IconE: NQ_ygb_E.xpm
	IconW: NQ_ygb_W.xpm
	IconN: NQ_ygb_N.xpm
	IconS: NQ_ygb_S.xpm
	Action: speedLimit 110

Aspect: short_yellow_green_30
	IconE: NQ_ygb_E.xpm
	IconW: NQ_ygb_W.xpm
	IconN: NQ_ygb_N.xpm
	IconS: NQ_ygb_S.xpm
	Action: speedLimit 110

Aspect: yellow_green_flashing
	IconE: NQ_ygb_E.xpm NQ_bbb_E.xpm
	IconW: NQ_ygb_W.xpm NQ_bbb_W.xpm
	IconN: NQ_ygb_N.xpm NQ_bbb_N.xpm
	IconS: NQ_ygb_S.xpm NQ_bbb_S.xpm
	Action: speedLimit 110

Aspect: short_yellow_green_60
	IconE: NQ_ygb_E.xpm NQ_bbb_E.xpm
	IconW: NQ_ygb_W.xpm NQ_bbb_W.xpm
	IconN: NQ_ygb_N.xpm NQ_bbb_N.xpm
	IconS: NQ_ygb_S.xpm NQ_bbb_S.xpm
	Action: speedLimit 110

Aspect: yellow_green_switching
	IconE: NQ_ybb_E.xpm NQ_bgb_E.xpm
	IconW: NQ_ybb_W.xpm NQ_bgb_W.xpm
	IconN: NQ_ybb_N.xpm NQ_bgb_N.xpm
	IconS: NQ_ybb_S.xpm NQ_bgb_S.xpm
	Action: speedLimit 150

Aspect: short_yellow_green_100
	IconE: NQ_ybb_E.xpm NQ_bgb_E.xpm
	IconW: NQ_ybb_W.xpm NQ_bgb_W.xpm
	IconN: NQ_ybb_N.xpm NQ_bgb_N.xpm
	IconS: NQ_ybb_S.xpm NQ_bgb_S.xpm
	Action: speedLimit 150

Aspect: red_yellow_yellow
	IconE: NQ_ryy_E.xpm
	IconW: NQ_ryy_W.xpm
	IconN: NQ_ryy_N.xpm
	IconS: NQ_ryy_S.xpm
	Action: speedLimit 30

Aspect: red_green_30
	IconE: NQ_rgb_E.xpm
	IconW: NQ_rgb_W.xpm
	IconN: NQ_rgb_N.xpm
	IconS: NQ_rgb_S.xpm
	Action: speedLimit 30

Aspect: red_green_60
	IconE: NQ_rgb_E.xpm
	IconW: NQ_rgb_W.xpm
	IconN: NQ_rgb_N.xpm
	IconS: NQ_rgb_S.xpm
	Action: speedLimit 60

Aspect: red_green_100
	IconE: NQ_rgb_E.xpm
	IconW: NQ_rgb_W.xpm
	IconN: NQ_rgb_N.xpm
	IconS: NQ_rgb_S.xpm
	Action: speedLimit 100

Aspect: red_yellow_30
	IconE: NQ_ryb_E.xpm
	IconW: NQ_ryb_W.xpm
	IconN: NQ_ryb_N.xpm
	IconS: NQ_ryb_S.xpm
	Action: speedLimit 30

Aspect: red_yellow_60
	IconE: NQ_ryb_E.xpm
	IconW: NQ_ryb_W.xpm
	IconN: NQ_ryb_N.xpm
	IconS: NQ_ryb_S.xpm
	Action: speedLimit 60

Aspect: red_yellow_flashing_30
	IconE: NQ_ryb_E.xpm NQ_rbb_E.xpm
	IconW: NQ_ryb_W.xpm NQ_rbb_W.xpm
	IconN: NQ_ryb_N.xpm NQ_rbb_N.xpm
	IconS: NQ_ryb_S.xpm NQ_rbb_S.xpm
	Action: speedLimit 30

Aspect: red_yellow_flashing_60
	IconE: NQ_ryb_E.xpm NQ_rbb_E.xpm
	IconW: NQ_ryb_W.xpm NQ_rbb_W.xpm
	IconN: NQ_ryb_N.xpm NQ_rbb_N.xpm
	IconS: NQ_ryb_S.xpm NQ_rbb_S.xpm
	Action: speedLimit 60

Aspect: red_yellow_flashing_100
	IconE: NQ_ryb_E.xpm NQ_rbb_E.xpm
	IconW: NQ_ryb_W.xpm NQ_rbb_W.xpm
	IconN: NQ_ryb_N.xpm NQ_rbb_N.xpm
	IconS: NQ_ryb_S.xpm NQ_rbb_S.xpm
	Action: speedLimit 100

Aspect: red_yellow_green_30
	IconE: NQ_ryg_E.xpm
	IconW: NQ_ryg_W.xpm
	IconN: NQ_ryg_N.xpm
	IconS: NQ_ryg_S.xpm
	Action: speedLimit 30

Aspect: red_yellow_green_60
	IconE: NQ_ryg_E.xpm
	IconW: NQ_ryg_W.xpm
	IconN: NQ_ryg_N.xpm
	IconS: NQ_ryg_S.xpm
	Action: speedLimit 60

Aspect: red_yellow_green_100
	IconE: NQ_ryg_E.xpm
	IconW: NQ_ryg_W.xpm
	IconN: NQ_ryg_N.xpm
	IconS: NQ_ryg_S.xpm
	Action: speedLimit 100

Aspect: red_yellow_green_flashing_30
	IconE: NQ_ryg_E.xpm NQ_rbb_E.xpm
	IconW: NQ_ryg_W.xpm NQ_rbb_W.xpm
	IconN: NQ_ryg_N.xpm NQ_rbb_N.xpm
	IconS: NQ_ryg_S.xpm NQ_rbb_S.xpm
	Action: speedLimit 30

Aspect: red_yellow_green_flashing_60
	IconE: NQ_ryg_E.xpm NQ_rbb_E.xpm
	IconW: NQ_ryg_W.xpm NQ_rbb_W.xpm
	IconN: NQ_ryg_N.xpm NQ_rbb_N.xpm
	IconS: NQ_ryg_S.xpm NQ_rbb_S.xpm
	Action: speedLimit 60

Aspect: red_yellow_green_flashing_100
	IconE: NQ_ryg_E.xpm NQ_rbb_E.xpm
	IconW: NQ_ryg_W.xpm NQ_rbb_W.xpm
	IconN: NQ_ryg_N.xpm NQ_rbb_N.xpm
	IconS: NQ_ryg_S.xpm NQ_rbb_S.xpm
	Action: speedLimit 100

Aspect: red_yellow_green_switching_30
	IconE: NQ_ryb_E.xpm NQ_rbg_E.xpm
	IconW: NQ_ryb_W.xpm NQ_rbg_W.xpm
	IconN: NQ_ryb_N.xpm NQ_rbg_N.xpm
	IconS: NQ_ryb_S.xpm NQ_rbg_S.xpm
	Action: speedLimit 30

Aspect: red_yellow_green_switching_60
	IconE: NQ_ryb_E.xpm NQ_rbg_E.xpm
	IconW: NQ_ryb_W.xpm NQ_rbg_W.xpm
	IconN: NQ_ryb_N.xpm NQ_rbg_N.xpm
	IconS: NQ_ryb_S.xpm NQ_rbg_S.xpm
	Action: speedLimit 60

Aspect: red_yellow_green_switching_100
	IconE: NQ_ryb_E.xpm NQ_rbg_E.xpm
	IconW: NQ_ryb_W.xpm NQ_rbg_W.xpm
	IconN: NQ_ryb_N.xpm NQ_rbg_N.xpm
	IconS: NQ_ryb_S.xpm NQ_rbg_S.xpm
	Action: speedLimit 100

Aspect: off
	IconE: NQ_bbb_E.xpm
	IconW: NQ_bbb_W.xpm
	IconN: NQ_bbb_N.xpm
	IconS: NQ_bbb_S.xpm
	Action: stop

Aspect: fault
	IconE: NQ_bbb_E.xpm
	IconW: NQ_bbb_W.xpm
	IconN: NQ_bbb_N.xpm
	IconS: NQ_bbb_S.xpm
	Action: speedLimit 30

# initial aspect

OnInit:
	.aspect = red
end

# the player or a script cleared the signal

OnCleared:
	# Se bloccato, spento o seguito da un segnale guasto superabile, si apre solo con Ctrl-click
	# If blocked, unlit or followed by a passable faulty signal, clears only with Ctrl-click
	if .aspect = off or next.aspect = fault or next.aspect = block
		return
	end
	# Se seguito da un segnale di partenza aperto, si apre solo con Ctrl-click
	# If followed by a clear exit signal, clears only with Ctrl-click
	if nextApproach.aspect = block or next.script > "rfi_part" and next.script < "rfi_paru" and next.aspect ! red and next.aspect ! off
		return
	end
	# Se seguito da un segnale di partenza (chiuso), lo blocca
	# If followed by an (unclear) exit signal, lock it
	if nextApproach.script = "rfi_avv_part.tds" or next.script > "rfi_part" and next.script < "rfi_paru"
		next.fixedred = 1
	end
	# Aspetto temporaneo: la OnUpdate imposterà quello giusto
	# Temporary aspect: the OnUpdate will set the right one
	.aspect = green
end

# this or another signal changed its aspect

OnUpdate:
	# Se chiuso, guasto o RGG con binario bianco, non cambia
	# If unclear, faulty or RYY with a white track, no change
	if .aspect = red or .aspect = off or .aspect = fault or .aspect = red_yellow_yellow and .color = white
		return
	end
	# Qui è aperto; se segue un segnale guasto superabile, si chiude
	# Here it's clear; if a passable faulty signal follows, unclears
	if next.aspect = fault
		.click = 1
		return
	end
	# Prosecuzione itinerario: shunt se il segnale dopo è R o S
	# Route continuation: shunt if next signal is R or S
	if .nextStation = V and next.aspect = red or next.aspect = shunt
		.aspect = shunt
		return
	end

       if .nexStation = Carimate@3 and next.next.aspect = red
               .aspect = red_yellow_30
               return
        end
		
		
# a train passed the signal

OnCross:
	# Se seguito da un segnale di partenza (bloccato), lo sblocca
	# If followed by a (locked) exit signal, unlock it
	if nextApproach.script = "rfi_avv_part.tds" or next.script > "rfi_part" and next.script < "rfi_paru"
		next.fixedred = 0
	end
	if .aspect = fault
		.aspect = off
	else
		.aspect = red
	end
end

# the player or a script uncleared the signal

OnUncleared:
	# Se seguito da un segnale di partenza (bloccato), lo sblocca
	# If followed by a (locked) exit signal, unlock it
	if nextApproach.script = "rfi_avv_part.tds" or next.script > "rfi_part" and next.script < "rfi_paru"
		next.fixedred = 0
	end
	if .aspect = fault
		.aspect = off
	else
		.aspect = red
	end
end
