// the includes have been moved to the end of the file to minimize discrepancies in
// the line number reported whenever there is a parsing error

BuildingType
    name = "BLD_CULTURE_ARCHIVES"
    description = "BLD_CULTURE_ARCHIVES_DESC"
    buildcost = 200
    buildtime = 1
    tags = "ANTIQUATED"
    location = Described description = "NOWHERE" condition = Not All
    effectsgroups = [
/*  // testing priority
    // Research should see a x2 bonus, then a +5 bonus.
    // Industry should see a +5 bonus, then a x2 bonus.
        EffectsGroup
            scope = Object id = Source.PlanetID
            accountinglabel = "research_plus_5"
            effects = SetTargetResearch value = Value + 5
        EffectsGroup
            scope = Object id = Source.PlanetID
            accountinglabel = "research_times_2"
            priority = [[EARLY_PRIORITY]]
            effects = SetTargetResearch value = Value * 2
        EffectsGroup
            scope = Object id = Source.PlanetID
            accountinglabel = "industry_plus_5"
            priority = [[DEFAULT_PRIORITY]]
            effects = SetTargetIndustry value = Value + 5
        EffectsGroup
            scope = Object id = Source.PlanetID
            accountinglabel = "industry_times_2"
            priority = [[LATE_PRIORITY]]
            effects = SetTargetIndustry value = Value * 2
*/
/*  // testing effects
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.Owner
            ]
            effects = SetEmpireTechProgress
                name = Source.OwnerTopPriorityEnqueuedTech
                progress = Value + 10

        EffectsGroup
            scope = And [
                Planet
                OwnedBy empire = Source.Owner
                ValueTest
                    high = 5
                    value = LocalCandidate.Population
            ]
            effects = SetTargetPopulation value = Value + 100

        EffectsGroup
            scope = Object id = Source.PlanetID
            effects = SetTargetConstruction 99
*/

        EffectsGroup          // These bonuses given to anyone
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.Owner
            ]
            effects = [
                SetTargetResearch value = Value + 5
                SetTargetIndustry value = Value + Target.Population / 2
            ]
            
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.Owner
                Not HasSpecial name = "HEAD_ON_A_SPIKE_SPECIAL"
            ]
            activation = Not ProducedByEmpire empire = Source.Owner                
            effects = [
                AddSpecial name = "HEAD_ON_A_SPIKE_SPECIAL"
                GenerateSitRepMessage
                    message = "HEAD_ON_A_SPIKE_MESSAGE"
                    icon = "icons/sitrep/empire_eliminated.png"
                    parameters = [
                        tag = "empire" data = Source.ProducedByEmpireID                        
                    ]
            ]
    ]
    icon = "icons/building/archive.png"

BuildingType
    name = "BLD_CULTURE_LIBRARY"
    description = "BLD_CULTURE_LIBRARY_DESC"
    buildcost = 200
    buildtime = 1
    tags = "ANTIQUATED"
    location = Not All
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = SetTargetResearch value = Value + 5

        EffectsGroup    // Destroy cultural library when the species is no longer present
            scope = Source
            activation = ContainedBy And [
                Object id = Source.PlanetID
                Population high = 0
            ]
            effects = Destroy
    ]
    icon = "icons/building/archive.png"

BuildingType
    name = "BLD_IMPERIAL_PALACE"
    description = "BLD_IMPERIAL_PALACE_DESC"
    buildcost = 10
    buildtime = 8
    captureresult = destroy
    location = And [
        Planet
        OwnedBy empire = Source.Owner
        Not Contains Building name = "BLD_IMPERIAL_PALACE"
        TargetPopulation low = 1
        Number low = 0 high = 0 condition = And [
            Building name = "BLD_IMPERIAL_PALACE"
            OwnedBy empire = Source.Owner
            ProducedByEmpire empire = Source.Owner
        ]
    ]
    enqueuelocation = And [
        // must own production location planet
        Planet
        OwnedBy empire = Source.Owner
        
        // can't build where another palace exists (even if not owned by this empire)
        Not Contains Building name = "BLD_IMPERIAL_PALACE"
        
        // must have a non-trivial population
        TargetPopulation low = 1
        
        // can't enqueue if already own a self-built palace
        Number low = 0 high = 0 condition = And [
            Building name = "BLD_IMPERIAL_PALACE"
            OwnedBy empire = Source.Owner
            ProducedByEmpire empire = Source.Owner
        ]
        
        // can't enqueue if already have an enqueued palace anywhere
        Number low = 0 high = 0 condition = And [
            Planet
            Enqueued
                type = Building
                name = "BLD_IMPERIAL_PALACE"
                empire = Source.Owner
                low = 1
        ]
    ]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.ProducedByEmpireID
            ]
            effects = SetEmpireCapital

        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.ProducedByEmpireID
            ]
            stackinggroup = "IMPERIAL_PALACE_SUPPLY_EFFECT"
            effects = SetMaxSupply value = Value + 2

        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.ProducedByEmpireID
            ]
            activation = Not Number low = 1 condition = And [
                Building name = "BLD_MEGALITH"
                OwnedBy empire = Source.Owner
            ]
            stackinggroup = "IMPERIAL_PALACE_EFFECT"
            effects =
                SetTargetConstruction value = Value + 20

        EffectsGroup
            scope = And [
                Planet
                OwnedBy empire = Source.Owner
                Species name = Source.Planet.Species
            ]
            activation = And [
                OwnedBy empire = Source.ProducedByEmpireID
                ContainedBy And [
                    Object id = Source.PlanetID
                    Planet
                    Species
                ]
            ]
            stackinggroup = "IMPERIAL_PALACE_HAPPINESS"
            effects =
                SetTargetHappiness value = Value + 5 // min(10, max(0, 10 - Target.DistanceToSource/20))
        ]
    icon = "icons/building/palace.png"

/* // commented out because current form doesn't work with current detection mechanics, which have a hard limit on range that can't be exceeded
BuildingType
    name = "BLD_OBSERVATORY"
    description = "BLD_OBSERVATORY_DESC"
    buildcost = 60
    buildtime = 3
    location = AND [
        Planet
        OwnedBy empire = Source.Owner
        Not Contains Building name = "BLD_OBSERVATORY"
    ]
    effectsgroups = [
        EffectsGroup
            scope = NumberOf number = 1 condition = System
            effects = SetStealth value = Value - 1
            
        EffectsGroup
            scope = And [ 
                Planet 
                Object id = Source.PlanetID
            ]
            effects = SetTargetConstruction value = Value - 10
    ]
    icon = "icons/building/lighthouse.png"
*/

