##################
# Purchase Land
# by Joe Parkin
##################

purchase_land_interaction = {
	category = interaction_category_laamp
	interface_priority = 30
	icon = purchase_land_interaction

	desc = purchase_land_interaction_desc
	target_type = title
	target_filter = recipient_de_jure_titles

	cooldown_against_recipient = { years = 10 }

	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:recipient = { top_liege != this }
					scope:target = {
						trigger_if = {
							limit = { tier = tier_duchy }
							scope:recipient.highest_held_title_tier = tier_duchy
						}
						trigger_else = { scope:recipient.highest_held_title_tier = tier_county }
					}
				}
				desc = PURCHASE_LAND_PROPOSAL_FELLOW_VASSAL
			}
			triggered_desc = {
				trigger = {
					scope:target = {
						trigger_if = {
							limit = { tier = tier_duchy }
							scope:recipient.highest_held_title_tier >= tier_kingdom
						}
						trigger_else = { scope:recipient.highest_held_title_tier >= tier_duchy }
					}
				}
				desc = PURCHASE_LAND_PROPOSAL_LIEGE
			}
			desc = PURCHASE_LAND_PROPOSAL
		}
	}

	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 60
	ai_min_reply_days = 4
	ai_max_reply_days = 9

	send_options_exclusive = no

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = {
			OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority }
		}
		scope:recipient = {
			is_landed = yes
			# Need to own the land/cannot directly buy a governorship (procure estate first)
			NOR = { 
				government_has_flag = government_is_administrative
				government_has_flag = government_is_japan_administrative
				government_has_flag = government_is_japan_feudal
				government_has_flag = government_is_celestial
				government_has_flag = government_is_meritocratic
			} 
		}
		scope:recipient != scope:actor
	}

	has_valid_target = {
		custom_tooltip = { #prevent unwanted trigger tooltip
			text = blank_line
			scope:target ?= {
				is_title_created = yes
				holder = {
					is_ai = yes
					this = scope:recipient
				}
				OR = { #flatten the selection
					tier = tier_duchy
					tier = tier_county
				}
				NOR = {
					this = scope:recipient.capital_county
					this = scope:recipient.capital_county.duchy
				}
			}
		}
	}

	send_option = {
		flag = offer_gold
		localization = purchase_land_interaction_offer_gold
		current_description = purchase_land_interaction_offer_gold_desc
	}

	send_option = {
		flag = hook
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		localization = GENERIC_SPEND_A_HOOK
	}

	send_option = {
		flag = offer_hook
		is_valid = {
			NOT = {
				scope:recipient = {
					has_usable_hook = scope:actor
				}
			}
		}
		localization = purchase_land_interaction_offer_hook
		current_description = purchase_land_interaction_offer_hook_desc
	}

	send_option = {
		flag = vassal_obligation
		is_shown = {
			scope:recipient = {
				government_has_flag = government_is_feudal
			}
		}
		localization = purchase_land_interaction_offer_obligations
		current_description = purchase_land_interaction_offer_obligations_desc
	}

	can_be_picked_title = {
		custom_tooltip = {
			text = purchase_land_tier_tt
			scope:target = {
				tier >= tier_county
				tier <= tier_duchy
			}
		}
		custom_tooltip = {
			text = purchase_land_capital_tt
			scope:target != scope:recipient.capital_county
		}
		custom_tooltip = {
			text = purchase_land_primary_tt
			scope:target != scope:recipient.primary_title
		}
		custom_tooltip = {
			text = purchase_land_duchy_tt
			scope:target = {
				trigger_if = {
					limit = { tier = tier_duchy }
					scope:actor = {
						prestige_level >= 3
					}
				}
			}
		}
		trigger_if = {
			limit = { exists = scope:target.holder }
			scope:target.holder = scope:recipient
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = { top_liege = this }
		scope:actor = {
			prestige_level >= 2
		}
		scope:actor = {
			NOT = {
				has_trait = gallowsbait
			}
		}
		scope:actor = {
			NOT = {
				has_trait = gallivanter
			}
		}
		scope:recipient = { is_at_war = no }
		trigger_if = { # Can't purchase Baronies
			limit = {
				scope:target ?= { tier = tier_barony }
			}
			custom_description = {
				text = purchase_land_barony_tt
				always = no
			}
		}
		trigger_if = { # Can't purchase Kingdoms or Empires
			limit = {
				scope:target ?= { tier >= tier_kingdom }
			}
			custom_description = {
				text = purchase_land_kingdom_tt
				always = no
			}
		}
		trigger_if = { # Can't purchase HoF titles
			limit = {
				scope:target ?= { is_head_of_faith = yes }
			}
			custom_description = {
				text = purchase_land_religious_tt
				always = no
			}
		}
		trigger_if = { # Can't purchase nf titles
			limit = {
				scope:target ?= { is_noble_family_title = yes }
			}
			custom_description = {
				text = purchase_land_noble_family_tt
				always = no
			}
		}
		trigger_if = { # Can't purchase laamp titles
			limit = {
				scope:target ?= { has_variable = adventurer_creation_reason }
			}
			custom_description = {
				text = purchase_land_landless_adventurer_tt
				always = no
			}
		}
		trigger_if = {
			limit = {
				scope:recipient = {
					any_held_county = { count < 2 }
				}
			}
			custom_description = {
				text = purchase_land_minimum_county_tt
				object = scope:recipient
				always = no
			}
		}
		scope:recipient = {
			NOT = { has_strong_hook = scope:actor }
		}
		#Has to be used instead of diplo range checks in laamp to landed interactions
		ep3_laamp_diplo_range_trigger = {
			TARGET = scope:recipient
			LAAMP = scope:actor
		}
		# Adventurers can't join wars in places they're exiled from.
		custom_tooltip = {
			text = the_wake_1
			NOT = {
				is_laamp_exiled_from_province_trigger = {
					PROVINCE = scope:recipient.capital_province
					LAAMP = scope:actor
				}
			}
		}
	}

	cost = {
		gold = {
			if = {
				limit = { scope:target.tier = tier_county }
				add = purchase_land_county_cost_value
			}
			else = {
				add = purchase_land_duchy_cost_value
				scope:target = {
					every_de_jure_county = {
						limit = { ep3_purchase_land_valid_county_trigger = yes }
						add = purchase_land_county_cost_value
					}
				}
			}
			if = {
				limit = { scope:offer_gold = yes }
				multiply = 2
			}
		}
	}

	on_accept = {
		scope:actor = { save_scope_as = adventurer }
		scope:target = {
			add_to_list = purchased_titles
			if = {
				limit = { tier = tier_duchy }
				every_de_jure_county = {
					limit = { ep3_purchase_land_valid_county_trigger = yes }
					add_to_list = purchased_titles
				}
			}
		}
		ordered_in_list = {
			list = purchased_titles
			order_by = tier
			save_scope_as = new_primary_title
		}
		save_scope_value_as = {
			name = purchase_land_cost
			value = {
				every_in_list = {
					list = purchased_titles
					if = {
						limit = { tier = tier_county }
						add = purchase_land_county_cost_value
					}
					else_if = {
						limit = { tier = tier_duchy }
						add = purchase_land_duchy_cost_value
					}
				}
				if = {
					limit = {
						scope:actor = { has_variable = ep3_grace_to_the_family_var }
					}
					multiply = 0.5
				}
				if = {
					limit = {
						scope:offer_gold = yes
					}
					multiply = 2
				}
			}
		}
		save_scope_value_as = {
			name = purchase_land_legitimacy_cost
			value = {
				value = 0
				if = {
					limit = {
						scope:target.holder != scope:recipient
					}
					every_in_list = {
						list = purchased_titles
						if = {
							limit = { tier = tier_county }
							add = minor_legitimacy_loss
						}
						else = { add = medium_legitimacy_loss }
					}
				}	
			}
		}
		show_as_tooltip = { purchase_land_interaction_shortened_effect = yes }
		if = {
			limit = { always = scope:hook }
			scope:actor = { use_hook = scope:recipient }
		}
		scope:actor = { trigger_event = ep3_laamps.0100 }
	}

	on_decline = {
		scope:actor = {
			save_scope_as = adventurer
			trigger_event = ep3_laamps.0102
		}
	}

	ai_targets = {
		ai_recipients = domicile_location_top_ruler
		ai_recipients = contacts
	}

	ai_targets = {
		ai_recipients = domicile_location_neighboring_top_realm_vassals
		ai_recipients = domicile_location_top_realm_vassals
		ai_recipients = domicile_location_neighboring_top_rulers
		max = 20
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 60
		kingdom = 0
		empire = 0
		hegemony = 0
	}

	ai_accept = {
		base = 0 # Try to make it 0 for most interactions

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1
			desc = AI_OPINION_REASON
		}
		modifier = { # Num of counties/duchy
			add = {
				value = 0
				scope:target = {
					if = {
						limit = { tier = tier_county }
						add = -30
					}
					else = {
						add = -60
						every_de_jure_county = {
							limit = { ep3_purchase_land_valid_county_trigger = yes }
							add = -30
						}
					}
				}
			}
			desc = PURCHASE_LAND_NUM_TITLES_REASON
		}
		modifier = { # Too close to capital
			add = -50
			desc = PURCHASE_LAND_DE_JURE_DUCHY_REASON
			scope:target.duchy = scope:recipient.capital_county.duchy
		}
		modifier = { # Province value
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = scope:target.development_level
					scope:target = {
						every_county_province = { add = num_buildings }
					}
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = { ep3_purchase_land_valid_county_trigger = yes }
							add = development_level
							every_county_province = { add = num_buildings }
						}
					}
				}
				multiply = -2
			}
			#add = 1
			desc = PURCHASE_LAND_VALUE_REASON
		}
		modifier = { # Land is foreign
			add = 10
			desc = PURCHASE_LAND_FOREIGN_COUNTY_CULTURE_REASON
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = 10
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_purchase_land_valid_county_trigger = yes
								culture != scope:recipient.culture
							}
							add = 10
						}
					}
				}
				max = 30
			}
			desc = PURCHASE_LAND_FOREIGN_CULTURE_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.culture != scope:recipient.culture
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_purchase_land_valid_county_trigger = yes
						culture != scope:recipient.culture
					}
				}
			}
		}
		modifier = { # Different faith
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = 10
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_purchase_land_valid_county_trigger = yes
								faith != scope:recipient.faith
							}
							add = 10
						}
					}
				}
				max = 30
			}
			desc = PURCHASE_LAND_DIFFERENT_FAITH_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.faith != scope:recipient.faith
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_purchase_land_valid_county_trigger = yes
						faith != scope:recipient.faith
					}
				}
			}
		}
		modifier = { # My culture's lands
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = -40
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_purchase_land_valid_county_trigger = yes
								culture != scope:recipient.culture
							}
							add = -40
						}
					}
				}
				if = {
					limit = { scope:acceptance != 0 }
					divide = scope:acceptance
				}
				multiply = 2
				min = -200
			}
			desc = PURCHASE_LAND_MY_CULTURE_REASON
			scope:actor.culture != scope:recipient.culture
			trigger_if = {
				limit = {
					scope:target.tier = tier_county
				}
				scope:target.culture = scope:recipient.culture
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_purchase_land_valid_county_trigger = yes
						culture = scope:recipient.culture
					}
				}
			}
			scope:recipient.culture = {
				save_temporary_scope_value_as = {
					name = acceptance
					value = {
						value = "cultural_acceptance(scope:actor.culture)"
						divide = 25
						ceiling = yes
					}
				}
			}
		}
		modifier = { # My faith's lands
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = -40
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_purchase_land_valid_county_trigger = yes
								faith != scope:recipient.faith
							}
							add = -40
						}
					}
				}
				if = {
					limit = { scope:hostility != 0 }
					multiply = scope:hostility
				}
				multiply = 2
				min = -200
			}
			desc = PURCHASE_LAND_MY_FAITH_REASON
			scope:actor.faith != scope:recipient.faith	
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.faith = scope:recipient.faith
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_purchase_land_valid_county_trigger = yes
						faith = scope:recipient.faith
					}
				}
			}
			scope:recipient.faith = {
				save_temporary_scope_value_as = {
					name = hostility
					value = "faith_hostility_level(scope:actor.faith)"
				}
			}
		}
		modifier = { # Too close
			add = {
				value = 0
				add = scope:distance
				divide = scope:recipient.highest_held_title_tier
				if = {
					limit = { scope:distance <= squared_distance_medium }
					multiply = -0.01
				}
				else = { multiply = 0.0005 }
				min = -25
				max = 50
			}
			desc = PURCHASE_LAND_DISTANCE_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target = { save_temporary_scope_as = distance_target }
			}
			trigger_else = {
				scope:target.title_capital_county = { save_temporary_scope_as = distance_target }
			}
			scope:recipient.capital_county = {
				save_temporary_scope_value_as = {
					name = distance
					value = "squared_distance(scope:distance_target)"
				}
			}
		}
		opinion_modifier = { # Opinion Factor of title holder
			trigger = {
				NOT = {
					scope:target.holder ?= scope:recipient
				}
			}
			who = scope:recipient
			opinion_target = scope:target.holder
			multiplier = -1
			desc = AI_OPINION_REASON
		}
		modifier = {
			add = {
				value = scope:actor.prestige_level
				multiply = 20
			}
			scope:actor.prestige_level >= 4
			desc = PURCHASE_LAND_PRESTIGE_LEVEL_REASON
		}
		modifier = {
			add = 40
			trigger = {
				scope:actor.culture != scope:recipient.culture
				OR = {
					AND = {
						scope:target.tier = tier_county
						scope:target.culture = scope:recipient.culture
					}		
					scope:target = {
						any_de_jure_county = {
							ep3_purchase_land_valid_county_trigger = yes
							culture = scope:recipient.culture
						}
					}
				}
			}
			scope:actor.primary_spouse ?= {
				culture = scope:recipient.culture
				dynasty ?= {
					dynasty_prestige_level >= 2
				}
			}
			desc = PURCHASE_LAND_MARRIED_TO_CULTURE_NOBLE_REASON
		}
		modifier = {
			add = 40
			trigger = {
				scope:actor.faith != scope:recipient.faith
			}
			scope:actor.primary_spouse ?= {
				faith = scope:recipient.faith
				dynasty ?= {
					dynasty_prestige_level >= 2
				}
			}
			desc = PURCHASE_LAND_MARRIED_TO_FAITH_NOBLE_REASON
		}
		modifier = {
			add = 80
			scope:actor.primary_spouse ?= {
				house ?= scope:recipient.house
			}
			desc = PURCHASE_LAND_MARRIED_TO_HOUSE_REASON
		}
		modifier = {
			add = 20
			trigger = {
				OR = {
					AND = {
						scope:target.tier = tier_county
						scope:target.culture = scope:recipient.culture
					}		
					scope:target = {
						any_de_jure_county = {
							ep3_purchase_land_valid_county_trigger = yes
							culture = scope:recipient.culture
						}
					}
				}
			}
			scope:actor = {
				knows_language_of_culture = scope:recipient.culture
				culture != scope:recipient.culture
			}
			desc = PURCHASE_LAND_SPEAKS_LANGUAGE_REASON
		}
		modifier = {
			add = -40
			scope:actor = {
				NOT = {
					knows_language_of_culture = scope:recipient.culture
					culture = scope:recipient.culture
				}
			}
			desc = PURCHASE_LAND_DOES_NOT_SPEAK_LANGUAGE_REASON
		}
		modifier = {
			add = {
				value = 40
				multiply = scope:recipient.debt_level
			}
			trigger = {
				scope:recipient = {
					OR = {
						debt_level > 0
						treasury_debt_level > 0
					}
				}
			}
			desc = PURCHASE_LAND_IS_IN_DEBT_REASON
		}
		modifier = {
			add = 60
			scope:actor = {
				has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:recipient }
			}
			desc = PURCHASE_LAND_RELATIONSHIP_REASON
		}
		modifier = {
			add = 80
			scope:offer_gold = yes
			desc = PURCHASE_LAND_DOUBLE_GOLD_REASON
		}
		modifier = {
			add = 160
			scope:hook = yes
			desc = YOU_HAVE_HOOK_REASON
		}
		modifier = {
			add = 60
			scope:offer_hook = yes
			desc = PURCHASE_LAND_OFFER_HOOK_REASON
		}
		modifier = {
			add = 80
			scope:vassal_obligation = yes
			desc = PURCHASE_LAND_OBLIGATIONS_REASON
		}	
	}

	ai_will_do = {
		base = -25
		ai_value_modifier = {
			ai_boldness = -0.5
			ai_energy = -0.5
			ai_greed = 0.5
		}
		modifier = {
			gold < 1000
			factor = 0
		}
		modifier = {
			gold > 2000
			add = 50
		}
	}
}

##################
# Request Invasion Sponsorship
# by Joe Parkin
##################

