﻿ai_histmod_conqueror_yearly_effect = {
	if = {
		limit = {
			ai_has_conqueror_personality = yes
			top_liege = this
		}
		save_scope_as = root_scope
		
		###### BUDGETING
		if = {
			limit = {
				is_at_war = no
				years_from_game_start >= 5
				NOT = { has_character_flag = conqueror_anti_pacing_flag }
				primary_title = { title_held_years >= 3 } # Do not go into buildup mode if there's a brother you need to destroy immediately!
				OR = {
					any_neighboring_and_across_water_top_liege_realm_owner = {
						count = all
						current_military_strength >= scope:root_scope.max_military_strength
						NOR = {
							is_allied_to = scope:root_scope
							has_relation_friend = scope:root_scope
							has_relation_lover = scope:root_scope
						}
					}
					any_neighboring_and_across_water_top_liege_realm_owner = {
						count >= 2
						current_military_strength >= scope:root_scope.max_military_strength
						NOR = {
							is_allied_to = scope:root_scope
							has_relation_friend = scope:root_scope
							has_relation_lover = scope:root_scope
						}
					}
					any_vassal = {
						exists = joined_faction
						joined_faction = { faction_power >= faction_power_halfway_threshold }
					}
					AND = {
						NOT = { ai_should_get_conqueror_bonuses = yes } # They have a 75% discount
						monthly_character_income_minus_expenses > half_monthly_character_income # Try to spend at least 50% of income on MaA!
					}
				}
			}
			add_character_flag = { # Conquerors should sometimes spend on MaA, if there are strong neighbours
				flag = conqueror_pacing_flag
				years = 3
			}
			add_character_flag = {
				flag = conqueror_anti_pacing_flag
				years = 10
			}
		}
		
		if = {
			limit = { has_character_flag = conqueror_pacing_flag }
			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 = { # Conquerors are special; they don't care about states or eras - they just want to conquer, and put their resources towards just that.
			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 }
		}
		
		###### LIFESTYLES
		if = { # Conquerors change lifestyle focus to suit their needs
			limit = {
				NOT = { has_character_flag = ai_focus_cd } # No cheating!
			}
			if = {
				limit = {
					is_at_war = no
					war_chest_gold < war_chest_gold_maximum
					NOT = { has_focus = stewardship_wealth_focus }
				}
				set_focus = stewardship_wealth_focus
				add_character_flag = { flag = ai_focus_cd years = 5 }
			}
			else_if = { # Prestige is important for CB's
				limit = {
					is_at_war = no
					OR = {
						AND = {
							prestige_level < 4
							can_use_conquest_cbs_trigger = yes
						}
						prestige < 1000
					}
					NOT = { has_focus = diplomacy_majesty_focus }
					NOT = { has_trait = august }
				}
				set_focus = diplomacy_majesty_focus
				add_character_flag = { flag = ai_focus_cd years = 5 }
			}
			else_if = { # Certain conquerors will level up the skulduggery tree so they can murder important enemies to weaken them
				limit = {
					is_at_war = no
					intrigue >= martial
					intrigue >= 12
					NOT = { has_focus = intrigue_skulduggery_focus }
					NOT = { has_trait = schemer }
				}
				set_focus = intrigue_skulduggery_focus
				add_character_flag = { flag = ai_focus_cd years = 5 }
			}
			else_if = {
				limit = {
					is_commanding_army = yes
					NOT = { has_focus = martial_chivalry_focus }
				}
				set_focus = martial_chivalry_focus
				add_character_flag = { flag = ai_focus_cd years = 5 }
			}
			else_if = {
				limit = {
					NOT = { has_focus = martial_authority_focus }
					any_held_title = {
						title_tier = county
						county_control < 100
					}
				}
				set_focus = martial_authority_focus
				add_character_flag = { flag = ai_focus_cd years = 5 }
			}
			else_if = {
				limit = {
					NOT = { has_focus = martial_strategy_focus }
				}
				set_focus = martial_strategy_focus
				add_character_flag = { flag = ai_focus_cd years = 5 }
			}
		}
		
		##### SCHEMES
		if = { # End murder schemes that are useless
			limit = {
				any_scheme = {
					type = murder
					is_scheme_exposed = yes
				}
			}
			every_scheme = {
				type = murder
				limit = { is_scheme_exposed = yes }
				end_scheme = yes
			}
		}
		if = { # Murders
			limit = {
				NOT = { # Really honorable characters do not stoop this low, ever
					ai_honor >= high_positive_ai_value
				}
				OR = {
					NOT = {
						any_scheme = {
							OR = {
								scheme_type = murder
								scheme_type = abduct
								scheme_type = claim_throne
								scheme_type = fabricate_hook
							}
						}
					}
					AND = {
						has_perk = twice_schemed_perk
						any_scheme = {
							count < 2
							OR = {
								scheme_type = murder
								scheme_type = abduct
								scheme_type = claim_throne
								scheme_type = fabricate_hook
							}
						}
					}
				}
			}
			every_neighboring_and_across_water_top_liege_realm_owner = { # Weaken strong neighbours
				limit = {
					save_temporary_scope_as = target_scope
					current_military_strength >= scope:target_scope.current_military_strength
					scope:root_scope = { has_any_cb_on = scope:target_scope }
					NOR = {
						is_allied_to = scope:root_scope
						has_relation_friend = scope:root_scope
						has_relation_lover = scope:root_scope
					}
					OR = {
						primary_title = { title_held_years >= 10 } # long-time rulers are better to replace with new ones
						AND = { # Children are usually weaker rulers
							is_adult = yes
							primary_heir ?= { is_adult = no }
						}
						AND = { # Split the realm
							has_partition_succession_realm_law_trigger = yes
							any_heir = { count >= 2 }
						}
					}
					scope:root_scope = {
						can_start_scheme = {
							type = murder
							target_character = scope:target_scope
						}
					}
				}
				add_to_list = targets
			}
			every_targeting_faction = {
				limit = {
					faction_is_type = claimant_faction
					exists = special_character
					save_temporary_scope_as = faction_in_question
					scope:root_scope = {
						can_start_scheme = {
							type = murder
							target_character = scope:faction_in_question.special_character 
						}
					}
				}
				special_character = {
					add_to_list = targets
				}
			}
			every_relation = {
				type = rival
				limit = {
					is_ruler = yes
					scope:root_scope = {
						can_start_scheme = {
							type = murder
							target_character = prev
						}
					}
				}
				add_to_list = targets
			}
			random_in_list = {
				list = targets
				save_scope_as = murder_target
			}
			if = {
				limit = { exists = scope:murder_target }
				start_scheme = {
					type = murder
					target_character = scope:murder_target
				}
			}
		}
		if = { # End hook schemes that are useless
			limit = {
				any_scheme = {
					type = fabricate_hook
					is_scheme_exposed = yes
				}
			}
			every_scheme = {
				type = fabricate_hook
				limit = { is_scheme_exposed = yes }
				end_scheme = yes
			}
		}
		if = { # Fabricate Hook
			limit = {
				has_perk = truth_is_relative_perk
				OR = {
					NOT = {
						any_scheme = {
							OR = {
								scheme_type = murder
								scheme_type = abduct
								scheme_type = claim_throne
								scheme_type = fabricate_hook
							}
						}
					}
					AND = {
						has_perk = twice_schemed_perk
						any_scheme = {
							count < 2
							OR = {
								scheme_type = murder
								scheme_type = abduct
								scheme_type = claim_throne
								scheme_type = fabricate_hook
							}
						}
					}
				}
			}
			every_courtier = {
				limit = {
					is_theocratic_lessee = yes
					scope:root_scope = {
						can_start_scheme = {
							type = fabricate_hook
							target_character = prev
						}
					}
				}
				add_to_list = hook_targets
			}
			every_vassal = {
				limit = {
					highest_held_title_tier >= tier_county
					government_has_flag = government_is_feudal
					vassal_contract_can_be_modified_trigger = yes
					scope:root_scope = {
						can_start_scheme = {
							type = fabricate_hook
							target_character = prev
						}
					}
				}
				add_to_list = hook_targets
			}
			if = {
				limit = { has_perk = golden_obligations_perk }
				every_vassal = {
					limit = {
						gold >= 200
						scope:root_scope = {
							can_start_scheme = {
								type = fabricate_hook
								target_character = prev
							}
						}
					}
					add_to_list = hook_targets
				}
				every_neighboring_and_across_water_top_liege_realm_owner = {
					limit = {
						gold >= 100
						scope:root_scope = {
							can_start_scheme = {
								type = fabricate_hook
								target_character = prev
							}
						}
					}
					add_to_list = hook_targets
				}
			}
			random_in_list = {
				list = hook_targets
				weight = {
					base = 1
					modifier = {
						add = primary_title.tier
					}
				}
				save_scope_as = hook_target
			}
			if = {
				limit = {
					exists = scope:hook_target
				}
				start_scheme = {
					type = fabricate_hook
					target_character = scope:hook_target
				}
			}
		}
		
		if = { # Sway
			limit = {
				OR = {
					NOT = {
						any_scheme = {
							OR = {
								scheme_type = befriend
								scheme_type = sway
								scheme_type = seduce
								scheme_type = courting
								scheme_type = elope
								scheme_type = learn_language
							}
						}
					}
					AND = {
						OR = {
							dynasty ?= { has_dynasty_perk = glory_legacy_5 }
							AND = {
								has_royal_court = yes
								has_dlc_feature = royal_court
								has_court_type = court_diplomatic
								court_grandeur_current_level >= 10
							}
						}
						any_scheme = {
							count < 2
							OR = {
								scheme_type = befriend
								scheme_type = sway
								scheme_type = seduce
								scheme_type = courting
								scheme_type = elope
								scheme_type = learn_language
							}
						}
					}
					AND = {
						dynasty ?= { has_dynasty_perk = glory_legacy_5 }
						has_royal_court = yes
						has_dlc_feature = royal_court
						has_court_type = court_diplomatic
						court_grandeur_current_level >= 10
						any_scheme = {
							count < 3
							OR = {
								scheme_type = befriend
								scheme_type = sway
								scheme_type = seduce
								scheme_type = courting
								scheme_type = elope
								scheme_type = learn_language
							}
						}
					}
				}
			}
			every_courtier = {
				limit = {
					OR = {
						is_theocratic_lessee = yes
						has_council_position = councillor_spymaster
					}
					opinion = {
						target = scope:root_scope
						value < 100
					}
					scope:root_scope = {
						can_start_scheme = {
							type = sway
							target_character = prev
						}
					}
				}
				add_to_list = sway_targets
			}
			every_vassal = {
				limit = {
					highest_held_title_tier >= tier_county
					opinion = {
						target = scope:root_scope
						value < 100
					}
					scope:root_scope = {
						can_start_scheme = {
							type = sway
							target_character = prev
						}
					}
				}
				add_to_list = sway_targets
			}
			random_in_list = {
				list = sway_targets
				weight = {
					base = 1
					modifier = {
						add = 1000
						is_theocratic_lessee = yes
					}
					modifier = {
						add = 1000
						opinion = {
							target = scope:root_scope
							value <= 0
						}
					}
				}
				save_scope_as = sway_target
			}
			if = {
				limit = { exists = scope:sway_target }
				start_scheme = {
					type = sway
					target_character = scope:sway_target
				}
			}
		}
		
		# Don't forget to have children!
		if = {
			limit = {
				is_ai = yes
				exists = global_var:lmf_is_loaded
				fertility > 0.10
				is_pregnant = no
				primary_spouse ?= {
					is_ai = yes
					fertility > 0.10
					is_pregnant = no
				}
				any_child = {
					NOT = { has_trait = bastard }
					count < 4
				}
			}
			had_sex_with_effect = {
				CHARACTER = primary_spouse
				PREGNANCY_CHANCE = pregnancy_chance
			}
		}
		
		###### WAR	
		if = {
			limit = {
				is_at_war = no
				is_available_ai_adult = yes
				war_chest_gold >= war_chest_gold_maximum
				NOT = { has_character_flag = conqueror_pacing_flag }
			}
			if = {
				limit = { has_character_flag = histmod_seeking_hre }
				ai_start_best_war = {
					cb = {
						claim_cb
					}
					is_valid = {
						scope:target_title = {
							tier = tier_kingdom
							this = scope:target_character.primary_title
						}
						scope:target_character = { is_hre_claim_war_target = yes }
					}
					on_failure = {
						ai_start_best_war = {
							cb = {
								hre_conquest_cb
								claim_cb
							}
							is_valid = {
								trigger_if = {
									limit = { scope:casus_belli_type = casus_belli_type:claim_cb }
									scope:target_title = {
										tier = tier_duchy
										is_contested = no
										any_de_jure_county = {
											is_neighbor_to_realm = root
											title_province = {
												NOT = { has_holding_type = tribal_holding }
												OR = {
													geographical_region = world_europe_west_germania
													geographical_region = custom_northern_italy
													geographical_region = custom_burgundy
												}
											}
										}
									}
									scope:target_character = {
										root.strength_with_relevant_allies_vs_target_character_value > scope:target_character.one_and_a_half_times_current_strength_with_relevant_allies_vs_root
									}
								}
								trigger_else = {
									scope:target_title = {
										tier = tier_duchy
										any_de_jure_county = { is_better_hre_county_target = yes }
									}
									scope:target_character = {
										is_better_hre_war_target = yes
									}
								}
							}
							on_failure = {
								ai_start_best_war = {
									cb = {
										hre_conquest_cb
									}
									is_valid = {
										scope:target_title = {
											tier = tier_duchy
											any_de_jure_county = { is_hre_county_target = yes }
										}
										scope:target_character = {
											is_appropriate_hre_war_target = yes
										}
									}
									on_failure = {
										ai_start_best_war = {
											cb = {
												claim_cb
											}
											is_valid = {
												scope:target_title = {
													tier = tier_county
													is_contested = no
													is_neighbor_to_realm = root
													NOT = { has_holding_type = tribal_holding }
													OR = {
														geographical_region = world_europe_west_germania
														geographical_region = custom_northern_italy
														geographical_region = custom_burgundy
													}
												}
												scope:target_character = {
													root.strength_with_relevant_allies_vs_target_character_value > scope:target_character.one_and_a_half_times_current_strength_with_relevant_allies_vs_root
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			if = {
				limit = { has_character_flag = histmod_seeking_russia }
				ai_start_best_war = {
					cb = {
						claim_cb
					}
					is_valid = {
						scope:target_title = {
							tier = tier_kingdom
							empire = title:e_russia
						}
						scope:target_character = {
							is_appropriate_rurikid_war_target = yes
						}
					}
					on_failure = {
						ai_start_best_war = {
							cb = {
								duchy_conquest_cb
							}
							is_valid = {
								scope:target_title = {
									tier = tier_duchy
									any_de_jure_county = {
										count > 1
										is_better_rurikid_county_target = yes
										OR = {
											holder = scope:target_character
											holder = { target_is_liege_or_above = scope:target_character }
										}
									}
								}
								scope:target_character = {
									is_appropriate_rurikid_war_target = yes
								}
							}
							on_failure = {
								ai_start_best_war = { # Start one very high-impact war with a very good CB
									cb = {
										duchy_conquest_cb
									}
									is_valid = {
										scope:target_title = {
											tier = tier_duchy
											any_de_jure_county = {
												count > 1
												is_rurikid_county_target = yes
												OR = {
													holder = scope:target_character
													holder = { target_is_liege_or_above = scope:target_character }
												}
											}
										}
										scope:target_character = {
											is_appropriate_rurikid_war_target = yes
										}
									}
									on_failure = {
										ai_start_best_war = {
											cb = {
												county_conquest_cb
											}
											is_valid = {
												scope:target_title = {
													tier = tier_county
													is_better_rurikid_county_target = yes
												}
												scope:target_character = {
													is_appropriate_rurikid_war_target = yes
												}
											}
											on_failure = {
												ai_start_best_war = {
													cb = {
														county_conquest_cb
													}
													is_valid = {
														scope:target_title = {
															tier = tier_county
															is_rurikid_county_target = yes
														}
														scope:target_character = {
															is_appropriate_rurikid_war_target = yes
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			if = {
				limit = { has_character_flag = histmod_seeking_sicily }
				ai_start_best_war = {
					cb = {
						claim_cb
						religious_war
					}
					is_valid = {
						scope:target_title = {
							tier = tier_duchy
							title_capital_county = { is_sicily_county_target = yes }
						}
						scope:target_character = {
							is_appropriate_sicily_war_target = yes
						}
					}
					on_failure = {
						ai_start_best_war = { # Start one very high-impact war with a very good CB
							cb = {
								sicily_conquest_cb
							}
							is_valid = {
								scope:target_title = {
									tier = tier_county
									is_better_sicily_county_target = yes
								}
								scope:target_character = {
									is_appropriate_sicily_war_target = yes
								}
							}
							on_failure = {
								ai_start_best_war = { # Start one very high-impact war with a very good CB
									cb = {
										sicily_conquest_cb
									}
									is_valid = {
										scope:target_title = {
											tier = tier_county
											is_sicily_county_target = yes
										}
										scope:target_character = {
											is_appropriate_sicily_war_target = yes
										}
									}
								}
							}
						}
					}
				}
			}
			if = {
				limit = { has_character_flag = histmod_seeking_norway }
				ai_start_best_war = {
					cb = {
						duchy_conquest_cb
					}
					is_valid = {
						scope:target_title = {
							tier = tier_duchy
							kingdom = title:k_norway
							is_neighbor_to_realm = root
							is_contested = no
							any_de_jure_county = {
								count > 1
								OR = {
									holder = scope:target_character
									holder = { target_is_liege_or_above = scope:target_character }
								}
							}
						}
						scope:target_character = {
							is_ai = yes
							has_no_war_preventing_relation = yes
							root.strength_with_relevant_allies_vs_target_character_value > scope:target_character.strength_with_relevant_allies_vs_root_value
							NOR = {
								dynasty = root.dynasty
								any_ally = {
									is_ruler = yes
									OR = {
										dynasty = root.dynasty
										has_no_war_preventing_relation = no
									}
								}
							}
						}
					}
					on_failure = {
						ai_start_best_war = {
							cb = {
								county_conquest_cb
							}
							is_valid = {
								scope:target_title = {
									tier = tier_county
									kingdom = title:k_norway
									is_neighbor_to_realm = root
									is_contested = no
									duchy = { is_contested = no }
								}
								scope:target_character = {
									is_ai = yes
									has_no_war_preventing_relation = yes
									root.strength_with_relevant_allies_vs_target_character_value > scope:target_character.strength_with_relevant_allies_vs_root_value
									NOR = {
										dynasty = root.dynasty
										any_ally = {
											is_ruler = yes
											OR = {
												dynasty = root.dynasty
												has_no_war_preventing_relation = no
											}
										}
									}
								}
							}
						}
					}
				}
			}
			if = {
				limit = { has_character_flag = histmod_seeking_north_sea }
				ai_start_best_war = {
					cb = {
						claim_cb
					}
					is_valid = {
						scope:target_title = {
							tier > tier_county
							is_european_norway_denmark = yes
							is_contested = no
						}
						scope:target_character = {
							is_appropriate_north_sea_war_target = yes
						}
					}
					on_failure = {
						ai_start_best_war = {
							cb = {
								north_sea_duchy_conquest_cb
							}
							is_valid = {
								scope:target_title = {
									tier = tier_duchy
									is_european_norway_denmark = yes
									is_contested = no
								}
								scope:target_character = {
									is_appropriate_north_sea_war_target = yes
								}
							}
							on_failure = {
								ai_start_best_war = {
									cb = {
										claim_cb
										county_conquest_cb
										individual_county_de_jure_cb
										minor_religious_war
										ducal_conquest_cb
									}
									is_valid = {
										scope:target_title = {
											tier = tier_county
											duchy = { is_european_norway_denmark = yes }
											is_contested = no
											duchy = { is_contested = no }
										}
										scope:target_character = {
											is_appropriate_north_sea_war_target = yes
										}
									}
									on_failure = {
										ai_start_best_war = {
											cb = {
												county_conquest_cb
												claim_cb
												county_conquest_cb
												individual_county_de_jure_cb
												minor_religious_war
												ducal_conquest_cb
											}
											is_valid = {
												scope:target_title = {
													tier = tier_county
													is_contested = no
													duchy = { is_contested = no }
												}
												scope:target_character = {
													is_appropriate_north_sea_war_target = yes
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