BuildingType
    name = "BLD_SHIPYARD_BASE"
    description = "BLD_SHIPYARD_BASE_DESC"
    buildcost = 10
    buildtime = 4
    tags = "ORBITAL"
    location = And [
        Planet
        TargetPopulation low = 1
        Not Contains Building name = "BLD_SHIPYARD_BASE"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups =
        EffectsGroup
            scope = And [ 
                Object id = Source.PlanetID
                Planet
            ]
            effects = SetTargetConstruction value = Value - 10
    icon = "icons/building/shipyard.png"

BuildingType
    name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
    description = "BLD_SHIPYARD_ORBITAL_DRYDOCK_DESC"
    buildcost = 20
    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 = [
        EffectsGroup
            scope = And [
                Ship
                InSystem id = Source.SystemID
                OwnedBy empire = Source.Owner
                Structure high = LocalCandidate.MaxStructure - 0.001
            ]
            activation = Turn low = Source.System.LastTurnBattleHere + 1
            effects = [
                //SetStructure value = Value + 10
                SetStructure value = Target.MaxStructure
                GenerateSitRepMessage
                    message = "EFFECT_DRYDOCK_SHIP_REPAIR"
                    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
            ]
            
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = SetTargetConstruction value = Value - 15
        ]
    icon = "icons/building/shipyard-1.png"

BuildingType
    name = "BLD_SHIPYARD_CON_NANOROBO"
    description = "BLD_SHIPYARD_CON_NANOROBO_DESC"
    buildcost = 250
    buildtime = 5
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_CON_NANOROBO"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        Contains And [
            Building name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-2.png"

BuildingType
    name = "BLD_SHIPYARD_CON_GEOINT"
    description = "BLD_SHIPYARD_CON_GEOINT_DESC"
    buildcost = 750
    buildtime = 5
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_CON_GEOINT"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        Contains And [
            Building name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-3.png"

BuildingType
    name = "BLD_SHIPYARD_CON_ADV_ENGINE"
    description = "BLD_SHIPYARD_CON_ADV_ENGINE_DESC"
    buildcost = 500
    buildtime = 5
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_CON_ADV_ENGINE"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        Contains And [
            Building name = "BLD_SHIPYARD_ORBITAL_DRYDOCK"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-4.png"

BuildingType
    name = "BLD_SHIPYARD_AST"
    description = "BLD_SHIPYARD_AST_DESC"
    buildcost = 75
    buildtime = 5
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_AST"
        Planet type = Asteroids
        OwnedBy empire = Source.Owner
        
        /* TODO: Check that one, seems to be weird */
        Not Contains And [
            Building name = "BLD_SHIPYARD_AST"
            OwnedBy empire = Source.Owner
        ]
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-5.png"

BuildingType
    name = "BLD_SHIPYARD_AST_REF"
    description = "BLD_SHIPYARD_AST_REF_DESC"
    buildcost = 500
    buildtime = 5
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_AST_REF"
        Planet type = Asteroids
        OwnedBy empire = Source.Owner
        Contains And [
            Building name = "BLD_SHIPYARD_AST"
            OwnedBy empire = Source.Owner
        ]
        Not Contains And [
            Building name = "BLD_SHIPYARD_AST_REF"
            OwnedBy empire = Source.Owner
        ]
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-6.png"

BuildingType
    name = "BLD_SHIPYARD_ORG_ORB_INC"
    description = "BLD_SHIPYARD_ORG_ORB_INC_DESC"
    buildcost = 40
    buildtime = 8
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_ORG_ORB_INC"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-7.png"

BuildingType
    name = "BLD_SHIPYARD_ORG_CELL_GRO_CHAMB"
    description = "BLD_SHIPYARD_ORG_CELL_GRO_CHAMB_DESC"
    buildcost = 64
    buildtime = 8
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_ORG_CELL_GRO_CHAMB"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        Contains And [
            Building name = "BLD_SHIPYARD_ORG_ORB_INC"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-8.png"

BuildingType
    name = "BLD_SHIPYARD_ORG_XENO_FAC"
    description = "BLD_SHIPYARD_ORG_XENO_FAC_DESC"
    buildcost = 120
    buildtime = 8
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_ORG_XENO_FAC"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        Contains And [
            Building name = "BLD_SHIPYARD_ORG_ORB_INC"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-9.png"

BuildingType
    name = "BLD_SHIPYARD_ENRG_COMP"
    description = "BLD_SHIPYARD_ENRG_COMP_DESC"
    buildcost = 200
    buildtime = 5
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_ENRG_COMP"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-10.png"

BuildingType
    name = "BLD_SHIPYARD_ENRG_SOLAR"
    description = "BLD_SHIPYARD_ENRG_SOLAR_DESC"
    buildcost = 1200
    buildtime = 5
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_SHIPYARD_ENRG_SOLAR"
        Contains And [
            Building name = "BLD_SHIPYARD_ENRG_COMP"
            OwnedBy empire = Source.Owner
        ]
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-16.png"

BuildingType
    name = "BLD_BIOTERROR_PROJECTOR"
    description = "BLD_BIOTERROR_PROJECTOR_DESC"
    buildcost = 75
    buildtime = 15
    location = And [
        Planet
        Not Contains Building name = "BLD_BIOTERROR_PROJECTOR"
        OwnedBy empire = Source.Owner
        HasSpecial name = "RESONANT_MOON_SPECIAL"
        Not Contains Or [
            Building name = "BLD_CLONING_CENTER"
            Building name = "BLD_BIOTERROR_PROJECTOR"
        ]
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/bioterror_projector.png"

BuildingType
    name = "BLD_LIGHTHOUSE"
    description = "BLD_LIGHTHOUSE_DESC"
    buildcost = 25
    buildtime = 10
    location = AND [
        Planet
        Not Contains Building name = "BLD_LIGHTHOUSE"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = WithinDistance distance = 0.00001 condition = Source
            stackinggroup = "LIGHTHOUSE_STEALTH_STACK"
            effects = SetStealth value = Value - 30

        EffectsGroup
            scope = And [
                Ship
                OwnedBy empire = Source.Owner
                WithinDistance distance = 50 condition = Source
                Speed low = 1 // Immobile objects do not get the starlane speed boost.
            ]
            stackinggroup = "LIGHTHOUSE_SPEED_STACK"
            effects = SetSpeed value = Value + 20
    ]
    icon = "icons/building/lighthouse.png"

BuildingType
    name = "BLD_SCANNING_FACILITY"
    description = "BLD_SCANNING_FACILITY_DESC"
    buildcost = 25
    buildtime = 5
    location = AND [
        Planet
        Not Contains Building name = "BLD_SCANNING_FACILITY"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = SetDetection value = Value + 75
    ]
    icon = "icons/building/lighthouse.png"

BuildingType
    name = "BLD_HEAVY_MINE_PROCESSOR"
    description = "BLD_HEAVY_MINE_PROCESSOR_DESC"
    buildcost = 75
    buildtime = 1
    location = And [
        Planet
        Not Contains Building name = "BLD_HEAVY_MINE_PROCESSOR"
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/heavy_mining_processor.png"

BuildingType
    name = "BLD_INDUSTRY_CENTER"
    description = "BLD_INDUSTRY_CENTER_DESC"
    buildcost = 75
    buildtime = 5
    location = And [
        Planet
        Not Contains Building name = "BLD_INDUSTRY_CENTER"
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
                Focus type = "FOCUS_INDUSTRY"
            ]
            activation = And [
                OwnerHasTech name = "PRO_INDUSTRY_CENTER_I"
                Not OwnerHasTech name = "PRO_INDUSTRY_CENTER_II"
            ]
            stackinggroup = "INDUSTRY_CENTER_STACK"
            effects = SetTargetIndustry value = Value + Target.Population * 1.0 * [[INDUSTRY_PER_POP]]

        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
                Focus type = "FOCUS_INDUSTRY"
            ]
            activation = And [
                OwnerHasTech name = "PRO_INDUSTRY_CENTER_II"
                Not OwnerHasTech name = "PRO_INDUSTRY_CENTER_III"
            ]
            stackinggroup = "INDUSTRY_CENTER_STACK"
            effects = SetTargetIndustry value = Value + Target.Population * 2 * [[INDUSTRY_PER_POP]]

        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
                Focus type = "FOCUS_INDUSTRY"
            ]
            activation = OwnerHasTech name = "PRO_INDUSTRY_CENTER_III"
            stackinggroup = "INDUSTRY_CENTER_STACK"
            effects = SetTargetIndustry value = Value + Target.Population * 3 * [[INDUSTRY_PER_POP]]
    ]
    icon = "icons/tech/industrial_centre_ii.png"

