ToLiSS_Auto_Lights.lua

--[[
	ToLiSS Auto Lights
	v1.1.0 FrankLFRS 2024
	
	The assistant checks a few parameters to set lights for you (Rwy turn off, land and nose)
	
	For ToLiSS only
--]]


-- ToLiSS only
if PLANE_ICAO == "A319" or PLANE_ICAO == "A321" or PLANE_ICAO == "A20N" or PLANE_ICAO == "A346" then


-- Set runway turn off lights: 0 = OFF / 1 = ON

function ToLiSS_Lights_Rwy_Turn_Off(Switch_Position)
	if Switch_Position == 1 then
		command_once("toliss_airbus/lightcommands/TurnoffLightOn")
	else
		command_once("toliss_airbus/lightcommands/TurnoffLightOff")
	end
end


-- Set landing lights: 0 = RETRACT / 1 = OFF / 2 = ON

function ToLiSS_Lights_Land(Switch_Position)
	command_once("toliss_airbus/lightcommands/LLandLightDown")
	command_once("toliss_airbus/lightcommands/LLandLightDown")
	command_once("toliss_airbus/lightcommands/RLandLightDown")
	command_once("toliss_airbus/lightcommands/RLandLightDown")
	for i = Switch_Position, 1, -1 do
		command_once("toliss_airbus/lightcommands/LLandLightUp")
		command_once("toliss_airbus/lightcommands/RLandLightUp")
	end
end


-- Set nose lights: 0 = OFF / 1 = TAXI / 2 = ON

function ToLiSS_Lights_Nose(Switch_Position)
	command_once("toliss_airbus/lightcommands/NoseLightDown")
	command_once("toliss_airbus/lightcommands/NoseLightDown")
	for i = Switch_Position, 1, -1 do
		command_once("toliss_airbus/lightcommands/NoseLightUp")
	end
end


-- Set all lights: Runway turn off, Land, Nose

function ToLiSS_Lights(Rwy_Turn_Off_Switch_Position, Land_Switch_Position, Nose_Switch_Position)
	ToLiSS_Lights_Rwy_Turn_Off(Rwy_Turn_Off_Switch_Position)
	ToLiSS_Lights_Land(Land_Switch_Position)
	ToLiSS_Lights_Nose(Nose_Switch_Position)
end


-- Flight steps

TOLISS_STEP_AT_GATE = 0
TOLISS_STEP_TAXI = 1
TOLISS_STEP_TAKEOFF = 2
TOLISS_STEP_DEPARTURE = 3
TOLISS_STEP_EN_ROUTE = 4
TOLISS_STEP_ARRIVAL = 5
TOLISS_STEP_FINAL = 6
TOLISS_STEP_LAND = 7
TOLISS_STEP_VACATE = 8
TOLISS_STEP_END = 9


-- Init first flight step

ToLiSS_Event_Step = TOLISS_STEP_AT_GATE


-- Datarefs

DataRef("ToLiSS_Is_Engine_Running", "sim/flightmodel/engine/ENGN_running") -- Engine on and using fuel (only reliable in 740 and later)
DataRef("ToLiSS_Gear_Deploy_Ratio", "sim/aircraft/parts/acf_gear_deploy") -- Landing gear deployment, 0.0->1.0
DataRef("ToLiSS_Altitude_ft", "sim/cockpit2/gauges/indicators/altitude_ft_pilot") -- Indicated height, MSL, in feet, primary system, based on pilots barometric pressure input
DataRef("ToLiSS_Is_On_Ground", "sim/flightmodel2/gear/on_ground") -- Is this wheel on the ground
DataRef("ToLiSS_APU_Switch", "sim/cockpit/engine/APU_switch") -- APU starter switch 0 = off, 1 = on, 2 = start
DataRef("ToLiSS_Chrono_Time_ND_1", "AirbusFBW/ChronoTimeND1") -- Captain ND chrono value
DataRef("ToLiSS_Chrono_Time_ND_2", "AirbusFBW/ChronoTimeND2") -- Copilot ND chrono value
DataRef("ToLiSS_ParkBrake", "AirbusFBW/ParkBrake") -- ParkBrake status 0 = off, 1 = on
DataRef("ToLiSS_Throttle_Input", "AirbusFBW/throttle_input") -- Throttles position, idle->toga, 0.0->1.0


--[[ Debug tool
function ToLiSS_LogDebug()
	logMsg("PLANE_ICAO: "..PLANE_ICAO)
end

create_command("FlyWithLua/ToLiSS/Lights/Debug", "ToLiSS Autolights Debugger", "ToLiSS_LogDebug()", "", "")
--]]


-- Next step event

function ToLiSS_Event_Next()
	ToLiSS_Event_Step = (ToLiSS_Event_Step+1)%TOLISS_STEP_END
end


-- Autolights (every 10s)

function ToLiSS_Auto_Lights_Do_Sometimes()
	if(ToLiSS_Event_Step == TOLISS_STEP_TAKEOFF and ToLiSS_Gear_Deploy_Ratio < 0.9) then
		ToLiSS_Lights(0, 2, 0)
		ToLiSS_Event_Next()
	elseif(ToLiSS_Event_Step == TOLISS_STEP_DEPARTURE and ToLiSS_Altitude_ft > 10500) then
		ToLiSS_Lights(0, 0, 0)
		ToLiSS_Event_Next()
	elseif(ToLiSS_Event_Step == TOLISS_STEP_EN_ROUTE and ToLiSS_Altitude_ft < 10200) then
		ToLiSS_Lights(0, 2, 0)
		ToLiSS_Event_Next()
	elseif(ToLiSS_Event_Step == TOLISS_STEP_ARRIVAL and ToLiSS_Gear_Deploy_Ratio > 0.9) then
		ToLiSS_Lights(1, 2, 2)
		ToLiSS_Event_Next()
	elseif(ToLiSS_Event_Step == TOLISS_STEP_FINAL and ToLiSS_Is_On_Ground == 1) then
		ToLiSS_Event_Next()
	elseif(ToLiSS_Event_Step == TOLISS_STEP_VACATE and ToLiSS_Is_Engine_Running == 0 and ToLiSS_ParkBrake == 1) then
		ToLiSS_Lights(0, 0, 0)
		ToLiSS_Event_Next()
	end
end


-- Autolights (every 1s)

function ToLiSS_Auto_Lights_Do_Often()
	if(ToLiSS_Event_Step == TOLISS_STEP_AT_GATE and ToLiSS_Is_Engine_Running == 1 and ToLiSS_Throttle_Input > 0.1 and ToLiSS_ParkBrake == 0) then
		ToLiSS_Lights(1, 0, 1)
		ToLiSS_Event_Next()
	elseif(ToLiSS_Event_Step == TOLISS_STEP_TAXI and (ToLiSS_Chrono_Time_ND_1+ToLiSS_Chrono_Time_ND_2) > 0) then
		ToLiSS_Lights(1, 2, 2)
		ToLiSS_Event_Next()
	elseif(ToLiSS_Event_Step == TOLISS_STEP_LAND and ToLiSS_APU_Switch > 0) then
		ToLiSS_Lights(1, 0, 1)
		ToLiSS_Event_Next()
	end
end


-- Beginning of the periodic checking

do_sometimes("ToLiSS_Auto_Lights_Do_Sometimes()")
do_often("ToLiSS_Auto_Lights_Do_Often()")


end -- ToLiSS only