#Tooltips will be generated from these rules when they fail. Use of custom_tooltip is recommended
#There are still multiple hard-coded rules that apply in addition to these.
#If rules here evaluate to true, there might still be other rules that make the action unavailable.
#If a rule here evaluates to false the action will become unavailable

#Root = target country
#This = country
is_valid_rival = {
	custom_tooltip = {
		fail_text = INVALID_RIVAL_DESC
		root = {
			OR = {
				prev = { NOT = { is_country_type = default } }
				AND = {
					relative_power = { who = prev value >= inferior }
					OR = {
						relative_power = { who = prev value <= superior }
						prev = { has_policy_flag = diplo_stance_animosity }
					}
				}
				is_country_type = mirrored_country # Mirrored Empire doesn't have relative power but we want to be rivals anyway
			}
		}
	}
}

# Root = claimer
# This = system
can_add_claim = {
	hidden_trigger = {
		if = {
			limit = {
				root = { NOT = { is_country_type = default } }
			}
			always = yes
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_POLICY
		OR = {
			root = { has_policy_flag = unrestricted_wars }
			AND = {
				exists = owner
				owner = { is_gestalt = yes }
			}
			AND = {
				exists = owner
				owner = {
					any_war = {
						any_attacker = { is_same_value = prevprev }
						any_defender = { is_same_value = root }
					}
				}
			}
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_NOT_VALID_EMPIRE
		NAND = {
			exists = owner
			owner = {
				NOR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_DEVOURING_SWARM
		NAND = {
			root = { has_valid_civic = civic_hive_devouring_swarm }
			root = { is_lithoid_empire = no }
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_DEVOURING_SWARM_LITHOID
		NAND = {
			root = { has_valid_civic = civic_hive_devouring_swarm }
			root = { is_lithoid_empire = yes }
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_ASSIMILATOR
		NOT = { root = { has_valid_civic = civic_machine_assimilator } }
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_NOMAD
		NOT = {
			root = { is_nomadic = yes }
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_PURIFIERS
		NAND = {
			exists = owner
			root = { has_valid_civic = civic_fanatic_purifiers }
			NOT = {	owner = { is_same_species = root } }
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_EXTERMINATOR
		NAND = {
			exists = owner
			root = { has_valid_civic = civic_machine_terminator }
			owner = {
				NOR = {
					is_same_species = root
					has_country_flag = synthetic_empire
					is_machine_empire = yes
					is_individual_machine = yes
				}
			}
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_THEY_THREAT
		NAND = {
			exists = owner
			owner = {
				OR = {
					has_valid_civic = civic_hive_devouring_swarm
					has_valid_civic = civic_fanatic_purifiers
					has_valid_civic = civic_machine_terminator
					has_valid_civic = civic_machine_assimilator
					has_valid_civic = civic_scorched_earth
					has_valid_civic = civic_hive_scorched_earth
				}
			}
			root = {
				NOR = {
					has_valid_civic = civic_hive_devouring_swarm
					has_valid_civic = civic_fanatic_purifiers
					has_valid_civic = civic_machine_terminator
					has_valid_civic = civic_machine_assimilator
					has_valid_civic = civic_scorched_earth
					has_valid_civic = civic_hive_scorched_earth
				}
			}
		}
	}
}

# Root = taker country
# This = giver country
can_get_claim_from_lost_system = {
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_NOT_VALID_EMPIRE
		OR = {
			is_country_type = default
			is_country_type = fallen_empire
			is_country_type = awakened_fallen_empire
		}
		root = {
			OR = {
				is_country_type = default
				is_country_type = fallen_empire
				is_country_type = awakened_fallen_empire
			}
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_DEVOURING_SWARM
		NOT = { has_valid_civic = civic_hive_devouring_swarm }
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_ASSIMILATOR
		NOT = { has_valid_civic = civic_machine_assimilator }
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_PURIFIERS
		NAND = {
			has_valid_civic = civic_fanatic_purifiers
			NOT = {	is_same_species = root }
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_EXTERMINATOR
		NAND = {
			has_valid_civic = civic_machine_terminator
			NOR = {
				is_same_species = root
				has_country_flag = synthetic_empire
				is_machine_empire = yes
			}
		}
	}
}

# Root = Bombarding country
# This = Receiving country
should_surrender_planets_to = {
	is_country_type = default
	is_gestalt = no
	is_homicidal = no
	is_crisis_faction = no
	is_primitive = no
	root = {
		is_homicidal = no
		is_crisis_faction = no
		is_primitive = no
		is_country_type = default
		NOT = {
			is_nomadic = yes
			has_contract = contract_raid_planet_small
			NOT = { is_at_war_with = this }
		}
	}
}

#Root = country
#This = species
species_has_citizenship = {
	has_citizenship_type = { type = citizenship_full country = root }
}

#Root = country
#This = pop_group
should_abduct_pop_group = {
	planet = {
		exists = owner
		owner = { is_hostile = root }
	}
	has_virtual_species_trait = no
	NAND = {
		has_citizenship_type = { type = citizenship_purge country = root }
		NOR = {
			has_purge_type = { type = purge_matrix country = root }
			has_purge_type = { type = purge_processing country = root }
			has_purge_type = { type = purge_labor_camps country = root }
			has_purge_type = { type = purge_necrophage country = root }
			has_purge_type = { type = purge_cosmogenesis_lathe_resettle country = root }
		}
	}
}

#Root = planet
#This = species
species_can_live_on_planet = {
	hidden_trigger = {
		exists = root #somehow this appeared in the error log. No idea how!
		NAND = {
			root = { is_planet_class = pc_machine }
			NOR = {
				has_trait = trait_machine_unit
				has_trait = trait_mechanical
				has_trait = trait_cybernetic
				has_unplugged_positive_trait = yes
			}
		}
		if = {
			limit = {
				root = { is_planet_class = pc_hive }
			}
			OR = {
				has_trait = trait_hive_mind
				AND = { #Necrophage Hive Mind can have Necrophytes
					exists = root.owner
					root.owner = {
						is_hive_empire = yes
						has_origin = origin_necrophage
						prev = { species_can_be_necrophaged = yes }
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = root.owner
			root.owner = {
				has_origin = origin_clone_army
			}
			OR = {
				has_trait = trait_clone_soldier_infertile
				has_trait = trait_clone_soldier_infertile_full_potential
			}
		}
		custom_tooltip = {
			fail_text = CLONE_ARMY_FORCE_DECLINE
			root = {
				check_variable = { which = clone_pops_missing value >= 0 }
			}
		}
	}
	custom_tooltip = {
		fail_text = WILDERNESS_NATURAL_WORLDS_DECLINE
		if = {
			limit = {
				has_trait = trait_wilderness
			}
			root = { is_habitable_for_wilderness = yes }
		}
	}

	custom_tooltip = {
		fail_text = WILDERNESS_DEAD_WORLD
		if = {
			limit = {
				has_trait = trait_wilderness
				exists = root.owner
			}
			root.owner = { is_wilderness_empire = yes }
		}
	}
}

#Root = ship
#This = species
species_can_live_on_ship = {
	hidden_trigger = {
		exists = root #somehow this appeared in the error log. No idea how!
		if = {
			limit = {
				exists = root.owner
				root.owner = {
					has_origin = origin_clone_army
				}
				OR = {
					has_trait = trait_clone_soldier_infertile
					has_trait = trait_clone_soldier_infertile_full_potential
				}
			}
			custom_tooltip = {
				fail_text = CLONE_ARMY_FORCE_DECLINE
				root = {
					check_variable = { which = clone_pops_missing value >= 0 }
				}
			}
		}
	}
}

#Root = planet
#This = species
species_has_uncapped_habitability_on_planet = {
	exists = root.owner
	OR = {
		AND = {
			is_robotic = no
			root.owner = {
				is_mutation_authority = yes
			}
		}
		AND = {
			has_trait = trait_auto_hab_preference
			root.owner = {
				has_mutation_environmental_integration_tradition = yes
			}
		}
		AND = {
			root = { is_wet = yes }
			has_trait = trait_cyborg_climate_adjustment_wet
		}
		AND = {
			root = { is_dry = yes }
			has_trait = trait_cyborg_climate_adjustment_dry
		}
		AND = {
			root = { is_cold = yes }
			has_trait = trait_cyborg_climate_adjustment_cold
		}
	}
}

#Root = ship
#This = species
species_has_uncapped_habitability_on_ship = {
	exists = root.owner
	OR = {
		AND = {
			is_robotic = no
			root.owner = {
				is_mutation_authority = yes
			}
		}
		AND = {
			has_trait = trait_auto_hab_preference
			root.owner = {
				has_mutation_environmental_integration_tradition = yes
			}
		}
	}
}

#Root = country
#This = pop_group
pop_group_can_join_factions = {
	if = {
		limit = {
			owner? = {
				is_democratic_authority = yes
				has_country_flag = cyber_collectivist
			}
		}
		OR = {
			pop_group_has_happiness = yes
			has_trait = trait_cybernetic
		}
	}
	else = {
		pop_group_has_happiness = yes
	}
}

#Note: this is also used in pop faction support calculations
#Root = country
#This = pop_group
can_vote_in_democratic_election = {
	has_citizenship_rights = yes
	is_sapient = yes
	is_enslaved = no
	is_being_purged = no
}

#This = leader
should_pay_for_leader_upkeep = {
	NOR = {
		AND = {
			exists = owner
			owner = {
				is_gestalt = yes
			}
			is_councilor = yes
		}
		has_trait = leader_trait_spacebender
		has_leader_flag = should_not_have_upkeep
	}
	NOR = {
		has_leader_flag = marauder_merc_leader
		has_leader_flag = hired_admiral_merc_leader
		has_trait = leader_trait_paladin
	}
}

#Checks whether any leaders (scientists etc) can be formed from this pop group
#Root = country
#This = pop_group
can_generate_leader_from_pop_group = {
	can_be_leader = yes
}

#Checks whether this species is eligible to become rulers through elections
#Root = country
#This = species
can_generate_leader_from_species = {
	is_sapient = yes
	OR = {
		NAND = {
			NOT = { root = { has_technology = "tech_synthetic_leaders" } }
			has_trait = "trait_mechanical"
		}
		has_citizenship_type = { type = citizenship_full country = root }
	}
}

#Checks whether this species can become espionage assets (note that some checks are also done in code)
#Root = country
#This = species
can_create_espionage_asset_from_species = {
	OR = {
		NOT = { has_trait = "trait_mechanical" }
		root = { has_technology = "tech_droid_workers" }
	}
}

#Root = country
#This = pop_group
can_generate_military_leader_from_pop_group = {
	can_be_leader = yes
	can_be_military_leader = yes
}

#Root = species
#This = colony
can_generate_army_from_colony = {
	NOT = {
		is_planet_class = pc_cosmogenesis_world
	}
	any_owned_species = {
		is_same_species = root
		can_be_soldier = yes
	}
}

#Root = planet
#This = country
can_release_vassal = {
	custom_tooltip = {
		fail_text = default
		NOT = { has_valid_civic = civic_fanatic_purifiers }
	}
	custom_tooltip = {
		fail_text = default
		NOT = { has_valid_civic = civic_machine_terminator }
	}
	custom_tooltip = {
		fail_text = default
		NOT = { has_valid_civic = civic_machine_assimilator }
	}
	custom_tooltip = {
		fail_text = default
		NOT = { has_valid_civic = civic_hive_devouring_swarm }
	}
	custom_tooltip = {
		fail_text = default
		is_genocidal_infernal = no
	}
}

#Root = species
#This = country
can_release_vassal_from_species = {
	root = {
		is_sapient = yes
		NOR = {
			AND = {
				has_trait = trait_machine_unit
				prev.this = { is_individual_machine = no }
			}
			AND = {
				has_trait = trait_hive_mind
				NOT = { prev.this = { has_origin = origin_progenitor_hive } }
			}
			AND = {
				NOT = { has_trait = trait_hive_mind }
				prev.this = { is_hive_empire = yes }
			}
			AND = {
				has_trait = trait_mechanical
				prev.this = {
					NOR = {
						is_mechanical_empire = yes
						has_technology = tech_synthetic_leaders
					}
				}
			}
			can_think = no
			has_trait = trait_clone_soldier_infertile
			has_trait = trait_clone_soldier_infertile_full_potential
		}
	}
}

# Root = species
# This = country
can_generate_army_from_species = {
	root = {
		NOT = { has_trait = trait_tankbound }
		OR = {
			has_military_service_type = { type = military_service_full country = prev }
			has_military_service_type = { type = military_service_limited country = prev }
			AND = {
				exists = prev
				prev = {
					is_primitive = yes
				}
			}
		}
	}
}

# Root = country
# This = species
can_modify_species = {
	# Can't modify root Species during Ascension Situations
	if = {
		limit = {
			is_same_species = ROOT
		}
		custom_tooltip = {
			fail_text = "ASCENSION_SITUATION_ONGOING"

			ROOT = {
				NOT = {
					any_situation = {
						has_ascension_situation_ongoing = yes
					}
				}
			}
		}
	}
	# Organic Species
	if = {
		limit = {
			OR = {
				is_archetype = BIOLOGICAL
				is_archetype = LITHOID
			}
			NOR = {
				has_trait = trait_clone_soldier_infertile
				has_trait = trait_clone_soldier_infertile_full_potential
			}
		}
		custom_tooltip = {
			fail_text = "NEEDS_GENE_TAILORING_OR_INTERGRATED_ANATOMY"
			ROOT = {
				OR = {
					has_technology = tech_gene_tailoring
					has_active_tradition = tr_cybernetics_integrated_anatomy
					has_active_tradition = tr_cybernetics_assimilator_integrated_anatomy
				}
			}
		}
	}
	# Clone Army
	if = {
		limit = {
			OR = {
				has_trait = trait_clone_soldier_infertile
				has_trait = trait_clone_soldier_infertile_full_potential
			}
		}
		custom_tooltip = {
			fail_text = "NEEDS_GENE_TAILORING_AND_CLONE_SYNTHESIZATION"
			ROOT = {
				has_technology = tech_gene_tailoring
				has_active_tradition = tr_cloning_somatic_synthesization_clone_army
			}
		}
	}
	# Robots & Machines
	else_if = {
		limit = {
			OR = {
				is_archetype = ROBOT
				is_archetype = MACHINE
			}
		}
		if = {
			limit = {
				ROOT = {
					OR = {
						AND = { ## organic trying to robomod
							is_individual_machine = no
							is_machine_empire = no
						}
						AND = { ## Machines with origins that delay them
							OR = {
								is_individual_machine = yes
								is_machine_empire = yes
							}
							OR  = {
								has_origin = origin_life_seeded
								has_origin = origin_ocean_machines
								has_origin = origin_void_machines
								has_origin = origin_shattered_ring
							}
						}
					}
				}
			}
			custom_tooltip = {
				fail_text = "NEEDS_ROBOT_MODDING"

				ROOT = {
					OR = {
						has_technology = tech_robomodding
						has_technology = tech_robomodding_m
					}
				}
			}
		}
	}
	# Self-Modified
	custom_tooltip = {
		fail_text = "CANNOT_MODIFY_SELF_MODIFIED_SPECIES"
		AND = {
			NOT = {
				has_trait = trait_self_modified
			}
			#is_same_species = root
		}
	}
	# Malleable Genes
	custom_tooltip = {
		fail_text = "CANNOT_MODIFY_MALLEABLE_GENES_SPECIES"
		NOT = {
			has_trait = trait_malleable_genes
		}
	}
}

# Root = country
# This = species
can_uplift_species = {
	custom_tooltip = {
		fail_text = "CANNOT_UPLIFT_NASCENT"
		NOT = {
			has_trait = trait_nascent_stage
		}
	}
}

#This = country, suject
#Root = country, overlord
#From = target solar system
#This rule is a condition for when subjects that normally cannot colonize outside their borders are allowed to
subject_can_expand = {
	root = { has_valid_civic = civic_feudal_realm }
	NAND = {
		is_ai = yes
		AND = {
			exists = overlord
			overlord = { has_origin = origin_imperial_vassal_overlord }
		}
	}
}

#Root = country, attacker
#This = country, target
#This rule is a condition for taking hostile actions towards neutral targets. Such as landing troops or attacking their fleets
can_take_hostile_actions = {
	if = {
		limit = {
			ROOT = {
				NOT = { has_policy_flag = first_contact_attack_allowed }
			}
			NOT = {
				is_hostile = ROOT
				has_communications = ROOT
			}
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_FIRST_CONTACT_PROTOCOL"
			always = no
		}
	}
	else_if = {
		limit = {
			has_country_flag = fotd_seperatist_country@ROOT
			ROOT = {
				has_origin = origin_fear_of_the_dark
			}
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_FOTD"
			always = no
		}
	}
	else_if = {
		limit = {
			AND = {
				NOT = { has_country_flag = hostile_to_nomads }
				ROOT = {
					is_country_type = nomad
				}
			}
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_FIRST_CONTACT_PROTOCOL"
			always = no
		}
	}
	else_if = {
		#nomads have no borders, so require the target pre-FTL world to be surveyed in place of the in-borders requirement
		limit = {
			ROOT = {
				is_nomadic = yes
			}
			is_primitive = yes
			capital_scope = {
				NOT = {
					is_surveyed = {
						who = ROOT
						status = yes
					}
				}
			}
		}
		custom_tooltip = {
			text = "arkship_target_not_surveyed_fail_tt"
			always = no
		}
	}
	else_if = {
		#attacking primitives with non-interference
		limit = {
			ROOT = {
				is_country_type = default
				NOT = {
					has_policy_flag = interference_aggressive
				}
			}
			is_primitive = yes
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_NATIVE_INTERFERENCE"
			always = no
		}
	}
	else_if = {
		limit = {
			ROOT = { any_agreement = { agreement_preset = preset_satrapy } }
			is_country_type = faction
			root = {
				any_owned_planet = {
					exists = controller
					controller = {
						is_same_value = prevprevprev
					}
				}
			}
		}
		custom_tooltip = {
			text = ""
			always = yes
		}
	}
	else_if = {
		limit = {
			exists = overlord
			ROOT = { any_agreement = { agreement_preset = preset_satrapy } }
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_SATRAPY"
			always = no
		}
	}
	else_if = {
		limit = {
			exists = overlord
			overlord = { is_same_value = root }
			ROOT = { any_agreement = { agreement_preset = preset_satrapy } }
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_OVERLORD"
			always = no
		}
	}
	else = {
		always = yes
	}
}

#Root = country, attacker
#This = country, target
#This rule is a condition for declaring war
can_declare_war = {
	custom_tooltip = {
		fail_text = "ACTION_DECLARE_WAR_OVERLORD_ON_SUBJECT"
		NOT = {
			has_overlord = ROOT
		}
	}
	if = {
		limit = {
			exists = root
			ROOT = {
				has_policy_flag = no_wars
				NOR = {
					has_casus_belli = {
						target = prev
						type = cb_stop_dyson_gun
					}
					has_casus_belli = {
						target = prev
						type = cb_expel_nomads
					}
				}
				OR = {
					is_subject = no
					overlord = {
						is_same_value = root
					}
				}
			}
		}
		custom_tooltip = {
			text = "CAN_DECLARE_WAR_POLICY"
			always = no
		}
	}
	if = {
		limit = {
			exists = root
			ROOT = {
				is_ai = yes
			}
		}
		ROOT = {
			is_at_war = no
		}
	}
}

#Root = country
#This = planet
can_colonize_planet = {
	hidden_trigger = {
		exists = root
	}

	can_colonize_planet_trigger = {
		SCOPE = root
	}

	custom_tooltip = {
		fail_text = "BLANK_STRING"
		if = {
			limit = {
				exists = root
				root = {
					is_country_type = swarm
				}
			}
			is_artificial = no
		}
	}
}

#Root = country
#This = planet
can_colonize_outside_borders = {
	ROOT? = {
		is_nomadic = yes
	}
}

#this = country
has_borders = {
	has_borders_trigger = yes
}

#Root = country
#This = planet
# NOTE: Any time you update this game rule, update the nomad_can_settle_trigger trigger as well
can_nomad_settle = {
	hidden_trigger = {
		root = {
			is_nomadic = yes
		}
	}
	custom_tooltip = {
		fail_text = "SETTLE_BLOCKED_BY_CONSECRATION"
		NOT = {
			has_modifier = consecrated_world
		}
	}

	custom_tooltip = {
		fail_text = "SETTLE_BLOCKED_BY_ORIGIN"
		root = {
			NOR = {
				has_origin = origin_sacred_path
				has_origin = origin_heirs_of_the_khan
				AND = {
					has_origin = origin_forever_cruise
					NOT = {
						has_country_flag = forever_cruise_planetfall
					}
				}
			}
		}
	}

	if = {
		limit = {
			root = {
				has_origin = origin_toxic_knights
			}
		}
		custom_tooltip = {
			fail_text = "SETTLE_BLOCKED_BY_KNIGHTS_QUEST"
			root = {
				OR = {
					has_country_flag = visited_toxic_god_system
					has_country_flag = toxic_knights_destroyed_keep
				}
			}
		}
	}

	custom_tooltip = {
		fail_text = "SETTLE_MISSING_TRADITION"
		if = {
			limit = {
				root = {
					NOR = {
						has_origin = origin_forever_cruise
						has_origin = origin_sacred_path
						has_origin = origin_heirs_of_the_khan
						AND = {
							has_origin = origin_toxic_knights
							has_country_flag = visited_toxic_god_system
						}
					}
				}
			}
			root = {
				OR = {
					has_tradition = tr_adaptability_finish
					has_tradition = tr_versatility_finish
				}
			}
		}
	}
}

#Root = country
#This = planet
can_terraform_planet = {
	custom_tooltip = {
		fail_text = terraform_fail_consecrated
		NOT = {
			has_modifier = consecrated_world
		}
	}

	custom_tooltip = {
		fail_text = terraform_fail_no_presapient_protection_or_no_presapients
		NAND = {
			AND = {
				exists = root
				root = {
					has_policy_flag = pre_sapients_protect
				}
			}
			any_owned_species = {
				is_sapient = no
				NOT = { has_trait = trait_nascent_stage }
			}
		}
	}
	custom_tooltip = {
		fail_text = "requires_actor_not_devouring_swarm_lithoid"
		exists = root
		root = { is_lithoid_devouring_swarm = no }
	}
	custom_tooltip = {
		fail_text = "legendary_leader_planet_no_terraform"
		exists = root
		if = {
			limit = {
				root = { is_wilderness_empire = no }
			}
			this = {
				NOT = { has_planet_flag = legendary_leader_planet }
			}
		}
	}

	custom_tooltip = {
		fail_text = requires_not_relentless_industrialists_situation
		NOT = {
			any_targeting_situation = {
				is_situation_type = relentless_industrialists_situation
			}
		}
	}

	custom_tooltip = {
		fail_text = terraform_fail_is_metal_planet
		NOT = {
			has_modifier = metal_planet
		}
	}

	custom_tooltip = {
		fail_text = terraform_fail_knights_detox_underway
		NOT = {
			has_planet_flag = knights_detox_underway
		}
	}

	custom_tooltip = {
		fail_text = COLONIZATION_IMPOSSIBLE_DEFENDED_PLANET
		NOT = { has_planet_flag = planet_defended_forbid_colonization }
	}

	custom_tooltip = {
		fail_text = "COLONIZATION_IMPOSSIBLE_SMEEGIBB_PLANET"
		NOT = { has_planet_flag = cannot_colonize_smeegibb_shelter }
	}

	custom_tooltip = {
		fail_text = "COLONIZATION_IMPOSSIBLE_WILDERNESS_TECH"
		OR = {
			NAND = {
				owner? = {
					is_wilderness_empire = yes
					NOT = { has_technology = tech_terrestrial_sculpting }
				}
				OR = {
					AND = {
						root = { has_country_flag = started_wet }
						is_wet = no
					}
					AND = {
						root = { has_country_flag = started_dry }
						is_dry = no
					}
					AND = {
						root = { has_country_flag = started_cold }
						is_cold = no
					}
				}
			}
			is_planet_class = pc_gaia
		}
	}
}

#Root = country
#This = species
can_colonize_with_species = {
	has_colonization_control = {
		type = colonization_control_no
		country = root
	}
	OR = {
		NOT = { has_trait = trait_self_modified }
		is_same_species = root
	}
	NOT = {
		has_trait = trait_pathogenic_genes
	}
	if = {
		limit = {
			OR = {
				is_sapient = no
				has_trait = trait_zombie
			}
		}
		custom_tooltip = {
			always = no
		}
	}
	else_if = {
		limit = { has_trait = trait_hive_mind }
		root = { is_hive_empire = yes }
	}
	else = {
		always = yes
	}
}

# Root = Pop Group
# From = Country
# This = Species
# NOTE: species_has_happiness is also checked for this
pop_group_has_random_ethics = {
	exists = from
	NOT = {
		has_citizenship_type = {
			country = from
			type = citizenship_organic_trophy
		}
	}
}

# Root = Pop Group
# From = Country
# This = Species
species_has_happiness = {
	NOR = {
		AND = {
			has_trait = trait_machine_unit
			exists = from
			from = { is_machine_empire = yes }
		}
		has_trait = trait_hive_mind
		can_think = no
		AND = {
			has_trait = trait_mechanical
			OR = {
				NOT = { exists = from }
				from = { is_machine_empire = yes }
				from = { NOT = { has_technology = tech_synthetic_workers } }
				from = { has_policy_flag = ai_outlawed }
			}
		}
		AND = {
			has_trait = trait_cybernetic
			exists = from
			from = {
				OR = {
					AND = {
						is_machine_empire = yes
						has_civic = civic_machine_assimilator # Assimilator empire
					}
					AND = {
						is_democratic_authority = yes
						has_country_flag = cyber_collectivist
					}
				}
			}
		}
	}
}

# Checks if a species can procreate
# This = Species
# Root = Colony
can_species_procreate = {
	species_can_grow_on_colony = {
		COLONY = root
	}
}

# Checks if a species can be assembled
# This = Species
# Root = Colony
can_species_be_assembled = {
	is_robotic = yes
	is_sapient = yes
	NOT = {
		has_trait = trait_exd
	}
	NAND = {
		has_trait = trait_mechanical
		root.owner = {
			is_individual_machine = yes
		}
	}
	NAND = {
		has_virtual_species_trait = yes
		root.owner = {
			is_fallen_empire = no
			has_active_tradition = tr_virtuality_finish
		}
	}
	NOT = {
		is_being_integrated_by = root.owner
	}
	has_population_control = {
		type = no
		country = root.owner
	}
}

# Checks if a species uses bonus_growth modifier
# This = Species
# Root = Colony
species_uses_bonus_growth = {
	can_use_bonus_pop_growth = {
		COLONY = root
	}
}

#Root = country
#From = species
founder_species_uses_logistic_growth_mult = {
	exists = from
	from = {
		is_robotic_species = no
		is_infertile = no
	}
}

founder_species_uses_assembly = {
	exists = from
	from = {
		is_robotic_species = yes
	}
}

founder_species_uses_bonus_growth_mult = {
	exists = from
	from = {
		is_robotic_species = no
		is_infertile = yes
	}
}

#This rule is a condition for bombarding planets
#Root = fleet, potential attacker
#From = planet, potential target
can_orbital_bombard = {
	exists = FROM.OWNER
	NOT = {
		any_controlled_ship = {
			is_ship_size = colossus
		}
	}
	OR = {
		AND = {
			ROOT.CONTROLLER = {
				NOT = { has_communications = FROM.OWNER }
			}
			FROM.OWNER = {
				NOT = { is_country_type = primitive }
			}
		}
		AND = {
			ROOT.CONTROLLER = {
				NOR = {
					has_policy_flag = interference_not_allowed
					has_policy_flag = interference_subtle
				}
			}
			FROM.OWNER = { is_country_type = primitive }
		}
		ROOT.CONTROLLER = { is_at_war_with = FROM.CONTROLLER }
		FROM = { is_planet_class = pc_infested }
		FROM.CONTROLLER = {
			OR = {
				is_country_type = swarm
				is_country_type = ai_empire
				is_country_type = cybrex_empire
				is_country_type = abandoned_hatchery
				AND = {
					OR = {
						is_country_type = awakened_marauders
						is_country_type = mechanocalibrator
					}
					ROOT.CONTROLLER = { is_hostile = prev }
				}
			}
		}
		ROOT.CONTROLLER = {
			OR = {
				is_country_type = extradimensional
				is_country_type = extradimensional_2
				is_country_type = extradimensional_3
				is_country_type = swarm
				is_country_type = ai_empire
				is_country_type = cybrex_empire
				is_country_type = dormant_marauders
				is_country_type = marauder_raiders
				is_country_type = ruined_marauders
				is_country_type = debt_collectors
				is_country_type = gray_goo
				is_country_type = vol
				is_country_type = behemoth_hatchling
				is_country_type = abandoned_hatchery
				AND = {
					is_country_type = frenzied_voidworms
					has_country_flag = can_bombard
					FROM.CONTROLLER = { has_country_flag = tormented_by_frenzied_voidworms }
				}
				AND = {
					OR = {
						is_country_type = awakened_marauders
						is_country_type = formless
					}
					FROM.CONTROLLER = { is_hostile = prev }
				}
				AND = {
					is_voidworms_hostile_country_type = yes
					ROOT = {
						is_ship_size = voidworms_titan
					}
				}
				has_country_flag = revengeful_patron
			}
		}
		AND = {
			from.controller = {
				is_country_type = faction
				NOT = { is_same_value = ROOT.CONTROLLER }
			}
			from.owner = {
				is_same_value = ROOT.CONTROLLER
			}
		}
	}
}

#Check if it is possible to build orbital stations on this planet
#this: planet
#root: country
can_planet_support_orbital_station = {
	custom_tooltip = {
		fail_text = "ORBITAL_STATION_BLOCKED_BY_MEGASTRUCTURE"
		has_any_megastructure = no
		NOT = { has_planet_flag = has_megastructure }
	}
}

# Check if full sensor range in entire galaxy
# root: country
has_full_sensor_range = {
	OR = {
		has_megastructure = spy_orb_4
		has_megastructure = spy_orb_restored
	}
}

# Determines if notification messages should be sent after a ground combat
# this: colony
# from: winner
# root: loser
show_notification_for_ground_combat = {
	planet = { NOT = { has_planet_flag = slave_revolt } }
}

# This = country
has_planet_specific_war_attack_priority = {
	has_ascension_perk = ap_galactic_hyperthermia
}

# This = planet
calc_planet_specific_war_attack_priority = {
	base = 1
	modifier = {
		OR = {
			is_planet_class = pc_volcanic
			is_planet_class = pc_m_giant_star
		}
		add = 3
	}
}

# Root = country
# This = system
calc_system_specific_war_attack_priority = {
	base = 1
	modifier = {
		AND = {
			has_star_flag = hyperthermia_system
			root = { has_ascension_perk = ap_galactic_hyperthermia }
		}
		add = 9
	}
	modifier = {
		AND = {
			has_star_flag = hyperthermia_system
			root = { is_at_war_with = event_target:hyperthermia_empire }
		}
		add = 9
	}
	modifier = {
		has_star_flag = nomad_aetherophasic_anchor_system
		add = 9
	}
}

# Determines if the "per_neighbor_rival" static modifier should be applied to an empire
# this: country
should_apply_neighbor_rival_modifier = {
	has_tradition = tr_enmity_adopt
}

#Root: Rebelling against country
#This: Rebel country
are_rebels_hostile = {
	is_country_type = rebel	#Rebels change country type after taking their initial planet (action.30)
}

#Root: planet
should_colony_be_destroyed_on_pc_change = {
	OR = {
		is_planet_class = pc_infested
		is_planet_class = pc_ai
	}
}

# Is the given fleet the portal for given extradimensional country
# root: country (ED)
# this: fleet
is_extradimensional_portal = { # is this portal mine
	is_ship_size = dimensional_portal_ed
	OR = {
		owner = { is_same_value = root } # owner is me
		OR = {
			AND = { # portal is unbidden and I am unbidden
				root = { is_country_type = extradimensional }
				any_controlled_ship = { has_ship_flag = unbidden_portal }
			}
			AND = { # portal is aberrant and I am aberrant
				root = { is_country_type = extradimensional_2 }
				any_controlled_ship = { has_ship_flag = aberrant_portal }
			}
			AND = { # portal is vehement and I am vehement
				root = { is_country_type = extradimensional_3 }
				any_controlled_ship = { has_ship_flag = vehement_portal }
			}
		}
	}
}

# Is the given fleet an anchor for given extradimensional country
# root: country (ED)
# this: fleet
is_extradimensional_anchor = {
	root = {
		OR = {
			is_country_type = extradimensional
			is_country_type = extradimensional_2
			is_country_type = extradimensional_3
			is_country_type = formless
		}
	}
	any_controlled_ship = { is_ship_size = starbase_exd }
	owner = { is_same_value = root }
}

# Will colonizing this planet tick off a fallen empire?
# Root = fallen empire
# This = planet
will_anger_fallen_empire = {
	OR = {
		AND = {
			root = { has_ai_personality_behaviour = demands_clear_borders }
			is_neighbor_of = root
		}
		AND = {
			root = { has_ai_personality_behaviour = holy_planets }
			has_modifier = "holy_planet"
			space_owner? = {
				NOT = { has_relic = r_severed_head }
			}
		}
	}
}

# Can the country have robot pop
# root: country
can_have_robot_pops = {
	exists = this
	is_hive_empire = no
	NOT = { has_policy_flag = robots_outlawed }
}

# THIS = Fleet
fleet_uses_templates = {
	is_ship_class = shipclass_military
	is_designable = yes
	is_alliance_fleet = no
}

# this = starbase, megastructure or planet
can_orbitable_repair_ships = {
	OR = {
		AND = {
			is_scope_type = starbase
			OR = {
				has_starbase_size >= starbase_starport
				has_starbase_size >= starbase_deep_space_citadel_1
				has_starbase_size = juggernaut
				has_starbase_size = enclave_mercenary_station
				is_arkship_starbase = yes
				has_starbase_module = waystation_repair_bay
			}
			fleet = {
				is_disabled = no
			}
		}
		AND = {
			is_scope_type = megastructure
			OR = {
				is_megastructure_type = mega_shipyard_1
				is_megastructure_type = mega_shipyard_2
				is_megastructure_type = mega_shipyard_3
			}
		}
	}
}

# this = ship
# root = country that kills ship
can_spawn_debris = {
	root = {
		is_country_type = default
		NOT = { has_policy_flag = debris_destruction }
	}
	exists = owner
	owner = { NOT = { has_ascension_perk = ap_enigmatic_engineering } }
}

# this = system
# root = country
# Determines if AI-controlled empires should restrict (and thus avoid) a system
# This game rule takes priority over ai_should_not_restrict_system below
# Used by regular empires, fallen empires & crises
ai_should_restrict_system = {
	OR = {
		AND = {
			root = {
				is_country_type = fallen_empire
			}
			exists = owner
			owner = { is_country_type = awakened_marauders }
		}
		AND = {
			root = { is_country_type = awakened_marauders }
			exists = owner
			owner = { is_country_type = fallen_empire }
		}
		AND = {
			root = { is_country_type = voidworms }
			exists = owner
			owner = {
				OR = {
					is_country_type = dormant_marauders
					is_country_type = awakened_marauders
				}
			}
		}
		AND = {
			root = {
				OR = {
					is_country_type = fallen_empire
					is_country_type = awakened_marauders
					is_country_type = voidworms
				}
			}
			has_star_flag = guardian
			any_fleet_in_system = {
				exists = owner
				owner = {
					is_guardian_country = yes
				}
			}
		}
		AND = {
			root = {
				OR = {
					is_crisis_faction = yes
					is_country_type = awakened_fallen_empire
				}
			}
			has_star_flag = guardian
			any_fleet_in_system = {
				exists = owner
				owner = {
					is_country_type = guardian_fortress
				}
			}
		}
	}
}

# this = system
# root = country
# Determines if AI-controlled empires should never restrict (and thus avoid) a system
# Used by regular empires, fallen empires & crises
ai_should_not_restrict_system = {
	AND = {
		has_star_flag = starlit_sealed_system
		root = {
			has_origin = origin_starlit_citadel
		}
	}
}

# this = country
can_subjugate_empires = {
	always = yes
}

# this = leader
can_leader_get_council_trait = {
	# See 03_scripted_triggers_paragon.txt
	can_leader_get_council_trait_trigger = yes
}

# this = leader
can_leader_get_normal_trait = {
	can_leader_get_normal_trait_trigger = yes
}

# this = leader
can_leader_get_subclass_trait = {
	NAND = {
		exists = owner
		owner = { is_gestalt = yes }
		OR = {
			is_councilor = yes
			has_leader_flag = restored_node
		}
	}
	has_any_subclass = no
}

# this = leader
can_leader_get_destiny_trait = {
	NAND = {
		exists = owner
		owner = { is_gestalt = yes }
	}
	exists = owner
	owner = {
		is_crisis_faction = no
	}
}

# this = country
has_extra_council_position = {
	country_has_extra_council_position = yes
}

# THIS = Fleet
can_jump_drive = {
	# Cooldown handled by code
	is_system_locked = no
}

# this = country
can_spawn_occupation_armies = {
	OR = {
		is_country_type = default
		is_country_type = fallen_empire
		is_country_type = awakened_fallen_empire
	}
}

# this = system
# root = country
system_blocks_sensors = {
	OR = {
		is_inside_nebula = yes
		has_modifier = space_storm
		has_modifier = zroni_storm
		has_modifier = queen_scorn_storm
		has_star_flag = lcluster
		has_star_flag = sealed_system
		has_star_flag = surveillance_supercomputer_system
		has_star_flag = storm_system
	}
}

# Root = country attempting to target system for movement
# This = system being targeted
can_enter_system_by_jump = {
	can_enter_system_by_jump_trigger = {
		SCOPE = root
	}
}

# This = pop_group
can_resettle_pop_group = {
	# Self-modified Pops won't leave
	custom_tooltip = {
		fail_text = "self_modified_refuse"
		NOT = { has_trait = trait_self_modified }
	}
	# Pathogenic Pops Can't Resettle
	custom_tooltip = {
		fail_text = "pathogenic_genes_warning"
		NOT = { has_trait = trait_pathogenic_genes }
	}
	custom_tooltip = {
		fail_text = "origin_egalitarian_refuse"
		NAND = {
			has_ethic = ethic_egalitarian
			is_enslaved = no
			planet = { has_modifier = paragon_origin_reformists }
		}
	}
	custom_tooltip = {
		fail_text = "no_resettling_apps"
		if = {
			limit = {
				has_virtual_species_trait = yes
			}
			exists = owner
			owner = {
				is_fallen_empire = no
				NOT = {
					has_active_tradition = tr_virtuality_adopt
				}
			}
		}
	}
	if = { #Done as an IF/ELSE so the tooltip isn't hideous and unreadable
		limit = { is_shackled_robot = yes }
		always = yes
	}
	else_if = {
		limit = {
			planet = {
				is_doomsday_planet = yes
			}
		}
		always = yes
	}
	else_if = {
		limit = {
			planet = {
				has_modifier = planet_culture_shock
			}
		}
		OR = {
			is_same_species = planet.owner
			planet = {
				NOT = { has_modifier = planet_culture_shock }
			}
		}
	}
	else_if = {
		limit = {
			planet = {
				is_capital = yes
				pop_amount < 200
			}
			NOT = {
				owner = {
					any_owned_colony = {
						is_capital = no
						NOT = {
							planet = {
								is_planet_class = pc_cosmogenesis_world
							}
						}
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "no_abandon_capital_for_unviable_planet"
			always = no
		}
	}
	else_if = {
		limit = {
			planet = {
				is_planet_class = pc_cosmogenesis_world
				owner = {
					has_ascension_perk = ap_cosmogenesis
				}
				pop_amount < 200
			}
		}
		custom_tooltip = {
			fail_text = "no_unfueled_lathe"
			always = no
		}
	}
	else_if = {
		limit = {
			planet = {
				has_deposit = d_genesis_preserve
				sapient_pop_amount < 200
			}
			owner = {
				is_guided_sapience_empire = yes
			}
		}
		custom_tooltip = {
			fail_text = "no_abandon_genesis_preserve"
			always = no
		}
	}
	else_if = {
		limit = {
			owner = {
				has_ascension_perk = ap_cosmogenesis
			}
		}
		always = yes
	}
	else_if = {
		limit = {
			owner = {
				is_gestalt = yes
			}
		}
		always = yes
	}
	else = {
		custom_tooltip = {
			fail_text = RESETTLEMENT_POLICY_FAIL
			owner = {
				has_policy_flag = resettlement_allowed
			}
		}
	}
}

# This = Country
can_country_resettle = {
	OR = {
		has_policy_flag = resettlement_allowed
		is_gestalt = yes
		has_ascension_perk = ap_cosmogenesis
		custom_tooltip = {
			text = owns_non_sapient_robots
			country_has_shackled_robots = yes
		}
		hidden_trigger = { #don't need to show this as tooltip only shown when all game rule fails, and this is a special case
			any_owned_planet = {
				is_doomsday_planet = yes
			}
		}
	}
}

# This = Colony
can_resettle_colony = {
	is_controlled_by = owner
	has_ground_combat = no
	has_orbital_bombardment = no
	custom_tooltip = {
		text = "RESETTLE_PLANET_UNDER_COLONIZATION"
		exists = this
		is_under_colonization = no
	}
}

# This = Carrier
should_spawn_deposits = {
	is_artificial = no
}

# This = Colony
can_get_colony_events = {
	NOT = {
		is_planet_class = "pc_cosmogenesis_world"
	}
}

# This = Colony
is_new_colony = {
	is_colony = yes
	is_under_colonization = no
	NOT = {
		has_planet_flag = synth_queen_bastille
	}
	colony_age < 180
	pop_amount < 1000
}

# This = pop_group
pop_group_generates_crime = {
	is_sapient = yes
	is_non_sapient_robot = no
	is_being_assimilated = no
	# Cryo-Sleepers (Deep Sleep / Alternating Torpor / Standby Protocols civics) are dormant and produce no crime/deviancy
	NOT = { is_pop_category = deep_sleep }
}

# This = colony
can_pops_grow_on_colony = {
	NOR = {
		has_modifier = planet_population_control
		has_modifier = planet_population_control_gestalt
		is_planet_class = pc_cosmogenesis_world
		is_planet_class = pc_nanotech
		AND = { # conquered by crisis
			exists = controller
			controller = {
				OR = {
					is_country_type = ai_empire
					is_country_type = swarm
				}
			}
		}
	}
}

# This = colony
can_pops_assemble_on_colony = {
	NOR = {
		has_modifier = planet_robot_assembly_control
		has_modifier = planet_population_control_gestalt
		is_planet_class = pc_cosmogenesis_world
		AND = { # conquered by crisis
			exists = controller
			controller = {
				OR = {
					is_country_type = ai_empire
					is_country_type = swarm
				}
			}
		}
	}
}

# This = colony
can_pops_decline_on_colony = {
	always = yes
}

## If this returns true, the species will be set to decline even if it is not being purged
# This = species
# From = colony
## Note: success text tooltips are shown in alert_pops_declining alert.
should_force_decline_species = {
	OR = {
		# Infertile Clone Army pops should decline if in Country that can't have Vats, or there is a Vat shortage.
		custom_tooltip = {
			success_text = CLONE_ARMY_FORCE_DECLINE
			exists = this
			OR = {
				has_trait = trait_clone_soldier_infertile
				has_trait = trait_clone_soldier_infertile_full_potential
			}
			from = {
				OR = {
					# Check if country is unable to build Vats for this species.
					AND = {
						exists = owner
						owner = {
							OR = {
								NOT = { has_origin = origin_clone_army }					# Can't have Vats.
								has_country_flag = clone_army_fertility_unlocked			# Can't have Vats anymore.
								NOT = { owner_main_species = { is_same_species = root } }	# Our Vats can't build this species.
							}
						}
					}
					# Vats check out, but are there more pops than Vats support?
					check_variable = { which = clone_pops_missing value < 0 }
				}
			}
		}

		from = {
			custom_tooltip = {
				success_text = VOIDSPAWN_FORCE_DECLINE
				exists = this
				has_modifier = voidspawn_colony_mod
			}
		}
	}
}

# This = country
can_build_branch_offices = {
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_MEGACORP
		OR = {
			is_megacorp = yes
			has_civic = civic_galactic_sovereign_megacorp
		}
	}
}

# This = country
can_support_branch_offices = {
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_FALLEN_EMPIRES
		NOR = {
			is_country_type = fallen_empire
			is_country_type = awakened_fallen_empire
		}
	}
	#custom_tooltip = {
	#	fail_text = BRANCH_OFFICE_NOT_ON_MEGACORP_PLANET
	#	NOR = {
	#		is_megacorp = yes
	#		has_civic = civic_galactic_sovereign_megacorp
	#	}
	#}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_REGULAR_EMPIRE
		is_gestalt = no
		OR = {
			is_country_type = default
			is_country_type = primitive
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_ADVANCED_PRE_FTL
		if = {
			limit = {
				is_primitive = yes
				is_gestalt = no
			}
			OR = {
				has_pre_ftl_age = atomic_age
				has_pre_ftl_age = early_space_age
			}
		}
	}
}

# This = colony
# Root = country
can_build_branch_office_on_colony = {
	custom_tooltip = {
		fail_text = requires_recipient_not_fanatic_purifiers
		exists = owner
		owner = {
			OR = {
				is_same_species = root
				NOT = { has_civic = civic_fanatic_purifiers }
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NO_OBSERVATION_OUTPOST
		if = {
			limit = {
				root = { is_criminal_syndicate = yes }
				owner = { is_primitive = yes }
			}
			has_observation_outpost = yes
			observation_outpost_owner = {
				is_same_value = root
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_COMMERCIAL_PACT
		exists = owner
		OR = {
			root = { is_criminal_syndicate = yes }
			owner = { has_commercial_pact = root }
			owner = { is_in_federation_with = root }
			AND = {
				has_observation_outpost = yes
				observation_outpost = {
					has_modifier = preftl_action_trade_pact
				}
			}

			# allow branch offices in subsidiaries and vice versa (and fellow subjects)
			root = {
				is_subject = yes
				OR = {
					is_megacorp = yes
					has_civic = civic_galactic_sovereign_megacorp
				}
				overlord = {
					OR = {
						is_same_value = prevprev.owner
						is_same_value = prevprev.owner.overlord
					}
				}
			}
			owner = {
				is_subject = yes
				overlord = {
					OR = {
						is_same_value = root
						is_same_value = root.overlord
					}
					OR = {
						is_megacorp = yes
						has_civic = civic_galactic_sovereign_megacorp
					}
				}
			}
			AND = {
				root = { has_civic = civic_galactic_sovereign_megacorp }
				owner = { is_galactic_community_member = yes }
			}
			AND = {
				root = { has_modifier = imperial_charter }
				owner = { is_galactic_community_member = yes }
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_MEGACORP_OVERLORD
		exists = owner
		# failure message for having a corporate overlord

		if = {
			limit = {
				OR = {
					# Not valid if you aren't a subject.
					root = {
						is_subject = no
					}
					# Or if your overlord isn't a megacorp.
					root = {
						is_subject = yes
						overlord = {
							NOR = {
								is_megacorp = yes
								has_civic = civic_galactic_sovereign_megacorp
							}
						}
					}
				}
			}
			always = yes
		}
		else = {
			AND = {
				root = {
					is_subject = yes
					overlord = {
						OR = {
							is_megacorp = yes
							has_civic = civic_galactic_sovereign_megacorp
						}
					}
				}
				owner = {
					OR = {
						is_subject = no
						AND = {
							is_subject = yes
							overlord = { NOT = { is_same_value = root.overlord } }
						}
					}
				}
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_CRIMINAL_HERITAGE_HAS_TRUCE
		exists = owner
		OR = {
			root = { is_criminal_syndicate = no }
			owner = {
				NOR = {
					has_truce = root
					is_at_war_with = root
				}
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_CRIMINAL_COOLDOWN
		exists = owner
		OR = {
			root = { is_criminal_syndicate = no }
			AND = {
				root = { is_criminal_syndicate = yes }
				NOT = { has_carrier_flag = criminal_branch_closed_cooldown_planet@root }
			}
		}
	}
	custom_tooltip = {
		fail_text = NO_BRANCH_OFFICE_ON_AI_WORLD
		NOT = { is_planet_class = pc_ai }
	}
	custom_tooltip = {
		fail_text = NO_BRANCH_OFFICE_ON_NANITE_WORLD
		NOT = { is_planet_class = pc_gray_goo }
	}
	custom_tooltip = {
		fail_text = NO_BRANCH_OFFICE_ON_SYNAPTIC_LATHE
		NOT = { is_planet_class = pc_cosmogenesis_world }
	}
	custom_tooltip = {
		fail_text = NO_BRANCH_OFFICE_ON_SELF
		owner = {
			NOT = { is_same_value = root }
		}
	}
}

# This = country
# Checked by is_criminal_syndicate trigger
is_criminal_syndicate_rule = {
	OR = {
		is_megacorp = yes
		has_valid_civic = civic_galactic_sovereign_megacorp
	}
	has_valid_civic = civic_criminal_heritage
}

# This = colony
can_crossbreed_species_on_colony = {
	exists = owner
	owner = { has_ascension_perk = ap_xeno_compatibility }
	count_species = {
		limit = {
			is_crossbreeding_possible = yes
		}
		count >= 2
	}
}

# This = species
can_crossbreed_species = {
	is_crossbreeding_possible = yes
}

# No scope
can_win_timed_victory = {
	custom_tooltip = {
		fail_text = VICTORY_CONDITION_CRISIS_IN_PROGRESS
		NOT = {
			any_country = {
				is_crisis_faction = yes
				NOT = { is_country_type = awakened_marauders }
			}
		}
	}
	custom_tooltip = {
		fail_text = VICTORY_CONDITION_WAR_IN_HEAVEN_IN_PROGRESS
		NOT = { has_global_flag = war_in_heaven_ongoing }
	}
}

# This = country
can_use_galactic_market = {
	is_country_type = default
	NOR = {
		has_civic = civic_fanatic_purifiers
		has_civic = civic_hive_devouring_swarm
		has_civic = civic_machine_terminator
		has_civic = civic_scorched_earth
		has_civic = civic_hive_scorched_earth
	}
}

# ROOT = country
# This = pop_group
can_be_sold_on_slave_market = {
	exists = owner
	OR = {
		is_enslaved = yes
		is_non_sapient_robot = yes
		is_shackled_robot = yes
	}
	OR ={
		ROOT = { is_ai = no }
		has_citizenship_rights = no
		is_on_slave_market = yes	# pops that were already on the market should stay there
		AND = {
			is_unemployed = yes
			planet = {
				num_unemployed > 300
			}
		}
	}
	if = {
		limit = {
			has_global_flag = organic_slave_trade_banned_flag
		}
		is_organic_species = no
	}
	if = {
		limit = {
			has_global_flag = sentient_slave_trade_banned_flag
		}
		is_sapient = no
	}
	NOR = {
		# Since they can't reproduce, they die out too fast for anyone to want to buy them.
		has_trait = trait_clone_soldier_infertile
		has_trait = trait_clone_soldier_infertile_full_potential
		has_virtual_species_trait = yes
	}
}

# This = country
can_be_part_of_galactic_community = {
	custom_tooltip = {
		fail_text = "community_default_failtext"
		OR = {
			is_country_type = default
			AND = {
				is_country_type = awakened_fallen_empire
				has_country_flag = fallen_empire_hive_control
			}
		}
	}
	custom_tooltip = {
		fail_text = "community_homicidal_failtext"
		NOR = {
			has_civic = civic_fanatic_purifiers
			has_civic = civic_hive_devouring_swarm
			has_civic = civic_machine_terminator
			has_menace_perk = menp_behemoth_ever_hungry
			has_civic = civic_scorched_earth
			has_civic = civic_hive_scorched_earth
			has_country_flag = heir_of_the_khan_khan
		}
	}
	custom_tooltip = {
		fail_text = "community_declared_crisis_failtext"
		has_been_declared_crisis = no
		NOT = { has_country_flag = malevolent_aura_empire }
	}
	custom_tooltip = {
		fail_text = "community_declared_crisis_failtext"
		#fail_text = "community_overlord_declared_crisis_failtext"
		OR = {
			NOT = { exists = overlord }
			overlord = {
				has_been_declared_crisis = no
				NOT = { has_country_flag = malevolent_aura_empire }
			}
		}
	}
}

# This = country
can_be_part_of_galactic_empire = {
	custom_tooltip = {
		fail_text = "community_default_failtext"
		OR = {
			is_country_type = default
			AND = {
				is_country_type = awakened_fallen_empire
				has_country_flag = fallen_empire_hive_control
			}
		}
	}
	custom_tooltip = {
		fail_text = "community_homicidal_failtext"
		NOR = {
			has_civic = civic_fanatic_purifiers
			has_civic = civic_hive_devouring_swarm
			has_civic = civic_machine_terminator
			has_menace_perk = menp_behemoth_ever_hungry
			has_civic = civic_scorched_earth
			has_civic = civic_hive_scorched_earth
		}
	}
	custom_tooltip = {
		fail_text = "community_declared_crisis_failtext"
		has_been_declared_crisis = no
	}
	custom_tooltip = {
		fail_text = "community_declared_crisis_failtext"
		#fail_text = "community_overlord_declared_crisis_failtext"
		OR = {
			NOT = { exists = overlord }
			overlord = { has_been_declared_crisis = no }
		}
	}
}

# This = country
can_see_galactic_community = {
	OR = {
		has_country_flag = galcom_form_aye
		has_country_flag = galcom_form_nay
		is_galactic_community_member = yes
	}
}

# This = country
can_be_part_of_galactic_council = {
	OR = {
		is_country_type = default
		has_country_flag = fallen_empire_hive_control
	}
	NOR = {
		has_civic = civic_fanatic_purifiers
		has_civic = civic_hive_devouring_swarm
		has_civic = civic_machine_terminator
		has_menace_perk = menp_behemoth_ever_hungry
		has_civic = civic_scorched_earth
		has_civic = civic_hive_scorched_earth
	}
}

# If, 'yes', forces an empire to be advanced at game start (overrides game settings)
# This = country
must_be_advanced_empire = {
	OR = {
		has_country_flag = lost_colony_parent
		has_country_flag = MSI
	}
}

# If 'no', blocks an empire from being advanced at game start (overrides 'must_be_advanced_empire' above)
# This = country
is_allowed_to_be_advanced_empire = {
	always = yes
}

# This = colony
has_enough_pops_for_necrophage_on_colony = {
	owner = { has_origin = origin_necrophage }
	has_available_jobs = necro_apprentice
	if = {
		limit = {
			has_carrier_flag = necrophage_alert_timer
		}
		count_owned_species = {
			count = 0
			limit = { species_can_be_necrophaged = yes }
		}
	}
	else = {
		NOT = { any_owned_pop_group = { can_work_specific_job = necro_apprentice } }
	}
}

# Root = current colony
# This = pop_group
# Purge, Sapience, Migration Controls policy (ignored by Gestalts and Synths), and target habitability are checked in code.
can_pop_group_auto_migrate = {
	hidden_trigger = { # This is always true where this tooltip is called
		OR = {
			is_unemployed = yes
			is_pop_category = civilian
			is_pop_category = maintenance_drone
			is_shackled_robot = yes
		}
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_ASSIMILATED
		is_being_assimilated = no
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_ROOTED
		NOT = { has_trait = trait_rooted }
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_VIRTUALITY
		OR = {
			has_virtual_species_trait = no
			AND = {
				has_virtual_species_trait = yes
				exists = owner
				owner = {
					OR = {
						is_individual_machine = no
						is_machine_empire = no
					}
				}
			}
		}
	}
}

# Root = current colony
# This = pop_group
# Checked after can_pop_group_auto_migrate, and only for destinations outside the pop's own empire.
# Pops with no say in where they live do not leave the empire on their own.
can_pop_group_auto_migrate_abroad = {
	hidden_trigger = {
		NOR = {
			is_shackled_robot = yes
			is_robot_servant = yes
			is_enslaved = yes
			is_pop_category = maintenance_drone
			has_citizenship_type = {
				country = owner
				type = citizenship_organic_trophy
			}
		}
	}
}

# This = colony
# Checked before can_pop_auto_migrate to save performance
can_colony_auto_migrate = {
	hidden_trigger = {
		is_colony = yes
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_CULTURE_SHOCK
		NOT = { has_modifier = planet_culture_shock }
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_COLONY_TOO_NEW
		OR = {
			AND = {
				is_under_colonization = no
				colony_age > 60
			}
			is_capital = yes # Don't break Doomsday!
		}
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_ONLY_HAS_ONE_POP
		sapient_pop_amount > 100
	}
	hidden_trigger = {
		exists = owner
		owner = {
			OR = {
				is_country_type = default
				is_country_type = awakened_fallen_empire
			}
		}
	}
}

# This = colony
can_colony_receive_auto_migration = {
	NOR = {
		planet_devastation >= 10
		has_modifier = planet_culture_shock
		is_doomsday_planet = yes
		is_planet_class = pc_cosmogenesis_world
	}
}

#This, root = country
can_gene_mod_habitability = {
	always = yes
	# See can_change_habitability_preference in 05__scripted_triggered_traits.txt
}

#This, root = country
can_add_advanced_traits = {
	OR = {
		has_tradition = tr_genetics_enhanced_expression
		has_active_tradition = tr_purity_exemplary_genetics
		has_active_tradition = tr_cloning_somatic_synthesization
		has_active_tradition = tr_cloning_somatic_synthesization_clone_army
		has_active_tradition = tr_mutation_unnatural_selection
	}
}

#This, root = country
can_add_negative_traits_organic = {
	OR = {
		has_tradition = tr_genetics_resequencing
		has_active_tradition = tr_cybernetics_integrated_anatomy
		has_active_tradition = tr_cybernetics_assimilator_integrated_anatomy
		has_active_tradition = tr_purity_genotype_regeneration
		has_active_tradition = tr_purity_genotype_regeneration_wilderness
		has_active_tradition = tr_mutation_unnatural_selection
	}
}

#This, root = country
can_remove_positive_traits_organic = {
	OR = {
		AND = {
			OR = {
				has_tradition = tr_genetics_resequencing
				has_active_tradition = tr_cybernetics_integrated_anatomy
				has_active_tradition = tr_cybernetics_assimilator_integrated_anatomy
				has_active_tradition = tr_purity_genotype_regeneration
				has_active_tradition = tr_purity_genotype_regeneration_wilderness
				has_active_tradition = tr_cloning_somatic_synthesization
				has_active_tradition = tr_cloning_somatic_synthesization_clone_army
				has_active_tradition = tr_mutation_unnatural_selection
			}
		}
		AND = {
			has_toxoids = yes
			has_origin = origin_overtuned
		}
		has_technology = tech_gene_expressions
		has_technology = tech_epigenetic_triggers
	}
}

#This, root = country
can_modify_organic_portraits = {
	OR = {
		has_tradition = tr_genetics_creation
		has_tradition = tr_purity_exemplary_genetics
		has_active_tradition = tr_cloning_somatic_synthesization
		has_active_tradition = tr_cloning_somatic_synthesization_clone_army
		has_tradition = tr_mutation_unnatural_selection
	}
}

# This = Country establishing contact | From = Target country
# Should the contact logic happen?
can_have_contact = {
	if = {
		limit = {
			NOT = { has_global_flag = game_started }
			OR = {
				has_origin = origin_mindwardens
				from = { has_origin = origin_mindwardens }
			}
		}
		always = no
	}
	else_if = {
		limit = {
			from = { is_country_type = exiled }
			has_origin = origin_mindwardens
			is_same_value = event_target:mindwarden_country@from
		}
		has_country_flag = can_contact_exiled
		NOT = { has_country_flag = exiled_contact }
	}
	else_if = {
		limit = {
			is_country_type = exiled
			from = { has_origin = origin_mindwardens }
			is_same_value = event_target:exiled_country@from
		}
		always = no
	}
	else = {
		always = yes
	}
}

# This = country establishing contact | From = target country
# Final check for whether there should be a first contact site spawned. Main check is via country type 'contact_rule', in code. See also: error-logging/fixing event 'action.240', which duplicates this trigger.
can_have_first_contact = {
	if = {
		limit = { from = { is_amoeba_country_type = yes } }
		NOT = {
			any_active_first_contact = {
				contact_country = { is_amoeba_country_type = yes }
			}
		}
	}
	else_if = {
		limit = { from = { is_tiyanki_country_type = yes } }
		NOT = {
			any_active_first_contact = {
				contact_country = { is_tiyanki_country_type = yes }
			}
		}
	}
	else_if = {
		limit = { from = { is_cutholoids_country_type = yes } }
		NOT = {
			any_active_first_contact = {
				contact_country = { is_cutholoids_country_type = yes }
			}
		}
	}
	else_if = {
		limit = { from = { is_voidworms_country_type = yes } }
		NOT = {
			any_active_first_contact = {
				contact_country = { is_voidworms_country_type = yes }
			}
		}
	}
	else_if = {
		limit = { from = { is_crystal_country_type = yes } }
		NOT = {
			any_active_first_contact = {
				contact_country = {
					is_crystal_country_type = yes
					NOT = { has_country_flag = queued_to_destroy }
				}
			}
		}
	}
	else_if = {
		limit = { from = { is_drone_country_type = yes } }
		NOT = {
			any_active_first_contact = {
				contact_country = {
					is_drone_country_type = yes
					NOT = { has_country_flag = queued_to_destroy }
				}
			}
		}
	}
	else_if = {
		limit = { from = { is_country_type = cloud } }
		NOT = {
			any_active_first_contact = {
				contact_country = { is_country_type = cloud }
			}
		}
	}
	else_if = {
		limit = {
			from = {
				OR = {
					is_country_type = caravaneer_fleet
					is_country_type = caravaneer_home
				}
			}
		}
		NOT = {
			any_active_first_contact = {
				contact_country = {
					OR = {
						is_country_type = caravaneer_fleet
						is_country_type = caravaneer_home
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			from = {
				is_country_type = dragon_dummy
			}
		}
		has_origin = origin_here_be_dragons
		from = { has_country_flag = dragon_of@prev }
	}
	else_if = {
		limit = {
			from = {
				is_country_type = jaunting_traveler_dummy
			}
		}
		from = { has_country_flag = jaunting_traveler_of@prev }
	}
}

# Root = Country with envoys to send
# This = Imperium leader country
can_use_envoys_to_raise_imperial_authority = {
	always = yes
}

# Root = Country with envoys to send
# This = Imperium leader country
can_use_envoys_to_lower_imperial_authority = {
	custom_tooltip = {
		fail_text = cannot_lower_authority_emperor
		root = { is_galactic_emperor = no }
	}
	custom_tooltip = {
		fail_text = cannot_lower_authority_espionage_operation
		root = {
			NOT = { has_modifier = undermining_efforts_stalled }
		}
	}
}

#this/root = pop
#checked if job's possible_precalc = can_fill_drone_job
can_fill_drone_job = {
	custom_tooltip = DRONE_JOB_TRIGGER
	hidden_trigger = {
		exists = owner
		owner = { is_gestalt = yes }
		is_being_purged = no
		is_enslaved = no
		is_being_assimilated = no
		NOR = {
			has_citizenship_type = {
				type = citizenship_organic_trophy
				country = owner
			}
			has_citizenship_type = {
				type = citizenship_cruise_passenger
				country = owner
			}
		}
	}
}

#this/root = pop
#checked if job's possible_precalc = can_fill_worker_job
can_fill_worker_job = {
	custom_tooltip = WORKER_JOB_TRIGGER
	hidden_trigger = {
		if = {
			limit = {
				planet = {
					is_planet_class = pc_cosmogenesis_world #Everyone can become a chip slave
				}
			}
			always = yes
		}
		else = {
			NOT = { has_ethic = ethic_gestalt_consciousness }
			NOT = { pop_group_has_trait = trait_tankbound }
			exists = owner
			is_being_purged = no
			is_being_assimilated = no
			NOR = {
				has_citizenship_type = {
					type = citizenship_organic_trophy
					country = owner
				}
				has_citizenship_type = {
					type = citizenship_cruise_passenger
					country = owner
				}
			}
			NAND = {
				has_citizenship_type = { type = citizenship_slavery }
				OR = {
					has_slavery_type = { type = slavery_livestock }
					has_slavery_type = { type = slavery_matrix }
				}
			}
			if = {
				limit = {
					has_disconnected_drone_citizenship_type = yes
				}
				disconnected_drone_can_be_worker = yes
			}
			if = {
				limit = {
					divinity_right_to_work_job_check_trigger_exempt = no
				}
				divinity_right_to_work_job_check_trigger_worker = yes
			}
		}
	}
}

#this/root = pop
#checked if job's possible_precalc = can_fill_specialist_job
can_fill_specialist_job = {
	can_fill_specialist_job_trigger = yes
	# Defined in 01_scripted_triggers_jobs.txt
}

#this/root = pop
#checked if job's possible_precalc = can_fill_ruler_job
can_fill_ruler_job = {
	if = {
		limit = {
			exists = owner
			owner = {
				has_origin = origin_necrophage
				has_trait = trait_necrophage
			}
		}
		custom_tooltip = {
			text = RULER_JOB_NECROPHAGE_TRIGGER
			has_trait = trait_necrophage
			is_enslaved = no
			is_being_purged = no
			is_being_assimilated = no
		}
	}
	else = {
		custom_tooltip = RULER_JOB_TRIGGER
		hidden_trigger = {
			NOT = { has_ethic = ethic_gestalt_consciousness }
			exists = owner
			is_enslaved = no
			is_being_purged = no
			is_being_assimilated = no
			NOT = { has_trait = trait_syncretic_proles }
			# Rule out Traits for servitude & lack of free will
			can_think = yes
			# Rule out Disconnected Drones
			has_disconnected_drone_citizenship_type = no
			# Rule out Machine Pops, unless they're rendered sapient
			OR = {
				NOT = { has_trait = trait_mechanical }
				AND = {
					owner = { has_technology = tech_synthetic_workers }
					owner = { has_policy_flag = ai_full_rights }
				}
			}
			# Rule out Organic Trophies
			NOR = {
				has_citizenship_type = {
					type = citizenship_organic_trophy
					country = owner
				}
				has_citizenship_type = {
					type = citizenship_cruise_passenger
					country = owner
				}
			}
			# Rule out violations of the 'Right to Work' Resolution (prioritises organic workers)
			if = {
				limit = {
					divinity_right_to_work_job_check_trigger_exempt = no
				}
				divinity_right_to_work_job_check_trigger_ruler = yes
			}
		}
	}
}

#this/root = pop
#checked if job's possible_precalc = can_fill_precursor_job
can_fill_precursor_job = {
	custom_tooltip = SPECIALIST_JOB_TRIGGER
	hidden_trigger = {
		exists = owner
		owner = { is_fallen_empire = yes }
	}
}

# Checks whether the game should try and build a building or district on a colony via scripts in common/colony_automation
# Does not apply to "emergency building" (colony_automation_exceptions)
# this = colony
should_colony_automate = {
	always = yes
}

dismiss_leader_cost = {
	base = 0
}

# this = leader
can_dismiss_leader = {
	if = {
		limit = {
			leader_class = commander
			is_idle = no
			# Rulers and heirs can't be dismissed any way and can be non-idle
			is_ruler = no
			is_heir = no
			NOT = {
				exists = planet
			}
		}
		custom_tooltip = {
			fail_text = "requires_fleet_control"
			exists = fleet
			fleet = {
				controller = {
					is_same_value = prev.owner
					is_same_value = prevprev.owner
				}
			}
		}
	}
	if = {
		limit = {
			OR = {
				is_heir = yes
				is_ruler = yes
			}
		}
		custom_tooltip = {
			always = no
			fail_text = "can_not_dismiss_rulers"
		}
	}

	if = {
		limit = {
			leader_class = scientist
		}
		custom_tooltip = {
			fail_text = "can_not_dismiss_rift_explorer"
			NAND = {
				exists = fleet
				fleet = {
					has_fleet_order = explore_astral_rift_fleet_order
				}
			}
		}
	}
	if = {
		limit = {
			leader_class = commander
		}
		custom_tooltip = {
			fail_text = "no_dismissing_tacs"
			NOT = { has_trait = leader_trait_tactical_algorithms }
		}
	}
	if = {
		limit = {
			has_leader_flag = manifesti_divine_leader
		}
		custom_tooltip = {
			fail_text = "manifesti_leader_tt"
			NOT = { has_leader_flag = manifesti_divine_leader }
		}
	}
	if = {
		limit = {
			has_leader_flag = nomadic_princess # The princess should not be dismissed before finishing her event chain
			owner = {
				has_country_flag = hired_nomadic_princess
			}
		}
		custom_tooltip = {
			fail_text = "nomadic_princess_tt"
			NOT = {
				has_leader_flag = nomadic_princess
				owner = {
					has_country_flag = hired_nomadic_princess
				}
			}
		}
	}
	if = {
		limit = {
			has_leader_flag = eternal_chorus_researcher
			owner = {
				has_event_chain = arkship_eternal_chorus_chain
			}
		}
		custom_tooltip = {
			always = no
			fail_text = "leader_no_dismiss_tt"
		}
	}
	if = {
		limit = {
			has_leader_flag = diplomatic_envoy_official
			owner = {
				has_event_chain = arkship_diplomatic_envoy_chain
			}
		}
		custom_tooltip = {
			always = no
			fail_text = "leader_no_dismiss_tt"
		}
	}
}

# this = country
can_country_change_councilors = {
	is_gestalt = no
}

# this = leader
should_count_towards_leader_cap = {
	NOR = {
		leader_class = envoy
		is_hidden = yes
		has_leader_flag = hired_admiral_merc_leader
		is_leader_tier = leader_tier_legendary
		has_trait = leader_trait_rift_warped
		AND = {
			has_trait = leader_trait_eager
			has_base_skill < 4
		}
		AND = {
			is_ruler = yes
			OR = {
				has_trait = leader_trait_ruler_machine_intelligence
				has_trait = leader_trait_ruler_hive_mind
			}
		}
		has_trait = leader_trait_paladin
		has_trait = leader_trait_nomadic_sage
		has_leader_flag = temp_contestant_admiral
	}
}

# From = country, country that tries to connect using relay network
# ROOT/This = country, target country to connect to using relay network
can_country_connect_to_relay_network = {
	is_primitive = no
	is_nomadic = no
	OR = {
		is_country_type = default
		is_subject = yes
		is_overlord = yes
	}
	from = {
		is_primitive = no
		is_nomadic = no
		OR = {
			is_country_type = default
			is_subject = yes
			is_overlord = yes
		}
	}
}

# this = colony
should_ai_build_stronghold_on_vulnerable_colony = {
	planet_stability > 50
	OR = {
		AND = {
			pop_amount <= 500
			num_buildings = { type = building_stronghold value < 1 }
		}
		AND = {
			pop_amount <= 1000
			num_buildings = { type = building_stronghold value < 2 }
		}
		AND = {
			pop_amount <= 1500
			num_buildings = { type = building_stronghold value < 3 }
		}
		AND = {
			pop_amount <= 2500
			num_buildings = { type = building_stronghold value < 4 }
		}
	}

	exists = sector
	sector = {
		NOT = { has_sector_type = core_sector }
	}

	IF = {
		LIMIT = {
			exists = owner
			owner = { is_gestalt = yes }
		}
		NOT = { has_available_jobs = warrior_drone }
		any_owned_pop_group = {
			is_unemployed = yes
			can_work_specific_job = warrior_drone
		}
	}
	ELSE = {
		NOT = { has_available_jobs = soldier }
		any_owned_pop_group = {
			is_unemployed = yes
			can_work_specific_job = soldier
		}
	}
}

# Used to call on the Retirement Toast when a leader dies
# This = leader
will_leader_retire = {
	exists = owner
	NOT = { has_leader_flag = use_unspecific_death_reason }
	owner = {
		NOT = { has_ethic = ethic_gestalt_consciousness }
	}
	has_leader_flag = retirement_is_default # Randomized retirement instead of death in paragon.590
}

# Used to call the Second Chance toast when a leader dies, still need to clone it and actually have the leader survive
# This = leader
will_leader_survive = {
	exists = owner
	OR = {
		# Spare Organs trait
		AND = {
			species = { has_trait = trait_spare_organs }
			NOR = {
				has_trait = trait_leader_second_chanced
				leader_class = envoy
			}
		}
		leader_backup_clone_can_activate = yes
	}
}

# This = country, Country that wants to get an external leader
# From = country, country that we are trying to get from
can_get_external_leader_pool_candidate = {
	exists = from
	OR = {
		has_diplo_migration_treaty = from
		is_in_federation_with = from
		is_overlord_to = from
	}
	# Both empires must be regular empires, machine intelligences or hive-minds.
	OR = {
		AND = {
			ROOT = {
				is_regular_empire = yes
			}
			FROM = {
				is_regular_empire = yes
			}
		}
		AND = {
			ROOT = {
				is_machine_empire = yes
			}
			FROM = {
				is_machine_empire = yes
			}
		}
		AND = {
			ROOT = {
				is_hive_empire = yes
			}
			FROM = {
				is_hive_empire = yes
			}
		}
	}
}

# This = leader, leader we'd like to trade
# Root = country, country that owns the leader currently
# From = country, country that would receive the leader
can_trade_leader = {
	# The leader must have a trait from a specialist subject type.
	this = {
		has_specialist_subject_leader_trait = yes
		is_councilor = no
		is_heir = no
	}
	OR = {
		AND = { # Either from subject to Overlord
			ROOT = {
				is_subject = yes
			}
			FROM = {
				is_overlord_to = ROOT
			}
		}
		AND = { # Or from a tactical cogitator empire to a NON tactical cogitator empire
			ROOT = { has_valid_civic = civic_machine_tactical_algorithms }
			FROM = {
				NOT = { has_valid_civic = civic_machine_tactical_algorithms }
				is_fallen_empire = no
				NOT = {
					any_owned_leader = {
						has_trait = leader_trait_tactical_algorithms
					}
				}
			}
		}
	}
	# The subject and overlord must both be regular empires, machine intelligences or hive-minds.
	OR = {
		AND = {
			ROOT = {
				is_regular_empire = yes
			}
			FROM = {
				is_regular_empire = yes
			}
		}
		AND = {
			ROOT = {
				is_machine_empire = yes
			}
			FROM = {
				is_machine_empire = yes
			}
		}
		AND = {
			ROOT = {
				is_hive_empire = yes
			}
			FROM = {
				is_hive_empire = yes
			}
		}
		AND = { # Tactical Cogitator option
			ROOT = {
				has_valid_civic = civic_machine_tactical_algorithms
			}
			FROM = {
				OR = {
					is_regular_empire = yes
					is_hive_empire = yes
					is_machine_empire = yes
				}
				NOT = { has_country_flag = no_murdering_tactical_leaders }
				NOT = {
					any_owned_leader = {
						has_trait = leader_trait_tactical_algorithms
					}
				}
			}
		}
	}
	# Can't trade gestalt nodes
	is_gestalt_node = no
	# The overlord can't be a fallen or awakened empire.
	ROOT = {
		NAND = {
			is_overlord = yes
			OR = {
				is_country_type = fallen_empire
				is_country_type = awakened_fallen_empire
			}
		}
	}
	NOR = {
		has_trait = leader_trait_fotd_scientist
		has_trait = leader_trait_fotd_commander
	}
}

# This = fleet, fleet we'd like to trade
# Root = country, country that owns the fleet currently
# From = country, country that would receive the fleet
can_trade_fleet = {
	AND = {
		ROOT = {
			NAND = {
				is_overlord = yes
				OR = {
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
		}
		NOT = { has_fleet_flag = dimensional_fleet }
	}
}

# this = leader
# from = country scope
can_leader_be_ruler = {
	NOT = { leader_class = envoy }
}

# this = leader
# from = country scope
can_leader_be_councilor = {
	is_exploring_astral_rift = no
}

# this = country
is_country_psionic = {
	is_psionic = yes
}

# this = species
is_psionic_species = {
	has_psionic_species_trait = yes
}

chance_for_external_leader_pool = {
	base = 10
	modifier = {
		is_xenophile = yes
		add = 30
	}
	modifier = {
		is_xenophobe = yes
		mult = 0.5
	}
}

# this = country scope
council_agenda_early_activation_cost = {
	base = 0
	add = value:council_agenda_early_activation_cost
}

# The colony automation doesn't attempt to build new buildings while the number of unemployed
# pops (excluding civilians) doesn't exceed the number of vacant workspaces by this threshold,
# and the number of civilians is less than building_automation_civilians_threshold.
# this = colony scope
building_automation_unemployment_threshold = {
	base = 50
}

# The colony automation doesn't attempt to build new buildings while the number of unemployed pops (excluding civilians)
# doesn't exceed the number of vacant workspaces by building_automation_unemployment_threshold, and the number of civilians
# is less than this threshold.
# this = colony scope
building_automation_civilians_threshold = {
	base = 50
}

shroud_delve_cooldown_days = {
	base = @breach_cooldown_days
	mult = value:scripted_modifier_mult_capped|MODIFIER|shroud_delve_cooldown|CAP|0.1|
	min = 1
}

count_shroud_worlds_to_display = {
	base = 0
	modifier = {
		exists = from
		from = {
			has_country_flag = has_activated_the_lost_patron
		}
		add = value:count_planet_type_in_system|TYPE|pc_shrouded|VALUE|1|
	}
}

# this = leader scope
# from = country scope
leader_election_elect_cost = {
	# Cost = (1 + election_cost_mult) * empire_size * 10
	base = 1
	add = modifier:country_election_cost_mult
	mult = from.trigger:empire_size

	# Rounded to closest 10, by applying x10 last.
	round = yes
	mult = 10
}

# this = country scope
emergency_election_cost_rule = {
	# Cost = (1 + election_cost_mult) * empire_size * 5
	base = 1
	add = modifier:country_election_cost_mult
	mult = trigger:empire_size
	mult = 5

	# Rounded to closest 50.
	divide = 50
	round = yes
	mult = 50
}

# this = leader scope (not valid in UI if no leader is selected)
# from = country scope
leader_election_support_cost = {
	# Cost = (1 + election_cost_mult) * empire_size * 4 * (1 + TimesSupported * 0.5)
	base = 1
	add = modifier:country_election_cost_mult
	mult = from.trigger:empire_size
	mult = 4

	# Rounded to closest 10; before applying TimesSupported, so player understands better how it increases.
	divide = 10
	round = yes
	mult = 10

	# If there's a Valid leader, cost increases if they're already Supported.
	modifier = {
		exists = this
		mult = value:leader_election_candidate_supported_mult
	}
}

# this = leader scope
leader_election_weight = {
	base = 0

	# Purity Elections
	modifier = {
		custom_tooltip = {
			success_text = concept_genetic_purity
			owner? = {
				is_purity_authority = yes
			}
			species = {
				is_variable_set = species_traits_purity_count
			}
		}
		add = species.value:species_purity_times_ten
	}

	# Multiply by Faction support.
	modifier = {
		custom_tooltip = {
			success_text = election_weight.faction_support
			owner? = { has_country_flag = first_faction }
		}
		add = value:leader_faction_support_election_bonus
	}

	# If no Factions formed, fake faction support based on if it's an Empire ethic or not.
	modifier = {
		custom_tooltip = {
			success_text = election_weight.no_faction_empire_ethic
			NOT = { owner? = { has_country_flag = first_faction } }
			leader_has_empire_ethic = yes
		}
		add = 50
	}
	modifier = {
		custom_tooltip = {
			success_text = election_weight.no_faction_not_empire_ethic
			NOT = { owner? = { has_country_flag = first_faction } }
			leader_has_empire_ethic = no
		}
		add = 25
	}

	# Not changing Ruler should be slightly preferred.
	modifier = {
		is_ruler = yes
		mult = 1.1
	}

	# Bonus if Leader was Councilor when election started. Or if no Election, is a Councilor now.
	modifier = {
		custom_tooltip = {
			success_text = election_weight.iswas_councilor
			if = {
				limit = {
					owner? = { has_country_flag = election_under_way }
				}
				has_leader_flag = councilor_at_election_start
			}
			else = {
				is_councilor = yes
				is_ruler = no
			}
		}
		mult = 1.05
	}

	# Governments (based on Ethics and Civics) might prefer certain Leader classes. Hide if +/- 0%.
	modifier = {
		custom_tooltip = {
			success_text = election_weight.leader_class_government_approves
			check_variable_arithmetic = {
				which = value:leader_class_government_match_bonus_mult
				value != 1
			}
		}
		mult = value:leader_class_government_match_bonus_mult
	}

	# Extra bonus for Councilor Subclass (which is a Trait).
	modifier = {
		custom_tooltip = {
			success_text = election_weight.leader_councilor_subclass
			has_council_subclass = yes
		}
		mult = 1.2
	}

	# Penalty for Non-Councilor Subclass (which is a Trait).
	modifier = {
		custom_tooltip = {
			success_text = election_weight.leader_councilor_subclass
			has_non_council_subclass = yes
		}
		mult = 0.8
	}

	# Bonus/penalty from Traits.
	modifier = {
		custom_tooltip = {
			success_text = election_weight.leader_traits
			check_variable_arithmetic = {
				which = value:leader_traits_election_bonus_mult
				value != 1
			}
		}
		mult = value:leader_traits_election_bonus_mult
	}

	# Bonus from Skill level.
	modifier = {
		custom_tooltip = {
			success_text = election_weight.leader_skill
			check_variable_arithmetic = {
				which = value:leader_skill_election_bonus_mult
				value != 1
			}
		}
		mult = value:leader_skill_election_bonus_mult
	}

	# Penalty from old Age, unless already ruler or immortal.
	modifier = {
		custom_tooltip = {
			success_text = election_weight.leader_age
			is_immortal = no
			is_ruler = no
			check_variable_arithmetic = {
				which = value:leader_age_percentage_of_lifespan
				value > 0.9
			}
		}
		mult = 0.5
	}
	# Titanic reduction if you are fanatic xenophobic and the candidate is a xeno
	modifier = {
		custom_tooltip = {
			success_text = Xeno
			owner? = {
				has_ethic = ethic_fanatic_xenophobe
				NOT = { is_same_species = prev } #Invalidated if you have selective kinship
				NAND = {
					has_valid_civic = civic_selective_kinship
					is_same_species_class = prev
				}
			}
		}
		mult = 0.001
	}
	# Huge Reduction if you are a xenophobe and the candidate is a xeno
	modifier = {
		custom_tooltip = {
			success_text = Xeno
			owner? = {
				has_ethic = ethic_xenophobe
				NOT = { is_same_species = prev }
				NAND = {
					has_valid_civic = civic_selective_kinship
					is_same_species_class = prev
				}
			}
		}
		mult = 0.01
	}
	# Halve the chance if you are neither a xenophobe or xenophile
	modifier = {
		custom_tooltip = {
			success_text = Xeno
			owner? = {
				NOR = {
					is_xenophobe = yes
					is_xenophile = yes
				}
				NOT = { is_same_species = prev }
				NAND = {
					has_valid_civic = civic_selective_kinship
					is_same_species_class = prev
				}
			}
		}
		mult = 0.5
	}
	# No modifiers for regular xenophiles or selective kinship empires
	# Fanatic Xenophiles prefer electing aliens that are not the same species as the current one in charge.
	modifier = {
		custom_tooltip = {
			success_text = Xeno
			owner? = { is_fanatic_xenophile = yes }
			exists = owner.ruler
			NOT = {
				is_same_species = owner.ruler
			}
		}
		mult = 1.2
	}
}

# Determines if pre-ftl country is unaware of alien life
# this: pre-ftl country
are_pre_ftls_unaware = {
	NOT = {
		current_awareness_level = full
	}
}

##TERRAFORMING CANDIDATES

# This = planet
# Checked if planet is a terraforming candidate
is_terraforming_candidate = {
	OR = {
		has_modifier = terraforming_candidate
		has_modifier = frozen_terraforming_candidate
		has_modifier = toxic_terraforming_candidate
	}
}

# Root = country
# This = planet
# Checked if the planet is special terraforming candidate and the country has the required matching ascension perk to do it
has_ascension_for_terraforming_candidate = {
	exists = root
	OR = {
		AND = {
			#No ascension perk required for terraforming_candidate modifier
			has_modifier = terraforming_candidate
		}
		AND = {
			root = {
				OR = {
					has_ascension_perk = ap_detox
					has_origin = origin_fallen_empire_hive
					has_country_flag = knights_detox
				}
			}
			has_modifier = toxic_terraforming_candidate
		}
		AND = {
			root = {
				OR = {
					has_ascension_perk = ap_hydrocentric
					has_origin = origin_fallen_empire_hive
				}
			}
			has_modifier = frozen_terraforming_candidate
		}
	}
}

# This = country that tries to rename
# Root = country owning the planet
# From = colony
# Checks if the colony (from) can be renamed by the country (this) that contains it in its borders, even if it has a different owner (Root)
# Renaming is never possible if the colony is not in your borders; and it is always possible if it is in your borders but the owner is yourself/no-one
can_be_renamed_by = {
	FROM = {
		exists = owner
		owner = {
			is_same_species = root
		}
		has_planet_flag = haven_flag

	}
}

# Root/this = country
# From = planet OR astral_rift
# Checks if the country is allowed to build any orbital station around this planet/star/astral_rift
can_build_station_around = {
	exists = root
	always = yes
}

# Root/this = country
# From = planet OR astral_rift
# Checks if the country is allowed to build a mining station around this planet/star/astral_rift
can_build_mining_station_around = {
	exists = root
	always = yes
}

# Root/this = country
# From = planet OR astral_rift
# Checks if the country is allowed to build a research station around this planet/star/astral_rift
can_build_research_station_around = {
	exists = root
	always = yes
}

# Root/this = country
# From = planet OR astral_rift
# Checks if the country is allowed to build an observation station around this planet/star/astral_rift
can_build_observation_station_around = {
	exists = root
	FROM = {
		is_scope_type = planet
		NOR = {
			has_planet_flag = haven_flag #FotD
			has_carrier_flag = no_observation #Solarpunk
		}
	}
}

# Root/this = country
# From = planet OR astral_rift
# Checks if the country is allowed to build a starbase around this planet/star/astral_rift
can_build_starbase_around = {
	exists = root
	custom_tooltip = {
		fail_text = NOMAD_SETTLING_IN_PROGRESS_FAIL
		from = {
			NOT = {
				solar_system = { has_star_flag = nomad_settling_in_progress }
			}
		}
	}
}

# Root/this = country
# Checks if the country's capital has any kind of relevancy (aka is the capital in the galaxy really theirs)
has_inworld_capital = {
	NOT = { is_country_type = mirrored_country }
}

# Root/this = trading country
# From = country traded with
# Checks if target country has economy power relative to begin with
has_relative_economy_power = {
	exists = this
	exists = from
	NOR = {
		is_country_type = mirrored_country
		from = { is_country_type = mirrored_country }
	}
}

# Root/this = trading country
# From = country traded with
# Checks if the country can trade sensor
can_trade_sensor = {
	NOR = {
		is_country_type = mirrored_country
		from = { is_country_type = mirrored_country }
	}
}

# Root/this = trading country
# From = country traded with
# Checks if the country can trade favor
can_trade_favor = {
	NOR = {
		OR = {
			is_country_type = mirrored_country
			is_country_type = fallen_empire
			is_country_type = awakened_fallen_empire
		}
		from = {
			OR = {
				is_country_type = mirrored_country
				is_country_type = fallen_empire
				is_country_type = awakened_fallen_empire
			}
		}
	}
}

# THIS = country
# FROM = Astral Rift checked
can_interact_with_astral_rift = {
	OR = {
		from = {
			is_surveyed = { who = prev status = yes }
		}
		intel_level = {
			level >= low
			system = from.solar_system
		}
		from = {
			exists = explorer
			explorer = {
				is_same_value = prevprev
			}
		}
	}
}

# THIS = country
# FROM = Astral Rift checked
can_explore_astral_rift = {
	has_technology = tech_rift_sphere
	hidden_trigger = {
		OR = {
			is_astral_rift_pool_empty = no
			AND = {
				exists = from.explorer
				from.explorer = {
					is_same_value = prevprev
				}
			}
		}
	}
}

# THIS = leader
# FROM = Astral Rift checked
# Checks if the selected leader can explore the rift
can_leader_explore_astral_rift = {
	custom_tooltip = {
		fail_text = "CANNOT_EXPLORE_WITH_COUNCIL_LEADER"
		is_councilor = no
	}
	custom_tooltip = {
		fail_text = "CANNOT_EXPLORE_WITH_HEIR_LEADER"
		is_heir = no
	}
}

# FROM = country of the ai (in case it's relevant)
# THIS = colony
# this determines if the colony is allowed to receive a governor at all, and isn't called for a specific candidate
can_ai_assign_governor = {
	OR = {
		is_sector_capital = yes
		is_planet_class = pc_ark
		is_capital = yes
	}
	pop_amount >= 100	# replaces the MINIMUM_POPS_TO_CONSIDER_GOVERNOR define
	NOT = {
		exists = leader
	}
}

# if the galactic community exists, is a galactic empire, and the country is part of it
# the ai will call this to see if it should set its delegate to strengthn it
# It is guaranteed that the galactic community exists, is an empire, and the country is part of it
# when this rule is called
# THIS = Country
# FROM = Galactic Emperor (Country)
should_ai_support_galactic_empire = {
	OR = {
		is_galactic_emperor = yes
		AND = {
			NOT = {
				FROM = {
					is_overlord_to = prev
				}
			}
			imperial_authority <= 0.75
			is_part_of_galactic_council = yes
			opinion = {
				who = FROM
				value >= 300	# value defined as "GOOD" in for opinion 00_defines.txt
			}
		}
		AND = {
			NOT = {
				FROM = {
					is_overlord_to = prev
				}
			}
			imperial_authority <= 0.75
			opinion = {
				who = FROM
				value >= 750	# value define as "EXCELLENT" for opinion in 00_defines.txt
			}
		}
		AND = {
			# Subjects of the Galactic Emperor that are loyal or cannot vote
			# should support the Imperium
			FROM = {
				is_overlord_to = prev
			}
			OR = {
				has_loyalty >= 0
				NOT = {
					any_agreement = {
						has_term_value = {
							term = subject_diplomacy
							value = subject_can_do_diplomacy
						}
					}
				}
			}
		}
	}
}

# if the galactic community exists, is a galactic empire, and the country is part of it
# and if it didn't decided to strengthen the empire,
# the ai will call this to see if it should set its delegate to undermine it
# if the ai neither wants to support nor undermine the galactic empire, it will just keep
# its emissary as a standard delegate
# It is guaranteed that the galactic community exists, is an empire, and the country is part of it
# when this rule is called
# THIS = Country
# FROM = Galactic Emperor (Country)
should_ai_undermine_galactic_empire = {
	is_galactic_emperor = no
	imperial_authority >= 0.25
	OR = {
		AND = {
			NOT = {
				FROM = {
					is_overlord_to = prev
				}
			}
			opinion = {
				who = FROM
				value <= -300	# value defined as "POOR" for opinion in 00_defines.txt
			}
		}
		AND = {
			FROM = {
				is_overlord_to = prev
			}
			has_loyalty < 0
			any_agreement = {
				has_term_value = {
					term = subject_diplomacy
					value = subject_can_do_diplomacy
				}
			}
		}
	}
}

# root/this = country
can_country_initiate_storm = {
	has_ascension_perk = ap_weather_control
}

# root/this = star
can_initiate_storm_on_planet = {
	custom_tooltip = {
		fail_text = "FLEETORDER_INITIATE_COSMIC_STORM_IS_NOT_STAR"
		is_star = yes
	}
	custom_tooltip = {
		fail_text = "FLEETORDER_INITIATE_COSMIC_STORM_ALREADY_INITIATED"
		NOT = {
			any_fleet_in_orbit = {
				has_fleet_flag = is_initiating_storm_at@root
			}
		}
	}
	custom_tooltip = {
		fail_text = "FLEETORDER_INITIATE_COSMIC_STORM_OCCUPIED"
		solar_system = {
			NOT = { has_star_flag = storm_system }
		}
	}
}

# root/this = system
can_force_see_fleets_in_system = {
	is_inside_storm = solar_storm
}

# root/this = country
# from = cosmic storm
has_breached_shroud_storm = {
	exists = this
	exists = from
	has_country_flag = breached_shroud
	from = { is_storm_type = shroud_storm }
}

# THIS = Fleet
is_science_fleet_gravity_snare_on_cooldown = {
	custom_tooltip = {
		success_text = FLEETORDER_SEND_SNARE_ON_COOLDOWN
		OR = {
			has_fleet_flag = gravity_snare_is_in_cooldown
			controller = {
				has_country_flag = gravity_snare_is_in_cooldown_ai
			}
		}
	}
}

# THIS = Fleet
# FROM = Country / Snare sender
can_fleet_be_captured_by_snares = {
	custom_tooltip = {
		fail_text = "FLEETORDER_GRAVITY_SNARE_NO_COMMUNICATIONS"
		exists = from
		exists = owner
		from = {
			has_communications = root.owner
		}
	}
	custom_tooltip = {
		fail_text = "FLEETORDER_MUST_BE_SPACE_CRITTER"
		exists = owner
		owner = {
			OR = {
				is_capturable_space_critter_country_type = yes
				is_country_type = grave_guardian
				is_country_type = lured_fauna_faction
			}
			is_guardian_country = no
		}
		any_owned_ship = {
			is_space_fauna = yes
		}
	}
}

# THIS = country
can_fauna_grow = {
	NOT = { has_policy_flag = nutrient_rationing }
}

# THIS = country
can_use_exhibits = {
	hidden_trigger = { exists = this }
	custom_tooltip = {
		fail_text = "GALACTIC_ARCHIVISM_NOT_RESEARCHED"
		has_technology = tech_galactic_archivism
	}
	custom_tooltip = {
		fail_text = "GRAND_ARCHIVE_NOT_BUILT"
		has_grand_archive = yes
	}
}

# THIS = country
can_access_vivarium = {
	custom_tooltip = {
		fail_text = "GRAVITY_WELLS_NOT_RESEARCHED"
		has_technology = tech_gravity_wells
	}
	custom_tooltip = {
		fail_text = "VIVARIUM_NO_ROOM"
		check_variable_arithmetic = {
			which = value:get_vivarium_capacity
			value > 0
		}
	}
}

# this = debris
# root = country who can explore debris
can_scavenge_and_research_debris = {
	OR = {
		AND = {
			exists = root
			root = {
				has_policy_flag = scavenge_and_research_debris
			}
		}
		AND = {
			must_scavenge = yes
			must_research = yes
		}
	}
}

# this = debris
# root = country who can explore debris
can_scavenge_debris = {
	OR = {
		AND = {
			exists = root
			root = {
				has_policy_flag = scavenge_debris
			}
		}
		AND = {
			must_scavenge = yes
			must_research = no
		}
	}
}

# this = country
# from = location (galactic_object)
can_explore_rifts_outside_borders = {
	exists = this
	exists = from
	OR = {
		is_nomadic = yes
		has_origin = origin_riftworld
	}
	OR = {
		from = { # regular case
			solar_system = { NOT = { exists = owner } }
		}
		AND = { # Nomadic with Research Agreement
			is_nomadic = yes
			from = {
				solar_system = { exists = owner }
			}
			is_nomadic = yes
			has_research_agreement = from.solar_system.owner
		}
	}
}

# this = country (fleet controller)
# from = solar_system
can_lock_bypass = {
	exists = this
	exists = from
	OR = {
		# The owner of the science ship owns the system.
		is_same_value = from.owner
		# Or is the overlord of an empire that owns the system.
		AND = {
			is_overlord = yes
			any_subject = {
				is_same_value = from.owner
			}
		}
		# Or is a nomadic empire with a waystation in the system.
		AND = {
			is_nomadic = yes
			any_owned_nonprimary_starbase = {
				is_waystation_starbase = yes
				solar_system = {
					is_same_value = from
				}
			}
		}
		# Or is the overlord of an empire of a nomadic empire with a waystation in the system.
		AND = {
			is_overlord = yes
			any_subject = {
				is_nomadic = yes
				any_owned_nonprimary_starbase = {
					is_waystation_starbase = yes
					solar_system = {
						is_same_value = from
					}
				}
			}
		}
	}
}

# this = country
# from = location (galactic_object)
can_excavate_outside_borders = {
	exists = this
	exists = from
	OR = {
		has_tradition = tr_archivism_frontier_archaeology
		is_nomadic = yes
	}
	OR = {
		from = { # regular case
			solar_system = { NOT = { exists = owner } }
		}
		AND = { # Nomadic with Research Agreement
			OR = {
				AND = {
					is_ai = no
					from = {
						solar_system = { exists = owner }
					}
				}
				from.solar_system.owner? = { # AI nomads will only poach sites from other AI empires
					is_ai = yes
				}
			}
			is_nomadic = yes
			has_research_agreement = from.solar_system.owner
		}
		# Nomadic empire with Archivism - will upset the system owner
		AND = {
			is_ai = no
			from = {
				solar_system = { exists = owner }
			}
			is_nomadic = yes
			has_tradition = tr_archivism_frontier_archaeology
		}
	}
}

# this = country
can_voidworms_bombard_country = {
	hidden_trigger = {
		exists = event_target:voidworms_country
		exists = this
	}
	OR = {
		is_country_type = default
		is_country_type = primitive
	}
	# During crisis, they should bombard no matter what, otherwise check tech and max voidworms
	if = {
		limit = {
			event_target:voidworms_country = {
				is_country_type = voidworms_crisis
			}
		}
		always = yes
	}
	else = {
		NOT = { has_technology = tech_voidworm_immunity }
		event_target:voidworms_country = {
			check_variable_arithmetic = {
				which = value:voidworm_ships_owned
				value < value:voidworm_maximum_population
			}
		}
	}
}

# this = country
can_use_mutations = {
	hidden_trigger = { exists = this }
	has_technology = tech_controlled_mutations
}

can_use_artificial_breeding = {
	hidden_trigger = { exists = this }
	has_technology = tech_alien_cloning
}

# this = actor (country)
# from = recipient (country)
can_trade_specimens = {
	exists = this
	exists = from
	count_exhibit = {
		limit = { contains_specimen = yes }
		count > 0
	}
	NOT = { has_country_flag = has_already_traded_specimens }
	from = {
		count_exhibit = {
			limit = { contains_specimen = yes }
			count < @archive_max_exhibit_count
		}
	}
}

# this = ship design
# from = owner (country)
force_create_default_ship_design = {
	exists = this
	exists = from
	is_designable = yes
	OR = {
		AND = {
			is_space_fauna = yes
			from = {
				uses_fauna_ship_sizes = yes
			}
		}
		# Add other cases here if any
	}
}


#this = leader
is_organic_leader = {
	exists = this
	is_organic_species = yes
}

# from = owner (country)
#This = fleet
is_seeded = {
	has_fleet_flag = fruitful_seeded_critter
}

#this = popgroup
is_organic_pop_group = {
	exists = this
	is_organic_species = yes
}

# this = country with whom to communicate
empire_diplomacy_leads_to_shroud = {
	has_shroud_dlc = yes
	is_country_type = shroud
	NOT = {
		has_country_flag = animator_of_clay_country_flag
	}
}

# this = destroyed ship
# from = attacker (ship)
can_capture_ship = {
	exists = this
	exists = from
	exists = controller
	controller = {
		is_country_type = default
	}
	OR = {
		# Civilian Ships
		is_ship_size = constructor
		is_ship_size = nomads_constructor
		is_ship_size = science
		# Regular Ships
		is_ship_size = corvette
		is_ship_size = frigate
		is_ship_size = destroyer
		is_ship_size = cruiser
		is_ship_size = battleship
		# Biological Ships
		is_ship_size = mauler_stage_1
		is_ship_size = mauler_stage_2
		is_ship_size = mauler_stage_3
		is_ship_size = weaver_stage_1
		is_ship_size = weaver_stage_2
		is_ship_size = weaver_stage_3
		is_ship_size = harbinger_stage_1
		is_ship_size = harbinger_stage_2
		is_ship_size = harbinger_stage_3
		is_ship_size = stinger_stage_1
		is_ship_size = stinger_stage_2
		is_ship_size = stinger_stage_3
		# Galactic Nemesis Ships
		is_ship_size = crisis_corvette
		is_ship_size = crisis_destroyer
		is_ship_size = crisis_cruiser
		# Offspring Ships
		AND = {
			controller = {
				has_origin = origin_progenitor_hive
			}
			OR = {
				is_ship_size = offspring_corvette
				is_ship_size = offspring_destroyer
				is_ship_size = offspring_cruiser
				is_ship_size = offspring_battleship
				is_ship_size = offspring_mauler_stage_1
				is_ship_size = offspring_mauler_stage_2
				is_ship_size = offspring_mauler_stage_3
				is_ship_size = offspring_weaver_stage_1
				is_ship_size = offspring_weaver_stage_2
				is_ship_size = offspring_weaver_stage_3
				is_ship_size = offspring_harbinger_stage_1
				is_ship_size = offspring_harbinger_stage_2
				is_ship_size = offspring_harbinger_stage_3
				is_ship_size = offspring_stinger_stage_1
				is_ship_size = offspring_stinger_stage_2
				is_ship_size = offspring_stinger_stage_3
			}
		}
		# Cosmogenesis Ships
		is_ship_size = cosmo_crisis_destroyer
		is_ship_size = cosmo_crisis_battlecruiser
		# Nanotech Ships
		AND = {
			controller = {
				has_tradition = tr_nanotech_finish
			}
			OR = {
				is_ship_size = nanite_swarmer
				is_ship_size = nanite_interdictor
			}
		}
		# Fallen Empire Ships
		is_ship_size = large_ship_fallen_empire
		is_ship_size = small_ship_fallen_empire
		# Marauder Ships
		is_ship_size = marauder_corvette
		is_ship_size = marauder_destroyer
		is_ship_size = marauder_cruiser
		is_ship_size = marauder_galleon
	}
}

# this = country
is_planetary_ascension_free = {
	custom_tooltip = {
		success_text = "ascension_accord_free"
		has_country_flag = free_planetary_ascension
	}
}

# this = country
# all countries which pass this rule only add 1 to num_fallen_empires
count_as_split_fallen_empire = {
	OR = {
		has_country_flag = fallen_empire_hive_war
		has_country_flag = fallen_empire_hive_growth
		has_country_flag = fallen_empire_hive_control
	}
}

# this = country
# from = planet
# When "colonize" is clicked, open the terraform menu instead
#
# This also means the ai_colonize_plans will only contain planets the AI can colonize via colony ships.
# The rest will be stored as ai_terraform_plans instead.
# See energy_expenditure_terraforming_to_colonize in common/ai_budget/00_energy_budget.txt for the AI budget implementation.
#
should_colonize_use_terraform = {
	exists = this
	is_wilderness_empire = yes
	# in case this is true for empires that can build Ring Worlds/Habitats, this should be false for these specific planet classes
}

# this = country
are_all_leaders_forced_immortal = {
	OR = {
		has_edict = energy_drain
		has_relic = r_eternal_throne
	}
}

# this = country using proxy war
# from = country to be sent to war
# root = victim country
can_be_sent_to_war = {
	OR = {
		from = {
			trust = {
				who = prev
				value >= @proxy_war_min_trust
			}
		}
		any_spynetwork = {
			is_spynetwork_level >= @proxy_war_min_infiltration
			target = {
				is_same_value = from
			}
		}
	}
}

# This = Country
can_open_shroud_tab = {
	has_shroud_dlc = yes
	OR = {
		can_access_shroud = yes
		has_breached_shroud = yes
	}
}

# This = Country
can_unlock_accords = {
	OR = {
		has_breached_shroud = yes
		is_chosen_empire = yes
	}
}

# This = Country
can_delve = {
	custom_tooltip = {
		fail_text = "REQUIRES_BREACHED_SHROUD"
		has_breached_shroud = yes
	}
}

# Will set the Delve blocked plane icon instead of the disabled one
# This = Country
is_delve_blocked = {
	custom_tooltip = {
		success_text = "contacting_great_patron"
		has_country_flag = contacting_great_patron
	}
}

# from = psionic aura (system)
# This = Country
can_see_aura_intensity = {
	exists = from
	OR = {
		has_technology = tech_psionic_aura
		has_technology = tech_aura_intensification
		has_technology = tech_psionic_suppression
		AND = {
			has_origin = origin_mindwardens
			from = { has_psionic_aura = exiled_aura }
		}
	}
}

# from = psionic aura owner
# This = country
is_hostile_to_aura_owner = {
	is_hostile_towards_aura = {
		PSIONIC_OWNER = from
	}
}

# this = country
should_reduce_aura_hostile_effect = {
	OR = {
		is_fallen_empire = yes
		is_crisis_faction = yes
	}
}

# this = pop_group
# from = country
should_hide_pop_group_size = {
	exists = this
	exists = from
	species = {
		has_trait = trait_wilderness
	}
	from = {
		is_wilderness_empire = yes
	}
}

# this = country
should_hide_planet_sector_pop_amount = {
	exists = this
	is_wilderness_empire = yes
}

# this = leader
should_leader_gain_trait_every_level = {
	has_trait = "leader_trait_legendary_leader"
}

# this = country scope
unbidden_additional_conquer_desire = {
	base = 0
	modifier = {
		add = 50
		exists = ruler
		ruler = {
			has_any_chosen_one_leader_trait = yes
		}
	}
	modifier = {
		add = 20
		has_psionic_species_trait = yes
	}
}

# This = Country
is_mercenary = {
	is_mercenary = yes
}

# this = country
# from = contact country
is_mindwarden_exiled_first_contact = {
	exists = this
	exists = from
	this = {
		has_origin = origin_mindwardens
		has_country_flag = exiled_partial_contact
	}
	from = {
		is_country_type = exiled
	}
}

has_shroud_insights = {
	has_shroud_dlc = yes
	OR = {
		has_country_flag = breached_shroud
		AND = {
			can_access_shroud = yes
			is_chosen_empire = yes
		}
	}
}

needs_psionic_aura_insights = {
	NOT = {
		has_origin = origin_endbringers
	}
}

# This = Country
is_ending_cycle = {
	has_patron_relation = {
		patron = end_of_the_cycle
		contact = completed
	}
}

# This rule is called if country THIS has custom_diplomacy = yes in its country type
# This = Country tested
# From = Target country trying to access diplomacy
has_custom_diplomacy = {
	if = { # Exiled countries behave as default empires with empires other than the Mindwardens who summoned them
		limit = { is_country_type = exiled }
		exists = event_target:exiled_country@from
		is_same_value = event_target:exiled_country@from
	}
	else = {
		always = yes
	}
}

# This = Country
can_issue_contracts = {
	OR = {
		can_issue_contracts_trigger = yes
		has_country_flag = cfl_hoster
	}
}

# This = Country
can_accept_contracts = {
	can_accept_contracts_trigger = yes
}

# This = Country
# From = Network Owner Country
benefits_from_waystation_network = { #Game rule definition
	benefits_from_waystation_network = { #Call to a scripted trigger
		NOMAD = from
		SYSTEM_OWNER = root
	}
}

# This = Carrier
can_toggle_encampment = {
	inline_script = {
		script = "encampment/encampment_requirement"
	}
}

# This = Country
is_star_seeker_empire = { #Game rule definition
	is_star_seeker_empire = yes #Call to a scripted trigger
}

# This = Carrier
# From = Viewing Country
can_access_decisions = {
	OR = {
		AND = { #This makes regular colonies as well as arkships work
			is_colony = yes
			colony.owner? = {
				is_same_value = from
			}
		}
		AND = { #This lets us do decisions on uninhabited planets without them being colonies, such as using the consecrate world decision to make a toxic world or gaia world holy
			is_colony = no
			carrier_is_type = planet
			space_owner? = {
				is_same_value = from
			}
		}
	}
}

# This = Country
ai_will_join_wars = {
	custom_tooltip = {
		fail_text = REFUSE_JOIN_FALLEN_EMPIRE
		is_fallen_empire = no
	}
}

# This = Country
ai_will_leave_wars = {
	custom_tooltip = {
		fail_text = REFUSE_LEAVE_MILITARIST
		is_militarist = no
	}
}

# This = Country
ai_will_join_offensive_wars = {
	custom_tooltip = {
		fail_text = REFUSE_JOIN_OFFENSIVE_WAR
		is_pacifist = no
	}
}

# This = Country
# From = Allied Country
ai_will_accept_ally = {
	from = {
		custom_tooltip = {
			fail_text = REFUSE_JOIN_GENOCIDERS
			is_homicidal = no
		}
	}
}

# This = Country
# From = Enemy Country
ai_will_accept_enemy = {
	from = {
		custom_tooltip = {
			fail_text = REFUSE_JOIN_GUARANTEED_INDEPENDENCE
			not = { root = { is_guaranteeing = prev } }
		}
		custom_tooltip = {
			fail_text = REFUSE_JOIN_NON_AGGRESSION_PACT
			nor = {
				has_non_aggression_pact = root
				has_defensive_pact = root
			}
		}
		custom_tooltip = {
			fail_text = REFUSE_JOIN_TRUCE
			not = { has_truce = root }
		}
	}
}

# This = country, the potential trespasser
# From = country, the border owner
# Note: Only evaluated when This has a truce with From. Enforces closed borders even if a country would ignore borders otherwise.
is_expelled_from = {
	has_country_flag = expelled_from_@from
}
# This = country
# If true, the country can reform government without paying unity and ignoring cooldown.
can_reform_government_for_free = {
	has_country_flag = free_government_reform
}
