﻿#Give characters money to improve their opinion of you
gift_interaction = {
	icon = icon_gold
	category = interaction_category_friendly
	common_interaction = yes
	interface_priority = 60
	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_targets = {
		ai_recipients = liege
	}
	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 = 60

	is_shown = {
		NOT = {
			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_valid_showing_failures_only = {
		scope:actor.gold >= gift_value
		scope:recipient = { is_busy_in_events_localised = yes }
	}

	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
			}
		}

		# Warning for multiple gifts
		if = {
			limit = {
				scope:recipient = {
					has_opinion_modifier = {
						target = scope:actor
						modifier = gift_opinion
					}
				}
			}
			custom_tooltip = ALREADY_SENT_GIFT_WARNING
		}
		else = { # Struggle Catalyst
			limit = {
				#scope:recipient = {
				#	is_independent_ruler = yes
				#}
				scope:actor = {
					any_character_struggle = {
						involvement = involved
						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
						activate_struggle_catalyst = {
							catalyst = catalyst_gift_independent_ruler
							character = scope:actor
						}
					}
				}
			}
		}

		scope:actor = {
			send_interface_message = {
				type = event_generic_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
					generous = medium_stress_impact_loss
				}
				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
				}
			}
		}
	}
	
	ai_accept = {
		base = 0
		modifier = {
			add = 100
			desc = GOLD_REASON
		}

		# Struggle motive
		modifier = {
			desc = AI_STRUGGLE_INTENT
			scope:recipient = {
				is_independent_ruler = yes
				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
		}
	}
	
	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 vassals, lovers, friends and a bankrupt liege should be considered
						OR = {
							# [LTAF]
							AND = {
								is_vassal_of = scope:actor
								LTAF_is_loyalist = yes
							}
							# [/LTAF]
							AND = {
								exists = liege
								liege = scope:actor
							}
							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
						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
							# [LTAF]
							AND = {
								is_vassal_of = scope:actor
								LTAF_is_loyalist = yes
							}
							# [/LTAF]
						}
						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
						OR = {
							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
						# [LTAF]
						LTAF_is_loyalist = no
						# [/LTAF]
						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
					}
				}
			}
		}
	}
}