request_laamp_invasion_sponsorship_interaction = {
	common_interaction = no
	category = interaction_category_laamp
	interface_priority = 20
	cooldown_against_recipient = { years = 5 }

	greeting = positive
	notification_text = REQUEST_LAAMP_INVASION_SPONSORSHIP_PROPOSAL

	answer_accept_key = REQUEST_LAAMP_INVASION_SPONSORSHIP_ACCEPT
	answer_reject_key = REQUEST_LAAMP_INVASION_SPONSORSHIP_REJECT

	icon = request_invasion_sponsorship_interaction
	desc = request_laamp_invasion_sponsorship_interaction_desc

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = { OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority } }
		scope:recipient = { is_landed = yes }
		scope:recipient != scope:actor
		NOT = { scope:actor = { has_realm_law = camp_purpose_legitimists } }
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			top_liege = this
			custom_tooltip = {
				text = request_laamp_invasion_sponsorship_existing_supporter_tt
				NOT = { exists = var:adventurer_invasion_supporter }
			}
		}
		scope:recipient = {
			top_liege = this
			highest_held_title_tier >= tier_kingdom
			custom_tooltip = {
				text = request_laamp_invasion_sponsorship_existing_supportee_tt
				NOT = { exists = var:adventurer_invasion_supportee }
			}
		}
		#Has to be used instead of diplo range checks in laamp to landed interactions
		ep3_laamp_diplo_range_trigger = {
			TARGET = scope:recipient
			LAAMP = scope:actor
		}
		#You need to be at least Illustrious
		scope:actor = {
			prestige_level >= 3
		}
	}

	populate_recipient_list = {
		every_independent_ruler = {
			limit = {
				highest_held_title_tier >= tier_kingdom
				is_landed = yes
				in_diplomatic_range = scope:actor
				in_diplomatic_range = scope:recipient
				NOR = {
					is_at_war_with = scope:actor
					is_at_war_with = scope:recipient
					is_allied_to = scope:actor
					is_allied_to = scope:recipient
					has_truce = scope:actor
					has_truce = scope:recipient
					this = scope:actor
					this = scope:recipient
				}
			}
			add_to_list = characters
		}
	}
	
	send_options_exclusive = no

	send_option = {
		flag = request_soldiers
		localization = request_laamp_invasion_sponsorship_option_request_soldiers
		current_description = request_laamp_invasion_sponsorship_option_request_soldiers_desc
		starts_enabled = { always = yes }
	}

	send_option = {
		flag = join_war
		localization = request_laamp_invasion_sponsorship_option_join_war
		current_description = request_laamp_invasion_sponsorship_option_join_war_desc
		is_valid = {
			scope:secondary_recipient ?= {
				NOR = {
					is_at_war_with = scope:recipient
					is_allied_to = scope:recipient
					has_truce = scope:recipient
				}
			}
		}
	}

	send_option = {
		flag = offer_gold
		localization = request_laamp_invasion_sponsorship_option_offer_gold
		current_description = request_laamp_invasion_sponsorship_option_offer_gold_desc
		is_shown = { exists = scope:recipient }
		is_valid = { scope:actor.gold >= scope:recipient.request_laamp_invasion_sponsorship_gold_cost }
	}

	send_option = {
		flag = hook
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		localization = GENERIC_SPEND_A_HOOK
	}

	cost = {
		prestige = {
			value = massive_prestige_value
			if = {
				limit = { scope:join_war = yes }
				add = major_prestige_value
			}
		}
		gold = {
			if = {
				limit = { scope:offer_gold = yes }
				value = scope:recipient.request_laamp_invasion_sponsorship_gold_cost
			}
		}
	}

	can_send = {
		custom_tooltip = {
			text = request_laamp_invasion_sponsorship_minimum_request_tt
			OR = {
				scope:join_war = yes
				scope:request_soldiers = yes
			}
		}	
	}

	on_accept = {
		scope:actor = { save_scope_as = adventurer }
		scope:recipient ?= {
			if = {
				limit = {
					scope:request_soldiers = yes
					scope:join_war = yes
				}
				custom_tooltip = adventurer_invasion_supporter_tt
			}
			else_if = {
				limit = { scope:request_soldiers = yes }
				custom_tooltip = adventurer_invasion_supporter_soldiers_tt
			}
			else_if = {
				limit = { scope:join_war = yes }
				custom_tooltip = adventurer_invasion_supporter_join_tt
			}
			else = { custom_tooltip = request_laamp_invasion_sponsorship_minimum_request_tt }
			hidden_effect = {
				set_variable = {
					name = adventurer_invasion_supportee
					value = scope:actor
					years = 5
				}
				set_variable = {
					name = adventurer_invasion_target
					value = scope:secondary_recipient
					years = 5
				}
				if = {
					limit = { scope:join_war = yes }
					set_variable = {
						name = adventurer_invasion_join_war
						value = scope:actor
						years = 5
					}
				}
				if = {
					limit = { scope:request_soldiers = yes }
					set_variable = {
						name = adventurer_invasion_request_soldiers
						value = scope:actor
						years = 5
					}
				}
			}
		}
		scope:actor = {
			if = {
				limit = { scope:offer_gold = yes }
				show_as_tooltip = {
					pay_short_term_gold = {
						gold = scope:recipient.request_laamp_invasion_sponsorship_gold_cost
						target = scope:recipient
					}
				}
			}
			if = {
				limit = { scope:hook = yes }
				use_hook = scope:recipient
			}

			custom_tooltip = {
				text = adventurer_invasion_supporter_victory_tt
				set_variable = {
					name = adventurer_invasion_supporter
					value = scope:recipient
					years = 5
				}
				set_variable = {
					name = adventurer_invasion_target
					value = scope:secondary_recipient
					years = 5
				}
			}
			trigger_event = ep3_laamps.0200
		}
	}

	on_decline = {
		scope:actor = { trigger_event = ep3_laamps.0201 }
	}

	use_diplomatic_range = yes

	ai_accept = {
		base = -25

		# Relationships: actor -> recipient / We avoid more relationship checks for balance reasons (and because important decisions tend to care little for relationships)
		modifier = { # Rivalry modifier.
			desc = offer_vassalization_interaction_aibehavior_rival_tt
			scope:recipient = {
				has_relation_rival = scope:actor
				NOT = { has_relation_nemesis = scope:actor }
			}
			add = -10
		}
		modifier = { # Nemesis modifier.
			desc = offer_vassalization_interaction_aibehavior_nemesis_tt
			scope:recipient = { has_relation_nemesis = scope:actor }
			add = -100
		}
		modifier = { # Same Dynasty modifier.
			desc = offer_vassalization_interaction_aibehavior_dynasty_tt
			scope:recipient.dynasty = scope:actor.dynasty
			add = 10
		}

		# Compare Opinion modifier
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.5
		}
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:secondary_recipient
			multiplier = -0.5
		}

		# Relative power (taken from PT interaction)
		modifier = { # A lot less levies than scope:secondary_recipient
			add = -50
			scope:recipient.max_military_strength <= scope:secondary_recipient.purchase_truce_interaction_recipient_far_weaker_value
			scope:join_war = yes
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_MUCH_WEAKER_REASON
		}
		modifier = { # Few less levies than scope:secondary_recipient
			add = -25
			scope:recipient.max_military_strength > scope:secondary_recipient.purchase_truce_interaction_recipient_far_weaker_value
			scope:recipient.max_military_strength <= scope:secondary_recipient.purchase_truce_interaction_recipient_somewhat_weaker_value
			scope:join_war = yes
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_SOMEWHAT_WEAKER_REASON
		}
		modifier = { # Roughly equal levies with scope:secondary_recipient
			add = -10
			scope:recipient.max_military_strength > scope:secondary_recipient.purchase_truce_interaction_recipient_somewhat_weaker_value
			scope:recipient.max_military_strength <= scope:secondary_recipient.purchase_truce_interaction_recipient_somewhat_stronger_value
			scope:join_war = yes
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_ROUGHLY_EQUAL_REASON
		}

		# AI who are in debt will take whatever windfalls they can.
		modifier = {
			add = {
				value = scope:recipient.debt_level
				add = 1 # Our first level is 0, so we add one to always get a positive number.
				multiply = 10
			}
			scope:offer_gold = yes
			scope:recipient.debt_level > 0
			desc = PT_AI_IS_IN_DEBT
		}

		# Pacifist AI don't like raiding
		modifier = {
			add = -25
			scope:recipient.faith = { has_doctrine_parameter = pacifist_opinion_active }
			desc = PT_AI_IS_PACIFIST_FAITH
		}
		modifier = {
			add = -25
			scope:recipient.culture = { has_cultural_parameter = reduced_cost_for_conversion_to_pacifist_faith }
			desc = PT_AI_IS_PACIFIST_CULTURE
		}

		# Small realm
		modifier = {
			add = -50
			scope:recipient.realm_size < minor_realm_size
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_SMALL_REALM_REASON
		}
		# Own war
		modifier = {
			add = -50
			scope:recipient = { is_at_war = yes }
			scope:join_war = yes
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_OWN_WAR_REASON
		}

		modifier = { # Distance
			add = {
				value = 0
				add = scope:distance
				if = {
					limit = { scope:distance >= squared_distance_medium }
					add = scope:distance
					multiply = -0.00001
				}
				min = -100
			}
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_DISTANCE_REASON
			scope:recipient = {
				NOT = {
					any_neighboring_top_liege_realm_owner = { this = scope:secondary_recipient }
				}
			}
			scope:recipient.capital_county = {
				save_temporary_scope_value_as = {
					name = distance
					value = "squared_distance(scope:secondary_recipient.capital_county)"
				}
			}
		}

		modifier = {
			add = -30
			scope:recipient.faith = scope:secondary_recipient.faith
			desc = I_AM_ATTACKING_A_CORELIGIONIST
		}
		modifier = {
			add = -10
			scope:actor.faith != scope:recipient.faith
			desc = DIFFERENT_FAITH
		}
		modifier = {
			add = -200
			scope:recipient.primary_heir ?= scope:secondary_recipient
			desc = WONT_FIGHT_HEIR_REASON
		}
		modifier = {
			add = -200
			scope:recipient = { is_consort_of = scope:secondary_recipient }
			desc = WONT_FIGHT_SPOUSE_REASON
		}
		modifier = {
			add = -100
			scope:recipient = {
				has_relation_friend = scope:secondary_recipient
				NOT = { has_relation_best_friend = scope:secondary_recipient }
			}
			desc = WONT_FIGHT_FRIEND_REASON
		}
		modifier = {
			add = -200
			scope:recipient = { has_relation_best_friend = scope:secondary_recipient }
			desc = WONT_FIGHT_BEST_FRIEND_REASON
		}
		modifier = {
			add = -100
			scope:recipient = {
				has_relation_lover = scope:secondary_recipient
				NOT = { has_relation_soulmate = scope:secondary_recipient }
			}
			desc = WONT_FIGHT_LOVER_REASON
		}
		modifier = {
			add = -200
			scope:recipient = { has_relation_soulmate = scope:secondary_recipient }
			desc = WONT_FIGHT_SOULMATE_REASON
		}
		modifier = {
			add = -200
			scope:recipient = { is_allied_to = scope:secondary_recipient }
			desc = WAR_WITH_ALLY_REASON
		}

		modifier = {
			add = 50
			scope:hook = yes
			desc = YOU_HAVE_HOOK_REASON
		}

		# Offered proportional gold
		modifier = {
			add = -25
			scope:request_soldiers = yes
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_REQUEST_SOLDIERS_REASON
		}
		modifier = {
			add = -50
			scope:join_war = yes
			desc = REQUEST_LAAMP_INVASION_SPONSORSHIP_JOIN_WAR_REASON
		}
		modifier = {
			add = 35
			scope:offer_gold = yes
			desc = GOLD_REASON
		}

		# AI value mod
		ai_value_modifier = {
			ai_greed = 0.5
			ai_boldness = 0.25
			min = 0
			max = 25
		}
	}
	can_send_despite_rejection = no
}

##################
# Negotiate Settlement
# by Joe Parkin
##################

