
@plnmg_t0_jobs = 500
#icons are temp
# effect set_global_flag = planetmega_pret1_test #effect add_situation_progress = 10000

building_plnmg_t0_lab = {
    icon = building_research_lab_3
    category = research
    building_sets = { government urban research }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_lab }

    potential = { planetmega_pret1_visible = yes }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = { has_plnmg_any_lab = yes }
    prerequisites = { "tech_basic_science_lab_3" }
    show_tech_unlock_if = { planetmega_pret1_visible = yes }

    planet_modifier = {
        planet_jobs_physics_research_produces_mult = 0.05
        planet_jobs_society_research_produces_mult = 0.05
        planet_jobs_engineering_research_produces_mult = 0.05
    }
    inline_script = { script = "jobs/researchers_add" AMOUNT = 150 }

    inline_script = buildings/on_all_wilderness_buildings_districts
    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = {
            physics_research = 15
            engineering_research = 15
            society_research = 15
        }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_lab = {
    icon = building_institute
    category = research
    building_sets = { government urban research }
    inline_script = buildings/plnmg_t1temp_general

    potential = { is_building_plnmg_any_lab = no }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            has_technology = tech_global_research_initiative
        }
    }

    inline_script = buildings/on_all_wilderness_buildings_districts
    on_built = {
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_great_laboratory
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_lab }
    }
}

building_plnmg_t0_forge = {
    icon = building_foundry_3
    category = manufacturing
    building_sets = { government urban foundry industrial }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_forge }

    potential = { planetmega_pret1_visible = yes }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = { has_plnmg_any_forge = yes }
    prerequisites = { "tech_alloys_2" }
    show_tech_unlock_if = { planetmega_pret1_visible = yes }

    planet_modifier = { planet_jobs_alloys_produces_mult = 0.05 }
    inline_script = { script = "jobs/foundry_add" AMOUNT = @plnmg_t0_jobs }

    inline_script = buildings/on_all_wilderness_buildings_districts
    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { alloys = 10 }
        upkeep = {
            trigger = { owner? = { is_catalytic_empire = no } }
            minerals = 10
        }
        upkeep = {
            trigger = { owner? = { is_catalytic_empire = yes } }
            food = 10
        }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_forge = {
    icon = building_nano_forge
    category = manufacturing
    building_sets = { government urban foundry industrial }
    inline_script = buildings/plnmg_t1temp_general

    potential = {
        is_building_plnmg_any_forge = no
        owner? = { is_wilderness_empire = no }
    }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            has_technology = tech_advanced_metallurgy_2
        }
    }

    on_built = {
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_advanced_forge
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_forge }
    }
}

#unity building is waiting...

building_plnmg_t0_energy = {
    icon = building_energy_nexus
    category = resource
    building_sets = { government urban generator generator_automation fallen_empire_generator }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_energy }

    potential = { planetmega_pret1_visible = yes }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = { has_plnmg_any_generator = yes }
    prerequisites = { "tech_power_plant_3" }
    show_tech_unlock_if = { planetmega_pret1_visible = yes }

    planet_modifier = { planet_jobs_energy_produces_mult = 0.05 }
    inline_script = { script = "jobs/technicians_add" AMOUNT = @plnmg_t0_jobs }

    inline_script = buildings/on_all_wilderness_buildings_districts
    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { energy = 25 }
    }
}
building_plnmg_t1_energy = {
    icon = building_generator_districts_4
    category = resource
    building_sets = { government urban foundry generator generator_automation fallen_empire_generator }
    inline_script = buildings/plnmg_t1temp_general

    potential = {
        is_building_plnmg_any_generator = no
        owner? = { is_wilderness_empire = no }
    }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            has_technology = tech_power_plant_4
            has_technology = tech_power_hub_2
        }
    }

    on_built = {
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_giant_power_battery
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_energy }
    }
}

