// sets stability (happiness) on planets in empire that have the building this macro effect is added to, if those planets' species like or dislike the building

STABILITY_PER_LIKED_BUILDING_ON_PLANET
'''4.0'''

// Policy Liberty doubles de building dislike effect, policy Conformance halves it, macro is in macros/opinion.macros
STABILITY_PER_DISLIKED_BUILDING_ON_PLANET
'''4.0 * [[POLICY_DISLIKE_SCALING]]'''

STABILITY_PER_LIKED_BUILDING_IN_SYSTEM
'''1.0'''

STABILITY_PER_DISLIKED_BUILDING_IN_SYSTEM
'''1.0 * [[POLICY_DISLIKE_SCALING]]'''

SPECIES_LIKES_OR_DISLIKES_BUILDING_STABILITY_EFFECTS
'''
        // species like building on the same planet
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Population low = 0.001
                SpeciesLikes name = ThisBuilding
            ]
            accountinglabel = "LIKES_BUILDING_LABEL"
            effects = SetTargetHappiness value = Value + [[STABILITY_PER_LIKED_BUILDING_ON_PLANET]]

        // species dislike building on the same planet
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Population low = 0.001
                SpeciesDislikes name = ThisBuilding
            ]
            accountinglabel = "DISLIKES_BUILDING_LABEL"
            effects = SetTargetHappiness value = Value - [[STABILITY_PER_DISLIKED_BUILDING_ON_PLANET]]
                
        // species like building in the same system
        EffectsGroup
            scope = And [
                Planet
                InSystem id = Source.SystemID
                Population low = 0.001
                Not Object id = Source.PlanetID // but not on same planet, which is covered by above case
                SpeciesLikes name = ThisBuilding
            ]
            accountinglabel = "LIKES_BUILDING_LABEL"
            effects = SetTargetHappiness value = Value + [[STABILITY_PER_LIKED_BUILDING_IN_SYSTEM]]

        // species dislike building in the same system
        EffectsGroup
            scope = And [
                Planet
                InSystem id = Source.SystemID
                Population low = 0.001
                Not Object id = Source.PlanetID // but not on same planet, which is covered by above case
                SpeciesDislikes name = ThisBuilding
            ]
            accountinglabel = "DISLIKES_BUILDING_LABEL"
            effects = SetTargetHappiness value = Value - [[STABILITY_PER_DISLIKED_BUILDING_IN_SYSTEM]]

        // species like building in empire
        EffectsGroup
            scope = And [
                Planet
                OwnedBy empire = Source.Owner
                Population low = 0.001
                Not InSystem id = Source.SystemID
                SpeciesLikes name = ThisBuilding
            ]
            accountinglabel = "LIKES_BUILDING_LABEL"
            effects =
                SetTargetHappiness value = Value + ((NamedReal name = "BUILDING_LIKE_EMPIRE_SQRT_SCALING" value = 0.5) * 
                    max(1.0,
                        Statistic Count condition = And [
                            Building
                            Building name = ThisBuilding
                            OwnedBy empire = Source.Owner
                            Not InSystem id = Target.SystemID
                        ]
                    )^(-0.5)
                )
 
        // species dislike building in empire
        EffectsGroup
            scope = And [
                Planet
                OwnedBy empire = Source.Owner
                Population low = 0.001
                Not InSystem id = Source.SystemID
                SpeciesDislikes name = ThisBuilding
            ]
            accountinglabel = "DISLIKES_BUILDING_LABEL"
            effects =
                SetTargetHappiness value = Value - ((NamedRealLookup name = "BUILDING_LIKE_EMPIRE_SQRT_SCALING") * 
                    max(1.0,
                        Statistic Count condition = And [
                            Building
                            Building name = ThisBuilding
                            OwnedBy empire = Source.Owner
                            Not InSystem id = Target.SystemID
                        ]
                    )^(-0.5)
                ) * [[POLICY_DISLIKE_SCALING]]
'''

CAN_ADD_STARLANE_TO_SOURCE
'''And [
    System

    Not Contains condition = Source

    Not HasStarlane from = Object id = Source.SystemID

    Not StarlaneToWouldCrossExistingStarlane
        from = Object id = Source.SystemID  // specify the system, not the source object. the condition will consider multiple lanes connecting to a system as not crossing, but if the from object is not itself a system but is in a system, then the system may be considered as crossing a lane from the from object's location

    Not StarlaneToWouldBeAngularlyCloseToExistingStarlane 
        from = Object id = Source.SystemID
        maxdotprod = 0.87

    Not StarlaneToWouldBeCloseToObject
        distance = 20.0
        from = Object id = Source.SystemID
        closeto = And [
            System                              // don't go near other systems
            Not Source                          // source is probably a building or planet, exclude any planet or system at the location of the source, but don't assume that the source is in a system or is on a planet when doing so
            Not Contains Source
            Not ContainedBy Contains Source
            Not RootCandidate
            Not Contains RootCandidate
            Not ContainedBy Contains RootCandidate
        ]
]'''

DO_STARLANE_BORE
'''AddStarlanes endpoint = MinimumNumberOf
       number = 1
       sortkey = DirectDistanceBetween object = Source.ID object = LocalCandidate.ID
       condition = [[CAN_ADD_STARLANE_TO_SOURCE]]
'''

BORE_POSSIBLE
'''Number
       low = 1
       condition = [[CAN_ADD_STARLANE_TO_SOURCE]]
'''

#include "/scripting/macros/opinion.macros"
