﻿#Give characters money to improve their opinion of you
gift_interaction = {
	icon = icon_gold
	category = interaction_category_friendly
	common_interaction = yes
	interface_priority = 65
	desc = gift_interaction_desc

	greeting = positive
	notification_text = SEND_GIFT_PROPOSAL

	answer_accept_key = SEND_GIFT_ACCEPT
	answer_reject_key = SEND_GIFT_REJECT

	ai_targets = {
		ai_recipients = scripted_relations
		ai_recipients = liege
		ai_recipients = head_of_faith
	}
	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = peer_vassals
		max = 10
	}
	ai_targets = {
		ai_recipients = vassals
		max = 10
	}
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_frequency_by_tier = {
		barony = 120
		county = 60
		duchy = 60
		kingdom = 36
		empire = 36
		hegemony = 36
	}

	is_shown = {
		scope:recipient != scope:actor

		# Prioritize paying back loans first
		scope:actor = {
			trigger_if = {
				limit = {
					exists = var:loan_amount_owed
					exists = var:loan_holder
				}
				NOT = { var:loan_holder = scope:recipient }
			}
		}
	}

	is_highlighted = {
		scope:recipient = {
			OR = {
				house ?= {
					OR = {
						is_dominant_family = yes
						is_powerful_family = yes
					}
				}
			}
		}
	}

	highlighted_reason = HIGHLIGHTED_FAMILY_RATING

	is_valid_showing_failures_only = {
		scope:actor.gold >= gift_value
		scope:recipient = { 
			NOT = { is_imprisoned_by = scope:actor }
		}
	}

	on_auto_accept = {
		scope:recipient = {
			trigger_event = char_interaction.0100
		}
	}

	on_accept = {
		scope:recipient = {
			# Verify that they could become friend
			if = {
				limit = {
					NAND = {
						has_relation_friend = scope:actor
						has_relation_lover = scope:actor
						has_relation_soulmate = scope:actor
						has_relation_best_friend = scope:actor
					}
				}
				gifting_leads_towards_friendship_effect = yes
			}
		}
		# HOUSE RELATIONS
		if = {
			limit = {
				scope:actor = {
					is_house_head = yes
				}
				scope:recipient = {
					is_house_head = yes
					NOT = {
						has_opinion_modifier = {
							target = scope:actor
							modifier = gift_opinion
						}
					}
				}
			}
			scope:recipient.house = {
				change_house_relation_effect = {
					HOUSE = scope:actor.house
					VALUE = house_relation_improve_medium_value
					REASON = sent_a_gift
					CHAR = scope:actor
					TARGET_CHAR = scope:recipient
					TITLE = scope:dummy_gender
				}
			}
		}
		scope:actor = {
			# Warning for multiple gifts
			if = {
				limit = {
					scope:recipient = {
						has_opinion_modifier = {
							target = scope:actor
							modifier = gift_opinion
						}
					}
				}
				custom_tooltip = ALREADY_SENT_GIFT_WARNING
			}
			
			send_interface_message = {
				type = event_gold_neutral
				title = gift_interaction_notification
				right_icon = scope:recipient
				pay_short_term_gold = {
					gold = gift_value
					target = scope:recipient
				}
				stress_impact = {
					greedy = medium_stress_impact_gain
				}
				if = {
					limit = {
						scope:recipient = {
							NOT = {
								is_heir_of = scope:actor
							}
						}
					}
					stress_impact = {
						generous = medium_stress_impact_loss
					}
				}
				
				#FP3 Tenet Communal Possessions Perk - piety gain for gift giving. 
				if = {
					limit = {
						scope:actor = {
							faith = {
					 			has_doctrine_parameter = piety_from_gifts_active
					 		}
					 	}
					}
					scope:actor = {
						add_piety = minor_piety_gain
					}
				}
				
				# Check if the target already has been sent a gift or not. If not, apply the following effects
				if = {
					limit = {
						NOT = {
							scope:recipient = {
								has_opinion_modifier = {
									target = scope:actor
									modifier = gift_opinion
								}
							}
						}
					}
					
					# Struggle Catalyst
					if = {
						limit = {
							scope:actor = {
								any_character_struggle = {
									involvement = involved
									phase_has_catalyst = catalyst_gift_independent_ruler
									activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
										CATALYST = catalyst_gift_independent_ruler
										CHAR = scope:recipient
									}
								}
							}
						}
						hidden_effect = {
							scope:actor = {
								every_character_struggle = {
									involvement = involved
									limit = { phase_has_catalyst = catalyst_gift_independent_ruler }
									activate_struggle_catalyst = {
										catalyst = catalyst_gift_independent_ruler
										character = scope:actor
									}
								}
							}
						}
					}
					if = {
						limit = {
							fp3_struggle_involves_one_supporter_and_one_detractor = {
								FIRST = scope:actor
								SECOND = scope:recipient
							}
							scope:actor = {
								any_character_struggle = {
									#involvement = involved
									activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
										CATALYST = catalyst_gift_supporter_detractor_ruler
										CHAR = scope:recipient
									}
								}
							}
						}
						hidden_effect = {
							scope:actor = {
								every_character_struggle = {
									#involvement = involved
									limit = {
										activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
											CATALYST = catalyst_gift_supporter_detractor_ruler
											CHAR = scope:recipient
										}
									}
									activate_struggle_catalyst = {
										catalyst = catalyst_gift_supporter_detractor_ruler
										character = scope:actor
									}
									log_debug_variable_for_persian_struggle_effect = { VAR = concession_catalyst_gift_supporter_detractor_ruler }
								}
							}
						}
					}
					
					# FP2 Urbanism Legacy Perk 2: increase cultural acceptance when gifting a republican vassal
					if = {
						limit = {
							scope:actor = {
								dynasty ?= { has_dynasty_perk = fp2_urbanism_legacy_4 }
							}
							scope:recipient = {
								AND = {
									is_vassal_of = scope:actor
									government_has_flag = government_is_republic
								}
							}
							scope:actor.culture != scope:recipient.capital_province.county.culture
						}
						scope:recipient.capital_province.county.culture = {
							change_cultural_acceptance = {
								target = scope:actor.culture
								value = {
									value = 20
									multiply = scope:recipient.capital_province.county.development_level
									divide = 100
								}
								desc = cultural_acceptance_gain_gift_dynasty_perk
							}
						}
					}
				}
				
				# Let's apply the opinion modifier last, as to apply everything else correctly first
				scope:recipient = {
					add_opinion = {
						target = scope:actor
						modifier = gift_opinion
						opinion = send_gift_opinion
					}
				}
			}

			if = { # for tutorial purposes
				limit = {
					is_ai = no
					scope:recipient = { is_child_of = scope:actor }
				}
				add_character_flag = {
					flag = tutorial_sent_gift
					days = 200
				}
			}
			#Influence gain between admin rulers
			if = {
				limit = {
					is_ruler = yes
					government_allows = administrative
					scope:recipient = {
						is_ruler = yes
						government_allows = administrative
						any_held_title = {
							is_noble_family_title = yes
						}
					}
				}
				if = {
					limit = {
						scope:recipient = {
							OR = {
								house = {
									is_dominant_family = yes
								}
								this = scope:actor.top_liege
							}
							influence_level >= 5
						}
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 8
					}
				}
				else_if = {
					limit = {
						scope:recipient = {
							OR = {
								house = {
									is_dominant_family = yes
								}
								this = scope:actor.top_liege
							}
							influence_level >= 3
						}
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 7
					}
				}
				else_if = {
					limit = {
						scope:recipient = {
							OR = {
								house = {
									is_dominant_family = yes
								}
								this = scope:actor.top_liege
							}
						}
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 6
					}
				}
				else_if = {
					limit = {
						scope:recipient = {
							OR = {
								house = {
									is_powerful_family = yes
								}
								this = scope:actor.top_liege
							}
							influence_level >= 5
						}
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 6
					}
				}
				else_if = {
					limit = {
						scope:recipient = {
							OR = {
								house = {
									is_powerful_family = yes
								}
								this = scope:actor.top_liege
							}
							influence_level >= 3
						}
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 5
					}
				}
				else_if = {
					limit = {
						scope:recipient = {
							OR = {
								house = {
									is_powerful_family = yes
								}
								this = scope:actor.top_liege
							}
						}
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 4
					}
				}
				else_if = {
					limit = {
						scope:recipient = { influence_level >= 5 }
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 3
					}
				}
				else_if = {
					limit = {
						scope:recipient = { influence_level >= 3 }
					}
					change_influence = {
						value = miniscule_influence_gain
						multiply = 2
					}
				}
				else = {
					change_influence = miniscule_influence_gain
				}
			}
		}
	}
	
	ai_accept = {
		base = 0
		modifier = {
			add = 100
			desc = GOLD_REASON
		}

		# Struggle motive
		modifier = {
			desc = AI_STRUGGLE_INTENT
			scope:recipient = {
				top_liege = this
				any_character_struggle = {
					involvement = involved
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:recipient = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_gift_independent_ruler
							}
							has_character_flag = agenda_towards_escalation
						}
					}
					add = -100
				}
				else_if = {
					limit = {
						scope:recipient = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_gift_independent_ruler
							}
						}
					}
					add = 200
				}
			}
		}
	}
	
	ai_potential = {
		is_available_at_peace_ai_adult = yes
		ai_greed < medium_positive_ai_value
		short_term_gold >= gift_interaction_cutoff
		NOT = {
			has_trait = greedy
		}
		NOT = { has_variable = conqueror }
		ai_should_focus_on_building_in_their_capital = no
	}
	
	auto_accept = {
		custom_description = {
			text = auto_accept_interaction_ai	
			object = scope:recipient
			scope:recipient = {
				is_ai = yes
			}
		}
	}
	
	ai_min_reply_days = 1
	ai_max_reply_days = 1
	
	ai_will_do = {
		base = 100
		
		modifier = { # Do not send double-gifts
			factor = 0
			scope:recipient = {
				has_opinion_modifier = {
					target = scope:actor
					modifier = gift_opinion
				}
			}
		}
		
		modifier = { # Basic Filtering
			factor = 0
			scope:recipient = {
				NOR = {
					AND = { # Bankrupt lovers, friends and a bankrupt liege should be considered
						OR = {
							scope:actor = {
								any_liege_or_above = {
									this = scope:recipient
								}
							}
							has_secret_relation_lover = scope:actor
							has_relation_lover = scope:actor
							has_relation_soulmate = scope:actor
							has_relation_friend = scope:actor
							has_relation_best_friend = scope:actor
						}
						gold < 0
					}
					AND = { # Generous characters will give gifts to their friends/lovers
						is_ruler = yes
						OR = {
							has_secret_relation_lover = scope:actor
							has_relation_lover = scope:actor
							has_relation_soulmate = scope:actor
							has_relation_friend = scope:actor
							has_relation_best_friend = scope:actor
						}
						scope:actor = {
							OR = {
								ai_greed <= high_negative_ai_value
								AND = {
									ai_greed < 0
									culture = {
										has_cultural_parameter = gives_more_gifts
									}
								}
							}
						}
					}
					AND = { # Characters with the Generous Cultural Tradition will give gifts to more people
						is_ruler = yes
						OR = {
							is_allied_to = scope:actor
							is_close_or_extended_family_of = scope:actor
						}
						scope:actor = {
							ai_greed < 0
							culture = {
								has_cultural_parameter = gives_more_gifts
							}
						}
					}
					AND = { # Characters with the loyal trait more likely to give gifts to friends/lieges
						OR = {
							scope:actor.liege ?= this
							has_relation_friend = scope:actor
							has_relation_best_friend = scope:actor
							is_allied_to = scope:actor
							is_close_or_extended_family_of = scope:actor
						}
						scope:actor = {
							ai_greed <= 0
							has_trait = loyal
						}
					}
					AND = { # Zealous characters want to support the defending side in GHW's
						gold < 200
						any_character_war = {
							OR = {
								using_cb = undirected_great_holy_war
								using_cb = directed_great_holy_war
							}
							primary_defender = scope:recipient
						}
						scope:actor = {
							ai_zeal >= 50
						}
					}
					AND = { # Powerful vassals should be considered
						is_powerful_vassal_of = scope:actor
						opinion = {
							target = scope:actor
							value < 0
						}
						NOT ={
							has_opinion_modifier = {
								target = scope:actor
								modifier = gift_opinion
							}
						}
					}
					AND = { # Factioneering vassals should be considered
						is_vassal_of = scope:actor
						is_a_faction_member = yes
						NOT ={
							has_opinion_modifier = {
								target = scope:actor
								modifier = gift_opinion
							}
						}
					}
					AND = { # Realm Priests should be considered
						scope:actor = {
							faith = { has_doctrine = doctrine_theocracy_temporal }
							exists = cp:councillor_court_chaplain
							cp:councillor_court_chaplain = scope:recipient
						}
						opinion = {
							target = scope:actor
							value <= 25
						}
						NOT = {
							has_opinion_modifier = {
								target = scope:actor
								modifier = gift_opinion
							}
						}
					}
					AND = { # Rulers at war with your rivals deserve gold if they're bankrupt
						gold < 0
						is_at_war = yes
						any_war_enemy = {
							has_relation_rival = scope:actor
						}
					}
					AND = { # Independent ruler within the struggle should be considered
					 	any_character_struggle = {
					 		phase_has_catalyst = catalyst_gift_independent_ruler
					 	}
					}
				}
			}
		}

		# Struggle
		modifier = {
			scope:recipient = {
				any_character_struggle = {
					involvement = involved
					phase_has_catalyst = catalyst_gift_independent_ruler
				}
			}
			scope:actor = {
				any_character_struggle = {
					involvement = involved
					phase_has_catalyst = catalyst_gift_independent_ruler
				}
				NOT = {
					has_relation_rival = scope:recipient
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:actor = {
							has_character_flag = agenda_towards_escalation
						}
					}
					add = -100
				}
				else_if = {
					limit = {
						scope:actor = {
							has_character_flag = agenda_towards_deescalation
						}
						scope:recipient = {
							has_character_flag = agenda_towards_deescalation
						}
					}
					add = {
						value = 150
						if = {
							limit = { scope:recipient = { is_ai = no} }
							add = 150 # higher for human player to Prioritize them
						}
						# Then lower the intent depending on the gold reserve
						# Base value applied if the AI has 3x the gold cutoff
						multiply = {
							value = short_term_gold
							divide = {
								value = gift_interaction_cutoff
								multiply = 3
							}
						}
					}
				}
				else_if = {
					limit = {
						scope:actor = {
							has_character_flag = agenda_towards_deescalation
						}
					}
					add = {
						value = 25
						if = {
							limit = { scope:recipient = { is_ai = no} }
							add = 150 # higher for human player to Prioritize them
						}
						# Then lower the intent depending on the gold reserve
						# Base value applied if the AI has 3x the gold cutoff
						multiply = {
							value = short_term_gold
							divide = {
								value = gift_interaction_cutoff
								multiply = 3
							}
						}
					}
				}
			}
		}
		
		modifier = {
			factor = 0.1
			scope:recipient = {
				opinion = {
					target = scope:actor
					value < 0
				}
				NAND = {
					is_vassal_of = scope:actor
					scope:actor = {
						ai_rationality > 50
					}
				}
			}
		}
		
		modifier = {
			add = 100
			scope:actor = {
				is_at_war = no
				has_trait = fp3_struggle_supporter
				any_character_struggle = { is_struggle_type = persian_struggle }
			}
			scope:recipient = {
				title:d_sunni.holder ?= this
				gold < 0
			}
		}
		
		# Nomads are slightly less likely to do this since they'd prefer to send Herd instead
		modifier = {
			add = -10
			scope:actor = {
				government_has_flag = government_is_nomadic
			}
			scope:recipient = {
				government_has_flag = government_is_nomadic
			}
		}
		
		modifier = {
			factor = 0.1
			scope:recipient = {
				has_relation_rival = scope:actor
			}
		}

		modifier = {
			factor = 2
			scope:actor.house.house_confederation ?= {
				has_cohesion_level_parameter = bloc_members_send_leader_gifts
				leading_house.house_head ?= scope:recipient
			}
		}
	}
}
