﻿### spawn special troops for one war
# triggered within CB
historicinvasions_spawn_army_effect = {
	if = {
		limit = { current_military_strength < war_enemy_total_strength_value }
		spawn_army = {
			name = event_troop_default_name
			location = scope:attacker.capital_province
			war = scope:war
			uses_supply = no
			inheritable = no
			
			levies = {
				value = 0
				add = war_enemy_total_strength_difference_value
				multiply = 0.4
				min = 0
				max = 15000
				multiply = historicinvasions_spawn_army_multiplier_value
			}
		}
		spawn_army = {
			name = event_troop_default_name
			location = scope:attacker.capital_province
			war = scope:war
			uses_supply = no
			inheritable = no
			
			levies = {
				value = 0
				add = war_enemy_total_strength_difference_value
				multiply = 0.4
				min = 0
				max = 15000
				multiply = historicinvasions_spawn_army_multiplier_value
			}
		}
		spawn_army = {
			name = event_troop_default_name
			location = scope:attacker.capital_province
			war = scope:war
			uses_supply = no
			inheritable = no
			
			levies = {
				value = 0
				add = war_enemy_total_strength_difference_value
				multiply = 0.4
				min = 0
				max = 15000
				multiply = historicinvasions_spawn_army_multiplier_value
			}
		}
	}
}

historicinvasions_spawn_unlanded_army_effect = {
	spawn_army = {
		name = event_troop_default_name
		location = scope:attacker.primary_title.title_capital_county.title_province
		war = scope:war
		uses_supply = no
		inheritable = no
		
		levies = {
			value = 0
			# add = war_enemy_total_strength_value
			# subtract = scope:attacker.max_military_strength
			add = war_enemy_total_strength_difference_value
			multiply = 0.4
			min = 0
			max = 12000
			multiply = historicinvasions_spawn_army_multiplier_value
		}
	}
	spawn_army = {
		name = event_troop_default_name
		location = scope:attacker.primary_title.title_capital_county.title_province
		war = scope:war
		uses_supply = no
		inheritable = no
		
		levies = {
			value = 0
			add = war_enemy_total_strength_difference_value
			multiply = 0.4
			min = 0
			max = 12000
			multiply = historicinvasions_spawn_army_multiplier_value
		}
	}
	spawn_army = {
		name = event_troop_default_name
		location = scope:attacker.primary_title.title_capital_county.title_province
		war = scope:war
		uses_supply = no
		inheritable = no
		
		levies = {
			value = 0
			add = war_enemy_total_strength_difference_value
			multiply = 0.4
			min = 0
			max = 12000
			multiply = historicinvasions_spawn_army_multiplier_value
		}
	}

	### 4 Rûm
	if = {
		limit = {
			scope:attacker.primary_title = { has_variable = seljuk_rum_invasion_title_var }
			scope:attacker.primary_title = { is_landless_type_title = yes }
			NOT = {
				is_target_in_global_variable_list = {
					name = unavailable_unique_decisions
					target = flag:flag_formed_rum_sultanate
				}
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:seljuk_rum_have_appeared
			}
		}
		trigger_event = seljuk_invasion.0101
	}

	### 14 Vlaanderen; fourth Crusade
	if = {
		limit = {
			scope:attacker = title:e_vlaanderen.holder
			title:e_vlaanderen = { is_landless_type_title = yes }
			scope:defender = title:e_byzantium.holder
			NOT = {
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:vlaanderen_story_ended
				}
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:vlaanderen_story_started
			}
		}
		trigger_event = vlaanderen_invasion.0100
	}

	### 28 Qarghuyid
	if = {
		limit = {
			scope:attacker = title:d_qarghuyid.holder
			title:d_qarghuyid = { is_landless_type_title = yes }
			NOT = {
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:qarghuyid_story_ended
				}
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:qarghuyid_story_started
			}
		}
		trigger_event = qarghuyid_invasion.0100
	}

	### 29 Vivar
	if = {
		limit = {
			scope:attacker = title:d_vivar.holder
			title:d_vivar = { is_landless_type_title = yes }
			NOT = {
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:vivar_story_ended
				}
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:vivar_story_started
			}
		}
		trigger_event = vivar_invasion.0100
	}

	### 30 Normandie?
	# if = {
	# 	limit = {
	# 		scope:attacker = title:k_normandie.holder
	# 		title:k_normandie = { is_landless_type_title = yes }
	# 		NOT = {
	# 			is_target_in_global_variable_list = {
	# 				name = historicinvasions_story_ended
	# 				target = flag:normandie_story_ended
	# 			}
	# 		}
	# 		is_target_in_global_variable_list = {
	# 			name = historicinvasions_story_started
	# 			target = flag:normandie_story_started
	# 		}
	# 	}
	# 	trigger_event = normandie_invasion.0100
	# }

	### 24 Bearn
	if = {
		limit = {
			scope:attacker = title:d_bearn.holder
			title:d_bearn = { is_landless_type_title = yes }
			NOT = {
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:bearn_story_ended
				}
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:bearn_story_started
			}
		}
		trigger_event = bearn_invasion.0100
	}

	### 42 Ahom
	if = {
		limit = {
			scope:attacker = title:d_ahom.holder
			title:d_ahom = { is_landless_type_title = yes }
			NOT = {
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:ahom_story_ended
				}
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:ahom_story_started
			}
		}
		trigger_event = ahom_invasion.0100
	}
}

