#################
# Nomads Events #
#################

# nomads.1   -  99	Reserved for important base events
# nomads.100 - 499	Champions Forge Enclave
# nomads.500 - 999	Heirs of the Khan Origin
# nomads.1000-1500	Forever Cruise Origin
# nomads.1505-1999	Nomadic Paragons
# nomads.2000-2999  The Sacred Path
# nomads.3000-3999  Contracts Functionality
# nomads.4000-4999	Arkship Functionality
# nomads.5000-5999	Civics
# nomads.6000-6999	Contract Events
# nomads.7000-7999	Forgotten Empires
# nomads.8000-8100	Dyson Gun

namespace = nomads

# Fired whenever a new owner is set for a planet, following a war or through trade
# This = Planet scope
# From = Country scope (new owner)
# FromFrom = Country scope (previous owner, if any)
carrier_event = {
	id = nomads.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOR = {
			is_planet_class = pc_ark
			has_carrier_flag = nomad_recently_settled_flag
		}
		from = { # Nomads should *never* have planets
			is_nomadic = yes
			has_policy_flag = nomad_total_war_subject
		}
	}

	immediate = {
		if = { # We split the check for attacker war and defender war into two different if's just to make scripting easier.
			inline_script = {
				script = events/nomad_total_war_ally_system_manager
				NOMAD = from
				NOMAD_SIDE = defender
				ENEMY = fromfrom
				ENEMY_SIDE = attacker
				TARGET_SYSTEM = root.solar_system
				PLANET = root
			}
		}
		else_if = {
			inline_script = {
				script = events/nomad_total_war_ally_system_manager
				NOMAD = from
				NOMAD_SIDE = attacker
				ENEMY = fromfrom
				ENEMY_SIDE = defender
				TARGET_SYSTEM = root.solar_system
				PLANET = root
			}
		}
		else_if = {
			limit = {
				exists = fromfrom
				fromfrom = {
					num_owned_planets > 0
				}
			}
			if = {
				limit = {
					from = {
						any_subject = {
							has_country_flag = nomad_vassal@fromfrom
						}
					}
				}
				from = {
					random_subject = {
						limit = {
							has_country_flag = nomad_vassal@fromfrom
						}
						save_event_target_as = nomad_vassal
						give_planet_and_system_to_rebel = {
							PLANET = root
							REBEL = event_target:nomad_vassal
							OLD_OWNER = fromfrom
						}
						nomad_vassal_join_overlord_wars_effect = yes
					}
				}
			}
			else_if = {
				limit = {
					fromfrom = { is_country_type = default }
				}
				fromfrom.capital_scope.planet = {
					create_rebels = {
						name = random
						ethos = fromfrom
						authority = fromfrom
						civics = fromfrom
						origin = origin_default
						species = fromfrom.species
						flag = random
						effect = {
							set_country_type = default
							set_country_flag = nomad_vassal@fromfrom
							establish_communications_no_message = from
							establish_communications_no_message = fromfrom
							if = {
								limit = {
									is_homicidal = yes
								}
								change_government = {
									civics = random
								}
							}
							if = {
								limit = {
									has_trait = trait_necrophage
								}
								set_origin = origin_necrophage
							}
							else_if = {
								limit = {
									has_trait = trait_void_dweller_1
								}
								set_origin = origin_void_dwellers
							}
							else_if = {
								limit = {
									has_trait = trait_void_dweller_2
								}
								set_origin = origin_void_machines
							}
							else_if = {
								limit = {
									has_trait = trait_cave_dweller
								}
								set_origin = origin_subterranean
							}
							else_if = {
								limit = {
									has_trait = trait_robot_cave_dweller
								}
								set_origin = origin_subterranean_machines
							}
							else_if = {
								limit = {
									root.owner = { has_origin = origin_tree_of_life }
									has_trait = trait_hive_mind
								}
								set_origin = origin_tree_of_life
							}

							if = {
								limit = {
									has_trait = trait_machine_unit
									root.owner = { is_individual_machine = no }
									NOT = { has_ethic = ethic_gestalt_consciousness }
								}
								shift_ethic = ethic_gestalt_consciousness
								change_government = {
									authority = auth_machine_intelligence
									civics = random
								}
							}
							else_if = {
								limit = {
									has_trait = trait_hive_mind
									NOT = { has_ethic = ethic_gestalt_consciousness }
								}
								shift_ethic = ethic_gestalt_consciousness
								change_government = {
									authority = auth_hive_mind
									civics = random
								}
							}
							save_event_target_as = nomad_vassal
							set_subject_of = {
								who = root.owner
								preset = preset_nomad_tributary
							}
							give_planet_and_system_to_rebel = {
								PLANET = root
								REBEL = event_target:nomad_vassal
								OLD_OWNER = fromfrom
							}
							nomad_vassal_join_overlord_wars_effect = yes
						}
					}
				}
			}
			else = { #If we are taking the planet from some weird non default country we create a regular vassal
				create_country= {
					name = random
					ethos = from
					authority = from
					civics = from
					remove_invalid_civics = yes # drop nomad-only civics that aren't valid for this non-nomad vassal
					origin = origin_liberated
					species = from.species
					flag = random
					effect = {
						set_country_type = default
						set_country_flag = nomad_vassal@fromfrom
						establish_communications_no_message = from
						establish_communications_no_message = fromfrom
						from = {
							every_relation = {
								establish_communications_no_message = prevprev
								add_intel_report = { #Ensuring that they know we are purging pops
									category = economy
									level = 1
									who = prevprev
								}
							}
						}
						set_policy = {
							policy = robot_pop_policy
							option = robot_pops_outlawed
							cooldown = yes
						}
						add_opinion_modifier = { modifier = opinion_liberated_us who = from }
						copy_techs_from = {
							target = from
						}
						copy_traditions_from = {
							target = from
						}
						save_event_target_as = nomad_vassal
						set_subject_of = {
							who = from
							preset = preset_nomadic_purger
						}
						random_agreement = {
							add_loyalty = 100
						}
						give_planet_and_system_to_rebel = {
							PLANET = root
							REBEL = event_target:nomad_vassal
							OLD_OWNER = fromfrom
						}
						nomad_vassal_join_overlord_wars_effect = yes
						refresh_species_rights = yes
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = fromfrom
				fromfrom = {
					num_owned_planets = 0
				}
			}
			if = {
				limit = {
					fromfrom = {
						is_homicidal = no
						is_country_type = default
					}
				}
				from = {
					if = {
						limit = {
							any_subject = {
								has_country_flag = nomad_vassal@fromfrom
							}
						}
						random_subject = {
							limit = {
								has_country_flag = nomad_vassal@fromfrom
							}
							save_event_target_as = nomad_vassal
							fromfrom = {
								set_subject_of = {
									who = root.owner
									preset = preset_nomad_tributary
									allow_instant_negotiation = yes
								}
							}
							give_planet_and_system_to_rebel = {
								PLANET = root
								REBEL = fromfrom
								OLD_OWNER = event_target:nomad_vassal
							}
							every_owned_planet = {
								save_event_target_as = target_planet
								give_planet_and_system_to_rebel = {
									PLANET = event_target:target_planet
									REBEL = fromfrom
									OLD_OWNER = event_target:nomad_vassal
								}
							}
						}
					}
					else = {
						fromfrom = {
							set_subject_of = {
								who = root.owner
								preset = preset_nomad_tributary
								allow_instant_negotiation = yes
							}
						}
						root = {
							set_owner = fromfrom
						}
					}
				}
			}
			else = {
				if = {
					limit = {
						from = {
							any_subject = {
								has_country_flag = nomad_vassal@fromfrom
							}
						}
					}
					from = {
						random_subject = {
							limit = {
								has_country_flag = nomad_vassal@fromfrom
							}
							save_event_target_as = nomad_vassal
							give_planet_and_system_to_rebel = {
								PLANET = root
								REBEL = event_target:nomad_vassal
								OLD_OWNER = fromfrom
							}
							nomad_vassal_join_overlord_wars_effect = yes
						}
					}
				}

				else_if = {
					limit = {
						fromfrom = { is_country_type = default }
					}
					fromfrom.capital_scope.planet = {
						create_rebels = {
							name = random
							ethos = fromfrom
							authority = fromfrom
							civics = fromfrom
							origin = origin_default
							species = fromfrom.species
							flag = random
							effect = {
								set_country_type = default
								set_country_flag = nomad_vassal@fromfrom
								establish_communications_no_message = from
								establish_communications_no_message = fromfrom
								if = {
									limit = {
										is_homicidal = yes
									}
									change_government = {
										civics = random
									}
								}
								if = {
									limit = {
										has_trait = trait_necrophage
									}
									set_origin = origin_necrophage
								}
								else_if = {
									limit = {
										has_trait = trait_void_dweller_1
									}
									set_origin = origin_void_dwellers
								}
								else_if = {
									limit = {
										has_trait = trait_void_dweller_2
									}
									set_origin = origin_void_machines
								}
								else_if = {
									limit = {
										has_trait = trait_cave_dweller
									}
									set_origin = origin_subterranean
								}
								else_if = {
									limit = {
										has_trait = trait_robot_cave_dweller
									}
									set_origin = origin_subterranean_machines
								}
								else_if = {
									limit = {
										root.owner = { has_origin = origin_tree_of_life }
										has_trait = trait_hive_mind
									}
									set_origin = origin_tree_of_life
								}

								if = {
									limit = {
										has_trait = trait_machine_unit
										root.owner = { is_individual_machine = no }
										NOT = { has_ethic = ethic_gestalt_consciousness }
									}
									shift_ethic = ethic_gestalt_consciousness
									change_government = {
										authority = auth_machine_intelligence
										civics = random
									}
								}
								else_if = {
									limit = {
										has_trait = trait_hive_mind
										NOT = { has_ethic = ethic_gestalt_consciousness }
									}
									shift_ethic = ethic_gestalt_consciousness
									change_government = {
										authority = auth_hive_mind
										civics = random
									}
								}
								save_event_target_as = nomad_vassal
								set_subject_of = {
									who = root.owner
									preset = preset_nomad_tributary
								}
								give_planet_and_system_to_rebel = {
									PLANET = root
									REBEL = event_target:nomad_vassal
									OLD_OWNER = fromfrom
								}
								nomad_vassal_join_overlord_wars_effect = yes
							}
						}
					}
				}
				else = { #If we are taking the planet from some weird non default country we create a regular vassal
					create_country= {
						name = random
						ethos = from
						authority = from
						civics = from
						remove_invalid_civics = yes # drop nomad-only civics that aren't valid for this non-nomad vassal
						origin = origin_liberated
						species = from.species
						flag = random
						effect = {
							set_country_type = default
							set_country_flag = nomad_vassal@fromfrom
							establish_communications_no_message = from
							establish_communications_no_message = fromfrom
							from = {
								every_relation = {
									establish_communications_no_message = prevprev
									add_intel_report = { #Ensuring that they know we are purging pops
										category = economy
										level = 1
										who = prevprev
									}
								}
							}
							set_policy = {
								policy = robot_pop_policy
								option = robot_pops_outlawed
								cooldown = yes
							}
							add_opinion_modifier = { modifier = opinion_liberated_us who = from }
							copy_techs_from = {
								target = from
							}
							copy_traditions_from = {
								target = from
							}
							save_event_target_as = nomad_vassal
							set_subject_of = {
								who = from
								preset = preset_nomad_tributary
							}
							random_agreement = {
								add_loyalty = 100
							}
							give_planet_and_system_to_rebel = {
								PLANET = root
								REBEL = event_target:nomad_vassal
								OLD_OWNER = fromfrom
							}
							nomad_vassal_join_overlord_wars_effect = yes
							refresh_species_rights = yes
						}
					}
				}
			}
		}

		else = { #If there was no previous owner there will be no vassal
			destroy_colony = yes
		}
	}
}

# Fired whenever a new owner is set for a planet, following a war or through trade
# This = Planet scope
# From = Country scope (new owner)
# FromFrom = Country scope (previous owner, if any)
carrier_event = { # Purge variant
	id = nomads.5
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOR = {
			is_planet_class = pc_ark
			has_carrier_flag = nomad_recently_settled_flag
		}
		from = { # Nomads should *never* have planets
			is_nomadic = yes
			OR = {
				AND = {
					has_valid_civic = civic_machine_terminator
					fromfrom = {
						is_machine_empire = no
					}
				}
				has_policy_flag = nomad_total_war_purge
				has_policy_flag = nomad_total_war_displace
			}
		}
	}

	immediate = {
		if = { # We split the check for attacker war and defender war into two different if's just to make scripting easier.
			inline_script = {
				script = events/nomad_total_war_ally_system_manager
				NOMAD = From
				NOMAD_SIDE = defender
				ENEMY = FromFrom
				ENEMY_SIDE = attacker
				TARGET_SYSTEM = root.solar_system
				PLANET = root
			}
		}
		else_if = {
			inline_script = {
				script = events/nomad_total_war_ally_system_manager
				NOMAD = From
				NOMAD_SIDE = attacker
				ENEMY = FromFrom
				ENEMY_SIDE = defender
				TARGET_SYSTEM = root.solar_system
				PLANET = root
			}
		}
		else_if = {
			limit = {
				exists = fromfrom
			}
			# Displace approach: absorb a fraction of the population before the vassal displaces the rest.
			if = {
				limit = { from = { has_policy_flag = nomad_total_war_displace } }
				nomad_assimilate_displaced_pops_effect = { NOMAD = from }
			}
			if = {
				limit = {
					from = {
						any_subject = {
							has_country_flag = nomad_vassal@fromfrom
						}
					}
				}
				from = {
					random_subject = {
						limit = {
							has_country_flag = nomad_vassal@fromfrom
						}
						save_event_target_as = nomad_vassal
						give_planet_and_system_to_rebel = {
							PLANET = root
							REBEL = event_target:nomad_vassal
							OLD_OWNER = fromfrom
						}
						nomad_vassal_join_overlord_wars_effect = yes
						refresh_species_rights = yes
					}
				}
			}
			else = {
				create_country= {
					name = random
					ethos = from
					authority = from
					civics = from
					remove_invalid_civics = yes # drop nomad-only civics that aren't valid for this non-nomad vassal
					origin = origin_nomadic_purger
					species = from.species
					flag = random
					effect = {
						set_country_type = default
						set_country_flag = nomad_vassal@fromfrom
						establish_communications_no_message = from
						establish_communications_no_message = fromfrom
						from = {
							every_relation = {
								establish_communications_no_message = prevprev
								add_intel_report = { #Ensuring that they know we are purging pops
									category = economy
									level = 1
									who = prevprev
								}
							}
						}
						set_policy = {
							policy = robot_pop_policy
							option = robot_pops_outlawed
							cooldown = yes
						}
						add_opinion_modifier = { modifier = opinion_liberated_us who = from }
						copy_techs_from = {
							target = from
						}
						copy_traditions_from = {
							target = from
						}
						save_event_target_as = nomad_vassal
						set_subject_of = {
							who = from
							preset = preset_nomadic_purger
						}
						random_agreement = {
							add_loyalty = 100
						}
						give_planet_and_system_to_rebel = {
							PLANET = root
							REBEL = event_target:nomad_vassal
							OLD_OWNER = fromfrom
						}
						nomad_vassal_join_overlord_wars_effect = yes
						refresh_species_rights = yes
					}
				}
			}
		}

		else = { #If there was no previous owner there will be no vassal
			destroy_colony = yes
		}
	}
}

# A pop has finished purging
# This = Planet scope
# From = Country
# FromFrom = Pop group
# local_pop_amount = variable of how many pops were purged
carrier_event = {
	id = nomads.10
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from? = {
			has_origin = origin_nomadic_purger
		}
		has_current_purge = no
		is_controlled_by = FROM
	}

	immediate = { # We have no more pops to purge, time to abandon this planet
		nomad_purge_vassal_abandon_colony_effect = yes
	}
}

# Assimilation never fires on_pop_purged (which nomads.10 handles), so check each month whether the vassal has finished
# purging and assimilating. Once it has, abandon it and hand every remaining pop (including the assimilated ones) up to
# the overlord in one lump sum, rather than trickling them up month by month.
event = {
	id = nomads.11
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_playable_country = {
			has_origin = origin_nomadic_purger
			exists = overlord
		}
	}

	immediate = {
		every_playable_country = {
			limit = {
				has_origin = origin_nomadic_purger
				exists = overlord
			}
			every_owned_colony = {
				# Nothing left to purge or assimilate: the vassal's work is done, so abandon it. The abandon step hands up
				# every remaining pop (including the assimilated ones) to the overlord in one lump sum.
				if = {
					limit = {
						has_current_purge = no
						NOT = { any_owned_pop_group = { is_being_assimilated = yes } }
					}
					nomad_purge_vassal_abandon_colony_effect = yes
				}
			}
		}
	}
}

# Hands a purge-vassal colony's stockpile and the NOMADIC_PURGE_COMPLETE toast up to the overlord. Fired from on_colony_destroyed (owner still valid) so it works however the colony is removed. Scope = the destroyed colony.
carrier_event = {
	id = nomads.13
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = owner
		owner = {
			has_origin = origin_nomadic_purger
			exists = overlord
		}
	}

	immediate = {
		# Colony message targets are not implemented (AUG-55014) and everything here is destroyed soon after, so
		# all values are frozen into the message at creation and the target is the purged planet, which persists.
		owner = {
			save_event_target_as = purge_vassal_country
			set_variable = { which = purge_xfer_pops  value = 0 }
			if = {
				limit = { is_variable_set = purge_resettled_pops }
				set_variable = { which = purge_xfer_pops  value = purge_resettled_pops }
			}
			set_variable = { which = purge_xfer_total  value = purge_xfer_pops }
			set_purge_xfer_resource_effect = { RESOURCE = minerals }
			set_purge_xfer_resource_effect = { RESOURCE = food }
			set_purge_xfer_resource_effect = { RESOURCE = energy }
			set_purge_xfer_resource_effect = { RESOURCE = alloys }
			set_purge_xfer_resource_effect = { RESOURCE = consumer_goods }
			set_purge_xfer_resource_effect = { RESOURCE = trade }
			set_purge_xfer_resource_effect = { RESOURCE = volatile_motes }
			set_purge_xfer_resource_effect = { RESOURCE = exotic_gases }
			set_purge_xfer_resource_effect = { RESOURCE = rare_crystals }
			set_purge_xfer_resource_effect = { RESOURCE = sr_living_metal }
			set_purge_xfer_resource_effect = { RESOURCE = sr_zro }
			set_purge_xfer_resource_effect = { RESOURCE = sr_dark_matter }
			set_purge_xfer_resource_effect = { RESOURCE = nanites }
			set_purge_xfer_resource_effect = { RESOURCE = minor_artifacts }
			set_purge_xfer_resource_effect = { RESOURCE = astral_threads }
			# Resources-only total, so the toast can line-break its resources and pops sections independently.
			set_variable = { which = purge_xfer_res_total  value = purge_xfer_total }
			subtract_variable = { which = purge_xfer_res_total  value = purge_xfer_pops }
		}
		# Created before the transfer below empties the vassal's stockpile
		owner.overlord = {
			create_message = {
				type = NOMADIC_PURGE_COMPLETE
				localization = NOMADIC_PURGE_COMPLETE_DESC
				days = 30
				target = root
				variable = {
					type = name
					localization = PLANET
					scope = root
				}
				variable = {
					type = resources
					localization = RESOURCES
					scope = event_target:purge_vassal_country
					resources = { minerals food energy alloys consumer_goods trade volatile_motes exotic_gases rare_crystals sr_living_metal sr_zro sr_dark_matter nanites minor_artifacts astral_threads }
					trigger = { event_target:purge_vassal_country = { check_variable = { which = purge_xfer_res_total  value > 0 } } }
				}
				variable = {
					type = variable
					varname = purge_xfer_pops
					localization = POPS
					scope = event_target:purge_vassal_country
					trigger = { event_target:purge_vassal_country = { check_variable = { which = purge_xfer_pops  value > 0 } } }
				}
				variable = {
					type = key
					localization = POPS_UNIT
					value = purge_xfer_unit_pops
					trigger = { event_target:purge_vassal_country = { check_variable = { which = purge_xfer_pops  value > 0 } } }
				}
				variable = {
					# " and " joins the resources and pops, but only shows when both sections are present
					type = key
					localization = AND
					value = purge_xfer_and
					trigger = {
						event_target:purge_vassal_country = {
							check_variable = { which = purge_xfer_res_total  value > 0 }
							check_variable = { which = purge_xfer_pops  value > 0 }
						}
					}
				}
				variable = {
					# Single line-break putting the gained resources/pops on their own line below the headline
					type = key
					localization = LINEBREAK
					value = purge_xfer_linestart
					trigger = { event_target:purge_vassal_country = { check_variable = { which = purge_xfer_total  value > 0 } } }
				}
				variable = {
					type = key
					localization = GAINED
					value = purge_xfer_lineend
					trigger = { event_target:purge_vassal_country = { check_variable = { which = purge_xfer_total  value > 0 } } }
				}
			}
		}
		if = {
			limit = {
				owner = { num_owned_planets <= 1 }
			}
			owner = {
				set_timed_country_flag = {
					flag = supress_country_destroyed_message
					days = 2
				}
				transfer_resources_to_empire = {
					target = overlord
					resources = { minerals food energy alloys consumer_goods trade volatile_motes exotic_gases rare_crystals sr_living_metal sr_zro sr_dark_matter nanites minor_artifacts astral_threads }
					percentage = 1.0
				}
			}
		}
		# Clear the snapshot so a later engine-removed colony cannot show this colony's pops
		owner = {
			set_variable = { which = purge_resettled_pops  value = 0 }
		}
	}
}

# From = system
# FromFrom = country (previous owner)
# This = country (new owner)
country_event = {
	id = nomads.15
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
	}

	immediate = {
		if = { # We split the check for attacker war and defender war into two different if's just to make scripting easier.
			inline_script = {
				script = events/nomad_total_war_ally_system_manager_no_planet
				NOMAD = Root
				NOMAD_SIDE = attacker
				ENEMY = FromFrom
				ENEMY_SIDE = defender
				TARGET_SYSTEM = from
			}
		}
		else_if = {
			inline_script = {
				script = events/nomad_total_war_ally_system_manager_no_planet
				NOMAD = Root
				NOMAD_SIDE = defender
				ENEMY = FromFrom
				ENEMY_SIDE = attacker
				TARGET_SYSTEM = from
			}
		}
		else_if = { #If we are the Khan we give the system to our system holder
			limit = {
				has_country_flag = heir_of_the_khan_khan
				exists = event_target:marauder_country_1
			}
			event_target:marauder_country_1 = {
				from.starbase = {
					set_owner = prev
				}
			}
		}
		else_if = { #If we have a vassal we give the system to them
			limit = {
				exists = fromfrom
				any_subject = {
					has_country_flag = nomad_vassal@fromfrom
				}
			}
			random_subject = {
				limit = {
					has_country_flag = nomad_vassal@fromfrom
				}
				from.starbase = {
					set_owner = prev
				}
			}
		}
		else_if = { #If we have no vassal we give the system to the closest ally
			inline_script = {
				script = events/nomad_total_war_ally_system_manager_no_claim
				NOMAD = Root
				NOMAD_SIDE = attacker
				ENEMY = FromFrom
				ENEMY_SIDE = defender
				TARGET_SYSTEM = from
			}
		}
		else_if = {
			inline_script = {
				script = events/nomad_total_war_ally_system_manager_no_claim
				NOMAD = Root
				NOMAD_SIDE = defender
				ENEMY = FromFrom
				ENEMY_SIDE = attacker
				TARGET_SYSTEM = from
			}
		}
		else = { #No vassal, no allies, no fleets.
			from.starbase = {
				fleet = { destroy_fleet = this }
			}
		}
	}
}

#Fired whenever you gain an arkship just to ensure no colony events sneak through
carrier_event = {
	id = nomads.16
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_ark
	}

	immediate = {
		set_carrier_flag = colony_event
	}
}

# Fired by: on_game_start_country
# Starts the Operational Reserves situation and Star Journal chain (Star Seekers only).
# Also relocates all starting fleets to orbit a random non-star planet in their system.
country_event = {
	id = nomads.20
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
	}

	immediate = {
		start_situation = {
			type = situation_nomad_economy
		}
		if = {
			limit = {
				is_star_seeker_empire = yes
			}
			begin_event_chain = {
				event_chain = nomad_star_journal_chain
				target = this
			}
			add_event_chain_counter = {
				event_chain = nomad_star_journal_chain
				counter = star_journal_progress
				amount = 1
			}
			enable_mission = {
				name = NOMAD_STAR_JOURNAL_UNKNOWN_SIGHTS
			}
		}
		every_owned_fleet = {
			move_nomadic_fleet_to_random_system_planet_effect = yes
		}
		# Remove the dummy binary broken pair anchor in Black Hole systems.
		# Nomads have no borders, so random_planet_within_border cannot be used here.
		if = {
			limit = { exists = capital_scope }
			capital_scope.solar_system = {
				random_system_planet = {
					limit = {
						has_planet_flag = delete_me_planet
					}
					remove_planet = yes
				}
			}
		}
	}
}

country_event = {
	id = nomads.25
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
	}

	immediate = {
		set_variable = {
			which = visited_systems_nomad
			value = 1
		}
	}
}

ship_event = {
	id = nomads.30
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
			is_star_seeker_empire = yes
		}
		from = {
			NOT = {
				has_star_flag = nomad_visited_system@fromfrom
			}
		}
	}

	immediate = {
		owner? = {
			change_variable = {
				which = visited_systems_nomad
				value = 1
			}
			add_event_chain_counter = {
				event_chain = nomad_star_journal_chain
				counter = star_journal_progress
				amount = 1
			}
		}
		from = {
			set_timed_star_flag = {
				flag = nomad_visited_system@fromfrom
				days = 3600
			}
		}
		ship_event = {
			id = nomads.31
		}
		ship_event = {
			id = nomads.32
		}
	}
}

ship_event = {
	id = nomads.31
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
			is_star_seeker_empire = yes
		}
	}
	immediate = {
		from = {
			switch = {
				## Black Hole Bonus
				trigger = is_star_class
				sc_black_hole = {
					owner? = {
						change_variable = {
							which = visited_systems_nomad
							value = 1
						}
						add_event_chain_counter = {
							event_chain = nomad_star_journal_chain
							counter = star_journal_progress
							amount = 1
						}
						enable_star_journal_missions_and_add_counter = {
							check_mission_flag = NOMAD_STAR_JOURNAL_BLACK_HOLES_FLAG
							mission = NOMAD_STAR_JOURNAL_BLACK_HOLES
							set_mission_flag = NOMAD_STAR_JOURNAL_BLACK_HOLES_FLAG
							counter = star_journal_progress_black_holes
						}
						add_mission_counter = {
							mission = NOMAD_STAR_JOURNAL_UNKNOWN_SIGHTS
							counter = star_journal_progress_unknown_sights
							amount = 1
						}
					}
				}
				sc_neutron_star = {
					owner? = {
						change_variable = {
							which = visited_systems_nomad
							value = 1
						}
						add_event_chain_counter = {
							event_chain = nomad_star_journal_chain
							counter = star_journal_progress
							amount = 1
						}
						enable_star_journal_missions_and_add_counter = {
							check_mission_flag = NOMAD_STAR_JOURNAL_NEUTRON_STAR_FLAG
							mission = NOMAD_STAR_JOURNAL_NEUTRON_STAR
							set_mission_flag = NOMAD_STAR_JOURNAL_NEUTRON_STAR_FLAG
							counter = star_journal_progress_neutron_star
						}
						add_mission_counter = {
							mission = NOMAD_STAR_JOURNAL_UNKNOWN_SIGHTS
							counter = star_journal_progress_unknown_sights
							amount = 1
						}
					}
				}
				sc_pulsar = {
					owner? = {
						change_variable = {
							which = visited_systems_nomad
							value = 1
						}
						add_event_chain_counter = {
							event_chain = nomad_star_journal_chain
							counter = star_journal_progress
							amount = 1
						}
						enable_star_journal_missions_and_add_counter = {
							check_mission_flag = NOMAD_STAR_JOURNAL_PULSAR_FLAG
							mission = NOMAD_STAR_JOURNAL_PULSAR
							set_mission_flag = NOMAD_STAR_JOURNAL_PULSAR_FLAG
							counter = star_journal_progress_pulsar
						}
						add_mission_counter = {
							mission = NOMAD_STAR_JOURNAL_UNKNOWN_SIGHTS
							counter = star_journal_progress_unknown_sights
							amount = 1
						}
					}
				}
			}
		}
		if = {
			limit = {
				from = {
					OR = {
						is_binary_star = yes
						is_trinary_star = yes
					}
				}
			}
			owner? = {
				change_variable = {
					which = visited_systems_nomad
					value = 1
				}
				add_event_chain_counter = {
					event_chain = nomad_star_journal_chain
					counter = star_journal_progress
					amount = 1
				}
				enable_star_journal_missions_and_add_counter = {
					check_mission_flag = NOMAD_STAR_JOURNAL_BINARY_TRINARY_FLAG
					mission = NOMAD_STAR_JOURNAL_BINARY_TRINARY
					set_mission_flag = NOMAD_STAR_JOURNAL_BINARY_TRINARY_FLAG
					counter = star_journal_progress_binary_trinary
				}
				add_mission_counter = {
					mission = NOMAD_STAR_JOURNAL_UNKNOWN_SIGHTS
					counter = star_journal_progress_unknown_sights
					amount = 1
				}
			}
		}
		if = {
			limit = {
				solar_system = {
					has_star_flag = guardian
				}
			}
			owner? = {
				change_variable = {
					which = visited_systems_nomad
					value = 1
				}
				add_event_chain_counter = {
					event_chain = nomad_star_journal_chain
					counter = star_journal_progress
					amount = 1
				}
				enable_star_journal_missions_and_add_counter = {
					check_mission_flag = NOMAD_STAR_JOURNAL_MIGHTY_BEASTS_FLAG
					mission = NOMAD_STAR_JOURNAL_MIGHTY_BEASTS
					set_mission_flag = NOMAD_STAR_JOURNAL_MIGHTY_BEASTS_FLAG
					counter = star_journal_progress_mighty_beasts
				}
				add_mission_counter = {
					mission = NOMAD_STAR_JOURNAL_UNKNOWN_SIGHTS
					counter = star_journal_progress_unknown_sights
					amount = 1
				}
			}
		}
	}
}

ship_event = {
	id = nomads.32
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
			is_star_seeker_empire = yes
		}
		solar_system = {
			any_fleet_in_system = {
				owner? = {
					is_guardian_country = yes
				}
			}
		}
	}
	immediate = {
		owner? = {
			change_variable = {
				which = visited_systems_nomad
				value = 1
			}
			add_event_chain_counter = {
				event_chain = nomad_star_journal_chain
				counter = star_journal_progress
				amount = 1
			}
		}
	}
}

# Nomad fleet leaves a system owned by a country with closed borders
# Sets a timed flag so cb_expel_nomads stays valid while the fleet is jumping between systems
# Scope = Fleet
# From = System
fleet_event = {
	id = nomads.35
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
		}
		from? = {
			space_owner? = {
				NOT = { is_same_value = root.owner }
				is_country_type = default
				has_closed_borders = root.owner
			}
		}
	}

	immediate = {
		from = {
			space_owner = {
				set_timed_country_flag = {
					flag = nomad_trespassing@root.owner
					days = 30
				}
			}
		}
	}
}

ship_event = {
	id = nomads.36
	hide_window = yes
	is_triggered_only = yes

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

	immediate = {
		if = {
			limit = {
				owner? = {
					check_variable = {
						which = visited_systems_nomad
						value >= 50
					}
				}
			}
			owner = {
				random_situation = {
					limit = {
						is_situation_type = situation_nomad_economy
					}
					set_situation_approach = situation_nomad_economy_harvest_everything
					# Snapshot reserves so the Festival upkeep drains them evenly over its duration.
					export_trigger_value_to_variable = {
						trigger = situation_progress
						variable = celebration_reserves_at_start
						rounded = yes
					}
					set_variable = {
						which = celebration_months_remaining
						value = @celebration_duration_months_6
					}
				}
				set_country_flag = celebration_active
				set_variable = {
					which = visited_systems_nomad
					value = 1
				}
				reset_event_chain_counter = {
					event_chain = nomad_star_journal_chain
					counter = star_journal_progress
				}
			}
		}
	}
}

country_event = {
	id = nomads.37
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
		is_star_seeker_empire = yes
	}

	immediate = {
		# Switching to Conserve ends any active Festival directly (cooldown + cleanup),
		# rather than relying on on_arkship_mobile firing from a scripted un-encamp.
		end_nomad_festival_effect = yes
		every_owned_ship = {
			limit = {
				is_system_locked = yes
			}
			set_system_locked = no
		}
	}
}

# Festival duration countdown, fired monthly by situation_nomad_economy's on_monthly
# This = situation
situation_event = {
	id = nomads.38
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		current_situation_approach = situation_nomad_economy_harvest_everything
	}

	immediate = {
		# Back-fill tracking for Festivals already running before this change (legacy saves).
		if = {
			limit = {
				owner = { NOT = { has_country_flag = celebration_active } }
			}
			owner = { set_country_flag = celebration_active }
			export_trigger_value_to_variable = {
				trigger = situation_progress
				variable = celebration_reserves_at_start
				rounded = yes
			}
			set_variable = {
				which = celebration_months_remaining
				value = @celebration_duration_months_6
			}
		}
		# Count the Festival down; it ends after @celebration_duration_months_6 months.
		change_variable = {
			which = celebration_months_remaining
			value = -1
		}
		if = {
			limit = {
				check_variable = {
					which = celebration_months_remaining
					value <= 0
				}
			}
			owner = {
				end_nomad_festival_effect = yes
			}
		}
	}
}

# Festival ended (arkship became mobile), fired by on_arkship_mobile
# This = arkship (carrier), From = owner
ship_event = {
	id = nomads.39
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
			is_star_seeker_empire = yes
			has_country_flag = celebration_active
		}
	}

	immediate = {
		owner = {
			end_nomad_festival_effect = yes
		}
	}
}

# Nomad excavates owned dig site WITHOUT permission
# This = Fleet ( science vessel )
# From = Archaeological Site
fleet_event = {
	id = nomads.40
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = solar_system.owner
		owner = {
			is_nomadic = yes
			has_tradition = tr_archivism_frontier_archaeology
			NOT = { has_research_agreement = root.solar_system.owner }
		}
	}

	immediate = {
		# Upset the solar system owner
		solar_system.owner = {
			add_opinion_modifier = {
				modifier = opinion_arkship_archeological_theft
				who = root.owner
			}
		}
		#message owner
		solar_system.owner  = {
			create_message = {
				type = MESSAGE_ARCHAEOLOGY_STAGE_FINISHED_NOMAD_NO_PERMISSION_OWNER
				localization = MESSAGE_ARCHAEOLOGY_STAGE_FINISHED_NOMAD_NO_PERMISSION_OWNER_DESC
				days = 30
				target = root
			}
		}
		#message nomad
		create_message = {
			type = MESSAGE_ARCHAEOLOGY_STAGE_FINISHED_NOMAD_NO_PERMISSION_OWNER
			localization = MESSAGE_ARCHAEOLOGY_STAGE_FINISHED_NOMAD_NO_PERMISSION_DESC
			days = 30
			target = this
		}
	}
}

# Nomad excavates owned dig site WITH permission
fleet_event = {
	id = nomads.41
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = solar_system.owner
		owner = {
			is_nomadic = yes
			has_research_agreement = root.solar_system.owner
		}
	}

	immediate = {
		#grant a reward to the solar system owner
		solar_system.owner = {
			random_list = { #grant monthly research
				1 = {
					random_list = {
						1 = {
							add_monthly_resource_mult = {
								resource = physics_research
								value = @tier2researchreward
								min = @tier2researchmin
								max = @tier2researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = society_research
								value = @tier2researchreward
								min = @tier2researchmin
								max = @tier2researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = engineering_research
								value = @tier2researchreward
								min = @tier2researchmin
								max = @tier2researchmax
							}
						}
					}
				}
				3 = {
					random_list = {
						1 = {
							add_monthly_resource_mult = {
								resource = physics_research
								value = @tier3researchreward
								min = @tier3researchmin
								max = @tier3researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = society_research
								value = @tier3researchreward
								min = @tier3researchmin
								max = @tier3researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = engineering_research
								value = @tier3researchreward
								min = @tier3researchmin
								max = @tier3researchmax
							}
						}
					}
				}
				1 = {
					random_list = {
						1 = {
							add_monthly_resource_mult = {
								resource = physics_research
								value = @tier4researchreward
								min = @tier4researchmin
								max = @tier4researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = society_research
								value = @tier4researchreward
								min = @tier4researchmin
								max = @tier4researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = engineering_research
								value = @tier4researchreward
								min = @tier4researchmin
								max = @tier4researchmax
							}
						}
					}
				}
			}
			random_list = { #grant artifacts
				5 = { small_artifact_reward = yes }
				3 = { medium_artifact_reward = yes }
				1 = { large_artifact_reward = yes }
			}
		}
		#message owner
		solar_system.owner  = {
			create_message = {
				type = MESSAGE_ARCHAEOLOGY_STAGE_FINISHED_NOMAD_PERMISSION
				localization = MESSAGE_ARCHAEOLOGY_STAGE_FINISHED_NOMAD_PERMISSION_DESC
				days = 30
				target = root
			}
		}
	}
}

# Nomad excavates owned Astral Scar WITH permission
country_event = {
	id = nomads.42
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = fromfrom.solar_system.owner
		is_nomadic = yes
		has_research_agreement = fromfrom.solar_system.owner
	}

	immediate = {
		#grant a reward to the solar system owner
		fromfrom.solar_system.owner = {
			random_list = { #grant monthly research
				1 = {
					random_list = {
						1 = {
							add_monthly_resource_mult = {
								resource = physics_research
								value = @tier2researchreward
								min = @tier2researchmin
								max = @tier2researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = society_research
								value = @tier2researchreward
								min = @tier2researchmin
								max = @tier2researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = engineering_research
								value = @tier2researchreward
								min = @tier2researchmin
								max = @tier2researchmax
							}
						}
					}
				}
				3 = {
					random_list = {
						1 = {
							add_monthly_resource_mult = {
								resource = physics_research
								value = @tier3researchreward
								min = @tier3researchmin
								max = @tier3researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = society_research
								value = @tier3researchreward
								min = @tier3researchmin
								max = @tier3researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = engineering_research
								value = @tier3researchreward
								min = @tier3researchmin
								max = @tier3researchmax
							}
						}
					}
				}
				1 = {
					random_list = {
						1 = {
							add_monthly_resource_mult = {
								resource = physics_research
								value = @tier4researchreward
								min = @tier4researchmin
								max = @tier4researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = society_research
								value = @tier4researchreward
								min = @tier4researchmin
								max = @tier4researchmax
							}
						}
						1 = {
							add_monthly_resource_mult = {
								resource = engineering_research
								value = @tier4researchreward
								min = @tier4researchmin
								max = @tier4researchmax
							}
						}
					}
				}
			}
			random_list = { #grant artifacts
				5 = { astral_thread_small_reward = yes }
				3 = { astral_thread_medium_reward = yes }
				1 = { astral_thread_large_reward = yes }
			}
		}
		#message owner
		fromfrom.solar_system.owner  = {
			create_message = {
				type = MESSAGE_ASTRAL_RIFT_STAGE_FINISHED_NOMAD_PERMISSION
				localization = MESSAGE_ASTRAL_RIFT_STAGE_FINISHED_NOMAD_PERMISSION_TITLE
				days = 30
				target = fromfrom
			}
		}
	}
}

#######################
# Arkship Readiness Checklist
#######################

# Initialise checklist.
country_event = {
	id = nomads.50
	title = nomads.50.name
	desc = {
		trigger = {
			text = nomads.50.default.desc
			success_text = {
				text = nomads.50.shields
				has_country_flag = checklist_shield_realignment
			}
			success_text = {
				text = nomads.50.comms
				has_country_flag = checklist_decipher_signal
			}
			success_text = {
				text = nomads.50.sensors
				has_country_flag = checklist_sensors_calibration
			}
			success_text = {
				text = nomads.50.coolant
				has_country_flag = checklist_flush_coolant
			}
			success_text = {
				text = nomads.50.hangars
				has_country_flag = checklist_cluttered_hangars
			}
			success_text = {
				text = nomads.50.harvest
				has_country_flag = checklist_harvest_resources
			}
			text = nomads.50.end.desc
		}
	}
	picture = GFX_evt_arkship_in_orbit
	show_sound = event_ambient_arkship_engine
	is_triggered_only = yes
	auto_opens = no
	notification_event_icon = "GFX_message_arkship_malfunction"
	notification_event_icon_frame = 2
	trigger = {
		is_nomadic = yes
		is_ai = no
	}

	immediate = {
		begin_event_chain = {
			event_chain = "nomad_checklist_chain"
			target = capital_scope.ship
		}
		begin_checklist_harvest_resources_effect = yes
		while = {
			count = 2
			random_list = {
				1 = {
					# Shield Calibration
					begin_checklist_shield_realignment_effect = yes
					modifier = {
						factor = 0
						has_country_flag = checklist_shield_realignment
					}
				}
				1 = {
					# Update Comms Frequencies
					begin_checklist_decipher_signal_effect = yes
					modifier = {
						factor = 0
						has_country_flag = checklist_decipher_signal
					}
				}
				1 = {
					# Recalibrate Sensors
					begin_checklist_sensor_calibration_effect = yes
					modifier = {
						factor = 0
						has_country_flag = checklist_sensors_calibration
					}
				}
				1 = {
					# Flush Coolant
					begin_checklist_flush_coolant_effect = yes
					modifier = {
						factor = 0
						has_country_flag = checklist_flush_coolant
					}
				}
				1 = {
					# Cluttered Hangar Bays
					begin_checklist_clear_hangar_bays_effect = yes
					modifier = {
						factor = 0
						has_country_flag = checklist_cluttered_hangars
					}
				}
			}
		}
	}

	option = {
		name = ACKNOWLEDGED
		tooltip = {
			begin_event_chain = {
				event_chain = "nomad_checklist_chain"
				target = capital_scope.ship
			}
		}
	}
}

# Complete NOMAD_CHECKLIST_SHIELD_REALIGNMENT
# Fired by on_damage_taken
# This = damaged ship
# From = attacker ship
ship_event = {
	id = nomads.51
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
			has_country_flag = checklist_shield_realignment
		}
		colony? = {
			has_modifier = checklist_unaligned_shields
		}
		check_modifier_value = { modifier = ship_shield_reduction value = 0 }
		has_shield_percentage < 0.1
	}

	immediate = {
		owner? = {
			add_mission_counter = {
				mission =  NOMAD_CHECKLIST_SHIELD_REALIGNMENT
				counter = SHIELD_REALIGNMENT
				amount = 1
			}
		}
	}
}

# Increment "Unique Stars Surveyed" counter for NOMAD_CHECKLIST_SENSOR_CALIBRATION
# Fired by on_survey_planet
# Scope = Ship
# From = Planet
ship_event = {
	id = nomads.52
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
			has_country_flag = checklist_sensors_calibration
		}
		colony? = {
			has_modifier = checklist_sensor_calibration
		}
		from = {
			is_star = yes
		}
	}

	immediate = {
		from = {
			switch = {
				trigger = is_planet_class
				pc_b_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_b_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_b_star
					}
				}
				pc_a_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_a_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_a_star
					}
				}
				pc_f_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_f_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_f_star
					}
				}
				pc_g_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_g_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_g_star
					}
				}
				pc_k_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_k_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_k_star
					}
				}
				pc_m_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_m_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_m_star
					}
				}
				pc_m_giant_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_m_giant_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_m_giant_star
					}
				}
				pc_t_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_t_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_t_star
					}
				}
				pc_black_hole = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_black_hole
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_black_hole
					}
				}
				pc_neutron_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_neutron_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_neutron_star
					}
				}
				pc_pulsar = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_pulsar
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_pulsar
					}
				}
				pc_rift_star = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_rift_star
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_rift_star
					}
				}
				pc_protostar = {
					root.owner? = {
						if = {
							limit = {
								NOT = {
									has_country_flag = sensor_calibration_pc_protostar
								}
							}
							add_mission_counter = {
								mission = NOMAD_CHECKLIST_SENSOR_CALIBRATION
								counter = SENSOR_CALIBRATION_UNIQUE_STARS
								amount = 1
							}
						}
						set_country_flag = sensor_calibration_pc_protostar
					}
				}
				default = {
					log = "Error: An Arkship has surveyed a star class that isn't listed in nomads.52, please update the event."
				}
			}
		}
	}
}

# Source of strange signal has been surveyed.
# Fired by on_system_survey_ship
# Scope = ship
# From = system
ship_event = {
	id = nomads.53
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner? = {
			is_nomadic = yes
			has_country_flag = checklist_decipher_signal
		}
		from.owner? = {
			is_same_value = event_target:comms_signal_system@owner
		}
	}

	immediate = {
		owner? = {
			add_mission_counter = {
				mission =  NOMAD_CHECKLIST_DECIPHER_SIGNAL
				counter = SIGNAL_DECIPHERED
				amount = 1
			}
		}
		clear_global_event_target = event_target:comms_signal_system@from.owner
	}
}

# Source of strange signal has been deciphered.
# Fired by on_first_contact_finished
# This = first contact scope
# From = other country
first_contact_event = {
	id = nomads.54
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			AND = {
				from = {
					is_nomadic = yes
					has_country_flag = checklist_decipher_signal
				}
				owner = {
					is_same_value = event_target:comms_source@from
				}
			}
			AND = {
				owner = {
					is_nomadic = yes
					has_country_flag = checklist_decipher_signal
				}
				from = {
					is_same_value = event_target:comms_source@root.owner
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				from = {
					is_nomadic = yes
					has_country_flag = checklist_decipher_signal
				}
				owner = {
					is_same_value = event_target:comms_source@from
				}
			}
			from = {
				add_mission_counter = {
					mission =  NOMAD_CHECKLIST_DECIPHER_SIGNAL
					counter = SIGNAL_DECIPHERED
					amount = 1
				}
			}
		}
		if = {
			limit = {
				owner = {
					is_nomadic = yes
					has_country_flag = checklist_decipher_signal
				}
				from = {
					is_same_value = event_target:comms_source@root.owner
				}
			}
			owner = {
				add_mission_counter = {
					mission =  NOMAD_CHECKLIST_DECIPHER_SIGNAL
					counter = SIGNAL_DECIPHERED
					amount = 1
				}
			}
		}
	}
}

####################
# AI Helper Events #
####################

country_event = {
	id = nomads.80
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_ai = yes
		is_nomadic = yes
		last_increased_tech = tech_arkship_construction
	}

	immediate = {
		capital_scope = {
			solar_system = {
				save_event_target_as = target_system
			}
			ship = {
				if = {
					limit = { is_civilian_arkship = yes }
					create_arkship_effect = {
						TYPE = civilian_arkship
						OWNER = root
						LOCATION = event_target:target_system
					}
				}
				else_if = {
					limit = { is_science_arkship = yes }
					create_arkship_effect = {
						TYPE = science_arkship
						OWNER = root
						LOCATION = event_target:target_system
					}
				}
				else_if = {
					limit = { is_military_arkship = yes }
					create_arkship_effect = {
						TYPE = military_arkship
						OWNER = root
						LOCATION = event_target:target_system
					}
				}
			}
		}
	}
}

# on_war_ended: ROOT = Loser, FROM = Main Winner
# on_becoming_subject: ROOT = Subject, FROM = Overlord
country_event = {
	id = nomads.94
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		# A Champions Forge team country is a temporary subject, not a subjugation
		NOT = { is_country_type = champions_forge_contestant }
		OR = {
			is_nomadic = yes
			from = { is_nomadic = yes }
		}
	}

	immediate = {
		country_event = { id = nomads.96 }
		from = { country_event = { id = nomads.96 } }
	}
}

# Re-enable one empire's disabled arkships and waystations
# ROOT = the empire whose vessels are recovered
country_event = {
	id = nomads.96
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		# An arkship still held by an occupier stays disabled until the occupation ends
		every_owned_fleet = {
			limit = {
				is_arkship_fleet = yes
				is_controlled_by = owner
			}
			every_owned_ship = {
				limit = {
					is_arkship_ship = yes
					NOT = { has_ship_flag = disabled_by_gatekeeper_event }
					is_disabled = yes
				}
				repair_amount = 1000
				set_disabled = no
				remove_ship_flag = disabled_by_default_empire_event
			}
		}
		every_owned_fleet = {
			limit = {
				any_owned_ship = { is_waystation_ship = yes  is_disabled = yes }
			}
			every_owned_ship = {
				limit = { is_waystation_ship = yes  is_disabled = yes }
				repair_amount = 1000
				set_disabled = no
			}
		}
	}
}

## Place pops on newly built Arkships
carrier_event = {
	id = nomads.95
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		carrier_is_type = ship
		is_capital = no
	}

	immediate = {
		while = {
			count = 10
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
}

#Notify about losing a waystation
# THIS = Starbase (Not The Ship)
# FROM = Previous Owner Country
starbase_event = {
	id = nomads.99
	hide_window = yes
	is_triggered_only = yes

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

	immediate = {
		create_message = {
			recipient = FROM
			type = MESSAGE_WAYSTATION_LOST
			localization = MESSAGE_WAYSTATION_LOST_DESC
			target = solar_system
			days = @toast_important_message_days
			variable = {
				type = name
				localization = WAYSTATION
				scope = THIS
			}
			variable = {
				type = name
				localization = SYSTEM
				scope = solar_system
			}
		}
	}
}

#######################
# CHAMPIONS FORGE ENCLAVE
#######################

# NOTE: any changes to these event IDs should be reflected in the has_champions_forge_event = {} scripted effect.

# MAIN MENU

# Champions Forge Main Menu
country_event = {
	id = nomads.100
	title = "nomads.100.name"
	desc = {
		text = "nomads.100a.desc"
		text = "nomads.100b.desc"
		text = "nomads.100c.desc"
	}
	# desc_clear = yes
	# option_clear = yes
	event_message_type = ENCLAVE_DIPLOMACY
	is_triggered_only = yes
	diplomatic = yes
	custom_gui = enclave_champions_forge_window
	custom_gui_option = enclave_champions_forge_option
	picture_event_data = {
		portrait = event_target:champions_forge_enclave_country
		room = champions_forge_room
	}
	location = event_target:champions_forge_enclave_fleet

	trigger = {
		exists = event_target:champions_forge_enclave_country
		NOT = {	has_country_flag = enclave_champions_forge_ignores }
	}

	immediate = {
		set_country_flag = enclave_champions_forge_encountered
	}

	# Standard options
	inline_script = enclaves/champions_forge_guardians_options
}

# Champions Forge Enclave Ignore Diplomacy (after stealing from you or being attacked by you)
# via on_custom_diplomacy | from = Champions Forge Enclave country
country_event = {
	id = nomads.105
	title = "nomads.105.name"
	desc = {
		trigger = {
			has_country_flag = enclave_champions_forge_encountered
		}
		text = "nomads.105.desc"
	}
	desc = {
		trigger = {
			NOT = { has_country_flag = enclave_champions_forge_encountered }
		}
		text = "nomads.105.desc.unknown"
	}
	picture = GFX_evt_space_station
	show_sound = event_radio_chatter
	location = event_target:champions_forge_enclave_fleet
	is_triggered_only = yes
	event_message_type = ENCLAVE_DIPLOMACY

	trigger = {
		from = {
			is_country_type = enclave
			has_country_flag = champions_forge_enclave_country
		}
		has_country_flag = enclave_champions_forge_ignores
	}

	option = {
		name = {
			exclusive_trigger = { has_valid_civic = civic_fanatic_purifiers }
			text = "SCUM"
		}
		name = {
			exclusive_trigger = { has_valid_civic = civic_hive_devouring_swarm }
			text = "TASTY"
		}
		name = {
			exclusive_trigger = { has_valid_civic = civic_machine_terminator }
			text = "EXTERMINATE"
		}
		# Previously met the Champions Forge Enclave
		name = {
			trigger = {
				has_country_flag = enclave_champions_forge_encountered
			}
			text = "nomads.105.a"
		}
		# Not met the Champions Forge Enclave yet
		name = {
			trigger = {
				NOT = {
					has_country_flag = enclave_champions_forge_encountered
				}
			}
			text = "PECULIAR"
		}
	}
}

# Champions Forge Stop Invitations
country_event = {
	id = nomads.110
	base = nomads.100
	desc_clear = yes
	option_clear = yes
	desc = "nomads.110.desc"
	event_message_type = ENCLAVE_DIPLOMACY

	# Standard options
	inline_script = enclaves/champions_forge_guardians_options
}

# Champions Forge Enable Invitations
country_event = {
	id = nomads.115
	base = nomads.100
	desc_clear = yes
	option_clear = yes
	desc = "nomads.115.desc"
	event_message_type = ENCLAVE_DIPLOMACY

	# Standard options
	inline_script = enclaves/champions_forge_guardians_options
}

# Champions Forge Introduce a Paragon
country_event = {
	id = nomads.120
	base = nomads.100
	desc_clear = yes
	option_clear = yes
	desc = "nomads.120.desc"
	event_message_type = ENCLAVE_DIPLOMACY


	#Back to menu
	option = {
		name = enclave.410.a
		hidden_effect = {
			country_event = { id = nomads.100 }
		}
	}
	option = {
		name = nomads.120.a
		allow = {
			custom_tooltip = {
				fail_text = nomadic_paragon_cooldown_tt
				NOT = {
					has_country_flag = nomadic_paragon_cooldown
				}
			}
			custom_tooltip = {
				fail_text = nomadic_paragon_already_hired
				NOR = {
					has_country_flag = hired_nomadic_revenant
					has_country_flag = hired_nomadic_sage
					has_country_flag = hired_nomadic_naturalist
					has_country_flag = hired_nomadic_caregiver
					has_country_flag = hired_nomadic_champion
					has_country_flag = hired_nomadic_princess
				}
			}
			custom_tooltip = {
				fail_text = nomadic_paragon_all_hired
				NAND = {
					can_hire_nomadic_paragon_1 = no
					can_hire_nomadic_paragon_2 = no
					can_hire_nomadic_paragon_3 = no
					can_hire_nomadic_paragon_4 = no
					can_hire_nomadic_paragon_5 = no
					can_hire_nomadic_paragon_6 = no
				}
			}
			resource_stockpile_compare = {
				resource = energy
				value >= 10
				mult = root.trigger:empire_size
			}
		}
		add_resource = {
			energy = -10
			mult = root.trigger:empire_size
		}
		custom_tooltip = nomadic_paragon_explanation
		hidden_effect = {
			set_timed_country_flag = {
				flag = nomadic_paragon_cooldown
				days = 3600
			}
			set_saved_date = {
				key = nomadic_paragon_timer
				days_from_present = 3600
				expires = 3600
			}
			country_event = {
				id = nomads.1500
			}
		}
	}
	option = {
		name = GOODBYE
		default_hide_option = yes
	}
}

# Champions Forge Combat Briefing Renewal Reminder
# Scheduled 10 years after a cfl_combat_data purchase
country_event = {
	id = nomads.125
	title = "nomads.125.name"
	desc = "nomads.125.desc"
	picture = GFX_evt_champions_forge
	show_sound = event_radio_chatter
	location = event_target:champions_forge_enclave_fleet
	is_triggered_only = yes
	event_message_type = ENCLAVE_DIPLOMACY

	trigger = {
		exists = event_target:champions_forge_enclave_country
		is_diplomatic = yes
		NOT = { has_country_flag = enclave_champions_forge_ignores }
		NOT = { has_modifier = cfl_combat_data }
		num_rivals > 0
	}

	# Renew the briefing
	option = {
		name = "nomads.125.a"
		inline_script = {
			script = events/resource_cost_with_allow
			RESOURCE = trade
			AMOUNT = 2500
		}
		add_modifier = {
			modifier = cfl_combat_data
			days = 3600
		}
		custom_tooltip = opinion10
		hidden_effect = {
			event_target:champions_forge_enclave_country = {
				add_trust = {
					who = root
					amount = 10
				}
			}
			country_event = {
				id = nomads.125
				days = 3600
			}
		}
	}

	# Decline
	option = {
		name = "nomads.125.b"
		default_hide_option = yes
	}
}

# ESTABLISH COMMUNICATIONS

# Champions Forge gatekeeper event
# via on_custom_diplomacy | this = target country (player); from = country being contacted
country_event = {
	id = nomads.150
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_champions_forge_event = no
		from = {
			has_country_flag = champions_forge_enclave_country
		}
	}

	immediate = {
		if = {
			limit = {
				NOT = { exists = event_target:champions_forge_enclave_country }
			}
			from = {
				save_global_event_target_as = champions_forge_enclave_country
			}
		}
		country_event = { id = nomads.100 }
	}
}

# Communications established (establishing scopes)
# via finish_first_contact_effect / action.11 / leviathans.98
# this = enclave country
country_event = {
	id = nomads.155
	hide_window = yes
	is_triggered_only = yes

	# The 'ignores' flag may be set (indefinitely) during an act of aggression towards the Champions Forge Enclave, OR as a timed flag following certain actions the Champions Forge Enclave takes towards the player.
	immediate = {
		from = {
			if = {
				limit = { has_country_flag = enclave_champions_forge_ignores }
				country_event = { id = nomads.105 } # Champions Forge refuse comms.
			}
			else = {
				country_event = { id = nomads.160 } # Champions Forge introduction
			}
		}
	}
}

# Champions Forge Introduction
# this = player country | from = Champions Forge Enclave country
country_event = {
	id = nomads.160
	title = "nomads.100.name"
	desc = "nomads.160.desc"
	event_message_type = ENCLAVE_DIPLOMACY

	is_triggered_only = yes
	diplomatic = yes
	custom_gui = enclave_champions_forge_window
	custom_gui_option = enclave_champions_forge_option
	picture_event_data = {
		portrait = from
		room = champions_forge_room
	}
	location = event_target:champions_forge_enclave_fleet

	option = {
		name = {
			trigger = { has_valid_civic = civic_fanatic_purifiers }
			text = "SCUM"
		}
		name = {
			trigger = { has_valid_civic = civic_hive_devouring_swarm }
			text = "TASTY"
		}
		name = {
			trigger = { has_valid_civic = civic_machine_terminator }
			text = "EXTERMINATE"
		}
		name = {
			trigger = {
				NOR = {
					has_valid_civic = civic_fanatic_purifiers
					has_valid_civic = civic_hive_devouring_swarm
					has_valid_civic = civic_machine_terminator
				}
			}
			text = "INTERESTING"
		}
		default_hide_option = yes
	}
}

# Spawn the Champions Forge enclave
# via on_galactic_community_formed
country_event = {
	id = nomads.200
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_nomads_dlc = yes
		NOT = { has_global_flag = champions_forge_spawned }
	}

	immediate = {
		create_champions_forge = yes
		if = {
			limit = { exists = event_target:champions_forge_enclave_country }
			set_global_flag = champions_forge_spawned
		}
	}
}

# Champions Forge invitation to tournament
country_event = {
	id = nomads.250
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOT = {
			has_country_flag = champions_forge_tournament_ongoing
		}
	}

	immediate = {
		event_target:champions_forge_enclave_fleet.solar_system = {
			save_global_event_target_as = champions_forge_enclave_system
		}
		set_country_flag = champions_forge_tournament_ongoing
		if = {
			limit = { has_country_flag = champions_of_the_forge_weight_class_heavy_late }
			remove_country_flag = champions_of_the_forge_weight_class_heavy_late
		}
		if = {
			limit = { has_country_flag = champions_of_the_forge_weight_class_medium_late }
			remove_country_flag = champions_of_the_forge_weight_class_medium_late
		}
		if = {
			limit = { has_country_flag = champions_of_the_forge_weight_class_medium_mid }
			remove_country_flag = champions_of_the_forge_weight_class_medium_mid
		}
		if = {
			limit = { has_country_flag = champions_of_the_forge_weight_class_small_mid }
			remove_country_flag = champions_of_the_forge_weight_class_small_mid
		}
		if = {
			limit = { has_country_flag = champions_of_the_forge_weight_class_small_early }
			remove_country_flag = champions_of_the_forge_weight_class_small_early
		}
		change_variable = { which = cfl_tournament_count value = 1 }
		random_list = {
			1 = { # Fleet Size 500
				modifier = {
					factor = 2
					check_variable = { which = cfl_tournament_count value >= 5 }
				}
				modifier = {
					factor = 0
					check_variable = { which = cfl_tournament_count value <= 3 }
				}
				set_country_flag = champions_of_the_forge_weight_class_heavy_late
			}
			1 = { # Fleet Size 200
				modifier = {
					factor = 2
					check_variable = { which = cfl_tournament_count value = 4 }
				}
				modifier = {
					factor = 0
					check_variable = { which = cfl_tournament_count value <= 2 }
				}
				set_country_flag = champions_of_the_forge_weight_class_medium_late
			}
			1 = { # Fleet Size 100
				modifier = {
					factor = 0
					check_variable = { which = cfl_tournament_count value >= 5 }
				}
				modifier = {
					factor = 2
					check_variable = { which = cfl_tournament_count value = 3 }
				}
				modifier = {
					factor = 0
					check_variable = { which = cfl_tournament_count value = 1 }
				}
				set_country_flag = champions_of_the_forge_weight_class_medium_mid
			}
			1 = { # Fleet Size 50
				modifier = {
					factor = 0
					check_variable = { which = cfl_tournament_count value >= 4 }
				}
				modifier = {
					factor = 2
					check_variable = { which = cfl_tournament_count value = 2 }
				}
				set_country_flag = champions_of_the_forge_weight_class_small_mid
			}
			1 = { # Fleet Size 25
				modifier = {
					factor = 0
					check_variable = { which = cfl_tournament_count value >= 3 }
				}
				modifier = {
					factor = 2
					check_variable = { which = cfl_tournament_count value = 1 }
				}
				set_country_flag = champions_of_the_forge_weight_class_small_early
			}
		}
		every_relation = {
			limit = {
				is_country_type = default
				has_communications = prev
				is_ai = no
				is_diplomatic = yes
				is_homicidal = no
				is_level_4_player_crisis = no
				NOR = {
					has_country_flag = champions_forge_stop_invitations
					has_event_chain = champions_forge_tournament_chain
				}
			}
			event_target:champions_forge_enclave_system = {
				set_surveyed = {
					surveyed = yes
					surveyor = prev
				}
			}
			country_event = {
				id = nomads.255
				days = 1 # Delay one day to let the previous system survey occur
			}
		}

		event_target:champions_forge_enclave_system = {
			system_event = {
				id = nomads.260
				months = 12
			}
		}
	}
}

# Hey Country, do you want to join the tournament?
country_event = {
	id = nomads.255
	title = "nomads.255.name"
	desc = {
		text = "nomads.255.desc"
		trigger = { NOT = { has_relic = r_cfl_trophy } }
	}
	desc = {
		text = "nomads.255.desc.relic"
		trigger = { has_relic = r_cfl_trophy }
	}
	picture = GFX_evt_champions_forge
	show_sound = event_public_unrest
	event_chain = champions_forge_tournament_chain
	location = capital_scope.solar_system
	is_triggered_only = yes

	immediate = {
		begin_event_chain = {
			event_chain = champions_forge_tournament_chain
			target = this
		}
	}

	option = {
		name = "nomads.255.a"
		custom_tooltip = "nomads.255.a.tooltip"
		# Only players get the special project, AI empires are spawned when the first player finishes the project
		enable_special_project = {
			name = "CHAMPIONS_FORGE_TRAVEL"
			location = capital_scope.solar_system.star
			owner = ROOT
		}
		set_country_flag = champions_forge_interest
		remove_country_flag = champions_forge_stop_invitations
		set_country_flag = champions_forge_send_invitations
	}
	option = {
		name = "nomads.255.b"
		end_event_chain = champions_forge_tournament_chain
	}
	option = {
		name = "nomads.255.c"
		custom_tooltip = "nomads.255.c.tooltip"
		remove_country_flag = champions_forge_send_invitations
		set_country_flag = champions_forge_stop_invitations
		end_event_chain = champions_forge_tournament_chain
	}

	after = {
		if = {
			limit = {
				has_relic = r_cfl_trophy
			}
			remove_relic = r_cfl_trophy
		}
	}
}

# Champions Forge start tournament
system_event = {
	id = nomads.260
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		from = {
			set_country_flag = champions_forge_battle_started
			every_relation = {
				limit = {
					is_ai = yes
					is_country_type = default
					is_diplomatic = yes
				}
				if = { # We want a minimum of contestants of either the amount of countires the Enclave knows or 12
					limit = {
						root = {
							count_fleet_in_system = {
								count < 12
								limit = {
									controller = {
										is_country_type = champions_forge_contestant
									}
									is_disabled = no
								}
							}
						}
					}
					event_target:champions_forge_enclave_country = {
    					switch = {
     						trigger = has_country_flag
        						champions_of_the_forge_weight_class_heavy_late 	= {
            						prev = {
            							create_ai_champions_forge_contestant = 	{ FLEET_SIZE = 500 }
            						}
        						}
        						champions_of_the_forge_weight_class_medium_late 	= {
            						prev = {
        						        create_ai_champions_forge_contestant = 	{ FLEET_SIZE = 200 }
        						    }
        						}
        						champions_of_the_forge_weight_class_medium_mid 	= {
        						    prev = {
        						        create_ai_champions_forge_contestant = 	{ FLEET_SIZE = 100 }
        						    }
        						}
								champions_of_the_forge_weight_class_small_mid = 	{
        						    prev = {
        						        create_ai_champions_forge_contestant = 	{ FLEET_SIZE = 50 }
        						    }
        						}
								champions_of_the_forge_weight_class_small_early 	= {
        						    prev = {
        						        create_ai_champions_forge_contestant = 	{ FLEET_SIZE = 25 }
        						    }
        						}
						}
					}
				}
			}
		}
		if = {
			limit = {
				count_fleet_in_system = {
					count >= 2
					limit = {
						controller = {
							is_country_type = champions_forge_contestant
						}
						is_disabled = no
					}
				}
			}
			every_playable_country = {
				limit = {
					has_country_flag = champions_forge_contestant_country
				}
				remove_country_flag = champions_forge_contestant_country
				country_event = {
					id = nomads.270
				}
			}

			every_fleet_in_system = {
				limit = {
					has_fleet_flag = champions_forge_contestant_fleet
				}
				clear_orders = yes
				set_aggro_range_measure_from = self
				set_aggro_range = 1000
				set_fleet_stance = aggressive
			}

			every_country = {
				limit = {
					is_country_type = champions_forge_contestant
				}
				country_event = {
					id = nomads.265
				}
			}

			# Fallback 3 years later if no winner is declared
			system_event = {
				id = nomads.281
				years = 3
			}
		}
		else_if = {
			limit = {
				count_fleet_in_system = {
					count <= 1
					limit = {
						controller = {
							is_country_type = champions_forge_contestant
						}
						is_disabled = no
					}
				}
			}
			every_country = {
				limit = {
					is_country_type = champions_forge_contestant
				}
				country_event = {
					id = nomads.266
				}
			}
			# Not enough participants, return to flying around the galaxy
			event_target:champions_forge_enclave_fleet = {
				set_timed_fleet_flag = {
					flag = champions_forge_match_cooldown
					years = 20
				}
				champions_forge_departure_effect = yes
			}
		}
	}
}

# Champions Forge set every champion country hostile to each other
country_event = {
	id = nomads.265
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_country = {
			limit = {
				is_country_type = champions_forge_contestant
				NOT = { is_same_value = root }
			}
			set_faction_hostility = {
				target = root
				set_hostile = yes
				set_neutral = no
				set_friendly = no
			}
			set_hostile = root
		}
	}
}

# If any fleets are here we tell the owners that the fight isn't happening
country_event = {
	id = nomads.266
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		overlord? = {
			create_message = {
				type = CFL_NO_TOURNAMENT
				localization = CFL_NO_TOURNAMENT_DESC
				days = 30
			}
		}
		every_controlled_fleet = {
			limit = {
				has_fleet_flag = player_champions_forge_contestant_fleet
			}
			remove_fleet_flag = champions_forge_contestant_fleet
			remove_fleet_flag = player_champions_forge_contestant_fleet
			owner = {
				if = {
					limit = {
						any_owned_leader = {
							has_leader_flag = temp_contestant_admiral
						}
					}
					random_owned_leader = {
						limit = { has_leader_flag = temp_contestant_admiral }
						kill_leader = { show_notification = no }
					}
				}
			}
			end_fleet_contract = {
				initiator = this.controller
				reason = cancelled
			}
			every_owned_ship = {
				set_disable_at_health = no
			}
			clear_orders = yes
			set_mia = mia_return_home
			set_mia_return_delay = 180
		}
		every_controlled_fleet = {
			limit = {
				has_fleet_flag = champions_forge_contestant_fleet
			}
			destroy_fleet = this
		}
	}
}

# Notify countries that sent a team that the tournament starts now
country_event = {
	id = nomads.270
	title = "nomads.270.name"
	desc = "nomads.270.desc"
	picture = GFX_evt_champions_forge
	show_sound = event_public_unrest
	location = event_target:champions_forge_enclave_system
	event_chain = champions_forge_tournament_chain
	is_triggered_only = yes

	option = {
		name = EXCELLENT
	}
}

# Put a flag on the third, second, and first place teams.
# Fired by: on_space_battle_over
# This = owner of fleet 1 (winner)
# From = owner of fleet 2 (loser)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = nomads.275
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = champions_forge_contestant
	}

	immediate = {
		# from = losing country
		from = {
			# fromfrom = winning fleet
			from = {
				# fromfromfrom = losing fleet
				from = {
					if = {
						limit = {
							solar_system = {
								count_fleet_in_system = {
									count = 2
									limit = {
										controller = {
											is_country_type = champions_forge_contestant
										}
										is_disabled = no
									}
								}
							}
						}
						set_fleet_flag = champions_forge_third_place
					}
					else_if = {
						limit = {
							solar_system = {
								count_fleet_in_system = {
									count = 1
									limit = {
										controller = {
											is_country_type = champions_forge_contestant
										}
										is_disabled = no
									}
								}
							}
						}
						set_fleet_flag = champions_forge_second_place
						prev = {
							set_fleet_flag = champions_forge_first_place
						}
						solar_system = {
							system_event = {
								id = nomads.280
								days = 30
							}
						}
					}
				}
			}
		}
	}
}

# Notify the winning countries, return the fleets to their owners, and destroy the team countries
system_event = {
	id = nomads.280
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		# Notify winners and losers
		every_fleet_in_system = {
			controller = {
				if = {
					limit = { is_subject = yes }
					save_event_target_as = cfl_wrapper
					random_playable_country = {
						limit = {
							event_target:cfl_wrapper = {
								has_relation_flag = {
									who = prev
									flag = cfl_parent_empire
								}
							}
						}
						save_event_target_as = cfl_contestant_parent
					}
				}
			}
			switch = {
				trigger = has_fleet_flag
				champions_forge_third_place = {
					remove_fleet_flag = champions_forge_third_place
					event_target:cfl_contestant_parent = {
						country_event = { id = nomads.285 }
					}
				}
				champions_forge_second_place = {
					remove_fleet_flag = champions_forge_second_place
					event_target:cfl_contestant_parent = {
						country_event = { id = nomads.290 }
					}
				}
				champions_forge_first_place = {
					remove_fleet_flag = champions_forge_first_place
					event_target:cfl_contestant_parent = {
						country_event = { id = nomads.295 }
					}
				}
				default = {
					if = {
						limit = {
							has_fleet_flag = champions_forge_contestant_fleet
						}
						event_target:cfl_contestant_parent = {
							country_event = { id = nomads.305 }
						}
					}
				}
			}
		}
		champions_forge_tournament_cleanup_effect = yes
	}
}

# Fallback conclusion: fired 3 years after a tournament begins (nomads.260).
# If it still has not concluded, declare no winner and clean everything up.
system_event = {
	id = nomads.281
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		event_target:champions_forge_enclave_country = {
			has_country_flag = champions_forge_tournament_ongoing
		}
	}

	immediate = {
		# Tell every still-participating empire that no winner could be determined
		every_playable_country = {
			limit = {
				has_event_chain = champions_forge_tournament_chain
			}
			country_event = { id = nomads.306 }
		}
		champions_forge_tournament_cleanup_effect = yes
	}
}

# Third place notification
country_event = {
	id = nomads.285
	title = "nomads.285.name"
	desc = "nomads.285.desc"
	picture = GFX_evt_champions_forge
	show_sound = event_public_unrest
	location = event_target:champions_forge_enclave_system
	event_chain = champions_forge_tournament_chain
	is_triggered_only = yes

	option = {
		name = nomads.285.a
		add_resource = { trade = 1000 }
		end_event_chain = champions_forge_tournament_chain
		custom_tooltip = opinion10
		hidden_effect = {
			event_target:champions_forge_enclave_country = {
				add_trust = {
					who = root
					amount = 10
				}
			}
		}
	}
}

# Second place notification
country_event = {
	id = nomads.290
	title = "nomads.290.name"
	desc = "nomads.290.desc"
	picture = GFX_evt_champions_forge
	show_sound = event_public_unrest
	location = event_target:champions_forge_enclave_system
	event_chain = champions_forge_tournament_chain
	is_triggered_only = yes

	option = {
		name = nomads.290.a
		add_resource = { trade = 2000 }
		end_event_chain = champions_forge_tournament_chain
		custom_tooltip = opinion15
		hidden_effect = {
			event_target:champions_forge_enclave_country = {
				add_trust = {
					who = root
					amount = 15
				}
			}
		}
	}
}

# First place notification
country_event = {
	id = nomads.295
	title = "nomads.295.name"
	desc = "nomads.295.desc"
	picture = GFX_evt_champions_forge
	show_sound = event_public_unrest
	location = event_target:champions_forge_enclave_system
	event_chain = champions_forge_tournament_chain
	is_triggered_only = yes

	option = {
		name = nomads.295.a
		add_resource = { trade = 5000 }
		if = { #This should never happen
			limit = {
				NOT = { has_relic = r_cfl_trophy }
			}
			add_relic = r_cfl_trophy
		}
		hidden_effect = {
			inverted_switch = {
				trigger = has_global_flag
				cfl_winner_1_set = {
					random_list = {
						1 = { #25% of the time a Fallen Empire has already won this once
							modifier = {
								factor = 0
								count_country = {
									count =  0
									limit = {
										is_fallen_empire = yes
									}
								}
							}
							random_country = {
								limit = {
									is_fallen_empire = yes
								}
								save_global_event_target_as = cfl_winner_1
							}
							save_global_event_target_as = cfl_winner_2
							set_global_flag = cfl_winner_1_set
							set_global_flag = cfl_winner_2_set
						}
						3 = { # Most of the time they have not won
							save_global_event_target_as = cfl_winner_1
							set_global_flag = cfl_winner_1_set
						}
					}
				}
				cfl_winner_2_set = {
					save_global_event_target_as = cfl_winner_2
					set_global_flag = cfl_winner_2_set
				}
				cfl_winner_3_set = {
					save_global_event_target_as = cfl_winner_3
					set_global_flag = cfl_winner_3_set
				}
				cfl_winner_4_set = {
					save_global_event_target_as = cfl_winner_4
					set_global_flag = cfl_winner_4_set
				}
				cfl_winner_5_set = {
					save_global_event_target_as = cfl_winner_5
					set_global_flag = cfl_winner_5_set
				}
				cfl_winner_6_set = {
					save_global_event_target_as = cfl_winner_6
					set_global_flag = cfl_winner_6_set
				}
				cfl_winner_7_set = {
					save_global_event_target_as = cfl_winner_7
					set_global_flag = cfl_winner_7_set
				}
				cfl_winner_8_set = {
					save_global_event_target_as = cfl_winner_8
					set_global_flag = cfl_winner_8_set
				}
				cfl_winner_9_set = {
					save_global_event_target_as = cfl_winner_9
					set_global_flag = cfl_winner_9_set
				}
				cfl_winner_10_set = {
					save_global_event_target_as = cfl_winner_10
					set_global_flag = cfl_winner_10_set
				}
				cfl_winner_11_set = {
					save_global_event_target_as = cfl_winner_11
					set_global_flag = cfl_winner_11_set
				}
				cfl_winner_12_set = {
					save_global_event_target_as = cfl_winner_12
					set_global_flag = cfl_winner_12_set
				}
				default = {}
			}
		}
		end_event_chain = champions_forge_tournament_chain
		custom_tooltip = opinion30
		hidden_effect = {
			event_target:champions_forge_enclave_country = {
				add_trust = {
					who = root
					amount = 30
				}
			}
		}
	}
}

# Losers notification
country_event = {
	id = nomads.305
	title = "nomads.305.name"
	desc = "nomads.305.desc"
	picture = GFX_evt_champions_forge
	show_sound = event_public_unrest
	location = event_target:champions_forge_enclave_system
	event_chain = champions_forge_tournament_chain
	is_triggered_only = yes

	option = {
		name = nomads.305.a
		end_event_chain = champions_forge_tournament_chain
		custom_tooltip = opinion5
		hidden_effect = {
			event_target:champions_forge_enclave_country = {
				add_trust = {
					who = root
					amount = 5
				}
			}
		}
	}
}

# No winner notification (fallback timeout, nomads.281)
country_event = {
	id = nomads.306
	title = "nomads.306.name"
	desc = "nomads.306.desc"
	picture = GFX_evt_champions_forge
	show_sound = event_public_unrest
	location = event_target:champions_forge_enclave_system
	event_chain = champions_forge_tournament_chain
	is_triggered_only = yes

	option = {
		name = nomads.306.a
		end_event_chain = champions_forge_tournament_chain
	}
}

country_event = {
	id = nomads.310
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_modifier = cfl_trophy
			has_modifier = tarnished_cfl_trophy
		}
	}

	immediate = {
		remove_modifier = cfl_trophy
		remove_modifier = tarnished_cfl_trophy
	}
}

# CHAMPIONS FORGE LIVE! DESTROYED

# This = owner of ship 1 (destroyed)
# From = owner of ship 2 (combatant)
# FromFrom = ship 1
# FromFromFrom = ship 2 # can be invalid if damage is done via other methods (like scripted action)
country_event = {
	id = nomads.400
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = enclave
		has_country_flag = champions_forge_enclave_country
		FROMFROM = {
			is_ship_size = military_arkship_champions_forge
		}
	}

	immediate = {
		FROMFROM = {
			solar_system = { save_event_target_as = champions_forge_destruction_system }
		}
		FROM = {
			set_country_flag = champions_forge_destroyer
			country_event = { id = nomads.405 }
			save_event_target_as = champions_forge_destroyer
		}
		every_playable_country = {
			limit = {
				has_communications = ROOT
				NOT = { is_country = event_target:champions_forge_destroyer }
			}
			country_event = { id = nomads.410 }
		}
		random_playable_country = {
			observer_event = { id = observer.105 }
		}
	}
}

country_event = {
	id = nomads.405
	title = nomads.100.name
	desc = {
		text = nomads.405.desc
		trigger = { NOT = { has_relic = r_cfl_trophy } }
	}
	desc = {
		text = nomads.405.desc.relic
		trigger = { has_relic = r_cfl_trophy }
	}
	location = event_target:champions_forge_destruction_system
	picture = GFX_evt_arkship_crisis
	show_sound = event_arkship_in_crisis
	is_triggered_only = yes

	immediate = {
		hidden_effect = {
			if = {
				limit = { has_event_chain = champions_forge_tournament_chain }
				end_event_chain = champions_forge_tournament_chain
			}
		}
	}

	option = {
		name = nomads.410.a
		add_resource = {
			alloys = 10000
			trade = 3000
		}
	}

	after = {
		if = {
			limit = {
				has_relic = r_cfl_trophy
			}
			remove_relic = r_cfl_trophy
			add_relic = r_tarnished_cfl_trophy
		}
	}
}

country_event = {
	id = nomads.410
	title = nomads.100.name
	desc = {
		text = nomads.410.desc
		trigger = { NOT = { has_relic = r_cfl_trophy } }
	}
	desc = {
		text = nomads.410.desc.relic
		trigger = { has_relic = r_cfl_trophy }
	}
	location = event_target:champions_forge_destruction_system
	picture = GFX_evt_arkship_crisis
	show_sound = event_arkship_in_crisis
	is_triggered_only = yes

	immediate = {
		hidden_effect = {
			if = {
				limit = { has_event_chain = champions_forge_tournament_chain }
				end_event_chain = champions_forge_tournament_chain
			}
		}
	}

	option = {
		exclusive_trigger = {
			has_country_flag = enclave_champions_forge_ignores
		}
		name = OK
	}
	option = {
		name = nomads.410.a
	}

	after = {
		if = {
			limit = {
				has_relic = r_cfl_trophy
			}
			remove_relic = r_cfl_trophy
			add_relic = r_tarnished_cfl_trophy
		}
	}
}

#############################
##### Heirs of the Khan #####
#### By Victor Haeggman #####
#############################

country_event = {
	id = nomads.500
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_heirs_of_the_khan
		NOT = { has_country_flag = won_khan_duel }
		exists = event_target:marauder_heir
	}

	immediate = {
		random_list = {
			16 = { #We did not spawn hunters today
				country_event = { id = nomads.500 days = 200 random = 100 }
			}
			5 = { #Spawning hunters
				modifier = {
					factor = 0
					capital_scope = { has_carrier_flag = nomad_recent_combat }
				}
				modifier = {
					factor = 0
					capital_scope = { has_carrier_flag = nomad_recent_encamped }
				}
				modifier = {
					factor = 0
					capital_scope = { has_carrier_flag = nomad_recent_accepted_contract }
				}
				modifier = {
					factor = 0
					capital_scope = { has_carrier_flag = nomad_recent_war }
				}
				modifier = {
					factor = 0
					root.owner = {
						has_country_flag = intimidated_marauder_hunters
					}
				}
				country_event = { id = nomads.505 }
			}
			3 = { #Spawn Contract
				modifier = {
					factor = 0
					check_variable = {
						which = defeated_heirs_hunters_spawner
						value = 0
					}
				}
				modifier = {
					factor = 0
					any_issued_mission = {
						OR = {
							is_mission_type = contract_intercept_raid_heirs
							is_mission_type = contract_destroy_station_heirs
						}
					}
				}
				modifier = {
					factor = 0
					root.owner = {
						has_country_flag = intimidated_marauder_hunters
					}
				}
				random_system = {
					limit = {
						distance = {
							source = root.capital_scope.solar_system
							min_jumps = 1
							max_jumps = 5
						}
						NOR = {
							has_star_flag = hostile_system
							has_star_flag = guardian
							has_star_flag = marauder_system
							has_star_flag = lcluster1
							has_star_flag = lcluster2
							has_star_flag = lcluster3
							has_star_flag = lcluster4
							has_star_flag = lcluster5
							has_star_flag = lcluster_lgate
							is_fe_cluster = yes
						}
						root = {
							intel_level = {
								level > none
								system = prev
							}
						}
					}
					save_event_target_as = contract_system
				}
				random_list = {
					1 = {
						modifier = {
							factor = 0
							has_country_flag = contract_intercept_raid_heirs_issued
						}
						root = {
							issue_contract = {
								contract = contract_intercept_raid_heirs
								location = event_target:contract_system
								target = root
							}
							set_timed_country_flag = {
								flag = contract_intercept_raid_heirs_issued
								days = 2000 # Avoiding getting the same one twice
							}
						}
					}
					1 = {
						modifier = {
							factor = 0
							has_country_flag = contract_destroy_station_heirs_issued
						}
						root = {
							issue_contract = {
								contract = contract_destroy_station_heirs
								location = event_target:contract_system
								target = root
							}
							set_timed_country_flag = {
								flag = contract_destroy_station_heirs_issued
								days = 2000 # Avoiding getting the same one twice
							}
						}
					}
				}
			}
		}
	}
}

country_event = {
	id = nomads.501
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		random_system = {
			limit = {
				distance = {
					source = root.capital_star
					use_bypasses = no
					type = hyperlane
					min_jumps = 1
					max_jumps = 5
				}
				NOR = {
					has_star_flag = hostile_system
					has_star_flag = guardian
					has_star_flag = marauder_system
					has_star_flag = lcluster1
					has_star_flag = lcluster2
					has_star_flag = lcluster3
					has_star_flag = lcluster4
					has_star_flag = lcluster5
					has_star_flag = lcluster_lgate
					is_fe_cluster = yes
				}
				root = {
					intel_level = {
						level > none
						system = prev
					}
				}
			}
			save_event_target_as = contract_system
		}
		issue_contract = {
			contract = contract_destroy_station_heirs_start
			location = event_target:contract_system
			target = this
		}
	}
}

country_event = {
	id = nomads.505
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = { #Clean up the old fleet if it's still around
				exists = event_target:hunter_fleet
				event_target:hunter_fleet = {
					is_in_combat = no
				}
			}
			event_target:hunter_fleet = {
				delete_fleet = this
			}
		}

		capital_scope.ship.fleet = {
			save_event_target_as = hunted_fleet
			set_fleet_flag = hunted_fleet
			solar_system = {
				closest_system = {
					limit = {
						NOR = {
							has_star_flag = guardian
							has_star_flag = hostile_system
						}
					}
					use_bypasses = no
					min_steps = 2
					max_steps = 5
					save_event_target_as = spawning_system
				}
			}

			owner = {
				if = { #Giving the hunters access to more ship designs as the variable increases
					limit = {
						root = {
							check_variable = {
								which = defeated_heirs_hunters
								value < 5
							}
						}
					}
					create_balanced_fleet = {
						name = "NAME_Hunting_Fleet"
						size = root.value:heirs_hunter_fleet_size
						ship_designs = {
							NAME_Outrider
						}
						effect = {
							set_owner = event_target:marauder_heir
							set_location = event_target:spawning_system
							set_fleet_stance = aggressive
							set_fleet_bombardment_stance = indiscriminate
							set_aggro_range_measure_from = self
							set_aggro_range = 150
							set_owner = event_target:marauder_hunters #Real Owner
							save_event_target_as = hunter_fleet
							set_fleet_flag = heirs_hunter_fleet
							set_fleet_settings = {
								spawn_debris = no
							}

							queue_actions = {
								repeat = {
									find_random_fleet = {
										trigger = {
											id = "heirs_100"
											is_same_value = event_target:hunted_fleet
										}
										found_fleet = {
											move_to = this
										}
									}
								}
							}
						}
					}
				}
				else_if = {
					limit = {
						root = {
							check_variable = {
								which = defeated_heirs_hunters
								value >= 5
							}
							check_variable = {
								which = defeated_heirs_hunters
								value < 10
							}
						}
					}
					create_balanced_fleet = {
						name = "NAME_Hunting_Fleet"
						size = root.value:heirs_hunter_fleet_size
						ship_designs = {
							NAME_Outrider
							NAME_Lancer
						}
						effect = {
							set_owner = event_target:marauder_heir
							set_location = event_target:spawning_system
							set_fleet_stance = aggressive
							set_fleet_bombardment_stance = indiscriminate
							set_aggro_range_measure_from = self
							set_aggro_range = 150
							set_owner = event_target:marauder_hunters #Real Owner
							save_event_target_as = hunter_fleet
							set_fleet_flag = heirs_hunter_fleet
							set_fleet_settings = {
								spawn_debris = no
							}

							queue_actions = {
								repeat = {
									find_random_fleet = {
										trigger = {
											id = "heirs_101"
											is_same_value = event_target:hunted_fleet
										}
										found_fleet = {
											move_to = this
										}
									}
								}
							}
						}
					}
				}
				else = {
					create_balanced_fleet = {
						name = "NAME_Hunting_Fleet"
						size = root.value:heirs_hunter_fleet_size
						ship_designs = {
							NAME_Outrider
							NAME_Lancer
							NAME_Void_Champion
						}
						effect = {
							set_owner = event_target:marauder_heir
							set_location = event_target:spawning_system
							set_fleet_stance = aggressive
							set_fleet_bombardment_stance = indiscriminate
							set_aggro_range_measure_from = self
							set_aggro_range = 150
							set_owner = event_target:marauder_hunters #Real Owner
							save_event_target_as = hunter_fleet
							set_fleet_flag = heirs_hunter_fleet
							set_fleet_settings = {
								spawn_debris = no
							}

							queue_actions = {
								repeat = {
									find_random_fleet = {
										trigger = {
											id = "heirs_102"
											is_same_value = event_target:hunted_fleet
										}
										found_fleet = {
											move_to = this
										}
									}
								}
							}
						}
					}
				}
				event_target:hunter_fleet = {
					owner = {
						set_faction_hostility = {
							target = root
							set_hostile = yes
							set_neutral = no
						}
					}
					random_list = {
						10 = { #No special interaction

						}
						5 = { #Negotiate at loss effect
							modifier = {
								factor = 0
								root.owner = { # No special interaction if you haven't defeated any yet
									check_variable = {
										which = defeated_heirs_hunters
										value = 0
									}
								}
							}
							modifier = {
								add = root.owner.defeated_heirs_hunters
							}
							every_owned_ship = {
								set_disable_at_health = 0.01
								set_ship_flag = marauder_hunter
							}
							set_fleet_flag = will_surrender_heirs
						}
						5 = { #Negotiation at start effect
							modifier = {
								factor = 0
								root.owner = {
									check_variable = {
										which = defeated_heirs_hunters
										value = 0
									}
								}
							}
							modifier = {
								add = root.owner.defeated_heirs_hunters
							}
							root.owner = {
								set_relation_flag = {
									who = prev.owner
									flag = negotiate_on_encounter
								}
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				has_country_flag = heirs_seen_first_hunters
			}
			create_message = {
				type = HEIRS_HUNTERS_HAVE_SPAWNED
				localization = HEIRS_HUNTERS_HAVE_SPAWNED_DESC
				days = 30
				variable = {
					type = name
					localization = ARKSHIP
					scope = capital_scope
				}
			}
		}
		else = {
			country_event = {
				id = nomads.506
			}
		}

		country_event = {
			id = nomads.500
			days = 1000
			random = 600
		}
	}
}

country_event = {
	id = nomads.506
	title = nomads.506.name
	desc = nomads.506.desc
	picture = GFX_evt_pirate_armada
	show_sound = event_sensor_ping
	location = event_target:hunter_fleet

	is_triggered_only = yes

	immediate = {
		set_country_flag = heirs_seen_first_hunters
	}

	option = {
		name = BATTLESTATIONS
		add_modifier = {
			modifier = heirs_battlestations
			days = 360
		}
	}

	option = {
		name = EVASIVE_ACTIONS
		add_modifier = {
			modifier = heirs_evasive_actions
			days = 360
		}
	}
}

fleet_event = {
	id = nomads.510
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_arkship_fleet = yes
		owner? = {
			is_country_type = default
		}
	}

	immediate = {
		owner.capital_scope = {
			set_timed_carrier_flag = {
				flag = nomad_recent_combat
				days = 60
			}
		}
	}
}

country_event = {
	id = nomads.515
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
	}

	immediate = {
		set_timed_country_flag = {
			flag = nomad_recent_war
			days = 60
		}
	}
}

# A fleet has entered a system | See also: on_fleet_auto_move_arrival
# Scope = Fleet
# From = System
fleet_event = {
	id = nomads.520
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_arkship_fleet = yes
		owner? = {
			has_origin = origin_heirs_of_the_khan
		}
		from = {
			has_star_flag = intercept_raid@root.owner
		}
	}

	immediate = {
		save_event_target_as = heirs_fleet
		from = {
			remove_star_flag = intercept_raid@root.owner #prevent the event from happening multiple times
		}
		owner = {
			remove_point_of_interest = intercept_raid@root.owner
			create_message = {
				type = HEIRS_RAID_INTERCEPTED
				localization = HEIRS_RAID_INTERCEPTED_DESC
				days = 30
				variable = {
					type = name
					localization = SYSTEM
					scope = from
				}
			}
			if = { #Giving the hunters access to more ship designs as the variable increases
				limit = {
					check_variable = {
						which = defeated_heirs_hunters
						value < 5
					}
				}
				create_balanced_fleet = {
					name = "NAME_Hunting_Fleet"
					size = root.owner.value:heirs_hunter_fleet_size_small
					ship_designs = {
						NAME_Outrider
					}
					effect = {
						marauder_heir_contract_effect = yes
						queue_actions = {
							find_random_fleet = {
								trigger = {
									id = "intercepting_raid_fleet_1"
									is_same_value = event_target:heirs_fleet
								}
								found_fleet = {
									move_to = this
								}
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					check_variable = {
						which = defeated_heirs_hunters
						value >= 5
					}
					check_variable = {
						which = defeated_heirs_hunters
						value < 10
					}
				}
				create_balanced_fleet = {
					name = "NAME_Hunting_Fleet"
					size = root.owner.value:heirs_hunter_fleet_size_small
					ship_designs = {
						NAME_Outrider
						NAME_Lancer
					}
					effect = {
						marauder_heir_contract_effect = yes
						queue_actions = {
							find_random_fleet = {
								trigger = {
									id = "intercepting_raid_fleet_2"
									is_same_value = event_target:heirs_fleet
								}
								found_fleet = {
									move_to = this
								}
							}
						}
					}
				}
			}
			else = {
				create_balanced_fleet = {
					name = "NAME_Hunting_Fleet"
					size = root.owner.value:heirs_hunter_fleet_size_small
					ship_designs = {
						NAME_Outrider
						NAME_Lancer
						NAME_Void_Champion
					}
					effect = {
						marauder_heir_contract_effect = yes
						queue_actions = {
							find_random_fleet = {
								trigger = {
									id = "intercepting_raid_fleet_3"
									is_same_value = event_target:heirs_fleet
								}
								found_fleet = {
									move_to = this
								}
							}
						}
					}
				}
			}
		}
	}
}

# This = owner of fleet 1 (destroyed)
# From = owner of fleet 2 (combatant)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = nomads.525
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_country_flag = heirs_marauder_hunters
		fromfrom = {
			has_fleet_flag = heirs_raider_fleet
		}
	}

	immediate = {
		random_relation = {
			limit = {
				reverse_has_relation_flag = {
					who = root
					flag = contract_heirs_hunter_raid
				}
			}
			root = {
				remove_relation_flag = {
					who = prev
					flag = contract_heirs_hunter_raid
				}
			}
			add_mission_counter = {
				mission = contract_intercept_raid_heirs
				counter = contract_progress_counter
				amount = 1
			}
		}
	}
}

# This = owner of fleet 1 (destroyed)
# From = owner of fleet 2 (combatant)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = nomads.530
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_country_flag = heirs_marauder_hunters
		fromfrom = {
			OR = {
				has_fleet_flag = heirs_raider_starbase
				has_fleet_flag = heirs_raider_starbase_start
			}
		}
	}

	immediate = {
		random_relation = {
			limit = {
				reverse_has_relation_flag = {
					who = root
					flag = contract_heirs_hunter_starbase
				}
			}
			root = {
				remove_relation_flag = {
					who = prev
					flag = contract_heirs_hunter_starbase
				}
			}
			if = {
				limit = {
					fromfrom = {
						has_fleet_flag = heirs_raider_starbase
					}
				}
				add_mission_counter = {
					mission = contract_destroy_station_heirs
					counter = contract_progress_counter
					amount = 1
				}
			}
			else = {
				add_mission_counter = {
					mission = contract_destroy_station_heirs_start
					counter = contract_progress_counter
					amount = 1
				}
			}
		}
	}
}

country_event = {
	id = nomads.545
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_heirs_of_the_khan
		fromfromfrom = {
			has_fleet_flag = heirs_hunter_fleet
			NOT = { has_fleet_flag = will_surrender_heirs }
		}
	}

	immediate = {
		if = {
			limit = {
				check_variable = {
					which = defeated_heirs_hunters
					value = 0
				}
			}
			country_event = {
				id = nomads.546
			}
		}
		change_variable = {
			which = defeated_heirs_hunters
			value = 1
		}
		change_variable = {
			which = defeated_heirs_hunters_spawner
			value = 1
		}
	}
}

country_event = {
	id = nomads.546
	title = nomads.546.name
	desc = nomads.546.desc
	picture = GFX_evt_pirate_armada
	show_sound = relic_activation_khans_throne

	is_triggered_only = yes

	option = {
		name = nomads.546.a
		add_monthly_resource_mult = {
			resource = unity
			value = @tier1unityreward
			min = @tier1unitymin
			max = @tier1unitymax
		}
	}
}

ship_event = {
	id = nomads.550
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ship_flag = marauder_hunter
		fleet = {
			NOT = {
				any_owned_ship = {
					is_disabled = no
				}
			}
			NOT = { has_fleet_flag = heirs_hunters_surrendered }
		}
	}

	immediate = {
		if = {
			limit = {
				from.owner = {
					has_origin = origin_heirs_of_the_khan
				}
			}
			from.owner = { country_event = { id = nomads.555 } }
		}
		else = {
			fleet = {
				delete_fleet = this
			}
		}
	}
}

country_event = {
	id = nomads.555
	title = nomads.555.name
	desc = {
		text = nomads.555.desc.1
		trigger = {
			has_country_flag = heirs_bargain_after_1
		}
	}
	desc = {
		text = nomads.555.desc.2
		trigger = {
			has_country_flag = heirs_bargain_after_2
		}
	}
	desc = {
		text = nomads.555.desc.3
		trigger = {
			has_country_flag = heirs_bargain_after_3
		}
	}

	diplomatic = yes
	picture_event_data = {
		portrait = species
		room = marauder_room
	}
	show_sound = relic_activation_khans_throne

	is_triggered_only = yes

	immediate = {
		from.fleet = {
			clear_fleet_actions = this
			set_fleet_flag = heirs_hunters_surrendered
		}
		change_variable = {
			which = defeated_heirs_hunters
			value = 1
		}
		change_variable = {
			which = defeated_heirs_hunters_spawner
			value = 1
		}
		random_list = { # Description Selector, ensuring you never get the same one twice in a row
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_bargain_after_1
				}
				clean_up_heirs_bargain_description = yes
				set_country_flag = heirs_bargain_after_1
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_bargain_after_2
				}
				clean_up_heirs_bargain_description = yes
				set_country_flag = heirs_bargain_after_2
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_bargain_after_3
				}
				clean_up_heirs_bargain_description = yes
				set_country_flag = heirs_bargain_after_3
			}
		}
		random_list = { # Option Selector, ensuring you never get the same one twice in a row
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_option_after_1
				}
				clean_up_heirs_bargain_option = yes
				set_country_flag = heirs_option_after_1
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_option_after_2
				}
				clean_up_heirs_bargain_option = yes
				set_country_flag = heirs_option_after_2
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_option_after_3
				}
				clean_up_heirs_bargain_option = yes
				set_country_flag = heirs_option_after_3
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_option_after_4
				}
				clean_up_heirs_bargain_option = yes
				set_country_flag = heirs_option_after_4
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_option_after_5
				}
				clean_up_heirs_bargain_option = yes
				set_country_flag = heirs_option_after_5
			}
		}
	}

	option = {
		name = nomads.555.a #Just go
		heirs_hunter_retreat_effect = yes
	}

	option = {
		name = {
			trigger = {
				has_country_flag = heirs_bargain_after_1
			}
			text = nomads.555.b.1 #We will not accept surrender.
		}
		name = {
			trigger = {
				has_country_flag = heirs_bargain_after_2
			}
			text = nomads.555.b.2 #This is the real galaxy, there is no surrender.
		}
		name = {
			trigger = {
				has_country_flag = heirs_bargain_after_3
			}
			text = nomads.555.b.3 #If you were truly loyal you would have left with us.
		}
		custom_tooltip = nomads.555.b.tt
		hidden_effect = {
			from.fleet = {
				destroy_fleet = this
			}
		}
	}

	option = {
		name = nomads.555.c.1 #Give us resources
		trigger = { has_country_flag = heirs_option_after_1 }
		locked_random_list = {
			1 = {
				add_monthly_resource_mult = {
					resource = energy
					value = @tier2materialreward
					min = @tier2materialmin
					max = @tier2materialmax
				}
			}
			1 = {
				modifier = {
					factor = 0
					country_uses_food = yes
				}
				add_monthly_resource_mult = {
					resource = food
					value = @tier2materialreward
					min = @tier2materialmin
					max = @tier2materialmax
				}
			}
			1 = {
				add_monthly_resource_mult = {
					resource = minerals
					value = @tier2materialreward
					min = @tier2materialmin
					max = @tier2materialmax
				}
			}
			1 = {
				add_monthly_resource_mult = {
					resource = alloys
					value = @tier1materialreward
					min = @tier1materialmin
					max = @tier1materialmax
				}
			}
			1 = {
				add_monthly_resource_mult = {
					resource = trade
					value = @tier1materialreward
					min = @tier1materialmin
					max = @tier1materialmax
				}
			}
			1 = {
				modifier = {
					factor = 0
					has_ancrel = no
				}
				add_monthly_resource_mult = {
					resource = minor_artifacts
					value = @tier1materialreward
					min = 100
					max = 100
				}
			}
			1 = {
				modifier = {
					factor = 0
					has_astral_planes_dlc = no
				}
				add_monthly_resource_mult = {
					resource = astral_threads
					value = @tier1materialreward
					min = 100
					max = 100
				}
			}
		}
		heirs_hunter_retreat_effect = yes
	}

	option = {
		name = nomads.555.c.2 #The strongest survive
		trigger = { has_country_flag = heirs_option_after_2 }
		hidden_effect = {
			from.fleet = {
				while = {
					limit = {
						num_ships > 1
					}
					random_owned_ship = {
						delete_ship = this
					}
				}
				random_owned_ship = {
					set_disabled = no
					set_disable_at_health = no
				}
				set_owner = root
			}
		}
	}

	option = {
		name = nomads.555.c.3 #You can join us, but leave your ships.
		trigger = { has_country_flag = heirs_option_after_3 }
		capital_scope = {
			create_pop_group = {
				species = root.species
				size = 400
				random = 38
			}
		}
		hidden_effect = {
			from.fleet = {
				delete_fleet = this
			}
		}
	}

	option = {
		name = nomads.555.c.4 #We will send one of you back as a warning
		trigger = { has_country_flag = heirs_option_after_4 }
		custom_tooltip = nomads.555.c.4.tt
		hidden_effect = {
			heirs_hunter_retreat_effect = yes
			from.fleet = {
				while = {
					limit = {
						num_ships > 1
					}
					random_owned_ship = {
						delete_ship = this
					}
				}
			}
			set_timed_country_flag = {
				flag = intimidated_marauder_hunters
				days = 2200
			}
		}
	}

	option = {
		name = nomads.555.c.5 #Leave us your leader
		trigger = { has_country_flag = heirs_option_after_5 }
		create_leader = {
			class = commander
			species =  root.species
			name = random
			skill = 5
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = leader_trait_mercenary_warrior
				trait = subclass_commander_admiral
			}
		}
		heirs_hunter_retreat_effect = yes
	}
}

fleet_event = {
	id = nomads.560
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		delete_fleet = this
	}
}

fleet_event = {
	id = nomads.565
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_fleet_flag = heirs_hunter_fleet
		solar_system = {
			any_fleet_in_system = {
				has_fleet_flag = hunted_fleet
				owner = {
					has_relation_flag = {
						who = root.owner
						flag = negotiate_on_encounter
					}
				}
			}
		}
	}

	immediate = {
		solar_system = {
			random_fleet_in_system = {
				limit = {
					has_fleet_flag = hunted_fleet
					owner = {
						has_relation_flag = {
							who = root.owner
							flag = negotiate_on_encounter
						}
					}
				}
				save_event_target_as = hunted_fleet
				remove_fleet_flag = hunted_fleet
				owner = {
					country_event = { id = nomads.570 }
					remove_relation_flag = {
						who = root.owner
						flag = negotiate_on_encounter
					}
				}
			}
		}
		clear_fleet_actions = this
	}
}

country_event = {
	id = nomads.570
	title = nomads.570.name
	desc = {
		text = nomads.570.desc.1
		trigger = {
			has_country_flag = heirs_bargain_before_1
		}
	}
	desc = {
		text = nomads.570.desc.2
		trigger = {
			has_country_flag = heirs_bargain_before_2
		}
	}
	desc = {
		text = nomads.570.desc.3
		trigger = {
			has_country_flag = heirs_bargain_before_3
		}
	}

	diplomatic = yes
	picture_event_data = {
		portrait = species
		room = marauder_room
	}
	show_sound = relic_activation_khans_throne

	is_triggered_only = yes

	immediate = {
		from.owner = {
			set_faction_hostility = {
				target = root
				set_hostile = no
				set_neutral = yes
			}
		}
		random_list = { # Description Selector, ensuring you never get the same one twice in a row
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_bargain_before_1
				}
				clean_up_heirs_bargain_before_description = yes
				set_country_flag = heirs_bargain_before_1
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_bargain_before_2
				}
				clean_up_heirs_bargain_before_description = yes
				set_country_flag = heirs_bargain_before_2
			}
			1 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_bargain_before_3
				}
				clean_up_heirs_bargain_before_description = yes
				set_country_flag = heirs_bargain_before_3
			}
		}
		random_list = { # Option Selector, ensuring you never get the same one twice in a row
			3 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_option_before_1
				}
				clean_up_heirs_bargain_before_option = yes
				set_country_flag = heirs_option_before_1
			}
			3 = {
				modifier = {
					factor = 0
					has_country_flag = heirs_option_before_2
				}
				clean_up_heirs_bargain_before_option = yes
				set_country_flag = heirs_option_before_2
			}
			1 = { # Rarer since it's strong
				modifier = {
					factor = 0
					has_country_flag = heirs_option_before_3
				}
				clean_up_heirs_bargain_before_option = yes
				set_country_flag = heirs_option_before_3
			}
		}
	}

	option = {
		name = nomads.570.a # Cease your babbling and fight
		from = {
			queue_actions = { # In case the player has been running away from the marauder while the event window was up
				move_to = event_target:hunted_fleet
			}
			owner = {
				set_faction_hostility = {
					target = root
					set_hostile = yes
					set_neutral = no
				}
			}
		}
	}

	option = { # Bribe to leave
		name = nomads.570.b
		trigger = {
			has_country_flag = heirs_option_before_1
		}
		inline_script = {
			script = events/dynamic_resource_cost
			RESOURCE = energy
			COST1 = 200
			INCOME1 = 10
			COST2 = 500
			INCOME2 = 50
			COST3 = 1000
			INCOME3 = 100
			COST4 = 2000
			INCOME4 = 200
			COST5 = 5000
			INCOME5 = 500
			OWNER_SCOPE = root
		}
		heirs_hunter_retreat_effect = yes
	}

	option = { # Have leaders fight
		name = nomads.570.c
		trigger = {
			has_country_flag = heirs_option_before_2
		}

		random_list = {
			21 = { #21 is there so that it defaults to 20/80 at skill lvl 1
				modifier = {
					subtract = ruler.trigger:has_total_skill
				}
				ruler = {
					add_trait = {
						trait = leader_trait_maimed
					}
				}
				custom_tooltip = nomads.570.c.tt.1
			}
			80 = {
				custom_tooltip = nomads.570.c.tt.2
				heirs_hunter_retreat_effect = yes
				hidden_effect = {
					from = {
						queue_actions = { # In case the player has been running away from the marauder while the event window was up
							move_to = event_target:hunted_fleet
						}
						owner = {
							set_faction_hostility = {
								target = root
								set_hostile = yes
								set_neutral = no
							}
						}
					}
				}
			}
		}
	}

	option = { # Bribe to join
		name = nomads.570.d
		trigger = {
			has_country_flag = heirs_option_before_3
		}
		inline_script = {
			script = events/dynamic_resource_cost
			RESOURCE = energy
			COST1 = 500
			INCOME1 = 10
			COST2 = 1000
			INCOME2 = 50
			COST3 = 2000
			INCOME3 = 100
			COST4 = 4000
			INCOME4 = 200
			COST5 = 10000
			INCOME5 = 500
			OWNER_SCOPE = root
		}
	}
}

country_event = {
    id = nomads.600
    title = nomads.600.name
    desc = nomads.600.desc
    picture = GFX_evt_khan_throne_room
    show_sound = relic_activation_khans_throne
    location = event_target:marauder_rally_point
    is_triggered_only = yes

	event_chain = heirs_khan_candidate

    immediate = {
		hidden_effect = {
			every_country = {
				limit = {
					OR = {
						is_country_type = dormant_marauders
						has_country_flag = aspiring_khan
					}
				}
				set_faction_hostility = {
					target = root
					set_hostile = no
					set_neutral = yes
				}
			}
		}
		if = {
			limit = {
				is_ai = no
			}
			begin_event_chain = {
				event_chain = heirs_khan_candidate
				target = root
			}
			create_point_of_interest = {
				id = marauder_rally_point
				name = heirs_khan_rally_point_name
				desc = heirs_khan_rally_point_desc
				event_chain = heirs_khan_candidate
				location = event_target:marauder_rally_point
			}
		}
    }
    option = {
		trigger = {
			is_ai = no
		}
        name = nomads.600.a
		set_country_flag = heirs_khan_contestant
		tooltip = {
			create_point_of_interest = {
				id = marauder_rally_point
				name = heirs_khan_rally_point_name
				desc = heirs_khan_rally_point_desc
				event_chain = heirs_khan_candidate
				location = event_target:marauder_rally_point
			}
		}
    }

	option = {
		name = nomads.600.a # The AI go usurping the khanate, it wont be too different from the player perspective
		trigger = {
			is_ai = yes
		}
	}
}

# A fleet has entered a system | See also: on_fleet_auto_move_arrival
# Scope = Fleet
# From = System
fleet_event = {
    id = nomads.605
    title = $TRANSMISSION$
    desc = nomads.605.desc
    is_triggered_only = yes
    trigger = {
        owner = {
            has_origin = origin_heirs_of_the_khan
			NOT = { has_country_flag = confronted_aspiring_khan }
        }
        solar_system = {
            any_fleet_in_system = {
                has_fleet_flag = aspiring_khan
            }
        }
    }
    diplomatic = yes
    picture_event_data = {
        portrait = event_target:khan_aspirant@root.owner
        room = khan_room
    }

    immediate = {
		owner = {
			set_country_flag = confronted_aspiring_khan
			create_leader = {
				class = commander
				species = owner_main_species
				effect = {
					save_event_target_as = khan_aspirant@root.owner
					exile_leader_as = khan_aspirant
				}
			}
		}
        solar_system = {
            random_fleet_in_system = {
                limit = {
                    has_fleet_flag = aspiring_khan
                }
                save_event_target_as = aspiring_khan_fleet
				owner = {
					set_faction_hostility = {
						target = root.owner
						set_hostile = yes
						set_neutral = no
					}
				}
            }
        }
		if = {
			limit = {
				owner = { has_country_flag = marauder_2_pledged }
			}
			event_target:marauder_country_2 = {
				create_fleet = {
					effect = {
						set_owner = prev
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Lancer" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Lancer" graphical_culture = "pirate_01" }
						set_location = { target = root distance = 100 angle = 60 }
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 500
					}
				}
			}
		}
		if = {
			limit = {
				owner = { has_country_flag = marauder_3_pledged }
			}
			event_target:marauder_country_3 = {
				create_fleet = {
					effect = {
						set_owner = prev
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Outrider" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Lancer" graphical_culture = "pirate_01" }
						create_ship = { name = random design = "NAME_Lancer" graphical_culture = "pirate_01" }
						set_location = { target = root distance = 100 angle = 120 }
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 500
					}
				}
			}
		}
    }

	option = {
		name = nomads.605.a
	}
}

# This = owner of fleet 1 (combatant)
# From = owner of fleet 2 (destroyed)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = { # You defeated the Khan Aspirant, time to be the Khan?
	id = nomads.610
	title = nomads.610.name
	desc = nomads.610.desc
	picture = GFX_evt_heirs_of_the_khan
	is_triggered_only = yes
	event_chain = heirs_khan_candidate

	trigger = {
		has_origin = origin_heirs_of_the_khan
		NOT = { has_global_flag = marauder_crisis_started }
		fromfromfrom = {
			OR = {
				has_fleet_flag = aspiring_khan
				AND = {
					owner? = { has_origin = origin_heirs_of_the_khan }
					any_owned_ship = { is_capital = yes }
				}
			}
		}
		NOT = {
			any_playable_country = {
				has_origin = origin_heirs_of_the_khan
				NOT = { is_same_value = root }
				is_ai = no
				NOT = { has_country_flag = won_khan_duel } # Previous winners who now have been defeated will have this flag
			}
		}
	}

	immediate = {
		set_global_flag = player_khan_imminent
		remove_global_flag = aspiring_khan_spawned
		set_country_flag = won_khan_duel
		from = { # Making sure to remove the Khan in Waiting flag from previous winners
			remove_country_flag = khan_in_waiting
			if = {
				limit = {
					has_origin = origin_heirs_of_the_khan
				}
				country_event = { id = nomads.630 }
			}
		}
	}

	option = {
		allow = {
			custom_tooltip = {
				fail_text = "cannot_be_defender_of_the_galaxy"
				has_defender_of_the_galaxy_ap = no
			}
			custom_tooltip = {
				fail_text = "cannot_be_custodian"
				is_galactic_custodian = no
			}
			custom_tooltip = {
				fail_text = "cannot_be_gal_emperor"
				is_galactic_emperor = no
			}
		}
		name = nomads.610.a
		custom_tooltip = nomads.610.a.tt
		hidden_effect = {
			make_heirs_khan = yes
			play_sound = relic_activation_khans_throne
			save_event_target_as = this_country
			country_event = { id = timeline.130 }
		}
	}

	option = {
		name = nomads.610.b
		custom_tooltip = nomads.610.b.tt
		hidden_effect = {
			save_event_target_as = heirs_marauder_overlord
			set_country_flag = marauder_overlord
			set_country_flag = marauder_overlord_1
			event_target:marauder_country_1 = {
				set_faction_hostility = {
					target = root
					set_friendly = yes
				}
				country_event = { id = nomads.750 days = 720 random = 20 }
			}

		}
	}

	option = {
		name = nomads.610.c
		custom_tooltip = nomads.610.c.tt
	}

	after = {
		add_relic = r_khans_throne
		end_event_chain = heirs_khan_candidate
	}
}

# This = owner of fleet 1 (combatant)
# From = owner of fleet 2 (destroyed)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = { # You defeated the Khan Aspirant, but there is another player out there
	id = nomads.615
	title = nomads.610.name
	desc = nomads.615.desc
	picture = GFX_evt_heirs_of_the_khan
	is_triggered_only = yes

	event_chain = heirs_khan_candidate

	trigger = {
		has_origin = origin_heirs_of_the_khan
		NOR = {
			has_country_flag = won_khan_duel
			has_global_flag = marauder_crisis_started
		}
		fromfromfrom = {
			OR = {
				has_fleet_flag = aspiring_khan
				AND = {
					owner? = {
						has_origin = origin_heirs_of_the_khan
						has_country_flag = khan_in_waiting
					}
					any_owned_ship = { is_capital = yes }
				}
			}
		}
		any_playable_country = {
			has_origin = origin_heirs_of_the_khan
			NOT = { is_same_value = root }
			is_ai = no
			NOT = { has_country_flag = won_khan_duel } # Previous winners who now have been defeated will have this flag
		}
	}

	immediate = {
		set_global_flag = player_khan_imminent
		set_country_flag = won_khan_duel
		set_country_flag = khan_in_waiting
		from = { # Making sure to remove the Khan in Waiting flag from previous winners
			remove_country_flag = khan_in_waiting
			if = {
				limit = {
					has_origin = origin_heirs_of_the_khan
				}
				country_event = { id = nomads.630 }
			}
		}
		country_event = {
			id = nomads.620
			years = 3
		}
		if = { #We remove the POIs from the winner
			limit = {
				has_point_of_interest = { poi = heirs_khan_in_waiting }
			}
			remove_point_of_interest = heirs_khan_in_waiting
		}
		if = {
			limit = {
				has_point_of_interest = { poi = marauder_rally_point }
			}
			remove_point_of_interest = marauder_rally_point
		}
		every_playable_country = {
			limit = {
				has_origin = origin_heirs_of_the_khan
				NOR = {
					is_same_value = root
					has_country_flag = won_khan_duel
				}
			}
			if = { #And we remove them from the other countries
				limit = {
					has_point_of_interest = { poi = heirs_khan_in_waiting }
				}
				remove_point_of_interest = heirs_khan_in_waiting
			}
			if = {
				limit = {
					has_point_of_interest = { poi = marauder_rally_point }
				}
				remove_point_of_interest = marauder_rally_point
			}
			country_event = { id = nomads.625 }
		}
	}

	option = {
		name = nomads.615.a
	}
}

country_event = { # Three years have passed and you are now the Khan
	id = nomads.620
	title = nomads.620.name
	desc = nomads.620.desc
	picture = GFX_evt_heirs_of_the_khan
	is_triggered_only = yes

	event_chain = heirs_khan_candidate

	trigger = {
		has_country_flag = khan_in_waiting
	}

	immediate = {
		remove_global_flag = aspiring_khan_spawned
	}

	option = {
		allow = {
			custom_tooltip = {
				fail_text = "cannot_be_defender_of_the_galaxy"
				has_defender_of_the_galaxy_ap = no
			}
			custom_tooltip = {
				fail_text = "cannot_be_custodian"
				is_galactic_custodian = no
			}
			custom_tooltip = {
				fail_text = "cannot_be_gal_emperor"
				is_galactic_emperor = no
			}
		}
		name = nomads.610.a
		custom_tooltip = nomads.610.a.tt
		hidden_effect = {
			make_heirs_khan = yes
			save_event_target_as = this_country
			country_event = { id = timeline.130 }
		}
	}

	option = {
		name = nomads.610.b
		custom_tooltip = nomads.610.b.tt
		hidden_effect = {
			save_event_target_as = heirs_marauder_overlord
			set_country_flag = marauder_overlord
			set_country_flag = marauder_overlord_1
			event_target:marauder_country_1 = {
				set_faction_hostility = {
					target = root
					set_friendly = yes
				}
				country_event = { id = nomads.750 days = 720 random = 20 }
			}
		}
	}

	option = {
		name = nomads.610.c
		custom_tooltip = nomads.610.c.tt
	}

	after = {
		add_relic = r_khans_throne
		end_event_chain = heirs_khan_candidate
	}
}

country_event = { # Informing other players that another player became the Khan in waiting
	id = nomads.625
	title = nomads.625.name
	desc = nomads.625.desc
	picture = GFX_evt_heirs_of_the_khan
	is_triggered_only = yes

	event_chain = heirs_khan_candidate

	immediate = {
		create_point_of_interest = {
			id = heirs_khan_in_waiting
			name = heirs_khan_in_waiting_name
			desc = heirs_khan_in_waiting_desc
			event_chain = heirs_khan_candidate
			location = from.capital_scope.ship
		}
	}

	option = {
		name = ENGINES
		tooltip = {
			create_point_of_interest = {
				id = heirs_khan_in_waiting
				name = heirs_khan_in_waiting_name
				desc = heirs_khan_in_waiting_desc
				event_chain = heirs_khan_candidate
				location = from.capital_scope.ship
			}
		}
	}
}

country_event = { # Informing last Khan in waiting that they have lost
	id = nomads.630
	title = nomads.630.name
	desc = nomads.630.desc
	picture = GFX_evt_heirs_of_the_khan
	is_triggered_only = yes

	event_chain = heirs_khan_candidate

	option = {
		name = DOTDOTDOT
		end_event_chain = heirs_khan_candidate
	}
}

# Your Khan died!
# This = Country
# From = Leader
country_event = {
	id = nomads.635
    title = nomads.635.name
    desc = nomads.635.desc
    picture = GFX_evt_khan_throne_room
    show_sound = event_shroud_song
	is_triggered_only = yes

	trigger = {
		from = {
			has_trait = leader_trait_ruler_great_khan
			NOT = {
				has_leader_flag = leader_death_events_blocked

			}
		}
		has_country_flag = heir_of_the_khan_khan
	}

	immediate = {
		set_country_flag = heir_of_the_khan_khan_died
		event_target:marauder_country_1? = { # When your Khan dies you stop getting reinforcments and your vassal loses all the constructors
			every_owned_fleet = {
				limit = {
					is_ship_size = constructor
				}
				delete_fleet = this
			}
		}
	}

	option = { # We are done being the Khan
		name = nomads.635.a
		custom_tooltip = nomads.635.a.tt
		hidden_effect = {
			remove_country_flag = heir_of_the_khan_khan
			restore_country_backup_data = yes
			if = {
				limit = {
					event_target:marauder_country_1? = {
						count_system_within_border = {
							count > 0
						}
					}
				}
				event_target:marauder_country_1? = {
					if = { #First we check if you still have the original marauder systems
						limit = {
							any_system_within_border = {
								has_star_flag = marauder_capital_1
							}
						}
						random_system_within_border = {
							limit = {
								has_star_flag = marauder_capital_1
							}
							save_event_target_as = closest_system
							save_event_target_as = capital_system
						}
					}
					else = {
						random_system_within_border = {
							weights = {
								base = 5
								modifier = {
									factor = value:num_owned_neighbor_systems
									always = yes
								}
							}
							save_event_target_as = closest_system
							save_event_target_as = capital_system
						}
					}
					while = {
						count = value:twentyfive_percent_systems_min_5
						event_target:closest_system = {
							set_star_flag = heir_marauder_system
							if = {
								limit = {
									any_neighbor_system = {
										space_owner? = {
											is_same_value = event_target:marauder_country_1
										}
										NOT = { has_star_flag = heir_marauder_system }
									}
								}
								random_neighbor_system = {
									limit = {
										space_owner? = {
											is_same_value = event_target:marauder_country_1
										}
										NOT = { has_star_flag = heir_marauder_system }
									}
									save_event_target_as = closest_system
								}
							}
							else = {
								closest_system = {
									limit = {
										space_owner? = {
											is_same_value = event_target:marauder_country_1
										}
										NOT = { has_star_flag = heir_marauder_system }
									}
									save_event_target_as = closest_system
								}
							}
						}
					}
					change_country_flag = {
						icon = {
							category = "pirate"
							file = "flag_pirate_10.dds"
						}
						background = {
							category = "backgrounds"
							file = "00_solid.dds"
						}
						colors = {
							"black"
							"black"
							"null"
							"null"
						}
					}
					set_country_type = dormant_marauders
					remove_country_flag = heirs_of_khan_host
					every_system_within_border = {
						if = {
							limit = {
								NOT = { has_star_flag = heir_marauder_system }
							}
							every_system_planet_colony = {
								limit = {
									owner? = {
										is_primitive = no
									}
								}
								destroy_colony = yes
							}
							starbase.fleet = {
								destroy_fleet = this
							}
						}
						else = {
							populate_heir_marauder_system_defenses = yes
						}
					}
				}
			}
			every_subject = {
				limit = {
					NOT = {
						has_origin = origin_nomadic_purger
					}
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_liberated_us
				}
				add_trust = {
					who = root
					amount = 20
				}
				release_subject = yes
			}
			save_event_target_as = heirs_marauder_overlord
			set_country_flag = marauder_overlord
			set_country_flag = marauder_overlord_1
			event_target:marauder_country_1 = {
				country_event = { id = nomads.750 days = 720 random = 20 }
			}
		}
	}

	option = { # We will rule forever
		name = nomads.635.b
		add_modifier = {
			modifier = khans_forever
			days = -1
		}
	}
}

country_event = { # Marauders approach heir of the khan after being destroyed
	id = nomads.700
	title = nomads.700.name
	desc = nomads.700.desc
	picture = GFX_evt_heirs_of_the_khan
	is_triggered_only = yes

	immediate = {
		set_country_flag = heir_approached_by_marauder_remains
	}

	option = {
		allow = {
			custom_tooltip = {
				fail_text = "cannot_be_defender_of_the_galaxy"
				has_defender_of_the_galaxy_ap = no
			}
			custom_tooltip = {
				fail_text = cannot_be_custodian
				is_galactic_custodian = no
			}
			custom_tooltip = {
				fail_text = cannot_be_gal_emperor
				is_galactic_emperor = no
			}
		}
		name = nomads.700.a
		custom_tooltip = nomads.610.a.tt
		hidden_effect = {
			make_heirs_khan_tiny = yes
			save_event_target_as = this_country
			country_event = { id = timeline.130 }
		}
	}

	option = {
		name = nomads.700.b
		ordered_country = {
			limit = {
				has_origin = origin_heirs_of_the_khan
				is_ai = no
				NOT = { has_country_flag = heir_approached_by_marauder_remains }
			}
			position = 0
			order_by = defeated_heirs_hunters
			country_event = { id = nomads.700 days = 5}
		}
	}
}

#Marauder Dividends
country_event = {
	id = nomads.750
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		event_target:heirs_marauder_overlord? = {
			is_forced_friendly = root # Checking you are still friendly to your Marauder
		}
	}

	immediate = {
		event_target:heirs_marauder_overlord = {
			random_list = {
				10 = {}
				2 = {
					create_message = {
						type = HEIRS_DIVIDENDS
						localization = HEIRS_DIVIDENDS_ENERGY_DESC
						days = @toast_message_days
						variable = {
							type = name
							localization = MARAUDER
							scope = root
						}
					}
					add_monthly_resource_mult = {
						resource = energy
						value = @tier3materialreward
						min = @tier3materialmin
						max = @tier3materialmax
					}
				}
				2 = {
					create_message = {
						type = HEIRS_DIVIDENDS
						localization = HEIRS_DIVIDENDS_FOOD_DESC
						days = @toast_message_days
						variable = {
							type = name
							localization = MARAUDER
							scope = root
						}
					}
					add_monthly_resource_mult = {
						resource = food
						value = @tier3materialreward
						min = @tier3materialmin
						max = @tier3materialmax
					}
				}
				2 = {
					create_message = {
						type = HEIRS_DIVIDENDS
						localization = HEIRS_DIVIDENDS_MINERALS_DESC
						days = @toast_message_days
						variable = {
							type = name
							localization = MARAUDER
							scope = root
						}
					}
					add_monthly_resource_mult = {
						resource = minerals
						value = @tier3materialreward
						min = @tier3materialmin
						max = @tier3materialmax
					}
				}
				2 = {
					modifier = {
						factor = 0
						has_country_flag = block_reinforcements
					}
					root = {
						random_planet_within_border = {
							save_event_target_as = rally_point
						}
					}
					create_balanced_fleet = {
						name = "NAME_Hunting_Fleet"
						size = 125
						ship_designs = {
							NAME_Outrider
							NAME_Lancer
							NAME_Void_Champion
							NAME_Ancestral_Glory
						}
						effect = {
							set_owner = root #For ship names
							set_location = event_target:rally_point
							set_fleet_stance = aggressive
							set_fleet_bombardment_stance = indiscriminate
							set_aggro_range_measure_from = self
							set_aggro_range = 150
							give_fleet = { controller = prev days = 999999 }
							add_modifier = { modifier = marauder_friend_discount }
						}
					}
					create_message = {
						type = HEIRS_DIVIDENDS
						localization = HEIRS_DIVIDENDS_SHIPS_DESC
						days = @toast_message_days
						target = event_target:rally_point
						variable = {
							type = name
							localization = MARAUDER
							scope = root
						}
						variable = {
							type = name
							localization = PLANET
							scope = event_target:rally_point
						}
					}
				}
			}
		}
		country_event = { id = nomads.750 days = 360 random = 30 }
	}
}

# Fired when a war begins for the Khan; sets the Host hostile to everyone fighting the Khan.
# Root = Country (war participant), From = War
country_event = {
	id = nomads.755
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_country_flag = heir_of_the_khan_khan
		exists = event_target:marauder_country_1
	}

	immediate = {
		from = {
			every_war_participant = {
				limit = {
					is_at_war_with = root
				}
				set_country_flag = khan_war_enemy
				set_faction_hostility = {
					target = event_target:marauder_country_1
					set_hostile = yes
					set_neutral = no
					set_friendly = no
				}
			}
		}
	}
}

# Catches late-joiners who enter an existing Khan war via call-to-arms; on_war_beginning does not re-fire for them.
country_event = {
	id = nomads.756
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = event_target:great_khan_empire
		exists = event_target:marauder_country_1
		is_at_war_with = event_target:great_khan_empire
		NOR = {
			has_country_flag = khan_war_enemy
			has_country_flag = heir_of_the_khan_khan
			is_same_value = event_target:marauder_country_1
		}
	}

	immediate = {
		set_country_flag = khan_war_enemy
		set_faction_hostility = {
			target = event_target:marauder_country_1
			set_hostile = yes
			set_neutral = no
			set_friendly = no
		}
	}
}

# Fired when a war ends or an ally peaces out; resets Host hostility for any flagged enemy the Khan is no longer fighting.
country_event = {
	id = nomads.760
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = event_target:great_khan_empire
		exists = event_target:marauder_country_1
		any_country = {
			has_country_flag = khan_war_enemy
		}
	}

	immediate = {
		every_country = {
			limit = {
				has_country_flag = khan_war_enemy
				NOT = { is_at_war_with = event_target:great_khan_empire }
			}
			remove_country_flag = khan_war_enemy
			set_faction_hostility = {
				target = event_target:marauder_country_1
				set_hostile = no
				set_neutral = yes
				set_friendly = no
			}
		}
	}
}

########################
# Forever Cruise Origin
########################

# Passenger Satisfaction situation start
# Fired by: on_game_start_country
country_event = {
	id = nomads.1000
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_forever_cruise
	}

	immediate = {
		start_situation = {
			type = situation_passenger_satisfaction
		}
	}
}

# Passenger Disembarkation -> Become a planet-bound non-nomad empire
# Fired by: situation on_first_enter
situation_event = {
	id = nomads.1005
	title = "nomads.1005.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1005.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1005.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1005.machine.desc"
	}
	picture = GFX_evt_big_landing_ship
	show_sound = event_planet_thrusters
	is_triggered_only = yes

	immediate = {
		owner = {
			set_country_flag = forever_cruise_planetfall # This stops crew and passenger contracts from spawning
			every_issued_mission = {
				cancel_contract = {
					refund = no
					show_message = no
				}
			}
			every_owned_contract = {
				cancel_contract = {
					refund = no
				}
			}
		}
	}

	option = { # Regular
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1005.regular.a"
		custom_tooltip = "nomads.1005.all.a.tt"
	}
	option = { # Hive
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1005.hive.a"
		custom_tooltip = "nomads.1005.all.a.tt"
	}
	option = { # Machine
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1005.machine.a"
		custom_tooltip = "nomads.1005.all.a.tt"
	}
}

# Crew Mutiny -> Become a regular nomad empire
# Fired by: situation on_first_enter
situation_event = {
	id = nomads.1025
	title = "nomads.1025.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1025.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1025.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1025.machine.desc"
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = no }
		}
		picture = GFX_evt_surrender
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = yes }
		}
		picture = GFX_evt_organic_oppression
	}
	show_sound = event_salute
	is_triggered_only = yes

	immediate = {
		owner = {
			set_country_flag = forever_cruise_mutiny # This stops crew and passenger contracts from spawning
			set_country_flag = free_government_reform
			every_issued_mission = {
				cancel_contract = {
					refund = no
					show_message = no
				}
			}
			every_owned_contract = {
				cancel_contract = {
					refund = no
				}
			}
		}
	}

	option = { # Regular
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1025.regular.a"
		custom_tooltip = "nomads.1025.all.a.tt"
	}
	option = { # Hive
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1025.hive.a"
		custom_tooltip = "nomads.1025.all.a.tt"
	}
	option = { # Machine
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1025.machine.a"
		custom_tooltip = "nomads.1025.all.a.tt"
	}
}

# Mutiny Completed
# Fired by: on_post_government_changed
# THIS = country
country_event = {
	id = nomads.1030
	title = "nomads.1030.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1030.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1030.hive.desc"
	}

	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
				has_civic = civic_machine_servitor
			}
		}
		text = "nomads.1030.machine_servitor.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
				NOT = { has_civic = civic_machine_servitor }
			}
		}
		text = "nomads.1030.machine.desc"
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = no }
		}
		picture = GFX_evt_nomad_default
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = yes }
		}
		picture = GFX_evt_arkship_in_orbit
	}
	location = capital_scope
	show_sound = event_hand_shake
	is_triggered_only = yes

	trigger = {
		owner = {
			has_country_flag = forever_cruise_mutiny
			NOT = { has_country_flag = forever_cruise_over }
		}
	}

	immediate = {
		remove_country_flag = forever_cruise_mutiny
		set_country_flag = forever_cruise_over
		remove_forever_cruise_citizenships = yes
		random_situation = {
			limit = { is_situation_type = situation_passenger_satisfaction }
			destroy_situation = this
		}
	}

	option = { # Regular
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1030.regular.a"
		custom_tooltip = "nomads.1030.all.a.tt"
	}
	option = { # Hive
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1030.hive.a"
		custom_tooltip = "nomads.1030.all.a.tt"
	}
	option = { # Machine (Rogue Servitor → Determined Exterminator)
		exclusive_trigger = {
			owner = {
				has_civic = civic_machine_servitor
			}
		}
		name = "nomads.1030.machine.a"
		custom_tooltip = "nomads.1030.machine_servitor.a.tt"
		hidden_effect = {
			force_remove_civic = civic_machine_servitor
			force_add_civic = civic_machine_terminator
		}
	}
	option = { # Machine
		trigger = {
			owner = {
				is_machine_empire = yes
				NOT = {	has_civic = civic_machine_servitor }
			}
		}
		name = "nomads.1030.machine.a"
		custom_tooltip = "nomads.1030.all.a.tt"
	}

	after = {
		hidden_effect = {
			save_event_target_as = this_country
			country_event = { id = timeline.120 }
		}
	}
}

# Stage start event for stage 1
# Fired by: situation on_first_enter
situation_event = {
	id = nomads.1050
	title = "nomads.1050.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1050.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1050.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1050.machine.desc"
	}
	picture = GFX_evt_civil_action
	show_sound = event_assembly_fight
	is_triggered_only = yes

	immediate = {
		set_situation_flag = passenger_planetfall_demanded
	}

	option = { # Regular A
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1050.regular.a"
		custom_tooltip = "nomads.1100.a.tt"
		custom_tooltip = "situation_continues_planetfall_tt"
	}
	option = { # Regular B
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1050.regular.b"
		custom_tooltip = "nomads.1050.b.tt"
		custom_tooltip = "situation_flips_mutiny_tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
			set_timed_situation_flag = {
				flag = satisfaction_passengers_grace_period
				months = 24
			}
			owner = {
				set_timed_country_flag = {
					flag = emergency_entertainment
					days = 3600
				}
			}
		}
	}
	option = { # Regular C
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1050.regular.c"
		custom_tooltip = "nomads.1050.c.tt"
		custom_tooltip = "situation_flips_mutiny_tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
			set_timed_situation_flag = {
				flag = satisfaction_passengers_grace_period
				months = 24
			}
			owner = {
				set_timed_country_flag = {
					flag = deferred_maintenance
					days = 3600
				}
			}
		}
	}
	option = { # Hive A
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1050.hive.a"
		custom_tooltip = "nomads.1100.a.tt"
		custom_tooltip = "situation_continues_planetfall_tt"
	}
	option = { # Hive B
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1050.hive.b"
		custom_tooltip = "nomads.1050.b.tt"
		custom_tooltip = "situation_flips_mutiny_tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
			set_timed_situation_flag = {
				flag = satisfaction_passengers_grace_period
				months = 24
			}
			owner = {
				set_timed_country_flag = {
					flag = emergency_entertainment
					days = 3600
				}
			}
		}
	}
	option = { # Hive C
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1050.hive.c"
		custom_tooltip = "nomads.1050.c.tt"
		custom_tooltip = "situation_flips_mutiny_tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
			set_timed_situation_flag = {
				flag = satisfaction_passengers_grace_period
				months = 24
			}
			owner = {
				set_timed_country_flag = {
					flag = deferred_maintenance
					days = 3600
				}
			}
		}
	}
	option = { # Machine A
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1050.machine.a"
		custom_tooltip = "nomads.1100.a.tt"
		custom_tooltip = "situation_continues_planetfall_tt"
	}
	option = { # Machine B
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1050.machine.b"
		custom_tooltip = "nomads.1050.b.tt"
		custom_tooltip = "situation_flips_mutiny_tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
			set_timed_situation_flag = {
				flag = satisfaction_passengers_grace_period
				months = 24
			}
			owner = {
				set_timed_country_flag = {
					flag = emergency_entertainment
					days = 3600
				}
			}
		}
	}
	option = { # Machine C
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1050.machine.c"
		custom_tooltip = "nomads.1050.c.tt"
		custom_tooltip = "situation_flips_mutiny_tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
			set_timed_situation_flag = {
				flag = satisfaction_passengers_grace_period
				months = 24
			}
			owner = {
				set_timed_country_flag = {
					flag = deferred_maintenance
					days = 3600
				}
			}
		}
	}
}

# Check if the planetfall demand has been handled
# Fired by: situation on_monthly
situation_event = {
	id = nomads.1055
	title = "nomads.1055.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1055.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1055.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1055.machine.desc"
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = no }
		}
		picture = GFX_evt_friendly_infiltration
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = yes }
		}
		picture = GFX_evt_robot_encounter
	}
	show_sound = event_slot_machine
	is_triggered_only = yes

	trigger = {
		has_situation_flag = passenger_planetfall_demanded
		situation_progress > 200
	}

	immediate = {
		remove_situation_flag = passenger_planetfall_demanded
	}

	option = { # Regular
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1055.regular.a"
	}
	option = { # Hive
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1055.hive.a"
	}
	option = { # Machine
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1055.machine.a"
	}
}

# Stage start event for stage 2
# Fired by: situation on_first_enter
situation_event = {
	id = nomads.1100
	title = "nomads.1100.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1100.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1100.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1100.machine.desc"
	}
	picture = GFX_evt_alien_segregation
	show_sound = event_ghost_town
	is_triggered_only = yes

	immediate = {

	}

	option = { # Regular A
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1100.regular.a"
		custom_tooltip = "nomads.1100.a.tt"
		custom_tooltip = "situation_continues_planetfall_tt"
	}
	option = { # Regular B
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1100.regular.b"
		custom_tooltip = "nomads.1100.b.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value >= -0.5
				}
			}
			custom_tooltip = "situation_flips_mutiny_tt"
		}
		else = {
			custom_tooltip = "situation_slows_planetfall_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = passenger_entertainment_programs
					days = 1800
				}
			}
		}
		owner = { increase_passenger_satisfaction = yes }
	}
	option = { # Regular C
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1100.regular.c"
		custom_tooltip = "nomads.1100.c.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value >= -0.5
				}
			}
			custom_tooltip = "situation_flips_mutiny_tt"
		}
		else = {
			custom_tooltip = "situation_slows_planetfall_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = double_crew_shifts
					days = 1800
				}
			}
		}
		owner = { increase_passenger_satisfaction = yes }
	}
	option = { # Hive A
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1100.hive.a"
		custom_tooltip = "nomads.1100.a.tt"
		custom_tooltip = "situation_continues_planetfall_tt"
	}
	option = { # Hive B
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1100.hive.b"
		custom_tooltip = "nomads.1100.b.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value >= -0.5
				}
			}
			custom_tooltip = "situation_flips_mutiny_tt"
		}
		else = {
			custom_tooltip = "situation_slows_planetfall_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = passenger_entertainment_programs
					days = 1800
				}
			}
		}
		owner = { increase_passenger_satisfaction = yes }
	}
	option = { # Hive C
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1100.hive.c"
		custom_tooltip = "nomads.1100.c.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value >= -0.5
				}
			}
			custom_tooltip = "situation_flips_mutiny_tt"
		}
		else = {
			custom_tooltip = "situation_slows_planetfall_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = double_crew_shifts
					days = 1800
				}
			}
		}
		owner = { increase_passenger_satisfaction = yes }
	}
	option = { # Machine A
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1100.machine.a"
		custom_tooltip = "nomads.1100.a.tt"
		custom_tooltip = "situation_continues_planetfall_tt"
	}
	option = { # Machine B
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1100.machine.b"
		custom_tooltip = "nomads.1100.b.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value >= -0.5
				}
			}
			custom_tooltip = "situation_flips_mutiny_tt"
		}
		else = {
			custom_tooltip = "situation_slows_planetfall_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = passenger_entertainment_programs
					days = 1800
				}
			}
		}
		owner = { increase_passenger_satisfaction = yes }
	}
	option = { # Machine C
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1100.machine.c"
		custom_tooltip = "nomads.1100.c.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value >= -0.5
				}
			}
			custom_tooltip = "situation_flips_mutiny_tt"
		}
		else = {
			custom_tooltip = "situation_slows_planetfall_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = double_crew_shifts
					days = 1800
				}
			}
		}
		owner = { increase_passenger_satisfaction = yes }
	}
}

# Stage start event for stage 3
# Fired by: situation on_first_enter
situation_event = {
	id = nomads.1150
	title = "nomads.1150.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1150.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1150.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1150.machine.desc"
	}
	picture = GFX_evt_obelisk_tourists
	show_sound = event_announcement
	is_triggered_only = yes

	immediate = {

	}

	option = { # Regular A
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1150.regular.a"
		custom_tooltip = "nomads.1150.regular.a.tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
		}
	}
	option = { # Hive A
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1150.hive.a"
		custom_tooltip = "nomads.1150.hive.a.tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
		}
	}
	option = { # Machine A
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1150.machine.a"
		custom_tooltip = "nomads.1150.machine.a.tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_add
			}
		}
	}
	option = { # Regular B
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1150.regular.b"
		custom_tooltip = "nomads.1150.regular.b.tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
		}
	}
	option = { # Hive B
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1150.hive.b"
		custom_tooltip = "nomads.1150.hive.b.tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
		}
	}
	option = { # Machine B
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1150.machine.b"
		custom_tooltip = "nomads.1150.machine.b.tt"
		hidden_effect = {
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
		}
	}
}

# Stage start event for stage 4
# Fired by: situation on_first_enter
situation_event = {
	id = nomads.1200
	title = "nomads.1200.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1200.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1200.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1200.machine.desc"
	}
	picture = GFX_evt_astral_rift_interdimensional_circus_2
	show_sound = event_metal_fatigue
	is_triggered_only = yes

	immediate = {

	}

	option = { # Regular A
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1200.regular.a"
		custom_tooltip = "nomads.1200.a.tt"
		custom_tooltip = "situation_continues_mutiny_tt"
	}
	option = { # Regular B
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1200.regular.b"
		custom_tooltip = "nomads.1200.b.regular.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value <= 0.5
				}
			}
			custom_tooltip = "situation_flips_planetfall_tt"
		}
		else = {
			custom_tooltip = "situation_slows_mutiny_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = mandatory_crew_rest
					days = 1800
				}
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Regular C
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1200.regular.c"
		custom_tooltip = "nomads.1200.c.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value <= 0.5
				}
			}
			custom_tooltip = "situation_flips_planetfall_tt"
		}
		else = {
			custom_tooltip = "situation_slows_mutiny_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = scaled_back_passenger_programs
					days = 1800
				}
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Hive A
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1200.hive.a"
		custom_tooltip = "nomads.1200.a.tt"
		custom_tooltip = "situation_continues_mutiny_tt"
	}
	option = { # Hive B
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1200.hive.b"
		custom_tooltip = "nomads.1200.b.gestalt.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value <= 0.5
				}
			}
			custom_tooltip = "situation_flips_planetfall_tt"
		}
		else = {
			custom_tooltip = "situation_slows_mutiny_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = mandatory_crew_rest
					days = 1800
				}
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Hive C
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1200.hive.c"
		custom_tooltip = "nomads.1200.c.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value <= 0.5
				}
			}
			custom_tooltip = "situation_flips_planetfall_tt"
		}
		else = {
			custom_tooltip = "situation_slows_mutiny_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = scaled_back_passenger_programs
					days = 1800
				}
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Machine A
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1200.machine.a"
		custom_tooltip = "nomads.1200.a.tt"
		custom_tooltip = "situation_continues_mutiny_tt"
	}
	option = { # Machine B
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1200.machine.b"
		custom_tooltip = "nomads.1200.b.gestalt.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value <= 0.5
				}
			}
			custom_tooltip = "situation_flips_planetfall_tt"
		}
		else = {
			custom_tooltip = "situation_slows_mutiny_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = mandatory_crew_rest
					days = 1800
				}
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Machine C
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1200.machine.c"
		custom_tooltip = "nomads.1200.c.tt"
		if = {
			limit = {
				check_variable = {
					which = passenger_satisfaction_index
					value <= 0.5
				}
			}
			custom_tooltip = "situation_flips_planetfall_tt"
		}
		else = {
			custom_tooltip = "situation_slows_mutiny_tt"
		}
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = scaled_back_passenger_programs
					days = 1800
				}
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
}

# Stage start event for stage 5
# Fired by: situation on_first_enter
situation_event = {
	id = nomads.1250
	title = "nomads.1250.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1250.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1250.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1250.machine.desc"
	}
	picture = GFX_evt_arkship_bridge
	show_sound = event_public_unrest
	is_triggered_only = yes

	immediate = {
		set_situation_flag = crew_mutiny_demanded
	}

	option = { # Regular A
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1250.regular.a"
		custom_tooltip = "nomads.1250.a.tt"
		custom_tooltip = "situation_continues_mutiny_tt"
	}
	option = { # Regular B
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1250.regular.b"
		custom_tooltip = "nomads.1250.b.tt"
		custom_tooltip = "situation_flips_planetfall_tt"
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = empowered_crew_leaders
					days = 3600
				}
			}
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
			set_timed_situation_flag = {
				flag = satisfaction_crew_grace_period
				months = 24
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Regular C
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1250.regular.c"
		custom_tooltip = "nomads.1250.c.tt"
		custom_tooltip = "situation_flips_planetfall_tt"
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = suspended_passenger_services
					days = 3600
				}
			}
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
			set_timed_situation_flag = {
				flag = satisfaction_crew_grace_period
				months = 24
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Hive A
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1250.hive.a"
		custom_tooltip = "nomads.1250.a.tt"
		custom_tooltip = "situation_continues_mutiny_tt"
	}
	option = { # Hive B
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1250.hive.b"
		custom_tooltip = "nomads.1250.b.tt"
		custom_tooltip = "situation_flips_planetfall_tt"
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = empowered_crew_leaders
					days = 3600
				}
			}
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
			set_timed_situation_flag = {
				flag = satisfaction_crew_grace_period
				months = 24
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Hive C
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1250.hive.c"
		custom_tooltip = "nomads.1250.c.tt"
		custom_tooltip = "situation_flips_planetfall_tt"
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = suspended_passenger_services
					days = 3600
				}
			}
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
			set_timed_situation_flag = {
				flag = satisfaction_crew_grace_period
				months = 24
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Machine A
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1250.machine.a"
		custom_tooltip = "nomads.1250.a.tt"
		custom_tooltip = "situation_continues_mutiny_tt"
	}
	option = { # Machine B
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1250.machine.b"
		custom_tooltip = "nomads.1250.b.tt"
		custom_tooltip = "situation_flips_planetfall_tt"
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = empowered_crew_leaders
					days = 3600
				}
			}
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
			set_timed_situation_flag = {
				flag = satisfaction_crew_grace_period
				months = 24
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
	option = { # Machine C
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1250.machine.c"
		custom_tooltip = "nomads.1250.c.tt"
		custom_tooltip = "situation_flips_planetfall_tt"
		hidden_effect = {
			owner = {
				set_timed_country_flag = {
					flag = suspended_passenger_services
					days = 3600
				}
			}
			set_variable = {
				which = passenger_satisfaction_index
				value = @cruise_contract_sub
			}
			set_timed_situation_flag = {
				flag = satisfaction_crew_grace_period
				months = 24
			}
		}
		owner = { decrease_passenger_satisfaction = yes }
	}
}

# Check if the mutiny demand has been handled
# Fired by: situation on_monthly
situation_event = {
	id = nomads.1255
	title = "nomads.1255.name"
	desc = {
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		text = "nomads.1255.regular.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		text = "nomads.1255.hive.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		text = "nomads.1255.machine.desc"
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = no }
		}
		picture = GFX_evt_salute
	}
	picture = {
		trigger = {
			owner = { is_synthetic_empire = yes }
		}
		picture = GFX_evt_robot_assembly_plant
	}
	show_sound = event_conversation
	is_triggered_only = yes

	trigger = {
		has_situation_flag = crew_mutiny_demanded
		situation_progress < 800
	}

	immediate = {
		remove_situation_flag = crew_mutiny_demanded
	}

	option = { # Regular
		trigger = {
			owner = {
				is_hive_empire = no
				is_machine_empire = no
			}
		}
		name = "nomads.1255.regular.a"
	}
	option = { # Hive
		trigger = {
			owner = {
				is_hive_empire = yes
			}
		}
		name = "nomads.1255.hive.a"
	}
	option = { # Machine
		trigger = {
			owner = {
				is_machine_empire = yes
			}
		}
		name = "nomads.1255.machine.a"
	}
}

# Forever Cruise contract generation delayer
# Fired by: on_yearly_pulse_country
country_event = {
	id = nomads.1300
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
		has_origin = origin_forever_cruise
		NOR = {
			has_country_flag = forever_cruise_planetfall
			has_country_flag = forever_cruise_mutiny
			has_country_flag = forever_cruise_over
		}
	}

	immediate = {
		if = {
			limit = { cruise_favoring_side_approach = yes }
			# Favoring passengers or crew: generate contracts more often
			country_event = { id = nomads.1305 days = 15 random = 160 }
			country_event = { id = nomads.1305 days = 180 random = 160 }
		}
		else = {
			country_event = { id = nomads.1305 days = 15 random = 340 }
		}
	}
}

# Forever Cruise passenger and crew contracts.
# Fired by: nomads.1300
# this = country
country_event = {
	id = nomads.1305
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
		has_origin = origin_forever_cruise
		OR = {
			count_issued_contracts < 3
			# Higher backlog allowed while favoring passengers or crew
			AND = {
				count_issued_contracts < 5
				cruise_favoring_side_approach = yes
			}
		}
		any_situation = {
			is_situation_type = situation_passenger_satisfaction
			OR = {
				situation_progress < 450
				situation_progress > 550
			}
		}
		NOR = {
			has_country_flag = forever_cruise_planetfall
			has_country_flag = forever_cruise_mutiny
			has_country_flag = forever_cruise_over
		}
	}

	immediate = {
		save_event_target_as = nomad_country
		set_variable = { which = cruise_contract_attempts value = 0 }
		while = {
			limit = {
				NOT = { has_country_flag = cruise_contract_issued }
				check_variable = { which = cruise_contract_attempts value < 5 }
			}
			change_variable = { which = cruise_contract_attempts value = 1 }
			reroll_random = yes
			random_list = {
				# Sun Trip Passenger
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = passenger
						VARIANT = 1
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = passenger
						VARIANT = 2
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = passenger
						VARIANT = 3
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = passenger
						VARIANT = 4
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = passenger
						VARIANT = 5
					}
				}
				# Sun Trip Crew
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = crew
						VARIANT = 1
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = crew
						VARIANT = 2
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = crew
						VARIANT = 3
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = crew
						VARIANT = 4
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_suntrip_cruise_event
						ISSUER = crew
						VARIANT = 5
					}
				}
				# Shore Leave Passenger
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = passenger
						VARIANT = 1
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = passenger
						VARIANT = 2
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = passenger
						VARIANT = 3
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}

					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = passenger
						VARIANT = 4
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = passenger
						VARIANT = 5
					}
				}
				# Shore Leave Crew
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = crew
						VARIANT = 1
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = crew
						VARIANT = 2
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = crew
						VARIANT = 3
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = crew
						VARIANT = 4
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_shoreleave_cruise_event
						ISSUER = crew
						VARIANT = 5
					}
				}
				# Espionage Support Passenger
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = passenger
						VARIANT = 1
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = passenger
						VARIANT = 2
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = passenger
						VARIANT = 3
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = passenger
						VARIANT = 4
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_passenger
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = passenger
						VARIANT = 5
					}
				}
				# Espionage Support Crew
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = crew
						VARIANT = 1
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = crew
						VARIANT = 2
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = crew
						VARIANT = 3
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = crew
						VARIANT = 4
					}
				}
				10 = {
					modifier = {
						factor = 2
						any_situation = {
							is_situation_type = situation_passenger_satisfaction
							current_situation_approach = situation_passenger_satisfaction_crew
						}
					}
					inline_script = {
						script = events/nomads_espionage_support_cruise_event
						ISSUER = crew
						VARIANT = 5
					}
				}
			}
		}
		remove_country_flag = cruise_contract_issued
		remove_country_flag = cruise_suntrip_failed
		remove_country_flag = cruise_shoreleave_failed
		remove_country_flag = cruise_espionage_failed
	}
}

#######################
# NOMADIC PARAGONS
#######################

# Introduction from the Champions Forge Enclave
country_event = {
	id = nomads.1500
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		random_list = {
			1 = { # The Revenant
				modifier = {
					factor = 0
					can_hire_nomadic_paragon_1 = no
				}
				ethic_leader_hire_event = { LEADER_ID = nomadic_revenant EVENT_ID = nomads.1505 }
			}
			1 = { # The Sage
				modifier = {
					factor = 0
					can_hire_nomadic_paragon_2 = no
				}
				modifier = {
					factor = 0
					is_gestalt = yes
				}
				ethic_leader_hire_event = { LEADER_ID = nomadic_sage EVENT_ID = nomads.1515 }
			}
			1 = { # The Naturalist
				modifier = {
					factor = 0
					can_hire_nomadic_paragon_3 = no
				}
				modifier = {
					factor = 1.5
					is_materialist = yes
				}
				ethic_leader_hire_event = { LEADER_ID = nomadic_naturalist EVENT_ID = nomads.1520 }
			}
			1 = { # The Caregiver
				modifier = {
					factor = 0
					can_hire_nomadic_paragon_4 = no
				}
				modifier = {
					factor = 1.5
					is_spiritualist = yes
				}
				ethic_leader_hire_event = { LEADER_ID = nomadic_caregiver EVENT_ID = nomads.1530 }
			}
			1 = { # The Champion
				modifier = {
					factor = 0
					can_hire_nomadic_paragon_5 = no
				}
				ethic_leader_hire_event = { LEADER_ID = nomadic_champion EVENT_ID = nomads.1555 }
			}
			1 = { # The Lost Princess
				modifier = {
					factor = 0
					can_hire_nomadic_paragon_6 = no
				}
				ethic_leader_hire_event = { LEADER_ID = nomadic_princess EVENT_ID = nomads.1580 }
			}
		}
	}
}

# Orun Veyth, The Revenant
country_event = {
	id = nomads.1505
	title = "nomads.1505.name"
	desc = "nomads.1505.desc"
	show_sound = nom_paragon_1_orun_veyth_the_revenant
	is_triggered_only = yes

	picture = GFX_leader_recruitment_bg_renowned
	auto_opens = no
	event_window_type = leader_recruit
	notification_event_icon = "GFX_message_leader_recruitment_revenant"

	immediate = {
		ethic_leader_creator = {
			SPECIES_CLASS = random_non_machine
			LEADER_PORTRAIT = par_renown_17
			CLASS = commander
			TIER = leader_tier_renowned
			NAME = NAME_nomadic_revenant
			LEVEL = 4
			GENDER = indeterminable
			IMMORTAL = no
			ETHIC = egalitarian
			DESC = DESC_nomadic_revenant
			CATCH_PHRASE = catch_phrase_nomadic_revenant
		}
		last_created_leader = {
			add_trait = {
				trait = subclass_commander_admiral
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_unyielding
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_artillerist
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_volatile
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_adaptable
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_nomadic_revenant
				consume_selection = yes
			}
			set_age = 42
		}
	}

	picture_event_data = {
		portrait = event_target:ethic_leader
		room = ethic_spaceship_room
	}

	option = {
		name = DISMISS
		tag = dismiss_leader
		hidden_effect = {
			remove_global_flag = nomadic_revenant # Put this leader back into the pool, so player can still get them
			ethic_leader_kill_effect = yes
			set_country_flag = dismissed_nomadic_revenant
		}
	}
	option = {
		name = nomads.1505.a
		response_text = nomads.1505.a.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1505.b
		response_text = nomads.1505.b.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1505.c
		response_text = nomads.1505.c.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1505.d
		response_text = nomads.1505.d.response
		is_dialog_only = yes
	}
	option = {
		trigger = {
			species_portrait = humanoid_02
		}
		name = nomads.1505.e
		response_text = nomads.1505.e.response
		is_dialog_only = yes
	}
	option = {
		name = HIRE
		tag = hire_leader
		custom_tooltip = nomads.1505.hire_custom_tooltip
		hidden_effect = {
			ethic_leader_hire_effect = {
				GLOBAL_EVENT_TARGET = nomadic_revenant
			}
			set_country_flag = hired_nomadic_revenant
			set_variable = {
				which = revenant_oath
				value = 1
			}
			set_global_flag = hired_nomadic_paragon_1
		}
	}
}

# Fired by on_fleet_destroyed_perp
# From = owner of fleet 2 (destroyed)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = nomads.1510
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_country_flag = hired_nomadic_revenant
		NOT = {
			has_country_flag = revenant_oath_flag
		}
		FromFrom.leader = {
			has_leader_flag = nomadic_revenant
		}
		from = {
			has_country_flag = first_pop_purged
			NOT = {
				has_country_flag = razed_by_revenant
			}
		}
	}

	immediate = {
		if = {
			limit = {
				check_variable = {
					which = revenant_oath
					value = 4
				}
			}
			set_country_flag = revenant_oath_flag
		}
		else = {
			change_variable = {
				which = revenant_oath
				value = 1
			}
		}
		from = {
			set_country_flag = razed_by_revenant
		}
		if = {
			limit = {
				NOT = {
					has_country_flag = nomadic_revenant_first_revenge
				}
			}
			set_country_flag =  nomadic_revenant_first_revenge
			country_event = {
				id = nomads.1511
			}
		}
		else = {
			country_event = {
				id = nomads.1512
			}
		}
	}
}

country_event = {
	id = nomads.1511
	title = "nomads.1511.name"
	desc = "nomads.1511.desc"
	picture = GFX_evt_large_explosion
	show_sound = event_screams
	event_window_type = leader_story

	picture_event_data = {
		portrait = event_target:nomadic_revenant
		room = root.owner
	}

	is_triggered_only = yes

	option = {
		name = nomads.1511.a
		custom_tooltip = nomads.1511.a.tt
	}
}

country_event = {
	id = nomads.1512
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		create_message = {
			type = MESSAGE_NOMADIC_REVENANT
			localization =  MESSAGE_NOMADIC_REVENANT_DESC
			days = @toast_message_days
			target = event_target:nomadic_revenant
			variable = {
				type = name
				localization = LEADER
				scope = event_target:nomadic_revenant
			}
			variable = {
				key = "border"
				value = "GFX_border_veteran"
			}
		}
	}
}

#Ashar, The Sage
country_event = {
	id = nomads.1515
	title = "nomads.1515.name"
	desc = "nomads.1515.desc"
	show_sound = nom_paragon_2_ashar_the_wandering_sage
	is_triggered_only = yes

	picture = GFX_leader_recruitment_bg_renowned
	auto_opens = no
	event_window_type = leader_recruit
	notification_event_icon = "GFX_message_leader_recruitment_sage"

	immediate = {
		ethic_leader_creator = {
			SPECIES_CLASS = random_non_machine
			LEADER_PORTRAIT = par_renown_20
			CLASS = official
			TIER = leader_tier_renowned
			NAME = NAME_nomadic_sage
			LEVEL = 4
			GENDER = female
			IMMORTAL = no
			ETHIC = pacifist
			DESC = DESC_nomadic_sage
			CATCH_PHRASE = catch_phrase_nomadic_sage
		}
		last_created_leader = {
			add_trait = {
				trait = subclass_official_economy_councilor
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_intellectual
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_cultural_focus
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_irenic
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_venerated
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_nomadic_sage
				consume_selection = yes
			}
			set_age = 61
		}
	}

	picture_event_data = {
		portrait = event_target:ethic_leader
		room = ethic_spaceship_room
	}

	option = {
		name = DISMISS
		tag = dismiss_leader
		hidden_effect = {
			remove_global_flag = nomadic_sage # Put this leader back into the pool, so player can still get them
			ethic_leader_kill_effect = yes
			set_country_flag = dismissed_nomadic_sage
		}
	}
	option = {
		name = nomads.1515.a
		response_text = nomads.1515.a.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1515.b
		response_text = nomads.1515.b.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1515.c
		response_text = nomads.1515.c.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1515.d
		response_text = nomads.1515.d.response
		is_dialog_only = yes
	}
		option = {
		name = nomads.1515.e
		response_text = nomads.1515.e.response
		is_dialog_only = yes
	}
	option = {
		name = HIRE
		tag = hire_leader
		custom_tooltip = nomads.1515.hire_custom_tooltip
		hidden_effect = {
			ethic_leader_hire_effect = {
				GLOBAL_EVENT_TARGET = nomadic_sage
			}
			set_country_flag = hired_nomadic_sage
			set_global_flag = hired_nomadic_paragon_2
		}
	}
}

#Sero the Younger, the Naturalist
country_event = {
	id = nomads.1520
	title = "nomads.1520.name"
	desc = "nomads.1520.desc"
	show_sound = nom_paragon_4_sero_the_younger_the_naturalist
	is_triggered_only = yes

	picture = GFX_leader_recruitment_bg_renowned
	auto_opens = no
	event_window_type = leader_recruit
	notification_event_icon = "GFX_message_leader_recruitment_naturalist"

	immediate = {
		ethic_leader_creator = {
			SPECIES_CLASS = random_non_machine
			LEADER_PORTRAIT = par_renown_18
			CLASS = scientist
			TIER = leader_tier_renowned
			NAME = NAME_nomadic_naturalist
			LEVEL = 4
			GENDER = male
			IMMORTAL = no
			ETHIC = materialist
			DESC = DESC_nomadic_naturalist
			CATCH_PHRASE = catch_phrase_nomadic_naturalist
		}
		last_created_leader = {
			add_trait = {
				trait = subclass_scientist_scholar
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_roamer
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_meticulous
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_observant
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_lawless
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_nomadic_naturalist
				consume_selection = yes
			}
			set_age = 50
		}
	}

	picture_event_data = {
		portrait = event_target:ethic_leader
		room = ethic_spaceship_room
	}

	option = {
		name = DISMISS
		tag = dismiss_leader
		hidden_effect = {
			remove_global_flag = nomadic_naturalist # Put this leader back into the pool, so player can still get them
			ethic_leader_kill_effect = yes
			set_country_flag = dismissed_nomadic_naturalist
		}
	}
	option = {
		name = nomads.1520.a
		response_text = nomads.1520.a.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1520.b
		response_text = nomads.1520.b.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1520.c
		response_text = nomads.1520.c.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1520.d
		response_text = nomads.1520.d.response
		is_dialog_only = yes
	}
	option = {
		trigger = {
			is_species_class = ART
		}
		name = nomads.1520.e
		response_text = nomads.1520.e.response
		is_dialog_only = yes
	}
	option = {
		name = HIRE
		tag = hire_leader
		custom_tooltip = nomads.1520.hire_custom_tooltip
		add_tech_option_or_research_effect = {
			TECH = tech_alien_life_studies
			PROGRESS = 0.25
			CATEGORY = society_research
		}
		hidden_effect = {
			ethic_leader_hire_effect = {
				GLOBAL_EVENT_TARGET = nomadic_naturalist
			}
			set_country_flag = hired_nomadic_naturalist
			country_event = {
				id = nomads.1522
				days = 1
			}
			set_global_flag = hired_nomadic_paragon_3
		}
	}
}

# Hidden Event to spawn Sero's pre-sapients after hiring
country_event = {
	id = nomads.1522
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				NOT = {
					has_country_flag = found_presapients
				}
			}
			set_country_flag = found_presapients
		}
		capital_scope = {
			save_event_target_as = pre_sapient_planet
			spawn_presapient_species_randomizer_effect = yes
		}
	}
}

# Hidden Event to trigger Leader UI Popup and a Toast Thereafter
# Fired by Deep Scan, life_in_motion_effect
country_event = {
	id = nomads.1523
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				NOT = {
					has_country_flag = nomadic_naturalist_first_presapients
				}
			}
			set_country_flag =  nomadic_naturalist_first_presapients
			country_event = {
				id = nomads.1525
			}
		}
		else = {
			country_event = {
				id = nomads.1526
			}
		}
	}
}

# Hidden Event to trigger Leader UI Popup and a Toast Thereafter
# Fired by on_survey_planet
# Scope = Ship
# From = Planet
ship_event = {
	id = nomads.1524
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		exists = leader
		leader = {
			has_leader_flag = nomadic_naturalist
		}
		from = {
			any_owned_pop_group = {
				is_sapient = no
			}
		}
	}

	immediate = {
		from = {
			random_owned_pop_group = {
				limit = {
					is_sapient = no
				}
				save_event_target_as = nomadic_naturalist_presapients
			}
		}
		owner = {
			if = {
				limit = {
					NOT = {
						has_country_flag = nomadic_naturalist_first_presapients
					}
				}
				set_country_flag =  nomadic_naturalist_first_presapients
				country_event = {
					id = nomads.1525
				}
			}
			else = {
				country_event = {
					id = nomads.1526
				}
			}
		}
	}
}

country_event = {
	id = nomads.1525
	title = "nomads.1525.name"
	desc = "nomads.1525.desc"
	picture = GFX_evt_animal_wildlife
	show_sound = event_dangerous_animals
	event_window_type = leader_story

	picture_event_data = {
		portrait = event_target:nomadic_naturalist
		room = root.owner
	}

	is_triggered_only = yes

	option = {
		trigger = {
			is_xenophile = yes
		}
		name = nomads.1525.a
		capital_scope = {
			create_pop_group = {
				species = event_target:nomadic_naturalist_presapients
			}
		}
	}
	option = {
		trigger = {
			is_xenophile = no
		}
		name = nomads.1525.b
		capital_scope = {
			create_pop_group = {
				species = event_target:nomadic_naturalist_presapients
			}
		}
	}
}

country_event = {
	id = nomads.1526
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		create_message = {
			type = MESSAGE_NOMADIC_NATURALIST
			localization =  MESSAGE_NOMADIC_NATURALIST_DESC
			days = @toast_message_days
			target = event_target:nomadic_naturalist
			variable = {
				type = name
				localization = LEADER
				scope = event_target:nomadic_naturalist
			}
			variable = {
				key = "border"
				value = "GFX_border_veteran"
			}
		}
		capital_scope = {
			create_pop_group = {
				species = event_target:nomadic_naturalist_presapients
			}
		}
	}
}

#Unit Solace, The Caregiver
country_event = {
	id = nomads.1530
	title = "nomads.1530.name"
	desc = "nomads.1530.desc"
	show_sound = nom_paragon_5_unit_solace_the_caregiver
	is_triggered_only = yes

	picture = GFX_leader_recruitment_bg_renowned
	auto_opens = no
	event_window_type = leader_recruit
	notification_event_icon = "GFX_message_leader_recruitment_caregiver"

	immediate = {
		ethic_leader_creator = {
			SPECIES_CLASS = AI
			LEADER_PORTRAIT = par_renown_22
			CLASS = official
			TIER = leader_tier_renowned
			NAME = NAME_nomadic_caregiver
			LEVEL = 4
			GENDER = male
			IMMORTAL = no
			ETHIC = spiritualist
			DESC = DESC_nomadic_caregiver
			CATCH_PHRASE = catch_phrase_nomadic_caregiver
		}
		last_created_leader = {
			add_trait = {
				trait = subclass_official_governor
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_pop_focused
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_righteous
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_bureaucrat
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_procrastinator
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_nomadic_caregiver
				consume_selection = yes
			}
			give_trait_no_notify_ignore_autocracy_restriction = {
				TRAIT = leader_trait_synthetic
			}
			set_age = 35
		}
	}

	picture_event_data = {
		portrait = event_target:ethic_leader
		room = ethic_spaceship_room
	}

	option = {
		name = DISMISS
		tag = dismiss_leader
		hidden_effect = {
			remove_global_flag = nomadic_caregiver # Put this leader back into the pool, so player can still get them
			ethic_leader_kill_effect = yes
			set_country_flag = dismissed_nomadic_caregiver
		}
	}
	option = {
		name = nomads.1530.a
		response_text = nomads.1530.a.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1530.b
		response_text = nomads.1530.b.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1530.c
		response_text = nomads.1530.c.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1530.d
		response_text = nomads.1530.d.response
		is_dialog_only = yes
	}
	option = {
		trigger = {
			has_country_flag = synthetic_empire
		}
		name = nomads.1530.e
		response_text = nomads.1530.e.response
		is_dialog_only = yes
	}
	option = {
		name = HIRE
		tag = hire_leader
		hidden_effect = {
			ethic_leader_hire_effect = {
				GLOBAL_EVENT_TARGET = nomadic_caregiver
			}
			set_country_flag = hired_nomadic_caregiver
			set_global_flag = hired_nomadic_paragon_4
			set_variable = {
				which = caregiver_contracts_completed
				value = 0
			}
		}
	}
}

# Hidden Event to improve trait buffs; triggers Leader UI Popup and a Toast Thereafter
# Fired by nomads.3002 (Contract Completed)
country_event = {
	id = nomads.1535
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		NOT = {
			has_country_flag = caregiver_contract_flag
		}
	}

	immediate = {
		if = {
			limit = {
				check_variable = {
					which = caregiver_contracts_completed
					value = 5
				}
			}
			set_country_flag = caregiver_contract_flag
		}
		else = {
			change_variable = {
				which = caregiver_contracts_completed
				value = 1
			}
		}
		if = {
			limit = {
				NOT = {
					has_country_flag = first_creche
				}
			}
			set_country_flag = first_creche
			country_event = {
				id = nomads.1545
			}
		}
		else = {
			country_event = {
				id = nomads.1550
			}
		}
	}
}

country_event = {
	id = nomads.1545
	title = "nomads.1545.name"
	desc = {
		trigger = {
			is_machine_empire = no
			is_gestalt = no
		}
		text = nomads.1545.desc.normal
	}
	desc = {
		trigger = {
			is_machine_empire = yes
		}
		text = nomads.1545.desc.machine
	}
	desc = {
		trigger = {
			is_machine_empire = no
			is_gestalt = yes
		}
		text = nomads.1545.desc.hive
	}
	picture = GFX_evt_friendly_enlightenment
	show_sound = event_hand_shake
	event_window_type = leader_story

	picture_event_data = {
		portrait = event_target:nomadic_caregiver
		room = root.owner
	}

	is_triggered_only = yes

	option = {
		trigger = {
			is_egalitarian = yes
			is_gestalt = no
		}
		name = EXCELLENT
		custom_tooltip = nomads.1545.effect_tt
	}
	option = {
		trigger = {
			is_egalitarian = no
			is_gestalt = no
		}
		name = nomads.1545.b
		custom_tooltip = nomads.1545.effect_tt
	}
	option = {
		trigger = {
			is_gestalt = yes
		}
		name = nomads.1545.c
		custom_tooltip = nomads.1545.effect_tt
	}
}

country_event = {
	id = nomads.1550
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		create_message = {
			type = MESSAGE_NOMADIC_CAREGIVER
			localization =  MESSAGE_NOMADIC_CAREGIVER_DESC
			days = @toast_message_days
			target = event_target:nomadic_caregiver
			variable = {
				type = name
				localization = LEADER
				scope = event_target:nomadic_caregiver
			}
			variable = {
				key = "border"
				value = "GFX_border_veteran"
			}
		}
	}
}

#Th'aal Hask'Guron, The Champion
country_event = {
	id = nomads.1555
	title = "nomads.1555.name"
	desc = "nomads.1555.desc"
	show_sound = nom_paragon_6_thaal_huskgoron_the_champion
	is_triggered_only = yes

	picture = GFX_leader_recruitment_bg_renowned
	auto_opens = no
	event_window_type = leader_recruit
	notification_event_icon = "GFX_message_leader_recruitment_champion"

	immediate = {
		ethic_leader_creator = {
			SPECIES_CLASS = AI
			LEADER_PORTRAIT = par_renown_21
			CLASS = commander
			TIER = leader_tier_renowned
			NAME = NAME_nomadic_champion
			LEVEL = 4
			GENDER = male
			IMMORTAL = no
			ETHIC = authoritarian
			DESC = DESC_nomadic_champion
			CATCH_PHRASE = catch_phrase_nomadic_champion
		}
		last_created_leader = {
			add_trait = {
				trait = subclass_commander_general
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_aggressive
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_dreaded
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_hardy
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_victory_or_death
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_nomadic_champion
				consume_selection = yes
			}
			set_age = 24
		}
		set_variable = {
			which = champion_trophies_collected
			value = 0
		}
	}

	picture_event_data = {
		portrait = event_target:ethic_leader
		room = ethic_spaceship_room
	}

	option = {
		name = DISMISS
		tag = dismiss_leader
		hidden_effect = {
			remove_global_flag = nomadic_champion # Put this leader back into the pool, so player can still get them
			ethic_leader_kill_effect = yes
			set_country_flag = dismissed_nomadic_champion
		}
	}
	option = {
		name = nomads.1555.a
		response_text = nomads.1555.a.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1555.b
		response_text = nomads.1555.b.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1555.c
		response_text = nomads.1555.c.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1555.d
		response_text = nomads.1555.d.response
		is_dialog_only = yes
	}
	option = {
		trigger = {
			species_portrait = mam9
		}
		name = nomads.1555.e
		response_text = nomads.1555.e.response
		is_dialog_only = yes
	}
	option = {
		trigger = {
			species_portrait = pro2
		}
		name = nomads.1555.f
		response_text = nomads.1555.f.response
		is_dialog_only = yes
	}
	option = {
		name = HIRE
		tag = hire_leader
		random_owned_colony = {
			limit = {
				is_capital = yes
			}
			add_deposit = d_trophy_hall
		}
		hidden_effect = {
			ethic_leader_hire_effect = {
				GLOBAL_EVENT_TARGET = nomadic_champion
			}
			set_country_flag = hired_nomadic_champion
			set_global_flag = hired_nomadic_paragon_5
		}
	}
}

# Trigger Event for Champion Trophies
# Fired by on_colony_attackers_win
# This = country, lead attacker
# From = country, colony owner
# FromFrom = colony
country_event = {
	id = nomads.1556
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		FromFrom = {
			NOT = {
				planet = { has_planet_flag = invaded_by_champion }
			}
		}
		has_country_flag = hired_nomadic_champion
		FromFrom = {
			any_ground_combat_attacker = {
				leader? = {
					has_leader_flag = nomadic_champion
				}
			}
		}
	}

	immediate = {
		FromFrom = {
			planet = { set_planet_flag = invaded_by_champion }
		}
		if = {
			limit = {
				check_variable = {
					which = champion_trophies_collected
					value < 7
				}
			}
			random_list = {
				3 = {
					# Nothing Happens
				}
				1 = { # Trophy Found
					change_variable = {
						which = champion_trophies_collected
						value = 1
					}
					country_event = {
						id = nomads.1560 # Trophy Acquired event
						days = 2
					}
				}
			}
		}
	}
}

# Hidden Event if Champion Trophy found during invasion; triggers Leader UI Popup and a Toast Thereafter
country_event = {
	id = nomads.1560
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				NOT = {
					has_country_flag = first_trophy
				}
			}
			set_country_flag = first_trophy
			country_event = {
				id = nomads.1565
			}
		}
		else = {
			country_event = {
				id = nomads.1570
			}
		}
	}
}

country_event = {
	id = nomads.1565
	title = "nomads.1565.name"
	desc = "nomads.1565.desc"
	picture = GFX_evt_analyzing_artifacts
	show_sound = event_celebration
	event_window_type = leader_story

	picture_event_data = {
		portrait = event_target:nomadic_champion
		room = root.owner
	}

	is_triggered_only = yes

	option = {
		name = EXCELLENT
		custom_tooltip = nomads.1565.a.tt
	}
}

country_event = {
	id = nomads.1570
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		create_message = {
			type = MESSAGE_NOMADIC_CHAMPION
			localization =  MESSAGE_NOMADIC_CHAMPION_DESC
			days = @toast_message_days
			target = event_target:nomadic_champion
			variable = {
				type = name
				localization = LEADER
				scope = event_target:nomadic_champion
			}
			variable = {
				key = "border"
				value = "GFX_border_veteran"
			}
		}
	}
}

#Seralune, the Lost Princess
country_event = {
	id = nomads.1580
	title = "nomads.1580.name"
	desc = "nomads.1580.desc"
	show_sound = nom_paragon_3_seralune_the_lost_princess
	is_triggered_only = yes

	picture = GFX_leader_recruitment_bg_renowned
	auto_opens = no
	event_window_type = leader_recruit
	notification_event_icon = "GFX_message_leader_recruitment_princess"

	immediate = {
		ethic_leader_creator = {
			SPECIES_CLASS = random_non_machine
			LEADER_PORTRAIT = par_renown_19
			CLASS = official
			TIER = leader_tier_legendary
			NAME = NAME_nomadic_princess
			LEVEL = 4
			GENDER = female
			IMMORTAL = no
			ETHIC = xenophile
			DESC = DESC_nomadic_princess
			CATCH_PHRASE = catch_phrase_nomadic_princess
		}
		last_created_leader = {
			add_trait = {
				trait = subclass_official_delegate
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_politician
				consume_selection = yes
			}
			add_trait = {
				trait = trait_ruler_architectural_sense
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_reformer
				consume_selection = yes
			}
			add_trait = {
				trait = leader_trait_amnesiac
				consume_selection = yes
			}
			set_age = 17
		}
	}

	picture_event_data = {
		portrait = event_target:ethic_leader
		room = ethic_spaceship_room
	}

	option = {
		name = DISMISS
		tag = dismiss_leader
		hidden_effect = {
			remove_global_flag = nomadic_princess # Put this leader back into the pool, so player can still get them
			ethic_leader_kill_effect = yes
			set_country_flag = dismissed_nomadic_princess
		}
	}
	option = {
		name = nomads.1580.a
		response_text = nomads.1580.a.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1580.b
		response_text = nomads.1580.b.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1580.c
		response_text = nomads.1580.c.response
		is_dialog_only = yes
	}
	option = {
		name = nomads.1580.d
		response_text = nomads.1580.d.response
		is_dialog_only = yes
	}
	option = {
		trigger = {
			is_human_species = yes
		}
		name = nomads.1580.e
		response_text = nomads.1580.e.response
		is_dialog_only = yes
	}
	option = {
		name = HIRE
		tag = hire_leader
		custom_tooltip = nomads.1580.hire_custom_tooltip
		hidden_effect = {
			ethic_leader_hire_effect = {
				GLOBAL_EVENT_TARGET = nomadic_princess
				TIER = leader_tier_legendary
			}
			set_country_flag = hired_nomadic_princess
			country_event = {
				id = nomads.1585
				days = 720
				random = 120
			}
			set_global_flag = hired_nomadic_paragon_6
		}
	}
}

country_event = {
	id = nomads.1585
	title = "nomads.1585.name"
	desc = "nomads.1585.desc"
	picture = GFX_evt_binary_stars
	show_sound = event_whispering
	event_window_type = leader_story

	picture_event_data = {
		portrait = event_target:nomadic_princess
		room = root.owner
	}

	is_triggered_only = yes

	immediate = {
		begin_event_chain = {
			event_chain = nomadic_princess_chain
			target = root
		}
		create_princess_poi = {
			NUMBER = 1
		}
		create_princess_poi = {
			NUMBER = 2
		}
		create_princess_poi = {
			NUMBER = 3
		}
	}

	option = {
		name = INTRIGUING
		tooltip = {
			begin_event_chain = {
				event_chain = nomadic_princess_chain
				target = nomadic_princess_poi_target
			}
		}
	}
}

# Hidden event determines if the first POI successfully triggers the princess' memories, fired by on_entering_system_fleet
# from = system
fleet_event = {
	id = nomads.1590
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		from = {
			has_star_flag = princess_poi_system
		}
		owner = {
			NOT = {
				has_country_flag = princess_poi_first_memory
			}
			any_owned_leader = {
				has_leader_flag = nomadic_princess
			}
		}
	}

	immediate = {
		from = {
			if = {
				limit = {
					any_system_planet = {
						has_planet_flag = princess_poi_planet_1
					}
				}
				root.owner = {
					remove_point_of_interest = nomadic_princess_poi_1
				}
				remove_star_flag = princess_poi_system
			}
			if = {
				limit = {
					any_system_planet = {
						has_planet_flag = princess_poi_planet_2
					}
				}
				root.owner = {
					remove_point_of_interest = nomadic_princess_poi_2
				}
				remove_star_flag = princess_poi_system
			}
			if = {
				limit = {
					any_system_planet = {
						has_planet_flag = princess_poi_planet_3
					}
				}
				root.owner = {
					remove_point_of_interest = nomadic_princess_poi_3
				}
				remove_star_flag = princess_poi_system
			}
		}
		owner = {
			if = {
				limit = {
					NOT = {
						has_country_flag = princess_poi_still_forgets
					}
				}
				random_list = {
					75 = {
						country_event = {
							id = nomads.1595
						}
					}
					25 = {
						set_country_flag = princess_poi_first_memory
						country_event = {
							id = nomads.1600
						}
					}
				}
			}
			else = {
				set_country_flag = princess_poi_first_memory
				country_event = {
					id = nomads.1600
				}
			}
		}
	}
}

country_event = {
	id = nomads.1595
	title = nomads.1595.name
	desc = nomads.1595.desc
	picture = GFX_evt_arkship_in_orbit
	event_chain = nomadic_princess_chain
	show_sound = event_arkship_bridge
	location = fromfrom

	is_triggered_only = yes

	immediate = {
		set_country_flag = princess_poi_still_forgets
	}

	option = {
		name = UNFORTUNATE
	}
}

country_event = {
	id = nomads.1600
	title = nomads.1600.name
		desc = {
		trigger = {
			is_human_species = no
		}
		text = nomads.1600.nonhuman
	}
	desc = {
		trigger = {
			is_human_species = yes
		}
		text = nomads.1600.human
	}
	picture = GFX_evt_arkship_bridge
	event_chain = nomadic_princess_chain
	show_sound = event_arkship_bridge
	location = fromfrom

	is_triggered_only = yes

	immediate = {
		set_country_flag = princess_poi_first_memory
	}

	option = {
		name = nomads.1600.a
	}
	option = {
		name = nomads.1600.b
		hidden_effect = {
			# This option already reveals the wreck, so block the branch that reveals a second one.
			set_country_flag = princess_poi_every_memory
			remove_point_of_interest = nomadic_princess_poi_1
			remove_point_of_interest = nomadic_princess_poi_2
			remove_point_of_interest = nomadic_princess_poi_3
			random_system = {
				limit = {
					NOT = {
						has_star_flag = princess_poi_system
					}
					distance = {
						source = fromfrom
						min_jumps = 5
					}
					OR = {
						is_star_class = sc_black_hole
						is_inside_nebula = yes
					}
				}
				star = {
					create_ambient_object = {
						type = ambient_arkship_destroyed
					}
					last_created_ambient_object = {
						set_location = {
							target = prev
							distance = 20
							angle = random
						}
						save_event_target_as = seralune_throne
					}
				}
			}
		}
		enable_special_project = {
			name = "NOMADIC_PRINCESS_SPECIAL_PROJECT"
			location = event_target:seralune_throne
			owner = ROOT
		}
	}
}

# The player has chosen to investigate further; fired by on_entering_system_fleet
# from = system
fleet_event = {
	id = nomads.1605
	title = nomads.1605.name
	desc = nomads.1605.desc
	picture = GFX_evt_arkship_bridge
	event_chain = nomadic_princess_chain
	show_sound = event_arkship_bridge
	location = from

	is_triggered_only = yes

	trigger = {
		from = {
			has_star_flag = princess_poi_system
		}
		owner = {
			has_country_flag = princess_poi_first_memory
			NOR = {
				has_country_flag = princess_poi_still_forgets
				has_country_flag = princess_poi_second_memory
			}
			any_owned_leader = {
				has_leader_flag = nomadic_princess
			}
		}
	}

	immediate = {
		owner = {
			set_country_flag = princess_poi_second_memory
		}
		from = {
			if = {
				limit = {
					any_system_planet = {
						has_planet_flag = princess_poi_planet_1
					}
				}
				root.owner = {
					remove_point_of_interest = nomadic_princess_poi_1
				}
				remove_star_flag = princess_poi_system
			}
			if = {
				limit = {
					any_system_planet = {
						has_planet_flag = princess_poi_planet_2
					}
				}
				root.owner = {
					remove_point_of_interest = nomadic_princess_poi_2
				}
				remove_star_flag = princess_poi_system
			}
			if = {
				limit = {
					any_system_planet = {
						has_planet_flag = princess_poi_planet_3
					}
				}
				root.owner = {
					remove_point_of_interest = nomadic_princess_poi_3
				}
				remove_star_flag = princess_poi_system
			}
		}
	}

	option = {
		name = nomads.1605.a
		owner = {
			random_owned_leader = {
				limit = {
					has_leader_flag = nomadic_princess
				}
				add_experience = 1500
			}
		}
	}
}

# The player has chosen to investigate further; fired by on_entering_system_fleet
# from = system
fleet_event = {
	id = nomads.1610
	title = nomads.1610.name
	desc = nomads.1610.desc
	picture = GFX_evt_arkship_crisis
	event_chain = nomadic_princess_chain
	show_sound = event_arkship_bridge
	location = from

	is_triggered_only = yes

	trigger = {
		from = {
			has_star_flag = princess_poi_system
		}
		owner = {
			OR = {
				AND = {
					has_country_flag = princess_poi_still_forgets
					has_country_flag = princess_poi_first_memory
				}
				has_country_flag = princess_poi_second_memory
			}
			NOT = {
				has_country_flag = princess_poi_every_memory
			}
			any_owned_leader = {
				has_leader_flag = nomadic_princess
			}
		}
	}

	immediate = {
		owner = {
			set_country_flag = princess_poi_every_memory
		}
		random_system = {
			limit = {
				NOT = {
					has_star_flag = princess_poi_system
				}
				distance = {
					source = fromfrom
					min_jumps = 5
				}
				OR = {
					is_star_class = sc_black_hole
					is_inside_nebula = yes
				}
			}
			set_star_flag = nomadic_princess_star
			star = {
				create_ambient_object = {
					type = ambient_arkship_destroyed
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 20
						angle = random
					}
					save_event_target_as = seralune_throne
				}
			}
		}
	}

	option = {
		name = nomads.1610.a
		owner = {
			random_owned_leader = {
				limit = {
					has_leader_flag = nomadic_princess
				}
				add_experience = 2000
			}
		}
		enable_special_project = {
			name = "NOMADIC_PRINCESS_SPECIAL_PROJECT"
			location = event_target:seralune_throne
			owner = root.owner
		}
		hidden_effect = {
			owner = {
				remove_point_of_interest = nomadic_princess_poi_1
				remove_point_of_interest = nomadic_princess_poi_2
				remove_point_of_interest = nomadic_princess_poi_3
			}
		}
	}
}

ship_event = {
	id = nomads.1620
	title = nomads.1620.name
	desc = nomads.1620.desc
	picture = GFX_evt_federation_fleet
	event_chain = nomadic_princess_chain
	show_sound = event_arkship_bridge
	location = from

	is_triggered_only = yes

	immediate = {
		owner = {
			random_owned_leader = {
				limit = {
					has_leader_flag = nomadic_princess
				}
				remove_trait = leader_trait_amnesiac
				add_trait = {
					trait = leader_trait_nomadic_princess_1
					show_message = no
				}
				add_trait = {
					trait = leader_trait_nomadic_princess_2
					show_message = no
				}
			}
		}
	}

	option = {
		name = EXCELLENT
		custom_tooltip = nomads.1620.tt
		owner = {
			enable_special_project = {
				name = "NOMADIC_PRINCESS_SPECIAL_PROJECT_2"
				location = event_target:seralune_throne
				owner = root.owner
			}
		}
	}
}

ship_event = {
	id = nomads.1625
	title = nomads.1625.name
	desc = nomads.1625.desc
	picture = GFX_evt_arkship_in_orbit
	event_chain = nomadic_princess_chain
	show_sound = event_celebration
	location = from
	event_window_type = leader_story

	picture_event_data = {
		portrait = event_target:nomadic_princess
		room = root.owner
	}

	is_triggered_only = yes

	immediate = {
		owner = {
			if = {
				limit = {
					NOT = {
						has_technology = tech_civilian_arkship
					}
				}
				give_technology = {
					tech = tech_civilian_arkship
				}
			}
			create_fleet = {
				name = "NAME_princess_fleet"
				effect = {
					set_owner = prev
					set_location = event_target:seralune_throne
					create_ship = {
						name = "NAME_princess_ship"
						random_existing_design = civilian_arkship_tier_1
						effect = {
							save_event_target_as = nomadic_princess_ship
							set_ship_flag = nomadic_princess_ship
						}
					}
				}
			}
		}
		event_target:seralune_throne = {
			destroy_ambient_object = this
		}
	}

	option = {
		name = nomads.1625.a
	}

	after = {
		owner = {
			end_event_chain = nomadic_princess_chain
			remove_country_flag = hired_nomadic_princess
			set_country_flag = princess_can_die
		}
	}
}

country_event = {
	id = nomads.1630
	title = nomads.1630.name
	desc = nomads.1630.desc
	picture = GFX_evt_space_funeral
	event_chain = nomadic_princess_chain
	show_sound = event_bad_omen

	is_triggered_only = yes

	trigger = {
		from = {
			has_leader_flag = nomadic_princess
		}
		NOT = {
			has_country_flag = princess_can_die
		}
	}

	option = {
		name = nomads.1630.a
		owner = {
			end_event_chain = nomadic_princess_chain
			remove_country_flag = hired_nomadic_princess
			set_country_flag = princess_can_die
		}
	}
}

##############################
### Contract Functionality ###
##############################

# Non-nomad AI contract generation delayer
# Fired by: on_yearly_pulse_country
country_event = {
	id = nomads.3000
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_ai = yes
	}

	immediate = {
		country_event = { id = nomads.3001 days = 15 random = 340 }
	}
}

# Non-nomad AI contract issuer
# Fired by: nomads.3000
# this = country
country_event = {
	id = nomads.3001
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_ai = yes
		can_issue_contracts_trigger = yes
		any_relation = {
			is_nomadic = yes
			has_communications = prev
			NOT = {
				is_at_war_with = prev
			}
			root = { #check that ai likes at least one nomad enough
				opinion_level = {
					who = prev
					level >= neutral
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				if = {
					limit = {
						count_country = {
							count >= 15
							limit = { is_country_type = default }
						}
					}
					count_issued_contracts = 0
				}
				else_if = {
					limit = {
						count_country = {
							count >= 10
							limit = { is_country_type = default }
						}
					}
					count_issued_contracts <= 1
				}
				else = {
					count_issued_contracts <= 2
				}
			}
			random_list = {
				10 = { # Chance of nothing
					modifier = {
						factor = 3
						is_xenophobe = yes
					}
					modifier = {
						factor = 5
						is_fanatic_xenophobe = yes
					}
					modifier = {
						factor = 2
						has_civic = civic_pompous_purists
					}
					modifier = {
						factor = 4
						is_gestalt = yes
					}
					modifier = {
						factor = 0.1
						is_megacorp = yes
					}
				}
				20 = { # Harvest
					modifier = { # skip if a harvest contract is already issued/picked up
						factor = 0
						has_country_flag = harvest_contract_active
					}
					modifier = { #more likely if deficit
						factor = 5
						OR = {
							has_deficit = minerals
							has_deficit = energy
							has_deficit = alloys
							has_deficit = exotic_gases
							has_deficit = rare_crystals
							has_deficit = volatile_motes
						}
					}
					random_system_within_border = {
						limit = {
							NOT = {
								has_star_flag = issued_harvest_contract
								any_system_planet = {
									is_star = no
									has_modifier = harvested_resources_mining
								}
							}
							if = {
								limit = {
									root = {
										has_deficit = exotic_gases
									}
									any_system_planet = {
										is_star = no
										has_resource = {
											type = exotic_gases
											amount > 0
										}
									}
								}
								any_system_planet = {
									is_star = no
									has_resource = {
										type = exotic_gases
										amount > 0
									}
								}
							}
							else_if = {
								limit = {
									root = {
										has_deficit = rare_crystals
									}
									any_system_planet = {
										is_star = no
										has_resource = {
											type = rare_crystals
											amount > 0
										}
									}
								}
								any_system_planet = {
									is_star = no
									has_resource = {
										type = rare_crystals
										amount > 0
									}
								}
							}
							else_if = {
								limit = {
									root = {
										has_deficit = volatile_motes
									}
									any_system_planet = {
										is_star = no
										has_resource = {
											type = volatile_motes
											amount > 0
										}
									}
								}
								any_system_planet = {
									is_star = no
									has_resource = {
										type = volatile_motes
										amount > 0
									}
								}
							}
							else_if = {
								limit = {
									root = {
										has_deficit = minerals
									}
									any_system_planet = {
										is_star = no
										has_resource = {
											type = minerals
											amount > 0
										}
									}
								}
								any_system_planet = {
									is_star = no
									has_resource = {
										type = minerals
										amount > 0
									}
								}
							}
							else_if = {
								limit = {
									root = {
										has_deficit = energy
									}
									any_system_planet = {
										is_star = no
										has_resource = {
											type = energy
											amount > 0
										}
									}
								}
								any_system_planet = {
									is_star = no
									has_resource = {
										type = energy
										amount > 0
									}
								}
							}
							else_if = {
								limit = {
									root = {
										has_deficit = alloys
									}
									any_system_planet = {
										is_star = no
										has_resource = {
											type = alloys
											amount > 0
										}
									}
								}
								any_system_planet = {
									is_star = no
									has_resource = {
										type = alloys
										amount > 0
									}
								}
							}
						}
						save_event_target_as = contract_system
						#Resource Type picker
						random_list = {
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = minerals
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = energy
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = consumer_goods
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = alloys
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = volatile_motes
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = exotic_gases
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = rare_crystals
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = sr_living_metal
							}
							inline_script = {
								script = contracts/AIHarvestOption
								RESOURCE = nanites
							}
						}
						set_timed_star_flag = {
							flag = issued_harvest_contract
							days = 1800
						}
					}
				}
				10 = { # Espionage
					modifier = { # skip if an espionage support contract is already issued/picked up
						factor = 0
						has_country_flag = espionage_support_contract_active
					}
					modifier = { #check for valid spynets
						factor = 0
						NOT = {
							any_spynetwork = {
								NOT = { has_spynetwork_flag = issued_spy_support_contract }
							}
						}
						NOT = {
							any_envoy = {
								has_envoy_task = {
									task = spy_network
								}
							}
						}
					}
					#reduced for nice guys
					modifier = {
						factor = 0.5
						NOT = { has_tradition = tr_subterfuge_adopt }
						is_xenophile = yes
						has_civic = civic_warrior_culture #tis dishonorable
						has_origin = origin_heirs_of_the_khan
					}
					#increases for sneaky people
					modifier = {
						factor = 2.5
						OR = {
							has_civic = civic_cutthroat_politics
							has_civic = civic_shadow_council
							has_civic = civic_secret_societies
							has_civic = civic_criminal_heritage
							has_civic = civic_secret_societies_corporate
							has_civic = civic_hive_bodysnatcher
							has_civic = civic_hive_familiar_face
							has_civic = civic_machine_spybots
						}
					}
					#increased for weak empire
					modifier = {
						factor = 1.5
						any_neighbor_country = {
							is_country_type = default #don't count FE's
							relative_power = {
								who = root
								category = all
								value = overwhelming
							}
						}
					}

					random_spynetwork = {
						limit = {
							NOT = { has_spynetwork_flag = issued_spy_support_contract }
							has_spy_power > 9
							target = { is_country_type = default }
						}
						save_event_target_as = espionage_support_spynetwork
						target = { save_event_target_as = espionage_support_target }
						random_list = {
							10 = {
								modifier = { #always a chance for simple contract
									factor = 0.1
									target = {
										num_owned_planets > 3
									}
								}
								create_espionage_contract = { CONTRACT = contract_espionage_support }
							}
							10 = {
								modifier = {
									factor = 0
									target = {
										OR = {
											num_owned_planets < 3
											num_owned_planets > 7
										}
									}
								}
								create_espionage_contract = { CONTRACT = contract_espionage_support_2 }
							}
							10 = {
								modifier = {
									factor = 0
									target = {
										OR = {
											num_owned_planets < 7
											num_owned_planets > 13
										}
									}
								}
								create_espionage_contract = { CONTRACT = contract_espionage_support_3 }
							}
							10 = {
								modifier = {
									factor = 0
									target = {
										num_owned_planets <= 13
									}
								}
								create_espionage_contract = { CONTRACT = contract_espionage_support_4 }
							}
						}
					}
				}
				20 = { # Deep Scan
					modifier = { # skip if a deep scan contract is already issued/picked up
						factor = 0
						has_country_flag = deep_scan_contract_active
					}
					modifier = { #increased for materialists and explorers
						factor = 2
						OR = {
							is_materialist = yes
							has_tradition = tr_discovery_adopt
							has_civic = civic_eager_explorers
							has_civic = civic_hive_stargazers
							has_civic = civic_privatized_exploration
							has_civic = civic_machine_exploration_protocol
						}
					}
					modifier = { #increased if boxed in -won't catch all edge cases like xenophobe FE or leviathans, but good enough.
						factor = 3
						NOT = {
							any_system_within_border = {
								any_neighbor_system = {
									NOT = { exists = owner }
								}
							}
						}
					}
					modifier = { #decrease if everything that can be deep scanned has been deep scanned
						factor = 0.25
						NOT = {
							any_planet_within_border = {
								has_anomaly = no
								is_artificial = no
								is_ringworld = no
								NOT = {
									is_planet_class = pc_astral_scar
								}
								if = {
									limit = {
										habitable_planet = yes
									}
									root = {
										any_owned_leader = {
											has_leader_flag = nomadic_naturalist
										}
									}
								}
								NOT = { is_variable_set = times_deep_scanned }
							}
						}
					}
					modifier = { #blocked if no possible contractor
						factor = 0
						NOT = {
							any_relation = {
								is_nomadic = yes
								has_communications = prev
								has_deep_scan_capability = yes
							}
						}
					}
					random_system_within_border = {
						limit = {
							NOT = { has_star_flag = issued_deep_scan_contract }
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_deep_scan
								location = event_target:contract_system
							}
							prev = {
								set_timed_star_flag = {
									flag = issued_deep_scan_contract
									days = 3600
								}
							}
						}
					}
				}
				5 = { # Raid Planet
					modifier = { # skip if a raid planet contract is already issued/picked up
						factor = 0
						has_country_flag = raid_planet_contract_active
					}
					modifier = { #check for valid targets (rival or war)
						factor = 0
						num_rivals < 1
						count_war = {
							count < 1
						}
					}
					#reduced for pacifists
					modifier = {
						factor = 0.1
						is_pacifist = yes
					}
					#increased for militarists
					modifier = {
						factor = 3
						is_militarist = yes
					}
					modifier = {
						factor = 1.5
						is_fanatic_militarist = yes
					}
					#increased for weaker empires
					modifier = {
						factor = 1.5
						any_neighbor_country = {
							is_country_type = default #don't count FE's
							OR = {
								is_rival = root
								is_at_war_with = root
							}
							relative_power = {
								who = root
								category = all
								value >= superior
							}
						}
					}
					#increased for enmity empires
					modifier = {
						factor = 2
						has_tradition = tr_enmity_adopt
					}

					#War Case (takes priority over being petty)
					if = {
						limit = {
							count_war = {
								count < 0
							}
						}
						ordered_country = {
							limit = {
								is_at_war_with = root
							}
							position = 0
							order_by = fleet_power
							save_event_target_as = raid_target
							random_owned_planet = {
								save_event_target_as = contract_planet
							}
						}
						issue_contract = {
							contract = contract_raid_planet_small
							location = event_target:contract_planet
						}
					}
					#Rival Case
					else_if = {
						limit = {
							num_rivals > 0
						}
						random_rival_country = {
							limit = {
								is_country_type = default
							}
							save_event_target_as = raid_target
							random_owned_planet = {
								save_event_target_as = contract_planet
							}
						}
						issue_contract = {
							contract = contract_raid_planet_small
							location = event_target:contract_planet
						}
					}
				}
				50 = { # Megastructure Support (this supports megastructure upgrades, their base construction is handled in nomads.3025)
					modifier = { # skip if a megastructure support contract is already issued/picked up
						factor = 0
						has_country_flag = megastructure_support_contract_active
					}
					#Higher Prio than other contracts due the limited timeframe where its relevant
					modifier = {
						factor = 0
						NOT = {
							any_owned_megastructure = {
								is_upgrading = yes
							}
						}
					}
					random_system_within_border = {
						limit = {
							any_system_megastructure = {
								is_upgrading = yes
							}
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_megastructure_support
								location = event_target:contract_system
							}
						}
					}
				}
				10 = { # Diplomatic Support
					modifier = { # skip if already has an active diplomatic support contract
						factor = 0
						has_country_flag = diplomatic_support_contract_active
					}
					modifier = { # xenophiles and diplomacy bonus
						factor = 4
						OR = {
							is_xenophile = yes
							has_tradition = tr_diplomacy_adopt
						}
					}
					modifier = { # xenophobe penalty
						factor = 0.25
						is_xenophobe = yes
					}
					modifier = { #increased for weak empire
						factor = 1.5
						any_neighbor_country = {
							is_country_type = default #don't count FE's
							relative_power = {
								who = root
								category = all
								value = overwhelming
							}
						}
					}
					modifier = { #check for valid targets (not rival or war)
						factor = 0
						NOT = {
							any_playable_country = {
								is_country_type = default
								has_communications = root
								NOT = {
									is_at_war_with = root
									is_rival = root
									is_same_value = root
									is_nomadic = yes
								}
							}
						}
					}
					random_country = {
						limit = {
							is_country_type = default
							has_communications = root
							NOT = {
								is_at_war_with = root
								is_rival = root
								is_same_value = root
								is_nomadic = yes
							}
						}
						capital_scope = {
							root.owner = {
								issue_contract = {
									contract = contract_diplomatic_support
									location = prev
								}
							}
						}
					}
				}
				10 = { # Scan Debris
					modifier = { # skip if a scan debris contract is already issued/picked up
						factor = 0
						has_country_flag = scan_debris_contract_active
					}
					modifier = {
						factor = 0
						count_available_debris = { count = 0 }
					}
					random_available_debris = {
						solar_system = {
							set_star_flag = debris_contract_system@root
							save_event_target_as = debris_contract_system
						}
					}
					issue_contract = {
						contract = contract_scan_debris
						location = event_target:debris_contract_system
					}
				}
				10 = { # Clear Blockers
					modifier = { # skip if a clear blockers contract is already issued/picked up
						factor = 0
						has_country_flag = clear_blockers_contract_active
					}
					modifier = {
						factor = 0
						any_owned_planet = {
							is_colony = no
							num_uncleared_blockers = 0
						}
					}
					random_owned_planet = {
						limit = {
							is_colony = yes
							num_uncleared_blockers > 0
						}
						root = {
							issue_contract = {
								contract = contract_clear_blockers
								location = prev
							}
						}
					}
				}
				10 = { # Build Waystation
					modifier = { # skip if a build waystation contract is already issued/picked up
						factor = 0
						has_country_flag = build_waystation_contract_active
					}
					modifier = {
						factor = 0
						NOT = {
							benefits_from_waystation_network = {
								NOMAD = from
								SYSTEM_OWNER = this
							}
						}
					}
					modifier = {
						factor = 2
						is_megacorp = yes
					}
					modifier = {
						factor = 0.25
						is_xenophobe = yes
					}
					random_system_within_border = {
						limit = {
							NOT = {
								any_ship_in_system = {
									is_waystation_ship = yes
								}
							}
						}
						save_event_target_as = build_waystation_contract_system
						owner = {
							issue_contract = {
								contract = contract_build_waystation
								location = event_target:build_waystation_contract_system
							}
						}
						set_star_flag = waystation_contract_system@this
					}
				}
				10 = { # Prisoner Disposal
					modifier = { # skip if a prisoner disposal contract is already issued/picked up
						factor = 0
						has_country_flag = prisoner_disposal_contract_active
					}
					modifier = {
						factor = 0
						OR = {
							NOT = {
								any_owned_colony = {
									num_assigned_jobs = {
										job = criminal
										value > 0
									}
								}
							}
							is_gestalt = yes
						}
					}
					# increase for authoritarians
					modifier = {
						factor = 2
						is_authoritarian = yes
					}
					modifier = {
						factor = 5
						is_fanatic_authoritarian = yes
					}
					# increase for police state or gene ID or oppressive autocracy
					modifier = {
						factor = 2
						OR = {
							has_valid_civic = civic_police_state
							has_valid_civic = civic_genetic_identification
							has_valid_civic = civic_corporate_genetic_identification
						}
					}
					# decrease for experimental sentencing or civil education civic or slavers
					# decrease for xenophiles

					random_owned_colony = {
						limit = {
							num_assigned_jobs = {
								job = criminal
								value > 0
							}
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_prisoner_disposal
								location = event_target:contract_system
							}
						}
					}
				}
				10 = { # Pest Control
					modifier = { # skip if a pest control contract is already issued/picked up
						factor = 0
						has_country_flag = pest_control_contract_active
					}
					modifier = {
						factor = 0
						NOT = {
							any_system_within_border = {
								any_neighbor_system = {
									can_pest_control_system = yes
								}
							}
						}
					}
					modifier = {
						factor = 0.1
						is_xenophile = yes
					}
					random_system_within_border = {
						limit = {
							any_neighbor_system = {
								can_pest_control_system = yes
							}
						}
						random_neighbor_system = {
							limit = {
								can_pest_control_system = yes
							}
							save_event_target_as = pest_control_contract_target
						}
						if = {
							limit = {
								exists = event_target:pest_control_contract_target
							}
							owner = {
								issue_contract = {
									contract = contract_pest_control
									location = event_target:pest_control_contract_target
								}
							}
						}
					}
				}
				5 = { # Sightseeing Tour
					modifier = { # skip if a sightseeing tour contract is already issued/picked up
						factor = 0
						has_country_flag = sightseeing_tour_contract_active
					}
					modifier = {
						factor = 0
						NOT = {
							any_owned_planet = {
								is_colony = yes
								NOT = {	has_modifier = nomad_contract_sightseeing }
							}
						}
					}
					modifier = {
						factor = 1.5
						AND = {
							NOT = { is_authoritarian = yes }
							any_owned_planet = {
								happiness_planet < 0.5
							}
						}
					}
					modifier = {
						factor = 2
						OR = {
							is_egalitarian = yes
							is_xenophile = yes
						}
					}
					random_owned_planet = {
						limit = {
							is_colony = yes
							NOT = {	has_modifier = nomad_contract_sightseeing }
							solar_system = {
								NOT = { has_star_flag = sightseeing_tour_start_system@root }
							}
						}
						root = {
							if = {
								limit = { exists = prev.solar_system }
								issue_contract = {
									contract = contract_sightseeing_tour
									location = prev
								}
							}
						}
					}
				}
				10 = { # Join Wars
					modifier = { # skip if a join war contract is already issued/picked up
						factor = 0
						has_country_flag = join_war_contract_active
					}
					modifier = { #check for war
						factor = 0
						count_war = {
							count < 1
						}
					}
					modifier = { #check for war exhaustion (to limit cases of contract issued just when a war would end)
						factor = 0
						NOT = {
							any_war = {
								AND = {
									defender_war_exhaustion < 0.6
									attacker_war_exhaustion < 0.6
								}
							}
						}
					}
					modifier = { #increased if multiple wars
						factor = 3
						count_war = {
							count > 1
						}
					}
					#reduced for pacifists
					modifier = {
						factor = 0.1
						is_pacifist = yes
					}
					#increased for militarists
					modifier = {
						factor = 3
						is_militarist = yes
					}
					#increased for corpos
					modifier = {
						factor = 5
						is_megacorp = yes
					}
					#increased for weaker empires
					modifier = {
						factor = 1.5
						any_neighbor_country = {
							is_country_type = default #don't count FE's
							OR = {
								is_rival = root
								is_at_war_with = root
							}
							relative_power = {
								who = root
								category = all
								value >= superior
							}
						}
					}
					if = {
						limit = { exists = capital_scope }
						issue_contract = {
							contract = contract_join_war
							location = capital_scope
						}
					}
				}
				50 = { # Colony Startup
					modifier = { # skip if a colony startup contract is already issued/picked up
						factor = 0
						has_country_flag = colony_startup_contract_active
					}
					#Higher Prio than other contracts due the limited timeframe where its relevant
					modifier = {
						factor = 0
						NOT = {
							any_owned_planet = {
								is_colony = yes
								is_under_colonization = no
								colony_age_years < 10
								NOR = {
									is_homeworld = yes
									has_carrier_flag = colony_startup_planet@root
									has_modifier = nomad_contract_colony_support_civilian
									has_modifier = nomad_contract_colony_support_scientific
									has_modifier = nomad_contract_colony_support_military
								}
							}
						}
					}
					# reduced for xenophobes
					modifier = {
						factor = 0.1
						is_xenophobe = yes
					}
					random_system_within_border = {
						limit = {
							any_system_planet = {
								is_colony = yes
								is_under_colonization = no
								colony_age_years < 10
								NOR = {
									is_homeworld = yes
									has_carrier_flag = colony_startup_planet@root
									has_modifier = nomad_contract_colony_support_civilian
									has_modifier = nomad_contract_colony_support_scientific
									has_modifier = nomad_contract_colony_support_military
								}
							}
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_colony_startup
								location = event_target:contract_system
							}
						}
					}
				}
				10 = { # Stellar Ignition
					modifier = { # skip if a stellar ignition contract is already issued/picked up
						factor = 0
						has_country_flag = stellar_ignition_contract_active
					}
					modifier = {
						factor = 0
						NOT = {
							any_system_within_border = {
								any_system_planet = {
									contract_gas_giant_can_be_ignited = yes
								}
							}
						}
					}
					# never for environmentalists
					modifier = {
						factor = 0
						has_valid_civic = civic_environmentalist
					}
					# increased for militarists
					modifier = {
						factor = 2
						is_militarist = yes
					}
					# increased for megacorp
					modifier = {
						factor = 2
						is_megacorp = yes
					}
					# increased for planescapers
					modifier = {
						factor = 2
						is_planetscaper_empire = yes
					}

					random_system_within_border = {
						limit = {
							any_system_planet = {
								contract_gas_giant_can_be_ignited = yes
							}
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_stellar_ignition
								location = event_target:contract_system
							}
						}
					}
				}
				20 = { # Onboard Physics Research
					modifier = { # skip if an onboard physics research contract is already issued/picked up
						factor = 0
						has_country_flag = onboard_physics_research_contract_active
					}
					modifier = {
						factor = 2
						is_materialist = yes
					}
					modifier = {
						factor = 2
						is_fanatic_materialist = yes
					}
					modifier = {
						factor = 0.5
						is_spiritualist = yes
					}
					modifier = {
						factor = 2
						has_valid_civic = civic_technocracy
					}
					issue_contract = {
						contract = contract_onboard_physics_research
						location = capital_scope
					}
				}
				20 = { # Onboard Society Research
					modifier = { # skip if an onboard society research contract is already issued/picked up
						factor = 0
						has_country_flag = onboard_society_research_contract_active
					}
					modifier = {
						factor = 2
						is_materialist = yes
					}
					modifier = {
						factor = 2
						is_xenophile = yes
					}
					modifier = {
						factor = 0.5
						is_xenophobe = yes
					}
					modifier = {
						factor = 0.5
						has_valid_civic = civic_pompous_purists
					}
					issue_contract = {
						contract = contract_onboard_society_research
						location = capital_scope
					}
				}
				20 = { # Onboard Engineering Research
					modifier = { # skip if an onboard engineering research contract is already issued/picked up
						factor = 0
						has_country_flag = onboard_engineering_research_contract_active
					}
					modifier = {
						factor = 2
						is_materialist = yes
					}
					modifier = {
						factor = 2
						is_militarist = yes
					}
					modifier = {
						factor = 2
						has_valid_civic = civic_distinguished_admiralty
					}
					modifier = {
						factor = 0.5
						is_pacifist = yes
					}
					issue_contract = {
						contract = contract_onboard_engineering_research
						location = capital_scope
					}
				}
				20 = { # Onboard Minerals Resource
					modifier = { # skip if an onboard minerals resource contract is already issued/picked up
						factor = 0
						has_country_flag = onboard_minerals_resource_contract_active
					}
					modifier = {
						factor = 0
						NOT = {
							any_planet_within_border = {
								mineral_candidate = yes
								OR = {
									has_deposit = d_minerals_1
									has_deposit = d_minerals_2
									has_deposit = d_minerals_3
									has_deposit = d_minerals_4
									has_deposit = d_minerals_5
									has_deposit = d_minerals_6
									has_deposit = d_minerals_7
									has_deposit = d_minerals_8
									has_deposit = d_minerals_9
									has_deposit = d_minerals_10
								}
							}
						}
					}
					modifier = {
						factor = 2
						is_megacorp = yes
					}
					modifier = {
						factor = 2
						has_valid_civic = civic_mining_guilds
					}
					modifier = {
						factor = 0.5
						is_xenophobe = yes
					}
					random_system_within_border = {
						limit = {
							any_system_planet = {
								mineral_candidate = yes
								OR = {
									has_deposit = d_minerals_1
									has_deposit = d_minerals_2
									has_deposit = d_minerals_3
									has_deposit = d_minerals_4
									has_deposit = d_minerals_5
									has_deposit = d_minerals_6
									has_deposit = d_minerals_7
									has_deposit = d_minerals_8
									has_deposit = d_minerals_9
									has_deposit = d_minerals_10
								}
								NOT = { has_carrier_flag = contract_onboard_minerals_resource }
							}
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_onboard_minerals_resource
								location = event_target:contract_system
							}
						}
					}
				}
				20 = { # Onboard Energy Resource
					modifier = { # skip if an onboard energy resource contract is already issued/picked up
						factor = 0
						has_country_flag = onboard_energy_resource_contract_active
					}
					modifier = {
						factor = 0
						NOT = {
							any_planet_within_border = {
								energy_candidate = yes
								OR = {
									has_deposit = d_energy_1
									has_deposit = d_energy_2
									has_deposit = d_energy_3
									has_deposit = d_energy_4
									has_deposit = d_energy_5
									has_deposit = d_energy_6
									has_deposit = d_energy_7
									has_deposit = d_energy_8
									has_deposit = d_energy_9
									has_deposit = d_energy_10
								}
							}
						}
					}
					modifier = {
						factor = 2
						is_megacorp = yes
					}
					modifier = {
						factor = 2
						is_materialist = yes
					}
					modifier = {
						factor = 0.5
						is_xenophobe = yes
					}
					random_system_within_border = {
						limit = {
							any_system_planet = {
								energy_candidate = yes
								OR = {
									has_deposit = d_energy_1
									has_deposit = d_energy_2
									has_deposit = d_energy_3
									has_deposit = d_energy_4
									has_deposit = d_energy_5
									has_deposit = d_energy_6
									has_deposit = d_energy_7
									has_deposit = d_energy_8
									has_deposit = d_energy_9
									has_deposit = d_energy_10
								}
								NOT = { has_carrier_flag = contract_onboard_energy_resource }
							}
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_onboard_energy_resource
								location = event_target:contract_system
							}
						}
					}
				}
				20 = { # Onboard Food Resource
					modifier = { # skip if an onboard food resource contract is already issued/picked up
						factor = 0
						has_country_flag = onboard_food_resource_contract_active
					}
					modifier = {
						factor = 0
						NOT = {
							any_planet_within_border = {
								food_candidate = yes
								OR = {
									has_deposit = d_food_3
									has_deposit = d_food_4
									has_deposit = d_food_5
									has_deposit = d_food_6
									has_deposit = d_food_7
									has_deposit = d_food_10
								}
							}
						}
					}
					modifier = {
						factor = 2
						is_pacifist = yes
					}
					modifier = {
						factor = 2
						has_valid_civic = civic_agrarian_idyll
					}
					modifier = {
						factor = 0.5
						is_xenophobe = yes
					}
					random_system_within_border = {
						limit = {
							any_system_planet = {
								food_candidate = yes
								OR = {
									has_deposit = d_food_3
									has_deposit = d_food_4
									has_deposit = d_food_5
									has_deposit = d_food_6
									has_deposit = d_food_7
									has_deposit = d_food_10
								}
								NOT = { has_carrier_flag = contract_onboard_food_resource }
							}
						}
						save_event_target_as = contract_system
						owner = {
							issue_contract = {
								contract = contract_onboard_food_resource
								location = event_target:contract_system
							}
						}
					}
				}
			}
		}
	}
}

# Contract Completed, increment variable
country_event = {
	id = nomads.3002
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				is_variable_set = contract_completed
			}
			change_variable = {
				which = contract_completed
				value = 1
			}
		}
        else = {
          set_variable = {
            which = contract_completed
            value = 1
          }
       }
	   if = { # Nomadic Caregiver Trait Effect
			limit = {
				any_owned_leader = {
					has_trait = leader_trait_nomadic_caregiver
				}
			}
			country_event = {
				id = nomads.1535
				days = 15
			}
		}
	}
}

# Forever Cruise Contract Completed, increment variable
country_event = {
	id = nomads.3003
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				is_variable_set = cruise_contract_completed
			}
			change_variable = {
				which = cruise_contract_completed
				value = 1
			}
		}
        else = {
          set_variable = {
            which = cruise_contract_completed
            value = 1
          }
       }
	}
}

# Non-nomad AI contract generation delayer
# Fired by: on_yearly_pulse_country
country_event = {
	id = nomads.3005
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_ai = yes
	}

	immediate = {
		country_event = { id = nomads.3010 days = 15 random = 340 }
	}
}

# Nomads issue contracts for themselves. - DISABLED FOR NOW
# Fired by: on_yearly_pulse_country
# this = country
country_event = {
	id = nomads.3010
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		always = no
		is_nomadic = yes
		NOT = { has_origin = origin_forever_cruise }
	}

	immediate = {
		save_event_target_as = nomad_country
		if = {
			limit = {
				num_communications = 0
				count_available_contracts = 0
			}

			random_owned_fleet = {
				limit = {
					is_arkship_fleet = yes
				}
				solar_system = {
					save_event_target_as = arkship_system
				}
			}

			event_target:nomad_country = {
				random_list = {
					1 = {
						inline_script = {
							script = events/nomads_find_resource_internal_event
							RESOURCE = minerals
						}
					}
					1 = {
						inline_script = {
							script = events/nomads_find_resource_internal_event
							RESOURCE = energy
						}
					}
					1 = {
						inline_script = {
							script = events/nomads_find_resource_internal_event
							RESOURCE = alloys
						}
					}
					1 = {
						inline_script = {
							script = events/nomads_scientific_observation_internal_event
							RESOURCE = physics_research
						}
					}
					1 = {
						inline_script = {
							script = events/nomads_scientific_observation_internal_event
							RESOURCE = engineering_research
						}
					}
				}
			}
		}
	}
}

planet_event = { # Raid Contract bombardement damage tracker - from on_planet_bombarded
	id = nomads.3015
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from = {
			is_nomadic = yes
			has_event_chain = contract_raid_planet_chain
		}
		owner = { has_country_flag = raid_target@from }
	}

	immediate = {
		set_carrier_flag = raid_contract_planet@from
		export_trigger_value_to_variable = {
			trigger = planet_devastation
			variable = bomba
		}
		if = {
			limit = {
				NOT = { is_variable_set = bombatrack }
			}
			export_trigger_value_to_variable = {
				trigger = planet_devastation
				variable = bombatrack
			}
		}
		if = {
			limit = {
				check_variable_arithmetic = {
					which = bomba
					subtract = bombatrack
					value >= 1
				}
			}
			change_variable = {
				which = bombatrack
				value = 1
			}
			from = {
				add_mission_counter = {
					mission = contract_raid_planet_small
					counter = contract_progress_counter
					amount = 1
				}
			}
		}
	}
}

# Megastructure Support Contract
# Root = Country
# From = System
# FromFrom = Fleet

country_event = { #AIs auto issue a contract when first building a new Megastructure (with a 5 year cooldown )
	id = nomads.3025
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		is_ai = yes
		can_issue_contracts_trigger = yes
		any_relation = {
			is_nomadic = yes
			has_communications = prev
			NOT = {
				is_at_war_with = prev
			}
		}
		NOT = { has_country_flag = issued_megastructure_contract }
	}

	immediate = {
		owner = {
			issue_contract = {
				contract = contract_megastructure_support
				location = from
			}
			set_timed_country_flag = { flag = issued_megastructure_contract days = 1800 }
		}
	}
}

# Scan Debris Contract Complete

country_event = {
	id = nomads.3035
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
		has_event_chain = contract_scan_debris_chain
	}

	immediate = {
		add_mission_counter = {
			mission = contract_scan_debris
			counter = contract_scan_debris_counter
			amount = 1
		}
		# Mark that the debris was scanned so the contract's abort_trigger does not
		# cancel it on the same day: scanning consumes the debris, and the contract
		# is only completed on the following day when the counter is checked.
		set_timed_country_flag = { flag = scan_debris_contract_scanned days = 10 }
	}
}

# Diplomatic Support

fleet_event = { #pick up delegates event and destination poi creation
	id = nomads.3036
	title = "nomads.3036.name"
	desc = { #has_communications
		trigger = {
			event_target:pickup_planet = {
				owner = {
					has_communications = root.owner
				}
			}
		}
		text = "nomads.3036.desc"
	}
	desc = {#no communications
		trigger = {
			event_target:pickup_planet = {
				owner = {
					NOT = { has_communications = root.owner }
				}
			}
		 }
		text = "nomads.3036.desc.nocomm"
	}
	picture = GFX_evt_tradedeal
	show_sound = event_hand_shake

	is_triggered_only = yes

	trigger = {
		owner = {
			has_event_chain = contract_diplomatic_support_chain
		}
		solar_system = {
			any_system_planet = {
				has_carrier_flag = diplomatic_support_pickup_planet@root.owner
			}
		}
		is_arkship_fleet = yes
	}

	immediate = {
		solar_system = {
			random_system_planet = {
				limit = {
					has_carrier_flag = diplomatic_support_pickup_planet@root.owner
				}
				save_event_target_as = pickup_planet
				if = {
					limit = {
						owner = {
							NOT = {
								has_communications = root.owner
							}
						}
					}
					owner = { establish_communications = root.owner }
				}
			}
		}
		set_fleet_flag = carrying_delegates
		random_galaxy_planet = {
			limit = {
				has_carrier_flag = diplomatic_support_dropoff_planet@root.owner
			}
			save_event_target_as = dropoff_planet
		}

		owner = {
			remove_point_of_interest = diplomatic_support_pickup_poi@this
			remove_point_of_interest = diplomatic_support_destination_poi@this
			create_point_of_interest = {
				id = diplomatic_support_dropoff_poi@this
				name = "diplomatic_support_dropoff_poi_name"
				desc = diplomatic_support_dropoff_poi_desc
				event_chain = contract_diplomatic_support_chain
				location = event_target:dropoff_planet
			}
			if = {
				limit = { is_ai = yes }
				abort_special_project = {
					type = CONTRACT_DIPLOMATIC_SUPPORT_TRAVEL_PROJECT
					location = event_target:pickup_planet.solar_system
				}
				enable_special_project = {
					name = "CONTRACT_DIPLOMATIC_SUPPORT_TRAVEL_PROJECT"
					location = event_target:dropoff_planet.solar_system
					owner = this
				}
			}
		}
	}

	option = {
		name = nomads.3036.a
		custom_tooltip = contract_dropoff_planet_tt
	}
}

fleet_event = { #Delegate destination check - From on_fleet_enter_system
	id = nomads.3045
	title = "nomads.3045.name"
	desc = "nomads.3045.desc"
	picture = GFX_evt_diplomatic_visit
	show_sound = event_hand_shake

	is_triggered_only = yes

	trigger = {
		owner = {
			has_event_chain = contract_diplomatic_support_chain
		}
		solar_system = {
			any_system_planet = {
				has_carrier_flag = diplomatic_support_dropoff_planet@root.owner
			}
		}
		is_arkship_fleet = yes
		has_fleet_flag = carrying_delegates
	}

	immediate = {
		random_galaxy_planet = {
			limit = {
				has_carrier_flag = diplomatic_support_pickup_planet@root.owner
			}
			remove_carrier_flag = diplomatic_support_pickup_planet@root.owner
			owner = {
				save_event_target_as = diplomatic_support_issuer
			}
		}
		solar_system = {
			random_system_planet = {
				limit = { has_carrier_flag = diplomatic_support_dropoff_planet@root.owner }
				remove_carrier_flag = diplomatic_support_dropoff_planet@root.owner
				owner = {
					save_event_target_as = diplomatic_support_target
				}
			}
		}
		remove_fleet_flag = carrying_delegates
		random_list = {
			1 = { #meh success
				event_target:diplomatic_support_issuer = {
					add_opinion_modifier = {
						modifier = opinion_diplomacy_contract_small
						who = event_target:diplomatic_support_target
					}
					add_trust = {
						amount = 5
						who = event_target:diplomatic_support_target
					}
				}
			}
			3 = { #sucess
				event_target:diplomatic_support_issuer = {
					add_opinion_modifier = {
						modifier = opinion_diplomacy_contract
						who = event_target:diplomatic_support_target
					}
					add_trust = {
						amount = 10
						who = event_target:diplomatic_support_target
					}
				}
			}
			1 = { #great success
				event_target:diplomatic_support_issuer = {
					add_opinion_modifier = {
						modifier = opinion_diplomacy_contract_large
						who = event_target:diplomatic_support_target
					}
					add_trust = {
						amount = 15
						who = event_target:diplomatic_support_target
					}
				}
			}
		}
		event_target:diplomatic_support_target = {
			add_opinion_modifier = {
				modifier = opinion_diplomacy_contract
				who = event_target:diplomatic_support_issuer
			}
		}
	}

	option = {
		name = EXCELLENT
		owner = {
			add_mission_counter = {
				mission = contract_diplomatic_support
				counter = contract_diplomatic_support_counter
				amount = 1
			}
		}
	}
}

# Prisoner Disposal
fleet_event = {
	id = nomads.3050
	title = "nomads.3050.name"
	desc = "nomads.3050.desc"
	picture = GFX_evt_experimental_testing
	show_sound = event_criminal_activity

	is_triggered_only = yes

	trigger = {
		owner = {
			has_event_chain = contract_prisoner_disposal_chain
		}
		solar_system = {
			any_system_planet = {
				has_carrier_flag = prisoner_disposal_pickup_planet@root.owner
			}
		}
		is_arkship_fleet = yes
	}

	immediate = {
		solar_system = {
			random_system_planet = {
				limit = {
					has_carrier_flag = prisoner_disposal_pickup_planet@root.owner
				}
				save_event_target_as = pickup_planet
			}
		}
		random_owned_ship = {
			colony = {
				save_event_target_as = arkship
			}
		}
	}

	option = {	#well drop them off later
		name = nomads.3050.a
		#flag fleet for scripted action
		custom_tooltip = transporting_prisoners_tt
		hidden_effect = {
			set_fleet_flag = transporting_prisoners
		}
		#add crime modifier
		event_target:arkship = {
			add_modifier = { #add crime mod to planet
				modifier = nomad_criminals_on_board
				years = 10
			}
		}
		#Can't choose this if awe already have another contract action
		trigger = {
			has_contract_scripted_action = no
		}

		ai_chance = {
			factor = 0
		}
	}
	#other options finish the contract there
	option = {#keep em ourselves
		name = nomads.3050.b
		trigger = {
			owner = { is_slaver = no }
		}
		owner = {
			add_mission_counter = {
				mission = contract_prisoner_disposal
				counter = contract_prisoner_disposal_counter
				amount = 1
			}
		}
		#add crime modifier
		event_target:arkship = {
			add_modifier = { #add crime mod to planet
				modifier = nomad_criminals_on_board
				years = 10
			}
		}
	}
	option = { #keep em ourselves (slaver)
		name = nomads.3050.c
		trigger = {
			event_target:pickup_planet.owner = { is_slaver = yes }
		}
		#no crime modifier for this one, you just get the pops
		owner = {
			add_mission_counter = {
				mission = contract_prisoner_disposal
				counter = contract_prisoner_disposal_counter
				amount = 1
			}
		}
	}
	option = { #keep and free them
		name = nomads.3050.d
		owner = {
			add_mission_counter = {
				mission = contract_prisoner_disposal
				counter = contract_prisoner_disposal_counter
				amount = 1
			}
		}
		random_list = {
			1 = { #add reduced crime modifier
				modifier = {
					factor = 2
					event_target:pickup_planet.owner = {
						OR = {
							is_xenophile = yes #They're actually not that bad
							is_authoritarian = yes #They were actually unjustly jailed
						}
					}
				}
				modifier = {
					factor = 10
					root.owner = {
						has_valid_civic = civic_civil_education
					}
				}
				event_target:arkship = {
					add_modifier = { #add crime mod to planet
						modifier = nomad_reformed_criminals_on_board
						years = 5
					}
				}
			}
			1 = { #add worse crime modifier
				modifier = {
					factor = 3
					event_target:pickup_planet.owner = {
						OR = {
							has_valid_civic = civic_void_reavers
							has_valid_civic = civic_criminal_heritage
							has_valid_civic = civic_hive_bodysnatcher
							has_valid_civic = civic_barbaric_despoilers
							has_ascension_perk = ap_nihilistic_acquisition
							is_xenophobe = yes
						}
					}
				}
				event_target:arkship = {
					add_modifier = {
						modifier = nomad_criminals_loose_on_board
						years = 5
					}
				}
			}
		}
	}

	after = {
		hidden_effect = {
			event_target:pickup_planet = {
				add_modifier = { #add anti crime mod to planet
					modifier = nomad_criminal_disposal
					years = 10
				}
				#transfer pop if applicable
				random_owned_pop_group = {
					weights = {
						base = 1
						modifier = {
							factor = 1000
							pop_group_size >= 300
						}
					}
					save_event_target_as = source_pop_group
					if = {
						limit = {
							root.event_target:arkship = {
								has_modifier = nomad_reformed_criminals_on_board
							}
						}
						root.event_target:arkship = {
							create_pop_group = {
								pop_group = event_target:source_pop_group
								size = 0
								effect = {
									transfer_pop_amount = {
										source = event_target:source_pop_group
										target = this
										amount = 300
									}
									save_event_target_as = resettled_pop_group
								}
							}
						}
					}
					else = {
						root.event_target:arkship = {
							create_pop_group = {
								pop_group = event_target:source_pop_group
								size = 0
								category = criminal
								effect = {
									transfer_pop_amount = {
										source = event_target:source_pop_group
										target = this
										amount = 300
									}
									save_event_target_as = resettled_pop_group
									set_pop_group_flag = criminal_pops
								}
							}
						}
					}
				}
				remove_carrier_flag = prisoner_disposal_pickup_planet@root.owner
			}
		}
	}
}

#3051 dropped off prisoners
planet_event = {
	id = nomads.3051
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		from.owner = {
			random_owned_contract = {
				limit = { is_mission_type = contract_prisoner_disposal }
				issuer = {
					save_event_target_as = prisoner_disposal_issuer
				}
			}
		}
		from = {
			remove_fleet_flag = transporting_prisoners
			fleet_event = { id = nomads.3052 }
		}
	}
}

fleet_event = {
	id = nomads.3052
	title = "nomads.3052.name"
	desc = "nomads.3052.desc"
	picture = GFX_evt_experimental_testing
	show_sound = event_criminal_activity

	is_triggered_only = yes

	option = { #Good riddance
		name = 03_RECALL_EMBASSY_HOSTILE
		random_owned_ship = {
			colony = {
				remove_modifier = nomad_criminals_on_board
				custom_tooltip = prisoner_tranfer_tt
				hidden_effect = {
					random_owned_pop_group = {
						limit = {
							NOT = {
								is_species = owner_main_species
								is_subspecies = owner_main_species
							}
						}
						weights = {
							base = 1
							modifier = {
								factor = 1000
								has_pop_group_flag = criminal_pops
							}
						}
						resettle_pop_group = {
							POP_GROUP = this
							PLANET = from
							AMOUNT = 300
						}
					}
				}
			}
		}
		hidden_effect = {
			from = {
				if = {
					limit = {
						owner = {
							is_gestalt = no
						}
						NOT = {
							has_modifier = penal_colony
							has_modifier = slave_colony
							is_planet_class = pc_cosmogenesis_world
						}
					}
					add_modifier = {
						modifier = nomad_criminals_dropped_off
						years = 10
					}
				}
			}
		}
	}

	after = {
		hidden_effect = {
			fleet_event = { id = nomads.3053 days = 1 }
		}
	}
}

fleet_event = {
	id = nomads.3053
	title = "nomads.3053.name"
	desc = {	# No problems
		text = "nomads.3053.desc"
		trigger = {
			NOT = {
				has_fleet_flag = detected_by_issuer
				has_fleet_flag = detected
			}
		}
	}
	desc = {	# issuer problems
		text = "nomads.3053.desc.issuer.problem"
		trigger = {
			has_fleet_flag = detected_by_issuer
		}
	}
	desc = {	# Yes problems
		text = "nomads.3053.desc.problem"
		trigger = {
			has_fleet_flag = detected
		}
	}
	picture = GFX_evt_experimental_testing
	show_sound = event_criminal_activity

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				fromfrom.owner = {
					is_gestalt = no
				}
				fromfrom = {
					NOT = {
						has_modifier = penal_colony
						has_modifier = slave_colony
						is_planet_class = pc_cosmogenesis_world
					}
				}
			}
			random_list = {
				1 = { #detected
					modifier = {
						factor = 0.1
						owner = {
							relative_encryption_decryption = {
								target = event_target:prisoner_disposal_issuer
								value > 1
							}
						}
					}
					if = { #by issuer?
						limit = {
							fromfrom.owner? = {
								is_same_value = event_target:prisoner_disposal_issuer
							}
						}
						set_fleet_flag = detected_by_issuer

					}
					else = { #not by issuer
						set_fleet_flag = detected
					}

				}
				1 = { #sneaky
					modifier = {
						factor = 0.1
						owner = {
							relative_encryption_decryption = {
								target = event_target:prisoner_disposal_issuer
								value < 1
							}
						}
					}
					#we just don't add a flag
				}
			}
		}
	}

	option = { #detected
		name = UNFORTUNATE
		trigger = {
			has_fleet_flag = detected
		}
		from.owner = {
			add_opinion_modifier = {
				modifier = opinion_dropped_off_prisoners
				who = root.owner
			}
		}
	}

	option = { #issuer detected
		name = UNFORTUNATE
		trigger = {
			has_fleet_flag = detected_by_issuer
		}
		#big opinion loss and contract cancel
		fromfrom.owner = {
			add_opinion_modifier = {
				modifier = opinion_dropped_off_our_prisoners
				who = root.owner
			}
		}
		owner = {
			random_owned_contract = {
				limit = {
					is_mission_type = contract_prisoner_disposal
				}
				cancel_contract = {
					refund = no
				}
			}
		}
	}

	option = {
		name = EXCELLENT
		trigger = {
			NOT = {
				has_fleet_flag = detected_by_issuer
				has_fleet_flag = detected
			}
		}
	}

	after = {
		#if not detected by issuer flag
		if = {
			limit = {
				NOT = { has_fleet_flag = detected_by_issuer }
			}
			owner = {
				add_mission_counter = {
					mission = contract_prisoner_disposal
					counter = contract_prisoner_disposal_counter
					amount = 1
				}
			}
		}
		#flag cleanup
		remove_fleet_flag = detected_by_issuer
		remove_fleet_flag = detected
	}
}

# Build Waystation Contract Complete
# Fired when a Waystation is constructed
# Fired by: on_waystation_built
# this = galactic object/solar system
# from = waystation owner
# fromfrom = waystation starbase
system_event = {
	id = nomads.3054
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from = {
			is_nomadic = yes
			has_event_chain = contract_build_waystation_chain
		}
		has_star_flag = waystation_contract_system@root
	}

	immediate = {
		owner = {
			set_country_flag = build_waystation_complete
		}
		from = {
			# Gate the contract's abort trigger: marks "success is in flight" so the
			# any_fleet_in_system { is_waystation_ship } check on the next daily tick
			# doesn't race ahead of the counter-based success check.
			set_country_flag = build_waystation_contract_succeeded
			add_mission_counter = {
				mission = contract_build_waystation
				counter = contract_build_waystation_counter
				amount = 1
			}
		}
	}
}

# Pest Control Contract Complete
# Fired by: on_space_battle_won
# This = owner of fleet 1 (winner)
# From = owner of fleet 2 (loser)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = nomads.3055
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
		has_event_chain = contract_pest_control_chain
		fromfromfrom = {
			has_fleet_flag = pest_contract_target@root
		}
	}

	immediate = {
		add_mission_counter = {
			mission = contract_pest_control
			counter = contract_pest_control_counter
			amount = 1
		}
		set_country_flag = successful_pest_control@fromfrom.solar_system
	}
}

# Sightseeing Tour - Check if Arkship has entered the system to pick up tourists
# Fired by: on_entering_system_fleet
# A fleet has entered a system
# Scope = Fleet
# From = System
fleet_event = {
	id = nomads.3060
	title = "nomads.3060.name"
	desc = "nomads.3060.desc"
	picture = GFX_evt_obelisk_tourists
	show_sound = event_announcement
	location = root.solar_system
	event_chain = contract_sightseeing_tour_chain
	is_triggered_only = yes

	trigger = {
		owner = {
			is_nomadic = yes
			has_event_chain = contract_sightseeing_tour_chain
		}
		is_arkship_fleet = yes
		from = {
			has_star_flag = sightseeing_tour_start_system@root.owner
			NOT = { has_star_flag = sightseeing_tour_tourists_picked_upp@root.owner }
		}
	}

	immediate = {
		from = {
			set_star_flag = sightseeing_tour_tourists_picked_upp@root.owner
		}
		set_fleet_flag = sightseeing_tour_arkship
		random_system = {
			limit = {
				distance = {
					source = prev.from
					use_bypasses = no
					min_jumps = 3
					max_jumps = 5
				}
				NOR = {
					has_star_flag = guardian
					has_star_flag = hostile_system
				}
			}
			weights = {
				base = 1
				modifier = {
					factor = 10
					OR = {
						is_star_class = sc_black_hole
						is_star_class = sc_neutron_star
						is_star_class = sc_pulsar
					}
				}
			}
			set_star_flag = sightseeing_tour_destination_system@root.owner
			save_event_target_as = sightseeing_tour_destination_system
		}
		owner = {
			remove_point_of_interest = sightseeing_tour_start_poi
			create_point_of_interest = {
				id = sightseeing_tour_destination_poi
				name = "sightseeing_tour_destination_poi_name"
				desc = "sightseeing_tour_destination_poi_desc"
				event_chain = contract_sightseeing_tour_chain
				location = event_target:sightseeing_tour_destination_system
			}
			if = {
				limit = { is_ai = yes }
				abort_special_project = {
					type = CONTRACT_SIGHTSEEING_TOUR_TRAVEL_PROJECT
					location = from
				}
				enable_special_project = {
					name = "CONTRACT_SIGHTSEEING_TOUR_TRAVEL_PROJECT"
					location = event_target:sightseeing_tour_destination_system
					owner = this
				}
			}
			create_message = {
				type = MESSAGE_CONTRACT_SIGHTSEEING_TOUR_START
				localization = MESSAGE_CONTRACT_SIGHTSEEING_TOUR_START_LOCATION_DESC
				days = 30
				variable = {
					type = name
					localization = SYSTEM
					scope = event_target:sightseeing_tour_destination_system
				}
			}
		}
	}

	option = {
		name = nomads.3060.a
		custom_tooltip = nomads.3060.a.tt
	}
}

# Sightseeing Tour - Check if Arkship has entered the system for tourists to sightsee
# Fired by: on_entering_system_fleet
# A fleet has entered a system
# Scope = Fleet
# From = System
fleet_event = {
	id = nomads.3065
	title = "nomads.3065.name"
	desc = "nomads.3065.desc"
	picture = GFX_evt_space_funeral
	show_sound = evn_ove_obelisk_tourists
	location = root.solar_system
	event_chain = contract_sightseeing_tour_chain
	is_triggered_only = yes

	trigger = {
		owner = {
			is_nomadic = yes
			has_event_chain = contract_sightseeing_tour_chain
		}
		from = {
			has_star_flag = sightseeing_tour_destination_system@root.owner
		}
		has_fleet_flag = sightseeing_tour_arkship
	}

	immediate = {
		from = {
			remove_star_flag = sightseeing_tour_destination_system@root.owner
		}
		random_system = {
			limit = {
				has_star_flag = sightseeing_tour_start_system@root.owner
			}
			remove_star_flag = sightseeing_tour_start_system@root.owner
			set_star_flag = sightseeing_tour_end_system@root.owner
			save_event_target_as = sightseeing_tour_end_system
		}
		owner = {
			remove_point_of_interest = sightseeing_tour_destination_poi
			create_point_of_interest = {
				id = sightseeing_tour_end_poi
				name = "sightseeing_tour_end_poi_name"
				desc = "sightseeing_tour_end_poi_desc"
				event_chain = contract_sightseeing_tour_chain
				location = event_target:sightseeing_tour_end_system
			}
			if = {
				limit = { is_ai = yes }
				abort_special_project = {
					type = CONTRACT_SIGHTSEEING_TOUR_TRAVEL_PROJECT
					location = from
				}
				enable_special_project = {
					name = "CONTRACT_SIGHTSEEING_TOUR_TRAVEL_PROJECT"
					location = event_target:sightseeing_tour_end_system
					owner = this
				}
			}
			create_message = {
				type = MESSAGE_CONTRACT_SIGHTSEEING_TOUR_END
				localization = MESSAGE_CONTRACT_SIGHTSEEING_TOUR_END_LOCATION_DESC
				days = 30
				variable = {
					type = name
					localization = SYSTEM
					scope = event_target:sightseeing_tour_end_system
				}
			}
		}
	}

	option = {
		name = nomads.3065.a
		custom_tooltip = nomads.3060.a.tt
	}
}

# Sightseeing Tour - Check if Arkship has entered the system to drop off tourists
# Fired by: on_entering_system_fleet
# A fleet has entered a system
# Scope = Fleet
# From = System
fleet_event = {
	id = nomads.3070
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner = {
			is_nomadic = yes
			has_event_chain = contract_sightseeing_tour_chain
		}
		from = {
			has_star_flag = sightseeing_tour_end_system@root.owner
		}
		has_fleet_flag = sightseeing_tour_arkship
	}

	immediate = {
		remove_fleet_flag = sightseeing_tour_arkship
		from = {
			remove_star_flag = sightseeing_tour_end_system@root.owner
			remove_star_flag = sightseeing_tour_tourists_picked_upp@root.owner
		}
		owner = {
			remove_point_of_interest = sightseeing_tour_destination_poi
			add_mission_counter = {
				mission = contract_sightseeing_tour
				counter = contract_progress_counter
				amount = 1
			}
		}
	}
}

country_event = { # War with nomad mercs is won. Remove event chain and hired guns modifier
	id = nomads.3075
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = {
			has_war_flag = nomad_mercs
		}
	}

	immediate = {
		fromfrom = {
			every_war_participant = {
				limit = { # we check that they have the flag that connects them to this war and that they aren't involved in another war with the contract issuer
					has_country_flag = war@fromfrom
					NOT = {
						any_war = {
							NOT = { is_same_value = fromfrom }
							any_war_participant = {
								has_country_flag = hired_nomad_merc@prevprev
							}
						}
					}
				}
				end_event_chain = contract_join_war_chain
				#remove hired_gun modifier
				remove_modifier = hired_guns_modifier
				#clean up issuer flag
				every_country = {
					limit = {
						has_country_flag = hired_nomad_merc@prev
					}
					remove_country_flag = hired_nomad_merc@prev
					remove_country_flag = join_war_contract_active
				}
			}
			every_war_participant = {#Clean up the flag related to the war that just ended
				remove_country_flag = war@fromfrom
			}
		}
	}
}

country_event = { # War with nomad mercs is Status Quo. Remove event chain and hired guns modifier
	id = nomads.3076
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfromfromfrom = {
			has_war_flag = nomad_mercs
		}
	}

	immediate = {
		fromfromfromfrom = {
			every_war_participant = {
				limit = { # we check that they have the flag that connects them to this war and that they aren't involved in another war with the contract issuer
					has_country_flag = war@fromfromfromfrom
					NOT = {
						any_war = {
							NOT = { is_same_value = fromfromfromfrom }
							any_war_participant = {
								has_country_flag = hired_nomad_merc@prevprev
							}
						}
					}
				}
				end_event_chain = contract_join_war_chain
				hired_guns_modifier_cleanup = yes
				#clean up issuer flag
				every_country = {
					limit = {
						has_country_flag = hired_nomad_merc@prev
					}
					remove_country_flag = hired_nomad_merc@prev
					remove_country_flag = join_war_contract_active
				}
			}
			every_war_participant = {#Clean up the flag related to the war that just ended
				remove_country_flag = war@fromfromfromfrom
			}
		}
	}
}

country_event = { # Hired Guns safety cleanup
	id = nomads.3080
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_valid_civic = civic_hired_guns
			has_valid_civic = civic_corporate_hired_guns
		}
		has_modifier = hired_guns_modifier
	}

	immediate = {
		hired_guns_modifier_cleanup = yes
	}
}

# Hired guns upkeep reduction handler
fleet_event = {
	id = nomads.3085
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		controller = {
			OR = {
				has_valid_civic = civic_hired_guns
				has_valid_civic = civic_corporate_hired_guns
			}
		}
		from.controller? = {
			is_hostile = root.controller
		}
	}

	immediate = {
		every_owned_ship = {
			limit = {
				NOT = { has_modifier = hg_ship_upkeep_bombing }
			}
			add_modifier = {
				modifier = hg_ship_upkeep_bombing
				days = -1
			}
		}
	}
}

fleet_event = {
	id = nomads.3090
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		controller = {
			OR = {
				has_valid_civic = civic_hired_guns
				has_valid_civic = civic_corporate_hired_guns
			}
		}
		OR = {
			from.controller? = {
				is_hostile = root.controller
			}
			any_owned_ship = {
				has_modifier = hg_ship_upkeep_bombing
			}
		}
	}

	immediate = {
		every_owned_ship = {
			limit = {
				has_modifier = hg_ship_upkeep_bombing
			}
			remove_modifier = hg_ship_upkeep_bombing
		}
	}
}

country_event = {
	id = nomads.3095
	title = "nomads.3075.name"
	desc = "nomads.3075.desc"
	picture = GFX_evt_nomad_contracts
	show_sound = event_criminal_activity

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = contract_intro }
		is_nomadic = no
		is_country_type = default
		from = {
			is_nomadic = yes
			can_accept_contracts_trigger = yes
		}
	}

	immediate = { set_country_flag = contract_intro }

	option = {
		trigger = {
			is_xenophile = yes
		}
		name = MARVELOUS
	}
	option = {
		trigger = {
			is_xenophile = no
			is_xenophobe = no
		}
		name = INTERESTING
	}
	option = {
		trigger = {
			is_xenophobe = yes
		}
		name = SUSPICIOUS
	}

	after = {
		custom_tooltip = nomads.3075.tt
	}
}

# Cancel unaccepted Join Wars contracts when the issuer's last war ends
# on_war_won: Root = Winner Warleader, From = Loser Warleader, FromFrom = War
country_event = {
	id = nomads.3100
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = {
			any_war_participant = {
				is_at_war = no
				any_issued_mission = {
					is_mission_type = contract_join_war
				}
			}
		}
	}

	immediate = {
		fromfrom = {
			every_war_participant = {
				if = {
					limit = { is_at_war = no }
					every_issued_mission = {
						limit = { is_mission_type = contract_join_war }
						cancel_contract = { refund = yes }
					}
				}
			}
		}
	}
}

# Cancel unaccepted Join Wars contracts when the issuer's last war ends (white peace)
# on_status_quo: Root = Actor, From = Recipient, FromFrom = Main Attacker, FromFromFrom = Main Defender, FromFromFromFrom = War
country_event = {
	id = nomads.3101
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfromfromfrom = {
			any_war_participant = {
				is_at_war = no
				any_issued_mission = {
					is_mission_type = contract_join_war
				}
			}
		}
	}

	immediate = {
		fromfromfromfrom = {
			every_war_participant = {
				if = {
					limit = { is_at_war = no }
					every_issued_mission = {
						limit = { is_mission_type = contract_join_war }
						cancel_contract = { refund = yes }
					}
				}
			}
		}
	}
}

# Total wars only end in status quo, so the raid war goal's ark-seizing surrender path never runs.
# on_status_quo: Root = Actor, From = Recipient, FromFrom = Main Attacker, FromFromFrom = Main Defender, FromFromFromFrom = War
country_event = {
	id = nomads.3105
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfromfromfrom = {
			is_total_war = yes
			any_war_participant = {
				is_nomadic = yes
				# Fire on a defeated arkship OR ownership of any waystation.
				OR = {
					any_owned_fleet = {
						is_arkship_fleet = yes
						any_owned_ship = {
							is_arkship_ship = yes
							is_disabled = yes
						}
					}
					any_owned_fleet = {
						any_owned_ship = {
							is_waystation_ship = yes
						}
					}
				}
			}
		}
	}

	immediate = {
		# Resolve the on_status_quo scope chain (see header) once into readable named targets.
		fromfrom = { save_event_target_as = total_war_main_attacker }
		fromfromfrom = { save_event_target_as = total_war_main_defender }
		fromfromfromfrom = { save_event_target_as = total_war }
		event_target:total_war = {
			# Defeated nomads forfeit their disabled arkships (with colony + pops) and waystations to the opposing war leader.
			if = {
				limit = { event_target:total_war_main_attacker = { is_nomadic = yes } }
				every_defender = {
					limit = { is_nomadic = yes }
					save_event_target_as = nomad_raid_defender
					event_target:total_war_main_attacker = {
						nomad_raid_seize_ark_effect = yes
						nomad_raid_seize_disabled_waystations_effect = yes
					}
				}
			}
			if = {
				limit = { event_target:total_war_main_defender = { is_nomadic = yes } }
				every_attacker = {
					limit = { is_nomadic = yes }
					save_event_target_as = nomad_raid_defender
					event_target:total_war_main_defender = {
						nomad_raid_seize_ark_effect = yes
						nomad_raid_seize_disabled_waystations_effect = yes
					}
				}
			}
			if = {
				limit = {
					event_target:total_war_main_attacker = { is_nomadic = no }
					OR = {
						using_war_goal = { type = wg_wipe_them_out     owner = event_target:total_war_main_attacker }
						using_war_goal = { type = wg_cleansing        owner = event_target:total_war_main_attacker }
						using_war_goal = { type = wg_absorption       owner = event_target:total_war_main_attacker }
						using_war_goal = { type = wg_assimilation     owner = event_target:total_war_main_attacker }
						using_war_goal = { type = wg_machine_uprising owner = event_target:total_war_main_attacker }
					}
				}
				every_defender = {
					limit = { is_nomadic = yes }
					annihilate_captured_arks_effect = yes
				}
			}
		}
	}
}

# Deferred by annihilate_captured_arks_effect: destroys the arkships flagged during status-quo resolution once the war has fully ended.
# this = the defeated nomad
country_event = {
	id = nomads.3110
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_owned_fleet = {
			limit = { has_fleet_flag = arkship_pending_annihilation }
			destroy_fleet = this
		}
	}
}

##############################
### Arkships Functionality ###
##############################

# Fired by on_arkship_deep_scan
planet_event = {
	id = nomads.4000
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		from = {
			random_owned_ship = {
				save_event_target_as = arkship
			}
		}
		# If anything is not surveyed, survey it.
		if = {
			limit = {
				is_surveyed = {
					who = from.controller
					status = no
				}
			}
			set_surveyed = {
				surveyed = yes
				surveyor = from.controller
			}
			save_event_target_as = target_planet
			event_target:arkship = {
				fire_on_action = {
					on_action = on_survey_planet
					scopes = {
						from = event_target:target_planet
					}
				}
			}
		}
		else = {
			# Already surveyed: on_survey_planet won't fire, so call the Sensor Calibration
			# checklist event directly so progress still ticks for a fully-explored galaxy.
			save_event_target_as = target_planet
			event_target:arkship = {
				ship_event = {
					id = nomads.52
					scopes = {
						from = event_target:target_planet
					}
				}
			}
		}
		# Chance to find hidden stuff...
		if = {
			limit = {
				habitable_planet = no
				is_artificial = no
				NOR = {
					is_planet_class = pc_ringworld_habitable_damaged
					is_planet_class = pc_ringworld_tech_damaged
					is_planet_class = pc_ringworld_tech
					is_planet_class = pc_ringworld_seam
					is_planet_class = pc_ringworld_seam_damaged
					is_planet_class = pc_astral_scar
				}
				# check that the random list won't come up empty
				OR = {
					energy_candidate = yes
					mineral_candidate = yes
					physics_candidate = yes
					society_candidate = yes
					engineering_candidate = yes
					alloy_candidate = yes
					exotic_gas_candidate = yes
					rare_crystal_candidate = yes
					volatile_motes_candidate = yes
				}
			}
			arkship_deep_scan_effect = yes
		}
		if = {
			limit = {
				habitable_planet_not_urban = yes
				event_target:arkship.leader? = {
					has_leader_flag = nomadic_naturalist
				}
			}
			life_in_motion_effect = yes
		}
		from.controller = {
			create_message = {
				type = TOAST_DEEP_SCAN_COMPLETE
				localization = TOAST_DEEP_SCAN_COMPLETE_DESC
				days = @toast_message_days
				variable = {
					type = name
					localization = ARKSHIP
					scope = event_target:arkship
				}
				variable = {
					type = name
					localization = PLANET
					scope = root
				}
			}
		}
	}
}

# Fired by on_harvest_resources
carrier_event = {
	id = nomads.4100
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		from = {
			every_owned_ship = {
				save_event_target_as = arkship
			}
			if = {
				limit = {
					is_civilian = yes
				}
				root = {
					arkship_harvest_resources_effect = {
						DURATION = @logistic_harvest_resources_duration
						TARGET = event_target:arkship
						EFFECT = add_resource_to_local_stockpile
					}
				}
			}
			else = {
				root = {
					arkship_harvest_resources_effect = {
						DURATION = @arkship_harvest_resources_duration
						TARGET = event_target:arkship.controller
						EFFECT = add_resource
					}
				}
			}
		}
		# Decrease opinion of system owner
		if = {
			limit = {
				NOT = {
					exempt_from_harvest_resources_opinion_reduction_trigger = {
						NOMAD = event_target:arkship.controller
					}
				}
				has_deposit = yes
			}
			space_owner? = {
				add_opinion_modifier = {
					modifier = opinion_arkship_harvested_resources
					who = event_target:arkship.controller
				}
			}
		}
	}
}

# Track Resource Harvesting Checklist progress
# Fired by on_harvest_resources
carrier_event = {
	id = nomads.4101
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from = {
			is_arkship_fleet = yes
		}
		from.controller = {
			is_nomadic = yes
			has_country_flag = checklist_harvest_resources
		}
	}
	immediate = {
		# Gas Giant or Toxic World → Exotic Gases
		if = {
			limit = {
				OR = {
					is_planet_class = pc_gas_giant
					is_planet_class = pc_toxic
				}
				from.controller = {
					NOT = { has_country_flag = checklist_harvested_exotic_gases }
				}
			}
			from.controller = {
				set_country_flag = checklist_harvested_exotic_gases
				add_mission_counter = {
					mission = NOMAD_CHECKLIST_HARVEST_RESOURCES
					counter = EXOTIC_GASES_HARVESTED
					amount = 1
				}
			}
		}
		# Frozen World → Rare Crystals
		if = {
			limit = {
				is_planet_class = pc_frozen
				from.controller = {
					NOT = { has_country_flag = checklist_harvested_rare_crystals }
				}
			}
			from.controller = {
				set_country_flag = checklist_harvested_rare_crystals
				add_mission_counter = {
					mission = NOMAD_CHECKLIST_HARVEST_RESOURCES
					counter = RARE_CRYSTALS_HARVESTED
					amount = 1
				}
			}
		}
		# Molten World → Volatile Motes
		if = {
			limit = {
				is_planet_class = pc_molten
				from.controller = {
					NOT = { has_country_flag = checklist_harvested_volatile_motes }
				}
			}
			from.controller = {
				set_country_flag = checklist_harvested_volatile_motes
				add_mission_counter = {
					mission = NOMAD_CHECKLIST_HARVEST_RESOURCES
					counter = VOLATILE_MOTES_HARVESTED
					amount = 1
				}
			}
		}
	}
}

# Notification for a regular empire when a nomadic fleet harvests one of their deposits within their borders
# this = harvested celestial body (planet)
# from = harvesting fleet (arkship or logistic ship)
carrier_event = {
	id = nomads.4105
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from? = {
			owner = { is_nomadic = yes }
		}
		space_owner? = {
			is_country_type = default
			is_nomadic = no
			NOT = { has_country_flag = nomad_harvest_notified_recently }
		}
		NOT = {
			exempt_from_harvest_resources_opinion_reduction_trigger = {
				NOMAD = from.owner
			}
		}
	}

	immediate = {
		solar_system = { save_event_target_as = nomad_harvested_system }
		from = {
			owner = { save_event_target_as = nomad_harvesting_deposits_country }
		}
		space_owner = {
			# setting flag to decrease spam
			set_timed_country_flag = {
				flag = nomad_harvest_notified_recently
				years = 3
			}
			if = {
				# the event itself
				limit = {
					NOT = { has_country_flag = nomad_harvesting_deposits_seen }
				}
				set_country_flag = nomad_harvesting_deposits_seen
				country_event = { id = nomads.4110 }
			}
			else = {
				# if happened before, toast
				create_message = {
					type = MESSAGE_NOMAD_HARVESTING_DEPOSITS
					localization = MESSAGE_NOMAD_HARVESTING_DEPOSITS_DESC
					days = @toast_important_message_days
					target = event_target:nomad_harvested_system
					variable = {
						type = name
						localization = COUNTRY
						scope = event_target:nomad_harvesting_deposits_country
					}
					variable = {
						type = name
						localization = SYSTEM
						scope = event_target:nomad_harvested_system
					}
				}
			}
		}
	}
}

country_event = {
	id = nomads.4110
	title = nomads.4110.name
	desc = nomads.4110.desc
	picture = GFX_evt_arkship_in_orbit
	location = event_target:nomad_harvested_system

	is_triggered_only = yes

	option = {
		name = UNDERSTOOD
	}
}

# Fired by on_arkship_strip_mine
carrier_event = {
	id = nomads.4200
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		from = {
			every_owned_ship = {
				save_event_target_as = arkship
			}
		}
		arkship_strip_mine_effect = yes
		# Decrease opinion of system owner
		if = {
			limit = {
				NOT = {
					exempt_from_harvest_resources_opinion_reduction_trigger = {
						NOMAD = event_target:arkship.controller
					}
				}
				has_deposit = yes
			}
			space_owner? = {
				add_opinion_modifier = {
					modifier = opinion_arkship_strip_mine
					who = event_target:arkship.controller
				}
			}
		}
	}
}

# Fired by on_arkship_subspace_image
system_event = {
	id = nomads.4300
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		set_star_flag = subspace_image_completed
		from = {
			random_owned_ship = {
				save_event_target_as = arkship
			}
		}
		# Sensor Calibration: stars already surveyed before this subspace image won't
		# fire on_survey_planet below, so call nomads.52 directly for those so progress
		# still ticks in a fully-explored galaxy. Unsurveyed stars are handled by the
		# on_survey_planet chain fired in the loop below.
		every_system_planet = {
			limit = {
				is_star = yes
				is_surveyed = {
					who = from.controller
					status = yes
				}
			}
			save_event_target_as = target_star
			event_target:arkship = {
				ship_event = {
					id = nomads.52
					scopes = {
						from = event_target:target_star
					}
				}
			}
		}
		# If anything is not surveyed, survey it.
		if = {
			limit = {
				any_system_planet = {
					is_surveyed = {
						who = from.controller
						status = no
					}
				}
			}
			every_system_planet = {
			limit = {
				is_surveyed = {
					who = from.controller
					status = no
				}
			}
			set_surveyed = {
				surveyed = yes
				surveyor = from.controller
			}
			save_event_target_as = target_planet
			event_target:arkship = {
				fire_on_action = {
					on_action = on_survey_planet
					scopes = {
						from = event_target:target_planet
					}
				}
			}
		}
		}
		# Find hidden stuff...
		every_system_planet = {
			limit = {
				habitable_planet = no
				is_artificial = no
				NOR = {
					is_planet_class = pc_ringworld_habitable_damaged
					is_planet_class = pc_ringworld_tech_damaged
					is_planet_class = pc_ringworld_tech
					is_planet_class = pc_ringworld_seam
					is_planet_class = pc_ringworld_seam_damaged
					is_planet_class = pc_astral_scar
				}
				# check that the random list won't come up empty
				OR = {
					energy_candidate = yes
					mineral_candidate = yes
					physics_candidate = yes
					society_candidate = yes
					engineering_candidate = yes
					alloy_candidate = yes
					exotic_gas_candidate = yes
					rare_crystal_candidate = yes
					volatile_motes_candidate = yes
				}
			}
			arkship_subspace_image_planet_effect = yes
		}
		every_system_planet = {
			limit = {
				habitable_planet_not_urban = yes
				event_target:arkship.leader? = {
					has_leader_flag = nomadic_naturalist
				}
			}
			life_in_motion_effect = yes
		}
		arkship_subspace_image_system_effect = yes

		from.controller = {
			create_message = {
				type = TOAST_SUBSPACE_IMAGE_COMPLETE
				localization = TOAST_SUBSPACE_IMAGE_COMPLETE_DESC
				days = @toast_message_days
				variable = {
					type = name
					localization = ARKSHIP
					scope = event_target:arkship
				}
				variable = {
					type = name
					localization = SYSTEM
					scope = root
				}
			}
		}
	}
}

# Fired by on_arkship_reprocess_planet
planet_event = {
	id = nomads.4400
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		from = {
			every_owned_ship = {
				save_event_target_as = arkship
			}
		}
		arkship_planetary_reprocessor_effect = yes
		# Decrease opinion of system owner
		if = {
			limit = {
				NOT = {
					exempt_from_harvest_resources_opinion_reduction_trigger = {
						NOMAD = event_target:arkship.controller
					}
				}
			}
			space_owner? = {
				add_opinion_modifier = {
					modifier = opinion_arkship_reprocess_planet
					who = event_target:arkship.controller
				}
			}
		}
	}
}

# Fired by on_arkship_stellar_ignition
# THIS = planet
# FROM = triggering fleet
carrier_event = {
	id = nomads.4500
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				is_star = yes
			}
			arkship_stellar_ignition_effect = yes
		}
		else = {
			arkship_protostar_effect = yes
		}
		apply_stellar_ignition_habitability_penalties = yes
		from = {
			random_owned_ship = {
				save_event_target_as = arkship
			}
		}
		from.controller = {
			create_message = {
				type = TOAST_STELLAR_IGNITION_COMPLETE
				localization = TOAST_STELLAR_IGNITION_COMPLETE_DESC
				days = @toast_message_days
				variable = {
					type = name
					localization = ARKSHIP
					scope = event_target:arkship
				}
				variable = {
					type = name
					localization = PLANET
					scope = root
				}
			}
			if = {
				limit = {
					OR = {
						is_active_resolution = "resolution_ecology_environmental_control_board"
						is_active_resolution = "resolution_ecology_paradise_initiative"
					}
				}
				set_timed_country_flag = { flag = resolution_breached_terraformed days = 3600 }
			}
			save_event_target_as = nomad_country
			if = {
				limit = {
					has_event_chain = contract_stellar_ignition_chain
					root = {
						has_planet_flag = stellar_ignition_planet@event_target:nomad_country
					}
				}
				add_mission_counter = {
					mission = contract_stellar_ignition
					counter = contract_progress_counter
					amount = 1
				}
				set_country_flag = successful_stellar_ignition@root.solar_system
			}
		}
	}
}

# Fired by on_arkship_exodus_jump
# THIS = System jumping to
# FROM = Fleet
system_event = {
	id = nomads.4600
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		FROM = {
			random_owned_ship = {
				save_event_target_as = arkship
			}
			solar_system = {
				arkship_exodus_jump_system_effects = yes
			}
		}
	}
}

## Fired by on_fleet_arkship_exodus_jump
# THIS = Fleet
# FROM = System jumping to
# FROMFROM = System jumped from
fleet_event = {
	id = nomads.4650
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		random_owned_ship = {
			save_event_target_as = arkship
		}
		fromfrom = {
			every_fleet_in_system = {
				limit = {
					is_mobile = yes
					controller = {
						is_same_value = root.controller
					}
				}
				remove_auto_move_target = yes
				set_location = {
					target = event_target:arkship
					distance = 20
					angle = random
					direction = in_system
				}
			}
		}
		controller = {
			create_message = {
				type = TOAST_EXODUS_JUMP_COMPLETE
				localization = TOAST_EXODUS_JUMP_COMPLETE_DESC
				days = @toast_message_days
				variable = {
					type = name
					localization = ARKSHIP
					scope = event_target:arkship
				}
				variable = {
					type = name
					localization = SYSTEM
					scope = from
				}
			}
		}
		FROM = {
			arkship_exodus_jump_system_effects = yes
		}
	}
}

# THIS = planet
# FROM = triggering fleet
# Fired by on_arkship_settle
planet_event = {
	id = nomads.4700
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_timed_carrier_flag = {
			flag = nomad_recently_settled_flag
			days = 30
		}
		save_event_target_as = settled_planet
		solar_system = {
			# Block rival outposts and pre-FTL emergence while the Settle dialog is open.
			set_timed_star_flag = {
				flag = nomad_settling_in_progress
				days = 300
			}
			save_event_target_as = settled_system
		}
		from = {
			random_owned_ship = {
				limit = {
					is_colony = yes
				}
				save_event_target_as = settling_arkship
			}
		}
		from.owner = {
			save_event_target_as = player_country
			country_event = {
				id = nomads.4705
			}
		}
	}
}

country_event = {
	id = nomads.4705
	title = nomads.4705.name

	desc = {	# Regular empire
		text = nomads.4705.desc.regular
		trigger = {
			NOR = {
				is_hive_empire = yes
				is_machine_empire = yes
				has_origin = origin_forever_cruise
			}
		}
	}

	desc = { 	# Machine Intelligence
		text = nomads.4705.desc.machine
		trigger = {
			is_machine_empire = yes
			NOT = { has_origin = origin_forever_cruise }
		}
	}

	desc = { 	# Hive Mind
		text = nomads.4705.desc.hive
		trigger = {
			is_hive_empire = yes
			NOT = { has_origin = origin_forever_cruise }
		}
	}

	desc = {	# Regular empire Forever Cruise
		text = nomads.1010.regular.desc
		trigger = {
			NOR = {
				is_hive_empire = yes
				is_machine_empire = yes
			}
			has_origin = origin_forever_cruise
		}
	}

	desc = {	# Hive Mind Forever Cruise
		text = nomads.1010.hive.desc
		trigger = {
			is_hive_empire = yes
			has_origin = origin_forever_cruise
		}
	}

	desc = {	# Machine Intelligence Forever Cruise
		text = nomads.1010.machine.desc
		trigger = {
			is_machine_empire = yes
			has_origin = origin_forever_cruise
		}
	}

	inline_script = {
		script = events/biogenesis_event_art
		OWNER = owner
		REGULAR_PICTURE = GFX_evt_big_landing_ship
		BIOSHIP_PICTURE = GFX_evt_bioship_landing
	}

	location = from
	is_triggered_only = yes

	# We only have one Arkship, and thus we Settle.
	option = {
		exclusive_trigger = {
			count_owned_ship = {
				count = 1
				limit = { is_arkship_ship = yes }
			}
		}
		name = nomads.4700.a
		custom_tooltip = nomads.4700.settle.tt
		arkship_settle_planet_effect = {
			SETTLING_COUNTRY = event_target:player_country
			SETTLING_ARKSHIP = event_target:settling_arkship
			SETTLED_PLANET = event_target:settled_planet
			SETTLED_SYSTEM = event_target:settled_system
		}
		convert_waystations_into_outposts_effect = {
			SETTLING_COUNTRY = event_target:player_country
		}
		convert_logistic_ships_into_construction_ships_effects = yes
		custom_tooltip = convert_waystations_into_outposts_tt
		custom_tooltip = convert_logistics_to_construction_tt
	}

	# We have many Arkships, we Settle and scuttle them.
	option = {
		name = nomads.4700.b
		custom_tooltip = nomads.4700.settle.tt
		arkship_settle_planet_effect = {
			SETTLING_COUNTRY = event_target:player_country
			SETTLING_ARKSHIP = event_target:settling_arkship
			SETTLED_PLANET = event_target:settled_planet
			SETTLED_SYSTEM = event_target:settled_system
		}
		convert_waystations_into_outposts_effect = {
			SETTLING_COUNTRY = event_target:player_country
		}
		settlement_scuttle_arkships_effect = {
			SETTLING_COUNTRY = event_target:player_country
			SETTLED_PLANET = event_target:settled_planet
			SETTLING_ARKSHIP = event_target:settling_arkship
		}
		convert_logistic_ships_into_construction_ships_effects = yes
		custom_tooltip = settlement_scuttle_arkships_effect_tt
		custom_tooltip = convert_waystations_into_outposts_tt
		custom_tooltip = convert_logistics_to_construction_tt
	}

	# We have many Arkships, we stay Nomadic and release a Settled Subject.
	option = {
		trigger = {		# You need to be able to release a subject to have this option
			is_homicidal = no
			NOT = {
				has_origin = origin_toxic_knights
			}
			OR = {
				is_gestalt = no
				has_origin = origin_progenitor_hive
			}
		}
		name = nomads.4700.c
		custom_tooltip = nomads.4700.settle_subject.tt
		create_settlement_subject_country_effect = {
			NOMAD_COUNTRY = event_target:player_country
		}
		tooltip = {
			event_target:settled_planet = {
				add_modifier = {
					modifier = arkship_parts
				}
			}
		}
		if = {
			limit = {
				exists = event_target:subject_country
			}
			arkship_settle_planet_effect = {
				SETTLING_COUNTRY = event_target:subject_country
				SETTLING_ARKSHIP = event_target:settling_arkship
				SETTLED_PLANET = event_target:settled_planet
				SETTLED_SYSTEM = event_target:settled_system
			}
			hidden_effect = {
				give_planet_and_system_to_rebel = {
					PLANET = event_target:settled_planet
					REBEL = event_target:subject_country
					OLD_OWNER = event_target:player_country
				}
			}
			event_target:subject_country? = {
				clean_up_settlement_subject_country_effect = {
					OVERLORD = event_target:player_country
				}
				set_origin_effect = { SOURCE_COUNTRY = event_target:player_country }
				if = {
					limit = {
						has_origin = origin_default
					}
					set_origin = origin_nomadic_settled
				}
			}
			create_outposts_in_waystation_systems_effect = {
				NOMAD_COUNTRY = event_target:player_country
				SETTLING_COUNTRY = event_target:subject_country
				SETTLED_SYSTEM = event_target:settled_system
				RANGE = @arkship_subject_settle_outpost_range
			}
		}
		custom_tooltip = create_outposts_in_waystation_systems_for_subject_tt
		# We keep our logistic ships as we stay Nomadic
	}

	# We have many Arkships, we Settle and release a Nomadic Subject.
	option = {
		trigger = {		# You need to be able to release a subject to have this option
			is_homicidal = no
			OR = {
				is_gestalt = no
				has_origin = origin_progenitor_hive
			}
		}
		name = nomads.4700.d
		custom_tooltip = nomads.4700.settle.tt
		create_settlement_subject_country_effect = {
			NOMAD_COUNTRY = event_target:player_country
		}
		arkship_settle_planet_effect = {
			SETTLING_COUNTRY = event_target:player_country
			SETTLING_ARKSHIP = event_target:settling_arkship
			SETTLED_PLANET = event_target:settled_planet
			SETTLED_SYSTEM = event_target:settled_system
		}
		if = {
			limit = {
				exists = event_target:subject_country
			}
			nomad_give_arkships_to_subject_effect = {
				NOMAD_COUNTRY = event_target:subject_country
				SETTLING_ARKSHIP = event_target:settling_arkship
			}
			event_target:subject_country? = {
				set_country_as_nomad_effect = { IS_NOMAD = yes }
				# Start the Operational Reserves situation (and Star Journal chain) the same way game start and embarking do.
				country_event = {
					id = nomads.20
				}
				set_faction_properties = {
					needs_border_access = no
				}
				set_resource_converter = nomadic_operational_reserves
				clean_up_settlement_subject_country_effect = {
					OVERLORD = event_target:player_country
				}
				set_origin_effect = { SOURCE_COUNTRY = event_target:player_country }
				if = {
					limit = {
						has_origin = origin_default
					}
					set_origin = origin_nomadic_subject
				}
			}
			create_outposts_in_waystation_systems_effect = {
				NOMAD_COUNTRY = event_target:player_country
				SETTLING_COUNTRY = event_target:player_country
				SETTLED_SYSTEM = event_target:settled_system
				RANGE = @arkship_subject_settle_outpost_range
			}
			nomad_give_waystations_to_subject_effect = {
				NOMAD_COUNTRY = event_target:subject_country
				SETTLING_COUNTRY = event_target:player_country
			}
			nomad_give_logistic_ships_to_subject_effect = {
				NOMAD_COUNTRY = event_target:subject_country
				SETTLING_COUNTRY = event_target:player_country
			}
		}
		custom_tooltip = create_outposts_in_waystation_systems_for_overlord_tt
		custom_tooltip = make_subject_nomadic_tt
	}

	after = {
		hidden_effect = {
			if = {
				limit = { has_origin = origin_forever_cruise }
				remove_forever_cruise_citizenships = yes
				set_country_flag = forever_cruise_over
				random_situation = {
					limit = { is_situation_type = situation_passenger_satisfaction }
					destroy_situation = this
				}
				save_event_target_as = this_country
				country_event = { id = timeline.115 }
			}
		}
	}
}

country_event = {
	id = nomads.4710
	title = nomads.4710.name

	desc = {	# Regular empire (Doomsday escape)
		text = nomads.4710.desc.regular.doomsday
		trigger = {
			from = { has_carrier_flag = doomed_planet_origin }
			NOR = {
				is_hive_empire = yes
				is_machine_empire = yes
				has_origin = origin_forever_cruise
			}
		}
	}

	desc = { 	# Machine Intelligence (Doomsday escape)
		text = nomads.4710.desc.machine.doomsday
		trigger = {
			from = { has_carrier_flag = doomed_planet_origin }
			is_machine_empire = yes
		}
	}

	desc = { 	# Hive Mind (Doomsday escape)
		text = nomads.4710.desc.hive.doomsday
		trigger = {
			from = { has_carrier_flag = doomed_planet_origin }
			is_hive_empire = yes
		}
	}

	desc = {	# Regular empire
		text = nomads.4710.desc.regular
		trigger = {
			NOT = { from = { has_carrier_flag = doomed_planet_origin } }
			NOR = {
				is_hive_empire = yes
				is_machine_empire = yes
				has_origin = origin_forever_cruise
			}
		}
	}

	desc = { 	# Machine Intelligence
		text = nomads.4710.desc.machine
		trigger = {
			NOT = { from = { has_carrier_flag = doomed_planet_origin } }
			is_machine_empire = yes
		}
	}

	desc = { 	# Hive Mind
		text = nomads.4710.desc.hive
		trigger = {
			NOT = { from = { has_carrier_flag = doomed_planet_origin } }
			is_hive_empire = yes
		}
	}

	picture = GFX_evt_arkship_in_orbit
	location = from
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				from = { has_carrier_flag = doomed_planet_origin }
			}
			set_country_flag = embarking_from_doomsday
			from = {
				remove_modifier = doomsday_1
				remove_modifier = doomsday_2
				remove_modifier = doomsday_3
				remove_modifier = doomsday_4
				remove_modifier = doomsday_5
				remove_modifier = doomsday_arkship_preparation
			}
			country_event = {
				id = origin.95
				days = 180
				random = 90
				scopes = { from = from }
			}
		}
	}

	# We are embarking as military.
	option = {
		name = nomads.4710.a.military
		custom_tooltip = nomads.4710.embark.tt
		hidden_effect = {
			create_embarking_arkship = {
				NOMAD_COUNTRY = event_target:player_country
				ARKSHIP_TYPE = military
				EMBARKED_PLANET = from
			}
			embark_planet_effect = {
				EMBARKING_PLANET = from
				NEW_ARKSHIP = event_target:embarking_arkship
			}
			give_embarking_empire_logistic_ship = {
				EMBARKING_PLANET = from
			}
		}
		if = {
			limit = {
				count_owned_colony = {
					count > 1
				}
			}
			hidden_effect = {
				create_embarking_subject_country_effect = {
					PLAYER_COUNTRY = event_target:player_country
				}
				last_created_country? = {
					save_event_target_as = subject_country
				}
				convert_starbases_into_waystation_effect = {
					PLAYER_COUNTRY = event_target:player_country
					EMBARKING_COUNTRY = event_target:player_country
				}
				every_owned_colony = {
					limit = {
						NOT = { is_planet_class = pc_ark }
					}
					set_owner = last_created_country
				}
				every_owned_fleet = {
					limit = {
						OR = {
							is_ship_size = constructor
							is_ship_size = engineer_vessel
							is_ship_class = shipclass_colonizer
						}
					}
					set_owner = last_created_country
				}
				every_owned_megastructure = {
					limit = { is_megastructure_incompatible_with_nomads = yes }
					set_owner = last_created_country
				}
				last_created_country? = {
					clean_up_settlement_subject_country_effect = {
						OVERLORD = event_target:player_country
					}
					set_origin_effect = { SOURCE_COUNTRY = event_target:player_country }
				}
			}
			custom_tooltip = embark_leaves_behind_subject
		}
		else = {
			hidden_effect = {
				convert_starbases_into_waystation_effect = {
					PLAYER_COUNTRY = event_target:player_country
					EMBARKING_COUNTRY = event_target:player_country
				}
				every_owned_megastructure = {
					limit = { is_megastructure_incompatible_with_nomads = yes }
					remove_megastructure = this
				}
				every_owned_fleet = {
					limit = {
						is_ship_class = shipclass_colonizer
					}
					destroy_fleet = this
				}
			}
		}
		custom_tooltip = create_waystations_in_systems_for_nomad_tt
	}

	# We are embarking as sciences.
	option = {
		name = nomads.4710.a.science
		custom_tooltip = nomads.4710.embark.tt
		hidden_effect = {
			create_embarking_arkship = {
				NOMAD_COUNTRY = event_target:player_country
				ARKSHIP_TYPE = science
				EMBARKED_PLANET = from
			}
			embark_planet_effect = {
				EMBARKING_PLANET = from
				NEW_ARKSHIP = event_target:embarking_arkship
			}
			give_embarking_empire_logistic_ship = {
				EMBARKING_PLANET = from
			}
		}
		if = {
			limit = {
				count_owned_colony = {
					count > 1
				}
			}
			hidden_effect = {
				create_embarking_subject_country_effect = {
					PLAYER_COUNTRY = event_target:player_country
				}
				last_created_country? = {
					save_event_target_as = subject_country
				}
				convert_starbases_into_waystation_effect = {
					PLAYER_COUNTRY = event_target:player_country
					EMBARKING_COUNTRY = event_target:player_country
				}
				every_owned_colony = {
					limit = {
						NOT = { is_planet_class = pc_ark }
					}
					set_owner = last_created_country
				}
				every_owned_fleet = {
					limit = {
						OR = {
							is_ship_size = constructor
							is_ship_size = engineer_vessel
							is_ship_class = shipclass_colonizer
						}
					}
					set_owner = last_created_country
				}
				every_owned_megastructure = {
					limit = { is_megastructure_incompatible_with_nomads = yes }
					set_owner = last_created_country
				}
				last_created_country? = {
					clean_up_settlement_subject_country_effect = {
						OVERLORD = event_target:player_country
					}
					set_origin_effect = { SOURCE_COUNTRY = event_target:player_country }
				}
			}
			custom_tooltip = embark_leaves_behind_subject
		}
		else = {
			hidden_effect = {
				convert_starbases_into_waystation_effect = {
					PLAYER_COUNTRY = event_target:player_country
					EMBARKING_COUNTRY = event_target:player_country
				}
				every_owned_megastructure = {
					limit = { is_megastructure_incompatible_with_nomads = yes }
					remove_megastructure = this
				}
				every_owned_fleet = {
					limit = {
						is_ship_class = shipclass_colonizer
					}
					destroy_fleet = this
				}
			}
		}
		custom_tooltip = create_waystations_in_systems_for_nomad_tt
	}

	# We are embarking as civilians.
	option = {
		name = nomads.4710.a.civilian
		custom_tooltip = nomads.4710.embark.tt
		hidden_effect = {
			create_embarking_arkship = {
				NOMAD_COUNTRY = event_target:player_country
				ARKSHIP_TYPE = civilian
				EMBARKED_PLANET = from
			}
			embark_planet_effect = {
				EMBARKING_PLANET = from
				NEW_ARKSHIP = event_target:embarking_arkship
			}
			give_embarking_empire_logistic_ship = {
				EMBARKING_PLANET = from
			}
		}
		if = {
			limit = {
				count_owned_colony = {
					count > 1
				}
			}
			hidden_effect = {
				create_embarking_subject_country_effect = {
					PLAYER_COUNTRY = event_target:player_country
				}
				last_created_country? = {
					save_event_target_as = subject_country
				}
				convert_starbases_into_waystation_effect = {
					PLAYER_COUNTRY = event_target:player_country
					EMBARKING_COUNTRY = event_target:player_country
				}
				every_owned_colony = {
					limit = {
						NOT = { is_planet_class = pc_ark }
					}
					set_owner = last_created_country
				}
				every_owned_fleet = {
					limit = {
						OR = {
							is_ship_size = constructor
							is_ship_size = engineer_vessel
							is_ship_class = shipclass_colonizer
						}
					}
					set_owner = last_created_country
				}
				every_owned_megastructure = {
					limit = { is_megastructure_incompatible_with_nomads = yes }
					set_owner = last_created_country
				}
				last_created_country? = {
					clean_up_settlement_subject_country_effect = {
						OVERLORD = event_target:player_country
					}
					set_origin_effect = { SOURCE_COUNTRY = event_target:player_country }
				}
			}
			custom_tooltip = embark_leaves_behind_subject
		}
		else = {
			hidden_effect = {
				convert_starbases_into_waystation_effect = {
					PLAYER_COUNTRY = event_target:player_country
					EMBARKING_COUNTRY = event_target:player_country
				}
				every_owned_megastructure = {
					limit = { is_megastructure_incompatible_with_nomads = yes }
					remove_megastructure = this
				}
				every_owned_fleet = {
					limit = {
						is_ship_class = shipclass_colonizer
					}
					destroy_fleet = this
				}
			}
		}
		custom_tooltip = create_waystations_in_systems_for_nomad_tt
	}

	# Our vassal is embarking.
	option = {
		trigger = {
			count_owned_colony = {
				count > 1
			}
			OR = {
				is_gestalt = no
				has_origin = origin_progenitor_hive
			}
		}
		name = nomads.4710.b
		hidden_effect = {
			create_embarking_subject_country_effect = {
				PLAYER_COUNTRY = event_target:player_country
			}
			create_embarking_arkship = {
				NOMAD_COUNTRY = last_created_country
				ARKSHIP_TYPE = civilian
				EMBARKED_PLANET = from
			}
			from = {
				set_owner = last_created_country
				leader? = {
					unassign_leader = this
				}
			}

			embark_planet_effect = {
				EMBARKING_PLANET = from
				NEW_ARKSHIP = event_target:embarking_arkship
			}
			last_created_country? = {
				clean_up_settlement_subject_country_effect = {
					OVERLORD = event_target:player_country
				}
				set_origin_effect = { SOURCE_COUNTRY = event_target:player_country }
				give_embarking_empire_logistic_ship = { EMBARKING_PLANET = from }
			}
			last_created_country? = {
				save_event_target_as = subject_country
			}
			convert_starbases_into_waystation_effect = {
				PLAYER_COUNTRY = event_target:player_country
				EMBARKING_COUNTRY = event_target:subject_country
			}
		}
		custom_tooltip = embark_create_subject
		custom_tooltip = create_waystations_in_systems_for_nomad_tt
	}

	after = {
		hidden_effect = {
			event_target:embarking_arkship = {
				set_name = {
					key = NAME_Embarked_Arkship
					variable_string = "[From.GetName]"
				}
				fleet = {
					set_name = {
						key = NAME_Embarked_Arkship
						variable_string = "[From.GetName]"
					}
				}
			}
		}
		if = {
			limit = {
				has_country_flag = embarking_from_doomsday
			}
			remove_country_flag = embarking_from_doomsday
			if = {
				limit = { has_ethic = ethic_gestalt_consciousness }
				capital_scope = {
					add_modifier = {
						modifier = doomsday_first_arkship_gestalt
					}
				}
			}
			else = {
				capital_scope = {
					add_modifier = {
						modifier = doomsday_first_arkship
					}
				}
			}
		}
	}
}

# Scope = Ship
# From = System
# FromFrom = Country
ship_event = { # Nomad Exploration Rewards
	id = nomads.4720
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owner = {
			is_nomadic = yes
			has_ascension_perk = ap_wanderlust
			NOT = {
				has_country_flag = nomad_exploration_complete
			}
		}
		is_arkship_ship = yes
		from = {
			NOT = { has_star_flag = visited_by_nomad@root.owner }
			#Flags Check
			OR = {
				has_star_flag = ancient_wonders_system
				has_star_flag = antique_threat_system
				has_star_flag = enclave
				has_star_flag = galactic_landmark_system
				has_star_flag = marauder_system
				has_star_flag = natural_spectacle_system
				has_star_flag = precursor_system
				has_star_flag = space_ecology_system
			}
		}
	}

	immediate = {
		from = {
			set_star_flag = visited_by_nomad@root.owner
		}
		if = {
			limit = {
				from = { has_star_flag = curator_guidance_target@root.owner }
			}
			from = { remove_star_flag = curator_guidance_target@root.owner }
			owner = { remove_point_of_interest = curator_guidance_poi@from }
		}
		if = { #failsafe check for event chain
			limit = {
				NOT = {
					owner = { has_event_chain = nomad_exploration_chain }
				}
			}
			owner = {
				begin_event_chain = {
					event_chain = nomad_exploration_chain
					target = this
				}
			}
		}
		#increment the counters and update the modifiers,
		if = {
			limit = {
				from = { has_star_flag = ancient_wonders_system }
			}
			owner = {
				change_variable = {
					which = ancient_wonders_system_seen
					value = 1
				}
				add_modifier = {
					modifier = ancient_wonders_modifier
					mult = value:ancient_wonders_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:ancient_wonders_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = ancient_wonders_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_ANCIENT_WONDERS
						counter = ancient_wonders_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_WONDERS_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		else_if = {
			limit = {
				from = { has_star_flag = antique_threat_system }
			}
			owner = {
				change_variable = {
					which = antique_threat_system_seen
					value = 1
				}
				add_modifier = {
					modifier = antique_threat_modifier
					mult = value:antique_threat_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:antique_threat_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = antique_threat_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_ANTIQUE_THREAT
						counter = antique_threat_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_THREAT_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		else_if = {
			limit = {
				from = { has_star_flag = enclave }
			}
			owner = {
				change_variable = {
					which = enclave_system_seen
					value = 1
				}
				add_modifier = {
					modifier = enclave_modifier
					mult = value:enclave_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:enclave_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = enclave_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_ENCLAVE
						counter = enclave_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_ENCLAVE_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		else_if = {
			limit = {
				from = { has_star_flag = galactic_landmark_system }
			}
			owner = {
				change_variable = {
					which = galactic_landmark_system_seen
					value = 1
				}
				add_modifier = {
					modifier = galactic_landmark_modifier
					mult = value:galactic_landmark_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:galactic_landmark_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = galactic_landmark_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_GALACTIC_LANDMARK
						counter = galactic_landmark_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_LANDMARK_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		else_if = {
			limit = {
				from = { has_star_flag = marauder_system }
			}
			owner = {
				change_variable = {
					which = marauder_system_seen
					value = 1
				}
				add_modifier = {
					modifier = marauder_modifier
					mult = value:marauder_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:marauder_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = marauder_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_MARAUDER
						counter = marauder_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_MARAUDERS_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		else_if = {
			limit = {
				from = { has_star_flag = natural_spectacle_system }
			}
			owner = {
				change_variable = {
					which = natural_spectacle_system_seen
					value = 1
				}
				add_modifier = {
					modifier = natural_spectacle_modifier
					mult = value:natural_spectacle_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:natural_spectacle_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = natural_spectacle_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_NATURAL_SPECTACLE
						counter = natural_spectacle_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_SPECTACLE_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		else_if = {
			limit = {
				from = { has_star_flag = precursor_system }
			}
			owner = {
				change_variable = {
					which = precursor_system_seen
					value = 1
				}
				add_modifier = {
					modifier = precursor_modifier
					mult = value:precursor_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:precursor_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = precursor_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_PRECURSOR
						counter = precursor_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_PRECURSOR_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		else_if = {
			limit = {
				from = { has_star_flag = space_ecology_system }
			}
			owner = {
				change_variable = {
					which = space_ecology_system_seen
					value = 1
				}
				add_modifier = {
					modifier = space_ecology_modifier
					mult = value:space_ecology_system_ratio
					days = -1
				}
				if = { #if category is finished, give flag for arkship size
					limit = {
						check_variable_arithmetic = {
							which = value:space_ecology_system_ratio
							multiply = 1
							value >= 1
						}
						NOT = {
							has_country_flag = space_ecology_completed
						}
					}
					add_mission_counter = {
						mission = NOMAD_EXPLORATION_SPACE_ECOLOGY
						counter = space_ecology_progress
						amount = 1
					}
				}
				#notify
				create_message = {
					type = NOMAD_EXPLORATION
					localization = NOMAD_EXPLORATION_ECOLOGY_DESC
					days = 20
					target = from
					variable = {
						type = name
						localization = SYSTEM
						scope = from
					}
				}
			}
		}
		#if no system of a given category were spawned, complete the category but do not grant the modifier
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = ancient_wonders_system
					}
				}
				NOT = { owner = { has_country_flag = ancient_wonders_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_ANCIENT_WONDERS
					counter = ancient_wonders_progress
					amount = 1
				}
			}
		}
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = antique_threat_system
					}
				}
				NOT = { owner = { has_country_flag = antique_threat_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_ANTIQUE_THREAT
					counter = antique_threat_progress
					amount = 1
				}
			}
		}
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = galactic_landmark_system
					}
				}
				NOT = { owner = { has_country_flag = galactic_landmark_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_GALACTIC_LANDMARK
					counter = galactic_landmark_progress
					amount = 1
				}
			}
		}
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = marauder_system
					}
				}
				NOT = { owner = { has_country_flag = marauder_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_MARAUDER
					counter = marauder_progress
					amount = 1
				}
			}
		}
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = natural_spectacle_system
					}
				}
				NOT = { owner = { has_country_flag = natural_spectacle_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_NATURAL_SPECTACLE
					counter = natural_spectacle_progress
					amount = 1
				}
			}
		}
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = precursor_system
					}
				}
				NOT = { owner = { has_country_flag = precursor_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_PRECURSOR
					counter = precursor_progress
					amount = 1
				}
			}
		}
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = space_ecology_system
					}
				}
				NOT = { owner = { has_country_flag = space_ecology_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_SPACE_ECOLOGY
					counter = space_ecology_progress
					amount = 1
				}
			}
		}
		if = {
			limit = {
				NOT = {
					any_system = {
						has_star_flag = enclave
					}
				}
				NOT = { owner = { has_country_flag = enclave_completed } }
			}
			owner = {
				add_mission_counter = {
					mission = NOMAD_EXPLORATION_ENCLAVE
					counter = enclave_progress
					amount = 1
				}
			}
		}
		# grant small unity reward and notify
		owner = {
			add_monthly_resource_mult = {
				resource = unity
				value = @tier1unityreward
				min = @tier1unitymin
				max = @tier1unitymax
			}
		}
	}
}

country_event = { # Nomad Exploration Intro
	id = nomads.4715
	title = "nomads.4715.name"
	desc = "nomads.4715.desc"
	picture = GFX_evt_tiyanki_observation
	show_sound = relic_activation_the_surveyor

	is_triggered_only = yes

	immediate = {
		# exploration trackers - set here (rather than on becoming nomadic) so
		# that mid-game nomad creation paths (GRS spawn, mid-game embark,
		# settlement subjects) are also covered the moment they pick the perk
		calculate_nomads_exploration_targets = yes
		setup_nomad_exploration_trackers = yes
	}

	option = {
		name = nomads.4715.a
		trigger = { owner = { is_xenophobe = no } }
	}

	option = {
		name = nomads.4715.b
		trigger = { owner = { is_xenophobe = yes } }

	}

	after = {
		begin_event_chain = {
			event_chain = nomad_exploration_chain
			target = this
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_ANCIENT_WONDERS
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_ANTIQUE_THREAT
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_ENCLAVE
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_GALACTIC_LANDMARK
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_MARAUDER
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_NATURAL_SPECTACLE
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_PRECURSOR
		}
		enable_mission = {
			name = NOMAD_EXPLORATION_SPACE_ECOLOGY
		}
	}
}

# Fired by on_arkship_lithoid_consume_world (on_progress_start)
# Starts the consume situation on first arrival, or resumes it if the fleet returns after cancelling.
# THIS = planet
# FROM = triggering fleet
planet_event = {
	id = nomads.4750
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = { NOT = { has_planet_flag = being_devoured } }
			# First arrival: full initialisation
			set_timed_planet_flag = {
				days = 360
				flag = recently_eaten_planet
			}
			set_carrier_flag = colony_event
			set_planet_flag = being_devoured
			add_modifier = {
				modifier = being_devoured_modifier
				days = -1
			}
			export_trigger_value_to_variable = {
				trigger = planet_size
				variable = num_districts_terravore
			}
			export_trigger_value_to_variable = {
				trigger = count_deposits
				parameters = {
					type = d_lithoid_devastation
				}
				variable = num_lithoid_blockers
			}
			if = {
				limit = {
					is_variable_set = num_lithoid_blockers
				}
				subtract_variable = {
					which = num_districts_terravore
					value = num_lithoid_blockers
				}
			}
			from.controller = {
				start_situation = {
					type = situation_arkship_lithoid_consume_planet
					target = root
				}
			}
		}
		else = {
			# Returning after cancellation: clear the grace period flag
			remove_planet_flag = arkship_absent_grace
		}
		from = { set_fleet_flag = consuming_world_arkship@root }
	}
}

# Fired by on_arkship_lithoid_consume_world_cancelled
# Sets the 6-month grace period immediately when the action is cancelled.
# THIS = planet
# FROM = triggering fleet
planet_event = {
	id = nomads.4751
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_timed_planet_flag = {
			flag = arkship_absent_grace
			days = 180
		}
		from = { remove_fleet_flag = consuming_world_arkship@root }
	}
}

# Arkship Lithoid Consume Planet - Monthly
# Delivers resources when the consuming arkship is present (grace flag not active).
# THIS = situation, owner = consuming empire, target = planet
situation_event = {
	id = nomads.4755
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		# Consume tick: only when arkship is present and not on cooldown
		if = {
			limit = {
				target = {
					NOR = {
						has_planet_flag = recently_eaten_planet
						has_planet_flag = arkship_absent_grace
					}
				}
			}
			random_list = {
				10 = { # Pop: new bodies absorbed into the arkship colony
					modifier = {
						factor = 0
						target = { num_free_districts = { type = any value < 2 } }
					}
					target = {
						add_deposit = d_lithoid_devastation
						add_deposit = d_lithoid_devastation
						add_planet_devastation = 20
					}
					owner = {
						random_owned_planet = {
							limit = {
								is_colony = yes
								ship = { fleet = { has_fleet_flag = consuming_world_arkship@root.target } }
							}
							create_pop_group = { species = root.owner.owner_main_species }
						}
						create_message = {
							type = MESSAGE_TERRAVORE_CONSUME_WORLD
							localization = MESSAGE_TERRAVORE_CONSUME_WORLD_POP_TEXT
							days = 30
							target = root.target
							variable = { type = name localization = PLANET scope = root.target }
						}
					}
				}
				10 = { # Minerals
					modifier = {
						factor = 0
						target = { num_free_districts = { type = any value < 2 } }
					}
					target = {
						add_deposit = d_lithoid_devastation
						add_deposit = d_lithoid_devastation
						add_planet_devastation = 20
					}
					owner = {
						random_list = {
							1 = {
								add_resource = {
									minerals = 500
								}
							}
							1 = {
								add_resource = {
									minerals = 1000
								}
							}
							1 = {
								add_resource = {
									minerals = 3000
								}
							}
						}
						create_message = {
							type = MESSAGE_TERRAVORE_CONSUME_WORLD
							localization = MESSAGE_TERRAVORE_CONSUME_WORLD_MINERALS_TEXT
							days = 30
							target = root.target
							variable = { type = name localization = PLANET scope = root.target }
						}
					}
				}
				10 = { # Food
					modifier = {
						factor = 0
						target = { num_free_districts = { type = any value < 2 } }
					}
					target = {
						add_deposit = d_lithoid_devastation
						add_deposit = d_lithoid_devastation
						add_planet_devastation = 20
					}
					owner = {
						add_monthly_resource_mult = {
							resource = food
							value = 3
							min = @tier2materialmin
							max = @tier2materialmax
						}
						create_message = {
							type = MESSAGE_TERRAVORE_CONSUME_WORLD
							localization = MESSAGE_ARKSHIP_LITHOID_CONSUME_WORLD_FOOD_TEXT
							days = 30
							target = root.target
							variable = { type = name localization = PLANET scope = root.target }
						}
					}
				}
				10 = { # Last slot - reduced yield
					modifier = {
						factor = 0
						target = { num_free_districts = { type = any value >= 2 } }
					}
					target = {
						add_deposit = d_lithoid_devastation
						add_planet_devastation = 10
					}
					owner = {
						random_list = {
							1 = {
								add_resource = {
									minerals = 250
								}
							}
							1 = {
								add_resource = {
									minerals = 500
								}
							}
							1 = {
								add_resource = {
									minerals = 1500
								}
							}
						}
						create_message = {
							type = MESSAGE_TERRAVORE_CONSUME_WORLD
							localization = MESSAGE_TERRAVORE_CONSUME_WORLD_MINERALS_TEXT
							days = 30
							target = root.target
							variable = { type = name localization = PLANET scope = root.target }
						}
					}
				}
			}
			target = {
				set_timed_planet_flag = { flag = recently_eaten_planet days = 360 }
			}
		}
	}
}

# Arkship Lithoid Consume Planet - Complete
# THIS = situation, owner = consuming empire, target = planet
situation_event = {
	id = nomads.4760
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		owner = {
			if = {
				limit = { has_ascension_perk = ap_become_the_crisis }
				complete_crisis_objective = crisobj_destroy_worlds
			}
			random_owned_fleet = {
				limit = { has_fleet_flag = consuming_world_arkship@root.target }
				remove_fleet_flag = consuming_world_arkship@root.target
				save_event_target_as = devouring_fleet
			}
			create_message = {
				type = MESSAGE_LITHOID_CONSUME_WORLD_COMPLETE
				localization = TOAST_LITHOID_CONSUME_WORLD_COMPLETE_DESC
				days = 30
				target = root.target
				variable = { type = name localization = PLANET scope = root.target }
				variable = { type = name localization = ARKSHIP scope = event_target:devouring_fleet }
			}
		}
		target = {
			if = {
				limit = { has_modifier = holy_planet }
				carrier_event = { id = planet_destruction.605 scopes = { fromfrom = event_target:devouring_fleet } }
			}
			change_pc = pc_shattered
			clear_deposits = yes
			remove_planet_flag = being_devoured
			remove_carrier_flag = colony_event
			remove_planet_flag = arkship_absent_grace
			remove_modifier = being_devoured_modifier
		}
		destroy_situation = this
	}
}

# Fired by on_arkship_nanotech_consume_world (on_progress_start)
# Starts the consume situation on first arrival, or resumes it if the fleet returns after cancelling.
# THIS = planet
# FROM = triggering fleet
planet_event = {
	id = nomads.4800
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = { NOT = { has_planet_flag = being_devoured } }
			# First arrival: full initialisation
			set_timed_planet_flag = {
				days = 360
				flag = recently_eaten_planet
			}
			set_carrier_flag = colony_event
			set_planet_flag = being_devoured
			add_modifier = {
				modifier = being_devoured_modifier_nanotech
				days = -1
			}
			export_trigger_value_to_variable = {
				trigger = planet_size
				variable = num_districts_terravore
			}
			export_trigger_value_to_variable = {
				trigger = count_deposits
				parameters = {
					type = d_nanotech_devastation
				}
				variable = num_lithoid_blockers
			}
			if = {
				limit = {
					is_variable_set = num_lithoid_blockers
				}
				subtract_variable = {
					which = num_districts_terravore
					value = num_lithoid_blockers
				}
			}
			from.controller = {
				start_situation = {
					type = situation_arkship_nanotech_consume_planet
					target = root
				}
			}
		}
		else = {
			# Returning after cancellation: clear the grace period flag
			remove_planet_flag = arkship_absent_grace
		}
		from = { set_fleet_flag = consuming_world_arkship@root }
	}
}

# Fired by on_arkship_nanotech_consume_world_cancelled
# Sets the 6-month grace period immediately when the action is cancelled.
# THIS = planet
# FROM = triggering fleet
planet_event = {
	id = nomads.4801
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_timed_planet_flag = {
			flag = arkship_absent_grace
			days = 180
		}
		from = { remove_fleet_flag = consuming_world_arkship@root }
	}
}

# Arkship Nanotech Consume Planet - Monthly
# Delivers resources when the consuming arkship is present (grace flag not active).
# THIS = situation, owner = consuming empire, target = planet
situation_event = {
	id = nomads.4805
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		# Consume tick: only when arkship is present and not on cooldown
		if = {
			limit = {
				target = {
					NOR = {
						has_planet_flag = recently_eaten_planet
						has_planet_flag = arkship_absent_grace
					}
				}
			}
			random_list = {
				10 = { # Nanites - large yield
					modifier = {
						factor = 0
						target = { num_free_districts = { type = any value < 2 } }
					}
					target = {
						add_deposit = d_nanotech_devastation
						add_deposit = d_nanotech_devastation
						add_planet_devastation = 10
					}
					owner = {
						add_monthly_resource_mult = {
							resource = nanites
							value = 3
							min = @tier1materialmin
							max = @tier1materialmax
						}
						create_message = {
							type = MESSAGE_TERRAVORE_CONSUME_WORLD
							localization = MESSAGE_ARKSHIP_NANOTECH_CONSUME_WORLD_NANITES_TEXT
							days = 30
							target = root.target
							variable = { type = name localization = PLANET scope = root.target }
						}
					}
				}
				10 = { # Nanites - large yield (second slot for variety)
					modifier = {
						factor = 0
						target = { num_free_districts = { type = any value < 2 } }
					}
					target = {
						add_deposit = d_nanotech_devastation
						add_deposit = d_nanotech_devastation
						add_planet_devastation = 10
					}
					owner = {
						add_monthly_resource_mult = {
							resource = nanites
							value = 5
							min = @tier1materialmin
							max = @tier1materialmax
						}
						create_message = {
							type = MESSAGE_TERRAVORE_CONSUME_WORLD
							localization = MESSAGE_ARKSHIP_NANOTECH_CONSUME_WORLD_NANITES_TEXT
							days = 30
							target = root.target
							variable = { type = name localization = PLANET scope = root.target }
						}
					}
				}
				10 = { # Last slot - reduced yield
					modifier = {
						factor = 0
						target = { num_free_districts = { type = any value >= 2 } }
					}
					target = {
						add_deposit = d_nanotech_devastation
						add_planet_devastation = 5
					}
					owner = {
						add_monthly_resource_mult = {
							resource = nanites
							value = 2
							min = @tier1materialmin
							max = @tier1materialmax
						}
						create_message = {
							type = MESSAGE_TERRAVORE_CONSUME_WORLD
							localization = MESSAGE_ARKSHIP_NANOTECH_CONSUME_WORLD_FINAL_TEXT
							days = 30
							target = root.target
							variable = { type = name localization = PLANET scope = root.target }
						}
					}
				}
			}
			target = {
				set_timed_planet_flag = { flag = recently_eaten_planet days = 360 }
			}
		}
	}
}

# Arkship Nanotech Consume Planet - Complete
# THIS = situation, owner = consuming empire, target = planet
situation_event = {
	id = nomads.4810
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		target = {
			if = {
				limit = { has_planet_flag = fallen_empire_capital }
				owner = { set_country_flag = past_the_expiration_date_achievement }
			}
			change_pc = pc_gray_goo
			clear_deposits = yes
			add_deposit = d_nanite_harvester_deposit
			random_list = {
				10 = {
					add_deposit = d_nanite_harvester_deposit_regular
				}
				5 = {
					add_deposit = d_nanite_harvester_deposit_regular
					add_deposit = d_nanite_harvester_deposit
					add_deposit = d_nanite_harvester_deposit
					add_deposit = d_nanite_harvester_deposit
				}
				5 = {
					add_deposit = d_nanite_harvester_deposit_regular
					add_deposit = d_nanite_harvester_deposit_regular
				}
				1 = {
					add_deposit = d_nanite_harvester_deposit_large
				}
			}
			remove_planet_flag = being_devoured
			remove_carrier_flag = colony_event
			remove_planet_flag = arkship_absent_grace
			remove_modifier = being_devoured_modifier_nanotech
			carrier_event = { id = action.68 }
		}
		owner = {
			save_event_target_as = nanotech_consuming_empire
			random_owned_fleet = {
				limit = { has_fleet_flag = consuming_world_arkship@root.target }
				remove_fleet_flag = consuming_world_arkship@root.target
				save_event_target_as = consuming_fleet
			}
			change_variable = {
				which = nanotech_worlds_created
				value = 1
			}
			create_message = {
				type = MESSAGE_NANOTECH_CONSUME_WORLD_COMPLETE
				localization = TOAST_NANOTECH_CONSUME_WORLD_COMPLETE_DESC
				days = 30
				target = root.target
				variable = { type = name localization = PLANET scope = root.target }
				variable = { type = name localization = ARKSHIP scope = event_target:consuming_fleet }
			}
		}
		every_country = {
			limit = {
				is_nomadic = no
				NOT = { is_same_value = event_target:nanotech_consuming_empire }
				has_communications = event_target:nanotech_consuming_empire
			}
			add_opinion_modifier = {
				modifier = opinion_arkship_nanotech_consume_world
				who = event_target:nanotech_consuming_empire
			}
		}
		destroy_situation = this
	}
}

####################
### Civic Events ###
####################

#Reavers plunder resources while bombing a planet - FROM = Bombarder
carrier_event = {
	id = nomads.5005
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOT = { has_carrier_flag = recently_plundered }
		has_orbital_bombardment_stance = plundering
		planet_devastation <= 70
	}

	immediate = {
		log = "booty plundered"
		set_timed_carrier_flag = {
			flag = recently_plundered
			days = 30
		}
		random_list = {
			2.5 = { #No Booty
				#  Increased by armies on the planet
				modifier = {
					factor = trigger:num_armies
				}
			}
			50 = { #Steal 40% of a month of prod
				modifier = {
					factor = 1.1
					any_fleet_in_orbit = {
						controller = {
							is_same_value = from
						}
						leader? = {
							has_trait = leader_trait_plunderer
						}
					}
				}
				modifier = {
					factor = 1.15
					any_fleet_in_orbit = {
						controller = {
							is_same_value = from
						}
						leader? = {
							has_trait = leader_trait_plunderer_2
						}
					}
				}
				steal_planet_output = {
					planet = this
					who = FROM
					resource = all
					percentage = 0.4
					mode = steal
					notification_mode = both
				}
			}
		}
	}
}

starbase_event = {
	id = nomads.5010
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		# waystation still exists and still has module
		exists = root
		solar_system = {
			any_fleet_in_system = {
				is_waystation_ship = yes
				starbase = {
					has_starbase_module = waystation_pirate_hideout
				}
			}
		}
		NOR = {
			has_starbase_flag = piracy_collected
			solar_system = {
				space_owner = { is_same_value = root.owner }
			}
		}
	}

	immediate = {
		set_timed_starbase_flag = {
			flag = piracy_collected
			days = 29
		}
		if = {
			limit = { root.fleet = { is_cloaked = no } }
			solar_system = {
				every_system_planet = {
					limit = { is_colony = yes }
					owner = {
						add_opinion_modifier = {
							who = root.owner
							modifier = opinion_waystation_piracy
						}
					}
				}
			}
		}
	}

	after = {
		starbase_event = { id = nomads.5010 days = 30 }
	}
}
## Add and remove Deep Sleep blockers for loser (on_war_ended)
country_event = {
	id = nomads.5020
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_colony = {
			NOR = {
				has_deposit = d_deep_sleep_cryo_pods
				has_deposit = d_deep_sleep_cryo_pods_renewed
			}
		}
		is_deep_sleep_country = yes
	}

	immediate = {
		every_owned_colony = {
			limit = {
				NOR = {
					has_deposit = d_deep_sleep_cryo_pods
					has_deposit = d_deep_sleep_cryo_pods_renewed
				}
			}
			while = {
				count < 3
				add_deposit = d_deep_sleep_cryo_pods
			}
		}
	}
}

country_event = {
	id = nomads.5025
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_colony = {
			OR = {
				has_deposit = d_deep_sleep_cryo_pods
				has_deposit = d_deep_sleep_cryo_pods_renewed
			}
		}
		NOT ={ is_deep_sleep_country = yes }
	}

	immediate = {
		every_owned_colony = {
			limit = {
				OR = {
					has_deposit = d_deep_sleep_cryo_pods
					has_deposit = d_deep_sleep_cryo_pods_renewed
				}
			}
			every_deposit = {
				limit = {
					OR = {
						is_deposit_type = d_deep_sleep_cryo_pods
						is_deposit_type = d_deep_sleep_cryo_pods_renewed
					}
				}
				remove_deposit = yes
			}
		}
	}
}
## Add and remove Deep Sleep blockers for winner (on_war_ended)
country_event = {
	id = nomads.5030
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from? = {
			any_owned_colony = {
				NOR = {
					has_deposit = d_deep_sleep_cryo_pods
					has_deposit = d_deep_sleep_cryo_pods_renewed
				}
			}
			is_deep_sleep_country = yes
		}
	}

	immediate = {
		from = {
			every_owned_colony = {
				limit = {
					NOR = {
						has_deposit = d_deep_sleep_cryo_pods
						has_deposit = d_deep_sleep_cryo_pods_renewed
					}
				}
				while = {
					count < 3
					add_deposit = d_deep_sleep_cryo_pods
				}
			}
		}
	}
}

country_event = {
	id = nomads.5035
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from? = {
			any_owned_colony = {
				OR = {
					has_deposit = d_deep_sleep_cryo_pods
					has_deposit = d_deep_sleep_cryo_pods_renewed
				}
			}
			NOT ={ is_deep_sleep_country = yes }
		}
	}

	immediate = {
		from = {
			every_owned_colony = {
				limit = {
					OR = {
						has_deposit = d_deep_sleep_cryo_pods
						has_deposit = d_deep_sleep_cryo_pods_renewed
					}
				}
				every_deposit = {
					limit = {
						OR = {
							is_deposit_type = d_deep_sleep_cryo_pods
							is_deposit_type = d_deep_sleep_cryo_pods_renewed
						}
					}
					remove_deposit = yes
				}
			}
		}
	}
}

########################## Star Seekers Encampment

ship_event = {
	id = nomads.5040
	hide_window = yes
	is_triggered_only = yes

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

	immediate = {
		if = {
			limit = {
				is_arkship_tier_1 = yes
			}
			spread_tent_ships_tier_1 = yes
		}
		if = {
			limit = {
				is_arkship_tier_2 = yes
			}
			spread_tent_ships_tier_2 = yes
		}
		if = {
			limit = {
				is_arkship_tier_3 = yes
			}
			spread_tent_ships_tier_3 = yes
		}
	}
}

#######################
### Contract Events ###
#######################

country_event = { # Delayed cleanup: removes contract_downtime_taken after the contract completion popup has been built
	id = nomads.6998
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		remove_country_flag = contract_downtime_taken
	}
}

country_event = { #Contract event gatekeeper
	id = nomads.6000
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOT = {
			has_country_flag = no_contract_event_allowed
		}
		exists = fromfrom
	}

	immediate = {
		#roll event chance
		random_list = {
			18 = {
				modifier = {
					has_country_flag = recent_contract_event_check
					factor = 10
				}
			}
			2 = {
				#save_event_targets
				fromfrom = {
					save_event_target_as = contract_issuer
				}
				fromfromfrom = {
					save_event_target_as = contract_system
				}
				random_owned_contract = {
					limit = {
						issuer = {
							is_same_value = event_target:contract_issuer
						}
					}
					save_event_target_as = ongoing_contract
				}
				#roll event type
				random_list = { #We use an on_action to simplify filtering by contract category
					70 = { fire_on_action = { on_action = on_contract_event_common } } #70
					25 = { fire_on_action = { on_action = on_contract_event_rare } } #25
					5 = { fire_on_action = { on_action = on_contract_event_unique } } #5
				}
			}
		}
	}

	after = {
		set_timed_country_flag = { flag = recent_contract_event_check days = 360 }
	}
}

#Global Contract Event Pool
#Scopes:
#FROM = Root
#FROMFROM = Ship
#FROMFROMFROM = Root
#FROMFROMFROMFROM = Contract Issuer
#event_target:contract_issuer = Contract issuer
#event_target:ongoing_contract = Contract

country_event = { # Global Common - Found Loot. Keep it?
	id = nomads.6005
	title = "nomads.6005.name"
	desc = "nomads.6005.desc"
	picture = GFX_evt_asteroid_field
	show_sound = event_ambient_arkship_engine

	is_triggered_only = yes

	fire_only_once = yes

	immediate = {
		#roll random reward and set flag
		random_list = {
			1 = { # Alloys
				set_country_flag = nomads.6005.alloys
			}
			1 = { # Consumer Goods
				modifier = {
					country_uses_consumer_goods = no
					factor = 0
				}
				set_country_flag = nomads.6005.cg
			}
			1 = { # Minerals
				set_country_flag = nomads.6005.minerals
			}
			1 = { # Physics
				set_country_flag = nomads.6005.physics
			}
			1 = { # Society
				set_country_flag = nomads.6005.society
			}
			1 = { # Engineering
				set_country_flag = nomads.6005.engineering
			}
			1 = { # Lots of Alloys
				set_country_flag = nomads.6005.alloys2
			}
			1 = { # Lots of Consumer Goods
				modifier = {
					country_uses_consumer_goods = no
					factor = 0
				}
				set_country_flag = nomads.6005.cg2
			}
			1 = { # Lots of Minerals
				set_country_flag = nomads.6005.minerals2
			}
			1 = { # Lots of Physics
				set_country_flag = nomads.6005.physics2
			}
			1 = { # Lots of Society
				set_country_flag = nomads.6005.society2
			}
			1 = { # Lots of Engineering
				set_country_flag = nomads.6005.engineering2
			}
			1 = { # Rare Crystals
				set_country_flag = nomads.6005.crystals
			}
			1 = { # Exotic Gases
				set_country_flag = nomads.6005.gases
			}
			1 = { # Volatile Motes
				set_country_flag = nomads.6005.motes
			}
			1 = { # Zro
				set_country_flag = nomads.6005.zro
			}
			1 = { # Nanites - DLC lock
				modifier = {
					NOR = {
						has_distar = no
						has_machine_age_dlc = no
					}
					factor = 0
				}

				set_country_flag = nomads.6005.nanites
			}
			1 = { # Living Metal
				set_country_flag = nomads.6005.livingmetal
			}
			1 = { # Astral Threads - DLC lock
				modifier = {
					has_astral_planes_dlc = no
					factor = 0
				}
				set_country_flag = nomads.6005.threads
			}
			1 = { # Minor Artifacts - DLC lock
				modifier = {
					has_ancrel = no
					factor = 0
				}
				set_country_flag = nomads.6005.artifacts
			}
			1 = { # Dark Matter
				set_country_flag = nomads.6005.darkmatter
			}
		}
		if = { #issuer existence redundancy
			limit = {
				NOT = {
					exists = event_target:contract_issuer
				}
			}
			random_owned_contract = {
				limit = {
					exists = issuer
				}
				issuer = {
					save_event_target_as = contract_issuer
				}
			}
		}
	}

	option = {
		name = nomads.6005.alloys
		trigger = {
			has_country_flag = nomads.6005.alloys
		}
		add_resource = {
			alloys = 500
		}
	}

	option = {
		name = nomads.6005.cg
		trigger = {
			has_country_flag = nomads.6005.cg
		}
		add_resource = {
			consumer_goods = 1000
		}
	}

	option = {
		name = nomads.6005.minerals
		trigger = {
			has_country_flag = nomads.6005.minerals
		}
		add_resource = {
			minerals = 3000
		}
	}

	option = {
		name = nomads.6005.physics
		trigger = {
			has_country_flag = nomads.6005.physics
		}
		add_resource = {
			physics_research = 1000
		}
	}

	option = {
		name = nomads.6005.society
		trigger = {
			has_country_flag = nomads.6005.society
		}
		add_resource = {
			society_research = 1000
		}
	}

	option = {
		name = nomads.6005.engineering
		trigger = {
			has_country_flag = nomads.6005.engineering
		}
		add_resource = {
			engineering_research = 1000
		}
	}

	option = {
		name = nomads.6005.alloys
		trigger = {
			has_country_flag = nomads.6005.alloys2
		}
		add_resource = {
			alloys = 2000
		}
	}

	option = {
		name = nomads.6005.cg
		trigger = {
			has_country_flag = nomads.6005.cg2
		}
		add_resource = {
			consumer_goods = 4000
		}
	}

	option = {
		name = nomads.6005.minerals
		trigger = {
			has_country_flag = nomads.6005.minerals2
		}
		add_resource = {
			minerals = 12000
		}
	}

	option = {
		name = nomads.6005.physics
		trigger = {
			has_country_flag = nomads.6005.physics2
		}
		add_resource = {
			physics_research = 4000
		}
	}

	option = {
		name = nomads.6005.society
		trigger = {
			has_country_flag = nomads.6005.society2
		}
		add_resource = {
			society_research = 4000
		}
	}

	option = {
		name = nomads.6005.engineering
		trigger = {
			has_country_flag = nomads.6005.engineering2
		}
		add_resource = {
			engineering_research = 4000
		}
	}

	option = {
		name = nomads.6005.crystals
		trigger = {
			has_country_flag = nomads.6005.crystals
		}
		add_resource = {
			rare_crystals = 500
		}
	}

	option = {
		name = nomads.6005.gases
		trigger = {
			has_country_flag = nomads.6005.gases
		}
		add_resource = {
			exotic_gases = 500
		}
	}

	option = {
		name = nomads.6005.motes
		trigger = {
			has_country_flag = nomads.6005.motes
		}
		add_resource = {
			volatile_motes = 500
		}
	}

	option = {
		name = nomads.6005.zro
		trigger = {
			has_country_flag = nomads.6005.zro
		}
		add_resource = {
			sr_zro = 500
		}
	}

	option = {
		name = nomads.6005.nanites
		trigger = {
			has_country_flag = nomads.6005.nanites
		}
		add_resource = {
			nanites = 500
		}
	}

	option = {
		name = nomads.6005.livingmetal
		trigger = {
			has_country_flag = nomads.6005.livingmetal
		}
		add_resource = {
			sr_living_metal = 500
		}
	}

	option = {
		name = nomads.6005.threads
		trigger = {
			has_country_flag = nomads.6005.threads
		}
		add_resource = {
			astral_threads = 500
		}
	}

	option = {
		name = nomads.6005.artifacts
		trigger = {
			has_country_flag = nomads.6005.artifacts
		}
		add_resource = {
			minor_artifacts = 500
		}
	}

	option = {
		name = nomads.6005.darkmatter
		trigger = {
			has_country_flag = nomads.6005.darkmatter
		}
		add_resource = {
			sr_dark_matter = 500
		}
	}

	option = {
		name = nomads.6005.giveback
		add_resource = {
			influence = 25
		}
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_returned_contract_chance_find
				who = root
			}
		}
	}

	after = {
		hidden_effect = {
			remove_country_flag = nomads.6005.alloys
			remove_country_flag = nomads.6005.cg
			remove_country_flag = nomads.6005.minerals
			remove_country_flag = nomads.6005.physics
			remove_country_flag = nomads.6005.society
			remove_country_flag = nomads.6005.engineering
			remove_country_flag = nomads.6005.alloys2
			remove_country_flag = nomads.6005.cg2
			remove_country_flag = nomads.6005.minerals2
			remove_country_flag = nomads.6005.physics2
			remove_country_flag = nomads.6005.society2
			remove_country_flag = nomads.6005.engineering2
			remove_country_flag = nomads.6005.crystals
			remove_country_flag = nomads.6005.gases
			remove_country_flag = nomads.6005.motes
			remove_country_flag = nomads.6005.zro
			remove_country_flag = nomads.6005.nanites
			remove_country_flag = nomads.6005.livingmetal
			remove_country_flag = nomads.6005.threads
			remove_country_flag = nomads.6005.artifacts
			remove_country_flag = nomads.6005.darkmatter
		}
	}
}

country_event = { # Global Rare 1/5 - Arkship Equipment Breaks :c
	id = nomads.6015
	title = "nomads.6015.name"
	desc = "nomads.6015.desc"
	picture = GFX_evt_nomad_default
	show_sound = event_arkship_in_crisis

	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		#owned arkship has four modules
		any_owned_fleet = {
			starbase? = {
				is_arkship_starbase = yes
				count_starbase_modules = {
					include_being_constructed = no
					count >= 3
				}
			}
		}
	}

	immediate = {
		#save the target
		random_owned_fleet = {
			limit = {
				starbase? = {
					is_arkship_starbase = yes
					count_starbase_modules = {
						include_being_constructed = no
						count >= 3
					}
				}
			}
			starbase = {
				save_event_target_as = target_arkship
				colony = {
					save_event_target_as = arkship_colony
				}
			}
		}
	}

	option = { #Fix em
		name = nomads.6015.a
		trigger = {
			NOR = {
				has_valid_civic = civic_scavengers
				has_valid_civic = civic_corporate_scavengers
			}
		}
		allow = {
			resource_stockpile_compare = {
				resource = alloys
				value >= 200
			}
		}
		add_resource = {
			alloys = -200
		}
	}

	option = { #let them break
		name = nomads.6015.b
		while = {
			count = 3
			event_target:target_arkship = {
				remove_starbase_module = {
					slot = all
					module = all
				}
			}
		}
		add_modifier = {
			modifier = module_recycling
			days = 720
		}
	}

	option = { #Perhaps an upgrade - gain an extra arkship district
		name = nomads.6015.c
		allow = {
			resource_stockpile_compare = {
				resource = alloys
				value >= 500
			}
		}
		add_resource = {
			alloys = -500
		}
		event_target:arkship_colony = {
			add_deposit = d_compact_modules
		}
	}

	option = { #Scavengers fix it for almost free and get unity
		name = nomads.6015.d
		trigger = {
			OR = {
				has_valid_civic = civic_scavengers
				has_valid_civic = civic_corporate_scavengers
			}
		}
		allow = {
			resource_stockpile_compare = {
				resource = alloys
				value >= 50
			}
		}
		add_resource = {
			alloys = -50
			unity = 500
		}
	}

	option = { # Charge the contract issuer instead
		name = nomads.6015.e
		trigger = {
			is_megacorp = no
		}
		custom_tooltip = nomads.6015.e.tt
		#Contract issuer loses opinion
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_abused_contract
				who = root
			}
		}
	}

	option = { # Charge the contract issuer - Megacorp
		name = nomads.6015.e
		trigger = {
			is_megacorp = yes
		}
		custom_tooltip = nomads.6015.e.tt
		add_resource = {
			trade = 150
		}
		#Contract issuer loses more opinion
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_abused_contract_2
				who = root
			}
		}
	}
}

# Unexpected Cargo - Event Chain
country_event = {
	id = nomads.6020
	title = "nomads.6020.name"
	desc = "nomads.6020.desc"
	picture = GFX_evt_nomad_default
	show_sound = event_mystic_reveal

	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		is_nomadic = yes
		is_gestalt = no
		has_contract_of_category = resource_contract
		NOT = { has_country_flag = unexpected_cargo_flag }
	}

	immediate = {
		owner = {
			set_country_flag = unexpected_cargo_flag
		}
	}

	option = { #Discard
		name = nomads.6020.a
		#fail ongoing contract, reward with unity because we are protecting the crew
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_negative
				who = root.owner
			}
		}
		owner = {
			add_monthly_resource_mult = {
				resource = unity
				value = @tier1unityreward
				min = @tier1unitymin
				max = @tier1unitymax
			}
		}
		event_target:ongoing_contract = {
			cancel_contract = {
				refund = no
			}
		}
	}

	option = { #Check up on the noises, will give a random chance
		name = nomads.6020.b
		hidden_effect = {
			random_list = {
				1 = { # bioluminescent critters
					country_event = { id = nomads.6025 days = 10 random = 20 } }
				1 = { # space kitten
					country_event = { id = nomads.6030 days = 10 random = 20 } }
				1 = { # mechanoid
					country_event = { id = nomads.6035 days = 10 random = 20 } }
				1 = { # minor explosions
					country_event = { id = nomads.6040 days = 10 random = 20 } }
				1 = { # leader
					country_event = { id = nomads.6045 days = 10 random = 20 } }
				1 = { # stowaways
					country_event = { id = nomads.6050 days = 10 random = 20 } }
			}
		}
	}
}

country_event = { # bioluminescent critters
	id = nomads.6025
	title = "nomads.6025.name"
	desc = "nomads.6025.desc"
	picture = GFX_evt_nomad_default
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	option = {
		name = nomads.6025.a
		owner = {
			add_monthly_resource_mult = {
				resource = physics_research
				value = @tier2researchreward
				min = @tier2researchmin
				max = @tier2researchmax
			}
		}
		allow = {
			resource_stockpile_compare = {
				resource = alloys
				value >= 500
			}
		}
		add_resource = {
			alloys = -500
		}
	}

	option = {
		name = nomads.6025.b
		owner = {
			add_monthly_resource_mult = {
				resource = unity
				value = @tier1unityreward
				min = @tier1unitymin
				max = @tier1unitymax
			}
		}
	}
}

country_event = { # space kitten
	id = nomads.6030
	title = "nomads.6030.name"
	desc = "nomads.6030.desc"
	picture = GFX_evt_but_they_are_cute
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	immediate = {
		random_owned_leader = {
			limit = { is_ruler = no }
			save_event_target_as = cat_dad
		}
	}

	option = {
		name = nomads.6030.a
		owner = {
			add_monthly_resource_mult = {
				resource = society_research
				value = @tier1researchreward
				min = @tier1researchmin
				max = @tier1researchmax
			}
		}
		allow = {
			resource_stockpile_compare = {
				resource = alloys
				value >= 100
			}
		}
		add_resource = {
			alloys = -100
		}
	}

	option = { #give leader the pet trait, otherwise eager trait
		name = nomads.6030.b
		if = {
			limit = {
				event_target:cat_dad = {
					NOT = { has_trait = leader_trait_emotional_support_pet }
				}
			}
			event_target:cat_dad = {
				add_trait = {
					trait = leader_trait_emotional_support_pet
					show_message = yes
				}
			}
		}
		else_if = {
			limit = {
				event_target:cat_dad = {
					NOT = { has_trait = leader_trait_eager }
				}
			}
			event_target:cat_dad = {
				add_trait = {
					trait = leader_trait_eager
					show_message = yes
				}
			}
		}
		else = {
			event_target:cat_dad = {
				add_experience = 500
			}
		}
	}
}

country_event = { # mechanoid, only for robo/mech empires
	id = nomads.6035
	title = "nomads.6035.name"
	desc = "nomads.6035.desc"
	picture = GFX_evt_nomad_default
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	trigger = {
		event_target:contract_issuer = {
			OR = {
				is_individual_machine = yes
				is_mechanical_empire = yes
				is_robot_empire = yes
			}
		}
	}

	option = {
		name = nomads.6035.a
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root.owner
			}
		}
		add_favors = {
			target = event_target:contract_issuer
			value = 1
		}
	}

	option = {
		name = nomads.6035.b
		event_target:contract_issuer = {
			add_intel = {
				amount = 70
				who = root.owner
			}
		}
	}

	option = {
		name = nomads.6035.c
		owner = {
			add_monthly_resource_mult = {
				resource = engineering_research
				value = @tier1researchreward
				min = @tier1researchmin
				max = @tier1researchmax
			}
			add_resource = { rare_crystals = 100 }
		}
	}
}

country_event = { # minor explosions
	id = nomads.6040
	title = "nomads.6040.name"
	desc = "nomads.6040.desc"
	picture = GFX_evt_nomad_default
	show_sound = event_arkship_in_crisis

	is_triggered_only = yes

	option = {
		name = nomads.6040.a
		owner = { add_resource = { exotic_gases = 100 } }
		allow = {
			resource_stockpile_compare = {
				resource = alloys
				value >= 500
			}
		}
		add_resource = {
			alloys = -500
		}
	}

	option = {
		name = nomads.6040.b
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_negative
				who = root.owner
			}
		}
		allow = {
			resource_stockpile_compare = {
				resource = alloys
				value >= 200
			}
		}
		add_resource = {
			alloys = -200
		}
		owner = {
			add_monthly_resource_mult = {
				resource = engineering_research
				value = @tier1researchreward
				min = @tier1researchmin
				max = @tier1researchmax
			}
		}
	}
}

country_event = { # leader
	id = nomads.6045
	title = "nomads.6045.name"
	desc = "nomads.6045.desc"
	picture = GFX_evt_nomad_default
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	immediate = {
		owner = {
			create_leader = {
				class = commander
				species = event_target:contract_issuer.species
				name = random
			}
			last_created_leader = {
				exile_leader_as = arkship_cargo_leader
				save_event_target_as = arkship_cargo_leader
			}
		}
	}

	option = {
		name = nomads.6045.a
		allow = {
			resource_stockpile_compare = {
				resource = trade
				value >= 500
			}
		}
		add_resource = {
			trade = -500
		}
		set_leader = arkship_cargo_leader
		create_message = {
			type = MESSAGE_RECRUITED_LEADER
			localization =  MESSAGE_RECRUITED_LEADER_DESC
			days = @toast_message_days
			target = event_target:arkship_cargo_leader
			variable = {
				type = name
				localization = LEADER
				scope = event_target:arkship_cargo_leader
			}
			variable = {
				key = "border"
				value = "GFX_invisible"
			}
		}
	}

	option = {
		name = nomads.6045.b
		owner = {
			add_monthly_resource_mult = {
				resource = alloys
				value = @tier2materialreward
				min = @tier2materialmin
				max = @tier2materialmax
			}
		}
	}

	option = {
		name = nomads.6045.c
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root.owner
			}
		}
	}
}

country_event = { # stowaways
	id = nomads.6050
	title = "nomads.6050.name"
	desc = "nomads.6050.desc"
	picture = GFX_evt_nomad_default
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	trigger = {
		owner = {
			NOR = {
				is_xenophobe = yes
				is_genocidal_infernal = yes
				is_homicidal = yes
				is_militarist = yes
				has_valid_civic = civic_hive_devouring_swarm
				has_valid_civic = civic_barbaric_despoilers
				has_valid_civic = civic_fanatic_purifiers
				has_ascension_perk = ap_nihilistic_acquisition
				has_policy_flag = slavery_allowed
			}
		}
	}

	immediate = {
		random_owned_colony = {
			save_event_target_as = arkship_colony
		}
	}

	option = {
		name = nomads.6050.a
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root.owner
			}
		}
	}

	option = {
		name = nomads.6050.b
		owner = {
			add_intel = {
				amount = 30
				who = event_target:contract_issuer
			}
		}
	}

	option = {
		name = nomads.6050.c
		event_target:arkship_colony = {
			create_pop_group = {
				species = event_target:contract_issuer.species
				size = 50
			}
		}
		allow = {
			resource_stockpile_compare = {
				resource = consumer_goods
				value >= 500
			}
		}
		add_resource = {
			consumer_goods = -500
		}
	}
}

country_event = { # Global Common - Academic Inquiry
	id = nomads.6055
	title = "nomads.6055.name"

	desc = {
		trigger = { is_machine_empire = yes }
		text = "nomads.6055.desc.machine"
	}
	desc = {
		trigger = { is_hive_empire = yes }
		text = "nomads.6055.desc.hive"
	}
	desc = {
		trigger = { is_gestalt = no }
		text = "nomads.6055.desc"
	}
	picture = GFX_evt_nomad_contracts
	show_sound = event_ambient_arkship_engine

	is_triggered_only = yes
	fire_only_once = yes

	option = {
		name = {
			trigger = { is_gestalt = yes }
			text = nomads.6055.a.gestalt
		}
		name = {
			trigger = { is_gestalt = no }
			text = nomads.6055.a
		}
		add_monthly_resource_mult = {
			resource = society_research
			value = @tier1researchreward
			min = @tier1researchmin
			max = @tier1researchmax
		}
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root
			}
		}
	}

	option = { # Grant limited access for a fee
		name = nomads.6055.b
		add_monthly_resource_mult = {
			resource = trade
			value = @tier1materialreward
			min = @tier1materialmin
			max = @tier1materialmax
		}
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_negative
				who = root
			}
		}
	}

	option = { # Decline
		name = nomads.6055.c
		add_monthly_resource_mult = {
			resource = unity
			value = @tier1unityreward
			min = @tier1unitymin
			max = @tier1unitymax
		}
	}

}

country_event = { # Global Common - Operational Dispute
	id = nomads.6060
	title = "nomads.6060.name"

	desc = {
		trigger = { is_machine_empire = yes }
		text = "nomads.6060.desc.machine"
	}
	desc = {
		trigger = { is_hive_empire = yes }
		text = "nomads.6060.desc.hive"
	}
	desc = {
		trigger = { is_gestalt = no }
		text = "nomads.6060.desc"
	}
	picture = GFX_evt_nomad_contracts
	show_sound = event_ambient_arkship_engine

	is_triggered_only = yes
	fire_only_once = yes

	option = { # Mediate between the parties
		name = {
			trigger = { is_gestalt = yes }
			text = nomads.6060.a.gestalt
		}
		name = {
			trigger = { is_gestalt = no }
			text = nomads.6060.a
		}
		add_modifier = {
			modifier = jurisdictional_mediation
			days = 360
		}
	}

	option = { # Enforce operational protocols - defer to the issuer's chain of command
		name = nomads.6060.b
		add_modifier = {
			modifier = command_reassigned
			days = 360
		}
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root
			}
		}
	}
}

country_event = { # Global Common - Downtime Request
	id = nomads.6065
	title = "nomads.6065.name"
	desc = {
		trigger = { is_machine_empire = yes }
		text = "nomads.6065.desc.machine"
	}
	desc = {
		trigger = { is_hive_empire = yes }
		text = "nomads.6065.desc.hive"
	}
	desc = {
		trigger = { is_gestalt = no }
		text = "nomads.6065.desc"
	}
	picture = GFX_evt_nomad_contracts
	show_sound = event_ambient_arkship_engine

	is_triggered_only = yes
	fire_only_once = yes

	option = { # Approve the request
		name = nomads.6065.a
		custom_tooltip = nomads.6065.a.tt
		set_country_flag = contract_downtime_taken
		add_modifier = {
			modifier = downtime_granted
			days = 360
		}
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_negative
				who = root
			}
		}
	}

	option = {
		name = nomads.6065.b
		add_modifier = {
			modifier = downtime_denied
			days = 360
		}
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root
			}
		}
	}
}

country_event = { # Global Common - A Growing Reputation
	id = nomads.6070
	title = "nomads.6070.name"

	desc = {
		trigger = { is_machine_empire = yes }
		text = "nomads.6070.desc.machine"
	}
	desc = {
		trigger = { is_hive_empire = yes }
		text = "nomads.6070.desc.hive"
	}
	desc = {
		trigger = { is_gestalt = no }
		text = "nomads.6070.desc"
	}
	picture = GFX_evt_nomad_contracts
	show_sound = event_ambient_arkship_engine

	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		check_variable = {
			which = contract_completed
			value >= 2
		}
		count_country = {
			count >= 2
			limit = {
				is_country_type = default
				NOT = { is_same_value = root }
				has_communications = root
			}
		}
	}

	immediate = {
		random_country = {
			limit = {
				is_country_type = default
				NOR = {
					is_same_value = root
					is_same_value = event_target:contract_issuer
				}
				has_communications = root
			}
			save_event_target_as = reputation_noticed_empire
		}
	}

	option = { # Seize upon the opportunity
		name = nomads.6070.a
		event_target:reputation_noticed_empire = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root
			}
		}
	}

	option = { # A good reputation is its own reward
		name = nomads.6070.b
		add_monthly_resource_mult = {
			resource = unity
			value = @tier1unityreward
			min = @tier1unitymin
			max = @tier1unitymax
		}
	}

	option = { # Pass credit to the contract issuer
		name = nomads.6070.c
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root
			}
		}
	}
}

country_event = { # Global Common - Survey Data Windfall
	id = nomads.6075
	title = "nomads.6075.name"
	desc = "nomads.6075.desc"
	picture = GFX_evt_nomad_contracts
	show_sound = event_ambient_arkship_engine

	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		has_contract_of_category = science_contract
	}

	option = { # Sell the data to the broker
		name = nomads.6075.a
		add_resource = {
			trade = 500
		}
	}

	option = { # Deliver to the contract issuer as a bonus
		name = nomads.6075.b
		add_resource = {
			influence = 25
		}
		event_target:contract_issuer = {
			add_opinion_modifier = {
				modifier = opinion_unexpected_cargo_positive
				who = root
			}
		}
	}

	option = { # Archive it for our own use
		name = nomads.6075.c
		add_monthly_resource_mult = {
			resource = physics_research
			value = @tier2researchreward
			min = @tier2researchmin
			max = @tier2researchmax
		}
	}
}

############
#Migration##
############

country_event = {
	id = nomads.7000
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_homicidal = no
		is_country_type = default
		has_encountered_other_species = yes
		OR = {
			NOT = { has_ethic = "ethic_gestalt_consciousness" }
			has_valid_civic = civic_machine_servitor
			has_origin = origin_forever_cruise
		}
		any_owned_fleet = {
			is_arkship_ship = yes
			solar_system? = {
				any_system_planet_colony = {
					can_receive_nomad_migration = yes
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					has_country_flag = migration_information
				}
			}
			country_event = {
				id = nomads.7005
			}
		}
		else_if = {
			limit = { has_policy_flag = refugees_allowed }
			random_owned_fleet = {
				limit = {
					is_arkship_ship = yes
					solar_system? = {
						any_system_planet_colony = {
							can_receive_nomad_migration = yes
						}
					}
				}
				random_owned_ship = {
					limit = { is_arkship_ship = yes }
					save_event_target_as = migrating_arkship
				}
				solar_system = {
					random_system_planet = {
						limit = {
							can_receive_nomad_migration = yes
						}
						save_event_target_as = migrating_planet
					}
				}
				owner = {
					arkship_migration_effect = yes
				}
			}
			# Player toast — at most once per year, regardless of how many migrations fired
			if = {
				limit = { NOT = { has_country_flag = nomad_migration_toast_recent } }
				set_timed_country_flag = {
					flag = nomad_migration_toast_recent
					years = 1
				}
				create_message = {
					type = MESSAGE_NOMAD_MIGRATION
					localization = MESSAGE_NOMAD_MIGRATION_DESC
					days = 30
					target = event_target:migrating_planet
					variable = {
						type = name
						localization = PLANET
						scope = event_target:migrating_planet
					}
					variable = {
						type = name
						localization = ARKSHIP
						scope = event_target:migrating_arkship
					}
				}
			}
		}
	}
}

country_event = {
	id = nomads.7005
	title = "nomads.7005.title"

	desc = {
		trigger = { has_origin = origin_forever_cruise }
		text = "nomads.7005.desc.forever_cruise"
	}
	desc = {
		trigger = {
			NOT = { has_origin = origin_forever_cruise }
			has_valid_civic = civic_machine_servitor
		}
		text = "nomads.7005.desc.servitor"
	}
	desc = {
		trigger = {
			NOR = {
				has_origin = origin_forever_cruise
				has_valid_civic = civic_machine_servitor
			}
			event_target:migrating_planet = {
				owner = { is_same_species = root }
			}
		}
		text = "nomads.7005.desc.same_species"
	}
	desc = {
		trigger = {
			NOR = {
				has_origin = origin_forever_cruise
				has_valid_civic = civic_machine_servitor
				event_target:migrating_planet = {
					owner = { is_same_species = root }
				}
			}
		}
		text = "nomads.7005.desc"
	}

	picture = GFX_evt_nomad_default
	show_sound = event_ambient_arkship_engine
	is_triggered_only = yes
	location = event_target:migrating_planet

	trigger = {
		# Must be able to set a Refugee Policy
		has_encountered_other_species = yes
		is_country_type = default
		is_homicidal = no
		OR = {
			NOT = { has_ethic = "ethic_gestalt_consciousness" }
			has_valid_civic = civic_machine_servitor
			has_origin = origin_forever_cruise
		}
		any_owned_fleet = {
			is_arkship_ship = yes
			solar_system? = {
				any_system_planet_colony = {
					can_receive_nomad_migration = yes
				}
			}
		}
	}

	immediate = {
		set_country_flag = migration_information
		random_owned_fleet = {
			limit = {
				is_arkship_ship = yes
				solar_system? = {
					any_system_planet_colony = {
						can_receive_nomad_migration = yes
					}
				}
			}
			random_owned_ship = {
				limit = { is_arkship_ship = yes }
				save_event_target_as = migrating_arkship
			}
			solar_system = {
				random_system_planet = {
					limit = {
						can_receive_nomad_migration = yes
					}
					save_event_target_as = migrating_planet
				}
			}
		}
	}

	option = {
		name = nomads.7005.a
		allow = {
			custom_tooltip = {
				text = civic_tooltip_not_xenophobe
				is_xenophobe = no
			}
		}
		set_policy = {
			policy = refugees
			option = refugees_allowed
		}
		arkship_migration_effect = yes
	}

	option = {
		name = nomads.7005.b
		arkship_migration_effect = yes
	}

	option = {
		name = nomads.7005.c
		allow = {
			custom_tooltip = {
				text = civic_tooltip_not_xenophile
				is_xenophile = no
			}
		}
		set_policy = {
			policy = refugees
			option = refugees_not_allowed
		}
	}
}

############
# DYSON GUN
############

#Nomad Dyson Gun Placeholder
system_event = {
	id = nomads.8000
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		dyson_gun_firing = yes
	}
}

#Nomad Dyson Gun Impact
# THIS = Target System
# FROM = Megastructure
system_event = {
	id = nomads.8001
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		dyson_gun_impact_placement = yes
		# Warn the targeted system owner so they can move away before the cannon fires
		if = {
			limit = {
				space_owner? = {
					NOT = { is_same_value = from.owner }
				}
			}
			space_owner = {
				create_message = {
					type = MESSAGE_DYSON_TARGETED
					localization = MESSAGE_DYSON_TARGETED_DESC
					days = @toast_message_days
					target = root
					variable = {
						type = name
						localization = SYSTEM
						scope = root
					}
				}
			}
		}
	}
}

# === Stellar Cannon crisis responses ===
# These fire on the empire that fired the Stellar Cannon (ROOT), from dyson_gun_firing.

# The Contingency hijacks our targeting and scatters the shot
# ROOT = Firing empire
country_event = {
	id = nomads.8050
	title = "nomads.8050.name"
	desc = "nomads.8050.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = ai_crisis_1
	}

	is_triggered_only = yes

	trigger = {
		is_ai = no
	}

	option = {
		name = nomads.8050.a
	}
}

# The Unbidden relish the energy we just delivered to them
# ROOT = Firing empire
country_event = {
	id = nomads.8055
	title = "nomads.8055.name"
	diplomatic_title = BLANK_STRING
	desc = "nomads.8055.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = exd1
		room = "extradimensional_blue_room"
	}

	is_triggered_only = yes

	trigger = {
		is_ai = no
	}

	immediate = {
		establish_communications_no_message = event_target:extradimensionals
	}

	option = {
		name = nomads.8055.a
	}
}

# Cetana, struck for the first time, is disappointed in us - uses her crisis conversation UI
# ROOT = Firing empire
country_event = {
	id = nomads.8060
	title = "nomads.8060.name"
	diplomatic_title = "nomads.8060.name"
	desc = "nomads.8060.desc"
	show_sound = nomads_8060_desc

	diplomatic = yes

	event_window_type = crisis_leader_conversation
	situation = event_target:synth_queen_doomclock_situation

	picture_event_data = {
		portrait = event_target:cetana_face
		room = event_target:synth_queen_country_global.owner
	}
	picture = GFX_crisis_conversation_bg_synth_queen

	is_triggered_only = yes

	trigger = {
		is_ai = no
	}

	immediate = {
		get_cetana_face = yes
		establish_communications_no_message = event_target:synth_queen_country_global
	}

	option = {
		name = nomads.8060.a
	}
}

# Cetana reproaches us as she reflects the shot back onto our capital - uses her crisis conversation UI
# ROOT = Firing empire
country_event = {
	id = nomads.8065
	title = "nomads.8065.name"
	diplomatic_title = "nomads.8065.name"
	desc = "nomads.8065.desc"
	show_sound = nomads_8065_desc

	diplomatic = yes

	event_window_type = crisis_leader_conversation
	situation = event_target:synth_queen_doomclock_situation

	picture_event_data = {
		portrait = event_target:cetana_face
		room = event_target:synth_queen_country_global.owner
	}
	picture = GFX_crisis_conversation_bg_synth_queen

	is_triggered_only = yes

	trigger = {
		is_ai = no
	}

	immediate = {
		get_cetana_face = yes
		establish_communications_no_message = event_target:synth_queen_country_global
	}

	option = {
		name = nomads.8065.a
	}
}

# Disable waystation in a nomad raid war
# THIS = Waystation Ship (taking damage)
# FROM = Attacking Ship
ship_event = {
	id = nomads.8004
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_disabled = no
		has_hp_percentage <= 0.5
		is_waystation_ship = yes
		exists = from
		from = { exists = owner }
		OR = {
			# Disabled by an enemy during a nomad raid war
			from = {
				owner = {
					any_war = {
						# Symmetric raid: either side may disable the other side's waystations
						OR = {
							any_defender = { is_same_value = root.owner }
							any_attacker = { is_same_value = root.owner }
						}
						OR = {
							using_war_goal = {
								type = wg_nomad_raid
								owner = prev
							}
							using_war_goal = {
								type = wg_raid_contract
								owner = prev
							}
							AND = {
								is_total_war = yes
								prev = { is_nomadic = yes }
							}
						}
					}
				}
			}
			# Disabled by hostile space fauna instead of being destroyed (matching regular starbase behavior)
			from = { owner = { is_fauna_country = yes } }
		}
	}

	immediate = {
		set_disabled = yes
		if = {
			limit = {
				from = { owner = { is_fauna_country = yes } }
			}
			from.fleet = {
				set_fleet_flag = disabled_waystation
			}
		}
	}
}

# Enabling Waystation Fauna Disabled
# Scope = Fleet
# From = System
fleet_event = {
	id = nomads.8005
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_fleet_flag = disabled_waystation
	}

	immediate = {
		remove_fleet_flag = disabled_waystation
		from = {
			random_fleet_in_system = {
				limit = {
					is_waystation_ship = yes
				}
				random_owned_ship = {
					set_disabled = no
				}
			}
		}
	}
}

# Enabling Waystation Fauna Disabled
# This = owner of fleet 1 (destroyed)
# From = owner of fleet 2 (combatant)
# FromFrom = fleet 1
# FromFromFrom = fleet 2

country_event = {
	id = nomads.8006
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = { has_fleet_flag = disabled_waystation }
	}

	immediate = {
		fromfrom.solar_system = {
			random_fleet_in_system = {
				limit = {
					is_waystation_ship = yes
				}
				random_owned_ship = {
					set_disabled = no
				}
			}
		}
	}
}

#Seize Waystation Operation
espionage_operation_event = {
	id = nomads.8010
	title = nomads.8010.name
	desc = nomads.8010.desc
	picture = GFX_evt_waystation
	show_sound = event_spymaster
	espionage_operation = yes

	is_triggered_only = yes

	option = {
		name = ACKNOWLEDGED
	}
}

espionage_operation_event = {
	id = nomads.8015
	title = nomads.8015.name
	desc = nomads.8015.desc
	picture = GFX_evt_waystation
	show_sound = event_spymaster
	espionage_operation = yes

	is_triggered_only = yes

	option = {
		name = ACKNOWLEDGED
	}
}

espionage_operation_event = {
	id = nomads.8020
	title = nomads.8020.name
	desc = nomads.8020.desc
	picture = GFX_evt_waystation
	show_sound = event_espionage_concluded
	espionage_operation = yes

	is_triggered_only = yes

	immediate = {
		set_espionage_operation_progress_locked = yes
		target = { save_event_target_as = target_waystation }
	}

	option = {
		name = LAUNCH_OPERATION
		hidden_effect = {
			owner = {
				country_event = { id = nomads.8025 days = 1 }
			}
		}
	}

	after = {
		set_espionage_operation_progress_locked = no
	}
}

#8025 - steal station stockpile and choose to destroy it or not if nomad (regular automatically destroys)
country_event = {
	id = nomads.8025
	title = nomads.8025.name
	desc = nomads.8025.desc
	picture = GFX_evt_gunrunning
	show_sound = event_ground_battle
	location = event_target:target_waystation

	is_triggered_only = yes

	immediate = {
		event_target:target_waystation = {
			owner = {
				country_event = { id = nomads.8030 days = 3 }
			}
		}
		#roll for detection
		random_list = {
			50 = {
				modifier = {
					factor = 1.5
					has_tradition = tr_subterfuge_finish
					event_target:target_waystation.owner = {
						NOT = {
							has_tradition = tr_subterfuge_finish
						}
					}
				}
				modifier = {
					factor = 1.5
					event_target:target_waystation.owner = {
						relative_encryption_decryption = {
							target = root
							value < 1.0
						}
					}
				}
			}
			50 = {
				modifier = {
					factor = 1.5
					NOT = { has_tradition = tr_subterfuge_finish }
					event_target:target_waystation.owner = {
						has_tradition = tr_subterfuge_finish
					}
				}
				modifier = {
					factor = 1.5
					relative_encryption_decryption = {
						target = event_target:target_waystation.owner
						value < 1.0
					}
				}
				from = { set_espionage_operation_flag = detected }
			}
		}
	}

	option = { #destroy the station
		trigger = {
			from = {
				has_espionage_asset_sabotage = yes
			}
		}
		name = nomads.8025.a
		custom_tooltip = nomads.8025.a.tt
		hidden_effect = {
			event_target:target_waystation = {
				fleet = {
					random_owned_ship = {
						transfer_resource_stockpile = {
							target = root
						}
					}
				}
				ship = {
					destroy_ship = this
				}
			}
		}
		from = {
			destroy_espionage_asset_sabotage = yes
			set_espionage_operation_flag = destroyed
		}
		from.spynetwork = {
			add_spy_network_level_on_success_effect = { VALUE = -75 }
		}
	}

	option = { #just loot it
		name = nomads.8025.b
		#destroy some modules
		event_target:target_waystation = {
			remove_starbase_module = {
				slot = 1
			}
		}
		from = { set_espionage_operation_flag = looted }
		from.spynetwork = {
			add_spy_network_level_on_success_effect = { VALUE = -35 }
		}
	}

	option = { #keep it
		trigger = {
			is_nomadic = yes
			from = {
				has_espionage_asset_manipulation = yes
			}
		}
		name = nomads.8025.c
		from = {
			destroy_espionage_asset_manipulation = yes
			set_espionage_operation_flag = stolen
		}
		#change ownership
		event_target:target_waystation = {
			set_owner = root
		}
		from.spynetwork = {
			add_spy_network_level_on_success_effect = { VALUE = -75 }
		}
	}

	after = {
		if = {
			limit = { exists = event_target:target_waystation }
			event_target:target_waystation = {
				fleet = {
					random_owned_ship = {
						transfer_resource_stockpile = {
							target = root
						}
					}
				}
			}
		}
	}
}

country_event = {
	id = nomads.8030
	title = nomads.8030.name
	desc = {
		trigger = {
			fromfrom = {
				has_espionage_operation_flag = detected
				has_espionage_operation_flag = destroyed
			}
		}
		text = nomads.8030.detected_destroyed.desc
	}
	desc = {
		trigger = {
			fromfrom = {
				has_espionage_operation_flag = detected
				has_espionage_operation_flag = looted
			}
		}
		text = nomads.8030.detected_looted.desc
	}
	desc = {
		trigger = {
			fromfrom = {
				NOT = { has_espionage_operation_flag = detected }
				has_espionage_operation_flag = destroyed
			}
		}
		text = nomads.8030.destroyed.desc
	}
	desc = {
		trigger = {
			fromfrom = {
				NOT = { has_espionage_operation_flag = detected }
				has_espionage_operation_flag = looted
			}
		}
		text = nomads.8030.looted.desc
	}
	desc = {
		trigger = { fromfrom = { has_espionage_operation_flag = stolen } }
		text = nomads.8030.stolen.desc
	}
	picture = GFX_evt_guardians
	show_sound = event_ground_battle

	is_triggered_only = yes

	option = { #tame option for looting cases
		trigger = {
			fromfrom = { has_espionage_operation_flag = looted }
		}
		name = UNFORTUNATE
		if = {
			limit = {
				fromfrom = {
					has_espionage_operation_flag = detected
				}
			}
			add_opinion_modifier = {
				modifier = opinion_waystation_looted_detected
				who = from
			}
		}
	}

	option = { #angry option for stealing and destroying
		trigger = {
			OR = {
				fromfrom = {
					has_espionage_operation_flag = destroyed
					has_espionage_operation_flag = stolen
				}
			}
		}
		name = CURSES
		if = {
			limit = {
				fromfrom = {
					OR = {
						has_espionage_operation_flag = detected
						has_espionage_operation_flag = stolen
					}
				}
			}
			add_opinion_modifier = {
				modifier = opinion_waystation_destroyed_detected
				who = from
			}
		}
	}

	after = {
		hidden_effect = {
			destroy_espionage_operation = fromfrom
		}
	}
}


# nomads.8040 - Tribute Vault: clean up stale loyalty modifier for former subjects
country_event = {
	id = nomads.8040
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_modifier = tribute_vault_loyalty
		is_subject = no
	}

	immediate = {
		erase_modifier = { MOD = tribute_vault_loyalty }
		set_variable = { which = tribute_vault_count value = 0 }
	}
}

# Fired by on_starbase_destroyed
# This = starbase being destroyed (not ship!)
# From = fleet that destroyed the starbase
# nomads.8045 - Spawn debris when a disabled waystation is destroyed
# THIS = starbase (destroyed), FROM = destroying fleet
starbase_event = {
	id = nomads.8045
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_waystation_starbase = yes
		exists = from
		ship = { is_disabled = yes }
	}

	immediate = {
		fleet = {
			spawn_custom_debris = {
				for = from.owner
				reset_killed_ship_designs = yes
				reset_killed_ship_stockpile = yes
			}
		}
	}
}

### Clues of the Chrysanthemum

# A Voice from the Past
country_event = {
	id = nomads.9000
	title = "nomads.9000.name"
	desc = "nomads.9000.desc"
	picture = GFX_evt_arkship_bridge
	show_sound = event_radio_chatter_02
	event_chain = clues_chrysanthemum

	is_triggered_only = yes

	trigger = {
		is_ai = no
		has_country_flag = human_3
		any_system = {
			is_surveyed = {
				who = root
				status = no
			}
			distance = {
				source = root.capital_scope
				use_bypasses = no
				min_jumps = 1
				max_jumps = 5
			}
			NOT = { has_star_flag = hostile_system }
			has_owner = no
		}
	}

	immediate = {
		begin_event_chain = {
			event_chain = "clues_chrysanthemum"
			target = ROOT
		}
		capital_scope.ship.fleet = {
			set_fleet_flag = hyacinth_fleet
		}
	}

	option = {
		name = "nomads.9000.a"
		tooltip = {
			begin_event_chain = {
				event_chain = "clues_chrysanthemum"
				target = ROOT
			}
		}
		hidden_effect = {
			random_system = {
				limit = {
					is_surveyed = {
						who = root
						status = no
					}
					distance = {
						source = root.capital_scope
						use_bypasses = no
						min_jumps = 1
						max_jumps = 5
					}
					NOT = { has_star_flag = hostile_system }
					has_owner = no
				}
				save_event_target_as = chrysanthemum_relay_system
				set_star_flag = chrysanthemum_relay_system
			}
		}
		create_point_of_interest = {
			id = chrysanthemum_poi.1
			name = "chrysanthemum_poi_1"
			desc = "chrysanthemum_poi_1_desc"
			event_chain = "clues_chrysanthemum"
			category = chrysanthemum_cat
			location = event_target:chrysanthemum_relay_system
		}
	}
	option = {
		name = "nomads.9000.b"
		hidden_effect = {
			end_event_chain = "clues_chrysanthemum"
		}
	}
}

# Signal Found
fleet_event = {
	id = nomads.9005
	title = "nomads.9005.name"
	desc = "nomads.9005.desc"
	picture = GFX_evt_star_chart
	show_sound = event_scanner
	location = root
	event_chain = clues_chrysanthemum

	is_triggered_only = yes

	trigger = {
		owner? = {
			has_event_chain = "clues_chrysanthemum"
		}
		has_fleet_flag = hyacinth_fleet
		solar_system = { has_star_flag = chrysanthemum_relay_system }
	}

	immediate = {
		solar_system = { save_event_target_as = chrysanthemum_relay_system } # Just in case the player clicks the event option while they are in a hyper lane
	}

	option = {
		name = "nomads.9005.a"
		hidden_effect = {
			event_target:chrysanthemum_relay_system = {
				remove_star_flag = chrysanthemum_relay_system
				random_neighbor_system = {
					weights = {
						base = 1
						modifier = { # For narative sake I would really like you not to end up back in Vela
							factor = 0.00001
							has_star_flag = vela_system
						}
					}
					spawn_system = {
						min_distance >= 10
						max_distance <= 30
						initializer = "chrysanthemum_tomb_system"
					}
				}
			}
			owner = {
				remove_point_of_interest = chrysanthemum_poi.1
				create_point_of_interest = {
					id = chrysanthemum_poi.2
					name = "chrysanthemum_poi_2"
					desc = "chrysanthemum_poi_2_desc"
					event_chain = "clues_chrysanthemum"
					category = chrysanthemum_cat
					location = last_created_system
				}
			}
		}
	}
}

# Unity
fleet_event = {
	id = nomads.9010
	title = "nomads.9010.name"
	desc = "nomads.9010.desc"
	picture = GFX_evt_arkship_in_orbit
	location = root
	trackable = yes
	event_chain = clues_chrysanthemum

	is_triggered_only = yes

	trigger = {
		owner? = {
			has_event_chain = "clues_chrysanthemum"
		}
		has_fleet_flag = hyacinth_fleet
		solar_system = { has_star_flag = chrysanthemum_tomb_system }
	}

	immediate = {
		owner = { remove_point_of_interest = chrysanthemum_poi.2 }
		solar_system = {
			remove_star_flag = chrysanthemum_tomb_system
			random_system_planet = {
				limit = { has_planet_flag = unity_tomb }
				save_event_target_as = unity_planet
				create_ambient_object = {
					type = "chrysanthemum_object"
					location = THIS
				}
			}
		}
		last_created_ambient_object = {
			save_event_target_as = the_chrysanthemum
		}
	}

	option = {
		name = "nomads.9010.a"
		event_target:unity_planet = {
			create_archaeological_site = site_chrysanthemum_colony
		}
	}
}

# =========================================================
# Clues of the Chrysanthemum - Digsite Events
# =========================================================

fleet_event = {	# The Escalation
	id = nomads.9015
	title = "nomads.9015.name"
	desc = "nomads.9015.desc"
	picture = GFX_evt_tomb_world
	show_sound = event_dig_site
	location = from
	archaeology = yes
	is_triggered_only = yes

	immediate = {
		from = { set_site_progress_locked = yes }
	}

	after = {
		from = { set_site_progress_locked = no }
	}

	option = {
		name = "nomads.9015.a"
		owner = {
			add_monthly_resource_mult = {
				resource = unity
				value = @tier1unityreward
				min = @tier1unitymin
				max = @tier1unitymax
			}
		}
	}
}

fleet_event = {	# The Vote
	id = nomads.9020
	title = "nomads.9020.name"
	desc = "nomads.9020.desc"
	picture = GFX_evt_nuclear_explosion
	show_sound = event_dig_site
	location = from
	archaeology = yes
	is_triggered_only = yes

	immediate = {
		from = { set_site_progress_locked = yes }
	}

	after = {
		from = { set_site_progress_locked = no }
	}

	option = {
		name = "nomads.9020.a"
		owner = {
			add_monthly_resource_mult = {
				resource = unity
				value = @tier1unityreward
				min = @tier1unitymin
				max = @tier1unitymax
			}
		}
	}
}

fleet_event = {	# The Logs
	id = nomads.9025
	title = "nomads.9025.name"
	desc = "nomads.9025.desc"
	picture = GFX_evt_derelict_interior
	show_sound = event_dig_site
	location = from
	archaeology = yes
	is_triggered_only = yes

	immediate = {
		from = { set_site_progress_locked = yes }
	}

	after = {
		from = { set_site_progress_locked = no }
		owner = { end_event_chain = "clues_chrysanthemum" }
	}

	option = {
		name = "nomads.9025.a"
		owner = {
			add_monthly_resource_mult = {
				resource = physics_research
				value = @tier2researchreward
				min = @tier2researchmin
				max = @tier2researchmax
			}
			add_monthly_resource_mult = {
				resource = unity
				value = @tier1unityreward
				min = @tier1unitymin
				max = @tier1unitymax
			}
		}
	}
}

country_event = {
	id = nomads.9030
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		capital_scope.solar_system = {
			random_system_planet = {
				limit = {
					has_planet_flag = delete_me
				}
				remove_planet = yes
			}
		}
	}
}

# Stores each waystation's stockpile fill, or its network's for a logistic hub, on its system, and each logistic ship's stockpile fill on the fleet
country_event = {
	id = nomads.9035
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_nomadic = yes
	}

	immediate = {
		every_owned_nonprimary_starbase = {
			limit = {
				is_waystation_starbase = yes
			}
			if = {
				limit = {
					waystation_has_remote_collection = yes
				}
				export_trigger_value_to_variable = {
					trigger = waystation_network_stockpile_used
					variable = waystation_collection_fill
				}
			}
			else = {
				export_trigger_value_to_variable = {
					trigger = used_stockpile_capacity_percentage
					variable = waystation_collection_fill
				}
			}
			solar_system = {
				set_variable = {
					which = waystation_collection_fill
					value = prev.waystation_collection_fill
				}
			}
		}
		every_owned_fleet = {
			limit = {
				is_ship_size = nomads_constructor
			}
			set_variable = {
				which = logistic_drop_off_fill
				value = 0
			}
			every_owned_ship = {
				limit = {
					is_ship_size = nomads_constructor
				}
				export_trigger_value_to_variable = {
					trigger = used_stockpile_capacity_percentage
					variable = logistic_drop_off_fill
				}
				if = {
					limit = {
						prev = {
							check_variable = {
								which = logistic_drop_off_fill
								value < prev.logistic_drop_off_fill
							}
						}
					}
					prev = {
						set_variable = {
							which = logistic_drop_off_fill
							value = prev.logistic_drop_off_fill
						}
					}
				}
			}
		}
	}
}

# THIS = Starbase (Not The Ship)
# FROM = Previous Owner Country
starbase_event = {
	id = nomads.9040
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_waystation_starbase = yes
	}

	immediate = {
		solar_system = {
			clear_variable = waystation_collection_fill
		}
	}
}
