// python would definitely help here
[[TARGET_TERRAFORMING(Desert,Terran,Ocean,TUNDRA,DESERT,TERRAN,OCEAN,SWAMP)]]
[[TARGET_TERRAFORMING(Terran,Ocean,Swamp,DESERT,TERRAN,OCEAN,SWAMP,TOXIC)]]
[[TARGET_TERRAFORMING(Ocean,Swamp,Toxic,TERRAN,OCEAN,SWAMP,TOXIC,INFERNO)]]
[[TARGET_TERRAFORMING(Swamp,Toxic,Inferno,OCEAN,SWAMP,TOXIC,INFERNO,RADIATED)]]
[[TARGET_TERRAFORMING(Toxic,Inferno,Radiated,SWAMP,TOXIC,INFERNO,RADIATED,BARREN)]]
[[TARGET_TERRAFORMING(Inferno,Radiated,Barren,TOXIC,INFERNO,RADIATED,BARREN,TUNDRA)]]
[[TARGET_TERRAFORMING(Radiated,Barren,Tundra,INFERNO,RADIATED,BARREN,TUNDRA,DESERT)]]
[[TARGET_TERRAFORMING(Barren,Tundra,Desert,RADIATED,BARREN,TUNDRA,DESERT,TERRAN)]]
[[TARGET_TERRAFORMING(Tundra,Desert,Terran,BARREN,TUNDRA,DESERT,TERRAN,OCEAN)]]


