# Segnale basso di partenza per il caso in cui il segnale vero è comune a più binari; ha l'aspetto
# di una marmotta per il binario di destra (comanda a sinistra) e per i segnali precedenti è un
# segnale normale a due vele: quando è aperto il suo vero aspetto è mostrato dal segnale comune
# che segue ("rfi_com_**.tds"). Da spento è apribile solo con Ctrl-click.
#
# Aspetti: off (spento), fault (guasto), R (_), S (|), B (|),
#          V, G, Gx, GG, GV, GxVx, Gx/Vx, RV, RG, RGx (tutti mostrati come | lampeggiante)

# Departure dwarf for the case when the real signal is shared by 2 or more tracks; it looks like
# a dwarf commanding to the left, and for the previous signal it's a normal signal with 2 heads:
# when it's clear its real aspect is shown by the shared signal that follows ("rfi_com_**.tds").
# When unlit it can be cleared only with Ctrl-click.
#
# Aspects: off, fault, R (_), S (|), B (|),
#          G, Y, Yx, YY, YG, YxGx, Yx/Gx, RG, RY, RYx (all shown as a flashing |)

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

Aspect: red
	IconE: MF_-_E.xpm
	IconW: MF_-_W.xpm
	IconN: MF_-_N.xpm
	IconS: MF_-_S.xpm
	Action: stop

Aspect: block
	IconE: MF_I_E.xpm
	IconW: MF_I_W.xpm
	IconN: MF_I_N.xpm
	IconS: MF_I_S.xpm
	Action: stop

Aspect: shunt
	IconE: MF_I_E.xpm
	IconW: MF_I_W.xpm
	IconN: MF_I_N.xpm
	IconS: MF_I_S.xpm
	Action: speedLimit 29

Aspect: green
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: proceed

Aspect: yellow
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 110

Aspect: short_yellow
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 110

Aspect: yellow_flashing
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 150

Aspect: yellow_yellow
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 50

Aspect: yellow_green
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 110

Aspect: short_yellow_green_30
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 110

Aspect: yellow_green_flashing
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 110

Aspect: short_yellow_green_60
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 110

Aspect: yellow_green_switching
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 150

Aspect: short_yellow_green_100
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 150

Aspect: red_green_30
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 30

Aspect: red_green_60
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 60

Aspect: red_green_100
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 100

Aspect: red_yellow_30
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 30

Aspect: red_yellow_60
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 60

Aspect: red_yellow_flashing_30
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 30

Aspect: red_yellow_flashing_60
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 60

Aspect: red_yellow_flashing_100
	IconE: MF_I_E.xpm MF___E.xpm
	IconW: MF_I_W.xpm MF___W.xpm
	IconN: MF_I_N.xpm MF___N.xpm
	IconS: MF_I_S.xpm MF___S.xpm
	Action: speedLimit 100

Aspect: off
	IconE: MF___E.xpm
	IconW: MF___W.xpm
	IconN: MF___N.xpm
	IconS: MF___S.xpm
	Action: stop

