# Segnale basso (marmotta) imperativo per il binario alla sua sinistra; lampeg-
# gia se il segnale successivo è spento, o se segue una stazione fittizia 'X'
# (quando funge da indicatore di partenza per segnale comune a più binari).
# Se spento si apre solo con Ctrl-Click.
#
# Aspetti: _ (red), | (shunt), |x (flashing), spento (off), guasto (fault)

# Shunting dwarf commanding to the left; it flashes if the next
# signal is disabled or a fake 'X' station follows (used when it's
# actually the departure indicator before a shared exit signal).
# When unlit clears only with Ctrl-click.
#
# Aspects: _ (red), | (shunt), |x (flashing), off, fault (off)

# 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: 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

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: 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: proceed

# initial aspect

OnInit:
	.aspect = red
	if .nextStation = X and next.script > "rfi_part" and next.script < "rfi_paru"
		next.noclickpenalty = 1
	end
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 seguita da un segnale di partenza comune chiuso, prova ad aprirlo; se non ci riesce non si apre
	# If followed by an unclear shared exit signal, try to clear it; if not successful, doesn't clear
	if .nextStation = X and next.script > "rfi_part" and next.script < "rfi_paru" and next.aspect = red
		if next.color = black and next.locked = 0
			.aspect = flashing
			next.click = 1
		end
		return
	end
	.aspect = shunt
end

# this or another signal changed its aspect

OnUpdate:
	# Se chiusa e seguita da un segnale di partenza (comune) aperto, lo chiude
	# If unclear and followed by a clear shared exit signal, unclear it
	if .nextStation = X and next.script > "rfi_part" and next.script < "rfi_paru" and next.aspect ! red and next.color ! white and .color = black and .occupied = 0 and .aspect = red or .aspect = off
		next.click = 1
	end
	# Se chiusa o guasta non cambia - If unclear or faulty no change
	if .aspect = red or .aspect = off or .aspect = fault
		return
	end
	# Qui è aperta; 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 seguita da un segnale di partenza comune chiuso, prova ad aprirlo; se non ci riesce si richiude
	# If followed by an unclear shared exit signal, try to clear it; if not successful, unclears
	if .nextStation = X and next.script > "rfi_part" and next.script < "rfi_paru" and next.aspect = red
		if and next.color = black and next.locked = 0
			.aspect = flashing
			next.click = 1
		else
			if .color ! white
				.click = 1
			end
		end
		return
	end
	.aspect = shunt

	# Manovre forzate: lampeggio non ammesso - Forced shunting: flashing not allowed
	if next.aspect = red or next.aspect = shunt or .color = white
		return
	end
	if next.aspect = off and next.script > "rfi_LL" and next.script < "rfi_LN"
		return
	end
	# Lampeggio se il prossimo è spento o è un segnale di partenza comune aperto
	# Flashing if next is unlit or is an uncleared shared exit signal
	if .nextStation = X and next.script > "rfi_part" and next.script < "rfi_paru" and next.aspect ! red and next.color ! white
		.aspect = flashing
		return
	end
	if next.aspect = disabled or next.aspect = off
		.aspect = flashing
	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:
	if .aspect = fault
		.aspect = off
	else
		.aspect = red
	end
end

# the player or a script uncleared the signal

OnUncleared:
	if .aspect = fault
		.aspect = off
	else
		.aspect = red
	end
end
