highest_held_title_tier_minus_two_landed = { # for theomonarchy_elective
	value = highest_held_title_tier
	subtract = 2
	min = tier_barony
}

roman_fervour_and_size = {
	value = 0

	every_independent_ruler = {
		limit = {
			faith = faith:roman_catholic
			NOT = {
				has_title = title:k_papal_state
			}
		}
		if = {
			limit = {
				highest_held_title_tier = tier_hegemony
				NOT = {
					has_title = title:k_papal_state
				}
			}
			add = roman_limit_bonus_15
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_empire
				NOT = {
					has_title = title:k_papal_state
				}
			}
			add = roman_limit_bonus_5
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_kingdom
				NOT = {
					has_title = title:k_papal_state
				}
			}
			add = roman_limit_bonus_3
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_duchy
				NOT = {
					has_title = title:k_papal_state
				}
			}
			add = roman_limit_bonus_2
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_county
				NOT = {
					has_title = title:k_papal_state
				}
			}
			add = roman_limit_bonus_1
		}
		else = {
		}
	}
	if = {
		limit = {
			faith:roman_catholic = { fervor >= 90 }
		}
		add = roman_limit_bonus_5
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 80 }
		}
		add = roman_limit_bonus_4
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 70 }
		}
		add = roman_limit_bonus_3
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 60 }
		}
		subtract = roman_limit_bonus_2
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 50 }
		}
		subtract = roman_limit_penalty_4
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 40 }
		}
		subtract = roman_limit_penalty_6
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 30 }
		}
		subtract = roman_limit_penalty_8
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 20 }
		}
		subtract = roman_limit_penalty_10
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 10 }
		}
		subtract = roman_limit_penalty_14
	}
	else_if = {
		limit = {
			faith:roman_catholic = { fervor >= 5 }
		}
		subtract = roman_limit_penalty_16
	}
	else = {
		subtract = roman_limit_penalty_20
	}
	min = 0 # Can't go lower than 0
	max = 100 # Can't go higher than 100
}
roman_papal_size_piety_learning_stewardship = {
	value = 0

	every_independent_ruler = {
		limit = {
			faith = faith:roman_catholic
			has_title = title:k_papal_state
		}
		
		if = {
			limit = {
				realm_size >= 60
			}
			add = roman_limit_bonus_65
		}
		else_if = {
			limit = {
				realm_size >= 55
			}
			add = roman_limit_bonus_60
		}
		else_if = {
			limit = {
				realm_size >= 50
			}
			add = roman_limit_bonus_55
		}
		else_if = {
			limit = {
				realm_size >= 45
			}
			add = roman_limit_bonus_50
		}
		else_if = {
			limit = {
				realm_size >= 40
			}
			add = roman_limit_bonus_45
		}
		else_if = {
			limit = {
				realm_size >= 35
			}
			add = roman_limit_bonus_40
		}
		else_if = {
			limit = {
				realm_size >= 30
			}
			add = roman_limit_bonus_35
		}
		else_if = {
			limit = {
				realm_size >= 25
			}
			add = roman_limit_bonus_30
		}
		else_if = {
			limit = {
				realm_size >= 20
			}
			add = roman_limit_bonus_25
		}
		else_if = {
			limit = {
				realm_size >= 15
			}
			add = roman_limit_bonus_20
		}
		else_if = {
			limit = {
				realm_size >= 10
			}
			add = roman_limit_bonus_15
		}
		else_if = {
			limit = {
				realm_size >= 5
			}
			add = roman_limit_bonus_10
		}
		else_if = {
			limit = {
				realm_size >= 3
			}
			add = roman_limit_bonus_5
		}
		else_if = {
			limit = {
				realm_size >= 1
			}
			add = roman_limit_bonus_1
		}
		else = {
		}
	}
	every_independent_ruler = {
		limit = {
			faith = faith:roman_catholic
			has_title = title:k_papal_state
		}
		
		if = {
			limit = {
				piety_level = 8
			}
			subtract = roman_limit_bonus_25
		}
		else_if = {
			limit = {
				piety_level = 7
			}
			subtract = roman_limit_bonus_20
		}
		else_if = {
			limit = {
				piety_level = 6
			}
			subtract = roman_limit_bonus_15
		}
		else_if = {
			limit = {
				piety_level = 5
			}
			subtract = roman_limit_bonus_10
		}
		else_if = {
			limit = {
				piety_level = 4
			}
			subtract = roman_limit_bonus_5
		}
		else_if = {
			limit = {
				piety_level = 3
			}
			add = roman_limit_penalty_10
		}
		else_if = {
			limit = {
				piety_level = 2
			}
			add = roman_limit_penalty_15
		}
		else_if = {
			limit = {
				piety_level = 1
			}
			add = roman_limit_penalty_20
		}
		else_if = {
			limit = {
				piety_level = 0
			}
			add = roman_limit_penalty_25
		}
		else = {
		}
	}
	
	every_independent_ruler = {
		limit = {
			faith = faith:roman_catholic
			has_title = title:k_papal_state
		}
		
		if = {
			limit = {
				prestige_level = 8
			}
			subtract = roman_limit_bonus_25
		}
		else_if = {
			limit = {
				prestige_level = 7
			}
			subtract = roman_limit_bonus_20
		}
		else_if = {
			limit = {
				prestige_level = 6
			}
			subtract = roman_limit_bonus_15
		}
		else_if = {
			limit = {
				prestige_level = 5
			}
			subtract = roman_limit_bonus_10
		}
		else_if = {
			limit = {
				prestige_level = 4
			}
			subtract = roman_limit_bonus_5
		}
		else_if = {
			limit = {
				prestige_level = 3
			}
			add = roman_limit_penalty_10
		}
		else_if = {
			limit = {
				prestige_level = 2
			}
			add = roman_limit_penalty_15
		}
		else_if = {
			limit = {
				prestige_level = 1
			}
			add = roman_limit_penalty_20
		}
		else_if = {
			limit = {
				prestige_level = 0
			}
			add = roman_limit_penalty_25
		}
		else = {
		}
	}
	every_independent_ruler = {
		limit = {
			faith = faith:roman_catholic
			has_title = title:k_papal_state
		}
		
		if = {
			limit = {
				stewardship >= 20
			}
			subtract = roman_limit_bonus_20
		}
		else_if = {
			limit = {
				stewardship >= 15
			}
			subtract = roman_limit_bonus_10
		}
		else_if = {
			limit = {
				stewardship >= 10
			}
			add = roman_limit_bonus_5
		}
		else_if = {
			limit = {
				stewardship >= 8
			}
			add = roman_limit_bonus_10
		}
		else_if = {
			limit = {
				stewardship >= 5
			}
			add = roman_limit_penalty_15
		}
		else_if = {
			limit = {
				stewardship < 5
			}
			add = roman_limit_penalty_25
		}
		else = {
		}
	}
	
	every_independent_ruler = {
		limit = {
			faith = faith:roman_catholic
			has_title = title:k_papal_state
		}
		if = {
			limit = {
				num_sinful_traits >= 7
			}
			add = roman_limit_penalty_30
		}
		else_if = {
			limit = {
				num_sinful_traits >= 6
			}
			add = roman_limit_penalty_25
		}
		else_if = {
			limit = {
				num_sinful_traits >= 5
			}
			add = roman_limit_penalty_20
		}
		else_if = {
			limit = {
				num_sinful_traits >= 4
			}
			add = roman_limit_penalty_15
		}
		else_if = {
			limit = {
				num_sinful_traits >= 3
			}
			add = roman_limit_penalty_10
		}
		else_if = {
			limit = {
				num_sinful_traits >= 2
			}
			add = roman_limit_penalty_5
		}
		else = {
		}
	}
	every_independent_ruler = {
		limit = {
			faith = faith:roman_catholic
			has_title = title:k_papal_state
		}
		if = {
			limit = {
				num_virtuous_traits >= 7
			}
			subtract = roman_limit_penalty_15
		}
		else_if = {
			limit = {
				num_virtuous_traits >= 6
			}
			subtract = roman_limit_penalty_10
		}
		else_if = {
			limit = {
				num_virtuous_traits >= 5
			}
			subtract = roman_limit_penalty_5
		}
		else = {
		}
	}
	
	add = roman_limit_bonus_40
	
	min = 0 # Can't go lower than 0
	max = 100 # Can't go higher than 100
}

