﻿@dr_military_power_multiplier = 100
@dr_military_power_swing_max = 70
@dr_county_val = 5
@dr_duchy_val = 10
@dr_kingdom_val = 15
@dr_empire_val = 20
@dr_prestige_level_multiplier = 3
@dr_piety_level_multiplier = 3
@dr_influence_level_multiplier = 5
@dr_influence_level_to_non_admin_multiplier = 3
@dr_vassal_opinions_divider = 2
@dr_vassal_opinions_swing_max = 50
@dr_vassal_opinions_swing_min = -50
@dr_crown_authority_0_val = 10
@dr_crown_authority_1_val = 5
@dr_crown_authority_2_val = -5
@dr_crown_authority_3_val = -10
@dr_very_short_reign = 15
@dr_short_reign = 10
@dr_long_reign = -10
@dr_very_long_reign = -15
@dr_diarch_imprisonment_incapable_val = -30
@dr_liege_imprisonment_incapable_val = 30
@dr_councillor_val = 15
@dr_liege_close_family = 15
@dr_positive_relation_with_liege = 10
@dr_life_long_positive_relation_with_liege = 20
@dr_negative_relation_with_liege = -10
@dr_life_long_negative_relation_with_liege = -20
@dr_positive_relation_with_religious_head = 10
@dr_life_long_positive_relation_with_religious_head = 20
@dr_negative_relation_with_religious_head = 10
@dr_life_long_negative_relation_with_religious_head = 20
@dr_line_of_succession_near_limit = 2
@dr_line_of_succession_far_limit = 4
@dr_line_of_succession_near_val = 20
@dr_line_of_succession_far_val = 10
@dr_popularity_among_peers_level_3_val = 45
@dr_popularity_among_peers_level_2_val = 30
@dr_popularity_among_peers_level_1_val = 15
@dr_swing_residual_monthly_decay = 2


dr_swing_value = {
	value = 0

	### Military Power Difference
	if = {
		limit = {
			#exists = this
			exists = liege
			is_alive = yes
			liege = {is_alive = yes}
		}
		save_temporary_scope_as = dr_char
		add = {
			desc = dr_military_difference
			add = max_military_strength
			every_hired_mercenary = {
				limit = {
					exists = this
					#is_ruler = yes
				}
				add = {
					value = mercenary_company_leader.max_military_strength
				}
			}
			every_ally = {
				limit = {NOT = {this = prev.liege}}
				add = max_military_strength
				every_hired_mercenary = {
					limit = {
						exists = this
						#is_ruler = yes
					}
					add = {
						value = mercenary_company_leader.max_military_strength
					}
				}
			}
		
			divide = {
				liege = {
					add = max_military_strength
					every_hired_mercenary = {
						limit = {
							exists = this
							#is_ruler = yes
						}
						add = {
							value = mercenary_company_leader.max_military_strength
						}
					}
					every_ally = {
						limit = {NOT = {this = scope:dr_char}}
						add = max_military_strength
						every_hired_mercenary = {
							limit = {
								exists = this
								#is_ruler = yes
							}
							add = {
								value = mercenary_company_leader.max_military_strength
							}
						}
					}	
				}
	
				min = 1
			}
		
			multiply = @dr_military_power_multiplier
	
			min = 0
			max = @dr_military_power_swing_max
		}
		### Military Power Difference/
	
		### Rank difference
		add = {
			desc = dr_rank_difference
			if = {
				limit = {highest_held_title_tier = tier_county}
				add = @dr_county_val
			}
			else_if = {
				limit = {highest_held_title_tier = tier_duchy}
				add = @dr_duchy_val
			}
			else_if = {
				limit = {highest_held_title_tier = tier_kingdom}
				add = @dr_kingdom_val
			}
			else_if = {
				limit = {highest_held_title_tier = tier_empire}
				add = @dr_empire_val
			}
	
			subtract = {
				liege = {
					if = {
						limit = {highest_held_title_tier = tier_county}
						add = @dr_county_val
					}
					else_if = {
						limit = {highest_held_title_tier = tier_duchy}
						add = @dr_duchy_val
					}
					else_if = {
						limit = {highest_held_title_tier = tier_kingdom}
						add = @dr_kingdom_val
					}
					else_if = {
						limit = {highest_held_title_tier = tier_empire}
						add = @dr_empire_val
					}
				}
			}
		}
		### Rank difference/

		### Influence level difference
		if = {
			limit = {
				liege = { government_has_flag = government_has_influence }
			}
			if = {
				limit = {
					government_allows = administrative
				}
				add = {
					desc = dr_influence_level
					add = influence_level
					subtract = liege.influence_level
					multiply = @dr_influence_level_multiplier
				}
			}

			else = {
				add = {
					desc = dr_influence_level
					subtract = liege.influence_level
					multiply = @dr_influence_level_to_non_admin_multiplier
				}
			}
		}
		### Influence level difference/		

		### Prestige level difference
		add = {
			desc = dr_prestige_level
			add = prestige_level
			subtract = liege.prestige_level
			multiply = @dr_prestige_level_multiplier
		}
		### Prestige level difference/

		### Piety level difference
		add = {
			desc = dr_piety_level
			add = piety_level
			subtract = liege.piety_level
			multiply = @dr_piety_level_multiplier
		}
		### Piety level difference/
	
		### Imprisonment
		add = {
			desc = dr_imprisonment
			if = {
				limit = {is_imprisoned = yes}
				add = @dr_diarch_imprisonment_incapable_val
			}
		}
	
		add = {
			desc = dr_liege_Imprisonment
			liege = {
				if = {
					limit = {is_imprisoned = yes}
					add = @dr_liege_imprisonment_incapable_val
				}
			}
		}
		### Imprisonment/
	
		### Popularity among Peers
		if = {
			limit = { has_global_variable = esr_is_loaded }
			add = {
				desc = trait_track_esr_popular_among_peers
				if = {
					limit = {
						has_trait = esr_social_standing
						has_trait_xp = {
							trait = esr_social_standing
							track = esr_popular_among_peers
							value >= 90
						}
					}
					add = @dr_popularity_among_peers_level_3_val
				}
				else_if = {
					limit = {
						has_trait = esr_social_standing
						has_trait_xp = {
							trait = esr_social_standing
							track = esr_popular_among_peers
							value >= 60
						}
					}
					add = @dr_popularity_among_peers_level_2_val
				}
				else_if = {
					limit = {
						has_trait = esr_social_standing
						has_trait_xp = {
							trait = esr_social_standing
							track = esr_popular_among_peers
							value >= 30
						}
					}
					add = @dr_popularity_among_peers_level_1_val
				}
			}
		}
		### Popularity among Peers/

		### Crown/Tribal Authority
		add = {
			desc = dr_crown_authority_level
			liege = {
				if = {
					limit = {has_realm_law_flag = uses_crown_authority}
					if = {
						limit = { has_realm_law = crown_authority_0 }
						add = @dr_crown_authority_0_val
					}
					else_if = {
						limit = { has_realm_law = crown_authority_1 }
						add = @dr_crown_authority_1_val
					}
					else_if = {
						limit = { has_realm_law = crown_authority_2 }
						add = @dr_crown_authority_2_val
					}
					else_if = {
						limit = { has_realm_law = crown_authority_3 }
						add = @dr_crown_authority_3_val
					}
				}
			}
		}

		add = {
			desc = dr_tribal_authority_level
			liege = {
				if = {
					limit = {has_realm_law_flag = uses_tribal_authority}
					if = {
						limit = { has_realm_law = tribal_authority_0 }
						add = @dr_crown_authority_0_val
					}
					else_if = {
						limit = { has_realm_law = tribal_authority_1 }
						add = @dr_crown_authority_1_val
					}
					else_if = {
						limit = { has_realm_law = tribal_authority_2 }
						add = @dr_crown_authority_2_val
					}
					else_if = {
						limit = { has_realm_law = tribal_authority_3 }
						add = @dr_crown_authority_3_val
					}
				}
			}
		}
		### Crown/Tribal Authority/

		### Heir to liege's title
		add = {
			desc = dr_line_of_succession
			every_claim = {
				limit = {
					holder ?= {this = scope:dr_char.liege}
					tier = scope:dr_char.liege.primary_title.tier
				}
				if = {
					limit = {
						place_in_line_of_succession = {
							target = scope:dr_char
							value <= @dr_line_of_succession_near_limit
						}
					}
					add = @dr_line_of_succession_near_val
				}
				else_if = {
					limit = {
						place_in_line_of_succession = {
							target = scope:dr_char
							value <= @dr_line_of_succession_far_limit
						}
					}
					add = @dr_line_of_succession_far_val
				}
			}
		}
		### Heir to liege's title/

		### Seat on liege's council
		if = {
			limit = { is_councillor_of = liege }
			add = {
				desc = dr_on_council
				value = @dr_councillor_val
			}
		}
		### Seat on liege's council/

		### Close Family
		add = {
			desc = dr_close_family
			if = {
				limit = {
					OR = {
						is_close_family_of = liege
						is_consort_of = liege
					}
				}
				add = @dr_liege_close_family
			}
		}
		### Close Family/

		### Relations
		add = {
			desc = dr_liege_close_relation
			if = {
				limit = {
					OR = {
						has_relation_friend = liege
						has_relation_lover = liege
					}
					NOR = {
						has_relation_best_friend = liege
						has_relation_soulmate = liege
					}
				}
				add = @dr_positive_relation_with_liege
			}
		}

		add = {
			desc = dr_liege_close_relation
			if = {
				limit = {
					OR = {
						has_relation_best_friend = liege
						has_relation_soulmate = liege
					}
				}
				add = @dr_life_long_positive_relation_with_liege
			}
		}

		add = {
			desc = dr_liege_enemy
			if = {
				limit = {
					has_relation_rival = liege
					NOT = {has_relation_nemesis = liege}
				}
				add = @dr_negative_relation_with_liege
			}
		}

		add = {
			desc = dr_liege_enemy
			if = {
				limit = {
					has_relation_nemesis = liege
				}
				add = @dr_life_long_negative_relation_with_liege
			}
		}

		if = {
			limit = {
				exists = liege.faith.religious_head
				liege.faith.religious_head = {is_alive = yes}
			}
			add = {
				desc = dr_religious_head_close_relation
				if = {
					limit = {
						OR = {
							has_relation_friend = liege.faith.religious_head
							has_relation_lover = liege.faith.religious_head
						}
						NOR = {
							has_relation_best_friend = liege.faith.religious_head
							has_relation_soulmate = liege.faith.religious_head
						}
					}
					add = @dr_positive_relation_with_religious_head
				}
			}
	
			add = {
				desc = dr_religious_head_close_relation
				if = {
					limit = {
						OR = {
							has_relation_best_friend = liege.faith.religious_head
							has_relation_soulmate = liege.faith.religious_head
						}
					}
					add = @dr_life_long_positive_relation_with_religious_head
				}
			}
	
			add = {
				desc = dr_religious_head_enemy
				if = {
					limit = {
						has_relation_rival = liege.faith.religious_head
						NOT = {has_relation_nemesis = liege.faith.religious_head}
					}
					add = @dr_negative_relation_with_religious_head
				}
			}
	
			add = {
				desc = dr_religious_head_enemy
				if = {
					limit = {
						has_relation_nemesis = liege.faith.religious_head
					}
					add = @dr_life_long_negative_relation_with_religious_head
				}
			}
		}
		### Relations/
		
		### liege Short/Long Reign
		if = {
			limit = {
				liege = { is_landed = yes }
			}
			add = {
				desc = dr_short_reign
				value = 0
				if =  {
					limit =  {
						liege = {
							primary_title = {
								title_held_years <= 1
							}
						}
					}
					add = @dr_very_short_reign
				}
				else_if =  {
					limit =  {
						liege = {
							primary_title = {
								title_held_years <= 5
							}
						}
					}
					add = @dr_short_reign
				}
			}
	
			add = {
				desc = dr_long_reign
				value = 0
				if =  {
					limit =  {
						liege = {
							primary_title = {
								title_held_years >= 25
							}
						}
					}
					add = @dr_very_long_reign
				}
				else_if =  {
					limit =  {
						liege = {
							primary_title = {
								title_held_years >= 15
							}
						}
					}
					add = @dr_long_reign
				}
			}
		}
		### liege Short/Long Reign/

		### Vassal Standing Difference
		add = {
			desc = dr_standing_difference
			liege = {
				every_vassal = {
					limit = {
						is_powerful_vassal = yes
						NOT = {this = scope:dr_char}
					}
					add = dr_calc_rough_char_opinion_value
					subtract = calc_rough_liege_opinion_value
				}
			}

			divide = {
				value = liege.vassal_count
				min = 1
			}
			divide = @dr_vassal_opinions_divider

			max = @dr_vassal_opinions_swing_max
			min = @dr_vassal_opinions_swing_min
		}
		### Vassal Standing Difference/
		
		### Incapable	
		add = {
			desc = dr_liege_incapable
			liege = {
				if = {
					limit = {
						NOT = {is_imprisoned = yes}
						is_incapable = yes
						has_character_modifier = isolating_modifier
					}
					add = @dr_liege_imprisonment_incapable_val
				}
			}
		}

		if = {
			limit = {
				is_incapable = yes
				has_character_modifier = isolating_modifier
			}
			multiply = {
				desc = dr_incapable
				value = 0
			}
		}
		### Incapable/			
	}
}