BuildingType
    name = "BLD_MEGALITH"
    description = "BLD_MEGALITH_DESC"
    buildcost = 250
    buildtime = 10
    location = And [
        Planet
        Contains Building name = "BLD_IMPERIAL_PALACE"
        Not Contains Building name = "BLD_MEGALITH"
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = Source
            stackinggroup = "BLD_MEGALITH_EFFECT"
            effects = [
                SetTargetConstruction value = Value + 30
                SetEmpireCapital
                SetIndustry value = Target.TargetIndustry
                SetTrade value = Target.TargetTrade
                SetResearch value = Target.TargetResearch
            ]

        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                TargetPopulation low = 1
            ]
            activation = Source
            stackinggroup = "BLD_MEGALITH_EFFECT"
            effects = SetMaxSupply value = Value + 1
    ]
    icon = "icons/building/megalith.png"

BuildingType
    name = "BLD_COLLECTIVE_NET"
    description = "BLD_COLLECTIVE_NET_DESC"
    buildcost = 250
    buildtime = 10
    location = And [
        Planet
        Not Contains Building name = "BLD_COLLECTIVE_NET"
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                Focus type = "FOCUS_INDUSTRY"
            ]
            activation = Not WithinDistance distance = 200 condition = And [
                Ship
                Not Stationary
            ]
            stackinggroup = "BLD_COLLECTIVE_NET_INDUSTRY_EFFECT"
            effects = SetTargetIndustry value = Value + Target.Population * 2.5 * [[INDUSTRY_PER_POP]]

        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                Focus type = "FOCUS_RESEARCH"
            ]
            activation = Not WithinDistance distance = 200 condition = And [
                Ship
                Not Stationary
            ]
            stackinggroup = "BLD_COLLECTIVE_NET_RESEARCH_EFFECT"
            effects = SetTargetResearch value = Value + Target.Population * 2.5 * [[RESEARCH_PER_POP]]
    ]
    icon = "icons/building/psi-corps.png"

BuildingType
    name = "BLD_GENOME_BANK"
    description = "BLD_GENOME_BANK_DESC"
    buildcost = 15
    buildtime = 16
    location = And [
        Planet
        Not Contains Building name = "BLD_GENOME_BANK"
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/genome_bank.png"

BuildingType
    name = "BLD_SPACE_ELEVATOR"
    description = "BLD_SPACE_ELEVATOR_DESC"
    buildcost = 150
    buildtime = 6
    location = And [
        Planet
        Not Contains Building name = "BLD_SPACE_ELEVATOR"
        Not Planet type = Asteroids
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet size = Tiny
            ]
            activation = Source
            effects = SetMaxSupply value = Value + 1

        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet size = Small
            ]
            activation = Source
            effects = SetMaxSupply value = Value + 2

        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet size = Medium
            ]
            activation = Source
            effects = SetMaxSupply value = Value + 3

        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet size = Large
            ]
            activation = Source
            effects = SetMaxSupply value = Value + 4

        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet size = Huge
            ]
            activation = Source
            effects = SetMaxSupply value = Value + 5

        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet type = GasGiant
            ]
            activation = Source
            effects = SetMaxSupply value = Value + 4
    ]
    icon = ""

BuildingType
    name = "BLD_GAIA_TRANS"
    description = "BLD_GAIA_TRANS_DESC"
    buildcost = 500
    buildtime = 12
    location = And [
        Planet
        Not Contains Building name = "BLD_GAIA_TRANS"
        OwnedBy empire = Source.Owner
        Planet environment = Good
        Not Planet type = [Asteroids GasGiant]
        Not HasSpecial name = "GAIA_SPECIAL"
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = Source
            effects = [
                AddSpecial name = "GAIA_SPECIAL"
                GenerateSitRepMessage
                    message = "EFFECT_GAIA"
                    icon = "icons/specials_huge/gaia.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
            ]

        EffectsGroup
            scope = Source
            activation = Source
            effects = Destroy
    ]
    icon = "icons/specials_huge/gaia.png"
    
BuildingType
    name = "BLD_ART_BLACK_HOLE"
    description = "BLD_ART_BLACK_HOLE_DESC"
    buildcost = 40
    buildtime = 10
    location = And [
        Planet
        Not Contains Building name = "BLD_ART_BLACK_HOLE"
        OwnedBy empire = Source.Owner
        Star type = Red
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.SystemID
                System
            ]
            activation = Star type = Red
            effects = [
                SetStarType type = BlackHole
                GenerateSitRepMessage
                    message = "EFFECT_BLACKHOLE"
                    icon = "icons/building/blackhole.png"
                    parameters = tag = "system" data = Source.SystemID
                    empire = Source.Owner
                ]

        EffectsGroup
            scope = Source
            activation = Source
            effects = Destroy
    ]
    icon = "icons/building/blackhole.png"

BuildingType
    name = "BLD_HYPER_DAM"
    description = "BLD_HYPER_DAM_DESC"
    buildcost = 250
    buildtime = 10
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_HYPER_DAM"
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                PopulationCenter
                Focus type = "FOCUS_INDUSTRY"
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
            ]
            activation = Source
            stackinggroup = "BLD_HYPER_DAM_BONUS"
            effects = SetTargetIndustry value = Value + Target.Population * 2 * [[INDUSTRY_PER_POP]]

        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
                Not Star type = BlackHole
                Focus type = "FOCUS_INDUSTRY"
            ]
            activation = Source
            effects = SetTargetPopulation value = Value - 5
    ]
    icon = "icons/building/blackhole.png"

