﻿# As a Supporter, demand that a Muslim ruler recognize your authority
# Success makes the Strengthen the Caliphate struggle decision easier to take
fp3_demand_submission_interaction = {
	icon = demand_submission
	category = interaction_category_religion
	popup_on_receive = yes
	pause_on_receive = yes
	can_send_despite_rejection = yes
	ai_maybe = yes

	greeting = positive
	notification_text = DEMAND_SUBMISSION_NOTIFICATION
	
	is_shown = {
		NOT = { scope:actor = scope:recipient }
		struggle:persian_struggle ?= { has_struggle_phase_parameter = demand_submission_interaction_unlocked }
		scope:actor = { has_title = title:d_sunni }
		scope:recipient = {
			faith.religious_head ?= scope:actor
			has_trait = fp3_struggle_supporter
			any_character_struggle = { this = struggle:persian_struggle }
		}
	}

	cooldown_against_recipient = { years = 10 }

	ai_min_reply_days = 2
	ai_max_reply_days = 7
	
	ai_accept = {
		base = 0
		# This is more flavorful than "base acceptance"
		modifier = {
			desc = DEMAND_SUBMISSION_I_AM_HEAD_OF_FAITH
			add = 50
		}
		##	Piety
		# How pious are you?
		modifier = {
			# (Stolen shamelessly from piety_level_affects_vassalage_acceptance)
			add = {
				value = {
					value = -1
					add = scope:actor.piety_level
				}
				multiply = 10
			}
			desc = I_AM_PIOUS
			# Only matters if they aren't cynical
			NOT = { scope:recipient = { has_trait = cynical } }
		}
		# Are they cynical/zealous?
		modifier = {
			desc = ASK_FOR_CONVERSION_RECIPIENT_IS_CYNICAL
			add = -30
			scope:recipient = {
				has_trait = cynical
			}
		}
		modifier = {
			desc = ASK_FOR_CONVERSION_RECIPIENT_IS_ZEALOUS
			add = 20
			scope:recipient = { has_trait = zealous }
		}
		# Are you giving them extra piety?
		modifier = {
			add = 30
			desc = STRUGGLE_MARRIAGE_ACCEPTANCE_USING_PIETY_AS_CURRENCY
			scope:piety_cost_reduction ?= yes
		}
		## Opinion
		# Do they like you?
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.25
			desc = AI_OPINION_REASON
		}
		## Personality
		# Are they ambitious?
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_AMBITIOUS
			add = -30
			scope:recipient = { has_trait = ambitious }
		}
		# Are they content?
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_CONTENT
			add = 10
			scope:recipient = { has_trait = content }
		}
		# Are they stubborn?
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_STUBBORN
			add = -10
			scope:recipient = { has_trait = stubborn }
		}
		# Are they arrogant?
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_ARROGANT
			add = -10
			scope:recipient = { has_trait = arrogant }
		}
		# Are they humble?
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_HUMBLE
			add = 10
			scope:recipient = { has_trait = humble }
		}
		# Are they trusting?
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_TRUSTING
			add = 10
			scope:recipient = { has_trait = trusting }
		}
		# Are they paranoid?
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_PARANOID
			add = -20
			scope:recipient = { has_trait = paranoid }
		}
		# Are they loyal?
		# Independent rulers should rarely have this trait, but if your predecessor installed a puppet and died, then this makes it easier to demand submission from the puppet
		modifier = {
			desc = DEMAND_SUBMISSION_RECIPIENT_IS_LOYAL
			add = 20
			scope:recipient = { has_trait = loyal }
		}
		## Realm
		# Are you stronger/weaker than them?
		modifier = {
			add = {
				value = 1
				subtract = {
					value = scope:recipient.max_military_strength # For foreign diplomacy, consider recipients max
					divide = { value = scope:actor.current_military_strength min = 1 }
				}
				multiply = 10
			}
			desc = offer_vassalization_interaction_aibehavior_power_tt
		}
		# Reign duration
		modifier = {
			add = {
				value = scope:actor.primary_title.title_held_years
				subtract = 21
				max = 20
			}
			desc = DEMAND_SUBMISSION_SHORT_REIGN
		}
		## Dread
		modifier = {
			add = intimidated_halved_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_halved_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
	}

	on_accept = {
		scope:actor = {
			if = {
				limit = { scope:piety_cost_reduction ?= yes }
				add_piety = massive_piety_loss
			}
			add_hook = {
				target = scope:recipient
				type = caliphal_submission_hook
			}
			if = {
				limit = { has_variable = last_explicit_defied_caliph }
				remove_variable = last_explicit_defied_caliph
			}
			trigger_event = fp3_misc_decisions.0010 # Letter event informing player of success
		}
		scope:recipient = {
			if = {
				limit = { scope:piety_cost_reduction ?= yes }
				add_piety = massive_piety_gain
			}
		}

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

	on_decline = {
		scope:recipient = {
			custom_tooltip = DEMAND_SUBMISSION_INSTALL_LOYALIST_WARNING
			add_piety = medium_piety_loss
			remove_trait = fp3_struggle_supporter
			add_trait_force_tooltip = fp3_struggle_detractor
			# This is for the install loyalist CB discount
			set_variable = {
				name = last_explicit_defied_caliph
				value = scope:actor
				years = 30
			}
		}
		scope:actor = {
			trigger_event = fp3_misc_decisions.0011 # Letter event informing player of failure
		}

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

	# Use piety to increase acceptance
	send_option = {
		flag = piety_cost_reduction
		localization = TRADE_PIETY_FOR_BETTER_AI_ACCEPTANCE
	}
	send_options_exclusive = no

	ai_potential = {
		is_physically_able = yes
	}

	ai_targets = {
		ai_recipients = neighboring_rulers
	}

	ai_frequency = 12
	
	# Very similar to ai_accept, but ignores effects that are < 10 in value for brevity's sake
	ai_will_do = {
		# The caliph generally _wants_ to be chucking these out all over the place.
		## Basically, the more rulers submit to their authority, the better.
		### Subs over drubs, as it were.
		base = 100
		# Will demand rulers who were installed via the puppet CB
		modifier = {
			add = 200
			scope:recipient = {
				has_trait = loyal
				scope:actor = { has_hook = prev }
			}
		}
		modifier = {
			add = {
				value = {
					value = -1
					add = scope:actor.piety_level
				}
				multiply = 10
			}
			NOT = { scope:recipient = { has_trait = cynical } }
		}
		modifier = {
			add = -30
			scope:recipient = {
				has_trait = cynical
			}
		}
		modifier = {
			add = 20
			scope:recipient = { has_trait = zealous }
		}
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.25
		}
		modifier = {
			add = -30
			scope:recipient = { has_trait = ambitious }
		}
		modifier = {
			add = {
				value = 1
				subtract = {
					value = scope:recipient.max_military_strength
					divide = { value = scope:actor.current_military_strength min = 1 }
				}
				multiply = 10
			}
		}
		# Reign duration
		modifier = {
			add = {
				value = scope:actor.primary_title.title_held_years
				subtract = 21
				max = 20
			}
		}
		## Dread
		modifier = {
			add = intimidated_halved_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
		}
		modifier = {
			add = cowed_halved_reason_value
			scope:recipient = {
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
		}
	}
}

###################################
# Request Turkic invasion (can_request_incursion_interaction)
# By Hugo Cortell
###################################
fp3_request_turkic_invasion_interaction = {
	interface_priority = 100
	common_interaction = yes
	category = interaction_category_hostile
	cooldown_against_recipient = { years = 2 }

	icon = request_mercenary
	desc = fp3_request_turkic_invasion_interaction_desc

	is_shown = {
		scope:actor = {
			has_trait = fp3_struggle_detractor
			any_character_struggle = { is_struggle_type = persian_struggle }
		}
		scope:recipient = {
			is_playable_character = yes
			OR = {
				any_character_struggle = { is_struggle_type = persian_struggle }
				title:d_sunni.holder ?= this
			}
			is_independent_ruler = yes
			is_ai = yes
		}
		NOT = { scope:actor = scope:recipient }
	}

	is_valid_showing_failures_only = {
	}

	populate_recipient_list = {
		scope:actor.top_liege = {
			every_independent_ruler = {
				limit = {
					is_ai = yes
					is_landed = yes
					is_physically_able = yes
					has_raised_armies = no
					in_diplomatic_range = scope:actor
					OR = {
						government_has_flag = government_is_clan
						government_has_flag = government_is_tribal
					}
					culture = { has_cultural_pillar = heritage_turkic }
					highest_held_title_tier >= tier_county
					NOR = {
						is_at_war_with = scope:actor
						is_at_war_with = scope:recipient
						is_allied_to = scope:recipient
						has_truce = scope:recipient
						this = scope:actor
						this = scope:recipient
					}
				}
				add_to_list = characters
			}
		}
	}
	
	send_options_exclusive = no

	send_option = {
		flag = war_support
		localization = fp3_request_turkic_invasion_interaction_option_join_war
		current_description = fp3_request_turkic_invasion_interaction_option_join_war_desc
		is_shown = { exists = scope:secondary_recipient }
		is_valid = {
			scope:actor = {
				NOR = {
					is_at_war_with = scope:recipient
					is_allied_to = scope:recipient
					has_truce = scope:recipient
				}
			}
		}
	}

	send_option = {
		flag = financial_support
		localization = fp3_request_turkic_invasion_interaction_option_more_gold
		current_description = fp3_request_turkic_invasion_interaction_option_more_gold_desc
		is_shown = { exists = scope:secondary_recipient }
		is_valid = {
			custom_description = {
				text = fp3_please_dont_give_me_negative_gold
				fp3_request_invasion_troop_value_estimate > 0
			}
		}
	}

	cost = {
		gold = {
			value = {
				value = scope:secondary_recipient.max_military_strength
				divide = 20
				round = yes
			}
			add = {
				if = {
					limit = { scope:financial_support = yes }
					add = fp3_request_invasion_troop_value_estimate
				}
			}
			if = {
				limit = {
					scope:recipient = { has_title = title:d_sunni }
					struggle:persian_struggle = { has_struggle_phase_parameter = cheaper_incursions_vs_caliph }
				}
				divide = 2
			}
		}
	}

	on_accept = {
		if = {
			limit = { exists = scope:secondary_recipient }

			scope:recipient = {
				if = {
					limit = {
						any_realm_county = {
							NOT = { this = scope:recipient.capital_county }
							any_neighboring_county = { holder.top_liege = scope:secondary_recipient.top_liege }
							any_county_province = { geographical_region = world_persian_empire }
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
					}
					random_realm_county = {
						limit = {
							NOT = { this = scope:recipient.capital_county }
							any_neighboring_county = { holder.top_liege = scope:secondary_recipient.top_liege }
							any_county_province = { geographical_region = world_persian_empire }
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
						save_scope_as = invasion_target_title
					}
				}
				else_if = {
					limit = {
						any_realm_county = {
							NOT = { this = scope:recipient.capital_county }
							any_neighboring_county = {
								any_county_province = { geographical_region = world_persian_empire }
								NOT = {
									holder.top_liege = prev.holder.top_liege
								}
							}
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
					}
					random_realm_county = {
						limit = {
							NOT = { this = scope:recipient.capital_county }
							any_neighboring_county = {
								any_county_province = { geographical_region = world_persian_empire }
								NOT = {
									holder.top_liege = prev.holder.top_liege
								}
							}
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
						save_scope_as = invasion_target_title
					}
				}
				else_if = {
					limit = {
						any_realm_county = {
							NOT = { this = scope:recipient.capital_county }
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
					}
					random_realm_county = {
						limit = {
							NOT = { this = scope:recipient.capital_county }
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
						save_scope_as = invasion_target_title
					}
				}
				else_if = {
					limit = {
						any_realm_county = {
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
					}
					random_realm_county = {
						limit = {
							NOR = {
								holder = scope:actor
								holder = { is_vassal_or_below_of = scope:actor }
							}
						}
						save_scope_as = invasion_target_title
					}
				}
				else = {
					random_realm_county = { save_scope_as = invasion_target_title }
				}
				
				scope:invasion_target_title.duchy = { save_scope_as = text_title }

				hidden_effect = { # Not worth cluttering the UI with
					add_opinion = {
						modifier = fp3_incited_conflict
						target = scope:actor
					}
					add_opinion = {
						modifier = declared_war
						target = scope:secondary_recipient
					}
				}
			}

			scope:secondary_recipient = {
				add_gold = {
					value = {
						value = scope:secondary_recipient.max_military_strength
						divide = 20
						round = yes
					}
					add = {
						if = {
							limit = { scope:financial_support = yes }
							add = fp3_request_invasion_troop_value_estimate
						}
					}
					if = {
						limit = {
							scope:recipient = { has_title = title:d_sunni }
							struggle:persian_struggle = { has_struggle_phase_parameter = cheaper_incursions_vs_caliph }
						}
						divide = 2
					}
				}
				start_war = {
					casus_belli = fp3_turkic_invasion_cb
					target = scope:recipient
					target_title = scope:invasion_target_title.duchy
				}
				custom_tooltip = fp3_turkic_invasion_cb_result_tt
				if = {
					limit = {
						scope:financial_support = yes
					}
					trigger_event = fp3_misc_decisions.0006
					show_as_tooltip = {
						spawn_army = {
							men_at_arms = {
								type = horse_archers
								stacks = fp3_request_invasion_troop_value_bonus_troops
							}
							men_at_arms = {
								type = light_horsemen
								stacks = fp3_request_invasion_troop_value_bonus_troops
							}
							levies = fp3_request_invasion_troop_value_bonus_levy_troops
							location = capital_province
							uses_supply = yes
							inheritable = no
							name = turkic_tribal_event_troops
						}
					}
				}
				if = {
					limit = { scope:war_support = yes }
					custom_tooltip = fp3_request_turkic_invasion_interaction_option_join_war_outcome_tooltip
					trigger_event = fp3_misc_decisions.0005 
				}
			}
			
			scope:actor = {
				hidden_effect = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = fp3_request_turkic_invasion_interaction_accept_notification.t
						left_icon = scope:secondary_recipient
						right_icon = scope:recipient
	
						show_as_tooltip = {
							custom_tooltip = fp3_request_turkic_invasion_interaction_accept_notification.desc
						}
					}
				}
			}
			
			scope:recipient = {
				add_character_flag = {
					flag = incursion_ai_pacing
					years = 5
				}
				hidden_effect = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = fp3_request_turkic_invasion_interaction_accept_notification.t
						left_icon = scope:secondary_recipient
						right_icon = scope:actor
	
						show_as_tooltip = {
							custom_tooltip = fp3_request_turkic_invasion_interaction_accept_target_notification.desc
						}
					}
				}
			}
		}
	}

	use_diplomatic_range = yes

	ai_accept = {
		base = 0

		# 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_2
			trigger = {
				scope:secondary_recipient = {
					has_relation_rival = scope:actor
					NOT = { has_relation_nemesis = scope:actor }
				}
			}
			add = -10
		}
		modifier = { # Nemesis modifier.
			desc = offer_vassalization_interaction_aibehavior_nemesis_tt_2
			trigger = {
				scope:secondary_recipient = {
					has_relation_nemesis = scope:actor
				}
			}
			add = -30
		}
		modifier = { # Same Dynasty modifier.
			desc = offer_vassalization_interaction_aibehavior_dynasty_tt_2
			trigger = {
				scope:secondary_recipient = {
					dynasty = scope:actor.dynasty
				}
			}
			add = 10
		}

		# Compare Opinion modifier
		opinion_modifier = {
			who = scope:secondary_recipient
			opinion_target = scope:actor
			multiplier = 0.25
		}

		# Pledged to help in invasion
		modifier = {
			add = {
				value = scope:actor.max_military_strength
				divide = 70
				round = yes
			}
			desc = HAS_YOUR_SUPPORT_REASON
			scope:war_support = yes
		}

		# Relative power (taken from PT interaction)
		modifier = { # A lot less levies than scope:secondary_recipient
			add = -100
			scope:secondary_recipient.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_far_weaker_value
			desc = fp3_RTI_AI_RECIPIENT_IS_MUCH_WEAKER
		}
		modifier = { # Few less levies than scope:secondary_recipient
			add = -75
			scope:secondary_recipient.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_far_weaker_value
			scope:secondary_recipient.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_somewhat_weaker_value
			desc = fp3_RTI_AI_RECIPIENT_IS_SOMEWHAT_WEAKER
		}
		modifier = { # Roughly equal levies with scope:secondary_recipient
			add = 10
			scope:secondary_recipient.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_somewhat_weaker_value
			scope:secondary_recipient.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_somewhat_stronger_value
			desc = fp3_RTI_AI_RECIPIENT_IS_ROUGHLY_EQUAL
		}
		modifier = { # More levies than scope:secondary_recipient
			add = 50
			scope:secondary_recipient.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_somewhat_stronger_value
			scope:secondary_recipient.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_far_stronger_value
			desc = fp3_RTI_AI_RECIPIENT_IS_SOMEWHAT_STRONGER
		}
		modifier = { # Many more levies than scope:secondary_recipient
			add = 75
			scope:secondary_recipient.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_far_stronger_value
			desc = fp3_RTI_AI_RECIPIENT_IS_MUCH_STRONGER
		}

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

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

		# Offered proportional gold
		modifier = {
			add = 85
			scope:financial_support = yes
			desc = GOLD_REASON
		}

		# AI value mod
		ai_value_modifier = {
			ai_greed = 0.5
			ai_boldness = 0.25
			min = 0
			max = 25
		}
	}
	#auto_accept = {
	#	scope:recipient = { is_ai = yes }
	#}
	can_send_despite_rejection = no
	ai_frequency = 24
	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = liege
	}
	ai_potential = {
		has_trait = fp3_struggle_detractor
		is_at_war = no
		NOT = { ai_has_economical_boom_personality = yes }
		ai_greed <= 25
		highest_held_title_tier > tier_barony
		NOT = { culture = { has_cultural_pillar = heritage_turkic } }
	}
	ai_will_do = {
		base = 0

		modifier = {
			add = 50
			scope:recipient = {
				has_trait = fp3_struggle_supporter
				scope:recipient.max_military_strength >= scope:actor.max_military_strength
			}
		}

		modifier = {
			add = 50
			title:d_sunni.holder ?= scope:recipient
		}

		modifier = {
			add = 100
			scope:recipient = {
				has_relation_rival = scope:actor
			}
		}

		modifier = {
			factor = 0.1
			scope:actor.liege ?= scope:recipient
		}

		modifier = {
			factor = 0
			scope:recipient = {
				has_character_flag = incursion_ai_pacing
			}
		}

		modifier = {
			factor = 0
			scope:recipient = {
				is_at_war = yes
				is_ai = no
			}
		}

		modifier = {
			factor = 0
			scope:recipient = {
				is_ai = no
				is_liege_or_above_of = scope:actor
			}
		}

		modifier = {
			factor = 0
			scope:recipient = {
				OR = {
					has_relation_friend = scope:actor
					has_relation_lover = scope:actor
					opinion = {
						target = scope:actor
						value >= 45
					}
				}
			}
		}
	}
}

###################################
# Challenge House Head
# By Hugo Cortell
# Reworked by Joe Parkin
###################################
fp3_challenge_house_head_interaction = {
	icon = house_unity_sword
	category = interaction_category_hostile
	common_interaction = no
	ai_min_reply_days = 4
	ai_max_reply_days = 9
	popup_on_receive = yes
	pause_on_receive = yes
	use_diplomatic_range = no

	interface_priority = 107

	desc = fp3_challenge_house_head_interaction_desc

	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:diplomacy = yes }
				desc = fp3_challenge_house_head_interaction_notification_diplomacy
			}
			desc = fp3_challenge_house_head_interaction_notification
		}
	}
	
	greeting = positive

	ai_maybe = yes
	
	cooldown_against_recipient = { years = 10 }

	is_shown = {
		has_fp3_dlc_trigger = yes
		scope:actor = {
			NOT = { this = scope:recipient }
			government_has_flag = government_is_clan
		}
		scope:actor.house ?= {
			house_head = scope:recipient
			OR = {
				has_house_unity_parameter = unity_can_challenge_house_head
				scope:actor = {
					any_character_struggle = { has_struggle_phase_parameter = unlocks_challenge_house_head_for_clans }
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_at_war = no
			is_available_adult = yes
		}
		scope:recipient = {
			is_at_war = no
			is_available_adult = yes
			# Stop challenges from barons and counts against kings
			custom_tooltip = {
				text = fp3_challenge_house_head_interaction_tier_tt
				tier_difference = {
					target = scope:actor
					value <= 1
				}
			}
			# Give some respite after a recent challenge
			custom_tooltip = {
				text = fp3_challenge_house_head_interaction_recent_tt
				NOT = { has_character_flag = fp3_challenge_house_head_recent_flag }
			}
		}
	}
	
	auto_accept = {
		trigger_if = {
			limit = {
				scope:recipient = { is_ruler = no }
			}
			scope:recipient = { is_ruler = no }
		}
		trigger_else_if = {
			limit = { scope:hook = yes }
			custom_description = {
				text = "spending_hook"
				subject = scope:actor
				object = scope:recipient
				scope:hook = yes
			}
		}
		trigger_else_if = {
			limit = {
				scope:actor = { is_heir_of = scope:recipient }
			}
			custom_tooltip = {
				text = fp3_challenge_house_head_interaction_heir_bonus_desc
				scope:actor = { is_heir_of = scope:recipient }
			}
		}
		trigger_else = { always = no }
	}
	
	can_send = {
		scope:actor = {
			# Do not allow house heads who accepted a challenge to ping pong back and forth
			custom_tooltip = {
				text = fp3_challenge_house_head_interaction_accepted_tt
				NOT = { has_character_flag = accepted_house_head_challenge_flag }
			}
		}
		scope:recipient = {
			# Do not allow multiple challenges at the same time
			custom_tooltip = {
				text = fp3_challenge_house_head_interaction_ongoing_tt
				NOT = { has_character_flag = ongoing_house_head_challenge_flag }
			}
		}
	}

	on_send = {
		scope:recipient = {
			add_character_flag = { # stop multiple challenges at same time
				flag = ongoing_house_head_challenge_flag
				months = 1
			}
			add_character_flag = { # stop AI constantly challenging players
				flag = ai_cooldown_house_head_challenge_flag
				years = 5
			}
		}
	}

	on_accept = {
		scope:recipient = { save_scope_as = house_head }
		if = {
			limit = {
				scope:recipient = {
					is_ruler = no
				}
			}
			scope:actor.house = { set_house_head = scope:actor }
		}
		else = {
			scope:actor = {
				save_scope_as = house_challenger
				if = { # Hook consumption
					limit = { scope:hook = yes }
					use_hook = scope:house_head
				}
				if = {
					limit = { scope:diplomacy = yes }
					custom_tooltip = fp3_challenge_house_head_interaction_tooltip_actor.diplomacy
					trigger_event = fp3_misc_decisions.0026
					scope:house_head = { custom_tooltip = fp3_challenge_house_head_interaction_tooltip_recipient.diplomacy }
					house = {
						add_clan_unity_interaction_effect = {
							CHARACTER = scope:house_challenger
							TARGET = scope:house_head
							VALUE = minor_unity_loss
							DESC = clan_unity_challenged_to_duel.desc
							REVERSE_NON_HOUSE_TARGET = yes
						}
					}
				}
				else = {
					if = {
						limit = { scope:lethal = no }
						custom_tooltip = fp3_challenge_house_head_interaction_tooltip_actor.a
						scope:house_head = { custom_tooltip = fp3_challenge_house_head_interaction_tooltip_recipient.b }
						house = {
							add_clan_unity_interaction_effect = {
								CHARACTER = scope:house_challenger
								TARGET = scope:house_head
								VALUE = minor_unity_loss
								DESC = clan_unity_challenged_to_duel.desc
								REVERSE_NON_HOUSE_TARGET = yes
							}
						}
					} 
					else = {
						custom_tooltip = fp3_challenge_house_head_interaction_tooltip_actor.a.lethal
						scope:house_head = { custom_tooltip = fp3_challenge_house_head_interaction_tooltip_recipient.b.lethal }
						house = {
							add_clan_unity_interaction_effect = {
								CHARACTER = scope:house_challenger
								TARGET = scope:house_head
								VALUE = major_unity_loss
								DESC = clan_unity_challenged_to_duel.desc
								REVERSE_NON_HOUSE_TARGET = yes
							}
						}
					}
					trigger_event = fp3_misc_decisions.0022
				}
			}
			if = {
				limit = { scope:diplomacy = no }
				if = {
					limit = { scope:lethal = no }
					custom_tooltip = fp3_challenge_house_head_interaction_duel_tooltip_warning
				}
				else = { custom_tooltip = fp3_challenge_house_head_interaction_duel_tooltip_warning_lethal }
			}
		}
	}

	on_decline = { # Get letter of rejection, to further escalate the situation if the player wills it
		scope:actor = { trigger_event = fp3_misc_decisions.0025 }
		scope:recipient = {
			add_prestige = major_prestige_loss
			remove_character_flag = ongoing_house_head_challenge_flag
		}
	}

	send_option = {
		flag = diplomacy
		localization = CHALLENGE_HOUSE_HEAD_DIPLOMACY_DUEL
		is_valid = {
			custom_tooltip = {
				text = CHALLENGE_HOUSE_HEAD_LETHAL_DUEL_INVALID_TT
				NOT = { scope:lethal = yes }
			}
		}
	}
	send_option = {
		flag = lethal
		localization = CHALLENGE_HOUSE_HEAD_LETHAL_DUEL
		is_valid = {
			scope:actor = { has_relation_rival = scope:recipient }
			custom_tooltip = {
				text = CHALLENGE_HOUSE_HEAD_LETHAL_DUEL_INVALID_TT
				NOT = { scope:diplomacy = yes }
			}
		}
	}
	send_option = {
		flag = hook
		localization = GENERIC_SPEND_A_HOOK
		is_valid = {
			scope:actor = { has_usable_hook = scope:recipient }
		}
	}
	should_use_extra_icon = { scope:actor = { has_usable_hook = scope:recipient } }
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	send_options_exclusive = no

	# AI
	
	ai_accept = {
		base = 25
		modifier = {
			add = -50
			scope:lethal = yes
			desc = AI_AVOID_LETHAL_DUEL
		}
		modifier = {
			add = 15
			scope:recipient = { has_trait = ambitious }
			desc = INTERACTION_AMBITIOUS
		}
		modifier = {
			add = 25
			scope:recipient = { has_trait = brave }
			desc = INTERACTION_BRAVE
		}
		modifier = {
			add = {
				value = -25
				if = {
					limit = { scope:lethal = yes }
					multiply = 2
				}
			}
			scope:recipient = { has_trait = craven }
			desc = INTERACTION_CRAVEN
		}
		modifier = {
			scope:recipient.ai_greed != 0
			add = { # Greedy people won't risk a position of power, even if their prestige suffers
				value = scope:recipient.ai_greed
				multiply = -1
				max = 35
				min = -35
			}
			desc = AI_VALUE_MODIFIER_GREED
		}
		modifier = {
			scope:recipient.ai_honor != 0
			add = {
				value = scope:recipient.ai_honor
				max = 75
				min = -35
			}
			desc = AI_VALUE_MODIFIER_HONOR
		}
		modifier = {
			scope:recipient.ai_energy != 0
			add = {
				value = scope:recipient.ai_energy
				max = 10
				min = -10
			}
			desc = AI_VALUE_MODIFIER_ENERGY
		}
		modifier = {
			NOT = { scope:diplomacy = yes }
			add = {
				value = scope:recipient.prowess
				subtract = scope:actor.prowess
				multiply = 2
			}
			desc = CTSC_RELATIVE_PROWESS
		}
		modifier = {
			scope:lethal = yes
			scope:recipient = {
				OR = {
					is_parent_of = scope:actor
					is_child_of = scope:actor
				}
				NOT = { has_relation_rival = scope:actor }
			}
			desc = AI_AVOID_LETHAL_DUEL_WITH_FAMILY
		}
		opinion_modifier = {
			opinion_target = scope:actor
			multiplier = 0.5
		}
	}
	
	ai_frequency = 24
	
	ai_target_quick_trigger = {
		adult = yes
	}
	
	ai_potential = {
		NOR = {
			has_trait = craven
			has_trait = content
		}
		government_has_flag = government_is_clan
		is_physically_able = yes 
	}

	ai_targets = {
		ai_recipients = dynasty
		ai_recipients = family
		max = 10
	}

	ai_targets = {
		ai_recipients = liege
		ai_recipients = head_of_faith
		ai_recipients = hooked_characters
		ai_recipients = scripted_relations
	}

	ai_targets = {
		ai_recipients = peer_vassals
		ai_recipients = vassals
		ai_recipients = neighboring_rulers
		max = 30
	}
	
	ai_will_do = {
		base = 5
		# Base requirements for AI
		modifier = { 
			add = -1000
			NOR = {
				AND = {
					is_independent_ruler = yes
					highest_held_title_tier >= scope:recipient.highest_held_title_tier
				}
				is_powerful_vassal = yes
				is_close_or_extended_family_of = scope:recipient
				opinion = {
					target = scope:recipient
					value >= 25
				}
			}
		}
		# Stop AI constantly challenging players
		modifier = { 
			scope:recipient = { has_character_flag = ai_cooldown_house_head_challenge_flag }
			add = -1000
		}
		# Tiers
		modifier = {
			add = 10
			piety_level >= scope:recipient.piety_level
		}
		modifier = {
			add = 10
			prestige_level >= scope:recipient.prestige_level
		}
		modifier = {
			add = 10
			highest_held_title_tier >= scope:recipient.highest_held_title_tier
		}
		# Traits
		modifier = {
			add = 30
			has_trait = ambitious
		}
		modifier = {
			add = 30
			has_trait = brave
		}
		modifier = {
			add = -30
			has_trait = craven
		}
		# Dread
		modifier = {
			add = -25
			has_dread_level_towards = {
				target = scope:recipient
				level >= 1
			}
		}
		modifier = {
			add = -25
			has_dread_level_towards = {
				target = scope:recipient
				level >= 2
			}
		}
		# Personality
		modifier = {
			ai_boldness > 0
			add = {
				value = ai_boldness
				divide = 2
				min = 1
			}
		}
		modifier = {
			ai_greed >= 50
			add = {
				value = ai_greed
				divide = 2
			}
		}
		modifier = {
			ai_sociability = 100
			add = 10
		}
	}
}


###################################
# Change struggle allegiance
###################################
fp3_change_allegiance = {
	interface_priority = 30
	category = interaction_category_diplomacy
	common_interaction = yes
	desc = {
		first_valid = {
			# Become a supporter.
			triggered_desc = {
				trigger = {
					scope:actor = { has_trait = fp3_struggle_supporter }
				}
				desc = fp3_change_allegiance.desc.actor_is_supporter
			}
			# Become a detractor.
			triggered_desc = {
				trigger = {
					scope:actor = { has_trait = fp3_struggle_detractor }
				}
				desc = fp3_change_allegiance.desc.actor_is_detractor
			}
			desc = fp3_change_allegiance.desc.fallback
		}
	}
	icon = change_allegiance

	greeting = positive

	should_use_extra_icon = { scope:actor = { has_usable_hook = scope:recipient } }
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	send_options_exclusive = no

	ai_maybe = yes

	is_shown = {
		has_fp3_dlc_trigger = yes
		# There has to be a caliph around to sway on.
		## Arguably narratively not, but mechanically, everything will break if we don't.
		exists = title:d_sunni.holder
		NOT = { scope:recipient = scope:actor }
		scope:actor = {
			any_character_struggle = { is_struggle_type = persian_struggle }
			OR = {
				# Detractors are made from supporters.
				AND = {
					has_trait = fp3_struggle_detractor
					scope:recipient = { has_trait = fp3_struggle_supporter }
				}
				# Supporters are made from detractors.
				AND = {
					has_trait = fp3_struggle_supporter
					scope:recipient = { has_trait = fp3_struggle_detractor }
				}
				# Neutrals can be asked either way, should they exist.
				AND = {
					OR = {
						has_trait = fp3_struggle_detractor
						has_trait = fp3_struggle_supporter
					}
					scope:recipient = {
						NOR = {
							has_trait = fp3_struggle_detractor
							has_trait = fp3_struggle_supporter
						}
					}
				}
			}
		}
		scope:recipient = {
			any_character_struggle = { is_struggle_type = persian_struggle }
			# You cannot ask the Caliph!
			NOT = { this = title:d_sunni.holder }
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			# We want to disable some stuff for supporters who can't be persuaded away from the caliph.
			trigger_if = {
				limit = { has_trait = fp3_struggle_supporter }
				# Can't be allied to the caliph.
				NOT = { is_allied_to = title:d_sunni.holder }
				# Can't be strong hooked _by_ the caliph.
				NOT = {
					title:d_sunni.holder = { has_strong_hook = prev }
				}
				# Can't have only recently changed to become a supporter.
				custom_tooltip = {
					text = fp3_change_allegiance.tt.recently_voluntarily_became_supporter
					OR = {
						NOT = { has_variable = last_explicit_friday_prayer_caliph }
						NOT = { var:last_explicit_friday_prayer_caliph = title:d_sunni.holder }
					}
				}
			}
			# Likewise, some circumstances can force them to stay as a detractor.
			trigger_if = {
				limit = { has_trait = fp3_struggle_detractor }
				# Can't have only recently changed to become a detractor.
				custom_tooltip = {
					text = fp3_change_allegiance.tt.recently_voluntarily_became_detractor
					OR = {
						NOT = { has_variable = last_explicit_friday_prayer_caliph }
						NOT = { var:last_explicit_friday_prayer_caliph = title:d_sunni.holder }
					}
				}
				# Can't have been forced into becoming a detractor by the caliph overstepping.
				custom_tooltip = {
					text = fp3_change_allegiance.tt.recently_forced_to_detractor
					OR = {
						NOT = { has_variable = last_explicit_defied_caliph }
						NOT = { var:last_explicit_defied_caliph = title:d_sunni.holder }
					}
				}
			}
			# Plus some general stuff.
			is_at_war = no
			# Can't have recently been convinced to change their minds.
			custom_tooltip = {
				text = fp3_change_allegiance.tt.recently_changed_mind
				NOT = { has_character_flag = fp3_change_allegiance_recipient_cooldown }
			}
			custom_tooltip = {
				text = fp3_change_allegiance.tt.recently_refused
				NOT = { has_character_flag = fp3_change_allegiance_refused_cooldown }
			}
			# They must be an orthodox Sunni.
			custom_tooltip = {
				text = fp3_change_allegiance.tt.hof_is_d_sunni
				faith.religious_head ?= title:d_sunni.holder
			}
		}
		scope:actor = { is_available_adult = yes }
	}

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

	ai_accept = {
		base = 0

		# Those who already have a stance are more set in their ways.
		## Detractors
		modifier = {
			add = -25
			scope:actor = { has_trait = fp3_struggle_supporter }
			scope:recipient = { has_trait = fp3_struggle_detractor }
			desc = INTERACTION_DETRACTOR
		}
		## Supporters
		modifier = {
			add = -25
			scope:actor = { has_trait = fp3_struggle_detractor }
			scope:recipient = { has_trait = fp3_struggle_supporter }
			desc = INTERACTION_SUPPORTER
		}

		# Opinions
		## Opinion of you
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = {
				value = 1
				# Despawn this if you're not the caliph.
				if = {
					limit = { scope:actor = title:d_sunni.holder }
					multiply = 0
				}
			}
		}
		## Opinion of the caliph
		opinion_modifier = {
			who = scope:recipient
			opinion_target = title:d_sunni.holder
			multiplier = {
				value = 0.5
				# If you're the caliph, you can take the full amount.
				if = {
					limit = { scope:actor = title:d_sunni.holder }
					add = 0.5
				}
			}
		}
		# Traits.
		## Become supporter.
		### ++ Zealous.
		modifier = {
			add = 25
			desc = ASK_FOR_CONVERSION_RECIPIENT_IS_ZEALOUS
			scope:actor = { has_trait = fp3_struggle_supporter }
			scope:recipient = { has_trait = zealous }
		}
		## Become detractor.
		### -- Cynical.
		modifier = {
			add = 25
			desc = ASK_FOR_CONVERSION_RECIPIENT_IS_CYNICAL
			scope:actor = { has_trait = fp3_struggle_detractor }
			scope:recipient = { has_trait = cynical }
		}
		## Neutral.
		### ++ Fickle.
		modifier = {
			add = 25
			desc = ASK_FOR_CONVERSION_RECIPIENT_IS_FICKLE
			scope:recipient = { has_trait = fickle }
		}
		### + Disloyal.
		modifier = {
			add = 15
			desc = HAS_LOYAL_TRAIT
			scope:recipient = { has_trait = disloyal }
		}
		### - Loyal.
		modifier = {
			add = 15
			desc = HAS_DISLOYAL_TRAIT
			scope:recipient = { has_trait = loyal }
		}
		### -- Stubborn.
		modifier = {
			add = 25
			desc = ASK_FOR_CONVERSION_RECIPIENT_IS_STUBBORN
			scope:recipient = { has_trait = stubborn }
		}
		# Diplomacy
		## Scope:actor's for convincing
		modifier = {
			add = {
				value = scope:actor.diplomacy
				multiply = 5
			}
			desc = INTERACTION_DIPLOMACY_ACTOR
		}
		## Scope:recipient's for resisting
		modifier = {
			add = {
				value = scope:recipient.diplomacy
				multiply = -3
			}
			desc = INTERACTION_DIPLOMACY_RECIPIENT
		}
	}

	cost = {
		prestige = medium_prestige_value
	}

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

	on_accept = {
		# Use our hook if we have to.
		if = {
			limit = { always = scope:hook }
			scope:actor = {
				use_hook = scope:recipient
			}
		}
		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_gain
			DESC = clan_unity_changed_alignment.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		# Run the actual effects.
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = recipient_joined_your_side_title
				left_icon = scope:recipient

				scope:recipient = {
					if = {
						limit = { scope:actor = { has_trait = fp3_struggle_detractor } }
						if = {
							limit = { has_trait = fp3_struggle_supporter }
							remove_trait = fp3_struggle_supporter
						}
						add_trait_force_tooltip = fp3_struggle_detractor

						# Catalyst
						hidden_effect = {
							if = {
								limit = {
									scope:actor = {
										any_character_struggle	 = {
											involvement = involved
											phase_has_catalyst = catalyst_turn_supporter_into_detractor
										}
									}
								}
								every_character_struggle = {
									involvement = involved
									activate_struggle_catalyst = {
										catalyst = catalyst_turn_supporter_into_detractor
										character = scope:actor
									}
									log_debug_variable_for_persian_struggle_effect = { VAR = unrest_catalyst_turn_supporter_into_detractor }
								}
							}
						}
					}
					else = {
						if = {
							limit = { has_trait = fp3_struggle_detractor }
							remove_trait = fp3_struggle_detractor
						}
						add_trait_force_tooltip = fp3_struggle_supporter

						# Catalyst
						hidden_effect = {
							if = {
								limit = {
									scope:actor = {
										any_character_struggle	 = {
											involvement = involved
											phase_has_catalyst = catalyst_turn_detractor_into_supporter
										}
									}
								}
								every_character_struggle = {
									involvement = involved
									activate_struggle_catalyst = {
										catalyst = catalyst_turn_detractor_into_supporter
										character = scope:actor
									}
									log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_turn_detractor_into_supporter }
								}
							}
						}
					}
				}
			}
		}
		# Aaaaand flag scope:recipient as on cooldown for a while.
		scope:recipient = {
			set_variable = {
				name = fp3_change_allegiance_recipient_cooldown
				years = 20
			}
		}
	}

	on_decline = {
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_loss
			DESC = clan_unity_refused_alignment.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		scope:actor = {
			if = {
				limit = {
					scope:actor = { has_trait = fp3_struggle_detractor }
				}
				send_interface_toast = {
					type = event_toast_effect_bad
					title = recipient_refused_to_join_your_side_title
					left_icon = scope:recipient
					progress_towards_rival_effect = {
						REASON = rival_rejected_switching_to_struggle_detractor
						CHARACTER = scope:recipient
						OPINION = default_rival_opinion
					}
				}
			}
			else = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = recipient_refused_to_join_your_side_title
					left_icon = scope:recipient
					progress_towards_rival_effect = {
						REASON = rival_rejected_switching_to_struggle_supporter
						CHARACTER = scope:recipient
						OPINION = default_rival_opinion
					}
				}
			}
		}
		scope:recipient = {
			set_variable = {
				name = fp3_change_allegiance_refused_cooldown
				years = 20
			}
		}
	}
	ai_frequency = 120

	ai_potential = {
		primary_title.tier >= tier_duchy
		OR = {
			has_trait = fp3_struggle_detractor
			has_trait = fp3_struggle_supporter
		}
	}

	ai_targets = {
		ai_recipients = liege
		ai_recipients = neighboring_rulers
		ai_recipients = peer_vassals
		ai_recipients = vassals
		ai_recipients = top_realm_domicile_owners
		max = 10
	}

	ai_will_do = {
		base = -50 # Let's limit it to specific character to avoid pure chaos

		###############
		## BASE
		###############

		# AI Values
		## Detractors run off of lacking zeal
		modifier = {
			add = {
				value = ai_zeal
				multiply = -2
			}
			has_trait = fp3_struggle_detractor
			ai_zeal <= -1
		}
		## Supporters run off of turbo-charged zeal
		modifier = {
			add = {
				value = ai_zeal
				multiply = 2
			}
			has_trait = fp3_struggle_supporter
			ai_zeal >= 1
		}

		# The caliph wants to use this all the time.
		modifier = {
			has_title = title:d_sunni
			add = 200
		}
		
		###############
		## TRAITS
		###############

		# schemer
		modifier = {
			add = 15
			has_trait = schemer
		}

		# theologian
		modifier = {
			add = 25
			has_trait = theologian
		}

		# intrigue education
		modifier = {
			add = 15
			has_trait = education_intrigue
		}

		# learning education
		modifier = {
			add = 25
			has_trait = education_learning
		}

		################
		## RELATIONSHIP
		################

		# more likely to do it against friend and lover
		modifier = {
			add = 50
			scope:recipient = {
				OR = {
					has_relation_friend = scope:actor
					has_relation_lover = scope:actor
					has_relation_soulmate = scope:actor
					has_relation_best_friend = scope:actor
				}
			}
		}

		# boost against liege for Ambitious
		modifier = {
			add = 50
			scope:actor = {
				has_trait = ambitious
				is_vassal_of = scope:recipient
			}
		}

		# never target your rivals
		modifier = {
			add = -1000
			scope:recipient = {
				OR = {
					has_relation_rival = scope:actor
					has_relation_nemesis = scope:actor
				}
			}
		}

		################
		## STRUGGLE
		################

		modifier = {
			add = 100
			OR = {
				AND = {
					scope:recipient = { has_trait = fp3_struggle_supporter }
					scope:actor = {
						has_trait = fp3_struggle_detractor
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_turn_supporter_into_detractor
						}
					}
				}
				AND = {
					scope:recipient = { has_trait = fp3_struggle_detractor }
					scope:actor = {
						has_trait = fp3_struggle_supporter
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_turn_detractor_into_supporter
						}
					}
				}
			}
		}
	}
}


