# Root = target country
# This = country
is_valid_rival = {
	# Vanilla
	custom_tooltip = {
		fail_text = INVALID_RIVAL_DESC
		root = {
			OR = {
				prev = { merg_is_default_empire = no }
				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 = { merg_is_default_empire = no } }
			always = yes
		}
	}
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_POLICY
		OR = {
			root = {
				has_policy_flag = unrestricted_wars
				# Expanded Traits, Civics, Pops, and More
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						NOR = {
							has_valid_civic = civic_closed_collective
							has_valid_civic = civic_closed_network
						}
					"
					toggle = @expops_compatch_var
				}
			}
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					AND = {
						root = {
							has_policy_flag = liberation_wars
							has_authority = auth_democratic
							has_valid_ai_personality = yes
							has_ai_personality = democratic_crusaders
						}
						exists = owner
						owner = {
							NOR = {
								has_authority = auth_democratic
								is_exp_cooperative = yes
							}
						}
					}
					AND = {
						root = { has_policy_flag = liberation_wars }
						exists = owner
						owner = { is_gestalt = yes }
					}
				"
				toggle = @expops_compatch_var
			}
			# Vanilla
			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 = {
					merg_is_default_empire = yes
					is_fallen_empire = yes
					# ACOT SBTG
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = sbtg_stellarborne_empire_active
							is_country_type = sbtg_light_seeker
						"
						toggle = @has_sbtg_mod
					}
					# The Sleeper
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = awakened_sleeper
						"
						toggle = @has_the_sleeper_mod
					}
					# Twinks Eternal Empire
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = eternal_empire
						"
						toggle = @has_eternal_empire
					}
					# ZOFE
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_zofe_oe = yes
						"
						toggle = @has_zofe_vanilla
					}
				}
			}
		}
	}
	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 = {
				OR = {
					has_valid_civic = civic_machine_assimilator
					# Expanded Mods Base
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_valid_civic = civic_infestation
							has_government = gov_parasitic_overmind
						"
						toggle = @expops_compatch_var
					}
				}
			}
		}
	}
	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 = {
					is_homicidal = yes
					has_valid_civic = civic_machine_assimilator
					# Expanded Mods Base
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_government = gov_parasitic_overmind
						"
						toggle = @expops_compatch_var
					}
				}
			}
			root = {
				NOR = {
					is_homicidal = yes
					has_valid_civic = civic_machine_assimilator
					# Expanded Mods Base
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_government = gov_parasitic_overmind
						"
						toggle = @expops_compatch_var
					}
				}
			}
		}
	}
	# Reworked Advanced Ascension
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = AAR_failtip_PREROGATIVE_no_claim
				NOT = { has_star_flag = AAR_flg_sstm_NO_claim@root }
			}
		"
		toggle = @aar_svar_mod
	}
	# StarnetAI/StarTech
	custom_tooltip = {
        fail_text = CANNOT_ADD_CLAIM_NOT_VALID_EMPIRE
        NAND = {
            exists = owner
            root = { is_ai = yes }
            owner = {
                OR = {
                    merg_is_fallen_empire = yes
                    merg_is_awakened_fe = yes
                }
                relative_power = {
                    who = root
                    category = fleet
                    value >= equivalent
                }
            }
        }
    }
}

# Root = taker country
# This = giver country
can_get_claim_from_lost_system = {
	custom_tooltip = {
		fail_text = CANNOT_ADD_CLAIM_NOT_VALID_EMPIRE
		OR = {
			merg_is_default_empire = yes
			is_country_type = fallen_empire
			is_country_type = awakened_fallen_empire
			# ACOT SBTG
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = sbtg_stellarborne_empire_active
					is_country_type = sbtg_light_seeker
				"
				toggle = @has_sbtg_mod
			}
			# Twinks Eternal Empire
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = eternal_empire
				"
				toggle = @has_eternal_empire
			}
			# ZOFE
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = ascended_empire
					is_zofe_oe = yes
				"
				toggle = @has_zofe_vanilla
			}
		}
		root = {
			OR = {
				merg_is_default_empire = yes
				is_country_type = fallen_empire
				is_country_type = awakened_fallen_empire
				# ACOT SBTG
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
							is_country_type = sbtg_stellarborne_empire_active
							is_country_type = sbtg_light_seeker
						"
					toggle = @has_sbtg_mod
				}
				# Twinks Eternal Empire
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = eternal_empire
					"
					toggle = @has_eternal_empire
				}
				# ZOFE
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = ascended_empire
						is_zofe_oe = yes
					"
					toggle = @has_zofe_vanilla
				}
			}
		}
	}
	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
			}
		}
	}
}

# should_surrender_planets_to
# species_has_citizenship

#Root = country
#This = pop
should_abduct_pop = {
	planet = {
		exists = owner
		owner = { is_hostile = root }
	}
	has_virtual_species_trait = no
	# Genshin Impact
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			has_lingjing_species_trait = no
		"
		toggle = @trait_shijianzhe_sci_amt
	}
	# Vanilla
	OR = {
		# Expanded Mods Base
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				root = { has_valid_civic = civic_living_hosts }
				root = { has_government = gov_parasitic_overmind }
			"
			toggle = @expops_compatch_var
		}
		# Vanilla
		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 }
			}
		}
	}
	# V_TRAITS
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			is_v_mobile = yes
			has_trait_v_active_camouflage = no
			has_trait_v_self_decoration = no
		"
		toggle = @has_v_traits_mod
	}
}

#Root = planet
#This = species
species_can_live_on_planet = {
	hidden_trigger = {
		exists = root
		# Machine Worlds
		NAND = {
			root = { merg_is_machine_world = yes }
			NOR = {
				has_trait = trait_machine_unit
				has_trait = trait_mechanical
				# Ethics and Civics Bug Branch
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_bugged_happy_machine = yes
					"
					toggle = @has_bug_branch
				}
				# Origin - F.C.S.S & M.C.S.S
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_trait = trait_cybernetic
					"
					toggle = @fcss_mod
				}
				# Vanilla
				AND = {
					has_trait = trait_cybernetic
					exists = root.owner
					root.owner = {
						is_machine_empire = yes
						OR = {
							has_civic = civic_machine_assimilator
							has_ascension_perk = ap_mechromancy
						}
					}
				}
				# Girls Frontline
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_trait = GF_trait_doll
					"
					toggle = @gf_compatch_var
				}
			}
		}
		# Hive Worlds
		if = {
			limit = { root = { merg_is_hive_world = yes } }
			OR = {
				merg_has_hive_trait = yes
				# Zob
				is_zob_uplifted = yes
				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 }
					}
				}
				# Expanded Gestalts: Forgotten Queens
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						AND = {
							exists = root.owner
							root.owner = {
								is_hive_empire = yes
								has_valid_civic = civic_hive_parasitic
								prev = { any_species_pop = { has_job = purge_parasitic } }
							}
						}
					"
					toggle = @has_forgotten_queens
				}
			}
		}
	}
	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 }
			}
		}
	}
	# Planetary Population Restrictions
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = RESIDENT_LIVING_RESTRICTED
				has_policy_residentliving_restricted = yes
			}
			custom_tooltip = {
				fail_text = SLAVE_LIVING_RESTRICTED
				has_policy_slaveliving_restricted = yes
			}
		"
		toggle = @planet_pop_restrict_mod
	}
	# Time Framework
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			TIME_RULE_trgr_spcs_CAN_live_on_planet = yes
		"
		toggle = @time_framework
	}
	# Expanded Gestalts: Forgotten Queens
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NAND = {
				root = { is_fq_matryoshka = no }
				is_fq_trait_machine_digital = yes
			}
		"
		toggle = @has_forgotten_queens
	}
	# Zob
	if = {
		limit = { is_zob_uplifted = yes }
		root = {
			is_planet_class = pc_hive
			has_planet_flag = zob_planet
		}
	}
}

# can_vote_in_democratic_election
# should_pay_for_leader_upkeep

#Checks whether any leaders (scientists etc) can be formed FROM this pop
#Root = country
#This = pop
can_generate_leader_from_pop = {
	can_be_leader = yes
	NOR = {
		has_trait = trait_syncretic_proles
		# Assault Lily
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_trait = trait_AL_puffer
			"
			toggle = @change_planet_size_bullet_num
		}
		# Ethics & Civics: Bug Branch
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_bugged_slave_gene = yes
			"
			toggle = @has_bug_branch
		}
		# Expanded Traits, Civics, Pops, and More
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_infested_basic = yes
				is_exp_tree_caretaker = yes
			"
			toggle = @expops_compatch_var
		}
		# Expanded Gestalts: Forgotten Queens
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				root = { has_origin = origin_hive_overmind }
			"
			toggle = @has_forgotten_queens
		}
		# Lithoid Supremacy
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_pearl_born_drone = yes
			"
			toggle = @has_lithoid_mod
		}
	}
}

#Checks whether this species is eligible to become rulers through elections
#Root = country
#This = species
can_generate_leader_from_species = {
	is_sapient = yes
	OR = {
		# Machines & Robot Expansion Continued
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				oxr_mdlc_can_generate_leader_from_species = yes
			"
			toggle = @oxr_mdlc_mod
		}
		# Vanilla
		NAND = {
			NOT = { root = { has_technology = tech_synthetic_leaders } }
			has_trait = trait_mechanical
		}
		has_citizenship_type = { type = citizenship_full country = root }
		# Special Leadership Privileges for Battle Thralls & Bio-Trophies
		AND = {
			has_species_flag = allow_leaders_organic_advisors@root
			has_citizenship_type = { type = citizenship_organic_trophy country = root }
		}
	}
	NOR = {
		# ThaumStellaris
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				species = {
					OR = {
						is_archetype = TC_GOLEM
						is_archetype = TC_GOLEM_HIVE
						is_archetype = TC_GOLEM_MACHINE
					}
				}
			"
			toggle = @has_tc_mod
		}
		# Ethics and Civics - Bug Branch
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_bugged_symbiotic = yes
			"
			toggle = @has_bug_branch
		}
	}
}

# can_create_espionage_asset_from_species
# can_generate_military_leader_from_pop
# can_generate_army_from_planet

#Root = planet
#This = country
can_release_vassal = {
	if = {
		limit = { merg_has_expanded_pops = yes }
		always = yes
	}
	else = {
		custom_tooltip = {
			fail_text = default
			is_homicidal = no
		}
		custom_tooltip = {
			fail_text = default
			NOT = { has_valid_civic = civic_machine_assimilator }
		}
		# Twinks Eternal Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				custom_tooltip = {
					fail_text = default
					NOT = { is_country_type = eternal_empire }
				}
			"
			toggle = @has_eternal_empire
		}
		# Electroids Species Pack
		custom_tooltip = {
			fail_text = default
			NOT = { has_valid_civic = civic_elec_stellar_devourers }
		}
		# Libra: Gestalt Age
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				custom_tooltip = {
					fail_text = default
					NOT = { has_ascension_perk = ap_libra_gestalt_arcology }
				}
				custom_tooltip = {
					fail_text = default
					OR = {
						NOT = { has_ascension_perk = ap_libra_ultimate_directive }
						has_origin = origin_progenitor_hive
					}
				}
				custom_tooltip = {
					fail_text = default
					OR = {
						NOT = { has_ascension_perk = ap_libra_imperial_vermillion }
						ruler = { has_trait = leader_trait_libra_imperial_vermillion_lord_forever }
						ruler = { has_trait = leader_trait_libra_imperial_vermillion_galactic_emperor }
					}
				}
			"
			toggle = @libra_mod
		}
	}
}

#Root = species
#This = country
can_release_vassal_from_species = {
	root = {
		OR = {
			# Machine & Robot Expansion Continued
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					oxr_mdlc_can_release_vassal_from_species = yes
				"
				toggle = @oxr_mdlc_mod
			}
			# Vanilla
			AND = {
				is_sapient = yes
				NOR = {
					AND = {
						AND = {
							has_trait = trait_machine_unit
							prev.this = {
								NOR = {
									is_individual_machine = yes
									# Libra: Gestalt Age
									inline_script = {
										script = merger_of_rules/toggled_code
										code = "
											has_ascension_perk = ap_libra_gestalt_release_vassals
										"
										toggle = @has_guillis_planet_modifiers
									}
								}
							}
						}
						# Expanded Traits, Civics, Pops, and More
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								NOT = { prev.this = { has_origin = origin_master_computer } }
							"
							toggle = @has_planetary_diversity
						}
					}
					AND = {
						has_trait = trait_hive_mind
						NOR = {
							prev.this = {
								OR = {
									has_origin = origin_progenitor_hive
									# Plentiful Perks
									has_ascension_perk = ap_plentiful_traditions_mass_effect
									# Libra: Gestalt Age
									inline_script = {
										script = merger_of_rules/toggled_code
										code = "
											has_ascension_perk = ap_libra_gestalt_release_vassals
										"
										toggle = @libra_mod
									}
								}
							}
							# Empires Expanded
							inline_script = {
								script = merger_of_rules/toggled_code
								code = "
									prev.this = { has_valid_civic = civic_hive_semi_autonomous_drones }
								"
								toggle = @has_emex_mod
							}
						}
					}
					AND = {
						NOT = { has_trait = trait_hive_mind }
						prev.this = { is_hive_empire = yes }
					}
					can_think = no
					has_trait = trait_clone_soldier_infertile
					has_trait = trait_clone_soldier_infertile_full_potential
					# Empires Expanded
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							AND = {
								has_trait = trait_machine_unit
								NOR = {
									prev.this = { has_valid_civic = civic_machine_mighty_processors }
									prev.this = { is_individual_machine = yes }
								}
							}
						"
						toggle = @has_emex_mod
					}
					# Ethics & Civcs Infinity
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_eci_flesh_components = yes
						"
						toggle = @post_revolutionary_re_revolution_years
					}
					# Girls Frontline
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_trait = GF_trait_doll
						"
						toggle = @gf_compatch_var
					}
					# Expanded Traits, Civics, Pops, and More
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							AND = {
								NOT = { has_trait = trait_machine_unit }
								prev.this = { is_machine_empire = yes }
							}
							is_infested_basic = yes
							is_exp_tree_caretaker = yes
						"
						toggle = @expops_compatch_var
					}
				}
			}
		}
	}
}

# can_generate_army_from_species

# 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
			}
		}
		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
				}
			}
		}
	}
	# 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"
		NOT = {
			has_trait = trait_self_modified
		}
	}
	# Genshin Impact
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = CANNOT_MODIFY_SELF_MODIFIED_LINGJING_SPECIES
				NOT = { has_trait = trait_robot_lingjing_digital }
			}
		"
		toggle = @trait_shijianzhe_sci_amt
	}
}

