// EffectsGroups in this definition are commented with a keyword for reference:
// If using the stackinggroup SHIP_REPAIR, note the priority of ZERO-POP
// ZERO-POP  :  Prevents any later priority repairs or other sitreps when
//                planet has no population (outpost).
// COMPLETE  :  Split from partial only for a different sitrep.
// PARTIAL   :  Partial repair of a ship, amount of the actual repair depends on
//                planet happiness and the ship's max structure.
// FALLBACK  :  No repair due to planet happiness
BuildingType
    name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
    description = "BLD_SHIPYARD_ORBITAL_DRYDOCK_DESC"
    buildcost = 20 * [[BUILDING_COST_MULTIPLIER]]
    buildtime = 5
    tags = "ORBITAL"
    location = And [
        Planet
        OwnedBy empire = Source.Owner
        Not Contains Building name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
        [[LOCATION_ALLOW_ENQUEUE_IF_PREREQ_ENQUEUED(BLD_SHIPYARD_BASE)]]
    ]
    enqueuelocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        [[SPECIES_LIKES_OR_DISLIKES_BUILDING_STABILITY_EFFECTS]]

        // ZERO-POP : With no pop, no repair or sitrep messages
        EffectsGroup
            scope = And [
                Ship
                InSystem id = Source.SystemID
                Or [
                    OwnedBy empire = Source.Owner
                    And [
                        // either ally providing repair or owner of ship beign repaired must adopt policy to share repairs
                        Or [
                            EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_ALLIED_REPAIR"
                            EmpireHasAdoptedPolicy empire = LocalCandidate.Owner name = "PLC_ALLIED_REPAIR"
                        ]
                        OwnedBy affiliation = AllyOf empire = Source.Owner
                    ]
                ]
                Structure high = LocalCandidate.MaxStructure - 0.001
            ]
            activation = ContainedBy And [
                Object id = Source.PlanetID
                [[PLANET_OWNED_DRYDOCK_HIGHEST_HAPPINESS]]
                Population high = 0
            ]
            stackinggroup = "SHIP_REPAIR"
            priority = 75
            effects = [
                GenerateSitRepMessage
                    message = "EFFECT_DRYDOCK_SHIP_REPAIR_NOPOP"
                    label = "SITREP_SHIP_REPAIR_DOCK_NOPOP"
                    icon = "icons/sitrep/ship-repair.png"
                    parameters = [
                        tag = "ship" data = Target.ID
                        tag = "building" data = Source.ID
                        tag = "planet" data = Source.PlanetID
                    ]
                    empire = Source.Owner
            ]
        // COMPLETE : Normal repair, completely restored
        EffectsGroup
            scope = And [
                Ship
                InSystem id = Source.SystemID
                Or [
                    OwnedBy empire = Source.Owner
                    And [
                        // either ally providing repair or owner of ship beign repaired must adopt policy to share repairs
                        Or [
                            EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_ALLIED_REPAIR"
                            EmpireHasAdoptedPolicy empire = LocalCandidate.Owner name = "PLC_ALLIED_REPAIR"
                        ]
                        OwnedBy affiliation = AllyOf empire = Source.Owner
                    ]
                ]
                Structure low = LocalCandidate.MaxStructure - 
                    [[ORB_DRYDOCK_REPAIR_VAL(LocalCandidate.MaxStructure)]] - 0.001
                    high = LocalCandidate.MaxStructure - 0.001
                Turn low = LocalCandidate.ArrivedOnTurn + 1
                (Source.Planet.Happiness >= [[CONST_ORB_DRYDOCK_MIN_HAPPY]])
            ]
            activation = And [
                ContainedBy [[PLANET_OWNED_DRYDOCK_HIGHEST_HAPPINESS]]
                Turn low = Source.System.LastTurnBattleHere + 1
            ]
            stackinggroup = "SHIP_REPAIR"
            priority = 80  // not macroed to prevent unwanted changes
            effects = [
                SetStructure value = Target.MaxStructure
                GenerateSitRepMessage
                    message = "EFFECT_DRYDOCK_SHIP_REPAIR_COMPLETE"
                    label = "SITREP_SHIP_REPAIR_DOCK_COMPLETE"
                    icon = "icons/sitrep/ship-repair-complete.png"
                    parameters = [
                        tag = "ship" data = Target.ID
                        tag = "building" data = Source.ID
                        tag = "planet" data = Source.PlanetID
                    ]
                    empire = Source.Owner
            ]
        // PARTIAL : Normal repair
        EffectsGroup
            scope = And [
                Ship
                InSystem id = Source.SystemID
                Or [
                    OwnedBy empire = Source.Owner
                    And [
                        // either ally providing repair or owner of ship beign repaired must adopt policy to share repairs
                        Or [
                            EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_ALLIED_REPAIR"
                            EmpireHasAdoptedPolicy empire = LocalCandidate.Owner name = "PLC_ALLIED_REPAIR"
                        ]
                        OwnedBy affiliation = AllyOf empire = Source.Owner
                    ]
                ]
                Structure high = LocalCandidate.MaxStructure -
                    [[ORB_DRYDOCK_REPAIR_VAL(LocalCandidate.MaxStructure)]]
                Turn low = LocalCandidate.ArrivedOnTurn + 1
                (Source.Planet.Happiness >= [[CONST_ORB_DRYDOCK_MIN_HAPPY]])
            ]
            activation = And [
                ContainedBy [[PLANET_OWNED_DRYDOCK_HIGHEST_HAPPINESS]]
                Turn low = Source.System.LastTurnBattleHere + 1
            ]
            stackinggroup = "SHIP_REPAIR"
            priority = 80  // not macroed to prevent unwanted changes
            effects = [
                SetStructure value = Value + [[ORB_DRYDOCK_REPAIR_VAL(Target.MaxStructure)]]
                GenerateSitRepMessage
                    message = "EFFECT_DRYDOCK_SHIP_REPAIR_PARTIAL"
                    label = "SITREP_SHIP_REPAIR_DOCK_PARTIAL"
                    icon = "icons/sitrep/ship-repair.png"
                    parameters = [
                        tag = "ship" data = Target.ID
                        tag = "building" data = Source.ID
                        tag = "planet" data = Source.PlanetID
                    ]
                    empire = Source.Owner
            ]
        // FALLBACK : No repair
        EffectsGroup
            scope = And [
                Ship
                InSystem id = Source.SystemID
                Or [
                    OwnedBy empire = Source.Owner
                    And [
                        // either ally providing repair or owner of ship beign repaired must adopt policy to share repairs
                        Or [
                            EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_ALLIED_REPAIR"
                            EmpireHasAdoptedPolicy empire = LocalCandidate.Owner name = "PLC_ALLIED_REPAIR"
                        ]
                        OwnedBy affiliation = AllyOf empire = Source.Owner
                    ]
                ]
                Structure high = LocalCandidate.MaxStructure - 0.001
                Turn low = LocalCandidate.ArrivedOnTurn + 1
                (Source.Planet.Happiness < [[CONST_ORB_DRYDOCK_MIN_HAPPY]])
            ]
            activation = And [
                ContainedBy [[PLANET_OWNED_DRYDOCK_HIGHEST_HAPPINESS]]
                Turn low = Source.System.LastTurnBattleHere + 1
            ]
            stackinggroup = "SHIP_REPAIR"
            priority = 120  // not macroed to prevent unwanted changes
            effects = [
                GenerateSitRepMessage
                    message = "EFFECT_DRYDOCK_SHIP_REPAIR_NONE"
                    label = "SITREP_SHIP_REPAIR_DOCK_NONE"
                    icon = "icons/sitrep/ship-repair.png"
                    parameters = [
                        tag = "ship" data = Target.ID
                        tag = "building" data = Source.ID
                        tag = "planet" data = Source.PlanetID
                    ]
                    empire = Source.Owner
            ]
    ]
    icon = "icons/building/shipyard-1.png"