building_plnmg_t0_commerce = {
    icon = building_commercial_megaplex
    category = trade
    building_sets = { government urban trade }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_commerce }

    potential = {
        planetmega_pret1_visible = yes
        owner? = { is_gestalt = no }
    }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = {
        or = {
            has_plnmg_any_commerce = yes
            owner? = { is_gestalt = yes }
        }
    }
    prerequisites = { "tech_colonial_bureaucracy" }
    show_tech_unlock_if = {
        planetmega_pret1_visible = yes
        is_gestalt = no
    }
    convert_to = { building_plnmg_t0_logistics }

    planet_modifier = {
        planet_jobs_trade_produces_mult = 0.05
        job_trader_add = @plnmg_t0_jobs
    }
    triggered_planet_modifier = {
        potential = { owner? = { has_corporate_government = yes } }
        planet_jobs_trade_produces_mult = 0.05
        job_bureaucrat_add = 200
        job_politician_add = 100
    }

    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { trade = 20 }
        produces = {
            trigger = { owner? = { has_corporate_government = yes } }
            trade = 10
        }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_commerce = {
    icon = building_fe_market_2
    category = trade
    building_sets = { government urban trade }
    inline_script = buildings/plnmg_t1temp_general

    potential = { is_building_plnmg_any_commerce = no }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            has_technology = tech_galactic_markets
            #has_technology = tech_consumer_good_refinement_2
        }
    }

    on_built = {
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_commerce }
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_commercial_shikara #building_logistics_shikara
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
    }
}

building_plnmg_t0_logistics = {
    icon = building_commercial_megaplex
    category = trade
    building_sets = { government urban trade }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_commerce }

    potential = {
        planetmega_pret1_visible = yes
        owner? = {
            is_gestalt = yes
            is_wilderness_empire = no
        }
    }
    allow = { planetmega_pret1_allowed = yes }
    destroy_trigger = {
        or = {
            has_plnmg_any_commerce = yes
            owner? = { is_gestalt = no }
        }
    }
    prerequisites = { "tech_colonial_bureaucracy" }
    show_tech_unlock_if = {
        planetmega_pret1_visible = yes
        is_gestalt = yes
        is_wilderness_empire = no
    }
    convert_to = { building_plnmg_t0_commerce }

    planet_modifier = {
        planet_jobs_trade_produces_mult = 0.05
        job_coordinator_add = @plnmg_t0_jobs
    }

    inline_script = buildings/on_all_wilderness_buildings_districts
    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { trade = 20 }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_logistics = {
    icon = building_fe_market_2
    category = trade
    building_sets = { government urban trade }
    inline_script = buildings/plnmg_t1temp_general

    potential = { is_building_plnmg_any_commerce = no }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            has_technology = tech_doctrine_navy_size_3
        }
    }

    on_built = {
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_logistics }
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_logistics_shikara
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
    }
}

#war building is passed

building_plnmg_t0_factory = {
    icon = building_factory_3
    category = manufacturing
    building_sets = { government urban factory industrial }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_factory }

    potential = {
        planetmega_pret1_visible = yes
        owner = {
            or = {
                has_technology = tech_luxuries_2
                # has_valid_civic = civic_machine_servitor
                has_valid_civic = civic_machine_obsessional_directive
                has_country_flag = need_goods_production_but_not_using
            }
        }
    }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = { has_plnmg_any_factory = yes }
    # show_in_tech = tech_luxuries_2
    # show_tech_unlock_if = { planetmega_pret1_visible = yes }

    planet_modifier = { planet_jobs_consumer_goods_produces_mult = 0.05 }
    inline_script = { script = "jobs/factory_add" AMOUNT = @plnmg_t0_jobs }

    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { consumer_goods = 15 }
        upkeep = {
            energy = 5
            minerals = 15
        }
    }
}
building_plnmg_t1_factory = {
    icon = building_affluence_center
    category = manufacturing
    building_sets = { government urban factory industrial }
    inline_script = buildings/plnmg_t1temp_general

    potential = { is_building_plnmg_any_factory = no }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            or = {
                has_civic = civic_machine_obsessional_directive
                hidden_trigger = { has_country_flag = need_goods_production_but_not_using }
                has_technology = tech_consumer_good_refinement_2
            }
        }
    }

    on_built = {
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_centralized_manufactory
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_factory }
    }
}

