﻿histmod_spawn_equal_army_effect = {
	if = {
		limit = { scope:attacker.strength_with_relevant_allies_vs_war_defender_value < scope:defender.strength_with_relevant_allies_vs_war_attacker_value }
		spawn_army = {
			name = event_troop_default_name
			location = scope:attacker.capital_province
			war = scope:war
			uses_supply = no
			inheritable = no
			
			levies = {
				value = 0
				add = histmod_war_strength_difference_value
				multiply = 0.5
				min = 0
				max = 15000
			}
		}
		spawn_army = {
			name = event_troop_default_name
			location = scope:attacker.capital_province
			war = scope:war
			uses_supply = no
			inheritable = no
			
			levies = {
				value = 0
				add = histmod_war_strength_difference_value
				multiply = 0.5
				min = 0
				max = 15000
			}
		}
		spawn_army = {
			name = event_troop_default_name
			location = scope:attacker.capital_province
			war = scope:war
			uses_supply = no
			inheritable = no
			
			levies = {
				value = 0
				add = histmod_war_strength_difference_value
				multiply = 0.5
				min = 0
				max = 15000
			}
		}
	}
}

remove_bad_congenital_traits = {
	save_temporary_scope_as = new_character
	every_character_trait = {
		limit = {
			OR = {
				this = trait:beauty_bad_1
				this = trait:beauty_bad_2
				this = trait:beauty_bad_3
				this = trait:intellect_bad_1
				this = trait:intellect_bad_2
				this = trait:intellect_bad_3
				this = trait:physique_bad_2
				this = trait:physique_bad_3
				this = trait:hunchbacked
				this = trait:dwarf
				this = trait:giant
				this = trait:inbred
				this = trait:spindly
				this = trait:scaly
				this = trait:albino
				this = trait:infertile
				this = trait:giant
			}
		}
		scope:new_character = { remove_trait = prev }
	}
}

