ep3_laamp_adventurer_county_invasion = {
	icon = invasion
	group = invasion

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

	allowed_for_character = { 
		ep3_adventurer_invasion_allowed_for_character_trigger = yes
		scope:defender.highest_held_title_tier >= tier_county
	}

	allowed_for_character_display_regardless = { 
		trigger_if = {
			limit = {
				culture = {
					has_cultural_parameter = cb_lower_prestige_level_requirement
				}
			}
			prestige_level >= 1
		}
		trigger_else = {
			prestige_level >= 2
		}
		ep3_adventurer_invasion_allowed_for_character_display_regardless_trigger = yes
	}

	allowed_against_character = { ep3_adventurer_landless_allowed_against_character_trigger = yes }

	target_titles = all
	target_title_tier = county
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	ai_can_target_all_titles = { always = yes }
	ai_score_mult = {
		value = ep3_laamp_invasion_ai_targeting_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		scope:target = {
			is_landless_type_title = no
			# Also make sure you can't war for, like, the Papal States
			trigger_if = {
				limit = { holder.faith ?= root.faith }
				NOT = {
					holder ?= { is_theocratic_lessee = yes }
				}
			}
		}
	}

	should_invalidate = { ep3_adventurer_landless_should_invalidate_trigger = yes }

	on_invalidated_desc = msg_duchy_conquest_war_invalidated_message #Re-use
	
	on_invalidated = {}

	cost = {
		piety = {
			add = ep3_laamp_invasion_base_piety_cost
			multiply = common_cb_piety_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
			multiply = 0.5 #Half it for Counties
		}
		prestige = {
			value = ep3_laamp_invasion_cb_cost
			multiply = common_cb_prestige_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker.var:adventurer_invasion_supporter ?= { save_scope_as = supporter }
		if = {
			limit = { ep3_adventurer_sponsorship_valid_trigger = yes }
			scope:attacker = {
				# Save scopes and cleanup variables
				set_variable = {
					name = adventurer_invasion_supporter
					value = scope:attacker.var:adventurer_invasion_supporter
				}
				scope:war ?= {
					set_variable = {
						name = adventurer_invasion_supporter
						value = scope:attacker.var:adventurer_invasion_supporter
					}
				}
				scope:attacker.var:adventurer_invasion_supporter ?= {
					set_variable = {
						name = adventurer_invasion_supportee
						value = scope:attacker
					}
				}
				# Opinion loss
				reverse_add_opinion = {
					target = scope:defender
					modifier = sponsored_laamp_invasion_against_me_opinion
				}
				# Support
				send_interface_toast = {
					type = msg_troops_gained
					title = ep3_laamp_sponsor_troops_tt
					left_icon = scope:supporter
					right_icon = scope:defender
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_request_soldiers }
						capital_barony = { save_scope_as = capital_barony }
						spawn_army = {
							men_at_arms = {
								type = armored_footmen
								stacks = 3
							}
							men_at_arms = {
								type = accolade_maa_archers
								stacks = 4
							}
							men_at_arms = {
								type = armored_horsemen
								stacks = 2
							}
							levies = 100
							inheritable = yes
							location = scope:attacker.capital_province
							war = scope:war
							war_keep_on_attacker_victory = yes
							save_scope_as = new_army
							name = ep3_laamp_invasion_event_army
						}
						scope:supporter = { remove_variable = adventurer_invasion_request_soldiers }
					}
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_join_war }
						scope:war ?= { add_attacker = scope:supporter }
						scope:supporter = { remove_variable = adventurer_invasion_join_war }
					}
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					scope:supporter ?= scope:defender
					scope:supporter.top_liege ?= scope:defender.top_liege
				}
			}
			scope:attacker = { ep3_laamp_supporter_invalidated_effect = yes }
		}
	}

	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
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter
					ep3_adventurer_sponsorship_valid_trigger = no
				}
				desc = {
					desc = ep3_laamp_sponsor_no_support_tt
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { var:adventurer_invasion_target != scope:defender }
						}
						desc = ep3_laamp_sponsor_target_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									this = scope:defender
									top_liege = scope:defender.top_liege
								}
							}
						}
						desc = ep3_laamp_sponsor_defender_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_allied_to = scope:defender }
						}
						desc = ep3_laamp_sponsor_ally_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_spouse_of = scope:defender }
						}
						desc = ep3_laamp_sponsor_spouse_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								this != scope:defender
								is_close_family_of = scope:defender
								opinion = {
									target = scope:defender
									value >= -25
								}
							}
						}
						desc = ep3_laamp_sponsor_family_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									has_relation_friend = scope:defender
									has_relation_lover = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_relation_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								any_character_war = {
									primary_attacker = scope:attacker.var:adventurer_invasion_supporter
									primary_defender = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_attacking_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								NOT = { in_diplomatic_range = scope:defender }
							}
						}
						desc = ep3_laamp_sponsor_range_tt
					}
				}
			}
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers
				}
				desc = ep3_laamp_sponsor_support_both_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war }
				desc = ep3_laamp_sponsor_support_join_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers }
				desc = ep3_laamp_sponsor_support_soldiers_tt
			}
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter
					ep3_adventurer_sponsorship_valid_trigger = yes
				}
				desc = ep3_laamp_sponsor_support_tt
			}
		}
	}

	on_victory = {
		scope:attacker = {
			add_character_flag = {
				flag = domicile_culture_and_faith_check
				days = 1
			}
			# Save target
			ordered_in_list = {
				list = target_titles
				order_by = tier
				save_scope_as = target
			}
			scope:defender = {
				every_held_title = {
					limit = { duchy ?= scope:target }
					add_to_list = target_titles
				}
			}
			# Organise general results for the attacker.
			if = {
				limit = { ep3_adventurer_invasion_sponsorship_applies_trigger = yes }
				var:adventurer_invasion_supporter = { save_scope_as = supporter }
				ep3_laamp_support_variable_cleanup_effect = yes
				conquest_supporter_memory_variable_effect = yes
			}
			ep3_landless_invasion_titles_taken_effect = {
				TITLE_GIVER = scope:defender
				TITLE_RECEIVER = scope:attacker
				TITLE = scope:target
				TITLE_LIST = target_titles
			}
			### Set a new capital.
			#### We prefer the de jure capital of the other.
			if = {
				limit = {
					scope:target.title_capital_county = { is_in_list = target_titles }
				}
				scope:target.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 }
				}
			}
			### Generally everyone 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 }
				}
			}
			## Seizing control so things *vaguely* work.
			custom_tooltip = fp1_varangian_adventurer_conquest.summaries.control_rebate
			scope:attacker = { trigger_event = fp1_varangian_adventures.0001 }
			# Finally, general cleanup.
			ep3_become_landed_cleanup_effect = yes
			# Prestige Progress for the Attacker
			add_prestige_experience = minor_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
		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 }

		debug_log_scopes = 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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_effect = yes }

		#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 = miniscule_prestige_value #Less for counties
				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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_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 for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 2 #Less for counties
			}
			add_prestige = {
				value = medium_prestige_value #Less for counties
				multiply = -1.0
			}
		}

		# 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 = "EP3_LAAMP_INVASION_WAR_NAME"
	war_name_base = "EP3_LAAMP_INVASION_WAR_NAME_BASE"
	cb_name = "EP3_LAAMP_INVASION_CB_COUNTY_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_attacker_score_from_battles = 75

	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}

ep3_laamp_adventurer_duchy_invasion = {
	icon = invasion
	group = invasion

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

	allowed_for_character = { 
		ep3_adventurer_invasion_allowed_for_character_trigger = yes
		scope:defender.highest_held_title_tier >= tier_duchy
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				culture = {
					has_cultural_parameter = cb_lower_prestige_level_requirement
				}
			}
			prestige_level >= 2
		}
		trigger_else = {
			prestige_level >= 3
		}
		ep3_adventurer_invasion_allowed_for_character_display_regardless_trigger = yes
	}

	allowed_against_character = { ep3_adventurer_landless_allowed_against_character_trigger = yes }

	target_titles = all
	target_title_tier = duchy
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	ai_can_target_all_titles = { always = yes }
	ai_score_mult = {
		value = ep3_laamp_invasion_ai_targeting_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		scope:target = {
			any_in_de_jure_hierarchy = {
				count >= 1
				tier = tier_county
			}
			# Also make sure you can't war for, like, the Papal States
			trigger_if = {
				limit = { holder.faith ?= root.faith }
				NOT = {
					holder ?= { is_theocratic_lessee = yes }
				}
			}
			trigger_if = {
				limit = {
					scope:attacker = { is_ai = yes }
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 2
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
	}

	should_invalidate = { ep3_adventurer_landless_should_invalidate_trigger = yes }

	on_invalidated_desc = msg_county_conquest_war_invalidated_message
	
	on_invalidated = {}

	cost = {
		piety = {
			add = ep3_laamp_invasion_base_piety_cost
			multiply = common_cb_piety_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
		}
		prestige = {
			value = ep3_laamp_invasion_cb_cost
			multiply = common_cb_prestige_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker.var:adventurer_invasion_supporter ?= { save_scope_as = supporter }
		if = {
			limit = { ep3_adventurer_sponsorship_valid_trigger = yes }
			scope:attacker = {
				# Save scopes and cleanup variables
				set_variable = {
					name = adventurer_invasion_supporter
					value = scope:attacker.var:adventurer_invasion_supporter
				}
				scope:war ?= {
					set_variable = {
						name = adventurer_invasion_supporter
						value = scope:attacker.var:adventurer_invasion_supporter
					}
				}
				scope:attacker.var:adventurer_invasion_supporter ?= {
					set_variable = {
						name = adventurer_invasion_supportee
						value = scope:attacker
					}
				}
				# Opinion loss
				reverse_add_opinion = {
					target = scope:defender
					modifier = sponsored_laamp_invasion_against_me_opinion
				}
				# Support
				send_interface_toast = {
					type = msg_troops_gained
					title = ep3_laamp_sponsor_troops_tt
					left_icon = scope:supporter
					right_icon = scope:defender
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_request_soldiers }
						capital_barony = { save_scope_as = capital_barony }
						spawn_army = {
							men_at_arms = {
								type = armored_footmen
								stacks = 3
							}
							men_at_arms = {
								type = accolade_maa_archers
								stacks = 4
							}
							men_at_arms = {
								type = armored_horsemen
								stacks = 2
							}
							levies = 100
							inheritable = yes
							location = scope:attacker.capital_province
							war = scope:war
							war_keep_on_attacker_victory = yes
							save_scope_as = new_army
							name = ep3_laamp_invasion_event_army
						}
						scope:supporter = { remove_variable = adventurer_invasion_request_soldiers }
					}
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_join_war }
						scope:war ?= { add_attacker = scope:supporter }
						scope:supporter = { remove_variable = adventurer_invasion_join_war }
					}
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					scope:supporter ?= scope:defender
					scope:supporter.top_liege ?= scope:defender.top_liege
				}
			}
			scope:attacker = { ep3_laamp_supporter_invalidated_effect = yes }
		}
	}

	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
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter
					ep3_adventurer_sponsorship_valid_trigger = no
				}
				desc = {
					desc = ep3_laamp_sponsor_no_support_tt
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { var:adventurer_invasion_target != scope:defender }
						}
						desc = ep3_laamp_sponsor_target_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									this = scope:defender
									top_liege = scope:defender.top_liege
								}
							}
						}
						desc = ep3_laamp_sponsor_defender_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_allied_to = scope:defender }
						}
						desc = ep3_laamp_sponsor_ally_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_spouse_of = scope:defender }
						}
						desc = ep3_laamp_sponsor_spouse_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								this != scope:defender
								is_close_family_of = scope:defender
								opinion = {
									target = scope:defender
									value >= -25
								}
							}
						}
						desc = ep3_laamp_sponsor_family_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									has_relation_friend = scope:defender
									has_relation_lover = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_relation_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								any_character_war = {
									primary_attacker = scope:attacker.var:adventurer_invasion_supporter
									primary_defender = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_attacking_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								NOT = { in_diplomatic_range = scope:defender }
							}
						}
						desc = ep3_laamp_sponsor_range_tt
					}
				}
			}
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers
				}
				desc = ep3_laamp_sponsor_support_both_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war }
				desc = ep3_laamp_sponsor_support_join_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers }
				desc = ep3_laamp_sponsor_support_soldiers_tt
			}
		}
	}

	on_victory = {
		scope:attacker = {
			add_character_flag = {
				flag = domicile_culture_and_faith_check
				days = 1
			}
			# Save target
			ordered_in_list = {
				list = target_titles
				order_by = tier
				save_scope_as = target
			}
			ep3_become_landed_warning_effect = {
				TITLE_RECEIVER = scope:attacker
				#TITLE_GIVER = scope:defender
				TITLE = scope:target
			}
			scope:defender = {
				every_held_title = {
					limit = { duchy ?= scope:target }
					add_to_list = target_titles
				}
				# Ensure that the target's capital county and counties within the target held directly by the defender are taken
				if = {
					limit = {
						NOT = {
							any_in_list = {
								list = target_titles
								tier = tier_county
							}
						}
					}
					every_sub_realm_county = {
						limit = {
							OR = {
								AND = {
									duchy = scope:target
									holder = scope:target.holder
								}
								scope:target.title_capital_county = this
								AND = {
									duchy = scope:target
									holder = {
										government_allows = administrative
									}
								}
							}
						}
						add_to_list = target_titles
					}
				}
			}
			# Organise general results for the attacker.
			if = {
				limit = { ep3_adventurer_invasion_sponsorship_applies_trigger = yes }
				var:adventurer_invasion_supporter = { save_scope_as = supporter }
				ep3_laamp_support_variable_cleanup_effect = yes
				conquest_supporter_memory_variable_effect = yes
			}
			ep3_landless_invasion_titles_taken_effect = {
				TITLE_GIVER = scope:defender
				TITLE_RECEIVER = scope:attacker
				TITLE = scope:target
				TITLE_LIST = target_titles
			}
			### Set a new capital.
			#### We prefer the de jure capital of the other.
			if = {
				limit = {
					scope:target.title_capital_county = { is_in_list = target_titles }
				}
				scope:target.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 }
				}
			}
			### Generally everyone 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 }
				}
			}
			## Seizing control so things *vaguely* work.
			custom_tooltip = fp1_varangian_adventurer_conquest.summaries.control_rebate
			scope:attacker = { trigger_event = fp1_varangian_adventures.0001 }
			# Finally, general cleanup.
			ep3_become_landed_cleanup_effect = yes
			# Prestige Progress for the 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
		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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_effect = yes }

		#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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_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 for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

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

		# 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 = "EP3_LAAMP_INVASION_WAR_NAME"
	war_name_base = "EP3_LAAMP_INVASION_WAR_NAME_BASE"
	cb_name = "EP3_LAAMP_INVASION_CB_DUCHY_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_attacker_score_from_battles = 75

	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}

