Policy
    name = "PLC_FLANKING"
    description = "PLC_FLANKING_DESC"
    short_description = "PLC_FLANKING_SHORT_DESC"
    category = "MILITARY_CATEGORY"
    adoptioncost = 5 + [[SHIPS_OWNED_BY_EMPIRE]]
    effectsgroups = [
        [[SPECIES_LIKES_OR_DISLIKES_POLICY_STABILITY_EFFECTS]]

        // Two effects: Stationary ships bonus, Multi starlane attack bonus
        //  Stationary ships bonus:
        // if, on the previous turn, the empire that adopted this policy had more ships in the system
        // than there were enemy ships in the system, give them a bonus. ships arriving on the turn of
        // a battle do not receive or contribute to this effect.
        //
        // Multi starlane attack bonus:
        // if, on the previous turn ships were coming in from different starlanes, give them a bonus
        // Note: the system must be set as the final destination
        EffectsGroup
            scope = And [
                Ship
                OwnedBy empire = Source.Owner
                Armed
                 Or [
                   // Stationary ships bonus (prepared defensive flanking)
                   And [
                       InSystem
                       Stationary
                       ((
                           Statistic Count condition = And [
                               Ship
                               InSystem id = RootCandidate.SystemID
                               OwnedBy empire = Source.Owner
                               Stationary
                               Armed
                           ]
                        ) > max( 1, // no bonus if there is only a single defender
                           Statistic Count condition = And [
                               Ship
                               InSystem id = RootCandidate.SystemID
                               OwnedBy affiliation = EnemyOf empire = Source.Owner
                           ]
                        )
                       )
                   ]
                   And [
                       (LocalCandidate.Fleet.ETA = 1)

                       // Multi starlane attack bonus (pincer attack)
                       (( Statistic CountUnique
                          value = LocalCandidate.PreviousToFinalDestinationID
                          condition = And [
                              Fleet
                              OwnedBy empire = RootCandidate.Owner
                              (LocalCandidate.FinalDestinationID = RootCandidate.Fleet.FinalDestinationID)
                              (LocalCandidate.ETA = 1)
                              Contains condition = And [ Ship Armed ]
                          ]
                    ) > 1
                   )
                   ]
                   
                ]
            ]
            priority = [[TARGET_AFTER_SCALING_PRIORITY]]
            effects = [
                // Boost damage for ship weapons, strikers, *bombers. Arc disruptor instead increases shots.
                SetMaxDamage partname = "SR_WEAPON_1_1" value = Value + (NamedReal name = "PLC_FLANKING_DAMAGE_BOOST" value = 1.0 * [[SHIP_WEAPON_DAMAGE_FACTOR]] )
                SetMaxDamage partname = "SR_WEAPON_2_1" value = Value + NamedRealLookup name = "PLC_FLANKING_DAMAGE_BOOST"
                SetMaxDamage partname = "SR_WEAPON_3_1" value = Value + NamedRealLookup name = "PLC_FLANKING_DAMAGE_BOOST"
                SetMaxDamage partname = "SR_WEAPON_4_1" value = Value + NamedRealLookup name = "PLC_FLANKING_DAMAGE_BOOST"
                SetMaxSecondaryStat partname = "SR_ARC_DISRUPTOR" value = Value + NamedReal name = "PLC_FLANKING_ARC_DISRUPTOR_SHOT_BONUS" value = 1.0
                SetMaxSecondaryStat partname = "FT_HANGAR_2" value = Value + (NamedReal name = "PLC_FLANKING_FIGHTER_DAMAGE_BOOST" value = 2.0 * [[FIGHTER_DAMAGE_FACTOR]] )
                SetMaxSecondaryStat partname = "FT_HANGAR_3" value = Value + NamedRealLookup name = "PLC_FLANKING_FIGHTER_DAMAGE_BOOST"
                SetMaxSecondaryStat partname = "FT_HANGAR_4" value = Value + NamedRealLookup name = "PLC_FLANKING_FIGHTER_DAMAGE_BOOST"
                // Boost interceptor launch rate by 2
                SetMaxCapacity partname = "FT_BAY_1" value = Value
                + (  (Statistic If condition = And [ Object id = Target.ID  DesignHasPart low = 1 high = 999 name = "FT_HANGAR_1" ] )
                   * (NamedReal name = "PLC_FLANKING_BAY_LAUNCH_BONUS" value = 2.0 ) )
                // TODO check monster weapons
                // no bonus to point defense
            ]

    ]
    graphic = "icons/policies/military_flanking.png"

#include "/scripting/policies/policies.macros"
#include "/scripting/macros/priorities.macros"
#include "/scripting/macros/misc.macros"
