﻿# Request support from another character
request_appointment_support = {
	category = interaction_category_diplomacy
	common_interaction = yes
	interface_priority = 70

	desc = request_appointment_support_desc

	target_type = title
	target_filter = actor_top_liege_de_jure_titles

	populate_recipient_list = {
		scope:actor = {
			every_succession_appointment_invested_candidate = {
				limit = {
					is_alive = yes
					government_allows = administrative
					# Check that they are still valid to be appointed
					any_valid_title_to_grant_trigger = {
						CANDIDATE = this
						TOP_LIEGE = scope:actor.top_liege
					}
				}
				add_to_list = characters
			}
			# We limit how many recipients to check for the AI for performance, player gets the unabridged list
			if = {
				limit = {
					is_ai = no
				}
				house = {
					every_house_member = {
						limit = {
							government_allows = administrative
							any_valid_title_to_grant_trigger = {
								CANDIDATE = this
								TOP_LIEGE = scope:actor.top_liege
							}
						}
						add_to_list = characters
					}
				}
			}
		}
	}

	can_be_picked_title = {
		trigger_if = {
			limit = { exists = scope:secondary_recipient }
			is_appointment_valid_trigger = {
				TITLE = scope:target
				CANDIDATE = scope:secondary_recipient
				TOP_LIEGE = scope:actor.top_liege
			}
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
					scope:secondary_recipient = { has_succession_appointment_investors = scope:target }
				}
				scope:target = {
					any_succession_appointment_investors = {
						candidate = scope:secondary_recipient
						this = scope:actor
					}
				}
			}
		}
	}

	has_valid_target_showing_failures_only = {
		scope:target = {
			tier >= tier_barony
			exists = holder
			holder = {
				top_liege = scope:actor.top_liege
			}
		}
		scope:actor = {
			trigger_if = {
				limit = { is_ai = yes }
				any_succession_appointment_invested_title = {
					this = scope:target
				}
			}
		}
	}

	is_shown = {
		scope:actor = { government_allows = administrative }
		scope:recipient = {
			this != scope:actor
			government_allows = administrative
			top_liege = scope:actor.top_liege
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			house = {
				any_house_member = {
					government_allows = administrative
					any_valid_title_to_grant_trigger = {
						CANDIDATE = this
						TOP_LIEGE = scope:actor.top_liege
					}
					count >= 1
				}
			}
			is_in_civil_war = no
		}
		scope:recipient = {
			influence >= appointment_minor_influence_cost
			is_in_civil_war = no
		}
	}

	can_send = {
		trigger_if = {
			limit = { scope:hook = no }
			scope:actor = {
				gold >= medium_gold_value
			}
		}
	}

	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = GENERIC_SPEND_A_HOOK
	}
	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

	on_accept = {
		scope:actor = {
			if = {
				limit = {
					scope:hook = yes
					has_hook = scope:recipient
				}
				use_hook = scope:recipient
			}
		}
		if = {
			limit = {
				scope:hook = no
			}
			scope:actor = {
				pay_short_term_gold = {
					target = scope:recipient
					gold = medium_gold_value
				}
			}
		}
		if = {
			limit = { # Players gets to accept/decline a specific suggestion
				scope:recipient = { is_ai = no }
			}
			scope:recipient ={
				change_influence = {
					value = appointment_medium_influence_cost
					multiply = -1
				}
			}
			scope:target = {
				change_appointment_investment = {
					target = scope:secondary_recipient
					investor = scope:recipient
					value = appointment_score_medium_value
				}
			}
			scope:actor = {
				add_opinion = {
					target = scope:recipient
					modifier = thankful_opinion
					opinion = 30
				}
				trigger_event = ep3_interactions_events.0201
			}
		}
		else = { # The AI randomizes the amount of support
			scope:recipient = { custom_tooltip = request_appointment_support_tt }
			hidden_effect = {
				random_list = {
					60 = {
						scope:actor = {
							save_scope_value_as = {
								name = request_support_score
								value = appointment_score_minor_value
							}
							send_interface_message = {
								type = msg_candidacy_improved
								title = supported_candidacy_toast
								left_icon = scope:secondary_recipient
								right_icon = scope:target
								custom_tooltip = request_appointment_support_toast_tt
								scope:target = {
									change_appointment_investment = {
										target = scope:secondary_recipient
										investor = scope:recipient
										value = scope:request_support_score
									}
								}
							}
						}
						scope:recipient = {
							change_influence = {
								value = appointment_minor_influence_cost
								multiply = -1
							}
						}
					}
					30 = {
						scope:actor = {
							save_scope_value_as = {
								name = request_support_score
								value = appointment_score_medium_value
							}
							send_interface_message = {
								type = msg_candidacy_improved
								title = supported_candidacy_toast
								left_icon = scope:secondary_recipient
								right_icon = scope:target
								custom_tooltip = request_appointment_support_toast_tt
								scope:target = {
									change_appointment_investment = {
										target = scope:secondary_recipient
										investor = scope:recipient
										value = scope:request_support_score
									}
								}
							}
						}
						scope:recipient = {
							change_influence = {
								value = appointment_medium_influence_cost
								multiply = -1
							}
						}
						opinion_modifier = {
							who = scope:recipient
							opinion_target = scope:actor
							multiplier = 0.2
							min = 0
						}
						modifier = {
							factor = 1.5
							scope:recipient = { has_any_good_relationship_with_character_trigger = { CHARACTER = scope:actor } }
						}
						modifier = {
							factor = 2
							scope:recipient = { has_any_best_good_relationship_with_character_trigger = { CHARACTER = scope:actor } }
						}
						modifier = {
							factor = 0
							scope:recipient = { influence < appointment_medium_influence_cost }
						}
					}
					10 = {
						scope:actor = {
							save_scope_value_as = {
								name = request_support_score
								value = appointment_score_major_value
							}
							send_interface_message = {
								type = msg_candidacy_improved
								title = supported_candidacy_toast
								left_icon = scope:secondary_recipient
								right_icon = scope:target
								custom_tooltip = request_appointment_support_toast_tt
								scope:target = {
									change_appointment_investment = {
										target = scope:secondary_recipient
										investor = scope:recipient
										value = scope:request_support_score
									}
								}
							}
						}
						scope:recipient = {
							change_influence = {
								value = appointment_major_influence_cost
								multiply = -1
							}
						}
						opinion_modifier = {
							who = scope:recipient
							opinion_target = scope:actor
							multiplier = 0.3
							min = 0
						}
						modifier = {
							factor = 1.5
							scope:recipient = { has_any_good_relationship_with_character_trigger = { CHARACTER = scope:actor } }
						}
						modifier = {
							factor = 2
							scope:recipient = { has_any_best_good_relationship_with_character_trigger = { CHARACTER = scope:actor } }
						}
						modifier = {
							factor = 0
							scope:recipient = { influence < appointment_major_influence_cost }
						}
					}
				}
			}
			scope:actor = { trigger_event = ep3_interactions_events.0201 }
		}
	}

	on_decline = {
		scope:actor = {
			add_opinion = {
				target = scope:recipient
				modifier = disappointed_opinion
				opinion = -10
			}
			trigger_event = ep3_interactions_events.0202
		}
	}

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

	ai_accept = {
		base = -40

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

		# Admin
		modifier = {
			influence >= {
				value = monumental_influence_value
				multiply = 3
			}
			add = 20
			desc = plenty_of_influence_reason
		}
		modifier = {
			influence >= massive_influence_value
			influence < {
				value = monumental_influence_value
				multiply = 3
			}
			add = 10
			desc = has_influence_reason
		}
		modifier = {
			influence < major_influence_value
			add = -100
			desc = not_enough_influence_reason
		}
		modifier = { # Don't support actor if recipient holds the title (they should want to secure it for their own family first)
			exists = scope:target
			scope:target.holder = scope:recipient
			exists = scope:recipient.house
			exists = scope:secondary_recipient
			NOT = { scope:secondary_recipient.house ?= scope:recipient.house }
			add = -25
			desc = requesting_my_title_reason
		}
		modifier = { # Don't support actor if there is a close family member within the top 3 candidates
			scope:target ?= {
				any_title_heir = {
					scope:target ?= {
						place_in_line_of_succession = {
							target = prev
							value <= 3
						}
					}
					is_close_family_of = scope:recipient
				}
			}
			exists = scope:secondary_recipient
			NOT = { # Unless secondary_recipient is, in fact, one of these
				scope:secondary_recipient = {
					scope:target ?= {
						place_in_line_of_succession = {
							target = prev
							value <= 3
						}
					}
					is_close_family_of = scope:recipient
				}
			}
			add = -100
			desc = close_family_in_line_of_succession
		}
		modifier = { # More reluctant to lend support towards titles held by top liege
			exists = scope:target
			scope:target.holder = {
				is_independent_ruler = yes
				NOT = { this = scope:actor } # Assuming the top liege isn't the one doing the requesting
			}
			add = -25
			desc = targeting_top_liege_title
		}
		modifier = { # If you are from a powerful family, and they are not, they are more likely to accept
			scope:actor.house ?= {
				is_powerful_family = yes
			}
			scope:recipient.house ?= {
				is_powerful_family = no
				is_dominant_family = no
			}
			add = 15
			desc = your_house_is_powerful_reason
		}
		modifier = { # If you are from a dominant family, and they are not, they are more likely to accept
			scope:actor.house ?= {
				is_dominant_family = yes
			}
			scope:recipient.house ?= {
				is_dominant_family = no
			}
			add = 25
			desc = your_house_is_dominant_reason
		}
		modifier = { # If they are from a powerful family, and you are not, they are more likely to refuse
			scope:recipient.house ?= {
				is_powerful_family = yes
			}
			scope:actor.house ?= {
				is_powerful_family = no
				is_dominant_family = no
			}
			add = -15
			desc = their_house_is_powerful_reason
		}
		modifier = { # If they are from a dominant family, and you are not, they are more likely to refuse
			scope:recipient.house ?= {
				is_dominant_family = yes
			}
			scope:actor.house ?= {
				is_dominant_family = no
			}
			add = -25
			desc = their_house_is_dominant_reason
		}
		modifier = { # Difference in influence level
			NOT = { scope:actor.influence_level = scope:recipient.influence_level }
			add = {
				add = {
					add = 10
					multiply = scope:actor.influence_level
				}
				subtract = {
					add = 10
					multiply = scope:recipient.influence_level
				}
			}
			desc = difference_in_influence_level_reason
		}

		# Relations to actor
		modifier = {
			scope:actor = {
				has_relation_rival = scope:recipient
			}
			add = -100
			desc = AI_YOUR_RIVAL
		}
		modifier = {
			scope:actor = {
				has_relation_nemesis = scope:recipient
			}
			add = -150
			desc = offer_vassalization_interaction_aibehavior_nemesis_tt # Reused loc
		}
		modifier = {
			scope:recipient = {
				is_close_family_of = scope:actor
			}
			add = 50
			desc = CLOSE_FAMILY_REASON
		}
		modifier = {
			scope:recipient = {
				is_extended_family_of = scope:actor
			}
			add = 25
			desc = EXTENDED_FAMILY_REASON
		}
		modifier = {
			exists = scope:actor.house
			scope:recipient = {
				NOR = {
					is_close_family_of = scope:actor
					is_extended_family_of = scope:actor
				}
				exists = house
				house = scope:actor.house
			}
			add = 5
			desc = YOU_ARE_MY_HOUSE_MEMBER
		}
		modifier = {
			scope:actor = {
				has_relation_friend = scope:recipient
				NOT = { has_relation_best_friend = scope:recipient }
			}
			add = 15
			desc = WE_ARE_FRIENDS
		}
		modifier = {
			scope:actor = {
				has_relation_best_friend = scope:recipient
			}
			add = 30
			desc = WE_ARE_BEST_FRIENDS
		}

		# Relations to secondary_recipient (the character you want them to support)
		modifier = {
			scope:recipient = {
				has_relation_rival = scope:secondary_recipient
				NOT = { has_relation_nemesis = scope:secondary_recipient }
			}
			add = -50
			desc = RIVAL_TO_ME_REASON
		}
		modifier = {
			scope:recipient = {
				has_relation_nemesis = scope:secondary_recipient
			}
			add = -100
			desc = NEMESIS_TO_ME_REASON
		}

		# Relations to current title holder
		modifier = {
			exists = scope:target
			scope:recipient = {
				has_relation_rival = scope:target.holder
				NOT = { has_relation_nemesis = scope:target.holder }
				liege = scope:target.holder
				liege != scope:actor # Don't worry about liege relation when the liege is the one requesting support
			}
			add = 25
			desc = AI_THEIR_RIVAL
		}
		modifier = {
			exists = scope:target
			scope:recipient = {
				has_relation_nemesis = scope:target.holder
				liege = scope:target.holder
				liege != scope:actor # Don't worry about liege relation when the liege is the one requesting support
			}
			add = 50
			desc = AI_THEIR_RIVAL
		}
		modifier = {
			exists = scope:target
			scope:recipient = {
				has_relation_friend = scope:target.holder
				NOT = { has_relation_best_friend = scope:target.holder }
				liege = scope:target.holder
				liege != scope:actor # Don't worry about liege relation when the liege is the one requesting support
			}
			add = -50
			desc = AI_THEIR_FRIEND
		}
		modifier = {
			exists = scope:target
			scope:recipient = {
				has_relation_best_friend = scope:target.holder
				liege = scope:target.holder
				liege != scope:actor # Don't worry about liege relation when the liege is the one requesting support
			}
			add = -100
			desc = AI_THEIR_FRIEND
		}
		modifier = {
			exists = scope:target
			scope:recipient = {
				has_relation_lover = scope:target.holder
				liege = scope:target.holder
				liege != scope:actor # Don't worry about liege relation when the liege is the one requesting support
			}
			add = -30
			desc = AI_THEIR_LOVER
		}
		modifier = {
			exists = scope:target
			scope:recipient = {
				OR = {
					is_close_family_of = scope:target.holder
					is_extended_family_of = scope:target.holder
				}
				opinion = { target = scope:target.holder value > -25 }
				liege = scope:target.holder
				liege != scope:actor # Don't worry about liege relation when the liege is the one requesting support
			}
			add = -20
			desc = AI_THEIR_RELATIVE
		}

		# Traits
		modifier = {
			scope:recipient = { # Ambitious characters doesn't want to spend their influence on anyone not from their house
				has_trait = ambitious
				exists = house
			}
			scope:secondary_recipient ?= {
				exists = house
				NOT = { house = scope:recipient.house }
			}
			add = -50
			desc = INTERACTION_AMBITIOUS
		}
		modifier = {
			scope:recipient = {
				has_trait = callous
			}
			add = -20
			desc = INTERACTION_CALLOUS
		}
		modifier = {
			scope:recipient = {
				has_trait = fickle
			}
			add = -30
			desc = INTERACTION_FICKLE
		}
		modifier = {
			scope:recipient = {
				has_trait = paranoid
			}
			add = -25
			desc = INTERACTION_PARANOID
		}
		modifier = {
			scope:recipient = {
				has_trait = trusting
			}
			add = 10
			desc = INTERACTION_TRUSTING
		}
		modifier = {
			scope:recipient = {
				has_trait = arbitrary
			}
			add = -15
			desc = INTERACTION_ARBITRARY
		}
		modifier = { # Cravens dare not help you
			scope:recipient = {
				has_trait = craven
			}
			scope:actor.house ?= {
				is_dominant_family = no
			}
			add = -20
			desc = INTERACTION_CRAVEN
		}
		modifier = { # Cravens dare not refuse a dominant family
			scope:recipient = {
				has_trait = craven
			}
			scope:actor.house ?= {
				is_dominant_family = yes
			}
			add = 20
			desc = INTERACTION_CRAVEN
		}
	}

	ai_potential = {
		government_allows = administrative
		house ?= {
			OR = {
				is_powerful_family = yes
				is_dominant_family = yes
			}
		}
	}

	ai_frequency = 48

	ai_targets = {
		ai_recipients = peer_vassals
		max = 8
	}

	ai_targets = {
		ai_recipients = hooked_characters
		max = 2
	}
}