ep3_laamp_adventurer_kingdom_invasion = {
	icon = invasion
	group = invasion

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

	allowed_for_character = { 
		ep3_adventurer_invasion_allowed_for_character_trigger = yes
		#Ensure they actually have a kingdom title
		scope:defender.highest_held_title_tier >= tier_kingdom
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				culture = {
					has_cultural_parameter = cb_lower_prestige_level_requirement
				}
			}
			prestige_level >= 3
		}
		trigger_else = {
			prestige_level >= 4
		}
		ep3_adventurer_invasion_allowed_for_character_display_regardless_trigger = yes
	}

	allowed_against_character = { ep3_adventurer_landless_allowed_against_character_trigger = yes }

	allowed_against_character_display_regardless = {
		scope:attacker = {
			trigger_if = {
				limit = {
					ep3_adventurer_invasion_sponsorship_applies_trigger = yes 
					var:adventurer_invasion_supporter.highest_held_title_tier < tier_empire
				}
				scope:defender.primary_title = { tier < tier_kingdom }
			}
		}
	}

	target_titles = all
	target_title_tier = kingdom
	target_de_jure_regions_above = no #Don't want to try to take de jure Kingdoms
	ignore_effect = change_title_holder

	ai_can_target_all_titles = { always = yes }
	ai_score_mult = {
		value = ep3_laamp_invasion_ai_targeting_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		scope:target = {
			any_in_de_jure_hierarchy = {
				count >= 1
				tier = tier_county
			}
			# Also make sure you can't war for, like, the Papal States
			trigger_if = {
				limit = { holder.faith ?= root.faith }
				NOT = {
					holder ?= { is_theocratic_lessee = yes }
				}
			}
			trigger_if = {
				limit = {
					scope:attacker = { is_ai = yes }
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 8
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
	}

	should_invalidate = { ep3_adventurer_landless_should_invalidate_trigger = yes }

	on_invalidated_desc = msg_duchy_conquest_war_invalidated_message #Re-use
	
	on_invalidated = {}

	cost = {
		piety = {
			add = ep3_laamp_invasion_base_piety_cost
			multiply = common_cb_piety_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
		}
		prestige = {
			value = ep3_laamp_invasion_cb_cost
			multiply = common_cb_prestige_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker.var:adventurer_invasion_supporter ?= { save_scope_as = supporter }
		if = {
			limit = { ep3_adventurer_sponsorship_valid_trigger = yes }
			scope:attacker = {
				# Save scopes and cleanup variables
				set_variable = {
					name = adventurer_invasion_supporter
					value = scope:attacker.var:adventurer_invasion_supporter
				}
				scope:war ?= {
					set_variable = {
						name = adventurer_invasion_supporter
						value = scope:attacker.var:adventurer_invasion_supporter
					}
				}
				scope:attacker.var:adventurer_invasion_supporter ?= {
					set_variable = {
						name = adventurer_invasion_supportee
						value = scope:attacker
					}
				}
				# Opinion loss
				reverse_add_opinion = {
					target = scope:defender
					modifier = sponsored_laamp_invasion_against_me_opinion
				}
				# Support
				send_interface_toast = {
					type = msg_troops_gained
					title = ep3_laamp_sponsor_troops_tt
					left_icon = scope:supporter
					right_icon = scope:defender
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_request_soldiers }
						capital_barony = { save_scope_as = capital_barony }
						spawn_army = {
							men_at_arms = {
								type = armored_footmen
								stacks = 3
							}
							men_at_arms = {
								type = accolade_maa_archers
								stacks = 4
							}
							men_at_arms = {
								type = armored_horsemen
								stacks = 2
							}
							levies = 100
							inheritable = yes
							location = scope:attacker.capital_province
							war = scope:war
							war_keep_on_attacker_victory = yes
							save_scope_as = new_army
							name = ep3_laamp_invasion_event_army
						}
						scope:supporter = { remove_variable = adventurer_invasion_request_soldiers }
					}
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_join_war }
						scope:war ?= { add_attacker = scope:supporter }
						scope:supporter = { remove_variable = adventurer_invasion_join_war }
					}
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					scope:supporter ?= scope:defender
					scope:supporter.top_liege ?= scope:defender.top_liege
				}
			}
			scope:attacker = { ep3_laamp_supporter_invalidated_effect = yes }
		}
	}

	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
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter
					ep3_adventurer_sponsorship_valid_trigger = no
				}
				desc = {
					desc = ep3_laamp_sponsor_no_support_tt
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { var:adventurer_invasion_target != scope:defender }
						}
						desc = ep3_laamp_sponsor_target_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									this = scope:defender
									top_liege = scope:defender.top_liege
								}
							}
						}
						desc = ep3_laamp_sponsor_defender_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_allied_to = scope:defender }
						}
						desc = ep3_laamp_sponsor_ally_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_spouse_of = scope:defender }
						}
						desc = ep3_laamp_sponsor_spouse_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								this != scope:defender
								is_close_family_of = scope:defender
								opinion = {
									target = scope:defender
									value >= -25
								}
							}
						}
						desc = ep3_laamp_sponsor_family_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									has_relation_friend = scope:defender
									has_relation_lover = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_relation_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								any_character_war = {
									primary_attacker = scope:attacker.var:adventurer_invasion_supporter
									primary_defender = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_attacking_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								NOT = { in_diplomatic_range = scope:defender }
							}
						}
						desc = ep3_laamp_sponsor_range_tt
					}
				}
			}
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers
				}
				desc = ep3_laamp_sponsor_support_both_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war }
				desc = ep3_laamp_sponsor_support_join_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers }
				desc = ep3_laamp_sponsor_support_soldiers_tt
			}
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter
					ep3_adventurer_sponsorship_valid_trigger = yes
				}
				desc = ep3_laamp_sponsor_support_tt
			}
		}
	}

	on_victory = {
		scope:attacker = {
			add_character_flag = {
				flag = domicile_culture_and_faith_check
				days = 1
			}
			# Save target
			ordered_in_list = {
				list = target_titles
				order_by = tier
				save_scope_as = target
			}
			ep3_become_landed_warning_effect = {
				TITLE_RECEIVER = scope:attacker
				#TITLE_GIVER = scope:defender
				TITLE = scope:target
			}
			scope:defender = {
				every_held_title = {
					limit = { kingdom ?= scope:target }
					add_to_list = target_titles
				}
				# Ensure that the target's capital county and counties within the target held directly by the defender are taken
				if = {
					limit = {
						NOT = {
							any_in_list = {
								list = target_titles
								tier = tier_county
							}
						}
					}
					every_sub_realm_county = {
						limit = {
							OR = {
								AND = {
									kingdom = scope:target
									holder = scope:target.holder
								}
								scope:target.title_capital_county = this
							}
						}
						add_to_list = target_titles
					}
				}
			}
			# Organise general results for the attacker.
			if = {
				limit = { ep3_adventurer_invasion_sponsorship_applies_trigger = yes }
				var:adventurer_invasion_supporter = { save_scope_as = supporter }
				ep3_laamp_support_variable_cleanup_effect = yes
				conquest_supporter_memory_variable_effect = yes
			}
			ep3_landless_invasion_titles_taken_effect = {
				TITLE_GIVER = scope:defender
				TITLE_RECEIVER = scope:attacker
				TITLE = scope:target
				TITLE_LIST = target_titles
			}
			### Set a new capital.
			#### We prefer the de jure capital of the other.
			if = {
				limit = {
					scope:target.title_capital_county = { is_in_list = target_titles }
				}
				scope:target.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 }
				}
			}
			### Generally everyone 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 }
				}
			}
			## Seizing control so things *vaguely* work.
			custom_tooltip = fp1_varangian_adventurer_conquest.summaries.control_rebate
			scope:attacker = { trigger_event = fp1_varangian_adventures.0001 }
			# Finally, general cleanup.
			ep3_become_landed_cleanup_effect = yes
			# Prestige Progress for the Attacker
			add_prestige_experience = major_prestige_value #More for kingdoms
		}

		# 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
		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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_effect = yes }

		#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 = medium_prestige_value #More for kingdoms
				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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_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 for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 4 #More for kingdoms
			}
			add_prestige = {
				value = massive_prestige_value #More for kingdoms
				multiply = -1.0
			}
		}

		# 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 = "EP3_LAAMP_INVASION_WAR_NAME"
	war_name_base = "EP3_LAAMP_INVASION_WAR_NAME_BASE"
	cb_name = "EP3_LAAMP_INVASION_CB_KINGDOM_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_attacker_score_from_battles = 75

	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}

ep3_laamp_adventurer_empire_invasion = {
	icon = invasion
	group = invasion

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

	allowed_for_character = { 
		ep3_adventurer_invasion_allowed_for_character_trigger = yes 
		#Ensure they actually have an empire title
		scope:defender.highest_held_title_tier >= tier_empire
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				culture = {
					has_cultural_parameter = cb_lower_prestige_level_requirement
				}
			}
			prestige_level >= 4
		}
		trigger_else = {
			prestige_level >= 5
		}
		ep3_adventurer_invasion_allowed_for_character_display_regardless_trigger = yes
	}

	allowed_against_character = { ep3_adventurer_landless_allowed_against_character_trigger = yes }

	allowed_against_character_display_regardless = {
		scope:attacker = {
			trigger_if = {
				limit = { ep3_adventurer_invasion_sponsorship_applies_trigger = yes }
				scope:defender.primary_title = { tier < tier_empire }
			}
		}
	}

	target_titles = all
	target_title_tier = empire
	target_de_jure_regions_above = no #Don't want to try to take de jure Empires
	ignore_effect = change_title_holder

	ai_can_target_all_titles = { always = yes }
	ai_score_mult = {
		value = ep3_laamp_invasion_ai_targeting_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		scope:target = {
			any_in_de_jure_hierarchy = {
				count >= 1
				tier = tier_county
			}
			# Also make sure you can't war for, like, the Papal States
			trigger_if = {
				limit = { holder.faith ?= root.faith }
				NOT = {
					holder ?= { is_theocratic_lessee = yes }
				}
			}
			trigger_if = {
				limit = {
					scope:attacker = { is_ai = yes }
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 16
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
	}

	should_invalidate = { ep3_adventurer_landless_should_invalidate_trigger = yes }

	on_invalidated_desc = msg_duchy_conquest_war_invalidated_message #Re-use
	
	on_invalidated = {}

	cost = {
		piety = {
			add = ep3_laamp_invasion_base_piety_cost
			multiply = common_cb_piety_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
		}
		prestige = {
			value = ep3_laamp_invasion_cb_cost
			multiply = common_cb_prestige_cost_multiplier
			multiply = common_claim_reduction_cost_multiplier_core
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker.var:adventurer_invasion_supporter ?= { save_scope_as = supporter }
		if = {
			limit = { ep3_adventurer_sponsorship_valid_trigger = yes }
			scope:attacker = {
				# Save scopes and cleanup variables
				set_variable = {
					name = adventurer_invasion_supporter
					value = scope:attacker.var:adventurer_invasion_supporter
				}
				scope:war ?= {
					set_variable = {
						name = adventurer_invasion_supporter
						value = scope:attacker.var:adventurer_invasion_supporter
					}
				}
				scope:attacker.var:adventurer_invasion_supporter ?= {
					set_variable = {
						name = adventurer_invasion_supportee
						value = scope:attacker
					}
				}
				# Opinion loss
				reverse_add_opinion = {
					target = scope:defender
					modifier = sponsored_laamp_invasion_against_me_opinion
				}
				# Support
				send_interface_toast = {
					type = msg_troops_gained
					title = ep3_laamp_sponsor_troops_tt
					left_icon = scope:supporter
					right_icon = scope:defender
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_request_soldiers }
						capital_barony = { save_scope_as = capital_barony }
						spawn_army = {
							men_at_arms = {
								type = armored_footmen
								stacks = 3
							}
							men_at_arms = {
								type = accolade_maa_archers
								stacks = 4
							}
							men_at_arms = {
								type = armored_horsemen
								stacks = 2
							}
							levies = 100
							inheritable = yes
							location = scope:attacker.capital_province
							war = scope:war
							war_keep_on_attacker_victory = yes
							save_scope_as = new_army
							name = ep3_laamp_invasion_event_army
						}
						scope:supporter = { remove_variable = adventurer_invasion_request_soldiers }
					}
					if = {
						limit = { exists = scope:supporter.var:adventurer_invasion_join_war }
						scope:war ?= { add_attacker = scope:supporter }
						scope:supporter = { remove_variable = adventurer_invasion_join_war }
					}
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					scope:supporter ?= scope:defender
					scope:supporter.top_liege ?= scope:defender.top_liege
				}
			}
			scope:attacker = { ep3_laamp_supporter_invalidated_effect = yes }
		}
	}

	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
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter
					ep3_adventurer_sponsorship_valid_trigger = no
				}
				desc = {
					desc = ep3_laamp_sponsor_no_support_tt
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { var:adventurer_invasion_target != scope:defender }
						}
						desc = ep3_laamp_sponsor_target_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									this = scope:defender
									top_liege = scope:defender.top_liege
								}
							}
						}
						desc = ep3_laamp_sponsor_defender_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_allied_to = scope:defender }
						}
						desc = ep3_laamp_sponsor_ally_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= { is_spouse_of = scope:defender }
						}
						desc = ep3_laamp_sponsor_spouse_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								this != scope:defender
								is_close_family_of = scope:defender
								opinion = {
									target = scope:defender
									value >= -25
								}
							}
						}
						desc = ep3_laamp_sponsor_family_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								OR = {
									has_relation_friend = scope:defender
									has_relation_lover = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_relation_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								any_character_war = {
									primary_attacker = scope:attacker.var:adventurer_invasion_supporter
									primary_defender = scope:defender
								}
							}
						}
						desc = ep3_laamp_sponsor_attacking_tt
					}
					triggered_desc = {
						trigger = {
							scope:attacker.var:adventurer_invasion_supporter ?= {
								NOT = { in_diplomatic_range = scope:defender }
							}
						}
						desc = ep3_laamp_sponsor_range_tt
					}
				}
			}
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war
					exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers
				}
				desc = ep3_laamp_sponsor_support_both_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_join_war }
				desc = ep3_laamp_sponsor_support_join_tt
			}
			triggered_desc = {
				trigger = { exists = scope:attacker.var:adventurer_invasion_supporter.var:adventurer_invasion_request_soldiers }
				desc = ep3_laamp_sponsor_support_soldiers_tt
			}
			triggered_desc = {
				trigger = {
					exists = scope:attacker.var:adventurer_invasion_supporter
					ep3_adventurer_sponsorship_valid_trigger = yes
				}
				desc = ep3_laamp_sponsor_support_tt
			}
		}
	}

	on_victory = {
		scope:attacker = {
			add_character_flag = {
				flag = domicile_culture_and_faith_check
				days = 1
			}
			# Save target
			ordered_in_list = {
				list = target_titles
				order_by = tier
				save_scope_as = target
			}
			ep3_become_landed_warning_effect = {
				TITLE_RECEIVER = scope:attacker
				#TITLE_GIVER = scope:defender
				TITLE = scope:target
			}
			scope:defender = {
				every_held_title = {
					limit = { empire ?= scope:target }
					add_to_list = target_titles
				}
				# Ensure that the target's capital county and counties within the target held directly by the defender are taken
				if = {
					limit = {
						NOT = {
							any_in_list = {
								list = target_titles
								tier = tier_county
							}
						}
					}
					every_sub_realm_county = {
						limit = {
							OR = {
								AND = {
									empire = scope:target
									holder = scope:target.holder
								}
								scope:target.title_capital_county = this
							}
						}
						add_to_list = target_titles
					}
				}
			}
			# Organise general results for the attacker.
			if = {
				limit = { ep3_adventurer_invasion_sponsorship_applies_trigger = yes }
				var:adventurer_invasion_supporter = { save_scope_as = supporter }
				ep3_laamp_support_variable_cleanup_effect = yes
				conquest_supporter_memory_variable_effect = yes
			}
			ep3_landless_invasion_titles_taken_effect = {
				TITLE_GIVER = scope:defender
				TITLE_RECEIVER = scope:attacker
				TITLE = scope:target
				TITLE_LIST = target_titles
			}
			### Set a new capital.
			#### We prefer the de jure capital of the other.
			if = {
				limit = {
					scope:target.title_capital_county = { is_in_list = target_titles }
				}
				scope:target.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 }
				}
			}
			### Generally everyone 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 }
				}
			}
			## 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 = {
					value = varangian_adventures_bounty_amount_value
					multiply = 0.5 #Half it for counties
				}
			}
			# Finally, general cleanup.
			ep3_become_landed_cleanup_effect = yes
			# Prestige Progress for the Attacker
			add_prestige_experience = massive_prestige_value #More for empires
		}

		# 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
		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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_effect = yes }

		#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 = major_prestige_value #More for empires
				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 = {
		scope:attacker = { ep3_laamp_support_variable_cleanup_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 for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 5 #More for empires
			}
			add_prestige = {
				value = monumental_prestige_value #More for empires
				multiply = -1.0
			}
		}

		# 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 = "EP3_LAAMP_INVASION_WAR_NAME"
	war_name_base = "EP3_LAAMP_INVASION_WAR_NAME_BASE"
	cb_name = "EP3_LAAMP_INVASION_CB_EMPIRE_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_attacker_score_from_battles = 75

	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}

