namespace = game_start

### Hidden Game Start Event
event = {
	id = game_start.1
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_global_flag = game_started


		every_country = {
			limit = { is_ai = no }
			country_event = { id = country.900 days = 460 random = 60 }
			country_event = { id = game_start.74 }
		}

		### HUMAN SPAWNS
		if = { #FTP spawner
			limit = {
				has_lithoids = yes
				num_ai_empires_setting >= 2
				any_country = { #First check that UNE has spawned
					has_country_flag = human_1
					is_human_species = yes
					NOT = { has_origin = origin_lost_colony }
					NOT = { has_origin = origin_broken_shackles }
				}
				any_country = { #Then check that FTP isn't around
					NOR = {
						species_portrait = lith_human
						has_country_flag = human_lithoid
					}
				}
			}
			random_list = {
				20 = {
					modifier = {
						any_country = { #If the UNE is spawned and is a human we increase the chance. This means it's only a 20% if you are playing as the CoM
							has_country_flag = human_1
							is_ai = no
						}
						factor = 4
					}
					random_system = {
						limit = {
							has_owner = no
							has_special_star_flag_trigger = no
						}
						spawn_system = { initializer = "ai_titawin_init" }
					}
				}
				80 = {}
			}
		}
		if = { #If someone is playing human lithoids and there is no UNE lets give it a chance to spawn
			limit = {
				any_country = {
					has_country_flag = human_lithoid
					species_portrait = lith_human
				}
				NOR = {
					any_country = {
						has_country_flag = human_1
					}
					any_system = {
						has_star_flag = sol
					}
				}
				num_ai_empires_setting >= 1
			}
			random_list = {
				80 = {
					random_system = {
						limit = {
							has_owner = no
							has_special_star_flag_trigger = no
						}
						spawn_system = { initializer = "ai_sol_system" }
					}
				}
				20 = {}
			}
		}
		if = {
			# If there are no Sols
			limit = {
				NOT = {
					any_system = {
						has_star_flag = sol
					}
				}
			}
			random_list = {
				450 = { }	# No Sol
				400 = {		# Pre-FTLs
					complex_trigger_modifier = {
						trigger = check_galaxy_setup_value
						parameters = {
							setting = primitive_worlds_scale
						}
						mode = factor
					}
					random_system = {
						limit = { has_owner = no }
						spawn_system = { initializer = "pre_ftl_init_sol" }
					}
					add_extra_hyperlane_to_spawned_system_effect = yes
				}
				100 = {		# Tomb World
					complex_trigger_modifier = {
						trigger = check_galaxy_setup_value
						parameters = {
							setting = primitive_worlds_scale
						}
						mode = factor
					}
					random_system = {
						limit = { has_owner = no }
						spawn_system = { initializer = "special_init_04" }
					}
					add_extra_hyperlane_to_spawned_system_effect = yes
				}
				50 = {		# Geocentric Experiment
					complex_trigger_modifier = {
						trigger = check_galaxy_setup_value
						parameters = {
							setting = habitable_worlds_scale
						}
						mode = factor
						mult = 0.4 # e.g. 0.5x habitable = factor of 0.2; 5x habitable worlds = factor of 2
					}
					random_system = {
						limit = { has_owner = no }
						spawn_system = { initializer = "init_sol_geocentric" }
					}
					add_extra_hyperlane_to_spawned_system_effect = yes
				}
			}
		}

		### SPAWN INVISIBLE GLOBAL EVENT COUNTRY
		create_country = {
			name = "global_event_country"
			type = global_event
			effect = {
				save_global_event_target_as = global_event_country
			}
		}

		### SPAWN SPECIAL COUNTRIES
		create_country = {
			name = "NAME_The_Shroud"
			type = shroud
			flag = {
				icon = {
					category = "special"
					file = "the_shroud.dds"
				}
				background= {
					category = "backgrounds"
					file = "00_solid.dds"
				}
				colors={
					"red"
					"black"
					"null"
					"null"
				}
			}
			effect = {
				set_country_flag = suppress_first_contact_comms
				set_country_flag = shroud_country_flag
			}
		}

		create_country = {
			name = "name_animator"
			type = shroud
			flag = {
				icon = {
					category = "zoological"
					file = "flag_zoological_5.dds"
				}
				background= {
					category = "backgrounds"
					file = "flag_BG_12.dds"
				}
				colors={
					"purple"
					"black"
					"null"
					"null"
				}
			}
			effect = {
				set_country_flag = suppress_first_contact_comms
				set_country_flag = animator_of_clay_country_flag
			}
		}

		### HANDLE EMPIRE RELATIONS
		every_country = {
			limit = {
				has_country_flag = fallen_empire_machine
			}
			every_country = {
				limit = {
					is_drone_country_type = yes
				}
				set_faction_hostility = {
					target = prev
					set_hostile = no
					set_neutral = no
					set_friendly = yes
				}
			}
		}

		every_country = {
			limit = {
				is_tiyanki_country_type = yes
			}
			every_country = {
				limit = {
					OR = {
						is_amoeba_country_type = yes
						is_drone_country_type = yes
						is_crystal_country_type = yes
						is_country_type = cloud
					}
				}
				set_faction_hostility = {
					target = prev
					set_hostile = no
					set_neutral = yes
					set_friendly = no
				}
			}
		}

		every_country = {
			limit = { is_voidworm_country = yes }
			every_country = {
				limit = {
					OR = {
						is_amoeba_country_type = yes
						is_tiyanki_country_type = yes
						is_drone_country_type = yes
						is_crystal_country_type = yes
						is_country_type = cloud
						is_guardian_country = yes
					}
				}
				set_faction_hostility = {
					target = prev
					set_hostile = no
					set_neutral = yes
					set_friendly = no
				}
			}
		}

		### MARAUDER FLAGS
		every_country = {
			limit = { is_country_type = dormant_marauders }
			randomize_flag_symbol = "pirate"
		}

		### MARAUDERS SHOULD NOT ATTACK ENCLAVES
		every_country = {
			limit = { is_country_type = enclave }
			every_country = {
				limit = { is_country_type = dormant_marauders }
				set_faction_hostility = {
					target = prev
					set_hostile = no
					set_neutral = no
					set_friendly = yes
				}
			}
		}

		### SLOW DOWN PRE-FTL EVENTS
		set_timed_global_flag = {
			flag = asteroid_sighted_pre_ftl_global
			years = 10
		}
		set_timed_global_flag = {
			flag = nuclear_war_pre_ftl_global
			years = 10
		}

		every_playable_country = {
			add_modifier = { modifier = game_start days = 3600 }
		}

		### ORIGINS

		# Scion
		every_playable_country = {
			limit = { has_origin = origin_scion }
			country_event = { id = origin.5 }
		}

		# Common Ground & Hegemon
		every_playable_country = {
			limit = {
				OR = {
					has_origin = origin_common_ground
					has_origin = origin_hegemon
				}
			}
			country_event = { id = origin.55 }
		}

		# Doomsday
		every_playable_country = {
			limit = { has_origin = origin_doomsday }
			country_event = { id = origin.60 }
		}

		# Shattered Ring
		every_playable_country = {
			limit = { has_origin = origin_shattered_ring }
			country_event = { id = origin.3150 }
		}

		# Slingshot to the Stars
		every_playable_country = {
			limit = { has_origin = origin_star_slingshot }
			country_event = { id = origin.3500 }
		}

		## Lost Colony
		# if advanced starts are enabled or a player has picked it, let it ride
		if = {
			limit = {
				OR = {
					any_playable_country = {
						has_origin = origin_lost_colony
						is_ai = no
					}
					check_galaxy_setup_value = { setting = num_advanced_empires value > 0 }
				}
			}
			every_playable_country = {
				limit = {
					has_origin = origin_lost_colony
					is_human_prescripted_empire = no
				}
				country_event = { id = origin.100 }
			}
		}
		# if an AI has picked it and advanced starts are not enabled,
		# give them default origin instead.
		# also affects lost_colony_1 initializer
		else = {
			every_playable_country = {
				limit = {
					has_origin = origin_lost_colony
					is_human_prescripted_empire = no
				}
				log = "REMOVED LOST COLONY"
				set_origin = origin_default
			}
		}

		# Galactic Doorstep
		every_playable_country = {
			limit = { has_origin = origin_galactic_doorstep }
			country_event = { id = origin.1001 }
		}

		# On the Shoulders of Giants
		every_playable_country = {
			limit = { has_origin = origin_shoulders_of_giants }
			country_event = { id = origin.1199 }
		}

		# Here Be Dragons
		every_playable_country = {
			limit = { has_origin = origin_here_be_dragons }
			country_event = { id = aquatics.100 }
		}

		# Ocean Paradise
		every_playable_country = {
			limit = {
				OR  = {
					has_origin = origin_ocean_paradise
					has_origin = origin_ocean_machines
				}
			}
			country_event = { id = aquatics.1000 }
		}

		# Broken Shackles
		every_playable_country = {
			limit = { has_origin = origin_broken_shackles }
			country_event = { id = origin.110 }
		}

		# Teachers of the Shroud
		every_playable_country = {
			limit = { has_origin = origin_shroudwalker_apprentice }
			country_event = { id = origin.5600 }
		}

		# Imperial Vassal
		if = {
			limit = {
				any_playable_country = {
					has_origin = origin_imperial_vassal
				}
			}
			imperial_origin_start_effect = yes
		}

		# Shroud-Forged
		every_playable_country = {
			limit = { has_origin = origin_shroud_forged }
			country_event = { id = shroud_forged.1 }
		}

		# Payback
		every_playable_country = {
			limit = { has_origin = origin_payback }
			country_event = { id = origin.4000 }
		}

		# Fruitful Partnership
		every_playable_country = {
			limit = { has_origin = origin_fruitful }
			fruitful_start_effect = yes
		}

		# Mindwarden
		every_playable_country = {
			limit = { has_origin = origin_mindwardens }
			country_event = { id = shroud.6000 }
		}

		# Endbringers
		every_playable_country = {
			limit = { has_origin = origin_endbringers }
			country_event = { id = shroud.8000 }
		}

		# Wilderness
		every_playable_country = {
			limit = { has_origin = origin_wilderness }
			country_event = { id = wilderness.150 } # stellar flare
		}

		# Nomads
		every_playable_country = {
			limit = {
				is_nomadic = yes
			}
			nomad_country_initial_setup_effect = yes
			if = {
				limit = {
					has_origin = origin_heirs_of_the_khan
				}
				heirs_of_the_khan_setup_effect = yes
			}
		}

		## Payback/Escaped Slaves Slaver empire
		# if advanced starts are enabled or a player has picked it, continue
		if = {
			limit = {
				OR = {
					any_playable_country = {
						has_slave_origin = yes
						is_ai = no
					}
					check_galaxy_setup_value = { setting = num_advanced_empires value > 0 }
				}
			}
		}
		# if an AI has picked it and advanced starts are not enabled,
		# give them default origin instead.
		else = {
			every_playable_country = {
				limit = {
					has_slave_origin = yes
				}
				log = "REMOVED SLAVE ORIGIN"
				set_origin = origin_default
			}
		}

		# Spawn Broken Shackles for Payback
		every_playable_country = {
			limit = {
				has_origin = origin_payback
			}
			country_event = {
				id = origin.112
			}
		}

		# Spawn Payback for Broken Shackles
		every_playable_country = {
			limit = {
				has_origin = origin_broken_shackles
			}
			country_event = {
				id = origin.113
			}
		}

		# Fear of the Dark
		every_playable_country = {
			limit = { has_origin = origin_fear_of_the_dark }
			fear_of_the_dark_binary_planets = yes
		}

		# Legendary Leader
		every_playable_country = {
			limit = { has_origin = origin_legendary_leader }
			country_event = { id = paragon.5000 }
		}

		# Eager Explorers
		every_playable_country = {
			limit = {
				is_eager_explorer_empire = yes
			}
			low_tech_start_add_research_options_effect = yes
			add_research_option = tech_hyper_drive_1
		}

		# Cybernetic Creed
		every_playable_country = {
			limit = {
				has_origin = origin_cybernetic_creed
			}
			add_research_option = tech_integrated_cybernetics
		}

		# Synthetic Fertility
		every_playable_country = {
			limit = {
				has_origin = origin_synthetic_fertility
			}
			country_event = { id = machine_age.3100 }
		}

		# Arc Welders
		every_playable_country = {
			limit = {
				has_origin = origin_arc_welders
			}
			arc_welders_start_effect = yes
		}

		# Starlit Citadel
		every_playable_country = {
			limit = {
				has_origin = origin_starlit_citadel
			}
			starlit_citadel_start_effect = yes
		}

		# Astro-Mining Drones
		every_playable_country = {
			limit = {
				has_valid_civic = civic_machine_astromining_drones
			}
			random_owned_starbase = {
				set_starbase_module = {
					slot = 2
					module = astromining_bay
				}
			}
		}

		# Genesis Guides
		every_playable_country = {
			limit = {
				is_guided_sapience_empire = yes
			}
			set_variable = {
				which = presapients_guided
				value = 1
			}
			set_country_flag = found_presapients
			capital_scope = {
				spawn_presapient_species_randomizer_homeworld_effect = yes
				add_deposit = d_genesis_preserve
				set_planet_flag = pre_sapient_planet
			}
		}

		# Synthetic Fertility
		every_playable_country = {
			limit = {
				has_origin = origin_evolutionary_predators
			}
			country_event = { id = bio.1000 }
		}

		# Spyware Directives
		every_playable_country = {
			limit = {
				has_first_contact_dlc = yes
				has_civic = civic_machine_spybots
			}
			add_research_option = tech_cloaking_1
		}

		every_playable_country = {
			limit = {
				is_ascensionist_empire = yes
			}
			every_owned_planet = {
				set_planetary_ascension_tier = 1
			}
		}

		every_playable_country = {
			limit = {
				has_valid_civic = civic_machine_obsessional_directive
			}
			obsessional_directive_start_effect = yes
		}

		every_playable_country = { # Shared Genetics
			limit = {
				has_valid_civic = civic_hive_shared_genetics
			}
			add_modifier = {
				modifier = shared_genetics_fire_rate
				days = -1
				mult = value:count_negative_traits_from_country
			}
			add_modifier = {
				modifier = shared_genetics_hull
				days = -1
				mult = value:count_positive_traits_from_country
			}
		}

		# Chosen civic
		every_playable_country = {
			limit = { is_chosen_empire = yes }
			country_event = { id = shroud.5100 }
		}

		if = { #If someone has Beastmasters civic, spawn amoeba already to give them communication
			limit = {
				any_country = {
					is_beastmasters_empire = yes
				}
			}
			create_amoeba_country = yes
		}

		# Unplugged
		if = {
			limit = {
				any_playable_country = {
					has_origin = origin_unplugged
				}
			}
			unplugged_origin_start_effect = yes
		}

		# The Sacred Path
		every_playable_country = {
			limit = {
				has_origin = origin_sacred_path
			}
			country_event = { id = nomads.2000 }
		}

		# Gundersen Research Society
		every_playable_country = {
			limit = {
				has_country_flag = human_3
				is_human_species = yes
				is_nomadic = yes
				is_ai = no
			}
			country_event = { id = nomads.9000 days = 60 random = 200 }
		}
	}
}

