﻿### Mongol war target evaluation and declaration
mongol_war_target_evaluation_and_declaration_effect = {
	# Set the Mongol Emperor as reference point
	save_scope_as = mongol_emperor
	add_character_flag = {
		flag = free_mongol_cb
		days = 14
	}
	# Select a new target
	random_neighboring_top_liege_realm_owner = {
		limit = {
			NOT = { is_allied_to = scope:mongol_emperor }
			this != scope:mongol_emperor
			top_liege != scope:mongol_emperor
			save_temporary_scope_as = truce_check
			NOT = {
				scope:mongol_emperor = {
					any_truce_target = {
						this = scope:truce_check
					}
				}
			}
			trigger_if = { # We don't want the Byzantine empire to get annihilated immediately
				limit = {
					OR = {
						has_primary_title = title:e_byzantium
						has_primary_title = title:e_latin_empire
					}
				}
				OR = {
					current_year >= 1279 # Roughly when the mongols should fracture historically
					scope:mongol_emperor = {
						completely_controls_region = special_mongol_empire_golden_horde_region # Controls all of Russia and the Steppe
					}
					any_realm_county = { # Byzantium is expansionist
						count >= 50
						title_province = {
							OR = {
								geographical_region = special_mongol_empire_conquest_region_prio_5
								geographical_region = special_mongol_empire_conquest_region_prio_4
								geographical_region = special_mongol_empire_conquest_region_prio_3
								geographical_region = special_mongol_empire_conquest_region_prio_2
								geographical_region = special_mongol_empire_conquest_region_prio_1
								geographical_region = special_mongol_empire_conquest_region_prio_1_alpha
							}
						}
					}
				}
			}
		}
		weight = {
			base = 0
			modifier = {
				add = mongol_invasion_target_character_weight
				always = yes
			}
		}
		save_temporary_scope_as = next_invasion_target
		primary_title = {
			save_temporary_scope_as = next_invasion_title_target
		}
	}
	if = {
		limit = {
			exists = scope:next_invasion_target
		}
		if = {
			limit = { # Declare war on every same-tier count or duke in same empire
				scope:next_invasion_target = {
					OR = {
						highest_held_title_tier = tier_county
						highest_held_title_tier = tier_duchy
					}
				}
			}
			scope:next_invasion_title_target = {
				add_to_temporary_list = all_titles_to_declare_on
				#if = {
				#	limit = { has_game_rule = default_empires }
				#	empire = { save_temporary_scope_as = real_invasion_target }
				#}
				#else = {
				#	kingdom = { save_temporary_scope_as = real_invasion_target }
				#}
				#scope:real_invasion_target = {
				empire = { #Remove this line if the above is enabled later
					every_in_de_jure_hierarchy = { # Find all other same-tier titles in same de jure empire
						continue = {
							tier > tier_county
							# Stop searching if you hit the Mongol Emperor's territory at any point
							trigger_if = {
								limit = { exists = holder }
								holder != scope:mongol_emperor
								holder.top_liege != scope:mongol_emperor
							}
						}
						limit = {
							exists = this
							OR = {
								tier = tier_county
								tier = tier_duchy
							}
							holder ?= {
								top_liege = this
								this != scope:mongol_emperor
								top_liege != scope:mongol_emperor
								NOT = { is_at_war_with = scope:mongol_emperor }
								NOT = { is_allied_to = scope:mongol_emperor }
								save_temporary_scope_as = truce_check
								NOT = {
									scope:mongol_emperor = {
										any_truce_target = {
											this = scope:truce_check
										}
									}
								}
							}
						}
						add_to_temporary_list = all_titles_to_declare_on
					}
				}
			}
			every_in_list = {
				list = all_titles_to_declare_on
				limit = {
					holder = {
						# An extra check to make sure we haven't snuck any Mongol territory in
						this != scope:mongol_emperor
						top_liege != scope:mongol_emperor

						# Standard conditions
						NOT = { is_at_war_with = scope:mongol_emperor }
						NOT = {
							is_in_list = has_been_sent_subjugation_offer
						}
						save_temporary_scope_as = truce_check
					}
					NOT = {
						scope:mongol_emperor = {
							any_truce_target = {
								this = scope:truce_check
							}
						}
					}
				}

				holder = {
					add_to_temporary_list = has_been_sent_subjugation_offer
					send_mongol_subjugation_demand_effect = yes
				}
			}
		}
		else_if = {
			limit = {
				scope:next_invasion_target = {
					highest_held_title_tier < tier_empire
				}
			}
			scope:next_invasion_target = {
				send_mongol_subjugation_demand_effect = yes
			}
		}
		else = { # Emperors are not offered ways out
			start_war = {
				cb = mongol_realm_invasion_war
				target = scope:next_invasion_target
				target_title = scope:next_invasion_title_target.empire
			}
		}
	}
}
