﻿hre_conquest_ai_score_value = {
	value = 1
	if = {
		limit = {
			any_in_list = {
				list = target_titles
				this = title:d_julich
			}
		}
		multiply = 40
	}
	else_if = {
		limit = {
			OR = {
				AND = {
					scope:attacker.capital_county.title_province = { geographical_region = world_europe_west_germania }
					any_in_list = {
						list = target_titles
						tier = tier_duchy
						title_capital_county.title_province = { geographical_region = world_europe_west_germania }
					}
				}
				AND = {
					scope:attacker.capital_county.title_province = { geographical_region = custom_northern_italy }
					any_in_list = {
						list = target_titles
						tier = tier_duchy
						title_capital_county.title_province = { geographical_region = custom_northern_italy }
					}
				}
				AND = {
					scope:attacker.capital_county.title_province = { geographical_region = custom_carolingian_francia }
					any_in_list = {
						list = target_titles
						tier = tier_duchy
						title_capital_county.title_province = { geographical_region = custom_carolingian_francia }
					}
				}
			}
		}
		multiply = 20
	}
	else_if = {
		limit = {
			any_in_list = {
				list = target_titles
				tier = tier_duchy
				title_capital_county.title_province = { geographical_region = world_europe_west_germania }
			}
		}
		multiply = 10
	}
	else_if = {
		limit = {
			OR = {
				any_in_list = {
					list = target_titles
					tier = tier_duchy
					title_capital_county.title_province = { geographical_region = custom_northern_italy }
				}
				any_in_list = {
					list = target_titles
					tier = tier_duchy
					title_capital_county.title_province = { geographical_region = custom_burgundy }
				}
			}
		}
		multiply = 5
	}
	scope:attacker = {
		if = {
			limit = { dynasty = scope:defender.dynasty }
			multiply = 0.5
		}
		if = {
			limit = {
				OR = {
					NOT = { faith = scope:defender.faith }
					has_relation_rival = scope:defender
				}
			}
			multiply = 5
		}
	}
}

total_tribal_conversion_cost_value = {
	value = 0
	scope:possible_county = {
		duchy = {
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
					any_county_province = { has_holding_type = tribal_holding }
				}
				add = 500
			}
		}
	}
}

sub_realm_size_plus_10 = {
	value = sub_realm_size
	add = 10
}

sub_realm_size_plus_20 = {
	value = sub_realm_size
	add = 20
}

military_strength_inverted_hre_score = {
	value = 0
	subtract = strength_with_relevant_allies_vs_root_value
	if = {
		limit = {
			any_realm_county = {
				title_province = {
					OR = {
						geographical_region = world_europe_west_germania
						geographical_region = custom_northern_italy
					}
				}
			}
		}
		multiply = 0.5
	}
}

military_strength_inverted_score = {
	value = 0
	subtract = strength_with_relevant_allies_vs_root_value
}

strength_with_relevant_allies_vs_root_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = root
				}
			}
		}
		add = {
			add = current_military_strength
			if = {
				limit = { is_at_war = yes }
				multiply = 0.25
			}
			else = {
				multiply = 0.75
			}
		}
	}
}

strength_with_relevant_allies_vs_war_attacker_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = scope:attacker
				}
			}
		}
		add = {
			add = current_military_strength
			if = {
				limit = { is_at_war = yes }
				multiply = 0.50
			}
		}
	}
}

strength_with_relevant_allies_vs_war_defender_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			is_at_war = no
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = scope:defender
				}
			}
		}
		add = {
			add = current_military_strength
			multiply = 0.75
		}
	}
}

histmod_war_strength_difference_value = {
	value = 0
	add = scope:defender.strength_with_relevant_allies_vs_war_attacker_value
	subtract = scope:attacker.strength_with_relevant_allies_vs_war_defender_value
	min = 0
}

