BuildingType
    name = "BLD_EVACUATION"
    description = "BLD_EVACUATION_DESC"
    buildcost = 10 * [[BUILDING_COST_MULTIPLIER]]
    buildtime = 1
    location = And [
        Planet
        OwnedBy empire = Source.Owner
        Not Contains Building name = "BLD_EVACUATION"
        Not Contains Building name = "BLD_CONC_CAMP"
        Not HasSpecial name = "CONC_CAMP_SLAVE_SPECIAL"
        Not Enqueued type = Building name = "BLD_CONC_CAMP"
        Species
    ]
    enqueuelocation = And [
        Planet
        OwnedBy empire = Source.Owner
        Not Contains Building name = "BLD_EVACUATION"
        Not Contains Building name = "BLD_CONC_CAMP"
        Not HasSpecial name = "CONC_CAMP_SLAVE_SPECIAL"
        Not Enqueued type = Building name = "BLD_EVACUATION"
        Not Enqueued type = Building name = "BLD_CONC_CAMP"
        Species
    ]
    effectsgroups = [
        [[SPECIES_LIKES_OR_DISLIKES_BUILDING_STABILITY_EFFECTS]]
    
        EffectsGroup            // add population to connected plants with room and suitable species
            scope = NumberOf number = 1 condition = And [
                Planet
                Species name = Source.Planet.Species
                Not Object id = Source.PlanetID
                ResourceSupplyConnected empire = Source.Owner condition = Source
                Population low = 1 high = LocalCandidate.TargetPopulation - 1
                Not Contains Building name = "BLD_EVACUATION"
                Not Contains Building name = "BLD_CONC_CAMP"
            ]
            activation = Number low = 1 condition = And [
                Planet
                Species name = Source.Planet.Species
                Not Object id = Source.PlanetID
                ResourceSupplyConnected empire = Source.Owner condition = Source
                Population low = 1 high = LocalCandidate.TargetPopulation - 1
            ]
            priority = [[POPULATION_DEFAULT_PRIORITY]]
            effects = [
                SetPopulation value = Value + min(Source.Planet.Population, 2)
                GenerateSitRepMessage
                    message = "EFFECT_EVACUEES"
                    label = "EFFECT_EVACUEES_LABEL"
                    icon = "icons/building/evacuation.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
            ]

        EffectsGroup            // remove population from location & nullify production
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = ContainedBy And [
                Object id = Source.PlanetID
                OwnedBy empire = Source.Owner
                // evacuation building needs to exist one turn for technical reasons.
                // it should not destroy a new colony
                (LocalCandidate.TurnsSinceColonization > 0)
            ]
            priority = [[CONCENTRATION_CAMP_PRIORITY]]
            effects = [
                SetPopulation value = min(Value, Target.Population - 2)
                SetIndustry value = 0
                SetTargetIndustry value = 0
                SetResearch value = 0
                SetTargetResearch value = 0
                SetInfluence value = 0
                SetTargetInfluence value = 0
            ]

        EffectsGroup            // remove evacuation when planet is depopulated or no longer owned by empire that produced this building
            scope = Source
            activation = Or [
                Not OwnedBy empire = Source.Owner
                ContainedBy Or [
                    And [
                        Object id = Source.PlanetID
                        Or [
                            Population high = 0
                            // evacuation building needs to exist one turn for technical reasons.
                            // it should not destroy a new colony
                            (LocalCandidate.TurnsSinceColonization < 1)
                        ]
                    ]
                    Contains Building name = "BLD_CONC_CAMP"
                    HasSpecial name = "CONC_CAMP_SLAVE_SPECIAL"
                ]
            ]
            priority = [[END_CLEANUP_PRIORITY]]
            effects = Destroy
    ]
    icon = "icons/building/evacuation.png"

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