#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 = {
		OR = {
			has_valid_civic = civic_feudal_realm
			# Ethics & Civics Infinity
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_valid_civic = civic_aristocratic_elite
					has_valid_civic = civic_coalition_government
					has_valid_civic = civic_altruism
				"
				toggle = @post_revolutionary_re_revolution_years
			}
		}
	}
	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 = {
	# Vanilla
	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 = {
				NOR = {
					has_country_flag = hostile_to_nomads
					# Ariphaos Patch
					is_hostile = root
				}
				root = { is_country_type = nomad }
			}
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_FIRST_CONTACT_PROTOCOL"
			always = no
		}
	}
	# Origins of Civilization
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = {
					root = { merg_is_default_empire = yes }
					is_country_type = mpo_lost_spacefarers
				}
				hidden_trigger = {
					has_country_flag = mpo_attack_spacefarers
					OR = {
						any_owned_fleet = {
							exists = solar_system.space_owner
							solar_system.space_owner = { is_same_value = root }
						}
						any_owned_planet = {
							exists = solar_system.space_owner
							solar_system.space_owner = { is_same_value = root }
						}
					}
				}
			}
		"
		toggle = @has_origins_of_civilization
	}
	# Vanilla
	else_if = {
		#attacking primitives with non-interference
		limit = {
			root = {
				merg_is_default_empire = yes
				NOT = { has_policy_flag = interference_aggressive }
			}
			is_primitive = yes
		}
		custom_tooltip = {
			text = "CAN_TAKE_HOSTILE_ACTION_NATIVE_INTERFERENCE"
			always = no
		}
	}
	else_if = {
		limit = {
			is_primitive = yes
			root = { merg_is_default_empire = yes }
			capital_scope = {
				root = { NOT = { any_planet_within_border = { is_same_value = prevprev } } }
			}
		}
		custom_tooltip = {
			text = ""
			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 = {
			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
		}
	}
	# ACOT STBG
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = { this = { is_country_type = sbtg_stellarborne_empire_passive } }
				custom_tooltip = { always = no }
			}
		"
		toggle = @has_sbtg_mod
	}
	# ACOT
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = { this = { is_country_type = void_lifeforms_1 } }
				custom_tooltip = { always = yes }
			}
			else_if = {
				limit = { root = { is_country_type = void_lifeforms_1 } }
				custom_tooltip = { always = yes }
			}
			else_if = {
				limit = { this = { is_country_type = void_lifeforms_2 } }
				custom_tooltip = { 	always = yes }
			}
			else_if = {
				limit = { root = { is_country_type = void_lifeforms_2 } }
				custom_tooltip = { always = yes }
			}
		"
		toggle = @has_ancient_caches
	}
	# Planet States
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = {
					is_country_type = planet_state
					root = { is_country_type = default }
					capital_scope = { root = { NOT = { any_planet_within_border = { is_same_value = prevprev } } } }
				}
				custom_tooltip = {
					text = CAN_TAKE_HOSTILE_ACTION_PLANET_STATE
					always = no
				}
			}
			else_if = {
				limit = {
					is_country_type = planet_state
					root = {
						OR = {
							is_country_type = dormant_marauders
							is_country_type = ruined_marauders
							is_country_type = awakened_marauders
							# The Empty Vagrant
							inline_script = {
								script = merger_of_rules/toggled_code
								code = \"
									is_country_type = awakened_marauders_ev
								\"
								toggle = @homeless_class_trait_weight
							}
						}
					}
				}
				always = no
			}
		"
		toggle = @planet_states_mod
	}
	# Some Events of Outside Cluster
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = {
					OR = {
						AND = {
							root = { has_country_flag = nep_gaia_assist_war }
							has_country_flag = nep_highlander_country
						}
						AND = {
							root = { has_country_flag = nep_highlander_assist_war }
							has_country_flag = nep_gaia_country
						}
					}
				}
				always = yes
			}
			else_if = {
				limit = {
					root = { NOT = { has_technology = tech_nep_phantom_phase_shift } }
					is_country_type = nep_phantom_passive
				}
				custom_tooltip = {
					text = CAN_TAKE_HOSTILE_ACTION_NEP_PHANTOM
					always = no
				}
			}
			else_if = {
				limit = {
					root = { is_ai = no }
					OR = {
						AND = {
							root = { has_country_flag = nep_gaia_assist_war }
							has_country_flag = nep_gaia_country
						}
						AND = {
							has_country_flag = nep_gaia_assist_war
							root = { has_country_flag = nep_gaia_country }
						}
						AND = {
							root = { has_country_flag = nep_highlander_assist_war }
							has_country_flag = nep_highlander_country
						}
						AND = {
							has_country_flag = nep_highlander_assist_war
							root = { has_country_flag = nep_highlander_country }
						}
						AND = {
							has_country_flag = nep_gpc_country
							root = { has_country_flag = nep_caravaneer_fleet_country }
						}
						AND = {
							root = { has_country_flag = nep_gpc_country }
							has_country_flag = nep_caravaneer_fleet_country
						}
					}
				}
				custom_tooltip = {
					text = CAN_TAKE_HOSTILE_ACTION_NEP_GAIA
					always = no
				}
			}
			else_if = {
				limit = {
					is_country_type = nep_arena_hologram
					is_country_type = nep_bounty_hologram
				}
				custom_tooltip = {
					text = CAN_TAKE_HOSTILE_ACTION_NEP_HOLOGRAM
					always = no
				}
			}
		"
		toggle = @has_seooc_mod
	}
	# Vanilla
	else = { always = yes }
}

# can_declare_war

#Root = country
#This = planet
can_colonize_planet = {
	hidden_trigger = {
		exists = root
	}
	custom_tooltip = {
		fail_text = "COLONIZATION_IMPOSSIBLE_UNDER_INVESTIGATION"
		NOT = { has_planet_flag = planet_under_investigation }
	}
	custom_tooltip = {
		fail_text = "COLONIZATION_IMPOSSIBLE_DYSON_SPHERE"
		if = {
			limit = { is_artificial = no } # Colonizing habitats is fine
			system_has_dyson_sphere = no
		}
	}
	custom_tooltip = {
		fail_text = "COLONIZATION_IMPOSSIBLE_DYSON_SPHERE_CONSTRUCTION"
		if = {
			limit = {
				system_has_dyson_sphere = no
				is_artificial = no	# Colonizing habitats is fine
			}
			NOT = { solar_system = { has_star_flag = dyson_sphere_construction } }
		}
	}
	custom_tooltip = {
		fail_text = "COLONIZATION_IMPOSSIBLE_COSMOGENESIS_EXODUS"
		exists = root
		root = {
			NOT = { owner = { has_event_chain = cosmogenesis_planet_pickup_chain } }
		}
	}
	custom_tooltip = {
		fail_text = "COLONIZATION_IMPOSSIBLE_CRIMSON_CRAWLERS"
		if = {
			limit = { has_modifier = crimson_crawlers }
			root = { cares_for_crimson_crawlers = yes }
		}
	}
	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 }
	}
	# Machine & Robot Expansion Continued
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = oxr_mdlc_can_colonize_planet_pc_habitat.fail_active
				NAND = {
					is_planet_class = pc_habitat
					oxr_mdlc_planet_decision_dismantle_habitat_active = yes
				}
			}
			custom_tooltip = {
				fail_text = oxr_mdlc_can_colonize_planet.wm_is_rebooting
				NOT = { oxr_mdlc_planet_world_machine_is_rebooting = yes }
			}
		"
		toggle = @oxr_mdlc_mod
	}
	# StarnetAI/StarTech
	if = {
		limit = {
			has_modifier = "holy_planet"
			exists = root
			root = {
				is_ai = yes
				NOT = { has_relic = r_severed_head }
			}
			any_country = { has_ai_personality_behaviour = holy_planets }
		}
		always = no
	}
	# Vanilla
	else = { always = yes }
}

# can_colonize_outside_borders

#Root = country
#This = planet
can_terraform_planet = {
	exists = root
	custom_tooltip = {
		fail_text = terraform_fail_no_presapient_protection_or_no_presapients
		NAND = {
			root = { has_policy_flag = pre_sapients_protect }
			any_owned_pop = { is_sapient = no }
		}
	}
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = requires_actor_not_devouring_swarm_lithoid
				root = { is_lithoid_devouring_swarm = no }
			}
		"
		toggle = @[ 1 - has_bug_branch ]
	}
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = legendary_leader_planet_no_terraform
				this = {
					NOT = { has_planet_flag = legendary_leader_planet }
				}
			}
		"
		toggle = @[ 1 - has_planetary_diversity ]
	}
	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 = 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 }
	}
	# Forgotten Empires
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = scfe_cant_terraform_special_planet
				NOT = { has_planet_flag = cant_terraform_planet }
			}
		"
		toggle = @forgotten_empires
	}
	# Real Space New Frontiers
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = NO_TERRAFORM_HOLLOW_WORLD
				NOT = { is_planet_class = pc_hollow }
			}
			custom_tooltip = {
				fail_text = NO_TERRAFORM_OVERMIND
				NOT = { has_deposit = d_heart_of_the_overmind }
			}
		"
		toggle = @rs_compatch_var
	}
	# Planetary Diversity Ascension Worlds
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			pd_can_terraform_planet = yes
		"
		toggle = @has_planetary_diversity_ascension_worlds
	}
}

#Root = country
#This = pop
can_colonize_with_pop = {
	has_colonization_control = {
		type = colonization_control_no
		country = root
	}
	OR = {
		NOT = { has_trait = trait_self_modified }
		is_same_species = root
	}
	has_cybernetic_creed_trait = no
	NOR = {
		has_trait = trait_pathogenic_genes
		has_trait = trait_limited_cybernetic
		# Genshin Impact
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_trait = trait_robot_lingjing_digital
			"
			toggle = @trait_shijianzhe_sci_amt
		}
	}
	# The Empty Vagrant
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			Root = {
				OR = {
					is_default_ev_country_type = no
					AND = {
						is_default_ev_country_type = yes
						OR = {
							has_origin = origin_ev_both_choosen
							has_country_flag = has_ev_to_planets
						}
					}
				}
			}
		"
		toggle = @homeless_class_trait_weight
	}
	# Madoka Magica NEW
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NOT = { has_trait = trait_mahoshoujo }
		"
		toggle = @madoka_compatch_var
	}
	# Vanilla
	if = {
		limit = {
			OR = {
				merg_is_sapient = no
				# Diagrapher's Trait Mod
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_trait = trait_zombie
						dtraits_colonization_restrictions = yes
					"
					toggle = @has_diagraphers_trait_mod
				}
			}
		}
		custom_tooltip = { always = no }
	}
	else = { always = yes }
	# Gigastructural Engineering & More
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			giga_can_colonize_with_pop = yes
		"
		toggle = @has_gigastructures
	}
}

#Root = country
#This = species
can_colonize_with_species = {
	has_colonization_control = {
		type = colonization_control_no
		country = root
	}
	NOR = {
		# ThaumStellaris
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				species = {
					OR = {
						is_archetype = TC_GOLEM
						is_archetype = TC_GOLEM_HIVE
						is_archetype = TC_GOLEM_MACHINE
					}
				}
			"
			toggle = @has_tc_mod
		}
		# Assault Lily
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_species_class = PUFFER
			"
			toggle = @change_planet_size_bullet_num
		}
	}
	OR = {
		NOT = { has_trait = trait_self_modified }
		is_same_species = root
	}
	# The Empty Vagrant
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			root = {
				OR = {
					is_default_ev_country_type = no
					AND = {
						is_default_ev_country_type = yes
						OR = {
							has_origin = origin_ev_both_choosen
							has_country_flag = has_ev_to_planets
						}
					}
				}
			}
		"
		toggle = @homeless_class_trait_weight
	}
	# Vanilla
	if = {
		limit = {
			OR = {
				merg_is_sapient = no
				# Diagrapher's Trait Mod
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_trait = trait_zombie
						dtraits_colonization_restrictions = yes
					"
					toggle = @has_diagraphers_trait_mod
				}
			}
		}
		custom_tooltip = { always = no }
	}
	else_if = {
		limit = { has_trait = trait_hive_mind }
		root = { is_hive_empire = yes }
	}
	# Lithoid Supremacy
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = { is_pearl_born_drone = yes }
				custom_tooltip = { always = no }
			}
		"
		toggle = @has_lithoid_mod
	}
	# Vanilla
	else = { always = yes }
}

# pop_has_random_ethics

# Root = Species
# From = Country
# This = Species
species_has_happiness = {
	AND = {
		# Timemod
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				TIME_RULE_trgr_Force_has_happiness = no
			"
			toggle = @timemod
		}
		# Vanilla
		NOR = {
			AND = {
				has_trait = trait_machine_unit
				exists = FROM
				FROM = { is_machine_empire = yes }
				# Ethics & Civics Bug Branch
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						FROM = { is_bugged_happy_gestalt = no }
					"
					toggle = @has_bug_branch
				}
			}
			AND = {
				has_trait = trait_hive_mind
				# Ethics & Civics Bug Branch
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						FROM = { is_bugged_happy_gestalt = no }
					"
					toggle = @has_bug_branch
				}
			}
			can_think = no
			# Diagrapher's Trait Mod
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					dtraits_happiness_restrictions = yes
				"
				toggle = @has_diagraphers_trait_mod
			}
			# ThaumStellaris
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					species = {
						OR = {
							is_archetype = TC_GOLEM
							is_archetype = TC_GOLEM_HIVE
							is_archetype = TC_GOLEM_MACHINE
						}
					}
				"
				toggle = @has_tc_mod
			}
			# Trait Diversity
			td_has_no_happiness = yes
			# Warship Girls R And MIST Species
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					wg_compat_has_sh_trait = yes
				"
				toggle = @wsg_mod
			}
			# Zob
			is_zob_uplifted = yes
			# Vanilla
			AND = {
				has_trait = trait_mechanical
				# Vanilla
				OR = {
					NOT = { exists = FROM }
					FROM = {
						OR = {
							is_machine_empire = yes
							NOT = { has_technology = tech_synthetic_workers }
							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
						}
					}
				}
			}
		}
	}
}

# can_be_hostile_towards