strength_with_relevant_allies_vs_england_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			is_at_war = no
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = title:k_england.holder
				}
			}
		}
		add = {
			add = current_military_strength
			multiply = 0.75
		}
	}
}

strength_with_relevant_allies_vs_root_seventy_five_percent_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = root
				}
			}
		}
		add = {
			add = current_military_strength
			if = {
				limit = { is_at_war = yes }
				multiply = 0.25
			}
			else = {
				multiply = 0.75
			}
		}
	}
	multiply = 0.75
}

strength_with_relevant_allies_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			OR = {
				exists = global_var:miv_is_loaded
				NOT = { target_is_liege_or_above = prev }
			}
		}
		add = {
			add = current_military_strength
			if = {
				limit = { is_at_war = yes }
				multiply = 0.25
			}
			else = {
				multiply = 0.75
			}
		}
	}
}

one_and_a_half_times_current_strength_with_relevant_allies_vs_root = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = root
				}
			}
		}
		add = {
			add = current_military_strength
			if = {
				limit = { is_at_war = yes }
				multiply = 0.25
			}
			else = {
				multiply = 0.75
			}
		}
	}
	multiply = 1.50
}

double_current_strength_with_relevant_allies_vs_root = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = root
				}
			}
		}
		add = {
			add = current_military_strength
			if = {
				limit = { is_at_war = yes }
				multiply = 0.25
			}
			else = {
				multiply = 0.75
			}
		}
	}
	multiply = 2
}

strength_with_relevant_allies_vs_target_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			is_at_war = no
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = scope:target
				}
			}
		}
		add = {
			add = current_military_strength
			multiply = 0.75
		}
	}
}

strength_with_relevant_allies_vs_target_character_value = {
	value = current_military_strength
	every_ally = {
		limit = {
			is_ai = yes
			is_at_war = no
			OR = {
				exists = global_var:miv_is_loaded
				NOR = {
					target_is_liege_or_above = prev
					target_is_liege_or_above = scope:target_character
				}
			}
		}
		add = {
			add = current_military_strength
			multiply = 0.75
		}
	}
}

mogyer_summoned_host_troops = {
	value = 0
	culture:mogyer = {
		every_culture_county = {
			limit = { holder.house = root.house }
			add = {
				value = 100
				if = {
					limit = { has_game_rule = extra_extra_mogyer_forces }
					multiply = 2
				}
			}
		}
	}
	if = {
		limit = { highest_held_title_tier = tier_duchy }
		add = 100
	}
	else_if = {
		limit = { highest_held_title_tier = tier_kingdom }
		add = 200
	}
	else_if = {
		limit = { highest_held_title_tier = tier_empire }
		add = 300
	}
	max = {
		value = 1000
		if = {
			limit = { has_game_rule = extra_extra_mogyer_forces }
			add = 1000
		}
	}
}

mogyer_summoned_host_stacks = {
	value = 0
	culture:mogyer = {
		every_culture_county = {
			limit = { holder.house = root.house }
			add = {
				value = 1
				if = {
					limit = { has_game_rule = extra_extra_mogyer_forces }
					multiply = 2
				}
			}
		}
	}
	if = {
		limit = { highest_held_title_tier = tier_duchy }
		add = 1
	}
	else_if = {
		limit = { highest_held_title_tier = tier_kingdom }
		add = 2
	}
	else_if = {
		limit = { highest_held_title_tier = tier_empire }
		add = 3
	}
	max = {
		value = 10
		if = {
			limit = { has_game_rule = extra_extra_mogyer_forces }
			add = 10
		}
	}
}

norman_summoned_bannermen_stacks = {
	value = 0
	culture = {
		every_culture_county = {
			limit = {
				OR = {
					holder = root
					holder = {
						culture = root.culture
						target_is_liege_or_above = root
					}
				}
			}
			add = 1
		}
	}
	if = {
		limit = { has_government = landless_adventurer_government }
		add = 2
	}
	if = {
		limit = { highest_held_title_tier = tier_duchy }
		add = 1
	}
	else_if = {
		limit = { highest_held_title_tier = tier_kingdom }
		add = 2
	}
	else_if = {
		limit = { highest_held_title_tier = tier_empire }
		add = 3
	}
	max = {
		value = prestige_level
		multiply = 2
	}
}