event = {
	id = game_start.2
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_country = {
			capital_scope? = {
				solar_system = {
					every_system_planet = {
						if = {
							limit = { has_modifier = hazardous_weather }
							remove_modifier = hazardous_weather
						}
						if = {
							limit = { has_modifier = dangerous_wildlife }
							remove_modifier = dangerous_wildlife
						}
						if = {
							limit = { has_modifier = weak_magnetic_field }
							remove_modifier = weak_magnetic_field
						}
						if = {
							limit = { has_modifier = strong_magnetic_field }
							remove_modifier = strong_magnetic_field
						}
						if = {
							limit = { has_modifier = unstable_tectonics }
							remove_modifier = unstable_tectonics
						}
						if = {
							limit = { has_modifier = tidal_locked }
							remove_modifier = tidal_locked
						}
						if = {
							limit = { has_modifier = chthonian_planet }
							remove_modifier = chthonian_planet
						}
						if = {
							limit = { has_modifier = asteroid_impacts }
							remove_modifier = asteroid_impacts
						}
						if = {
							limit = { has_modifier = extensive_moon_system }
							remove_modifier = extensive_moon_system
						}
						if = {
							limit = { has_modifier = carbon_world }
							remove_modifier = carbon_world
						}
						if = {
							limit = { has_modifier = wild_storms }
							remove_modifier = wild_storms
						}
						if = {
							limit = { has_modifier = low_gravity }
							remove_modifier = low_gravity
						}
						if = {
							limit = { has_modifier = high_gravity }
							remove_modifier = high_gravity
						}
						#if = {
						#	limit = { has_modifier = hollow_planet }
						#	remove_modifier = hollow_planet
						#}
						if = {
							limit = { has_modifier = mineral_rich }
							remove_modifier = mineral_rich
						}
						if = {
							limit = { has_modifier = ultra_rich }
							remove_modifier = ultra_rich
						}
						if = {
							limit = { has_modifier = mineral_poor }
							remove_modifier = mineral_poor
						}
						if = {
							limit = { has_modifier = titanic_life }
							remove_modifier = titanic_life
						}
						if = {
							limit = { has_modifier = mineral_poor }
							remove_modifier = mineral_poor
						}
						if = {
							limit = { has_modifier = asteroid_belt }
							remove_modifier = asteroid_belt
						}
						if = {
							limit = { has_modifier = natural_beauty }
							remove_modifier = natural_beauty
						}
						if = {
							limit = { has_modifier = atmospheric_aphrodisiac }
							remove_modifier = atmospheric_aphrodisiac
						}
						if = {
							limit = { has_modifier = atmospheric_hallucinogen }
							remove_modifier = atmospheric_hallucinogen
						}
						if = {
							limit = { has_modifier = lush_planet }
							remove_modifier = lush_planet
						}
						if = {
							limit = { has_modifier = bleak_planet }
							remove_modifier = bleak_planet
						}
						if = {
							limit = { has_modifier = irradiated_planet }
							remove_modifier = irradiated_planet
						}
					}
				}
			}
		}
		every_system = {
			limit = {
				NOR = {
					is_star_class = sc_black_hole
					is_star_class = sc_neutron_star
					is_star_class = sc_pulsar
				}
			}
			every_system_planet = {
				limit = {
					OR = {
						is_planet_class = pc_barren
						is_planet_class = pc_barren_cold
					}
					NOR = {
						has_deposit = d_nanites_deposit
						has_anomaly = yes
						has_modifier = terraforming_candidate
					}
					planet_size > 9
					planet_size < 31
				}
				random_list = {
					100 = {}
					1 = { #about 20 on a huge galaxy with default settings.
						add_modifier = { modifier = terraforming_candidate }
						complex_trigger_modifier = {
							trigger = check_galaxy_setup_value
							parameters = {
								setting = habitable_worlds_scale
							}
							mode = factor
						}
					}
				}
			}
			if = {
				limit = { host_has_dlc = "Aquatics Species Pack" }
				every_system_planet = {
					limit = {
						is_planet_class = pc_frozen
						NOR = {
							has_deposit = d_nanites_deposit
							has_anomaly = yes
							has_modifier = frozen_terraforming_candidate
						}
						planet_size > 9
						planet_size < 31
					}
					random_list = {
						100 = {}
						1 = { #Not very common, Hydrocentric gives you lots of other things
							add_modifier = { modifier = frozen_terraforming_candidate }
							complex_trigger_modifier = {
								trigger = check_galaxy_setup_value
								parameters = {
									setting = habitable_worlds_scale
								}
								mode = factor
							}
						}
					}
				}
			}
			if = {
				limit = { host_has_dlc = "Toxoids Species Pack" }
				every_system_planet = {
					limit = {
						is_planet_class = pc_toxic
						NOR = {
							has_deposit = d_nanites_deposit
							has_anomaly = yes
							has_modifier = toxic_terraforming_candidate
						}
						planet_size > 9
						planet_size < 31
					}
					random_list = {
						100 = {}
						15 = { #Detox only gives you this. About 50 planets in a huge galaxy
							add_modifier = { modifier = toxic_terraforming_candidate }
							set_carrier_flag = toxic_terraforming_candidate
							complex_trigger_modifier = {
								trigger = check_galaxy_setup_value
								parameters = {
									setting = habitable_worlds_scale
								}
								mode = factor
							}
						}
					}
				}
			}
		}
		every_galaxy_planet = {
			limit = {
				is_planet_class = pc_nuked
			}
			set_carrier_flag = ancient_history_planet
		}
		# Polytechnic Districts are rare, but shouldnt be > 2
		every_galaxy_planet = {
			limit = {
				is_planet_class = pc_volcanic
			}
			if = {
				limit = {
					check_modifier_value = {
						modifier = district_polytechnic_max_add
						value < 2
					}
				}
				add_deposit = d_volcanic_basalt_plains
			}
		}
		every_system = {
			limit = {
				has_owner = no
			}
			every_system_planet = {
				limit = {
					colonizable_planet = yes
					has_owner = no
					AND = {
						NOT = { is_same_value = event_target:sol_system_earth? }
						NOT = { has_planet_flag = prescripted_ideal }
						NOT = { has_planet_flag = ratling_planet }
						NOT = { has_planet_flag = pre_sapients_forbidden }
					}
				}
				save_event_target_as = pre_sapient_planet
				random_list = {
					100 = {
						modifier = {
							subtract = value:presapient_spawn_chance
						}
					}
					0 = { # 7.5% chance at 1x on the slider.
						spawn_presapient_species_randomizer_effect = yes
						modifier = {
							add = value:presapient_spawn_chance
						}
					}
				}
			}
		}
		if = {
			limit = {
				any_system = {
					has_star_flag = old_foes_system
				}
			}
			random_system = {
				limit = {
					has_star_flag = old_foes_system
				}
				spawn_old_foes_effect = yes
			}
		}
	}
}