########################### For Swing Momentum UI ######################################
dr_military_power_change_diarch = { # scope should be diarch
	value = 0
	add = {
		desc = dr_military_difference
		add = max_military_strength
		every_hired_mercenary = {
			limit = {
				exists = this
				#is_ruler = yes
			}
			add = {
				value = mercenary_company_leader.max_military_strength
			}
		}
		every_ally = {
			limit = {NOT = {this = prev.liege}}
			add = max_military_strength
			every_hired_mercenary = {
				limit = {
					exists = this
					#is_ruler = yes
				}
				add = {
					value = mercenary_company_leader.max_military_strength
				}
			}
		}
	}
	
	if = {
		limit = {exists = var:dr_diarch_initial_military_power}
		subtract = var:dr_diarch_initial_military_power
	}
}

dr_military_power_change_liege = { # scope should be liege
	value = 0
	add = {
		desc = dr_military_difference
		add = max_military_strength
		every_hired_mercenary = {
			limit = {
				exists = this
				#is_ruler = yes
			}
			add = {
				value = mercenary_company_leader.max_military_strength
			}
		}
		every_ally = {
			limit = {NOT = {this = diarch}}
			add = max_military_strength
			every_hired_mercenary = {
				limit = {
					exists = this
					#is_ruler = yes
				}
				add = {
					value = mercenary_company_leader.max_military_strength
				}
			}
		}
	}
	if = {
		limit = {exists = diarch.var:dr_liege_initial_military_power}
		subtract = diarch.var:dr_liege_initial_military_power
	}
}


dr_military_power_diarch = { # scope should be diarch
	value = 0
	add = {
		desc = dr_military_difference
		add = max_military_strength
		every_hired_mercenary = {
			limit = {
				exists = this
				#is_ruler = yes
			}
			add = {
				value = mercenary_company_leader.max_military_strength
			}
		}
		every_ally = {
			limit = {NOT = {this = prev.liege}}
			add = max_military_strength
			every_hired_mercenary = {
				limit = {
					exists = this
					#is_ruler = yes
				}
				add = {
					value = mercenary_company_leader.max_military_strength
				}
			}
		}
	}
}