negotiate_settlement_interaction = {
	common_interaction = yes
	category = interaction_category_diplomacy
	icon = negotiate_settlement_interaction

	desc = negotiate_settlement_interaction_desc
	target_type = title
	target_filter = actor_domain_titles

	cooldown_against_recipient = { years = 5 }

	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:actor = { top_liege != this }
					scope:target = {
						trigger_if = {
							limit = { tier = tier_duchy }
							scope:actor.highest_held_title_tier = tier_duchy
						}
						trigger_else = { scope:actor.highest_held_title_tier = tier_county }
					}
				}
				desc = NEGOTIATE_SETTLEMENT_PROPOSAL_FELLOW_VASSAL
			}
			triggered_desc = {
				trigger = {
					scope:target = {
						trigger_if = {
							limit = { tier = tier_duchy }
							scope:actor.highest_held_title_tier >= tier_kingdom
						}
						trigger_else = { scope:actor.highest_held_title_tier >= tier_duchy }
					}
				}
				desc = NEGOTIATE_SETTLEMENT_PROPOSAL_LIEGE
			}
			desc = NEGOTIATE_SETTLEMENT_PROPOSAL
		}
	}

	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 60
	ai_min_reply_days = 4
	ai_max_reply_days = 9

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = { is_landed = yes }
		scope:recipient = {
			OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority }
		}
		scope:recipient != scope:actor
	}

	can_be_picked_title = {
		custom_tooltip = {
			text = purchase_land_tier_tt
			scope:target = {
				tier >= tier_county
				tier <= tier_duchy
			}
		}
		custom_tooltip = {
			text = purchase_land_capital_tt
			NOR = {
				scope:target = scope:actor.capital_county
				scope:target = scope:actor.capital_county.duchy
			}
		}
		custom_tooltip = {
			text = purchase_land_primary_tt
			scope:target != scope:actor.primary_title
		}
		custom_tooltip = {
			text = negotiate_settlement_title_cant_receive_reason_higher_tier_without_county
			scope:target = {
				tier = tier_duchy
				any_de_jure_county = {
					ep3_negotiate_settlement_valid_county_trigger = yes
				}	
			}
		}
		trigger_if = {
			limit = {
				scope:actor = { is_ai = yes }
				scope:recipient = { is_ai = yes }
				scope:target.tier < tier_duchy
			}
			scope:recipient = {
				OR = {
					current_military_strength < 1000 # You can do better
					global_variable_list_size = {
						name = laamps_tally
						value >= too_many_laamps_value
					}
				}
				OR = {
					AND = {
						has_character_modifier = ep3_lust_for_land_modifier
						ai_greed >= 25
						ai_boldness >= 25
						ai_rationality <= -25
					}
					AND = {
						has_trait = greedy
						ai_rationality <= -50
					}
					global_variable_list_size = {
						name = laamps_tally
						value >= too_many_laamps_value
					}
				}
			}
		}
	}

	has_valid_target_showing_failures_only = {
		trigger_if = { # Can't purchase HoF titles
			limit = {
				scope:target ?= { is_head_of_faith = yes }
			}
			custom_description = {
				text = negotiate_settlement_religious_tt
				always = no
			}
		}
		trigger_if = { # Can't purchase nf titles
			limit = {
				scope:target ?= { is_noble_family_title = yes }
			}
			custom_description = {
				text = negotiate_settlement_noble_family_tt
				always = no
			}
		}
		trigger_if = { # Can't purchase laamp titles
			limit = {
				scope:target ?= { has_variable = adventurer_creation_reason }
			}
			custom_description = {
				text = negotiate_settlement_landless_adventurer_tt
				always = no
			}
		}
		trigger_if = {
			limit = {
				scope:actor = { government_has_flag = government_is_administrative }
			}
			scope:target.tier = tier_duchy
		}
		trigger_else = {
			scope:target = {
				tier >= tier_county
				tier <= tier_duchy
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			custom_tooltip = {
				text = negotiate_settlement_target_tt
				OR = {
					any_character_war = {
						is_attacker = scope:recipient
						primary_defender = scope:actor
						save_temporary_scope_as = war_temp
					}
					AND = {
						exists = scope:recipient.capital_province
						scope:recipient.capital_province.county.holder.top_liege = scope:actor.top_liege
					}
				}
			}
		}
		# Can only settle if not at risk of losing land, outside of war of adventurer
		custom_tooltip = {
			text = negotiate_settlement_defensive_wars_tt
			scope:actor = {
				NOT = {
					any_character_war = {
						any_war_defender = { this = scope:actor }
						NOT = {
							any_war_attacker = { this = scope:recipient }
						}
					}
				}
			}
		}
		NOT = {
			scope:actor = { has_relation_nemesis = scope:recipient }
		}
		trigger_if = { # Can't purchase Baronies
			limit = {
				scope:target ?= { tier = tier_barony }
			}
			custom_description = {
				text = negotiate_settlement_barony_tt
				always = no
			}
		}
		trigger_if = { # Can't purchase Kingdoms or Empires
			limit = {
				scope:target ?= { tier >= tier_kingdom }
			}
			custom_description = {
				text = negotiate_settlement_kingdom_tt
				always = no
			}
		}
		trigger_if = {
			limit = {
				scope:actor = {
					any_held_county = { count < 2 }
				}
			}
			custom_description = {
				text = negotiate_settlement_minimum_county_tt
				always = no
			}
		}
		scope:recipient = {
			NOT = { has_strong_hook = scope:actor }
		}
		# Adventurers can't be negotiated back into places they're exiled from.
		trigger_if = {
			limit = {
				scope:actor = { top_liege != this }
			}
			custom_tooltip = {
				text = the_wake_1
				NOT = {
					is_laamp_exiled_from_province_trigger = {
						PROVINCE = scope:actor.capital_province
						LAAMP = scope:recipient
					}
				}
			}
		}
		# Story blockers.
		## Hereward cannot be exiled by active participants in the Harrying of the North.
		disable_interaction_for_hereward_trigger = yes
		## Hasan cannot be exiled by anyone during his story.
		disable_interaction_for_hasan_trigger = yes
	}

	on_send = {
		debug_log = "NEGOTIATE_SETTLEMENT"
		debug_log_scopes = yes
	}

	on_accept = {
		scope:actor = { save_scope_as = adventurer }
		scope:target = {
			add_to_list = offered_titles
			if = {
				limit = { tier = tier_duchy }
				every_de_jure_county = {
					limit = { ep3_negotiate_settlement_valid_county_trigger = yes }
					add_to_list = offered_titles
				}
			}
		}
		ordered_in_list = {
			list = offered_titles
			save_scope_as = new_primary_title
		}
		save_scope_value_as = {
			name = negotiate_settlement_legitimacy_cost
			value = {
				value = 0
				every_in_list = {
					list = offered_titles
					if = {
						limit = { tier = tier_county }
						add = -25
					}
					else = { add = -50 }
				}
				multiply = scope:actor.highest_held_title_tier
			}
		}
		show_as_tooltip = { negotiate_settlement_interaction_effect = yes }
		scope:actor = { trigger_event = ep3_laamps.0300 }
	}

	on_decline = {
		scope:actor = {
			trigger_event = ep3_laamps.0302
		}
	}

	ai_targets = {
		ai_recipients = scripted_relations
		ai_recipients = spouses
		max = 5
	}
	ai_targets = {
		ai_recipients = top_realm_domicile_owners
		ai_recipients = nearby_domicile_owners
		max = 5
	}
	ai_targets = {
		ai_recipients = war_enemies
		max = 10
	}

	# Must be fairly common as it can happen during war
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 3
		kingdom = 3
		empire = 3
		hegemony = 3
	}

	ai_potential = {
		OR = {
			top_liege = this
			highest_held_title_tier >= tier_kingdom
		}
		OR = {
			is_at_war = yes
			has_trait = craven
			has_trait = generous
		}
		NOT = {
			government_has_flag = government_is_nomadic
		}
	}

	ai_accept = {
		base = 0 # Try to make it 0 for most interactions

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.5
			desc = AI_OPINION_REASON
		}
		ai_value_modifier = {
			ai_boldness = -0.25
			ai_energy = -0.25
			ai_greed = 0.25
		}
		modifier = { # Claims
			add = {
				value = 0
				if = {
					limit = {
						scope:recipient = { has_claim_on = scope:target }
					}
					add = 25
				}
				if = {
					limit = { scope:target.tier = tier_duchy }
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_negotiate_settlement_valid_county_trigger = yes
								save_temporary_scope_as = claim_temp
								scope:recipient = { has_claim_on = scope:claim_temp }
							}
							add = 25
						}
					}
				}
			}
			desc = NEGOTIATE_SETTLEMENT_CLAIM_REASON
			OR = {
				scope:recipient = { has_claim_on = scope:target }
				trigger_if = {
					limit = { scope:target.tier = tier_duchy }
					scope:target = {
						any_de_jure_county = {
							ep3_negotiate_settlement_valid_county_trigger = yes
							save_temporary_scope_as = county_temp
							scope:recipient = { has_claim_on = scope:county_temp }
						}
					}
				}
				trigger_else = { always = no }
			}
		}
		modifier = {
			add = -300
			scope:recipient = { has_realm_law = camp_purpose_legitimists }
			desc = NEGOTIATE_SETTLEMENT_LEGITIMIST_REASON
		}
		modifier = { # Tier lower than war claim
			add = {
				value = 0
				scope:recipient = {
					every_character_war = {
						limit = {
							primary_attacker = scope:recipient
							primary_defender = scope:actor
							casus_belli = {
								any_target_title = { tier > scope:target.tier }
							}
						}
						casus_belli = {
							every_target_title = {
								limit = { tier > scope:target.tier }
								add = -25
							}
						}
					}
				}
			}
			desc = NEGOTIATE_SETTLEMENT_LOWER_THAN_WAR_TARGET_REASON
			scope:recipient = {
				any_character_war = {
					primary_attacker = scope:recipient
					primary_defender = scope:actor
					casus_belli = {
						any_target_title = { tier > scope:target.tier }
					}
				}
			}
		}
		modifier = { # War Targets
			add = {
				value = 0
				scope:recipient = {
					every_character_war = {
						limit = {
							primary_attacker = scope:recipient
							casus_belli = {
								any_target_title = {
									OR = {
										this = scope:target
										de_jure_liege = scope:target
									}
								}
							}
						}
						casus_belli = {
							every_target_title = {
								limit = {
									OR = {
										this = scope:target
										de_jure_liege = scope:target
									}
								}
								add = 25
							}
						}
					}
				}
			}
			desc = NEGOTIATE_SETTLEMENT_WAR_TARGET_REASON
			scope:recipient = {
				any_character_war = {
					primary_attacker = scope:recipient
					casus_belli = {
						any_target_title = {
							OR = {
								this = scope:target
								de_jure_liege = scope:target
							}
						}
					}
				}
			}
		}
		modifier = { # Num of counties/duchy
			add = {
				value = 0
				scope:target = {
					if = {
						limit = { tier = tier_county }
						add = 25
					}
					else = {
						add = 50
						every_de_jure_county = {
							limit = { ep3_negotiate_settlement_valid_county_trigger = yes }
							add = 25
						}
					}
				}
			}
			desc = NEGOTIATE_SETTLEMENT_NUM_TITLES_REASON
		}
		modifier = { # Province value
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = scope:target.development_level
					scope:target = {
						every_county_province = { add = num_buildings }
					}
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = { ep3_negotiate_settlement_valid_county_trigger = yes }
							add = development_level
							every_county_province = { add = num_buildings }
						}
					}
				}
			}
			#add = 1
			desc = NEGOTIATE_SETTLEMENT_VALUE_REASON
		}
		modifier = { # Land is foreign
			add = -10
			desc = NEGOTIATE_SETTLEMENT_FOREIGN_COUNTY_CULTURE_REASON
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = -10
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_negotiate_settlement_valid_county_trigger = yes
								culture != scope:recipient.culture
							}
							add = -10
						}
					}
				}
				min = -30
			}
			desc = NEGOTIATE_SETTLEMENT_FOREIGN_CULTURE_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.culture != scope:recipient.culture
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_negotiate_settlement_valid_county_trigger = yes
						culture != scope:recipient.culture
					}
				}
			}
		}
		modifier = { # Different faith
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = -10
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_negotiate_settlement_valid_county_trigger = yes
								faith != scope:recipient.faith
							}
							add = -10
						}
					}
				}
				min = -30
			}
			desc = NEGOTIATE_SETTLEMENT_DIFFERENT_FAITH_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.faith != scope:recipient.faith
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_negotiate_settlement_valid_county_trigger = yes
						faith != scope:recipient.faith
					}
				}
			}
		}
		modifier = { # Losing/winning as attacker
			add = {
				value = 0
				scope:recipient = {
					ordered_character_war = {
						limit = {
							OR = {
								primary_attacker = scope:actor
								primary_defender = scope:actor
							}
							OR = {
								primary_attacker = scope:recipient
								primary_defender = scope:recipient
							}
						}
						order_by = war_days
						if = {
							limit = {
								primary_attacker = scope:actor
								primary_defender = scope:recipient
							}
							add = defender_war_score
						}
						else = { add = attacker_war_score }
					}
				}
				divide = 4
			}
			desc = NEGOTIATE_SETTLEMENT_LOSING_WAR_REASON
			scope:recipient = {
				any_character_war = {
					OR = {
						primary_attacker = scope:actor
						primary_defender = scope:actor
					}
					OR = {
						primary_attacker = scope:recipient
						primary_defender = scope:recipient
					}
					trigger_if = {
						limit = {
							primary_attacker = scope:actor
							primary_defender = scope:recipient
						}
						defender_war_score >= 25
					}
					trigger_else = { attacker_war_score >= 25 }
				}
			}
		}
		modifier = { # Losing/winning as defender
			add = {
				value = 0
				scope:recipient = {
					ordered_character_war = {
						limit = {
							OR = {
								primary_attacker = scope:actor
								primary_defender = scope:actor
							}
							OR = {
								primary_attacker = scope:recipient
								primary_defender = scope:recipient
							}
						}
						order_by = war_days
						if = {
							limit = {
								primary_attacker = scope:actor
								primary_defender = scope:recipient
							}
							add = attacker_war_score
						}
						else = { add = defender_war_score }
					}
				}
				divide = 4
			}
			desc = NEGOTIATE_SETTLEMENT_LOSING_WAR_REASON
			scope:recipient = {
				any_character_war = {
					OR = {
						primary_attacker = scope:actor
						primary_defender = scope:actor
					}
					OR = {
						primary_attacker = scope:recipient
						primary_defender = scope:recipient
					}
					trigger_if = {
						limit = {
							primary_attacker = scope:actor
							primary_defender = scope:recipient
						}
						attacker_war_score >= 25
					}
					trigger_else = { defender_war_score >= 25 }
				}
			}
		}
		modifier = { # Number of troops at peace,
			add = {
				add = current_military_strength
				multiply = -0.02
			}
			desc = laamp_has_soldiers_negotiate_settlement_tt
		}
		modifier = {
			add = -50000
			scope:actor = { is_ai = yes }
			NOT = {
				scope:target.tier >= tier_duchy
			}
			scope:recipient = {
				NOR = {
					current_military_strength < 1000 # You can do better
					global_variable_list_size = {
						name = laamps_tally
						value >= too_many_laamps_value
					}
				}
				NOR = {
					AND = {
						has_character_modifier = ep3_lust_for_land_modifier
						ai_greed >= 25
						ai_boldness >= 25
						ai_rationality <= -25
					}
					AND = {
						has_trait = greedy
						ai_rationality <= -50
					}
					global_variable_list_size = {
						name = laamps_tally
						value >= too_many_laamps_value
					}
				}
			}
		}
	}

	ai_will_do = {
		base = -25
		modifier = {
			add = 25
			scope:actor.domain_size > scope:actor.domain_limit
		}
		opinion_modifier = {
			who = scope:actor
			opinion_target = scope:recipient
			multiplier = 0.25
		}
		ai_value_modifier = {
			ai_boldness = -0.25
			ai_energy = -0.25
			ai_greed = -0.25
		}
		modifier = { # Tier lower than war claim
			add = {
				value = 0
				scope:recipient = {
					every_character_war = {
						limit = {
							primary_attacker = scope:recipient
							primary_defender = scope:actor
							casus_belli = {
								any_target_title = { tier > scope:target.tier }
							}
							attacker_war_score >= 50
						}
						add = 150
					}
				}
			}
			desc = NEGOTIATE_SETTLEMENT_SAVE_TITLE_REASON
			scope:recipient = {
				any_character_war = {
					primary_attacker = scope:recipient
					primary_defender = scope:actor
					casus_belli = {
						any_target_title = { tier > scope:target.tier }
					}
					attacker_war_score >= 50
				}
			}
		}
		modifier = {
			scope:recipient = {
				has_government = landless_adventurer_government
				has_realm_law = camp_purpose_legitimists
				any_claim = {
					OR = {
						holder ?= {
							OR = {
								this = scope:actor
								target_is_liege_or_above = scope:actor
							}
						}
						target_is_de_jure_liege_or_above = scope:actor.primary_title
					}
				}
			}
			desc = NEGOTIATE_SETTLEMENT_DE_JURE_LEGITIMIST_REASON
			add = -150
		}
		modifier = { # Defending against a LAAMP
			any_character_war = {
				primary_defender = scope:actor
				any_war_attacker = {
					scope:recipient ?= this
					has_government = landless_adventurer_government
					NOT = { has_realm_law = camp_purpose_legitimists }
					# Attacking laamp's troops at least 10% of defender's total troops
					current_military_strength > war_defender_total_strength_tenth_value
				}
				OR = {
					attacker_war_score >= 25
					# Attackers outnumber defenders at least 2:1
					war_attacker_total_current_strength_halved_value >= war_defender_total_current_strength_value
				}
			}
			desc = NEGOTIATE_SETTLEMENT_DEFENDING_REASON
			add = {
				value = 0
				every_character_war = {
					limit = {
						primary_defender = scope:actor
						any_war_attacker = {
							scope:recipient ?= this
							has_government = landless_adventurer_government
							NOT = { has_realm_law = camp_purpose_legitimists }
							# Attacking laamp's troops at least 10% of defender's total troops
							current_military_strength > war_defender_total_strength_tenth_value
						}
					}
					add = {
						value = 25
						add = {
							value = attacker_war_score
							subtract = 25
							min = 0
						}
						if = {
							limit = {
								exists = primary_attacker
								scope:recipient ?= primary_attacker
							}
							multiply = 2
						}
						# Difference in strength of attackers and defenders
						multiply = war_attacker_total_current_strength_divided_by_defender_value
					}
				}
			}
		}
		modifier = { # Attacking a LAAMP
			any_character_war = {
				primary_attacker = scope:actor
				any_war_defender = {
					scope:recipient ?= this
					has_government = landless_adventurer_government
					# Defending laamp's troops at least 10% of attacker's total troops
					current_military_strength > war_attacker_total_strength_tenth_value
				}
				OR = {
					defender_war_score >= 25
					# Defenders outnumber attackers at least 2:1
					war_defender_total_current_strength_halved_value >= war_attacker_total_current_strength_value
				}
			}
			desc = NEGOTIATE_SETTLEMENT_ATTACKING_REASON
			add = {
				value = 0
				every_character_war = {
					limit = {
						primary_attacker = scope:actor
						any_war_defender = {
							scope:recipient ?= this
							has_government = landless_adventurer_government
							# Defending laamp's troops at least 10% of attacker's total troops
							current_military_strength > war_attacker_total_strength_tenth_value
						}
					}
					add = {
						value = 25
						add = {
							value = defender_war_score
							subtract = 25
							min = 0
						}
						# Difference in strength of defenders and attackers
						multiply = war_defender_total_current_strength_divided_by_attacker_value
					}
				}
			}
		}
		modifier = { # Potential war target of a LAAMP
			scope:recipient ?= {
				ep3_adventurer_might_attack_target_trigger = { TARGET = scope:actor }
				trigger_if = {
					limit = {
						NOT = { is_at_war_with = scope:actor }
					}
					ep3_laamp_diplo_range_trigger = {
						TARGET = scope:actor
						LAAMP = scope:recipient
					}
				}
			}
			desc = NEGOTIATE_SETTLEMENT_POTENTIAL_WAR_TARGET_REASON
			add = {
				value = 25
				multiply = recipient_strength_divided_by_actors_value
				# Scale of threat
				scope:recipient = {
					if = {
						limit = {
							can_declare_war = {
								casus_belli = ep3_laamp_adventurer_duchy_invasion
								defender = scope:actor
							}
						}
						multiply = 1.25
					}
					if = {
						limit = {
							can_declare_war = {
								casus_belli = ep3_laamp_adventurer_kingdom_invasion
								defender = scope:actor
							}
						}
						multiply = 1.5
					}
					if = {
						limit = {
							can_declare_war = {
								casus_belli = ep3_laamp_adventurer_empire_invasion
								defender = scope:actor
							}
						}
						multiply = 2
					}
				}
				scope:actor = {
					# Worried about ongoing wars
					if = {
						limit = {
							any_character_war = {
								primary_defender = scope:actor
								# Only care if attackers are really a threat
								war_attacker_total_current_strength_value > war_defender_total_current_strength_halved_value
							}
						}
						multiply = 2
					}
					# Already at war
					if = {
						limit = { is_at_war_with = scope:recipient }
						multiply = 0.5
					}
				}
			}
		}
		modifier = { # Potential faction claimant
			exists = scope:recipient
			scope:actor = {
				any_targeting_faction = {
					special_character ?= scope:recipient
					faction_power >= faction_power_threshold
				}
			}
			desc = NEGOTIATE_SETTLEMENT_CLAIMANT_REASON
			add = {
				value = 0
				every_targeting_faction = {
					limit = {
						special_character ?= scope:recipient
						faction_power >= faction_power_threshold
					}
					add = 25
				}
			}
		}
		modifier = { # Give claims to laamps
			scope:target ?= {
				any_claimant = { scope:recipient ?= this }
			}
			desc = NEGOTIATE_SETTLEMENT_CLAIM_REASON
			add = {
				value = 25
				if = {
					limit = {
						scope:target.tier = tier_county
					}
					subtract = scope:target.development_level
				}
				if = {
					limit = {
						scope:target.tier > tier_county
					}
					subtract = scope:target.title_capital_county.development_level
				}
			}
		}
		# Essentially the inverse of Purchase Land
		modifier = { # Land is foreign
			add = 10
			desc = NEGOTIATE_SETTLEMENT_FOREIGN_COUNTY_CULTURE_REASON
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = 10
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_negotiate_settlement_valid_county_trigger = yes
								culture != scope:recipient.culture
							}
							add = 10
						}
					}
				}
				max = 30
			}
			desc = NEGOTIATE_SETTLEMENT_FOREIGN_CULTURE_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.culture != scope:actor.culture
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_negotiate_settlement_valid_county_trigger = yes
						culture != scope:actor.culture
					}
				}
			}
		}
		modifier = { # Different faith
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = 10
				}
				else = {
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_negotiate_settlement_valid_county_trigger = yes
								faith != scope:actor.faith
							}
							add = 10
						}
					}
				}
				max = 30
			}
			desc = NEGOTIATE_SETTLEMENT_DIFFERENT_FAITH_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.faith != scope:actor.faith
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_negotiate_settlement_valid_county_trigger = yes
						faith != scope:actor.faith
					}
				}
			}
		}
		modifier = { # My culture's lands
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = -15
				}
				else = {
					add = -15
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_negotiate_settlement_valid_county_trigger = yes
								faith != scope:actor.faith
							}
							add = -15
						}
					}
				}
				if = {
					limit = { scope:acceptance != 0 }
					divide = scope:acceptance
				}
				min = -50
			}
			desc = NEGOTIATE_SETTLEMENT_MY_CULTURE_REASON
			scope:actor.culture != scope:recipient.culture
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.culture = scope:actor.culture
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_negotiate_settlement_valid_county_trigger = yes
						culture = scope:actor.culture
					}
				}
			}
			scope:actor.culture = {
				save_temporary_scope_value_as = {
					name = acceptance
					value = {
						value = "cultural_acceptance(scope:recipient.culture)"
						divide = 25
						ceiling = yes
					}
				}
			}
		}
		modifier = { # My faith's lands
			add = {
				value = 0
				if = {
					limit = { scope:target.tier = tier_county }
					add = -15
				}
				else = {
					add = -15
					scope:target = {
						every_de_jure_county = {
							limit = {
								ep3_negotiate_settlement_valid_county_trigger = yes
								faith != scope:actor.faith
							}
							add = -15
						}
					}
				}
				if = {
					limit = { scope:hostility != 0 }
					multiply = scope:hostility
				}
				min = -50
			}
			desc = NEGOTIATE_SETTLEMENT_MY_FAITH_REASON
			scope:actor.faith != scope:recipient.faith
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target.faith = scope:actor.faith
			}
			trigger_else = {
				scope:target = {
					any_de_jure_county = {
						ep3_negotiate_settlement_valid_county_trigger = yes
						faith = scope:actor.faith
					}
				}
			}
			scope:actor.faith = {
				save_temporary_scope_value_as = {
					name = hostility
					value = "faith_hostility_level(scope:recipient.faith)"
				}
			}
		}
		modifier = { # Too close
			add = {
				value = 0
				add = scope:distance
				divide = scope:actor.highest_held_title_tier
				if = {
					limit = { scope:distance <= squared_distance_medium }
					multiply = -0.01
				}
				else = { multiply = 0.0005 }
				min = -25
				max = 50
			}
			desc = NEGOTIATE_LAND_DISTANCE_REASON
			trigger_if = {
				limit = { scope:target.tier = tier_county }
				scope:target = { save_temporary_scope_as = distance_target }
			}
			trigger_else = {
				scope:target.title_capital_county = { save_temporary_scope_as = distance_target }
			}
			scope:actor.capital_county = {
				save_temporary_scope_value_as = {
					name = distance
					value = "squared_distance(scope:distance_target)"
				}
			}
		}
	}
}

##################
# Trade Oath
# by Joe Parkin
##################

trade_oath_interaction = {
	icon = hook_icon
	category = interaction_category_laamp
	interface_priority = 10

	desc = trade_oath_interaction_desc

	is_shown = {
		scope:actor = {
			domicile ?= { has_domicile_parameter = camp_unlock_trade_oath_interaction }
			NOT = { has_strong_hook = scope:recipient }
		}
		scope:recipient = {
			is_courtier_of = scope:actor
			NOR = {
				has_hook = scope:actor
				is_close_family_of = scope:actor
				is_spouse_of = scope:actor
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_at_war = no
			is_imprisoned = no
		}
		scope:recipient.location = scope:actor.location
	}

	cost = { prestige = 250 }

	on_accept = {
		show_as_tooltip = {
			scope:actor = {
				add_hook = {
					type = loyalty_hook
					target = scope:recipient
				}
			}		
			scope:recipient = {
				add_hook = {
					type = follower_oath_hook
					target = scope:actor
				}
			}
		}
		scope:actor = { trigger_event = ep3_laamps.1011 }
	}

	ai_accept = {
		base = -75

		opinion_modifier = { opinion_target = scope:actor }

		# Traits.
		## Loyal
		modifier = {
			add = 30
			has_trait = loyal
		}
		## Disloyal
		modifier = {
			add = -30
			has_trait = disloyal
		}
		## Arbitrary
		modifier = {
			add = -20
			has_trait = arbitrary
		}
		## Just
		modifier = {
			add = 20
			has_trait = just
		}
		## Fickle
		modifier = {
			add = -10
			has_trait = fickle
		}
		## Stubborn
		modifier = {
			add = 20
			has_trait = stubborn
		}
		## Deceitful
		modifier = {
			add = -5
			has_trait = deceitful
		}
		## Honest
		modifier = {
			add = 5
			has_trait = honest
		}
	}
	ai_potential = {
		has_realm_law = camp_purpose_legitimists
	}
	# Always do it if you get the chance.
	ai_will_do = { base = 1000 }

	ai_targets = { ai_recipients = courtiers }
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 12
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	auto_accept = yes
}

##################
# Pawn Gear
# by Joe Parkin
##################

pawn_gear_interaction = {
	icon = artisan_inspiration
	category = interaction_category_laamp
	interface_priority = 10
	common_interaction = yes
	desc = pawn_gear_interaction_desc

	target_type = artifact
	target_filter = actor_artifacts

	is_shown = {
		scope:actor = {
			domicile ?= { has_domicile_parameter = camp_unlocks_pawn_gear_interaction }
			this = scope:recipient
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_at_war = no
			is_imprisoned = no
			has_any_artifact = yes
			trigger_if = {
				limit = { is_ai = no }
				custom_description = {
					text = "any_unequipped_artifact_tt"
					any_character_artifact = { is_equipped = no }
				}
			}
		}
	}

	can_be_picked_artifact = {
		trigger_if = {
			limit = {
				scope:actor = { is_ai = no }
			}
			scope:target = { is_equipped = no }
		}
		scope:actor = {
			any_character_artifact = { this = scope:target }
		}
	}

	on_accept = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = pawn_gear_interaction_notification
				left_icon = scope:actor
				add_short_term_gold = artifact_wealth_quality_average_value
				destroy_artifact = scope:target
			}
		}
	}

	auto_accept = yes
}

##################
# Complete the Contract
# by Arkadiusz Majewski
# Needed for the action_can_complete_task_contract and to be able to end contracts if you cancelled the initial travel planner
##################

complete_task_contract_interaction = {
	category = interaction_category_laamp
	interface_priority = 120
	common_interaction = yes
	icon = icon_contract_escort
	use_diplomatic_range = no

	popup_on_receive = yes
	pause_on_receive = yes

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = {
			any_character_active_contract = {
				OR = {
					ep3_transport_contract_trigger = yes
					has_task_contract_type = laamp_help_increase_control_contract
				}
				OR = {
					task_contract_employer ?= scope:recipient
					task_contract_destination.province_owner ?= scope:recipient
					task_contract_target ?= scope:recipient
					var:task_contract_object ?= scope:recipient
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
	}

	is_highlighted = {
		always = yes
	}

	send_option = {
		flag = without_domicile
	}

	send_option = {
		flag = with_domicile
	}

	on_accept = {
		scope:actor = {
			random_character_active_contract = {
				limit = {
					OR = {
						ep3_transport_contract_trigger = yes
						has_task_contract_type = laamp_help_increase_control_contract
					}
					#check for any contract that has the recipient as any relevant character to it (employer, target, target destination owner, escortee)
					OR = {
						task_contract_employer ?= scope:recipient
						task_contract_destination.province_owner ?= scope:recipient
						task_contract_target ?= scope:recipient
						var:task_contract_object ?= scope:recipient
					}
				}
				save_scope_as = task_contract
			}
			#set all the scopes
			if = {
				limit = {
					exists = scope:task_contract.var:task_contract_employer
				}
				scope:task_contract.var:task_contract_employer ?= {
					save_scope_as = task_contract_employer
				}
			}
			if = {
				limit = {
					scope:task_contract = {
						has_task_contract_type = laamp_help_increase_control_contract
					}
				}
				scope:task_contract.var:task_contract_councillor_liege ?= {
					save_scope_as = task_contract_councillor_liege
				}
			}
			if = {
				limit = {
					exists = scope:task_contract.var:task_contract_destination
				}
				scope:task_contract.var:task_contract_destination = {
					save_scope_as = task_contract_destination
				}
			}
			if = {
				limit = {
					exists = scope:task_contract.var:task_contract_target
				}
				scope:task_contract.var:task_contract_target ?= {
					save_scope_as = task_contract_target
				}
			}
			if = {
				limit = {
					exists = scope:task_contract.var:task_contract_object
				}
				scope:task_contract.var:task_contract_object = {
					save_scope_as = task_contract_object
				}
			}
			if = {
				limit = {
					exists = scope:task_contract.var:escorted_artifact
				}
				scope:task_contract.var:escorted_artifact = {
					save_scope_as = escorted_artifact
				}
			}
			if = {
				limit = {
					exists = scope:task_contract.var:escorted_gold
				}
				scope:task_contract.var:escorted_gold = {
					save_scope_as = escorted_gold
				}
			}
			if = {
				limit = {
					exists = scope:task_contract.var:escorted_story
				}
				scope:task_contract.var:escorted_story = {
					save_scope_as = escorted_story
				}
			}
			#show a nice tooltip with which contract, who you are taking and where are you going info
			scope:task_contract = {
				switch = {
					trigger = has_task_contract_type
					laamp_transport_vip = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_vip }
					}
					laamp_transport_ward = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_ward }
					}
					laamp_transport_explorer = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_explorer }
					}
					laamp_transport_artifact = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_artifact }
					}
					laamp_transport_gold = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_gold }
					}
					laamp_transport_animal = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_animal }
					}
					laamp_construction_contract = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_construction }
					}
					laamp_help_increase_control_contract = {
						scope:actor = { custom_tooltip = ep3_contract_event.0001.tt_increase_control }
					}
				}
			}
			#trigger the travel planner
			switch = {
				trigger = yes
				scope:with_domicile = {
					start_travel_plan = {
						destination = scope:task_contract_destination
						travel_with_domicile = yes
						return_trip = no
						on_arrival_destinations = all
					}
					custom_tooltip = ep3_contract_event.0001.with_domicile.tt
				}
				scope:without_domicile = {
					start_travel_plan = {
						destination = scope:task_contract_destination
						on_arrival_destinations = all
					}
				}
			}
		}
	}

	auto_accept = yes
}

