﻿### EK NOTE: Removed reference to HRE
### EK NOTE: I messed with the HRE... CASUS BELLI

#######################################
# 			   AI Tweaks			  #
# For a better challenge, a better AI #
#######################################
ai_budget_manipulation_effect = {
	# Tweaks to the War Chest AI budget - sometimes the AI will be brave enough to boom
	
	if = { # Some special logic to make certain AI's save up gold to revoke titles in their desired domain
		limit = {
			is_ai = yes
			is_at_war = no
			is_playable_character = yes
			gold >= 50
			gold < minimum_ai_gold_value_for_tyranny_wars
			NOR = {
				has_character_flag = ai_revoke_recheck_cooldown
				government_has_flag = government_is_nomadic
			}
			primary_title.tier >= tier_duchy
			years_from_game_start >= 3
			domain_size < domain_limit
			has_realm_law_flag = title_revocation_allowed
			OR = { # Tyranny tolerance is based on rationality
				AND = {
					ai_rationality < 0
					tyranny <= 15 # Don't go crazy with tyranny, just a bit is fine
				}
				AND = {
					ai_rationality >= 0
					ai_rationality < 50
					tyranny <= 5 # Don't go crazy with tyranny, just a bit is fine
				}
				AND = {
					ai_rationality >= 50
					tyranny <= 0 # Don't go crazy with tyranny, just a bit is fine
				}
			}
			OR = {
				primary_title.title_capital_county = {
					NOT = {
						holder = root
					}
					holder = {
						current_military_strength < root.current_military_strength
						is_vassal_or_below_of = root
						NOR = {
							vassal_contract_has_flag = vassal_contract_cannot_revoke_titles
							has_relation_friend = root
							has_relation_best_friend = root
							has_relation_lover = root
							has_relation_soulmate = root
							is_child_of = root
							is_grandchild_of = root
							is_great_grandchild_of = root
						}
					}
				}
				AND = {
					primary_title.tier >= tier_kingdom
					exists = primary_title.title_capital_county.de_jure_liege.holder
					primary_title.title_capital_county.de_jure_liege = {
						NOT = {
							holder = root
						}
						holder = {
							current_military_strength < root.current_military_strength
							is_vassal_or_below_of = root
							NOR = {
								vassal_contract_has_flag = vassal_contract_cannot_revoke_titles
								has_relation_friend = root
								has_relation_best_friend = root
								has_relation_lover = root
								has_relation_soulmate = root
								is_child_of = root
								is_grandchild_of = root
								is_great_grandchild_of = root
							}
						}
					}
				}
				any_sub_realm_county = {
					NOT = {
						holder = root
					}
					title_province = { has_holding_type = castle_holding }
					NOT = {
						this = root.primary_title.title_capital_county
					}
					duchy = root.primary_title.title_capital_county.duchy
					holder = {
						current_military_strength < root.current_military_strength
						NOR = {
							vassal_contract_has_flag = vassal_contract_cannot_revoke_titles
							has_relation_friend = root
							has_relation_best_friend = root
							has_relation_lover = root
							has_relation_soulmate = root
							is_child_of = root
							is_grandchild_of = root
							is_great_grandchild_of = root
						}
					}
				}
			}
			trigger_if = {
				limit = {
					domain_size > 3
				}
				NOR = {
					has_trait = generous
					has_trait = just
				}
			}
			#EK DISABLED
			#NOT = {
			#	primary_title = title:e_hre
			#}
		}
		if = {
			limit = {
				primary_title.title_capital_county = {
					NOT = {
						holder = root
					}
					holder = {
						current_military_strength < root.current_military_strength
						is_vassal_or_below_of = root
						NOT = { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles }
					}
				}
			}
			add_character_flag = { flag = ai_save_gold_for_revoke years = 6 }
		}
		else_if = {
			limit = {
				primary_title.title_capital_county = {
					holder = root
					title_province = { free_building_slots <= 0 }
				}
			}
			add_character_flag = { flag = ai_save_gold_for_revoke years = 6 }
		}
		else = {
			random_list = {
				50 = {
					add_character_flag = { flag = ai_save_gold_for_revoke years = 6 }
				}
				50 = {
					add_character_flag = { flag = ai_revoke_recheck_cooldown years = 6 }
				}
			}
		}
	}
	else_if = {
		limit = {
			is_ai = yes
			is_playable_character = yes
			primary_title.tier >= tier_duchy
			years_from_game_start >= 3
			NOR = {
				has_character_flag = ai_revoke_recheck_cooldown
				has_character_flag = ai_save_gold_for_revoke
				government_has_flag = government_is_nomadic
			}
		}
		add_character_flag = { flag = ai_revoke_recheck_cooldown years = 6 }
	}
	
	if = { # Conquerors are special; they don't care about states or eras - they just want to conquer, and put their resources towards just that.
		limit = {
			ai_has_conqueror_personality = yes
		}
		# For now they just buff the war chest, and nothing else
		move_budget_gold = { gold = long_term_gold from = budget_long_term to = budget_war_chest }
		move_budget_gold = { gold = short_term_gold from = budget_short_term to = budget_war_chest }
		# Conquerors have no concern for the future; only here and now matters
		move_budget_gold = { gold = long_term_gold from = budget_long_term to = budget_short_term }
	}
	else_if = { # Adventurers only need short term gold.
		limit = {
			is_ai = yes
			is_landless_adventurer = yes
		}
		move_budget_gold = { gold = long_term_gold from = budget_long_term to = budget_short_term }
		move_budget_gold = { gold = war_chest_gold from = budget_war_chest to = budget_short_term }
	}
	else = {
		if = {
			limit = {
				is_ai = yes
				is_at_war = no # While at war, the War Chest should be preserved at all costs
				is_playable_character = yes
				gold > 0
				war_chest_gold >= 10
				NOR = {
					has_character_flag = ai_save_gold_for_revoke
					any_targeting_faction = {
						NOT = { faction_is_type = peasant_faction }
						faction_power >= halved_faction_power_threshold
					}
				}
				trigger_if = {
					limit = {
						government_has_flag = government_is_tribal
					}
					prestige >= expensive_building_tier_1_cost
				}
				NAND = { # Iberian Struggle rulers that go for escalation should always go for a full war chest
					any_character_struggle = {
						phase_has_catalyst = catalyst_winning_large_conquest_war
					}
					has_character_flag = agenda_towards_escalation
				}
			}
			
			# Chance to make an AI boom its economy if they do not fall into an archetype
			if = {
				limit = {
					ai_has_economical_boom_personality = no
					ai_has_warlike_personality = no
					ai_has_cautious_personality = no
					NOT = {
						has_character_flag = ai_boom_cooldown
					}
				}
				random = {
					chance = {
						value = primary_title.tier
						if = {
							limit = {
								has_trait = diligent
							}
							add = 1
						}
						if = {
							limit = {
								has_trait = architect
							}
							add = 4
						}
					}
					add_character_flag = { flag = ai_boom years = 15 }
					add_character_flag = { flag = ai_boom_cooldown years = 30 } # To balance the AI out
				}
			}
			
			# Early Game, there are still building slots to fill
			# At this point, the AI will boom more aggressively than normal
			if = {
				limit = {
					OR = {
						any_held_title = {
							tier = tier_county
							title_province = { free_building_slots > 0 }
						}
						ai_should_focus_on_building_in_their_capital = yes
					}
					ai_has_warlike_personality = no # Warlike AI's go straight to the more restrictive second set of conditions
				}
				# Early Game - Capital Focus
				# AI's want to fill up their Capital County's building slots, first level buildings have the highest RoI so it's stupid not to
				# There's no guarantee that they will spend the gold on buildings for specifically their capital, but they are heavily incentivised to do so
				# They may spend on buildings in secondary counties/baronies, or on MaA
				if = {
					limit = {
						capital_county.title_province = {
							free_building_slots > 0
						}
						ai_has_warlike_personality = no
						ai_has_cautious_personality = no
					}
					move_budget_gold = { gold = war_chest_gold from = budget_war_chest to = budget_short_term }
				}
				else_if = { # Cautious AI's keep a half of their war chest as a buffer
					limit = {
						capital_county.title_province = {
							free_building_slots > 0
						}
						ai_has_cautious_personality = yes
						war_chest_gold >= halved_ai_war_chest_gold_maximum
					}
					move_budget_gold = { gold = excess_over_halved_ai_war_chest_gold from = budget_war_chest to = budget_short_term }
				}
				
				# Early Game - Building up Secondary Counties
				# The AI wants first-level buildings in all of its Counties
				# This is when personalities will begin to manifest in a bigger way
				# The else_if is intended, it's a continuation of the above logic
				else_if = { # Economical boom AI's just go for it, hard
					limit = {
						ai_has_economical_boom_personality = yes
					}
					move_budget_gold = { gold = war_chest_gold from = budget_war_chest to = budget_short_term }
				}
				else_if = { # Cautious AI's will want a minimum war chest in absolute terms, based on tier
					limit = {
						war_chest_gold > cautious_ai_minimum_war_chest_gold
						ai_has_cautious_personality = yes
					}
					move_budget_gold = { gold = excess_over_cautious_ai_minimum_war_chest_gold from = budget_war_chest to = budget_short_term }
				}
				# Warlike AI's have a few conditions
				# If they have a full war chest and have been at peace for 3 years, they'll stuff half their war chest into short term
				# This is a somewhat close representation of if they want to declare war
				# But it's not a particularly aggressive cooldown, as we do want AI's to build up a minimum, even if they are warlike
				else_if = {
					limit = {
						ai_has_warlike_personality = yes
						war_chest_gold >= war_chest_gold_maximum
						days_of_continuous_peace >= 1825 # 5 years
					}
					move_budget_gold = { gold = excess_over_a_quarter_ai_war_chest_gold from = budget_war_chest to = budget_short_term }
				}
				# If no personality archetype is present, AI's with the ai_boom flag, appropriate traits, or decent boldness will boom
				else_if = {
					limit = {
						OR = {
							has_character_flag = ai_boom
							ai_boldness >= 25
							has_trait = diligent
							has_trait = architect
						}
					}
					if = { # If you have enough war chest gold and short term gold to build a holding, and doing so would be smart, do it!
						limit = {
							domain_size < domain_limit
							gold >= main_building_tier_1_cost
							long_term_gold < main_building_tier_1_cost
							NOT = {
								government_has_flag = government_is_tribal
							}
							culture = { # The requirements for Castles, temples, and Cities
								has_innovation = innovation_motte
								has_innovation = innovation_city_planning
							}
							any_held_title = {
								tier = tier_county
								any_county_province = {
									has_holding = no
									barony_cannot_construct_holding = no
								}
							}
						}
						move_budget_gold = { gold = main_building_tier_1_cost from = budget_war_chest to = budget_long_term }
						move_budget_gold = { gold = main_building_tier_1_cost from = budget_short_term to = budget_long_term }
					}
					else = {
						move_budget_gold = { gold = war_chest_gold from = budget_war_chest to = budget_short_term }
					}
				}
			}
			
			# Mid-Late game, there are no more building slots to fill
			# Heavily personality based, with some quirks here and there
			# The else_if is intended, it's a continuation of the above logic
			else_if = { # Economical boom AI's will empty their entire treasury to boom
				limit = {
					ai_has_economical_boom_personality = yes
				}
				if = { # If you have enough war chest gold and short term gold to build a holding, and doing so would be smart, do it!
					limit = {
						domain_size < domain_limit
						gold >= main_building_tier_1_cost
						long_term_gold < main_building_tier_1_cost
						NOT = {
							government_has_flag = government_is_tribal
						}
						culture = { # The requirements for Castles, temples, and Cities
							has_innovation = innovation_motte
							has_innovation = innovation_city_planning
						}
						any_held_title = {
							tier = tier_county
							any_county_province = {
								has_holding = no
								barony_cannot_construct_holding = no
							}
						}
					}
					move_budget_gold = { gold = main_building_tier_1_cost from = budget_war_chest to = budget_long_term }
					move_budget_gold = { gold = main_building_tier_1_cost from = budget_short_term to = budget_long_term }
				}
				else = {
					move_budget_gold = { gold = war_chest_gold from = budget_war_chest to = budget_short_term }
				}
			}
			else_if = { # Cautious AI's will only boom if they feel really, really safe
				limit = {
					war_chest_gold > cautious_ai_minimum_war_chest_gold
					ai_has_cautious_personality = yes
					current_military_strength >= max_military_strength
					days_of_continuous_peace >= 1825 # 5 years
					OR = {
						any_ally = {
							count >= 2
						}
						dread >= 50 # Dread makes cautious characters feel safe
					}
				}
				move_budget_gold = { gold = excess_over_cautious_ai_minimum_war_chest_gold from = budget_war_chest to = budget_short_term }
			}
			else_if = { # Warlike AI's will invest half their war chest if it's full, and nothing is going on
				limit = {
					ai_has_warlike_personality = yes
					war_chest_gold >= war_chest_gold_maximum
					NOT = {
						government_has_flag = government_is_nomadic
					}
					capital_county.title_province = {
						free_building_slots <= 0
					}
					days_of_continuous_peace >= 2555 # 7 years
				}
				move_budget_gold = { gold = halved_ai_war_chest_gold from = budget_war_chest to = budget_short_term }
			}
			else_if = { # Special logic for warlike AI's during the Iranian Intermezzo, they need to be more aggressive than usual
				limit = {
					has_trait = fp3_struggle_detractor
					ai_has_warlike_personality = yes
					war_chest_gold < war_chest_gold_maximum
					days_of_continuous_peace < 2555
				}
				move_budget_gold = { gold = short_term_gold from = budget_short_term to = budget_war_chest }
				move_budget_gold = { gold = long_term_gold from = budget_long_term to = budget_war_chest }
			}
			else_if = { # Warlike AI's will invest their long term gold into their war chest if it's not full
				limit = {
					ai_has_warlike_personality = yes
					war_chest_gold < war_chest_gold_maximum
					long_term_gold >= 1
				}
				move_budget_gold = { gold = long_term_gold from = budget_long_term to = budget_war_chest }
			}
			
			# For AI's with no particular personality, warlike, booming, or cautious, we have them boom through a flag system
			# Bold AI's boom with all their war chest gold, non-bold with half
			else_if = {
				limit = {
					has_character_flag = ai_boom
					ai_boldness >= 25
				}
				if = { # If you have enough war chest gold and short term gold to build a holding, and doing so would be smart, do it!
					limit = {
						domain_size < domain_limit
						gold >= main_building_tier_1_cost
						long_term_gold < main_building_tier_1_cost
						NOT = {
							government_has_flag = government_is_tribal
						}
						culture = { # The requirements for Castles, temples, and Cities
							has_innovation = innovation_motte
							has_innovation = innovation_city_planning
						}
						any_held_title = {
							tier = tier_county
							any_county_province = {
								has_holding = no
								barony_cannot_construct_holding = no
							}
						}
					}
					move_budget_gold = { gold = main_building_tier_1_cost from = budget_war_chest to = budget_long_term }
					move_budget_gold = { gold = main_building_tier_1_cost from = budget_short_term to = budget_long_term }
				}
				else = {
					move_budget_gold = { gold = war_chest_gold from = budget_war_chest to = budget_short_term }
				}
			}
			else_if = {
				limit = {
					has_character_flag = ai_boom
					war_chest_gold >= halved_ai_war_chest_gold_maximum
				}
				move_budget_gold = { gold = excess_over_halved_ai_war_chest_gold from = budget_war_chest to = budget_short_term }
			}
			
			if = { # Some special logic to catch the rare instances where a non-Tribe ends up with a tribal main holding
				limit = {
					NOT = {
						government_has_flag = government_is_tribal
					}
					capital_province = {
						has_holding_type = tribal_holding
					}
					long_term_gold < feudalize_holding_interaction_cost
				}
				move_budget_gold = { gold = feudalize_holding_interaction_cost from = budget_war_chest to = budget_long_term }
				move_budget_gold = { gold = feudalize_holding_interaction_cost from = budget_short_term to = budget_long_term }
			}
		}
	}
}