dr_military_power_liege = { # scope should be liege
	value = 0
	add = {
		desc = dr_military_difference
		add = max_military_strength
		every_hired_mercenary = {
			limit = {
				exists = this
				#is_ruler = yes
			}
			add = {
				value = mercenary_company_leader.max_military_strength
			}
		}
		every_ally = {
			limit = {NOT = {this = diarch}}
			add = max_military_strength
			every_hired_mercenary = {
				limit = {
					exists = this
					#is_ruler = yes
				}
				add = {
					value = mercenary_company_leader.max_military_strength
				}
			}
		}
	}
}

dr_military_power_swing_change = { # scope should be liege
	value = 0
	add = {
		desc = dr_military_difference
		value = diarch.dr_military_power_diarch
		divide = {
			value = dr_military_power_liege
			min = 1
		}
		multiply = @dr_military_power_multiplier
		max = @dr_military_power_swing_max
		min = 0
	}
	# Subtract Initial Value
	if = {
		limit = {exists = diarch.var:dr_diarch_initial_military_swing}
		subtract = {
			desc = dr_military_difference_initial
			value = diarch.var:dr_diarch_initial_military_swing
		}
	}
}

dr_military_power_swing_change_abs = {
	value = dr_military_power_swing_change
	abs = yes
}

dr_military_power_swing = { # scope should be liege
	value = diarch.dr_military_power_diarch
	divide = {
		value = dr_military_power_liege
		min = 1
	}
	multiply = @dr_military_power_multiplier
	
	min = 0
	max = @dr_military_power_swing_max
}

dr_liege_rank_change = { # scope should be liege
	value = 0
	add = {
		desc = dr_rank_difference
		if = {
			limit = {highest_held_title_tier = tier_county}
			add = 1
		}
		else_if = {
			limit = {highest_held_title_tier = tier_duchy}
			add = 2
		}
		else_if = {
			limit = {highest_held_title_tier = tier_kingdom}
			add = 3
		}
		else_if = {
			limit = {highest_held_title_tier = tier_empire}
			add = 4
		}

		if = {
			limit = {exists = diarch.var:dr_liege_initial_rank}
			subtract = {
				diarch = {
					if = {
						limit = {var:dr_liege_initial_rank = tier_county}
						add = 1
					}
					else_if = {
						limit = {var:dr_liege_initial_rank = tier_duchy}
						add = 2
					}
					else_if = {
						limit = {var:dr_liege_initial_rank = tier_kingdom}
						add = 3
					}
					else_if = {
						limit = {var:dr_liege_initial_rank = tier_empire}
						add = 4
					}
				}
			}
		}
	}
}

dr_diarch_rank_change = { # scope should be diarch
	value = 0
	add = {
		desc = dr_rank_difference
		if = {
			limit = {highest_held_title_tier = tier_county}
			add = 1
		}
		else_if = {
			limit = {highest_held_title_tier = tier_duchy}
			add = 2
		}
		else_if = {
			limit = {highest_held_title_tier = tier_kingdom}
			add = 3
		}
		else_if = {
			limit = {highest_held_title_tier = tier_empire}
			add = 4
		}

		if = {
			limit = {exists = var:dr_diarch_initial_rank}
			subtract = {
				if = {
					limit = {var:dr_diarch_initial_rank = tier_county}
					add = 1
				}
				else_if = {
					limit = {var:dr_diarch_initial_rank = tier_duchy}
					add = 2
				}
				else_if = {
					limit = {var:dr_diarch_initial_rank = tier_kingdom}
					add = 3
				}
				else_if = {
					limit = {var:dr_diarch_initial_rank = tier_empire}
					add = 4
				}
			}
		}
	}
}

dr_rank_swing_change = { # scope should be liege
	value = 0
	add = {
		desc = dr_rank_change_diarch
		value = diarch.dr_diarch_rank_change
		multiply = @dr_county_val
	}
	subtract = {
		desc = dr_rank_change_liege
		value = dr_liege_rank_change
		multiply = @dr_county_val
	}
}

dr_rank_swing_change_abs = {
	value = dr_rank_swing_change
	abs = yes
}

dr_liege_prestige_change = { # scope should be liege
	value = prestige_level
	if = {
		limit = {exists = diarch.var:dr_liege_initial_prestige}
		subtract = diarch.var:dr_liege_initial_prestige
	}
}

dr_diarch_prestige_change = { # scope should be Diarch
	value = prestige_level
	if = {
		limit = {exists = var:dr_diarch_initial_prestige}
		subtract = var:dr_diarch_initial_prestige
	}
}

dr_prestige_swing_change = { # scope should be liege
	value = 0
	add = {
		desc = dr_prestige_level_change_diarch
		value = diarch.dr_diarch_prestige_change
		multiply = @dr_prestige_level_multiplier
	}
	subtract = {
		desc = dr_prestige_level_change_liege
		value = dr_liege_prestige_change
		multiply = @dr_prestige_level_multiplier
	}
}

dr_prestige_swing_change_abs = {
	value = dr_prestige_swing_change
	abs = yes
}

dr_liege_piety_change = { # scope should be liege
	value = piety_level
	if = {
		limit = {exists = diarch.var:dr_liege_initial_piety}
		subtract = diarch.var:dr_liege_initial_piety
	}
}

dr_diarch_piety_change = { # scope should be Diarch
	value = piety_level
	if = {
		limit = {exists = var:dr_diarch_initial_piety}
		subtract = var:dr_diarch_initial_piety
	}
}

dr_piety_swing_change = { # scope should be liege
	value = 0
	add = {
		desc = dr_piety_level_change_diarch
		value = diarch.dr_diarch_piety_change
		multiply = @dr_piety_level_multiplier
	}
	subtract = {
		desc = dr_piety_level_change_liege
		value = dr_liege_piety_change
		multiply = @dr_piety_level_multiplier
	}
}

dr_piety_swing_change_abs = {
	value = dr_piety_swing_change
	abs = yes
}

dr_liege_influence_change = { # scope should be liege
	value = influence_level
	if = {
		limit = {exists = diarch.var:dr_liege_initial_influence}
		subtract = diarch.var:dr_liege_initial_influence
	}
}

dr_diarch_influence_change = { # scope should be Diarch
	value = influence_level
	if = {
		limit = {exists = var:dr_diarch_initial_influence}
		subtract = var:dr_diarch_initial_influence
	}
}

dr_influence_swing_change = { # scope should be liege
	value = 0
	if = {
		limit = {
			liege = { government_has_flag = government_has_influence }
		}
		if = {
			limit = {
				government_allows = administrative
			}
			add = {
				desc = dr_influence_level_change_diarch
				value = diarch.dr_diarch_influence_change
				multiply = @dr_influence_level_multiplier
			}
			subtract = {
				desc = dr_influence_level_change_liege
				value = dr_liege_influence_change
				multiply = @dr_influence_level_multiplier
			}
		}

		else = {
			subtract = {
				desc = dr_influence_level_change_liege
				value = dr_liege_influence_change
				multiply = @dr_influence_level_to_non_admin_multiplier
			}
		}
	}
}

dr_influence_swing_change_abs = {
	value = dr_influence_swing_change
	abs = yes
}

dr_vassal_standing = { # scope should be Diarch
	value = 0
	add = {
		desc = dr_standing_difference
		liege = {
			every_vassal = {
				limit = {
					is_powerful_vassal = yes
					NOT = {this = liege.diarch}
				}
				liege.diarch = { save_temporary_scope_as = dr_char }
				add = dr_calc_rough_char_opinion_value
				subtract = calc_rough_liege_opinion_value
			}
		}

		divide = {
			value = liege.vassal_count
			min = 1
		}
		divide = @dr_vassal_opinions_divider

		max = @dr_vassal_opinions_swing_max
		min = @dr_vassal_opinions_swing_min
	}
}

dr_vassal_standing_diarch = { # scope should be Diarch
	value = 0
	add = {
		liege = {
			every_vassal = {
				limit = {
					is_powerful_vassal = yes
					NOT = {this = liege.diarch}
				}
				liege.diarch = { save_temporary_scope_as = dr_char }
				add = dr_calc_rough_char_opinion_value
			}
		}

		divide = {
			value = liege.vassal_count
			min = 1
		}
	}
}

