#Conditional Hooks
$Application: FS2_Open

$On Game Init: [
    MjolnirArray = {}

    function MjolnirArray:TrackTarget(target,rbc)
        
        local entry = self.RBCs[rbc.Name]
        if not entry then
            entry = {}
            self.RBCs[rbc.Name] = entry
        end

        entry.Target = target
        entry.Ship = rbc
    end

    function MjolnirArray:StopTracking(rbc)
        self.RBCs[rbc.Name] = nil
    end

    mn.LuaSEXPs["lua-RBC-track-target"].Action = function(target,...)
        for i,v in ipairs(arg) do
            MjolnirArray:TrackTarget(target, v[1])
        end
    end

    mn.LuaSEXPs["lua-RBC-stop-tracking"].Action = function(...)
        for i,v in ipairs(arg) do
            MjolnirArray:StopTracking(v[1])
        end
    end

    function MjolnirArray:OnFrame()
        for rbcName, entry in pairs(self.RBCs) do
            if entry.Ship:isValid() and entry.Target:isValid() then
                mn.runSEXP("( set-object-facing-object !"..rbcName.."! !"..entry.Target.Name.."! 30000 )")
            end
        end
    end
    
]

$On Mission Start:
[
    MjolnirArray.RBCs = {}
]

$State: GS_STATE_GAME_PLAY
$On Frame:
[
    MjolnirArray:OnFrame()
]

#End