# Checks if a pop can procreate
# This = Species
# Root = Planet
can_species_procreate = {
	is_sapient = yes
	NOR = {
		has_trait = trait_machine_unit
		has_trait = trait_mechanical
		has_trait = trait_limited_cybernetic
		# Diagrapher's Trait Mod
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				dtraits_procreation_restrictions = yes
			"
			toggle = @has_diagraphers_trait_mod
		}
		# Ethics and Civics Bug Branch
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_bugged_happy_machine = yes
				AND = {
					is_exp_clone = yes
					root = {
						owner = {
							OR = {
								has_country_flag = clone_infertility_5
								AND = {
									NOT = { has_origin = origin_clones }
									years_passed > 200
								}
							}
						}
					}
				}
				AND = {
					root = { owner = { has_origin = origin_hive_queen } }
					has_trait = trait_hive_mind
				}
			"
			toggle = @has_bug_branch
		}
		# Expanded Events
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_exp_memorex_comatos = yes
			"
			toggle = @expevents_compatch_var
		}
		# Expanded Gestalts: Forgotten Queens
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_fq_trait_hive_mind_overlord = yes
			"
			toggle = @has_forgotten_queens
		}
		# Expanded Pops
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					is_exp_clone = yes
					root = {
						owner = {
							OR = {
								has_country_flag = clone_infertility_5
								AND = {
									NOT = { has_origin = origin_clones }
									years_passed > 200
								}
							}
						}
					}
				}
			"
			toggle = @expops_compatch_var
		}
		# Girls Frontline
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_trait = GF_trait_doll
			"
			toggle = @gf_compatch_var
		}
		# Lithoid Supremacy
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_pearl_born_drone = yes
			"
			toggle = @has_lithoid_mod
		}
		# Madoka Magica NEW
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_trait = trait_mahoshoujo
			"
			toggle = @madoka_compatch_var
		}
	}
	NAND = {
		root = { has_modifier = slave_colony }
		NOT = { has_citizenship_type = { type = citizenship_slavery country = root.owner } }
		# Special Colonies Overhaul
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				NOT = {
					root.owner = {
						OR = {
							has_valid_civic = civic_slaver_guilds
							has_valid_civic = civic_indentured_assets
						}
					}
				}
			"
			toggle = @is_SCO_enabled
		}
	}
	# Diagrapher's Trait Mod
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			dtraits_force_decline_exotic_gases = no
			dtraits_force_decline_delicates_enslaved = no
		"
		toggle = @has_diagraphers_trait_mod
	}
	# Expanded Traits, Civics, Pops, and More
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			if = {
				limit = { is_exp_tree_caretaker = yes }
				root = {
					OR = {
						has_deposit = d_tree_of_life_colony
						has_deposit = d_tree_of_life_home
					}
				}
			}
		"
		toggle = @expops_compatch_var
	}
	# Planetary Population Restrictions
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			policies_allow_breeding = yes
		"
		toggle = @planet_pop_restrict_mod
	}
	# V_Traits
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			if = {
				limit = { has_v_difficult_reproduction = yes }
				root = { habitability = { who = this value >= 0.8 } }
			}
			if = {
				limit = { has_v_heterogamous_reproduction = yes }
				root = { habitability = { who = this value <= 0.8 } }
			}
			if = {
				limit = { has_v_embryonic_diapause = yes }
				root = { NOT = { has_v_diapause_modifier = yes } }
			}
			if = {
				limit = { is_v_symbiont_a = yes }
				root = { check_variable = { which = v_num_symbiont value <= 0 } }
			}
			if = {
				limit = { is_v_symbiont_b = yes }
				root = { check_variable = { which = v_num_symbiont value >= 0 } }
			}
			if = {
				limit = { has_v_behavioral_sink = yes }
				root = { check_variable = { which = v_behavioral_sink_count value >= 0 } }
			}
		"
		toggle = @has_v_traits_mod
	}
	# Vanilla
	if = {
		limit = { has_trait = trait_necrophage }
		NOT = {
			root = {
				any_owned_species = {
					has_population_control = { country = root.owner type = no }
					species_can_be_necrophaged = yes
				}
			}
		}
	}
	NOT = { has_species_flag = nivlac_no_procreation }
	if = {
		limit = {
			has_cybernetic_creed_trait = yes
		}
		NOT = {
			root = {
				any_owned_species = {
					has_population_control = { country = root.owner type = no }
					has_cybernetic_creed_trait = no
				}
			}
		}
	}
	if = {
		limit = {
			has_cybernetic_creed_trait = no
		}
		root = {
			any_owned_species = {
				has_population_control = { country = root.owner type = no }
				has_cybernetic_creed_trait = no
			}
		}
	}
}

# Checks if a pop can be assembled
# This = Species
# Root = Planet
can_species_be_assembled = {
	is_sapient = yes
	NOT = { has_trait = trait_exd }
	NOT = {
		AND = {
			has_trait = trait_mechanical
			root.owner = { is_individual_machine = yes }
		}
	}
	NAND = {
		AND = {
			has_virtual_species_trait = yes
			root.owner = {
				is_fallen_empire = no
				has_active_tradition = tr_virtuality_finish
			}
		}
		# Genshin Impact
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					has_lingjing_species_trait = yes
					root.owner = { is_fallen_empire = no }
				}
			"
			toggle = @trait_shijianzhe_sci_amt
		}
	}
	# Expanded Gestalts: Forgotten Queens
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NAND = {
				root = { owner = { has_valid_civic = civic_machine_replicators } }
				is_fq_trait_machine_central = yes
			}
		"
		toggle = @has_forgotten_queens
	}
	# Vanilla
	OR = {
		# Arknights Near Future
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					has_trait = trait_ARK_ocean_seaborn
					root.owner = { has_country_flag = arknights_ocean_country }
				}
			"
			toggle = @arknights_ANF
		}
		# Eternal Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_archetype = ASCENDED
			"
			toggle = @has_eternal_empire
		}
		# Genshin Impact
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					is_archetype = TIWATE
					root = { check_modifier_value = { modifier = planet_pop_assembly_add value > 0 } }
				}
			"
			toggle = @trait_shijianzhe_sci_amt
		}
		# Robots
		AND = {
			is_robotic = yes
			# Girls Frontline
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					if = {
						limit = { has_trait = GF_trait_doll }
						root.owner = { GF_can_build_doll_species = yes }
					}
				"
				toggle = @gf_compatch_var
			}
			# Voyage to Eternity
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					KZ_VOY_trgr_plnt_ASSEMBLY = yes
					if = {
						limit = { vte_compat_magic_puppet = yes }
						KZ_VOY_can_assemble_magic_puppet = yes
					}
				"
				toggle = @vte_mod
			}
			# Vanilla
			root = {
				check_modifier_value = { modifier = planet_pop_assembly_add value > 0 }
				owner = {
					NOR = {
						has_country_flag = synth_assembly_stopped
						# Azur lane Stellaris DLC
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_authority = auth_azur_lane_1
								has_authority = auth_azur_lane_2
								has_authority = auth_azur_lane_k
							"
							toggle = @azl_mod
						}
					}
				}
			}
			# Machines & Robot Expansion Continued
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_xvcv_mdlc_trait_bio_robot = no
				"
				toggle = @oxr_mdlc_mod
			}
		}
		# Organics
		AND = {
			is_organic_species = yes
			# Hives can only clone drones
			if = {
				limit = { root.owner = { is_hive_empire = yes } }
				OR = {
					has_trait = trait_hive_mind
					# Ethics and Civics - Bug Branch
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_bugged_happy_drone = yes
						"
						toggle = @has_bug_branch
					}
					# Ethics and Civics: Infinity
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_eci_psionic_link = yes
						"
						toggle = @post_revolutionary_re_revolution_years
					}
				}
			}
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					else_if = {
						limit = {
							root = {
								owner = { has_origin = origin_sacred_tree }
								OR = {
									has_deposit = d_tree_of_life_home
									has_deposit = d_tree_of_life_colony
								}
							}
						}
						OR = {
							is_exp_tree_caretaker = yes
							root = {
								OR = {
									merg_has_cloning_building = yes
									has_modifier = tiyanki_trophy
									owner = { has_modifier = syamelles_curse }
									AND = {
										has_building = building_posthumous_employment_center
										owner = { has_civic = civic_permanent_employment }
									}
									has_planet_flag = can_organic_assemble_flag
								}
							}
						}
					}
				"
				toggle = @expops_compatch_var
			}
			# flourishing plantoids
			else_if = {
				limit = { has_budding_trait = yes }
				root = { any_owned_species = { is_exact_same_species = prevprev } }
			}
			else_if = {
				limit = { has_trait = trait_tiyanki }
				root = {
					any_owned_species = { is_exact_same_species = prevprev }
				}
			}
			# Lithoid supremacy
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					else_if = {
						limit = {
							OR = {
								is_pearl_born_drone = yes
								is_pearl_born_royal = yes
							}
						}
						root = {
							any_owned_species = {
								OR = {
									is_pearl_born_drone = yes
									is_pearl_born_royal = yes
								}
							}
						}
					}
				"
				toggle = @has_lithoid_mod
			}
			# Clone army
			else_if = {
				limit = {
					OR = {
						has_trait = trait_clone_soldier_infertile
						has_trait = trait_clone_soldier_infertile_full_potential
					}
				}
				root = {
					# Event 'clones.3' should cover this, but extra safety check.
					merg_has_clone_army_building = yes
					NOT = { check_variable = { which = clone_pops_missing value = 0 } }
				}
			}
			# Nirvana Empire Project
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					else_if = {
						limit = {
							root.owner = {
								OR = {
									has_valid_civic = civic_nirvana_empire_dragon
									has_valid_civic = civic_nirvana_empire_council
								}
							}
							is_same_species = root.owner
						}
						root = { check_modifier_value = { modifier = planet_pop_assembly_organic_add value > 0 } }
					}
				"
				toggle = @nirvana_mod
			}
			# V_TRAITS
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					else_if = {
						limit = { has_v_asexual_reproduction = yes }
						root = {
							any_owned_species = { is_exact_same_species = prevprev }
						}
					}
				"
				toggle = @has_v_traits_mod
			}
			# Vanilla
			else = {
				OR = {
					# Assault Lily
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_archetype = LILY
						"
						toggle = @change_planet_size_bullet_num
					}
					# EDA
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_archetype = Areta
						"
						toggle = @eda_mod
					}
					# Eternal Empire
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_archetype = ASCENDED
						"
						toggle = @has_eternal_empire
					}
					# KDC Story Pack
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_archetype = NANITES_CROP
						"
						toggle = @gray_t1_tech_days
					}
					# Star Wars: UnknownWorld Shipgirls Story
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_archetype = STARSHIPGIRLS
						"
						toggle = @swuss_mod
					}
					# Warship Girls R and MIST species
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_archetype = WARSHIPGIRL
							is_archetype = SHENHAI
							is_archetype = UNITED_FLEET
						"
						toggle = @wsg_mod
					}
					# Vanilla
					root = {
						OR = {
							# Vanilla
							merg_has_cloning_building = yes
							has_modifier = tiyanki_trophy
							owner = { has_modifier = syamelles_curse }
							AND = {
								has_building = building_posthumous_employment_center
								owner = { has_civic = civic_permanent_employment }
							}
							has_planet_flag = can_organic_assemble_flag #for mod compatibility
						}
					}
				}
			}
			# Special Colonies Overhaul
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					NAND = {
						root = { has_modifier = slave_colony }
						NOT = { has_citizenship_type = { type = citizenship_slavery country = root.owner } }
						not = {
							root.owner = {
								or = {
									has_valid_civic = civic_slaver_guilds
									has_valid_civic = civic_indentured_assets
								}
							}
						}
					}
				"
				toggle = @is_SCO_enabled
			}
		}
		# Azur lane Stellaris DLC
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					is_Azur_Lane_organic_species = yes
					root = { check_modifier_value = { modifier = planet_pop_assembly_organic_add value > 0 } }
				}
			"
			toggle = @azl_mod
		}
		# Steven Universe Homeworld Gems
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_geoloid = yes
			"
			toggle = @has_f93_gem_mod
		}
		# Voyage to Eternity
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				KZ_VOY_trgr_plnt_CLONE = yes
			"
			toggle = @vte_mod
		}
	}
	# Voyage to Eternity
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			if = {
				limit = { vte_compat_sub_dragon = yes }
				KZ_VOY_trgr_plnt_CLONE = yes
			}
		"
		toggle = @vte_mod
	}
	# TimeMod
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			TIME_RULE_trgr_CAN_spcs_be_assembled = yes
		"
		toggle = @timemod
	}
	# V_TRAITS
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			if = {
				limit = { has_v_difficult_reproduction = yes }
				root = { habitability = { who = this value >= 0.8 } }
			}
			if = {
				limit = { has_v_embryonic_diapause = yes }
				root = { NOT = { has_v_diapause_modifier = yes } }
			}
			if = {
				limit = { is_v_symbiont_a = yes }
				root = { check_variable = { which = v_num_symbiont value <= 0 } }
			}
			if = {
				limit = { is_v_symbiont_b = yes }
				root = { check_variable = { which = v_num_symbiont value >= 0 } }
			}
			if = {
				limit = { has_v_behavioral_sink = yes }
				root = { check_variable = { which = v_behavioral_sink_count value >= 0 } }
			}
		"
		toggle = @has_v_traits_mod
	}
	# Time Framework
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			TIME_RULE_trgr_CAN_spcs_be_assembled = yes
			better_ascension_traits_RULE_trgr_CAN_spcs_be_assembled = yes
		"
		toggle = @time_framework
	}
	# Vanilla
	has_population_control = { country = root.owner type = no }
}

# founder_species_uses_growth_speed
# founder_species_uses_organic_assembly
# founder_species_uses_assembly