dr_vassal_standing_liege = { # scope should be Diarch
	value = 0
	add = {
		liege = {
			every_vassal = {
				limit = {
					is_powerful_vassal = yes
					NOT = {this = liege.diarch}
				}
				liege.diarch = { save_temporary_scope_as = dr_char }
				add = calc_rough_liege_opinion_value
			}
		}

		divide = {
			value = liege.vassal_count
			min = 1
		}
	}
}

dr_vassal_standing_change_diarch = { # scope should be Diarch
	value = 0
	add = {
		liege = {
			every_vassal = {
				limit = {
					is_powerful_vassal = yes
					NOT = {this = liege.diarch}
				}
				liege.diarch = { save_temporary_scope_as = dr_char }
				add = dr_calc_rough_char_opinion_value
			}
		}

		divide = {
			value = liege.vassal_count
			min = 1
		}

		if = {
			limit = {exists = var:dr_initial_vassal_standing_diarch}
			subtract = var:dr_initial_vassal_standing_diarch
		}
	}
}

dr_vassal_standing_change_liege = { # scope should be Liege
	value = 0
	add = {
		every_vassal = {
			limit = {
				is_powerful_vassal = yes
				NOT = {this = liege.diarch}
			}
			liege.diarch = { save_temporary_scope_as = dr_char }
			add = calc_rough_liege_opinion_value
		}

		divide = {
			value = vassal_count
			min = 1
		}

		if = {
			limit = {exists = diarch.var:dr_initial_vassal_standing_liege}
			subtract = diarch.var:dr_initial_vassal_standing_liege
		}
	}
}


dr_vassal_standing_change = { # scope should be liege
	value = 0
	add = {
		desc = dr_standing_difference
		every_vassal = {
			limit = {
				is_powerful_vassal = yes
				NOT = {this = liege.diarch}
			}
			liege.diarch = { save_temporary_scope_as = dr_char }
			add = dr_calc_rough_char_opinion_value
			subtract = calc_rough_liege_opinion_value
		}
	
		divide = {
			value = vassal_count
			min = 1
		}
		divide = @dr_vassal_opinions_divider
		max = @dr_vassal_opinions_swing_max
	}

	if = {
		limit = {exists = diarch.var:dr_initial_vassal_standing_difference}
		subtract = {
			desc = dr_standing_difference_initial
			value = diarch.var:dr_initial_vassal_standing_difference
		}
	}

	max = @dr_vassal_opinions_swing_max
	min = @dr_vassal_opinions_swing_min
}

dr_vassal_standing_change_abs = {
	value = dr_vassal_standing_change
	abs = yes
}

dr_imprisonment_state = { # Can be in both
	value = 0
	if = {
		limit = {
			OR = {
				is_imprisoned = yes
				is_incapable = yes
				has_character_modifier = isolating_modifier
			}
		}
		add = 1
	}
}

dr_diarch_imprisonment_state_change = { # scope should be Diarch
	value = 0
	if = {
		limit = {exists = var:dr_inital_diarch_imprisonment_state}
		if = {
			limit = {dr_imprisonment_state > var:dr_inital_diarch_imprisonment_state}
			add = 1 #is imprisoned
		}
		if = {
			limit = {dr_imprisonment_state < var:dr_inital_diarch_imprisonment_state}
			add = -1 #is released
		}
	}
}

dr_liege_imprisonment_state_change = { # scope should be Liege
	value = 0
	if = {
		limit = {exists = diarch.var:dr_inital_liege_imprisonment_state}
		if = {
			limit = {dr_imprisonment_state > diarch.var:dr_inital_liege_imprisonment_state}
			add = 1 #is imprisoned
		}
		if = {
			limit = {dr_imprisonment_state < diarch.var:dr_inital_liege_imprisonment_state}
			add = -1 #is released
		}
	}
}

dr_imprisonment_swing_change = { # scope should be Liege
	value = 0
	add = {
		desc = dr_diarch_imprisonment_change
		value = diarch.dr_diarch_imprisonment_state_change
		multiply = @dr_diarch_imprisonment_incapable_val
	}
	add = {
		desc = dr_liege_imprisonment_change
		value = dr_liege_imprisonment_state_change
		multiply = @dr_liege_imprisonment_incapable_val
	}
}

dr_imprisonment_swing_change_abs = {
	value = dr_imprisonment_swing_change
	abs = yes
}


dr_crown_authority = { # scope should be Diarch
	add = {
		desc = dr_crown_authority_level
		liege = {
			if = {
				limit = {has_realm_law_flag = uses_crown_authority}
				if = {
					limit = { has_realm_law = crown_authority_0 }
					add = 1
				}
				else_if = {
					limit = { has_realm_law = crown_authority_1 }
					add = 2
				}
				else_if = {
					limit = { has_realm_law = crown_authority_2 }
					add = 3
				}
				else_if = {
					limit = { has_realm_law = crown_authority_3 }
					add = 4
				}
			}
		}
	}

	add = {
		desc = dr_tribal_authority_level
		liege = {
			if = {
				limit = {has_realm_law_flag = uses_tribal_authority}
				if = {
					limit = { has_realm_law = tribal_authority_0 }
					add = 1
				}
				else_if = {
					limit = { has_realm_law = tribal_authority_1 }
					add = 2
				}
				else_if = {
					limit = { has_realm_law = tribal_authority_2 }
					add = 3
				}
				else_if = {
					limit = { has_realm_law = tribal_authority_3 }
					add = 4
				}
			}
		}
	}
}

dr_crown_authority_change = { # scope should be Liege
	add = {
		desc = dr_crown_authority_level
		if = {
			limit = {has_realm_law_flag = uses_crown_authority}
			if = {
				limit = { has_realm_law = crown_authority_0 }
				add = 1
			}
			else_if = {
				limit = { has_realm_law = crown_authority_1 }
				add = 2
			}
			else_if = {
				limit = { has_realm_law = crown_authority_2 }
				add = 3
			}
			else_if = {
				limit = { has_realm_law = crown_authority_3 }
				add = 4
			}
		}
	}

	add = {
		desc = dr_tribal_authority_level
		if = {
			limit = {has_realm_law_flag = uses_tribal_authority}
			if = {
				limit = { has_realm_law = tribal_authority_0 }
				add = 1
			}
			else_if = {
				limit = { has_realm_law = tribal_authority_1 }
				add = 2
			}
			else_if = {
				limit = { has_realm_law = tribal_authority_2 }
				add = 3
			}
			else_if = {
				limit = { has_realm_law = tribal_authority_3 }
				add = 4
			}
		}
	}

	if = {
		limit = {exists = diarch.var:dr_initial_crown_authority}
		subtract = diarch.var:dr_initial_crown_authority
	}
}

dr_crown_authority_swing_change = { # scope should be Liege
	add = {
		desc = dr_crown_authority_level
		if = {
			limit = {has_realm_law_flag = uses_crown_authority}
			if = {
				limit = { has_realm_law = crown_authority_0 }
				add = @dr_crown_authority_0_val
			}
			else_if = {
				limit = { has_realm_law = crown_authority_1 }
				add = @dr_crown_authority_1_val
			}
			else_if = {
				limit = { has_realm_law = crown_authority_2 }
				add = @dr_crown_authority_2_val
			}
			else_if = {
				limit = { has_realm_law = crown_authority_3 }
				add = @dr_crown_authority_3_val
			}
		}
	}

	add = {
		desc = dr_tribal_authority_level
		if = {
			limit = {has_realm_law_flag = uses_tribal_authority}
			if = {
				limit = { has_realm_law = tribal_authority_0 }
				add = @dr_crown_authority_0_val
			}
			else_if = {
				limit = { has_realm_law = tribal_authority_1 }
				add = @dr_crown_authority_1_val
			}
			else_if = {
				limit = { has_realm_law = tribal_authority_2 }
				add = @dr_crown_authority_2_val
			}
			else_if = {
				limit = { has_realm_law = tribal_authority_3 }
				add = @dr_crown_authority_3_val
			}
		}
	}

	if = {
		limit = {exists = diarch.var:dr_initial_crown_authority}
		subtract = {
			desc = dr_authority_level_initial
			if = {
				limit = { diarch.var:dr_initial_crown_authority = 1 }
				add = @dr_crown_authority_0_val
			}
			else_if = {
				limit = { diarch.var:dr_initial_crown_authority = 2 }
				add = @dr_crown_authority_1_val
			}
			else_if = {
				limit = { diarch.var:dr_initial_crown_authority = 3 }
				add = @dr_crown_authority_2_val
			}
			else_if = {
				limit = { diarch.var:dr_initial_crown_authority = 4 }
				add = @dr_crown_authority_3_val
			}
		}
	}
}