BuildingType
    name = "BLD_SOL_ORB_GEN"
    description = "BLD_SOL_ORB_GEN_DESC"
    buildcost = 75
    buildtime = 6
    tags = "ORBITAL"
    location = AND [
        Not Contains Building name = "BLD_SOL_ORB_GEN"
        OwnedBy empire = Source.Owner
        Not Star type = [Neutron BlackHole NoStar]
        ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                PopulationCenter
                Focus type = "FOCUS_INDUSTRY"
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
            ]
            activation = Star type = [Blue White]
            stackinggroup = "BLD_SOL_ORB_GEN_EFFECT"
            effects = SetTargetIndustry value = Value + Target.Population * 2 * [[INDUSTRY_PER_POP]]

        EffectsGroup
            scope = And [
                PopulationCenter
                Focus type = "FOCUS_INDUSTRY"
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
            ]
            activation = And [
                Star type = [Yellow Orange]
                Not ResourceSupplyConnected empire = Source.Owner condition = And [
                    Building name = "BLD_SOL_ORB_GEN"
                    OwnedBy empire = Source.Owner
                    Star type = [Blue White]
                ]
            ]
            stackinggroup = "BLD_SOL_ORB_GEN_EFFECT"
            effects = SetTargetIndustry value = Value + Target.Population * 1.0 * [[INDUSTRY_PER_POP]]

        EffectsGroup
            scope = And [
                PopulationCenter
                Focus type = "FOCUS_INDUSTRY"
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
            ]
            activation = And [
                Star type = Red
                Not ResourceSupplyConnected empire = Source.Owner condition = And [
                    Building name = "BLD_SOL_ORB_GEN"
                    OwnedBy empire = Source.Owner
                    Star type = [Yellow Orange Blue White]
                ]
            ]
            stackinggroup = "BLD_SOL_ORB_GEN_EFFECT"
            effects = SetTargetIndustry value = Value + Target.Population * 0.5 * [[INDUSTRY_PER_POP]]
    ]
    icon = "icons/building/miniature_sun.png"
/*
BuildingType
    name = "BLD_CLONING_CENTER"
    description = "BLD_CLONING_CENTER_DESC"
    buildcost = 100
    buildtime = 5
    location = And [
        Planet
        Not Contains Building name = "BLD_CLONING_CENTER"
        OwnedBy empire = Source.Owner
        Not Contains Or [
            Building name = "BLD_CLONING_CENTER"
            Building name = "BLD_BIOTERROR_PROJECTOR"
        ]
    ]
    icon = ""
*/
BuildingType
    name = "BLD_TERRAFORM"
    description = "BLD_TERRAFORM_DESC"
    buildcost = 200 * (1 + Target.DistanceFromOriginalType)
    buildtime = 8
    location = And [
        Planet
        Not Contains Building name = "BLD_TERRAFORM"
        Not Contains Building name = "BLD_TERRAFORM_REVERT"
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
        Not Planet type = [Asteroids GasGiant]
        Not Planet environment = [Uninhabitable Good]
        Not And [
            Species name = "SP_EXOBOT"
            Planet environment = Adequate
        ]
    ]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = [
                SetPlanetType type = Target.NextBetterPlanetType
                GenerateSitRepMessage
                    message = "EFFECT_TERRAFORM"
                    icon = "icons/building/terraform.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
            ]

        EffectsGroup
            scope = Source
            effects = Destroy
    ]
    icon = "icons/building/terraform.png"

BuildingType
    name = "BLD_TERRAFORM_REVERT"
    description = "BLD_TERRAFORM_REVERT_DESC"
    buildcost = 150
    buildtime = 6
    location = And [
        Planet
        Not Contains Building name = "BLD_TERRAFORM"
        Not Contains Building name = "BLD_TERRAFORM_REVERT"
        OwnedBy empire = Source.Owner
        Not Planet type = [Asteroids GasGiant]
        Not Planet type = LocalCandidate.OriginalType
    ]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = [
                SetPlanetType type = Target.NextCloserToOriginalPlanetType
                GenerateSitRepMessage
                    message = "EFFECT_TERRAFORM"
                    icon = "icons/building/terraform.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
            ]

        EffectsGroup
            scope = Source
            effects = Destroy
    ]
    icon = "icons/building/terraform_revert.png"

/*  Remote Terraforming is broken badly so hidden
BuildingType
    name = "BLD_REMOTE_TERRAFORM"
    description = "BLD_REMOTE_TERRAFORM_DESC"
    buildcost = 6
    buildtime = 16
    location = And [
        Planet
        Not Contains Building name = "BLD_REMOTE_TERRAFORM"
        OwnedBy empire = Source.Owner
        Not Planet environment = [Uninhabitable Good]
        Not Planet type = [Asteroids GasGiant]
    ]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = SetPlanetType type = Target.NextBetterPlanetType

        EffectsGroup
            scope = Source
            effects = Destroy
    ]
    icon = ""
*/
BuildingType
    name = "BLD_NEUTRONIUM_EXTRACTOR"
    description = "BLD_NEUTRONIUM_EXTRACTOR_DESC"
    buildcost = 25
    buildtime = 8
    tags = "ORBITAL"
    location = AND [
        Planet
        Not Contains Building name = "BLD_NEUTRONIUM_EXTRACTOR"
        OwnedBy empire = Source.Owner
        Star type = Neutron
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/neutronium-forge.png"

BuildingType
    name = "BLD_NEUTRONIUM_FORGE"
    description = "BLD_NEUTRONIUM_FORGE_DESC"
    buildcost = 100
    buildtime = 3
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_NEUTRONIUM_FORGE"
        Contains And [
            Building name = "BLD_SHIPYARD_BASE"
            OwnedBy empire = Source.Owner
        ]
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/neutronium-forge.png"

BuildingType
    name = "BLD_NEUTRONIUM_SYNTH"
    description = "BLD_NEUTRONIUM_SYNTH_DESC"
    buildcost = 1200
    buildtime = 10
    location = AND [
        Planet
        Not Contains Building name = "BLD_NEUTRONIUM_SYNTH"
        OwnedBy empire = Source.Owner
        ]
    icon = "icons/building/neutronium-forge.png"

BuildingType
    name = "BLD_CONC_CAMP"
    description = "BLD_CONC_CAMP_DESC"
    buildcost = 5
    buildtime = 1
    location = AND [
        Not Contains Building name = "BLD_CONC_CAMP"
        OwnedBy empire = Source.Owner
        Population low = 3
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = ContainedBy And [
                PopulationCenter
                OwnedBy empire = Source.Owner
            ]
            effects = SetTargetIndustry value = Value + Target.Population * 10 * [[INDUSTRY_PER_POP]]
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = ContainedBy And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                Population low = 0.0001
                Not HasSpecial name = "CONC_CAMP_MASTER_SPECIAL"
            ]
            effects = AddSpecial name = "CONC_CAMP_MASTER_SPECIAL"
        EffectsGroup
            scope = Source
            activation =    ContainedBy And [
                                Object id = Source.PlanetID
                                Population high = 0
                            ]
            effects = [
                Destroy
                GenerateSitRepMessage
                    message = "EFFECT_CONC_CAMP_COMLETE"
                    icon = "icons/building/concentration-camp.png"
                    parameters = tag = "planet" data = Source.PlanetID
                    empire = Source.Owner
                ]
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = ContainedBy And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                HasSpecial name = "CONC_CAMP_MASTER_SPECIAL"
            ]
            effects = [
                SetPopulation value = min(Value - 3, Value + 0.5*(101+Target.TargetPopulation-2*Value - Max(0,(101+Target.TargetPopulation-2*Value)^2 -4*(Value*(Value-1-Target.TargetPopulation)-3*100))^0.5))
                SetIndustry value = Target.TargetIndustry
            ]
        EffectsGroup
            scope = Source
            activation = Not ProducedByEmpire empire = Source.Owner
            effects = Destroy
    ]
    icon = "icons/building/concentration-camp.png"