### victory effect
historicinvasions_invasion_war_victory_effect = {
	# which is the highest tier the CB has targeted?
	ordered_in_list = {
		list = target_titles
		order_by = tier
		save_scope_as = highest_tier_title
	}

	### take more than what was originally declared for
	# Target titles + dominant empires only
	scope:defender = {
		switch = {
			trigger = scope:highest_tier_title.tier
			5 = { # empire
				# Check whether any additional empires should be considered target titles
				every_realm_de_jure_empire = {
					limit = {
						any_in_de_jure_hierarchy = { # Then check how many de jure counties are in their own realm
							count >= 8 # This number (og = 8) determines how many counties activates the threshold for taking these titles
							continue = { tier > tier_county }
							exists = this
							tier = tier_county
							exists = holder
							holder.top_liege = scope:attacker
						}
					}
					add_to_temporary_list = target_titles
				}
			}
			4 = { # kingdom
				# Check whether any additional kingdoms should be considered target titles
				every_realm_de_jure_kingdom = {
					limit = {
						any_in_de_jure_hierarchy = { # Then check how many de jure counties are in the INVADER's realm
							count >= 7 # This number (og = 8) determines how many counties activate the threshold for taking these titles
							continue = { tier > tier_county }
							exists = this
							tier = tier_county
							exists = holder
							holder.top_liege = scope:attacker
						}
					}
					add_to_temporary_list = target_titles
				}
			}
			3 = { # duchy
				# Check whether any additional duchies should be considered target titles
				every_realm_de_jure_duchy = {
					limit = {
						any_in_de_jure_hierarchy = { # Then check how many de jure counties are in the INVADER's realm
							count >= 2 # This number (og = 8) determines how many counties activate the threshold for taking these titles
							continue = { tier > tier_county }
							exists = this
							tier = tier_county
							exists = holder
							holder.top_liege = scope:attacker
						}
					}
					add_to_temporary_list = target_titles
				}
			}
			2 = {} # county
			1 = {} # barony
			0 = {} # unlanded
		}
	}

	### title_change
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = title_change
		add_claim_on_loss = yes
	}

	### the goal is to vassalize as many characters as possible by dropping their tier below that of the invader
	# vassals of defender are ranked down first
	if = {
		limit = { scope:defender.primary_title.tier > scope:attacker.primary_title.tier }
		# empires are irrelevant here, as the defender can't possibly hold a higher tier title
		if = {
			limit = { scope:attacker.primary_title.tier = tier_kingdom }
			scope:defender = {
				every_realm_de_jure_kingdom = {
					limit = {
						holder.top_liege = scope:defender # can also be the defender
						#NOT = { holder = scope:defender }
						is_in_list = target_titles
					}
					add_to_list = titles_to_destroy
				}
			}
		}
		else_if = {
			limit = { scope:attacker.primary_title.tier = tier_duchy }
			scope:defender = {
				every_realm_de_jure_duchy = {
					limit = {
						holder.top_liege = scope:defender
						#NOT = { holder = scope:defender }
						is_in_list = target_titles
					}
					add_to_list = titles_to_destroy
				}
			}
		}
		# anything below duchies is also irrelevant as those titles can't be destroyed

		# destroy any vassal titles that should be destroyed
		every_in_list = {
			list = titles_to_destroy
			holder = { destroy_title = prev }
		}
	}

	###todo defender equal tier
	# if = {
	# 	limit = {
	# 		scope:defender.primary_title.tier = scope:attacker.primary_title.tier
	# 	}
	# 	scope:defender = {
	# 		# first transfer appropriate vassals
	# 		every_vassal_or_below = {
	# 			limit = {
	# 				switch = {
	# 					trigger = scope:highest_tier_title.tier
	# 					0 = {} # unlanded
	# 					1 = {} # barony
	# 					2 = { historicinvasions_invasion_war_should_vassalize_vassal_trigger = { TARGET_TIER = county } }
	# 					3 = { historicinvasions_invasion_war_should_vassalize_vassal_trigger = { TARGET_TIER = duchy } }
	# 					4 = { historicinvasions_invasion_war_should_vassalize_vassal_trigger = { TARGET_TIER = kingdom } }
	# 					5 = { historicinvasions_invasion_war_should_vassalize_vassal_trigger = { TARGET_TIER = empire } }
	# 				}
	# 			}
	# 			change_liege = {
	# 				liege = scope:attacker
	# 				change = scope:title_change
	# 			}
	# 		}

	# 		# then destroy defender titles
	# 		switch = {
	# 			trigger = scope:attacker.primary_title.tier
	# 			5 = { # = empire / destroy empire and higher
	# 				if = {
	# 					limit = {
	# 						# are ALL realm counties within the list of targeted titles? if so, the only thing preventing the defender from being vassalized by the attacker is the fact that their title tiers match
	# 						any_realm_county = {
	# 							count = all
	# 							empire = { is_in_list = target_titles }
	# 						}
	# 					}
	# 					# solution: rank down defender forcefully. otherwise, the defender is instead simply ejected and the attacker seizes all held counties within the targeted title list
	# 					every_held_title = {
	# 						limit = { tier >= scope:attacker.primary_title.tier }
	# 						scope:defender = { destroy_title = prev }
	# 					}
	# 				}
	# 			}
	# 			4 = { # = kingdom / destroy kingdom and higher
	# 				if = {
	# 					limit = {
	# 						any_realm_county = {
	# 							count = all
	# 							kingdom = { is_in_list = target_titles }
	# 						}
	# 					}
	# 					every_held_title = {
	# 						limit = { tier >= scope:attacker.primary_title.tier }
	# 						scope:defender = { destroy_title = prev }
	# 					}
	# 				}
	# 			}
	# 			3 = { # = duchy / destroy duchy and higher
	# 				if = {
	# 					limit = {
	# 						any_realm_county = {
	# 							count = all
	# 							duchy = { is_in_list = target_titles }
	# 						}
	# 					}
	# 					every_held_title = {
	# 						limit = { tier >= scope:attacker.primary_title.tier }
	# 						scope:defender = { destroy_title = prev }
	# 					}
	# 				}
	# 			}
	# 			# anything below duchies is irrelevant as those titles can't be destroyed
	# 		}
	# 	}
	# }
	
	# handles most of the title transfers and vassalizations; blows up de facto vassal structure (e.g. takes every duke from their de facto king)
	setup_invasion_cb = {
		titles = target_titles
		attacker = scope:attacker
		defender = scope:defender
		change = scope:title_change
		take_occupied = no
	}
	resolve_title_and_vassal_change = scope:title_change

	#todo reorganize new vassals by cutting them down to one primary title tier and then seizing all their non de jure vassals

	### band-aid solution to counteract 'offensive war' vassal opinion malus
	scope:attacker = {
		custom_tooltip = {
			text = liege_won_historicinvasions_war_tt
			every_vassal = {
				add_opinion = {
					modifier = liege_won_historicinvasions_war
					target = prev
				}
			}
		}
	}
}

