# New functionality: determine what projectiles to reflect in blueprint.
#
# BP example (taken from Loyalist)
#
#     Defense = {
#         AntiMissile = {
#             AttachBone = 'Turret_Muzzle',
#             ProjectileCategories = {
#                 'MISSILE -STRATEGIC',
#             },
#             Radius = 5,
#             RedirectRateOfFire = 1,
#         },
#     },

do


local CBFP_oldMissileRedirect = MissileRedirect
MissileRedirect = Class(CBFP_oldMissileRedirect) {

    OnCreate = function(self, spec)
        CBFP_oldMissileRedirect.OnCreate(self, spec)

        # rest of the code is for loyalist tactical missile deflection fix   [161]
        local bp = self:GetBlueprint()
        local ProjectileCategories = bp.ProjectileCategories or { 'MISSILE -STRATEGIC' }
        local ParsedProjectileCategories = {}

        for k, category in ProjectileCategories do
            if type(category) == 'string' then
                table.insert(ParsedProjectileCategories, ParseEntityCategory(category) )
            else
                table.insert(ParsedProjectileCategories, category)
            end
        end

        self.ProjectileCategories = ParsedProjectileCategories
    end,

    GetBlueprint = function(self)
        return self.Owner:GetBlueprint().Defense.AntiMissile
    end,

    WaitingState = State (CBFP_oldMissileRedirect.WaitingState) {
        OnCollisionCheck = function(self, other)

            if other != self.EnemyProj and IsEnemy( self:GetArmy(), other:GetArmy() ) then

                # check if we can touch the projectile  [161]
                local match = false
                for k, cat in self.ProjectileCategories do
                    if EntityCategoryContains(cat, other) then
                        match = true
                        break
                    end
                end
                if not match then
                    return false
                end

                # ok we can touch the projectile

                self.Enemy = other:GetLauncher()
                self.EnemyProj = other
                if self.Enemy then
                    other:SetNewTarget(self.Enemy)
                    other:TrackTarget(true)
                    other:SetTurnRate(720)
                end
                ChangeState(self, self.RedirectingState)
            end
            return false
        end,
    },

}

end