﻿### story war declaration
should_invader_declare_war_trigger = {
	is_at_war = no
	gold > 0
	any_held_title = { this = $INVADER_TITLE$ }
	current_military_strength >= invader_almost_full_military_strength_value
}

### CB
historicinvasions_invasion_war_allowed_for_character_trigger = {
	is_ai = no # the AI declares scripted wars
	has_character_modifier = historicinvasions_plot_armor
	any_owned_story = { has_variable = story_owner_counter }
}

historicinvasions_invasion_war_valid_to_start_trigger = {
	scope:attacker = { has_character_modifier = historicinvasions_plot_armor }
	trigger_if = {
		limit = {
			#scope:attacker = title:e_timurid.holder
			scope:attacker = { any_owned_story = { story_type = story_timurid_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_timurid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_fatimid.holder
			scope:attacker = { any_owned_story = { story_type = story_fatimid_invasion_one } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_fatimid_conquest_region_total_north_africa }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:d_shiite.holder
			scope:attacker = { any_owned_story = { story_type = story_fatimid_invasion_two } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_fatimid_conquest_region_total_arabia }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_ghaznavid.holder
			scope:attacker = { any_owned_story = { story_type = story_ghaznavid_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_ghaznavid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_seljuk.holder
			scope:attacker = { any_owned_story = { story_type = story_seljuk_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_seljuk_conquest_region_total_no_anatolia }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:k_ziyarid.holder
			scope:attacker = { any_owned_story = { story_type = story_ziyarid_invasion } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_ziyarid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_ayyubid.holder
			scope:attacker = { any_owned_story = { story_type = story_ayyubid_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_ayyubid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:k_bulgaria.holder
			scope:attacker = { any_owned_story = { story_type = story_balgarsko_invasion } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_balgarsko_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_almoravid.holder
			scope:attacker = { any_owned_story = { story_type = story_almoravid_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = {
					geographical_region = special_almoravid_conquest_region_core
				}
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_almohad.holder
			scope:attacker = { any_owned_story = { story_type = story_almohad_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = {
					geographical_region = special_almohad_conquest_region_core
				}
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_ghurid.holder
			scope:attacker = { any_owned_story = { story_type = story_ghurid_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_ghurid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:k_east_francia.holder
			scope:attacker = { any_owned_story = { story_type = story_ludolfinger_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_ludolfinger_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:k_hauteville.holder
			scope:attacker = { any_owned_story = { story_type = story_hauteville_invasion } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_hauteville_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:k_jannabid.holder
			scope:attacker = { any_owned_story = { story_type = story_jannabid_invasion } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_jannabid_conquest_region_total }
			}
		}
	}
	# trigger_else_if = {
	# 	limit = {
	# 		scope:attacker = title:e_vlaanderen.holder
	# 	}
	# 	scope:target = {
	# 		tier = tier_duchy
	# 		any_in_de_jure_hierarchy = {
	# 			tier = tier_county
	# 			title_province = { geographical_region = special_vlaanderen_conquest_region_total }
	# 		}
	# 	}
	# }
	# trigger_else_if = {
	# 	limit = {
	# 		scope:attacker = title:e_nikaea.holder
	# 	}
	# 	scope:target = {
	# 		tier = tier_duchy
	# 		any_in_de_jure_hierarchy = {
	# 			tier = tier_county
	# 			title_province = { geographical_region = special_laskaris_conquest_region_total }
	# 		}
	# 	}
	# }
	trigger_else_if = {
		limit = {
			scope:attacker = title:k_bassenheim.holder
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_bassenheim_conquest_region_1 }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:k_balk.holder
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_balk_conquest_region_1 }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_terteroba.holder
			scope:attacker = { any_owned_story = { story_type = story_terteroba_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_terteroba_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_rurikid.holder
			scope:attacker = { any_owned_story = { story_type = story_rurikid_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_rurikid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_chola.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_chola_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_anushtiginid.holder
			scope:attacker = { any_owned_story = { story_type = story_anushtiginid_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_anushtiginid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_yelu.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_yelu_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			has_game_rule = default_ottoman_invasion
			scope:attacker = title:e_ottoman.holder
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_ottoman_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			has_game_rule = extended_ottoman_invasion
			scope:attacker = title:e_ottoman.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_ottoman_conquest_region_total_extended }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_keita.holder
			scope:attacker = { any_owned_story = { story_type = story_keita_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_keita_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:d_al-qays.holder
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_al-qays_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_knytling.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_knytling_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_gediminid.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_gediminid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:d_yngling.holder
			scope:attacker = { any_owned_story = { story_type = story_yngling_invasion } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_yngling_conquest_region_total }
			}
		}
	}
	# Assassins
	# Qarghuyid
	# Vivar
	# Normandie
	trigger_else_if = {
		limit = {
			#scope:attacker = title:k_piast.holder
			scope:attacker = { any_owned_story = { story_type = story_piast_invasion } }
		}
		scope:target = {
			tier = tier_duchy
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_piast_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			#scope:attacker = title:e_sangama.holder
			scope:attacker = { any_owned_story = { story_type = story_sangama_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_sangama_conquest_region_total }
			}
		}
	}
	# Mamluk
	# Bearn
	trigger_else_if = {
		limit = {
			scope:attacker = title:k_eldiguzid.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_eldiguzid_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_mongol_empire.holder
		}
		scope:target = {
			tier = tier_empire
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_borjigin_conquest_region_total }
			}
			#todo somehow restrict these to kingdom-level invasions
			NOR = {
				this = title:e_byzantium
				this = title:e_vlaanderen
				this = title:e_nikaea

				this = title:e_rajastan
				this = title:e_ghurid
				
				this = title:e_arabia
				this = title:e_ayyubid

				this = title:e_bengal
				this = title:h_india

				this = title:e_west_slavia
				this = title:e_wendish_empire
				this = title:e_carpathia
				this = title:e_hre

				this = title:h_roman_empire
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_golden_horde.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_jochid_conquest_region_total }
			}
		}
	}
	# Chagatai
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_ilkhanate.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_hulagu_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_qara_qoyunlu.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_qara_qoyunlu_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_aq_qoyunlu.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_aq_qoyunlu_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = { any_owned_story = { story_type = story_wanggiyan_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_wanggiyan_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = { any_owned_story = { story_type = story_rajasa_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_rajasa_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = { any_owned_story = { story_type = story_liao_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_liao_conquest_region_total }
			}
		}
	}
	# Ahom
	trigger_else_if = {
		limit = {
			scope:attacker = { any_owned_story = { story_type = story_yuan_invasion } }
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_yuan_conquest_region_total }
			}
		}
	}

	####### ANCIENT HISTORY
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_attila.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_attila_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_argead.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_argead_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_maurya.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_maurya_conquest_region_total }
			}
		}
	}

	####### FANTASY
	trigger_else_if = {
		limit = {
			scope:attacker = title:e_song.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_song_conquest_region_total }
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:attacker = title:k_zunbil.holder
		}
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
				title_province = { geographical_region = special_zunbil_conquest_region_total }
			}
		}
	}

	##### FALLBACK
	trigger_else = { always = no }
}

### primary defender should be vassalized; scope:defender
# IF...
historicinvasions_invasion_war_should_vassalize_trigger = {
	primary_title.tier >= tier_county
	primary_title.tier < scope:attacker.primary_title.tier
	any_realm_county = {
		count = all
		$TARGET_TIER$ = { is_in_list = target_titles }
	}
}

### vassal of primary defender should be vassalized; scope:defender.every_vassal_or_below
# ELSE...
historicinvasions_invasion_war_should_vassalize_vassal_trigger = {
	primary_title.tier >= tier_county
	primary_title.tier < scope:attacker.primary_title.tier
	capital_county = {
		$TARGET_TIER$ = { is_in_list = target_titles }
	}
	any_sub_realm_county = {
		count = all
		$TARGET_TIER$ = { is_in_list = target_titles }
	}
}