#This rule is a condition for bombarding planets
#Root = fleet, potential attacker
#From = planet, potential target
can_orbital_bombard = {
	exists = FROM.OWNER
	# ACOT
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NAND = {
				OWNER = { is_progenitor_empires = no }
				FROM = {
					OR = {
						is_acot_pmc_vault = yes
						is_acot_awesome_planet_class = yes
					}
				}
			}
		"
		toggle = @has_ancient_caches
	}
	# SOTS
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NAND = {
				OWNER = { is_progenitor_empires = no }
				FROM = { is_sots_soulsphere = yes }
			}
		"
		toggle = @has_sots_mod
	}
	# Vanilla
	NOR = {
		any_controlled_ship = { is_ship_size = colossus }
		# Real Space New Frontiers
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				FROM = { is_planet_class = pc_hollow }
			"
			toggle = @rs_compatch_var
		}
		# Star Wars: Kuat Ancient Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				FROM.OWNER = { is_country_type = kuat_fire_base_hositle }
				FROM = { has_planet_flag = enable_kuat_shield }
			"
			toggle = @sw_kuat_mod
		}
	}
	OR = {
		AND = {
			ROOT.CONTROLLER = {
				NOT = { has_communications = FROM.OWNER }
			}
			FROM.CONTROLLER = {
				NOT = { is_country_type = primitive }
			}
		}
		AND = {
			ROOT.CONTROLLER = {
				NOR = {
					has_policy_flag = interference_not_allowed
					has_policy_flag = interference_subtle
				}
			}
			FROM.CONTROLLER = { is_country_type = primitive }
		}
		# Vanilla
		ROOT.CONTROLLER = { is_hostile = FROM.CONTROLLER }
		ROOT.CONTROLLER = { is_at_war_with = FROM.CONTROLLER }
		FROM = {
			OR = {
				is_planet_class = pc_infested
				# LEX
				has_planet_flag = bwm_ancestor_world
			}
		}
		FROM.CONTROLLER = {
			OR = {
				is_country_type = swarm
				is_country_type = ai_empire
				is_country_type = cybrex_empire
				AND = {
					is_country_type = awakened_marauders
					root.CONTROLLER = { is_hostile = prev }
				}
				has_country_flag = can_be_bombarded
				# Ancient Empire
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						ag_can_be_orbital_bombardment_country = yes
					"
					toggle = @ag_null_edict_length
				}
				# ACOT
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = sbtg_stellarborne_empire
						is_country_type = void_lifeforms_1
						is_country_type = void_lifeforms_2
						is_country_type = katzenartig_imperium
						is_country_type = acot_phanon_base
						is_country_type = acot_phanon_retaliator
					"
					toggle = @has_ancient_caches
				}
				# Star Wars: Kuat Ancient Empire
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						kuat_can_be_orbital_bombarment_country = yes
					"
					toggle = @sw_kuat_mod
				}
				# Gigastructural Engineering & More
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = katzenartig_imperium
						is_country_type = compound_empire
						is_country_type = giga_eawaf_sirens
					"
					toggle = @has_gigastructures
				}
				# LEX
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = mem_ancestor_guardian
					"
					toggle = @lex_compatch_var
				}
				# Scripted Triggers Undercoat
				has_country_flag = stu_bombardable_by_everyone
				# Shroud Rising
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = shroud_horrors
						is_country_type = shroud_cult
					"
					toggle = @has_shroud_rising_mod
				}
				# Solar Storm
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = abyssal_host_invaders_type
					"
					toggle = @ss_abyssal_laser_hull_damage
				}
				# StellarRegulator Array
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						SRA_can_do_orbital_bombardment_country = yes
					"
					toggle = @SRA_t_weapon_shield_damage
				}
				# The Sleeper
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_sleeper = yes
					"
					toggle = @has_the_sleeper_mod
				}
			}
		}
		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 - Ariphaos Patch - Should be governed by hostile rule
				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
				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
					}
				}
				# Ancient Empire
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						ag_can_do_orbital_bombardment_country = yes
					"
					toggle = @ag_null_edict_length
				}
				# ACOT
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_progenitor_empires = yes
						is_country_type = void_lifeforms_1
						is_country_type = void_lifeforms_2
						is_country_type = acot_crazed_stellarite
						is_country_type = acot_phanon_base
						is_country_type = acot_phanon_retaliator
					"
					toggle = @has_ancient_caches
				}
				# Destiny: The Darkness Crisis
				is_country_type = darkness_country
				# Gigastructural Engineering & More
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = katzenartig_imperium
						is_country_type = acot_crazed_stellarite
						is_country_type = compound_empire
						is_country_type = giga_hypersiphon_major_incursion
						is_country_type = giga_eawaf_sirens
					"
					toggle = @has_gigastructures
				}
				# AshArms Aquarius History
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = LsE_fallen_empire
					"
					toggle = @asharm_mod
				}
				# LEX
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = mem_ancestor_guardian
					"
					toggle = @lex_compatch_var
				}
				# More Events Mod
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_country_flag = mem_severance_harvesting_country
					"
					toggle = @has_more_events_mod
				}
				# Replicators
				is_country_type = replicators
				# Scripted Triggers Undercoat
				has_country_flag = stu_can_bombard_everyone
				# Shroud Rising
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = shroud_horrors
						is_country_type = shroud_cult
					"
					toggle = @has_shroud_rising_mod
				}
				# Solar Storm
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = abyssal_host_invaders_type
					"
					toggle = @ss_abyssal_laser_hull_damage
				}
				# Star Wars: Kuat Ancient Empire
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = mercenary_fleet
						kuat_can_do_bombarment_country = yes
					"
					toggle = @sw_kuat_mod
				}
				# Star Wars: UnknownWorld Shipgirls Story
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = afk_crisis_country
					"
					toggle = @swuss_mod
				}
				# The Empty Vagrant
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_default_ev_country_type = yes
					"
					toggle = @homeless_class_trait_weight
				}
				# The Sleeper
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_sleeper = yes
					"
					toggle = @has_the_sleeper_mod
				}
				# Warship Girls R And MIST Species
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_khan_country_type = yes
						is_country_type = wg_crisis_country
					"
					toggle = @wsg_mod
				}
			}
		}
		AND = {
			FROM.controller = {
				is_country_type = faction
				NOT = { is_same_value = root.CONTROLLER }
			}
			FROM.owner = {
				is_same_value = root.CONTROLLER
			}
		}
		# More Events Mod
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					exists = root.OWNER
					exists = FROM.CONTROLLER
					root.CONTROLLER = {
						OR = {
							is_country_type = mem_surveyor_country_red
							is_country_type = mem_vazurans
						}
					}
					FROM.CONTROLLER = { merg_is_default_empire = yes }
				}
			"
			toggle = @has_more_events_mod
		}
		# The Empty Vagrant
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				root.CONTROLLER = { is_country_type = ev_leviathans_crisis }
			"
			toggle = @homeless_class_trait_weight
		}
	}
	# TimeMod
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			TIME_RULE_trgr_CAN_orbital_bombard = yes
		"
		toggle = @time_framework
	}
}

# can_planet_support_orbital_station

# 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
		# ACOT SBTG
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_megastructure = sofe_omega_spy_orb_center
			"
			toggle = @has_sbtg_mod
		}
		# Frameworld extra goodies
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				xhk_has_sentry_deposit = yes
			"
			toggle = @xhk_frame_mod
		}
	}
}

# show_notification_for_ground_combat
# should_apply_neighbor_rival_modifier
# are_rebels_hostile
# should_colony_be_destroyed_on_pc_change
# is_extradimensional_portal
# is_extradimensional_anchor

# 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 = {
				OR = {
					NOT = { has_relic = r_severed_head }
					# Ethics and Civics Infinity
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_origin = origin_zarqlanism_idea
							NOT = { has_origin = origin_im_your_father }
						"
						toggle = @post_revolutionary_re_revolution_years
					}
				}
			}
		}
		# ZOFE
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					root = {
						AND = {
							is_country_type = lost_empire
							has_policy_flag = demand_clear_borders
						}
					}
					is_neighbor_of = root
				}
			"
			toggle = @has_zofe_vanilla
		}
		# Wula Fallen Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					root = { is_fallen_empire_machine = yes }
					solar_system = {
						has_star_flag = planet_guard_by_wula_machine_flag
					}
					space_owner = {
						NOT = { has_country_flag = was_enlightened }
					}
				}
			"
			toggle = @wula_compatch_var
		}
	}
}

# can_have_robot_pops
# can_stop_purge_pop_manually
# can_purge_pop_manually
# fleet_uses_templates

# this = starbase, megastructure or planet
can_orbitable_repair_ships = {
	OR = {
		AND = {
			is_scope_type = starbase
			OR = {
				has_starbase_size >= starbase_starport
				has_starbase_size = juggernaut
				has_starbase_size = enclave_mercenary_station
				# ACOT
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_acot_advanced_starbase = yes
					"
					toggle = @has_ancient_caches
				}
				# AOT
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_ascended_juggernaut = yes
						has_starbase_size = precursor_juggernaut
					"
					toggle = @has_acquisition_of_tech
				}
				# Assault Lily
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_starbase_size = lily_super_college_ship
						has_starbase_size = lily_college_ship
					"
					toggle = @change_planet_size_bullet_num
				}
				# Ethics & Civics Classic
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_starbase_size = enclave_pirate_mercenary_station
					"
					toggle = @has_ecc_cog
				}
				# ZOFE
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_starbase_size = fe_goliath
					"
					toggle = @has_acquisition_of_tech
				}
			}
			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
				# ACOT/AOT
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_megastructure_type = acot_precursor_colossus_base
						is_megastructure_type = acot_precursor_colossus_stage_two
						is_megastructure_type = acot_amerigo
						is_megastructure_type = acot_amerigo_ae
						is_megastructure_type = acot_amerigo_se
						is_megastructure_type = acot_amerigo_oe
						is_megastructure_type = acot_phanon_master_reactor_sphere
					"
					toggle = @has_ancient_caches
				}
				# Assault Lily
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_megastructure_type = college_ship_creater_3
					"
					toggle = @change_planet_size_bullet_num
				}
				# Gigastructural Engineering & More
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_megastructure_type = the_vat_1
						is_megastructure_type = the_vat_2
						is_megastructure_type = the_vat_3
						is_megastructure_type = the_vat_4
					"
					toggle = @has_gigastructures
				}
			}
		}
	}
}

# this = ship
# root = country that kills ship
can_spawn_debris = {
	root = { merg_is_default_empire = yes }
	exists = owner
	owner = {
		NOR = {
			has_ascension_perk = ap_enigmatic_engineering
			# Expanded Gestalts: Forgotten Queens
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_civic = civic_hive_strong_connection
				"
				toggle = @has_forgotten_queens
			}
			# AshArms Aquarius History
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
						is_country_type = LsE_fallen_empire
					"
				toggle = @asharm_mod
			}
			# Star Wars: UnknownWorld Shipgirls Story
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = afk_crisis_country
				"
				toggle = @swuss_mod
			}
			# The Empty Vagrant
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_default_ev_country_type = yes
				"
				toggle = @homeless_class_trait_weight
			}
			# Twinks Atlantean
			has_valid_civic = civic_tw_atlantis
			# Twinks Eternal Empire
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_government = gov_eternal_continuum
				"
				toggle = @has_eternal_empire
			}
			# Warship Girls R And MIST Species
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = wg_crisis_country
					is_country_type = wg_Khan_country_type
					is_country_type = wg_Khan_country_type_auto_delete
				"
				toggle = @wsg_mod
			}
		}
	}
}

# this = system
# root = country
# Determines if AI-controlled empires should restrict (and thus avoid) a system
# Used by regular empires, fallen empires & crises
ai_should_restrict_system = {
	OR = {
		AND = {
			root = {
				OR = {
					merg_is_fallen_empire = yes
					is_country_type = caravaneer_fleet
					# Twinks Eternal Empire
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = eternal_empire
						"
						toggle = @has_eternal_empire
					}
				}
			}
			exists = owner
			owner = {
				OR = {
					is_country_type = awakened_marauders
					# Ariphaos Patch
					is_country_type = dormant_marauders
					# The Empty Vagrant
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = awakened_marauders_ev
						"
						toggle = @homeless_class_trait_weight
					}
				}
			}
		}
		AND = {
			root = {
				OR = {
					is_country_type = caravaneer_fleet
					is_country_type = awakened_marauders
					# Ariphaos Patch
					is_country_type = marauder_raiders
					# The Empty Vagrant
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = awakened_marauders_ev
						"
						toggle = @homeless_class_trait_weight
					}
				}
			}
			exists = owner
			owner = { merg_is_fallen_empire = yes }
		}
		AND = {
			root = { is_country_type = voidworms }
			exists = owner
			owner = {
				OR = {
					is_country_type = dormant_marauders
					is_country_type = awakened_marauders
					# The Empty Vagrant
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = awakened_marauders_ev
						"
						toggle = @homeless_class_trait_weight
					}
				}
			}
		}
		AND = {
			root = {
				OR = {
					merg_is_fallen_empire = yes
					is_country_type = awakened_marauders
					is_country_type = marauder_raiders
					is_country_type = caravaneer_fleet
					is_country_type = voidworms
					# The Empty Vagrant
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = awakened_marauders_ev
						"
						toggle = @homeless_class_trait_weight
					}
				}
			}
			has_star_flag = guardian
			any_fleet_in_system = {
				exists = owner
				owner = { is_guardian_country = yes }
			}
		}
		AND = {
			root = {
				OR = {
					AND = {
						is_crisis_faction = yes
						# Gigastructural Engineering & More
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								is_blokkat_country_type = no
							"
							toggle = @has_gigastructures
						}
					}
					merg_is_awakened_fe = yes
				}
			}
			has_star_flag = guardian
			any_fleet_in_system = {
				exists = owner
				owner = { is_country_type = guardian_fortress }
			}
		}
		# SirBlackAxe's Unofficial Bug Patch
		AND = {
			root = {
				OR = {
					merg_is_fallen_empire = yes
					is_country_type = marauder_raiders
					is_country_type = caravaneer_fleet
				}
			}
			OR = {
				has_star_flag = lgate
				has_star_flag = lcluster
			}
			has_global_flag = gray_goo_crisis_active
		}
		AND = {
			root = {
				merg_is_default_empire = yes
				fleet_power < 30000
			}
			OR = {
				has_star_flag = lgate
				has_star_flag = lcluster
			}
			has_global_flag = gray_goo_crisis_active
		}
		# Gigastructural Engineering & More
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				giga_ai_should_restrict_system = yes
			"
			toggle = @has_gigastructures
		}
		# Spaceborne Civs Expanded
		AND = {
			root = {
				OR = {
					merg_is_fallen_empire = yes
					is_country_type = awakened_marauders
					is_country_type = marauder_raiders
					is_country_type = caravaneer_fleet
					# The Empty Vagrant
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = awakened_marauders_ev
						"
						toggle = @homeless_class_trait_weight
					}
				}
			}
			OR = {
				has_star_flag = crystal_home_system
				has_star_flag = amoeba_home_system
				has_star_flag = drone_home_system
			}
			any_fleet_in_system = {
				exists = owner
				owner = {
					OR = {
						is_crystal_country_type = yes
						is_amoeba_country_type = yes
						is_drone_country_type = yes
					}
				}
			}
		}
		# ACOT
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					root = { merg_is_default_empire = yes }
					exists = owner
					owner = { is_progenitor_empires = yes }
				}
			"
			toggle = @has_ancient_caches
		}
		# ACOT STBG
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					root = { merg_is_fallen_empire = yes }
					any_ship_in_system = {
						exists = owner
						owner = {
							OR = {
								is_guardian_country = yes
								is_country_type = sbtg_sentinel_active
							}
						}
					}
				}
			"
			toggle = @has_sbtg_mod
		}
		# A Deadly Tempest
		AND = {
			merg_has_deadly_tempest = yes
			root = {
				OR = {
					AND = {
						is_crisis_faction = yes
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								is_blokkat_country_type = no
							"
							toggle = @has_gigastructures
						}
					}
					has_country_flag = pike_assisted_ai
				}
			}
			has_star_flag = pike_star_flag_banned_system
		}
		# Ancient Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					root = {
						OR = {
							merg_is_fallen_empire = yes
							merg_is_awakened_fe = yes
							is_country_type = awakened_marauders
							is_country_type = ag_ancient_psi_guardian
							# The Empty Vagrant
							inline_script = {
								script = merger_of_rules/toggled_code
								code = \"
									is_country_type = awakened_marauders_ev
								\"
								toggle = @homeless_class_trait_weight
							}
						}
					}
					OR = {
						has_star_flag = ag_zeta_crisis_psionic_storm_system
						has_star_flag = ag_zeta_crisis_psionic_singularity_system
					}
				}
			"
			toggle = @ag_null_edict_length
		}
		# Harvest Engine Crisis
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					root = { is_harvester_country = yes }
					OR = {
						any_country_neighbor_to_system  = { merg_is_fallen_empire = yes }
						AND = {
							has_star_flag = guardian
							any_fleet_in_system = {
								exists = owner
								owner = { is_guardian_country = yes }
							}
						}
						AND = {
							exists = owner
							owner = { merg_is_fallen_empire = yes }
						}
					}
				}
			"
			toggle = @harvester_beamcage_ship_count
		}
		# Scripted Triggers Undercoat
		has_star_flag = stu_ai_restrict_system@root
		# The Sleeper
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					exists = owner
					has_star_flag = sleeper_system
					root = {
						merg_is_default_empire = yes
						is_homicidal = no
						OR = {
							AND = {
								OR = {
									has_valid_civic = civic_inwards_perfection
									has_global_flag = sleeper_warning_happened
								}
								NOT = { has_ethic = ethic_fanatic_xenophobe }
							}
							AND = {
								is_xenophile = yes
								is_militarist = no
							}
							is_pacifist = yes
							has_valid_civic = civic_machine_servitor
							AND = {
								is_active_resolution = resolution_sleeper_sanctions
								is_militarist = no
								OR = {
									is_xenophobe = no
									has_valid_civic = civic_inwards_perfection
								}
							}
							any_neighbor_country = { is_country_type = the_sleeper_fallen check_variable = { which = sleeper_aggro value > 500 } }
						}
					}
				}
				AND = {
					exists = owner
					root = { merg_is_fallen_empire = yes }
					OR = {
						owner = { is_marauder = yes }
						any_fleet_in_system = {
							exists = owner
							owner = { is_country_type = berserker_awoken_hive }
						}
						AND = {
							owner = { is_country_type = the_sleeper_fallen }
							has_star_flag = sleeper_system
						}
					}
				}
			"
			toggle = @has_the_sleeper_mod
		}
		# Forgotten Empires
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					has_star_flag = scfe_ai_avoid_system
					NOT = { exists = owner }
				}
			"
			toggle = @forgotten_empires
		}
	}
}