##################
# Make a Request
# by Arkadiusz Majewski
# As a LAAMP you can ask characters on your contact list for something 
##################

contact_list_request_interaction = { # make a request, make_a_request
	category = interaction_category_laamp
	interface_priority = 110
	icon = icon_scheme_befriend
	desc = contact_list_request_interaction_desc
	common_interaction = yes

	popup_on_receive = yes
	pause_on_receive = yes

	ai_min_reply_days = 0
	ai_max_reply_days = 0

	cooldown_against_recipient = { months = 8 }

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = { OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority } }
		scope:actor != scope:recipient
	}
	is_valid_showing_failures_only = {
		scope:recipient = {
			is_adult = yes
			is_imprisoned = no
			NOR = {
				is_at_war_with = scope:actor
				has_relation_rival = scope:actor
				has_relation_nemesis = scope:actor
				has_opinion_modifier = {
					target = scope:actor
					modifier = released_from_prison
				}
				has_opinion_modifier = {
					target = scope:actor
					modifier = eviction_ignored_opinion
				}
				house_has_feud_relation_with_trigger = { TARGET = scope:actor }
			}
		}
		scope:actor = {
			NOT = { has_truce = scope:recipient }
		}
		scope:recipient = {
			trigger_if = {
				limit = {
					NOR = {
						is_contact_of = scope:actor
						is_close_or_extended_family_of = scope:actor
						is_consort_of = scope:actor
						AND = {
							OR = {
								capital_province ?= scope:actor.domicile.domicile_location
								any_held_title = {
									title_tier = county
									any_county_province = {
										this = scope:actor.domicile.domicile_location
									}
								}
							}
							scope:actor = { has_perk = embassies_perk }
						}
					}
				}
				is_contact_of = scope:actor
			}
		}
		scope:recipient = {
			custom_tooltip = {
				text = RULER_DESIGNER_ACHIEVEMENTS_NOT_AVAILABLE
				OR = {
					is_landed = yes
					gold > 20
					AND = {
						is_landed_or_landless_administrative = yes
						scope:actor.maa_regiments_count < scope:actor.maa_regiments_max_count
					}
					AND = {
						#Recipient can marry the actor
						can_marry_character_trigger = { CHARACTER = scope:actor }
						#To account for a person marrying their concubines
						trigger_if = {
							limit = {
								is_concubine = yes
							}
							is_concubine_of = scope:actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
						}
					}
				}
			}
		}
	}
	is_highlighted = {
		scope:actor = {
			is_character_interaction_potentially_accepted = {
				recipient = scope:recipient
				interaction = contact_list_request_interaction
			}
		}
	}
	can_send = {
		#Has to be used instead of diplo range checks in laamp to landed interactions
		ep3_laamp_diplo_range_trigger = {
			TARGET = scope:recipient
			LAAMP = scope:actor
		}
	}
	#request provisions
	send_option = {
		flag = request_provisions
		is_valid = {
			scope:recipient = {
				is_landed = yes
			}
		}
	}
	#request gold
	send_option = {
		flag = request_gold
		is_valid = {
			custom_tooltip = {
				scope:recipient.gold > 20
				text = contact_list_request_interaction.request_gold_recipient_not_enough_gold
			}
		}
	}
	#request men-at-arms regiment
	send_option = {
		flag = request_maa
		is_valid = {
			scope:recipient = {
				is_landed_or_landless_administrative = yes
			}
			custom_tooltip = {
				scope:actor.maa_regiments_count < scope:actor.maa_regiments_max_count
				text = contact_list_request_interaction.request_maa_actor_too_many_regiments
			}
		}
	}
	#request a knight
	send_option = {
		flag = request_knights
		is_shown = {
			scope:recipient = { is_ai = yes }
		}
		is_valid = {
			custom_tooltip = {
				scope:recipient = {
					any_knight = {
						is_landed = no
						count > 1
					}
				}
				text = contact_list_request_interaction.request_knights_recipient_not_enough_knights
			}
		}
	}
	#request to arrange a marriage
	send_option = {
		flag = request_marriage
		is_valid = {
			#There is an available marriage, copied from 00_marriage_interactions is_valid_showing_failures_only
			custom_description = {
				text = "basic_can_marry_check_recipient"
				subject = scope:actor
				object = scope:recipient
				scope:recipient = {
					OR = {
						AND = {
							#Recipient can marry the actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
							#To account for a person marrying their concubines
							trigger_if = {
								limit = {
									is_concubine = yes
								}
								is_concubine_of = scope:actor
								can_marry_character_trigger = { CHARACTER = scope:actor }
							}
						}
						AND = {
							is_ruler = yes #(Following are only for rulers)
							OR = {
								any_child = {
									matchmaker = scope:actor
									OR = {
										#Any of recipient's children can marry
										AND = {
											can_marry_trigger = yes
											#To account for a person marrying their concubines
											trigger_if = {
												limit = {
													is_concubine = yes
												}
												is_concubine_of = scope:actor
											}
										}
										#Any of recipient's children are ready to marry their betrothed
										AND = {
											ready_to_marry_betrothed_trigger = yes
											betrothed.matchmaker = scope:actor
										}
									}
								}
								any_courtier = {
									OR = {
										#Any of recipient's courtiers can marry
										AND = {
											can_marry_trigger = yes
											#To account for a person marrying their concubines
											trigger_if = {
												limit = {
													is_concubine = yes
												}
												is_concubine_of = scope:actor
											}
										}
										#Any of recipient's courtiers are ready to marry their betrothed
										AND = {
											ready_to_marry_betrothed_trigger = yes
											betrothed.matchmaker = scope:actor
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	#request new contract
	send_option = {
		flag = request_contract
		is_shown = {
			scope:recipient = { is_ai = yes }
		}
		is_valid = {
			custom_tooltip = {
				text = contact_list_request_interaction.create_contracts.already_has_contract
				scope:actor = {
					any_character_task_contract = {
						count = all
						task_contract_employer != scope:recipient
					}
					is_ai = no
				}
			}
			custom_description = {
				text = contact_list_request_interaction_create_contracts
				object = scope:recipient
				scope:actor = {
					OR = {
						can_create_task_contract = {
							type_name = laamp_raid_contract
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_join_war_contract
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_steal_artifact_contract
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_construction_contract
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_treasure_hunting_contract
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_transport_vip
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_transport_artifact
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_transport_gold
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_transport_ward
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_transport_explorer
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_transport_animal
							employer = scope:recipient
						}
						can_create_task_contract = {
							type_name = laamp_join_faction_contract
							employer = scope:recipient
						}
					}
				}
			}
		}
	}
	#request feast
	send_option = {
		flag = request_feast
		is_shown = {
			scope:actor = {
				has_perk = inspiring_rule_perk
			}
			scope:recipient = {
				is_landed_or_landless_administrative = yes
				capital_province ?= {
					squared_distance = {
						target = scope:actor.capital_province
						value < define:NTaskContract|ADVENTURER_DISTANCE_RESTRICTION
					}
				}
			}
			scope:recipient = { is_ai = yes }
		}
		is_valid = {
			scope:recipient = {
				highest_held_title_tier > tier_barony
				is_at_war = no
				is_available_adult = yes
			}
			scope:actor = {
				custom_tooltip = {
					text = ACTIVITY_SUMMARY_COOLDOWN_HEADER
					NOT = { has_character_flag = laamp_recently_requested_feast }
				}
			}
		}
	}
	#request tournament
	send_option = {
		flag = request_tournament
		is_shown = {
			has_ep2_dlc_trigger = yes
			scope:actor = {
				has_perk = stalwart_leader_perk
			}
			scope:recipient = {
				is_landed = yes
				NOT = { has_variable = conqueror }
				capital_province ?= {
					squared_distance = {
						target = scope:actor.capital_province
						value < define:NTaskContract|ADVENTURER_DISTANCE_RESTRICTION
					}
				}
			}
			scope:recipient = { is_ai = yes }
		}
		is_valid = {
			scope:recipient = {
				highest_held_title_tier > tier_barony
				is_at_war = no
				is_available_adult = yes
			}
			scope:actor = {
				custom_tooltip = {
					text = contact_list_request_interaction.request_tournament_cooldown
					NOT = { has_character_flag = laamp_recently_requested_tourney }
				}
			}
		}
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	cost = {
		prestige = {
			add = 100
			# Upgrade for tier.
			multiply = {
				value = scope:recipient.highest_held_title_tier
				# Discount barons.
				add = -1
			}
			# Upgrade for the more egregious requests.
			## Marriage.
			if = {
				limit = { scope:request_marriage = yes }
				multiply = 1.5
			}
			## Activities.
			### Tournament.
			if = {
				limit = { scope:request_tournament = yes }
				multiply = 2
			}
			### Feast.
			if = {
				limit = { scope:request_feast = yes }
				multiply = 1.25
			}
			min = 100
		}
	}

	on_accept = {
		scope:actor = {
			switch = {
				trigger = yes
				scope:request_tournament ?= {
					scope:recipient = {
						add_opinion = {
							modifier = laamp_used_contact_opinion_special
							target = scope:actor
							opinion = -50
						}
						custom_tooltip = contact_list_request_interaction.request_tournament
					}
					hidden_effect = {
						scope:recipient = {
							add_character_flag = {
								flag = tournament_ai_override
							}
							ai_attempt_to_host_activity = activity_tournament
						}
						scope:actor = {
							add_character_flag = {
								flag = laamp_recently_requested_tourney
								days = 3650
							}
						}
					}
				}
				scope:request_feast ?= {
					scope:actor = { custom_tooltip = contact_list_request_interaction.request_feast_honor }
					scope:recipient = {
						add_opinion = {
							modifier = laamp_used_contact_opinion
							target = scope:actor
							opinion = -30
						}
						custom_tooltip = contact_list_request_interaction.request_feast
					}
					hidden_effect = {
						scope:recipient = {
							add_character_flag = {
								flag = feast_ai_override
							}
							ai_attempt_to_host_activity = activity_feast
						}
						scope:actor = {
							add_character_flag = {
								flag = laamp_recently_requested_feast
								days = 1875
							}
						}
					}
				}
				scope:request_provisions ?= {
					save_scope_value_as = {
						name = provisions_to_add_value
						value = {
							if = {
								limit = { scope:recipient.highest_held_title_tier >= tier_empire }
								add = monumental_provisions_gain
							}
							else_if = {
								limit = { scope:recipient.highest_held_title_tier >= tier_kingdom }
								add = massive_provisions_gain
							}
							else_if = {
								limit = { scope:recipient.highest_held_title_tier >= tier_duchy }
								add = major_provisions_gain
							}
							else_if = {
								limit = { scope:recipient.highest_held_title_tier >= tier_county }
								add = medium_provisions_gain
							}
							else = { add = minor_provisions_gain }
						}
					}
					if = {
						limit = {
							scope:actor.domicile.max_provisions < {
								value = {
									value = scope:provisions_to_add_value
									add = scope:actor.domicile.provisions
								}
							}
						}
						save_scope_value_as = {
							name = gained_provisions_value
							value = {
								value = scope:actor.domicile.max_provisions
								subtract = scope:actor.domicile.provisions
							}
						}
						save_scope_value_as = {
							name = provisions_overflow_value
							value = {
								value = scope:provisions_to_add_value
								subtract = scope:gained_provisions_value
							}
						}
						save_scope_value_as = {
							name = provisions_transformed_to_gold_value
							value = {
								value = scope:provisions_overflow_value
								multiply = 0.05
							}
						}
					}
					else = {
						save_scope_value_as = {
							name = gained_provisions_value
							value = scope:provisions_to_add_value
						}
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = contact_list_request_interaction.request_provisions_tt
						left_icon = scope:actor
						right_icon = scope:recipient
						scope:recipient = {
							add_opinion = {
								modifier = laamp_used_contact_opinion_special
								target = scope:actor
								opinion = -20
							}
							if = {
								limit = {
									exists = capital_county
								}
								hidden_effect = {
									capital_county = {
										add_county_modifier = {
											modifier = ep3_gave_out_provisions
											years = 3
										}
										#save it for loc in custom tooltip
										save_scope_as = capital_county
									}
								}
								custom_tooltip = contact_list_request_interaction.capital_county_modifier
							}
						}
						scope:actor = {
							custom_tooltip = contact_list_request_interaction.gained_provisions_value
							hidden_effect = {
								domicile = {
									change_provisions = scope:gained_provisions_value
								}
							}
							if = {
								limit = {
									exists = scope:provisions_transformed_to_gold_value
								}
								custom_tooltip = {
									text = overflowing_provisions_to_gold.tt
									add_gold = scope:provisions_transformed_to_gold_value
								}
							}
						}
					}
				}
				scope:request_gold ?= {
					save_scope_value_as = {
						name = requested_gold_value
						value = {
							value = scope:recipient.gold
							divide = 4
							multiply = {
								value = scope:recipient.ai_greed
								divide = -300
								add = 1
							}
							if = {
								limit = {
									scope:recipient = {
										has_any_best_good_relationship_with_character_trigger = { CHARACTER = scope:actor }
									}
								}
								multiply = 1.5
							}
							else_if = {
								limit = {
									scope:recipient = {
										has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:actor }
									}
								}
								multiply = 1.25
							}
							max = {
								scope:recipient.culture = {
									if = {
										limit = {
											has_cultural_era_or_later = culture_era_late_medieval
										}
										value = 250
									}
									else_if = {
										limit = {
											has_cultural_era_or_later = culture_era_high_medieval
										}
										value = 200
									}
									else_if = {
										limit = {
											has_cultural_era_or_later = culture_era_early_medieval
										}
										value = 150
									}
									else_if = {
										limit = {
											has_cultural_era_or_later = culture_era_tribal
										}
										value = 100
									}
									else = {
										value = 50
									}
								}
							}
							floor = yes
							min = 5
						}
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = contact_list_request_interaction.request_gold_tt
						left_icon = scope:actor
						right_icon = scope:recipient
						scope:actor = {
							show_as_tooltip = {
								add_gold = scope:requested_gold_value
							}
						}
						scope:recipient = {
							pay_short_term_gold = {
								target = scope:actor
								gold = scope:requested_gold_value
							}
							add_opinion = {
								modifier = laamp_used_contact_opinion
								target = scope:actor
								opinion = -40
							}
						}
					}
				}
				scope:request_maa ?= {
					custom_tooltip = contact_list_request_interaction.request_maa
					hidden_effect = {
						ep3_pick_random_maa_regiment_effect = {
							OWNER = scope:recipient
							INCLUDE_BASIC_MAA = no
							INCLUDE_SIEGE_MAA = no
						}
						send_interface_toast = {
							type = event_toast_effect_good
							title = contact_list_request_interaction.request_maa_tt
							left_icon = scope:actor
							right_icon = scope:recipient
							ep3_create_random_maa_regiment_effect = {
								OWNER = scope:actor
								SIZE = 1
							}
						}
					}
					scope:recipient = {
						add_opinion = {
							modifier = laamp_used_contact_opinion
							target = scope:actor
							opinion = -40
						}
					}
				}
				scope:request_knights ?= {
					scope:actor = {
						custom_tooltip = contact_list_request_interaction.request_knights
						trigger_event = ep3_interactions_events.0101
					}
					scope:recipient = {
						add_opinion = {
							modifier = laamp_used_contact_opinion
							target = scope:actor
							opinion = -30
						}
					}
				}
				scope:request_marriage ?= {
					scope:actor = {
						custom_tooltip = contact_list_request_interaction.request_marriage
						custom_tooltip = contact_list_request_interaction.request_marriage.marriagable_women
						custom_tooltip = contact_list_request_interaction.request_marriage.marriagable_men
						custom_tooltip = contact_list_request_interaction.request_marriage.warning
						if = {
							limit = { is_ai = no }
							open_interaction_window = {
								interaction = arrange_marriage_interaction
								actor = scope:actor
								recipient = scope:recipient
							}
						}
						add_to_variable_list = {
							name = contact_list_marriage
							target = scope:recipient
						}
					}
					scope:recipient = {
						add_opinion = {
							modifier = laamp_used_contact_opinion
							target = scope:actor
							opinion = -50
						}
					}
				}
				scope:request_contract ?= {
					scope:actor = {
						custom_tooltip = contact_list_request_interaction.request_contract
						hidden_effect = {
							random_list = {
								#laamp_raid_contract
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_raid_contract
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_mercenaries
									}
									create_task_contract = {
										task_contract_type = laamp_raid_contract
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_raid_contract
										value = yes
									}
								}
								#laamp_join_war_contract
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_join_war_contract
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_mercenaries
									}
									create_task_contract = {
										task_contract_type = laamp_join_war_contract
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_join_war_contract
										value = yes
									}
								}
								#laamp_steal_artifact_contract
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_steal_artifact_contract
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_brigands
									}
									create_task_contract = {
										task_contract_type = laamp_steal_artifact_contract
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_steal_artifact_contract
										value = yes
									}
								}
								#laamp_construction_contract
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_construction_contract
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_scholars
									}
									create_task_contract = {
										task_contract_type = laamp_construction_contract
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_construction_contract
										value = yes
									}
								}
								#laamp_treasure_hunting_contract
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_treasure_hunting_contract
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										OR = {
											has_realm_law = camp_purpose_brigands
											has_realm_law = camp_purpose_explorers
										}
									}
									create_task_contract = {
										task_contract_type = laamp_treasure_hunting_contract
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_treasure_hunting_contract
										value = yes
									}
								}
								#laamp_transport_vip
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_transport_vip
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_explorers
									}
									create_task_contract = {
										task_contract_type = laamp_transport_vip
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_transport_vip
										value = yes
									}
								}
								#laamp_transport_artifact
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_transport_artifact
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										OR = {
											has_realm_law = camp_purpose_brigands
											has_realm_law = camp_purpose_explorers
										}
									}
									create_task_contract = {
										task_contract_type = laamp_transport_artifact
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_transport_artifact
										value = yes
									}
								}
								#laamp_transport_gold
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_transport_gold
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										OR = {
											has_realm_law = camp_purpose_brigands
											has_realm_law = camp_purpose_explorers
										}
									}
									create_task_contract = {
										task_contract_type = laamp_transport_gold
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_transport_gold
										value = yes
									}
								}
								#laamp_transport_ward
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_transport_ward
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_explorers
									}
									create_task_contract = {
										task_contract_type = laamp_transport_ward
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_transport_ward
										value = yes
									}
								}
								#laamp_transport_explorer
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_transport_explorer
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_explorers
									}
									create_task_contract = {
										task_contract_type = laamp_transport_explorer
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_transport_explorer
										value = yes
									}
								}
								#laamp_transport_animal
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_transport_animal
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_explorers
									}
									create_task_contract = {
										task_contract_type = laamp_transport_animal
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_transport_animal
										value = yes
									}
								}
								#laamp_join_faction_contract
								1 = {
									trigger = {
										can_create_task_contract = {
											type_name = laamp_join_faction_contract
											employer = scope:recipient
										}
									}
									modifier = {
										add = 10
										has_realm_law = camp_purpose_mercenaries
									}
									create_task_contract = {
										task_contract_type = laamp_join_faction_contract
										location = scope:recipient.capital_province
										task_contract_employer = scope:recipient
										task_contract_tier = scope:recipient.task_contract_tier_value
										save_scope_as = new_contract
									}
									save_scope_value_as = {
										name = create_laamp_join_faction_contract
										value = yes
									}
								}
							}
							scope:new_contract = {
								open_view_data = {
									view = task_contract
									player = scope:actor
								}
							}
						}
					}
					scope:recipient = {
						add_opinion = {
							modifier = laamp_used_contact_opinion
							target = scope:actor
							opinion = -5
						}
						show_as_tooltip = {
							custom_description_no_bullet = { text = contact_list_request_interaction_possible_outcomes }
							if = {
								limit = {
									scope:create_laamp_raid_contract ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_raid_contract
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_join_war_contract ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_join_war_contract
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_construction_contract ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_construction_contract
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_steal_artifact_contract ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_steal_artifact_contract
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_treasure_hunting_contract ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_treasure_hunting_contract
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_transport_vip ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_transport_vip
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_transport_artifact ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_transport_artifact
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_transport_gold ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_transport_gold
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_transport_ward ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_transport_ward
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_transport_explorer ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_transport_explorer
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_transport_animal ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_transport_animal
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
							if = {
								limit = {
									scope:create_laamp_join_faction_contract ?= yes
								}
								create_task_contract = {
									task_contract_type = laamp_join_faction_contract
									location = scope:recipient.location
									task_contract_employer = scope:recipient
									task_contract_tier = scope:recipient.task_contract_tier_value
								}
							}
						}
					}
				}
			}
			if = {
				limit = {
					has_usable_hook = scope:recipient
					scope:recipient = { is_ai = yes }
				}
				use_hook = scope:recipient
				custom_tooltip = contact_list_request_interaction.tt.hook_auto_used
			}
		}
		scope:recipient = {
			add_prestige = {
				value = scope:actor.prestige_level
				multiply = 100
			}
		}
	}

	can_send_despite_rejection = no
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 3
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	ai_targets = { ai_recipients = contacts }
	ai_targets = { ai_recipients = hooked_characters }
	auto_accept = {
		scope:actor = { has_usable_hook = scope:recipient }
		scope:recipient = { is_ai = yes }
	}
	ai_potential = { OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority } }
	ai_accept = {
		base = 0

		modifier = {
			add = -50
			scope:recipient = {
				NOR = {
					is_contact_of = scope:actor
					is_close_or_extended_family_of = scope:actor
					is_consort_of = scope:actor
				}
			}
			desc = contact_list_request_interaction.no_contact
		}

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.5
			desc = AI_OPINION_REASON
		}

		modifier = {
			add = -50
			scope:recipient = {
				has_opinion_modifier = {
					modifier = laamp_used_contact_opinion
					target = scope:actor
				}
			}
			desc = contact_list_request_interaction.repeated_request
		}

		ai_value_modifier = {
			ai_compassion = -0.5
			ai_greed = -0.5
			min = -50
			max = 50
		}

		modifier = {
			add = 30
			desc = contact_list_request_interaction.request_gold.contract
			scope:request_contract ?= yes
		}

		modifier = {
			add = -30
			desc = contact_list_request_interaction.request_gold.poor_recipient
			scope:request_gold ?= yes
			scope:recipient = {
				gold < major_gold_value
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.request_gold.stewardship
			OR = {
				scope:request_provisions ?= yes
				scope:request_gold ?= yes
			}
			scope:recipient = {
				has_trait = education_stewardship
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.request_gold.martial
			OR = {
				scope:request_maa ?= yes
				scope:request_knights ?= yes
			}
			scope:recipient = {
				has_trait = education_martial
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.request.defensive_negotiations_perk
			OR = {
				scope:request_maa ?= yes
				scope:request_knights ?= yes
			}
			scope:actor = {
				has_perk = defensive_negotiations_perk
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.relevant_focus
			scope:request_marriage ?= yes
			scope:recipient = {
				OR = {
					has_focus = diplomacy_family_focus
					has_focus = intrigue_temptation_focus
					has_focus = martial_chivalry_focus
				}
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.relevant_focus
			OR = {
				scope:request_maa ?= yes
				scope:request_knights ?= yes
			}
			scope:recipient = {
				OR = {
					has_focus = martial_strategy_focus
					has_focus = martial_chivalry_focus
					has_focus = martial_authority_focus
				}
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.request_knights.plenty
			scope:request_knights ?= yes
			scope:recipient = {
				number_of_knights > 6
			}
		}

		modifier = {
			add = -25
			desc = contact_list_request_interaction.request_knights.little
			scope:request_knights ?= yes
			scope:recipient = {
				number_of_knights <= 6
			}
		}

		modifier = {
			add = -25
			desc = contact_list_request_interaction.powerful
			scope:request_maa ?= yes
			scope:actor = {
				max_military_strength >= 400
				max_military_strength < 800
			}
		}

		modifier = {
			add = -50
			desc = contact_list_request_interaction.very_powerful
			scope:request_maa ?= yes
			scope:actor.max_military_strength >= 800
		}

		modifier = {
			add = -30
			desc = contact_list_request_interaction.stronger_than_me
			scope:request_maa ?= yes
			scope:actor.max_military_strength > scope:recipient.max_military_strength
		}

		modifier = {
			add = -25
			desc = contact_list_request_interaction.traveler
			scope:request_provisions ?= yes
			scope:recipient = {
				has_trait = lifestyle_traveler
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.adventurer
			scope:recipient = {
				has_trait = adventurer
			}
		}

		modifier = {
			add = 25
			desc = contact_list_request_interaction.adventurer_follower
			scope:recipient = {
				has_trait = adventurer_follower
			}
		}

		modifier = {
			add = 20
			desc = contact_list_request_interaction.flexible_truces_perk
			scope:actor = {
				has_perk = flexible_truces_perk
			}
		}

		# Your level of prestige.
		## Impotent
		modifier = {
			add = -10
			desc = AI_OPINION_PRESTIGE_LEVEL_DISGRACED
			scope:actor.prestige_level = 0
		}
		## Influential
		### Would be zero, so it wouldn't appear.
		## Persuasive
		modifier = {
			add = 5
			desc = AI_OPINION_PRESTIGE_LEVEL_DISTINGUISHED
			scope:actor.prestige_level = 2
			scope:actor = {
				NOT = {
					has_perk = true_ruler_perk
				}
			}
		}
		## Controlling
		modifier = {
			add = 10
			desc = AI_OPINION_PRESTIGE_LEVEL_ILLUSTRIOUS
			scope:actor.prestige_level = 3
			scope:actor = {
				NOT = {
					has_perk = true_ruler_perk
				}
			}
		}
		## Authoritative
		modifier = {
			add = 20
			desc = AI_OPINION_PRESTIGE_LEVEL_EXALTED
			scope:actor.prestige_level = 4
			scope:actor = {
				NOT = {
					has_perk = true_ruler_perk
				}
			}
		}
		## Imperious
		modifier = {
			add = 30
			desc = AI_OPINION_PRESTIGE_LEVEL_LEGENDARY
			scope:actor.prestige_level = 5
			scope:actor = {
				NOT = {
					has_perk = true_ruler_perk
				}
			}
		}
		
		## Persuasive
		modifier = {
			add = 6
			desc = AI_OPINION_PRESTIGE_LEVEL_DISTINGUISHED
			scope:actor.prestige_level = 2
			scope:actor = { has_perk = true_ruler_perk }
		}
		## Controlling
		modifier = {
			add = 12
			desc = AI_OPINION_PRESTIGE_LEVEL_ILLUSTRIOUS
			scope:actor.prestige_level = 3
			scope:actor = { has_perk = true_ruler_perk }
		}
		## Authoritative
		modifier = {
			add = 24
			desc = AI_OPINION_PRESTIGE_LEVEL_EXALTED
			scope:actor.prestige_level = 4
			scope:actor = { has_perk = true_ruler_perk }
		}
		## Imperious
		modifier = {
			add = 36
			desc = AI_OPINION_PRESTIGE_LEVEL_LEGENDARY
			scope:actor.prestige_level = 5
			scope:actor = { has_perk = true_ruler_perk }
		}
	}

	ai_will_do = {
		base = 50

		# Set up the AI so that they actually know how to use the send_options.
		## Troops, if they don't have gold.
		modifier = {
			add = 50
			scope:recipient.gold < 50
			scope:request_maa = yes
		}
		## Gold is always good.
		modifier = {
			add = 50
			scope:recipient.gold >= 30
			scope:request_gold = yes
		}
		## Feasts are especially attractive for some adventurers.
		modifier = {
			add = 50
			scope:request_feast = yes
			OR = {
				prestige <= 500
				has_trait = arrogant
				has_trait = gluttonous
				has_trait = gregarious
				has_trait = drunkard
			}
		}
		## Provisions are fine, but nothing special for the AI.
		modifier = {
			add = 5
			scope:request_provisions = yes
			scope:actor.domicile.provisions < scope:actor.domicile.max_provisions
		}
		## Don't go crazy with tournaments
		modifier = {
			factor = 0
			scope:request_tournament = yes
		}
		modifier = {
			factor = 0
			NOR = {
				scope:request_provisions = yes
				scope:request_gold = yes
				scope:request_maa = yes
				scope:request_knights = yes
				scope:request_marriage = yes
				scope:request_contract = yes
				scope:request_feast = yes
				scope:request_tournament = yes
			}
		}
	}
}

##################
# Seize Realm Scheme
# by Joe Parkin
##################

seize_realm_interaction = {
	icon = icon_scheme_seizure
	category = interaction_category_laamp
	interface_priority = 10

	ignores_pending_interaction_block = yes

	scheme = seize_realm

	send_name = START_SCHEME

	is_shown = {
		scope:recipient != scope:actor
		scope:actor = { OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority } }
		scope:recipient = {
			is_landed = yes
			highest_held_title_tier >= tier_county
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			can_start_scheme = {
				type = seize_realm
				target_character = scope:recipient
			}
		}
		NOT = {
			scope:recipient = { has_strong_hook = scope:actor }
		}
		#Has to be used instead of diplo range checks in laamp to landed interactions
		ep3_laamp_diplo_range_trigger = {
			TARGET = scope:recipient
			LAAMP = scope:actor
		}
	}

	desc = {
		triggered_desc = {
			trigger = {
				scope:actor = {
					can_start_scheme = {
						type = seize_realm
						target_character = scope:recipient
					}
				}
			}
			desc = scheme_interaction_tt_seize_realm_approved
		}
	}

	# Scheme Starter Packages
	options_heading = schemes.t.agent_packages
	send_options_exclusive = yes
	## Balanced agents.
	send_option = {
		flag = agent_focus_balance
		current_description = seize_realm_interaction.tt.agent_focus_balance
	}
	## Focused on Success Chance.
	send_option = {
		flag = agent_focus_success
		current_description = seize_realm_interaction.tt.agent_focus_success
	}
	## Focused on Speed.
	send_option = {
		flag = agent_focus_speed
		current_description = seize_realm_interaction.tt.agent_focus_speed
	}
	## Focused on Secrecy.
	send_option = {
		flag = agent_focus_secrecy
		current_description = seize_realm_interaction.tt.agent_focus_secrecy
	}

	on_accept = {
		scope:actor = {
			stress_impact = {
				compassionate = major_stress_impact_gain
				honest = medium_stress_impact_gain
				just = medium_stress_impact_gain
			}
		}
		scope:actor = {
			send_interface_toast = {
				title = start_seize_realm_interaction_notification

				left_icon = scope:actor					
				right_icon = scope:recipient

				# Success.
				if = {
					limit = { scope:agent_focus_success ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = seize_realm
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_muscle
						AGENT_2 = agent_muscle
						AGENT_3 = agent_diplomat
						# Speed.
						AGENT_4 = agent_infiltrator
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}
				# Speed.
				else_if = {
					limit = { scope:agent_focus_speed ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = seize_realm
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Speed.
						AGENT_1 = agent_infiltrator
						AGENT_2 = agent_footpad
						AGENT_3 = agent_planner
						# Success.
						AGENT_4 = agent_muscle
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}
				# Secrecy.
				else_if = {
					limit = { scope:agent_focus_secrecy ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = seize_realm
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Secrecy.
						AGENT_1 = agent_lookout
						AGENT_2 = agent_decoy
						AGENT_3 = agent_decoy
						# Success.
						AGENT_4 = agent_muscle
						# Speed.
						AGENT_5 = agent_infiltrator
					}
				}
				# Balanced.
				else = {
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = seize_realm
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_muscle
						AGENT_2 = agent_muscle
						# Speed.
						AGENT_3 = agent_infiltrator
						AGENT_4 = agent_planner
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}

				show_as_tooltip = {
					stress_impact = {
						honest = medium_stress_impact_gain
						just = medium_stress_impact_gain
					}
				}
			}
		}
		custom_tooltip = seize_realm_warning
	}
	
	auto_accept = yes
}

##################
# Reinforce Soldiers
# by Joe Parkin
##################

reinforce_soldiers_interaction = {
	icon = owner_in_debt
	category = interaction_category_laamp
	interface_priority = 10
	common_interaction = yes
	desc = reinforce_soldiers_interaction_desc
	cooldown = { months = 6 }

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = {
			government_allows = conditional_maa_refill
			NOT = { government_has_flag = government_is_nomadic }
		}
		scope:recipient = scope:actor
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			text = reinforce_soldiers_any_regiment_tt
			scope:actor.maa_regiments_count >= 1
		}
		custom_tooltip = {
			text = reinforce_soldiers_unfull_regiment_tt
			scope:actor = {
				any_maa_regiment = { maa_regiments_valid_for_cheap_reinforcement_trigger = yes }
			}
		}
		custom_tooltip = {
			text = SUPPLY_LOSS_AT_SEA
			scope:actor = { any_laamp_portion_at_sea_trigger = no }
		}
	}

	send_options_exclusive = yes

	send_option = {
		flag = skill
		localization = reinforce_soldiers_option_skill
		current_description = reinforce_soldiers_option_skill_desc
	}

	send_option = {
		flag = prestige
		localization = reinforce_soldiers_option_prestige
		current_description = reinforce_soldiers_option_prestige_desc
	}

	send_option = {
		flag = piety
		localization = reinforce_soldiers_option_piety
		current_description = reinforce_soldiers_option_piety_desc
		is_valid = {
			custom_tooltip = {
				text = reinforce_soldiers_option_piety_tt
				scope:actor.domicile.domicile_location.county ?= {
					OR = {
						holder ?= { government_has_flag = government_is_theocracy }
						any_county_province = {
							barony = {
								is_under_holy_order_lease = yes
								lessee.faith = scope:actor.faith
							}
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = dread
		localization = reinforce_soldiers_option_dread
		current_description = reinforce_soldiers_option_dread_desc
		is_valid = {
			custom_tooltip = {
				text = reinforce_soldiers_option_dread_tt
				OR = {
					scope:actor.dread >= reinforce_soldiers_dread_cost_value
					scope:actor = {
						has_character_modifier = ep3_friend_to_old_soldiers_modifier
					}
				}
			}
		}
	}

	on_accept = {
		scope:actor = {
			switch = {
				trigger = yes
				scope:prestige = {
					if = {
						limit = {
							is_ai = no
						}
						add_prestige = {
							value = {
								value = -75
								if = {
									limit = {
										scope:actor.domicile.domicile_location.county.holder ?= { government_has_flag = government_is_tribal }
									}
									add = 25
								}
								multiply = reinforce_soldiers_reinforcable_regiments_value
							}
						}
					}
					reinforce_soldiers_regiment_effect = yes
				}
				scope:piety = {
					add_piety = {
						value = {
							value = minor_piety_loss
							multiply = reinforce_soldiers_reinforcable_regiments_value
						}
					}
					reinforce_soldiers_regiment_effect = yes
				}
				scope:skill = {
					duel = {
						skills = { diplomacy prowess }
						value = high_skill_rating
						10 = {
							compare_modifier = {
								value = scope:duel_value
								multiplier = 3.5
								min = 2
							}
							desc = reinforce_soldiers_skill_success
							send_interface_toast = {
								type = event_toast_effect_good
								title = reinforce_soldiers_skill_success
								reinforce_soldiers_regiment_effect = yes
							}
						}
						10 = {
							compare_modifier = {
								value = scope:duel_value
								multiplier = -3.5
								min = 2
							}
							desc = reinforce_soldiers_skill_failure
							send_interface_toast = {
								type = event_toast_effect_bad
								title = reinforce_soldiers_skill_failure
								if = {
									limit = {
										is_ai = no
									}
									add_prestige = -100
								}
							}
						}
					}
				}
				scope:dread = {
					random_list = {
						1 = {
							modifier = {
								add = {
									value = scope:actor.dread
									multiply = 2
								}
							}
							desc = reinforce_soldiers_skill_success
							send_interface_toast = {
								type = event_toast_effect_good
								title = reinforce_soldiers_skill_success
								reinforce_soldiers_regiment_effect = yes
							}
						}
						100 = {
							min = 25
							modifier = {
								add = {
									value = 0
									subtract = scope:actor.dread
								}
							}
							desc = reinforce_soldiers_skill_failure
							send_interface_toast = {
								type = event_toast_effect_bad
								title = reinforce_soldiers_skill_failure
								if = {
									limit = {
										is_ai = no
									}
									add_prestige = minor_prestige_loss
								}
							}
						}
					}
					add_dread = {
						value = reinforce_soldiers_dread_cost_value
						multiply = -1
					}
				}
			}
		}
	}

	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 6
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	ai_targets = {
		ai_recipients = self
	}
	ai_potential = {
		government_allows = conditional_maa_refill
		any_maa_regiment = { maa_current_troops_count < maa_max_troops_count }
	}

	ai_will_do = {
		base = 100

		modifier = {
			scope:dread = yes
			add = 5
		}

		modifier = {
			scope:prestige = yes
			add = 3
		}

		modifier = {
			scope:piety = yes
			add = 2
		}

		modifier = {
			scope:skill = yes
			add = 1
		}
	}
}

##################
# Hire LAAMP as mercenaries
# by Arkadiusz Majewski
# As a landed ruler hire a LAAMP to join your war
##################

hire_laamp_mercenaries_interaction = {
	category = interaction_category_diplomacy
	icon = alliance
	desc = hire_laamp_mercenaries_interaction_desc

	greeting = positive
	notification_text = hire_laamp_mercenaries_interaction_notification
	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 60
	ai_min_reply_days = 0
	ai_max_reply_days = 0

	cooldown_against_recipient = { years = 3 }
	can_send_despite_rejection = yes
	ai_accept_negotiation = yes
	send_options_exclusive = no
	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:recipient = {
			has_government = landless_adventurer_government
		}
		scope:actor != scope:recipient
		scope:actor = {
			trigger_if = { # Avoid calling Adventurers without armies
				limit = {
					is_ai = yes
				}
				scope:recipient.current_military_strength >= 100
			}
		}
	}
	is_valid_showing_failures_only = {
		scope:recipient = {
			is_travelling = no
			is_at_war = no
			is_imprisoned = no
		}
		scope:actor = {
			custom_tooltip = {
				text = MERCENARY_COMPANY_COST_BANKRUPTCY
				debt_level <= 1
			}
			is_at_war = yes
			custom_tooltip = {
				text = I_MUST_BE_THE_WAR_LEADER		
				any_character_war = {
					is_war_leader = scope:actor
				}
			}
			NOT = {
				is_at_war_with = scope:recipient
			}
		}
		scope:actor = {
			NOT = {
				any_war_ally = {
					count >= 3
					has_government = landless_adventurer_government
				}
			}
		}
		scope:actor = {
			OR = {
				is_ai = no
				any_character_war = {
					count = all
					OR = {
						AND = {
							primary_attacker = scope:actor
							defender_war_score <= 95
						}
						AND = {
							primary_defender = scope:actor
							attacker_war_score <= 95
						}
					}
				}
			}
		}
		# Adventurers can't join wars in places they're exiled from.
		trigger_if = {
			limit = {
				scope:actor = { top_liege != this }
			}
			custom_tooltip = {
				text = the_wake_1
				NOT = {
					is_laamp_exiled_from_province_trigger = {
						PROVINCE = scope:actor.capital_province
						LAAMP = scope:recipient
					}
				}
			}
		}
	}

	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:hook = yes
			scope:actor = { has_strong_hook = scope:recipient }
		}
	}

	on_accept = {
		scope:actor = {
			every_character_war = {
				add_to_list = actor_wars
			}
		}
		every_in_list = {
			list = actor_wars
			limit = {
				primary_defender = scope:actor
			}
			add_to_list = join_as_defender
			remove_from_list = actor_wars
		}
		every_in_list = {
			list = actor_wars
			limit = {
				primary_attacker = scope:actor
			}
			add_to_list = join_as_attacker
			remove_from_list = actor_wars
		}
		
		every_in_list = {
			list = join_as_attacker
			add_attacker = scope:recipient
		}
		every_in_list = {
			list = join_as_defender
			add_defender = scope:recipient
		}
		#list all wars that scope:recipient can't join
		every_in_list = {
			list = actor_wars
			custom = hire_laamp_mercenaries_interaction.not_primary_side
			# I swear it was the only way to make the tooltip look nice
			every_in_list = {
				list = actor_wars
				save_temporary_scope_as = war
				custom_tooltip = hire_laamp_mercenaries_interaction.war
			}
		}
		scope:actor = {
			pay_short_term_gold = {
				target = scope:recipient
				gold = ep3_hire_laamp_mercs_cost_value
			}
			add_to_variable_list = {
				name = hired_mercenaries
				target = scope:recipient
			}
			custom_tooltip = hire_laamp_mercenaries_interaction.contribution_pay
			if = {
				limit = {
					has_title = title:h_china
					situation:dynastic_cycle ?= {
						situation_top_has_catalyst = catalyst_hegemon_hired_mercenary
					}
				}
				trigger_situation_catalyst = {
					catalyst = catalyst_hegemon_hired_mercenary
					character = scope:actor
				}
			}
		}
		scope:recipient = {
			add_character_flag = joined_as_mercenary
		}
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_gain
			DESC = clan_unity_alliance.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		debug_log = "Landed ruler hired a LAAMP to join wars"
		debug_log_scopes = yes
		#for debug purposes:
		#if = {
		#	limit = { NOT = { exists = global_var:hire_laamp_as_mercs_accepted } }
		#	set_global_variable = {
		#		name = hire_laamp_as_mercs_accepted
		#		value = 0
		#	}
		#}
		#else = {
		#	change_global_variable = {
		#		name = hire_laamp_as_mercs_accepted
		#		add = 1
		#	}
		#}
	}

	on_decline = {
		scope:recipient = {
			trigger_event = ep3_interactions_events.0111
			custom_tooltip = hire_laamp_mercenaries_interaction.negotiation_offer
		}
		#for debug purposes:
		#if = {
		#	limit = { NOT = { exists = global_var:hire_laamp_as_mercs_rejected } }
		#	set_global_variable = {
		#		name = hire_laamp_as_mercs_rejected
		#		value = 0
		#	}
		#}
		#else = {
		#	change_global_variable = {
		#		name = hire_laamp_as_mercs_rejected
		#		add = 1
		#	}
		#}
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_alliance_refusal.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	# Use hook
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = SCHEME_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	ai_frequency_by_tier = {
		barony = 0
		county = 12
		duchy = 12
		kingdom = 6
		empire = 6
		hegemony = 6
	}
	ai_targets = {
		ai_recipients = nearby_domicile_owners
	}
	ai_potential = {
		any_character_war = {
			is_war_leader = prev
		}
		war_chest_gold >= quarter_ai_war_chest_gold_maximum
	}
	ai_accept = {
		base = -20

		modifier = {  # merc laamp wants to join
			add = 50
			scope:recipient = {
				has_realm_law = camp_purpose_mercenaries
			}
			desc = camp_purpose_mercenaries_reason
		}

		modifier = {  # don't join many losing wars
			save_temporary_scope_value_as = {
				name = attacker_war
				value = 2
			}
			scope:actor = {
				any_character_war = {
					primary_attacker = scope:actor
					attacker_war_score < 0
					save_temporary_scope_value_as = {
						name = attacker_war
						value = {
							value = scope:attacker_war
							multiply = attacker_war_score
						}
					}
				}
			}
			add = scope:attacker_war
			desc = attacker_losisng_too_much_reason
		}
		modifier = {  # don't join many losing wars
			add = scope:defender_war
			save_temporary_scope_value_as = {
				name = defender_war
				value = 2
			}
			scope:actor = {
				any_character_war = {
					primary_defender = scope:actor
					defender_war_score < 0
					save_temporary_scope_value_as = {
						name = defender_war
						value = {
							value = scope:defender_war
							multiply = defender_war_score
						}
					}
				}
			}
			desc = defender_losisng_too_much_reason
		}

		modifier = {  # Refuse call against Heir
			add = -1000
			exists = scope:recipient.player_heir
			scope:actor = {
				any_character_war = {
					OR = {
						primary_defender = scope:recipient.player_heir
						primary_attacker = scope:recipient.player_heir
					}
				}
			}
			desc = WONT_FIGHT_HEIR_REASON
		}

		modifier = {  # Refuse call against Spouse
			add = -1000
			scope:actor = {
				any_character_war = {
					OR = {
						primary_defender = {
							is_spouse_of = scope:recipient
						}
						primary_attacker =  {
							is_spouse_of = scope:recipient
						}
					}
				}
			}
			desc = WONT_FIGHT_SPOUSE_REASON
		}

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			desc = AI_OPINION_REASON
		}

		modifier = {  # Tends to join if low on cash
			add = 50
			scope:recipient = {
				short_term_gold <= minor_gold_value
			}
			desc = NEEDS_CASH
		}

		modifier = {  # Tends to join defensive wars
			add = 50
			scope:actor = {
				any_character_war = {
					primary_defender = scope:actor
				}
			}
			desc = DEFENSIVE_WAR_REASON
		}
		
		modifier = { # Same language
			add = 5
			desc = speaks_same_language_interaction_reason
			scope:actor = {
				knows_language_of_culture = scope:recipient.culture
			}
		}

		compare_modifier = { # Likes fighting infidels
			trigger = {
				scope:recipient.faith = scope:actor.faith
				scope:actor = {
					any_character_war = {
						trigger_if = {
							limit = {
								primary_defender = scope:actor
							}
							primary_attacker = {
								faith = {
									faith_hostility_level = {
										target = scope:actor.faith
										value >= religious_cb_enabled_hostility_level
									}
								}
							}
						}
						trigger_else_if = {
							limit = {
								primary_attacker = scope:actor
							}
							primary_defender = {
								faith = {
									faith_hostility_level = {
										target = scope:actor.faith
										value >= religious_cb_enabled_hostility_level
									}
								}
							}	
						}
						trigger_else = {
							always = no
						}
					}
				}
			}
			target = scope:recipient
			value = ai_zeal
			desc = "ZEAL_AGAINST_INFIDELS"
			min = 0
			multiplier = 0.5
		}

		modifier = {  # Reluctant to attack another ally
			add = -50
			scope:actor = {
				any_character_war = {
					primary_defender != scope:actor
					primary_defender = {
						is_allied_to = scope:recipient
					}
				}
			}
			desc = ATTACK_ON_ALLY_REASON
		}

		modifier = {  # Reluctant to defend against another ally
			add = -25
			scope:actor = {
				any_character_war = {
					primary_attacker != scope:actor
					primary_attacker = {
						is_allied_to = scope:recipient
					}
				}
			}
			desc = WAR_WITH_ALLY_REASON
		}

		modifier = {  # Reluctant to join wars against religious brethren.
			add = -50
			scope:recipient.faith != scope:actor.faith
			scope:actor = {
				any_character_war = {
					OR = {
						using_cb = minor_religious_war
						using_cb = religious_war
						using_cb = major_religious_war
						using_cb = undirected_great_holy_war
						using_cb = directed_great_holy_war
						OR = {
							AND = {
								primary_attacker != scope:actor
								primary_attacker.faith = scope:recipient.faith
							}
							AND = {
								primary_defender != scope:actor
								primary_defender.faith = scope:recipient.faith
							}
						}
					}
				}
			}
			desc = WONT_ATTACK_RELIGIOUS_BRETHREN_REASON
		}

		modifier = { # Reluctant to join against Friends
			add = -50
			scope:actor = {
				any_character_war = {
					OR = {
						AND = {
							primary_attacker != scope:actor
							primary_attacker = {
								has_relation_friend = scope:recipient
							}
						}
						AND = {
							primary_defender != scope:actor
							primary_defender = {
								has_relation_friend = scope:recipient
							}
						}
					}
				}
			}
		}

		modifier = { # Reluctant to join against Best Friends
			add = -100
			scope:actor = {
				any_character_war = {
					OR = {
						AND = {
							primary_attacker != scope:actor
							primary_attacker = {
								has_relation_best_friend = scope:recipient
							}
						}
						AND = {
							primary_defender != scope:actor
							primary_defender = {
								has_relation_best_friend = scope:recipient
							}
						}
					}
				}
			}
		}
		modifier = { # Reluctant to join against Lovers
			add = -100
			scope:actor = {
				any_character_war = {
					OR = {
						AND = {
							primary_attacker != scope:actor
							primary_attacker = {
								has_relation_lover = scope:recipient
							}
						}
						AND = {
							primary_defender != scope:actor
							primary_defender = {
								has_relation_lover = scope:recipient
							}
						}
					}
				}
			}
		}
		modifier = { # Reluctant to join against Soul mate
			add = -200
			scope:actor = {
				any_character_war = {
					OR = {
						AND = {
							primary_attacker != scope:actor
							primary_attacker = {
								has_relation_soulmate = scope:recipient
							}
						}
						AND = {
							primary_defender != scope:actor
							primary_defender = {
								has_relation_soulmate = scope:recipient
							}
						}
					}
				}
			}
		}
		modifier = {  # Refuse call for hostages' safety
			any_home_court_hostage = {
				warden = {
					OR = {
						is_at_war_with = scope:actor
						any_ally = { is_at_war_with = scope:actor }
					}
				}
			}
			add = {
				value = 0
				every_home_court_hostage = {
					limit = {
						warden = {
							OR = {
								is_at_war_with = scope:actor
								any_ally = { is_at_war_with = scope:actor }
							}
						}
					}
					subtract = call_to_arms_hostage_value
				}
				multiply = 0.25
			}
			desc = HOSTAGE_ENEMY_CALL_TO_ARMS_REASON
		}

		fp3_struggle_resist_allied_wars_modifier = yes
	}

	ai_will_do = {
		base = 50

		#don't do it if hired army wouldn't have an impact
		modifier = {
			scope:actor = {
				any_character_war = {
					count = all
					OR = {
						AND = {
							primary_attacker = scope:actor
							primary_defender.current_military_strength > {
								value = scope:recipient.current_military_strength
								add = scope:actor.current_military_strength
								multiply = 1.5
							}
						} 
						AND = {
							primary_defender = scope:actor
							primary_attacker.current_military_strength > {
								value = scope:recipient.current_military_strength
								add = scope:actor.current_military_strength
								multiply = 1.5
							}
						}
					}
				}
			}
			add = -100
		}

		#don't do it if you are stronger than the enemy
		modifier = {
			scope:actor = {
				any_character_war = {
					count = all
					OR = {
						AND = {
							primary_attacker = scope:actor
							primary_defender.current_military_strength < scope:actor.current_military_strength
						}
						AND = {
							primary_defender = scope:actor
							primary_attacker.current_military_strength < scope:actor.current_military_strength
						}
					}
				}
			}
			add = -200
		}

		#more likely to ask if on contact list
		modifier = {
			scope:actor = {
				is_contact_of = scope:recipient
			}
			add = 100
		}

		#merc laamp wants to join, so why not asking them more?
		modifier = { 
			scope:recipient = {
				has_realm_law = camp_purpose_mercenaries
			}
			add = 50
		}

		#more likely to ask if losing
		modifier = {
			scope:actor = {
				any_character_war = {
					OR = {
						AND = {
							primary_attacker = scope:actor
							defender_war_score > 25
						}
						AND = {
							primary_defender = scope:actor
							attacker_war_score > 25
						}
					}
				}
			}
			add = 20
		}

		#don't do it if they betrayed you already in any of your ongoing wars
		modifier = {
			scope:actor = {
				any_character_war = {
					OR = {
						is_target_in_variable_list = {
							name = already_switched_side
							target = scope:recipient	
						}
						is_target_in_variable_list = {
							name = left_voluntarily
							target = scope:recipient	
						}
					}
				}
			}
			add = -1000
		}

		#don't do it if you cannot afford it
		modifier = {
			scope:actor = {
				war_chest_gold < ep3_hire_laamp_mercs_cost_value
			}
			add = -50
		}
		modifier = {
			scope:actor = {
				gold < ep3_hire_laamp_mercs_cost_value
			}
			add = -1000
		}
	}
}

##################
# Refill MaA
##################

refill_maa_interaction = {
	icon = icon_soldier_survivor
	category = interaction_category_laamp
	interface_priority = 20
	common_interaction = yes
	desc = refill_maa_interaction_desc
	cooldown = { months = 6 }

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = {
			government_allows = conditional_maa_refill
			NOT = { government_has_flag = government_is_nomadic }
		}
		scope:recipient = scope:actor
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			text = reinforce_soldiers_any_regiment_tt
			scope:actor.maa_regiments_count >= 1
		}
		custom_tooltip = {
			text = reinforce_soldiers_unfull_regiment_tt
			scope:actor = {
				any_maa_regiment = { maa_regiments_valid_to_refill_trigger = yes }
			}
		}
		custom_tooltip = {
			text = SUPPLY_LOSS_AT_SEA
			scope:actor = { any_laamp_portion_at_sea_trigger = no }
		}
		custom_tooltip = {
			text = PROCURE_ESTATE_AT_WAR_TT
			scope:actor = {
				NOT = {
					any_army = {
						location.county.holder = {
							OR = {
								is_at_war_with = scope:actor
								any_liege_or_above = { is_at_war_with = scope:actor }
							}
						}
					}
				}
			}
		}
	}

	send_options_exclusive = yes

	send_option = {
		flag = provisions
		localization = reinforce_soldiers_option_provisions
		current_description = reinforce_soldiers_option_provisions_desc
		is_valid = {
			scope:actor.domicile.provisions >= 5
		}
	}

	send_option = {
		flag = gold
		localization = reinforce_soldiers_option_gold
		current_description = reinforce_soldiers_option_gold_desc
		is_valid = {
			scope:actor.gold >= 5
			custom_tooltip = {
				text = reinforce_soldiers_option_gold_tt
				NOT = {
					scope:actor.domicile.domicile_location.county.holder ?= { government_has_flag = government_is_tribal }
				}
			}
		}
	}

	on_accept = {
		scope:actor = {
			switch = {
				trigger = yes
				scope:gold = {
					if = {
						limit = { gold > replenishable_troops_payed_gold_tt_value }
						custom_tooltip = refill_maa_gold_full_tt
					}
					else = { custom_tooltip = refill_maa_gold_tt }
					custom_tooltip = refill_maa_gold_cost_tt
					refill_maa_with_gold_effect = yes
				}
				scope:provisions = {
					if = {
						limit = { domicile.provisions > replenishable_troops_payed_provisions_tt_value }
						custom_tooltip = refill_maa_provisions_full_tt
					}
					else = { custom_tooltip = refill_maa_provisions_tt }
					custom_tooltip = refill_maa_provision_cost_tt
					refill_maa_with_provisions_effect = yes
				}
			}
			if = {
				limit = {
					is_ai = yes
					NOR = {
						scope:gold = yes
						scope:provisions = yes
					}
				}
				refill_maa_with_provisions_effect = yes
			}
		}
	}
	
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 1
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	ai_targets = {
		ai_recipients = self
	}
	ai_potential = {
		government_allows = conditional_maa_refill
		any_maa_regiment = { maa_current_troops_count < maa_max_troops_count }
	}

	ai_will_do = {
		base = 100
		modifier = {
			factor = 0
			scope:gold = yes
			gold <= 500
		}
	}
}

##################
# Evict Adventurer
# by Joe Parkin
##################

evict_adventurer_interaction = {
	common_interaction = yes
	category = interaction_category_diplomacy
	cooldown_against_recipient = { years = 5 }
	greeting = positive
	icon = icon_evict_adventurer
	desc = evict_adventurer_interaction_desc

	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 60
	ai_min_reply_days = 4
	ai_max_reply_days = 9

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = { is_landed = yes }
		scope:recipient != scope:actor
		scope:recipient = {
			OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority }
			is_landed = no
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_at_war = no
			NOR = {
				is_spouse_of = scope:recipient
				has_relation_friend = scope:recipient
				has_relation_lover = scope:recipient
			}
		}
		scope:recipient = {
			# Adventurer must be in the subrealm
			domicile.domicile_location.county.holder ?= {
				OR = {
					this = scope:actor
					any_liege_or_above = { this ?= scope:actor }
				}
			}
		}
		# AI should not against hired adventurers
		trigger_if = {
			limit = {
				scope:actor = { is_ai = yes }
			}
			scope:actor = {
				opinion = {
					target = scope:recipient
					value < 25
				}
			}
			NOT = {
				scope:recipient = {
					any_character_active_contract = { task_contract_employer = scope:actor }
				}
			}
		}
		scope:recipient = {
			NOT = { has_strong_hook = scope:actor }
		}
		# Story blockers.
		## Hereward cannot be exiled by active participants in the Harrying of the North.
		disable_interaction_for_hereward_trigger = yes
		## Hasan cannot be exiled by anyone during his story.
		disable_interaction_for_hasan_trigger = yes
	}

	on_send = {
		scope:recipient = {
			send_interface_toast = {
				type = diplo_contract_scheme_invalidated_message
				title = evict_adventurer_interaction.tt.contracts_invalidated.t
				right_icon = scope:actor
				custom_tooltip = {
					text = evict_adventurer_interaction.tt.contracts_invalidated
					every_character_task_contract = {
						limit = {
							task_contract_location.county.holder = {
								OR = {
									this = scope:actor
									any_liege_or_above = { this = scope:actor }
								}
							}
						}
						invalidate_contract = yes
					}
					# We also need to invalidate your active contract, if you've got one, since you need to be travelling.
					every_character_active_contract = { invalidate_contract = yes }
				}
			}
		}
	}

	on_accept = {
		scope:actor = { save_scope_as = expeller }
		scope:recipient = {
			save_scope_as = adventurer
			add_opinion = {
				target = scope:actor
				modifier = demanded_eviction_opinion
			}
			custom_tooltip = evict_adventurer_contracts_tt
		}
		scope:actor = {
			custom_description_no_bullet = {
				text = evict_adventurer_consequences_tt
				object = scope:recipient
				subject = scope:actor
			}
			custom_tooltip = evict_adventurer_casus_belli_tt
			show_as_tooltip = {
				add_opinion = {
					target = scope:recipient
					modifier = eviction_ignored_opinion
				}
			}
		}
		scope:recipient = {
			set_variable = {
				name = adventurer_expeller
				value = scope:actor
				months = 4
			}
			trigger_event = {
				id = ep3_interactions_events.0500
				days = 1 
			}
			trigger_event = {
				id = ep3_interactions_events.0501
				months = 3
			}
		}
	}

	on_decline = {
		scope:recipient = {
			start_war = {
				cb = ep3_laamp_apprehend_adventurer_cb
				target = scope:actor
			}
		}
	}

	auto_accept = yes

	ai_frequency_by_tier = {
		barony = 0
		county = 12
		duchy = 6
		kingdom = 6
		empire = 6
		hegemony = 6
	}
	ai_targets = {
		ai_recipients = sub_realm_domicile_owners
		max = 10
	}
	ai_potential = {
		is_at_war = no
		is_imprisoned = no
		NOT = { has_trait = incapable }
	}
	ai_will_do = {
		base = -25
		opinion_modifier = {
			who = scope:actor
			opinion_target = scope:recipient
			multiplier = -0.5
		}
		# Independent rulers or kings and over will happily exile claimants.
		modifier = {
			add = 250
			OR = {
				top_liege = this
				highest_held_title_tier >= tier_kingdom
			}
			scope:recipient = {
				any_claim = {
					tier >= scope:actor.highest_held_title_tier
					holder ?= scope:actor
				}
			}
		}
		modifier = {
			add = 10
			has_opinion_modifier = {
				modifier = failed_task_contract_opinion
				target = scope:recipient
			}
		}
		modifier = {
			add = 30
			has_opinion_modifier = {
				modifier = left_war_early
				target = scope:recipient
			}
		}
		modifier = {
			add = 100
			has_opinion_modifier = {
				modifier = switched_sides_in_war
				target = scope:recipient
			}
		}
		modifier = {
			add = {
				if = {
					limit = {
						OR = {
							has_opinion_modifier = {
								modifier = gallowsbait_scum_opinion
								target = scope:recipient
								value >= 100
							}
							has_opinion_modifier = {
								modifier = succeeded_criminal_contract_against_me_opinion
								target = scope:recipient
								value >= 100
							}
						}
					}
					add = 200
				}
				else_if = {
					limit = {
						OR = {
							has_opinion_modifier = {
								modifier = gallowsbait_scum_opinion
								target = scope:recipient
								value >= 80
							}
							has_opinion_modifier = {
								modifier = succeeded_criminal_contract_against_me_opinion
								target = scope:recipient
								value >= 80
							}
						}
					}
					add = 150
				}
				else_if = {
					limit = {
						OR = {
							has_opinion_modifier = {
								modifier = gallowsbait_scum_opinion
								target = scope:recipient
								value >= 60
							}
							has_opinion_modifier = {
								modifier = succeeded_criminal_contract_against_me_opinion
								target = scope:recipient
								value >= 60
							}
						}
					}
					add = 100
				}
				else_if = {
					limit = {
						OR = {
							has_opinion_modifier = {
								modifier = gallowsbait_scum_opinion
								target = scope:recipient
								value >= 40
							}
							has_opinion_modifier = {
								modifier = succeeded_criminal_contract_against_me_opinion
								target = scope:recipient
								value >= 40
							}
						}
					}
					add = 75
				}
				else_if = {
					limit = {
						OR = {
							has_opinion_modifier = {
								modifier = gallowsbait_scum_opinion
								target = scope:recipient
								value >= 20
							}
							has_opinion_modifier = {
								modifier = succeeded_criminal_contract_against_me_opinion
								target = scope:recipient
								value >= 20
							}
						}
					}
					add = 50
				}
				else_if = {
					limit = {
						OR = {
							has_opinion_modifier = {
								modifier = gallowsbait_scum_opinion
								target = scope:recipient
								value >= 0
							}
							has_opinion_modifier = {
								modifier = succeeded_criminal_contract_against_me_opinion
								target = scope:recipient
								value >= 0
							}
						}
					}
					add = 25
				}
			}
			OR = {
				has_opinion_modifier = {
					modifier = gallowsbait_scum_opinion
					target = scope:recipient
				}
				has_opinion_modifier = {
					modifier = succeeded_criminal_contract_against_me_opinion
					target = scope:recipient
				}
			}
		}
		# Factors must got at the bottom.
		## Don't evict people we can't beat up.
		modifier = {
			factor = 0
			scope:actor.current_military_strength < scope:recipient.military_strength_75_percent_value
		}
		## Don't evict anyone currently doing a job for us.
		modifier = {
			factor = 0
			scope:recipient = {
				any_character_task_contract = { task_contract_employer = scope:actor }
			}
		}
		## We _only_ evict if we actually dislike someone.
		first_valid = {
			# People close to us are more willing to put up with our bullshit.
			modifier = {
				factor = 0
				has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:recipient }
				opinion = {
					target = scope:recipient
					value > -30
				}
			}
			modifier = {
				factor = 0
				opinion = {
					target = scope:recipient
					value > 0
				}
			}
		}
	}
}

#Become landless admin from laamp
procure_estate_interaction = {
	category = interaction_category_laamp
	icon = powerful_family_shield

	desc = procure_estate_interaction_desc

	cooldown_against_recipient = { years = 10 }

	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:hook = yes
				}
				desc = PROCURE_ESTATE_HOOK
			}
			triggered_desc = {
				trigger = {
					scope:gold_option = yes
				}
				desc = PROCURE_ESTATE_GOLD
			}
			triggered_desc = {
				trigger = {
					scope:piety_option = yes
				}
				desc = PROCURE_ESTATE_PIETY
			}
			desc = PROCURE_ESTATE_PRESTIGE
		}
		desc = PROCURE_ESTATE_PROPOSAL_OUTRO
	}

	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 100
	ai_min_reply_days = 4
	ai_max_reply_days = 9

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = { OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority } }
		scope:recipient = {
			is_landed = yes
			government_allows = noble_families
		}
		scope:recipient != scope:actor
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			top_liege = this
			prestige_level >= 2
			# Only house heads can have Noble Family titles
			custom_tooltip = {
				text = NOT_IS_HOUSE_HEAD
				house.house_head ?= {
					OR = {
						this = scope:actor
						is_ruler = no
					}
				}
			}
		}
		trigger_if = { # Only a Duke or higher can treat you right
			limit = {
				scope:recipient = {
					highest_held_title_tier < tier_duchy
				}
			}
			custom_description = {
				text = procure_estate_low_rank_tt
				always = no
			}
		}
		trigger_if = { # Not going to give you a title if you're at war
			limit = {
				scope:actor = {
					is_at_war_with = scope:recipient
				}
			}
			custom_description = {
				text = procure_estate_at_war_tt
				always = no
			}
		}
		scope:recipient = {
			NOT = { has_strong_hook = scope:actor }
		}
		#Has to be used instead of diplo range checks in laamp to landed interactions
		ep3_laamp_diplo_range_trigger = {
			TARGET = scope:recipient
			LAAMP = scope:actor
		}
		scope:recipient = { is_contact_of = scope:actor }

		# Adventurers can't join wars in places they're exiled from.
		custom_tooltip = {
			text = the_wake_1
			NOT = {
				is_laamp_exiled_from_province_trigger = {
					PROVINCE = scope:recipient.capital_province
					LAAMP = scope:actor
				}
			}
		}
	}

	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:hook = yes
		}
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = SCHEME_HOOK
	}
	send_option = {
		flag = prestige_option
		localization = laamp_to_ladmin_prestige_option_desc
	}
	send_option = {
		flag = gold_option
		localization = laamp_to_ladmin_gold_option_desc
	}
	send_option = {
		flag = piety_option
		localization = laamp_to_ladmin_piety_option_desc
	}

	cost = {
		gold = {
			add = {
				value = procure_estate_interaction_cost_base_gold
				desc = BASE
			}
			if = {
				limit = { scope:gold_option = yes }
				add = {
					value = procure_estate_interaction_cost_base_gold
					desc = laamp_to_ladmin_gold_option_desc
				}
			}
		}
		prestige = {
			if = {
				limit = {
					NOR = {
						scope:gold_option = yes
						scope:piety_option = yes
						scope:hook = yes
					}
				}
				# Base cost
				add = {
					value = procure_estate_interaction_cost_base_prestige
					desc = BASE
				}
				# Gender modifiers
				if = {
					limit = {
						scope:recipient ?= {
							top_liege = { has_realm_law = male_preference_law }
						}
						scope:actor = {
							is_female = yes
						}
					}
					add = { # Increase the base cost
						value = {
							value = procure_estate_interaction_cost_base_prestige
							multiply = 0.25
						}
						desc = male_preference_law
					}
				}
				if = {
					limit = {
						scope:recipient ?= {
							top_liege = { has_realm_law = female_preference_law }
						}
						scope:actor = {
							is_male = yes
						}
					}
					add = { # Increase the base cost
						value = {
							value = procure_estate_interaction_cost_base_prestige
							multiply = 0.25
						}
						desc = female_preference_law
					}
				}
				#Brigands need to pay more to be legitimized
				if = {
					limit = {
						scope:actor = {
							has_realm_law = camp_purpose_brigands
						}
					}
					add = { # Increase the base cost
						value = {
							value = major_prestige_value
						}
						desc = camp_purpose_brigands
					}
				}
			}
		}
		piety = {
			if = {
				limit = {
					scope:piety_option = yes
				}

				# Base cost
				add = {
					value = procure_estate_interaction_cost_base_piety
					desc = BASE
				}

				# Gender modifiers
				if = {
					limit = {
						scope:recipient ?= {
							top_liege = { has_realm_law = male_preference_law }
						}
						scope:actor = {
							is_female = yes
						}
					}
					add = { # Increase the base cost
						value = {
							value = procure_estate_interaction_cost_base_piety
							multiply = 0.25
						}
						desc = male_preference_law
					}
				}
				if = {
					limit = {
						scope:recipient ?= {
							top_liege = { has_realm_law = female_preference_law }
						}
						scope:actor = {
							is_male = yes
						}
					}
					add = { # Increase the base cost
						value = {
							value = procure_estate_interaction_cost_base_piety
							multiply = 0.25
						}
						desc = female_preference_law
					}
				}
				#Brigands need to pay more to be legitimized
				if = {
					limit = {
						scope:actor = {
							has_realm_law = camp_purpose_brigands
						}
					}
					add = { # Increase the base cost
						value = {
							value = procure_estate_interaction_cost_base_piety
						}
						desc = camp_purpose_brigands
					}
				}
			}
		}
	}

	is_highlighted = {
		always = yes
	}

	on_accept = {
		scope:actor = {
			save_scope_as = adventurer
			procure_estate_interaction_effect = yes
			create_character_memory = {
				type = procured_estate_memory
				participants = { benefactor = scope:recipient }
			}
			if = {
				limit = {
					is_ai = no
				}
				add_achievement_global_variable_effect = {
					VARIABLE = started_from_the_bottom_now_were_ere_procure
					VALUE = yes
				}
			}
		}
		scope:actor = { trigger_event = ep3_interactions_events.0610 }
	}

	on_decline = {
		scope:actor = {
			save_scope_as = adventurer
			trigger_event = ep3_interactions_events.0611
		}
	}

	ai_targets = {
		ai_recipients = contacts
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 60
		kingdom = 0
		empire = 0
		hegemony = 0
	}

	ai_accept = {
		base = 0 # Try to make it 0 for most interactions

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.5
			desc = AI_OPINION_REASON
		}
		#DIFFERENT FAITH FROM STATE FAITH (if recipient is state faith)
		modifier = {
			add = -20
			desc = PROCURE_ESTATE_STATE_FAITH_REASON
			exists = scope:recipient.top_liege.primary_title.state_faith
			scope:actor.faith = {
				this != scope:recipient.top_liege.primary_title.state_faith
			}
			scope:recipient.faith = {
				this = scope:recipient.top_liege.primary_title.state_faith
			}
			NOT = {
				scope:piety_option = yes
			}
		}
		#Hey you're actually a local in my capital
		modifier = {
			add = 10
			desc = PROCURE_ESTATE_SAME_CULTURE_AS_CAPITAL_REASON
			scope:recipient.capital_county.culture = {
				this = scope:actor.culture
			}
		}
		#Your dynasty is worthless
		modifier = {
			add = -10
			scope:actor.dynasty = {
				dynasty_prestige_level < 2
			}
			desc = PROCURE_ESTATE_DYNASTY_PRESTIGE_LEVEL_REASON
		}
		#Your dynasty is pretty great actually
		modifier = {
			add = 10
			scope:actor.dynasty = {
				dynasty_prestige_level >= 4
				dynasty_prestige_level < 7
			}
			desc = PROCURE_ESTATE_DYNASTY_PRESTIGE_LEVEL_REASON
		}
		#Your dynasty is pretty great actually
		modifier = {
			add = 20
			scope:actor.dynasty = {
				dynasty_prestige_level >= 7
				dynasty_prestige_level < 10
			}
			desc = PROCURE_ESTATE_DYNASTY_PRESTIGE_LEVEL_REASON
		}
		#Your dynasty is pretty great actually
		modifier = {
			add = 30
			scope:actor.dynasty = { dynasty_prestige_level >= 10 }
			desc = PROCURE_ESTATE_DYNASTY_PRESTIGE_LEVEL_REASON
		}
		#We are not related
		modifier = {
			add = -15
			desc = PROCURE_ESTATE_SHARED_RELATIVES_REASON
			NOR = {
				scope:actor.house ?= {
					any_house_member = {
						is_close_or_extended_family_of = scope:recipient
					}
				}
				scope:actor = {
					any_close_or_extended_family_member = {
						is_close_or_extended_family_of = scope:recipient
					}
				}
				scope:recipient.house ?= {
					any_house_member = {
						is_close_or_extended_family_of = scope:actor
					}
				}
				scope:recipient = {
					any_spouse = {
						is_close_or_extended_family_of = scope:actor
					}
				}
				scope:actor = {
					any_spouse = {
						is_close_or_extended_family_of = scope:recipient
					}
				}
				scope:actor.dynasty = {
					this = scope:recipient.dynasty
				}
			}
		}
		#You are criminal scum
		modifier = {
			add = -50
			desc = PROCURE_ESTATE_BRIGANDS_REASON
			scope:actor = {
				has_realm_law = camp_purpose_brigands
			}
		}
		#You are an upright legitimist
		modifier = {
			add = 25
			desc = PROCURE_ESTATE_LEGITIMISTS_REASON
			scope:actor = {
				has_realm_law = camp_purpose_legitimists
			}
		}
		#You have a criminal background
		modifier = {
			add = -10
			desc = PROCURE_ESTATE_GALLOWSBAIT_REASON
			scope:actor = {
				has_trait = gallowsbait
			}
		}
		#You have a criminal background
		modifier = {
			add = -5
			desc = PROCURE_ESTATE_GALLOWSBAIT_REASON
			scope:actor = {
				has_trait = gallowsbait
				NOR = {
					has_trait_xp = {
						trait = gallowsbait
						track = bandit
						value < 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = trickster
						value < 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = thief
						value < 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = poacher
						value < 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = marauder
						value < 40
					}
				}
			}
		}
		modifier = {
			add = -20
			desc = PROCURE_ESTATE_GALLOWSBAIT_REASON
			scope:actor = {
				has_trait = gallowsbait
				OR = {
					has_trait_xp = {
						trait = gallowsbait
						track = bandit
						value >= 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = trickster
						value >= 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = thief
						value >= 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = poacher
						value >= 40
					}
					has_trait_xp = {
						trait = gallowsbait
						track = marauder
						value >= 40
					}
				}
				NOR = {
					has_trait_xp = {
						trait = gallowsbait
						track = bandit
						value < 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = trickster
						value < 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = thief
						value < 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = poacher
						value < 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = marauder
						value < 80
					}
				}
			}
		}
		modifier = {
			add = -50
			desc = PROCURE_ESTATE_GALLOWSBAIT_REASON
			scope:actor = {
				has_trait = gallowsbait
				OR = {
					has_trait_xp = {
						trait = gallowsbait
						track = bandit
						value >= 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = trickster
						value >= 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = thief
						value >= 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = poacher
						value >= 80
					}
					has_trait_xp = {
						trait = gallowsbait
						track = marauder
						value >= 80
					}
				}
			}
		}
		#You have an interesting claim within my realm
		modifier = {
			add = 5
			desc = PROCURE_ESTATE_CLAIMS_REASON
			scope:actor = {
				any_claim = {
					save_temporary_scope_as = laamp_claim_temp
					scope:recipient.top_liege.primary_title = {
						is_de_jure_liege_or_above_target = scope:laamp_claim_temp
					}
					NOT = {
						holder ?= scope:recipient
					}
				}
			}
		}
		#You are a challenger for the empire and I want you indebted to me
		modifier = {
			add = 20
			desc = PROCURE_ESTATE_IMPERIAL_CLAIMS_REASON
			scope:actor = {
				any_claim = {
					save_temporary_scope_as = laamp_claim_temp
					this = scope:recipient.top_liege.primary_title
					NOT = {
						holder ?= scope:recipient
					}
					scope:recipient = {
						NOR = {
							has_any_good_relationship_with_character_trigger = { CHARACTER = scope:recipient.top_liege }
							opinion = {
								target = scope:recipient.top_liege
								value >= -20
							}
							is_allied_to = scope:recipient.top_liege
						}
					}
				}
			}
		}
		#You have a scary claim on one of my titles!
		modifier = {
			add = -50
			desc = PROCURE_ESTATE_RIVAL_CLAIMANT_REASON
			scope:actor = {
				any_claim = {
					holder ?= scope:recipient
				}
			}
		}
		#Needs gold
		modifier = {
			add = 10
			scope:recipient = {
				gold <= minor_gold_value
			}
			NOT = {
				scope:gold_option = yes
			}
			desc = PROCURE_ESTATE_NEEDS_GOLD_REASON
		}
		#Needs gold and offering gold
		modifier = {
			add = 20
			scope:recipient = {
				gold <= minor_gold_value
			}
			scope:gold_option = yes
			desc = PROCURE_ESTATE_NEEDS_GOLD_REASON
		}
		#Needs piety
		modifier = {
			add = 20
			scope:recipient = {
				piety_level <= 0
			}
			scope:piety_option = yes
			desc = PROCURE_ESTATE_NEEDS_PIETY_REASON
		}

		#Needs prestige
		modifier = {
			add = 20
			scope:recipient = {
				prestige_level <= 0
			}
			scope:prestige_option = yes
			desc = PROCURE_ESTATE_NEEDS_PRESTIGE_REASON
		}
	}

	ai_will_do = {
		base = -25
		ai_value_modifier = {
			ai_boldness = -0.5
			ai_energy = -0.5
			ai_greed = 0.5
			ai_rationality = 0.5
		}
		modifier = {
			gold < 100
			factor = 0
		}
		modifier = {
			gold > 100
			OR = {
				prestige >= massive_prestige_value
				piety >= massive_piety_value
				gold >= massive_gold_value
			}
			add = 50
		}
		modifier = {
			has_hook = scope:recipient
			add = 200
		}
	}
}

