# Character Interactions for EP1

##################
# Fund someone's inspiration
# by Linnéa Thimrén
##################
fund_inspiration_interaction = {
	icon = inspiration
	interface_priority = 30
	common_interaction = yes
	category = interaction_category_vassal
	desc = fund_inspiration_interaction_desc

	is_shown = {
		scope:recipient = {
			is_ruler = no
			inspiration ?= {
				NOT = {
					exists = inspiration_sponsor
				}
			}
		}
		scope:actor = {
			any_courtier_or_guest = {
				this = scope:recipient
			}
		}
	}
	
	is_highlighted = {
		always = yes
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			is_physically_able = yes
			is_ruler = no
			trigger_if = {
				limit = {
					NOT = { is_courtier_of = scope:actor }
				}
				can_recruit_character_to_court_trigger = {
					RECRUITER = scope:actor
					RECRUITEE = scope:recipient
				}
			}
		}
		scope:actor = {
			can_sponsor_inspiration = scope:recipient.inspiration
			gold >= scope:recipient.inspiration.base_inspiration_gold_cost
		}
	}
	
	on_accept = {
		fund_inspiration_effect = yes

		if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = weapon_inspiration } }
			custom_description_no_bullet = { text = weapon_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = armor_inspiration } }
			custom_description_no_bullet = { text = armor_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = book_inspiration } }
			custom_description_no_bullet = { text = book_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = weaver_inspiration } }
			custom_description_no_bullet = { text = weaver_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = adventure_inspiration } }
			custom_description_no_bullet = { text = adventure_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = artisan_inspiration } }
			custom_description_no_bullet = { text = artisan_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = smith_inspiration } }
			custom_description_no_bullet = { text = smith_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = alchemy_inspiration } }
			custom_description_no_bullet = { text = alchemy_inspiration_cost_tt }
		}
		else_if = {
			limit = { scope:recipient.inspiration = { has_inspiration_type = bow_inspiration } }
			custom_description_no_bullet = { text = bow_inspiration_cost_tt }
		}

		# Add a flag on AI to have an extra cooldown of 3 years
		if = {
			limit = {
				scope:actor = {
					is_ai = yes
					NOT = {
						gold >= 1000
					}
					OR = {
						short_term_gold < ai_inspiration_desired_gold_value
						ai_greed >= 50
					}
				}
			}
			scope:actor = {
				add_character_flag = {
					flag = recently_fund_inspiration
					years = 3
				}
			}

		}

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

		#Mandala Creation Aspect
		scope:actor = {
			if = {
				limit = { government_has_flag = government_is_mandala }
				increment_variable_effect = {
					VAR = num_commissioned_artifacts
					VAL = 1
				}
			}
		}
	}
	
	auto_accept = {
		custom_description = {
			text = auto_accept_interaction_ai	
			object = scope:recipient
			scope:recipient = {
				is_ai = yes
			}
		}
	}

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

	ai_targets = {
		ai_recipients = guests
		ai_recipients = courtiers
	}

	ai_target_quick_trigger = {
		adult = yes
	}

	ai_potential = {
		is_at_war = no
		OR = {
			has_royal_court = yes
			OR = { government_has_flag = government_is_landless_adventurer government_has_flag = government_is_landless_minority }
		}
		gold > ai_inspiration_desired_gold_value
		NOT = { has_character_flag = recently_fund_inspiration }
		NOT = { has_variable = conqueror }
		ai_should_focus_on_building_in_their_capital = no
	}

	ai_will_do = {
		base = 100
		
		modifier = { # Not as likely to fund inspirations if they already have some going (max three for very prestigious courts)
			add = {
				if = {
					limit = {
						highest_held_title_tier >= tier_empire
						any_sponsored_inspiration = {
							count >= 3
						}
					}
					add = -50
				}
				else_if = {
					limit = {
						highest_held_title_tier >= tier_kingdom
						any_sponsored_inspiration = {
							count >= 2
						}
					}
					add = -50
				}
			}
		}
		
		# Do not pointlessly fund inspirations you do not need
		modifier = { # Weapons
			factor = 0
			scope:recipient.inspiration = {
				has_inspiration_type = weapon_inspiration
			}
			OR = {
				any_character_artifact = { # Has a fantastic item already
					artifact_slot_type = primary_armament
					rarity = illustrious
				}
				AND = { # Have a lot of artifacts already, and the inspired person isn't even very good
					scope:recipient = {
						weapon_inspiration_average_skill_value < 15
					}
					any_character_artifact = {
						count >= 2
						artifact_slot_type = primary_armament
					}
				}
			}
		}
		modifier = { # Armor
			factor = 0
			scope:recipient.inspiration = {
				has_inspiration_type = armor_inspiration
			}
			OR = {
				any_character_artifact = { # Has a fantastic item already
					artifact_slot_type = armor
					rarity = illustrious
				}
				AND = { # Have a lot of artifacts already, and the inspired person isn't even very good
					scope:recipient = {
						armor_inspiration_average_skill_value < 15
					}
					any_character_artifact = {
						count >= 2
						artifact_slot_type = armor
					}
				}
			}
		}
		modifier = { # Warlike AI's only sponsor if they've been at peace for a long time
			factor = 0
			ai_has_warlike_personality = yes
			days_of_continuous_peace < 1825 # 5 years
		}
		modifier = { # Cautious AI's only sponsor if they feel safe enough
			factor = 0
			ai_has_cautious_personality = yes
			war_chest_gold < cautious_ai_minimum_war_chest_gold
		}
	}
}

##################
# Indebt Guest
# by Ewan Cowhig Croft
##################

