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 = {
		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
			}
			AND = {
				exists = character:242
				this = character:242 #Rollo
			}
			AND = {
				exists = character:500103
				this = character:500103 #Heahmund
			}
			AND = {
				exists = character:144000
				this = character:144000 #Harald Finehair
			}
			AND = {
				exists = character:40606
				this = character:40606 #Oleg the Prophet
			}
			AND = {
				exists = character:163108
				this = character:163108 # Bjorn Ironside
			}
			AND = {
				exists = character:163119
				this = character:163119 # Ubbe
			}
			AND = {
				exists = character:163112
				this = character:163112 # Hvitserk
			}
			AND = {
				exists = character:163110
				this = character:163110 # Sigurd
			}
			AND = {
				exists = character:163111
				this = character:163111 # Ivar
			}
			AND = {
				exists = character:306002
				this = character:306002 # Lagertha
			}
			AND = {
				exists = character:500109
				this = character:500109 # Earl Haraldson
			}
			AND = {
				exists = character:500111
				this = character:500111 # King Horik
			}
			AND = {
				exists = character:500112
				this = character:500112 # Jarl Borg
			}
			AND = {
				exists = character:500248
				this = character:500248 # Erik Karping
			}
		}
	}

	

	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
		}
	}
}

scripted_trigger weak_empire = {
	tier = tier_empire
	is_titular = no
	is_landless_type_title = no
	NOT = { exists = controlled_faith }
	NOT = { # Need to own 20% of De Jure counties
		any_in_de_jure_hierarchy = {
			filter = {
				tier = tier_county
			}
			continue = {
				tier > tier_county
			}
			OR = {
				holder = root
				holder = {
					target_is_liege_or_above = root
				}
			}
			percent >= 0.20
		}
	}
}

game_rule.4 = { # Empire Obscurity Game Rule
	type = character_event
	title = game_rule.4.t
	desc = game_rule.4.desc
	theme = realm
	left_portrait = {
		character = root
		animation = grief
	}
	
	cooldown = { years = 25 }

	trigger = {
		has_game_rule = on_empire_obscurity_requirement
		is_landed = yes
		is_at_war = no
		primary_title.tier >= tier_empire
		any_held_title = {
			weak_empire = yes
		}
	}

	immediate = {
		random_held_title = {
			limit = {
				weak_empire = yes
			}
			save_scope_as = falling_empire
		}
	}

	option = { # No! I just need time!
		name = game_rule.4.a
		
		add_prestige = -2000
		add_prestige_experience = -2000
		
		ai_chance = {
			base = 0
		}
	}

	option = { # Farewell, dear empire...
		name = game_rule.4.b
		
		destroy_title = scope:falling_empire
		add_pressed_claim = scope:falling_empire
		
		ai_chance = {
			base = 100
		}
	}
}

#######################
# 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 = 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 = title:k_hungary
			}
		}
	}
}
