# see README.txt for example

gateway_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	build_type = outside_gravity_well						# indicates this will use "free" placement between the system's inner and outer ring
															# if this option is active, the placement_rules will be completely ignored
	show_galactic_map_icon = no
	show_in_outliner = yes
	overlord_can_upgrade = yes
	build_time = 1080
	resources = {
		category = megastructures_gateways
		cost = {
			influence = 75
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 2500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 2500
			mult = @halved_alloy_to_food_cost_ratio
		}
	}

	construction_blocks_and_blocked_by = self_type
	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_OUTSIDE_GRAVITY_WELL"

	dismantle_cost = {
		category = megastructures_hyper_relay
		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
			}
		}
	}

	potential = {
		has_technology = tech_gateway_construction
	}

	possible = {
		if = {
			limit = {
				from = { is_nomadic = no }
			}
			hidden_trigger = {
				exists = starbase
			}
			custom_tooltip = {
				fail_text = "requires_inside_border_or_subject"
				OR = {
					is_inside_border = from
					AND = {
						exists = owner
						owner = {
							is_subject = yes
							overlord = { is_same_value = from }
						}
					}
					AND = {
						from = { has_menace_perk = menp_defender_galcom_2 }
						exists = solar_system
						exists = solar_system.owner
						solar_system.owner = {
							OR = {
								has_defensive_pact = from
								is_in_federation_with = from
								from = { has_subject = prev }
							}
						}
					}
				}
			}
		}
		else = {
			custom_tooltip = {
				fail_text = requires_tier_2_waystation_on_wayline
				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
							}
						}
					}
					AND = {
						from = { has_menace_perk = menp_defender_galcom_2 }
						exists = solar_system
						exists = solar_system.owner
						solar_system.owner = {
							OR = {
								has_defensive_pact = from
								is_in_federation_with = from
								from = { has_subject = prev }
							}
						}
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system" # or is in my subject's territory
			OR = {
				NOT = {
					any_system_planet = {
						is_surveyed = {
							who = prev.from
							status = no
						}
					}
				}
				any_starbase_in_system = {
					is_waystation_starbase = yes
					owner? = {
						is_same_value = from
					}
				}
				AND = {
					exists = owner
					owner = {
						is_subject = yes
						overlord = { is_same_value = from }
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_gateway"
			NOR = {
				has_megastructure = gateway_0
				has_megastructure = gateway_ruined
				has_megastructure = gateway_restored
				has_megastructure = gateway_final
			}
		}
		custom_tooltip = {
			fail_text = "requires_technology_gateway_construction"
			from = { has_technology = tech_gateway_construction }
		}
	}

	placement_rules = {			# those would be ignored since the gateway is being "free-placed" between the inner and outer radius of the system
	}

	# root = system
	# from = country
	ai_weight = {
		factor = 5

		modifier = {
			factor = 0
			NOT = {
				starbase? = {
					has_starbase_size >= starbase_starfortress
				}
			}
		}

		modifier = {
			factor = 0
			any_neighbor_system = {
				OR = {
					has_megastructure = gateway_0
					has_megastructure = gateway_ruined
					has_megastructure = gateway_restored
					has_megastructure = gateway_final
				}
			}
		}
	}

	on_build_start = {}
	on_build_cancel = {}
	on_build_complete = {
		from = { country_event = { id = apoc.14 } }
	}
}

gateway_final = {
	entity = "gateway_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	# build_type = outside_gravity_well						# indicates this will use "free" placement between the system's inner and outer ring
															# if this option is active, the placement_rules will be completely ignored
	show_galactic_map_icon = no
	show_in_outliner = no
	potential = {
		has_technology = tech_gateway_activation
	}
	possible = {
		custom_tooltip = {
			fail_text = "requires_technology_gateway_construction"
			from = { has_technology = tech_gateway_construction }
		}
	}
	upgrade_from = {
		gateway_0
	}
	build_time = 1800
	resources = {
		category = megastructures_gateways
		cost = {
			energy = 6000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 2500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 2500
			mult = @halved_alloy_to_food_cost_ratio
		}
	}
	construction_blocks_and_blocked_by = self_type

	dismantle_cost = {
		category = megastructures_hyper_relay
		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
			}
		}
	}


	bypass_type = gateway
	on_build_complete = {
		activate_gateway = fromfrom
		if = {
			limit = { num_active_gateways = 1 }
			random_megastructure = {
				limit = { is_megastructure_type = gateway_ruined }
				if = {
					limit = { exists = solar_system.starbase }
					solar_system.starbase.owner = { set_country_flag = did_not_repair_gateway }
				}
				upgrade_megastructure_to = gateway_restored
				finish_upgrade = yes
			}
		}
		from = {
			country_event = { id = apoc.13 }
			country_event = { id = apoc.11 }
		}
	}
}

# Disabled Gateway
gateway_ruined = {
	entity = "deactivated_gateway_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	build_type = outside_gravity_well
	bypass_type = gateway
	show_galactic_map_icon = no
	show_in_outliner = yes
	nomad_reactivatable = yes

	construction_blocks_and_blocked_by = self_type

	potential = {
		always = no
	}
}

