decisions = {
	ai_monk_title_gain = {
		ai_check_interval = 12
		is_high_prio = yes
		only_rulers = yes
		potential = {
			ai = yes
			is_ruler = no
			religion_group = christian
			OR = {
				AND = {
					trait = monk
					NOT = { has_character_flag = title_monk }
				}
				AND = {
					trait = nun
					NOT = { has_character_flag = title_nun }
				}
				AND = {
					trait = perfecti
					is_female = no
					NOT = { has_character_flag = title_monk }
				}
				AND = {
					trait = perfecti
					is_female = yes
					NOT = { has_character_flag = title_nun }
				}
			}
			NOR = {
				has_landed_title = d_pentarch_rome
				has_landed_title = d_pentarch_alexandria
			}
		}
		allow = {
			always = yes
		}
		effect = {
			if = {
				limit = { trait = monk }
				set_character_flag = title_monk
				set_special_character_title = MONK_BROTHER
			}
			if = {
				limit = { trait = nun }
				set_character_flag = title_nun
				set_special_character_title = NUN_SISTER
			}
			if = {
				limit = {
					trait = perfecti
					is_female = no
				}
				set_character_flag = title_monk
				set_special_character_title = MONK_BROTHER
			}
			if = {
				limit = {
					trait = perfecti
					is_female = yes
				}
				set_character_flag = title_nun
				set_special_character_title = NUN_SISTER
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	ai_monk_title_lose = {
		ai_check_interval = 12
		is_high_prio = yes
		potential = {
			ai = yes
			religion_group = christian
			OR = {
				AND = {
					NOT = { trait = monk }
					NOT = { trait = perfecti }
					has_character_flag = title_monk
				}
				AND = {
					NOT = { trait = nun }
					NOT = { trait = perfecti }
					has_character_flag = title_nun
				}
				AND = {
					is_ruler = yes
					OR = {
						has_character_flag = title_monk
						has_character_flag = title_nun
					}
				}
			}
		}
		allow = {
			always = yes
		}
		effect = {
			if = {
				limit = { is_female = no }
				clr_character_flag = title_monk
				remove_special_character_title = MONK_BROTHER
			}
			if = {
				limit = { is_female = yes }
				clr_character_flag = title_nun
				remove_special_character_title = NUN_SISTER
			}
		}
		ai_will_do = {
			factor = 1
		}
	}

	ai_desert_father_title_lose = {
		ai_check_interval = 12
		is_high_prio = yes
		potential = {
			ai = yes
			OR = {
				AND = {
					NOT = { trait = desert_father }
					has_character_flag = title_desert_father
				}
				AND = {
					NOT = { trait = desert_mother }
					has_character_flag = title_desert_mother
				}
				AND = {
					is_ruler = yes
					OR = {
						has_character_flag = title_desert_father
						has_character_flag = title_desert_mother
					}
				}
			}
		}
		allow = {
			always = yes
		}
		effect = {
			clr_character_flag = is_desert_mother_father
			if = {
				limit = { is_female = no }
				clr_character_flag = title_desert_father
				remove_special_character_title = DESERT_FATHER_TITLE
			}
			if = {
				limit = { is_female = yes }
				clr_character_flag = title_desert_mother
				remove_special_character_title = DESERT_MOTHER_TITLE
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	ai_become_monk = {
		ai_check_interval = 12
		potential = {
			ai = yes
			religion_group = christian
			is_adult = yes
			is_ruler = no
			prisoner = no
			OR = {
				trait = detached_priest
				trait = martial_cleric
				trait = scholarly_theologian
				trait = mastermind_theologian
				trait = zealous
				has_job_title = job_spiritual
			}
			NOR = {
				trait = cynical
				trait = ambitious
				trait = incapable
				trait = stressed
				trait = depressed
				trait = hedonist
				trait = monk
				trait = nun
				is_pregnant = yes
				trait = sympathy_islam
				trait = sympathy_pagans
				trait = sympathy_zoroastrianism
				trait = sympathy_judaism
				trait = sympathy_indian
				any_liege = { holy_order = yes }
				any_liege = { mercenary = yes }
				any_spouse = {
					trait = pregnant
					father_of_unborn = { character = ROOT }
				}
				any_heir_title = { always = yes }
				any_pretender_title = { always = yes }
				AND = {
					is_councillor = yes
					NOT = { has_job_title = job_spiritual }
				}
				has_minor_title = title_lord
				has_minor_title = title_commander
				religion = cathar
				religion = bogomilist
				religion = miaphysite
				religion = monophysite
				has_character_modifier = voice_of_satan
				has_character_modifier = planning_claimant_adventure
				has_character_flag = claimant_adventurer
				has_character_flag = monk_refused
			}
		}
		allow = {
			in_command_trigger = no
			is_ill = no
			calc_true_if = {
				amount = 2
				trait = zealous
				trait = humble
				trait = content
				trait = possessed
				trait = scholar
				trait = theologian
				trait = scholarly_theologian
				trait = mastermind_theologian
				has_character_modifier = voice_of_jesus
				has_job_title = job_spiritual
			}
			#won't go if married unless zealous/lunatic/possessed or he hates his wife
			OR = {
				is_married = no
				trait = zealous
				trait = lunatic
				trait = possessed
				NOT = { 
					any_spouse = { reverse_opinion = { who = ROOT value = 0 } }
				}
			}
			#won't go if he has a legit child unless lunatic/possessed
			OR = {
				NOT = {
					any_child = {
						is_alive = yes
						is_adult = no
						NOT = { trait = bastard }
					}
				}
				trait = lunatic
				trait = possessed
			}
			liege = {
				religion = ROOT
				war = no
			}
		}
		effect = {
			if = {
				limit = {
					liege = {
						OR = {
							ai = yes
							NOR = {
								dynasty = ROOT
								is_close_relative = ROOT
							}
						}
					}
				}
				any_spouse = { remove_spouse = ROOT }
				if = {
					limit = { is_female = no }
					change_learning = 1
					add_trait = monk
					set_character_flag = title_monk
					set_special_character_title = MONK_BROTHER
				}
				if = {
					limit = { is_female = yes }
					change_learning = 1
					add_trait = nun
					set_character_flag = title_nun
					set_special_character_title = NUN_SISTER
				}
			}
			if = {
				limit = {
					liege = {
						ai = no
						OR = {
							dynasty = ROOT
							is_close_relative = ROOT
						}
					}
				}
				liege = {
					letter_event = { id = Plus.2062 }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.1
				always = yes
			}
			modifier = {
				factor = 0.1
				NOT = { age = 20 }
			}
			modifier = {
				factor = 0.25
				NOT = { age = 25 }
			}
			modifier = {
				factor = 0.5
				NOT = { age = 30 }
			}
			modifier = {
				factor = 5
				OR = {
					liege = { is_theocracy = yes }
					has_job_title = job_spiritual
				}
			}
			modifier = {
				factor = 0.5
				NOR = {
					trait = detached_priest
					trait = martial_cleric
					trait = scholarly_theologian
					trait = mastermind_theologian
				}
			}
			modifier = {
				factor = 10
				has_character_modifier = voice_of_jesus
			}
			modifier = {
				factor = 2
				trait = zealous
			}
			modifier = {
				factor = 0.25
				trait = proud
			}
			modifier = {
				factor = 0.25
				trait = slothful
			}
			modifier = {
				factor = 0.25
				OR = {
					father_even_if_dead = { higher_tier_than = COUNT }
					mother_even_if_dead = { higher_tier_than = COUNT }
				}
			}
		}
	}

	ai_gain_title_pope = {
		is_high_prio = yes
		ai_check_interval = 12
		only_playable = yes
		potential = {
			ai = yes
			is_playable = yes
			controls_religion = no
			religion_group = christian
			OR = {
				has_landed_title = d_pentarch_rome
				has_landed_title = d_pentarch_alexandria
			}
			NOT = { has_character_flag = has_title_pope }
			NOT = { has_character_flag = has_title_ecumenical }
		}
		allow = {
			always = yes
		}
		effect = {
			set_special_character_title = POPE
			set_character_flag = has_title_pope
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	ai_lose_title_pope = {
		is_high_prio = yes
		ai_check_interval = 12
		only_playable = no
		potential = {
			ai = yes
			has_character_flag = has_title_pope
		}
		allow = {
			NAND = {
				is_playable = yes
				controls_religion = no
				religion_group = christian
				OR = {
					has_landed_title = d_pentarch_rome
					has_landed_title = d_pentarch_alexandria
				}
			}
		}
		effect = {
			remove_special_character_title = POPE
			clr_character_flag = has_title_pope
		}
		ai_will_do = {
			factor = 1
		}
	}

	ai_gain_title_ecumenical = {
		is_high_prio = yes
		ai_check_interval = 12
		only_playable = yes
		potential = {
			ai = yes
			is_playable = yes
			controls_religion = no
			religion = chalcedonian
			has_landed_title = d_pentarch_constantinople
			NOT = { has_character_flag = has_title_ecumenical }
			NOT = { has_character_flag = has_title_pope }
		}
		allow = {
			always = yes
		}
		effect = {
			set_special_character_title = ECUMENICAL_PATRIARCH
			set_character_flag = has_title_ecumenical
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	ai_lose_title_ecumenical = {
		is_high_prio = yes
		ai_check_interval = 12
		only_playable = no
		potential = {
			ai = yes
			has_character_flag = has_title_ecumenical
		}
		allow = {
			NAND = {
				is_playable = yes
				controls_religion = no
				religion = chalcedonian
				has_landed_title = d_pentarch_constantinople
			}
		}
		effect = {
			remove_special_character_title = ECUMENICAL_PATRIARCH
			clr_character_flag = has_title_ecumenical
		}
		ai_will_do = {
			factor = 1
		}
	}

	ai_abandon_NAP = {
		ai_check_interval = 60
		potential = {
			ai = yes
			is_adult = yes
			prisoner = no
			any_non_aggression_pact_character = {
				OR = {
					AND = {
						is_liege_of = ROOT
						NOT = { reverse_opinion = { who = ROOT value = 0 } }
					}
					AND = {
						any_demesne_title = {
							claimed_by = ROOT
							is_usable_claim_trigger = yes
						}
						OR = {
							NOT = { reverse_opinion = { who = ROOT value = 25 } }
							AND = {
								NOT = { reverse_opinion = { who = ROOT value = 50 } }
								ROOT = {
									OR = {
										trait = deceitful
										trait = ambitious
										trait = envious
										trait = proud
										trait = brave
									}
								}
							}
						}
					}
				}
				NOR = {
					is_allied_with = ROOT
					is_married = ROOT
					any_close_relative = { is_married = ROOT }
					ROOT = { any_heir_title = { holder = PREVPREV } }
					reverse_has_opinion_modifier = { who = ROOT modifier = hostile_family }
					reverse_has_opinion_modifier = { who = ROOT modifier = in_non_aggression_pact }
				}
			}
		}
		allow = {
			NOR = {
				trait = content
				trait = craven
				trait = incapable
				trait = imbecile
				trait = nun
				trait = monk
				is_inaccessible_trigger = yes
				any_liege = { holy_order = yes }
				any_liege = { mercenary = yes }
			}
		}
		effect = {
			random_non_aggression_pact_character = {
				limit = {
					OR = {
						AND = {
							is_liege_of = ROOT
							NOT = { reverse_opinion = { who = ROOT value = 0 } }
						}
						AND = {
							any_demesne_title = {
								claimed_by = ROOT
								is_usable_claim_trigger = yes
							}
							OR = {
								NOT = { reverse_opinion = { who = ROOT value = 25 } }
								AND = {
									NOT = { reverse_opinion = { who = ROOT value = 50 } }
									ROOT = {
										OR = {
											trait = deceitful
											trait = ambitious
											trait = envious
											trait = proud
											trait = brave
										}
									}
								}
							}
						}
					}
					NOR = {
						is_allied_with = ROOT
						is_married = ROOT
						any_close_relative = { is_married = ROOT }
						ROOT = { any_heir_title = { holder = PREVPREV } }
						reverse_has_opinion_modifier = { who = ROOT modifier = hostile_family }
						reverse_has_opinion_modifier = { who = ROOT modifier = in_non_aggression_pact }
					}
				}
				reverse_opinion = { who = ROOT modifier = hostile_family years = 1 }
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
}

title_decisions = {
	
	ai_institute_gavelkind = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_gavelkind } }
					}
				}
			}					
			NOT = { has_law = succ_gavelkind }
			OR = {
				NOR = {
					has_law = succ_elective_gavelkind
					has_law = succ_feudal_elective
					has_law = succ_tanistry
				}
				is_titular = yes
			}
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				OR = {
					is_feudal = yes
					is_tribal = yes
				}
				NOT = { religion = celtic_pagan }
				in_revolt = no
				NOT = { has_character_flag = voting_on_succession_law }
			}
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_basic_effect = yes
			tooltip = { succession = gavelkind }
			
			hidden_tooltip = {
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing law for [emf_succ_title.GetID] to Gavelkind"
				
				# Mark current heir and pretenders so that they may be tracked after law change
				emf_laws_change_succ_save_contenders_effect = yes
				
				# Actually change the law now
				add_law = succ_gavelkind
				
				current_heir = { save_event_target_as = emf_heir }

				holder_scope = {
					# Vassals are slightly upset
					any_vassal = {
						limit = {
							emf_laws_change_succ_affected_vassal_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_mild_reaction_effect = yes
					}
					# Legitimate children are happy
					any_child = {
						limit = {
							can_inherit_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_good_reaction_effect = yes
					}
					# Current heir is slightly upset if they were the previous heir (they have to share)
					event_target:emf_heir = {
						if = {
							limit = { character = event_target:emf_former_heir }
							emf_laws_change_succ_mild_reaction_effect = yes
						}
					}
				}
				
				# Specfic, event-driven succession law change reactions for the new heir, the former heir,
				# the former pretenders, and the new pretenders.
				set_title_flag = emf_succ_title
				holder_scope = { character_event = { id = Plus.604 } }
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_gavelkind
			}
			modifier = {
				factor = 0
				NOR = {
					# title was gavelkind previously
					AND = {
						is_primary_holder_title = yes
						has_title_flag = former_gavelkind
						OR = {
							has_law = succ_ultimogeniture
							# now primogeniture but ruler has more than one possible heir
							AND = {
								has_law = succ_primogeniture
								holder_scope = {
									any_child = {
										can_inherit_trigger = yes
										NOR = {
											is_dumb_trigger = yes
											trait = inbred
											trait = incapable
											trait = lunatic
										}
										OR = {
											AND = {
												ROOT = {
													NOR = {
														has_law = cognatic_succession
														has_law = agnatic_succession
													}
												}
												is_female = yes
											}
											AND = {
												ROOT = {
													NOR = {
														has_law = enatic_cognatic_succession
														has_law = enatic_succession
													}
												}
												is_female = no
											}
										}
										dynasty = PREV
										count = 2
									}
								}
							}
							# now seniority but ruler has at least one child who could be heir
							AND = {
								has_law = succ_seniority
								holder_scope = {
									NOR = {
										culture_group = east_slavic
										culture_group = west_slavic
										culture_group = south_slavic
										culture = hungarian
										culture = ethiopian
									}
									primary_title = {
										current_heir = {
											NOT = { is_child_of = PREVPREV }
										}
									}
									any_child = {
										can_inherit_trigger = yes
										NOR = {
											is_dumb_trigger = yes
											trait = inbred
											trait = incapable
											trait = lunatic
										}
										OR = {
											AND = {
												ROOT = {
													NOR = {
														has_law = cognatic_succession
														has_law = agnatic_succession
													}
												}
												is_female = yes
											}
											AND = {
												ROOT = {
													NOR = {
														has_law = enatic_cognatic_succession
														has_law = enatic_succession
													}
												}
												is_female = no
											}
										}
										dynasty = PREV
									}
								}
							}
						}
					}
					# primary title is gavelkind
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_gavelkind } }
					}
					# has multiple possible heirs and is kind or charitable
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
								reverse_opinion = { who = PREV value = 25 }
								is_adult = yes
								count = 2
							}
							OR = {
								trait = kind
								trait = charitable
								trait = just
							}
							NOR = {
								trait = ambitious
								trait = proud
								trait = cruel
							}
						}
					}
				}
			}
		}
	}

	ai_institute_seniority = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_seniority } }
					}
				}
			}
			NOT = { has_law = succ_seniority }
			OR = {
				NOR = {
					has_law = succ_elective_gavelkind
					has_law = succ_feudal_elective
					has_law = succ_tanistry
				}
				is_titular = yes
			}
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				OR = {
					is_merchant_republic = no
					primary_title = {
						NOT = { title = PREVPREV }
					}
				}
				OR = {
					is_republic = no
					is_patrician = yes
				}
				OR = {
					is_feudal = yes
					is_patrician = yes
				}
				NOT = { religion_group = indian_group }
				NOT = { religion = celtic_pagan }
				OR = {
					is_pagan_religion = no
					is_reformed_pagan_religion = yes
					is_tribal = no
					has_global_flag = ancient_religions
				}
				NOT = { has_character_flag = voting_on_succession_law }
			}
			OR = {
				NOT = { tier = baron }
				holder_scope = { is_patrician = yes }
			}
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
			OR = {
				emf_laws_crownlaw_title_medium_ca_plus_trigger = yes
				emf_laws_holder_has_late_adminstration_plus_trigger = yes
				emf_laws_holder_liege_has_late_adminstration_plus_trigger = yes
				custom_tooltip = {
					text = emf_laws_ctt_patrician_not_primary_title
					holder_scope = {
						is_patrician = yes
						OR = {
							is_merchant_republic = no
							PREV = {
								is_primary_holder_title = no
							}
							NOR = {
								in_revolt = yes
								succ_law_title = {
									title = PREVPREV
								}
							}
						}
					}
				}
			}
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_basic_effect = yes
			tooltip = { succession = seniority }
			
			hidden_tooltip = {
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing law for [emf_succ_title.GetID] to Seniority"
				
				emf_laws_change_succ_save_contenders_effect = yes
				add_law = succ_seniority
				current_heir = { save_event_target_as = emf_heir }

				holder_scope = {
					# Vassals are slightly upset
					any_vassal = {
						limit = {
							emf_laws_change_succ_affected_vassal_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_mild_reaction_effect = yes
					}
					# Dynasty members are happy
					any_dynasty_member = {
						limit = {
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_good_reaction_effect = yes
					}
					# Children that can inherit are angry
					any_child = {
						limit = {
							can_inherit_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_bad_reaction_effect = yes
					}
				}
				
				# Specfic, event-driven succession law change reactions for the new heir, the former heir,
				# the former pretenders, and the new pretenders.
				set_title_flag = emf_succ_title
				holder_scope = { character_event = { id = Plus.604 } }
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 20
				has_title_flag = former_seniority
			}
			modifier = {
				factor = 1.1
				holder_scope = { capital_scope = { TECH_LEGALISM = 2 }}
			}
			modifier = {
				factor = 1.2
				holder_scope = { capital_scope = { TECH_LEGALISM = 3 }}
			}
			modifier = {
				factor = 1.3
				holder_scope = { capital_scope = { TECH_LEGALISM = 4 }}
			}
			modifier = {
				factor = 0
				NOR = {
					# title was seniority previously
					AND = {
						is_primary_holder_title = yes
						has_title_flag = former_seniority
						# have a decent dynasty relative I like
						holder_scope = {
							any_dynasty_member = {
								dynasty = PREV
								is_older_than = PREV
								is_close_relative = PREV
								reverse_opinion = { who = PREV value = 25 }
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
									is_child_of = PREV
									father_even_if_dead = { is_child_of = PREVPREV }
									mother_even_if_dead = { is_child_of = PREVPREV }
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
							}
						}
						OR = {
							has_law = succ_ultimogeniture
							# now gavelkind and I don't have multiple children
							AND = {
								has_law = succ_gavelkind
								holder_scope = {
									NOT = {
										any_child = {
											can_inherit_trigger = yes
											NOR = {
												is_dumb_trigger = yes
												trait = inbred
												trait = incapable
												trait = lunatic
											}
											OR = {
												AND = {
													ROOT = {
														NOR = {
															has_law = cognatic_succession
															has_law = agnatic_succession
														}
													}
													is_female = yes
												}
												AND = {
													ROOT = {
														NOR = {
															has_law = enatic_cognatic_succession
															has_law = enatic_succession
														}
													}
													is_female = no
												}
											}
											dynasty = PREV
											count = 2
										}
									}
								}
							}
							# now primogeniture and my heir is either unsuitable or not my child
							AND = {
								has_law = succ_primogeniture
								current_heir = {
									OR = {
										is_dumb_trigger = yes
										trait = inbred
										trait = incapable
										trait = lunatic
										NOR = {
											is_child_of = FROM
											father_even_if_dead = { is_child_of = FROM }
											mother_even_if_dead = { is_child_of = FROM }
										}
									}
								}
							}
						}
					}
					# primary title is seniority
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_seniority } }
					}
					# older ruler has no children but has a liked close relative and the heir is neither liked nor a close relative
					AND = {
						is_primary_holder_title = no
						current_heir = {
							NOR = {
								is_close_relative = FROM
								reverse_opinion = { who = FROM value = 25 }
							}
						}
						holder_scope = {
							age = 40
							any_dynasty_member = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								reverse_opinion = { who = FROM value = 25 }
								is_adult = yes
								is_close_relative = PREV
							}
							NOT = {
								any_child = {
									can_inherit_trigger = yes
									NOR = {
										trait = imbecile
										trait = inbred
										trait = incapable
										trait = lunatic
									}
									OR = {
										AND = {
											ROOT = {
												NOR = {
													has_law = cognatic_succession
													has_law = agnatic_succession
												}
											}
											is_female = yes
										}
										AND = {
											ROOT = {
												NOR = {
													has_law = enatic_cognatic_succession
													has_law = enatic_succession
												}
											}
											is_female = no
										}
									}
									dynasty = PREV
								}
							}
						}
					}
					# is slavic and under gavelkind
					AND = {
						is_primary_holder_title = yes
						has_law = succ_gavelkind
						holder_scope = {
							OR = {
								culture_group = east_slavic
								culture_group = west_slavic
								culture_group = south_slavic
								culture = hungarian
								culture = ethiopian
							}
							NOR = {
								trait = content
								trait = charitable
								trait = kind
								trait = slothful
								is_dumb_trigger = yes
								trait = inbred
							}
						}
					}
				}
			}
		}
	}

	ai_institute_primogeniture = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_primogeniture } }
					}
				}
			}
			NOT = { has_law = succ_primogeniture }
			OR = {
				NOR = {
					has_law = succ_elective_gavelkind
					has_law = succ_feudal_elective
					has_law = succ_tanistry
				}
				is_titular = yes
			}
			OR = {
				holder_scope = {
					any_demesne_title = { is_vice_royalty = yes }
					NOT = { has_character_flag = voting_on_succession_law }
				}
				AND = {
					holder_scope = {
						NOT = { religion_group = muslim }
						NOT = { religion = celtic_pagan }
						is_merchant_republic = no
						is_patrician = no
						OR = {
							independent = yes
							NOT = { any_liege = { holy_order = yes } }
						}
						OR = {
							is_pagan_religion = no
							is_reformed_pagan_religion = yes
							is_tribal = no
							has_global_flag = ancient_religions
						}
						NOT = { has_character_flag = voting_on_succession_law }
					}
					is_primary_type_title = no
					OR = {
						AND = {
							tier = baron
							is_feudal = yes
						}
						holder_scope = { is_feudal = yes }
					}
				}
			}
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
			OR = {
				emf_laws_crownlaw_title_high_ca_plus_trigger = yes
				emf_laws_holder_has_late_adminstration_plus_trigger = yes
				emf_laws_holder_liege_has_late_adminstration_plus_trigger = yes
				custom_tooltip = {
					text = emf_ctt_holds_viceroyalty
					hidden_tooltip = {
						holder_scope = { any_demesne_title = { is_vice_royalty = yes } }
					}
				}
			}
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_basic_effect = yes
			tooltip = { succession = primogeniture }
			hidden_tooltip = {
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing law for [emf_succ_title.GetID] to Primogeniture"
				
				emf_laws_change_succ_save_contenders_effect = yes
				add_law = succ_primogeniture
				current_heir = { save_event_target_as = emf_heir }

				holder_scope = {
					# Vassals are slightly upset
					any_vassal = {
						limit = {
							emf_laws_change_succ_affected_vassal_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_mild_reaction_effect = yes
					}
					# Dynasty members are slightly upset
					any_dynasty_member = {
						limit = {
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_mild_reaction_effect = yes
					}
					# Legitimate children are angry
					any_child = {
						limit = {
							can_inherit_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_bad_reaction_effect = yes
					}
					# Heir is happy
					event_target:emf_heir = { emf_laws_change_succ_good_reaction_effect = yes }
				}
				
				set_title_flag = emf_succ_title
				holder_scope = { character_event = { id = Plus.604 } }
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 20
				has_title_flag = former_primogeniture
			}
			modifier = {
				factor = 1.1
				holder_scope = { capital_scope = { TECH_LEGALISM = 3 }}
			}
			modifier = {
				factor = 1.2
				holder_scope = { capital_scope = { TECH_LEGALISM = 4 }}
			}
			modifier = {
				factor = 1.3
				holder_scope = { capital_scope = { TECH_LEGALISM = 5 }}
			}
			modifier = {
				factor = 0
				NOR = {
					# primary title has primogeniture
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_primogeniture } }
					}
					# has an eligible heir over 6 years old and is proud/ambitious ( or the title was primogeniture previously )
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
								age = 6
							}
							OR = {
								trait = proud
								trait = ambitious
								ROOT = { has_title_flag = former_primogeniture }
							}
						}
					}
					# has an exceptional heir and is not content or humble
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								calc_true_if = {
									amount = 3
									diplomacy = 21
									martial = 21
									diplomacy = 15
									martial = 15
									diplomacy = 8
									martial = 8
									stewardship = 8
									trait = genius
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
								NOT = {
									any_sibling = {
										NOT = { trait = bastard }
										is_older_than = PREV
										OR = {
											AND = {
												ROOT = {
													NOR = {
														has_law = cognatic_succession
														has_law = agnatic_succession
													}
												}
												is_female = yes
											}
											AND = {
												ROOT = {
													NOR = {
														has_law = enatic_cognatic_succession
														has_law = enatic_succession
													}
												}
												is_female = no
											}
										}
									}
								}
							}
							NOR = {
								trait = content
								trait = humble
							}
						}
					}
					# has an eligible heir and doesn't like current heir, and is not content/humble/craven
					AND = {
						is_primary_holder_title = yes
						current_heir = {
							NOR = {
								dynasty = FROM
								is_close_relative = FROM
								reverse_opinion = { who = FROM value = 0 }
							}
						}
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
								age = 6
							}
							NOR = {
								trait = content
								trait = humble
								trait = craven
							}
						}
					}
				}
			}
		}
	}
	
	ai_institute_feudal_elective = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			is_primary_holder_title = yes
			higher_tier_than = DUKE
			NOT = { has_law = succ_feudal_elective }
			OR = {
				NOR = {
					has_law = succ_elective_gavelkind
					has_law = succ_tanistry
				}
				is_titular = yes
			}
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				NOT = { religion_group = indian_group }
				NOT = { religion = celtic_pagan }
				OR = {
					is_pagan_religion = no
					is_reformed_pagan_religion = yes
					is_tribal = no
					has_global_flag = ancient_religions
				}
				is_feudal = yes
				NOT = { has_character_flag = voting_on_succession_law }
			}			
			NOT = { tier = baron }
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_basic_effect = yes
			tooltip = { succession = feudal_elective }
			
			hidden_tooltip = {
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing law for [emf_succ_title.GetID] to Feudal Elective"
				
				emf_laws_change_succ_save_contenders_effect = yes
				add_law = succ_feudal_elective
				current_heir = { save_event_target_as = emf_heir }

				holder_scope = {
					# Vassals are slightly upset
					any_vassal = {
						limit = {
							emf_laws_change_succ_affected_vassal_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_mild_reaction_effect = yes
					}
					# Electors are happy
					any_vassal = {
						limit = {
							emf_laws_change_succ_vip_trigger = no
							OR = {
								AND = {
									event_target:emf_succ_title = { tier = EMPEROR }
									higher_real_tier_than = COUNT
									OR = {
										event_target:emf_succ_title = { is_titular = yes }
										any_demesne_title = {
											higher_tier_than = COUNT
											de_jure_liege_or_above = event_target:emf_succ_title
										}
									}
								}
								AND = {
									event_target:emf_succ_title = { tier = KING }
									real_tier = DUKE
									OR = {
										event_target:emf_succ_title = { is_titular = yes }
										any_demesne_title = {
											tier = DUKE
											de_jure_liege = event_target:emf_succ_title
										}
									}
								}
								AND = {
									event_target:emf_succ_title = { tier = DUKE }
									real_tier = COUNT
									OR = {
										event_target:emf_succ_title = { is_titular = yes }
										any_demesne_title = {
											tier = COUNT
											de_jure_liege = event_target:emf_succ_title
										}
									}
								}
								AND = {
									event_target:emf_succ_title = { tier = COUNT }
									real_tier = BARON
									any_demesne_title = {
										de_jure_liege = event_target:emf_succ_title
									}
								}
							}
						}
						emf_laws_change_succ_good_reaction_effect = yes
					}
					# Dynasty members are slightly upset
					any_dynasty_member = {
						limit = {
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_mild_reaction_effect = yes
					}
					# Legitimate children are angry
					any_child = {
						limit = {
							can_inherit_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_bad_reaction_effect = yes
					}
					# The former pretenders are upset
					any_opinion_modifier_target = {
						limit = { reverse_has_opinion_modifier = { who = PREV modifier = opinion_succ_former_pretender } }
						emf_laws_change_succ_bad_reaction_effect = yes
						event_target:emf_succ_title = {
							if = {
								limit = { NOT = { claimed_by = PREV } }
								add_weak_claim = PREV
							}
						}
						any_spouse = {
							limit = { emf_laws_change_succ_vip_trigger = no }
							emf_laws_change_succ_bad_reaction_effect = yes
						}
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							emf_laws_change_succ_bad_reaction_effect = yes
						}
					}
					# The former heir is upset
					event_target:emf_former_heir = {
						emf_laws_change_succ_bad_reaction_effect = yes
						event_target:emf_succ_title = { add_claim = PREV }
						any_spouse = {
							limit = { emf_laws_change_succ_vip_trigger = no }
							emf_laws_change_succ_bad_reaction_effect = yes
						}
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							emf_laws_change_succ_bad_reaction_effect = yes
						}
					}
				}
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_feudal_elective
			}
			modifier = {
				factor = 0
				NOR = {
					# is Hellenic
					holder_scope = { religion_group = hellenic_pagan_group }
					# has a lot of vassals, is way over the vassal limit, and not a strong ruler
					holder_scope = {
						num_of_vassals = 15
						over_vassal_limit = 5
						OR = {
							trait = trusting
							trait = humble
							trait = charitable
							is_dumb_trigger = yes
							trait = infirm
						}
						NOR = {
							trait = ambitious
							trait = proud
							trait = lunatic
							diplomacy = 12
							martial = 12
							stewardship = 12
							intrigue = 12
						}
					}
					# is culture appropriate with at least three dukes and over the vassal limit
					holder_scope = {
						OR = {
							culture_group = north_germanic
							culture_group = central_germanic
							culture_group = west_germanic
							culture = frankish
							culture = norman
							culture = roman
							culture = nubian
							culture = somali
						}
						any_vassal = {
							higher_real_tier_than = COUNT
							count = 3
						}
						over_vassal_limit = 3
					}
					# has no close relative heirs and has at least three vassals he likes
					holder_scope = {
						ROOT = { current_heir = { NOT = { is_close_relative = PREVPREV } } }
						age = 40
						OR = {
							trait = charitable
							trait = just
							trait = gregarious
							is_dumb_trigger = yes
						}
						NOR = {
							trait = ambitious
							trait = proud
						}
						any_vassal = {
							is_feudal = yes
							is_adult = yes
							primary_title = { de_jure_liege_or_above = ROOT }
							NOT = { any_heir_title = { title = ROOT } }
							reverse_opinion = { who = PREV value = 25 }
							count = 3
						}
					}
				}
			}
		}
	}

	ai_institute_tanistry = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_tanistry } }
					}
				}
			}
			NOT = { has_law = succ_tanistry }
			OR = {
				NOR = {
					has_law = succ_elective_gavelkind
					has_law = succ_feudal_elective
				}
				is_titular = yes
			}
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				NOT = { religion_group = indian_group }
				OR = {
					is_feudal = yes
					is_tribal = yes
				}
				OR = {
					culture_group = celtic
					religion = celtic_pagan
				}
				NOT = { has_character_flag = voting_on_succession_law }
			}
			NOT = { tier = baron }
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_basic_effect = yes
			tooltip = { succession = tanistry }
			
			hidden_tooltip = {
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing law for [emf_succ_title.GetID] to Tanistry"
				
				emf_laws_change_succ_save_contenders_effect = yes
				add_law = succ_tanistry
				current_heir = { save_event_target_as = emf_heir }

				# FIXME: I'm just guessing about Tanistry's behavior from the little, imprecise, and likely
				# inaccurate info on the CKII Wiki.

				holder_scope = {
					# Vassals are slightly upset
					any_vassal = {
						limit = {
							emf_laws_change_succ_affected_vassal_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_mild_reaction_effect = yes
					}
					# Elector vassals are happy.
					# Regarding Tanistry, my assumptions are that empire-tier titles have king- and
					# duke-tier electors, king-tier titles have duke- and count-tier electors, duke-tier
					# titles have count- and baron-tier electors, and count-tier titles have baron-tier
					# electors.
					any_vassal = {
						limit = {
							emf_laws_change_succ_vip_trigger = no
							OR = {
								AND = {
									event_target:emf_succ_title = { tier = EMPEROR }
									higher_real_tier_than = COUNT
									OR = {
										event_target:emf_succ_title = { is_titular = yes }
										any_demesne_title = {
											higher_tier_than = COUNT
											de_jure_liege_or_above = event_target:emf_succ_title
										}
									}
								}
								AND = {
									event_target:emf_succ_title = { tier = KING }
									higher_real_tier_than = BARON
									OR = {
										event_target:emf_succ_title = { is_titular = yes }
										any_demesne_title = {
											higher_tier_than = BARON
											de_jure_liege_or_above = event_target:emf_succ_title
										}
									}
								}
								AND = {
									event_target:emf_succ_title = { tier = DUKE }
									OR = {
										event_target:emf_succ_title = { is_titular = yes }
										any_demesne_title = {
											de_jure_liege_or_above = event_target:emf_succ_title
										}
									}
								}
								AND = {
									event_target:emf_succ_title = { tier = COUNT }
									any_demesne_title = {
										de_jure_liege = event_target:emf_succ_title
									}
								}
							}
						}
						emf_laws_change_succ_good_reaction_effect = yes
					}
					# Dynasty members are happy
					any_dynasty_member = {
						limit = { 
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_good_reaction_effect = yes
					}
					# Legitimate children are angry
					any_child = {
						limit = {
							can_inherit_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						emf_laws_change_succ_bad_reaction_effect = yes
					}
					# The former pretenders are upset if not of the ruler's dynasty
					any_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = { who = PREV modifier = opinion_succ_former_pretender }
							NOT = { dynasty = event_target:emf_succ_changer }
						}
						emf_laws_change_succ_bad_reaction_effect = yes
						event_target:emf_succ_title = {
							if = {
								limit = { NOT = { claimed_by = PREV } }
								add_weak_claim = PREV
							}
						}
						any_spouse = {
							limit = {
								emf_laws_change_succ_vip_trigger = no
								NOT = { dynasty = event_target:emf_succ_changer }
							}
							emf_laws_change_succ_bad_reaction_effect = yes
						}
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
								NOT = { dynasty = event_target:emf_succ_changer }
							}
							emf_laws_change_succ_bad_reaction_effect = yes
						}
					}
					# The former heir is upset if not of the ruler's dynasty
					event_target:emf_former_heir = {
						if = {
							limit = { NOT = { dynasty = event_target:emf_succ_changer } }
							event_target:emf_succ_title = { add_claim = PREV }
							emf_laws_change_succ_terrible_reaction_effect = yes
							any_spouse = {
								limit = {
									emf_laws_change_succ_vip_trigger = no
									NOT = { dynasty = event_target:emf_succ_changer }
								}
								emf_laws_change_succ_terrible_reaction_effect = yes
							}
							any_child = {
								limit = {
									can_inherit_trigger = yes
									emf_laws_change_succ_vip_trigger = no
									NOT = { dynasty = event_target:emf_succ_changer }
								}
								emf_laws_change_succ_terrible_reaction_effect = yes
							}
						}
					}
				}
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_tanistry
			}
			modifier = {
				factor = 0
				NOR = {
					# is celtic pagan
					AND = {
						is_primary_holder_title = yes
						holder_scope = { religion = celtic_pagan }
					}
					# was formerly tanistry and current heir is not of the ruler's dynasty
					AND = {
						is_primary_holder_title = yes
						has_title_flag = former_tanistry
						current_heir = { NOT = { dynasty = FROM } }
						holder_scope = {
							any_dynasty_member = {
								is_adult = yes
								same_realm = PREV
								can_inherit_trigger = yes
								NOT = { character = PREV }
							}
						}
					}
					# primary title has gavelkind and is proud or ambitious
					AND = {
						is_primary_holder_title = yes
						has_law = succ_gavelkind
						holder_scope = {
							OR = {
								trait = proud
								trait = ambitious
							}
						}
					}
					# primary title has tanistry
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_tanistry } }
					}
					# has no child or brother as heir and is charitable or just
					AND = {
						is_primary_holder_title = yes
						current_heir = {
							NOR = {
								is_child_of = FROM
								sibling = FROM
							}
						}
						holder_scope = {
							age = 30
							OR = {
								trait = charitable
								trait = just
							}
						}
					}
				}
			}
		}
	}

	ai_institute_elective_to_gavelkind = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_gavelkind } }
					}
				}
			}
			OR = {
				has_law = succ_elective_gavelkind
				has_law = succ_feudal_elective
				has_law = succ_tanistry
			}
			is_titular = no
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				OR = {
					is_feudal = yes
					is_tribal = yes
				}
				NOT = { religion = celtic_pagan }
				in_revolt = no
				NOT = { has_character_flag = voting_on_succession_law }
			}
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			current_inheritance_laws_effect = yes
			log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) holding vote to change [emf_succ_title.GetID] to Gavelkind"
			
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			custom_tooltip = { text = CUSTOMTOOLTIP7 }
			hidden_tooltip = {
				set_title_flag = emf_succ_title
				holder_scope = {
					set_character_flag = voting_on_succession_law
					set_character_flag = vote_is_for_gavelkind
					set_variable = { which = "succession_law_vote" value = 1 }
					character_event = { id = Plus.600 }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_gavelkind
			}
			modifier = {
				factor = 0
				NOR = {
					# title was gavelkind previously, now feudal elective/tanistry but ruler has at least one child who could be heir
					AND = {
						is_primary_holder_title = yes
						has_title_flag = former_gavelkind
						OR = {
							has_law = succ_feudal_elective
							has_law = succ_tanistry
						}
						holder_scope = {
							primary_title = {
								current_heir = {
									NOT = { is_child_of = PREVPREV }
								}
							}
							any_child = {
								NOR = {
									trait = bastard
									trait = disinherited
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
							}
						}
					}
					# ruler is tribal, has at least one child, and primary title is elective gavelkind
					AND = {
						is_primary_holder_title = yes
						has_law = succ_elective_gavelkind
						holder_scope = {
							is_tribal = yes
							any_child = {
								can_inherit_trigger = yes
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
							}
						}
					}
					# primary title is gavelkind
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_gavelkind } }
					}
					# has multiple possible adult heirs and is kind or charitable
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = { has_law = true_cognatic_succession }
										is_female = yes
									}
									is_female = no
								}
								reverse_opinion = { who = PREV value = 25 }
								is_adult = yes
								count = 2
							}
							OR = {
								trait = kind
								trait = charitable
								trait = just
							}
							NOR = {
								trait = ambitious
								trait = proud
								trait = cruel
							}
						}
					}
				}
			}
		}
	}
	
	ai_institute_elective_to_seniority = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_seniority } }
					}
				}
			}
			OR = {
				has_law = succ_elective_gavelkind
				has_law = succ_feudal_elective
				has_law = succ_tanistry
			}
			is_titular = no
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				OR = {
					is_merchant_republic = no
					primary_title = {
						NOT = { title = PREVPREV }
					}
				}
				OR = {
					is_republic = no
					is_patrician = yes
				}
				OR = {
					is_feudal = yes
					is_patrician = yes
				}
				NOT = { religion_group = indian_group }
				NOT = { religion = celtic_pagan }
				OR = {
					is_pagan_religion = no
					is_reformed_pagan_religion = yes
					is_tribal = no
					has_global_flag = ancient_religions
				}
				NOT = { has_character_flag = voting_on_succession_law }
			}
			OR = {
				NOT = { tier = baron }
				holder_scope = { is_patrician = yes }
			}
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
			OR = {
				emf_laws_crownlaw_title_medium_ca_plus_trigger = yes
				emf_laws_holder_has_late_adminstration_plus_trigger = yes
				emf_laws_holder_liege_has_late_adminstration_plus_trigger = yes
				custom_tooltip = {
					text = emf_laws_ctt_patrician_not_primary_title
					holder_scope = {
						is_patrician = yes
						OR = {
							is_merchant_republic = no
							PREV = {
								is_primary_holder_title = no
							}
							NOR = {
								in_revolt = yes
								succ_law_title = {
									title = PREVPREV
								}
							}
						}
					}
				}
			}
		}
		effect = {
			current_inheritance_laws_effect = yes
			log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) holding vote to change [emf_succ_title.GetID] to Seniority"
			
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			custom_tooltip = { text = CUSTOMTOOLTIP7 }
			hidden_tooltip = {
				set_title_flag = emf_succ_title
				holder_scope = {
					set_character_flag = voting_on_succession_law
					set_character_flag = vote_is_for_seniority
					set_variable = { which = "succession_law_vote" value = 1 }
					character_event = { id = Plus.600 }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_seniority
			}
			modifier = {
				factor = 1.1
				holder_scope = { capital_scope = { TECH_LEGALISM = 2 }}
			}
			modifier = {
				factor = 1.2
				holder_scope = { capital_scope = { TECH_LEGALISM = 3 }}
			}
			modifier = {
				factor = 1.3
				holder_scope = { capital_scope = { TECH_LEGALISM = 4 }}
			}
			modifier = {
				factor = 0
				NOR = {
					# title was seniority previously
					AND = {
						is_primary_holder_title = yes
						has_title_flag = former_seniority
						# have a decent dynasty relative I like
						holder_scope = {
							any_dynasty_member = {
								dynasty = PREV
								is_older_than = PREV
								is_close_relative = PREV
								reverse_opinion = { who = PREV value = 25 }
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
									is_child_of = PREV
									father_even_if_dead = { is_child_of = PREVPREV }
									mother_even_if_dead = { is_child_of = PREVPREV }
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
							}
						}
						OR = {
							# is now feudal elective and heir is neither the ruler's dynasty or a close relative
							AND = {
								has_law = succ_feudal_elective
								current_heir = {
									NOR = {
										is_close_relative = FROM
										dynasty = FROM
									}
								}
							}
							# is now tanistry and heir is not a close relative
							AND = {
								has_law = succ_tanistry
								current_heir = { NOT = { is_close_relative = FROM } }
							}
						}
					}
					# primary title is seniority
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_seniority } }
					}
					# older ruler has no children but has a liked close relative and heir is neither liked nor a close relative
					AND = {
						is_primary_holder_title = no
						current_heir = {
							NOR = {
								is_close_relative = FROM
								reverse_opinion = { who = FROM value = 25 }
							}
						}
						holder_scope = {
							age = 40
							any_dynasty_member = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								reverse_opinion = { who = FROM value = 25 }
								is_adult = yes
								is_close_relative = PREV
							}
							NOT = {
								any_child = {
									can_inherit_trigger = yes
									NOR = {
										trait = imbecile
										trait = inbred
										trait = incapable
										trait = lunatic
									}
									OR = {
										AND = {
											ROOT = {
												NOR = {
													has_law = cognatic_succession
													has_law = agnatic_succession
												}
											}
											is_female = yes
										}
										AND = {
											ROOT = {
												NOR = {
													has_law = enatic_cognatic_succession
													has_law = enatic_succession
												}
											}
											is_female = no
										}
									}
									dynasty = PREV
								}
							}
						}
					}
					# slavic ruler who is ambitious or proud
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							OR = {
								culture_group = east_slavic
								culture_group = west_slavic
								culture_group = south_slavic
								culture = hungarian
							}
							OR = {
								trait = ambitious
								trait = proud
								ROOT = { has_law = succ_elective_gavelkind }
							}
							NOR = {
								trait = content
								trait = charitable
								trait = kind
								trait = slothful
								is_dumb_trigger = yes
								trait = inbred
							}
						}
					}
				}
			}
		}
	}
	
	ai_institute_elective_to_primogeniture = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = no
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_primogeniture } }
					}
				}
			}
			OR = {
				has_law = succ_elective_gavelkind
				has_law = succ_feudal_elective
				has_law = succ_tanistry
			}
			is_titular = no
			OR = {
				holder_scope = {
					any_demesne_title = { is_vice_royalty = yes }
					NOT = { has_character_flag = voting_on_succession_law }
				}
				AND = {
					holder_scope = {
						NOT = { religion_group = muslim }
						NOT = { religion = celtic_pagan }
						is_merchant_republic = no
						is_patrician = no
						OR = {
							independent = yes
							NOT = { any_liege = { holy_order = yes } }
						}
						OR = {
							is_pagan_religion = no
							is_reformed_pagan_religion = yes
							is_tribal = no
							has_global_flag = ancient_religions
						}
						NOT = { has_character_flag = voting_on_succession_law }
					}
					is_primary_type_title = no
					OR = {
						AND = {
							tier = baron
							is_feudal = yes
						}
						holder_scope = { is_feudal = yes }
					}
				}
			}
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
			OR = {
				emf_laws_crownlaw_title_high_ca_plus_trigger = yes
				emf_laws_holder_has_late_adminstration_plus_trigger = yes
				emf_laws_holder_liege_has_late_adminstration_plus_trigger = yes
				custom_tooltip = {
					text = emf_ctt_holds_viceroyalty
					hidden_tooltip = {
						holder_scope = { any_demesne_title = { is_vice_royalty = yes } }
					}
				}
			}
		}
		effect = {
			current_inheritance_laws_effect = yes
			log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) holding vote to change [emf_succ_title.GetID] to Primogeniture"
			
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			custom_tooltip = { text = CUSTOMTOOLTIP7 }
			hidden_tooltip = {
				set_title_flag = emf_succ_title
				holder_scope = {
					set_character_flag = voting_on_succession_law
					set_character_flag = vote_is_for_primogeniture
					set_variable = { which = "succession_law_vote" value = 1 }
					character_event = { id = Plus.600 }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_primogeniture
			}
			modifier = {
				factor = 1.1
				holder_scope = { capital_scope = { TECH_LEGALISM = 3 }}
			}
			modifier = {
				factor = 1.2
				holder_scope = { capital_scope = { TECH_LEGALISM = 4 }}
			}
			modifier = {
				factor = 1.3
				holder_scope = { capital_scope = { TECH_LEGALISM = 5 }}
			}
			modifier = {
				factor = 0
				NOR = {
					# primary title has primogeniture
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_primogeniture } }
					}
					# has an eligible heir over 6 years old and is proud/ambitious ( or the title was primogeniture previously )
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
								age = 6
							}
							OR = {
								trait = proud
								trait = ambitious
								ROOT = { has_title_flag = former_primogeniture }
							}
						}
					}
					# has an exceptional heir and is not content or humble
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								calc_true_if = {
									amount = 3
									diplomacy = 21
									martial = 21
									diplomacy = 15
									martial = 15
									diplomacy = 8
									martial = 8
									stewardship = 8
									trait = genius
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
								NOT = {
									any_sibling = {
										NOT = { trait = bastard }
										is_older_than = PREV
										OR = {
											AND = {
												ROOT = {
													NOR = {
														has_law = cognatic_succession
														has_law = agnatic_succession
													}
												}
												is_female = yes
											}
											AND = {
												ROOT = {
													NOR = {
														has_law = enatic_cognatic_succession
														has_law = enatic_succession
													}
												}
												is_female = no
											}
										}
									}
								}
							}
							NOR = {
								trait = content
								trait = humble
							}
						}
					}
					# has an eligible heir and doesn't like current heir, and is not content/humble/craven
					AND = {
						is_primary_holder_title = yes
						current_heir = {
							NOR = {
								dynasty = FROM
								is_close_relative = FROM
								reverse_opinion = { who = FROM value = 0 }
							}
						}
						holder_scope = {
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								OR = {
									AND = {
										ROOT = {
											NOR = {
												has_law = cognatic_succession
												has_law = agnatic_succession
											}
										}
										is_female = yes
									}
									AND = {
										ROOT = {
											NOR = {
												has_law = enatic_cognatic_succession
												has_law = enatic_succession
											}
										}
										is_female = no
									}
								}
								dynasty = PREV
								age = 6
							}
							NOR = {
								trait = content
								trait = humble
								trait = craven
							}
						}
					}
				}
			}
		}
	}	
	
	ai_institute_elective_to_tanistry = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = { dejure_liege_title = { has_law = succ_tanistry } }
					}
				}
			}
			OR = {
				has_law = succ_elective_gavelkind
				has_law = succ_feudal_elective
			}
			is_titular = no
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				NOT = { religion_group = indian_group }
				OR = {
					is_feudal = yes
					is_tribal = yes
				}
				OR = {
					culture_group = celtic
					religion = celtic_pagan
				}
				NOT = { has_character_flag = voting_on_succession_law }
			}
			NOT = { tier = baron }
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			current_inheritance_laws_effect = yes
			log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) holding vote to change [emf_succ_title.GetID] to Tanistry"
			
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			custom_tooltip = { text = CUSTOMTOOLTIP7 }
			hidden_tooltip = {
				set_title_flag = emf_succ_title
				holder_scope = {
					set_character_flag = voting_on_succession_law
					set_character_flag = vote_is_for_tanistry
					set_variable = { which = "succession_law_vote" value = 1 }
					character_event = { id = Plus.600 }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_tanistry
			}
			modifier = {
				factor = 0
				NOR = {
					# is celtic pagan
					AND = {
						is_primary_holder_title = yes
						holder_scope = { religion = celtic_pagan }
					}
					# was formerly tanistry and current heir is not of the ruler's dynasty
					AND = {
						is_primary_holder_title = yes
						has_title_flag = former_tanistry
						current_heir = { NOT = { dynasty = FROM } }
						holder_scope = {
							any_dynasty_member = {
								is_adult = yes
								same_realm = PREV
								can_inherit_trigger = yes
								NOT = { character = PREV }
							}
						}
					}
					# primary title has elective gavelkind and is proud or ambitious or tribal
					AND = {
						is_primary_holder_title = yes
						has_law = succ_elective_gavelkind
						holder_scope = {
							OR = {
								trait = proud
								trait = ambitious
								is_tribal = yes
							}
							any_dynasty_member = {
								is_adult = yes
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
							}
						}
					}
					# primary title has tanistry
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = succ_tanistry } }
					}
					# has no child or brother as heir and is charitable or just
					AND = {
						is_primary_holder_title = yes
						current_heir = {
							NOR = {
								is_child_of = FROM
								sibling = FROM
							}
						}
						holder_scope = {
							age = 30
							OR = {
								trait = charitable
								trait = just
							}
						}
					}
				}
			}
		}
	}

	ai_institute_elective_to_feudal_elective = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			is_primary_holder_title = yes
			higher_tier_than = DUKE
			OR = {
				has_law = succ_elective_gavelkind
				has_law = succ_tanistry
			}
			is_titular = no
			emf_laws_succ_basic_potential_trigger = yes
			holder_scope = {
				NOT = { religion_group = indian_group }
				NOT = { religion = celtic_pagan }
				OR = {
					is_pagan_religion = no
					is_reformed_pagan_religion = yes
					is_tribal = no
					has_global_flag = ancient_religions
				}
				is_feudal = yes
				NOT = { has_character_flag = voting_on_succession_law }
			}			
			NOT = { tier = baron }
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			current_inheritance_laws_effect = yes
			log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) holding vote to change [emf_succ_title.GetID] to Feudal Elective"
			
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			custom_tooltip = { text = CUSTOMTOOLTIP7 }
			hidden_tooltip = {
				set_title_flag = emf_succ_title
				holder_scope = {
					set_character_flag = voting_on_succession_law
					set_character_flag = vote_is_for_feudal_elective
					set_variable = { which = "succession_law_vote" value = 1 }
					character_event = { id = Plus.600 }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_feudal_elective
			}
			modifier = {
				factor = 0
				NOR = {
					holder_scope = { religion_group = hellenic_pagan_group }
					# has a lot of vassals, is way over the vassal limit, and not a strong ruler
					holder_scope = {
						num_of_vassals = 15
						over_vassal_limit = 5
						OR = {
							trait = trusting
							trait = humble
							trait = charitable
							is_dumb_trigger = yes
							trait = infirm
						}
						NOR = {
							trait = ambitious
							trait = proud
							trait = lunatic
							diplomacy = 12
							martial = 12
							stewardship = 12
							intrigue = 12
						}
					}
					# is culture appropriate with at least three dukes and over the vassal limit
					holder_scope = {
						OR = {
							culture_group = north_germanic
							culture_group = central_germanic
							culture_group = west_germanic
							culture = frankish
							culture = norman
							culture = roman
							culture = nubian
							culture = somali
						}
						any_vassal = {
							higher_real_tier_than = COUNT
							count = 3
						}
						over_vassal_limit = 3
					}
					# has no close relative heirs and has at least three vassals he likes
					holder_scope = {
						ROOT = { current_heir = { NOT = { is_close_relative = PREVPREV } } }
						age = 40
						OR = {
							trait = charitable
							trait = just
							trait = gregarious
							is_dumb_trigger = yes
						}
						NOR = {
							trait = ambitious
							trait = proud
						}
						any_vassal = {
							is_feudal = yes
							is_adult = yes
							primary_title = { de_jure_liege_or_above = ROOT }
							NOT = { any_heir_title = { title = ROOT } }
							reverse_opinion = { who = PREV value = 25 }
							count = 3
						}
					}
				}
			}
		}
	}

	ai_institute_cognatic_succession = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = {
							dejure_liege_title = {
								OR = {
									has_law = cognatic_succession
									has_law = true_cognatic_succession
									has_law = enatic_cognatic_succession
									has_law = enatic_succession
								}
							}
						}
					}
				}
			}
			OR = {
				has_law = agnatic_succession
				has_law = true_cognatic_succession
			}
			holder_scope = {
				OR = {
					independent = yes
					NOT = { any_liege = { holy_order = yes } }
					is_merchant_republic = yes
				}
				NOT = { religion_group = muslim }
				NOT = { religion = aztec }
				is_merchant_republic = no
				is_patrician = no
				NOT = { has_character_flag = voting_on_succession_law }
			}
			OR = {
				AND = {
					tier = baron
					is_theocracy = no
					is_republic = no
				}
				holder_scope = {
					is_theocracy = no
					is_republic = no
				}
			}
			is_primary_type_title = no
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			set_allows_matrilineal_marriage = yes
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			pf_save_old_gender_law_effect = yes
			custom_tooltip = { text = CUSTOMTOOLTIP12 }

			hidden_tooltip = {
				if = {
					limit = { has_law = true_cognatic_succession }
					holder_scope = { set_character_flag = coming_from_true_cognatic }
				}
				if = {
					limit = { has_law = agnatic_succession }
					holder_scope = { set_character_flag = coming_from_agnatic }
				}
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing gender law for [emf_succ_title.GetID] to Agnatic-Cognatic"			
				
				# Mark current heir and pretenders so that they may be tracked after law change
				emf_laws_change_succ_save_contenders_effect = yes
				
				# Actually change the law now
				add_law = cognatic_succession				
				
				current_heir = { save_event_target_as = emf_heir }
				
				holder_scope = {
					# Changing from Agnatic to Cognatic
					if = {
						limit = { has_character_flag = coming_from_agnatic }
						# Male vassals are upset, female vassals are slightly upset
						any_vassal = {
							limit = {
								emf_laws_change_succ_affected_vassal_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_mild_reaction_effect = yes
							}
						}
						# Male children/dynasty members are upset, female children/dynasty members are happy
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
						any_dynasty_member = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
					}
					# Changing from True Cognatic to Cognatic
					if = {
						limit = { has_character_flag = coming_from_true_cognatic }
						# Female vassals are upset, male vassals are slightly upset
						any_vassal = {
							limit = {
								emf_laws_change_succ_affected_vassal_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_mild_reaction_effect = yes
							}
						}
						# Female children/dynasty members are upset, male children/dynasty members are happy
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
						any_dynasty_member = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
					}
					clr_character_flag = coming_from_agnatic
					clr_character_flag = coming_from_true_cognatic
				}
				
				# Specfic, event-driven succession law change reactions for the new heir, the former heir,
				# the former pretenders, and the new pretenders.
				set_title_flag = emf_succ_title
				holder_scope = { character_event = { id = Plus.604 } }
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_cognatic_succession
			}
			modifier = {
				factor = 0
				NOR = {
					# primary title is cognatic
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = cognatic_succession } }
					}
					# a female ruler under agnatic
					AND = {
						is_primary_holder_title = yes
						has_law = agnatic_succession
						holder_scope = {
							is_female = yes
							NOR = {
								trait = humble
								trait = content
								trait = zealous
							}
						}
					}
					# a very religious older male ruler under true cognatic with a son
					AND = {
						is_primary_holder_title = yes
						has_law = true_cognatic_succession
						holder_scope = {
							is_female = no
							age = 50
							trait = zealous
							NOR = {
								is_feminist_trigger = yes
								culture = basque
							}
							is_heretic = no
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								dynasty = PREV
								is_female = no
								age = 6
							}
						}
					}
					# a male ruler under true cognatic with an adult son and a female heir
					AND = {
						is_primary_holder_title = yes
						has_law = true_cognatic_succession
						OR = {
							has_law = succ_gavelkind
							has_law = succ_primogeniture
						}
						current_heir = { is_female = yes }
						holder_scope = {
							is_female = no
							NOR = {
								is_feminist_trigger = yes
								trait = just
								trait = content
							}
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								dynasty = PREV
								is_female = no
								is_adult = yes
							}
						}
					}
					# a ruler under agnatic with an adult daughter who could inherit and no child/grandchild who is heir
					AND = {
						is_primary_holder_title = yes
						has_law = agnatic_succession
						OR = {
							has_law = succ_gavelkind
							has_law = succ_primogeniture
						}
						current_heir = {
							NOR = {
								is_child_of = FROM
								father_even_if_dead = { is_child_of = FROM }
								mother_even_if_dead = { is_child_of = FROM }
							}
						}
						holder_scope = {
							any_child = {
								is_female = yes
								is_adult = yes
								dynasty = PREV
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
							}
						}
					}
					# a ruler under agnatic with an adult diplomatic daughter whom he loves
					AND = {
						is_primary_holder_title = yes
						has_law = agnatic_succession
						holder_scope = {
							any_child = {
								is_female = yes
								is_adult = yes
								dynasty = PREV
								reverse_opinion = { who = PREV value = 50 }
								diplomacy = 12
							}
							OR = {
								trait = charitable
								trait = kind
								trait = just
							}
							NOR = {
								trait = humble
								trait = craven
								trait = slothful
							}
						}
					}
					# ruler under agnatic who is feminist or basque
					AND = {
						is_primary_holder_title = yes
						has_law = agnatic_succession
						holder_scope = {
							OR = {
								is_feminist_trigger = yes
								culture = basque
							}
						}
					}
				}
			}
		}
	}
	
	ai_institute_agnatic_succession = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
			NOT = { has_global_flag = gender_equality }
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = {
							dejure_liege_title = {
								has_law = agnatic_succession
							}
						}
					}
				}
			}
			has_law = cognatic_succession
			holder_scope = { NOT = { has_character_flag = voting_on_succession_law } }
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			set_allows_matrilineal_marriage = no
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			pf_save_old_gender_law_effect = yes
			custom_tooltip = { text = CUSTOMTOOLTIP11 }
			
			hidden_tooltip = {
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing gender law for [emf_succ_title.GetID] to Agnatic"
				
				# Mark current heir and pretenders so that they may be tracked after law change
				emf_laws_change_succ_save_contenders_effect = yes
				
				# Actually change the law now
				add_law = agnatic_succession				
				
				current_heir = { save_event_target_as = emf_heir }
				
				holder_scope = {
					# Female vassals are upset, male vassals are slightly upset
					any_vassal = {
						limit = {
							emf_laws_change_succ_affected_vassal_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						if = {
							limit = { is_female = yes }
							emf_laws_change_succ_bad_reaction_effect = yes
						}
						if = {
							limit = { is_female = no }
							emf_laws_change_succ_mild_reaction_effect = yes
						}
					}
					# Female children/dynasty members are upset, male children/dynasty members are happy
					any_child = {
						limit = {
							can_inherit_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						if = {
							limit = { is_female = yes }
							emf_laws_change_succ_bad_reaction_effect = yes
						}
						if = {
							limit = { is_female = no }
							emf_laws_change_succ_good_reaction_effect = yes
						}
					}
					any_dynasty_member = {
						limit = {
							can_inherit_trigger = yes
							emf_laws_change_succ_vip_trigger = no
						}
						if = {
							limit = { is_female = yes }
							emf_laws_change_succ_bad_reaction_effect = yes
						}
						if = {
							limit = { is_female = no }
							emf_laws_change_succ_good_reaction_effect = yes
						}
					}
				}
			
				# Specfic, event-driven succession law change reactions for the new heir, the former heir,
				# the former pretenders, and the new pretenders.
				set_title_flag = emf_succ_title
				holder_scope = { character_event = { id = Plus.604 } }
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_agnatic_succession
			}
			modifier = {
				factor = 0
				NOR = {
					# is zealous and title was formerly agnatic and current heir is not female
					AND = {
						is_primary_holder_title = yes
						has_title_flag = former_agnatic_succession
						current_heir = { is_female = no }
						holder_scope = { trait = zealous }
					}
					# holder is muslim
					AND = {
						is_primary_holder_title = yes
						holder_scope = { religion_group = muslim }
					}
					# primary title is agnatic
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = agnatic_succession } }
					}
					# a very religious older male ruler under cognatic with a son
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							is_female = no
							age = 50
							trait = zealous
							NOR = {
								is_feminist_trigger = yes
								culture = basque
							}
							is_heretic = no
							any_child = {
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								dynasty = PREV
								is_female = no
								age = 6
							}
						}
					}
					# female heir who will cause title to fall out of dynasty and there
					# is a close family member in the realm who can inherit
					AND = {
						current_heir = {
							is_female = yes
							OR = {
								AND = {
									is_married = yes
									is_married_matrilineally = no
								}
								any_child = {
									can_inherit_trigger = yes
									NOT = { dynasty = FROM }
									OR = {
										AND = {
											ROOT = {
												NOR = {
													has_law = cognatic_succession
													has_law = agnatic_succession
												}
											}
											is_female = yes
										}
										AND = {
											ROOT = {
												NOR = {
													has_law = enatic_cognatic_succession
													has_law = enatic_succession
												}
											}
											is_female = no
										}
									}
								}
							}
						}
						holder_scope = {
							any_dynasty_member = {
								can_inherit_trigger = yes
								is_close_relative = PREV
								is_female = no
								same_realm = PREV
							}
							NOR = {
								is_feminist_trigger = yes
								trait = content
								trait = humble
								trait = slothful
							}
						}
					}
				}
			}
		}
	}
	
	ai_institute_true_cognatic_succession = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = {
							dejure_liege_title = {
								OR = {
									has_law = true_cognatic_succession
									has_law = enatic_cognatic_succession
									has_law = enatic_succession
								}
							}
						}
					}
				}
			}
			OR = {
				has_law = cognatic_succession
				has_law = enatic_cognatic_succession
			}
			holder_scope = {
				NOT = { religion_group = muslim }
				NOT = { religion = aztec }
				is_merchant_republic = no
				is_patrician = no
				NOT = { has_character_flag = voting_on_succession_law }
			}
			OR = {
				AND = {
					tier = baron
					is_theocracy = no
					is_republic = no
				}
				holder_scope = {
					is_theocracy = no
					is_republic = no
				}
			}
			is_primary_type_title = no
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			set_allows_matrilineal_marriage = yes
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			pf_save_old_gender_law_effect = yes
			custom_tooltip = { text = CUSTOMTOOLTIP13 }

			hidden_tooltip = {
				if = {
					limit = { has_law = cognatic_succession }
					holder_scope = { set_character_flag = coming_from_cognatic }
				}
				if = {
					limit = { has_law = enatic_cognatic_succession }
					holder_scope = { set_character_flag = coming_from_enatic_cognatic }
				}
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing gender law for [emf_succ_title.GetID] to True Cognatic"				
				
				# Mark current heir and pretenders so that they may be tracked after law change
				emf_laws_change_succ_save_contenders_effect = yes
				
				# Actually change the law now
				add_law = true_cognatic_succession				
				
				current_heir = { save_event_target_as = emf_heir }
				
				holder_scope = {
					# Changing from Agnatic-Cognatic to True Cognatic
					if = {
						limit = { has_character_flag = coming_from_cognatic }
						# Male vassals are upset, female vassals are slightly upset
						any_vassal = {
							limit = {
								emf_laws_change_succ_affected_vassal_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_mild_reaction_effect = yes
							}
						}
						# Male children/dynasty members are upset, female children/dynasty members are happy
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
						any_dynasty_member = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
					}
					# Changing from Enatic-Cognatic to True Cognatic
					if = {
						limit = { has_character_flag = coming_from_enatic_cognatic }
						# Female vassals are upset, male vassals are slightly upset
						any_vassal = {
							limit = {
								emf_laws_change_succ_affected_vassal_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_mild_reaction_effect = yes
							}
						}
						# Female children/dynasty members are upset, male children/dynasty members are happy
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
						any_dynasty_member = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
					}
					clr_character_flag = coming_from_cognatic
					clr_character_flag = coming_from_enatic_cognatic
				}
				
				# Specfic, event-driven succession law change reactions for the new heir, the former heir,
				# the former pretenders, and the new pretenders.
				set_title_flag = emf_succ_title
				holder_scope = { character_event = { id = Plus.604 } }
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 20
				has_title_flag = former_true_cognatic_succession
			}
			#slow down vassals from going full
			modifier = {
				factor = 0.1
				independent = no
				owner = {
					liege = {
						NOR = {
							has_law = true_cognatic_succession
							has_law = enatic_cognatic_succession
							has_law = enatic_succession
						}
					}
				}
			}
			modifier = {
				factor = 0
				NOR = {
					# is zealous and enatic-cognatic title was formerly true cognatic
					AND = {
						is_primary_holder_title = yes
						has_law = enatic_cognatic_succession
						holder_scope = { trait = zealous }
						has_title_flag = former_true_cognatic_succession
					}
					# primary title is true cognatic
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = true_cognatic_succession } }
					}
					# a female ruler under cognatic who is just
					AND = {
						is_primary_holder_title = yes
						has_law = cognatic_succession
						holder_scope = {
							is_female = yes
							OR = {
								trait = just
								trait = proud
								trait = charitable
								diplomacy = 15
							}
							NOR = {
								trait = humble
								trait = content
								trait = arbitrary
							}
						}
					}
					# has an awesome older daughter who could be heir instead
					AND = {
						is_primary_holder_title = yes
						has_law = cognatic_succession
						OR = {
							has_law = succ_gavelkind
							has_law = succ_primogeniture
						}
						holder_scope = {
							any_child = {
								is_female = yes
								dynasty = PREV
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								calc_true_if = {
									amount = 3
									diplomacy = 21
									martial = 21
									diplomacy = 15
									martial = 15
									diplomacy = 8
									martial = 8
									stewardship = 8
									trait = genius
								}
								any_sibling = {
									is_female = no
									NOT = { is_older_than = PREV }
									any_heir_title = { title = ROOT }
									OR = {
										is_dumb_trigger = yes
										trait = inbred
										trait = incapable
										trait = lunatic
										NOR = { 
											diplomacy = 12
											martial = 12
											is_smart_trigger = yes
										}
									}
								}
							}
						}
					}
					# a male ruler under enatic-cognatic with a son
					AND = {
						is_primary_holder_title = yes
						has_law = enatic_cognatic_succession
						holder_scope = {
							is_female = no
							is_feminist_trigger = no
							any_child = {
								is_female = no
								age = 6
								dynasty = PREV
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
							}
						}
					}
					# a basque ruler under cognatic with an adult daughter
					AND = {
						is_primary_holder_title = yes
						has_law = cognatic_succession
						holder_scope = {
							culture = basque
							OR = {
								is_female = yes
								any_child = {
									can_inherit_trigger = yes
									NOR = {
										is_dumb_trigger = yes
										trait = inbred
										trait = incapable
										trait = lunatic
									}
									dynasty = PREV
									is_female = yes
									is_adult = yes
								}
							}
						}
					}
					# ruler under cognatic who is feminist
					AND = {
						is_primary_holder_title = yes
						has_law = cognatic_succession
						holder_scope = { is_feminist_trigger = yes }
					}				
					# ruler is learned and believes in equality
					AND = {
						is_primary_holder_title = yes
						holder_scope = {
							trait = just
							OR = {
								trait = scholar
								learning = 12
							}
							NOR = {
								trait = zealous
								trait = content
								trait = humble
								trait = slothful
							}
						}
					}
				}
			}
		}
	}

	ai_institute_enatic_cognatic_succession = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		only_rulers = yes
		
		from_potential = {
			ai = yes
			is_ruler = yes
			NOT = { has_global_flag = gender_equality }
		}
		potential = {
			OR = {
				is_primary_holder_title = yes
				higher_tier_than = DUKE
			}
			OR = {
				is_primary_holder_title = no
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						independent = yes
						primary_title = {
							dejure_liege_title = {
								OR = {
									has_law = enatic_cognatic_succession
									has_law = enatic_succession
								}
							}
						}
					}
				}
			}
			OR = {
				has_law = true_cognatic_succession
				has_law = enatic_succession
			}
			holder_scope = {
				NOT = { religion_group = muslim }
				NOT = { religion = aztec }
				is_merchant_republic = no
				is_patrician = no
				NOT = { has_character_flag = voting_on_succession_law }
			}
			OR = {
				AND = {
					tier = baron
					OR = {
						is_feudal = yes
						is_tribal = yes
					}
				}
				holder_scope = { is_feudal = yes }
				holder_scope = { is_tribal = yes }
			}
			is_primary_type_title = no
		}
		allow = {
			can_change_succession_law = yes
			emf_laws_change_succ_prestige_cost_trigger = yes
		}
		effect = {
			holder_scope = { set_character_flag = changed_succession_@ROOT }
			set_allows_matrilineal_marriage = yes
			emf_laws_change_succ_prestige_cost_effect = yes
			holder_scope = { pf_tradition_minus2_effect = yes }
			pf_save_old_gender_law_effect = yes
			custom_tooltip = { text = CUSTOMTOOLTIP14 }

			hidden_tooltip = {
				if = {
					limit = { has_law = true_cognatic_succession }
					holder_scope = { set_character_flag = coming_from_true_cognatic }
				}
				if = {
					limit = { has_law = enatic_succession }
					holder_scope = { set_character_flag = coming_from_enatic }
				}
				save_event_target_as = emf_succ_title
				holder_scope = { save_event_target_as = emf_succ_changer }
				
				current_inheritance_laws_effect = yes
				log = "---> [emf_succ_changer.GetTitledFirstName] (ID: [emf_succ_changer.GetID]) changing gender law for [emf_succ_title.GetID] to Enatic-Cognatic"			
				
				# Mark current heir and pretenders so that they may be tracked after law change
				emf_laws_change_succ_save_contenders_effect = yes
				
				# Actually change the law now
				add_law = true_cognatic_succession				
				
				current_heir = { save_event_target_as = emf_heir }
				
				holder_scope = {
					# Changing from True Cognatic to Enatic-Cognatic
					if = {
						limit = { has_character_flag = coming_from_true_cognatic }
						# Male vassals are upset, female vassals are slightly upset
						any_vassal = {
							limit = {
								emf_laws_change_succ_affected_vassal_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_mild_reaction_effect = yes
							}
						}
						# Male children/dynasty members are upset, female children/dynasty members are happy
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
						any_dynasty_member = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
					}
					# Changing from Enatic to Enatic-Cognatic
					if = {
						limit = { has_character_flag = coming_from_enatic }
						# Female vassals are upset, male vassals are slightly upset
						any_vassal = {
							limit = {
								emf_laws_change_succ_affected_vassal_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_mild_reaction_effect = yes
							}
						}
						# Female children/dynasty members are upset, male children/dynasty members are happy
						any_child = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
						any_dynasty_member = {
							limit = {
								can_inherit_trigger = yes
								emf_laws_change_succ_vip_trigger = no
							}
							if = {
								limit = { is_female = yes }
								emf_laws_change_succ_bad_reaction_effect = yes
							}
							if = {
								limit = { is_female = no }
								emf_laws_change_succ_good_reaction_effect = yes
							}
						}
					}
					clr_character_flag = coming_from_true_cognatic
					clr_character_flag = coming_from_enatic
				}
				
				# Specfic, event-driven succession law change reactions for the new heir, the former heir,
				# the former pretenders, and the new pretenders.
				set_title_flag = emf_succ_title
				holder_scope = { character_event = { id = Plus.604 } }
				
				emf_laws_change_succ_cleanup_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			#slow down vassals from going full
			modifier = {
				factor = 0
				independent = no
				owner = {
					liege = {
						NOR = {
							has_law = enatic_cognatic_succession
							has_law = enatic_succession
						}
					}
				}
			}
			modifier = {
				factor = 0
				NOR = {
					# primary title is enatic cognatic
					AND = {
						is_primary_holder_title = no
						holder_scope = { primary_title = { has_law = enatic_cognatic_succession } }
					}
					# a female ruler with an awesome daughter who could be heir instead
					AND = {
						is_primary_holder_title = yes
						has_law = true_cognatic_succession
						OR = {
							has_law = succ_gavelkind
							has_law = succ_primogeniture
						}
						holder_scope = {
							is_female = yes
							any_child = {
								is_female = yes
								dynasty = PREV
								can_inherit_trigger = yes
								NOR = {
									is_dumb_trigger = yes
									trait = inbred
									trait = incapable
									trait = lunatic
								}
								calc_true_if = {
									amount = 3
									diplomacy = 21
									martial = 21
									diplomacy = 15
									martial = 15
									diplomacy = 8
									martial = 8
									stewardship = 8
									trait = genius
								}
								any_sibling = {
									is_female = no
									is_older_than = PREV
									any_heir_title = { title = ROOT }
									OR = {
										is_dumb_trigger = yes
										trait = inbred
										trait = incapable
										trait = lunatic
										NOR = { 
											diplomacy = 12
											martial = 12
											is_smart_trigger = yes
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

