﻿vta_faction_power_threshold = {
	value = faction_power_threshold
}

vta_claimant_faction_threshold = {
	value = 0
	if = {
		limit = {
			OR = {
				NOT = { exists = global_var:vta_claimant_faction_war }
				global_var:vta_claimant_faction_war = 1
			}
			exists = faction_leader
			exists = special_title
			special_title = { save_temporary_scope_as = claiming_title }
			this = { save_temporary_scope_as = faction }
			faction_target = { save_temporary_scope_as = target_character }
		}
		faction_target = {
			every_vassal_or_below = {
				limit = {
					save_temporary_scope_as = vassal
					primary_title.tier >= tier_county
					OR = {
						# I have a title inside the target
						any_sub_realm_county = {
							target_is_de_jure_liege_or_above = scope:claiming_title
						}
						# I am a direct vassal of the faction target AND the target title is the highest realm title tier
						AND = {
							is_vassal_of = scope:target_character
							has_game_rule = vta_rule_land_subject
							scope:target_character.primary_title.tier = scope:claiming_title.tier
						}
					}
					# Exclude faction members
					scope:faction = {
						NOT = {
							any_faction_member = {
								this = scope:vassal
							}
						}
					}
				}
				# Loses title
				if = {
					limit = { vta_claimant_faction_trigger = yes }
					every_held_title = {
						limit = { tier = tier_county }
						add = county_levies_to_raise
					}
				}
				# Gets new liege
				else_if = {
					limit = {
						has_game_rule = vta_rule_land_subject
						OR = {
							# Is vassal of the target character
							is_vassal_of = scope:target_character
							# Is NOT vassal of the target character AND ...
							AND = {
								NOT = { is_vassal_of = scope:target_character }
								# Liege would lose land AND I have something inside the target title
								AND = {
									liege = {
										vta_claimant_faction_trigger = yes
									}
									any_sub_realm_county = {
										target_is_de_jure_liege_or_above = scope:claiming_title
									}
								}
							}
						}
					}
					every_held_title = {
						limit = { tier = tier_county }
						add = {
							add = county_levies_to_raise
							divide = 2
						}
					}
				}
			}
			# Add defenders strength
			every_held_title = {
				limit = { tier = tier_county }
				add = county_levies_to_raise
			}
		}
		divide = vta_faction_strength
		multiply = 50
		min = 0
	}
}

vta_faction_strength = {
	value = 0
	every_faction_member = {
		every_held_title = {
			limit = { tier = tier_county }
			add = county_levies_to_raise
		}
	}
	min = 1
}

vta_join_liege = {
	value = 0

	# I will lose a title
	if = {
		limit = { exists = scope:war }
		if = {
			limit = {
				OR = {
					any_vassal_or_below = {
						save_temporary_scope_as = vassal
						scope:war = {
							is_target_in_variable_list = {
								name = vta_lose_land
								target = scope:vassal
							}
						}
					}
					scope:war = {
						is_target_in_variable_list = {
							name = vta_lose_land
							target = PREV
						}
					}
				}
			}
			add = 15
		}
	}

	# I will get a new liege
	if = {
		limit = { exists = scope:war }
		if = {
			limit = {
				has_game_rule = vta_rule_land_subject
				scope:war = {
					is_target_in_variable_list = {
						name = vta_change_liege
						target = PREV
					}
				}
			}
			add = 15
		}
	}
	else = {
		add = 15
	}
	
	# My liege is my friend
	if = {
		limit = {
			OR = {
				has_relation_friend = liege
				has_relation_best_friend = liege
			}
		}
		multiply = 2
	}

	# Liege is rival
	if = {
		limit = {
			has_relation_rival = liege
		}
		multiply = 0.2
	}

	# Liege is tyrannt
	if = {
		limit = {
			liege = { tyranny > 15 }
		}
		multiply = 0.5
	}

	# I like my liege
	if = {
		limit = {
			opinion = {
				target = liege
				value >= 50
			}
		}
		multiply = 1.2
	}

	# I like my liege a lot
	if = {
		limit = {
			opinion = {
				target = liege
				value >= 75
			}
		}
		multiply = 1.3
	}

	# My liege is the best
	if = {
		limit = {
			opinion = {
				target = liege
				value >= 100
			}
		}
		multiply = 2
	}
	
	# positive => joining more likely 
	if = {
		limit = { has_trait = brave }
		multiply = 1.2
	}
	if = {
		limit = { has_trait = diligent }
		multiply = 1.2
	}
	if = {
		limit = { has_trait = honest }
		multiply = 1.2
	}
	if = {
		limit = { has_trait = just }
		multiply = 1.2
	}
	if = {
		limit = { has_trait = trusting }
		multiply = 1.2
	}

	# negative => joining less likely
	if = {
		limit = { has_trait = content }
		multiply = 0.5
	}
	if = {
		limit = { has_trait = craven }
		multiply = 0.5
	}
	if = {
		limit = { has_trait = lazy }
		multiply = 0.5
	}
	if = {
		limit = { has_trait = fickle }
		multiply = 0.5
	}
	if = {
		limit = { has_trait = arbitrary }
		multiply = 0.5
	}

	min = 0
}