###################################
# Request honorary titles
###################################
fp3_request_honorary_titles_interaction = { # todo_cd_polish add different option to pick different titles
	interface_priority = 30
	category = interaction_category_religion
	common_interaction = yes
	desc = fp3_request_honorary_titles_interaction_desc
	icon = honorary_titles

	greeting = positive

	auto_accept = yes

	cooldown = { years = 10 }

	cost = {
		piety =  {
			value = major_piety_value
			if = {
				limit = { # cheaper for the actual liege
					any_vassal = {
						any_held_title = {
							this = title:d_sunni
						}
					}
				}
				subtract = medium_piety_value
			}
		}
	}

	is_shown = {
		fp3_reward_from_vassalization_ending = yes
	}

	is_valid = {
		# honorary titles should be unique
		custom_tooltip = {
			text = one_of_the_title_is_available_tt
			scope:actor = {
				house ?= {
					NAND = {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_intrigue_modifier
						}
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_scholarship_modifier
						}

						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_stewardship_modifier
						}
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_diplomacy_modifier
						}
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_piety_modifier
						}
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_prestige_modifier
						}
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_martial_modifier
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = intrigue_option
		localization = fp3_honorary_title_intrigue_modifier
		is_valid = {
			NOT = {
				scope:actor = {
					house ?= {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_intrigue_modifier
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = scholarship_option
		localization = fp3_honorary_title_scholarship_modifier
		is_valid = {
			NOT = {
				scope:actor = {
					house ?= {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_scholarship_modifier
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = stewardship_option
		localization = fp3_honorary_title_stewardship_modifier
		is_valid = {
			NOT = {
				scope:actor = {
					house ?= {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_stewardship_modifier
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = diplomacy_option
		localization = fp3_honorary_title_diplomacy_modifier
		is_valid = {
			NOT = {
				scope:actor = {
					house ?= {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_diplomacy_modifier
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = piety_option
		localization = fp3_honorary_title_piety_modifier
		is_valid = {
			NOT = {
				scope:actor = {
					house ?= {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_piety_modifier
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = prestige_option
		localization = fp3_honorary_title_prestige_modifier
		is_valid = {
			NOT = {
				scope:actor = {
					house ?= {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_prestige_modifier
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = martial_option
		localization = fp3_honorary_title_martial_modifier
		is_valid = {
			NOT = {
				scope:actor = {
					house ?= {
						any_house_member = {
				 			has_character_modifier = fp3_honorary_title_martial_modifier
						}
					}
				}
			}
		}
	}

	on_accept = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = new_honorary_title_acquired_title
				left_icon = scope:recipient

				switch = {
					trigger = yes
					scope:intrigue_option = {
						add_character_modifier = {
							modifier = fp3_honorary_title_intrigue_modifier
						}
					}
					scope:scholarship_option = {
						add_character_modifier = {
							modifier = fp3_honorary_title_scholarship_modifier
						}
					}
					scope:stewardship_option = {
						add_character_modifier = {
							modifier = fp3_honorary_title_stewardship_modifier
						}
					}
					scope:diplomacy_option = {
						add_character_modifier = {
							modifier = fp3_honorary_title_diplomacy_modifier
						}
					}
					scope:piety_option = {
						add_character_modifier = {
							modifier = fp3_honorary_title_piety_modifier
						}
					}
					scope:prestige_option = {
						add_character_modifier = {
							modifier = fp3_honorary_title_prestige_modifier
						}
					}
					scope:martial_option = {
						add_character_modifier = {
							modifier = fp3_honorary_title_martial_modifier
						}
					}
				}
			}
		}
	}

	ai_frequency = 60

	ai_potential = {
		any_vassal = {
			any_held_title = {
				this = title:d_sunni
			}
		}
	}

	ai_targets = {
		# For the AI, we limit it to neighboring_rulers
		ai_recipients = vassals
	}

	ai_will_do = {
		base = 100
	}
}


###################################
# Request house unity interference
###################################
fp3_request_house_unity_change_interaction = {
	interface_priority = 30
	category = interaction_category_religion
	common_interaction = yes
	desc = fp3_request_house_unity_change_interaction_desc
	icon = unity_harmonious

	greeting = positive


	auto_accept = yes

	cooldown = { years = 10 }

	cost = {
		piety = medium_piety_value
	}

	is_shown = {
		fp3_reward_from_vassalization_ending = yes
		scope:actor = {
			government_has_flag = government_is_clan
		}
	}

	is_valid = {
		# only valid for the actual liege
		custom_tooltip = {
			text = you_are_the_liege_of_the_caliph_tt
			scope:actor = {
				is_liege_or_above_of = scope:recipient
			}
			scope:recipient = {
				any_held_title = {
					this = title:d_sunni
				}
			}
		}
	}

	send_option = {
		flag = unity_gain
		localization = INCREASE_HOUSE_UNITY
		starts_enabled = { always = yes	}
	}

	send_option = {
		flag = unity_loss
		localization = DECREASE_HOUSE_UNITY
	}

	on_accept = {
		scope:recipient = { save_scope_as = unity_target }
		scope:actor = {
			save_scope_as = unity_character
			house = {
				add_unity_value = {
					value = {
						if = {
							limit = { scope:unity_gain = yes }
							add = medium_unity_gain
						}
						else_if = {
							limit = { scope:unity_loss = yes }
							add = medium_unity_loss
						}
					}
					character = root
					desc = clan_unity_caliph_interfered_with_unity.desc
				}
			}
		}
	}

	ai_frequency = 60

	ai_potential = {
		any_vassal = {
			any_held_title = {
				this = title:d_sunni
			}
		}
	}

	ai_targets = {
		# For the AI, we limit it to neighboring_rulers
		ai_recipients = vassals
	}

	ai_will_do = {
		base = 100
	}
}