indebt_guest_interaction = {
	icon = icon_hostile
	interface_priority = 120
	common_interaction = yes
	ai_max_reply_days = 0
	cooldown = { years = 5 }

	category = interaction_category_hostile
	ai_maybe = yes

	desc = indebt_guest_interaction_desc

	greeting = negative
	pre_answer_maybe_key = ANSWER_MIGHT_SUCCEED
	pre_answer_no_key = ANSWER_CANT_SUCCEED
	pre_answer_yes_key = ANSWER_WILL_SUCCEED
	pre_answer_maybe_breakdown_key = ANSWER_SUM_CHANCE
	
	ai_potential = {
		# Repeat some is_shown triggers for performance gains.
		indebt_guest_interaction_basic_checks_trigger = { ACTOR = this }
	}

	is_shown = {
		scope:actor != scope:recipient
		# Repeat some ai_potential triggers for performance gains.
		indebt_guest_interaction_basic_checks_trigger = { ACTOR = scope:actor }
		# Scope:recipient must be a guest.
		scope:recipient = { is_pool_guest_of = scope:actor }
	}

	is_valid_showing_failures_only = {
		scope:actor = { is_imprisoned = no }
		scope:actor = { is_commanding_army = no }
		# Scope:actor can't have a hook of any kind on scope:recipient already.
		NOT = {
			scope:actor = { has_hook = scope:recipient }
		}
		# Scope:actor must be able to afford the cost of the decision going wrong.
		scope:actor.court_grandeur_current >= indebt_guest_interaction_lost_test_grandeur_cost_value
	}

	can_send = {
		scope:actor = {
			custom_description = {
				text = "character_interactions_hostile_actions_disabled_delay"
				NOT = { has_character_flag = flag_hostile_actions_disabled_delay }
			}
		}
	}

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

	on_accept = {
		scope:actor = {
			# On accept, you gain a hook & lose some opinion.
			## Which we only show as a tooltip for now, since it's applied in the event.
			show_as_tooltip = { indebt_guest_interaction_accepted_effect = yes }
			# Pop the event.
			trigger_event = ep1_character_interaction.0001
			# Reminder that you'll have a chance to direct where they go.
			custom_tooltip = indebt_guest_interaction.tt.direct_indebted_guest
		}

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

	on_decline = {
		# Send a toast.
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = indebt_guest_interaction.tt.failure
				left_icon = scope:recipient
				# On decline, scope:recipient leaves your court...
				scope:recipient = { select_and_move_to_pool_effect = yes }
				# ... and you lose a bit of extra grandeur for your audacity.
				change_current_court_grandeur = indebt_guest_interaction_lost_test_grandeur_cost_value
			}
		}

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

	auto_accept = no

	ai_accept = {
		base = 50

		# Standard intrigue duel.
		modifier = {
			add = {
				value = scope:actor.intrigue
				multiply = 2
			}
			desc = INDEBT_GUEST_INTRIGUE_ACTOR
		}
		modifier = {
			add = {
				value = scope:recipient.intrigue
				multiply = -1
			}
			desc = INDEBT_GUEST_INTRIGUE_RECIPIENT
		}
		# Weight it a bit more in your favour for every point of excess CGV you have.
		modifier = {
			add = scope:actor.current_cgv_above_expected_exact_value
			desc = INDEBT_GUEST_CURRENT_CGV_ABOVE_EXPECTED
			scope:actor.current_cgv_above_expected_exact_value >= 1
		}
		# Some traits may also affect the chances for scope:actor...
		modifier = {
			add = 10
			scope:actor = { has_trait = schemer }
		}
		modifier = {
			add = 10
			scope:actor = { has_trait = education_intrigue }
		}
		modifier = {
			add = 5
			scope:actor = { has_trait = deceitful }
		}
		modifier = {
			add = 5
			scope:actor = { has_trait = vengeful }
		}
		modifier = {
			add = -10
			scope:actor = { has_trait = honest }
		}
		modifier = {
			add = -5
			scope:actor = { has_trait = forgiving }
		}
		# ... or else for scope:recipient.
		modifier = {
			add = -10
			scope:recipient = { has_trait = schemer }
		}
		modifier = {
			add = -5
			scope:recipient = { has_trait = education_intrigue }
		}
		modifier = {
			add = -5
			scope:recipient = { has_trait = deceitful }
		}
		modifier = {
			add = 10
			scope:recipient = { has_trait = honest }
		}
	}

	# AI
	ai_targets = {
		ai_recipients = guests
		chance = 0.5
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 0
		kingdom = 10
		empire = 10
		hegemony = 10
	}

	ai_will_do = {
		base = -100
		
		# This is a silly way for the AI to spend its CGV, so we keep 'em off it most of the time.
		# Unless they've got a scheme on the go at their nemesis' court...
		modifier = {
			add = 100
			scope:actor = {
				any_relation = { type = nemesis }
				any_scheme = {
					count >= 1
					scheme_target_character = {
						any_relation = {
							type = nemesis
							count >= 1
							this = scope:actor
						}
					}
				}
			}
		}
		# ... *and* they're likely to win due to having higher intrigue, but without scope:recipient being too terrible.
		modifier = {
			add = 25
			indebt_guest_interaction_get_intrigue_difference_value >= indebt_guest_interaction_likely_success_control_value
			scope:recipient = { intrigue >= high_skill_rating }
		}
		modifier = {
			add = 25
			indebt_guest_interaction_get_intrigue_difference_value >= indebt_guest_interaction_likely_success_control_value
			scope:recipient = { intrigue >= very_high_skill_rating }
		}
		modifier = {
			add = 50
			indebt_guest_interaction_get_intrigue_difference_value >= indebt_guest_interaction_likely_success_control_value
			scope:recipient = { intrigue >= extremely_high_skill_rating }
		}
	}	
}