request_laamp_legitimist_support = {
	interface_priority = 50
	common_interaction = yes
	category = interaction_category_laamp
	cooldown_against_recipient = { years = 5 }

	greeting = positive

	icon = request_invasion_sponsorship_interaction
	desc = request_laamp_legitimist_support_interaction_desc

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = {
			OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority }
			has_realm_law = camp_purpose_legitimists
		}
		scope:recipient = { 
			is_landed = yes
			is_ai = yes
			primary_title.tier >= tier_kingdom
		}
		scope:recipient != scope:actor
	}
	
	is_valid_showing_failures_only = {
		NOR = { #so legitimist AI can get lucky. It looks ugly but formats the tooltip correctly
			NOT = {
				ep3_laamp_diplo_range_trigger = {
					TARGET = scope:recipient
					LAAMP = scope:actor
				}
			}
			scope:actor = {
				is_available_ai_adult = yes
			}			
		}
			
		
		custom_tooltip = {
			text = request_laamp_legitimist_have_claim_on_target
			scope:actor = {
				NOT = {
					any_claim = {
						OR = {
							holder = {
								this = scope:recipient
							}
							holder.top_liege = {
								this = scope:recipient
							}
						}
					}
				}
			}
		}

		custom_tooltip = {
			text = request_laamp_legitimist_support_already_supporting
			scope:recipient = {
				NOT = {
					has_character_flag = legitimist_supporter
				}
			}
		}
		scope:recipient = {
			is_at_war = no
			is_available_ai_adult = yes
		}

		# Adventurers can't join wars in places they're exiled from.
		custom_tooltip = {
			text = the_wake_1
			NOT = {
				is_laamp_exiled_from_province_trigger = {
					PROVINCE = scope:recipient.capital_province
					LAAMP = scope:actor
				}
			}
		}
	}

	send_options_exclusive = no

	cost = {
		prestige = {
			value = massive_prestige_value
			if = {
				limit = {
					scope:actor.dynasty = scope:recipient.dynasty
				}
				divide = 2
			}
			if = {
				limit = { #so legitimist AI can get lucky
					scope:actor = { is_ai = yes }
				}
				divide = 6
			}
		}
	}

	on_accept = {
		scope:actor = {
			create_task_contract = {
				task_contract_type = laamp_legitimist_support_contract
				task_contract_tier = 3
				location = scope:recipient.capital_province
				task_contract_employer = scope:recipient
				save_temporary_scope_as = legitimist_contract
			}
			custom_tooltip = laamp_legitimist_support_contract_success
			if = {
				limit = {
					is_ai = yes
				}
				accept_task_contract = scope:legitimist_contract
			}
		}
	}

	on_decline = {

	}

	use_diplomatic_range = yes

	ai_accept = {
		base = -60
		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1
			desc = AI_OPINION_REASON
		}
		modifier = {
			add = 40
			scope:actor = {
				any_claim = {
					tier >= tier_kingdom
					holder = {
						save_temporary_scope_as = claim_holder
					}
				}
				prestige_level > scope:claim_holder.prestige_level
			}
			desc = AI_REASON_LEGITIMIST_HIGHER_PRESTIGE
		}
		modifier = {
			add = 60
			scope:actor = {
				has_hook = scope:recipient
			}
			desc = YOU_HAVE_HOOK_REASON
		}
		modifier = {
			add = 10
			scope:actor.dynasty = scope:recipient.dynasty
			desc = BF_AI_SAME_DYNASTY_REASON
		}
		modifier = {
			add = 20
			scope:actor.house = scope:recipient.house
			desc = IS_SAME_HOUSE_REASON
		}
		modifier = {
			add = 20
			scope:recipient = { has_trait = ambitious }
			desc = RECIPIENT_IS_AMBITIOUS
		}
		modifier = {
			add = 10
			scope:recipient = { has_trait = just }
			desc = RECIPIENT_IS_JUST
		}
		modifier = {
			add = 10
			scope:recipient = { has_trait = greedy }
			desc = RECIPIENT_IS_GREEDY
		}
		modifier = {
			add = 40 #so legitimist AI can get lucky
			scope:actor = { is_ai = yes }
		}
	}

	can_send_despite_rejection = no
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 60
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	ai_targets = {
		ai_recipients = domicile_location_neighboring_top_rulers
		ai_recipients = contacts
	}
	ai_potential = {
		is_at_war = no
		ai_greed <= 25
	}
	ai_will_do = {
		base = 100 #no downside to ai trying this
	}
}