### Hidden Game Start Event that resets tutorial for advisor trigger purposes
event = {
	id = game_start.3
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_country = {
			limit = { is_ai = no }
			country_event = { id = tutorial.104 }
		}
	}
}

### Countdown to Nomad Fleet
event = {
	id = game_start.5
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_country = {
			limit = { is_ai = no }
			random_list = {
				#1 = { country_event = { id = namarian.1 days = 1 } } # DEBUG
				20 = { country_event = { id = namarian.1 days = 7200 random = 500 } } # 25 years
				25 = { country_event = { id = namarian.1 days = 14400 random = 500 } } # 40 years
				25 = { country_event = { id = namarian.1 days = 1980 random = 500 } } # 55 years
				30 = { } # No nomads
			}
		}
	}
}

### Country-scope start event for advanced empires
country_event = {
	id = game_start.6
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = no
	}

	immediate = {
		advanced_empire_base_init_effect = yes

		every_planet_within_border = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
				has_any_rare_strategic_resource = no
			}
			create_mining_station = {
				owner = ROOT
			}
		}
		every_planet_within_border = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
				has_any_rare_strategic_resource = no
			}
			create_research_station = {
				owner = ROOT
			}
		}
	}
}

### Empire init - add technologies and some resources
country_event = {
	id = game_start.8
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				has_origin = origin_mechanists
			}
			give_technology = { message = no tech = tech_powered_exoskeletons }
			give_technology = { message = no tech = tech_robotic_workers }
			add_research_option = tech_robomodding
		}
		if = {
			limit = {
				has_origin = origin_forever_cruise
				is_regular_empire = yes
				has_machine_subspecies = yes
			}
			give_technology = { message = no tech = tech_powered_exoskeletons }
			give_technology = { message = no tech = tech_robotic_workers }
		}
		if = {
			limit = {
				has_origin = origin_overtuned
			}
			give_technology = { message = no tech = tech_gene_tailoring }
			country_event = {
				id = tutorial.64
				days = 5
			}
		}
		if = {
			limit = {
				has_civic = civic_corporate_dominion
			}
			give_technology = { message = no tech = tech_space_trading }
		}

		if = {
			limit = {
				has_origin = origin_shroud_forged
			}
			give_technology = { message = no tech = tech_mine_zro }
			add_resource = { sr_zro = 50 }
		}

		if = {
			limit = {
				has_origin = origin_mindwardens
			}
			give_technology = { message = no tech = tech_starbase_3 }
		}

		if = {
			limit = {
				has_civic = civic_privatized_exploration
			}
			give_technology = { message = no tech = tech_starbase_3 }
		}
		if = {
			limit = {
				has_origin = origin_storm_chasers
			}
			give_technology = { message = no tech = tech_storm_prediction_1 }
		}
		if = {
			limit = {
				has_origin = origin_primal_calling
			}
			give_technology = { message = no tech = tech_genome_mapping }
		}
		if = {
			limit = {
				is_flight_schools_empire = yes
			}
			give_technology = { message = no tech = tech_strike_craft_1 }
		}
	}

	# Additional resources that may be changed by subspecies are granted in generate_start_buildings_and_districts
}

### Empire init - add ships
country_event = {
	id = game_start.9
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		#science
		if = {
			limit = { is_low_tech_start = no }

			create_fleet = {
				effect = {
					set_owner = prev

					create_ship = {
						name = random
						random_existing_design = science
					}

					set_fleet_stance = evasive
					#set_location = prev.capital_star
					set_location = prev.capital_scope.star

					owner = {
						create_leader = {
							class = scientist
							sub_type = survey
							name = random
							species = owner_main_species
							randomize_traits = no
							traits = {
								1 = random_common
							}
						}
					}
					set_leader = last_created_leader
				}
			}
		}
		else_if = {
			limit = {
				has_origin = origin_payback
			}
			create_leader = {
				class = scientist
				name = random
				species = owner_main_species
				randomize_traits = no
				traits = {
					1 = random_common
				}
			}
		}

		if = {
			limit = {
				is_eager_explorer_empire = yes
			}
			while = {
				count = 2
				create_fleet = {
					effect = {
						set_owner = prev

						create_ship = {
							name = random
							random_existing_design = explorer_vessel
						}

						set_fleet_stance = passive
						set_location = prev.capital_scope.star

						owner = {
							create_leader = {
								class = scientist
								sub_type = survey
								name = random
								species = owner_main_species
								randomize_traits = no
								traits = {
									1 = random_common
								}
							}
						}
						if = {
							limit = {
								NOT = { root =  { has_country_flag = created_first_eager_ship } }
							}
							root = {
								set_country_flag = created_first_eager_ship
							}
							last_created_leader = {
								add_trait = {
									trait = leader_trait_meticulous
									show_message = no
								}
							}
						}
						else = {
							last_created_leader = {
								add_trait = {
									trait = leader_trait_roamer
									show_message = no
								}
							}
						}
						set_leader = last_created_leader
					}
				}
			}
		}

		if = {
			limit = {
				has_civic = civic_privatized_exploration
			}
			random_owned_starbase = {
				set_starbase_size = starbase_starhold
				set_starbase_module = {
					slot = 2
					module = shipyard
				}
				set_starbase_module = {
					slot = 3
					module = trading_hub
				}
				set_starbase_module = {
					slot = 4
					module = trading_hub
				}
				set_starbase_building = {
					slot = 2
					building = resource_silo
				}
			}
		}

		if = {
			limit = {
				has_origin = origin_mindwardens
			}
			random_owned_starbase = {
				set_starbase_size = starbase_starhold
				if = {
					limit = { owner? = { is_preferred_starbase_weapons = weapon_type_kinetic } }
					set_starbase_module = { slot = 3 module = starbase_mass_driver_battery }
					set_starbase_module = { slot = 4 module = starbase_mass_driver_battery }
				}
				else_if = {
					limit = { owner? = { is_preferred_starbase_weapons = weapon_type_energy } }
					set_starbase_module = { slot = 3 module = starbase_laser_battery }
					set_starbase_module = { slot = 4 module = starbase_laser_battery }
				}
				else = {
					set_starbase_module = { slot = 3 module = starbase_laser_battery }
					set_starbase_module = { slot = 4 module = starbase_mass_driver_battery }
				}
				set_starbase_building = {
					slot = 2
					building = resource_silo
				}
			}
		}

		#constructor
		if = {
			limit = {
				is_low_tech_start = no
				is_nomadic = no
			}

			create_fleet = {
				effect = {
					set_owner = prev

					create_ship = {
						name = random
						random_existing_design = constructor
					}

					set_fleet_stance = evasive
					set_location = prev.capital_star
				}
			}
		}

		if = {
			limit = {
				is_low_tech_start = no
				is_nomadic = yes
			}

			create_fleet = {
				effect = {
					set_owner = prev

					create_ship = {
						name = random
						random_existing_design = nomads_constructor
					}

					set_fleet_stance = evasive
					set_location = prev.capital_star
				}
			}
			if = {
				limit = {
					has_origin = origin_default_nomads
				}
				create_fleet = {
					effect = {
						set_owner = prev

						create_ship = {
							name = random
							random_existing_design = nomads_constructor
						}

						set_fleet_stance = evasive
						set_location = prev.capital_star
					}
				}
			}
		}

		if = {
			limit = {
				is_eager_explorer_empire = yes
				is_nomadic = no
			}
			create_fleet = {
				effect = {
					set_owner = prev

					create_ship = {
						name = random
						random_existing_design = engineer_vessel
					}

					set_fleet_stance = evasive
					set_location = prev.capital_star
				}
			}
		}

		if = {
			limit = {
				is_eager_explorer_empire = yes
				is_nomadic = yes
			}
			create_fleet = {
				effect = {
					set_owner = prev

					create_ship = {
						name = random
						random_existing_design = nomads_engineer_vessel
					}

					set_fleet_stance = evasive
					set_location = prev.capital_star
				}
			}
			if = {
				limit = {
					has_origin = origin_default_nomads
				}
				create_fleet = {
					effect = {
						set_owner = prev

						create_ship = {
							name = random
							random_existing_design = nomads_engineer_vessel
						}

						set_fleet_stance = evasive
						set_location = prev.capital_star
					}
				}
			}
		}

		# Beastmaster Amboebas
		if = {
			limit = {
				is_beastmasters_empire = yes
			}
			# required before trying to give space fauna
			give_dna = {
				ship_category = space_amoeba
				rarity = common
			}
			if = {
				limit = {
					is_nomadic = no
				}
				create_fleet = {
					effect = {
						set_owner = prev
						while = {
							count = 3
							create_ship = {
								name = random
								random_existing_design = space_amoeba
								rarity = common
								age = 0
							}
						}
						set_location = prev.capital_star
						owner = {
							create_leader = {
								class = commander
								name = random
								species = owner_main_species
								randomize_traits = no
								traits = {
									1 = random_common
								}
							}
						}
						set_leader = last_created_leader
						save_event_target_as = beastmasters_starting_fleet
					}
				}
			}

			if = {
				limit = { has_origin = origin_fruitful }
				event_target:beastmasters_starting_fleet = {
					fleet_event = {
						id = origin.7303
						days = 20
						scopes = {
							from = prev.capital_star.starbase
						}
					}
				}
			}
		}

		# Regular corvettes/maulers
		else_if = {
			limit = {
				is_nomadic = no
				is_low_tech_start = no
			}

			create_fleet = {
				effect = {
					set_owner = prev
					set_location = prev.capital_star
					if = {
						limit = {
							root = {
								country_uses_bio_ships = yes
								NOT = {
									has_valid_civic = civic_hive_cordyceptic_drones
								}
							}
						}
						while = {
							count = 2
							create_ship = {
								name = random
								random_existing_design = mauler_stage_1
							}
						}
						if = {
							limit = {
								root = { has_origin = origin_progenitor_hive }
							}
							create_ship = {
								name = random
								random_existing_design = offspring_weaver_stage_1
							}
						}
						else = {
							create_ship = {
								name = random
								random_existing_design = weaver_stage_1
							}
						}
					}
					else_if = {
						limit = {
							root = {
								NOT = {
									has_valid_civic = civic_hive_cordyceptic_drones
								}
							}
						}
						while = {
							count = 2
							create_ship = {
								name = random
								random_existing_design = corvette
							}
						}
						if = {
							limit = {
								root = { has_origin = origin_progenitor_hive }
							}
							create_ship = {
								name = random
								random_existing_design = offspring_corvette
							}
						}
						else = {
							create_ship = {
								name = random
								random_existing_design = corvette
							}
						}
					}
					else = {
						while = {
							count = 2
							create_ship = {
								name = NAME_Reanimated_Small_Space_Organism
								design = NAME_Reanimated_Small_Space_Organism
								prefix = yes
							}
						}
						if = {
							limit = {
								root = {
									country_uses_bio_ships = no
									has_origin = origin_progenitor_hive
								}
							}
							create_ship = {
								name = random
								random_existing_design = offspring_corvette
							}
						}
						else_if = {
							limit = {
								root = {
									country_uses_bio_ships = yes
									has_origin = origin_progenitor_hive
								}
							}
							create_ship = {
								name = random
								random_existing_design = offspring_weaver_stage_1
							}
						}
						else = {
							create_ship = {
								name = "NAME_Reanimated_Small_Space_Organism"
								design = NAME_Reanimated_Small_Space_Organism
								prefix = yes
							}
						}
					}
					owner = {
						create_leader = {
							class = commander
							name = random
							species = owner_main_species
							randomize_traits = no
							traits = {
								1 = random_common
							}
						}
					}
					set_leader = last_created_leader
				}
			}
		}

		# Reanimated Amboebas
		else_if = {
			limit = {
				is_nomadic = no
				has_civic = civic_hive_cordyceptic_drones
				has_civic = civic_hive_stargazers
			}
			create_fleet = {
				set_take_point = yes
				effect = {
					set_owner = prev
					while = {
						count = 3
						create_ship = {
							name = NAME_Reanimated_Small_Space_Organism
							design = NAME_Reanimated_Small_Space_Organism
							prefix = yes
						}
					}
					set_location = prev.capital_star
					owner = {
						create_leader = {
							class = commander
							name = random
							species = owner_main_species
							randomize_traits = no
							traits = {
								1 = random_common
							}
						}
					}
					set_leader = last_created_leader
				}
			}
		}

		else_if = {
			limit = {
				is_nomadic = no
				NOT = {
					has_origin = origin_broken_shackles
					# Assigned in origin.110 instead
				}
			}
			create_leader = {
				class = commander
				name = random
				species = owner_main_species
				randomize_traits = no
				traits = {
					1 = random_common
				}
			}
		}
	}
}