roman_limit_bonus_1 = 1
roman_limit_bonus_2 = 2
roman_limit_bonus_3 = 3
roman_limit_bonus_4 = 4
roman_limit_bonus_5 = 5
roman_limit_bonus_6 = 6
roman_limit_bonus_7 = 7
roman_limit_bonus_8 = 8
roman_limit_bonus_9 = 9
roman_limit_bonus_10 = 10
roman_limit_bonus_11 = 11
roman_limit_bonus_12 = 12
roman_limit_bonus_13 = 13
roman_limit_bonus_14 = 14
roman_limit_bonus_15 = 15
roman_limit_bonus_16 = 16
roman_limit_bonus_17 = 17
roman_limit_bonus_18 = 18
roman_limit_bonus_19 = 19
roman_limit_bonus_20 = 20
roman_limit_bonus_25 = 25
roman_limit_bonus_30 = 30
roman_limit_bonus_35 = 35
roman_limit_bonus_40 = 40
roman_limit_bonus_45 = 45
roman_limit_bonus_50 = 50
roman_limit_bonus_55 = 55
roman_limit_bonus_60 = 60

roman_limit_penalty_1 = 1
roman_limit_penalty_2 = 2
roman_limit_penalty_3 = 3
roman_limit_penalty_4 = 4
roman_limit_penalty_5 = 5
roman_limit_penalty_6 = 6
roman_limit_penalty_7 = 7
roman_limit_penalty_8 = 8
roman_limit_penalty_9 = 9
roman_limit_penalty_10 = 10
roman_limit_penalty_11 = 11
roman_limit_penalty_12 = 12
roman_limit_penalty_13 = 13
roman_limit_penalty_14 = 14
roman_limit_penalty_15 = 15
roman_limit_penalty_16 = 16
roman_limit_penalty_17 = 17
roman_limit_penalty_18 = 18
roman_limit_penalty_19 = 19
roman_limit_penalty_20 = 20

