namespace = game_rule

# Generated families 	- 1001
# Gender equality 		- 1010

scripted_trigger de_jure_liege_title_to_destroy = {
	tier > tier_county
	is_titular = no
	is_landless_type_title = no
	NOT = { exists = controlled_faith }
	save_temporary_scope_as = de_jure_liege_title
	
	NOT = {
		ROOT = {
			any_sub_realm_title = {
				tier = tier_county
				target_is_de_jure_liege_or_above = scope:de_jure_liege_title
			}
		}
	}
	
	NAND = {
		has_game_rule = on_limited_de_jure_requirement
		holder = {
			is_ai = no
		}
		this = holder.primary_title
	}
}

game_rule.1 = { # De Jure Requirement
	hidden = yes
	
	trigger = {
		NOT = { has_game_rule = off_de_jure_requirement }
		any_held_title = {
			de_jure_liege_title_to_destroy = yes
		}
	}

	immediate = {
		# Notification
		random_held_title = {
			limit = {
				de_jure_liege_title_to_destroy = yes
			}
			save_scope_as = title_to_destroy
		}
		send_interface_message = {
			type = event_chancellor_task_bad
			title = de_jure_requirement_title
			desc = de_jure_requirement_desc

			left_icon = scope:title_to_destroy

			destroy_title = scope:title_to_destroy
			add_pressed_claim = scope:title_to_destroy
		}
	}
}


######################
# GENERATED FAMILIES #
######################

scripted_trigger only_ai_if_ai_only_trigger = {
	trigger_if = {
		limit = { has_game_rule = on_generate_families_ai_only }
		is_ai = yes
	}
	trigger_else = {
		always = yes
	}
}


game_rule.1000 = {
	type = empty
	hidden = yes

	trigger = {
		NOT = { has_game_rule = yes_random_rulers } #why generate them if we'll kill them off?
		OR = {
			has_game_rule = on_generate_families
			has_game_rule = on_generate_families_ai_only
		}
	}

	immediate = {
		every_independent_ruler = {
			if = {
				limit = { only_ai_if_ai_only_trigger = yes }
				trigger_event = game_rule.1001
			}
			every_vassal_or_below = {
				limit = { only_ai_if_ai_only_trigger = yes }
				trigger_event = game_rule.1001
			}
		}
	}
}

game_rule.1001 = { #by Mathilda Bjarnehed
	type = character_event
	hidden = yes
	
	trigger = {
		is_from_ruler_designer = no # Do not modify the family of somoene from the ruler designer
		#Is in need of family
		NOT = {
			exists = player_heir
		}
		#Allowed to get family
		NOT = { # Otherwise historical characters sometimes generate inappropriate children
			any_child = {
				always = yes
			}
		}
		NOR = {
			has_trait = celibate
			has_trait = eunuch
		}
		age >= 17 #Because you need ~1 year of pregnancy
		is_landed = yes
		primary_title = {
			is_mercenary_company = no
			is_holy_order = no
		}
		NOR = {
			has_government = republic_government
			has_government = theocracy_government
			AND = {
				exists = character:7757
				this = character:7757 #Matilda Canossa, her bookmark start is about overcoming the issue of no heir
			}
		}
	}

	

	immediate = {
		if = {
			limit = { faith = { has_doctrine_parameter = marriage_event } }
			add_character_flag = ignore_marriage_event
		}
		game_rule_create_spouse_and_children = { CHARACTER = root }

		#In case your created player heir is an older woman (past childbearing age or with a very short window)
		if = {
			limit = {
				exists = player_heir
				player_heir = {
					is_female = yes
					age >= 40
				}
				#Nothing else should need to be checked, they shouldn't be celibate or something like that
			}
			player_heir = {
				save_scope_as = childless_heir
			 	game_rule_create_spouse_and_children = { CHARACTER = scope:childless_heir }
			}
		}
	}
}

########################
# EXCLAVE INDEPENDENCE #
########################

game_rule.2 = { # Exclave Independence, on death
	hidden = yes
	
	trigger = {
		exclave_independence_trigger = yes
	}

	immediate = {
	
		if = { # None
			limit = {
				has_game_rule = off_exclave_independence
			}
		}
		else_if = { # Delay for AI if at war, skip if player at war
			limit = {
				is_at_war = yes
			}
			if = {
				limit = {
					is_ai = yes
					exists = primary_heir
					primary_heir = {
						is_ai = yes
						NOT = {
							has_character_flag = delayed_cleanse
						}
					}
				}
				primary_heir = {
					add_character_flag = {
						flag = delayed_cleanse
					}
				}
			}
		}
		else = {
			game_rule_exclave_independence_effect = yes
		}
	}
}

game_rule.3 = { # Exclave Independence, on war end
	hidden = yes
	
	trigger = {
		exclave_independence_trigger = yes
	}

	immediate = {
		if = { # None
			limit = {
				has_game_rule = off_exclave_independence
			}
		}
		else_if = { # Only the last war should trigger the independence
			limit = {
				is_at_war = yes
			}
		}
		else = {
			game_rule_exclave_independence_effect = yes
		}
	}
}

#######################
# HUNGARIAN MIGRATION #
#######################

