#hungarian_migration_valid_ai_mogyer_ruler_trigger = {
#	culture = culture:mogyer
#	is_ai = yes
#	OR = {
#		government_has_flag = government_is_tribal
#		government_has_flag = government_is_nomadic
#	}
#	is_at_war = no
#	highest_held_title_tier <= tier_kingdom
#	OR = {
#		top_liege = this
#		liege.culture != culture:mogyer
#	}
#}
#
#hungarian_migration_valid_pannonian_target_trigger = {
#	NOR = {
#		culture = culture:mogyer
#		any_liege_or_above = {sre = culture:mogyer }
#	}
#	is_ai = yes
#	any_realm_county = {
#		count >= $COUNT$
#		title_province = { geographical_region = custom_hungary }
#	}
#}

exclave_independence_trigger = {
	top_liege = this
	NOT = { has_title = title:k_papal_state }
	exists = root.capital_province
	any_sub_realm_title = {
		tier = tier_county
		NOT = {
			is_connected_to = {
				target = root.capital_province.county
			}
		}
	}
	trigger_if = {
		limit = {
			exists = primary_heir
		}
		primary_heir = {
			NOR = {
				top_liege = this # Disallow this when the heir is independent, lest we risk breaking up realms that should be connected
				is_ai = no # Players always inherit everything
			}
		}
	}
}

# Sometimes we disable these events for specific characters.
harm_game_rule_enablement_trigger = {
	# Disable entirely if harm_safe.
	trigger_if = {
		limit = { has_game_rule = harm_safe }
		always = no
	}
	# harm_target_player_only
	trigger_else_if = {
		limit = { has_game_rule = harm_target_player_only }
		is_ai = no
	}
	# harm_target_ai_only
	trigger_else_if = {
		limit = { has_game_rule = harm_target_ai_only }
		is_ai = yes
	}
	# harm_target_all
	trigger_else = { always = yes }
	# Never just kill the player if it'd utterly game-over them.
	trigger_if = {
		limit = { is_ai = no }
		# Either because they have no heir.
		exists = player_heir
		# Or because they have an heir they can't survive with.
		player_heir.dynasty ?= dynasty
	}
	# Immortals do not receive harm events, as they would be inevitably killed by them over a long enough period of time.
	NOT = { has_trait_with_flag = is_immortal }
}

# Who do we care about when recording variable logs for harm events?
harm_game_rule_valid_for_logging_trigger = {
	OR = {
		AND = {
			highest_held_title_tier >= tier_duchy
			OR = {
				government_has_flag = government_is_feudal
				government_has_flag = government_is_clan
				government_has_flag = government_is_theocracy
			}
		}
		any_close_family_member = {
			highest_held_title_tier >= tier_duchy
			OR = {
				government_has_flag = government_is_feudal
				government_has_flag = government_is_clan
				government_has_flag = government_is_theocracy
			}
		}
	}
}

rule_title_creation_imperial_power_projection_title_creation_targeting_trigger = {
	trigger_if = {
		limit = { has_game_rule = title_creation_imperial_power_projection_targeting_ai_only }
		is_ai = yes
	}
	trigger_else_if = {
		limit = { has_game_rule = title_creation_imperial_power_projection_targeting_player_only }
		is_ai = no
	}
	trigger_else = { always = yes }
}