// No repairs below this happiness
CONST_ORB_DRYDOCK_MIN_HAPPY
'''5'''

// Max repair at this happiness
CONST_ORB_DRYDOCK_TARGET_HAPPY
'''15.0'''

// Minimum repair per turn, at target happiness
CONST_ORB_DRYDOCK_MIN_REPAIR
'''30.0 * [[SHIP_STRUCTURE_FACTOR]]'''

// Max repair rate, based on MaxStructure
CONST_ORB_DRYDOCK_TARGET_REPAIR
'''0.25'''

// Happiness rate formula
ORB_DRYDOCK_HAPPY_RATE
'''min(Source.Planet.Happiness / [[CONST_ORB_DRYDOCK_TARGET_HAPPY]], 1.0)'''

// Repair rate formula
// arg1 target max structure
ORB_DRYDOCK_REPAIR_RATE
'''max([[CONST_ORB_DRYDOCK_MIN_REPAIR]], @1@ * [[CONST_ORB_DRYDOCK_TARGET_REPAIR]])'''

// Formula for amount of structure to repair
// arg1 target max structure
ORB_DRYDOCK_REPAIR_VAL
'''[[ORB_DRYDOCK_HAPPY_RATE]] * [[ORB_DRYDOCK_REPAIR_RATE(@1@)]]'''

// Planet in system with highest happiness that is owned and has a drydock
PLANET_OWNED_DRYDOCK_HIGHEST_HAPPINESS
'''MaximumNumberOf number = 1 sortkey = LocalCandidate.Happiness condition = And [
    InSystem id = Source.SystemID
    Planet
    Or [
        OwnedBy empire = Source.Owner
        And [
            // either ally providing repair or owner of ship beign repaired must adopt policy to share repairs
            Or [
                EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_ALLIED_REPAIR"
                EmpireHasAdoptedPolicy empire = LocalCandidate.Owner name = "PLC_ALLIED_REPAIR"
            ]
            OwnedBy affiliation = AllyOf empire = Source.Owner
        ]
    ]
    Contains
        Building name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
]
'''

#include "/scripting/macros/enqueue.macros"
#include "/scripting/macros/priorities.macros"
#include "/scripting/macros/base_prod.macros"
#include "/scripting/macros/misc.macros"
#include "/scripting/buildings/buildings.macros"