###################
# Claim Throne
# by Petter Vilberg
###################
claim_throne_interaction = {
	icon = icon_scheme_claim_throne
	category = interaction_category_vassal
	interface_priority = 268 # after Start Claimant Faction

	ai_targets = {
		ai_recipients = liege
	}
	
	auto_accept = yes
	use_diplomatic_range = no
	ignores_pending_interaction_block = yes


	scheme = claim_throne

	desc = claim_throne_interaction_desc
	
	is_shown = {
		exists = scope:actor.liege
		scope:recipient = scope:actor.liege
		scope:actor = {
			is_landless_adventurer = no
			highest_held_title_tier > tier_barony
			OR = {
				has_perk = meritocracy_perk
				AND = {
					is_diarch_of_target = scope:recipient
					scope:recipient = {
						NOT = { has_diarchy_parameter = can_be_instantly_dismissed }
					}
				}
				AND = {
					is_powerful_vassal_of = scope:recipient
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = unlocks_claim_throne_for_powerful_vassals
						is_secondary_character_involvement_involved_trigger = {
							CHAR = scope:recipient
						}
					}
				}
				AND = {
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = unlocks_claim_throne_for_all
						is_secondary_character_involvement_involved_trigger = {
							CHAR = scope:recipient
						}
					}
				}
				AND = {
					exists = title:d_sunni.holder
					is_vassal_of = title:d_sunni.holder
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = unlocks_claim_throne_for_caliph_vassals
					}
				}
			}
		}
		NOT = { scope:recipient = scope:actor }	
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_physically_able_adult = yes
			can_start_scheme = {
				target_character = scope:recipient
				type = claim_throne
			}
		}
	}

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

	on_accept = {
		scope:actor = {
			stress_impact = {
				content = medium_stress_impact_gain
				just = medium_stress_impact_gain
			}
			send_interface_toast = {
				type = event_toast_effect_good
				title = claim_throne_interaction_toast
				left_icon = scope:actor
				right_icon = scope:recipient

				# Success.
				if = {
					limit = { scope:agent_focus_success ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = claim_throne
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_diplomat
						AGENT_2 = agent_diplomat
						AGENT_3 = agent_scribe
						# Speed.
						AGENT_4 = agent_cleric
						# Secrecy.
						AGENT_5 = agent_justiciar
					}
				}
				# Speed.
				else_if = {
					limit = { scope:agent_focus_speed ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = claim_throne
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Speed.
						AGENT_1 = agent_cleric
						AGENT_2 = agent_socialite
						AGENT_3 = agent_gabbler
						# Success.
						AGENT_4 = agent_diplomat
						# Secrecy.
						AGENT_5 = agent_justiciar
					}
				}
				# Secrecy.
				else_if = {
					limit = { scope:agent_focus_secrecy ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = claim_throne
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Secrecy.
						AGENT_1 = agent_justiciar
						AGENT_2 = agent_justiciar
						AGENT_3 = agent_decoy
						# Success.
						AGENT_4 = agent_diplomat
						# Speed.
						AGENT_5 = agent_cleric
					}
				}
				# Balanced.
				else = {
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = claim_throne
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_diplomat
						AGENT_2 = agent_scribe
						# Speed.
						AGENT_3 = agent_cleric
						AGENT_4 = agent_socialite
						# Secrecy.
						AGENT_5 = agent_decoy
					}
				}

				show_as_tooltip = {
					stress_impact = {
						content = medium_stress_impact_gain
						just = medium_stress_impact_gain
					}
				}
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_throne_claim.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	ai_potential = {
		is_adult = yes
		highest_held_title_tier > tier_barony
		ai_greed >= medium_positive_ai_value
		learning >= medium_skill_rating
		intrigue >= mediocre_skill_rating
		trigger_if = {
			limit = {
				liege = { save_temporary_scope_as = struggle_liege }
				any_character_struggle = {
					involvement = involved
					has_struggle_phase_parameter = unlocks_claim_throne_for_all
					is_secondary_character_involvement_involved_trigger = {
						CHAR = scope:struggle_liege
					}
				}
			}
			is_powerful_vassal = yes
		}
		NOR = {
			has_trait = content
			opinion = {
				target = liege
				value >= high_positive_opinion
			}
			any_scheme = {
				OR = {
					scheme_type = murder
					scheme_type = abduct
					scheme_type = claim_throne
					scheme_type = fabricate_hook
					scheme_type = steal_back_artifact
				}
			}
		}
	}

	ai_frequency = 36

	ai_will_do = {
		base = 100

		# Agenda impact
		modifier = {
			scope:actor = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = iberian_struggle
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_gain_claim_on_title_fp2
							}
							has_character_flag = agenda_towards_escalation
						}
					}
					add = 200
				}
				else_if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_gain_claim_on_title_fp2
							}
						}
					}
					add = -100
				}
			}
		}

		modifier = {
			scope:actor = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = persian_struggle
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_gain_claim_on_title_fp3
							}
							has_trait = fp3_struggle_detractor
						}
					}
					add = 200
				}
				else_if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_gain_claim_on_title_fp3
							}
							has_trait = fp3_struggle_supporter
						}
						scope:recipient = {
							has_trait = fp3_struggle_detractor
						}
					}
					add = 100
				}
				else_if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_gain_claim_on_title_fp3
							}
						}
					}
					add = -100
				}
			}
		}

		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 100
		}

		start_hostile_scheme_ai_base_modifiers = yes # At the end so Cowed can block it completely
	}
}