dr_crown_authority_swing_change_abs = {
	value = dr_crown_authority_swing_change
	abs = yes
}

dr_years_reigned_change = { # scope should be Liege
	value = primary_title.title_held_years
	if = {
		limit = {exists = diarch.var:dr_inital_years_reigned}
		subtract = diarch.var:dr_inital_years_reigned
	}
}

dr_years_reigned_swing_change = { # scope should be Liege
	value = 0
	add = {
		desc = dr_short_reign
		value = 0
		if =  {
			limit =  {
				primary_title = {
					title_held_years <= 1
				}
			}
			add = @dr_very_short_reign
		}
		else_if =  {
			limit =  {
				primary_title = {
					title_held_years <= 5
				}
			}
			add = @dr_short_reign
		}
	}

	add = {
		desc = dr_long_reign
		value = 0
		if =  {
			limit =  {
				primary_title = {
					title_held_years >= 25
				}
			}
			add = @dr_very_long_reign
		}
		else_if =  {
			limit =  {
				primary_title = {
					title_held_years >= 15
				}
			}
			add = @dr_long_reign
		}
	}

	if = {
		limit = {exists = diarch.var:dr_inital_years_reigned}
		subtract = {
			desc = dr_initial_reign
			value = 0
			add = {
				#desc = dr_short_reign
				value = 0
				if =  {
					limit =  {
						diarch.var:dr_inital_years_reigned <= 1
					}
					add = @dr_very_short_reign
				}
				else_if =  {
					limit =  {
						diarch.var:dr_inital_years_reigned <= 5
					}
					add = @dr_short_reign
				}
			}
		
			add = {
				#desc = dr_long_reign
				value = 0
				if =  {
					limit =  {
						diarch.var:dr_inital_years_reigned >= 25
					}
					add = @dr_very_long_reign
				}
				else_if =  {
					limit =  {
						diarch.var:dr_inital_years_reigned >= 15
					}
					add = @dr_long_reign
				}
			}
		}
	}
}

dr_years_reigned_swing_change_abs = {
	value = dr_years_reigned_swing_change
	abs = yes
}

dr_councillor_state = { # scope should be Diarch
	value = 0
	if = {
		limit = { is_councillor_of = liege }
		add = 1
	}
}

dr_councillor_state_change = { # scope should be Diarch
	value = 0
	if = {
		limit = {exists = var:dr_inital_councillor_state}
		if = {
			limit = { dr_councillor_state > var:dr_inital_councillor_state }
			add = 1
		}
	
		if = {
			limit = { dr_councillor_state < var:dr_inital_councillor_state }
			subtract = 1
		}
	}
}

dr_councillor_swing_change = { # scope should be Liege
	value = 0
	if = {
		limit = {exists = diarch.var:dr_inital_councillor_state}
		if = {
			limit = { diarch.dr_councillor_state > diarch.var:dr_inital_councillor_state }
			add = {
				desc = dr_become_councillor
				value = 1
				multiply = @dr_councillor_val
			}
		}
	
		if = {
			limit = { diarch.dr_councillor_state < diarch.var:dr_inital_councillor_state }
			subtract = {
				desc = dr_no_longer_councillor
				value = 1
				multiply = @dr_councillor_val
			}
		}
	}
}

dr_councillor_swing_change_abs = {
	value = dr_councillor_swing_change
	abs = yes
}

dr_diarch_relation_swing = { # scope should be Diarch
	value = 0

	### Close Family
	add = {
		desc = dr_close_family
		if = {
			limit = {
				OR = {
					is_close_family_of = liege
					is_consort_of = liege
				}
			}
			add = @dr_liege_close_family
		}
	}
	### Close Family/

	### Relations
	add = {
		desc = dr_liege_close_relation
		if = {
			limit = {
				OR = {
					has_relation_friend = liege
					has_relation_lover = liege
				}
				NOR = {
					has_relation_best_friend = liege
					has_relation_soulmate = liege
				}
			}
			add = @dr_positive_relation_with_liege
		}
	}

	add = {
		desc = dr_liege_close_relation
		if = {
			limit = {
				OR = {
					has_relation_best_friend = liege
					has_relation_soulmate = liege
				}
			}
			add = @dr_life_long_positive_relation_with_liege
		}
	}

	add = {
		desc = dr_liege_enemy
		if = {
			limit = {
				has_relation_rival = liege
				NOT = {has_relation_nemesis = liege}
			}
			add = @dr_negative_relation_with_liege
		}
	}

	add = {
		desc = dr_liege_enemy
		if = {
			limit = {
				has_relation_nemesis = liege
			}
			add = @dr_life_long_negative_relation_with_liege
		}
	}

	if = {
		limit = {
			exists = liege.faith.religious_head
			liege.faith.religious_head = {is_alive = yes}
		}
		add = {
			desc = dr_religious_head_close_relation
			if = {
				limit = {
					OR = {
						has_relation_friend = liege.faith.religious_head
						has_relation_lover = liege.faith.religious_head
					}
					NOR = {
						has_relation_best_friend = liege.faith.religious_head
						has_relation_soulmate = liege.faith.religious_head
					}
				}
				add = @dr_positive_relation_with_religious_head
			}
		}

		add = {
			desc = dr_religious_head_close_relation
			if = {
				limit = {
					OR = {
						has_relation_best_friend = liege.faith.religious_head
						has_relation_soulmate = liege.faith.religious_head
					}
				}
				add = @dr_life_long_positive_relation_with_religious_head
			}
		}

		add = {
			desc = dr_religious_head_enemy
			if = {
				limit = {
					has_relation_rival = liege.faith.religious_head
					NOT = {has_relation_nemesis = liege.faith.religious_head}
				}
				add = @dr_negative_relation_with_religious_head
			}
		}

		add = {
			desc = dr_religious_head_enemy
			if = {
				limit = {
					has_relation_nemesis = liege.faith.religious_head
				}
				add = @dr_life_long_negative_relation_with_religious_head
			}
		}
	}
	### Relations/

	if = {
		limit = { has_global_variable = esr_is_loaded }
		add = {
			desc = trait_track_esr_popular_among_peers
			if = {
				limit = {
					has_trait = esr_social_standing
					has_trait_xp = {
						trait = esr_social_standing
						track = esr_popular_among_peers
						value >= 90
					}
				}
				add = @dr_popularity_among_peers_level_3_val
			}
			else_if = {
				limit = {
					has_trait = esr_social_standing
					has_trait_xp = {
						trait = esr_social_standing
						track = esr_popular_among_peers
						value >= 60
					}
				}
				add = @dr_popularity_among_peers_level_2_val
			}
			else_if = {
				limit = {
					has_trait = esr_social_standing
					has_trait_xp = {
						trait = esr_social_standing
						track = esr_popular_among_peers
						value >= 30
					}
				}
				add = @dr_popularity_among_peers_level_1_val
			}
		}
	}
}

