﻿##########
# Special 'claim' CB used by William the Bastard to invade England in 1066.
# If victorious, Harold personally seizes all land in Northern England (ostensibly to distribute to good Norwegian vassals). Otherwise should be mostly-identical to a Claim CB.
# Repurposed for the mod, it also ensures the attacker becomes independent (if part of an empire)
##########

norman_conquest_cb = {
	icon = invasion
	group = event
	mutually_exclusive_titles = {
		always = yes
	}

	allow_hostages = no
	allowed_for_character = {}
	allowed_against_character = {
		faith.religion = scope:attacker.faith.religion
	}

	is_allowed_claim_title = {
		scope:claimant =  {
			NOT = { has_trait = incapable }
			is_imprisoned = no
			OR = {
				this = scope:attacker
				AND = {
					religion = scope:attacker.religion
					dynasty = scope:attacker.dynasty
				}
			}
		}
	}

	cost = {}
	target_titles = claim
	
	on_declaration = {
		on_declared_war = yes
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier >= tier_county
					holder ?= {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
			}
		}
	}
	
	on_invalidated = { set_global_variable = norman_conquest_cb_concluded }
	on_invalidated_desc = msg_claim_war_invalidated_message

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = norman_conquest_cb_victory_desc_defender
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = norman_conquest_cb_victory_desc_attacker
			}
			desc = norman_conquest_cb_victory_desc
		}
	}
	
	on_victory = {
		#just in case there is no current claimant
		if = {
			limit = { NOT = { exists = scope:claimant } }
			scope:attacker = { save_scope_as = claimant }
		}
		
		#frees the attacker from their imperial liege
		if = {
			limit = {
				scope:attacker = {
					top_liege != this
					top_liege ?= { highest_held_title_tier > tier_kingdom }
				}
			}
			scope:attacker = {
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
				}
				change_liege_or_become_independent = {
					CHANGE = scope:change
					VASSAL = this
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
		
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

		# Set land acquisition type to conquest.
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		
		# Transfer landed titles to the attacker.
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			change = scope:change
		}
		
		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }

		#Hereward the Wake departs for the Fenlands
		character:90028 = { save_scope_as = hereward }

		if = {
			limit = {
				exists = scope:hereward
			}

			scope:hereward = {
				trigger_event = {
					id = ep3_laamps.6000
					days = { 1 2 }
				}
			}
		}

		#Eadgar becomes a LAAMP
		character:112 = { save_scope_as = eadgar }

		if = {
			limit = {
				exists = scope:eadgar
			}

			scope:eadgar = {
				trigger_event = {
					id = ep3_laamps.7000
					days = { 1 2 }
				}
			}
		}

		every_in_list = {
			list = target_titles

			#Remove elective succession law from k_england.
			clear_title_laws = yes

			# Personally seize all duchy titles in Southern England AND duchy titles personally held by the King of England.
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_duchy
					special_invasion_cb_seize_land_in_region_trigger = {
						TARGET_REGION = custom_southern_england
						TARGET_KINGDOM = title:k_england
					}
				}
				if = {
					limit = {
						holder ?= {
							top_liege ?= scope:defender

							# Vassal players in England get to keep their holdings, so they can play as vassals of the new king instead of game-overing.
							NAND = {
								this != top_liege
								is_ai = no
							}
						}
					}
					custom_description = {
						text = custom_seize_title_and_below_de_jure
						subject = scope:attacker
						object = this
						change_title_holder = {
							holder = scope:attacker
							change = scope:change
							take_baronies = yes
						}
					}

					# Grab the counties that go with the duchies, too.
					every_in_de_jure_hierarchy = {
						limit = {
							tier = tier_county
							holder ?= {
								top_liege = scope:defender

								# Vassal players in England get to keep their holdings, so they can play as vassals of the new king instead of game-overing.
								NAND = {
									this != top_liege
									is_ai = no
								}
							}
						}
						hidden_effect = {
							change_title_holder = {
								holder = scope:attacker
								change = scope:change
								take_baronies = yes
							}
						}
						add_to_temporary_list = county_titles_under_transfer
					}
				}
			}
			# Personally seize every county title in the Southern England AND county titles personally held by the King of England.
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
					special_invasion_cb_seize_land_in_region_trigger = {
						TARGET_REGION = custom_southern_england
						TARGET_KINGDOM = title:k_england
					}
					NOT = { is_in_list = county_titles_under_transfer }
				}
				if = {
					limit = {
						holder ?= {
							top_liege ?= scope:defender

							# Vassal players in England get to keep their holdings, so they can play as vassals of the new king instead of game-overing.
							NAND = {
								this != top_liege
								is_ai = no
							}
						}
					}
					custom_description = {
						text = custom_seize_title
						subject = scope:attacker
						object = this
						change_title_holder = {
							holder = scope:attacker
							change = scope:change
							take_baronies = yes
						}
					}
				}
			}
		}

		resolve_title_and_vassal_change = scope:change
		
		scope:attacker = { 
			give_nickname = nick_the_conqueror 
			if = {
				limit = { exists = global_var:started_norman_yoke_achievement }
				add_achievement_global_variable_effect = {
					VARIABLE = achievement_norman_yoke_war_flag
					VALUE = yes
				}
			}
			if = {
				limit = {
					is_ai = yes
					has_title = title:c_middlesex
				}
				set_realm_capital = title:c_middlesex
			}
		}

		modify_all_participants_fame_values = {
			WINNER = scope:attacker
			LOSER = scope:defender
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = 10
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		# truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
		
		set_global_variable = norman_conquest_cb_concluded

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
		
		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = norman_conquest_cb_white_peace_desc_defender
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = norman_conquest_cb_white_peace_desc_attacker
			}
			desc = norman_conquest_cb_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }
		
		# press claims
		every_in_list = {
			list = target_titles
			if = {
				limit = { scope:attacker = { has_weak_claim_on = prev } }
				scope:attacker = {
					make_claim_strong = prev
				}
			}
		}
		
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}

		# Small prestige penalty to the attacker for failing to win their own war.
		scope:attacker = {
			add_prestige = {
				value = scope:cb_prestige_factor # Set by 'setup_claim_cb'
				multiply = -5
			}
			stress_impact = {
 				ambitious = medium_stress_impact_gain
 				arrogant = medium_stress_impact_gain
 			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		if = {
			limit = {
				scope:claimant != scope:attacker
				scope:claimant = {
					can_add_hook = {
						type = favor_hook
						target = scope:attacker
					}
				}
			}
			scope:claimant = {
				add_hook = {
					target = scope:attacker
					type = favor_hook
				}
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# Truce
		add_truce_white_peace_effect = yes
		
		set_global_variable = norman_conquest_cb_concluded
		
		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = norman_conquest_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = norman_conquest_cb_defeat_desc_attacker
			}
			desc = norman_conquest_cb_defeat_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = {
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes 
		}
	
		# lose claims
		if = {
			limit = { scope:attacker = scope:claimant }
			every_in_list = {
				list = target_titles
				scope:claimant = {
					remove_claim = prev
				}
			}
		}

		if = {
			limit = {
				scope:claimant != scope:attacker
				scope:claimant = {
					can_add_hook = {
						type = favor_hook
						target = scope:attacker
					}
				}
			}
			scope:claimant = {
				add_hook = {
					target = scope:attacker
					type = favor_hook
				}
			}
		}
		
		# Award the appropriate amount of prestige to all participants in the war.
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}
		modify_all_participants_fame_values = {
			WINNER = scope:defender
			LOSER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = scale_10_war_defender_win
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		# Truce
		add_truce_attacker_defeat_effect = yes
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes
		
		set_global_variable = norman_conquest_cb_concluded
		
		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
		
		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	transfer_behavior = transfer
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "CLAIM_WAR_NAME"
	my_war_name = "CLAIM_WAR_NAME_MY"
	war_name_base = "CLAIM_WAR_NAME_BASE"
	cb_name = "CLAIM_CB_NAME"
	interface_priority = 60

	should_show_war_goal_subview = yes
	
	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	
	max_ai_diplo_distance_to_title = 1000
}


##########
# Special 'invasion' CB used by the Hungarians to migrate from the steppe to Pannonia
# If victorious, the Hungarians directly usurp all land within Pannonia not held by a player.
##########

hungarian_migration_cb = {
	icon = invasion
	group = invasion	#Switching this to event will render the CB invisible.
	ai = no # AI targeting is handled through game_rule events.

	allowed_for_character = {
		culture = culture:mogyer
		faith = { has_doctrine_parameter = unreformed }
	}

	allowed_against_character = {
		#Realm must have at least some land in Hungary.
		scope:defender = {
			any_realm_county = {
				title_province = { geographical_region = custom_hungary }
			}
		}
	}

	target_titles = all
	target_title_tier = kingdom
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder
	use_de_jure_wargoal_only = yes
	combine_into_one = yes

	#Configure war-score. This is a bit different to usual, focusing mostly on battles and almost not at all on back-capping the attacker: the Mogyers literally do not care at all if you occupy their homelands, they're moving their whole people to Pannonia anyway.

	max_attacker_score_from_battles = 200
	max_defender_score_from_battles = 200
	max_defender_score_from_occupation = 25
	max_attacker_score_from_occupation = 150
	full_occupation_by_defender_gives_victory = no
	landless_attacker_needs_armies = no
	attacker_capital_gives_war_score = no

	valid_to_start = {
		#May only target Hungary.
		scope:target = title:k_hungary
		#Must have taken the appropriate decision.
		has_character_flag = undertaking_hungarian_migration
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						target_is_same_character_or_above = scope:defender
					}
				}
			}
		}
		#Emperors do not deign to migrate. Plus it'll mess up title destruction.
		scope:attacker = { highest_held_title_tier >= tier_empire }
	}

	on_invalidated = {
		if = {
			limit = { exists = scope:attacker.player_heir }
			scope:attacker.player_heir = {
				if = {
					limit = {
						culture = culture:mogyer
						faith = { has_doctrine_parameter = unreformed }
					}
					#Give scope:attacker back their ability to declare a migration in the event of invalidation.
					add_character_flag = undertaking_hungarian_migration
				}
			}
		}
	}

	on_invalidated_desc = msg_invasion_war_invalidated_message

	#No cost: that's paid in the decision.
	
	on_declaration = {
		on_declared_war = yes
		hidden_effect = {
			add_to_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_launched_hungarian_migration
			}
		}
		#Give scope:attacker some armies to ensure success & use up their free invasion.
		scope:attacker = {
			remove_character_flag = undertaking_hungarian_migration			
			if = {
				limit = {
					OR = {
						has_game_rule = partial_extra_mogyer_forces
						has_game_rule = no_mogyer_changes
					}
				}
				#One army to conquer.
				if = {
					limit = { has_mpo_dlc_trigger = yes }
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 20
						}
						men_at_arms = {
							type = heavy_horse_archers
							stacks = 5
						}
						men_at_arms = {
							type = steppe_raiders
							stacks = 5
						}
						location = scope:attacker.capital_province
						uses_supply = no
						war = scope:war
						inheritable = no
					}
				}
				else = {
					spawn_army = {
						name = event_troop_default_name
						levies = 2000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						war = scope:war
						inheritable = no
					}
				}
				#And one army to hold.
				if = {
					limit = { has_mpo_dlc_trigger = yes }
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 10
						}
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = no
					}
				}
				else = {
					spawn_army = {
						name = event_troop_default_name
						levies = 1000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = no
					}
				}
				#If we're a few decades in, beef the Mogyers a bit with another temporary army.
				if = {
					limit = { current_date >= 890.1.1 }
					if = {
						limit = { has_mpo_dlc_trigger = yes }
						spawn_army = {
							name = event_troop_default_name
							men_at_arms = {
								type = nomadic_riders
								stacks = 20
							}
							men_at_arms = {
								type = heavy_horse_archers
								stacks = 5
							}
							men_at_arms = {
								type = steppe_raiders
								stacks = 5
							}
							location = scope:attacker.capital_province
							uses_supply = no
							war = scope:war
							inheritable = no
						}
					}
					else = {
						spawn_army = {
							name = event_troop_default_name
							levies = 2000
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							war = scope:war
							inheritable = no
						}
					}
				}
				#And if we're almost out of the valid time-frame, add another holding army.
				if = {
					limit = { current_date >= 905.1.1 }
					if = {
						limit = { has_mpo_dlc_trigger = yes }
						spawn_army = {
							name = event_troop_default_name
							men_at_arms = {
								type = nomadic_riders
								stacks = 10
							}
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							inheritable = no
						}
					}
					else = {
						spawn_army = {
							name = event_troop_default_name
							levies = 1000
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							inheritable = no
						}
					}
				}
			}
			else_if = {
				limit = { has_game_rule = full_extra_mogyer_forces }
				#One army to conquer.
				if = {
					limit = { has_mpo_dlc_trigger = yes }
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 20
						}
						men_at_arms = {
							type = heavy_horse_archers
							stacks = 5
						}
						men_at_arms = {
							type = steppe_raiders
							stacks = 5
						}
						location = scope:attacker.capital_province
						uses_supply = no
						war = scope:war
						inheritable = no
					}
				}
				else = {
					spawn_army = {
						name = event_troop_default_name
						levies = 2000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						war = scope:war
						inheritable = no
					}
				}
				#And two armies to hold.
				if = {
					limit = { has_mpo_dlc_trigger = yes }
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 10
						}
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = no
					}
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 10
						}
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = yes
					}
				}
				else = {
					spawn_army = {
						name = event_troop_default_name
						levies = 1000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = yes
					}
					spawn_army = {
						name = event_troop_default_name
						levies = 1000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = no
					}
				}
				#If we're a few decades in, beef the Mogyers a bit with another temporary army.
				if = {
					limit = { current_date >= 890.1.1 }
					if = {
						limit = { has_mpo_dlc_trigger = yes }
						spawn_army = {
							name = event_troop_default_name
							men_at_arms = {
								type = nomadic_riders
								stacks = 20
							}
							men_at_arms = {
								type = heavy_horse_archers
								stacks = 5
							}
							men_at_arms = {
								type = steppe_raiders
								stacks = 5
							}
							location = scope:attacker.capital_province
							uses_supply = no
							war = scope:war
							inheritable = no
						}
					}
					else = {
						spawn_army = {
							name = event_troop_default_name
							levies = 2000
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							war = scope:war
							inheritable = no
						}
					}
				}
				#And if we're almost out of the valid time-frame, add another holding army.
				if = {
					limit = { current_date >= 905.1.1 }
					if = {
						limit = { has_mpo_dlc_trigger = yes }
						spawn_army = {
							name = event_troop_default_name
							men_at_arms = {
								type = nomadic_riders
								stacks = 10
							}
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							inheritable = no
						}
					}
					else = {
						spawn_army = {
							name = event_troop_default_name
							levies = 1000
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							inheritable = no
						}
					}
				}
			}
			else = {
				#One army to conquer.
				if = {
					limit = { has_mpo_dlc_trigger = yes }
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 20
						}
						men_at_arms = {
							type = heavy_horse_archers
							stacks = 5
						}
						men_at_arms = {
							type = steppe_raiders
							stacks = 5
						}
						location = scope:attacker.capital_province
						uses_supply = no
						war = scope:war
						inheritable = yes
					}
				}
				else = {
					spawn_army = {
						name = event_troop_default_name
						levies = 2000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						war = scope:war
						inheritable = yes
					}
				}
				#And two armies to hold.
				if = {
					limit = { has_mpo_dlc_trigger = yes }
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 10
						}
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = yes
					}
					spawn_army = {
						name = event_troop_default_name
						men_at_arms = {
							type = nomadic_riders
							stacks = 10
						}
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = no
					}
				}
				else = {
					spawn_army = {
						name = event_troop_default_name
						levies = 1000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = yes
					}
					spawn_army = {
						name = event_troop_default_name
						levies = 1000
						men_at_arms = {
							type = horse_archers
							stacks = 10
						}
						location = scope:attacker.capital_province
						uses_supply = no
						inheritable = no
					}
				}
				#If we're a few decades in, beef the Mogyers a bit with another temporary army.
				if = {
					limit = { current_date >= 890.1.1 }
					if = {
						limit = { has_mpo_dlc_trigger = yes }
						spawn_army = {
							name = event_troop_default_name
							men_at_arms = {
								type = nomadic_riders
								stacks = 20
							}
							men_at_arms = {
								type = heavy_horse_archers
								stacks = 5
							}
							men_at_arms = {
								type = steppe_raiders
								stacks = 5
							}
							location = scope:attacker.capital_province
							uses_supply = no
							war = scope:war
							inheritable = no
						}
					}
					else = {
						spawn_army = {
							name = event_troop_default_name
							levies = 2000
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							war = scope:war
							inheritable = no
						}
					}
				}
				#And if we're almost out of the valid time-frame, add another holding army.
				if = {
					limit = { current_date >= 905.1.1 }
					if = {
						limit = { has_mpo_dlc_trigger = yes }
						spawn_army = {
							name = event_troop_default_name
							men_at_arms = {
								type = nomadic_riders
								stacks = 10
							}
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							inheritable = yes
						}
					}
					else = {
						spawn_army = {
							name = event_troop_default_name
							levies = 1000
							men_at_arms = {
								type = horse_archers
								stacks = 10
							}
							location = scope:attacker.capital_province
							uses_supply = no
							inheritable = yes
						}
					}
				}
			}
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker = { is_local_player = yes }
				}
				desc = hungarian_invasion_war_victory_desc_attacker
			}
			desc = hungarian_invasion_war_victory_desc
		}
		triggered_desc = {
			trigger = {
				scope:attacker = { is_local_player = yes }
			}
			desc = hungarian_invasion_war_warning_desc
		}
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

		#If the Mogyers aren't independent, make them so.
		if = {
			limit = {
				scope:attacker = { top_liege != this }
			}
			create_title_and_vassal_change = {
				type = independency
				save_scope_as = change
				add_claim_on_loss = no
			}
			change_liege_or_become_independent = {
				CHANGE = scope:change
				VASSAL = scope:attacker
			}
			resolve_title_and_vassal_change = scope:change
		}

		#Take lands within Pannonia.
		hidden_effect = {
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
				add_claim_on_loss = yes
			}
			setup_invasion_cb = {
				titles = target_titles
				attacker = scope:attacker
				defender = scope:defender
				change = scope:change
				take_occupied = no
			}
			every_in_list = {
				list = target_titles

				# Personally seize all duchy titles in k_hungary AND duchy titles personally held by the target king.
				every_in_de_jure_hierarchy = {
					limit = {
						tier = tier_duchy
						special_invasion_cb_seize_land_in_region_trigger = {
							TARGET_REGION = custom_hungary
							TARGET_KINGDOM = title:k_hungary
						}
					}
					if = {
						limit = {
							holder ?= {
								top_liege ?= scope:defender

								# Vassal players in Hungary get to keep their holdings, so they can play as vassals of the new king instead of game-overing.
								NAND = {
									this != top_liege
									is_ai = no
								}
							}
						}
						hidden_effect = {
							# As a security set all the county below the duchy as under transfer in case the defender owned the duchy and the counties below
							every_in_de_jure_hierarchy = {
								limit = {
									tier = tier_county
									holder ?= scope:defender
								}
								add_to_temporary_list = county_titles_under_transfer
							}
						}
						custom_description = {
							text = custom_seize_title_and_below_de_jure
							subject = scope:attacker
							object = this
							change_title_holder = {
								holder = scope:attacker
								change = scope:change
								take_baronies = yes
							}
						}
					}
				}

				# Grab counties belonging to duchies that don't exist
				every_in_de_jure_hierarchy = {
					limit = {
						tier = tier_county
						holder ?= {
							top_liege = scope:defender

							# Vassal players in Hungary get to keep their holdings, so they can play as vassals of the new king instead of game-overing.
							NAND = {
								this != top_liege
								is_ai = no
							}
						}
					}
					hidden_effect = {
						if = { 
							limit = { NOT = { is_in_list = county_titles_under_transfer } } # Theoretically prevents cases in which a title was already on the list
							add_to_temporary_list = county_titles_under_transfer
							change_title_holder = {
								holder = scope:attacker
								change = scope:change
								take_baronies = yes
							}
						}
						if = {
							limit = { has_game_rule = extra_extra_mogyer_forces }
							change_county_control = 50
						}
						else = {
							change_county_control = 25
						}
					}
				}
			}
			resolve_title_and_vassal_change = scope:change
		}

		#Start processing the Hungarian-specific effects.
		scope:attacker = {
			launch_hungarian_migration_scripted_effect = { INVADER = scope:attacker }
		}

		# Attacker gets Prestige Experience, Defender loses Prestige, all other participants gain Prestige based on their war contribution.
		modify_all_participants_fame_values = {
			WINNER = scope:attacker
			LOSER = scope:defender
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_invasion_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = 10
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
		
		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
		
		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Prestige for the attacker's war allies

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
		}
		
		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes
		}

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 5
			}
			add_prestige = {
				value = massive_prestige_value
				multiply = -1.0
			}
		}
		
		setup_invasion_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}

		# Attacker loses Prestige, all other war participants gain Prestige (Defender gains full prestige, all allies on both sides gain based on war contribution).
		modify_all_participants_fame_values = {
			WINNER = scope:defender
			LOSER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = scale_10_war_defender_win
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}
	
		add_truce_attacker_defeat_effect = yes
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes
		
		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
		
		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	transfer_behavior = transfer
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "HUNGARIAN_INVASION_WAR_NAME"
	war_name_base = "HUNGARIAN_INVASION_WAR_NAME_BASE"
	cb_name = "HUNGARIAN_INVASION_CB_NAME"
	
	interface_priority = 100
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.5
}

