﻿# Get the country's GDP per capita.
#
# Input Scopes: country
kai_gdp_per_capita = {
	value = gdp
	divide = total_population
}

# Get the country's treasury income normalized by tax level.
#
# Input Scopes: country
kai_country_income_normalized = {
	value = fixed_income

	# Tax level
	divide = {
		value = 1.0
		if = {
			limit = {
				tax_level = very_high
			}
			add = 0.3
		}
		else_if = {
			limit = {
				tax_level = high
			}
			add = 0.15
		}
		else_if = {
			limit = {
				tax_level = low
			}
			add = -0.15
		}
		else_if = {
			limit = {
				tax_level = very_low
			}
			add = -0.3
		}
	}
}

# Estimates how much money is being spent on bureaucracy
# The actual cost per unit typically ranges from 12 to 15
#
# Input Scopes: country
kai_estimated_bureaucracy_cost = {
	value = produced_bureaucracy
	subtract = bureaucracy
	subtract = 100 # Base value
	# # (365 wage + 600 cost of paper) / 65 bureaucracy = 14.8
	multiply = 15
	min = 0
}

# How much income is generated per incorporated state population
# This can be used to gauge the effectiveness of taxation, using poll taxes (0.7 pounds annually per working adult) as reference
#
# Input Scopes: country
kai_income_efficiency = {
	value = kai_country_income_normalized
	multiply = 208 # 52 weeks, 25% taxable population
	divide = {
		value = cached_ai_incorporated_population
		min = 1 # Avoid divide by zero
	}
}

# What the real interest rate is
#
# Input Scopes: country
kai_interest_rate = {
	value = modifier:country_loan_interest_rate_add
	multiply = {
		value = 1
		add = modifier:country_loan_interest_rate_mult
	}
}

REPLACE:wanted_army_size_script_value = {
	# How many levels of barracks should the AI have
	# Using this value in other strategies will function additively
	value = 0

	# To increase base army size for small countries, capped at 1 million population
	add = {
		value = cached_ai_incorporated_population
		divide = 250000 # 1 level of barracks per 250k incorporated population

		max = 4
	}

	add = {
		value = cached_ai_incorporated_population
		divide = 250000 # 1 level of barracks per 250k incorporated population
	}

	add = {
		value = cached_ai_unincorporated_population
		divide = 1000000 # 1 level of barracks per 1 million unincorporated population
	}

	# Extra army size from rank
	if = {
		limit = { country_rank = rank_value:great_power }
		add = 25
	}
	if = {
		limit = { country_rank = rank_value:major_power }
		add = 10
	}

	save_temporary_value_as = pre_overseas_empire_value

	# Countries with a huge overseas subject population should focus more on navy
	subtract = {
		value = cached_ai_overseas_subject_population
		divide = 50000000
		max = {
			value = scope:pre_overseas_empire_value
			divide = 2
		}
	}

	max = {
		value = num_states
		multiply = 9
	}


	if = {
		limit = {
			cached_ai_incorporated_population > 0
		}

		min = {
			value = kai_country_income_normalized
			multiply = {
				value = 1
				subtract = { # Coastal population reduces spending on army as that spending is needed for navy too
					value = cached_ai_incorporated_coastal_population
					divide = cached_ai_incorporated_population
					divide = 4
				}
			}
			divide = 2100 # 1 level of barracks per 2.5k income
		}
	}

	# More advanced standing armies are expensive and should be smaller relative to population
	if = {
		limit = { has_technology_researched = general_staff }
		multiply = 0.8
	}

	if = {
		limit = { has_technology_researched = trench_works }
		multiply = 0.8
	}

	if = {
		limit = { has_technology_researched = nco_training }
		multiply = 0.8
	}

	if = {
		limit = { has_technology_researched = mobile_armor }
		multiply = 0.8
	}
	
	# General multiplier for easy adjustment
	multiply = 1.0

	# Factor in strategies
	if = {
		limit = {
			OR = {
				has_strategy = ai_strategy_territorial_expansion
				has_strategy = ai_strategy_unify_italy
				has_strategy = ai_strategy_unify_ethiopia
				has_strategy = ai_strategy_unify_china
			}
		}
		multiply = 1.25
	}
	else_if = {
		limit = {
			has_strategy = ai_strategy_unify_germany
		}
		multiply = 2.0
	}

	if = {
		limit = { has_law_or_variant = law_type:law_national_militia }
		multiply = 0.33
	}

	if = {
		limit = {
			OR = {
				has_journal_entry = je_prussia_of_the_balkans
				has_strategy = ai_strategy_prussia_of_the_balkans
			}
		}
		multiply = 2.5
	}

	if = {
		limit = {
			has_journal_entry = je_lands_of_anarchy
		}
		multiply = 1.5 # More eager to enlarge army in general during the makhzen je
	}
	
	# Multiply final value by diplomatic pact expenses ratio - countries paying a lot to pacts have lower spending ambitions
	multiply = {
		value = 1
		subtract = diplomatic_pact_expense_ratio
	}

	min = {
		value = 1

		if = {
			limit = {
				has_journal_entry = je_sick_man_army
			}
			value = 125
		}
		if = { #if Mon is raiding, troops pay for themselves and are the countries main source of income
			limit = {
				has_journal_entry = je_montenegrin_raiding
			}
			value = 20
		}
		if = {
			limit = {
				has_journal_entry = je_prussia_of_the_balkans
			}
			value = 35
		}
		if = {
			limit = {
				OR = {
					has_journal_entry = je_modernizing_paraguay
					has_journal_entry = je_expanding_paraguay
				}
			}
			value = 20
		}

		if = { #If accepted Negotiation quest, build barracks until JE ends
			limit = {
				has_journal_entry = je_negotiate_army_quest
			}
			add = {
				every_interest_group = { #first get the desired app from the IG
					limit = {
						has_variable = desired_army_size
					}
					add = var:desired_army_size
				}
				divide = average_barracks_power_projection #divide by the amount of barracks this will need based on average barracks power projection
				multiply = 1.10 #safety margin
			}
		}
	}
}