vta_dont_join_liege = {
	value = 0

	# I will NOT lose any land
	if = {
		limit = { exists = scope:war }
		if = {
			limit = {
				NOR = {
					any_vassal_or_below = {
						save_temporary_scope_as = vassal
						scope:war = {
							is_target_in_variable_list = {
								name = vta_lose_land
								target = scope:vassal
							}
						}
					}
					scope:war = {
						is_target_in_variable_list = {
							name = vta_lose_land
							target = PREV
						}
					}
				}
			}
			add = 30
		}
	}
	else = {
		add = 30
	}

	# liege is my friend
	if = {
		limit = {
			OR = {
				has_relation_friend = liege
				has_relation_best_friend = liege
			}
		}
		multiply = 0.2
	}

	# Liege is my rival
	if = {
		limit = {
			has_relation_rival = liege
		}
		multiply = 2
	}

	if = {
		limit = {
			opinion = {
				target = liege
				value <= 25
			}
		}
		multiply = 1.15
	}

	if = {
		limit = {
			opinion = {
				target = liege
				value <= -25
			}
		}
		multiply = 1.3
	}

	if = {
		limit = {
			opinion = {
				target = liege
				value <= -50
			}
		}
		multiply = 1.4
	}

	if = {
		limit = {
			opinion = {
				target = liege
				value <= -75
			}
		}
		multiply = 1.5
	}

	if = {
		limit = {
			opinion = {
				target = liege
				value <= -100
			}
		}
		multiply = 2
	}

	# positive => joining more likely 
	if = {
		limit = { has_trait = brave }
		multiply = 0.7
	}
	if = {
		limit = { has_trait = diligent }
		multiply = 0.7
	}
	if = {
		limit = { has_trait = honest }
		multiply = 0.7
	}
	if = {
		limit = { has_trait = just }
		multiply = 0.7
	}
	if = {
		limit = { has_trait = trusting }
		multiply = 0.7
	}

	# negative => joining less likely
	if = {
		limit = { has_trait = content }
		multiply = 1.5
	}
	if = {
		limit = { has_trait = craven }
		multiply = 1.5
	}
	if = {
		limit = { has_trait = lazy }
		multiply = 1.5
	}
	if = {
		limit = { has_trait = fickle }
		multiply = 1.5
	}
	if = {
		limit = { has_trait = arbitrary }
		multiply = 1.5
	}
	min = 0
}

vta_joining_chance = {
	value = vta_join_liege
	divide = {
		value = vta_join_liege
		add = vta_dont_join_liege
		min = 1
	}
	multiply = 100
}

vta_defender_military_title_loss = {
	value = 0
	every_in_list = {
		variable = vta_lose_land
		add = max_military_strength
	}
}

vta_defender_military_change_liege = {
	value = 0
	every_in_list = {
		variable = vta_change_liege
		add = max_military_strength
	}
}

