// 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
        Not Contains Building name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        // ZERO-POP : With no pop, no repair or sitrep messages
        EffectsGroup
            scope = And [
                Ship
                InSystem id = Source.SystemID
                OwnedBy empire = Source.Owner
                Structure high = LocalCandidate.MaxStructure - 0.001
            ]
            activation = And [
                Source
                ContainedBy And [
                    [[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
                OwnedBy 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
                OwnedBy 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
                OwnedBy 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'''

// 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 [
    Planet
    InSystem id = Source.SystemID
    OwnedBy empire = Source.Owner
    Contains
        Building name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
]
'''

#include "/scripting/common/enqueue.macros"
#include "/scripting/common/priorities.macros"
#include "/scripting/common/base_prod.macros"