#travel around the map to look for new contracts
relocate_camp_to_interaction = {
	category = interaction_category_laamp
	interface_priority = 100
	common_interaction = yes
	use_diplomatic_range = no
	ignores_pending_interaction_block = yes
	icon = icon_contract_escort
	desc = relocate_camp_to_interaction_desc
	is_shown = {
		scope:recipient != scope:actor
		scope:actor = {
			has_government = landless_adventurer_government
		}
		scope:recipient = { exists = capital_province } #just to make sure proper scopes will be set up
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			text = relocate_camp_to_interaction.here
			scope:actor.domicile.domicile_location != scope:recipient.capital_province
		}
		scope:actor = {
			is_travelling = no
			is_in_army = no
			is_imprisoned = no
		}
		# Adventurers can't join wars in places they're exiled from.
		custom_tooltip = {
			text = the_wake_1
			NOT = {
				is_laamp_exiled_from_province_trigger = {
					PROVINCE = scope:recipient.capital_province
					LAAMP = scope:actor
				}
			}
		}
	}

	on_accept = {
		scope:recipient.capital_province = {
			save_scope_as = relocation_destination
		}
		scope:actor = {
			show_as_tooltip = {
				domicile = {
					move_domicile = scope:relocation_destination
				}
			}
			start_travel_plan = {
				destination = scope:relocation_destination
				on_start_on_action = on_travel_relocation_start
				on_arrival_on_action = on_travel_relocation_end
				on_arrival_destinations = last
				return_trip = no # One way
				travel_with_domicile = yes
			}
		}
	}

	auto_accept = yes
}