roman_calculations = {
	value = 0
	add = roman_papal_size_piety_learning_stewardship
	subtract = roman_fervour_and_size
	min = 0 # Can't go lower than 0
	max = 100 # Can't go higher than 100
}

sevillian_fervour_and_size = {
	value = 0

	every_independent_ruler = {
		limit = {
			faith = faith:sevillian
			NOT = {
				has_title = title:k_sevillian_papacy
			}
		}
		if = {
			limit = {
				highest_held_title_tier = tier_hegemony
				NOT = {
					has_title = title:k_sevillian_papacy
				}
			}
			add = sevillian_limit_bonus_15
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_empire
				NOT = {
					has_title = title:k_sevillian_papacy
				}
			}
			add = sevillian_limit_bonus_5
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_kingdom
				NOT = {
					has_title = title:k_sevillian_papacy
				}
			}
			add = sevillian_limit_bonus_3
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_duchy
				NOT = {
					has_title = title:k_sevillian_papacy
				}
			}
			add = sevillian_limit_bonus_2
		}
		else_if = {
			limit = {
				highest_held_title_tier = tier_county
				NOT = {
					has_title = title:k_sevillian_papacy
				}
			}
			add = sevillian_limit_bonus_1
		}
		else = {
		}
	}
	if = {
		limit = {
			faith:sevillian = { fervor >= 90 }
		}
		add = sevillian_limit_bonus_5
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 80 }
		}
		add = sevillian_limit_bonus_4
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 70 }
		}
		add = sevillian_limit_bonus_3
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 60 }
		}
		subtract = sevillian_limit_bonus_2
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 50 }
		}
		subtract = sevillian_limit_penalty_4
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 40 }
		}
		subtract = sevillian_limit_penalty_6
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 30 }
		}
		subtract = sevillian_limit_penalty_8
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 20 }
		}
		subtract = sevillian_limit_penalty_10
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 10 }
		}
		subtract = sevillian_limit_penalty_14
	}
	else_if = {
		limit = {
			faith:sevillian = { fervor >= 5 }
		}
		subtract = sevillian_limit_penalty_16
	}
	else = {
		subtract = sevillian_limit_penalty_20
	}
	min = 0 # Can't go lower than 0
	max = 100 # Can't go higher than 100
}
sevillian_papal_size_piety_learning_stewardship = {
	value = 0

	every_independent_ruler = {
		limit = {
			faith = faith:sevillian
			has_title = title:k_sevillian_papacy
		}
		
		if = {
			limit = {
				realm_size >= 60
			}
			add = sevillian_limit_bonus_65
		}
		else_if = {
			limit = {
				realm_size >= 55
			}
			add = sevillian_limit_bonus_60
		}
		else_if = {
			limit = {
				realm_size >= 50
			}
			add = sevillian_limit_bonus_55
		}
		else_if = {
			limit = {
				realm_size >= 45
			}
			add = sevillian_limit_bonus_50
		}
		else_if = {
			limit = {
				realm_size >= 40
			}
			add = sevillian_limit_bonus_45
		}
		else_if = {
			limit = {
				realm_size >= 35
			}
			add = sevillian_limit_bonus_40
		}
		else_if = {
			limit = {
				realm_size >= 30
			}
			add = sevillian_limit_bonus_35
		}
		else_if = {
			limit = {
				realm_size >= 25
			}
			add = sevillian_limit_bonus_30
		}
		else_if = {
			limit = {
				realm_size >= 20
			}
			add = sevillian_limit_bonus_25
		}
		else_if = {
			limit = {
				realm_size >= 15
			}
			add = sevillian_limit_bonus_20
		}
		else_if = {
			limit = {
				realm_size >= 10
			}
			add = sevillian_limit_bonus_15
		}
		else_if = {
			limit = {
				realm_size >= 5
			}
			add = sevillian_limit_bonus_10
		}
		else_if = {
			limit = {
				realm_size >= 3
			}
			add = sevillian_limit_bonus_5
		}
		else_if = {
			limit = {
				realm_size >= 1
			}
			add = sevillian_limit_bonus_1
		}
		else = {
		}
	}
	every_independent_ruler = {
		limit = {
			faith = faith:sevillian
			has_title = title:k_sevillian_papacy
		}
		
		if = {
			limit = {
				piety_level = 8
			}
			subtract = sevillian_limit_bonus_25
		}
		else_if = {
			limit = {
				piety_level = 7
			}
			subtract = sevillian_limit_bonus_20
		}
		else_if = {
			limit = {
				piety_level = 6
			}
			subtract = sevillian_limit_bonus_15
		}
		else_if = {
			limit = {
				piety_level = 5
			}
			subtract = sevillian_limit_bonus_10
		}
		else_if = {
			limit = {
				piety_level = 4
			}
			subtract = sevillian_limit_bonus_5
		}
		else_if = {
			limit = {
				piety_level = 3
			}
			add = sevillian_limit_penalty_10
		}
		else_if = {
			limit = {
				piety_level = 2
			}
			add = sevillian_limit_penalty_15
		}
		else_if = {
			limit = {
				piety_level = 1
			}
			add = sevillian_limit_penalty_20
		}
		else_if = {
			limit = {
				piety_level = 0
			}
			add = sevillian_limit_penalty_25
		}
		else = {
		}
	}
	
	every_independent_ruler = {
		limit = {
			faith = faith:sevillian
			has_title = title:k_sevillian_papacy
		}
		
		if = {
			limit = {
				prestige_level = 8
			}
			subtract = sevillian_limit_bonus_25
		}
		else_if = {
			limit = {
				prestige_level = 7
			}
			subtract = sevillian_limit_bonus_20
		}
		else_if = {
			limit = {
				prestige_level = 6
			}
			subtract = sevillian_limit_bonus_15
		}
		else_if = {
			limit = {
				prestige_level = 5
			}
			subtract = sevillian_limit_bonus_10
		}
		else_if = {
			limit = {
				prestige_level = 4
			}
			subtract = sevillian_limit_bonus_5
		}
		else_if = {
			limit = {
				prestige_level = 3
			}
			add = sevillian_limit_penalty_10
		}
		else_if = {
			limit = {
				prestige_level = 2
			}
			add = sevillian_limit_penalty_15
		}
		else_if = {
			limit = {
				prestige_level = 1
			}
			add = sevillian_limit_penalty_20
		}
		else_if = {
			limit = {
				prestige_level = 0
			}
			add = sevillian_limit_penalty_25
		}
		else = {
		}
	}
	every_independent_ruler = {
		limit = {
			faith = faith:sevillian
			has_title = title:k_sevillian_papacy
		}
		
		if = {
			limit = {
				stewardship >= 20
			}
			subtract = sevillian_limit_bonus_20
		}
		else_if = {
			limit = {
				stewardship >= 15
			}
			subtract = sevillian_limit_bonus_10
		}
		else_if = {
			limit = {
				stewardship >= 10
			}
			add = sevillian_limit_bonus_5
		}
		else_if = {
			limit = {
				stewardship >= 8
			}
			add = sevillian_limit_bonus_10
		}
		else_if = {
			limit = {
				stewardship >= 5
			}
			add = sevillian_limit_penalty_15
		}
		else_if = {
			limit = {
				stewardship < 5
			}
			add = sevillian_limit_penalty_25
		}
		else = {
		}
	}
	
	every_independent_ruler = {
		limit = {
			faith = faith:sevillian
			has_title = title:k_sevillian_papacy
		}
		if = {
			limit = {
				num_sinful_traits >= 7
			}
			add = sevillian_limit_penalty_30
		}
		else_if = {
			limit = {
				num_sinful_traits >= 6
			}
			add = sevillian_limit_penalty_25
		}
		else_if = {
			limit = {
				num_sinful_traits >= 5
			}
			add = sevillian_limit_penalty_20
		}
		else_if = {
			limit = {
				num_sinful_traits >= 4
			}
			add = sevillian_limit_penalty_15
		}
		else_if = {
			limit = {
				num_sinful_traits >= 3
			}
			add = sevillian_limit_penalty_10
		}
		else_if = {
			limit = {
				num_sinful_traits >= 2
			}
			add = sevillian_limit_penalty_5
		}
		else = {
		}
	}
	every_independent_ruler = {
		limit = {
			faith = faith:sevillian
			has_title = title:k_sevillian_papacy
		}
		if = {
			limit = {
				num_virtuous_traits >= 7
			}
			subtract = sevillian_limit_penalty_15
		}
		else_if = {
			limit = {
				num_virtuous_traits >= 6
			}
			subtract = sevillian_limit_penalty_10
		}
		else_if = {
			limit = {
				num_virtuous_traits >= 5
			}
			subtract = sevillian_limit_penalty_5
		}
		else = {
		}
	}
	add = sevillian_limit_bonus_40
	min = 0 # Can't go lower than 0
	max = 100 # Can't go higher than 100
}

