namespace = rg_cluster
# Special project completed (overwrite)
country_event = {
	id = rg_cluster.2
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		set_country_flag = lcluster_opener
		set_global_flag = l_cluster_opened
		every_megastructure = {
			limit = {
				is_megastructure_type = lgate_base
				NOT = {
					has_megastructure_flag = lgate_activated
				}
			}
			activate_gateway = this
			set_megastructure_flag = lgate_activated
		}
		set_global_flag = lgates_activated_globally
		# centralized handling of chain and project clean-up
		every_playable_country = {
			if = {
				limit = {
					is_researching_technology = tech_lgate_activation
				}
				give_technology = {
					tech = tech_lgate_activation
					message = no
				}
			}
		}
		every_playable_country = {
			limit = {
				NOR = {
					has_event_chain = l_cluster_chain
					has_special_project = LCLUSTER_PROJECT
					is_same_value = root
				}
			}
			country_event = {
				id = distar.10954
				days = 1
			}
		}
		every_playable_country = {
			limit = {
				NOR = {
					has_special_project = LCLUSTER_PROJECT
					is_same_value = root
				}
				has_event_chain = l_cluster_chain
			}
			country_event = {
				id = distar.10953
				days = 1
			}
		}
		observer_event = {
			id = observer.62
		}
		every_playable_country = {
			if = {
				limit = {
					has_modifier = rg_distant_stars_modifier
				}
				remove_modifier = rg_distant_stars_modifier
			}
		}
		country_event = {
			id = rg_cluster.10
		}
	}
}

# Allow all nanite worlds to be terraformed
country_event = {
	id = rg_cluster.3
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		every_galaxy_planet = {
			limit = {
				is_planet_class = pc_gray_goo
			}
			add_modifier = {
				modifier = "terraforming_candidate"
				days = -1
			}
		}
	}
}

# Move home system
country_event = {
	id = rg_cluster.9
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes
	immediate = {
		random_system = {
			limit = {
				has_star_flag = lcluster4
				NOT = {
					has_star_flag = lcluster_lgate
				}
			}
			save_event_target_as = rg_cluster_home_system
			random_system_planet = {
				limit = {
					has_planet_flag = start_rg_cluster_gatebuilders
				}
				save_event_target_as = rg_cluster_home_planet
			}
			random_system_planet = {
				limit = {
					has_planet_flag = start_gatebuilders_mining_station1
				}
				save_event_target_as = start_gatebuilders_mining_station_target1
			}
		}
		capital_scope = {
			save_event_target_as = rg_cluster_previous_home_planet
			solar_system = {
				save_event_target_as = rg_cluster_previous_home_system
				spawn_megastructure = {
					type = lgate_base
					orbit_angle = 225
					orbit_distance = 30
				}
			}
			if = {
				limit = {
					exists = sector
				}
				sector = {
					leader = {
						unassign_leader = this
						save_event_target_as = rg_cluster_sector_gov
					}
				}
			}
			else = {
				root = {
					random_owned_leader = {
						limit = {
							leader_class = governor
						}
						save_event_target_as = rg_cluster_sector_gov
					}
				}
			}
		}
		# Spaceport setup
		if = {
			limit = {
				is_gestalt = yes
			}
			event_target:rg_cluster_home_system = {
				create_starbase = {
					size = "starbase_starport"
					module = "shipyard"
					module = "solar_panel_network"
					building = "crew_quarters"
					owner = root
				}
			}
		}
		else = {
			event_target:rg_cluster_home_system = {
				create_starbase = {
					size = "starbase_starport"
					module = "shipyard"
					module = "trading_hub"
					building = "crew_quarters"
					owner = root
				}
			}
		}
		event_target:start_gatebuilders_mining_station_target1 = {
			create_mining_station = {
				owner = root
			}
		}
		# Planet setup
		event_target:rg_cluster_home_planet = {
			change_pc = pc_gaia
			set_owner = root
			set_controller = root
			set_capital = yes
			set_sector_capital = yes
			sector = {
				assign_leader = event_target:rg_cluster_sector_gov
			}
			set_name = random
			generate_start_buildings_and_districts = yes
		}
		if = {
			limit = {
				owner = {
					is_machine_empire = no
				}
			}
			every_owned_pop = {
				modify_species = {
					species = this
					ideal_planet_class = pc_nuked
				}
				species = {
					save_event_target_as = target_species
				}
			}
			root = {
				change_dominant_species = {
					species = event_target:target_species
					change_all = yes
				}
			}
		}
		event_target:rg_cluster_home_system = {
			random_system_planet = {
				limit = {
					is_star = yes
				}
				save_event_target_as = rg_cluster_home_system_loc
			}
		}
		every_owned_fleet = {
			limit = {
				any_owned_ship = {
					OR = {
						is_ship_size = corvette
						is_ship_size = constructor
						is_ship_size = science
					}
				}
			}
			set_location = event_target:rg_cluster_home_system_loc
		}
		event_target:rg_cluster_previous_home_planet = {
			# move pops
			every_owned_pop = {
				resettle_pop = {
					pop = this
					planet = event_target:rg_cluster_home_planet
				}
				unemploy_pop = yes
			}
			remove_planet = yes
		}
		event_target:rg_cluster_home_planet = {
			every_owned_pop = {
				unemploy_pop = yes
				clear_pop_category = yes
			}
			check_planet_employment = yes
		}
		event_target:rg_cluster_previous_home_system = {
			set_name = random
			random_system_planet = {
				limit = {
					is_star = yes
				}
				set_name = solar_system
			}
			remove_star_flag = sol
			remove_star_flag = deneb_system
		}
		event_target:rg_cluster_previous_home_system = {
			every_fleet_in_system = {
				delete_fleet = this
			}
		}
		owner_species = {
			set_species_homeworld = event_target:rg_cluster_home_planet
		}
		every_owned_pop_species = {
			set_species_homeworld = event_target:rg_cluster_home_planet
		}
		country_event = {
			id = rg_cluster.3
			days = -1
		}
	}
}