building_plnmg_t0_minerals = {
    icon = building_mineral_purification_hub
    category = resource
    building_sets = { government urban mining mining_automation fallen_empire_mining }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_minerals }

    potential = { planetmega_pret1_visible = yes }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = { has_plnmg_any_mine = yes }
    prerequisites = { "tech_mining_2" }
    show_tech_unlock_if = { planetmega_pret1_visible = yes }

    planet_modifier = { planet_jobs_minerals_produces_mult = 0.05 }
    inline_script = { script = "jobs/miners_add" AMOUNT = @plnmg_t0_jobs }

    inline_script = buildings/on_all_wilderness_buildings_districts
    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { minerals = 20 }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_minerals = {
    icon = building_mining_districts_4
    category = resource
    building_sets = { government urban mining mining_automation fallen_empire_mining }
    inline_script = buildings/plnmg_t1temp_general

    potential = {
        is_building_plnmg_any_mine = no
        owner? = { is_wilderness_empire = no }
    }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            has_technology = tech_mining_3
            has_technology = tech_mineral_purification_2
        }
    }

    on_built = {
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_matter_demodulator
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_minerals }
    }
}

#farm is passed

building_plnmg_t0_admin = {
    icon = building_bureaucratic_3
    category = unity
    building_sets = { government urban unity }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_admin }

    potential = {
        planetmega_pret1_visible = yes
        owner? = { is_gestalt = no }
    }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = {
        or = {
            has_plnmg_any_admin = yes
            owner? = { is_gestalt = yes }
        }
    }
    prerequisites = { "tech_colonial_bureaucracy" }
    show_tech_unlock_if = {
        planetmega_pret1_visible = yes
        is_gestalt = no
    }
    convert_to = { building_plnmg_t0_mind }

    planet_modifier = {
        unity_tag_jobs_empire_size_add = -1
        planet_jobs_unity_produces_mult = 0.01
        job_bureaucrat_add = @plnmg_t0_jobs
    }

    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { unity = 15 }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_admin = {
    icon = building_fe_administration_2
    category = unity
    building_sets = { government urban unity }
    inline_script = buildings/plnmg_t1temp_general

    potential = {
        is_building_plnmg_any_admin = no
        not = { has_carrier_flag = cant_upgrade_plnmg_t0_admin }
    }
    allow = {
        owner = {
            is_enabled_building_planetmega = yes
            has_technology = tech_planetmega_administration
        }
    }

    on_built = {
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_admin }
        if = {
            limit = {
                or = {
                    and = {
                        is_capital = yes
                        owner = { not = { any_owned_colony = { has_carrier_flag = built_capital_pm_admin } } }
                    }
                    owner = {
                        count_owned_colony = {
                            limit = {
                                #is_capital = no
                                has_plnmg_any_admin = yes
                            }
                            count < owner.value:sv_plnmg_admin_limit
                        }
                    }
                }
            }
            owner = {
                start_situation = {
                    type = planet_megastructure_construction
                    target = prev
                    effect = {
                        set_situation_flag = building_admin_axis
                        sit_plnmg_t0upg_initializer = yes
                    }
                }
            }
        }
        else = { carrier_event = { id = planetmega.261 } }
    }
}

