﻿gpt_negotiate_alliance_interaction = {
	category = interaction_category_diplomacy
	common_interaction = yes
	interface_priority = 100
	ai_min_reply_days = 4
	ai_max_reply_days = 9
	icon = alliance

 #	cooldown = { years = 1 }

	send_name = gpt_negotiate_alliance_interaction

	interface_priority = 59
	desc = gpt_negotiate_alliance_interaction_desc

	greeting = positive
	notification_text = NEGOTIATE_ALLIANCE_NOTIFICATION

	is_shown = {
		OR = {
			has_game_rule = GPT_wareffects_enabled
			has_game_rule = GPT_wareffects_mixed
		}
		NOT = {
			scope:actor = {
				is_liege_or_above_of = scope:recipient
			}
		}
		NOT = {
			scope:recipient = {
				is_liege_or_above_of = scope:actor
			}
		}
		NOT = {
			scope:recipient = {
				is_vassal_or_below_of = scope:actor
			}
		}
		NOT = {
			scope:actor = {
				is_vassal_or_below_of = scope:recipient
			}
		}
		scope:recipient = {
			NOT = { this = scope:actor }
			NOT = { is_allied_to = scope:actor }
			is_ruler = yes
			is_confederation_member = no
			OR = {
				sub_realm_size <= 8
				AND = {
					is_independent_ruler = yes
					sub_realm_size <= 16
					scope:actor.faith = scope:recipient.faith
					fifty_percent_of_max_military_strength < scope:actor.max_military_strength
					OR = {
						any_neighboring_top_liege_realm_owner = {
							NOT = { is_allied_to = scope:recipient }
							NOT = {	faith = scope:recipient.faith }
						}
					}
				}
			}
			any_sub_realm_county = { is_neighbor_to_realm = scope:actor }
		}
		scope:actor = {
			is_ruler = yes
			is_confederation_member = no
			OR = {
				is_ai = no
				highest_held_title_tier = 2
				highest_held_title_tier = 3
			}
			OR = {
				sub_realm_size <= 8
				AND = {
					is_independent_ruler = yes
					sub_realm_size <= 16
					scope:actor.faith = scope:recipient.faith
					OR = {
						any_neighboring_top_liege_realm_owner = {
							NOT = { is_allied_to = scope:recipient }
							NOT = {	faith = scope:actor.faith }
							fifty_percent_of_max_military_strength > scope:actor.max_military_strength
						}
					}
				}
			}
			OR = {
				is_independent_ruler = yes
				AND = {
					is_independent_ruler = no
					exists = scope:recipient.liege
					liege = scope:recipient.liege
				}
			}
		}
		NOR = {
			#Ruler has the defensive negotiations perk
		 #	scope:actor = { has_perk = defensive_negotiations_perk }
			#Ruler is married to other ruler.
			scope:actor = { is_spouse_of = scope:recipient }
			#Ruler is relative of other ruler
			scope:actor = { is_parent_of = scope:recipient }
			scope:actor = { is_grandparent_of = scope:recipient }
			scope:actor = { is_great_grandparent_of = scope:recipient }
			scope:actor = { is_child_of = scope:recipient }
			scope:actor = { is_grandchild_of = scope:recipient }
			scope:actor = { is_great_grandchild_of = scope:recipient }
			scope:actor = { is_uncle_or_aunt_of = scope:recipient }
			scope:actor = { is_nibling_of = scope:recipient }
			scope:actor = { is_sibling_of = scope:recipient }
			#childhood friendship with hostage event, where they swear to be friends forever
			scope:actor = {
				hostage_oath_of_friendship_trigger = yes
			}
			

			custom_description = {
				text = rulers_family_is_married_to_other_ruler_trigger
				subject = scope:recipient
				object = scope:actor
				#Ruler's child/parent/sibling/grandchild/grandparent/uncle/aunt/niece/nephew is married to other ruler.
				scope:recipient = {
					any_spouse = {
						OR = {
							is_close_family_of = scope:actor
							is_nibling_of = scope:actor
							is_uncle_or_aunt_of = scope:actor
						}
					}
				}
			}

			custom_description = {
				text = rulers_family_is_married_to_other_ruler_trigger
				subject = scope:actor
				object = scope:recipient
				#Other ruler's child/parent/sibling/grandchild/grandparent/uncle/aunt/niece/nephew is married to first ruler.
				scope:actor = {
					any_spouse = {
						OR = {
							is_close_family_of = scope:recipient
							is_nibling_of = scope:recipient
							is_uncle_or_aunt_of = scope:recipient
						}
					}
				}
			}

			custom_description = {
				text = rulers_child_parent_sibling_is_married_to_other_rulers_trigger
				subject = scope:recipient
				object = scope:actor
				# Ruler's child/parent/sibling is married to other ruler's child/parent/sibling.
				scope:actor = {
					any_close_family_member = {
						OR = {
							is_parent_of = scope:actor
							is_child_of = scope:actor
							is_sibling_of = scope:actor
						}
						any_spouse = {
							OR = {
								is_parent_of = scope:recipient
								is_child_of = scope:recipient
								is_sibling_of = scope:recipient
							}
						}
					}
				}
			}

			custom_description = {
				text = hostage_oath_of_friendship_trigger
				subject = scope:recipient
				object = scope:actor
				#Ruler's child/parent/sibling/grandchild/grandparent/uncle/aunt/niece/nephew is married to other ruler.
				scope:actor = {
					hostage_oath_of_friendship_trigger = yes
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
		scope:recipient = {
			is_landed = yes
			is_imprisoned = no
		}
		scope:actor = {
			is_landed = yes
			is_imprisoned = no
			NOT = {
				is_at_war_with = scope:recipient
			}
		}
		scope:recipient = { basic_is_available_ai = yes }
		custom_description = {
			text = "has_already_rejected_renegotiation"
			subject = scope:recipient
			NOT = {
				scope:actor = {
					has_opinion_modifier = {
						modifier = refused_alliance_opinion
						target = scope:recipient
					}
				}
			}
		}
	}

	is_valid = {
	}

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

	#Use hook
	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 = {
			add_character_flag = { flag = fp3_is_a_loyalist_puppet years = 5 }
			trigger_event = char_interaction.0235

			stress_impact = {
				shy = minor_stress_impact_gain
			}

			if = {
				limit = {
					scope:hook = yes
					has_hook = scope:recipient
				}
				use_hook = scope:recipient
			}
		}

		scope:recipient = {
			add_character_flag = { flag = fp3_is_a_loyalist_puppet years = 5 }
			custom_tooltip = negotiate_alliance_effect_recipient_tooltip
		}

		#Set up alliance, depending on relation between rulers.
		hidden_effect = {
			scope:actor = {
				create_alliance = {
					target = scope:recipient
					allied_through_owner = scope:actor
					allied_through_target = scope:recipient
				}
			}
		}

		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_alliance.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	on_decline = {
		scope:actor = {
			trigger_event = char_interaction.0236
		}
		scope:actor = {
			add_opinion = {
				modifier = refused_alliance_opinion
				target = scope:recipient
			}
		}

		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_alliance_refusal.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

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

		# Opinion 			(+/-)
		# Dynasty Prestige. (+)
		# Different Faith.	(-)
		# Liege is asking 	(+)
		# Dread 			(+)
		# Power difference 	(+/-)
		# Claimants (+/-)
		# Same House (+)
		# Declared war opinion
		# Actor is at war penalty

		opinion_modifier = { # More likely to accept if Recipient likes the Actor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1
			desc = AI_OPINION_REASON
		}

		# Tier Differences
		modifier = {
			add = -20
			scope:actor = {
				highest_held_title_tier = 2
			}
			scope:recipient = {
				highest_held_title_tier = 3
			}
			desc = RANK_DIFFERENCE_REASON
		}

		modifier = {
			add = 20
			is_independent_ruler = yes
			OR = {
				any_neighboring_top_liege_realm_owner = {
					NOR = {
						is_allied_to = scope:recipient
					}
					sub_realm_size > 16
				}
			}
			desc = GPT_IS_THREATENED
		}

		modifier = {
			add = 20
			scope:recipient = {
				is_independent_ruler = yes
			}
			desc = GPT_IS_INDEPENDENT
		}

		modifier = {
			add = 20
			scope:recipient = {
				has_trait = loyal
			}
			desc = INTERACTION_LOYAL
		}

		modifier = {
			add = 20
			scope:recipient = {
				has_trait = trusting
			}
			desc = INTERACTION_TRUSTING
		}

		modifier = {
			add = 20
			scope:recipient = {
				has_trait = craven
			}
			desc = INTERACTION_CRAVEN
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = paranoid
			}
			desc = INTERACTION_PARANOID
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = arrogant
			}
			desc = INTERACTION_ARROGANT
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = ambitious
			}
			desc = INTERACTION_AMBITIOUS
		}

		modifier = {
			add = -40
			scope:recipient = {
				has_trait = disloyal
			}
			desc = INTERACTION_DISLOYAL
		}

		modifier = {
			add = -10
			scope:recipient = {
				has_trait = fickle
			}
			desc = INTERACTION_FICKLE
		}

		# Rivalry
		modifier = { # Say no to rivals
			add = -100
			scope:recipient = {
				has_relation_rival = scope:actor
				NOT = { has_relation_nemesis = scope:actor }
			}
			desc = "ACTOR_RIVAL_TO_ME_REASON"
		}
		modifier = { # Say no to nemesis
			add = -300
			scope:recipient = {
				has_relation_nemesis = scope:actor
			}
			desc = "ACTOR_NEMESIS_TO_ME_REASON"
		}
		
		# Dynasty Prestige Levels
		modifier = {
			add = -5
			desc = AI_DYNASTY_PRESTIGE_REASON
			scope:actor = { has_dynasty = yes }
			NOT = { scope:actor.dynasty = scope:recipient.dynasty }
			scope:actor.dynasty = { dynasty_prestige_level = 0 }
		}
		modifier = {
			desc = AI_DYNASTY_PRESTIGE_REASON
			scope:actor = { has_dynasty = yes }
			NOT = { scope:actor.dynasty = scope:recipient.dynasty }
			scope:actor.dynasty = { dynasty_prestige_level >= 2 }
			add = {
				value = 5
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 3 }
					}
					add = 2
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 4 }
					}
					add = 2
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 5 }
					}
					add = 2
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 6 }
					}
					add = 2
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 7 }
					}
					add = 2
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 8 }
					}
					add = 2
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 9 }
					}
					add = 2
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 10 }
					}
					add = 2
				}
			}
		}
		modifier = { #Faith compatibility
			scope:recipient.faith = {
				faith_hostility_level = {
					target = scope:actor.faith
					value > faith_fully_accepted_level
				}
			}
			NOT = {
				scope:recipient.ai_zeal <= -50
			}
			add = {
				value = -20
				if = {
					limit = {
						# Exempt certain struggle phases.
						NOT = { is_struggle_parameter_active_interfaith_marriages_available_between_involved_characters_trigger = yes }
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_astray_level
								}
							}
						}
						subtract = 20
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_hostile_level
								}
							}
						}
						subtract = 40
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_evil_level
								}
							}
						}
						subtract = 60
					}
					if = {
						limit = {
							scope:recipient.ai_zeal <= -25
						}
						divide = 2
					}
				}
			}
			desc = MARRY_DIFFERENT_FAITH_REASON
		}

		modifier = { # A Recipient is more likely to agree to an alliance requested by his liege.
			add = 20
			scope:recipient = {
				target_is_liege_or_above = scope:actor
			}
			desc = LIEGE_REASON
		}
		
	 #	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 = 30
	 #			min = -20
	 #			max = 20
	 # 		}
	 #		desc = AI_MILITARY_BALANCE_REASON
	 #	}
		# Recipient is a Claimant to Actor's titles.
		modifier = {
			scope:recipient = {
				NOT = { government_has_flag = government_is_clan }
				any_claim = {
					holder = scope:actor
				}
				NOR = {
					is_heir_of = scope:actor
					ai_greed <= -25
				}
			}
			add = {
				value = -50
				if = {
					limit = {
						scope:recipient.ai_greed >= 25
					}
					add = ai_greed
				}
				if = {
					limit = {
						NOT = {
							scope:recipient.house = scope:actor.house
						}
					}
					multiply = 2
				}
			}
			desc = IS_CLAIMANT_TO_ACTORS_TITLES
		}
		#Actor has claims on recipient's titles.
		modifier = {
			scope:actor = {
				any_claim = {
					holder = scope:recipient
				}
			}
			add = 25
			desc = IS_CLAIMANT_TO_RECIPIENT_TITLES
		}
		modifier = {
			scope:recipient = {
				NOT = {
					any_claim = {
						holder = scope:actor
					}
				}
			}
			scope:recipient.house = scope:actor.house
			add = 15
			desc = IS_SAME_HOUSE_REASON
		}
		modifier = {
			scope:recipient = {
				has_opinion_modifier = {
					target = scope:actor
					modifier = declared_war
				}
			}
			add = -100
			desc = DECLARED_WAR_REASON
		}

		# Don't form new alliances with people already at war.
		modifier = {
			scope:actor = {
				is_at_war = yes

				# Since vassals and lieges can't call us into wars, this doesn't matter for us if we have that relationship. 
				NOR = {
					target_is_liege_or_above = scope:recipient
					target_is_vassal_or_below = scope:recipient
				}
			}
			add = -50
			desc = IS_AT_WAR_REASON
		}

		#Bonus from Tradition Astute Diplomats
		modifier = {
			scope:actor.culture = { has_cultural_parameter = facilitate_alliance_acceptance }
			add = 50 #should be in a file 00_traditions_values
			desc = TRADITION_ASTUTE_DIPLOMATS_ACCEPTANCE_MODIFIER
		}

		# Feud
		modifier = {
			add = -25
			scope:recipient = {
				house_has_feud_relation_with_trigger = { TARGET = scope:actor }
			}
			desc = MARRY_FAMILY_FEUD_REASON
		}

		# Existing Alliances
		modifier = {
			add = {
				scope:actor = {
					every_ally = {
						add = -25
					}
				}
			}
			scope:actor = {
				any_ally = {
					count >= 5
				}
			}
			NAND = {
				scope:recipient = {
					government_has_flag = government_is_clan
					is_vassal_or_below_of = scope:actor
				}
			}
			desc = EXISTING_ALLIANCES_REASON
		}

		# Existing Alliances
		modifier = {
			add = {
				scope:recipient = {
					every_ally = {
						add = -25
					}
				}
			}
			scope:recipient = {
				any_ally = {
					count >= 5
				}
			}
			NAND = {
				scope:recipient = {
					government_has_flag = government_is_clan
					is_vassal_or_below_of = scope:actor
				}
			}
			desc = THEIR_EXISTING_ALLIANCES_REASON
		}
		
		modifier = {
			add = intimidated_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		
		modifier = {
			add = 10
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = 20
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}

		fp3_struggle_resist_allied_wars_modifier = yes
		
		# LOW LEGITIMACY
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			scope:actor = {
				OR = {
					has_legitimacy_flag = reduced_alliance_acceptance
					has_legitimacy_flag = very_reduced_alliance_acceptance
					has_legitimacy_flag = massively_reduced_alliance_acceptance
				}
			}
			add = {
				value = 0
				if = {
					limit = { scope:actor = { has_legitimacy_flag = reduced_alliance_acceptance } }
					add = -10
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = very_reduced_alliance_acceptance } }
					add = -20
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = massively_reduced_alliance_acceptance } }
					add = -40
				}
			}
		}

		# HIGH LEGITIMACY
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			scope:actor = {
				OR = {
					has_legitimacy_flag = increased_alliance_acceptance
					has_legitimacy_flag = very_increased_alliance_acceptance
					has_legitimacy_flag = extra_increased_alliance_acceptance
				}
			}
			add = {
				value = 0
				if = {
					limit = { scope:actor = { has_legitimacy_flag = increased_alliance_acceptance } }
					add = 10
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = very_increased_alliance_acceptance } }
					add = 20
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = extra_increased_alliance_acceptance } }
					add = 40
				}
			}
		}
	}

	ai_potential = {
		always = yes
	}
	
	ai_targets = {
		ai_recipients = neighboring_rulers
		max = 20
	}
	
	ai_frequency = 12
	
	ai_will_do = {
		base = 0
		
		modifier = {
			add = 100
			any_ally = {
				count <= 5
			}
		}

		modifier = {
			add = -100
			highest_held_title_tier = 3
			scope:recipient.highest_held_title_tier = 2
			NOR = {
				has_trait = craven
				has_trait = compassionate
				has_trait = content
			}
		}

		modifier = {
			add = -80
			scope:recipient = { is_ai = no }
		}

		modifier = {
			add = -100
			OR = {
				AND = {
					NOT = { faith = scope:recipient.faith }
					ai_zeal >= 0
				}
				opinion = {
					target = scope:recipient
					value <= 0
				}
			}
		}
	}
}
