﻿succession_gender_laws = {
	default = male_preference_law
	flag = succession_gender_laws

	#Regula gender succession law for the player. Male only inheritance, consorts allowed.
	regula_magister_gender_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		can_keep = {
			# is_independent_ruler = yes
			has_trait = magister_trait_group
			is_regula_trigger = yes
		}

		can_pass = { #UPDATE - figure out what this does.
			can_change_succession_law_trigger = yes
		}
		should_start_with = {

		}
		succession = {
			gender_law = male_only
		}
		flag = men_can_have_multiple_spouses
		flag = men_can_have_consorts
		# flag = elective_succession_law
		flag = no_male_commanders

		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}
	}
	#Regula gender succession law for the vassals.  Female inheritance only.
	regula_vassal_succession_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		### Update - Are the can_keep and can_have sections really necessary here?
		can_keep = {
			OR = {
				is_regula_trigger = yes
				root.top_liege = {
					has_realm_law = regula_magister_gender_law
				}
				liege = {
					OR = {
						has_realm_law = regula_magister_gender_law
						has_realm_law = regula_vassal_succession_law
					}
				}
			}
		}

		can_have = {
		}
		can_pass = {
			can_change_succession_law_trigger = yes
			is_independent_ruler = no
		}
		should_start_with = {
			# root.top_liege = {
			# 	has_realm_law = regula_magister_gender_law
			# }
		}
		succession = {
			gender_law = female_only
		}
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}
	}
	# Regula covert gender succession law for vassals. Female preference.
	regula_covert_vassal_succession_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		### Update - Are the can_keep and can_have sections really necessary here?
		can_keep = {
		}

		can_have = {
		}
		can_pass = {
			always = no
		}
		should_start_with = {
			# root.top_liege = {
			# 	has_realm_law = regula_magister_gender_law
			# }
		}
		succession = {
			gender_law = female_preference
		}
		# Pass and revoke costs aren't achievable.
		pass_cost = {
			prestige = 10000
		}
		revoke_cost = {
			prestige = 10000
		}
	}

	male_only_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		should_show_for_title = { # Never show this in the UI, just apply it through script
			always = no
		}
		can_keep = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = male_only_law }
			}
			trigger_else = {
				custom_tooltip = {
					text = culture_not_female_only_tt
					OR = {
						faith = { has_doctrine = doctrine_gender_male_dominated }
						NOT = {
							culture = { has_cultural_parameter = female_only_inheritance }
						}
					}
				}
			}
		}
		can_pass = {
			can_change_succession_law_trigger = yes
		}
		should_start_with = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = male_only_law }
			}
			trigger_else = {
				faith = {
					has_doctrine = doctrine_gender_male_dominated
					religion_tag = islam_religion
				}
			}
		}
		succession = {
			gender_law = male_only
		}
		flag = men_can_have_multiple_spouses
		flag = men_can_have_consorts
		flag = no_female_commanders

		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}
	}

	male_preference_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		should_show_for_title = { # Never show this in the UI, just apply it through script
			always = no
		}
		can_keep = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = male_preference_law }
			}
			trigger_else = {
				custom_description = {
					text = change_law_male_preference_faith_requirement
					faith = {
						NOT = { has_doctrine = doctrine_gender_female_dominated }
					}
				}
				custom_tooltip = {
					text = culture_not_female_only_tt
					OR = {
						faith = { has_doctrine = doctrine_gender_male_dominated }
						NOT = {
							culture = { has_cultural_parameter = female_only_inheritance }
						}
					}
				}
			}
		}
		can_pass = {
			can_change_succession_law_trigger = yes
		}
		should_start_with = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = male_preference_law }
			}
			trigger_else = {
				NOT = {
					culture = { has_cultural_parameter = female_preference_inheritance }
				}
				OR = {
					faith = {
						NOT = {
							religion_tag = islam_religion
						}
						has_doctrine = doctrine_gender_male_dominated
					}
					has_realm_law = male_only_law # For when male_only_law ceases to be a legal option
				}
			}
		}
		succession = {
			gender_law = male_preference
		}
		flag = men_can_have_multiple_spouses
		flag = men_can_have_consorts
		flag = no_female_commanders

		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}
	}

	equal_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		should_show_for_title = { # Never show this in the UI, just apply it through script
			always = no
		}
		can_keep = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = equal_law }
			}
			trigger_else = {
				custom_tooltip = {
					text = equal_law_culture_faith_or_innovation_trigger
					OR = {
						culture = { has_cultural_parameter = gender_equal_inheritance }
						#culture = { has_innovation = innovation_visigothic_codes }
						faith = { has_doctrine = doctrine_gender_equal }
						AND = { #Culture is female dominated but religion is male dominated
							faith = { has_doctrine = doctrine_gender_male_dominated }
							culture = { has_cultural_parameter = female_only_inheritance }
						}
						AND = { #Culture is male dominated but faith is female dominated
							faith = { has_doctrine = doctrine_gender_female_dominated }
							NOT = { culture = { has_cultural_parameter = female_only_inheritance } }
						}
						primary_title.var:can_keep_realm_law ?= flag:equal_law
					}
				}
			}
		}
		can_pass = {
			#can_change_to_equal_succession_law_trigger = yes
			can_change_succession_law_trigger = yes
		}
		should_start_with = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = equal_law }
			}
			trigger_else = {
				NOR = {
					culture = { has_cultural_parameter = female_preference_inheritance }
					culture = { has_cultural_tradition = tradition_nubian_warrior_queens }
				}
				faith = {
					has_doctrine = doctrine_gender_equal
				}
				NOT = { has_realm_law = female_only_law } # For when female_only_law ceases to be valid
			}
		}
		succession = {
			gender_law = equal
		}
		flag = men_can_have_multiple_spouses
		flag = men_can_have_consorts
		flag = women_can_have_multiple_spouses
		flag = women_can_have_consorts

		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		ai_will_do = {
			if = {
				limit = {
					primary_title.tier > tier_county
					faith = {
						NOT = {
							has_dominant_ruling_gender = prev
						}
					}
				}
				value = 2
			}
		}
	}

	female_preference_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		should_show_for_title = { # Never show this in the UI, just apply it through script
			always = no
		}
		can_keep = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = female_preference_law }
			}
			trigger_else = {
				custom_tooltip = {
					text = female_preference_law_faith_or_culture_trigger
					OR = {
						faith = {
							NOT = { has_doctrine = doctrine_gender_male_dominated }
						}
						culture = { has_cultural_parameter = female_preference_inheritance }
					}
				}
			}
		}
		should_start_with = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = female_preference_law }
			}
			trigger_else = {
				OR = {
					culture = { has_cultural_parameter = female_preference_inheritance }
					faith = {
						has_doctrine = doctrine_gender_female_dominated
					}
					has_realm_law = female_only_law # For when male_only_law ceases to be a legal option
				}
			}
		}
		can_pass = {
			can_change_succession_law_trigger = yes
		}
		succession = {
			gender_law = female_preference
		}
		flag = women_can_have_multiple_spouses
		flag = women_can_have_consorts

		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}
	}

	female_only_law = {
		can_title_have = {
			can_title_have_law_general_trigger = yes
		}
		should_show_for_title = { # Never show this in the UI, just apply it through script
			always = no
		}
		can_keep = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = female_only_law }
			}
			trigger_else = {
				custom_tooltip = {
					text = female_only_law_faith_or_culture_trigger
					OR = {
						faith = { has_doctrine = doctrine_gender_female_dominated }
						culture = { has_cultural_parameter = female_only_inheritance }
					}
				}
			}
		}
		can_pass = {
			can_change_succession_law_trigger = yes
		}
		succession = {
			gender_law = female_only
		}
		flag = women_can_have_multiple_spouses
		flag = women_can_have_consorts

		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		should_start_with = {
			trigger_if = {
				limit = {
					is_independent_ruler = no
					government_allows = administrative
				}
				top_liege = { has_realm_law = female_only_law }
			}
			trigger_else = {
				culture = { has_cultural_parameter = female_only_inheritance }
			}
		}
	}
}

