﻿#EK NOTE: removed achievement we don't track
# Custom CBs for CE1

# mythical_ancestor_war - like a mix of a claim and invasion war, allows you to invade anyone who holds land within a title you have a claim on

# legendary_adventure - Kingdom-tier Varangian Adventure

# leg_demand_fealty_cb - Vassalize neighbouring realms smaller than you

mythical_ancestor_war = {
	icon = invasion
	group = claim

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = {
		always = no
	}

	allowed_for_character = {
		is_alive = yes # error suppression
		has_variable_list = legendary_claims
		dynasty ?= { ce1_heroic_track_perks >= 2 }
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}
	
	allowed_against_character = {
		scope:attacker = {
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = all
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	valid_to_start = {
		scope:attacker = {
			OR = {
				is_target_in_variable_list = {
					name = legendary_claims
					target = scope:target
				}
				any_claim = {
					root = { 
						is_target_in_variable_list = {
							name = legendary_claims
							target = prev
						}
					}
					scope:target = {
						is_de_jure_liege_or_above_target = prev
					}
				}
			}
		}
	}

	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
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_invasion_war_invalidated_message
	
	on_invalidated = {
	}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = { # This should cost around the same as a regular claim war but without the discounts
			value = 0
			add = ancestor_cb_prestige_cost

			if = {
				limit = {
					scope:attacker = {
						has_character_flag = temp_no_claim_war_cost
					}
				}
				multiply = {
					value = 0
					desc = "not_imprisoned_filter" # Says 'free'
				}
			}
		}
	}
	
	on_declaration = {
		on_declared_war = yes
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = invasion_war_victory_desc_attacker
			}
			desc = invasion_war_victory_desc
		}
		triggered_desc = {
			trigger = { scope:attacker = { is_local_player = yes } }
			desc = 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 }
		
		# Truce
		add_truce_attacker_victory_effect = yes

		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 = yes
		}
		resolve_title_and_vassal_change = scope:change

		# 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
		}

		# 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
	}

	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 }

		# 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
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

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

		# 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

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { 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
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "ANCESTOR_WAR_NAME"
	war_name_base = "ANCESTOR_WAR_NAME_BASE"
	cb_name = "ANCESTOR_CB_NAME"
	
	interface_priority = 100
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	#EK DISABLED: No Frankokratia
	#ai_score_mult = {
	#	#Declaring war on the frankokratia leader can really ruin the whole thing
	#	if = {
	#		limit = {
	#			scope:defender = {
	#				any_owned_story = {
	#					story_type = frankokratia_story
	#					NOT = { has_variable = franko_war_complete }
	#				}
	#			}
	#		}
	#		multiply = 0
	#	}
	#}
}