# Game start - Fallen Empires
country_event = {
	id = game_start.10
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = fallen_empire
	}

	immediate = {
		add_modifier = {
			modifier = fe_ship_cost_modifier
			days = -1
		}
		if = {
			limit = {
				is_machine_empire = yes
			}
			random_list = {
				66 = { set_country_flag = fallen_machine_empire_awaken_1 }	# Custodian
				33 = { set_country_flag = fallen_machine_empire_awaken_2 }	# Berserker
			}
		}
		# contact other fallen empires & marauders
		every_country = {
			limit = {
				OR = {
					is_country_type = dormant_marauders
					is_country_type = fallen_empire
					is_country_type = enclave
				}
				NOT = { has_communications = root }
			}
			establish_communications_no_message = root
		}

		# setup mining & research stations
		every_planet_within_border = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = ROOT
			}
		}
		every_planet_within_border = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = ROOT
			}
		}

		# Give them a situation to handle pulse random events
		start_situation = {
			type = fallen_empires_situation
		}
	}
}

# this: capital colony
# from: founder species
colony_event = {
	id = game_start.12
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				OR = {
					# TODO [AA]: [AUG-56681] This event won't need "carrier_is_type" checks after "planet" is restricted to planets only
					carrier_is_type = ship
					planet? = {
						NOT = { has_planet_flag = special_start_planet }
					}
				}
			}
			if = {
				limit = {
					OR = {
						owner = { has_origin = origin_remnants } #no special earth for you, sorry
						carrier_is_type = ship
						planet? = {
							NOT = { has_planet_flag = planet_earth }
						}
					}
					NOR = {
						owner = { has_origin = origin_shattered_ring }	#setup in initializer
						is_planet_class = pc_ark
					}
				}
				generate_start_deposits_and_blockers = yes
			}
			else_if = {
				limit = {
					carrier_is_type = planet
					NOR = {
						owner = { has_origin = origin_shattered_ring }	 #setup in initializer
						owner = { has_origin = origin_remnants }		 #custom blockers
						is_planet_class = pc_ark
					}
				}
				planet? = {
					if = {
						limit = {
							has_planet_flag = planet_earth
						}
						generate_earth_deposits_and_blockers = yes
						set_earth_entity = yes
					}
				}
			}
			generate_start_buildings_and_districts = yes
			generate_start_pops = yes
			generate_home_planet_final_pass = yes
		}

		if = {
			limit = {
				carrier_is_type = planet
				planet? = {
					has_planet_flag = void_dweller_habitat_1
				}
			}
			void_dweller_home_planet_setup = yes
		}

		ship? = {
			home_arkship_initial_setup = yes
		}
	}
}

# on_initialize_advanced_colony: remove tile blockers on capital
colony_event = {
	id = game_start.20
	hide_window = yes
	is_triggered_only = yes

	pre_triggers = {
		is_capital = yes
	}

	trigger = {
		owner = {
			is_nomadic = no
			NOT = { has_origin = origin_shattered_ring }
		}
	}

	immediate = {
		clear_blockers = yes
	}
}

# on_initialize_advanced_colony: upgrade colony shelter
colony_event = {
	id = game_start.21
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_timed_carrier_flag = {
			flag = ignore_ai_building_limitations
			days = 1
		}
		upgrade_colony_shelter = yes
	}
}

@advanced_empire_capital_num_power_plants = 4
@advanced_empire_colony_num_power_plants = 2
@advanced_empire_num_mines = 1
@advanced_empire_num_farms = 1

# on_initialize_advanced_colony: set up districts and buildings for advanced start colonies
colony_event = {
	id = game_start.22
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = no
			NOR = {
				has_origin = origin_void_dwellers
				has_origin = origin_void_machines
			}
		}
	}

	immediate = {
		if = {
			limit = {
				is_capital = yes
				is_planet_class = pc_habitat
			}
			add_advanced_empire_habitat_capital_districts_effect = yes
		}
		else_if = {
			limit = {
				is_capital = yes
				owner = {
					has_origin = origin_shattered_ring
				}
			}
			add_advanced_empire_shattered_ring_capital_districts_effect = yes
		}
		else_if = {
			limit = {
				is_capital = yes
			}
			add_advanced_empire_capital_districts_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_habitat
			}
			add_advanced_empire_habitat_colony_districts_effect = yes
			add_advanced_empire_colony_buildings_effect = yes
		}
		else = {
			add_advanced_empire_colony_districts_effect = yes
			add_advanced_empire_colony_buildings_effect = yes
		}
	}
}

# on_initialize_advanced_colony: add additional pops
colony_event = {
	id = game_start.23
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = no
			NOR = {
				has_origin = origin_void_dwellers
				has_origin = origin_void_machines
			}
		}
	}

	immediate = {
		if = {
			limit = { is_capital = yes }
			add_advanced_empire_capital_pops_effect = yes
		}
		else = {
			add_advanced_empire_colony_pops_effect = yes
		}
	}
}

### Create wormhole pairs
event = {
	id = game_start.31
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		get_galaxy_setup_value = {
			setting = num_wormhole_pairs
			which = local_wormholes_num
			scale_by = 5
		}
		while = {
			count = local_wormholes_num
			random_system = {
				limit = {
					NOR = {
						has_natural_wormhole = yes
						starting_system = yes
						has_star_flag = empire_cluster
					}
				}
				random_system = {
					limit = {
						NOR = {
							has_natural_wormhole = yes
							starting_system = yes
							has_star_flag = empire_cluster
						}
						distance = {
							source = prev
							min_distance >= 500
						}
					}
					prev = { # done this way to ensure we don't spawn "orphan" wormholes (wormholes that aren't linked to anything)
						spawn_natural_wormhole = {
							bypass_type = wormhole
							random_pos = yes
							orbit_angle = 360
						}
					}
					spawn_natural_wormhole = {
						bypass_type = wormhole
						random_pos = yes
						orbit_angle = 360
					}
					link_wormholes = prev
				}
			}
		}
	}
}

### Empire init - populate home system
country_event = {
	id = game_start.33
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = default
	}

	immediate = {
		if = {
			limit = { #Make sure you are unlikely to be boxed in completely
				OR = {
					has_origin = origin_hegemon
					has_origin = origin_common_ground
				}
				capital_scope = {
					solar_system = {
						NOT = {
							any_neighbor_system = {
								NOT = { exists = owner }
							}
						}
					}
				}
			}
			if = {
				limit = {
					any_system = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 1
							max_distance <= 15
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
				}
				random_system = {
					limit = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 1
							max_distance <= 15
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
					save_event_target_as = hyperlane_system
					add_hyperlane = {
						from = this
						to = root.capital_scope.solar_system
					}
				}
			}
			else_if = {
				limit = {
					any_system = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 15
							max_distance <= 20
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
				}
				random_system = {
					limit = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 15
							max_distance <= 20
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
					save_event_target_as = hyperlane_system
					add_hyperlane = {
						from = this
						to = root.capital_scope.solar_system
					}
				}
			}
			else_if = {
				limit = {
					any_system = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 20
							max_distance <= 25
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
				}
				random_system = {
					limit = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 20
							max_distance <= 25
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
					save_event_target_as = hyperlane_system
					add_hyperlane = {
						from = this
						to = root.capital_scope.solar_system
					}
				}
			}
			else_if = {
				limit = {
					any_system = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 25
							max_distance <= 40
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
				}
				random_system = {
					limit = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 25
							max_distance <= 40
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
					save_event_target_as = hyperlane_system
					add_hyperlane = {
						from = this
						to = root.capital_scope.solar_system
					}
				}
			}
			else_if = {
				limit = {
					any_system = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 40
							max_distance <= 60
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
				}
				random_system = {
					limit = {
						distance = {
							source = root.capital_scope.solar_system
							type = euclidean
							min_distance >= 40
							max_distance <= 60
						}
						NOT = { has_hyperlane_to = root.capital_scope.solar_system }
						NOT = { exists = owner }
					}
					save_event_target_as = hyperlane_system
					add_hyperlane = {
						from = this
						to = root.capital_scope.solar_system
					}
				}
			}
			if = {
				limit = {
					event_target:hyperlane_system? = {
						NOT = {
							any_neighbor_system = {
								NOT = { exists = owner }
							}
						}
					}
				}
				event_target:hyperlane_system = {
					random_system = {
						limit = {
							distance = {
								source = prev
								type = euclidean
								min_distance >= 1
								max_distance <= 25
							}
							NOT = { has_hyperlane_to = prev }
							NOT = { exists = owner }
						}
						add_hyperlane = {
							from = this
							to = prev
						}
					}
				}
			}
		}
		if = {
			limit = {
				has_origin = origin_default
				NOT = { has_technology = tech_planetary_unification }
			}
			add_research_option = tech_planetary_unification
		}
		if = {
			every_planet_within_border = {
				limit = {
					has_deposit_for = shipclass_mining_station
					has_planet_flag = starting_mining_target
					has_mining_station = no
					root = {
						is_low_tech_start = no
					}
				}
				create_mining_station = {
					owner = ROOT
				}
			}
			every_planet_within_border = {
				limit = {
					has_deposit_for = shipclass_research_station
					has_planet_flag = starting_research_target
					has_research_station = no
					root = {
						is_low_tech_start = no
					}
				}
				create_research_station = {
					owner = ROOT
				}
			}
		}

		if = {
			limit = {
				has_origin = origin_necrophage
				NOT = { has_civic = civic_fanatic_purifiers }
				NOT = { has_ai_personality_behaviour = purger }
			}
			country_event = { id = necroids.2 days = 3420 } #tell them about the necrophage mechanic
		}
		if = {
			limit = {
				has_origin = origin_necrophage
				is_ai = yes
			}
			country_event = { id = necroids.6 days = 1800 }
			country_event = { id = necroids.6 days = 3600 }
			country_event = { id = necroids.6 days = 5400 }
			country_event = { id = necroids.6 days = 7200 }
		}
		if = {
			limit = {
				has_origin = origin_necrophage
				OR = {
					has_country_flag = necrophage_commonwealth
					has_country_flag = necrophage_une
				}
			}
			random_country = {
				limit = {
					OR = {
						AND = {
							has_country_flag = human_1
							prev = { has_country_flag = necrophage_commonwealth }
						}
						AND = {
							has_country_flag = human_2
							prev = { has_country_flag = necrophage_une }
						}
					}
				}
				weighted_random_owned_pop_group = {
					limit = {
						NOT = { is_same_species = prev }
					}
					species = { save_event_target_as = correct_prepatent_species }
				}
			}
			every_owned_pop_group = {
				limit = {
					NOT = { is_same_species = owner_species }
				}
				planet = {
					create_pop_group = {
						species = event_target:correct_prepatent_species
					}
				}
				kill_single_pop = yes
			}
			remove_country_flag = necrophage_une
			remove_country_flag = necrophage_commonwealth
		}
		if = {
			limit = {
				has_origin = origin_slavers
			}
			capital_scope = { generate_slaver_bonus = yes }
		}
		if = {
			limit = {
				OR = {
					has_valid_civic = civic_hive_stargazers
					is_hyperspace_specialty_empire = yes
					is_nomadic = yes
				}
			}
			every_system = {
				limit = {
					distance = {
						source = root.capital_scope
						use_bypasses = no
						max_jumps = 3
					}
				}
				root = { set_visited = prev }
			}
		}

		if = {
			limit = {
				is_ai = yes
				has_country_flag = necrophage_start_empire
				has_ethic = ethic_fanatic_xenophobe
				has_ethic = ethic_militarist
			}
			force_remove_civic = civic_cutthroat_politics
			force_add_civic = civic_fanatic_purifiers
		}
	}
}