### game over is prevented for player character
historicinvasions_prevent_player_game_over_effect = {
	# determine county
	scope:attacker.primary_title = {
		random_in_de_facto_hierarchy = {
			limit = {
				tier = tier_county
				holder = { is_ai = yes } #todo holds at least two counties or is merely a count
				NOT = { holder = scope:attacker }
			}
			save_scope_as = player_compensation_title
		}
	}

	# hand out county
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = title_compensation_change
		add_claim_on_loss = no
	}
	scope:player_compensation_title = {
		change_title_holder = {
			holder = scope:defender
			change = scope:title_compensation_change
		}
	}
	resolve_title_and_vassal_change = scope:title_compensation_change

	# hand out temporary duchy (if possible)
	if = {
		limit = { scope:attacker.primary_title.tier > tier_duchy }
		create_dynamic_title = {
			tier = duchy
			name = tmp_honorary_duchy
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:new_title = {
			set_capital_county = scope:player_compensation_title
			set_definitive_form = yes
			set_color_from_title = scope:player_compensation_title
			set_destroy_on_gain_same_tier = yes
			generate_coa = yes
			change_title_holder = {
				holder = scope:defender
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	else = {}
	
	# opinion bonus
	scope:attacker = {
		add_opinion = {
			target = scope:defender
			modifier = hi_honorable_defeat
		}
	}

	# spawn troops
	spawn_army = {
		uses_supply = no
		inheritable = yes
		name = honor_guard
		levies = {
			value = 1500
		}
		men_at_arms = {
			type = light_footmen
			stacks = 5
		}
		men_at_arms = {
			type = bowmen
			stacks = 5
		}
		location = scope:player_compensation_title.title_province
	}

	# notification event for player
	trigger_event = historicinvasions_event.1000
}