legendary_adventure = {
	icon = invasion
	group = conquest

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	allowed_for_character = {
		custom_tooltip = {
			text = legendary_adventure_cb_tt
			exists = var:legendary_adventure_cb
			var:legendary_adventure_cb >= 1
		}
		highest_held_title_tier >= tier_duchy
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}

	allowed_for_character_display_regardless = {
		# Most requirements are taken care of by the legend rewards
		is_at_war = no
	}

	allowed_against_character = {
		# Players vs. AI filtering.
		OR = {
			# Can always target the AI.
			is_ai = yes
			# Cannot target a player unless they completely control at least two duchies.
			any_sub_realm_kingdom = {
				count >= 2
				save_temporary_scope_as = player_controlled_kingdom
				# Custom description to suppress errors.
				custom_description = {
					text = SUPPRESS
					scope:defender = { completely_controls = scope:player_controlled_kingdom }
				}
			}
		}
		NOT = {
			is_liege_or_above_of = scope:attacker
		}
		# Scope:defender cannot be bordering your realm.
		scope:defender = {
			NOT = { character_is_land_realm_neighbor = scope:attacker }
		}
	}
	target_titles = all
	target_title_tier = kingdom
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	ai_can_target_all_titles = { always = yes }
	ai_score_mult = { 
		value = varangian_adventure_ai_targeting_value 
		#EK DISABLED: No Frankokratia
		#Declaring war on the frankokratia leader can really ruin the whole thing
		#if = {
		#	limit = {
		#		scope:defender = {
		#			any_owned_story = {
		#				story_type = frankokratia_story
		#				NOT = { has_variable = franko_war_complete }
		#			}
		#		}
		#	}
		#	multiply = 0
		#} 
	}

	valid_to_start = {
	}

	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
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_county_conquest_war_invalidated_message
	
	on_invalidated = {}

	cost = {
		piety = {
			value = fp1_varangian_adventure_base_piety_cost

			multiply = common_cb_piety_cost_multiplier
		}
		prestige = {
			value = varangian_adventurer_cb_cost
			multiply = common_cb_prestige_cost_multiplier
		}
	}

	on_declaration = {
		scope:attacker = {
			change_variable = {
				name = legendary_adventure_cb
				add = -1
			}
		}
		on_declared_war = yes
		# Dish out event troops, if appropriate.
		## We give out x200 troops per same-culture same-faith core mainland Scandinavian county, up to a maximum of ten counties.
		## These are evenly split between good MaA -> average MaA -> good MaA again, to give a decent bell curve.
		scope:attacker = {
			# Grab capital for loc
			capital_barony = { save_scope_as = capital_barony }
			# Send the actual troops.
			send_interface_toast = {
				type = msg_troops_gained
				title = legendary_adventure.troops_raised.tt
				left_icon = scope:attacker
				#Raise the adventuring host!
				spawn_army = {
					men_at_arms = {
						type = accolade_maa_vanguards
						stacks = 2
					}
					men_at_arms = {
						type = accolade_maa_lancers
						stacks = 2
					}

					men_at_arms = {
						type = accolade_maa_skirmishers
						stacks = 4
					}
					men_at_arms = {
						type = accolade_maa_archers
						stacks = 4
					}
					men_at_arms = {
						type = accolade_maa_pikes
						stacks = 4
					}
					
					inheritable = no
					uses_supply = no
					location = scope:attacker.capital_province
					war = scope:war
					war_keep_on_attacker_victory = yes
					save_scope_as = new_army
					name = adventuring_host
				}
			}
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker = { is_local_player = yes }
				}
				desc = county_conquest_cb_victory_desc_attacker
			}
			desc = county_conquest_cb_victory_desc
		}
	}

	on_victory = {
		# 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_med_effect = yes }

		# Organise general results for the attacker.
		scope:attacker = {
			if = {
				limit = {
					exists = liege
					NOT = { this = liege }
				}
				liege = {
					save_scope_as = title_beneficiary
				}
			}
			else = {
				custom_tooltip = fp1_varangian_adventurer_conquest.summaries.old_domains
				custom_tooltip = fp1_varangian_adventurer_conquest.summaries.landed_family_old_domains
				custom_tooltip = fp1_varangian_adventurer_conquest.summaries.old_vassals
			}
			every_vassal_or_below = {
				limit = { dynasty = scope:attacker.dynasty }
				add_to_list = vassalised_family_list
			}
			# Set up titles in the new colony.
			## First up, hand over counties in the target kingdom to scope:attacker.
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
				add_claim_on_loss = yes
			}
			every_in_list = {
				list = target_titles
				custom_tooltip = CONQUEST_CB_TITLE
				every_in_de_jure_hierarchy = {
					continue = {
						OR = {
							NOT = {
								exists = holder
							}
							NOT = {
								holder = {
									target_is_liege_or_above = scope:defender
								}
							}
							AND = {
								scope:attacker.faith = {
									faith_hostility_level_comparison = {
										prev.holder.faith <= scope:defender.faith
									}
								}
								holder = {
									OR = {
										this = scope:defender
										target_is_liege_or_above = scope:defender
									}
								}
							}
						}
						tier > tier_county
					}
					limit = {
						tier = tier_county
						exists = holder
						holder = {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}
					if = {
						limit = {
							OR = {
								scope:attacker.faith = {
									faith_hostility_level_comparison = { prev.holder.faith >= scope:defender.faith }
								}
								holder.primary_title.tier >= scope:attacker.primary_title.tier
							}
						}
						add_to_temporary_list = titles_taken
					}
					else = {
						holder = { add_to_temporary_list = vassals_taken }
					}
				}
			}
			every_in_list = {
				list = titles_taken
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
					take_baronies = yes
				}
			}
			every_in_list = {
				list = vassals_taken
				change_liege = {
					liege = scope:attacker
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
			## Grab the appropriate kingdom for future independence checks.
			random_in_list = {
				list = target_titles
				limit = { tier = tier_kingdom }
				save_scope_as = target_kingdom
			}
			## Next, if the appropriate kingdom doesn't already exist, create it.
			if = {
				limit = {
					any_in_list = {
						list = target_titles
						tier = tier_kingdom
						is_title_created = no
					}
				}
				random_in_list = {
					list = target_titles
					limit = { tier = tier_kingdom }
					save_scope_as = new_kingdom
				}
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				scope:new_kingdom = {
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
			## Else if it does & is a part of scope:defender's sub-realm, usurp it.
			else_if = {
				limit = {
					any_in_list = {
						list = target_titles
						tier = tier_kingdom
						holder = {
							OR = {
								this = scope:defender
								any_liege_or_above = { this = scope:defender }
							}
						}
					}
				}
				random_in_list = {
					list = target_titles
					# No need to check all triggers, since there'll only ever be one kingdom in the list.
					limit = { tier = tier_kingdom }
					save_scope_as = new_kingdom
				}
				create_title_and_vassal_change = {
					type = usurped
					save_scope_as = change
					add_claim_on_loss = yes
				}
				scope:new_kingdom = {
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
			## Else we want a titular kingdom.
			else = {
				# To keep tooltips neat, show the core kingdom as transferring (preventing a messy "unlanded of" from appearing).
				show_as_tooltip = {
					random_in_list = {
					list = target_titles
						limit = { tier = tier_kingdom }
						save_scope_as = new_kingdom
					}
					create_title_and_vassal_change = {
						type = created
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:new_kingdom = {
						change_title_holder = {
							holder = scope:attacker
							change = scope:change
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
				# Actual transfer.
				hidden_effect = {
					create_dynamic_title = {
						tier = kingdom
						name = ADVENTURER_KINGDOM_NAME
					}
					create_title_and_vassal_change = {
						type = created
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:new_title = {
						set_destroy_on_gain_same_tier = yes
						set_no_automatic_claims = yes
						set_can_be_named_after_dynasty = no
						change_title_holder = {
							holder = scope:attacker
							change = scope:change
						}
						save_scope_as = new_kingdom
					}
					resolve_title_and_vassal_change = scope:change
					# Generate a new CoA
					## We do this in a separate block so that the effect has time to see that the title has a holder, since it'll error otherwise.
					scope:new_title = { generate_coa = yes }
				}
			}
			## Set a new capital.
			### We prefer the de jure capital of the other.
			if = {
				limit = {
					any_in_list = {
						list = target_titles
						this = kingdom.title_capital_county
					}
				}
				random_in_list = {
					list = target_titles
					limit = { this = kingdom.title_capital_county }
					save_temporary_scope_as = new_capital
					scope:attacker = { set_realm_capital = scope:new_capital }
				}
			}
			### Otherwise, tribals go by pure levy count.
			else_if = {
				limit = {
					scope:attacker = { government_has_flag = government_is_tribal }
				}
				ordered_in_list = {
					list = target_titles
					limit = { tier = tier_county }
					order_by = total_county_levies
					save_temporary_scope_as = new_capital
					scope:attacker = { set_realm_capital = scope:new_capital }
				}
			}
			### If nomadic, they will prefer county fertility
			else_if = {
				limit = {
					any_in_list = {
						list = target_titles
						tier = tier_county 
						uses_county_fertility = yes 
					}
				}
				ordered_in_list = {
					list = target_titles
					limit = { tier = tier_county }
					order_by =  county_fertility
					save_temporary_scope_as = new_capital
					scope:attacker = { set_realm_capital = scope:new_capital }
				}
			}
			### Everyone else prefers the highest development.
			else = {
				ordered_in_list = {
					list = target_titles
					limit = { tier = tier_county }
					order_by = development_level
					save_temporary_scope_as = new_capital
					scope:attacker = { set_realm_capital = scope:new_capital }
				}
			}
			# Then re-order stuff in the Old Country.
			## First, we destroy any other titles scope:attacker holds (excepting HoF titles).
			scope:attacker = {
				if = {
					limit = {
						any_held_title = {
							tier = tier_duchy
							is_head_of_faith = no
							NOT = {
								scope:new_kingdom = { is_de_jure_liege_or_above_target = prev }
							}
						}
					}
					every_held_title = {
						limit = {
							tier = tier_duchy
							is_head_of_faith = no
							NOT = {
								scope:new_kingdom = { is_de_jure_liege_or_above_target = prev }
							}
						}
						save_temporary_scope_as = excess_duchy
						scope:attacker = {
							every_vassal = {
								limit = {
									NOT = {
										scope:new_kingdom = { is_de_jure_liege_or_above_target = prev.primary_title }
									}
								}
								add_to_temporary_list = vassal_transfer
							}
							# First, try to give it to your former liege
							if = {
								limit = {
									exists = scope:title_beneficiary
								}
								scope:excess_duchy = {
									create_title_and_vassal_change = {
										type = granted
										save_scope_as = change
										add_claim_on_loss = no
									}
									change_title_holder_include_vassals = {
										holder = scope:title_beneficiary
										change = scope:change
									}
									hidden_effect = {
										every_in_list = {
											limit = {
												NOT = {
													this = scope:title_beneficiary
												}
											}
											list = vassal_transfer
											change_liege = {
												liege = scope:title_beneficiary
												change = scope:change
											}
										}
									}
									resolve_title_and_vassal_change = scope:change
								}
							}
							# Failing that, try to give the title away to a powerful vassal
							else_if = {
								limit = {
									any_powerful_vassal = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_duchy
										}
									}
								}
								random_powerful_vassal = {
									limit = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_duchy
										}
									}
									save_temporary_scope_as = title_beneficiary
									scope:excess_duchy = {
										create_title_and_vassal_change = {
											type = granted
											save_scope_as = change
											add_claim_on_loss = no
										}
										change_title_holder_include_vassals = {
											holder = scope:title_beneficiary
											change = scope:change
										}
										resolve_title_and_vassal_change = scope:change
									}
								}
							}
							# Failing that, give it to some random vassal
							else_if = {
								limit = {
									any_vassal = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_duchy
										}
									}
								}
								random_vassal = {
									limit = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_duchy
										}
									}
									save_temporary_scope_as = title_beneficiary
									scope:excess_duchy = {
										create_title_and_vassal_change = {
											type = granted
											save_scope_as = change
											add_claim_on_loss = no
										}
										change_title_holder_include_vassals = {
											holder = scope:title_beneficiary
											change = scope:change
										}
										resolve_title_and_vassal_change = scope:change
									}
								}
							}
							# Final failure, destroy the title
							else = {
								destroy_title = scope:excess_duchy
							}
						}
					}
				}
				if = {
					limit = {
						any_held_title = {
							tier = tier_kingdom
							NOR = {
								this = scope:new_kingdom
								is_head_of_faith = yes
							}
						}
					}
					every_held_title = {
						limit = {
							tier = tier_kingdom
							NOR = {
								this = scope:new_kingdom
								is_head_of_faith = yes
							}
						}
						save_temporary_scope_as = excess_kingdom
						scope:attacker = {
							every_vassal = {
								limit = {
									NOT = {
										scope:new_kingdom = { is_de_jure_liege_or_above_target = prev.primary_title }
									}
								}
								add_to_temporary_list = vassal_transfer
							}
							# First, try to give it to your former liege
							if = {
								limit = {
									exists = scope:title_beneficiary
								}
								scope:excess_kingdom = {
									create_title_and_vassal_change = {
										type = granted
										save_scope_as = change
										add_claim_on_loss = no
									}
									change_title_holder_include_vassals = {
										holder = scope:title_beneficiary
										change = scope:change
									}
									hidden_effect = {
										every_in_list = {
											limit = {
												NOT = {
													this = scope:title_beneficiary
												}
											}
											list = vassal_transfer
											change_liege = {
												liege = scope:title_beneficiary
												change = scope:change
											}
										}
									}
									resolve_title_and_vassal_change = scope:change
								}
							}
							# Failing that, try to give the title away to a powerful vassal
							else_if = {
								limit = {
									any_powerful_vassal = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_kingdom
										}
									}
								}
								random_powerful_vassal = {
									limit = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_kingdom
										}
									}
									save_temporary_scope_as = title_beneficiary
									scope:excess_kingdom = {
										create_title_and_vassal_change = {
											type = granted
											save_scope_as = change
											add_claim_on_loss = no
										}
										change_title_holder_include_vassals = {
											holder = scope:title_beneficiary
											change = scope:change
										}
										resolve_title_and_vassal_change = scope:change
									}
								}
							}
							# Failing that, give it to some random vassal
							else_if = {
								limit = {
									any_vassal = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_kingdom
										}
									}
								}
								random_vassal = {
									limit = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_kingdom
										}
									}
									save_temporary_scope_as = title_beneficiary
									scope:excess_kingdom = {
										create_title_and_vassal_change = {
											type = granted
											save_scope_as = change
											add_claim_on_loss = no
										}
										change_title_holder_include_vassals = {
											holder = scope:title_beneficiary
											change = scope:change
										}
										resolve_title_and_vassal_change = scope:change
									}
								}
							}
							# Final failure, destroy the title
							else = {
								destroy_title = scope:excess_kingdom
							}
						}
					}
				}
				if = {
					limit = {
						any_held_title = {
							tier = tier_empire
							is_head_of_faith = no
						}
					}
					every_held_title = {
						limit = {
							tier = tier_empire
							is_head_of_faith = no
						}
						save_temporary_scope_as = excess_empire
						scope:attacker = {
							every_vassal = {
								limit = {
									NOT = {
										scope:new_kingdom = { is_de_jure_liege_or_above_target = prev.primary_title }
									}
								}
								add_to_temporary_list = vassal_transfer
							}
							# First, try to give the title away
							if = {
								limit = {
									any_powerful_vassal = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_empire
										}
									}
								}
								random_powerful_vassal = {
									limit = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_empire
										}
									}
									save_temporary_scope_as = title_beneficiary
									scope:excess_empire = {
										create_title_and_vassal_change = {
											type = granted
											save_scope_as = change
											add_claim_on_loss = no
										}
										change_title_holder_include_vassals = {
											holder = scope:title_beneficiary
											change = scope:change
										}
										hidden_effect = {
											every_in_list = {
												list = vassal_transfer
												change_liege = {
													liege = scope:title_beneficiary
													change = scope:change
												}
											}
										}
										resolve_title_and_vassal_change = scope:change
									}
								}
							}
							# Failing that, give it to some random vassal
							else_if = {
								limit = {
									any_vassal = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_empire
										}
									}
								}
								random_vassal = {
									limit = {
										any_held_title = {
											target_is_de_jure_liege_or_above = scope:excess_empire
										}
									}
									save_temporary_scope_as = title_beneficiary
									scope:excess_empire = {
										create_title_and_vassal_change = {
											type = granted
											save_scope_as = change
											add_claim_on_loss = no
										}
										change_title_holder_include_vassals = {
											holder = scope:title_beneficiary
											change = scope:change
										}
										every_in_list = {
											list = vassal_transfer
											change_liege = {
												liege = scope:title_beneficiary
												change = scope:change
											}
										}
										resolve_title_and_vassal_change = scope:change
									}
								}
							}
							# Final failure, destroy the title
							else = {
								destroy_title = scope:excess_empire
							}
						}
					}
				}
				# If at the end of this, you're somebody's vassal, break free
				if = {
					limit = {
						is_independent_ruler = no
					}
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					becomes_independent = { change = scope:change }
					resolve_title_and_vassal_change = scope:change
				}
			}
			## Sort out vassalised family.
			custom_tooltip = fp1_varangian_adventurer_conquest.summaries.family_transfer
			every_held_title = {
				limit = {
					kingdom = { is_in_list = target_titles }
					tier = tier_county
				}
				add_to_list = spare_counties_list
			}
			## If there are any to account for, we bring over other landed family members from the Old Country & try to give them counties in the colony.
			if = {
				limit = {
					any_in_list = {
						list = vassalised_family_list
						count >= 1
					}
				}
				# Then iterate through the family members, dishing out titles as appropriate.
				every_in_list = {
					list = vassalised_family_list
					# Players first.
					limit = { is_ai = no }
					# Then NPCs, prioritising counts.
					alternative_limit = { highest_held_title_tier = tier_county }
					# We don't process barons, they can just go landless.
					# Grab the currently affected vassal.
					save_temporary_scope_as = receiving_vassal
					# Then, if there's anything in that list, look at handing out titles.
					if = {
						limit = {
							any_in_list = {
								list = spare_counties_list
								NOT = { this = scope:attacker.capital_county }
								holder = scope:attacker
							}
						}
						random_in_list = {
							list = spare_counties_list
							limit = {
								NOT = { this = scope:attacker.capital_county }
								tier = tier_county
								holder = scope:attacker
							}
							save_temporary_scope_as = spare_county
							create_title_and_vassal_change = {
								type = granted
								save_scope_as = change
								add_claim_on_loss = no
							}
							scope:spare_county = {
								change_title_holder = {
									holder = scope:receiving_vassal
									change = scope:change
								}
							}
							scope:receiving_vassal = {
								change_liege = {
									liege = scope:attacker
									change = scope:change
								}
							}
							resolve_title_and_vassal_change = scope:change
						}
						# And add them to the post-processing list so we can divy out their lands elsewhere.
						add_to_list = vassal_family_processed_list
					}
					# Otherwise, we're going to have to do something special with this character, so take note of them for later.
					else = { add_to_list = vassal_family_not_processed_list }
				}
			}
			## Distribute scope:attacker's old domains to peasant vassals of the local culture/faith.
			every_held_title = {
				limit = {
					tier = tier_county
					kingdom = {
						NOT = { this = scope:target_kingdom }
					}
				}
				# Grab the county for ease of reference.
				save_temporary_scope_as = current_county
				# First, try to give it to the title beneficiary from earlier
				if = {
					limit = {
						exists = scope:title_beneficiary
					}
					scope:title_beneficiary = { save_temporary_scope_as = local_warlord }
				}
				# Failing that, make a new guy
				else = {
					# Create an elevated peasant to claim the county.
					create_character = {
						location = scope:current_county.title_province
						template = old_country_local_warlord_template
						save_temporary_scope_as = local_warlord
					}
				}
				# Assign the seceding title to the new upstart.
				create_title_and_vassal_change = {
					type = granted
					save_scope_as = change
					add_claim_on_loss = no
				}
				hidden_effect = {
					scope:current_county = {
						change_title_holder = {
							holder = scope:local_warlord
							change = scope:change
							take_baronies = yes
						}
					}
				}
				resolve_title_and_vassal_change = scope:change
				# And give them their independence if they have become your vassal.
				if = {
					limit = {
						scope:local_warlord = {
							is_vassal_of = scope:attacker
						}
					}
					create_title_and_vassal_change = {
						type = independency
						save_scope_as = change
						add_claim_on_loss = no
					}
					hidden_effect = {
						scope:local_warlord = {
							becomes_independent = { change = scope:change }
							save_scope_as = truce_loc
							add_truce_both_ways = {
								character = scope:attacker
								days = 14600
								name = CE1_ADVENTURER_TRUCE
							}
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			## Distribute landed family's domains outside of the colony to peasant vassals of the local culture/faith.
			every_in_list = {
				list = vassal_family_processed_list
				every_held_title = {
					limit = {
						tier = tier_county
						kingdom = {
							NOT = { this = scope:target_kingdom }
						}
					}
					# Grab the county for ease of reference.
					save_temporary_scope_as = current_county
					# Create an elevated peasant to claim the county.
					create_character = {
						location = scope:current_county.title_province
						template = old_country_local_warlord_template
						save_temporary_scope_as = local_warlord
					}
					# Assign the seceding title to the new upstart.
					create_title_and_vassal_change = {
						type = usurped
						save_scope_as = change
						add_claim_on_loss = no
					}
					hidden_effect = {
						scope:current_county = {
							change_title_holder = {
								holder = scope:local_warlord
								change = scope:change
								take_baronies = yes
							}
						}
					}
					resolve_title_and_vassal_change = scope:change
					# And give them their independence.
					create_title_and_vassal_change = {
						type = independency
						save_scope_as = change
						add_claim_on_loss = no
					}
					hidden_effect = {
						scope:local_warlord = {
							becomes_independent = { change = scope:change }
							save_scope_as = truce_loc
							add_truce_both_ways = {
								character = scope:attacker
								days = 14600
								name = CE1_ADVENTURER_TRUCE
							}
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			## Give all old vassals outside of the target_titles independence.
			every_vassal = {
				limit = {
					NOT = {
						any_held_title = { kingdom = scope:target_kingdom }
					}
				}
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
					add_claim_on_loss = no
				}
				hidden_effect = {
					becomes_independent = { change = scope:change }
					save_scope_as = truce_loc
					add_truce_both_ways = {
						character = scope:attacker
						days = 14600
						name = CE1_ADVENTURER_TRUCE
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
			## Adjusting the government type as appropriate.
			# Conquering tribes makes you tribal.
			if = {
				limit = {
					capital_province = { has_holding_type = tribal_holding }
					NOT = { government_has_flag = government_is_tribal }
				}
				# Change government to tribal.
				change_government = tribal_government
			}
			# Conquering elsewhere as a tribal makes you feudal.
			else_if = {
				limit = {
					NOT = {
						capital_province = { has_holding_type = tribal_holding }
					}
					government_has_flag = government_is_tribal
				}
				#change_government = feudal_government #EK DISABLED
				#EK EDIT: we default to our autocracy, unless they would actually become feudal
				if = {
					limit = { should_use_feudal_government = yes }
					change_government = feudal_government
				}
				else = {
					change_government = autocracy_government
				}
				#EK EDIT END
			}
			## Seizing control so things *vaguely* work.
			custom_tooltip = fp1_varangian_adventurer_conquest.summaries.control_rebate
			scope:attacker = { trigger_event = fp1_varangian_adventures.0001 }
			## And giving them a gold bounty to keep them afloat.
			scope:attacker = { add_gold = varangian_adventures_bounty_amount_value }
			# Finally, general cleanup.
			## Move any remaining newly-landless family to scope:attacker's court.
			scope:attacker = {
			#gather all your cool dudes
				every_courtier = {
					limit = {
						is_ai = yes
					}
					add_to_list = vassal_family_not_processed_list
				}
			}
			every_in_list = {
				list = vassal_family_not_processed_list
				limit = { is_ruler = no }
				save_temporary_scope_as = landless_family
				scope:attacker = { add_courtier = scope:landless_family }
				set_location = scope:attacker.capital_province
			}
			## If scope:attacker wasn't the house head, they found a new house. We ignore their probable-tribalism.
			if = {
				limit = {
					NOT = { this = house.house_head }
				}
				found_cadet_house_decision_effect = {
					CHARACTER = this
					PRESTIGE = 0
				}
			}
			## If scope:attacker *was* the house head, then all landed members of their house not in their new realm found new houses.
			else = {
				house = {
					every_house_member = {
						limit = {
							is_ruler = yes
							NOR = {
								# Exempt scope:attacker.
								this = scope:attacker
								# And anyone who made the trip with them.
								any_liege_or_above = { this = scope:attacker }
							}
						}
						found_cadet_house_decision_effect = {
							CHARACTER = this
							PRESTIGE = 0
						}
					}
				}
			}
			## If scope:attacker wasn't an adventurer before, make them one.
			if = {
				limit = {
					NOR = { 
						has_trait = adventurer 
						government_has_flag = government_is_nomadic
					}
				}
				add_trait = adventurer
			}
			## Increment their Varangian Adventurer CB tally.
			if = {
				limit = {
					NOT = { has_variable = va_cb_tally }
				}
				set_variable = {
					name = va_cb_tally
					# We set to two initially, since the first war cost prestige already, so we want to pretend you had a value of 1 the first time around.
					value = 2
				}
			}
			else = {
				change_variable = {
					name = va_cb_tally
					add = 1
				}
			}
			#EK DISABLED: we do not track this achievement
			# Aaaaaand track the A Dangerous Business achievement.
			#if = {
			#	limit = {
			#		has_trait = dwarf
			#		NOT = {
			#			capital_county = {
			#				title_province = { geographical_region = world_europe } 
			#			}
			#		}
			#	}
			#	add_achievement_flag_effect = { FLAG = a_dangerous_business_achievement_flag }
			#}
		}

		# Prestige Progress for the Attacker
		every_in_list = {
			list = target_titles
			scope:attacker = {
				add_prestige_experience = medium_prestige_value
			}
		}

		# 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 = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Break alliances now out of diplo range
		if = {
			limit = {
				scope:attacker = {
					any_ally = {
						NOT = { in_diplomatic_range = scope:attacker }
					}
				}
			}
			scope:attacker = {
				every_ally = {
					limit = {
						NOT = { in_diplomatic_range = scope:attacker }
					}
					break_alliance = scope:attacker
				}
			}
		}

		# 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
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = county_conquest_cb_white_peace_desc_defender
			}
			desc = county_conquest_cb_white_peace_desc
		}
	}

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

		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = minor_prestige_value
				multiply = -1.0
			}
			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 # Not important as the scales are identical
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

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

		add_truce_white_peace_effect = yes

		# 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 = county_conquest_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = county_conquest_cb_defeat_desc_attacker
			}
			desc = county_conquest_cb_defeat_desc
		}
	}

	on_defeat = {
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes
		
		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 3
			}
			add_prestige = {
				value = major_prestige_value
				multiply = -1.0
			}
		}

		# Prestige for Defender
		scope:defender = {
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = major_prestige_value
			}
		}

		# 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:defender
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		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
	}

	transfer_behavior = transfer

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "CE1_ADVENTURER_WAR"
	war_name_base = "CE1_ADVENTURER_WAR_BASE"
	cb_name = "CE1_ADVENTURER_CB_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}