game_rule.1011 = {
	type = empty
	hidden = yes

	trigger = {
		game_start_date <= 867.1.1
	}

	immediate = {
		#launch_historical
		if = {
			limit = { has_game_rule = launch_historical }
			trigger_event = {
				id = game_rule.1012
				months = { 360 480 }
			}
		}
		#launch_immediate
		if = {
			limit = { has_game_rule = launch_immediate }
			character:159137 = {
				add_character_flag = undertaking_hungarian_migration
				start_war = {
					cb = hungarian_migration_cb
					target = character:70382
					target_title = k_hungary
				}
			}
		}
		#launch_random
		if = {
			limit = { has_game_rule = launch_random }
			trigger_event = {
				id = game_rule.1012
				months = { 60 480 }
			}
		}
		#launch_off: just do nothing.
	}
}

game_rule.1012 = {
	type = empty
	hidden = yes

	trigger = {
		#The Migration hasn't already been attempted.
		NOT = {
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_launched_hungarian_migration
			}
		}
		#No player is in a position to be angling for this.
		NOT = {
			any_player = {
				culture = culture:mogyer
				OR = {
					#Independent Mogyers are valid launchers.
					is_independent_ruler = yes
					#... But notable vassals not beneath other Mogyers may also be eyeing it up.
					AND = {
						is_independent_ruler = no
						liege = {
							NOT = { culture = culture:mogyer }
							highest_held_title_tier = tier_kingdom
						}
					}
				}
			}
		}
		#There must be a suitable Eastern European Mogyer ruler around.
		any_county_in_region = {
			region = world_europe_east
			holder = { hungarian_migration_valid_ai_mogyer_ruler_trigger = yes }
		}
		#Pannonia must have a valid ruler to declare war on: someone with at least a duchy-ish sized chunk of the land, and who isn't a fellow Mogyer.
		any_county_in_region = {
			region = custom_hungary
			holder = {
				hungarian_migration_valid_pannonian_target_trigger = { COUNT = 6 }
			}
		}
	}

	#If the event fails, check again in a year or so, till the last available date for the decision has passed.
	on_trigger_fail = {
		if = {
			limit = {
				current_date <= 910.1.1
				#Don't just rerun the check forever if an attempt has already been made, of course.
				NOT = {
					is_target_in_global_variable_list = {
						name = unavailable_unique_decisions
						target = flag:flag_launched_hungarian_migration
					}
				}
			}
			trigger_event = {
				id = game_rule.1012
				days = 365
			}
		}
	}

	immediate = {
		#Designate a Hungarian attacker.
		random_county_in_region = {
			region = world_europe_east
			#Prefer higher tier non-emperor characters wherever possible.
			limit = {
				holder = {
					highest_held_title_tier = tier_kingdom
					hungarian_migration_valid_ai_mogyer_ruler_trigger = yes
				}
			}
			alternative_limit = {
				holder = {
					highest_held_title_tier = tier_duchy
					hungarian_migration_valid_ai_mogyer_ruler_trigger = yes
				}
			}
			alternative_limit = {
				holder = {
					highest_held_title_tier = tier_county
					hungarian_migration_valid_ai_mogyer_ruler_trigger = yes
				}
			}
			holder = { save_scope_as = hungarian_conqueror }
		}
		#... And a Pannonian defender.
		##Order these by rough size.
		if = {
			limit = {
				any_county_in_region = {
					region = custom_hungary
					holder = {
						hungarian_migration_valid_pannonian_target_trigger = { COUNT = 18 }
					}
				}
			}
			random_county_in_region = {
				region = custom_hungary
				limit = {
					holder = {
						any_held_county = {
							title_province = { geographical_region = custom_hungary }
						}
						hungarian_migration_valid_pannonian_target_trigger = { COUNT = 18 }
					}
				}
				holder.top_liege = { save_scope_as = pannonian_defender }
			}
		}
		else_if = {
			limit = {
				any_county_in_region = {
					region = custom_hungary
					holder = {
						hungarian_migration_valid_pannonian_target_trigger = { COUNT = 12 }
					}
				}
			}
			random_county_in_region = {
				region = custom_hungary
				limit = {
					holder = {
						any_held_county = {
							title_province = { geographical_region = custom_hungary }
						}
						hungarian_migration_valid_pannonian_target_trigger = { COUNT = 12 }
					}
				}
				holder.top_liege = { save_scope_as = pannonian_defender }
			}
		}
		else_if = {
			limit = {
				any_county_in_region = {
					region = custom_hungary
					holder = {
						hungarian_migration_valid_pannonian_target_trigger = { COUNT = 6 }
					}
				}
			}
			random_county_in_region = {
				region = custom_hungary
				limit = {
					holder = {
						any_held_county = {
							title_province = { geographical_region = custom_hungary }
						}
						hungarian_migration_valid_pannonian_target_trigger = { COUNT = 6 }
					}
				}
				holder.top_liege = { save_scope_as = pannonian_defender }
			}
		}
		##And just a fallback, in case something goes wrong elsewhere.
		else = {
			random_county_in_region = {
				region = custom_hungary
				limit = {
					holder = {
						hungarian_migration_valid_pannonian_target_trigger = { COUNT = 6 }
					}
				}
				holder.top_liege = { save_scope_as = pannonian_defender }
			}
		}
		#And sort notifications & setup.
		scope:hungarian_conqueror = {
			#Send the information event so that notifications are delivered correctly.
			trigger_event = east_europe.0141
			#Now, declare war using the pre-scripted flow.
			add_character_flag = undertaking_hungarian_migration
			start_war = {
				cb = hungarian_migration_cb
				target = scope:pannonian_defender
				target_title = k_hungary
			}
		}
	}
}