// @1@ clockwise previous type
// @2@ target type
// @3@ clockwise next type
// @4-8@ cw-2 - cw+2 in upper case
TARGET_TERRAFORMING
'''BuildingType
    name = "BLD_TERRAFORM_@6@"
    description = "BLD_TERRAFORM_@6@_DESC"
    buildcost = 100 * (1 - 0.4 * Statistic If condition = And [Source EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_TERRAFORMING"])
                    * (Target.HabitableSize * max(1, PlanetTypeDifference from = Target.OriginalType to = @2@)) * [[BUILDING_COST_MULTIPLIER]]
                    * (1 + 2 * Statistic If condition = And [Source EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_ENVIRONMENTALISM"])
    buildtime = 12 - 6 * Statistic If condition = And [Source EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_TERRAFORMING"]
    location = And [
        Planet
        OwnedBy empire = Source.Owner
        Not Planet type = [Asteroids GasGiant]
        Or [
            Planet type = @1@
            Planet type = @3@
            [[LOCATION_ALLOW_ENQUEUE_IF_PREREQ_ENQUEUED(BLD_TERRAFORM_@5@)]]
            [[LOCATION_ALLOW_ENQUEUE_IF_PREREQ_ENQUEUED(BLD_TERRAFORM_@7@)]]
        ]
    ]
    enqueuelocation = And [
        [[ENQUEUE_BUILD_ONE_PER_PLANET]]
        Not Planet type = @2@
        Not Contains Building name = "BLD_GAIA_TRANS"
        Not Enqueued type = Building name = "BLD_GAIA_TRANS"
        Not HasSpecial name = "GAIA_SPECIAL"
        Not Contains Building name = "BLD_TERRAFORM_BEST"
        Not Enqueued type = Building name = "BLD_TERRAFORM_BEST"
        // Options should go down to one after a player started going into one direction,
        // E.g. after enqueing Ocean on a Terran planet, Desert should be removed.
        // We can however not simply exclude Desert when Ocean is enqueued, since someone
        // may want to convert Swamp to Desert.
        Not And [
            Planet type = @1@
            Or [
                Contains Building name = "BLD_TERRAFORM_@4@"
                Enqueued type = Building name = "BLD_TERRAFORM_@4@"
            ]
        ]
        Not And [
            Planet type = @3@
            Or [
                Contains Building name = "BLD_TERRAFORM_@8@"
                Enqueued type = Building name = "BLD_TERRAFORM_@8@"
            ]
        ]
    ]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = [
                SetPlanetType type = @2@
                GenerateSitRepMessage
                    message = "EFFECT_TERRAFORM"
                    label = "EFFECT_TERRAFORM_LABEL"
                    icon = "icons/building/terraform.png"
                    parameters = [
                        tag = "planet" data = Target.ID
                        tag = "planettype" data = "PT_@6@"
                        tag = "environment" data = Target.ID
                    ]
                    empire = Source.Owner
            ]

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

BuildingType
    name = "BLD_TERRAFORM_BEST"
    description = "BLD_TERRAFORM_BEST_DESC"
    buildcost = 100 * (1 - 0.4 * Statistic If condition = And [Source EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_TERRAFORMING"])
                    * Target.HabitableSize
                      // calculating multiple steps: stepwise-cost(A,B) = 1+2+..+N == (1+N)*N/2  iff difference between A and B is N
                      // if one uses cost(OrigType, BestType) it completely ignores if you changed the type inbetween
                      // if one uses cost(CurrentType, BestType) the cost growth is too little (at least) if CurrentType is between Orig and Best on the wheel
                      // if one uses cost(OrigType, BestType) - cost(OrigType, CurrentType) you get a lot of corner cases (depending where the three types are in relation to another on the wheel)
                      //    a) CurrentType is somewhere on the way from Orig to Best: cost(OrigType, BestType) - cost(OrigType, CurrentType) should be right
                      //    b) CurrentType is not on the way from Orig to Best: ??? actually dont know - one could go back to Orig, so: cost(OrigType, BestType) + cost(OrigType, CurrentType)
                      //                                                        or one could go around the other direction: other_cost(OrigType, BestType) - other_cost(OrigType, CurrentType)
                      //                                                        also there could be a different best planet closest as well
                      // aggregating a sum stepwise with the right step formula would be straightforward and correct, but needs better backend support
                      // so we only support the standard case (i.e. CurrentType == OrigType) 100% and make sure the cost is not too low in other cases by taking a maximum
                    * max(1, [[AGGREGATED_STEPS_FROM_TO(Target.OriginalType,Target.NextBestPlanetType)]], [[AGGREGATED_STEPS_FROM_TO(Target.PlanetType,Target.NextBestPlanetType)]])
                      // Giving a discount to amend opportunity cost by not multi-stepping
                      // per extra step 10%, fast linear formula
                    * (1 - 0.1 * ((PlanetTypeDifference from = Target.PlanetType to = Target.NextBestPlanetType) - 1))
                    * [[BUILDING_COST_MULTIPLIER]]
                    * (1 + (2 * Statistic If condition = And [Source EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_ENVIRONMENTALISM"]))
    buildtime =  (PlanetTypeDifference from = Target.PlanetType to = Target.NextBestPlanetType)
                    * (12 - (6 * Statistic If condition = And [Source EmpireHasAdoptedPolicy empire = Source.Owner name = "PLC_TERRAFORMING"]))
                    // Keep the division operation last in order to keep rounding to integer errors at minimum
                    * (100 - 10 * ((PlanetTypeDifference from = Target.PlanetType to = Target.NextBestPlanetType) - 1)) / 100
    location = And [
        Planet
        OwnedBy empire = Source.Owner
        TargetPopulation low = 1
        Not Planet type = [Asteroids GasGiant]
        Not Planet environment = [Uninhabitable Good]
        Not Contains Building name = "BLD_GAIA_TRANS"
        Not Enqueued type = Building name = "BLD_GAIA_TRANS"
        Not HasSpecial name = "GAIA_SPECIAL"
        // no targeted terraforming buildings contained or enqueued
        [[FOR_ALL_TERRAFORM_PLANET_TYPES(Not Contains Building name = "BLD_TERRAFORM_,")]]
        [[FOR_ALL_TERRAFORM_PLANET_TYPES(Not Enqueued type = Building name = "BLD_TERRAFORM_,")]]
        // Hide building if the species can not improve (Check species' highest available target environment tag)
        Not HasTag name = "NO_TERRAFORM"
        (LocalCandidate.PlanetType != LocalCandidate.NextBestPlanetType)
    ]
    enqueuelocation = [[ENQUEUE_BUILD_ONE_PER_PLANET]]
    effectsgroups = [
        EffectsGroup
            scope = And [
                Object id = Source.PlanetID
                Planet
            ]
            effects = [
                SetPlanetType type = Target.NextBestPlanetType
                GenerateSitRepMessage
                    message = "EFFECT_TERRAFORM"
                    label = "EFFECT_TERRAFORM_LABEL"
                    icon = "icons/building/terraform.png"
                    parameters = [
                        tag = "planet" data = Target.ID
                        tag = "planettype" data = Target.ID
                        tag = "environment" data = Target.ID
                    ]
                    empire = Source.Owner
            ]

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


// @1@ from planet type
// @2@ to planet type
AGGREGATED_STEPS_FROM_TO
'''((1 + PlanetTypeDifference from = @1@ to = @2@) * (PlanetTypeDifference from = @1@ to = @2@) / 2 )'''

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