rule_title_creation_imperial_power_projection_title_creation_trigger = {
	trigger_if = {
		limit = {
			has_game_rule = title_creation_imperial_power_projection_any_power_advantage
			rule_title_creation_imperial_power_projection_title_creation_targeting_trigger = yes
			highest_held_title_tier <= tier_kingdom
		}
		custom_tooltip = {
			text = rule_title_creation_imperial_power_projection_title_creation_trigger.tt.any_power_advantage
			any_neighboring_top_liege_realm_owner = {
				count = all
				trigger_if = {
					limit = { highest_held_title_tier >= tier_empire }
					save_temporary_scope_as = char_temp
					OR = {
						any_close_family_member = { is_spouse_of = root }
						root = { has_strong_hook = scope:char_temp }
						root.current_military_strength >= scope:char_temp.fifty_percent_more_of_current_military_strength
					}
				}
				trigger_else = { always = yes }
			}
		}
	}
	trigger_else_if = {
		limit = {
			has_game_rule = title_creation_imperial_power_projection_hard_power_only
			rule_title_creation_imperial_power_projection_title_creation_targeting_trigger = yes
			highest_held_title_tier <= tier_kingdom
		}
		custom_tooltip = {
			text = rule_title_creation_imperial_power_projection_title_creation_trigger.tt.hard_power_only
			any_neighboring_top_liege_realm_owner = {
				count = all
				trigger_if = {
					limit = { highest_held_title_tier >= tier_empire }
					save_temporary_scope_as = char_temp
					root.current_military_strength >= scope:char_temp.fifty_percent_more_of_current_military_strength
				}
				trigger_else = { always = yes }
			}
		}
	}
	trigger_else_if = {
		limit = {
			has_game_rule = title_creation_imperial_power_projection_soft_power_only
			rule_title_creation_imperial_power_projection_title_creation_targeting_trigger = yes
			highest_held_title_tier <= tier_kingdom
		}
		custom_tooltip = {
			text = rule_title_creation_imperial_power_projection_title_creation_trigger.tt.soft_power_only
			any_neighboring_top_liege_realm_owner = {
				count = all
				trigger_if = {
					limit = { highest_held_title_tier >= tier_empire }
					save_temporary_scope_as = char_temp
					OR = {
						any_close_family_member = { is_spouse_of = root }
						root = { has_strong_hook = scope:char_temp }
					}
				}
				trigger_else = { always = yes }
			}
		}
	}
	trigger_else_if = {
		limit = {
			has_game_rule = title_creation_imperial_power_projection_hard_power_only_strict
			rule_title_creation_imperial_power_projection_title_creation_targeting_trigger = yes
			highest_held_title_tier <= tier_kingdom
		}
		custom_tooltip = {
			text = rule_title_creation_imperial_power_projection_title_creation_trigger.tt.hard_power_only_strict.neighbours
			any_neighboring_top_liege_realm_owner = {
				count = all
				trigger_if = {
					limit = { highest_held_title_tier >= tier_empire }
					save_temporary_scope_as = char_temp
					root.current_military_strength >= scope:char_temp.fifty_percent_more_of_current_military_strength
				}
				trigger_else = { always = yes }
			}
		}
		custom_tooltip = {
			text = rule_title_creation_imperial_power_projection_title_creation_trigger.tt.hard_power_only_strict.de_jure_sharers
			any_sub_realm_county = {
				count = all
				empire = {
					OR = {
						is_title_created = no
						holder = {
							save_temporary_scope_as = char_temp
							root.current_military_strength >= scope:char_temp.fifty_percent_more_of_current_military_strength
						}
					}
				}
			}
		}
	}
	trigger_else_if = {
		limit = {
			has_game_rule = title_creation_imperial_power_projection_any_power_strict
			rule_title_creation_imperial_power_projection_title_creation_targeting_trigger = yes
			highest_held_title_tier <= tier_kingdom
		}
		custom_tooltip = {
			text = rule_title_creation_imperial_power_projection_title_creation_trigger.tt.any_power_strict.neighbours
			any_neighboring_top_liege_realm_owner = {
				count = all
				trigger_if = {
					limit = { highest_held_title_tier >= tier_empire }
					save_temporary_scope_as = char_temp
					OR = {
						any_close_family_member = { is_spouse_of = root }
						root = { has_strong_hook = scope:char_temp }
						root.current_military_strength >= scope:char_temp.fifty_percent_more_of_current_military_strength
					}
				}
				trigger_else = { always = yes }
			}
		}
		custom_tooltip = {
			text = rule_title_creation_imperial_power_projection_title_creation_trigger.tt.any_power_strict.de_jure_sharers
			any_sub_realm_county = {
				count = all
				empire = {
					OR = {
						is_title_created = no
						holder = {
							save_temporary_scope_as = char_temp
							OR = {
								any_close_family_member = { is_spouse_of = root }
								root = { has_strong_hook = scope:char_temp }
								root.current_military_strength >= scope:char_temp.fifty_percent_more_of_current_military_strength
							}
						}
					}
				}
			}
		}
	}
	trigger_else = { always = yes }
}

game_rule_rig_norman_conquest_for_hereward_trigger = {
	OR = {
		has_game_rule = historicity_norman_conquest_determined_william
		# We also rig it for William if we have a player Hereward and no other input.
		AND = {
			has_game_rule = historicity_norman_conquest_default_random
			character:90028 ?= { is_ai = no }
			NOT = {
				any_player = {
					NOT = { this = character:90028 }
					any_character_war = {
						OR = {
							using_cb = norman_conquest_cb
							using_cb = norwegian_invasion_cb
						}
					}
				}
			}
		}
	}
}

game_rule_can_create_hybird_culture_trigger = {
	trigger_if = {
		limit = { is_ai = yes }
		NOR = {
			has_game_rule = none_hybrid_culture_ai_frequency
			has_global_variable = has_ai_hybrid_event_cooldown
		}
	}
}

game_rule_unrestricted_dynasty_legacies_trigger = {
	switch = {
		trigger = has_game_rule
		unrestricted_dynasty_legacies_all = { always = yes }
		unrestricted_dynasty_legacies_player_only = { is_ai = no }
		fallback = { always = no }
	}
}