###################
# Fabricate Hook
# by Petter Vilberg
###################
fabricate_hook_interaction = {
	icon = icon_scheme_fabricate_hook
	category = interaction_category_hostile

	auto_accept = yes
	ignores_pending_interaction_block = yes

	scheme = fabricate_hook

	desc = fabricate_hook_interaction_desc
	
	is_shown = {
		scope:actor = {
			OR = {
				has_perk = truth_is_relative_perk
				AND = {
					has_trait = vengeful
					has_relation_rival = scope:recipient
				}
				AND = {
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = unlocks_fabricate_hooks_for_all
						is_secondary_character_involvement_either_trigger = {
							CHAR = scope:recipient
						}
					}
				}
				AND = {
					government_has_flag = government_is_clan
					is_in_same_clan_as_trigger = { CHARACTER = scope:recipient }
					house = { has_house_unity_stage = competitive }	
				}	
			}
		}
		NOT = { scope:recipient = scope:actor }
	}

	is_valid = {
		NOT = { scope:actor = { has_hook = scope:recipient } }
		OR = {
			scope:actor = {
				OR = {
					has_perk = truth_is_relative_perk # To display that it's available because of the Perk
					AND = {
						has_trait = vengeful
						has_relation_rival = scope:recipient
					}
					AND = {
						any_character_struggle = {
							involvement = involved
							has_struggle_phase_parameter = unlocks_fabricate_hooks_for_all
							is_secondary_character_involvement_either_trigger = {
								CHAR = scope:recipient
							}
						}
					}
					AND = {
						government_has_flag = government_is_clan
						is_in_same_clan_as_trigger = { CHARACTER = scope:recipient }
						house = { has_house_unity_stage = competitive }	
					}	
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_physically_able_adult = yes
			can_start_scheme = {
				target_character = scope:recipient
				type = fabricate_hook
			}
		}
	}

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

	on_accept = {
		scope:actor = {
			stress_impact = {
				just = medium_stress_impact_gain
				honest = medium_stress_impact_gain
				compassionate = minor_stress_impact_gain
			}
			send_interface_toast = {
				type = event_toast_effect_good
				title = fabricate_hook_interaction_toast
				left_icon = scope:actor
				right_icon = scope:recipient

				# Success.
				if = {
					limit = { scope:agent_focus_success ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = fabricate_hook
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_outcast
						AGENT_2 = agent_diplomat
						AGENT_3 = agent_thief
						# 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 = fabricate_hook
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Speed.
						AGENT_1 = agent_infiltrator
						AGENT_2 = agent_footpad
						AGENT_3 = agent_socialite
						# Success.
						AGENT_4 = agent_outcast
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}
				# Secrecy.
				else_if = {
					limit = { scope:agent_focus_secrecy ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = fabricate_hook
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Secrecy.
						AGENT_1 = agent_lookout
						AGENT_2 = agent_decoy
						AGENT_3 = agent_justiciar
						# Success.
						AGENT_4 = agent_outcast
						# Speed.
						AGENT_5 = agent_footpad
					}
				}
				# Balanced.
				else = {
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = fabricate_hook
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_outcast
						AGENT_2 = agent_thief
						# Speed.
						AGENT_3 = agent_infiltrator
						AGENT_4 = agent_footpad
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}
				show_as_tooltip = {
					stress_impact = {
						just = medium_stress_impact_gain
						honest = medium_stress_impact_gain
						compassionate = minor_stress_impact_gain
					}
				}
			}
		}
	}

	ai_targets = {
		ai_recipients = liege
		ai_recipients = vassals
	}

	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = peer_vassals
		max = 10
	}
	
	ai_targets = {
		ai_recipients = courtiers
		max = 5
	}
	
	ai_targets = {
		ai_recipients = realm_characters
		ai_recipients = top_realm_domicile_owners
		max = 30
	}
	
	ai_potential = {
		is_adult = yes
		ai_greed >= low_positive_ai_value
		intrigue >= medium_skill_rating
		NOR = {
			has_trait = content
			any_scheme = {
				OR = {
					scheme_type = murder
					scheme_type = abduct
					scheme_type = claim_throne
					scheme_type = fabricate_hook
					scheme_type = steal_back_artifact
				}
			}
		}
	}
	
	ai_target_quick_trigger = {
		adult = yes
	}

	ai_frequency = 12

	ai_will_do = {
		base = 10

		modifier = {
			add = {
				value = 20
				if = {
					limit = {
						exists = scope:recipient.cp:councillor_spymaster
						scope:recipient.cp:councillor_spymaster = { intrigue < 10 }
					}
					add = 50
				}
			}
			
			scope:recipient = {
				is_vassal_of = scope:actor
				vassal_contract_has_modifiable_obligations = yes
			}
		}
		
		modifier = {
			add = -50
			
			opinion = {
				target = scope:recipient
				value >= high_positive_opinion
			}
		}
		
		modifier = {
			add = -40
			
			scope:recipient = {
				is_playable_character = yes
				NOT = {
					is_vassal_of = scope:actor
				}
			}
		}
		
		modifier = {
			add = 50
			
			scope:recipient = {
				is_playable_character = no
				OR = {
					liege = scope:actor.liege
					liege = {
						OR = {
							has_relation_rival = scope:actor
							has_relation_nemesis = scope:actor
						}
					}
				}
			}
		}
		
		modifier = {
			factor = 0
			
			scope:recipient = {
				has_council_position = councillor_spymaster
			}
		}
		start_hostile_scheme_ai_base_modifiers = yes # At the end so Cowed can block it completely
	}
}


###################
# Demand Payment
# by Petter Vilberg
###################
demand_payment_interaction = {
	category = interaction_category_friendly
	icon = icon_gold

	auto_accept = yes
	use_diplomatic_range = yes
	interface_priority = 35

	desc = demand_payment_interaction_desc

	is_shown = {
		scope:actor = {
			OR = {
				has_perk = golden_obligations_perk
				AND = {
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = unlocks_demand_payments_for_all
						is_secondary_character_involvement_either_trigger = {
							CHAR = scope:recipient
						}
					}
				}
			}
		}
		scope:recipient = {
			is_ai = yes
		}
		NOT = { scope:recipient = scope:actor }
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_physically_able_adult = yes
			NOT = {
				is_at_war_with = scope:recipient
			}
		}
		scope:recipient = {
			is_imprisoned = no
		}
		scope:recipient = {
			custom_description = {
				text = demand_payment_too_little_gold
				gold >= 1
			}
		}
	}

	is_valid = {
		scope:actor = {
			has_usable_hook = scope:recipient
		}
	}

	is_highlighted = {
		scope:actor = {
			has_usable_hook = scope:recipient
		}
	}

	on_accept = {
		if = {
			limit = {
				scope:recipient.gold < scope:recipient.golden_obligation_value
			}
			custom_tooltip = demand_payment_too_little_gold_tt
		}
		scope:actor = {
			send_interface_toast = {
				title = demand_payment_interaction_message
				left_icon = scope:recipient

				scope:actor = {
					use_hook = scope:recipient
				}
				scope:recipient = {
					if = {
						pay_short_term_gold = {
							target = scope:actor
							gold = golden_obligation_value
						}
						limit = {
							gold > golden_obligation_value
						}
					}
					else = {
						pay_short_term_gold = {
							target = scope:actor
							gold = {
								value = scope:recipient.gold
								floor = yes
							}
						}
					}
				}
			}
			stress_impact = {
				generous = medium_stress_impact_gain
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_demand_payment.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	ai_potential = {
		NOT = {
			has_trait = generous
		}
		OR = {
			ai_greed >= low_positive_ai_value
			is_at_war = yes
		}
	}
	
	ai_target_quick_trigger = {
		adult = yes
	}
	
	ai_targets = {
		ai_recipients = hooked_characters
	}
	
	ai_frequency = 36
	
	ai_will_do = {
		base = 100

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = -100
		}

		modifier = { #ai won't use the interaction if they'll get nothing out of it
			factor = 0
			scope:recipient.gold < 8
		}
		
		modifier = {
			factor = 0
			scope:actor = {
				OR = {
					has_relation_friend = scope:recipient
					has_relation_lover = scope:recipient
					has_relation_best_friend = scope:recipient
					has_relation_soulmate = scope:recipient
				}
			}
		}
	}
}



##################
# Buy Claim
# by Petter Vilberg
##################
buy_claim_interaction = {
	category = interaction_category_diplomacy
	icon = vassal_claim_liege_title_interaction

	desc = buy_claim_interaction_desc
	target_type = title
	target_filter = recipient_domain_titles
	ignores_pending_interaction_block = yes

	is_shown = {
		scope:actor = {
			is_landless_adventurer = no
			OR = {
				has_perk = sanctioned_loopholes_perk
				AND = {
					any_character_struggle = {
						involvement = involved
						has_struggle_phase_parameter = unlocks_buy_claim_for_all
						is_secondary_character_involvement_either_trigger = {
							CHAR = scope:recipient
						}
					}
				}
			}
			NOT = { target_is_liege_or_above = scope:recipient }
		}
		NOT = {
			scope:actor = scope:recipient
			AND = {
				scope:recipient = scope:actor.faith.religious_head
				scope:actor.faith = {
					NOT = { has_doctrine = doctrine_theocracy_temporal }
				}
			}
		}
		scope:recipient = {
			is_playable_character = yes
			primary_title.tier > tier_barony
			NOT = {	
				target_is_liege_or_above = scope:actor
			}
		}
	}

	can_be_picked_title = {
		scope:target = {
			tier >= tier_county
		}
	}
	
	is_valid_showing_failures_only = {
		trigger_if = { # Can't request on titles you already have a claim on
			limit = {
				scope:target ?= {
					any_claimant = { this = scope:actor }
				}
			}
			custom_description = {
				text = buy_claim_has_pressed_claim_already
				always = no
			}
		}
		trigger_if = { # Can't request claims on baronies
			limit = {
				scope:target ?= { tier = tier_barony }
			}
			custom_description = {
				text = buy_claim_has_pressed_claim_already
				always = no
			}
		}
		trigger_if = { # Can't buy Kingdom or Empire Claims if you're a King
			limit = {
				scope:target ?= { tier = tier_kingdom }
				scope:actor.highest_held_title_tier >= tier_kingdom
			}
			custom_description = {
				text = buy_claim_no_king_or_emperor_kingdom
				always = no
			}
		}
		trigger_if = { # Can't buy Empire Claims if you're an Emperor
			limit = {
				scope:target ?= { tier = tier_empire }
				scope:actor.highest_held_title_tier >= tier_empire
			}
			custom_description = {
				text = buy_claim_no_emperor_empire
				always = no
			}
		}
		trigger_if = { # Can't buy claims on head of faith titles
			limit = {
				scope:target ?= { is_head_of_faith = yes }
			}
			custom_description = {
				text = buy_claim_no_religious_titles
				always = no
			}
		}
		trigger_if = { # Can't buy claims on noble families
			limit = {
				scope:target ?= { is_noble_family_title = yes }
			}
			custom_description = {
				text = buy_claim_no_noble_family_titles
				always = no
			}
		}
		trigger_if = { # Can't buy claims on landless adventurer titles
			limit = {
				scope:target ?= { has_variable = adventurer_creation_reason }
			}
			custom_description = {
				text = buy_claim_no_landless_adventurer_title
				always = no
			}
		}
		scope:actor = { # Variable piety cost depending on tier
			trigger_if = {
				limit = {
					exists = scope:target
					scope:target = {
						tier = tier_county
					}
				}
				piety >= {
					value = minor_buy_claim_piety_value
					if = {
						limit = {
							has_government = administrative_government
						}
						multiply = 1.5
					}
				}
			}
			trigger_if = {
				limit = {
					exists = scope:target
					scope:target = {
						tier = tier_duchy
					}
				}
				piety >= {
					value = medium_buy_claim_piety_value
					if = {
						limit = {
							has_government = administrative_government
						}
						multiply = 1.5
					}
				}
			}
			trigger_if = {
				limit = {
					exists = scope:target
					scope:target = {
						tier = tier_kingdom
					}
				}
				piety >= {
					value = major_buy_claim_piety_value
					if = {
						limit = {
							has_government = administrative_government
						}
						multiply = 1.5
					}
				}
			}
			trigger_if = {
				limit = {
					exists = scope:target
					scope:target = {
						tier = tier_empire
					}
				}
				piety >= {
					value = massive_buy_claim_piety_value
					if = {
						limit = {
							has_government = administrative_government
						}
						multiply = 1.5
					}
				}
			}
		}
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
	}
	
	auto_accept = yes
	
	on_accept = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = buy_claim_interaction.message
				left_icon = scope:actor
				right_icon = scope:recipient
				add_piety = {
					value = 0
					if = {
						limit = {
							scope:target = {
								tier = tier_county
							}
						}
						add = minor_buy_claim_piety_loss
					}
					if = {
						limit = {
							scope:target = {
								tier = tier_duchy
							}
						}
						add = medium_buy_claim_piety_loss
					}
					if = {
						limit = {
							scope:target = {
								tier = tier_kingdom
							}
						}
						add = major_buy_claim_piety_loss
					}
					if = {
						limit = {
							scope:target = {
								tier = tier_empire
							}
						}
						add = massive_buy_claim_piety_loss
					}
					if = {
						limit = {
							has_government = administrative_government
						}
						multiply = 1.5
					}
				}
				add_unpressed_claim = scope:target
			}
			stress_impact = {
				just = medium_stress_impact_gain
				honest = medium_stress_impact_gain
			}
		}
		scope:recipient = {
			add_opinion = {
				target = scope:actor
				modifier = claimed_my_title_opinion
				opinion = -40
			}
			trigger_event = perk_interaction.0002
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_bought_claim.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
}