building_plnmg_t0_mind = {
    icon = building_bureaucratic_3
    category = unity
    building_sets = { government urban unity }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_mind }

    potential = {
        planetmega_pret1_visible = yes
        owner? = { is_gestalt = yes }
    }
    allow = { planetmega_pret1_allowed = yes }
    destroy_trigger = {
        or = {
            has_plnmg_any_admin = yes
            owner? = { is_gestalt = no }
        }
    }
    prerequisites = { "tech_colonial_bureaucracy" }
    show_tech_unlock_if = {
        planetmega_pret1_visible = yes
        is_gestalt = yes
    }
    convert_to = { building_plnmg_t0_admin }

    planet_modifier = {
        unity_tag_jobs_empire_size_add = -1
        planet_jobs_unity_produces_mult = 0.01
        job_coordinator_add = @plnmg_t0_jobs
    }

    inline_script = buildings/on_all_wilderness_buildings_districts
    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { unity = 15 }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_mind = {
    icon = building_fe_administration_2
    category = unity
    building_sets = { government urban unity }
    inline_script = buildings/plnmg_t1temp_general

    potential = {
        is_building_plnmg_any_admin = no
        not = { has_carrier_flag = cant_upgrade_plnmg_t0_admin }
        owner? = { is_wilderness_empire = no }
    }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            if = {
                limit = { hidden_trigger = { is_hive_empire = yes } }
                has_technology = tech_hive_confluence
            }
            else_if = {
                limit = { hidden_trigger = { is_machine_empire = yes } }
                has_technology = tech_micro_replicators
                has_technology = tech_galactic_bureaucracy
                has_technology = tech_hypercomms_forum
            }
        }
    }

    on_built = {
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_admin }
        if = {
            limit = {
                or = {
                    and = {
                        is_capital = yes
                        owner = { not = { any_owned_colony = { has_carrier_flag = built_capital_pm_admin } } }
                    }
                    owner = {
                        count_owned_colony = {
                            limit = {
                                #is_capital = no
                                has_plnmg_any_admin = yes
                            }
                            count < owner.value:sv_plnmg_admin_limit
                        }
                    }
                }
            }
            owner = {
                start_situation = {
                    type = planet_megastructure_construction
                    target = prev
                    effect = {
                        set_situation_flag = building_primary_mind_pyramid
                        sit_plnmg_t0upg_initializer = yes
                    }
                }
            }
        }
        else = { carrier_event = { id = planetmega.261 } }
    }
}

building_plnmg_t0_housing = {
    icon = building_paradise_dome
    category = amenity
    building_sets = { government urban mining mining_automation fallen_empire_mining }
    inline_script = buildings/plnmg_pret1_general
    upgrades = { building_plnmg_t1_minerals }

    potential = {
        planetmega_pret1_visible = yes
        owner? = { is_wilderness_empire = no }
    }
    allow = {
        planetmega_pret1_allowed = yes
        is_special_colony_type = no
    }
    destroy_trigger = { has_plnmg_any_house = yes }
    prerequisites = { "tech_housing_1" }
    show_tech_unlock_if = {
        planetmega_pret1_visible = yes
        owner? = { is_wilderness_empire = no }
    }

    planet_modifier = {
        planet_housing_add = @plnmg_house_improved_add
        planet_amenities_add = @plnmg_house_improved_add
    }
    triggered_planet_modifier = {
        potential = { owner? = { has_civic = civic_machine_servitor } }
        planet_housing_add = 50
        planet_amenities_add = 50
        mult = value:sv_count_plnmg_jobs|job|bio_trophy|
    }
    triggered_planet_modifier = {
        potential = { owner? = { has_civic = civic_machine_servitor } }
        #job_plnmg_mobile_storage_add = 150 # this job is WIP
        job_bio_trophy_add = @plnmg_house_improved_add
    }

    resources = {
        category = planet_buildings
        cost = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
        }
        produces = { minerals = 20 }
        upkeep = { energy = 5 }
    }
}
building_plnmg_t1_housing = {
    icon = building_fe_dome
    category = amenity
    building_sets = { government urban mining mining_automation fallen_empire_mining }
    inline_script = buildings/plnmg_t1temp_general

    potential = {
        is_building_plnmg_any_house = no
        owner? = { is_wilderness_empire = no }
    }
    allow = {
        hidden_trigger = { exists = owner }
        owner = {
            is_enabled_building_planetmega = yes
            if = {
                limit = { hidden_trigger = { is_machine_empire = yes } }
                has_technology = tech_micro_replicators
            }
            else_if = {
                limit = { hidden_trigger = { is_hive_empire = yes } }
                has_technology = tech_housing_2
            }
            else = { has_technology = tech_paradise_dome }
        }
    }

    on_built = {
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = {
                    set_situation_flag = building_ecodome_complex
                    sit_plnmg_t0upg_initializer = yes
                }
            }
        }
        carrier = { downgrade_buildings_of_type = building_plnmg_t1_housing }
    }
}