### Nebula System Art
event = {
	id = game_start.50
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		every_system = {
			limit = { is_inside_nebula = yes }
			nebula_cloaking_effect = yes
			# Class B
			if = {
				limit = {
					OR = {
						is_star_class = sc_b
						is_star_class = sc_binary_2
						is_star_class = sc_binary_5
						is_star_class = sc_trinary_2
						is_star_class = sc_trinary_4
					}
				}
				random_list = {
					20 = { create_nebula_3 = yes }
					20 = { create_nebula_4 = yes }
					10 = { create_rare_nebula_1 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_1 = yes
					}
				}
			}
			# Class A
			else_if = {
				limit = {
					OR = {
						is_star_class = sc_a
						is_star_class = sc_binary_1
						is_star_class = sc_binary_9
						is_star_class = sc_binary_10
					}
				}
				if = {
					limit = { has_star_flag = ocean_paradise_nebula }
					create_rare_nebula_1 = yes
				}
				else = {
					random_list = {
						20 = { create_nebula_3 = yes }
						20 = { create_nebula_4 = yes }
						10 = { create_rare_nebula_1 = yes }
						10 = { create_rare_nebula_2 = yes }
						4 = {
							modifier = {
								factor = 0
								has_star_flag = empire_home_system
							}
							create_turbulent_nebula_1 = yes
						}
						4 = {
							modifier = {
								factor = 0
								has_star_flag = empire_home_system
							}
							create_turbulent_nebula_2 = yes
						}
					}
				}
			}
			# Class F
			else_if = {
				limit = { is_star_class = sc_f }
				random_list = {
					20 = { create_nebula_3 = yes }
					20 = { create_nebula_4 = yes }
					10 = { create_rare_nebula_1 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_1 = yes
					}
				}
			}
			# Class G
			else_if = {
				limit = {
					OR = {
						is_star_class = sc_g
						is_star_class = sc_binary_8
						is_star_class = sc_trinary_1
					}
				}
				random_list = {
					20 = { create_nebula_1 = yes }
					10 = { create_rare_nebula_2 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_2 = yes
					}
				}
			}
			# Class K
			else_if = {
				limit = {
					OR = {
						is_star_class = sc_k
						is_star_class = sc_binary_7
						is_star_class = sc_trinary_3
					}
				}
				random_list = {
					20 = { create_nebula_1 = yes }
					20 = { create_nebula_2 = yes }
					10 = { create_rare_nebula_2 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_2 = yes
					}
				}
			}
			# Class M
			else_if = {
				limit = {
					OR = {
						is_star_class = sc_m
						is_star_class = sc_m_giant
						is_star_class = sc_binary_3
						is_star_class = sc_binary_4
						is_star_class = sc_binary_6
					}
				}
				random_list = {
					20 = { create_nebula_1 = yes }
					20 = { create_nebula_2 = yes }
					10 = { create_rare_nebula_2 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_2 = yes
					}
				}
			}
			# Class T
			else_if = {
				limit = { is_star_class = sc_t }
				random_list = {
					20 = { create_nebula_3 = yes }
					10 = { create_rare_nebula_1 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_1 = yes
					}
				}
			}
			# Black Hole
			else_if = {
				limit = { is_star_class = sc_black_hole }
				random_list = {
					20 = { create_nebula_1 = yes }
					20 = { create_nebula_2 = yes }
					20 = { create_nebula_3 = yes }
					20 = { create_nebula_4 = yes }
					10 = { create_rare_nebula_1 = yes }
					10 = { create_rare_nebula_2 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_1 = yes
					}
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_2 = yes
					}
				}
			}
			# Neutron Star
			else_if = {
				limit = { is_star_class = sc_neutron_star }
				random_list = {
					20 = { create_nebula_3 = yes }
					20 = { create_nebula_4 = yes }
					10 = { create_rare_nebula_1 = yes }
					10 = { create_rare_nebula_2 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_1 = yes
					}
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_2 = yes
					}
				}
			}
			# Pulsar
			else_if = {
				limit = { is_star_class = sc_pulsar }
				random_list = {
					20 = { create_nebula_3 = yes }
					20 = { create_nebula_4 = yes }
					10 = { create_rare_nebula_1 = yes }
					10 = { create_rare_nebula_2 = yes }
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_1 = yes
					}
					4 = {
						modifier = {
							factor = 0
							has_star_flag = empire_home_system
						}
						create_turbulent_nebula_2 = yes
					}
				}
			}
		}
	}
}

country_event = {
	id = game_start.60
	hide_window = yes
	trigger = {
		is_country_type = default
		#selectable = { host_has_dlc = "Galactic Paragons" }
	}

	is_triggered_only = yes

	immediate = {
		set_variable = {
			which = paragon_chance_negative
			value = 0
		}

		# Give Empire the starting Council Agenda.
		# Nomadic empires start halfway through the arkship engineering agenda.
		# All other empires are progressed to about 1-2 years from finishing agenda_infinite_opportunities.
		if = {
			limit = { is_nomadic = yes }
			if = {
				limit = { has_country_flag = starting_civilian_arkship }
				set_council_agenda = agenda_arkship_engineering_civilian
				add_council_agenda_progress = 3500
			}
			else_if = {
				limit = { has_country_flag = starting_science_arkship }
				set_council_agenda = agenda_arkship_engineering_science
				add_council_agenda_progress = 3500
			}
			else_if = {
				limit = { has_country_flag = starting_military_arkship }
				set_council_agenda = agenda_arkship_engineering_military
				add_council_agenda_progress = 3500
			}
			# Failsafe just in case.
			else = {
				if = {
					limit = { is_gestalt = yes }
					set_council_agenda = agenda_infinite_opportunities_gestalt
				}
				else = {
					set_council_agenda = agenda_infinite_opportunities
				}
				add_council_agenda_progress = 6000
			}
		}
		else_if = {
			limit = { is_gestalt = yes }
			set_council_agenda = agenda_infinite_opportunities_gestalt
			add_council_agenda_progress = 6000
		}
		else = {
			set_council_agenda = agenda_infinite_opportunities
			add_council_agenda_progress = 6000
		}
	}
}

# Change Ethic of first Ruler to empire's fanatic ethic.
# More likely they would've been elected, and easier to re-elect them.
country_event = {
	id = game_start.63
	hide_window = yes
	trigger = {
		is_country_type = default
		is_gestalt = no
	}

	is_triggered_only = yes

	immediate = {
		switch = {
			trigger = has_ethic
			ethic_fanatic_pacifist = {
				ruler = { change_background_ethic = ethic_pacifist }
			}
			ethic_fanatic_militarist = {
				ruler = { change_background_ethic = ethic_militarist }
			}
			ethic_fanatic_xenophile = {
				ruler = { change_background_ethic = ethic_xenophile }
			}
			ethic_fanatic_xenophobe = {
				ruler = { change_background_ethic = ethic_xenophobe }
			}
			ethic_fanatic_egalitarian = {
				ruler = { change_background_ethic = ethic_egalitarian }
			}
			ethic_fanatic_authoritarian = {
				ruler = { change_background_ethic = ethic_authoritarian }
			}
			ethic_fanatic_spiritualist = {
				ruler = { change_background_ethic = ethic_spiritualist }
			}
			ethic_fanatic_materialist = {
				ruler = { change_background_ethic = ethic_materialist }
			}
		}
	}
}

# Give techs if you have the Pharma civic at game start
country_event = {
	id = game_start.64
	hide_window = yes
	trigger = {
		has_civic = civic_private_healthcare_corporate
		is_country_type = default
		has_void_dweller_origin = no # Done in 01_start_of_game_effects.txt instead.
	}

	is_triggered_only = yes

	immediate = {
		give_technology = { message = no tech = tech_frontier_health }
		add_research_option = tech_subdermal_stimulation
	}
}

#Xenolinguistic tech for some Origins
country_event = {
	id = game_start.66
	hide_window = yes
	trigger = {
		has_paragon_dlc = yes
		is_country_type = default
		OR = {
			has_origin = origin_broken_shackles
			has_origin = origin_payback
			has_origin = origin_scion
			has_origin = origin_common_ground
			has_origin = origin_hegemon
			has_origin = origin_imperial_vassal
		}
		is_gestalt = no
	}

	is_triggered_only = yes

	immediate = {
		add_research_option = tech_xeno_linguistics
		add_tech_progress = {
			tech = tech_xeno_linguistics
			progress = 0.50
		}
	}
}