# Restored Gateway
gateway_restored = {
	entity = "reactivated_gateway_entity"
	construction_entity = "gateway_portal_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	# build_type = outside_gravity_well
	show_galactic_map_icon = no
	show_in_outliner = no

	potential = {
		has_technology = tech_gateway_activation
	}

	possible = {
		custom_tooltip = {
			fail_text = "requires_technology_gateway_activation"
			from = { has_technology = tech_gateway_activation }
		}
	}
	upgrade_from = {
		gateway_ruined
	}
	build_time = 720
	resources = {
		category = megastructures_gateways
		cost = {
			energy = 6000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 2500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 2500
			mult = @halved_alloy_to_food_cost_ratio
		}
	}
	construction_blocks_and_blocked_by = self_type

	dismantle_cost = {
		category = megastructures_hyper_relay
		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
			}
		}
	}

	bypass_type = gateway
	on_build_complete = {
		activate_gateway = fromfrom
		if = {
			limit = { num_active_gateways = 1 }
			random_megastructure = {
				limit = { is_megastructure_type = gateway_ruined }
				if = {
					limit = { exists = solar_system.starbase }
					solar_system.starbase.owner = { set_country_flag = did_not_repair_gateway }
				}
				upgrade_megastructure_to = gateway_restored
				finish_upgrade = yes
			}
		}
		if = {
			limit = {
				has_paragon_dlc = yes
				exists = from  #does not always exist when upgraded from script via "finish_upgrade = yes"
				from = {
					is_ai = no
				}
				NOT = {
					has_global_flag = gateway_dude_found
				}
			}
			from = {
				random_list = {
					20 = { #spawn chance
						set_country_flag = sharpbeak_known
						prev = { set_star_flag = sharpbeak_starsystem }
						fromfrom = {
							save_global_event_target_as = paragon_gateway_dude_homegate
							set_megastructure_flag = paragon_gateway_dude_homegate_flag
						}
						country_event = {
							id = paragon.3000
							days = 5
						}
						set_global_flag = gateway_dude_found
						set_global_flag = sharpbeak_recruit_phase
						modifier = {
							factor = 4
							OR = {
								has_country_flag = gateway_reactivated
								has_country_flag = gateway_built
								has_country_flag = found_skravird
							}
						}
						modifier = {
							factor = 2
							has_origin = origin_galactic_doorstep
						}
					}
					80 = {} # no luck
				}
				country_event = { id = origin.1050 }
				country_event = { id = apoc.12 }
				country_event = { id = apoc.10 }
			}
		}
	}
}

# Derelict Gateway - Imperial Fiefdom Unique
gateway_derelict = {
	entity = "gateway_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	build_type = outside_gravity_well
	show_galactic_map_icon = no
	show_in_outliner = yes

	bypass_type = gateway

	potential = {
		always = no
	}

	construction_blocks_and_blocked_by = self_type

	on_build_complete = {
		activate_gateway = fromfrom
	}
}



# Disabled Gateway linked to Ness System
gateway_ness_ruined = {
	entity = "deactivated_gateway_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	build_type = outside_gravity_well
	bypass_type = gateway_ness
	show_galactic_map_icon = no
	show_in_outliner = yes
	nomad_reactivatable = yes

	construction_blocks_and_blocked_by = self_type

	potential = {
		always = no
	}
}

# Gateway in Captain Ness treasure system
gateway_ness_restored = {
	entity = "reactivated_gateway_entity"
	construction_entity = "gateway_portal_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	show_galactic_map_icon = no
	show_in_outliner = no

	potential = {
		has_technology = tech_gateway_activation
	}

	upgrade_from = {
		gateway_ness_ruined
	}
	build_time = 720
	resources = {
		category = megastructures_gateways
		cost = {
			energy = 6000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 2500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 2500
			mult = @halved_alloy_to_food_cost_ratio
		}
	}
	construction_blocks_and_blocked_by = self_type
	dismantle_potential = { always = no }

	bypass_type = gateway_ness
	on_build_complete = {
		activate_gateway = fromfrom
		fromfrom = { save_event_target_as = ness_gate }
		if = {
			limit = {
				has_grand_archive_dlc = yes
				exists = from
				from = {
					has_origin = origin_treasure_hunters
					has_special_project = ACTIVATE_GATEWAY_PROJECT
				}
				fromfrom = { has_megastructure_flag = gateway_to_ness_for@root.from }
			}
			from = {
				if = {
					limit = { has_special_project = ACTIVATE_GATEWAY_PROJECT }
					complete_special_project = {
						type = "ACTIVATE_GATEWAY_PROJECT"
						location = from
					}
				}
				spawn_ness_treasure_system = yes
				country_event = {
					id = grand_archive.8400
					scopes = { from = root.fromfrom }
				}
			}
		}
		if = {
			limit = {
				any_playable_country = {
					has_origin = origin_treasure_hunters
					is_nomadic = yes
					has_country_flag = activating_ness_gate
				}
			}
			random_playable_country = {
				limit = {
					has_origin = origin_treasure_hunters
					is_nomadic = yes
					has_country_flag = activating_ness_gate
				}
				spawn_ness_treasure_system = yes
				country_event = {
					id = grand_archive.8400
					scopes = { from = event_target:ness_gate}
				}

			}
		}
	}
}
