--  copyright Laminar Research 2024

--======================================================================================================================
--  This script allows you to configure up to four independent strobe 'circuits'.  The first section below is used to 
--  configure each strobe's parameters by tweaking the values in the Lua tables.  To activate a strobe circuit, set 
--  its ACTIVE_STROBE index to true.  To disable a strobe, set it to false.  All strobes activated will come on with
--   X-Plane's  "strobe on" dataref.

--  XPlane strobe indicies      [0],    [1],    [2],    [3]
------------------------------------------------------------------------------------------------------------------------
ACTIVE_STROBE               =   {true,  true,   true,   true}   --  4 strobe circuits available.  true to activate
NUM_STROBE_PULSES           =   {2,     3,      1,      4}      --  strobe flashes per cycle. 2 = 'double flash', etc.
STROBE_PERIOD               =   {1.5,   1.5,    3,      2}      --  seconds between each strobe cycle.
STROBE_OFFSET               =   {0,     0.4,    .2,     .3}     --  Offset timing to fire strobes at different times.

--  strongly recommend leaving these values as is.  They account for low frame rates.
STROBE_DURATION             =   {0.08,  0.08,   0.08,   0.08}   --  How long the strobe is illuminated per pulse.
STROBE_PULSE_GAP            =   {0.12,  0.12,   0.12,   0.12}   --  Seconds between pulses.  Should be > 0.1

------------------------------------------------------------------------------------------------------------------------ 
--                                  GLOBAL 'Internal' VARS (don't touch these)
------------------------------------------------------------------------------------------------------------------------
g_strobe_running_time       =   {0, 0, 0, 0}                    --  running timer used to time the pulse events.
g_current_strobe_keyframe   =   {1, 1, 1, 1}                    --  keyframe sequencer.
g_strobe_keyframes          =   {{}, {}, {}, {}}                --  holds strobe timing keyframes

------------------------------------------------------------------------------------------------------------------------ 
--                                  FETCH REQUIRED X-PLANE DATAREFS
------------------------------------------------------------------------------------------------------------------------
xdr_sim_is_paused                   =   find_dataref("sim/time/paused")
xdr_override_beacons_and_strobes    =   find_dataref("sim/flightmodel2/lights/override_beacons_and_strobes")

-- Brightness Ratios
xdr_strobe_brightness_ratio         =   find_dataref("sim/flightmodel2/lights/strobe_brightness_ratio")

--  X-Plane strobe and beacon ON cockpit switches state
xdr_strobes_on                      =   find_dataref("sim/cockpit2/switches/strobe_lights_on")

------------------------------------------------------------------------------------------------------------------------ 
--                                          BUILD KEYFRAME TABLES
------------------------------------------------------------------------------------------------------------------------

for i = 1, 4 do
    table.insert(g_strobe_keyframes[i],     0 + STROBE_OFFSET[i])   --  insert initial keyframe,  always the 'first pulse'
end

    --  insert additional keyframes into the pulse table based on CONFIG specs entered above.  
for i = 1, 4 do
    for j = 1, (NUM_STROBE_PULSES[i] - 1)   do  
        table.insert(g_strobe_keyframes[i], g_strobe_keyframes[i][#g_strobe_keyframes[i]] + STROBE_PULSE_GAP[i])
    end
end

------------------------------------------------------------------------------------------------------------------------
function    run_strobes()

    --  Instead of putting continuously running code in the 'before_physics' callback, we put it in this function
    --  specifically for strobe behaviors and we call this function from the 'before_physics' callback instead.  This
    --  is simply a preference as it makes the code a bit more modular and easier to debug.

    for i = 1, 4 do
        if ACTIVE_STROBE[i] then

            g_strobe_running_time[i]    =   (g_strobe_running_time[i]   + SIM_PERIOD ) % STROBE_PERIOD[i]   

            if  g_strobe_running_time[i]    >   g_strobe_keyframes[i][g_current_strobe_keyframe[i]] and
                g_strobe_running_time[i]    <   g_strobe_keyframes[i][g_current_strobe_keyframe[i]] +   STROBE_DURATION[i]

            then    -- strobe is illuminated
                xdr_strobe_brightness_ratio[i-1]    =   1

            else    --  its not illuminated
                xdr_strobe_brightness_ratio[i-1]    =   0   

                -- increment current keyframe, reset back to 1 after the last keyframe
                g_current_strobe_keyframe[i]    =   (g_current_strobe_keyframe[i] % NUM_STROBE_PULSES[i]) + 1       
            end             
        end
    end
end

------------------------------------------------------------------------------------------------------------------------ 
--                                              CONTINUOUS FLCB
------------------------------------------------------------------------------------------------------------------------

function before_physics() -- Per Frame#

    if  xdr_sim_is_paused == 0  and xdr_strobes_on == 1 then    -- sim is not paused AND strobe switch is on.
        run_strobes()   
    end
end

------------------------------------------------------------------------------------------------------------------------ 
--                                              AIRCRAFT LOAD/ UNLOAD EVENTS
------------------------------------------------------------------------------------------------------------------------
function aircraft_load()
    xdr_override_beacons_and_strobes = 1
end

function aircraft_unload()
    xdr_override_beacons_and_strobes = 0
end