# The entry should be named in the same way that the elective succession law is named to define its voter's logic
# ROOT			Voter, the character voting in the election
# FROM			Candidate, character considered for the title
# FROMFROM		The title the nomination is for
# FROMFROMFROM	The title owner's candidate
# The voter will vote for the top score candidate calculated from this script

feudal_elective = {
	elector_vote_strength = {
		days = 1
	}

	elector_selection = {
		days = 1

		trigger = {
			# The title must currently exist
			FROMFROM = {
				has_holder = yes
			}

			OR = {
				# Current ruler is always an elector
				has_landed_title = FROMFROM

				AND = {
					# Either direct vassal, de jure if the title is not titular, or independent ruler who is de jure vassal
					trigger_if = {
						limit = { FROMFROM = { is_titular = no } }

						FROMFROM = { de_jure_vassal_or_below = ROOT }

						OR = {
							vassal_of = FROMFROM
							independent = yes
						}
					}
					trigger_else = {
						vassal_of = FROMFROM
					}

					# If the Elector Title is a Duchy or County, any vassal is an elector, otherwise only Dukes and above
					trigger_if = {
						limit = { FROMFROM = { real_tier = COUNT } }
						real_tier = BARON
					}
					trigger_else_if = {
						limit = { FROMFROM = { real_tier = DUKE } }
						higher_real_tier_than = BARON
					}
					trigger_else = {
						higher_real_tier_than = COUNT
					}
				}
			}
		}
	}

	# ROOT is current title holder
	# FROM is candidate
	# FROMFROM is title
	# FROMFROMFROM is null
	candidate_trigger = {
		# The title must currently exist
		FROMFROM = {
			has_holder = yes
		}

		FROM = {
			# Protected realm inheritance checks
			trigger_if = {
				limit = {
					is_ruler = yes
					NOT = { is_vassal_or_below_of = ROOT }

					ROOT_FROMFROM = {
						OR = {
							has_law = out_of_realm_inheritance_law_1
							has_law = crown_authority_3
							has_law = crown_authority_4
						}
					}
				}

				ROOT = { independent = yes }
			}

			OR = {
				# Title claimant
				has_claim = ROOT_FROMFROM

				# Family relations
				trigger_if = {
					limit = { is_close_relative = ROOT }

					OR = {
						dynasty = ROOT

						AND = {
							is_landed = yes
							higher_real_tier_than = BARON
						}
					}
				}

				trigger_if = {
					limit = {
						# Either defacto direct vassal, or independent ruler who is dejure vassal
						OR = {
							vassal_of = ROOT

							AND = {
								independent = yes
								ROOT_FROMFROM = { de_jure_vassal_or_below = PREV }
							}
						}
					}

					# Tier requirements
					trigger_if = {
						limit = { ROOT_FROMFROM = { real_tier = COUNT } }
						real_tier = BARON
					}
					trigger_else_if = {
						limit = { ROOT_FROMFROM = { real_tier = DUKE } }
						higher_real_tier_than = BARON
					}
					trigger_else = {
						higher_real_tier_than = COUNT
					}
				}
			}

			# Candidate should not be a landless Courtier who is about to inherit a Theocracy or Republic
			trigger_if = {
				limit = {
					any_heir_title = {
						OR = {
							is_republic = yes
							is_theocracy = yes
						}
					}
				}

				is_landed = yes
				is_republic = no
				is_theocracy = no
			}

			# If the current ruler controls the religion, candidates must be of the same religion
			trigger_if = {
				limit = { ROOT = { controls_religion = yes } }
				religion = ROOT
			}

			trigger_if = {
				limit = { trait = bastard }
				is_ruler = yes
			}

			is_ascetic_trigger = no
			is_theocracy = no
			is_republic = no

			NOR = {
				# Uncommenting this condition provokes that a ruler's son is not a valid candidate if the parent is married to the ruler and a candidate
				# mother = {
				#	is_alive = yes
				#	is_married = ROOT
				#	parent_for_elective_succession_trigger = yes
				# }
				# father = {
				#	is_alive = yes
				#	is_married = ROOT
				#	parent_for_elective_succession_trigger = yes
				# }

				trait = cat
				trait = disinherited
				trait = horse
				trait = eunuch
				trait = crusader_king
				trait = crusader_queen
				character = ROOT # Not the current ruler
			}
		}
	}

	candidate_vote_score = {
		days = 0

		### Elector voting himself ###
		elector_self_feudal_vote = yes

		### Candidate ###

		# Heir
		elector_heir_vote = yes

		## All candidates

		# Sexism
		elector_candidate_gender_vote = yes

		# Ageism
		elector_candidate_age_vote = yes
		elector_candidate_age_children_vote = yes

		# Culturism
		elector_candidate_culture_vote = yes

		# Faithism
		elector_candidate_religion_vote = yes

		# Barbarism
		elector_candidate_barbarism_vote = yes

		# Nepotism
		elector_candidate_nepotism_vote = yes

		# Prestige
		elector_candidate_prestige_vote = yes

		# Piety
		elector_candidate_piety_vote = yes

		# Education and lifestyle
		elector_candidate_education_lifestyle_vote = yes

		# Claims
		elector_candidate_claims_vote = yes

		# Traits
		elector_candidate_traits_vote = yes
		elector_candidate_traits_disfigured_vote = yes
		elector_candidate_traits_excommunication_vote = yes
		elector_candidate_traits_feudal_vote = yes
		elector_candidate_traits_pagan_branch_vote = yes

		# Ruler traits
		elector_ruler_traits_vote = yes

		# Candidate opinion
		elector_candidate_opinion_vote = yes

		# Ruler opinion
		elector_ruler_opinion_vote = yes
	}
}