# As Magister, we can choose a Paelex succession law
# This will force 
regula_magister_paelex_succession_order_laws = {
	flag = succession_order_laws
	default = magister_paelex_regula_succession_law

	# Default, makes Paelex drop their succession laws
	magister_paelex_default_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_pass = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		potential = {
			government_allows = administrative
		}

		# No modifier as default

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# Effect
		on_pass = {
			every_vassal_or_below = {
				trigger_event = {
					delayed = yes
					id = regula_paelex_event.0003
				}
			}
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}

	# The Default Regula law, adds age/traits as big factors
	# Designed to give you good Paelex targets
	magister_paelex_regula_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_pass = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		potential = {
			government_allows = administrative
		}

		# Modifier
		modifier = {
			legitimacy_gain_mult = 0.1
			monthly_piety_gain_per_legitimacy_level_add = 0.2
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# Effect
		on_pass = {
			every_vassal_or_below = {
				trigger_event = {
					delayed = yes
					id = regula_paelex_event.0003
				}
			}
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}

	# This succession law favours "young blood"
	# Younger characters are heavily favoured
	magister_paelex_young_blood_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_pass = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		potential = {
			government_allows = administrative
		}

		# Modifier
		modifier = {
			fertility = 0.1
			positive_random_genetic_chance = 0.1
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# Effect
		on_pass = {
			every_vassal_or_below = {
				trigger_event = {
					delayed = yes
					id = regula_paelex_event.0003
				}
			}
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}

	# This succession law favours characters with good genetic/personality/misc traits
	# Similar to above, but weights congenital traits over age
	magister_paelex_good_breeding_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_pass = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		potential = {
			government_allows = administrative
		}

		# Modifier
		modifier = {
			inbreeding_chance = -0.05
			genetic_trait_strengthen_chance = 0.1
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# Effect
		on_pass = {
			every_vassal_or_below = {
				trigger_event = {
					delayed = yes
					id = regula_paelex_event.0003
				}
			}
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}
	
	# This succession law favours stats, then traits, then age
	magister_paelex_talented_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		can_pass = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = yes
			is_magister = yes
		}

		potential = {
			government_allows = administrative
		}

		# Modifier
		modifier = {
			monthly_prestige_gain_per_happy_powerful_vassal_mult = 0.03
			monthly_piety_gain_per_happy_powerful_vassal_mult = 0.03
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# Effect
		on_pass = {
			every_vassal_or_below = {
				trigger_event = {
					delayed = yes
					id = regula_paelex_event.0003
				}
			}
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}
}

# This is the implementation of above
# Each Paelex will automatically embrace these laws based on the above
regula_paelex_succession_order_laws = {
	flag = succession_order_laws
	default = paelex_default_succession_law

	# Default, does nothing
	paelex_default_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_pass = {
			can_change_succession_law_trigger = yes
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		potential = {
			government_allows = administrative
			is_regula_trigger = yes
		}

		# No succession as default

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}

	# Regula, Basic Paelex Succesion law
	paelex_regula_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_pass = {
			can_change_succession_law_trigger = yes
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		potential = {
			government_allows = administrative
			is_regula_trigger = yes
		}

		# Succession
		succession = {
			order_of_succession = appointment
			appointment_type = regula_admin_governor_regula
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}

	# This succession law favours "young blood"
	# Younger characters are heavily favoured
	paelex_young_blood_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_pass = {
			can_change_succession_law_trigger = yes
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		potential = {
			government_allows = administrative
			is_regula_trigger = yes
		}

		# Succession
		succession = {
			order_of_succession = appointment
			appointment_type = regula_admin_governor_young_blood
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}

	# This succession law favours characters with good genetic traits
	# Similar to above, but weights congenital traits over age
	paelex_good_breeding_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_pass = {
			can_change_succession_law_trigger = yes
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		potential = {
			government_allows = administrative
			is_regula_trigger = yes
		}

		# Succession
		succession = {
			order_of_succession = appointment
			appointment_type = regula_admin_governor_good_breeding
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}
	
	# This succession law favours stats, then traits, then age
	paelex_talented_succession_law = {
		# Flags
		flag = advanced_succession_law

		# Triggers
		can_keep = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_have = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		can_pass = {
			can_change_succession_law_trigger = yes
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		should_start_with = {
			government_allows = administrative
			is_independent_ruler = no
			is_regula_trigger = yes
		}

		potential = {
			government_allows = administrative
			is_regula_trigger = yes
		}

		# Succession
		succession = {
			order_of_succession = appointment
			appointment_type = regula_admin_governor_talented
		}

		# Costs
		pass_cost = {
			prestige = change_succession_law_prestige_cost
		}
		revoke_cost = {
			prestige = change_succession_law_prestige_cost
		}

		# AI
		ai_will_do = {
			value = 1
		}
	}
}