BuildingType
    name = "BLD_CONC_CAMP_REMNANT"
    description = "BLD_CONC_CAMP_REMNANT_DESC"
    buildcost = 1
    buildtime = 1
    location = NOT ALL
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = ContainedBy And [
                PopulationCenter
                Not Contains Building name = "BLD_CONC_CAMP"
            ]
            effects = SetIndustry value = Target.TargetIndustry
        EffectsGroup
            scope = Source
            activation =  ContainedBy AND [
                Not HasSpecial name = "CONC_CAMP_MASTER_SPECIAL"
                Not HasSpecial name = "CONC_CAMP_SLAVE_SPECIAL"
                Industry High = Source.Planet.TargetIndustry + 2 
            ]
            effects = Destroy
    ]
    icon = "icons/building/concentration-camp.png"

BuildingType
    name = "BLD_BLACK_HOLE_POW_GEN"
    description = "BLD_BLACK_HOLE_POW_GEN_DESC"
    buildcost = 250
    buildtime = 8
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_BLACK_HOLE_POW_GEN"
        OwnedBy empire = Source.Owner
        Star type = BlackHole
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                PopulationCenter
                Focus type = "FOCUS_INDUSTRY"
                OwnedBy empire = Source.Owner
                ResourceSupplyConnected empire = Source.Owner condition = Source
            ]
            activation = Star type = BlackHole
            stackinggroup = "BLD_BLACK_HOLE_POW_GEN_PRIMARY_EFFECT"
            effects = SetTargetIndustry value = Value + Target.Population * 6 * [[INDUSTRY_PER_POP]]
    ]
    icon = "icons/building/blackhole.png"

