
# extra galaxy gateway
planetmega_eg_gateway_0 = {
    entity = "construction_platform_entity"
    construction_entity = "construction_platform_entity"
    portrait = "GFX_megastructure_construction_background"
    place_entity_on_planet_plane = no
    entity_offset = { x = 15 y = -5 }
    outliner_trigger = { owner? = { has_technology = tech_planetmega_transfer_prereq } }

    build_time = 1440
    resources = {
        category = megastructures
        cost = {
            #trigger: this=country
            alloys = 5000
            influence = 100
        }
        cost = {
            trigger = { country_uses_bio_ships = yes }
            alloys = -2500
            food = 4000
        }
        # prod & upkp: this=megastructure, from=country
    }

    construction_blocks_and_blocked_by = self_type
    custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_PLNMG_EG_GATEWAY"

    dismantle_cost = {
        category = megastructures_gateways
        cost = { energy = 2000 }
    }
    dismantle_time = 180
    dismantle_potential = { is_upgrading = no }
    dismantle_possible = { can_dismantle_megastructure = { TECH = tech_gateway_construction } }
    on_dismantle_complete = {
        from = {
            add_resource = {
                alloys = 2000
                mult = modifier:megastructure_dismantle_refund_mult
            }
        }
    }

    potential = {
        is_ai = no
        has_technology = tech_planetmega_transfer_prereq
    }

    possible = {
        has_plnmg_eg_gateway = no
        custom_tooltip = {
            fail_text = requires_technology_gateway_construction
            from = { has_technology = tech_gateway_construction }
        }
        #inherited from megastructures/system_ownership_or_waystation_check
        if = {
            limit = { from = { is_nomadic = no } }
            hidden_trigger = { exists = starbase }
            custom_tooltip = {
                fail_text = "requires_inside_border"
                is_inside_border = from
            }
        }
        else = {
            custom_tooltip = {
                fail_text = requires_tier_2_waystation_on_wayline
                any_starbase_in_system = {
                    is_waystation_starbase = yes
                    owner? = {
                        is_same_value = from
                    }
                    any_starbase_in_network = {
                        has_starbase_size >= starbase_waystation_2
                        owner? = {
                            is_same_value = from
                        }
                    }
                }
            }
            # or = {
            #     custom_tooltip = {
            #         success_text = no_space_owner
            #         not = { exists = owner }
            #     }
            #     custom_tooltip = {
            #         fail_text = requires_plnmg_eggw_owner_no_colony
            #         not = { any_system_planet_colony = { is_owned_by = root.owner } }
            #     }
            # }
            or = {
                custom_tooltip = {
                    success_text = no_space_owner
                    not = { exists = owner }
                }
                owner = {
                    or = {
                        opinion_level = { who = from level > good }
                        has_overlord = from
                    }
                }
                # custom_tooltip = {
                #     fail_text = default
                #     from = { has_country_flag = can_build_plnmg_eggw_in_@root.owner }
                # }
            }
        }
    }

    placement_rules = {
        planet_possible = {
            # this = planet, from = country
            custom_tooltip = {
                fail_text = "requires_no_existing_megastructure"
                not = { has_planet_flag = megastructure }
            }
            custom_tooltip = {
                fail_text = "requires_no_orbital_debris"
                NOT = { has_planet_flag = has_orbital_debris }
            }
            if = {
                limit = { from = { is_nomadic = yes } }
                is_plnmg_common_planet = yes
            }
            else = {
                #hidden_trigger = { exists = owner }
                is_colony = yes
                nor = {
                    has_ground_combat = yes
                    is_occupied_flag = yes
                }
                custom_tooltip = {
                    fail_text = "requires_colonized_planet_orbital_ring"
                    owner = { is_same_value = from }
                }
                custom_tooltip = {
                    fail_text = requires_plnmg_extra_galaxy
                    or = {
                        has_deposit = d_plnmg_extra_galaxy
                        has_planet_flag = planet_has_plnmg_extra_galaxy
                    }
                }
            }
        }
    }

    ai_weight = { factor = 0 }

    on_build_complete = {
        fromfrom = { save_event_target_as = location_megastructure }
        from = { country_event = { id = planetmega_exp.1 } }
    }
}