# can_subjugate_empires
# can_leader_get_council_trait
# can_leader_get_normal_trait
# can_leader_get_subclass_trait
# can_leader_get_destiny_trait
# has_extra_council_position

# this = Fleet
can_jump_drive = {
	# Ancient Empire
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = ag_jump_drive_unable_system_tooltip
				NOT = { solar_system = { has_star_flag = ag_no_jump_out_system } }
			}
		"
		toggle = @ag_null_edict_length
	}
	# Assault Lily
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = AL_cannot_start_jump
				AND = {
					exists = solar_system
					NOT = {
						solar_system = {
							exists = starbase
							starbase = {
								has_starbase_building = AL_AntiFTL
								owner = { is_hostile = prevprevprev.owner }
							}
						}
					}
				}
			}
		"
		toggle = @change_planet_size_bullet_num
	}
	# Some Events of Outside Cluster
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			if = {
				limit = {
					exists = solar_system
					solar_system = { has_star_flag = nep_Scrus_cluster }
				}
				custom_tooltip = {
					text = CAN_NOT_JUMP_WHEN_IN_SCRUS
					always = no
				}
			}
			else_if = {
				limit = {
					exists = solar_system
					solar_system = {
						OR = {
							has_star_flag = nep_whitegoo_1
							has_star_flag = nep_whitegoo_2
							has_star_flag = nep_whitegoo_3
						}
					}
					owner = { NOT = { has_technology = tech_nep_whitegoo_3_2 } }
				}
				custom_tooltip = {
					text = CAN_NOT_JUMP_IN_WHITEGOO
					always = no
				}
			}
			else = { always = yes }
		"
		toggle = @has_seooc_mod
	}
	# Fallback - if no mod is present, then set the vanilla value
	inline_script = {
		script = merger_of_rules/toggled_code_inverse
		code = "
			always = yes
		"
		toggle = @[ ag_null_edict_length + change_planet_size_bullet_num + has_seooc_mod ]
	}
}

# this = country
can_spawn_occupation_armies = {
	OR = {
		is_country_type = default
		merg_is_fallen_empire = yes
		merg_is_awakened_fe = yes
		# ACOT SBTG
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_progenitor_empires = yes
				is_country_type = sbtg_light_seeker
			"
			toggle = @has_sbtg_mod
		}
		# ZOFE
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_zofe_oe = yes
			"
			toggle = @has_zofe_vanilla
		}
	}
}

# this = system
# root = country
system_blocks_sensors = {
	# Vanilla, includes code FROM Ariphaos' Sensor Expansion
	OR = {
		AND = { # Nebulae
			OR = {
				is_inside_nebula = yes
				has_modifier = space_storm
				has_modifier = zroni_storm
				has_modifier = queen_scorn_storm
				merg_is_nebulae = yes
				# Wild Space
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_ws_system = yes
					"
					toggle = @has_wild_space_mod
				}
				# Real Space
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_star_class = sc_protostar
						is_star_class = sc_tt_red
						is_star_class = sc_tt_orange
						is_star_class = sc_tt_white
						is_star_class = sc_ae
					"
					toggle = @rs_compatch_var
				}
			}
			root = {
				NOR = {
					# Ariphaos Sensor Expansion
					has_technology = tech_ariphaos_sensors_nebula
					# Wild Space
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_technology = tech_WS_sensors_nebula_1
						"
						toggle = @has_wild_space_mod
					}
				}
			}
		}
		AND = { # Clusters, Sealed systems and Storms
			OR = {
				has_star_flag = lcluster
				has_star_flag = sealed_system
				has_star_flag = storm_system
				# Ariphaos Sensor Expansion
				has_star_flag = surveillance_supercomputer_system
			}
			# Making sure modded hidden systems will work as intended if Sensor Expansion is installed
			NOR = {
				# Ancient Empire
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_star_flag = ag_no_sensor_system
					"
					toggle = @ag_null_edict_length
				}
				# Solar Storm
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_star_flag = ss_abyssal_shockwave
					"
					toggle = @ss_abyssal_laser_hull_damage
				}
				# Warship Girls R And MIST Species
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_star_flag = wg_sealed_system
						has_star_flag = wg_psionic_systems
					"
					toggle = @wsg_mod
				}
			}
			root = { NOT = { has_technology = tech_ariphaos_sensors_sealed } }
		}
		# Ancient Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_star_flag = ag_no_sensor_system
			"
			toggle = @ag_null_edict_length
		}
		# Arknight Near Future
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_star_flag = ark_isolated
			"
			toggle = @arknights_ANF
		}
		# Assault Lily
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					exists = sector
					sector.sector_capital = {
						exists = leader
						leader = { al_has_rare_gov_skill = yes }
					}
				}
			"
			toggle = @change_planet_size_bullet_num
		}
		# Destiny: The Darkness Crisis
		has_star_flag = "black_fleet_in_system"
        # Forgotten Empires
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					exists = owner
					owner = {
						has_relic = r_scfe_invisibility_rune
						has_modifier = scfe_invisibility_rune
					}
					NOT = { root = { is_same_empire = prev.owner } }
				}
			"
			toggle = @forgotten_empires
		}
		# Solar Storm
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_star_flag = ss_abyssal_shockwave
			"
			toggle = @ss_abyssal_laser_hull_damage
		}
		# Sensor Expansion
		AND = { # Neutron stars
			OR = {
				merg_has_sensor_expansion = yes
				has_aso_space_exploration = yes
			}
			merg_is_neutron_star_class = yes
			root = { NOT = { has_technology = tech_sensors_4 } }
		}
		AND = { # Pulsars
			OR = {
				merg_has_sensor_expansion = yes
				has_aso_space_exploration = yes
			}
			merg_is_pulsar_star_class = yes
			root = { NOT = { has_technology = tech_ariphaos_sensors_pulsar } }
		}
		AND = { # Wormholes
			OR = {
				merg_has_sensor_expansion = yes
				check_variable = { which = has_wild_space_mod value = 1 }
				has_aso_space_exploration = yes
			}
			has_natural_wormhole = yes
			root = {
				NOR = {
					has_technology = tech_ariphaos_sensors_wormhole
					# Wild Space
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_technology = tech_WS_sensors_wormhole
						"
						toggle = @has_wild_space_mod
					}
				}
			}
		}
		AND = { # Black and White Holes
			OR = {
				merg_has_sensor_expansion = yes
				has_aso_space_exploration = yes
			}
			merg_is_hole_star_class = yes
			root = { NOT = { has_technology = tech_ariphaos_sensors_black_hole } }
		}
		AND = { # Jammer
			merg_has_sensor_expansion = yes
			exists = starbase
			starbase = { has_starbase_building = ariphaos_sensors_jammer_starbase }
			root = { NOT = { has_technology = tech_ariphaos_sensors_jammed } }
		}
		# Warship Girls R And MIST Species
		has_star_flag = wg_sealed_system
		has_star_flag = wg_psionic_systems
	}
}

# Root = country attempting to target system for movement
# This = system being targeted
can_enter_system_by_jump = {
	custom_tooltip = {
		fail_text = cannot_jump_to_system
		OR = {
			AND = {
				# Sealed system
				OR = {
					NOT = { has_star_flag = sealed_system }
					AND = {
						root = { has_country_flag = entered_sealed_system }
						has_star_flag = sealed_system
					}
				}
				OR = {
					# Clusters and forbidden systems
					NOR = {
						has_star_flag = lcluster
						# Formless system
						has_star_flag = formless_system
						# ACOT SBTG
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_star_flag = outercluster
							"
							toggle = @has_sbtg_mod
						}
						# Ancient Empire
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_star_flag = ag_no_jump_in_system
							"
							toggle = @ag_null_edict_length
						}
						# Arknight Near Future
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_star_flag = ark_isolated
							"
							toggle = @arknights_ANF
						}
						# Assault Lily
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_star_flag = al_huge_home_bed
							"
							toggle = @change_planet_size_bullet_num
						}
						# Forgotten Empires
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								AND = {
									has_star_flag = scfe_shaded_colony_system
									root = { scfe_can_jump_in_shaded_system = no }
								}
							"
							toggle = @forgotten_empires
						}
						# Gigastructural Engineering and More
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_star_flag = ehof_compound_system
								has_star_flag = giga_core_03
								cohesive_system = yes
								urmazin_system = yes
								everchanging_system = yes
								katzen_jump_blocked_system = yes
								has_star_flag = maginot_anti_jump_star
								has_star_flag = siegfried_anti_jump_star
							"
							toggle = @has_gigastructures
						}
						# Origin Beings
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_star_flag = darhmod_seres_home_system
								has_star_flag = sealed_seres_system
								has_star_flag = seres_lcluster_spawned
								has_star_flag = ftl_spawned
								has_star_flag = su_lu_station_system
								has_star_flag = su_spawned
								has_star_flag = god_route_seres_star
								has_star_flag = vultaumar
								has_star_flag = yuhtaan
								has_star_flag = fen_habbanis
								has_star_flag = irass
								has_star_flag = cybrex
								has_star_flag = last_baol_system
								has_star_flag = Zrocursor
							"
							toggle = @origin_beings_mod
						}
						# Some Events of Outside Cluster
						has_star_flag = nep_Krahen_cluster
						has_star_flag = nep_Scrus_cluster
						AND = {
							has_global_flag = nep_not_pass_Aemethms
							OR = {
								has_star_flag = nep_dragon_system
								has_star_flag = nep_heptagram_cluster
							}
						}
						has_star_flag = nep_arena
						# Sparble's Stellar Hyperconstructs
						has_star_flag = ssh_ark_system
						has_star_flag = ssh_ark_jump_capital
						has_star_flag = ssh_ark_jump_cluster_core
						has_star_flag = ssh_ark_jump_new_system
						has_star_flag = ssh_basement_universe_center
						has_star_flag = ssh_basement_universe_system
						# SW:Kuat Ancient Empire
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								kuat_is_kuat_system = yes
								has_star_flag = ag_kae_e_area_system
								AND = {
									has_global_flag = starkillerbase_not_passed
									has_star_flag = starkiller_system
								}
							"
							toggle = @sw_kuat_mod
						}
						# Warship Girls R And MIST Species
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_star_flag = wg_sealed_system
								has_star_flag = wg_psionic_systems
							"
							toggle = @wsg_mod
						}
					}
					# Gigastructural Engineering and More
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							AND = {
								cohesive_system = yes
								root = { is_eager_explorer_empire = yes }
								has_star_flag = ehof_system_created_by_@root
							}
						"
						toggle = @has_gigastructures
					}
				}
			}
			# Reworked Advanced Ascension
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					root = { has_country_flag = AAR_flg_cntr_JUMP_webway_TGT }
				"
				toggle = @aar_svar_mod
			}
		}
	}
}

# This = pop
can_resettle_pop = {
	# 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 = {
			pop_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 }
			}
		}
	}
	# Expanded Traits, Civics, Pops, and More
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = caretaker_refuse
				NOT = { is_exp_tree_caretaker = yes }
			}
		"
		toggle = @expops_compatch_var
	}
	# Genshin Impact
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = no_has_lingjing_species_trait_apps
				NOT = { has_lingjing_species_trait = yes }
			}
		"
		toggle = @trait_shijianzhe_sci_amt
	}
	# V_Traits
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = is_v_mobile_cant_be_resetttled
				is_v_mobile = yes
			}
		"
		toggle = @has_v_traits_mod
	}
	#Done as an IF/ELSE so the tooltip isn't hideous and unreadable
	# Vanilla
	if = {
		limit = { is_shackled_robot = yes }
		always = yes
	}
	# Diagrapher's Trait Mod
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = { is_rooted = yes }
				is_rooted = no
			}
		"
		toggle = @has_diagraphers_trait_mod
	}
	# Vanilla
	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
				num_pops < 2
			}
			NOT = {
				owner = {
					any_owned_planet = {
						is_capital = no
						NOT = { 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 }
				num_pops < 2
			}
		}
		custom_tooltip = {
			fail_text = "no_unfueled_lathe"
			always = no
		}
	}
	else_if = {
		limit = {
			planet = {
				has_deposit = d_genesis_preserve
				count_owned_pop = { limit = { is_sapient = yes } count < 2 }
			}
			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
	}
	# Vanilla
	else = {
		custom_tooltip = {
			fail_text = RESETTLEMENT_POLICY_FAIL
			owner = { has_policy_flag = resettlement_allowed }
		}
	}
}

# This = Planet
can_resettle_planet = {
	owner = {
		OR = {
			has_policy_flag = resettlement_allowed
			is_gestalt = yes
			country_has_shackled_robots = yes
			has_ascension_perk = ap_cosmogenesis
			any_owned_planet = { is_doomsday_planet = yes }
		}
	}
	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
	}
	# Gigastructural Engineering & More
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			giga_can_resettle_planet = yes
		"
		toggle = @has_gigastructures
	}
}

# can_resettle_planet_no_owner_check
# should_spawn_deposits
# can_get_colony_events
# is_new_colony

# This = pop
pop_generates_crime = {
	is_sapient = yes
	is_non_sapient_robot = no
	is_being_assimilated = no
	NOR = {
		# Warship Girls R And MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				OR = {
					has_wg_traits = yes
					wg_compat_has_intelligent_gallo = yes
					species = {
						OR = {
							is_archetype = UNITED_FLEET
							is_archetype = NANITES_CROP
						}
					}
				}
			"
			toggle = @wsg_mod
		}
		# Star Wars: UnknownWorld Shipgirls Story
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_swuss_starshipgirls = yes
			"
			toggle = @swuss_mod
		}
		# AshArms Aquarius History
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_trait = adf_trait_dolls
			"
			toggle = @asharm_mod
		}
	}
	# V_TRAITS
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			has_v_no_crime = no
		"
		toggle = @has_v_traits_mod
	}
}