Aspect: fault
	IconE: MF___E.xpm
	IconW: MF___W.xpm
	IconN: MF___N.xpm
	IconS: MF___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 segue un segnale comune, ne imposta l'aspetto - If a shared signal follows, set its aspect
	if nextApproach.script > "r"
		if .nextApproach.script > "rfi_com" and .nextApproach.script < "rfi_con"
			if .aspect = block 
				if .nextApproach.aspect = red
					.aspect = red
				end
			else
				# Dopo il comune c'è un avviso: forza RV o V - A distant follows the shared signal: force RG or G
				if .nextApproach.nextStation = G
					if .aspect = red_green_100 or .aspect = red_yellow_flashing_100
						.nextApproach.aspect = red_green_100
					end
					if .aspect = red_green_60 or .aspect = red_yellow_flashing_60 or .aspect = red_yellow_60
						.nextApproach.aspect = red_green_60
					end
					if .aspect = red_green_30 or .aspect = red_yellow_flashing_30 or .aspect = red_yellow_30
						.nextApproach.aspect = red_green_30
					end
					if .aspect < red or .aspect > ree
						.nextApproach.aspect = green
					end
					if .aspect = red or .aspect = off or .aspect = shunt
						.nextApproach.aspect = .aspect
					end
				else
					.nextApproach.aspect = .aspect
				end
				if .aspect = fault
					.nextApproach.aspect = off
				end
			end
		else
			# Se dopo un primo avviso segue un segnale comune, imposta l'aspetto di quest'ultimo
			# If after a first distant a shared signal follows, set the latter's aspect
			if nextApproach.nextApproach.script > "r" and nextApproach.nextApproach.script > "rfi_com" and nextApproach.nextApproach.script < "rfi_con"
				if .aspect = block
					if nextApproach.nextApproach.aspect = red
						.aspect = red
					end
				else
					# Dopo il comune c'è un avviso: forza RV o V - A distant follows the shared signal: force RG or G
					if nextApproach.nextApproach.nextStation = G
						if .aspect = red_green_100 or .aspect = red_yellow_flashing_100
							nextApproach.nextApproach.aspect = red_green_100
						end
						if .aspect = red_green_60 or .aspect = red_yellow_flashing_60 or .aspect = red_yellow_60
							nextApproach.nextApproach.aspect = red_green_60
						end
						if .aspect = red_green_30 or .aspect = red_yellow_flashing_30 or .aspect = red_yellow_30
							nextApproach.nextApproach.aspect = red_green_30
						end
						if .aspect < red or .aspect > ree
							nextApproach.nextApproach.aspect = green
						end
						if .aspect = red or .aspect = off or .aspect = shunt
							nextApproach.nextApproach.aspect = .aspect
						end
					else
						nextApproach.nextApproach.aspect = .aspect
					end
					if .aspect = fault
						nextApproach.nextApproach.aspect = off
					end
				end
			end
		end
	end

	# Se chiuso o guasto non cambia - If unclear or faulty no change
	if .aspect = block or .aspect = red or .aspect = off or .aspect = fault
		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
	# Se qualcosa forza la manovra o segue un segnale spento superabile o non mascherato: S
	# If something forces shunt or a passable or unmasked unlit signal follows: S
	if next.script = "rfi_sam_i.tds" or next.script = "rfi_marm_i.tds" or next.script = "rfi_marm_id.tds" or .nextLimit < 30 or .color = white or next.aspect = disabled or next.aspect = off
		.aspect = shunt
		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

	# Deviata vera o avviso nascosto che la forza - essendo un segnale basso non controllo gli avvisi
	# Diverging route, true or forced by the hidden distant - being a dwarf don't consider distants
	if .switchThrown = 1 or .nextStation = K or nextApproach.script = "rfi_avv_dev.tds"

		# Aspetti che richiedono RG: (R | S | RGG | GG) o anche il prossimo è deviato (segnale incompleto), anche forzato
		# Normal aspects requiring RY: (R | S | RYY | YY) or next is also diverging (incomplete aspect), even forced
		if next.aspect = yellow_yellow or next.aspect = red or next.aspect = shunt or next.aspect = red_yellow_yellow or next.switchThrown = 1 or next.nextStation = K or next.nextApproach.script = "rfi_avv_dev.tds"
			if .nextLength < 600 or nextApproach.aspect = red_short or .nextLimit < 60
				.aspect = red_yellow_30		# Blocco corto o deviata a 30
			else							# Short block or diverging at 30 km/h
				.aspect = red_yellow_60
			end
			return
		end
		# Prossimo non deviato e non (R | S | RGG | GG) - Next is not diverging and not showing (R | S | RYY | GG)

		# Aspetti che richiedono RGx: (G | GV | GxVx | Gx/Vx) se vicini al successivo
		# Aspects requiring RYx: (Y | YG | YxGx | Yx/Gx) if near the next signal
		if next.aspect > short and next.aspect < shoru
			if .nextLimit < 60
				.aspect = red_yellow_flashing_30
			else
				if .nextLimit < 100
					.aspect = red_yellow_flashing_60
				else
					.aspect = red_yellow_flashing_100
				end
			end
			return
		end
		# Ogni altro caso richiede RV (V | Gx | G | GV | GxVx | Gx/Vx), compreso nessun altro segnale (uscita)
		# Any other case requires RG (G | Yx | Y | YG | YxGx | Yx/Gx), including no signal follows (exit point)
		if .nextLimit < 60
			.aspect = red_green_30
		else
			if .nextLimit < 100
				.aspect = red_green_60
			else
				.aspect = red_green_100
			end
		end
	else
	# Corretto tracciato (.switchThrown = 0 e deviata non forzata da avvisi speciali)
	# Straight route (.switchThrown = 0 and no diversion forced by special distants)

		# Aspetti che richiedono GG (R | S | RGG se corti o forzati tali) o G (idem se normali, o GG):
		# Aspects requiring YY (R | S | RYY when short or forced so) or Y (the same when normal, or YY):
		if next.aspect = yellow_yellow or next.aspect = red or next.aspect = shunt or next.aspect = red_yellow_yellow
			if next.aspect ! yellow_yellow and .nextLength < 600 or nextApproach.aspect = red_short
				.aspect = yellow_yellow		# Blocco corto - Short block
			else
				if .nextLength < 1200
					.aspect = short_yellow
				else
					.aspect = yellow
				end
			end
			return
		end
		# Se il prossimo è deviato (non è R | S | RGG | GG) - If next is diverging (not showing R | S | RYY | YY)
		if next.switchThrown = 1 or next.nextStation = K or next.nextApproach.script = "rfi_avv_dev.tds"
			# Se il prossimo è (G | Gx | V) è deviato ma incompleto: G invece di (GV | GxVx | Gx/Vx)
			# If next shows (Y | Yx | G) it's diverging but incomplete: show Y instead of (YG | YxGx | Yx/Gx)
			if next.aspect = short_yellow or next.aspect = yellow or next.aspect = yellow_flashing or next.aspect = green
				if .nextLength < 1200
					.aspect = short_yellow
				else
					.aspect = yellow
				end
				return
			end
			# Prossimo deviato e completo - Next is diverging and complete

			# Se deviata a 30 o forzata tale, abbassa anche l'avviso
			# If next is diverging at 30, or forced so, slow down also the distant
			if next.nextLimit < 60 or next.aspect = red_yellow_30
				if .nextLength < 1200
					.aspect = short_yellow_green_30
				else
					.aspect = yellow_green
				end
			else
				# Se deviata a 60 o forzata tale, abbassa anche l'avviso
				# If next is diverging at 60, or forced so, slow down also the distant
				if next.nextLimit < 100 or next.aspect = red_yellow_60 or next.aspect = red_yellow_green_60
					if .nextLength < 1200
						.aspect = short_yellow_green_60
					else
						.aspect = yellow_green_flashing
					end
				else
					# Deviata a 100, non forzabile - Next is diverging at 100, no forcing
					if .nextLength < 1200
						.aspect = short_yellow_green_100
					else
						.aspect = yellow_green_switching
					end
				end
			end
			return
		end		# Prossimo non deviato e non (R | S | RGG | GG) - Next is not diverging and not showing (R | S | RYY | YY)

		# Aspetti che richiedono Gx: (G | GV | GxVx | Gx/Vx) se vicini al successivo
		# Aspects requiring Yx: (Y | YG | YxGx | Yx/Gx) if near the next signal
		if next.aspect > short and next.aspect < shoru
			.aspect = yellow_flashing
			return
		end
		# Ogni altro caso richiede V (V | Gx | G | GV | GxVx | Gx/Vx o nessun altro segnale: uscita)
		# Any other case requires G (G | Yx | Y | YG | YxGx | Yx/Gx or no signal follows: exit point)
		.aspect = green
	end
end

# the player or a script cleared the signal for shunting

OnShunt:
	if .aspect = off
		.aspect = fault
	else
		.aspect = shunt
	end
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
		if .nextApproach.script > "rfi_com" and .nextApproach.script < "rfi_con"
			.aspect = block
			return
		end
		if nextApproach.script > "r" and .nextApproach.nextApproach.script > "rfi_com" and .nextApproach.nextApproach.script < "rfi_con"
			.aspect = block
			return
		end
		.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