planetmega_eg_gateway_final = {
	entity = "gateway_entity"
    entity_offset = { x = 15 y = -5 }
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
    #portrait = "GFX_megastructure_hyper_relay_background"
	place_entity_on_planet_plane = no
    outliner_trigger = { owner? = { has_technology = tech_planetmega_transfer } }

	prerequisites = { tech_planetmega_transfer }
	show_prereqs = yes
	upgrade_from = { planetmega_eg_gateway_0 }
	build_time = 2160
	resources = {
		category = megastructures_gateways
		cost = {
			#alloys = 3500
			energy = 15000
		}
        upkeep = { energy = 5 }
	}
	construction_blocks_and_blocked_by = self_type

    possible = {
        hidden_trigger = { from = { is_ai = no } }
        custom_tooltip = {
            fail_text = "requires_technology_gateway_activation"
            from = { has_technology = tech_gateway_activation }
        }
    }

	dismantle_cost = {
		category = megastructures_gateways
		cost = { energy = 5000 }
	}
	dismantle_time = 360
	dismantle_potential = { is_upgrading = no }
	dismantle_possible = { can_dismantle_megastructure = { TECH = tech_gateway_construction } }
	on_dismantle_complete = {
		from = {
			add_resource = {
				alloys = 5000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}

	on_build_complete = {
		fromfrom = { save_event_target_as = location_megastructure }
		from = { country_event = { id = planetmega_exp.2 } }
	}

    scripted_action = { send_plnmg_exp_fleet_out }
}

# should be spawned with d_plnmg_eg_destroyed
# should be bundled with planet_flag has_plnmg_eg_gateway_ruined
planetmega_eg_gateway_ruined = {
	entity = "deactivated_gateway_entity"
    construction_entity = "gateway_portal_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
    entity_offset = { x = 16 y = -4 }
    show_in_outliner = yes
    outliner_trigger = { owner? = { has_technology = tech_planetmega_transfer_prereq } }
	potential = { always = no }
    upgrade_from = {
    }
}
planetmega_eg_gateway_restored = {
	entity = "reactivated_gateway_entity"
	construction_entity = "gateway_portal_entity"
	portrait = "GFX_megastructure_construction_background"
    #portrait = "GFX_megastructure_hyper_relay_background"
	place_entity_on_planet_plane = no
    entity_offset = { x = 16 y = -4 }
    outliner_trigger = {
        owner? = {
            is_nomadic = no
            has_technology = tech_planetmega_transfer
        }
    }

	possible = {
        #hidden_trigger = { from = { is_ai = no } }
		custom_tooltip = {
			fail_text = planet_not_has_plnmg_eg_deposit
			any_system_planet_colony = {
				is_owned_by = from
				or = {
                    has_deposit = d_plnmg_extra_galaxy
                    has_carrier_flag = planet_has_plnmg_extra_galaxy
                }
			}
		}
        custom_tooltip = {
            fail_text = "requires_technology_gateway_activation"
            from = { has_technology = tech_gateway_activation }
        }
        custom_tooltip = {
            fail_text = planetmega_nomads_wip
            from = { is_nomadic = no }
        }
	}
	upgrade_from = { planetmega_eg_gateway_ruined }
	build_time = 900
	resources = {
		category = megastructures_gateways
		cost = {
			alloys = 3000
			energy = 10000
		}
        cost = {
            trigger = { country_uses_bio_ships = yes }
            alloys = -1500
            food = 2500
        }

        upkeep = { energy = 5 }
	}
	construction_blocks_and_blocked_by = self_type

	dismantle_cost = {
		category = megastructures_gateways
		cost = { energy = 2500 }
	}
	dismantle_time = 360
	dismantle_potential = { always = yes }
	dismantle_possible = { can_dismantle_megastructure = { TECH = tech_gateway_construction } }
	on_dismantle_complete = {
		from = {
			add_resource = {
				alloys = 2500
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}

	on_build_complete = {
		fromfrom = { save_event_target_as = location_megastructure }
		from = { country_event = { id = planetmega_exp.2 } }
	}

    scripted_action = { send_plnmg_exp_fleet_out }
}

planetmega_eg_relay = {
    entity = "hyper_relay_entity"
    construction_entity = "hyper_relay_entity"
    portrait = "GFX_megastructure_hyper_relay_background"
    place_entity_on_planet_plane = no
    show_galactic_map_icon = no
    show_in_outliner = no
    build_time = 540 #360
    build_type = outside_gravity_well

    resources = {
        category = megastructures_hyper_relay
        cost = {
            rare_crystals = 350
            influence = 50
        }
        cost = { alloys = 750 }
        cost = {
            trigger = { country_uses_bio_ships = yes }
            alloys = -250
            food = 900
        }

        upkeep = { energy = 3 }
    }

    country_modifier = {
        plnmg_extra_galaxy_connect_res_mult = 0.01
        plnmg_extra_galaxy_cooldown_mult = -0.02
        planetmega_itss_recharge_cost_mult = -0.01
    }

    construction_blocks_and_blocked_by = self_type
    custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_OUTSIDE_GRAVITY_WELL"

    dismantle_cost = {
        category = megastructures_hyper_relay
        cost = { energy = 1000 }
    }
    dismantle_time = 360
    dismantle_potential = { always = yes }
    dismantle_possible = { can_dismantle_megastructure = { TECH = tech_hyper_relays } }
    on_dismantle_complete = {
        from = {
            add_resource = {
                alloys = 750
                rare_crystals = 350
                mult = modifier:megastructure_dismantle_refund_mult
            }
        }
    }

    prerequisites = { tech_planetmega_transfer_prereq }
    show_prereqs = yes
    potential = { is_ai = no }
    possible = {
        if = {
            limit = { from = { is_nomadic = no } }
            custom_tooltip = {
                fail_text = "requires_inside_border_or_subject"
                owner? = {
                    or = {
                        is_same_value = from
                        has_overlord = from
                        AND = {
                            from = { has_menace_perk = menp_defender_galcom_2 }
                            OR = {
                                has_defensive_pact = from
                                is_in_federation_with = from
                            }
                        }
                    }
                }
            }
        }
        else = {
            custom_tooltip = {
                fail_text = requires_tier_2_waystation_on_wayline_or_subject
                OR = {
                    any_starbase_in_system = {
                        is_waystation_starbase = yes
                        owner? = { is_same_value = from }
                        any_starbase_in_network = {
                            has_starbase_size >= starbase_waystation_2
                            owner? = { is_same_value = from }
                        }
                    }
                    owner? = {
                        or = {
                            has_overlord = from
                            AND = {
                                from = { has_menace_perk = menp_defender_galcom_2 }
                                or = {
                                    has_defensive_pact = from
                                    is_in_federation_with = from
                                }
                            }
                        }
                    }
                }
            }
        }
        custom_tooltip = {
            fail_text = "requires_no_existing_hyper_relay"
            has_plnmg_hyper_relay_like = no
        }
        custom_tooltip = {
            fail_text = requires_not_in_capital_system
            is_capital_system = no
        }
        custom_tooltip = {
            fail_text = requires_planetmega_eg_geteway_somewhere
            not = { has_star_flag = cant_build_plnmg_eg_relay }
        }
    }

    on_build_queued = { check_if_planetmega_eg_relay_enabled = yes }
    on_build_start = { check_if_planetmega_eg_relay_enabled = yes }

    ai_weight = { factor = 0 }
}