# Create Gestalt Council.
country_event = {
	id = game_start.70
	hide_window = yes
	trigger = {
		is_gestalt = yes
		OR = {
			is_country_type = default
			is_country_type = fallen_empire
		}
	}

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				has_paragon_dlc = yes
			}
			ruler = {
				assign_gestalt_ruler_subclass_trait = yes
			}
		}
		country_create_gestalt_councilor = {
			CLASS = official
			COUNCIL_POS = councilor_gestalt_growth
			DESC = councilor_gestalt_growth_desc
		}
		country_create_gestalt_councilor = {
			CLASS = scientist
			COUNCIL_POS = councilor_gestalt_cognitive
			DESC = councilor_gestalt_cognitive_desc
		}
		country_create_gestalt_councilor = {
			CLASS = commander
			COUNCIL_POS = councilor_gestalt_legion
			DESC = councilor_gestalt_legion_desc
		}
		country_create_gestalt_councilor = {
			CLASS = official
			COUNCIL_POS = councilor_gestalt_regulatory
			DESC = councilor_gestalt_regulatory_desc
		}
		if = {
			limit = {
				is_country_type = fallen_empire
			}
			every_owned_leader = {
				limit = {
					is_councilor = yes
					is_ruler = no
				}
				add_skill = 9
				set_gestalt_node_portrait_effect = yes
			}
		}
	}
}

# Set starting planet governors
country_event = {
	id = game_start.71
	hide_window = yes
	trigger = {
		is_country_type = default
	}

	is_triggered_only = yes

	immediate = {
		assign_governor_effect = yes
		every_owned_leader = {
			limit = {
				leader_class = commander
				is_idle = yes
			}
			owner = {
				random_owned_fleet = {
					limit = {
						is_ship_class = shipclass_military
						NOT = {
							exists = leader
						}
					}
					assign_leader = prevprev
				}
			}
		}
		every_owned_leader = {
			limit = {
				leader_class = scientist
				is_idle = yes
			}
			owner = {
				random_owned_fleet = {
					limit = {
						is_ship_class = shipclass_science_ship
						NOT = {
							exists = leader
						}
					}
					assign_leader = prevprev
				}
			}
		}
		# In case mysterious leaders are spawned.
		every_owned_leader = {
			limit = {
				is_idle = yes
				is_councilor = no
			}
			kill_leader = {
				show_notification = no
			}
		}
	}
}

# Create Regular Council.
country_event = {
	id = game_start.72
	hide_window = yes
	trigger = {
		is_gestalt = no
		OR = {
			is_country_type = default
			is_country_type = fallen_empire
		}
		NOT = {
			has_origin = origin_broken_shackles
			# Assigned in origin.110 instead
		}
	}

	is_triggered_only = yes

	immediate = {
		# Add Minister of Defense Position
		if = {
			limit = {
				NOR = {
					has_councilor = { COUNCILOR = councilor_defense }
					has_councilor = { COUNCILOR = councilor_defense_nomadic }
				}
			}
			if = {
				limit = {
					has_paragon_dlc = yes
				}
				unlock_council_slots = 1
				if = {
					limit = {
						is_nomadic = no
					}
					set_council_position_to_council = councilor_defense
				}
				else = {
					set_council_position_to_council = councilor_defense_nomadic
				}
			}
			if = {
				limit = {
					any_owned_leader = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
				}
				random_owned_leader = {
					limit = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
					if = {
						limit = {
							prev = {
								is_nomadic = no
							}
						}
						set_council_position = councilor_defense
					}
					else = {
						set_council_position = councilor_defense_nomadic
					}
				}
			}
			else = {
				create_leader = {
					class = commander
					name = random
					species = owner_main_species
					randomize_traits = no
					traits = {
						1 = random_common
					}
				}
				last_created_leader = {
					if = {
						limit = {
							prev = {
								is_nomadic = no
							}
						}
						set_council_position = councilor_defense
					}
					else = {
						set_council_position = councilor_defense_nomadic
					}
				}
			}
		}

		# Add Head of Research Position
		if = {
			limit = {
				NOR = {
					has_councilor = { COUNCILOR = councilor_research }
					has_councilor = { COUNCILOR = councilor_research_nomadic }
				}
			}
			if = {
				limit = {
					has_paragon_dlc = yes
				}
				unlock_council_slots = 1
				if = {
					limit = {
						is_nomadic = no
					}
					set_council_position_to_council = councilor_research
				}
				else = {
					set_council_position_to_council = councilor_research_nomadic
				}
			}
			if = {
				limit = {
					any_owned_leader = {
						leader_class = scientist
						is_councilor = no
						is_ruler = no
					}
				}
				random_owned_leader = {
					limit = {
						leader_class = scientist
						is_councilor = no
						is_ruler = no
					}
					if = {
						limit = {
							prev = {
								is_nomadic = no
							}
						}
						set_council_position = councilor_research
					}
					else = {
						set_council_position = councilor_research_nomadic
					}
				}
			}
			else = {
				create_leader = {
					class = scientist
					name = random
					species = owner_main_species
					randomize_traits = no
				}
				last_created_leader = {
					if = {
						limit = {
							prev = {
								is_nomadic = no
							}
						}
						set_council_position = councilor_research
					}
					else = {
						set_council_position = councilor_research_nomadic
					}
					add_trait = {
						trait = random_common
						show_message = no
						consume_selection = yes
					}
				}
			}
		}

		# Add Minister of State Position
		if = {
			limit = {
				blocks_minister_of_state = no
			}
			if = {
				limit = {
						NOR = {
						has_councilor = { COUNCILOR = councilor_state }
						has_councilor = { COUNCILOR = councilor_state_nomadic }
					}
				}
				if = {
					limit = {
						has_paragon_dlc = yes
					}
					unlock_council_slots = 1
					if = {
						limit = {
							is_nomadic = no
						}
						set_council_position_to_council = councilor_state
					}
					else = {
						set_council_position_to_council = councilor_state_nomadic
					}
				}
				if = {
					limit = {
						any_owned_leader = {
							leader_class = official
							is_councilor = no
							is_ruler = no
						}
					}
					random_owned_leader = {
						limit = {
							leader_class = official
							is_councilor = no
							is_ruler = no
						}
						if = {
							limit = {
								prev = {
									is_nomadic = no
								}
							}
							set_council_position = councilor_state
						}
						else = {
							set_council_position = councilor_state_nomadic
						}
					}
				}
				else = {
					create_leader = {
						class = official
						name = random
						species = owner_main_species
						randomize_traits = no
					}
					last_created_leader = {
						if = {
							limit = {
								prev = {
									is_nomadic = no
								}
							}
							set_council_position = councilor_state
						}
						else = {
							set_council_position = councilor_state_nomadic
						}
						add_trait = {
							trait = random_common
							show_message = no
							consume_selection = yes
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				has_valid_civic = civic_inwards_perfection
				has_paragon_dlc = yes
				NOT = {
					has_councilor = {
						COUNCILOR = councilor_inwards_perfection
					}
				}
			}
			unlock_council_slots = 1
			set_council_position_to_council = councilor_inwards_perfection
			if = {
				limit = {
					any_owned_leader = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
				}
				random_owned_leader = {
					limit = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
					set_council_position = councilor_inwards_perfection
				}
			}
			else = {
				create_leader = {
					class = commander
					name = random
					species = owner_main_species
					randomize_traits = no
				}
				last_created_leader = {
					set_council_position = councilor_inwards_perfection
					add_trait = {
						trait = random_common
						show_message = no
						consume_selection = yes
					}
				}
			}
		}
		else_if = {
			limit = {
				has_valid_civic = civic_fanatic_purifiers
				has_paragon_dlc = yes
				NOT = {
					has_councilor = {
						COUNCILOR = councilor_fanatic_purifiers
					}
				}
			}
			unlock_council_slots = 1
			set_council_position_to_council = councilor_fanatic_purifiers
			if = {
				limit = {
					any_owned_leader = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
				}
				random_owned_leader = {
					limit = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
					set_council_position = councilor_fanatic_purifiers
				}
			}
			else = {
				create_leader = {
					class = commander
					name = random
					species = owner_main_species
					randomize_traits = no
				}
				last_created_leader = {
					set_council_position = councilor_fanatic_purifiers
					add_trait = {
						trait = random_common
						show_message = no
						consume_selection = yes
					}
				}
			}
		}
		else_if = {
			limit = {
				has_valid_civic = civic_scorched_earth
				has_paragon_dlc = yes
				NOT = {
					has_councilor = {
						COUNCILOR = councilor_infernal_scorcher
					}
				}
			}
			unlock_council_slots = 1
			set_council_position_to_council = councilor_infernal_scorcher
			if = {
				limit = {
					any_owned_leader = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
				}
				random_owned_leader = {
					limit = {
						leader_class = commander
						is_councilor = no
						is_ruler = no
					}
					set_council_position = councilor_infernal_scorcher
				}
			}
			else = {
				create_leader = {
					class = commander
					name = random
					species = owner_main_species
					randomize_traits = no
				}
				last_created_leader = {
					set_council_position = councilor_infernal_scorcher
					add_trait = {
						trait = random_common
						show_message = no
						consume_selection = yes
					}
				}
			}
		}
	}
}

### Empire Focus
country_event = {
	id = game_start.74
	hide_window = yes
	is_triggered_only = yes
	event_message_type = EMPIRE_FOCUS

	immediate = {
		create_message = {
			type = EMPIRE_FOCUS
			localization = game_start.74.title
			days = 30
		}
	}
}

# on_become_advanced_empire: Void dweller empire init
country_event = {
	id = game_start.100
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_origin = origin_void_dwellers
			has_origin = origin_void_machines
		}
	}

	immediate = {
		set_country_flag = void_dweller_advanced_pending

		# Research habitat in t1 system
		capital_scope.solar_system = {
			closest_system = {
				limit = {
					has_star_flag = neighbor_t1_first_colony
				}
				max_steps = 3
				if = {
					limit = {
						NOT = {
							exists = owner
						}
					}
					create_starbase = {
						owner = root
						size = starbase_outpost
					}
				}
				random_system_planet = {
					limit = {
						is_planet_class = pc_frozen
					}
					save_event_target_as = void_dweller_advanced_t1_planet
				}
				if = {
					limit = {
						exists = event_target:void_dweller_advanced_t1_planet
					}
					spawn_habitat_effect = {
						HABITAT_OWNER = root
						TARGET_PLANET = event_target:void_dweller_advanced_t1_planet
						DISTANCE = 9.899
					}
					event_target:target_habitat = {
						set_planet_flag = void_dweller_advanced_t1_habitat
					}
				}
				create_advanced_empire_system_stations_effect = {
					OWNER = root
				}
			}
		}

		# Mining habitat in t2 system
		capital_scope.solar_system = {
			closest_system = {
				limit = {
					has_star_flag = neighbor_t2_second_colony
				}
				max_steps = 6
				if = {
					limit = {
						NOT = {
							exists = owner
						}
					}
					create_starbase = {
						owner = root
						size = starbase_outpost
					}
				}
				random_system_planet = {
					limit = {
						is_planet_class = pc_molten
					}
					save_event_target_as = void_dweller_advanced_t2_planet
				}
				if = {
					limit = {
						exists = event_target:void_dweller_advanced_t2_planet
					}
					spawn_habitat_effect = {
						HABITAT_OWNER = root
						TARGET_PLANET = event_target:void_dweller_advanced_t2_planet
						DISTANCE = 9.899
					}
					event_target:target_habitat = {
						set_planet_flag = void_dweller_advanced_t2_habitat
					}
				}
				create_advanced_empire_system_stations_effect = {
					OWNER = root
				}
			}
		}
	}
}

# on_initialize_advanced_colony: set up districts and buildings for advanced start Void dweller colonies
colony_event = {
	id = game_start.105
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			OR = {
				has_origin = origin_void_dwellers
				has_origin = origin_void_machines
			}
		}
	}

	immediate = {
		if = {
			limit = { is_capital = yes }
			add_advanced_empire_habitat_capital_districts_effect = yes
			add_advanced_empire_capital_pops_effect = yes
		}
	}
}