# This = planet
can_pops_grow_on_planet = {
	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
					# Shroud Rising
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = shroud_cult
						"
						toggle = @has_shroud_rising_mod
					}
				}
			}
		}
	}
}

# can_pops_assemble_on_planet
# can_pops_decline_on_planet
# can_force_grow_on_planet
# can_force_decline_on_planet
# can_force_decline_species

## If this returns true, the species will be set to decline even if it is not being purged
# This = species
# From = planet
## 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 = {	# Can't have Vats.
									has_origin = origin_clone_army
								}
								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 }
				}
			}
		}
		# This slowly kills off the Synthetic Fertility Species
		custom_tooltip = {
			success_text = PATHOGENIC_GENES_FORCE_DECLINE
			exists = this
			has_trait = trait_pathogenic_genes
		}
		# Expanded Traits, Civics, Pops, and More
		# Caretakers die without the Tree of Life
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					custom_tooltip = {
						success_text = TREEFOLK_FORCE_DECLINE
						exists = this
						is_exp_tree_caretaker = yes
						FROM = {
							NOR = {
								has_deposit = d_tree_of_life_colony
								has_deposit = d_tree_of_life_home
							}
						}
					}
				}
			"
			toggle = @expops_compatch_var
		}
		# Expanded Events
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					exists = this
					is_exp_memorex_comatos = yes
				}
			"
			toggle = @expevents_compatch_var
		}
		# V_TRAITS
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				custom_tooltip = {
					success_text = WEAK_IMMUNE_SYSTEM_FORCE_DECLINE
					has_v_weak_immune_system = yes
					has_v_cyborg_bubble = no
					FROM = {
						any_owned_species = {
							NOR = {
								has_v_weak_immune_system = yes
								has_v_cyborg_bubble = yes
								has_trait = trait_machine_unit
								has_trait = trait_mechanical
							}
						}
					}
				}
			"
			toggle = @has_v_traits_mod
		}
		# Diagrapher's Trait Mod
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				dtraits_force_decline_exotic_gases = yes
				dtraits_force_decline_delicates_enslaved = yes
			"
			toggle = @has_diagraphers_trait_mod
		}
	}
}

# This = country
can_build_branch_offices = {
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_MEGACORP
		is_exile_country = no
		OR = {
			is_megacorp = yes
			has_valid_civic = civic_galactic_sovereign_megacorp
			# Arknights Near Future
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_valid_civic = civic_business_union
				"
				toggle = @arknights_ANF
			}
			# Assault Lily
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_authority = auth_AL_democratic
				"
				toggle = @change_planet_size_bullet_num
			}
			# Azur Lane Stellaris DLC
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					AND = {
						exists = event_target:elizabeth_leader
						has_country_flag = azur_lane_leader_elizabeth
					}
				"
				toggle = @azl_mod
			}
			# EDA
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_authority = auth_traveler
				"
				toggle = @eda_mod
			}
			# Expanded Gestalts: Forgotten Queens
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_valid_civic = civic_mutualistic_behavior
					has_valid_civic = civic_machine_trading
				"
				toggle = @has_forgotten_queens
			}
			# Girls Frontline
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_civic = civic_gf_GK
				"
				toggle = @gf_compatch_var
			}
			# Voyage to Eternity
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					KZ_VOY_can_build_branch_offices = yes
				"
				toggle = @vte_mod
			}
			# Warship Girls R And MIST Species
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					AND = {
						has_authority = auth_shenhai
						OR = {
							has_country_flag = pantsu_shop_origin
							has_valid_civic = civic_sh_criminal_syndicate
						}
					}
				"
				toggle = @wsg_mod
			}
		}
	}
}

# This = country
can_support_branch_offices = {
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_FALLEN_EMPIRES
		NOR = {
			merg_is_fallen_empire = yes
			merg_is_awakened_fe = yes
			# Assault Lily
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = al_fallen_empire
					is_country_type = al_fallen_empire_no_ai
				"
				toggle = @change_planet_size_bullet_num
			}
			# Azur Lane Stellaris DLC
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_authority = auth_azur_dictatorial
				"
				toggle = @azl_mod
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_ON_MEGACORP_PLANET
		# Vanilla
		OR = {
			NOR = {
				is_megacorp = yes
				# Assault Lily
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = huge_country
					"
					toggle = @change_planet_size_bullet_num
				}
				# Arknights Near Future
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_valid_civic = civic_business_union
					"
					toggle = @arknights_ANF
				}
				# Ethics & Civics Infinity
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_eci_megacorp = yes
					"
					toggle = @post_revolutionary_re_revolution_years
				}
				# More Corporate Authorities
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_valid_civic = civic_galactic_sovereign_megacorp
					"
					toggle = @has_more_corporate_authorities
				}
				# Warship Girls R And MIST Species
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						AND = {
							wg_compat_has_auth_shenhai = yes
							OR = {
								has_country_flag = pantsu_shop_origin
								has_valid_civic = civic_sh_criminal_syndicate
							}
						}
					"
					toggle = @wsg_mod
				}
			}
			# Unique Ascension Perks
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_ascension_perk = ap_megacorp
				"
				toggle = @has_uap_mod
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_REGULAR_EMPIRE
		NOR = {
			is_gestalt = yes
			# Merger of Rules
			is_country_type = gate_builders
			is_country_type = awakened_marauders
		}
	}
	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 = planet
# Root = country
can_build_branch_office_on_planet = {
	custom_tooltip = {
		fail_text = requires_recipient_not_fanatic_purifiers
		exists = owner
		owner = {
			OR = {
				is_same_species = root
				NOT = { has_valid_civic = civic_fanatic_purifiers }
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NO_OBSERVATION_OUTPOST
		if = {
			limit = {
				root = {
					OR = {
						is_criminal_syndicate = yes
						AND = {
							inline_script = {
								script = merger_of_rules/toggled_code
								code = "
									is_zofe_country = yes
								"
								toggle = @has_zofe_vanilla
							}
							has_civic = civic_criminal_heritage
						}
					}
				}
				owner = { is_primitive = yes }
			}
			has_observation_outpost = yes
			observation_outpost_owner = { is_same_empire = root }
		}
	}
	# Vanilla
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_NOT_COMMERCIAL_PACT
		exists = owner
		OR = {
			root = {
				OR = {
					is_criminal_syndicate = yes
					# Assault Lily
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_authority = auth_AL_democratic
						"
						toggle = @change_planet_size_bullet_num
					}
					# Azur Lane Stellaris DLC
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							root = {
								exists = event_target:elizabeth_leader
								has_country_flag = azur_lane_leader_elizabeth
							}
						"
						toggle = @azl_mod
					}
					# Voyage to Eternity
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							KZ_VOY_can_build_branch_offices = yes
						"
						toggle = @vte_mod
					}
					# Warship Girls R And MIST Species
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_civic = civic_sh_criminal_syndicate
						"
						toggle = @wsg_mod
					}
				}
			}
			owner = { has_commercial_pact = root }
			owner = { is_in_federation_with = root }
			AND = {
				has_observation_outpost = yes
				observation_outpost = { has_modifier = preftl_action_trade_pact }
			}
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					owner = { has_country_flag = cb_markets_opened@root }
				"
				toggle = @expops_compatch_var
			}
			# Warship Girls R And MIST Species
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					AND = {
						owner = { has_country_flag = lose_wg_shop_war }
						root = { has_country_flag = pantsu_shop_origin }
					}
				"
				toggle = @wsg_mod
			}
			# Vanilla
			# 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
				}
				exists = overlord
				overlord = {
					OR = {
						is_same_empire = prevprev.owner
						is_same_empire = prevprev.owner.overlord
					}
				}
			}
			owner = {
				is_subject = yes
				exists = overlord
				overlord = {
					OR = {
						is_same_empire = root
						is_same_empire = root.overlord
					}
					OR = {
						is_megacorp = yes
						has_civic = civic_galactic_sovereign_megacorp
					}
				}
			}
			AND = {
				root = {
					OR = {
						has_valid_civic = civic_galactic_sovereign_megacorp
						has_modifier = imperial_charter
					}
				}
				owner = { is_galactic_community_member = yes }
			}
			# Vassals Expanded and Reworked
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					OR = {
						root = {
							is_subject = yes
							overlord = {
								OR = {
									is_same_value = prevprev.owner
									AND = {
										exists = prevprev.owner.overlord
										is_same_value = prevprev.owner.overlord
									}
								}
							}
						}
						owner = {
							is_subject = yes
							overlord = { is_same_empire = root }
						}
					}
				"
				toggle = @has_vear_f_mod
			}
			# Unique Ascension Perks
			AND = {
				owner = {
					OR = {
						is_megacorp = yes
						is_gestalt = yes
					}
				}
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						root = { has_ascension_perk = ap_megacorp }
					"
					toggle = @has_uap_mod
				}
			}
			# EDA
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					owner = {
						AND = {
							OR = { has_authority = auth_traveler }
							is_same_value = root
						}
					}
				"
				toggle = @eda_mod
			}
		}
	}
	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 = {
				OR = {
					is_criminal_syndicate = no
					# Assault Lily
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_authority = auth_AL_democratic
						"
						toggle = @change_planet_size_bullet_num
					}
				}
			}
			owner = {
				NOR = {
					has_truce = root
					is_at_war_with = root
				}
			}
		}
	}
	custom_tooltip = {
		fail_text = BRANCH_OFFICE_CRIMINAL_COOLDOWN
		exists = owner
		OR = {
			AND = {
				root = { is_criminal_syndicate = yes }
				NOT = { has_planet_flag = criminal_branch_closed_cooldown_planet@root }
			}
			root = {
				OR = {
					is_criminal_syndicate = no
					# Assault Lily
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							has_authority = auth_AL_democratic
						"
						toggle = @change_planet_size_bullet_num
					}
				}
			}
			# Powerful & Realistic Orbital Bombardments
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					root = { is_criminal_syndicate = yes has_country_flag = prob_no_criminal_branch@root.owner }
				"
				toggle = @has_prob_mod
			}
		}
	}
	# Warship Girls R And MIST Species
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = requires_recipient_not_warshipgirls
				exists = owner
				owner = {
					NOR = {
						wg_compat_has_auth_warshipgirls = yes
						AND = {
							wg_compat_has_auth_shenhai = yes
							OR = {
								has_country_flag = pantsu_shop_origin
								has_valid_civic = civic_sh_criminal_syndicate
							}
						}
					}
				}
			}
		"
		toggle = @wsg_mod
	}
	# More Corporate Authorities
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			hidden_trigger = {
				OR = {
					NOT = { root = { has_mca_personality_principled_rogues = yes is_ai = yes } }
					owner = { their_opinion = { who = root value < 0 } }
					owner = {
						their_opinion = { who = root value < 20 }
						NOT = { has_opinion_modifier = { who = root modifier = opinion_new_contact is_reverse = yes } }
					}
				}
			}
		"
		toggle = @has_more_corporate_authorities
	}
	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 }
	}
	# Ethics and Civics Alternative - FunEFork
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			ecaf_can_build_branch_office_on_planet_trigger = yes
		"
		toggle = @has_eca_funefork
	}
}

# This = country
# Checked by is_criminal_syndicate trigger
is_criminal_syndicate_rule = {
	is_megacorp = yes
	OR = {
		has_valid_civic = civic_criminal_heritage
		# The Empty Vagrant
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_valid_civic = civic_ev_criminal_ship
			"
			toggle = @homeless_class_trait_weight
		}
		# Ethics & Civics Classic
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_valid_civic = civic_military_dictatorship_corporate
			"
			toggle = @has_ecc_cog
		}
		# Ethics & Civics Infinity
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_valid_civic = civic_family_business
				has_valid_civic = civic_underworld_society
				has_valid_civic = civic_world_cooperative
			"
			toggle = @post_revolutionary_re_revolution_years
		}
	}
}

# This = planet
can_crossbreed_species_on_planet = {
	# Vanilla
	exists = owner
	OR = {
		owner = {
			OR = {
				has_ascension_perk = ap_xeno_compatibility
				# Ethics & Civics Infinity
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						AND = {
							has_valid_civic = civic_liberty_of_libido
							has_ascension_perk = ap_evolutionary_mastery
						}
					"
					toggle = @post_revolutionary_re_revolution_years
				}
				# Kek's Origins+
				has_country_flag = kokobreeed
			}
		}
	}
	# Warship Girls R And MIST Species
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			any_owned_species = {
				NOR = {
					is_archetype = WARSHIPGIRL
					is_archetype = SHENHAI
				}
			}
		"
		toggle = @wsg_mod
	}
	# Vanilla - putting this last for performance
	count_species = {
		limit = { is_crossbreeding_possible = yes }
		count >= 2
	}
}

# This = species one
# Root = species two
can_crossbreed_species = {
	is_crossbreeding_possible = yes
	root = {
		is_crossbreeding_possible = yes
		NOR = {
			# Warship Girls R And MIST Species
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_wg_traits = yes
					is_archetype = UNITED_FLEET
				"
				toggle = @wsg_mod
			}
			# KDC Story Pack
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_archetype = NANITES_CROP
				"
				toggle = @gray_t1_tech_days
			}
		}
	}
	NOR = {
		is_subspecies = root
		root = { is_subspecies = prev }
		# Maiden Frontier Gunstar Story Collection
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_msgsc_doll = yes
			"
			toggle = @GF_OGAS_leveled_thread_add_start_after_level
		}
		# KDC Story Pack
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_archetype = NANITES_CROP
			"
			toggle = @gray_t1_tech_days
		}
		# Warship Girls R And MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
					has_wg_traits = yes
					is_archetype = UNITED_FLEET
			"
			toggle = @wsg_mod
		}
	}
}

# can_win_timed_victory

# This = country
can_use_galactic_market = {
	OR = {
		merg_is_default_empire = yes
		# ACOT SBTG
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_country_type = sbtg_stellarborne_empire_active
			"
			toggle = @has_sbtg_mod
		}
		# The Empty Vagrant
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					is_default_ev_country_type = yes
					NOT = { has_policy_flag = ev_no_use_galactic_market }
				}
			"
			toggle = @homeless_class_trait_weight
		}
		# ZOFE
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_country_type = lost_empire
				is_zofe_oe = yes
			"
			toggle = @has_zofe_vanilla
		}
	}
	inline_script = {
		script = merger_of_rules/toggled_code_inverse
		code = "
			is_homicidal = no
		"
		toggle = @galactic_community_overhaul
	}
	# Galactic Community Overhaul
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NOR = {
				is_homicidal_no_terminator = yes
				AND = {
					has_civic = civic_machine_terminator
					NOT = { has_global_flag = terminators_can_join_galcom }
				}
			}
		"
		toggle = @galactic_community_overhaul
	}
	# Ethics and Civics : Infinity
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NAND = {
				is_gestalt = yes
				NOT = {  has_tradition = tr_future_economy }
			}
		"
		toggle = @post_revolutionary_re_revolution_years
	}
}