REPLACE:wanted_navy_size_script_value = {
	value = 0

	# 1 ship per 500k coastal incorporated population
	add = {
		value = cached_ai_incorporated_coastal_population
		divide = 500000
	}
	# 1 ship per 2 million coastal unincorporated population
	add = {
		value = cached_ai_unincorporated_coastal_population
		divide = 2000000
	}
	# 1 ship per 10 million population in direct overseas subjects
	add = {
		value = cached_ai_overseas_subject_population
		divide = 10000000
	}
	max = {
		value = num_states
		multiply = 4.5
	}
	# 1 ship per 5k income, multiplied by relative incorporated coastal population to total incorporated population
	if = {
		limit = {
			cached_ai_incorporated_population > 0
			cached_ai_incorporated_coastal_population > 0
		}
		min = {
			value = kai_country_income_normalized
			multiply = {
				value = cached_ai_incorporated_coastal_population
				divide = cached_ai_incorporated_population
				if = { # GPs and MPs should want to maintain a bit of a navy
					limit = { country_rank = rank_value:great_power }
					min = 0.25
				}
				else_if = {
					limit = { country_rank = rank_value:major_power }
					min = 0.10
				}
			}
			divide = 4200
		}
	}
	
	# General multiplier for easy adjustment
	multiply = 0.75
	
	# More advanced ships require more crew
	if = {
		limit = { has_technology_researched = ironclad_tech }
		multiply = 1.15
	}
	if = {
		limit = { has_technology_researched = dreadnought_tech }
		multiply = 1.25
	}
	if = {
		limit = { has_technology_researched = battleship_tech }
		multiply = 1.25
	}

	# Journal related AI behaviours, choose this if you DONT care about the exact number of ships
	if = {
		limit = {
			has_journal_entry = je_reconquista
		}
		multiply = 1.25
	}

	# Factor in strategies
	if = {
		limit = {
			OR = {
				has_strategy = ai_strategy_colonial_expansion
				has_strategy = ai_strategy_economic_imperialism
			}
		}
		multiply = 1.15
	}
	# KAI: Check the laws for isolationism instead of strategy
	if = {
		limit = { has_law_or_variant = law_type:law_isolationism }
		multiply = 0.5
	}
	# KAI: Similar to national militia, this replaces the country rank factors
	if = {
		limit = { has_law_or_variant = law_type:law_merchant_navy }
		multiply = 0.66
	}
	
	# Multiply final value by diplomatic pact expenses ratio - countries paying a lot to pacts have lower spending ambitions
	multiply = {
		value = 1
		subtract = diplomatic_pact_expense_ratio
	}	

	# Journal related AI behaviours, choose this if you DO care about the exact number of ships
	if = {
		limit = {
			has_journal_entry = je_hawaii
		}
		min = 25
	}
}