child_inherit_education_effect = {
	if = {
		limit = {
			is_adult = yes
			OR = {
				AND = {
					has_education_martial_trigger = yes
					$PRIMARY_PARENT$ = { has_education_martial_trigger = no }
				}
				AND = {
					has_education_intrigue_trigger = yes
					$PRIMARY_PARENT$ = { has_education_intrigue_trigger = no }
				}
				AND = {
					has_education_learning_trigger = yes
					$PRIMARY_PARENT$ = { has_education_learning_trigger = no }
				}
				AND = {
					has_education_diplomacy_trigger = yes
					$PRIMARY_PARENT$ = { has_education_diplomacy_trigger = no }
				}
				AND = {
					has_education_stewardship_trigger = yes
					$PRIMARY_PARENT$ = { has_education_stewardship_trigger = no }
				}
			}
		}
		# first lose the education trait
		every_character_trait = {
			limit = { has_trait_category = education }
			$NEW_CHILD$ = { remove_trait = prev }
		}
		# then re-add it with a slant towards the parent's education
		random_list = {
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_martial_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_martial_4 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_martial_1 }
				}
				add_trait = education_martial_1
			}
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_martial_trigger = yes }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_martial_1 }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_martial_4 }
				}
				add_trait = education_martial_2
			}
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_martial_trigger = yes }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_martial_1 }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_martial_4 }
				}
				modifier = {
					add = 10
					$PRIMARY_PARENT$ = { has_trait = adventurer }
				}
				add_martial_skill = 1
				add_trait = education_martial_3
			}
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_martial_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_martial_1 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_martial_4 }
				}
				modifier = {
					add = 10
					$PRIMARY_PARENT$ = { has_trait = adventurer }
				}
				add_martial_skill = 2
				add_trait = education_martial_4
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_intrigue_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_4 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_1 }
				}
				add_trait = education_intrigue_1
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_intrigue_trigger = yes }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_1 }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_4 }
				}
				add_trait = education_intrigue_2
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_intrigue_trigger = yes }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_1 }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_4 }
				}
				add_intrigue_skill = 1
				add_trait = education_intrigue_3
			}
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_intrigue_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_1 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_intrigue_4 }
				}
				add_intrigue_skill = 2
				add_trait = education_intrigue_4
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_learning_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_learning_4 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_learning_1 }
				}
				add_trait = education_learning_1
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_learning_trigger = yes }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_learning_1 }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_learning_4 }
				}
				add_trait = education_learning_2
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_learning_trigger = yes }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_learning_1 }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_learning_4 }
				}
				add_learning_skill = 1
				add_trait = education_learning_3
			}
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_learning_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_learning_1 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_learning_4 }
				}
				add_learning_skill = 2
				add_trait = education_learning_4
			}
			3 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_diplomacy_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_4 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_1 }
				}
				add_trait = education_diplomacy_1
			}
			3 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_diplomacy_trigger = yes }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_1 }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_4 }
				}
				add_trait = education_diplomacy_2
			}
			3 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_diplomacy_trigger = yes }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_1 }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_4 }
				}
				add_diplomacy_skill = 1
				add_trait = education_diplomacy_3
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_diplomacy_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_1 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_diplomacy_4 }
				}
				add_diplomacy_skill = 2
				add_trait = education_diplomacy_4
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_stewardship_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_4 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_1 }
				}
				add_trait = education_stewardship_1
			}
			2 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_stewardship_trigger = yes }
				}
				modifier = {
					add = 1
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_1 }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_4 }
				}
				add_trait = education_stewardship_2
			}
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_stewardship_trigger = yes }
				}
				modifier = {
					add = -2
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_1 }
				}
				modifier = {
					add = -1
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_4 }
				}
				add_stewardship_skill = 1
				add_trait = education_stewardship_3
			}
			1 = {
				modifier = {
					add = 8
					$PRIMARY_PARENT$ = { has_education_stewardship_trigger = yes }
				}
				modifier = {
					add = -3
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_1 }
				}
				modifier = {
					add = 3
					$PRIMARY_PARENT$ = { has_trait = education_stewardship_4 }
				}
				add_stewardship_skill = 2
				add_trait = education_stewardship_4
			}
		}
		spawned_soldier_traits_effect = yes
	}
	else_if = {
		limit = { is_adult = no }
		if = {
			limit = {
				$PRIMARY_PARENT$ = { has_trait = adventurer }
				NOR = {
					has_trait = bossy
					has_trait = rowdy
				}
			}
		}
		every_character_trait = {
			limit = { has_trait_category = childhood }
			$NEW_CHILD$ = { remove_trait = prev }
		}
		random_list = {
			50 = { add_trait = bossy }
			50 = { add_trait = rowdy }
		}
		add_prowess_skill = 2
		if = {
			limit = {
				$PRIMARY_PARENT$ = { has_education_martial_trigger = yes }
				OR = {
					can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $NEW_CHILD$ }
					AND = {
						is_female = yes
						culture = { has_cultural_parameter = has_access_to_shieldmaidens }
						NOR = {
							has_trait = shieldmaiden
							culture = { has_cultural_parameter = martial_custom_female_only_combatant }
							faith = { has_doctrine_parameter = combatant_must_be_female_if_no_roco }
							culture = { has_cultural_parameter = martial_custom_equal_combatant }
							faith = { has_doctrine_parameter = combatant_can_be_either_gender_if_no_roco }
						}
					}
				}		
				NOR = {
					has_focus = education_martial
					has_trait = curious
				}
			}
			add_martial_skill = 1
			add_prowess_skill = 1
			set_focus = education_martial
			set_variable = {
				name = education_martial_variable
				value = age
			}
		}
		else_if = {
			limit = {
				$PRIMARY_PARENT$ = { has_education_intrigue_trigger = yes }
				NOR = {
					has_focus = education_intrigue
					has_trait = pensive
				}
			}
			add_intrigue_skill = 1
			set_focus = education_intrigue
			change_variable = {
				name = education_intrigue_variable
				add = age
			}
		}
		else_if = {
			limit = {
				$PRIMARY_PARENT$ = { has_education_learning_trigger = yes }
				NOR = {
					has_focus = education_learning
					has_trait = rowdy
				}
			}
			add_learning_skill = 1
			set_focus = education_learning
			change_variable = {
				name = education_learning_variable
				add = age
			}
		}
		else_if = {
			limit = {
				$PRIMARY_PARENT$ = { has_education_diplomacy_trigger = yes }
				NOR = {
					has_focus = education_diplomacy
					has_trait = bossy
				}
			}
			add_diplomacy_skill = 1
			set_focus = education_diplomacy
			change_variable = {
				name = education_diplomacy_variable
				add = age
			}
		}
		else_if = {
			limit = {
				$PRIMARY_PARENT$ = { has_education_stewardship_trigger = yes }
				NOR = {
					has_focus = education_stewardship
					has_trait = charming
				}
			}
			add_stewardship_skill = 1
			set_focus = education_stewardship
			change_variable = {
				name = education_stewardship_variable
				add = age
			}
		}
	}
}