# root = country
# this = pop
can_be_sold_on_slave_market = {
	exists = owner
	OR = {
		is_enslaved = yes
		is_non_sapient_robot = yes
		is_shackled_robot = yes
		is_being_purged = yes		# Ariphaos Patch
		# Expanded Traits, Civics, Pops, and More
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					root = { allows_slavery = yes }
					has_job = servant_noble
				}
			"
			toggle = @expops_compatch_var
		}
	}
	# Expanded Traits, Civics, Pops, and More
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NOT = { owner = { has_country_flag = gai_machines_have_organics } }
			OR = {
				root = { is_ai = no }
				is_on_slave_market = yes	# pops that were already on the market should stay there
				AND = {
					OR = { is_unemployed = yes has_job = servant_noble }
					planet = {
						count_owned_pop = {
							count > 2
							limit = {
								OR = {
									is_unemployed = yes
									has_job = servant_noble
								}
								NOR = {
									is_pop_category = specialist
									is_pop_category = ruler
								}
							}
						}
					}
				}
				AND = {
					OR = { has_job = servant has_job = servant_noble }
					planet = { free_amenities > 20 }
				}
			}
		"
		toggle = @expops_compatch_var
	}
	# Vanilla
	OR = {
		root = { is_ai = no }
		# has_citizenship_rights = no # Ariphaos Patch
		AND = {
			is_unemployed = yes
			is_being_assimilated = no			# Ariphaos Patch
			planet = {
				num_unemployed > 2				# Ariphaos Patch, vanilla = 3
			}
		}
		# Ariphaos Patch
		OR = {
			NOT = { has_trait = trait_syncretic_proles }
			root = {
				NOT = { has_origin = origin_syncretic_evolution }
			}
		}
	}
	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
	}
	# Vanilla
	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
		# Diagrapher's Trait Mod
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_rooted = yes
			"
			toggle = @has_diagraphers_trait_mod
		}
		# Genshin Impact
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_lingjing_species_trait = yes
			"
			toggle = @trait_shijianzhe_sci_amt
		}
		# Warship Girls R And MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_wg_traits = yes
			"
			toggle = @wsg_mod
		}
	}
	# V_TRAITS
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			is_v_mobile = yes
		"
		toggle = @has_v_traits_mod
	}
}

# This = country
can_be_part_of_galactic_community = {
	custom_tooltip = {
		fail_text = community_default_failtext
		OR = {
			merg_is_default_empire = yes
			# ACOT SBTG
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = sbtg_stellarborne_empire_active
				"
				toggle = @has_sbtg_mod
			}
			# Darkspace
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_country_type = ds_nomads
				"
				toggle = @has_darkspace_installed
			}
			# ZOFE
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_zofe_oe = yes
				"
				toggle = @has_zofe_vanilla
			}
		}
	}
	custom_tooltip = {
		fail_text = community_homicidal_failtext
		inline_script = {
			script = merger_of_rules/toggled_code_inverse
			code = "
				is_homicidal = no
			"
			toggle = @galactic_community_overhaul
		}
		# Galactic Community Overhaul
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				NOR = {
					is_homicidal_no_terminator = yes
					AND = {
						has_civic = civic_machine_terminator
						NOT = { has_global_flag = terminators_can_join_galcom }
					}
				}
			"
			toggle = @galactic_community_overhaul
		}
	}
	custom_tooltip = {
		fail_text = community_declared_crisis_failtext
		has_been_declared_crisis = no
	}
	custom_tooltip = {
		fail_text = community_declared_crisis_failtext
		OR = {
			NOT = { exists = overlord }
			overlord = { has_been_declared_crisis = no }
			# Warship Girls R And MIST Species
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					NOT = { has_country_flag = wgfm_quit_community }
				"
				toggle = @wsg_mod
			}
		}
	}
	# Playable Katzenartig Imperium
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = community_declared_crisis_failtext
				OR = {
					NOT = {
						pki_is_katzenartig_imperium = yes
						has_global_flag = pki_galactic_tension_stage_5
					}
					NOT = {
						has_global_flag = galcom_kaiser_crusade
						OR = {
							pki_is_katzenartig_imperium = yes
							pki_is_katzprotektorat = yes
							pki_is_allied_with_katzen = yes
						}
					}
				}
			}
		"
		toggle = @has_pki_mod
	}
}

# This = country
can_be_part_of_galactic_empire = {
	custom_tooltip = {
		fail_text = community_default_failtext
		OR = {
			merg_is_default_empire = yes
			# ZOFE
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_zofe_oe = yes
				"
				toggle = @has_zofe_vanilla
			}
		}
	}
	custom_tooltip = {
		fail_text = "community_homicidal_failtext"
		inline_script = {
			script = merger_of_rules/toggled_code_inverse
			code = "
				is_homicidal = no
			"
			toggle = @galactic_community_overhaul
		}
		# Galactic Community Overhaul
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				NOR = {
					is_homicidal_no_terminator = yes
					AND = {
						has_civic = civic_machine_terminator
						NOT = { has_global_flag = terminators_can_join_galcom }
					}
				}
			"
			toggle = @galactic_community_overhaul
		}
	}
	custom_tooltip = {
		fail_text = "community_declared_crisis_failtext"
		has_been_declared_crisis = no
	}
	custom_tooltip = {
		fail_text = "community_declared_crisis_failtext"
		OR = {
			NOT = { exists = overlord }
			overlord = { has_been_declared_crisis = no }
			# Warship Girls R And MIST Species
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					NOT = { has_country_flag = wgfm_quit_community }
				"
				toggle = @wsg_mod
			}
		}
	}
}

# This = country
can_see_galactic_community = {
	OR = {
		has_country_flag = galcom_form_aye
		has_country_flag = galcom_form_nay
		is_galactic_community_member = yes
		# ACOT SBTG
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_country_type = sbtg_stellarborne_empire_active
			"
			toggle = @has_sbtg_mod
		}
		# ZOFE
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_country_type = lost_empire
				is_zofe_oe = yes
			"
			toggle = @has_zofe_vanilla
		}
	}
	# Warship Girls R And MIST Species
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = community_declared_crisis_failtext
				NOT = { has_country_flag = wgfm_quit_community }
			}
		"
		toggle = @wsg_mod
	}
}

# This = country
can_be_part_of_galactic_council = {
	OR = {
		merg_is_default_empire = yes
		# ACOT SBTG
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_country_type = sbtg_stellarborne_empire_active
			"
			toggle = @has_sbtg_mod
		}
		# ZOFE
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_country_type = lost_empire
				is_zofe_oe = yes
			"
			toggle = @has_zofe_vanilla
		}
	}
	inline_script = {
		script = merger_of_rules/toggled_code_inverse
		code = "
			is_homicidal = no
		"
		toggle = @galactic_community_overhaul
	}
	# Galactic Community Overhaul
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			NOR = {
				is_homicidal_no_terminator = yes
				AND = {
					has_civic = civic_machine_terminator
					NOT = { has_global_flag = terminators_can_join_galcom }
				}
			}
		"
		toggle = @galactic_community_overhaul
	}
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = community_declared_crisis_failtext
				NOT = { has_country_flag = wgfm_quit_community }
			}
		"
		toggle = @wsg_mod
	}
}

# must_be_advanced_empire
# is_allowed_to_be_advanced_empire
# has_enough_pops_for_necrophage_on_planet

# Root = current planet
# This = pop
# Purge, Sapience, Migration Controls policy (ignored by Gestalts and Synths), and target habitability are checked in code.
can_pop_auto_migrate = {
	hidden_trigger = {
		OR = {
			is_unemployed = yes
			has_job = servant
			has_job = slave_toiler
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_job = servant_noble
					has_job = grunt
				"
				toggle = @expops_compatch_var
			}
		}
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_ASSIMILATED
		is_being_assimilated = no
	}
	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
					}
				}
			}
		}
	}
	# Genshin Impact
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = AUTO_MIGRATE_LINGJING
				has_lingjing_species_trait = no
			}
		"
		toggle = @trait_shijianzhe_sci_amt
	}
	# Vanilla
	if = {
		limit = {
			OR = {
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_shackled_robot = yes
					"
					toggle = @[ 1 - expops_compatch_var ]
				}
				is_enslaved = yes
			}
		}
		custom_tooltip = {
			fail_text = AUTO_MIGRATE_SLAVE_PROCESSING
			OR = {
				planet = {
					OR = {
						OR = {
							has_building = building_slave_processing
							# Plentiful Traditions
							inline_script = {
								script = merger_of_rules/toggled_code
								code = "
									has_building = building_plentiful_traditions_advanced_slave_processing
								"
								toggle = @plentrad_compatch_var
							}
						}
						AND = {
							exists = orbital_defence
							orbital_defence = {
								exists = starbase
								starbase = { has_starbase_building = ring_slave_processing_facility }
							}
						}
					}
				}
				solar_system = {
					count_starbase_buildings = {
						type = transit_hub
						count >= 1
						include_being_constructed = no
					}
				}
			}
		}
	}
	# Expanded Traits, Civics, Pops, and More
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			if = {
				limit = { is_shackled_robot = yes }
				custom_tooltip = {
					fail_text = AUTO_MIGRATE_ROBOT_DROIDS
					OR = {
						planet = { owner = { is_machine_empire = yes } }
						planet = { owner = { has_technology = tech_droid_workers } }
						solar_system = {
							count_starbase_buildings = {
								type = transit_hub
								count >= 1
								include_being_constructed = no
							}
						}
					}
				}
			}
			if = {
				limit = { is_exp_tree_caretaker = yes }
				custom_tooltip = {
					fail_text = caretaker_refuse
					always = no
				}
			}
		"
		toggle = @expops_compatch_var
	}
	# V_Traits
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			is_v_mobile = yes
		"
		toggle = @has_v_traits_mod
	}
	# Diagrapher's Trait Mod
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			dtraits_auto_migration_restrictions = yes
		"
		toggle = @has_diagraphers_trait_mod
	}
}

# This = planet
# Checked before can_pop_auto_migrate to save performance
can_planet_auto_migrate = {
	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_colony = yes
				colony_age > 60
			}
			is_capital = yes
			has_planet_flag = void_dweller_habitat
		}
	}
	custom_tooltip = {
		fail_text = AUTO_MIGRATE_ONLY_HAS_ONE_POP
		num_sapient_pops > 1
	}
	hidden_trigger = {
		exists = owner
		owner = {
			OR = {
				merg_is_default_empire = yes
				merg_is_awakened_fe = yes
			}
		}
	}
}

# can_planet_receive_auto_migration
# can_gene_mod_habitability

#This, root = country
can_add_advanced_traits = {
	has_utopia = yes
	OR = {
		has_tradition = tr_genetics_enhanced_expression
		# Azur Lane Stellaris DLC
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					has_ascension_perk = ap_AL_R_kansen_species
					has_country_flag = ap_AL_R_kansen_species_traits_success
				}
			"
			toggle = @azl_mod
		}
		# EDA
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_valid_civic = civic_eda
			"
			toggle = @eda_mod
		}
		# Madoka Magica NEW
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_str_genemodding_tech = yes
			"
			toggle = @madoka_compatch_var
		}
		# Nirvana Empire DLC
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_nirvana_empire = yes
			"
			toggle = @nirvana_mod
		}
		# Warship Girls R And MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_wsg_genemodding_tech = yes
			"
			toggle = @wsg_mod
		}
		# Wula Fallen Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_fallen_empire = yes
			"
			toggle = @wula_compatch_var
		}
	}
}

#This, root = country
can_add_negative_traits_organic = {
	has_utopia = yes
	OR = {
		has_tradition = tr_genetics_resequencing
		has_active_tradition = tr_cybernetics_integrated_anatomy
		has_active_tradition = tr_cybernetics_assimilator_integrated_anatomy
		# Azur Lane Stellaris DLC
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					has_ascension_perk = ap_AL_R_kansen_species
					has_country_flag = ap_AL_R_kansen_species_traits_success
				}
			"
			toggle = @azl_mod
		}
		# EDA
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_valid_civic = civic_eda
			"
			toggle = @eda_mod
		}
		# Madoka Magica NEW
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_str_genemodding_tech = yes
			"
			toggle = @madoka_compatch_var
		}
		# Scripted Triggers Undercoat
		check_modifier_value = { modifier = stu_deleterious_organic_modding_flag value > 0 }
		# Warship Girls R And MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_wsg_genemodding_tech = yes
			"
			toggle = @wsg_mod
		}
		# Wula Fallen Empire
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_fallen_empire = yes
			"
			toggle = @wula_compatch_var
		}
	}
	# Nirvana Empire DLC
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			is_nirvana_empire = yes
		"
		toggle = @nirvana_mod
	}
}

#This, root = country
can_remove_positive_traits_organic = {
	OR = {
		AND = {
			has_utopia = yes
			OR = {
				has_tradition = tr_genetics_resequencing
				has_active_tradition = tr_cybernetics_integrated_anatomy
				has_active_tradition = tr_cybernetics_assimilator_integrated_anatomy
				# Azur Lane Stellaris DLC
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						AND = {
							has_ascension_perk = ap_AL_R_kansen_species
							has_country_flag = ap_AL_R_kansen_species_traits_success
						}
					"
					toggle = @azl_mod
				}
				# EDA
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_valid_civic = civic_eda
						is_eda_empire = yes
						has_technology = tech_eda_core
					"
					toggle = @eda_mod
				}
				# Madoka Magica NEW
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_str_genemodding_tech = yes
					"
					toggle = @madoka_compatch_var
				}
				# Nirvana Empire DLC
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_nirvana_empire = yes
					"
					toggle = @nirvana_mod
				}
				# Warship Girls R And MIST Species
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_wsg_genemodding_tech = yes
					"
					toggle = @wsg_mod
				}
			}
		}
		AND = {
			has_toxoids = yes
			has_origin = origin_overtuned
		}
		has_technology = tech_epigenetic_triggers
		# Scripted Triggers Undercoat
		check_modifier_value = {
			modifier = stu_deleterious_organic_modding_flag
			value > 0
		}
	}
}

#This, root = country
can_modify_organic_portraits = {
	has_utopia = yes
	OR = {
		has_tradition = tr_genetics_enhanced_expression
		# AssaultLily In Stellaris
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_lily_goverments = yes
			"
			toggle = @change_planet_size_bullet_num
		}
		# Azur Lane Stellaris DLC
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					has_ascension_perk = ap_AL_R_kansen_species
					has_country_flag = ap_AL_R_kansen_species_traits_success
				}
			"
			toggle = @azl_mod
		}
		# Madoka Magica NEW
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_str_genemodding_tech = yes
			"
			toggle = @madoka_compatch_var
		}
		# Warship Girls R And MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_wsg_genemodding_tech = yes
			"
			toggle = @wsg_mod
		}
	}
}

# can_have_first_contact
# can_use_envoys_to_raise_imperial_authority
# can_use_envoys_to_lower_imperial_authority