vta_attacker_military_claimant = {
	value = 0
	if = {
		limit = { has_variable_list = vta_claimants }
		every_in_list = {
			variable = vta_claimants
			add = max_military_strength
		}
	}
}

vta_select_war_title = {
	value = 0
	if = {
		limit = { has_variable_list = vta_war_target_title }
		add = 1
	}
}

vta_max_military_strength = {
	value = max_military_strength
}

vta_progress_bar_value = {
	value = vta_progress_bar_attacker
	if = {
		limit = { vta_progress_bar_attacker < 0.03 }
		multiply = 160
		subtract = 198
	}
	else_if = {
		limit = { vta_progress_bar_attacker < 1 }
		multiply = 71
		subtract = 171
	}
	else_if = {
		limit = { vta_progress_bar_attacker < 3 }
		multiply = 25
		subtract = 125
	}
	else = {
		multiply = 2.3
		subtract = 57
	}
	min = -190
	max = -10
}

vta_strength_comparison = {
	value = 0
	if = {
		limit = { vta_progress_bar_attacker >= 4 }
		add = -2
	}
	else_if = {
		limit = { vta_progress_bar_attacker > 1.5 }
		add = -1
	}
	else_if = {
		limit = { vta_progress_bar_attacker >= 0.7 }
		add = 0
	}
	else_if = {
		limit = { vta_progress_bar_attacker >= 0.25 }
		add = 1
	}
	else = {
		add = 2
	}
}

vta_strength_comparison_balance_icon = {
	value = 0
	if = {
		limit = { vta_progress_bar_attacker >= 4 }
		add = 5
	}
	else_if = {
		limit = { vta_progress_bar_attacker > 1.5 }
		add = 4
	}
	else_if = {
		limit = { vta_progress_bar_attacker >= 0.7 }
		add = 3
	}
	else_if = {
		limit = { vta_progress_bar_attacker >= 0.25 }
		add = 2
	}
	else = {
		add = 1
	}
}

vta_progress_bar_attacker = {
	value = 0
	# Attacker's militarty power
	add = max_military_strength
	if = {
		limit = {
			OR = {
				has_game_rule = vta_rule_attacker
				has_game_rule = vta_rule_both
			}
		}
		add = vta_attacker_military_claimant
	}
	every_ally = {
		limit = {
			NOT = { is_vassal_or_below_of = PREV }
			# Not vassal of target
			# Not target
		}
		add = max_military_strength
	}
	# Divide defender's militarty power
	divide = {
		value = 0
		if = {
			limit = {
				OR = {
					has_game_rule = vta_rule_defender
					has_game_rule = vta_rule_both
				}
			}
			add = vta_defender_military_title_loss
		}
		if = {
			limit = { exists = var:selected_war_defender }
			var:selected_war_defender = {
				add = max_military_strength
				every_ally = {
					limit = {
						NOT = { is_vassal_or_below_of = PREV }
						# Not vassal of target
						# Not target
					}
					add = max_military_strength
				}
			}
		}
		if = {
			limit = {
				has_game_rule = vta_rule_land_subject
				OR = {
					has_game_rule = vta_rule_defender
					has_game_rule = vta_rule_both
				}
			}
			add = vta_defender_military_change_liege
		}
		min = 1
	}
}

vta_progressbar_defender = {
	value = 0
	add = vta_defender_military_title_loss
	if = {
		limit = { exists = var:selected_war_defender }
		var:selected_war_defender = {
			add = max_military_strength
			every_ally = {
				limit = {
					NOT = { is_vassal_or_below_of = PREV }
					# Not vassal of target
					# Not target
				}
				add = max_military_strength
			}
		}
	}
	if = {
		limit = {
			OR = {
				has_game_rule = vta_rule_land
				has_game_rule = vta_rule_land_subject
			}
		}
		add = vta_defender_military_change_liege
	}
	min = 1
}

vta_progress_bar_icon = {
	value = vta_progress_bar_value
	add = 185
}

vta_order_title_tier = {
	value = 0

}