##################
# Challenge Rival
# by Ewan Cowhig Croft
##################

challenge_to_single_combat_interaction = {
	icon = icon_combat
	interface_priority = 30
	common_interaction = yes
	category = interaction_category_hostile
	ai_maybe = yes
	ai_max_reply_days = 0
	popup_on_receive = yes
	pause_on_receive = yes
	desc = challenge_to_single_combat_interaction_desc

	greeting = negative
	notification_text = REQUEST_TRIAL_BY_COMBAT_TEXT

	is_shown = {
		# First, check for the perk.
		scope:actor = { can_challenge_rival_to_single_combat_trigger = yes }
		# Then, look to see if this character is a rival.
		scope:actor = { has_relation_rival = scope:recipient }
		# Can't duel yourself.
		NOT = { scope:actor = scope:recipient }
	}

	is_valid_showing_failures_only = {
		# Calc some stuff that might invalidate elsewhere before the bout is sorted.
		rival_single_combat_shown_and_not_invalid_trigger = yes
		# Scope:recipient must be able to fight personally.
		scope:recipient = { can_start_single_combat_trigger = yes }
		# Scope:recipient must not be banned from single combat.
		scope:recipient = {
			custom_description = {
				text = fp1_tbc_recipient_banned_from_combat
				can_start_single_combat_banned_checks_trigger = no
			}
		}
		# Neither of the characters can be imprisoned.
		scope:actor = { is_imprisoned = no }
		scope:recipient = { is_imprisoned = no }
	}

	can_send = {
		scope:actor = {
			custom_description = {
				text = "character_interactions_hostile_actions_disabled_delay"
				NOT = { has_character_flag = flag_hostile_actions_disabled_delay }
			}
		}
	}
	
	cost = { prestige = medium_prestige_value }
	cooldown = { years = 5 }
	cooldown_against_recipient = { years = 10 }

	on_send = {
		scope:actor = {
			add_character_flag = {
				flag = flag_hostile_actions_disabled_delay
				days = 10
			}
		}
	}

	on_accept = {
		# Set up the bout immediately.
		## First inform.
		scope:actor = {
			custom_tooltip = challenge_to_single_combat_interaction.enter_non_lethal_bout.tt
			custom_tooltip = challenge_to_single_combat_interaction.victor_gets.tt
			custom_tooltip = challenge_to_single_combat_interaction.loser_gets.tt
		}
		## Finally, begin.
		configure_start_single_combat_effect = {
			SC_INITIATOR = scope:actor
			SC_ATTACKER = scope:actor
			SC_DEFENDER = scope:recipient
			FATALITY = no
			FIXED = no
			LOCALE = terrain_scope
			OUTPUT_EVENT = perk_interaction.0101
			INVALIDATION_EVENT = perk_interaction.0102
		}

		# If we're a clan this interaction affects unity
		if = {
			limit = {
				scope:actor = {
					exists = scope:actor.house
					house = { # But only if we have high unity!
						OR = {
							has_house_unity_stage = harmonious
							has_house_unity_stage = friendly
						}
					}
				}
			}
			add_clan_unity_interaction_effect = {
				CHARACTER = scope:actor
				TARGET = scope:recipient
				VALUE = medium_unity_loss
				DESC = clan_unity_challenge_to_combat.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
			custom_description_no_bullet = { text = active_unity_level_description_tt }
		}
	}

	on_decline = {
		# Scope:actor decline processing.
		scope:actor = {
			# Inform them of the decline & scope:recipient loses some prestige.
			send_interface_toast = {
				type = event_toast_effect_bad
				title = challenge_to_single_combat_interaction.challenge_rejected.t
				left_icon = scope:recipient
				right_icon = scope:actor
				scope:recipient = { add_prestige = medium_prestige_loss }
				show_as_tooltip = {
					add_prestige = medium_prestige_value
				}
			}
			hidden_effect = {
				add_prestige_no_experience = medium_prestige_value
			}
			# Cooldown is removed.
			hidden_effect = { remove_interaction_cooldown = challenge_to_single_combat_interaction }
			# Clear up hostile actions flag.
			if = {
				limit = {
					has_character_flag = flag_hostile_actions_disabled_delay
				}
				remove_character_flag = flag_hostile_actions_disabled_delay
			}
		}
	}

	auto_accept = no

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

		# Relative fightan ability.
		modifier = {
			desc = CTSC_RELATIVE_PROWESS
			scope:actor.prowess < scope:recipient.prowess
			add = {
				value = scope:recipient.prowess
				subtract = scope:actor.prowess
				multiply = 2
			}
		}
		modifier = {
			desc = CTSC_RELATIVE_PROWESS
			scope:recipient.prowess < scope:actor.prowess
			add = {
				value = scope:actor.prowess
				subtract = scope:recipient.prowess
				multiply = -2
			}
		}
		# Weight up for personality values.
		ai_value_modifier = {
			who = scope:recipient
			ai_boldness = {
				if = {
					limit = {
						scope:recipient = {
							ai_boldness > 0
							ai_rationality < 0
						}
					}
					value = 2
				}
				else_if = {
					limit = {
						scope:recipient = {
							NOT = { ai_boldness = 0 }
						}
					}
					value = 1
				}
			}
			ai_vengefulness = {
				if = {
					limit = {
						scope:recipient = {
							NOT = { ai_vengefulness = 0 }
						}
					}
					value = 0.75
				}
			}
			ai_honor = {
				if = {
					limit = {
						scope:recipient = {
							ai_honor > 0
						}
					}
					value = 0.5
				}
			}
		}
		# Apply tier differences.
		## King -> Baron.
		modifier = {
			desc = AI_REFUSAL_RANK_DIFFERENCE
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value >= 4
				}
			}
			add = 15
		}
		## Duke -> Baron.
		modifier = {
			desc = AI_REFUSAL_RANK_DIFFERENCE
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 3
				}
			}
			add = 10
		}
		## Count -> Baron.
		modifier = {
			desc = AI_REFUSAL_RANK_DIFFERENCE
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 2
				}
			}
			add = 5
		}
		## King <- Baron.
		modifier = {
			desc = AI_REFUSAL_RANK_DIFFERENCE
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -2
				}
			}
			add = -10
		}
		## Duke <- Baron.
		modifier = {
			desc = AI_REFUSAL_RANK_DIFFERENCE
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -3
				}
			}
			add = -20
		}
		## Count <- Baron.
		modifier = {
			desc = AI_REFUSAL_RANK_DIFFERENCE
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value <= -4
				}
			}
			add = -30
		}
		# Nemesis modifier.
		modifier = {
			desc = offer_vassalization_interaction_aibehavior_nemesis_tt
			trigger = {
				scope:recipient = {
					has_relation_nemesis = scope:actor
				}
			}
			add = 50
		}

		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 100
		}
	}

	# AI
	ai_potential = {
		can_challenge_rival_to_single_combat_trigger = yes
		NOT = { has_trait = craven }
		num_of_relation_rival > 0
		is_imprisoned = no
		can_start_single_combat_trigger = yes
	}
	
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_targets = {
		ai_recipients = scripted_relations
	}

	ai_frequency = 60

	ai_will_do = {
		base = -50
		
		# Factor for personality (excluding realism).
		modifier = {
			add = {
				value = ai_boldness
				multiply = 1
			}
		}
		modifier = { add = ai_vengefulness }
		modifier = {
			add = {
				value = ai_energy
				multiply = -0.25
			}
		}

		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 100
		}

		# Don't challenge kinslayer-counting family, unless they're your nemesis.
		modifier = {
			scope:actor = {
				murdering_character_is_kinslaying_in_faith_trigger = {
					CHARACTER = scope:recipient
					FAITH = scope:actor.faith
				}
			}
			scope:recipient = {
				NOT = { has_relation_nemesis = scope:actor }
			}
			add = -1000
		}
		# Don't challenge your spouse, unless they're your nemesis.
		modifier = {
			scope:recipient = {
				is_spouse_of = scope:actor
				NOT = { has_relation_nemesis = scope:actor }
			}
			add = -1000
		}
		# Don't challenge people hopelessly out of your league unless you're really, really not seeing things clearly.
		modifier = {
			scope:actor = {
				prowess_diff = {
					target = scope:recipient
					value <= -15
				}
				ai_rationality >= high_negative_ai_value
			}
			add = -1000
		}
	}
}