#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
		OR = {
			is_enslaved = no
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_enslaved = yes
				"
				toggle = @expops_compatch_var
			}
		}
		is_being_assimilated = no
		NOT = { has_citizenship_type = { type = citizenship_organic_trophy country = owner } }
		# Expanded Traits, Civics, Pops, and More
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				NAND = {
					has_citizenship_type = { type = citizenship_slavery }
					OR = {
						has_slavery_type = { type = slavery_livestock }
						has_slavery_type = { type = slavery_matrix }
					}
				}
			"
			toggle = @expops_compatch_var
		}
		# Origins of Civilization
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				if = {
					limit = { owner = { is_hive_empire = yes } }
					has_trait = trait_hive_mind
				}
			"
			toggle = @has_origins_of_civilization
		}
		# V_Traits
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				has_trait_v_social_parasite = no
			"
			toggle = @has_v_traits_mod
		}
	}
	# Time Framework
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			TIME_RULE_trgr_CAN_fill_drone_job = yes
		"
		toggle = @time_framework
	}
}

#this/root = pop
#checked if job's possible_precalc = can_fill_worker_job
can_fill_worker_job = {
	custom_tooltip = WORKER_JOB_TRIGGER
	if = {
		limit = {
			planet = { is_planet_class = pc_cosmogenesis_world }
		}
		always = yes
	}
	else = {
		exists = owner
		OR = {
			is_being_purged = no
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_purge_type = { type = purge_displacement }
					has_purge_type = { type = purge_neutering }
				"
				toggle = @expops_compatch_var
			}
		}
		# Vanilla
		is_being_assimilated = no
		NOT = { has_citizenship_type = { type = citizenship_organic_trophy country = owner } }
		NAND = {
			has_citizenship_type = { type = citizenship_slavery }
			OR = {
				has_slavery_type = { type = slavery_livestock }
				has_slavery_type = { type = slavery_matrix }
				# V_TRAITS
				has_slavery_type = { type = slavery_s_test_subjects }
			}
		}
		if = {
			limit = {
				divinity_right_to_work_job_check_trigger_exempt = no
			}
			divinity_right_to_work_job_check_trigger_worker = yes
		}
		# Diagrapher's Trait Mod
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				can_work_outdoors = yes
			"
			toggle = @has_diagraphers_trait_mod
		}
	}
	hidden_trigger = {
		# Ethics & Civics Infinity
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_sparta_citizenship_full = no
			"
			toggle = @post_revolutionary_re_revolution_years
		}
		# V_Traits
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				if = {
					limit = { has_trait_v_social_parasite = yes }
					has_citizenship_type = { type = citizenship_slavery }
				}
			"
			toggle = @has_v_traits_mod
		}
		# Ethics & Civics Infinity
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				if = {
					limit = {
						exists = owner
						owner = {
							OR = {
								has_valid_civic = civic_moth_chasing_flames
								has_valid_civic = civic_ruthless_competition
							}
						}
					}
					custom_tooltip = {
						text = POP_EDUCATED_LOW
						has_modifier = pop_highly_educated
					}
				}
			"
			toggle = @post_revolutionary_re_revolution_years
		}
		# Time Framework
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				TIME_RULE_trgr_CAN_fill_worker_job = yes
			"
			toggle = @time_framework
		}
	}
}

#this/root = pop
#checked if job's possible_precalc = can_fill_specialist_job
can_fill_specialist_job = {
	can_fill_specialist_job_trigger = yes
	# Ethics & Civics Infinity
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			if = {
				limit = {
					exists = owner
					owner = {
						OR = {
							has_valid_civic = civic_moth_chasing_flames
							has_valid_civic = civic_ruthless_competition
						}
					}
				}
				custom_tooltip = {
					text = POP_EDUCATED_LOW
					has_modifier = pop_highly_educated
				}
			}
		"
		toggle = @post_revolutionary_re_revolution_years
	}
	# Girls Frontline
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = { has_trait = GF_trait_doll }
				owner = {
					OR = {
						NOT = { has_authority = GF_auth_commonwealth }
						has_policy_flag = GF_doll_work_policy_high
						has_policy_flag = GF_doll_work_policy_free
						has_policy_flag = GF_doll_work_policy_middle
					}
				}
			}
		"
		toggle = @gf_compatch_var
	}
	# Time Framework
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			TIME_RULE_trgr_CAN_fill_specialist_job = yes
		"
		toggle = @time_framework
	}
}

#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
            OR = {
                is_enslaved = no
				# Ethics and Civcs Infinity
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
		                owner = { has_civic = civic_du_contrat_social }
					"
					toggle = @post_revolutionary_re_revolution_years
				}
            }
			is_being_purged = no
			is_being_assimilated = no
			# Diagrapher's Trait Mod
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					dtraits_ruler_job_restrictions = no
				"
				toggle = @has_diagraphers_trait_mod
			}
			NOR = {
				# Ethics and Civics Bug Branch
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_bugged_slave_gene = yes
					"
					toggle = @has_bug_branch
				}
				# Ethics and Civics Classic
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_ecc_slave_gene = yes
					"
					toggle = @has_ecc_cog
				}
				# Lithoid Supremacy
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_pearl_born_drone = yes
					"
					toggle = @has_lithoid_mod
				}
			}
		}
	}
	# Ethics & Civcs Infinity
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			else_if = {
				limit = {
					exists = owner
					owner = {
						OR = {
							has_valid_civic = civic_moth_chasing_flames
							has_valid_civic = civic_ruthless_competition
						}
					}
				}
				custom_tooltip = {
					text = POP_EDUCATED_LOW
					has_modifier = pop_highly_educated
				}
			}
		"
		toggle = @post_revolutionary_re_revolution_years
	}
	# Vanilla
	else = {
		custom_tooltip = RULER_JOB_TRIGGER
		hidden_trigger = {
			is_gestalt = no
			exists = owner
			is_enslaved = no
			is_being_purged = no
			is_being_assimilated = no
			# Rule out Traits for servitude & lack of free will
			NOR = {
				has_trait = trait_syncretic_proles
				can_think = no
				# Diagrapher's Trait Mod
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						dtraits_ruler_job_restrictions = yes
					"
					toggle = @has_diagraphers_trait_mod
				}
				# Ethics & Civics Infinity
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						has_eci_flesh_components = yes
						has_eci_fallen_god = yes
					"
					toggle = @post_revolutionary_re_revolution_years
				}
				# Expanded Traits, Civics, Pops, and More
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_exp_tree_caretaker = yes
					"
					toggle = @expops_compatch_var
				}
				# Lithoid Supremacy
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_pearl_born_drone = yes
					"
					toggle = @has_lithoid_mod
				}
			}
			# Vanilla
			# 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 }
				}
				# Empires Expanded
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						AND = {
							owner = { has_policy_flag = artificial_aristocracy }
							owner = { has_technology = tech_droid_workers }
						}
					"
					toggle = @has_emex_mod
				}
			}
			# Rule out Organic Trophies
			NOT = { has_citizenship_type = { type = citizenship_organic_trophy 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
			}
			# Ethics & Civics Infinity
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					if = {
						limit = {
							owner = { has_valid_civic = civic_sparta_spirit }
						}
						is_sparta_citizenship_full = yes
					}
				"
				toggle = @post_revolutionary_re_revolution_years
			}
		}
		# Girls Frontline
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				if = {
					limit = { has_trait = GF_trait_doll }
					owner = {
						OR = {
							gf_comp_auth_commonwealth = no
							has_policy_flag = GF_doll_work_policy_high
							has_policy_flag = GF_doll_work_policy_free
						}
					}
				}
			"
			toggle = @gf_compatch_var
		}
	}
	# Time Framework
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			TIME_RULE_trgr_CAN_fill_ruler_job = yes
		"
		toggle = @time_framework
	}
	# Diagrapher's Trait Mod
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			dtraits_repropol_synergy_ruler_job_restriction = yes
		"
		toggle = @has_diagraphers_trait_mod
	}
}

# can_fill_precursor_job
# should_planetary_automate
# dismiss_leader_cost

# 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
		}
		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 }
		}
	}
	# Assault Lily
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = requires_not_special_lily_leader
				NOT = { has_leader_flag = AL_special_lily_leader }
			}
		"
		toggle = @change_planet_size_bullet_num
	}
	# Civil Wars
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = cannot_fire_leader_during_crisis
				NOT = { has_leader_flag = cw_cannot_be_fired }
			}
		"
		toggle = @has_civil_wars_mod
	}
	# StellarisCraft
	custom_tooltip = {
		fail_text = cannot_fire_StellarisCraft_leader
		NOT = { has_leader_flag = StellarisCraft_event_leader }
	}
	# Warship Girls R And MIST Species
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = cannot_fire_wg_leader
				NOT = { has_leader_flag = wsg_event_leader }
				is_special_affection_leader = no
			}
		"
		toggle = @wsg_mod
	}
}

# can_country_change_councilors

# this = leader
should_count_towards_leader_cap = {
	NOR = {
		has_leader_flag = should_not_have_upkeep # Ariphaos Patch
		leader_class = envoy
		is_hidden = yes
		has_leader_flag = hired_admiral_merc_leader
		has_leader_flag = legendary_leader
		has_trait = leader_trait_rift_warped
		AND = {
			has_trait_tier1or2 = { TRAIT = leader_trait_eager }
			has_skill < 4
		}
		AND = {
			is_ruler = yes
			OR = {
				has_trait = leader_trait_ruler_machine_intelligence
				has_trait = leader_trait_ruler_hive_mind
			}
		}
		# Expanded Mods Base
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_heir = yes
				AND = {
					is_exp_eager3 = yes
					has_skill < 8
				}
				AND = {
					exists = owner
					owner = {
						any_owned_planet = {
							exists = leader
							leader = {
								is_same_value = PREVPREVPREV
							}
							has_deposit = d_venerable_elder_site
						}
					}
				}
			"
			toggle = @[ expops_compatch_var + expevents_compatch_var + emt_compatch_var ]
		}
		# E&C Bug Branch
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				AND = {
					is_bugged_highborn = yes
					has_skill > 9
				}
			"
			toggle = @has_bug_branch
		}
		# Playable Katzenartig Imperium
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				is_pki_katzen_trait_elite = yes
			"
			toggle = @has_pki_mod
		}
	}
}

# 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 = {
	AND = {
		is_primitive = no
		OR = {
			merg_is_default_empire = yes
			is_subject = yes
			is_overlord = yes
			# SirBlackAxe's Unofficial Bug Patch
			merg_is_fallen_empire = yes
			merg_is_awakened_fe = yes
			FROM = { is_same_value = root }
			is_in_federation_with = FROM
		}
		# SirBlackAxe's Unofficial Bug Patch
		NAND = {
			has_closed_borders = FROM
			can_control_access_for = FROM
		}
		NOT = { is_at_war_with = FROM }
	}
}

# should_ai_build_stronghold_on_vulnerable_planet
# will_leader_retire

# 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
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				giga_can_get_external_leader_pool_candidate = yes
			"
			toggle = @has_gigastructures
		}
	}
	# 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 = {
		OR = {
			has_leader_flag = tradable_leader
			has_specialist_subject_leader_trait = yes
			# Freebooters Origin
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					root = { is_freebooter_empire = yes }
				"
				toggle = @has_nsc_freebooter_mod
			}
			# Girls Frontline
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					GF_can_trade_robotic_leader = yes
				"
				toggle = @gf_compatch_var
			}
		}
		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
				NOR = {
					is_country_type = gate_builders
					is_country_type = awakened_marauders
				}
			}
			FROM = {
				is_regular_empire = yes
				NOR = {
					is_country_type = gate_builders
					is_country_type = awakened_marauders
				}
			}
		}
		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
					has_authority = auth_hive_mind
					has_authority = auth_machine_intelligence
				}
				NOT = { has_country_flag = no_murdering_tactical_leaders }
				NOT = { any_owned_leader = { has_trait = leader_trait_tactical_algorithms } }
			}
		}
		# Ethics and Civics Infinity
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				root = { is_servitor_empire = yes }
			"
			toggle = @post_revolutionary_re_revolution_years
		}
	}
	# 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 = {
				merg_is_fallen_empire = yes
				merg_is_awakened_fe = yes
				# ZOFE
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						is_country_type = original_empire
					"
					toggle = @has_zofe_vanilla
				}
			}
		}
	}
	NOR = {
		has_trait = leader_trait_fotd_scientist
		has_trait = leader_trait_fotd_commander
	}
	# Warship Girls R And MIST Species
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = cannot_sold_wg_leader
				is_special_affection_leader = no
				is_wg_event_leader = no
			}
		"
		toggle = @wsg_mod
	}
}

# 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 = {
					merg_is_fallen_empire = yes
					merg_is_awakened_fe = yes
					# ZOFE
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							is_country_type = original_empire
						"
						toggle = @has_zofe_vanilla
					}
				}
			}
		}
		NOT = { has_fleet_flag = dimensional_fleet }
	}
	# Warship Girls R And MIST Species
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = cannot_sold_wg_fleet
				any_owned_ship = { is_wg_event_limit_ships = no }
			}
		"
		toggle = @wsg_mod
	}
	# Star Wars: Kuat Ancient Empire
	inline_script = {
		script = merger_of_rules/toggled_code
		code = "
			custom_tooltip = {
				fail_text = cannot_sold_kuat_fleet
				any_owned_ship = { kuat_is_flagship_system_protection = no }
			}
		"
		toggle = @sw_kuat_mod
	}
}

# can_leader_be_ruler
# chance_for_external_leader_pool
# council_agenda_early_activation_cost
# leader_election_elect_cost
# emergency_election_cost_rule
# leader_election_support_cost
# leader_election_weight
# are_pre_ftls_unaware
# is_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 = { has_ascension_perk = ap_hydrocentric }
			has_modifier = frozen_terraforming_candidate
		}
		AND = {
			root = { has_ascension_perk = ap_detox }
			has_modifier = toxic_terraforming_candidate
		}
		# Wiirlak
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				wiirlak_ac_can_terraform = yes
			"
			toggle = @has_wiirlak_ac
		}
	}
}

# can_be_renamed_by
# can_build_station_around
# can_build_mining_station_around
# can_build_research_station_around
# can_build_military_station_around
# can_build_observation_station_around
# can_build_starbase_around
# has_inworld_capital
# has_relative_economy_power
# can_trade_sensor
# can_trade_favor
# can_interact_with_astral_rift
# can_explore_astral_rift
# can_leader_explore_astral_rift
# can_ai_assign_governor
# should_ai_support_galactic_empire
# should_ai_undermine_galactic_empire
# can_country_initiate_storm
# can_initiate_storm_on_planet
# can_force_see_fleets_in_system
# has_breached_shroud_storm
# is_science_fleet_gravity_snare_on_cooldown
# can_fleet_be_captured_by_snares
# can_fauna_grow
# can_use_exhibits
# can_access_vivarium
# can_scavenge_and_research_debris
# can_scavenge_debris
# can_excavate_outside_borders
# can_voidworms_bombard_country
# can_use_mutations
# can_use_artificial_breeding
# can_trade_specimens
# force_create_default_ship_design
# is_seeded

# this = destroyed ship
# from = attacker (ship)
can_capture_ship = {
	exists = this
	exists = from
	exists = controller
	controller = {
		merg_is_default_empire = yes
	}
	OR = {
		# Civilian Ships
		is_ship_size = 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
		# 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
			}
		}
		# 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
	}
}