# on_game_start_country: Colonise and set up habitats spawned during advanced start Void dweller / Void machines init
country_event = {
	id = game_start.110
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_origin = origin_void_dwellers
			has_origin = origin_void_machines
		}
		has_country_flag = void_dweller_advanced_pending
	}

	immediate = {
		remove_country_flag = void_dweller_advanced_pending

		# Research habitat in t1 system
		capital_scope.solar_system = {
			closest_system = {
				limit = {
					has_star_flag = neighbor_t1_first_colony
				}
				max_steps = 3
				random_system_planet = {
					limit = {
						has_planet_flag = void_dweller_advanced_t1_habitat
					}
					create_colony = {
						owner = root
						species = root
					}
					set_timed_carrier_flag = {
						flag = ignore_ai_building_limitations
						days = 1
					}
					upgrade_colony_shelter = yes
					add_advanced_empire_habitat_colony_districts_effect = yes
					add_district_and_planet_size_if_needed_effect = {
						district = district_hab_science
					}
					add_advanced_empire_colony_buildings_effect = yes
					add_advanced_empire_colony_pops_effect = yes
				}
			}
		}

		# Mining habitat in t2 system
		capital_scope.solar_system = {
			closest_system = {
				limit = {
					has_star_flag = neighbor_t2_second_colony
				}
				max_steps = 6
				random_system_planet = {
					limit = {
						has_planet_flag = void_dweller_advanced_t2_habitat
					}
					create_colony = {
						owner = root
						species = root
					}
					set_timed_carrier_flag = {
						flag = ignore_ai_building_limitations
						days = 1
					}
					upgrade_colony_shelter = yes
					add_advanced_empire_habitat_colony_districts_effect = yes
					add_district_and_planet_size_if_needed_effect = {
						district = district_hab_mining
					}
					add_advanced_empire_colony_buildings_effect = yes
					add_advanced_empire_colony_pops_effect = yes
				}
			}
		}
	}
}

# on_initialize_advanced_colony: set up districts and buildings for advanced start Nomadic colonies
colony_event = {
	id = game_start.130
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
		}
	}

	immediate = {
		set_timed_carrier_flag = {
			flag = ignore_ai_building_limitations
			days = 1
		}

		owner = {
			if = {
				limit = {
					NOT = {
						has_technology = tech_planetary_engineering
					}
				}
				give_technology = {
					tech = tech_planetary_engineering
					message = no
				}
			}
		}

		# Upgrade capital Arkship to tier 2
		fleet = {
			starbase? = {
				switch = {
					trigger = is_ship_size
					civilian_arkship_tier_1 = {
						set_starbase_size = civilian_arkship_tier_2
					}
					science_arkship_tier_1 = {
						set_starbase_size = science_arkship_tier_2
					}
					military_arkship_tier_1 = {
						set_starbase_size = military_arkship_tier_2
					}
				}
			}
		}

		if = {
			limit = {
				is_capital = yes
			}
			add_advanced_empire_arkship_capital_districts_effect = yes
		}
		else = {
			add_advanced_empire_arkship_colony_districts_effect = yes
			add_advanced_empire_colony_buildings_effect = yes
		}
		add_advanced_empire_capital_pops_effect = yes
	}
}

# on_become_advanced_empire: Nomadic empire init
country_event = {
	id = game_start.135
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
	}

	immediate = {
		advanced_empire_base_init_effect = yes
		give_technology = {
			tech = tech_arkship_construction
			message = no
		}

		# Capital system waystation. Creates one if none exists, or upgrades an existing one (e.g. Caravan Masters).
		capital_scope = {
			solar_system = {
				if = {
					limit = {
						NOT = {
							any_fleet_in_system = {
								is_waystation_ship = yes
							}
						}
					}
					create_waystation_effect = {
						OWNER = root
						ANGLE = 120
					}
				}
				else = {
					random_fleet_in_system = {
						limit = {
							is_waystation_ship = yes
							owner = {
								is_same_value = root
							}
						}
						starbase? = {
							set_starbase_size = starbase_waystation_2
						}
					}
				}
			}
		}

		# Research system
		closest_system = {
			limit = {
				has_star_flag = neighbor_t1_first_colony
			}
			max_steps = 3
			save_event_target_as = advanced_nomad_t1_system
			convert_to_research_system_effect = yes
			set_surveyed = {
				surveyed = yes
				surveyor = root
			}
			create_waystation_effect = {
				OWNER = root
				ANGLE = 120
			}
			random_ship_in_system = {
				limit = {
					is_waystation_ship = yes
					owner = {
						is_same_value = root
					}
				}
				starbase = {
					if = {
						limit = {
							solar_system = {
								# This should always be true, but as a failsafe.
								any_system_planet = {
									has_deposit_for = shipclass_research_station
								}
							}
						}
						set_starbase_module = {
							slot = 1
							module = waystation_research_module
						}
					}
				}
			}
		}
		capital_scope = {
			ship = {
				if = {
					limit = { is_civilian_arkship = yes }
					create_arkship_effect = {
						TYPE = civilian_arkship
						OWNER = root
						LOCATION = event_target:advanced_nomad_t1_system
					}
				}
				else_if = {
					limit = { is_science_arkship = yes }
					create_arkship_effect = {
						TYPE = science_arkship
						OWNER = root
						LOCATION = event_target:advanced_nomad_t1_system
					}
				}
				else_if = {
					limit = { is_military_arkship = yes }
					create_arkship_effect = {
						TYPE = military_arkship
						OWNER = root
						LOCATION = event_target:advanced_nomad_t1_system
					}
				}
			}
		}
		random_owned_colony = {
			limit = {
				solar_system = {
					is_same_value = event_target:advanced_nomad_t1_system
				}
			}
			colony_event = {
				id = game_start.21
			}
			add_advanced_empire_arkship_colony_districts_effect = yes
			add_advanced_empire_colony_buildings_effect = yes
		}

		# Resource system
		closest_system = {
			limit = {
				has_star_flag = neighbor_t2_second_colony
			}
			max_steps = 6
			save_event_target_as = advanced_nomad_t2_system
			convert_to_resource_system_effect = yes
			set_surveyed = {
				surveyed = yes
				surveyor = root
			}
			create_waystation_effect = {
				OWNER = root
				ANGLE = 120
			}
			random_ship_in_system = {
				limit = {
					is_waystation_ship = yes
					owner = {
						is_same_value = root
					}
				}
				starbase = {
					if = {
						limit = {
							solar_system = {
								# This should always be true, but as a failsafe.
								any_system_planet = {
									has_deposit_for = shipclass_mining_station
								}
							}
						}
						set_starbase_module = {
							slot = 1
							module = waystation_mining_module
						}
					}
				}
			}
		}
		capital_scope = {
			ship = {
				if = {
					limit = { is_civilian_arkship = yes }
					create_arkship_effect = {
						TYPE = civilian_arkship
						OWNER = root
						LOCATION = event_target:advanced_nomad_t2_system
					}
				}
				else_if = {
					limit = { is_science_arkship = yes }
					create_arkship_effect = {
						TYPE = science_arkship
						OWNER = root
						LOCATION = event_target:advanced_nomad_t2_system
					}
				}
				else_if = {
					limit = { is_military_arkship = yes }
					create_arkship_effect = {
						TYPE = military_arkship
						OWNER = root
						LOCATION = event_target:advanced_nomad_t2_system
					}
				}
			}
		}
		random_owned_colony = {
			limit = {
				solar_system = {
					is_same_value = event_target:advanced_nomad_t2_system
				}
			}
			colony_event = {
				id = game_start.21
			}
			add_advanced_empire_arkship_colony_districts_effect = yes
			add_advanced_empire_colony_buildings_effect = yes
		}
	}
}