BuildingType
    name = "BLD_REPLICATORS"
    description = "BLD_REPLICATORS_DESC"
    buildcost = 125
    buildtime = 4
    location = AND [
        Not Contains Building name = "BLD_REPLICATORS"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = ""

BuildingType
    name = "BLD_PLANET_DRIVE"
    description = "BLD_PLANET_DRIVE_DESC"
    buildcost = 125
    buildtime = 5
    location = And [
        Planet
        Not Contains Building name = "BLD_PLANET_DRIVE"
        OwnedBy empire = Source.Owner
        Not Planet type = Asteroids
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/shipyard-11.png"

BuildingType
    name = "BLD_PLANET_BEACON"
    description = "BLD_PLANET_BEACON_DESC"
    buildcost = 1
    buildtime = 1
    location = AND [
        Not Contains Building name = "BLD_PLANET_BEACON"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups =
        EffectsGroup
            scope = Source
            activation = WithinStarlaneJumps jumps = 1 condition = Building name = "BLD_PLANET_DRIVE"
            effects = Destroy
    icon = "icons/building/beacon.png"

BuildingType
    name = "BLD_ART_PLANET"
    description = "BLD_ART_PLANET_DESC"
    buildcost = 800
    buildtime = 8
    location = And [
        Planet
        Not Contains Building name = "BLD_ART_PLANET"
        Not Contains Building name = "BLD_ART_FACTORY_PLANET"
        Not Contains Building name = "BLD_ART_PARADISE_PLANET"
        OwnedBy empire = Source.Owner
        Planet type = [Asteroids GasGiant]
        Population high = 0
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = Source
            effects = [
                SetPlanetType type = Barren
                GenerateSitRepMessage
                    message = "EFFECT_ART_PLANET"
                    icon = "icons/specials_large/tidal_lock.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
                ]

        EffectsGroup
            scope = Source
            activation = Source
            effects = Destroy
    ]
    icon = "icons/specials_huge/tidal_lock.png"

BuildingType
    name = "BLD_ART_FACTORY_PLANET"
    description = "BLD_ART_FACTORY_PLANET_DESC"
    buildcost = 800 + ( 70 * [[COLONY_UPKEEP_MULTIPLICATOR]] )
    buildtime = 12
    location = And [
        Planet
        Not Contains Building name = "BLD_ART_PLANET"
        Not Contains Building name = "BLD_ART_FACTORY_PLANET"
        Not Contains Building name = "BLD_ART_PARADISE_PLANET"
        OwnedBy empire = Source.Owner
        Planet type = [Asteroids GasGiant]
        OwnerHasTech name = "PRO_EXOBOTS"
        Population high = 0
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = Source
            effects = [
                SetPlanetType type = Barren
                SetSpecies name = "SP_EXOBOT"
                SetPopulation value = 1
                GenerateSitRepMessage
                    message = "EFFECT_ART_PLANET"
                    icon = "icons/species/robotic-01.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
            ]

        EffectsGroup
            scope = Source
            activation = Source
            effects = Destroy
    ]
    icon = "icons/species/robotic-01.png"

BuildingType
    name = "BLD_ART_PARADISE_PLANET"
    description = "BLD_ART_PARADISE_PLANET_DESC"
    buildcost = 1000
    buildtime = 10
    location = And [
        Planet
        Not Contains Building name = "BLD_ART_PLANET"
        Not Contains Building name = "BLD_ART_FACTORY_PLANET"
        Not Contains Building name = "BLD_ART_PARADISE_PLANET"
        OwnedBy empire = Source.Owner
        Planet type = [Asteroids GasGiant]
        OwnerHasTech name = "GRO_GAIA_TRANS"
        Population high = 0
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = Source
            effects = [
                SetPlanetType type = Barren
                AddSpecial name = "GAIA_SPECIAL"
                GenerateSitRepMessage
                    message = "EFFECT_ART_PLANET"
                    icon = "icons/specials_large/gaia.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
                ]

        EffectsGroup
            scope = Source
            activation = Source
            effects = Destroy
    ]
    icon = "icons/tech/paradise_planet.png"

BuildingType
    name = "BLD_ART_MOON"
    description = "BLD_ART_MOON_DESC"
    buildcost = 250
    buildtime = 5
    location = And [
        Planet
        Not Contains Building name = "BLD_ART_MOON"
        OwnedBy empire = Source.Owner
        Not Planet type = [Asteroids GasGiant]
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups =
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            activation = Source
            effects = AddSpecial name = "RESONANT_MOON_SPECIAL"
    icon = "icons/specials_huge/resonant_moon.png"

BuildingType
    name = "BLD_TRANSFORMER"
    description = "BLD_TRANSFORMER_DESC"
    buildcost = 100
    buildtime = 8
    location = AND [
        Not Contains Building name = "BLD_TRANSFORMER"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/transformer.png"

BuildingType
    name = "BLD_GATEWAY_VOID"
    description = "BLD_GATEWAY_VOID_DESC"
    buildcost = 200
    buildtime = 10
    location = AND [
        Not Contains Building name = "BLD_GATEWAY_VOID"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Fleet
                InSystem id = Source.SystemID
            ]
            activation = Source
            effects = [
                Destroy
                GenerateSitRepMessage
                    message = "EFFECT_GATEWAY_VOID_DESTROY"
                    parameters = [
                        tag = "buildingtype" data = "BLD_GATEWAY_VOID"
                        tag = "planet" data = Source.PlanetID
                        tag = "fleet" data = Target.ID
                    ]
                    empire = Source.Owner
            ]
        EffectsGroup
            scope = And [
                PopulationCenter
                InSystem id = Source.SystemID
            ]
            activation = Source
            effects = SetTargetPopulation value = min(Value, 0.1)

        EffectsGroup
            scope = InSystem id = Source.SystemID
            activation = Source
            effects = SetStealth value = Value + 1000
    ]
    icon = "icons/building/monument_to_exodus.png"

BuildingType
    name = "BLD_ENCLAVE_VOID"
    description = "BLD_ENCLAVE_VOID_DESC"
    buildcost = 100
    buildtime = 3
    location = AND [
        Not Contains Building name = "BLD_ENCLAVE_VOID"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                PopulationCenter
                OwnedBy empire = Source.Owner
                Focus type = "FOCUS_RESEARCH"
            ]
            activation = Source
            stackinggroup = "BLD_ENCLAVE_VOID_STACK"
            effects = SetTargetResearch value = Value + Target.Population * 3.75 *  [[RESEARCH_PER_POP]]
    ]
    icon = "icons/building/science-institute.png"

BuildingType
    name = "BLD_PLANET_CLOAK"
    description = "BLD_PLANET_CLOAK_DESC"
    buildcost = 150
    buildtime = 8
    location = AND [
        Not Contains Building name = "BLD_PLANET_CLOAK"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = ""

BuildingType
    name = "BLD_SPATIAL_DISTORT_GEN"
    description = "BLD_SPATIAL_DISTORT_GEN_DESC"
    buildcost = 30
    buildtime = 10
    location = AND [
        Not Contains Building name = "BLD_SPATIAL_DISTORT_GEN"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = ""

BuildingType
    name = "BLD_STARGATE"
    description = "BLD_STARGATE_DESC"
    buildcost = 500 + 50 *
                    Count condition = And [
                        Building name = "BLD_STARGATE"
                        OwnedBy empire = Source.Owner
                    ]
    buildtime = 10
    tags = "ORBITAL"
    location = AND [
        TargetPopulation low = 1
        Not Contains Building name = "BLD_STARGATE"
        OwnedBy empire = Source.Owner
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    icon = "icons/building/stargate.png"

BuildingType
    name = "BLD_GAS_GIANT_GEN"
    description = "BLD_GAS_GIANT_GEN_DESC"
    buildcost = 25
    buildtime = 3
    tags = "ORBITAL"
    location = And [
        Planet
        Not Contains Building name = "BLD_GAS_GIANT_GEN"
        OwnedBy empire = Source.Owner
        Planet type = GasGiant
    ]
    EnqueueLocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Planet
                InSystem id = Source.SystemID
                Focus type = "FOCUS_INDUSTRY"
                NOT Population high = 0
                OwnedBy empire = Source.Owner
            ]
            stackinggroup = "GAS_GIANT_GEN_STACK"
            effects = SetTargetIndustry value = Value + 10
        EffectsGroup
            scope = Source
            activation = Not Planet type = GasGiant
            effects = Destroy
    ]
    icon = "icons/building/gas-giant-generator.png"

BuildingType
    name = "BLD_COLONY_BASE"
    description = "BLD_COLONY_BASE_DESC"
    buildcost = 45
    buildtime = 1
    location = And [
        Planet
        Not Contains Building name = "BLD_COLONY_BASE"
        Population low = [[MIN_RECOLONIZING_SIZE]]
        OwnedBy empire = Source.Owner
    ]
    /* TODO: Disable colony base if there is no possibility in the current system */
    effectsgroups = [
        EffectsGroup
            scope = Source
            activation = Source
            effects = CreateShip designname = "SD_COLONY_BASE" empire = Source.Owner species = Source.Planet.Species

        EffectsGroup
            scope = Source
            activation = Source
            effects = Destroy
    ]
    icon = "icons/ship_hulls/colony_base_hull_small.png"


/* Experimentor related variables / macros */

// the highest current MaxAIAggression is 5, so this would range from 0.2 to 1.2
EXPERIMENTOR_MONSTER_FREQ_FACTOR
'''((1 + GalaxyMaxAIAggression) / 5.0)'''

EXPERIMENTOR_SPAWN_BASE_TURN
'''200'''

EXPERIMENTOR_SPAWN_AI_AGGRESSION_CHECK
'''(If condition = ValueTest high = 2 testvalue = GalaxyMaxAIAggression)'''

// the following intermediate value is turn 200 for Manaical Max AI Aggression, and 50 turns later for each aggression tier less,
// for AI aggressions above Typical, otherwise adding 1000 turns
EXPERIMENTOR_SPAWN_CALC_A
'''( [[EXPERIMENTOR_SPAWN_BASE_TURN]] + (50 * (5 - GalaxyMaxAIAggression)) + (1000 * [[EXPERIMENTOR_SPAWN_AI_AGGRESSION_CHECK]]))'''

// the following modifies Experimentor spawn start by a factor of 0.1 up for low planet density, and 0.1 down for high planet density
// for galaxy sizes above 200 increases it somewhat as the galaxy size grows
EXPERIMENTOR_SPAWN_START_TURN
''' ( [[EXPERIMENTOR_SPAWN_CALC_A]] * ((1.2 - ( GalaxyPlanetDensity / 10 ))) * ((Max(1, GalaxySize / 200))^0.4))'''

EXPERIMENTOR_ADD_STARLANE
'''AddStarlanes endpoint = NumberOf number = 1 
                                    condition = And [
                                        MinimumNumberOf number = 5 
                                                        sortkey = DirectDistanceBetween object = Source.ID
                                                                                        object = LocalCandidate.ID
                                                        condition = System
                                        // CanAddStarlanesTo condition = And [ System Object id = Source.SystemID ] // parses but does not appear to be accepting any candidates
                                    ]
'''

BuildingType
    name = "BLD_EXPERIMENTOR_OUTPOST"
    description = "BLD_EXPERIMENTOR_OUTPOST_DESC"
    buildcost = 1
    buildtime = 1
   // captureresult = destroy
    location = All
    effectsgroups = [
        EffectsGroup
            scope = Or [
                Source
                And [
                    Object id = Source.PlanetID
                    Planet
                ]
            ]
            effects = SetStealth value = Value + 60

        EffectsGroup
            scope = Or [
                And [
                    Object id = Source.SystemID
                    System
                ]
                And [
                    Planet
                    InSystem id = Source.SystemID
                    Not Source
                ]
            ]
            effects = SetStealth value = Value + 40

        EffectsGroup
            scope = And [
                    Object id = Source.PlanetID
                    Planet
            ]
            effects = [
                SetMaxShield value = Value + 60000
                SetMaxDefense value = Value + [[EXPERIMENTOR_SPAWN_START_TURN]]
                SetMaxTroops value = Value + 300
                SetDetection value = Value + 100
            ]
        EffectsGroup
            scope = And [
                    Object id = Source.PlanetID
                    Planet
            ]
            activation = Or [
                Turn high = [[EXPERIMENTOR_SPAWN_START_TURN]]
                Not ContainedBy Contains And [ Ship OwnedBy affiliation = AnyEmpire ]
            ]
            effects = [
                // Regeneration
                SetDefense value = Value + 10
                SetShield value = Value + 50
            ]

        EffectsGroup
            scope = And [
                Source
                OwnedBy affiliation = AnyEmpire
                Species name = "SP_EXPERIMENTOR" 
            ]
            effects = [
                GenerateSitRepMessage
                    message = "SITREP_VICTORY_CAPTURE"
                    icon = "icons/sitrep/victory.png"
                    empire = Source.Owner
                [[EXPERIMENTOR_ADD_STARLANE]]
                [[EXPERIMENTOR_ADD_STARLANE]]
                [[EXPERIMENTOR_ADD_STARLANE]]
                [[EXPERIMENTOR_ADD_STARLANE]]
                [[EXPERIMENTOR_ADD_STARLANE]]
                Destroy
            ]

        EffectsGroup
            scope = And [
                Source
                Not OwnedBy affiliation = AnyEmpire
            ]
            activation = Or [
                Turn high = [[EXPERIMENTOR_SPAWN_START_TURN]]      // always remove lanes before spawn start turn
                Not ContainedBy Contains And [ Monster Unowned Not Design name = "SM_EXP_OUTPOST"]
            ]
            effects = RemoveStarlanes endpoint = WithinStarlaneJumps jumps = 1 condition = Source

        EffectsGroup
            scope = Source
            activation = And [
                Turn low = [[EXPERIMENTOR_SPAWN_START_TURN]]  high = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 35)
                Random probability = (0.2 * [[EXPERIMENTOR_MONSTER_FREQ_FACTOR]] )
            ]
            effects = [
                CreateShip designname = "SM_BLACK_KRAKEN" empire = Source.Owner
                CreateShip designname = "SM_BLACK_KRAKEN" empire = Source.Owner
                CreateShip designname = "SM_BLACK_KRAKEN" empire = Source.Owner
                GenerateSitRepMessage
                    message = "EFFECT_EXPERIMENT_MONSTERS_LAUNCH"
                    icon = "icons/specials_huge/ancient_ruins.png"
                    parameters = [
                        tag = "planet" data = Source.SystemID
                        tag = "predefinedshipdesign" data = "SM_BLACK_KRAKEN"
                        tag = "species" data = "SP_EXPERIMENTOR"
                        tag = "rawtext" data = "3"
                    ]
                [[EXPERIMENTOR_ADD_STARLANE]]
            ]

        EffectsGroup
            scope = Source
            activation = And [
                Turn low = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 36)
                Random probability = (0.1 * [[EXPERIMENTOR_MONSTER_FREQ_FACTOR]] )
            ]
            effects = [
                CreateShip designname = "SM_BLACK_KRAKEN" empire = Source.Owner
                CreateShip designname = "SM_BLACK_KRAKEN" empire = Source.Owner
                GenerateSitRepMessage
                    message = "EFFECT_EXPERIMENT_MONSTERS_LAUNCH"
                    icon = "icons/specials_huge/ancient_ruins.png"
                    parameters = [
                        tag = "planet" data = Source.SystemID
                        tag = "predefinedshipdesign" data = "SM_BLACK_KRAKEN"
                        tag = "species" data = "SP_EXPERIMENTOR"
                        tag = "rawtext" data = "2"
                    ]
                [[EXPERIMENTOR_ADD_STARLANE]]
            ]

        EffectsGroup
            scope = Source
            activation = And [
                Turn low = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 36) high = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 70)
                Random probability = (0.2 * [[EXPERIMENTOR_MONSTER_FREQ_FACTOR]] )
            ]
            effects = [
                CreateShip designname = "SM_BLOATED_JUGGERNAUT" empire = Source.Owner
                CreateShip designname = "SM_BLOATED_JUGGERNAUT" empire = Source.Owner
                CreateShip designname = "SM_BLOATED_JUGGERNAUT" empire = Source.Owner
                GenerateSitRepMessage
                    message = "EFFECT_EXPERIMENT_MONSTERS_LAUNCH"
                    icon = "icons/specials_huge/ancient_ruins.png"
                    parameters = [
                        tag = "planet" data = Source.SystemID
                        tag = "predefinedshipdesign" data = "SM_BLOATED_JUGGERNAUT"
                        tag = "species" data = "SP_EXPERIMENTOR"
                        tag = "rawtext" data = "3"
                    ]
                [[EXPERIMENTOR_ADD_STARLANE]]
            ]

        EffectsGroup
            scope = Source
            activation = And [
                Turn low = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 71)
                Random probability = (0.1 * [[EXPERIMENTOR_MONSTER_FREQ_FACTOR]] )
            ]
            effects = [
                CreateShip designname = "SM_BLOATED_JUGGERNAUT" empire = Source.Owner
                CreateShip designname = "SM_BLOATED_JUGGERNAUT" empire = Source.Owner
                GenerateSitRepMessage
                    message = "EFFECT_EXPERIMENT_MONSTERS_LAUNCH"
                    icon = "icons/specials_huge/ancient_ruins.png"
                    parameters = [
                        tag = "planet" data = Source.SystemID
                        tag = "predefinedshipdesign" data = "SM_BLOATED_JUGGERNAUT"
                        tag = "species" data = "SP_EXPERIMENTOR"
                        tag = "rawtext" data = "2"
                    ]
                [[EXPERIMENTOR_ADD_STARLANE]]
            ]

        EffectsGroup
            scope = Source
            activation = And [
                Turn low = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 71) high = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 130)
                Random probability = (0.2 * [[EXPERIMENTOR_MONSTER_FREQ_FACTOR]] )
            ]
            effects = [
                CreateShip designname = "SM_PSIONIC_SNOWFLAKE" empire = Source.Owner
                CreateShip designname = "SM_PSIONIC_SNOWFLAKE" empire = Source.Owner
                CreateShip designname = "SM_PSIONIC_SNOWFLAKE" empire = Source.Owner
                GenerateSitRepMessage
                    message = "EFFECT_EXPERIMENT_MONSTERS_LAUNCH"
                    icon = "icons/specials_huge/ancient_ruins.png"
                    parameters = [
                        tag = "planet" data = Source.SystemID
                        tag = "predefinedshipdesign" data = "SM_PSIONIC_SNOWFLAKE"
                        tag = "species" data = "SP_EXPERIMENTOR"
                        tag = "rawtext" data = "3"
                    ]
                [[EXPERIMENTOR_ADD_STARLANE]]
            ]

        EffectsGroup
            scope = Source
            activation = And [
                Turn low = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 131)
                Random probability = (0.1 * [[EXPERIMENTOR_MONSTER_FREQ_FACTOR]] )
            ]
            effects = [
                CreateShip designname = "SM_PSIONIC_SNOWFLAKE" empire = Source.Owner
                CreateShip designname = "SM_PSIONIC_SNOWFLAKE" empire = Source.Owner
                GenerateSitRepMessage
                    message = "EFFECT_EXPERIMENT_MONSTERS_LAUNCH"
                    icon = "icons/specials_huge/ancient_ruins.png"
                    parameters = [
                        tag = "planet" data = Source.SystemID
                        tag = "predefinedshipdesign" data = "SM_PSIONIC_SNOWFLAKE"
                        tag = "species" data = "SP_EXPERIMENTOR"
                        tag = "rawtext" data = "2"
                    ]
                [[EXPERIMENTOR_ADD_STARLANE]]
            ]

        EffectsGroup
            scope = Source
            activation = And [
                Turn low = ( [[EXPERIMENTOR_SPAWN_START_TURN]] + 131)
                Random probability = (0.2 * [[EXPERIMENTOR_MONSTER_FREQ_FACTOR]] )
            ]
            effects = [
                CreateShip designname = "SM_COSMIC_DRAGON" empire = Source.Owner
                GenerateSitRepMessage
                    message = "EFFECT_EXPERIMENT_MONSTERS_LAUNCH"
                    icon = "icons/specials_huge/ancient_ruins.png"
                    parameters = [
                        tag = "planet" data = Source.SystemID
                        tag = "predefinedshipdesign" data = "SM_COSMIC_DRAGON"
                        tag = "species" data = "SP_EXPERIMENTOR"
                        tag = "rawtext" data = "1"
                    ]
                [[EXPERIMENTOR_ADD_STARLANE]]
            ]
    ]
    icon = ""