#
task_contract_train_knight_interaction = {
	category = interaction_category_laamp
	interface_priority = 120
	common_interaction = yes
	icon = icon_combat

	popup_on_receive = yes
	pause_on_receive = yes

	is_shown = {
		has_ep3_dlc_trigger = yes
		scope:actor = {
			any_character_active_contract = {
				task_contract_type = laamp_help_train_commanders_contract
				OR = {
					task_contract_employer ?= scope:recipient
					task_contract_target ?= scope:recipient
					any_in_list = {
						variable = knights_to_train
						is_alive = yes
						is_knight = yes
						this = scope:recipient
					}
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
	}

	redirect = {
		scope:actor = {
			random_character_active_contract = {
				task_contract_type = laamp_help_train_commanders_contract
				limit = {
					OR = {
						task_contract_employer ?= scope:recipient
						task_contract_target ?= scope:recipient
						any_in_list = {
							variable = knights_to_train
							is_alive = yes
							is_knight = yes
							this = scope:recipient
						}
					}
				}
				save_scope_as = task_contract
			}
		}
		if = {
			limit = {
				scope:task_contract ?= {
					any_in_list = {
						variable = knights_to_train
						is_alive = yes
						is_knight = yes
						this = scope:recipient						
					}
				}
			}
			scope:recipient = {
				save_scope_as = secondary_recipient
			}
			scope:secondary_recipient.liege = {
				save_scope_as = recipient
			}
		}
		if = {
			limit = {
				scope:task_contract.task_contract_employer ?= scope:recipient
				scope:task_contract.task_contract_employer.liege ?= scope:task_contract.task_contract_target
			}

		}
	}

	populate_recipient_list = {
		scope:actor = {
			random_character_active_contract = {
				task_contract_type = laamp_help_train_commanders_contract
				limit = {
					OR = {
						task_contract_employer ?= scope:recipient
						task_contract_target ?= scope:recipient
						any_in_list = {
							variable = knights_to_train
							is_alive = yes
							is_knight = yes
							this = scope:recipient
						}
					}
				}
				every_in_list = {
					variable = knights_to_train
					limit = {
						is_alive = yes
						is_knight = yes
					}
					add_to_list = characters
				}
			}
		}
	}

	is_highlighted = {
		always = yes
	}

	on_accept = {
		scope:actor = {
			random_character_active_contract = {
				task_contract_type = laamp_help_train_commanders_contract
				limit = {
					OR = {
						task_contract_employer ?= scope:recipient
						task_contract_target ?= scope:recipient
						any_in_list = {
							variable = knights_to_train
							is_alive = yes
							is_knight = yes
							this = scope:recipient
						}
					}
				}
				save_scope_as = task_contract
			}
		}
		scope:secondary_recipient = {
			save_scope_as = knight
			custom_tooltip = task_contract_train_knight_interaction.tt
		}
		scope:actor = {
			trigger_event = ep3_contract_event.0091
		}
	}

	auto_accept = yes

	ai_accept = {
		base = 100
	}

	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 12
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = liege
		ai_recipients = contacts
	}
	ai_potential = {
		is_at_war = no
	}
	ai_will_do = {
		base = 100 #no downside to ai trying this
	}
}

#landless adventurer are allowed to change allegiances mid-war
laamp_war_change_sides_interaction = {
	category = interaction_category_laamp
	icon = alliance
	desc = laamp_war_change_sides_interaction_desc
	common_interaction = yes
	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 120
	ai_min_reply_days = 0
	ai_max_reply_days = 0

	cooldown_against_recipient = { years = 3 }
	send_options_exclusive = yes

	is_shown = {
		scope:actor = {
			is_at_war = yes
			has_government = landless_adventurer_government
		}
		scope:recipient = {
			this != scope:actor
			any_war_ally = { this = scope:actor }
			any_character_war = {
				#can't switch/leave wars you lead
				NOT = { is_war_leader = scope:actor }
				#can't switch/leave Great Holy Wars
				NOR = {
					using_cb = undirected_great_holy_war
					using_cb = directed_great_holy_war
				}
				any_war_participant = {
					this = scope:recipient
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			trigger_if = {
				limit = {
					any_character_war = {
						has_variable_list = already_switched_side
					}
				}
				custom_tooltip = {
					text = laamp_war_change_sides_interaction.already_switched_side
					is_target_in_variable_list = {
						name = already_switched_side
						target = scope:actor	
					}
				}
			}
		}
	}

	populate_recipient_list = {
		scope:recipient = {
			every_character_war = {
				limit = {
					OR = {
						primary_attacker = scope:recipient
						primary_defender = scope:recipient
					}
					#can't switch/leave wars you lead
					NOT = { is_war_leader = scope:actor }
					#can't switch/leave Great Holy Wars
					NOR = {
						using_cb = undirected_great_holy_war
						using_cb = directed_great_holy_war
					}
					any_war_participant = {
						this = scope:recipient
					}
				}
				if = {
					limit = { primary_attacker = scope:recipient }
					primary_defender = { add_to_list = characters }
				}
				else = {
					primary_attacker = { add_to_list = characters }
				}
			}
		}
	}

	on_accept = {
		scope:recipient = {
			save_scope_as = betrayed_leader
		}
		scope:secondary_recipient = {
			save_scope_as = new_leader
		}
		scope:actor = {
			#ROOT is  a 4th crusader and should not be one anymore
			frankokratia_remove__war_leader_var_effect = yes
			#ROOT is not a 4th crusader and should be one
			if = {
				limit = {
					any_character_war = {
						using_cb = crusading_claim_cb
						any_war_participant = {
							this = scope:recipient
						}
						any_war_participant = {
							any_owned_story = {
								type = frankokratia_story
								NOT = {
									any_in_list = {
										variable = frankokratia_leaders
										this = scope:actor
									}
								}
							}
						}
						any_war_defender = {
							this = scope:actor
						}
					}
				}
				random_character_war = {
					limit = {
						any_war_participant = {
							any_owned_story = {
								type = frankokratia_story
								NOT = {
									any_in_list = {
										variable = frankokratia_leaders
										this = scope:actor
									}
								}
							}
						}
						any_war_defender = {
							this = scope:actor
						}
					}
					random_war_participant = {
						limit = {
							any_owned_story = {
								type = frankokratia_story
								NOT = {
									any_in_list = {
										variable = frankokratia_leaders
										this = scope:actor
									}
								}
							}
						}
						random_owned_story = {
							type = frankokratia_story
							limit = {
								NOT = {
									any_in_list = {
										variable = frankokratia_leaders
										this = scope:actor
									}
								}
							}
							add_to_variable_list = {
								name = frankokratia_leaders
								target = scope:actor
							}
						}
					}
				}
			}
			random_character_war = {
				limit = {
					OR = {
						primary_attacker = scope:recipient
						primary_defender = scope:recipient
					}
					OR = {
						primary_attacker = scope:secondary_recipient
						primary_defender = scope:secondary_recipient
					}
				}
				save_scope_as = war
			}
			if = {
				limit = {
					scope:war = {
						using_non_ghw_holy_war_cb_trigger = yes
					}
					scope:recipient.faith = scope:actor.faith
				}
				add_piety_level = -1
			}
			every_character_active_contract = {
				limit = {
					var:task_contract_war = scope:war
				}
				complete_task_contract = failure_standard
			}
			# Clear Offer Assistance.
			if = {
				limit = {
					has_variable = owed_contract_assistance_war
					scope:recipient = { is_participant_in_war = scope:actor.var:owed_contract_assistance_war }
				}
				remove_variable = owed_contract_assistance_war
				remove_variable = owed_contract_assistance_contribution
				remove_variable = owed_contract_assistance_gold
			}
		}
		scope:war = {
			hidden_effect = {
				remove_participant = scope:actor
			}
			if = {
				limit = {
					is_defender = scope:secondary_recipient
				}
				add_defender = scope:actor
			}
			else = {
				add_attacker = scope:actor
			}
		}
		scope:actor = {
			every_character_war = {
				limit = {
					OR = {
						primary_attacker = scope:recipient
						primary_defender = scope:recipient
					}
					this != scope:war
				}
				remove_participant = scope:actor
			}
		}
		scope:actor = {
			# We deliberately charge you here to let you go into prestige debt; you shouldn't be blocked from switching sides due to a lack of prestige if you're willing to tank the loss.
			add_prestige = {
				value = -250
				multiply = {
					value = scope:recipient.highest_held_title_tier
					add = -1
				}
			}
			if = {
				limit = { scope:recipient.highest_held_title_tier >= tier_kingdom }
				add_prestige_level = -1
			}
			if = {
				limit = {
					NOT = { has_contact = scope:secondary_recipient }
				}
				add_contact = scope:secondary_recipient
			}
			# Break the alliance
			if = {
				limit = { is_allied_to = scope:recipient }
				break_alliance = scope:recipient
			}
			# And don't forget stress!
			stress_impact = {
				base = major_stress_impact_gain
				disloyal = medium_stress_impact_loss
				ambitious = minor_stress_impact_loss
				arrogant = minor_stress_impact_loss
				callous = miniscule_stress_impact_loss
				arbitrary = miniscule_stress_impact_loss
				just = minor_stress_impact_gain
				humble = medium_stress_impact_gain
				content = medium_stress_impact_gain
				loyal = major_stress_impact_gain
			}
		}
		scope:recipient = {
			if = {
				limit = {
					can_set_relation_potential_rival_trigger = { CHARACTER = scope:actor }
				}
				set_relation_potential_rival = scope:actor
			}
			add_opinion = {
				modifier = switched_sides_in_war
				target = scope:actor
			}
		}
		scope:secondary_recipient = {
			add_opinion = {
				modifier = switched_to_my_side
				target = scope:actor
			}
			if = {
				limit = { is_ai = yes }
				pay_short_term_gold = {
					target = scope:actor
					gold = major_gold_value
				}
			}
		}
		scope:recipient = {
			every_character_war = {
				limit = {
					OR = {
						primary_attacker = scope:recipient
						primary_defender = scope:recipient
					}
					#can't switch/leave wars you lead
					NOT = { is_war_leader = scope:actor }
					#can't switch/leave Great Holy Wars
					NOR = {
						using_cb = undirected_great_holy_war
						using_cb = directed_great_holy_war
					}
					any_war_participant = {
						this = scope:recipient
					}
					NOR = {
						primary_attacker = scope:secondary_recipient
						primary_defender = scope:secondary_recipient
					}
				}
				if = {
					limit = { primary_attacker = scope:recipient }
					primary_defender = {
						add_opinion = {
							modifier = left_war_early
							opinion = 40
							target = scope:actor
						}
					}
				}
				else = {
					primary_attacker = {
						add_opinion = {
							modifier = left_war_early
							opinion = 20
							target = scope:actor
						}
					}
				}
			}
		}
		scope:war = {
			add_to_variable_list = {
				name = already_switched_side
				target = scope:actor
			}
		}
	}

	auto_accept = yes

	ai_potential = {
		is_at_war = yes
	}

	ai_targets = {
		ai_recipients = war_allies
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 24
		kingdom = 0
		empire = 0
		hegemony = 0
	}

	ai_will_do = {
		base = -25
		#you have to be dishonorable and greedy to do this
		ai_value_modifier = {
			ai_honor = -0.1
			ai_greed = 0.1
		}
		##check if the recipient is the war leader and do it
		#if they are infamous
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			scope:recipient = {
				prestige_level < 0
			}
			add = 5
		}
		#if they are impious
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			scope:recipient = {
				piety_level < 0
			}
			add = 5
		}
		#if they are a criminal
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			has_any_criminal_trait_in_faith_trigger = {
				CHARACTER = scope:recipient
				FAITH = scope:actor.faith
			}
			add = 5
		}
		#if they wronged you or your family
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			scope:actor = {
				has_any_major_revenge_opinion_against_character_trigger = {
					CHARACTER = scope:recipient
				}
			}
			add = 5
		}
		#if they have good relation with the other side
		modifier = {
			any_character_war = {
				OR = {
					AND = {
						primary_defender = scope:recipient
						primary_attacker = {
							has_any_good_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
					AND = {
						primary_attacker = scope:recipient
						primary_defender = {
							has_any_good_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
				}
			}
			add = 5
		}
		#don't do it
		# if the other side has bad relation with you
		modifier = {
			any_character_war = {
				OR = {
					AND = {
						primary_defender = scope:recipient
						primary_attacker = {
							has_any_bad_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
					AND = {
						primary_attacker = scope:recipient
						primary_defender = {
							has_any_bad_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
				}
			}
			add = -1000
		}
		# you are winning
		modifier = {
			any_character_war = {
				OR = {
					AND = {
						primary_defender = scope:recipient
						defender_war_score >= 0
					}
					AND = {
						primary_attacker = scope:recipient
						attacker_war_score >= 0
					} 
				}
			}
			add = -1000
		}
		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = -0.1
			desc = AI_OPINION_REASON
		}
	}
}

#landless adventurer are allowed to leave the war outright
laamp_leave_war_interaction = {
	category = interaction_category_laamp
	icon = alliance
	desc = laamp_leave_war_interaction_desc
	common_interaction = yes
	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 120
	ai_min_reply_days = 0
	ai_max_reply_days = 0

	cooldown_against_recipient = { years = 3 }
	send_options_exclusive = yes

	is_shown = {
		scope:actor = {
			is_at_war = yes
			has_government = landless_adventurer_government
		}
		scope:recipient = {
			this != scope:actor
			any_war_ally = { this = scope:actor }
			any_character_war = {
				is_participant = scope:recipient
				#can't leave wars you lead
				NOT = { is_war_leader = scope:actor }
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			any_character_war = {
				is_participant = scope:recipient
				#can't leave wars you lead
				NOT = { is_war_leader = scope:actor }
				#can't leave Great Holy Wars
				NOR = {
					using_cb = undirected_great_holy_war
					using_cb = directed_great_holy_war
				}
			}
		}
		scope:recipient = {
			trigger_if = {
				limit = {
					any_character_war = {
						has_variable_list = already_switched_side
					}
				}
				custom_tooltip = {
					text = laamp_war_change_sides_interaction.already_switched_side
					is_target_in_variable_list = {
						name = already_switched_side
						target = scope:actor	
					}
				}
			}
		}
	}

	on_accept = {
		scope:recipient = {
			save_scope_as = betrayed_leader
		}
		scope:secondary_recipient = {
			save_scope_as = new_leader
		}
		scope:actor = {
			#ROOT is  a 4th crusader and should not be one anymore
			frankokratia_remove__war_leader_var_effect = yes
			every_character_war = {
				limit = {
					OR = {
						primary_attacker = scope:recipient
						primary_defender = scope:recipient
					}
					#can't switch/leave wars you lead
					NOT = { is_war_leader = scope:actor }
					#can't switch/leave Great Holy Wars
					NOR = {
						using_cb = undirected_great_holy_war
						using_cb = directed_great_holy_war
					}
					any_war_participant = {
						this = scope:recipient
					}
				}
				remove_participant = scope:actor
				if = {
					limit = { primary_defender = scope:recipient }
					primary_attacker = {
						add_opinion = {
							modifier = left_war_early
							opinion = 30
							target = scope:actor
						}
					}
				}
				else = {
					primary_defender = {
						add_opinion = {
							modifier = left_war_early
							opinion = 30
							target = scope:actor
						}
					}
				}
				add_to_variable_list = {
					name = left_voluntarily
					target = scope:actor
				}
			}
			if = {
				limit = {
					scope:war = {
						using_non_ghw_holy_war_cb_trigger = yes
					}
				}
				add_piety_level = -1
			}
			save_scope_value_as = {
				name = task_contract_reward
				value = flag:failure_standard
			}
			save_scope_value_as = {
				name = task_contract_tier
				value = scope:task_contract.task_contract_tier
			}
			every_character_active_contract = {
				limit = { var:task_contract_war = scope:war }
				task_contract_employer = { save_scope_as = task_contract_employer }
				invalidate_contract = yes
			}
			if = {
				limit = {
					NOT = {
						exists = scope:task_contract_employer
					}
				}
				scope:recipient = {
					save_scope_as = task_contract_employer
				}
			}
			trigger_event = ep3_contract_event.0013
			# We deliberately charge you here to let you go into prestige debt; you shouldn't be blocked from switching sides due to a lack of prestige if you're willing to tank the loss.
			add_prestige = {
				value = -200
				multiply = {
					value = scope:recipient.highest_held_title_tier
					add = -1
				}
			}
			if = {
				limit = { scope:recipient.highest_held_title_tier >= tier_kingdom }
				add_prestige_level = -1
			}
			# Break the alliance
			if = {
				limit = { is_allied_to = scope:recipient }
				break_alliance = scope:recipient
			}
			# And don't forget stress!
			stress_impact = {
				base = major_stress_impact_gain
				disloyal = medium_stress_impact_loss
				ambitious = minor_stress_impact_loss
				arrogant = minor_stress_impact_loss
				callous = miniscule_stress_impact_loss
				arbitrary = miniscule_stress_impact_loss
				just = minor_stress_impact_gain
				humble = medium_stress_impact_gain
				content = medium_stress_impact_gain
				loyal = major_stress_impact_gain
			}
			# Clear Offer Assistance.
			if = {
				limit = {
					has_variable = owed_contract_assistance_war
					scope:recipient = { is_participant_in_war = scope:actor.var:owed_contract_assistance_war }
				}
				remove_variable = owed_contract_assistance_war
				remove_variable = owed_contract_assistance_contribution
				remove_variable = owed_contract_assistance_gold
			}
		}
		scope:recipient = {
			if = {
				limit = {
					can_set_relation_potential_rival_trigger = { CHARACTER = scope:actor }
				}
				set_relation_potential_rival = scope:actor
			}
			add_opinion = {
				modifier = left_war_early
				target = scope:actor
			}
		}
	}

	auto_accept = yes

	ai_potential = {
		is_at_war = yes
	}

	ai_targets = {
		ai_recipients = war_allies
	}

	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 24
		kingdom = 0
		empire = 0
		hegemony = 0
	}

	ai_will_do = {
		base = -25
		#you have to be dishonorable and lazy to do this
		ai_value_modifier = {
			ai_honor = -0.1
			ai_energy = -0.1
		}
		##check if the recipient is the war leader and do it
		#if they are infamous
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			scope:recipient = {
				prestige_level < 0
			}
			add = 5
		}
		#if they are impious
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			scope:recipient = {
				piety_level < 0
			}
			add = 5
		}
		#if they are a criminal
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			has_any_criminal_trait_in_faith_trigger = {
				CHARACTER = scope:recipient
				FAITH = scope:actor.faith
			}
			add = 5
		}
		#if they wronged you or your family
		modifier = {
			any_character_war = {
				is_war_leader = scope:recipient
			}
			scope:actor = {
				has_any_major_revenge_opinion_against_character_trigger = {
					CHARACTER = scope:recipient
				}
			}
			add = 5
		}
		#if they have good relation with the other side
		modifier = {
			any_character_war = {
				OR = {
					AND = {
						primary_defender = scope:recipient
						primary_attacker = {
							has_any_good_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
					AND = {
						primary_attacker = scope:recipient
						primary_defender = {
							has_any_good_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
				}
			}
			add = 5
		}
		#don't do it
		# if the other side has bad relation with you
		modifier = {
			any_character_war = {
				OR = {
					AND = {
						primary_defender = scope:recipient
						primary_attacker = {
							has_any_bad_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
					AND = {
						primary_attacker = scope:recipient
						primary_defender = {
							has_any_bad_relationship_with_character_trigger = {
								CHARACTER = scope:actor
							}
						}
					}
				}
			}
			add = -1000
		}
		# you are winning
		modifier = {
			any_character_war = {
				OR = {
					AND = {
						primary_defender = scope:recipient
						defender_war_score >= 0
					}
					AND = {
						primary_attacker = scope:recipient
						attacker_war_score >= 0
					} 
				}
			}
			add = -1000
		}
		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = -0.1
			desc = AI_OPINION_REASON
		}
	}
}