num_aligned_vassals = {
	value = 0
	every_vassal = {
		limit = {
			is_physically_able_ai_adult = yes
			highest_held_title_tier >= tier_county
			capital_county.kingdom = liege.primary_title
			OR = {
				dynasty = liege.dynasty
				has_relation_friend = liege
				has_relation_friend = liege.primary_heir
				is_allied_to = liege
				is_allied_to = liege.primary_heir
				any_close_family_member = { is_hostage_of = prev.liege }
				AND = {
					exists = liege.faith.religious_head
					this = liege.faith.religious_head
				}
			}
		}
		if = {
			limit = { is_powerful_vassal = yes }
			add = 2
		}
		else = {
			add = 1
		}
	}
}

num_unaligned_vassals = {
	value = 0
	every_vassal = {
		limit = {
			is_physically_able_ai_adult = yes
			highest_held_title_tier >= tier_county
			capital_county.kingdom = liege.primary_title
			NOR = {
				dynasty = liege.dynasty
				has_relation_friend = liege
				has_relation_friend = liege.primary_heir
				is_allied_to = liege
				is_allied_to = liege.primary_heir
				any_close_family_member = { is_hostage_of = prev.liege }
				AND = {
					exists = liege.faith.religious_head
					this = liege.faith.religious_head
				}
			}
		}
		if = {
			limit = { is_powerful_vassal = yes }
			add = 2
		}
		else = {
			add = 1
		}
	}
}

# for scandi adventurer families/courts
scandi_wife_max_age = {
	value = scope:scandi_adventurer.age
	subtract = 5
	min = 20
	max = 35
}

scandi_child_max_age = {
	value = scope:scandi_adventurer.primary_spouse.age
	subtract = 16
	min = 1
	max = 20
}

scandi_child_adventurer_max_age = {
	value = scope:scandi_adventurer.age
	subtract = 16
	min = 1
	max = 20
}

scandi_commander_female_chance = {
	if = {
		limit = {
			OR = {
				scope:new_ruler.culture = { has_cultural_parameter = martial_custom_female_only_combatant }
				scope:new_ruler.faith = { has_doctrine_parameter = combatant_must_be_female_if_no_roco }
			}
		}
		value = 100
	}
	else_if = {
		limit = {
			OR = {
				scope:new_ruler.culture = { has_cultural_parameter = martial_custom_equal_combatant }
				scope:new_ruler.faith = { has_doctrine_parameter = combatant_can_be_either_gender_if_no_roco }
			}
		}
		value = 50
	}
	else_if = {
		limit = {
			scope:new_ruler.culture = { has_cultural_parameter = has_access_to_shieldmaidens }
		}
		value = 20
	}
	else = {
		value = 0
	}
}

scandi_laamp_deduction = {
	value = max_military_strength
	subtract = 1000
	min = 0
	max = 1500
}