dr_diarch_relation_swing_change = { # scope should be Liege
	value = 0
	add = {
		desc = dr_diarch_relations
		value = diarch.dr_diarch_relation_swing
	}

	if = {
		limit = {exists = diarch.var:dr_inital_diarch_relation}
		subtract = {
			desc = dr_diarch_relations_initial
			value = diarch.var:dr_inital_diarch_relation
		}
	}
}

dr_diarch_relation_swing_change_abs = {
	value = dr_diarch_relation_swing_change
	abs = yes
}

dr_diarch_inheritance_swing = { # scope should be Diarch
	value = 0

	add = {
		desc = dr_line_of_succession
		save_temporary_scope_as = dr_char
		every_claim = {
			limit = {
				holder ?= {this = scope:dr_char.liege}
				tier = scope:dr_char.liege.primary_title.tier
			}
			if = {
				limit = {
					place_in_line_of_succession = {
						target = scope:dr_char
						value <= @dr_line_of_succession_near_limit
					}
				}
				add = @dr_line_of_succession_near_val
			}
			else_if = {
				limit = {
					place_in_line_of_succession = {
						target = scope:dr_char
						value <= @dr_line_of_succession_far_limit
					}
				}
				add = @dr_line_of_succession_far_val
			}
		}
	}
}

dr_diarch_inheritance_swing_change = { # scope should be Liege
	value = 0

	diarch = {
		add = {
			desc = dr_line_of_succession
			save_temporary_scope_as = dr_char
			every_claim = {
				limit = {
					holder ?= {this = scope:dr_char.liege}
					tier = scope:dr_char.liege.primary_title.tier
				}
				if = {
					limit = {
						place_in_line_of_succession = {
							target = scope:dr_char
							value <= @dr_line_of_succession_near_limit
						}
					}
					add = @dr_line_of_succession_near_val
				}
				else_if = {
					limit = {
						place_in_line_of_succession = {
							target = scope:dr_char
							value <= @dr_line_of_succession_far_limit
						}
					}
					add = @dr_line_of_succession_far_val
				}
			}
		}
	
		if = {
			limit = {exists = var:dr_inheritance_swing_initial}
			subtract = {
				desc = dr_line_of_succession_initial
				value = var:dr_inheritance_swing_initial
			}
		}
	}
}

dr_diarch_inheritance_swing_change_abs = {
	value = dr_diarch_inheritance_swing_change
	abs = yes
}

####################################### dr_swing_value and its changes end ######################################

dr_initial_swing = {
	value = dr_swing_value

	if = { # If dr_swing_residual is higher than dr_swing_value than use dr_swing_residual as initial_swing
		limit = {
			dr_swing_residual > 0
		}
		add = {
			desc = dr_swing_residual
			value = dr_swing_residual
			subtract = dr_swing_value
			min = 0
		}		
	}

	max = 90
	min = 0
}

dr_balance_swing = {
	value = dr_swing_value

	if = {
		limit = {
			#exists = this
			exists = liege
			is_alive = yes
			liege = {is_alive = yes}
		}
		### Experience as Diarch
		add = {
			desc = dr_diarch_experience
			if = {
				limit = {has_variable = dr_diarch_rule_duration}
				if = {
					limit = { var:dr_diarch_rule_duration > 1 } # Remeber to change it back to 36
					add = {
						value = var:dr_diarch_rule_duration
						multiply = dr_experience_swing_rate
					}
				}
			}

			min = 0
			max = dr_max_diarch_experience
		}
		### Experience as Diarch/

		### Vanilla
		# Accolades
		## Politicker helps liege during a regency	
		if = {
			limit = {
				liege ?= {
					has_diarchy_active_parameter = diarchy_type_is_entrenched_regency
					any_active_accolade = {
						has_accolade_parameter = accolade_influences_regency
					}
				}
			}
			add = {
				desc = dr_liege_accolade_swing
				value = accolade_scales_of_power_value
			}
		}
		if = {
			limit = {
				liege ?= {
					has_diarchy_active_parameter = diarchy_type_is_entrenched_regency
					any_active_accolade = {
						has_accolade_parameter = accolade_influences_regency_high
					}
				}
			}
			add = {
				desc = dr_liege_accolade_swing
				value = accolade_scales_of_power_value_high
			}
		}
		# Liege is able to end diarchy.
		if = {
			limit = {
				liege ?= {
					has_diarchy_active_parameter = diarchy_type_is_entrenched_regency
					can_leave_diarchy_trigger = yes
				}
			}
			multiply = {
				desc = dr_can_end_diarchy
				value = 0.7
			}
		}

		# FP3: some phases empower viziers.
		if = {
			limit = {
				liege ?= {
					has_diarchy_active_parameter = diarchy_is_vizierate
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = empowered_vizierate
					}
				}
			}
			add = {
				desc = dr_empower_viziers
				value = fp3_empower_viziers_swing_value
			}
		}

		# Co-rulership
		if = {
			limit = { liege ?= {has_diarchy_active_parameter = diarchy_type_is_junior_emperorship} }
			# Pretty decent for a teen.
			if = {
				limit = {child_is_teen_trigger = yes }
				add = {
					desc = dr_co_ruler_is_child
					value = 50
				}
			}
			# Adequate for an older child
			else_if = {
				limit = { child_old_enough_for_responsibility_trigger = yes }
				add = {
					desc = dr_co_ruler_is_child
					value = 30
				}
			}
			# A bit higher for a child.
			else_if = {
				limit = { child_not_infant_trigger = yes }
				add = {
					desc = dr_co_ruler_is_child
					value = 15
				}
			}			
		}

		### Vanilla/

		### esr_trait_track
		add = {
			desc = dr_popular_among_peers
			if = {
				limit = { has_global_variable = esr_is_loaded }
				if = {
					limit = {
						has_trait_xp = {
							trait = esr_social_standing
							track = esr_popular_among_peers
							value >= 90
						}
					}
					multiply = 1.3
				}
	
				else_if = {
					limit = {
						has_trait_xp = {
							trait = esr_social_standing
							track = esr_popular_among_peers
							value >= 60
						}
					}
					multiply = 1.2
				}
	
				else_if = {
					limit = {
						has_trait_xp = {
							trait = esr_social_standing
							track = esr_popular_among_peers
							value >= 30
						}
					}
					multiply = 1.1
				}
			}
		}
		### esr_trait_track		

		### Entrenched regency difficulty
		if = {
			limit = {
				liege ?= {
					has_diarchy_active_parameter = diarchy_type_is_entrenched_regency
				}
			}
			multiply = {
				desc = dr_is_entrenched
				value = 0.7
			}
		}
		### Entrenched regency difficulty/
		
		### vizierate difficulty
		if = {
			limit = {
				always = no #Replaced with civil service importance
				liege ?= {
					has_diarchy_active_parameter = diarchy_is_vizierate
				}
			}
			multiply = {
				desc = dr_is_vizierate
				value = 0.8
			}
		}
		### vizierate difficulty/

		### co-rulership difficulty
		if = {
			limit = {
				liege ?= {
					has_diarchy_active_parameter = diarchy_is_co_rulership
				}
			}
			if = {
				limit = {is_adult = no}
				multiply = {
					desc = dr_is_co_rulership
					value = 1.3
				}
			}

			else = {
				multiply = {
					desc = dr_is_co_rulership
					value = 1.5
				}
			}
		}
		### co-rulership difficulty/

		### Civil Service Importance for Clan
		if = {
			limit = {
				liege ?= {
					has_diarchy_active_parameter = diarchy_is_vizierate
				}
			}
			multiply = {
				desc = dr_civil_service_importance
				value = liege.dr_civil_service_importance
			}
		}
		### Civil Service Importance for Clan/
	}

	max = 100
	min = 0
}

dr_max_diarch_experience = {
	value = learning
	multiply = 4

	max = 60
	min = 10
}

dr_experience_swing_rate = {
	value = learning
	divide = 10

	max = 2
	min = 0
}

dr_swing_change = {
	value = dr_swing_value

	if = {
		limit = {
			has_variable = dr_swing_value
		}
		subtract = var:dr_swing_value
	}
}