takes_same_tier_titles_then_vassalizes = {
	hidden_effect = {
		$TARGET$ = {
			every_held_title = {
				limit = { tier = $SUBJUGATOR$.highest_held_title_tier }
				add_to_list = titles_to_give
				every_in_de_facto_hierarchy = {
					limit = {
						holder ?= {
							NOT = { is_in_list = vassals_to_reassign }
						}
					}
					holder = {
						add_to_list = vassals_to_reassign
						if = {
							limit = {
								is_vassal_of = $TARGET$
								NOT = { is_in_list = vassals_to_convince }
							}
							add_to_list = vassals_to_convince
						}
					}
				}
			}
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = title_change_realm
				add_claim_on_loss = yes
			}
			every_in_list = {
				list = titles_to_give
				change_title_holder = {
					holder = $SUBJUGATOR$
					change = scope:title_change_realm
					take_baronies = no
				}
			}
			every_in_list = {
				list = vassals_to_reassign
				change_liege = {
					liege = $SUBJUGATOR$
					change = scope:title_change_realm
				}
			}
			change_liege = {
				liege = $SUBJUGATOR$
				change = scope:title_change_realm
			}
			resolve_title_and_vassal_change = scope:title_change_realm
			cancel_truce_both_ways = $SUBJUGATOR$
		}
	}
	$SUBJUGATOR$ = {
		add_truce_both_ways = {
			character = $TARGET$
			days = 7300
			override = yes
			name = DEMAND_IMPERIAL_SUBJUGATION_TRUCE_NAME
		}
		if = {
			limit = {
				NOT = { is_allied_to = $TARGET$ }
			}
			custom_tooltip = negotiate_hre_alliance_effect_tooltip
			create_alliance = {
				target = $TARGET$
				allied_through_owner = $SUBJUGATOR$
				allied_through_target = $TARGET$
			}
		}
		progress_towards_friend_effect = {
			CHARACTER = $TARGET$
			OPINION = 50
			REASON = friend_imperial_subjugation
		}
		reverse_add_opinion = { target = $TARGET$ modifier = working_together_hre_opinion }
		hidden_effect = {
			add_opinion = { target = $TARGET$ modifier = working_together_hre_opinion }
			add_opinion = { target = root modifier = event_negotiated_alliance_opinion }
			reverse_add_opinion = { target = root modifier = event_negotiated_alliance_opinion }
		}
	}
	every_in_list = {
		list = vassals_to_convince
		limit = {
			highest_held_title_tier >= tier_county
			NOR = {
				this = $TARGET$
				has_relation_rival = $SUBJUGATOR$
				has_relation_rival = $TARGET$
			}
			OR = {
				opinion = { target = $TARGET$ value >= 20 }
				opinion = { target = $SUBJUGATOR$ value >= 20 }
				has_relation_friend = $TARGET$
				has_relation_friend = $SUBJUGATOR$
				is_allied_to = $TARGET$
				is_allied_to = $SUBJUGATOR$
			}
		}
		add_opinion = {
			target = $SUBJUGATOR$
			modifier = former_liege_convinced_opinion
		}
		if = {
			limit = {
				NOT = { has_truce = $SUBJUGATOR$ }
			}
			add_truce_both_ways = {
				character = $SUBJUGATOR$
				days = 1825
				override = yes
				name = DEMAND_IMPERIAL_SUBJUGATION_TRUCE_NAME
			}
		}
	}
}