# giving the max scandi adventurer host totals some variability
scandi_levy_max = {
	value = 2500
	# landless adventurers get a free 1000 of their own forces before we start reducing their bonus spawns
	# non-player adventurers who are prioritised (like Rollo) don't get this reduction
	if = {
		limit = {
			has_government = landless_adventurer_government
			OR = {
				is_ai = no
				NOT = { has_character_flag = prioritised_scandinavian_adventurer }
			}
		}
		add = {
			value = scandi_laamp_deduction
			multiply = -1
		}
	}
	# add value based based on martial and prowess
	add = {
		value = martial
		add = prowess
		multiply = 100
		max = 7000
	}
	add = {
		value = diplomacy
		multiply = 50
		max = 1500
	}
	if = {
		limit = { has_trait = ambitious }
		add = 200
	}
	if = {
		limit = { has_trait = brave }
		add = 200
	}
	if = {
		limit = { has_trait = arrogant }
		add = -200
	}
	if = {
		limit = { has_trait = stubborn }
		add = -200
	}
	if = {
		limit = { has_trait = impatient }
		add = -200
	}
	if = {
		limit = { has_trait = strategist }
		add = 200
	}
	every_character_trait = {
		limit = { has_trait_category = commander }
		add = 500
	}
	if = {
		limit = { has_trait = viking }
		add = 300
	}
	if = {
		limit = { has_trait = berserker }
		add = 300
	}
	if = {
		limit = { has_trait = shieldmaiden }
		add = 300
	}
	if = {
		limit = { has_trait = varangian }
		add = 500
	}
	if = {
		limit = {
			OR = {
				scope:adventurer_target.holder.top_liege = { is_ai = no } #we still give players a break
				scope:scandi_adventurer = { is_ai = no } #unless they're the attacker
			}
		}
		max = 8000
	}
	else = {
		max = 12000
	}
	min = 5000
}

scandi_maa_after = {
	value = 0
	every_maa_regiment = {
		add = 1
	}
}

scandi_maa_full_after = {
	value = 0
	every_maa_regiment = {
		add = maa_size
	}
}

scandi_maa_min = {
	value = scope:adventurer_target.holder.top_liege.primary_title.tier
	multiply = 3
}

scandi_maa_total = {
	value = 0
	scope:adventurer_target.holder.top_liege = {
		# total their maa regiment sizes
		every_maa_regiment = {
			add = maa_size
		}
		# total maa regiment sizes of allies, halved
		every_ally = {
			limit = {
				NOT = { top_liege ?= scope:adventurer_target.holder.top_liege }
			}
			every_maa_regiment = {
				add = {
					value = maa_size
					multiply = 0.5
				}
			}
		}
	}
	# as holy orders start to make their appearance, add a few more maa for each one available
	if = {
		limit = {
			NOT = { faith = scope:adventurer_target.holder.top_liege.faith }
		}
		scope:adventurer_target.holder.top_liege = {
			every_faith_holy_order = {
				limit = {
					OR = {
						holy_order_patron = scope:adventurer_target.holder.top_liege
						leader = { is_at_war = no }
					}
				}
				add = 3
			}
		}
	}
	# add some variance for stronger/weaker invasions
	if = {
		limit = { scandi_levy_max > 10000 }
		multiply = 1.25
	}
	else_if = {
		limit = { scandi_levy_max <= 8000 }
		multiply = 0.75
	}
	floor = yes
	min = scandi_maa_min
}

# making it so very large adventurer hosts aren't super large after victory
scandi_levy_total = {
	value = scope:adventurer_target.holder.top_liege.max_military_strength
	scope:adventurer_target.holder.top_liege = {
		every_ally = {
			limit = {
				OR = {
					exists = global_var:miv_is_loaded
					NOT = { top_liege ?= scope:adventurer_target.holder.top_liege }
				}
			}
			add = this.max_military_strength
		}
	}
	multiply = 1.5
	#Cut it off so things don't get too ridiculous
	max = {
		value = scope:scandi_adventurer.scandi_levy_max
		#Subtract any excess that goes into maa regiments
		if = {
			limit = { scandi_maa_total > scandi_maa_min }
			subtract = {
				value = scandi_maa_total
				subtract = scandi_maa_min
				multiply = 100
			}
		}
	}
	#Make sure all adventurers have a moderately respectable force.
	min = 1000
}

# these are from vanilla and over-written because they're clearly wrong
current_strength_seventy_five_percent_value = {
	value = current_military_strength
	multiply = 0.75
}

current_strength_with_allies_seventy_five_percent_value = {
	value = current_military_strength
	every_ally = { add = current_military_strength }
	multiply = 0.75
}