# L-Cluster Opened
country_event = {
	id = rg_cluster.10
	is_triggered_only = yes
	title = "rg_cluster.10.name"
	desc = {
		text = "rg_cluster.10.desc.a"
		trigger = {
			NOR = {
				has_origin = origin_rg_gray
			}
		}
	}
	desc = {
		text = "rg_cluster.10.desc.b"
		trigger = {
			OR = {
				has_origin = origin_rg_gray
			}
		}
	}
	picture = GFX_evt_l-gateway
	show_sound = event_mystic_reveal
	location = event_target:candidate_gateway
	immediate = {
		every_playable_country = {
			limit = {
				OR = {
					has_origin = origin_rg_gray
				}
				NOT = {
					is_same_value = root
				}
			}
			country_event = {
				id = rg_cluster.11
			}
		}
		every_playable_country = {
			limit = {
				has_special_project = RG_LCLUSTER_PROJECT
				NOT = {
					is_same_value = root
				}
			}
			country_event = {
				id = rg_cluster.12
			}
		}
		every_playable_country = {
			limit = {
				NOR = {
					has_event_chain = l_cluster_chain
					has_special_project = RG_LCLUSTER_PROJECT
					is_same_value = root
				}
			}
			country_event = {
				id = rg_cluster.13
			}
		}
		every_playable_country = {
			limit = {
				NOR = {
					has_special_project = RG_LCLUSTER_PROJECT
					is_same_value = root
				}
				has_event_chain = l_cluster_chain
			}
			country_event = {
				id = rg_cluster.12
			}
		}
		observer_event = {
			id = observer.62
		}
	}
	option = {
		trigger = {
			NOR = {
				has_origin = origin_rg_gray
			}
		}
		name = "rg_cluster.10.a"
	}
	option = {
		trigger = {
			OR = {
				has_origin = origin_rg_gray
			}
		}
		name = "rg_cluster.10.b"
	}
}

# Inform the Gatebuilders of the L-Cluster being open
country_event = {
	id = rg_cluster.11
	is_triggered_only = yes
	title = "rg_cluster.11.name"
	desc = "rg_cluster.11.desc"
	picture = GFX_evt_l-gateway
	show_sound = event_mystic_reveal
	immediate = {
		if = {
			limit = {
				has_special_project = RG_LCLUSTER_PROJECT
			}
			abort_special_project = {
				type = RG_LCLUSTER_PROJECT
			}
		}
		else_if = {
			limit = {
				has_event_chain = l_cluster_chain
			}
			liquidate_clues = yes
			end_event_chain = "l_cluster_chain"
		}
	}
	option = {
		name = "rg_cluster.11.a"
	}
}

# Inform other empires of the L-Cluster being open (has project or event chain)
country_event = {
	id = rg_cluster.12
	is_triggered_only = yes
	title = "rg_cluster.11.name"
	desc = "rg_cluster.12.desc"
	picture = GFX_evt_l-gateway
	show_sound = event_mystic_reveal
	immediate = {
		if = {
			limit = {
				has_special_project = RG_LCLUSTER_PROJECT
			}
			abort_special_project = {
				type = RG_LCLUSTER_PROJECT
			}
		}
		else_if = {
			limit = {
				has_event_chain = l_cluster_chain
			}
			liquidate_clues = yes
			end_event_chain = "l_cluster_chain"
		}
	}
	option = {
		name = "rg_cluster.12.a"
	}
}

# Inform other empires of the L-Cluster being open (has nothing)
country_event = {
	id = rg_cluster.13
	is_triggered_only = yes
	title = "rg_cluster.11.name"
	desc = "rg_cluster.13.desc"
	picture = GFX_evt_l-gateway
	show_sound = event_mystic_reveal
	option = {
		name = "rg_cluster.13.a"
	}
}