subjugates_then_vassalizes_effect = {
	create_title_and_vassal_change = {
		type = swear_fealty
		save_scope_as = change
	}
	$TARGET$ = {
		change_liege = {
			liege = $SUBJUGATOR$
			change = scope:change
		}
		hidden_effect = { cancel_truce_both_ways = $SUBJUGATOR$ }
	}
	resolve_title_and_vassal_change = scope:change
	
	$SUBJUGATOR$ = {
		add_truce_both_ways = {
			character = $TARGET$
			days = 7300
			override = yes
			name = DEMAND_IMPERIAL_SUBJUGATION_TRUCE_NAME
		}
		if = {
			limit = {
				NOT = { is_allied_to = $TARGET$ }
			}
			custom_tooltip = negotiate_hre_alliance_effect_tooltip
			create_alliance = {
				target = $TARGET$
				allied_through_owner = $SUBJUGATOR$
				allied_through_target = $TARGET$
			}
		}
		progress_towards_friend_effect = {
			CHARACTER = $TARGET$
			OPINION = 50
			REASON = friend_after_subjugation
		}
		reverse_add_opinion = { target = $TARGET$ modifier = working_together_hre_opinion }
		hidden_effect = {
			add_opinion = { target = $TARGET$ modifier = working_together_hre_opinion }
			add_opinion = { target = $TARGET$ modifier = event_negotiated_alliance_opinion }
			reverse_add_opinion = { target = $TARGET$ modifier = event_negotiated_alliance_opinion }
		}
	}
}