dr_calc_rough_char_opinion_value = {
	if = {
		limit = {
			exists = scope:dr_char
		}
		if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 95
				}
			}
			add = 100
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 90
				}
			}
			add = 95
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 85
				}
			}
			add = 90
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 80
				}
			}
			add = 85
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 75
				}
			}
			add = 80
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 70
				}
			}
			add = 75
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 65
				}
			}
			add = 70
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 60
				}
			}
			add = 65
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 55
				}
			}
			add = 60
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 50
				}
			}
			add = 55
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 45
				}
			}
			add = 50
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 40
				}
			}
			add = 45
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 35
				}
			}
			add = 40
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 30
				}
			}
			add = 35
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 25
				}
			}
			add = 30
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 20
				}
			}
			add = 25
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 15
				}
			}
			add = 20
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 10
				}
			}
			add = 15
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 5
				}
			}
			add = 10
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= 0
				}
			}
			add = 5
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -4
				}
			}
			add = 0
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -5
				}
			}
			add = -5
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -10
				}
			}
			add = -10
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -15
				}
			}
			add = -15
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -20
				}
			}
			add = -20
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -25
				}
			}
			add = -25
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -30
				}
			}
			add = -30
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -35
				}
			}
			add = -35
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -40
				}
			}
			add = -40
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -45
				}
			}
			add = -45
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -50
				}
			}
			add = -50
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -55
				}
			}
			add = -55
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -60
				}
			}
			add = -60
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -65
				}
			}
			add = -65
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -70
				}
			}
			add = -70
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -75
				}
			}
			add = -75
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -80
				}
			}
			add = -80
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -85
				}
			}
			add = -85
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -90
				}
			}
			add = -90
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -95
				}
			}
			add = -95
		}
		else_if = {
			limit = {
				opinion = {
					target = scope:dr_char
					value >= -100
				}
			}
			add = -100
		}
	}
}

dr_current_swing = {
	value = 0
	if = {
		limit = {
			exists = liege
			liege = {
				is_alive = yes
				has_active_diarchy = yes
			}
			#is_diarch_of_target = liege
		}
		liege = {
			add = diarchy_swing
		}
	}
}

dr_swing_change_since_start = {
	value = 0
	if = {
		limit = {
			exists = this
			exists = liege
			is_alive = yes
			liege = {is_alive = yes}
			exists = var:dr_swing_value_beginning
			exists = var:dr_swing_value
		}
		add = var:dr_swing_value
		subtract = var:dr_swing_value_beginning
		multiply = -1
	}
}

dr_swing_change_since_start_for_gui = {
	value = 0
	if = {
		limit = {
			exists = this
			exists = liege
			is_alive = yes
			liege = {is_alive = yes}
			exists = var:dr_swing_value_beginning
			exists = var:dr_swing_value
		}
		add = {
			desc = dr_current_initial_swing_value
			value = var:dr_swing_value
		}
		subtract = {
			desc = dr_initial_initial_swing_value 
			value = var:dr_swing_value_beginning
		}
	}
}

dr_swing_change_since_start_absolute = {
	value = dr_swing_change_since_start
	abs = yes
}

dr_swing_momentum_for_gui = {
	value = 0
	add = {
		value = dr_swing_change_since_start_for_gui
		divide = 200
		max = 0.5
		min = -0.5
	}
}

dr_swing_momentum = {
	value = 0

	add = {
		desc = dr_swing_to_balance
		value = dr_swing_to_balance_absolute
		divide = 50
		max = 1
		min = 0.1
	}

	if = {
		limit = {var:dr_balance_swing < dr_current_swing} # Change dr_balance_swing to var:dr_balance_swing for performance
		add = {
			desc = dr_swing_momentum
			value = dr_swing_change_since_start
			divide = 200
			max = 0.5
			min = -0.5
		}
	}
	if = {
		limit = {var:dr_balance_swing > dr_current_swing}
		subtract = {
			desc = dr_swing_momentum
			value = dr_swing_change_since_start
			divide = 200
			max = 0.5
			min = -0.5
		}
	}
	min = 0.1
}

dr_swing_momentum_absolute = {
	value = dr_swing_momentum
	abs = yes
}

dr_swing_momentum_actual = {
	value = 0
	if = {
		limit = {
			exists = var:dr_balance_swing
			var:dr_balance_swing < dr_current_swing
		}
		if = {
			limit = {dr_swing_to_balance_absolute < dr_swing_momentum_absolute}
			subtract = dr_swing_to_balance_absolute
		}
		else = {
			subtract = dr_swing_momentum
		}
	}

	if = {
		limit = {
			exists = var:dr_balance_swing
			var:dr_balance_swing > dr_current_swing
		}
		if = {
			limit = {dr_swing_to_balance_absolute < dr_swing_momentum_absolute}
			add = dr_swing_to_balance_absolute
		}
		else = {
			add = dr_swing_momentum
		}
	}
}

dr_swing_momentum_ai = {
	value = dr_swing_momentum
	multiply = 3
}

dr_swing_momentum_absolute_ai = {
	value = dr_swing_momentum_ai
	abs = yes
}

dr_swing_momentum_actual_ai = {
	value = 0
	if = {
		limit = {
			exists = var:dr_balance_swing
			var:dr_balance_swing < dr_current_swing
		}
		if = {
			limit = {dr_swing_to_balance_absolute < dr_swing_momentum_absolute_ai}
			subtract = dr_swing_to_balance_absolute
		}
		else = {
			subtract = dr_swing_momentum_ai
		}
	}

	if = {
		limit = {
			exists = var:dr_balance_swing
			var:dr_balance_swing > dr_current_swing
		}
		if = {
			limit = {dr_swing_to_balance_absolute < dr_swing_momentum_absolute_ai}
			add = dr_swing_to_balance_absolute
		}
		else = {
			add = dr_swing_momentum_ai
		}
	}
}

dr_swing_to_balance_absolute = {
	value = 0
	if = {
		limit = { exists = var:dr_balance_swing }
		add = {
			value = dr_current_swing
			subtract = var:dr_balance_swing
			abs = yes
		}
	}
}

dr_test_value = {
	value = -200
}

dr_test_value_absolute = {
	value = dr_test_value
	if = {
		limit = {dr_test_value < 0}
		multiply = -1
	}
}

### For Succession Score overrides (Obsolete until reimplementation)

dr_succession_court_nerf_value = {
	value = 2
}

dr_degree_of_upset = {
	value = 0
	add = {
		# Get the base score
		## We start in reverse so that we can grok min-maxs easier, and will invert it later.
		value = diarchy_regent_succession_score_value
		# Now, subtract their score to see how badly we were screwed.
		subtract = scope:actor.diarchy_regent_succession_score_value
		# Sweet, next up, add some extra to act as a decent maximum without smoothing the scores out too much.
		add = 10
		# Make sure no one is ever _too_ pissed.
		max = 40
		# And, finally, invert.
		multiply = -1		
	}
}

dr_degree_of_upset_for_courtly_scandalise = {
	value = courtly_offence_scandalise_value

	save_temporary_scope_as = dr_vassal
	if = {
		limit = {
			diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value
			scope:recipient = {any_diarchy_succession_character = {this = scope:dr_vassal}}
		}
		subtract = dr_degree_of_upset
	}
}

dr_degree_of_upset_for_courtly_greatly_offend = {
	value = courtly_offence_greatly_offend_value
	
	save_temporary_scope_as = dr_vassal
	if = {
		limit = {
			diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value
			scope:recipient = {any_diarchy_succession_character = {this = scope:dr_vassal}}
		}
		subtract = dr_degree_of_upset
	}
}

dr_degree_of_upset_for_courtly_offend = {
	value = courtly_offence_offend_value
	
	save_temporary_scope_as = dr_vassal
	if = {
		limit = {
			diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value
			scope:recipient = {any_diarchy_succession_character = {this = scope:dr_vassal}}
		}
		subtract = dr_degree_of_upset
	}
}