sevillian_limit_bonus_1 = 1
sevillian_limit_bonus_2 = 2
sevillian_limit_bonus_3 = 3
sevillian_limit_bonus_4 = 4
sevillian_limit_bonus_5 = 5
sevillian_limit_bonus_6 = 6
sevillian_limit_bonus_7 = 7
sevillian_limit_bonus_8 = 8
sevillian_limit_bonus_9 = 9
sevillian_limit_bonus_10 = 10
sevillian_limit_bonus_11 = 11
sevillian_limit_bonus_12 = 12
sevillian_limit_bonus_13 = 13
sevillian_limit_bonus_14 = 14
sevillian_limit_bonus_15 = 15
sevillian_limit_bonus_16 = 16
sevillian_limit_bonus_17 = 17
sevillian_limit_bonus_18 = 18
sevillian_limit_bonus_19 = 19
sevillian_limit_bonus_20 = 20
sevillian_limit_bonus_25 = 25
sevillian_limit_bonus_30 = 30
sevillian_limit_bonus_35 = 35
sevillian_limit_bonus_40 = 40
sevillian_limit_bonus_45 = 45
sevillian_limit_bonus_50 = 50
sevillian_limit_bonus_55 = 55
sevillian_limit_bonus_60 = 60

sevillian_limit_penalty_1 = 1
sevillian_limit_penalty_2 = 2
sevillian_limit_penalty_3 = 3
sevillian_limit_penalty_4 = 4
sevillian_limit_penalty_5 = 5
sevillian_limit_penalty_6 = 6
sevillian_limit_penalty_7 = 7
sevillian_limit_penalty_8 = 8
sevillian_limit_penalty_9 = 9
sevillian_limit_penalty_10 = 10
sevillian_limit_penalty_11 = 11
sevillian_limit_penalty_12 = 12
sevillian_limit_penalty_13 = 13
sevillian_limit_penalty_14 = 14
sevillian_limit_penalty_15 = 15
sevillian_limit_penalty_16 = 16
sevillian_limit_penalty_17 = 17
sevillian_limit_penalty_18 = 18
sevillian_limit_penalty_19 = 19
sevillian_limit_penalty_20 = 20

sevillian_calculations = {
	value = 0
	add = sevillian_papal_size_piety_learning_stewardship
	subtract = sevillian_fervour_and_size
	min = 0 # Can't go lower than 0
	max = 100 # Can't go higher than 100
}