﻿# The entry should be named in the same way that the elective succession law is named to define it's voter's logic.
# Root scope - Elector, the character voting in the election.
# scope:candidate scope - Candidate, Character considered for the title.
# scope:title scope - Title, The title the nomination is for.
# scope:holder scope - Ruler, The ruler currently holding the title.
# scope:holder_candidate scope - Favored Candidate, The title owner's candidate.
# The voter will vote for the top score candidate calculated from this script.

brus_lestvitsa_elective = { 
	elector_vote_strength = {
		base = 0

		# Voting strength based on development for powerful vassals
		modifier = { 
			desc = tooltip_scandinavian_elective_development
			is_landed = yes
			domain_size >= 1
			any_held_title = {
				tier = tier_county
				development_level > 0
				#target_is_de_jure_liege_or_above = scope:title
			}
			add = {
				every_held_title = {
					limit = {
						tier = tier_county
						development_level > 0
						#target_is_de_jure_liege_or_above = scope:title
					}
					add = {
						value = this.development_level
						# Cut in half for wrong faith (and another half if wrong culture )
						if = {
							limit = {
								NOT = { this.faith = root.faith }
							}
							divide = 2
						}
						if = {
							limit = {
								NOT = { this.culture = { has_same_culture_heritage = root.culture } }
							}
							divide = 2
						}
						if = {
							limit = {
								NOT = {
									this.holder = {
										is_vassal_or_below_of = scope:holder
									}
								}
							}
							divide = 2
						}
						min = 1
					}
				}
			}
		}

		modifier = { #Further boost/reduction is given depending on capital county's opinion.
			desc = tooltip_scandinavian_elective_capital_opinion
			is_landed = yes
			exists = capital_county
			OR = {
				capital_county.county_opinion <= -5
				capital_county.county_opinion >= 5
			}
			add = {
				value = capital_county.county_opinion
				multiply = 0.2
			}
		}

		# More power if you share dynasty of holder
		modifier = {
			desc = brus_elective_dynasty
			dynasty = scope:holder.dynasty
			is_independent_ruler = no
			add = 3
		}
		# More power for powerful vassals
		modifier = {
			desc = brus_elective_powerful_vassal
			is_powerful_vassal = yes
			add = 2
		}
		
		min = 1
	}
	
	electors = {
		add = holder
		add = {
			type = title_dejure_vassals
			limit = {
				target_is_liege_or_above = scope:holder #All de jure vassals, no matter the tier, sub-vassals included.
				is_ruler = yes
				is_landed = yes
				exists = capital_county
				highest_held_title_tier > 1 #Barons excluded.
				domain_size >= 1
				any_held_title = { #Elector title must be a de jure vassal of the title being voted on; multi-Dukes (or similar) might have two duchies in multiple electoral domains, which they should be able to vote on. This is only valid for the highest-tier title (so a Duke-Elector who also owns a County which is an electoral title for another title does not get a vote from that county).
					tier = root.highest_held_title_tier
					exists = de_jure_liege
					target_is_de_jure_liege_or_above = scope:title
				}
			}
		}
		add = {
			type = holder_direct_vassals
			limit = {
				is_ruler = yes
				is_landed = yes
				domain_size >= 1
				OR = {
					is_councillor_of = scope:holder
					is_knight_of = scope:holder
				}
			}
		}

		# Other independent princes who have the same dynasty and at least same rank as you
		add = {
			type = holder_dynasty
			limit = {
				is_independent_ruler = yes
				tier_difference = {
					target = scope:holder
					value >= 0
				}
			}
		}
	}

	candidate_score = {
		base = 0
		# Prioritize brothers by seniority
		modifier = {
			desc = brus_elective_brothers
			scope:candidate = {
				is_sibling_of = scope:holder
				is_male = yes
			}
			add = {
				value = 3.5
				multiply = scope:candidate.age
			}
		}
		# A higher ranked candidate can never move down the ladder
		modifier = {
			desc = brus_elective_higher
			scope:candidate = {
				is_independent_ruler = yes
				tier_difference = {
					target = scope:holder
					value >= 1
				}
			}
			add = -200
		}
		# Discouraged from inheriting multiple titles in the ladder at once
		modifier = {
			desc = brus_elective_multiple
			scope:candidate = {
				any_heir_title = {
					has_title_law = brus_lestvitsa_succession_law
					NOT = { this = scope:title } 
					current_heir = scope:candidate
				}
			}
			add = {
				scope:candidate = {
					every_heir_title = {
						limit = { 
							has_title_law = brus_lestvitsa_succession_law 
							NOT = { this = scope:title } 
							current_heir = scope:candidate
						}
						subtract = {
							value = this.holder.realm_size
							multiply = 4
						}
					}
				}
			}
		}
		# Discourage moving down the ladder (inheriting smaller principalities)
		modifier = {
			desc = brus_elective_realm_size
			scope:candidate = {
				is_independent_ruler = yes
				realm_size > scope:holder.realm_size
			}
			add = {
				subtract = scope:candidate.sub_realm_size
				multiply = 3
			}
		}
		# Father having previously ruled there was a basis for eligibility after brothers went up the ladder
		modifier = {
			desc = brus_elective_father_ruled
			scope:title = {
				any_past_holder = {
					is_parent_of = scope:candidate
					is_alive = no
					is_male = yes
				}
			}
			add = 70
		}
		# Vassals of another ruler should be discouraged
		modifier = {
			desc = brus_elective_vassal
			scope:candidate = {
				is_independent_ruler = no
				is_landed = yes
				top_liege = { exists = yes }
				NOT = {
					is_vassal_or_below_of = scope:holder
				}
			}
			add = -100
		}
		# Duchy of Kiev should always go to holder of Kingdom of Kiev
		modifier = {
			desc = brus_elective_kiev
			scope:title = { this = title:d_kiev }
			scope:candidate = {
				any_heir_title = {
					this = title:k_ruthenia
					current_heir = scope:candidate
				}
			}
			add = 300
		}
		modifier = {
			desc = brus_elective_local_vassal
			scope:candidate = {
				is_vassal_or_below_of = scope:holder
			}
			add = 15
		}

		######################	Elector self-voting pattern	##########################
		elector_self_voting_pattern_feudal_elective_modifier = yes

		##########################	Elector voting patterns (circumstances)	##########################
		brus_elector_voting_pattern_circumstances_elective_modifier = yes

		##########################	Elector voting patterns (prestige/piety)	##########################
		brus_elector_voting_pattern_prestige_piety_elective_modifier = yes

		##########################	Elector voting patterns (traits)	##########################
		elector_voting_pattern_traits_feudal_elective_modifier = yes

		##########################	Elector voting patterns (opinion)	##########################
		brus_elector_voting_pattern_opinion_elective_modifier = yes
	}
	#scope:candidate = Candidate, scope:title = Title, scope:holder_candidate = Candidate currently selected by ruler.
	candidates = {
		add = {
			type = title_claimants
			limit = {
				OR = {
					scope:title = { has_title_law = female_preference_law }
					scope:title = { has_title_law = equal_law }
					is_male = yes
				}	
				feudal_elective_potential_landless_claimant_candidate_trigger = yes
			}
		}
		add = {
			type = holder_dynasty
			limit = {
				scope:title = {
					any_past_holder = {
						is_parent_of = root
						is_male = yes
					}
				}
				dynasty = scope:holder.dynasty
				feudal_elective_potential_landless_dynastic_candidate_trigger = yes
			}
		}
		add = { # Make sure heir to Kingdom of Kiev gets the Duchy too
			type = holder_dynasty
			limit = {
				any_heir_title = {
					this = title:k_ruthenia
					current_heir = root
				}
				scope:title = {
					this = title:d_kiev
				}
			}
		}
	}
}