# Rolls the starting fleet doctrine and weapon and defense preference policies, weighted by the personality
# The overrides are cleared first so the roll reads the personality, and each result sets its override again
# because set_policy does not run on_enabled when the option is unchanged
# Each option sets its preferred flag when enabled, which keeps the AI on the rolled option when it reviews its policies
# Station rolls come after the fleet rolls, so their weights read the rolled fleet preference
# The starbase rolls also set the arkship and waystation policies for nomads
country_event = {
	id = game_start.140
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = default
		has_global_flag = game_started
	}

	immediate = {
		clear_ship_preference_overrides = yes
		clear_starbase_preference_overrides = yes
		clear_platform_preference_overrides = yes
		# Fleet Doctrine Roll
		random_list = {
			1 = {
				modifier = {
					factor = @preference_roll_doctrine_factor
					has_spearhead_doctrine_personality = yes
				}
				set_policy = {
					policy = fleet_doctrine
					option = spearhead_fleet_doctrine
					cooldown = no
				}
			}
			1 = {
				modifier = {
					factor = @preference_roll_doctrine_default_weight
					always = yes
				}
				modifier = {
					factor = @preference_roll_doctrine_factor
					has_battleline_doctrine_personality = yes
				}
				set_policy = {
					policy = fleet_doctrine
					option = battleline_fleet_doctrine
					cooldown = no
				}
			}
			1 = {
				modifier = {
					factor = @preference_roll_doctrine_factor
					has_barrage_doctrine_personality = yes
				}
				set_policy = {
					policy = fleet_doctrine
					option = barrage_fleet_doctrine
					cooldown = no
				}
			}
			1 = {
				modifier = {
					factor = @preference_roll_doctrine_factor
					has_strike_wing_doctrine_personality = yes
				}
				modifier = {
					factor = @preference_roll_doctrine_civic_factor
					is_flight_schools_empire = yes
				}
				set_policy = {
					policy = fleet_doctrine
					option = strike_wing_fleet_doctrine
					cooldown = no
				}
			}
			1 = {
				modifier = {
					factor = @preference_roll_doctrine_factor
					has_ambush_doctrine_personality = yes
				}
				set_policy = {
					policy = fleet_doctrine
					option = ambush_fleet_doctrine
					cooldown = no
				}
			}
			1 = {
				modifier = {
					factor = @preference_roll_doctrine_factor
					has_phalanx_doctrine_personality = yes
				}
				set_policy = {
					policy = fleet_doctrine
					option = phalanx_fleet_doctrine
					cooldown = no
				}
			}
		}
		# Ship Weapon Preference Roll
		random_list = {
			1 = {
				modifier = {
					factor = @preference_roll_weapon_factor
					is_preferred_weapons = weapon_type_energy
				}
				set_policy = {
					policy = weapon_preference
					option = weapon_preference_energy
					cooldown = no
				}
				set_ai_weapon_preference = weapon_type_energy
			}
			1 = {
				modifier = {
					factor = @preference_roll_weapon_factor
					is_preferred_weapons = weapon_type_kinetic
				}
				set_policy = {
					policy = weapon_preference
					option = weapon_preference_kinetic
					cooldown = no
				}
				set_ai_weapon_preference = weapon_type_kinetic
			}
			1 = {
				modifier = {
					factor = @preference_roll_weapon_factor
					is_preferred_weapons = weapon_type_explosive
				}
				set_policy = {
					policy = weapon_preference
					option = weapon_preference_explosive
					cooldown = no
				}
				set_ai_weapon_preference = weapon_type_explosive
			}
			1 = {
				modifier = {
					factor = @preference_roll_weapon_factor
					is_preferred_weapons = weapon_type_strike_craft
				}
				modifier = {
					factor = @preference_roll_civic_factor
					is_flight_schools_empire = yes
				}
				set_policy = {
					policy = weapon_preference
					option = weapon_preference_strike_craft
					cooldown = no
				}
				set_ai_weapon_preference = weapon_type_strike_craft
			}
		}
		# Ship Defense Preference Roll
		random_list = {
			1 = {
				modifier = {
					factor = @preference_roll_ratio_factor
					ai_shields_ratio >= 0.6
				}
				set_policy = {
					policy = defense_preference
					option = shields_preference
					cooldown = no
				}
				set_ai_armor_ratio = @defense_preference_minor_ratio
				set_ai_shields_ratio = @defense_preference_major_ratio
			}
			1 = {
				modifier = {
					factor = @preference_roll_balanced_weight
					always = yes
				}
				set_policy = {
					policy = defense_preference
					option = balanced_defenses
					cooldown = no
				}
				set_ai_armor_ratio = @defense_preference_balanced_ratio
				set_ai_shields_ratio = @defense_preference_balanced_ratio
			}
			1 = {
				modifier = {
					factor = @preference_roll_ratio_factor
					ai_armor_ratio >= 0.5
				}
				set_policy = {
					policy = defense_preference
					option = armor_preference
					cooldown = no
				}
				set_ai_armor_ratio = @defense_preference_major_ratio
				set_ai_shields_ratio = @defense_preference_minor_ratio
			}
		}
		if = {
			limit = {
				is_ai = no
			}
			# Starbase/Arkship/Waystation Weapon Preference Roll
			random_list = {
				20 = {
					modifier = {
						factor = 0
						is_nomadic = no
					}
					if = {
						limit = {
							is_nomadic = yes
						}
						set_policy = {
							policy = arkship_weapon_preference
							option = arkship_weapon_preference_fleet
							cooldown = no
						}
						clear_ai_starbase_weapon_preference = yes
					}
				}
				1 = {
					modifier = {
						factor = @preference_roll_weapon_factor
						is_preferred_weapons = weapon_type_energy
					}
					if = {
						limit = {
							is_nomadic = no
						}
						set_policy = {
							policy = starbase_weapon_preference
							option = starbase_weapon_preference_energy
							cooldown = no
						}
						set_ai_starbase_weapon_preference = weapon_type_energy
					}
					else_if = {
						limit = {
							is_nomadic = yes
						}
						set_policy = {
							policy = arkship_weapon_preference
							option = arkship_weapon_preference_energy
							cooldown = no
						}
						set_ai_starbase_weapon_preference = weapon_type_energy
					}
				}
				1 = {
					modifier = {
						factor = @preference_roll_weapon_factor
						is_preferred_weapons = weapon_type_kinetic
					}
					if = {
						limit = {
							is_nomadic = no
						}
						set_policy = {
							policy = starbase_weapon_preference
							option = starbase_weapon_preference_kinetic
							cooldown = no
						}
						set_ai_starbase_weapon_preference = weapon_type_kinetic
					}
					else_if = {
						limit = {
							is_nomadic = yes
						}
						set_policy = {
							policy = arkship_weapon_preference
							option = arkship_weapon_preference_kinetic
							cooldown = no
						}
						set_ai_starbase_weapon_preference = weapon_type_kinetic
					}
				}
				1 = {
					modifier = {
						factor = @preference_roll_weapon_factor
						is_preferred_weapons = weapon_type_explosive
					}
					modifier = {
						factor = 0
						is_nomadic = yes
					}
					modifier = {
						factor = 0
						NOR = {
							has_technology = tech_swarmer_missiles_1
							has_technology = tech_swarmer_missiles_2
						}
					}
					if = {
						limit = {
							is_nomadic = no
						}
						set_policy = {
							policy = starbase_weapon_preference
							option = starbase_weapon_preference_explosive
							cooldown = no
						}
						set_ai_starbase_weapon_preference = weapon_type_explosive
					}
				}
			}
			# Starbase/Arkship/Waystation Defense Preference Roll
			random_list = {
				1 = {
					modifier = {
						factor = @preference_roll_ratio_factor
						ai_shields_ratio >= 0.6
					}
					if = {
						limit = {
							is_nomadic = no
						}
						set_policy = {
							policy = starbase_defense_preference
							option = starbase_shields_preference
							cooldown = no
						}
						set_ai_starbase_armor_ratio = @defense_preference_minor_ratio
						set_ai_starbase_shields_ratio = @defense_preference_major_ratio
					}
					else_if = {
						limit = {
							is_nomadic = yes
						}
						set_policy = {
							policy = waystation_defense_preference
							option = waystation_shields_preference
							cooldown = no
						}
						set_ai_starbase_armor_ratio = @defense_preference_minor_ratio
						set_ai_starbase_shields_ratio = @defense_preference_major_ratio
					}
				}
				1 = {
					modifier = {
						factor = @preference_roll_balanced_weight
						always = yes
					}
					if = {
						limit = {
							is_nomadic = no
						}
						set_policy = {
							policy = starbase_defense_preference
							option = starbase_balanced_defenses
							cooldown = no
						}
						set_ai_starbase_armor_ratio = @defense_preference_balanced_ratio
						set_ai_starbase_shields_ratio = @defense_preference_balanced_ratio
					}
					else_if = {
						limit = {
							is_nomadic = yes
						}
						set_policy = {
							policy = waystation_defense_preference
							option = waystation_balanced_defenses
							cooldown = no
						}
						set_ai_starbase_armor_ratio = @defense_preference_balanced_ratio
						set_ai_starbase_shields_ratio = @defense_preference_balanced_ratio
					}
				}
				1 = {
					modifier = {
						factor = @preference_roll_ratio_factor
						ai_armor_ratio >= 0.5
					}
					if = {
						limit = {
							is_nomadic = no
						}
						set_policy = {
							policy = starbase_defense_preference
							option = starbase_armor_preference
							cooldown = no
						}
						set_ai_starbase_armor_ratio = @defense_preference_major_ratio
						set_ai_starbase_shields_ratio = @defense_preference_minor_ratio
					}
					else_if = {
						limit = {
							is_nomadic = yes
						}
						set_policy = {
							policy = waystation_defense_preference
							option = waystation_armor_preference
							cooldown = no
						}
						set_ai_starbase_armor_ratio = @defense_preference_major_ratio
						set_ai_starbase_shields_ratio = @defense_preference_minor_ratio
					}
				}
			}
		}
		if = {
			limit = {
				is_ai = no
				is_nomadic = no
			}
			# Defense Platform Weapon Preference Roll
			random_list = {
				1 = {
					modifier = {
						factor = @preference_roll_weapon_factor
						is_preferred_weapons = weapon_type_energy
					}
					set_policy = {
						policy = platform_weapon_preference
						option = platform_weapon_preference_energy
						cooldown = no
					}
					set_ai_platform_weapon_preference = weapon_type_energy
				}
				1 = {
					modifier = {
						factor = @preference_roll_weapon_factor
						is_preferred_weapons = weapon_type_kinetic
					}
					set_policy = {
						policy = platform_weapon_preference
						option = platform_weapon_preference_kinetic
						cooldown = no
					}
					set_ai_platform_weapon_preference = weapon_type_kinetic
				}
				1 = {
					modifier = {
						factor = @preference_roll_weapon_factor
						is_preferred_weapons = weapon_type_explosive
					}
					set_policy = {
						policy = platform_weapon_preference
						option = platform_weapon_preference_explosive
						cooldown = no
					}
					set_ai_platform_weapon_preference = weapon_type_explosive
				}
				1 = {
					modifier = {
						factor = @preference_roll_weapon_factor
						is_preferred_weapons = weapon_type_strike_craft
					}
					modifier = {
						factor = @preference_roll_civic_factor
						is_flight_schools_empire = yes
					}
					set_policy = {
						policy = platform_weapon_preference
						option = platform_weapon_preference_strike_craft
						cooldown = no
					}
					set_ai_platform_weapon_preference = weapon_type_strike_craft
				}
			}
			# Defense Platform Defense Preference Roll
			random_list = {
				1 = {
					modifier = {
						factor = @preference_roll_ratio_factor
						ai_shields_ratio >= 0.6
					}
					set_policy = {
						policy = platform_defense_preference
						option = platform_shields_preference
						cooldown = no
					}
					set_ai_platform_armor_ratio = @defense_preference_minor_ratio
					set_ai_platform_shields_ratio = @defense_preference_major_ratio
				}
				1 = {
					modifier = {
						factor = @preference_roll_balanced_weight
						always = yes
					}
					set_policy = {
						policy = platform_defense_preference
						option = platform_balanced_defenses
						cooldown = no
					}
					set_ai_platform_armor_ratio = @defense_preference_balanced_ratio
					set_ai_platform_shields_ratio = @defense_preference_balanced_ratio
				}
				1 = {
					modifier = {
						factor = @preference_roll_ratio_factor
						ai_armor_ratio >= 0.5
					}
					set_policy = {
						policy = platform_defense_preference
						option = platform_armor_preference
						cooldown = no
					}
					set_ai_platform_armor_ratio = @defense_preference_major_ratio
					set_ai_platform_shields_ratio = @defense_preference_minor_ratio
				}
			}
		}
	}
}
