

# new global vars
CBFP_ModUID = '04F84894-5E6A-11DD-A058-5D6456D89593'


# -------------------------------------------------------------------------------------------------------------
# DON'T CHANGE THESE

CBFP_Path = '' # auto-filled-in below
SpecialWepRestricted = false
UnitCatRestricted = false
_UnitRestricted_cache = {}

# -------------------------------------------------------------------------------------------------------------
# MISC FUNCTIONS

# thanks to Manimal! Returns location to mod using mod Id. Edited by Brute51
function GetActiveModLocation( mod_Id )
    for k, mod in __active_mods do
        if  mod_Id == mod.uid then
            return mod.location
        end
    end
    WARN("Unable to get mod directory! Wrong or missing UID. Searched for UID "..repr(mod_Id))
end

CBFP_Path = GetActiveModLocation(CBFP_ModUID)

# -------------------------------------------------------------------------------------------------------------
# MISC FUNCTION CALLS

doscript(CBFP_Path .. '/lua/CBFPinit.lua') # haven't found a better place to put this call yet

# -------------------------------------------------------------------------------------------------------------
# UNIT RESTRICTION FUNCTIONS   [119] [157]


function UnitRestricted(unit)
    # checks if the unit is allowed to be build in the current game.

    if not CheckUnitRestrictionsEnabled() then     # if no restrictions defined then dont bother
        return false
    end

    local unitId = unit:GetUnitId()
    if _UnitRestricted_cache[unitId] then          # use cache if available
        return _UnitRestricted_cache[unitId]
    end

    CacheRestrictedUnitLists()
    _UnitRestricted_cache[unitId] = false
    for k, cat in UnitCatRestricted do
        if EntityCategoryContains( cat, unitId ) then   # because of this function we need the unit, not the unitId
            _UnitRestricted_cache[unitId] = true
            break
        end
    end

    return _UnitRestricted_cache[unitId]
end


function WeaponRestricted(weaponLabel)
    # tells you whether a weapon should be disabled (according to the unit restrictions)

    if not CheckUnitRestrictionsEnabled() then     # if no restrictions defined then dont bother
        return false
    end
    CacheRestrictedUnitLists()
    return SpecialWepRestricted[weaponLabel]
end


function NukesRestricted()
    return WeaponRestricted('StrategicMissile')
end


function TacticalMissilesRestricted()
    return WeaponRestricted('TacticalMissile')
end


# -------------------------------------------------------------------------------------------------------------
# HELPER FUNCTIONS

function CheckUnitRestrictionsEnabled()
    # tells you whether unit restrictions are enabled
    if ScenarioInfo.Options.RestrictedCategories then return true end
    return false
end

function CacheRestrictedUnitLists()
    # create tables of restricted units and special weapons. Only need to run once per game

    # check if we need to do this function at all
    if type(UnitCatRestricted) == 'table' then
        return
    end

    SpecialWepRestricted = {}
    UnitCatRestricted = {}
    local restrictedUnits = import('/lua/ui/lobby/restrictedUnitsData.lua').restrictedUnits
    local c

    # loops through enabled restrictions
    for k, restriction in ScenarioInfo.Options.RestrictedCategories do 

        # create a list of all unit category restrictions. TO be clear, this results in a table of categories
        # So, for example:   { categories.TECH1, categories.TECH2, categories.MASSFAB }
        if restrictedUnits[restriction].categories then
            for l, cat in restrictedUnits[restriction].categories do
                c = cat
                if type(c) == 'string' then c = ParseEntityCategory(c) end
                table.insert( UnitCatRestricted, c )
            end
        end

        # create a list of restricted special weapons (nukes, tactical missiles)
        if restrictedUnits[restriction].specialweapons then   
            for l, cat in restrictedUnits[restriction].specialweapons do

                # strategic missiles
                if cat == 'StrategicMissile' or cat == 'strategicmissile' or cat == 'sm' or cat == 'SM' then
                    SpecialWepRestricted['StrategicMissile'] = true

                # tactical missiles
                elseif cat == 'TacticalMissile' or cat == 'tacticalmissile' or cat == 'tm' or cat == 'TM' then
                    SpecialWepRestricted['TacticalMissile'] = true

                # mod added weapons
                else
                    SpecialWepRestricted[cat] = true
                end
            end
        end
    end
end

# -------------------------------------------------------------------------------------------------------------