BuildingType
    name = "BLD_NOVA_BOMB_ACTIVATOR"
    description = "BLD_NOVA_BOMB_ACTIVATOR_DESC"
    buildcost = 25
    buildtime = 1
    location = All
    effectsgroups =
        EffectsGroup
            scope = Source
            effects = Destroy
    icon = "icons/building/nova-bomb-activator.png"

BuildingType
    name = "BLD_EVACUATION"
    description = "BLD_EVACUATION_DESC"
    buildcost = 10
    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 = [
        EffectsGroup            // add population to connected plants with room and suitable species
            scope = NumberOf number = 1 condition = And [
                PopulationCenter
                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 [
                PopulationCenter
                Species name = Source.Planet.Species
                Not Object id = Source.PlanetID
                ResourceSupplyConnected empire = Source.Owner condition = Source
                Population low = 1 high = LocalCandidate.TargetPopulation - 1
            ]
            effects = [
                SetPopulation value = Value + 2
                GenerateSitRepMessage
                    message = "EFFECT_EVACUEES"
                    icon = "icons/building/evacuation.png"
                    parameters = tag = "planet" data = Target.ID
                    empire = Source.Owner
                ]

        EffectsGroup            // remove population from location
            scope = And [
                Object id = Source.PlanetID
                Planet            
            ]
            activation = ContainedBy And [
                PopulationCenter
                OwnedBy empire = Source.Owner
            ]
            effects = [ //complicated population formula necessary to actually peg pop reduction at -2, not just 2 less growth than it would have been
                // the fixed amount for pop change per turn is 'k' towards the end of the comment line immediately below, which is 2 in the functional line that follows it
                //SetPopulation value = min(Value - k, Value + 0.5*(101+Target.TargetPopulation-2*Value - Max(0,(101+Target.TargetPopulation-2*Value)^2 -4*(Value*(Value-1-Target.TargetPopulation)-k*100))^0.5))
                SetPopulation value = min(Value - 2, Value + 0.5*(101+Target.TargetPopulation-2*Value - Max(0,(101+Target.TargetPopulation-2*Value)^2 -4*(Value*(Value-1-Target.TargetPopulation)-2*100))^0.5))
                SetIndustry value = 0
                SetTargetIndustry value = 0
                SetResearch value = 0
                SetTargetResearch 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
                        Population high = 0
                    ]
                    Contains Building name = "BLD_CONC_CAMP"
                    HasSpecial name = "CONC_CAMP_SLAVE_SPECIAL"
                ]
            ]
            effects = Destroy
    ]
    icon = "icons/building/evacuation.png"

/*
BuildingType
    name = "BLD_SUPER_TEST"
    description = "BLD_SUPER_TEST_DESC"
    buildcost = 1
    buildtime = 1
    location = Planet
    effectsgroups = [
        Effectsgroup
        scope = And [
            Object id = Source.PlanetID
            Planet
        ]
        effects = [
            SetSpecies name = "SP_SUPER_TEST"
            SetPopulation value = max(Target.Population, 1)
        ]
        
        EffectsGroup            // remove building when done
            scope = Source
            activation = Species name = "SP_SUPER_TEST"
            effects = Destroy
    ]
    icon = "icons/species/other-04.png"
*/

#include "shared_macros.txt"
#include "col_buildings.txt"
