#Needs to be loaded after

################
# Stop War attacker chance calculation
################

stop_war_diplomacy_modifier = {
	value = diplomacy
	multiply = 5
}

stop_war_relation_penalties = {
	# Opinion Penalties
	if = {
		limit = {
			opinion = {
				target = scope:actor
				value < -49
			}
		}
		add = -50
	}
	else_if = {
		limit = {
			opinion = {
				target = scope:actor
				value < 0
			}
		}
		add = -25
	}

	# Relationship Penalty
	if = {
		limit = {
			has_relation_rival = scope:actor
		}
		add = -100
	}
}

stop_war_relation_bonuses = {
	# Opinion Bonuses
	if = {
		limit = {
			opinion = {
				target = scope:actor
				value >= 50
			}
		}
		add = 50
	}
	else_if = {
		limit = {
			opinion = {
				target = scope:actor
				value > 0
			}
		}
		add = 25
	}

	# Relationship Bonus
	if = {
		limit = {
			has_relation_friend = scope:actor
		}
		add = 25
	}
}

#Calculated in scope:recipient
stop_war_attacker_likelihood_calculation = {
	if = {
		# Cowed characters always accept.
		limit = {
			has_dread_level_towards = {
				target = scope:actor
				level = 2
			}
		}
		value = 100
	}
	else = {
		# Calculate chance of character accepting.
		# Penalties are added first, which then get reduced by Intimidation (if applicable).
		# Then bonuses are added, and finally circumstantial modifiers.

		###################
		# Negative Values #
		###################
		subtract = stop_war_diplomacy_modifier
		add = stop_war_relation_penalties

		# Trait Penalties
		if = {
			limit = {
				has_trait = stubborn
			}
			add = -50
		}
		if = {
			limit = {
				has_trait = ambitious
			}
			add = -30
		}
		if = {
			limit = {
				has_trait = disloyal
			}
			add = -25
		}

		#########################
		# Indimidation Modifier #
		#########################
		if = {
			limit = {
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			multiply = 0.5 # Reduces all current penalties by 50%
		}

		###################
		# Positive Values #
		###################

		# Liege's Diplomacy
		add = scope:actor.stop_war_diplomacy_modifier
		add = stop_war_relation_bonuses

		# Traits
		if = {
			limit = {
				has_trait = fickle
			}
			add = 30
		}
		if = {
			limit = {
				has_trait = loyal
			}
			add = 50
		}

		#######################
		# Other Circumstances #
		#######################

		# Warscore
		if = {
			limit = {
				exists = scope:target
			}
			subtract = scope:target.attacker_war_score
		}
		
		# Admin
		if = {
			limit = {
				is_governor = yes
			}
			add = 50
			if = {
				limit = {
					scope:target = { has_realm_law = imperial_bureaucracy_3 }
				}
				add = 30
			}
			else_if = {
				limit = {
					scope:target = { has_realm_law = imperial_bureaucracy_2 }
				}
				add = 20
			}
			else_if = {
				limit = {
					scope:target = { has_realm_law = imperial_bureaucracy_1 }
				}
				add = 10
			}
		}

		#TODO: Strong claim/Weak claim difference

		#TODO: Laws

		min = 5
		max = 95
	}
}

stop_war_defender_likelihood_calculation = {
	if = {
		# Cowed characters always accept.
		limit = {
			has_dread_level_towards = {
				target = scope:actor
				level = 2
			}
		}
		value = 100
	}
	else_if = {
		#If it's a war over the defender's last county, they won't say yes.
		if = {
			limit = {
				exists = scope:target
				any_held_title = {
					count = 1
					tier = tier_county
					always = yes
					save_temporary_scope_as = last_county_title
				}
				scope:target.casus_belli = {
					any_target_title = {
						this = scope:last_county_title
					}
				}
			}
			value = 0
		}
	}
	else = {
		# Calculate chance of character accepting.
		# Penalties are added first, which then get reduced by Intimidation (if applicable).
		# Then bonuses are added, and finally circumstantial modifiers.

		###################
		# Negative Values #
		###################
		subtract = stop_war_diplomacy_modifier
		add = stop_war_relation_penalties

		# Traits
		if = {
			limit = {
				has_trait = arrogant
			}
			add = -50
		}
		if = {
			limit = {
				has_trait = ambitious
			}
			add = -30
		}
		if = {
			limit = {
				has_trait = disloyal
			}
			add = -25
		}
		if = {
			limit = {
				has_trait = greedy
			}
			add = -20
		}

		#########################
		# Intimidation Modifier #
		#########################
		if = {
			limit = {
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			multiply = 0.75 # Reduces all current penalties by 25% (Intentionally less than the 50% reduction for attackers, because defenders have more to lose).
		}

		###################
		# Positive Values #
		###################

		# Liege's Diplomacy
		add = scope:actor.stop_war_diplomacy_modifier
		add = stop_war_relation_bonuses

		# Traits
		if = {
			limit = {
				has_trait = humble
			}
			add = 20
		}
		if = {
			limit = {
				has_trait = loyal
			}
			add = 50
		}

		#######################
		# Other Circumstances #
		#######################

		# Warscore
		if = {
			limit = {
				exists = scope:target
			}
			subtract = scope:target.defender_war_score
		}
		
		# Admin
		if = {
			limit = {
				is_governor = yes
			}
			add = 50
			if = {
				limit = {
					scope:target = { has_realm_law = imperial_bureaucracy_3 }
				}
				add = 30
			}
			else_if = {
				limit = {
					scope:target = { has_realm_law = imperial_bureaucracy_2 }
				}
				add = 20
			}
			else_if = {
				limit = {
					scope:target = { has_realm_law = imperial_bureaucracy_1 }
				}
				add = 10
			}
		}

		#TODO: Strong claim/Weak claim difference

		#TODO: Laws

		min = 1
		max = 95
	}
}

stop_war_attacker_bribe_size = {
	value = medium_gold_value
	multiply = 2
}

stop_war_defender_bribe_size = {
	value = medium_gold_value
	multiply = 4
}