leg_demand_fealty_cb = {
	icon = de_jure_cb
	group = conquest

	allowed_for_character = {
		scope:attacker = {
			has_character_modifier = fealty_demanded
		}
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}
	
	allowed_against_character = {
		scope:defender.highest_held_title_tier < scope:attacker.highest_held_title_tier
		scope:defender = {
			any_neighboring_and_across_water_top_liege_realm_owner = {
				this = scope:attacker
			}
		}
	}
	target_titles = none
	ignore_effect = change_title_holder
	ai_only_against_neighbors = yes
	
	valid_to_start = {
	}

	on_declaration = {
		on_declared_war = yes
	}

	should_invalidate = {
		scope:defender.highest_held_title_tier >= scope:attacker.highest_held_title_tier
	}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			value = 0
			add = {
				desc = CB_SIZE_OF_TARGET_REALM_COST
				scope:defender = {
					every_sub_realm_county = {
						add = {
							value = 40
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender.primary_title = {
						tier = tier_kingdom
					}
				}
				multiply = {
					value = 2
					desc = CB_VASSALIZING_KING
				}
			}

			multiply = common_cb_prestige_cost_multiplier
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = leg_demand_fealty_v_attacker
			}
			desc = leg_demand_fealty_v_neutral
		}
		
	}
	
	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_med_effect = yes }

		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		
		scope:defender = {
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
			
		}
		resolve_title_and_vassal_change = scope:change
		
		
		# 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
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = de_jure_cb_white_peace_desc_defender
			}
			desc = de_jure_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_low_effect = yes }
		
		# Truce
		add_truce_white_peace_effect = yes

		# 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 = de_jure_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = de_jure_cb_defeat_desc_attacker
			}
			desc = de_jure_cb_defeat_desc
		}
		
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }
	
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 10
			}
		}
	
		# Truce
		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
	}
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	transfer_behavior = transfer
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "DEMAND_FEALTY_NAME"
	war_name_base = "DEMAND_FEALTY_NAME_BASE"
	cb_name = DEMAND_FEALTY_CB_NAME
	interface_priority = 98 #Below take all and regular dejure.

	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	
	max_ai_diplo_distance_to_title = 500
	
	on_invalidated_desc = msg_invalidate_war_title

	#EK DISABLED: No Frankokratia
	#ai_score_mult = {
	#	#Declaring war on the frankokratia leader can really ruin the whole thing
	#	if = {
	#		limit = {
	#			scope:defender = {
	#				any_owned_story = {
	#					story_type = frankokratia_story
	#					NOT = { has_variable = franko_war_complete }
	#				}
	#			}
	#		}
	#		multiply = 0
	#	}
	#}
}
