BuildingType
    name = "BLD_IMPERIAL_PALACE"
    description = "BLD_IMPERIAL_PALACE_DESC"
    captureresult = Destroy
    buildcost = 5 * [[BUILDING_COST_MULTIPLIER]] * (Statistic Count condition = And [Planet OwnedBy empire = Source.Owner])
    buildtime = 8
    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 = [
        [[SPECIES_LIKES_OR_DISLIKES_BUILDING_STABILITY_EFFECTS]]

        // sets empire's capital
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.ProducedByEmpireID
            ]
            effects = SetEmpireCapital

        // one economic policy slot
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                Capital
            ]
            stackinggroup = "IMPERIAL_PALACE_ECO_SLOT"
            effects = SetEmpireMeter empire = Source.Owner
                                     meter = "ECONOMIC_CATEGORY_NUM_POLICY_SLOTS" value = Value + 1

        // flat bonus to supply range of capital
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.ProducedByEmpireID
                Capital
            ]
            stackinggroup = "IMPERIAL_PALACE_SUPPLY_EFFECT"
            effects = SetMaxSupply value = Value
                        + (NamedReal name = "BLD_IMPERIAL_PALACE_MAX_SUPPLY_FLAT" value = 2)

        // flat bonus to capital construction
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.ProducedByEmpireID
                Capital
            ]
            activation = Not Number low = 1 condition = And [
                Building name = "BLD_MEGALITH"
                OwnedBy empire = Source.Owner
            ]
            stackinggroup = "IMPERIAL_PALACE_EFFECT"
            effects = SetTargetConstruction value = Value
                        + (NamedReal name = "BLD_IMPERIAL_PALACE_TARGET_CONSTRUCTION_FLAT" value = 20)

        // flat bonus to stability of capital species
        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
                    Capital
                ]
            ]
            stackinggroup = "IMPERIAL_PALACE_HAPPINESS"
            accountinglabel = "IMPERIAL_PALACE_SPECIES_HAPPINESS"
            effects = SetTargetHappiness value = Value
                        + (NamedReal name = "BLD_IMPERIAL_PALACE_TARGET_HAPPINESS_FLAT" value = 5)

        // flat bonus to opinion of capital species
        EffectsGroup
            scope = Source
            activation = Not Unowned
            effects = SetSpeciesTargetOpinion
                species = Source.Planet.Species
                empire = Source.Owner
                opinion = Value + (NamedReal name = "BLD_IMPERIAL_PALACE_SPECIES_EMPIRE_OPINION_BOOST_FLAT" value = 5)

        // bonus or penalty to opinion of species that like or dislike the capital species
        EffectsGroup
            scope =
                Unique
                    sortkey = LocalCandidate.Species
                    condition = Or [
                        Planet
                        SpeciesLikes name = ThisSpecies
                        SpeciesDislikes name = ThisSpecies
                    ]
            activation = And [
                Not Unowned
                ContainedBy And [
                    Object id = Source.PlanetID
                    Planet
                    Species
                    Capital
                ]
            ]
            effects =
                SetSpeciesTargetOpinion
                    species = Target.Species
                    empire = Source.Owner
                    opinion = Value + (NamedReal name = "BLD_IMPERIAL_PALACE_SPECIES_EMPIRE_OPINION_FLAT" value = 2) * (
                        (Statistic If condition = And [Target SpeciesLikes name = Source.Planet.Species]) - 
                        (Statistic If condition = And [Target SpeciesDislikes name = Source.Planet.Species])
                    )

        // flat bonus to defense, troops and influence
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
                OwnedBy empire = Source.ProducedByEmpireID
            ]
            stackinggroup = "PALACE_DEFENSE_STACK"
            accountinglabel = "BLD_IMPERIAL_PALACE"
            priority = [[TARGET_AFTER_SCALING_PRIORITY]]
            effects = [
                SetMaxDefense value = Value + (NamedReal name = "BLD_IMPERIAL_PALACE_MAX_DEFENSE_FLAT"
                                                         value = (5 * [[PLANET_DEFENSE_FACTOR]]))
                SetMaxTroops value = Value
                    + (NamedReal name = "BLD_IMPERIAL_PALACE_MAX_TROOPS_FLAT" value = 6)
                SetTargetInfluence value = Value
                     + (NamedReal name = "BLD_IMPERIAL_PALACE_TARGET_INFLUENCE_FLAT" value = (GameRule name = "RULE_IMPERIAL_PALACE_INFLUENCE"))
            ]
    ]
    icon = "icons/building/palace.png"

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