dr_degree_of_upset_for_courtly_somewhat_offend = {
	value = courtly_offence_somewhat_offend_value
	
	save_temporary_scope_as = dr_vassal
	if = {
		limit = {
			diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value
			scope:recipient = {any_diarchy_succession_character = {this = scope:dr_vassal}}
		}
		subtract = dr_degree_of_upset
	}
}

dr_degree_of_upset_for_courtly_lightly_offend = {
	value = courtly_offence_lightly_offend_value
	
	save_temporary_scope_as = dr_vassal
	if = {
		limit = {
			diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value
			scope:recipient = {any_diarchy_succession_character = {this = scope:dr_vassal}}
		}
		subtract = dr_degree_of_upset
	}
}

dr_designate_regent_ai_opinion_malus = {
	value = 0

	add = {
		desc = dr_place_holder
		if = {
			limit = {
				any_diarchy_succession_character = { diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value }
			}
			every_diarchy_succession_character = {
				limit = {
					diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value 
				}
				add = dr_degree_of_upset
			}
			if = {
				limit = {
					scope:actor = {
						is_lowborn = yes
						NOT = {
							any_close_or_extended_family_member = { this = scope:recipient }
						}
					}
				}
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					if = {
						limit = { dr_degree_of_upset_for_courtly_scandalise <= -1 }
						add = dr_degree_of_upset_for_courtly_scandalise
					}
				}
			}
			## ... a lowborn family member.
			else_if = {
				limit = {
					scope:actor = { is_lowborn = yes }
				}
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					if = {
						limit = { dr_degree_of_upset_for_courtly_greatly_offend <= -1 }
						add = dr_degree_of_upset_for_courtly_greatly_offend
					}
				}
			}
			## ... someone with a weak succession score.
			else_if = {
				limit = { scope:actor.diarchy_regent_succession_score_value <= courtly_offence_at_succession_threshold_low }
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor}
					}
					if = {
						limit = { dr_degree_of_upset_for_courtly_offend <= -1 }
						add = dr_degree_of_upset_for_courtly_offend
					}
				}
			}
			## ... someone with a medium succession score.
			else_if = {
				limit = { scope:actor.diarchy_regent_succession_score_value <= courtly_offence_at_succession_threshold_medium }
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					if = {
						limit = { dr_degree_of_upset_for_courtly_somewhat_offend <= -1 }
						add = dr_degree_of_upset_for_courtly_somewhat_offend
					}
				}
			}
			## ... someone with a just-barely adequate succession score.
			else_if = {
				limit = { scope:actor.diarchy_regent_succession_score_value <= courtly_offence_at_succession_threshold_high }
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					if = {
						limit = { dr_degree_of_upset_for_courtly_lightly_offend <= -1 }
						add = dr_degree_of_upset_for_courtly_lightly_offend
					}
				}
			}				
		}
	}

	divide = 4

	max = 0
}

dr_designate_regent_ai_opinion_malus_simplified = {
	value = 0

	add = {
		desc = dr_place_holder
		if = {
			limit = {
				any_diarchy_succession_character = { diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value }
			}
			every_diarchy_succession_character = {
				limit = {
					diarchy_regent_succession_score_value > scope:actor.diarchy_regent_succession_score_value 
				}
				add = dr_degree_of_upset
			}
			if = {
				limit = {
					scope:actor = {
						is_lowborn = yes
						NOT = {
							any_close_or_extended_family_member = { this = scope:recipient }
						}
					}
				}
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					add = courtly_offence_scandalise_value
				}
			}
			## ... a lowborn family member.
			else_if = {
				limit = {
					scope:actor = { is_lowborn = yes }
				}
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					add = courtly_offence_greatly_offend_value
				}
			}
			## ... someone with a weak succession score.
			else_if = {
				limit = { scope:actor.diarchy_regent_succession_score_value <= courtly_offence_at_succession_threshold_low }
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor}
					}
					add = courtly_offence_offend_value
				}
			}
			## ... someone with a medium succession score.
			else_if = {
				limit = { scope:actor.diarchy_regent_succession_score_value <= courtly_offence_at_succession_threshold_medium }
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					add = courtly_offence_somewhat_offend_value
				}
			}
			## ... someone with a just-barely adequate succession score.
			else_if = {
				limit = { scope:actor.diarchy_regent_succession_score_value <= courtly_offence_at_succession_threshold_high }
				every_vassal = {
					limit = {
						has_vassal_stance = courtly
						NOT = { this = scope:actor }
					}
					add = courtly_offence_lightly_offend_value
				}
			}				
		}
	}

	divide = 6

	max = 0
	min = -100
}

dr_designate_regent_ai_initial_swing = {
	value = dr_initial_swing
	divide = -2
}

dr_civil_service_importance = {
	value = 0
	every_tax_collector = {
		limit = {
			exists = this
			is_alive = yes
		}
		save_temporary_scope_as = tax_collector
		add = dr_clan_government_tax_collector_aptitude_multiplier
	}
	divide = {
		value = 0
		every_tax_collector = {
			limit = {
				exists = this
				is_alive = yes
			}
			add = 1
		}
		min = 1
	}
	min = 0.25
}

dr_clan_government_tax_collector_aptitude_multiplier = {
	value =	{
		if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 5
				}
			}
			add = 1.5
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 4
				}
			}
			add = 1.25
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 3
				}
			}
			add = 1
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 2
				}
			}
			add = 0.75
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 1
				}
			}
			add = 0.5
		}
		else = {
			add = 0.25
		}
	}
}

dr_civil_service_importance_initial_swing = {
	value = dr_civil_service_importance
	add = -1
	multiply = {
		value = diarch.dr_balance_swing
		divide = dr_civil_service_importance
	}
}

dr_civil_service_importance_swing_change = {
	value = 0
	add = {
		desc = dr_civil_service_importance
		value = 0
		add = {
			value = dr_civil_service_importance
			add = -1
		}
		multiply = {
			value = diarch.dr_balance_swing
			divide = dr_civil_service_importance
		}
	}

	if = {
		limit = {exists = diarch.var:dr_inital_liege_civil_service_importance_swing}
		subtract = {
			desc = dr_civil_service_importance_initial
			value = diarch.var:dr_inital_liege_civil_service_importance_swing
		}
	}
}

dr_civil_service_importance_ui = {
	value = 0
	every_tax_collector = {
		limit = {
			exists = this
			is_alive = yes
		}
		save_temporary_scope_as = tax_collector
		add = dr_clan_government_tax_collector_aptitude_ui
	}
	divide = {
		value = 0
		every_tax_collector = {
			limit = {
				exists = this
				is_alive = yes
			}
			add = 1
		}
		min = 1
	}
}

dr_civil_service_importance_ui_change = {
	value = dr_civil_service_importance_ui
	if = {
		limit = {exists = diarch.var:dr_inital_liege_civil_service_importance}
		subtract = diarch.var:dr_inital_liege_civil_service_importance
	}
}

dr_clan_government_tax_collector_aptitude_ui = {
	value =	{
		if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 5
				}
			}
			add = 4
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 4
				}
			}
			add = 3
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 3
				}
			}
			add = 2
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 2
				}
			}
			add = 1
		}
		else_if = {
			limit = {
				check_tax_collector_aptitude = {
					CHARACTER = scope:tax_collector
					VALUE = 1
				}
			}
			add = -1
		}
		else = {
			add = -2
		}
	}
}

dr_swing_residual = {
	value = 0
	if = {
		limit = {
			exists = this
			has_variable = dr_swing_residual
		}
		add = {
			value = var:dr_swing_residual
		}
	}
}

dr_swing_residual_tick = {
	value = dr_swing_residual
	subtract = @dr_swing_residual_monthly_decay
	
	min = 0
}

dr_swing_residual_triple_tick = {
	value = dr_swing_residual
	subtract = {
		value = @dr_swing_residual_monthly_decay
		multiply = 3
	}

	min = 0
}