histmod_simple_gain_title_effect = {
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = change
		add_claim_on_loss = no
	}
	title:$TITLE$ = {
		change_title_holder = {
			holder = $CHARACTER$
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
}

histmod_create_random_cadet_house_effect = {
	save_temporary_scope_as = new_cadet_house_head
	
	# find every house member
	house = {
		every_house_member = {
			limit = { this != scope:new_cadet_house_head }
			add_to_list = house_members
		}
	}
		
	# join the new house
	set_house = scope:new_house
	
	# now create the cadet house
	create_cadet_branch = {}
	every_in_list = {
		list = house_members
		if = {
			limit = { house != scope:new_cadet_house_head.house }
			set_house = scope:new_cadet_house_head.house
		}
		remove_from_list = house_members
	}
}

histmod_create_dynasty_effect = {
	hidden_effect = {
		save_temporary_scope_as = new_dynasty_head
		if = {
			limit = { exists = dynasty }
			# find every house head in the realm - they'll join the new dynasty and form cadet branches
			dynasty = {
				every_dynasty_member = {
					limit = {
						this != scope:new_dynasty_head
						is_house_head = yes
						target_is_liege_or_above = scope:new_dynasty_head
					}
					add_to_list = old_house_heads
				}
			}
			# find every house member in the realm - they'll join the new dynasty
			house = {
				every_house_member = {
					limit = {
						this != scope:new_dynasty_head
						target_is_liege_or_above = scope:new_dynasty_head
					}
					add_to_list = old_house_members
				}
			}
			
			# who is most appropriate to be the new house head?
			if = {
				limit = {
					is_house_head = yes
					house = {
						any_house_member = {
							this != scope:new_dynasty_head
							NOT = { target_is_liege_or_above = scope:new_dynasty_head }
						}
					}
				}
				house = {
					ordered_house_member = {
						limit = {
							this != scope:new_dynasty_head
							NOT = { target_is_liege_or_above = scope:new_dynasty_head }
							OR = {
								is_faith_dominant_gender = yes
								is_ruler = yes
							}
							trigger_if = {
								limit = { is_married = yes }
								OR = {
									AND = {
										is_female = yes
										matrilinear_marriage = yes
									}
									AND = {
										is_male = yes
										patrilinear_marriage = yes
									}
									primary_spouse.dynasty ?= dynasty
								}
							}
						}
						alternative_limit = {
							this != scope:new_dynasty_head
							NOT = { target_is_liege_or_above = scope:new_dynasty_head }
						}
						order_by = {
							add = age
							if = {
								limit = { is_ruler = yes }
								add = {
									value = highest_held_title_tier
									multiply = 100
								}
							}
						}
						save_scope_as = appropriate_house_head
						scope:new_dynasty_head.house = { set_house_head = scope:appropriate_house_head }
					}
				}
			}
			else = {
				# create a random character as a temporary house head
				create_character = {
					location = scope:new_dynasty_head.location
					culture = scope:new_dynasty_head.culture
					faith = scope:new_dynasty_head.faith
					gender_female_chance = 0
					dynasty_house = scope:new_dynasty_head.house
					age = 30
					save_scope_as = temp_old_house_head
				}
				house = { set_house_head = scope:temp_old_house_head }
			}
		}
		
		# now create the new dynasty
		create_dynasty = {
			name = $NAME$
			prefix = $PREFIX$
			coat_of_arms = $COA$
			save_scope_as = new_dynasty
		}
		
		# create a new random house head
		create_character = {
			location = scope:new_dynasty_head.location
			culture = scope:new_dynasty_head.culture
			faith = scope:new_dynasty_head.faith
			gender_female_chance = 0
			dynasty_house = scope:new_dynasty_head.house
			age = 30
			save_scope_as = temp_house_head
		}
		house = { set_house_head = scope:temp_house_head }
		
		# create the cadet branch
		create_cadet_branch = {
			prefix = $PREFIX$
			name = $NAME$
			coat_of_arms = $COA$
			save_scope_as = new_house
		}
		
		# get realm house heads to make their own houses
		if = {
			limit = { list_size:old_house_heads > 0 }
			every_in_list = {
				list = old_house_heads
				histmod_create_random_cadet_house_effect = yes
				remove_from_list = old_house_heads
			}
		}
		
		# add realm house members to the new house
		if = {
			limit = { list_size:old_house_members > 0 }
			every_in_list = {
				list = old_house_members
				if = {
					limit = { house != scope:new_house }
					set_house = scope:new_house
				}
				remove_from_list = old_house_members
			}
		}
		
		# kill the old temporary house head
		if = {
			limit = { exists = scope:temp_old_house_head }
			scope:temp_old_house_head = {
				silent_disappearance_effect = yes
				set_to_lowborn = yes
			}
		}
		
		# also kill the new one
		scope:temp_house_head = {
			silent_disappearance_effect = yes
			set_to_lowborn = yes
		}
	}
}

histmod_simplified_create_dynasty_effect = {
	hidden_effect = {
		save_temporary_scope_as = new_dynasty_head
		
		# who is most appropriate to be the new house head?
		if = {
			limit = {
				is_house_head = yes
				house = {
					any_house_member = {
						this != scope:new_dynasty_head
						NOR = {
							is_child_of = scope:new_dynasty_head
							is_grandchild_of = scope:new_dynasty_head
							is_great_grandchild_of = scope:new_dynasty_head
						}
					}
				}
			}
			house = {
				ordered_house_member = {
					limit = {
						this != scope:new_dynasty_head
						NOR = {
							is_child_of = scope:new_dynasty_head
							is_grandchild_of = scope:new_dynasty_head
							is_great_grandchild_of = scope:new_dynasty_head
						}
						OR = {
							is_faith_dominant_gender = yes
							is_ruler = yes
						}
						trigger_if = {
							limit = { is_married = yes }
							OR = {
								AND = {
									is_female = yes
									matrilinear_marriage = yes
								}
								AND = {
									is_male = yes
									patrilinear_marriage = yes
								}
								primary_spouse.dynasty ?= dynasty
							}
						}
					}
					alternative_limit = {
						this != scope:new_dynasty_head
						NOT = { target_is_liege_or_above = scope:new_dynasty_head }
					}
					order_by = {
						add = age
						if = {
							limit = { is_ruler = yes }
							add = {
								value = highest_held_title_tier
								multiply = 100
							}
						}
					}
					save_scope_as = appropriate_house_head
					scope:new_dynasty_head.house = { set_house_head = scope:appropriate_house_head }
				}
			}
		}
		else = {
			# create a random character as a temporary house head
			create_character = {
				location = scope:new_dynasty_head.location
				culture = scope:new_dynasty_head.culture
				faith = scope:new_dynasty_head.faith
				gender_female_chance = 0
				dynasty_house = scope:new_dynasty_head.house
				age = 30
				save_scope_as = temp_old_house_head
			}
			house = { set_house_head = scope:temp_old_house_head }
		}
		
		# now create the new random dynasty based on their culture
		create_dynasty = {}
		
		# kill the old temporary house head
		if = {
			limit = { exists = scope:temp_old_house_head }
			scope:temp_old_house_head = {
				silent_disappearance_effect = yes
				set_to_lowborn = yes
			}
		}
	}
}