ep3_laamp_raid_contract_cb = {
	icon = ducal_conquest_cb
	group = invasion
	allow_hostages = no

	target_top_liege_if_outside_realm = no

	# Root is the title
	# scope:attacker is the attacker
	# scope:defender is the defender
	allowed_for_character = {
		any_character_active_contract = {
			task_contract_type = laamp_raid_contract
		}
	}
	allowed_against_character = {
		scope:attacker = {
			any_character_active_contract = {
				task_contract_type = laamp_raid_contract
				var:task_contract_target ?= scope:defender
			}
		}
	}
	#it's free, because someone employs you to do that
	cost = {
	}

	should_invalidate = {
		scope:attacker = {
			NOT = {
				any_character_active_contract = {
					task_contract_type = laamp_raid_contract
					var:task_contract_target ?= scope:defender
				}
			}
		}
	}

	on_invalidated_desc = msg_laamp_raid_contract_war_invalidated_message

	on_invalidated = {
		scope:attacker = {
			if = {
				limit = {
					any_character_active_contract = {
						task_contract_type = laamp_raid_contract
						var:task_contract_target ?= scope:defender
					}
				}
				every_character_active_contract = {
					task_contract_type = laamp_raid_contract
					limit = {
						var:task_contract_target ?= scope:defender
					}
					invalidate_contract = yes
				}
			}
		}		
	}

	on_declaration = {
		on_declared_war = yes
	}

	on_victory_desc = {
		desc = ep3_laamp_raid_contract_cb_victory_desc
	}

	on_victory = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			accolade_attacker_war_end_glory_gain_low_effect = yes
			random_character_active_contract = {
				task_contract_type = laamp_raid_contract
				limit = {
					var:task_contract_target = scope:defender
				}
				save_scope_as = raid_contract
				task_contract_employer = {
					save_scope_as = task_contract_employer
				}
			}
			custom_tooltip = ep3_laamp_raid_contract_cb_prisoners.tt_attacker
			hidden_effect = {
				scope:defender = {
					every_courtier_or_guest = { add_to_list = potential_captives_list }
					random_in_list = {	#Nab someone related to the defender, preferring tangentials.
						list = potential_captives_list
						limit = {
							OR = {
								AND = {
									has_dynasty = yes
									dynasty = scope:defender.dynasty
								}
								is_consort_of = scope:defender
							}
						}
						alternative_limit = { always = yes }
						weight = {
							base = 1
							modifier = {
								add = 25
								NOT = { is_close_family_of = scope:defender }
							}
							modifier = {
								add = 15
								NOT = { is_extended_family_of = scope:defender }
							}
						}
						add_to_list = definite_captives_list
						save_temporary_scope_as = future_captive
						scope:task_contract_employer = {
							imprison = {
								target = scope:future_captive
								type = dungeon
							}
						}
					}
					random_in_list = {	#Always try to grab a second person.
						list = potential_captives_list
						limit = {
							NOT = { is_in_list = definite_captives_list }
						}
						weight = {
							base = 1
							modifier = {
								add = 40
								NOT = {
									any_claim = {
										useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
									}
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
									diplomacy >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_marshal_trigger = { COURT_OWNER = scope:defender }
									martial >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_steward_trigger = { COURT_OWNER = scope:defender }
									stewardship >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
									intrigue >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NOT = { learning >= high_skill_rating }
							}
							modifier = {
								add = 10
								NOT = {	prowess >= high_skill_rating }
							}
							modifier = {
								add = 50
								NOT = { is_close_or_extended_family_of = scope:defender }
							}
						}
						add_to_list = definite_captives_list
						save_temporary_scope_as = future_captive
							scope:task_contract_employer = {
								imprison = {
									target = scope:future_captive
									type = dungeon
								}
							}
					}
					random_in_list = {	#Always try to grab a third person.
						list = potential_captives_list
						limit = {
							NOT = { is_in_list = definite_captives_list }
						}
						weight = {
							base = 1
							modifier = {
								add = 40
								NOT = {
									any_claim = {
										useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
									}
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
									diplomacy >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_marshal_trigger = { COURT_OWNER = scope:defender }
									martial >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_steward_trigger = { COURT_OWNER = scope:defender }
									stewardship >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NAND = {
									can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
									intrigue >= high_skill_rating
								}
							}
							modifier = {
								add = 10
								NOT = { learning >= high_skill_rating }
							}
							modifier = {
								add = 10
								NOT = {	prowess >= high_skill_rating }
							}
							modifier = {
								add = 50
								NOT = { is_close_or_extended_family_of = scope:defender }
							}
						}
						add_to_list = definite_captives_list
						save_temporary_scope_as = future_captive
							scope:task_contract_employer = {
								imprison = {
									target = scope:future_captive
									type = dungeon
								}
							}
					}
					if = {	#If at least duchy-tier, try to grab a fourth person.
						limit = { primary_title.tier >= tier_duchy }
						random_in_list = {
							list = potential_captives_list
							limit = {
								NOT = { is_in_list = definite_captives_list }
							}
							weight = {
								base = 1
								modifier = {
									add = 40
									NOT = {
										any_claim = {
											useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
										}
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
										diplomacy >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_marshal_trigger = { COURT_OWNER = scope:defender }
										martial >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_steward_trigger = { COURT_OWNER = scope:defender }
										stewardship >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
										intrigue >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NOT = { learning >= high_skill_rating }
								}
								modifier = {
									add = 10
									NOT = { prowess >= high_skill_rating }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
							scope:task_contract_employer = {
								imprison = {
									target = scope:future_captive
									type = dungeon
								}
							}
						}
					}
					if = {	#If at least kingdom-tier, try to grab a fifth person.
						limit = { primary_title.tier >= tier_kingdom }
						random_in_list = {
							list = potential_captives_list
							limit = {
								NOT = { is_in_list = definite_captives_list }
							}
							weight = {
								base = 1
								modifier = {
									add = 40
									NOT = {
										any_claim = {
											useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
										}
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
										diplomacy >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_marshal_trigger = { COURT_OWNER = scope:defender }
										martial >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_steward_trigger = { COURT_OWNER = scope:defender }
										stewardship >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
										intrigue >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NOT = { learning >= high_skill_rating }
								}
								modifier = {
									add = 10
									NOT = { prowess >= high_skill_rating }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
							scope:task_contract_employer = {
								imprison = {
									target = scope:future_captive
									type = dungeon
								}
							}
						}
					}
				}
			}
			every_in_list = {
				list = definite_captives_list
				add_character_flag = captured_in_ep3_laamp_raid_contract_cb
			}
			#complete the contract
			hidden_effect = {
				scope:raid_contract = {
					complete_task_contract = success_standard
				}
			}
		}
		# 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 = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		# Truce
		add_truce_attacker_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 = {
		desc = ep3_laamp_raid_contract_cb_white_peace_desc

	}

	on_white_peace = {
		scope:attacker = { 
			show_pow_release_message_effect = yes
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}
		scope:attacker = {
			add_prestige = {
				value = medium_prestige_loss
			}
		}
		# 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
		}
		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}
		# Truce
		add_truce_attacker_victory_effect = yes
		scope:attacker = {
			random_character_active_contract = {
				task_contract_type = laamp_raid_contract
				limit = {
					var:task_contract_target = scope:defender
				}
				save_scope_as = raid_contract
				task_contract_employer = {
					save_scope_as = task_contract_employer
				}
			}
			#complete the contract
			scope:raid_contract = {
				complete_task_contract = failure_standard
			}
		}

		# 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 = {
		desc = ep3_laamp_raid_contract_cb_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_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
		}

		scope:attacker = {
			switch = {
				trigger = scope:attacker.primary_title.tier
				tier_county = {
					add_prestige = { value = minor_prestige_loss }
				}
				tier_duchy = {
					add_prestige = { value = medium_prestige_loss }
				}
				tier_kingdom = {
					add_prestige = { value = major_prestige_loss }
				}
				tier_empire = {
					add_prestige = { value = massive_prestige_loss }
				}
			}
			random_character_active_contract = {
				task_contract_type = laamp_raid_contract
				limit = {
					var:task_contract_target = scope:defender
				}
				save_scope_as = raid_contract
				task_contract_employer = {
					save_scope_as = task_contract_employer
				}
			}
			#complete the contract
			scope:raid_contract = {
				complete_task_contract = failure_standard
			}
		}
		scope:defender = {
			switch = {
				trigger = scope:attacker.primary_title.tier
				tier_county = {
					add_prestige = { value = minor_prestige_gain }
				}
				tier_duchy = {
					add_prestige = { value = medium_prestige_gain }
				}
				tier_kingdom = {
					add_prestige = { value = major_prestige_gain }
				}
				tier_empire = {
					add_prestige = { value = massive_prestige_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:defender
			FAME_BASE = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# 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

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "EP3_LAAMP_RAID_CONTRACT_CB_WAR_NAME"
	war_name_base = "EP3_LAAMP_RAID_CONTRACT_CB_WAR_NAME_BASE"
	cb_name = "EP3_LAAMP_RAID_CONTRACT_CB_NAME"
	interface_priority = 80

	ticking_war_score_targets_entire_realm = yes
	attacker_ticking_warscore = 0
	attacker_wargoal_percentage = 0.01
	attacker_score_from_occupation_scale = 50
	defender_score_from_occupation_scale = 50
	attacker_score_from_battles_scale = 200
	defender_score_from_battles_scale = 200

	max_attacker_score_from_battles = 200
	max_defender_score_from_battles = 200
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500
	
	ai_score = {
		value = raid_for_captives_ai_score_value
	}

	ai_score_mult = {
		value = 1
		# Increase odds if the character is a vassal with Ghazi Contract
		add = {
			if = {
				limit = {
					scope:attacker = {
						top_liege != this
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}
		# Holy War are limited to connected neighbour to limit big empire expansions over weak realms.
		add = {
			if = {
				limit = {  religious_war_vassals_constraints = yes }
				add = -1000
			}
		}
	}
}
#
#crusading_claim_cb = {
#	icon = undirected_great_holy_war
#	group = religious_script_onlyFr
#
#	full_occupation_by_defender_gives_victory = no
#	attacker_capital_gives_war_score = no
#	check_all_defenders_for_ticking_war_score = yes
#	defender_ticking_warscore = 0.125
#	imprisonment_by_defender_give_war_score = no
#	defender_score_from_battles_scale = 125
#	attacker_score_from_battles_scale = 125
#
#	occupation_participation_mult = 0.5
#	siege_participation_mult = 1
#	battle_participation_mult = 7.5
#
#	# Root is the title
#	# scope:claimant is the claimant
#	# scope:attacker is the attacker
#	# scope:defender is the defender
#	is_allowed_claim_title = {
#		custom_description = {
#			text = "claimant_titles_held_by_you_or_vassal"
#			NOR = {
#				holder = scope:attacker
#				holder = {
#					target_is_liege_or_above = scope:attacker
#				}
#			}
#		}
#	}
#
#	cost = {
#	}
#
#	target_titles = claim
#	target_title_tier = all
#
#	on_declaration = {
#		on_declared_war = yes
#		if = {
#			limit = {
#				scope:defender = { is_sibling_of = scope:attacker }
#			}
#			scope:attacker = {
#				add_achievement_flag_effect = { FLAG = achievement_bad_blood_flag }
#			}
#		}
#		scope:attacker = {
#			random_character_war = {
#				limit = {
#					using_cb = crusading_claim_cb
#				}
#				every_war_attacker = {
#					save_temporary_scope_as = war_attackers
#					scope:attacker = {
#						random_owned_story = {
#							type = frankokratia_story
#							add_to_variable_list = {
#								name = frankokratia_leaders
#								target = scope:war_attackers
#							}
#						}
#					}
#				}
#			}
#		}
#	}
#
#	should_invalidate = {
#		OR = {
#			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
#							}
#						}
#					}
#				}
#			}
#			scope:claimant = {
#				is_alive = no
#			} 
#		}
#	}
#
#	on_invalidated_desc = {
#		first_valid = {
#			triggered_desc = {
#				trigger = {
#					scope:claimant != scope:attacker 
#					scope:claimant = { is_alive = no }
#				}
#				desc = msg_claim_war_invalidated_claimant_unavailable_dead_message
#			}
#			desc = msg_claim_war_invalidated_message
#		}
#	}
#	
#	on_invalidated = {
#		scope:attacker = {
#			add_prestige = medium_prestige_loss
#			add_piety = medium_piety_loss
#			random_owned_story = {
#				type = frankokratia_story
#				set_variable = {
#					name = franko_war_complete
#				}
#			}
#		}
#		scope:defender = {
#			add_piety = medium_piety_gain
#		}
#	}
#
#	on_victory_desc = {
#		first_valid = {
#			triggered_desc = {
#				trigger = {
#					scope:claimant = { is_local_player = yes }
#				}
#				desc = claim_cb_victory_desc_attacker_claimant
#			}
#			triggered_desc = {
#				trigger = { scope:attacker = { is_local_player = yes } }
#				desc = claim_cb_victory_desc_attacker
#			}
#			desc = claim_cb_victory_desc
#		}
#	}
#
#	on_victory = {
#		scope:attacker = {
#			random_character_war = {
#				limit = {
#					using_cb = crusading_claim_cb
#				}
#				every_war_attacker = {
#					save_temporary_scope_as = war_attackers
#					scope:attacker = {
#						random_owned_story = {
#							type = frankokratia_story
#							add_to_variable_list = {
#								name = frankokratia_leaders
#								target = scope:war_attackers
#							}
#						}
#					}
#				}
#			}
#			random_owned_story = {
#				type = frankokratia_story
#				set_variable = {
#					name = franko_war_complete
#				}
#			}
#		}
#
#		scope:attacker = { show_pow_release_message_effect = yes }
#
#		#EP2 Accolade glory gain from winning against higher ranked enemy
#		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }
#
#		scope:defender ?= {
#			if = {
#				limit = {
#					top_liege = this
#					government_allows = administrative
#					primary_title = { is_in_list = target_titles }
#				}
#				every_held_title = {
#					title_tier >= county
#					limit = {
#						is_noble_family_title = no
#					}
#					add_to_list = target_titles
#				}
#			}
#		}
#		
#		scope:claimant = {
#			custom_tooltip = crusading_claim_gain_byz_emp_tooltip
#		}
#		custom_tooltip = crusading_claim_crusader_rewards_tooltip
#		hidden_effect = {
#			create_title_and_vassal_change = {
#				type = conquest_claim
#				save_scope_as = change
#				add_claim_on_loss = yes
#			}
#			# Create a claim CB which is executed to handle title/vassal changes, in addition to being used to calculate Prestige awards for war participants.
#			setup_claim_cb = {
#				titles = target_titles
#				attacker = scope:attacker
#				defender = scope:defender
#				claimant = scope:claimant
#				change = scope:change
#			}
#			resolve_title_and_vassal_change = scope:change
#		}
#
#		if = {
#			limit = {
#				scope:claimant != scope:attacker
#				scope:attacker = {
#					can_add_hook = {
#					  	type = favor_hook
#					  	target = scope:claimant
#					}
#				}
#			}
#			scope:attacker = {
#				add_hook = {
#		  			target = scope:claimant
#		  			type = favor_hook
#		 		}
#			}
#		}
#
#		# 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_claim_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 }
#
#		#THE LATINS MUST BE PAID
#		scope:claimant = {
#			trigger_event = ep3_frankokratia_events.0040
#		}
#
#		# 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 = claim_cb_white_peace_desc_defender
#			}
#			desc = claim_cb_white_peace_desc
#		}
#		
#	}
#
#	on_white_peace = {
#		scope:attacker = {
#			random_character_war = {
#				limit = {
#					using_cb = crusading_claim_cb
#				}
#				every_war_attacker = {
#					save_temporary_scope_as = war_attackers
#					scope:attacker = {
#						random_owned_story = {
#							type = frankokratia_story
#							add_to_variable_list = {
#								name = frankokratia_leaders
#								target = scope:war_attackers
#							}
#						}
#					}
#				}
#			}
#			random_owned_story = {
#				type = frankokratia_story
#				set_variable = {
#					name = franko_war_complete
#				}
#			}
#		}
#
#		scope:attacker = {
#			show_pow_release_message_effect = yes
#			stress_impact = {
# 				ambitious = medium_stress_impact_gain
# 				arrogant = medium_stress_impact_gain
# 			}
#		}
#
#		#EP2 accolade glory gain for defender doing alright
#		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }
#
#		# press claims
#		every_in_list = {
#			list = target_titles
#			if = {
#				limit = { scope:claimant = { has_weak_claim_on = prev } }
#				scope:claimant = {
#					make_claim_strong = prev
#				}
#			}
#		}
#
#		# Create an 'helper' claim CB which we won't execute, but can use to determine how much Prestige should be given to war participants.
#		setup_claim_cb = {
#			titles = target_titles
#			attacker = scope:attacker
#			defender = scope:defender
#			claimant = scope:claimant
#			victory = no
#		}
#		# Prestige and piety loss for the attacker
#		scope:attacker = {
#			add_piety = major_piety_loss
#		}
#		# Attackers all loses Prestige, defenders gain it
#		modify_all_participants_fame_values_crusading_claim = {
#			WINNER = scope:defender
#			LOSER = scope:attacker
#			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
#			WINNER_FAME_SCALE = scale_10_war_defender_win
#			LOSER_FAME_SCALE = -10
#			WINNER_ALLY_FAME_SCALE = 10
#			LOSER_ALLY_FAME_SCALE = 10
#		}
#		# Defender neither gains nor loses any prestige.
#		scope:defender = {
#			stress_impact = {
# 				arrogant = medium_stress_impact_gain
# 			}
#		}
#
#		if = {
#			limit = {
#				scope:claimant != scope:attacker
#				scope:attacker = {
#					can_add_hook = {
#					  	type = favor_hook
#					  	target = scope:claimant
#					}
#				}
#			}
#			scope:attacker = {
#				add_hook = {
#		  			target = scope:claimant
#		  			type = favor_hook
#		 		}
#			}
#		}
#
#		# Truce
#		add_truce_white_peace_effect = yes
#
#
#		#General event that tells all players what happened
#		every_player = {
#			limit = {
#				OR = {
#					capital_province = {
#						OR = {
#							geographical_region = world_europe
#							geographical_region = world_asia_minor
#							geographical_region = world_middle_east
#							geographical_region = world_africa_north
##							county = {
##								OR = {
##									empire = title:e_caspian-pontic_steppe
##									empire = title:e_byzantium
##								}
##							}
#						}
#					}
#					liege = scope:defender
#					OR = { faith.religion = religion:eastern_orthodox_religion faith.religion = religion:catholic_religion faith.religion = religion:protestant_religion }
#				}
#			}
#			add_character_flag = frankokratia_crusaders_lose_outcome
#			add_character_flag = frankokratia_crusaders_white_peace_outcome
#			trigger_event = {
#				id = ep3_frankokratia_events.0060
#				days = 1
#			}
#		}
#		#Force end on story owner to be safe (necessary for AI owner)
#		if = {
#			limit = {
#				global_var:byz_claimant_champion ?= {
#					any_owned_story = {
#						type = frankokratia_story
#					}
#				}
#			}
#			global_var:byz_claimant_champion = {
#				trigger_event = {
#					id = ep3_frankokratia_events.0061
#					days = 3
#				}
#			}
#		}
#
#		# 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 = claim_cb_defeat_desc_defender
#			}
#			triggered_desc = {
#				trigger = { scope:claimant = { is_local_player = yes } }
#				desc = claim_cb_defeat_desc_attacker_claimant
#			}
#			triggered_desc = {
#				trigger = { scope:attacker = { is_local_player = yes } }
#				desc = claim_cb_defeat_desc_attacker
#			}
#			desc = claim_cb_defeat_desc
#		}
#		
#	}
#
#	on_defeat = {
#		scope:attacker = {
#			random_character_war = {
#				limit = {
#					using_cb = crusading_claim_cb
#				}
#				every_war_attacker = {
#					save_temporary_scope_as = war_attackers
#					scope:attacker = {
#						random_owned_story = {
#							type = frankokratia_story
#							add_to_variable_list = {
#								name = frankokratia_leaders
#								target = scope:war_attackers
#							}
#						}
#					}
#				}
#			}
#			random_owned_story = {
#				type = frankokratia_story
#				set_variable = {
#					name = franko_war_complete
#				}
#			}
#		}
#		scope:attacker = { show_pow_release_message_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
#		every_in_list = {
#			list = target_titles
#			scope:claimant = {
#				remove_claim = prev
#			}
#		}
#
#		if = {
#			limit = {
#				scope:claimant != scope:attacker
#				scope:attacker = {
#					can_add_hook = {
#					  	type = favor_hook
#					  	target = scope:claimant
#					}
#				}
#			}
#			scope:attacker = {
#				add_hook = {
#		  			target = scope:claimant
#		  			type = favor_hook
#		 		}
#			}
#		}
#
#		# Attacker pays gold to the defender as reparations.
#		scope:attacker = {
#			pay_short_term_gold_reparations_effect = {
#				GOLD_VALUE = 3
#			}
#			#Loses piety for this debacle
#			add_piety = massive_piety_loss
#		}
#		#Defender should gain piety
#		scope:defender = {
#			add_piety = massive_piety_gain
#		}
#		# Create an 'helper' claim CB which we won't execute, but can use to determine how much Prestige should be given to war participants.
#		setup_claim_cb = {
#			titles = target_titles
#			attacker = scope:attacker
#			defender = scope:defender
#			claimant = scope:claimant
#			victory = no
#		}
#		# Attackers all loses Prestige, defenders gain it
#		modify_all_participants_fame_values_crusading_claim = {
#			WINNER = scope:defender
#			LOSER = scope:attacker
#			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
#			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
#
#
#		#General event that tells all players what happened
#		every_player = {
#			limit = {
#				OR = {
#					capital_province = {
#						OR = {
#							geographical_region = world_europe
#							geographical_region = world_asia_minor
#							geographical_region = world_middle_east
#							geographical_region = world_africa_north
##							county = {
##								OR = {
##									empire = title:e_caspian-pontic_steppe
##									empire = title:e_byzantium
##								}
##							}
#						}
#					}
#					liege = scope:defender
#					
#					OR = { faith.religion = religion:eastern_orthodox_religion faith.religion = religion:catholic_religion faith.religion = religion:protestant_religion }
#				}
#			}
#			add_character_flag = frankokratia_crusaders_lose_outcome
#			trigger_event = {
#				id = ep3_frankokratia_events.0060
#				days = 1
#			}
#		}
#		#Force end on story owner to be safe (necessary for AI owner)
#		if = {
#			limit = {
#				global_var:byz_claimant_champion ?= {
#					any_owned_story = {
#						type = frankokratia_story
#					}
#				}
#			}
#			global_var:byz_claimant_champion = {
#				trigger_event = {
#					id = ep3_frankokratia_events.0061
#					days = 3
#				}
#			}
#		}
#
#		# 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 = "CRUSADING_CLAIM_WAR_NAME"
#	my_war_name = "CRUSADING_CLAIM_WAR_NAME_MY"
#	war_name_base = "CRUSADING_CLAIM_WAR_NAME_BASE"
#	cb_name = "CRUSADING_CLAIM_CB_NAME"
#	interface_priority = 60
#
#	should_show_war_goal_subview = yes
#
#	attacker_wargoal_percentage = 0.8
#	
#	max_defender_score_from_occupation = 50
#	max_attacker_score_from_occupation = 150
#
#	max_ai_diplo_distance_to_title = 10000
#}
#
#ep3_hasan_assassin_war = {
#	icon = dissolution_war
#	group = event
#	allow_hostages = no
#	allowed_for_character = {}
#	valid_to_start = {
#		always = no
#	}
#
#	on_declaration = {
#		on_declared_war = yes
#	}
#
#	on_victory_desc = {
#		first_valid = {
#			triggered_desc = {
#				trigger = { # Desc for only one player attacker
#					scope:attacker = {
#						is_local_player = yes
#					}
#				}
#				desc = nation_fracturing_faction_war_victory_desc_local_player_attacker_alone
#			}
#			triggered_desc = { # Desc for only one attacker, player defender
#				trigger = {
#					scope:defender = {
#						is_local_player = yes
#					}
#				}
#				desc = player_nation_fracturing_faction_war_victory_desc_local_player_defender_attacker_alone
#			}
#			desc = nation_fracturing_faction_war_victory_desc_local_player_attacker_alone # Desc for a third party involved
#		}
#
#	}
#
#	on_victory = {
#
#		scope:attacker = { show_pow_release_message_effect = yes }
#
#		#EP2 Accolade glory gain from winning against higher ranked enemy
#		scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }
#		hasan_sabbah_end_war = {
#			HASAN = scope:attacker
#			SELJUK = scope:defender
#		}
#
#		# 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 = player_nation_fracturing_faction_war_white_peace_defender_desc
#			}
#			triggered_desc = {
#				trigger = {
#					scope:attacker = {
#						is_local_player = yes
#					}
#				}
#				desc = player_nation_fracturing_faction_war_white_peace_attacker_desc
#			}
#			desc = player_nation_fracturing_faction_war_white_peace_desc
#		}
#
#	}
#
#	on_white_peace = {
#		scope:attacker = {
#			show_pow_release_message_effect = yes
#			stress_impact = {
# 				ambitious = medium_stress_impact_gain
# 				arrogant = medium_stress_impact_gain
# 			}
#		}
#		hidden_effect = {
#			scope:attacker = {
#				add_truce_both_ways = {
#					character = scope:defender
#					days = 1825
#					war = root.war
#					result = white_peace
#				}
#			}
#		}
#
#		scope:defender = {
#			stress_impact = {
# 				arrogant = medium_stress_impact_gain
# 			}
#
#			add_character_flag = {
#				flag = recent_nation_fracturing_faction_war
#				years = faction_nation_fracturing_war_white_peace_cooldown
#			}
#			add_prestige = minor_prestige_value
#		}
#
#		on_white_peace_faction_revolt_war = yes
#		# End Hasan's story cycle
#		hidden_effect = {
#			scope:attacker = {
#				if = {
#					limit = {
#						any_owned_story = {
#							type = story_hasan
#						}
#					}
#					random_owned_story = {
#						type = story_hasan
#						end_story = 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 = nation_fracturing_faction_war_defeat_defender_desc
#			}
#			triggered_desc = {
#				trigger = {
#					scope:attacker = {
#						is_local_player = yes
#					}
#				}
#				desc = player_nation_fracturing_defeat_attacker_desc
#			}
#			desc = player_nation_fracturing_faction_war_defeat_desc
#		}
#
#	}
#
#	on_defeat = {
#		scope:attacker = { show_pow_release_message_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 for Defender
#			add_prestige_war_defender_effect = {
#				PRESTIGE_VALUE = medium_prestige_value
#			}
#			add_character_flag = {
#				flag = recent_nation_fracturing_faction_war
#				years = faction_nation_fracturing_war_defeat_cooldown
#			}
#			add_dread = medium_dread_gain
#			add_achievement_flag_effect = { FLAG = achievement_know_your_place_flag }
#
#			# LEGITIMACY FROM WINNING FACTION WAR
#			faction_war_end_victory_legitimacy_effect = yes
#		}
#		# End Hasan's story cycle
#		hidden_effect = {
#			scope:attacker = {
#				if = {
#					limit = {
#						any_owned_story = {
#							type = story_hasan
#						}
#					}
#					random_owned_story = {
#						type = story_hasan
#						end_story = yes
#					}
#				}
#			}
#		}
#
#		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
#		laamp_as_mercenary_payout_tooltip_effect = yes
#	}
#
#	on_invalidated_desc = msg_invalidate_war_title
#
#	check_defender_inheritance_validity = no
#
#	on_primary_attacker_death = inherit
#	on_primary_defender_death = inherit
#
#	attacker_allies_inherit = yes
#	defender_allies_inherit = yes
#
#	transfer_behavior = transfer
#
#	war_name = "ASSASSIN_NATION_FRACTURING_WAR_NAME"
#
#	interface_priority = 120
#
#	attacker_wargoal_percentage = 0.8
#
#	max_attacker_score_from_battles = 100
#	max_defender_score_from_battles = 50
#	
#	max_defender_score_from_occupation = 150
#	max_attacker_score_from_occupation = 150
#
#	max_ai_diplo_distance_to_title = 500
#}
#
imperial_expedition_cb = {
	icon = invasion
	group = conquest

	combine_into_one = yes
	should_show_war_goal_subview = yes
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	allowed_for_character = {
		top_liege != this
		liege ?= { has_diarchy_active_parameter = unlock_diarch_requests_military_expedition_interaction }
		is_diarch_of_target = liege
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}

	allowed_for_character_display_regardless = {
		custom_tooltip = {
			text = imperial_expedition_cb.tt.requires_expedition_charge
			has_variable = imperial_expedition_charged
		}
		
		# Struggle constraint
		custom_tooltip = {
			text = struggle_involvement_cb_check_failure
			struggle_blocks_invasion_conquest_cb_trigger = yes
		}

		NOR = {
			scope:defender ?= { is_allied_to = scope:attacker.top_liege }
			# Stop the AI accidentally attacking allies due to forced wars.
			scope:attacker ?= {
				is_ai = yes
				scope:defender ?= { is_allied_to = scope:attacker }
			}
		}
	}

	allowed_against_character = {
		scope:attacker = {
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = all
	target_title_tier = duchy
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	ai_can_target_all_titles = { always = yes }
	ai_score = {
		value = byzantium_conquests_ai_score_value

		# As a feudal gov, do not attack tribal
		add = {
			every_in_list = {
				list = target_titles
				if = {
					limit = { feudal_clan_tribal_conquest_constraints = yes }
					add = -1000
				}
			}
		}
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		multiply = byzantium_conquests_ai_score_value
	}
	ai_score_mult = {
		value = 1
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		scope:target ?= {
			OR = {
				any_this_title_or_de_jure_above = { holder ?= scope:attacker.liege }
				any_title_to_title_neighboring_duchy = { holder.top_liege ?= scope:attacker.liege }
			}
		}
	}

	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_religious_war_invalidation_region_message

	on_invalidated = {
		# Give us our charge back.
		scope:attacker = {
			set_variable = {
				name = imperial_expedition_charged
				value = yes
			}
		}
	}

	cost = {
		piety = {
			add = common_cb_impious_piety_cost
			if = {
				limit = {
					scope:defender.faith = scope:attacker.faith
				}
				add = {
					value = 100
					desc = CB_SAME_FAITH_COST
				}
				add = {
					value = 10
					multiply = scope:attacker.realm_size
					desc = CB_REALM_SIZE_COST
					max = 800
				}
			}
			multiply = common_cb_piety_cost_multiplier
			# We want the AI to do this AQAP, so make it free for them.
			if = {
				limit = {
					scope:attacker = { is_ai = yes }
				}
				multiply = 0
			}
		}
		prestige = {
			value = 0
			if = {
				limit = {
					scope:defender.faith != scope:attacker.faith
				}
				add = {
					value = 150
					desc = CB_COST_DIFFERENT_FAITH
				}
				add = {
					value = 10
					multiply = scope:attacker.realm_size
					desc = CB_REALM_SIZE_COST
					max = 800
				}
			}
			multiply = common_cb_prestige_cost_multiplier
			if = {
				limit = {
					scope:attacker.house = {
						OR = {
							has_house_modifier = fp2_struggle_hostility_house_culture_modifier
							has_house_modifier = fp2_struggle_hostility_house_combined_modifier
						}
					}
					any_in_global_list = {
						variable = fp2_struggle_ending_culture_list
						this = scope:attacker.culture
					}
					any_in_global_list = {
						variable = fp2_struggle_ending_culture_list
						this = scope:defender.culture
					}
				}
				divide = {
					value = 2
					desc = CB_COST_FP2_STRUGGLE_HOSTILITY_CULTURE_DISCOUNT
				}
			}
			if = {
				limit = {
					has_global_variable = fp2_struggle_compromise_ending
					any_in_global_list = {
						variable = fp2_struggle_ending_culture_list
						this = scope:attacker.culture
					}
					any_in_global_list = {
						variable = fp2_struggle_ending_culture_list
						this = scope:defender.culture
					}
				}
				multiply = {
					value = 2
					desc = CB_COST_FP2_STRUGGLE_COMPROMISE_MULTIPLIER
				}
			}
			# We want the AI to do this AQAP, so make it free for them.
			if = {
				limit = {
					scope:attacker = { is_ai = yes }
				}
				multiply = 0
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			# Clear our variable.
			remove_variable = imperial_expedition_charged
			# Forcibly add everyone appropriate to the war.
			scope:defender = {
				every_realm_border_county = {
					every_neighboring_county = {
						holder = {
							if = {
								limit = {
									government_has_flag = government_is_administrative
									top_liege = scope:attacker.top_liege
									NOR = {
										is_allied_to = scope:defender
										is_at_war_with = scope:defender
										is_at_war_with = scope:attacker
									}
								}
								trigger_event = diarchy.8005
							}
						}
					}
				}
			}
			liege = {
				every_vassal = {
					limit = {
						government_has_flag = government_is_administrative
						NOR = {
							is_allied_to = scope:defender
							is_at_war_with = scope:defender
							is_at_war_with = scope:attacker
							this = scope:attacker
							has_character_flag = pinged_to_join_expedition
						}
						vassal_contract_has_flag = admin_martial_obligation_bonus
						# Tried to add naval too but it defeated me; we wind up iterating over _every_ coastal province in the game — other than the Caspian — _multiple_ times. No good way to do it performantly without code support.
					}
					trigger_event = diarchy.8006
				}
			}
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = imperial_expedition_cb_victory_desc_attacker
			}
			desc = imperial_expedition_cb_victory_desc
		}
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		# Track which titles scope:attacker already has, so that we can leave them with holdings that should stay under their pre-existing titles if they opt to hand their gains over.
		scope:attacker = {
			every_held_title = {
				title_tier >= duchy
				add_to_list = attacker_pre_held_titles
			}
		}

		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# go through the dejure hierarchy under target titles, transfer eligible vassals and sieze counties from ineligible ones
		every_in_list = {
			list = target_titles
			custom_tooltip = CONQUEST_CB_TITLE

			expedition_cb_title_transfer = { RELIGIOUS_WAR = yes }
		}

		hidden_effect = {
			every_in_list = {
				list = vassals_taken
				change_liege = {
					liege = scope:attacker
					change = scope:change
				}
			}
		}

		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		resolve_title_and_vassal_change = scope:change

		# Prestige Progress for the Attacker
		scope:attacker = {
			add_prestige_experience = {
				every_in_list = {
					list = target_titles
					add = medium_prestige_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 = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_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 }
		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }

		# Aaaaand fire off the consequences event.
		scope:attacker = { trigger_event = diarchy.8011 }

		# 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 = imperial_expedition_cb_white_peace_desc_defender
			}
			desc = imperial_expedition_cb_white_peace_desc
		}
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }
		# Prestige loss for the attacker

		#EP2 accolade glory gain for doing pretty good against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

		scope:attacker = {
			add_prestige = {
				value = minor_prestige_value
				multiply = -1.0
			}
		}

		# 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 impactful as the scale are identical
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}


		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 = imperial_expedition_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = imperial_expedition_cb_defeat_desc_attacker
			}
			desc = imperial_expedition_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
			# Prestige for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_prefer_greater_income_effect = { FACTOR = 5 }
			add_prestige = {
				value = major_prestige_value
				multiply = -1.0
			}
		}

		# 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 = "IMPERIAL_EXPEDITION_WAR_NAME"
	war_name_base = "IMPERIAL_EXPEDITION_WAR_NAME_BASE"
	cb_name = "IMPERIAL_EXPEDITION_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

	# Always, always, _always_ do this.
	## Rationale: you request this via interaction, so we don't want you to get a request then just nothing happens for 20 years.
	## We don't do it manually because Code gets antsy if we sort complex war targeting from script.
	### We've got a little hack in anyway but it's just a teensy lil guy.
	ai_score = 9999
}

ep3_pillaging_foray = {
	icon = ducal_conquest_cb
	group = conquest

	combine_into_one = yes
	should_show_war_goal_subview = yes
	allow_hostages = no
	mutually_exclusive_titles = { always = yes }
	interface_priority = 110
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	allowed_for_character = {
		has_character_flag = warmongering_admin
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}

	allowed_against_character = {
		# Defender must be bordering your realm.
		OR = {
			character_is_land_realm_neighbor = scope:attacker
			AND = {
				exists = scope:attacker.liege
				character_is_land_realm_neighbor = scope:attacker.liege
			}
		}
		top_liege = this
		OR = {
			scope:attacker = {
				top_liege = this
			}
			NOR = {
				has_truce = scope:attacker.liege
				is_vassal_or_below_of = scope:attacker.liege
			}
		}
		NOT = {
			is_liege_or_above_of = scope:attacker
		}
	}

	target_titles = neighbor_land # border
	target_title_tier = county # county
	target_de_jure_regions_above = no
	ignore_effect = change_title_holder

	ai_score_mult = {
		value = fp2_border_raid_ai_targeting_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		exists = scope:target
		NOT = {
			scope:target.title_province = { has_province_modifier = recently_looted_modifier }
		}
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				holder = { target_is_same_character_or_above = scope:defender }
			}
		}
	}

	on_invalidated_desc = msg_county_conquest_war_invalidated_message
	
	on_invalidated = {}

	cost = {
		influence = {
			value = minor_influence_value
		}
	}

	on_declaration = {
		on_declared_war = yes
	}

	on_victory_desc = {
		triggered_desc = {
			trigger = {
				scope:attacker = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_victory_attacker
		}
		triggered_desc = {
			trigger = {
				scope:defender = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_victory_defender
		}
	}

	on_victory = {
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		scope:attacker = {
			if = {
				limit = {
					NOT = {
						has_character_modifier = ep3_loyal_troops_raid_modifier
					}
				}
				add_character_modifier = ep3_loyal_troops_raid_modifier
				custom_tooltip = ep3_war_modifier_improve_tooltip
			}
			else_if = {
				limit = {
					NOT = {
						has_character_modifier = ep3_loyal_troops_2_raid_modifier
					}
				}
				add_character_modifier = ep3_loyal_troops_2_raid_modifier
				remove_character_modifier = ep3_loyal_troops_raid_modifier
				custom_tooltip = ep3_war_modifier_improve_tooltip
			}
			else_if = {
				limit = {
					NOT = {
						has_character_modifier = ep3_loyal_troops_3_raid_modifier
					}
				}
				add_character_modifier = ep3_loyal_troops_3_raid_modifier
				remove_character_modifier = ep3_loyal_troops_2_raid_modifier
				custom_tooltip = ep3_war_modifier_improve_tooltip
			}
			else_if = {
				limit = {
					NOT = {
						has_character_modifier = ep3_loyal_troops_4_raid_modifier
					}
				}
				add_character_modifier = ep3_loyal_troops_4_raid_modifier
				remove_character_modifier = ep3_loyal_troops_3_raid_modifier
				custom_tooltip = ep3_war_modifier_improve_tooltip
			}
			else_if = {
				limit = {
					NOT = {
						has_character_modifier = ep3_loyal_troops_5_raid_modifier
					}
				}
				add_character_modifier = ep3_loyal_troops_5_raid_modifier
				remove_character_modifier = ep3_loyal_troops_4_raid_modifier
			}
			else = {
				change_influence = medium_influence_gain
			}
		}
		
		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		
		every_in_list = {
			list = target_titles
			save_scope_as = raid_county
			# Set raided province modifier
			title_province = {
				add_province_modifier = {
					modifier = recently_looted_modifier
					years = 5
				}
			}
			show_as_tooltip = {
				every_in_list = {
					list = target_titles
					fp2_border_raid_damage_effect = yes
				}
			}
			# Find province for building destruction
			ordered_county_province = {
				order_by = num_buildings
				random = {
					chance = 25
					modifier = { factor = destroy_building_soldier_multiplier_value } # chance increases with more soldiers
					modifier = { add = destroy_building_development_multiplier_value } # chance increases with development
					custom_tooltip = destroy_random_building_tt
					destroy_random_building_variable_effect = yes
				}
			}
		}

		hidden_effect = {
			scope:attacker = {
				send_interface_message = {
					type = event_raid_good
					title = border_raid_message_title
					show_as_tooltip = {
						every_in_list = {
							list = target_titles
							fp2_border_raid_damage_effect = yes
						}
					}
				}
			}
			scope:defender = {
				send_interface_message = {
					type = event_raid_bad
					title = border_raid_message_title
					every_in_list = {
						list = target_titles
						fp2_border_raid_damage_effect = yes
					}
				}
			}
		}

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

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# 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

		# 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 = {
		triggered_desc = {
			trigger = {
				scope:attacker = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_white_peace_attacker
		}
		triggered_desc = {
			trigger = {
				scope:defender = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_white_peace_defender
		}
	}

	on_white_peace = {
		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = miniscule_prestige_value
				multiply = -1.0
			}
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		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 = {
		triggered_desc = {
			trigger = {
				scope:attacker = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_defeat_attacker
		}
		triggered_desc = {
			trigger = {
				scope:defender = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_defeat_defender
		}
	}

	on_defeat = {
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes
		
		scope:attacker = {
			change_influence = medium_influence_value
		}
		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 for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

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

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		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 = invalidate
	on_primary_defender_death = inherit

	attacker_allies_inherit = no
	defender_allies_inherit = no

	war_name = "EP3_PILLAGING_FORAY_WAR_NAME"
	war_name_base = "EP3_PILLAGING_FORAY_WAR_NAME_BASE"
	cb_name = "EP3_PILLAGING_FORAY_CB_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	# The war will last 2 years at most
	defender_ticking_warscore_delay = { months = 3 }
	defender_ticking_warscore = 0.15

	attacker_wargoal_percentage = 1
	attacker_ticking_warscore = 1

	max_defender_score_from_occupation = 100
	max_attacker_score_from_occupation = 1

	ticking_war_score_targets_entire_realm = no
}
#
#ep3_roman_empire_border_war = {
#	icon = ducal_conquest_cb
#	group = invasion
#
#	combine_into_one = yes
#	should_show_war_goal_subview = yes
#	allow_hostages = no
#	mutually_exclusive_titles = { always = yes }
#	interface_priority = 110
#	
#	attacker_score_from_occupation_scale = 150
#	attacker_score_from_battles_scale = 150
#	defender_score_from_battles_scale = 150
#
#	allowed_for_character = {
#		OR = {
#			title:h_roman_empire ?= { is_title_created = yes }
#			title:h_eastern_roman_empire ?= { is_title_created = yes }
#			any_player = {
#				any_owned_story = {
#					type = ep3_story_cycle_restoring_rome
#				}
#			}
#			is_ai = no
#		}
#		top_liege = {
#			NOR = {
#				any_owned_story = {
#					type = ep3_story_cycle_restoring_rome
#				}
#				is_roman_emperor_excluding_byzantium_trigger = yes
#			}
#		}
#	}
#
#	allowed_for_character_display_regardless = {
#		tgp_japan_offensive_wars_ban_trigger = yes
#	}
#
#	allowed_against_character = {
#		is_landed = yes
#		top_liege = {
#			OR = {
#				any_owned_story = {
#					type = ep3_story_cycle_restoring_rome
#				}
#				is_roman_emperor_excluding_byzantium_trigger = yes
#			}
#		}
#	}
#
#	target_titles = neighbor_land # border
#	target_title_tier = county
#	target_de_jure_regions_above = no
#	ignore_effect = change_title_holder
#
#	ai_score_mult = {
#		value = 0.5
#		if = {
#			limit = { scope:attacker.prestige < scope:attacker.massive_prestige_value }
#			multiply = 0
#		}
#		
#		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
#	}
#
#	valid_to_start = {
#		exists = scope:target
#		NOT = {
#			scope:target.title_province = { has_province_modifier = recently_looted_modifier }
#		}
#	}
#
#	should_invalidate = {
#		NOT = {
#			any_in_list = {
#				list = target_titles
#				holder = { target_is_same_character_or_above = scope:defender }
#			}
#		}
#	}
#
#	on_invalidated_desc = msg_county_conquest_war_invalidated_message
#	
#	on_invalidated = {}
#
#	cost = {
#		prestige = massive_prestige_value
#	}
#
#	on_declaration = {
#		on_declared_war = yes
#	}
#
#	on_victory_desc = {
#		triggered_desc = {
#			trigger = {
#				scope:attacker = { is_local_player = yes }
#			}
#			desc = border_raid_cb_desc_victory_attacker
#		}
#		triggered_desc = {
#			trigger = {
#				scope:defender = { is_local_player = yes }
#			}
#			desc = border_raid_cb_desc_victory_defender
#		}
#	}
#
#	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_low_effect = yes }
#		
#		every_in_list = {
#			list = target_titles
#			save_scope_as = raid_county
#			# Set raided province modifier
#			title_province = {
#				add_province_modifier = {
#					modifier = recently_looted_modifier
#					years = 5
#				}
#			}
#			show_as_tooltip = {
#				every_in_list = {
#					list = target_titles
#					fp2_border_raid_damage_effect = yes
#				}
#			}
#			# Find province for building destruction
#			ordered_county_province = {
#				order_by = num_buildings
#				random = {
#					chance = 25
#					modifier = { factor = destroy_building_soldier_multiplier_value } # chance increases with more soldiers
#					modifier = { add = destroy_building_development_multiplier_value } # chance increases with development
#					custom_tooltip = destroy_random_building_tt
#					destroy_random_building_variable_effect = yes
#				}
#			}
#		}
#
#		hidden_effect = {
#			scope:attacker = {
#				send_interface_message = {
#					type = event_raid_good
#					title = border_raid_message_title
#					show_as_tooltip = {
#						every_in_list = {
#							list = target_titles
#							fp2_border_raid_damage_effect = yes
#						}
#					}
#				}
#			}
#			scope:defender = {
#				send_interface_message = {
#					type = event_raid_bad
#					title = border_raid_message_title
#					every_in_list = {
#						list = target_titles
#						fp2_border_raid_damage_effect = yes
#					}
#				}
#			}
#		}
#
#		show_as_tooltip = {
#			create_title_and_vassal_change = {
#				type = conquest
#				save_scope_as = change
#				add_claim_on_loss = yes
#			}
#			every_in_list = {
#				list = target_titles
#				change_title_holder = {
#					holder = scope:attacker
#					change = scope:change
#				}
#			}
#			resolve_title_and_vassal_change = scope:change
#		}
#
#		# Prestige Progress for the Attacker
#		every_in_list = {
#			list = target_titles
#			scope:attacker = {
#				add_prestige_experience = miniscule_prestige_value
#			}
#		}
#
#		# Prestige for the attacker's war allies
#		add_from_contribution_attackers = {
#			prestige = minor_prestige_value
#			opinion = {
#				modifier = contributed_in_war
#			}
#		}
#
#		# Prestige for the defender's war allies
#		add_from_contribution_defenders = {
#			prestige = minor_prestige_value
#			opinion = {
#				modifier = contributed_in_war
#			}
#		}
#
#		# 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
#
#		# 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 = {
#		triggered_desc = {
#			trigger = {
#				scope:attacker = { is_local_player = yes }
#			}
#			desc = border_raid_cb_desc_white_peace_attacker
#		}
#		triggered_desc = {
#			trigger = {
#				scope:defender = { is_local_player = yes }
#			}
#			desc = border_raid_cb_desc_white_peace_defender
#		}
#	}
#
#	on_white_peace = {
#		# Prestige loss for the attacker
#		scope:attacker = {
#			add_prestige = {
#				value = miniscule_prestige_value
#				multiply = -1.0
#			}
#			stress_impact = {
#				ambitious = medium_stress_impact_gain
#				arrogant = medium_stress_impact_gain
#			}
#		}
#
#		# Prestige for the attacker's war allies
#		add_from_contribution_attackers = {
#			prestige = minor_prestige_value
#			opinion = {
#				modifier = contributed_in_war
#			}
#		}
#
#		# Prestige for the defender's war allies
#		add_from_contribution_defenders = {
#			prestige = minor_prestige_value
#			opinion = {
#				modifier = contributed_in_war
#			}
#		}
#
#		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 = {
#		triggered_desc = {
#			trigger = {
#				scope:attacker = { is_local_player = yes }
#			}
#			desc = border_raid_cb_desc_defeat_attacker
#		}
#		triggered_desc = {
#			trigger = {
#				scope:defender = { is_local_player = yes }
#			}
#			desc = border_raid_cb_desc_defeat_defender
#		}
#	}
#
#	on_defeat = {
#		# 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 for Defender
#			add_prestige_war_defender_effect = {
#				PRESTIGE_VALUE = medium_prestige_value
#			}
#		}
#
#		# Prestige loss for the attacker
#		scope:attacker = {
#			pay_short_term_gold_reparations_effect = {
#				GOLD_VALUE = 1
#			}
#			add_prestige = {
#				value = medium_prestige_value
#				multiply = -1.0
#			}
#		}
#
#		# Prestige for the attacker's war allies
#		add_from_contribution_attackers = {
#			prestige = minor_prestige_value
#			opinion = {
#				modifier = contributed_in_war
#			}
#		}
#
#		# Prestige for the defender's war allies
#		add_from_contribution_defenders = {
#			prestige = minor_prestige_value
#			opinion = {
#				modifier = contributed_in_war
#			}
#		}
#
#		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 = invalidate
#	on_primary_defender_death = inherit
#
#	attacker_allies_inherit = no
#	defender_allies_inherit = no
#
#	war_name = "EP3_BORDER_RAID_WAR_NAME"
#	war_name_base = "EP3_BORDER_RAID_WAR_NAME_BASE"
#	cb_name = "EP3_BORDER_RAID_CB_NAME"
#
#	interface_priority = 79
#
#	use_de_jure_wargoal_only = yes
#
#	# The war will last 2 years at most
#	defender_ticking_warscore_delay = { months = 3 }
#	defender_ticking_warscore = 0.15
#
#	attacker_wargoal_percentage = 1
#	attacker_ticking_warscore = 1
#
#	max_defender_score_from_occupation = 100
#	max_attacker_score_from_occupation = 1
#
#	ticking_war_score_targets_entire_realm = no
#}
#
ep3_laamp_apprehend_adventurer_cb = {
	icon = apprehend_adventurer
	group = event
	allow_hostages = no
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	target_top_liege_if_outside_realm = no

	allowed_for_character = {}
	allowed_against_character = {}

	valid_to_start = { always = no }

	should_invalidate = {
		OR = {
			scope:attacker = { is_landless_adventurer = no }
			scope:defender = { is_landed = no }
		}
	}

	on_invalidated_desc = {
		desc = msg_invalidate_war_title
	}

	on_declaration = { on_declared_war = yes }

	on_victory_desc = { desc = ep3_laamp_apprehend_adventurer_cb_victory_desc }

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		#EP2 Accolade glory gain from winning against higher ranked enemy
		scope:defender = {
			accolade_defender_war_end_glory_gain_low_effect = yes
			add_prestige = { value = major_prestige_gain }
		}
		scope:defender = {
			switch = {
				trigger = scope:defender.primary_title.tier
				tier_county = {
					add_prestige = { value = minor_prestige_loss }
				}
				tier_duchy = {
					add_prestige = { value = medium_prestige_loss }
				}
				tier_kingdom = {
					add_prestige = { value = major_prestige_loss }
				}
				tier_empire = {
					add_prestige = { value = massive_prestige_loss }
				}
			}
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
				add_claim_on_loss = yes
			}
			every_held_title = {
				title_tier >= county
				limit = {
					is_landless_type_title = no
				}
				change_title_holder_include_vassals = {
					holder = scope:attacker
					change = scope:change
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
		# 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 = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		scope:defender = { save_temporary_scope_as = loser }
		on_lost_aggression_war_discontent_loss = yes
		# Truce
		add_truce_attacker_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 = { desc = ep3_laamp_apprehend_adventurer_cb_white_peace_desc }

	on_white_peace = {
        scope:attacker = { 
            show_pow_release_message_effect = yes
            stress_impact = {
                ambitious = medium_stress_impact_gain
                arrogant = medium_stress_impact_gain
            }
            add_prestige = { value = medium_prestige_loss }
        }
        # 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
        }
        scope:defender = {
            stress_impact = {
                arrogant = medium_stress_impact_gain
            }
        }
        # 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 = { desc = ep3_laamp_apprehend_adventurer_cb_defeat_desc }

	on_defeat = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			accolade_attacker_war_end_glory_gain_low_effect = yes
		}
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:attacker
			IMPRISONER = scope:defender
		}
		destroy_laamp_effect = { ADVENTURER = scope:attacker }
		scope:attacker = {
			if = {
				limit = { gold > 0 }
				pay_short_term_gold = {
					gold = gold
					target = scope:defender
				}
			}
			every_character_artifact = {
				limit = { NOT = { has_variable = artifact_prevent_transfer } }
				set_owner = {
					target = scope:defender
					history = {
						type = taken_in_battle
						actor = scope:defender
						recipient = scope:attacker
						location = scope:attacker.location
					}
				}
			}
		}
		scope:defender = { 
			mandala_peacemaker_perk_serenity_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:defender
			FAME_BASE = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		# Truce
		add_truce_attacker_defeat_effect = yes

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

	full_occupation_by_defender_gives_victory = no
	full_occupation_by_attacker_gives_victory = yes

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "EP3_LAAMP_APPREHEND_ADVENTURER_CB_WAR_NAME"
	war_name_base = "EP3_LAAMP_APPREHEND_ADVENTURER_CB_WAR_NAME_BASE"
	cb_name = "EP3_LAAMP_APPREHEND_ADVENTURER_CB_NAME"
	interface_priority = 80

	ticking_war_score_targets_entire_realm = yes

	attacker_ticking_warscore = 0
	defender_ticking_warscore = 0.1
	defender_ticking_warscore_delay = { days = 1 }

	attacker_wargoal_percentage = 0.01
	
	attacker_score_from_occupation_scale = 200
	defender_score_from_occupation_scale = 0

	attacker_score_from_battles_scale = 75
	defender_score_from_battles_scale = 200

	max_attacker_score_from_battles = 200
	max_defender_score_from_battles = 200
	
	max_defender_score_from_occupation = 0
	max_attacker_score_from_occupation = 200

	landless_attacker_needs_armies = no
}

#greek_anarchy_cb = {
#	icon = duchy_expansion
#	group = conquest
#
#	combine_into_one = yes
#	should_show_war_goal_subview = yes
#	mutually_exclusive_titles = { always = yes }
#	
#	attacker_score_from_occupation_scale = 150
#	attacker_score_from_battles_scale = 150
#	defender_score_from_battles_scale = 150
#
#	allowed_for_character = {
#		#This counter determines whether the collapse is still relatively fresh
#		exists = global_var:byz_collapse_counter
#		#Either the character is under the Latin Emperor or the Byzantine Emperor doesn't exist
#		OR = {
#			NOT = {
#				exists = title:e_byzantium.holder
#			}
#			top_liege = {
#				this = title:e_latin_empire.holder
#			}
#			#Byzantines can use this against any latin empire lands
#			AND = {
#				top_liege = title:e_byzantium.holder
#				title:e_latin_empire = {
#					any_in_de_jure_hierarchy = {
#						tier <= tier_kingdom
#					}
#				}
#			}
#		}
#		#Must have interests within the danger zone
#		any_held_title = {
#			tier <= tier_empire
#			OR = {
#				this = title:e_byzantium
#				this = title:e_latin_empire
#				empire = title:e_byzantium
#				empire = title:e_latin_empire
#			}
#		}
#		#Byzantium must still have de jure land
#		title:e_byzantium = {
#			any_in_de_jure_hierarchy = {
#				tier <= tier_kingdom
#			}
#		}
#	}
#
#	allowed_against_character = {
#		scope:attacker = {
#			NOR = {
#				top_liege = scope:defender.top_liege
#				liege = scope:defender
#			}
#		}
#		#Must be within Byzantine empire
#		scope:defender = {
#			any_realm_county = {
#				OR = {
#					empire = title:e_byzantium
#					empire = title:e_latin_empire
#				}
#			}
#		}
#		#Byzantine emperor only uses this against Latin empire land
#		trigger_if = {
#			limit = {
#				top_liege = title:e_byzantium.holder 
#			}
#			scope:defender = {
#				any_realm_county = {
#					empire = title:e_latin_empire
#				}
#			}
#		}
#	}
#	target_titles = neighbor_land_or_water
#	target_title_tier = duchy
#	target_de_jure_regions_above = yes
#	ignore_effect = change_title_holder
#
#	ai_score_mult = {
#		value = struggle_conquest_ai_score_value
#		add = 1
#		# HOUSE RELATIONS
#		add = house_relation_ai_score_value
#		if = {
#			limit = {
#				scope:attacker.top_liege.primary_title = {
#					this = title:e_latin_empire
#				}
#			}
#			multiply = 5
#		}
#		# AI in struggles act insularly for wars that don't have a familial or legal basis.
#		multiply = muslim_invasions_prefer_nonmuslims_ai_score_value
#		multiply = byzantium_conquests_ai_score_value
#		if = {
#			limit = {
#				scope:attacker = {
#					OR = {
#						top_liege.primary_title = {
#							this = title:e_byzantium
#						}
#						culture = culture:greek
#						culture = {
#							any_parent_culture_or_above = {
#								this = culture:greek
#							}
#						}
#						faith = faith:orthodox
#						#Venetians...
#						AND = {
#							government_has_flag = government_is_republic
#							religion = religion:catholic_religion
#						}
#						has_trait = despoiler_of_byzantium
#					}
#				}
#			}
#			multiply = 3
#		}
#		
#		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
#	}
#
#	valid_to_start = {
#		#Needs to be a de jure duchy of byzlatinium
#		scope:target = {
#			OR = {
#				empire = title:e_byzantium
#				empire = title:e_latin_empire
#			}
#		}
#		#Only players and the extra-motivated should be attacking over water
#		scope:target = {
#			any_in_de_jure_hierarchy = {
#				tier = tier_county
#				OR = {
#					#You a player
#					scope:attacker = {
#						is_ai = no
#					}
#					#is a neighbor
#					any_neighboring_county = {
#						holder = {
#							target_is_same_character_or_above = scope:attacker
#						}
#					}
#					#YOU SERVE THE 'RIGHTFUL' EMPEROR
#					scope:attacker.top_liege = {
#						primary_title = {
#							OR = {
#								this = title:e_byzantium
#								this = title:e_latin_empire
#							}
#						}
#					}
#					#You are a sneaky republic
#					scope:attacker = {
#						government_has_flag = government_is_republic
#					}
#					#You have a naval theme
#					scope:attacker = {
#						government_has_flag = government_is_administrative
#						vassal_contract_has_flag = admin_theme_naval
#					}
#					#You are a gutsy bastard
#					scope:attacker = {
#						ai_greed >= high_positive_ai_value
#						ai_boldness >= high_positive_ai_value
#					}
#				}
#			}
#		}
#	}
#
#	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_religious_war_invalidation_region_message
#
#	on_invalidated = {
#	}
#
#	cost = {
#		piety = {
#			add = common_cb_impious_piety_cost
#			if = {
#				limit = {
#					scope:defender.faith = scope:attacker.faith
#				}
#				add = {
#					value = 100
#					desc = CB_SAME_FAITH_COST
#				}
#				add = {
#					value = 10
#					multiply = scope:attacker.realm_size
#					desc = CB_REALM_SIZE_COST
#					max = 800
#				}
#			}
#			multiply = common_cb_piety_cost_multiplier
#			#Increased cost if you aren't really one of the main players
#			if = {
#				limit = {
#					scope:attacker = {
#						NOR = {
#							top_liege.primary_title = {
#								OR = {
#									this = title:e_latin_empire
#									this = title:e_byzantium
#								}
#							}
#							culture = culture:greek
#							culture = {
#								any_parent_culture_or_above = {
#									this = culture:greek
#								}
#							}
#							has_trait = despoiler_of_byzantium
#						}
#					}
#				}
#				multiply = 2
#			}
#		}
#		prestige = {
#			value = 0
#			if = {
#				limit = {
#					scope:defender.faith != scope:attacker.faith
#				}
#				add = {
#					value = 150
#					desc = CB_COST_DIFFERENT_FAITH
#				}
#				add = {
#					value = 10
#					multiply = scope:attacker.realm_size
#					desc = CB_REALM_SIZE_COST
#					max = 800
#				}
#			}
#			multiply = common_cb_prestige_cost_multiplier
#			if = {
#				limit = {
#					exists = scope:attacker.house
#					scope:attacker.house = {
#						OR = {
#							has_house_modifier = fp2_struggle_hostility_house_culture_modifier
#							has_house_modifier = fp2_struggle_hostility_house_combined_modifier
#						}
#					}
#					any_in_global_list = {
#						variable = fp2_struggle_ending_culture_list
#						this = scope:attacker.culture
#					}
#					any_in_global_list = {
#						variable = fp2_struggle_ending_culture_list
#						this = scope:defender.culture
#					}
#				}
#				divide = {
#					value = 2
#					desc = CB_COST_FP2_STRUGGLE_HOSTILITY_CULTURE_DISCOUNT
#				}
#			}
#			if = {
#				limit = {
#					has_global_variable = fp2_struggle_compromise_ending
#					any_in_global_list = {
#						variable = fp2_struggle_ending_culture_list
#						this = scope:attacker.culture
#					}
#					any_in_global_list = {
#						variable = fp2_struggle_ending_culture_list
#						this = scope:defender.culture
#					}
#				}
#				multiply = {
#					value = 2
#					desc = CB_COST_FP2_STRUGGLE_COMPROMISE_MULTIPLIER
#				}
#			}
#			#Increased cost if you aren't really one of the main players
#			if = {
#				limit = {
#					scope:attacker = {
#						NOR = {
#							top_liege.primary_title = {
#								OR = {
#									this = title:e_latin_empire
#									this = title:e_byzantium
#								}
#							}
#							ep3_orthodox_faith_trigger = yes
#							faith = faith:hellenic_pagan
#							religion = religion:catholic_religion
#							has_trait = despoiler_of_byzantium
#						}
#					}
#				}
#				multiply = 2
#			}
#		}
#	}
#
#	on_declaration = {
#		on_declared_war = yes
#	}
#
#	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 = {
#		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 }
#
#		#Gain a little army
#		hidden_effect = {
#			scope:attacker = {
#				save_scope_as = army_receiver
#				greek_anarchy_army_spawn_effect = yes
#			}
#		}
#		create_title_and_vassal_change = {
#			type = conquest
#			save_scope_as = change
#			add_claim_on_loss = yes
#		}
#
#		# EP3 Laamp possibility warning
#		scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }
#
#		# go through the dejure hierarchy under target titles, transfer eligible vassals and sieze counties from ineligible ones
#		every_in_list = {
#			list = target_titles
#			custom_tooltip = CONQUEST_CB_TITLE
#
#			conquest_cb_title_transfer = {
#				RELIGIOUS_WAR = no
#			}
#		}
#
#		hidden_effect = {
#			every_in_list = {
#				list = vassals_taken
#				change_liege = {
#					liege = scope:attacker
#					change = scope:change
#				}
#			}
#		}
#
#		every_in_list = {
#			list = titles_taken
#			change_title_holder = {
#				holder = scope:attacker
#				change = scope:change
#				take_baronies = yes
#			}
#		}
#
#		resolve_title_and_vassal_change = scope:change
#
#		# Prestige Progress for the Attacker
#		every_in_list = {
#			list = target_titles
#			scope:attacker = {
#				add_prestige_experience = medium_prestige_value
#			}
#		}
#
#		custom_tooltip = greek_anarchy_development_loss_cb
#		hidden_effect = {
#			every_in_list = {
#				list = target_titles
#				every_in_de_jure_hierarchy = {
#					limit = {
#						tier = tier_county
#						OR = {
#							holder = scope:attacker
#							holder.top_liege = scope:attacker
#						}
#					}
#					change_development_progress_with_overflow = greek_anarchy_development_value
#				}
#			}
#		}
#
#		#Extra fun rewards
#		scope:attacker = {
#			#Tooltip
#			custom_tooltip = greek_anarchy_troops_tt
#			if = {
#				limit = {
#					government_has_flag = government_is_administrative
#					scope:defender = {
#						highest_held_title_tier >= tier_duchy
#					}
#				}
#				change_influence = medium_influence_gain
#			}
#			else_if = {
#				limit = {
#					top_liege = {
#						primary_title = title:e_latin_empire
#					}
#					NOR = {
#						ep3_orthodox_faith_trigger = yes
#						faith = faith:hellenic_pagan
#						culture = culture:greek
#						culture = {
#							any_parent_culture_or_above = {
#								this = culture:greek
#							}
#						}
#						top_liege = {
#							any_held_title = {
#								this = title:e_byzantium
#							}
#						}
#					}
#					scope:defender = {
#						highest_held_title_tier >= tier_duchy
#					}
#				}
#				add_dread = minor_dread_gain
#				add_gold = minor_gold_value
#			}
#			else = {
#				add_prestige = minor_prestige_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 = medium_prestige_value
#			IS_RELIGIOUS_WAR = no
#			WINNER_ALLY_FAME_SCALE = 1
#			LOSER_ALLY_FAME_SCALE = 1
#		}
#
#		# 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 }
#
#		if = { # Warning that only counties are changing hands
#			limit = {
#				any_in_list = {
#					list = target_titles
#					tier = tier_duchy
#				}
#			}
#			random_in_list = {
#				list = target_titles
#				limit = { tier = tier_duchy }
#				save_scope_as = de_jure_target
#			}
#		}
#		if = {
#			limit = {
#				any_in_list = {
#					list = target_titles
#					tier = tier_duchy
#					holder = scope:defender
#				}
#			}
#			if = {
#				limit = {
#					scope:defender = {
#						any_held_title = {
#							title_tier = county
#							NOT = { target_is_de_jure_liege_or_above = scope:de_jure_target }
#						}
#					}
#				}
#				custom_tooltip = conquest_title_not_transferred_tt
#			}
#			else = { custom_tooltip = conquest_title_destroyed_tt }
#		}
#		else = {
#			if = {
#				limit = {
#					any_in_list = {
#						list = target_titles
#						tier = tier_duchy
#						is_title_created = yes
#					}
#				}
#				custom_tooltip = conquest_title_usurp_tt
#			}
#			else = { custom_tooltip = conquest_title_create_tt }
#		}
#
#		# 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 = {
#		scope:attacker = { show_pow_release_message_effect = yes }
#		# Prestige loss for the attacker
#
#		#EP2 accolade glory gain for doing pretty good against higher ranked enemy
#		scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }
#
#		scope:attacker = {
#			add_prestige = {
#				value = minor_prestige_value
#				multiply = -1.0
#			}
#		}
#
#		# 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 impactful as the scale are identical
#			FAME_BASE = medium_prestige_value
#			IS_RELIGIOUS_WAR = no
#			WINNER_ALLY_FAME_SCALE = 1
#			LOSER_ALLY_FAME_SCALE = 1
#		}
#
#
#		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 = {
#		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 for Defender
#			add_prestige_war_defender_effect = {
#				PRESTIGE_VALUE = medium_prestige_value
#			}
#		}
#
#		# 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
#			}
#		}
#
#		# 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 = "GREEK_ANARCHY_WAR_NAME"
#	war_name_base = "GREEK_ANARCHY_WAR_NAME_BASE"
#	cb_name = "GREEK_ANARCHY_CB_NAME"
#
#	interface_priority = 120
#
#	use_de_jure_wargoal_only = yes
#
#	attacker_wargoal_percentage = 0.8
#	
#	max_defender_score_from_occupation = 150
#	max_attacker_score_from_occupation = 150
#}

ep3_laamp_peasant_war = {
	icon = invasion
	group = invasion
	ai_only_against_liege = yes
	combine_into_one = yes
	should_show_war_goal_subview = no
	allow_hostages = no
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder
	allowed_for_character =	{
		government_has_flag = government_is_landless_adventurer
		has_variable_list = war_supported_counties_list
	}

	allowed_against_character = {
		scope:attacker = {
			any_in_list = {
				variable = war_supported_counties_list
				holder = {
					OR = {
						this = scope:defender
						any_liege_or_above = {
							this = scope:defender
						}
					}
				}
			}
		}
	}
	target_top_liege_if_outside_realm = yes

	on_declaration = {
		scope:attacker = {
			add_trait = peasant_leader
			custom_tooltip = ep3_laamp_peasant_war.peasant_army
			hidden_effect = {
				#spawn peasant army as levies
				every_in_list = {
					variable = war_supported_counties_list
					set_variable = {
						name = number_rebel_reinforcements
						value = {
							value = 100
							add = {
								if = {
									limit = { has_county_modifier = small_investment_in_revolt_modifier }
									add = 250
								}
								if = {
									limit = { has_county_modifier = medium_investment_in_revolt_modifier }
									add = 750
								}
								if = {
									limit = { has_county_modifier = high_investment_in_revolt_modifier }
									add = 2000
								}
							}
							add = county_levies_to_raise
						}
					}
					scope:attacker = {
						#get a peasant you met earlier in the event chain into your army
						if = {
							limit = {
								variable_list_size = {
									name = war_met_peasant_leaders_list
									value > 0
								}	
							}
							random_in_list = {
								variable = war_met_peasant_leaders_list
								limit = {
									is_knight = no
								}
								scope:attacker = {
									add_courtier = prev
								}
								set_knight_status = force
								save_scope_as = met_peasant_leader
							}
						}
						#have to check if variable exists, although it will always exists, because the error log goes crazy
						if = {
							limit = {
								exists = prev.var:number_rebel_reinforcements
							}
							spawn_army = {
								name = peasant_faction_event_troops
								levies = prev.var:number_rebel_reinforcements
								location = prev.title_province
								war = scope:war
								save_scope_as = new_army
							}
						}
						#set that peasant as army commander
						if = {
							limit = { exists = scope:met_peasant_leader }
							scope:new_army ?= {
								assign_commander = scope:met_peasant_leader
							}
						}
					}
					remove_variable = number_rebel_reinforcements
				}
				if = {
					limit = {
						has_variable = ep3_laamp_peasant_war_remain_laamp
						variable_list_size = {
							name = war_met_peasant_leaders_list
							value <= 0
						}
					}
					ordered_in_list = {
						variable = war_supported_counties_list
						order_by = development_level
						save_scope_as = new_capital
					}
					create_character = {
						location = scope:new_capital.title_province
						template = peasant_faction_leader_template
						faith = scope:new_capital.faith
						culture = scope:new_capital.culture
						gender_female_chance = {
							if = {
								limit = { scope:new_capital.faith = { has_doctrine = doctrine_gender_male_dominated } }
								add = 0
							}
							else_if = {
								limit = { scope:new_capital.faith = { has_doctrine = doctrine_gender_female_dominated } }
								add = 100
							}
							else = {
								add = 50
							}
						}
						save_scope_as = peasant_leader
					}
					add_to_variable_list = {
						name = war_met_peasant_leaders_list
						target = scope:peasant_leader
					}
				}
			}
		}
				
		scope:defender = {
			add_opinion = {
				modifier = peasant_revolt_opinion
				target = scope:attacker
			}
			
			# Legitimacy loss because why would you have peasants revolting
			if = {
				limit = { is_valid_for_legitimacy_change = yes }
				add_legitimacy = medium_legitimacy_loss
			}

			# Struggle Stuffs
#			if = {
#				limit = {
#					any_character_struggle = {
#						phase_has_catalyst = catalyst_populist_uprise
#						involvement = involved
#					}
#				}
#				every_character_struggle = {
#					involvement = involved
#					activate_struggle_catalyst = {
#						catalyst = catalyst_populist_uprise
#						character = scope:defender
#					}
#				}
#			}
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_variable = ep3_laamp_peasant_war_remain_laamp
					}
				}
				desc = county_conquest_cb_victory_desc_remain_laamp
			}
			desc = county_conquest_cb_victory_desc_attacker
		}
	}

	on_victory = {
		scope:attacker = {
			ordered_in_list = {
				variable = war_supported_counties_list
				order_by = development_level
				save_scope_as = new_capital
			}
			primary_title = {
				save_scope_as = old_title
			}
			show_pow_release_message_effect = yes
			#set this to not display the event to keep playing as landless
			set_variable = became_landed_through_separatist_uprising_var
			#swap the peasant trait for the better one 
			remove_trait = peasant_leader
			add_trait = populist_leader
			if = {
				limit = {
					has_variable = ep3_laamp_peasant_war_remain_laamp
				}
				random_in_list = {
					variable = war_met_peasant_leaders_list
					limit = {
						this != scope:attacker
					}
					save_scope_as = peasant_leader
				}
				if = {
					limit = {
						NOT = { exists = scope:peasant_leader }
					}
					create_character = {
						location = scope:new_capital.title_province
						template = peasant_faction_leader_template
						faith = scope:new_capital.faith
						culture = scope:new_capital.culture
						gender_female_chance = {
							if = {
								limit = { scope:new_capital.faith = { has_doctrine = doctrine_gender_male_dominated } }
								add = 0
							}
							else_if = {
								limit = { scope:new_capital.faith = { has_doctrine = doctrine_gender_female_dominated } }
								add = 100
							}
							else = {
								add = 50
							}
						}
						save_scope_as = peasant_leader
					}
				}
			}
			else = {
				save_scope_as = peasant_leader
			}
		}
		# Set up titles in the new realm.
		# Determine potential breakaways
		create_title_and_vassal_change = {
			type = conquest
			add_claim_on_loss = yes
			save_scope_as = change
		}
		scope:attacker = {
			every_in_list = {
				variable = war_supported_counties_list
				custom_tooltip = CONQUEST_CB_TITLE
				every_in_de_jure_hierarchy = {
					continue = { tier >= tier_duchy }
					limit = {
						tier >= tier_county
						holder ?= {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}
					save_temporary_scope_as = title_temp
					if = {
						limit = {
							OR = {
								scope:peasant_leader.faith = {
									faith_hostility_level_comparison = { prev.holder.faith >= scope:defender.faith }
								}
								holder = scope:defender
								holder.primary_title.tier >= scope:target.tier
								scope:peasant_leader = { has_claim_on = scope:title_temp }
							}
						}
						add_to_list = titles_taken
					}
					else = {
						holder = { add_to_list = vassals_taken }
					}
				}
			}
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:peasant_leader
				change = scope:change
				take_baronies = yes
			}
		}
		hidden_effect = {
			every_in_list = {
				list = vassals_taken
				change_liege = {
					liege = scope:peasant_leader
					change = scope:change
				}
			}
		}
		resolve_title_and_vassal_change = scope:change
		## Grab the best county to be the capital
		scope:new_capital = {
			duchy = {
				save_scope_as = new_capital_duchy
			}
		}
		# Create a new unique title
		create_dynamic_title = {
			tier = duchy
			name = ADVENTURER_PEASANT_LEADER_DUCHY_NAME
			adj = ADVENTURER_PEASANT_LEADER_DUCHY_NAME_adj
		}
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		scope:new_title = {
			change_title_holder = {
				holder = scope:peasant_leader
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		#make the new title look like your landless adventurer title
		hidden_effect = {
			scope:new_title = {
				set_coa = scope:old_title
				set_color_from_title = scope:new_capital
				set_capital_county = scope:new_capital
			}
		}

		scope:peasant_leader = {
			if = {
				limit = {
					has_variable = ep3_laamp_peasant_war_remain_laamp
				}
				#stop being a landless adventurer
				destroy_laamp_effect = { ADVENTURER = scope:attacker }
				return_home = yes
			}
		}
		scope:defender = {
			# Legitimacy effect
			faction_war_end_defeat_legitimacy_effect = yes
			# Struggle Catalysts
#			hidden_effect = {
#				if = {
#					limit = {
#						is_important_or_vip_struggle_character = yes
#						any_character_struggle = {
#							involvement = involved
#							activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
#								CATALYST = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
#								CHAR = scope:attacker
#							}
#						}
#					}
#					every_character_struggle = {
#						involvement = involved
#						limit = {
#							activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
#								CATALYST = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
#								CHAR = scope:attacker
#							}
#						}
#						activate_struggle_catalyst = {
#							catalyst = catalyst_accepted_demand_from_faction_requesting_culture_faith_conversion
#							character = scope:defender
#						}
#					}
#				}
#			}
		}
		scope:attacker = {
			#increase county control and remove all county corruption modifiers
			every_in_list = {
				variable = war_supported_counties_list
				if = {
					limit = {
						scope:attacker = {
							has_variable = ep3_laamp_peasant_war_remain_laamp
						}
					}
					custom_tooltip = taken_county_remain_laamp
				}
				change_county_control = 100
				remove_every_county_corruption_modifier_effect = yes
				add_county_modifier = {
					modifier = ep3_peasant_leader_modifier
					years = 10
				}
			}
			clear_variable_list = war_supported_counties_list
			clear_variable_list = war_met_peasant_leaders_list
			remove_variable = ep3_laamp_peasant_war_remain_laamp
		}
		# 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 = {
		scope:attacker = { 
			show_pow_release_message_effect = yes
			#boost the county control up and remove the county corruption modifiers
			every_in_list = {
				variable = war_supported_counties_list
				change_county_control = 20
				remove_every_county_corruption_modifier_effect = yes
			}
			#clear all variable lists, you only get one chance!
			clear_variable_list = war_supported_counties_list
			clear_variable_list = war_met_peasant_leaders_list
			remove_variable = ep3_laamp_peasant_war_remain_laamp
			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 = county_conquest_cb_defeat_desc_defender
			}
			desc = county_conquest_cb_defeat_desc
		}
	}

	on_defeat = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			#boost the county control up and remove the county corruption modifiers
			every_in_list = {
				variable = war_supported_counties_list
				change_county_control = 50
				remove_every_county_corruption_modifier_effect = yes
			}
			#clear all variable lists, you only get one chance!
			clear_variable_list = war_supported_counties_list
			clear_variable_list = war_met_peasant_leaders_list
			remove_variable = ep3_laamp_peasant_war_remain_laamp
			#lose the trait, because you didn't win
			remove_trait = peasant_leader
		}
		scope:defender = {
			if = {
				limit = {
					NOT = { #In case defender has changed
						has_opinion_modifier = {
							modifier = peasant_revolt_opinion
							target = scope:attacker
						}
					}
				}
				add_opinion = {
					modifier = peasant_revolt_opinion
					target = scope:attacker
				}
			}
			if = {
				limit = {
					scope:attacker = { is_imprisoned = no }
				}
				imprison = {
					target = scope:attacker
					type = dungeon
				}
			}
			mandala_peacemaker_perk_serenity_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_invalidated_desc = msg_invalidate_war_title

	on_primary_attacker_death = invalidate
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = no
	defender_allies_inherit = yes

	war_name = "PEASANT_WAR_NAME"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_attacker_score_from_battles = 100
	max_defender_score_from_battles = 50

	max_ai_diplo_distance_to_title = 500
}

admin_barbarian_conquest_cb = {
	icon = duchy_expansion
	group = conquest

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }
	
	attacker_score_from_occupation_scale = 450
	defender_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 450
	defender_score_from_battles_scale = 150

	allowed_for_character = {
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
		is_landed = yes
	}

	allowed_for_character_display_regardless = {
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	allowed_against_character = {
		scope:defender = {
			government_allows = administrative
		}
		scope:attacker = {
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = duchy
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder
	
	ai_only_against_neighbors = yes
	
	ai_score_mult = {
		value = 3.5

		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
		multiply = muslim_invasions_prefer_nonmuslims_ai_score_value

		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		scope:target = {
			# You need to border the targeted area, unless it's coastal
			any_in_de_jure_hierarchy = {
				tier = tier_county
				OR = {
					is_coastal_county = yes
					any_neighboring_county = {
						holder = {
							target_is_same_character_or_above = root
						}
					}
				}
			}
			# You may never take the capital duchy
			this != scope:defender.top_liege.capital_county.duchy
			# If the top liege is an emperor or hegemon, we cannot target their de jure empire
			trigger_if = {
				limit = {
					scope:defender.top_liege.primary_title.tier >= tier_empire
				}
				empire != scope:defender.top_liege.capital_county.empire
			}
			# If the top liege is a king, we cannot target their de jure kingdom
			trigger_if = {
				limit = {
					scope:defender.top_liege.primary_title.tier = tier_kingdom
				}
				kingdom != scope:defender.top_liege.primary_title
			}
		}
	}

	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_religious_war_invalidation_region_message

	on_invalidated = {
	}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			value = 0
			# scale base value based on number of counties x 50
			add = {
				every_in_list = {
					list = target_titles
					every_in_de_jure_hierarchy = { # goes down, not up
						limit = {
							tier = tier_county
							holder.top_liege = scope:defender
						}
						add = 50
					}
				}
				desc = CB_CLAIM_COST_TARGET_TITLES
			}

			multiply = common_cb_prestige_cost_multiplier
		}
	}

	on_declaration = {
		on_declared_war = yes
	}

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

		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }

		# go through the dejure hierarchy under target titles, transfer eligible vassals and sieze counties from ineligible ones
		every_in_list = {
			list = target_titles
			custom_tooltip = CONQUEST_CB_TITLE

			conquest_cb_title_transfer = {
				RELIGIOUS_WAR = no
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}

		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		resolve_title_and_vassal_change = scope:change

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

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

		if = { # Warning that only counties are changing hands
			limit = {
				any_in_list = {
					list = target_titles
					tier = tier_duchy
				}
			}
			random_in_list = {
				list = target_titles
				limit = { tier = tier_duchy }
				save_scope_as = de_jure_target
			}
		}
		if = {
			limit = {
				any_in_list = {
					list = target_titles
					tier = tier_duchy
					holder = scope:defender
				}
			}
			if = {
				limit = {
					scope:defender = {
						any_held_title = {
							tier = tier_county
							NOT = { target_is_de_jure_liege_or_above = scope:de_jure_target }
						}
					}
				}
				custom_tooltip = conquest_title_not_transferred_tt
			}
			else = { custom_tooltip = conquest_title_destroyed_tt }
		}
		else = {
			if = {
				limit = {
					any_in_list = {
						list = target_titles
						tier = tier_duchy
						is_title_created = yes
					}
				}
				custom_tooltip = conquest_title_usurp_tt
			}
			else = { custom_tooltip = conquest_title_create_tt }
		}

		# 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 = {
		scope:attacker = { show_pow_release_message_effect = yes }
		# Prestige loss for the attacker

		#EP2 accolade glory gain for doing pretty good against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

		scope:attacker = {
			add_prestige = {
				value = minor_prestige_value
				multiply = -1.0
			}
		}

		# 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 impactful as the scale are identical
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		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 = {
		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 for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

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

		# 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 = "CONQUEST_WAR_NAME"
	war_name_base = "CONQUEST_WAR_NAME_BASE"
	cb_name = "BARBARIAN_ADMIN_CONQUEST_DUCHY_CB_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.5
	attacker_ticking_warscore = 0.165
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 450
}
