﻿
#########################################################################
# Vassal Swear Fealty to Heir
# Developed by idev
#########################################################################
fealty_to_heir_interaction = {
	category = interaction_category_diplomacy
 #	common_interaction = yes
	notification_text = liege_dismiss_entrenched_regency_interaction_proposal
	icon = court_gardener_court_position

	desc = fealty_to_heir_interaction_desc

	can_send_despite_rejection = yes
	popup_on_receive = yes
	pause_on_receive = yes

	interface_priority = 60
	ai_min_reply_days = 4
	ai_max_reply_days = 9

	is_shown = {
		has_game_rule = GPT_interactions_enabled
		# Be'ith not thyself
		NOT = { scope:actor = scope:recipient }
		# Character is vassal of of player
		scope:recipient = {
			is_powerful_vassal_of = scope:actor
			highest_held_title_tier >= 2
			is_landed = yes
		}
		scope:actor = {
			is_landed = yes
			highest_held_title_tier >= 4
		}
		# Heir does not wish to play his part as his imaginary friend / vassal
		NOT = {
			scope:recipient = scope:actor.player_heir
		}
		# Heir is not already allied to vassal
		NOT = {
			scope:actor.player_heir = {
				is_allied_to = scope:recipient
			}
		}
	}

	cooldown = { years = 3 }


	can_send = {
		AND = {
			scope:actor = {
				AND = {
					exists = scope:actor.player_heir
					is_ruler = yes
					is_ai = no
				}
			}

		}
	}


	# Costs inclusive of send options picked by player
	send_options_exclusive = yes

	# Prestige
	send_option = {
		starts_enabled = { scope:actor.prestige >= scope:actor.swing_scales_prestige_cost_value }
		flag = swing_prestige
		localization = GPT_SWING_PRESTIGE
		current_description = m_prestige_option_desc
	}
	# Piety
	send_option = {
		is_valid = {
			# You must share a HoF to be able to petition one.
			custom_tooltip = {
				text = swing_scales_currency_interaction.tt.no_shared_hof
				# We're also secretly just testing if characters are the same faith.
				OR = {
					scope:actor.faith = scope:recipient.faith
					AND = {
						exists = scope:actor.faith.religious_head
						exists = scope:recipient.faith.religious_head
						scope:actor.faith.religious_head = scope:recipient.faith.religious_head
					}
				}
			}
		}
		starts_enabled = { scope:actor.piety >= scope:actor.swing_scales_piety_cost_value }
		flag = swing_piety
		localization = GPT_SWING_PIETY
		current_description = m_piety_option_desc
	}
	# Gold
	send_option = {
		starts_enabled = { scope:actor.gold >= scope:actor.swing_scales_cash_cost_value }
		flag = swing_gold
		localization = GPT_SWING_GOLD
		current_description = m_gold_option_desc
	}
	# Hook
	send_option = {
		is_valid = {
			scope:actor = { has_usable_hook = scope:recipient }
		}
		starts_enabled = {
			scope:actor = { has_usable_hook = scope:recipient }
		}
		flag = swing_hook
		localization = SWING_HOOK
		current_description = m_hook_option_desc
	}
	# HoF
	send_option = {
		is_valid = { diarch_swing_scales_valid_shared_hof_trigger = yes }
		# Top of the prio order for enablement.
		starts_enabled = { diarch_swing_scales_valid_shared_hof_trigger = yes }
		flag = swing_hof
		localization = SWING_HOF
		current_description = m_hof_option_desc
	}

	on_accept = {
		
		# Player appeciates vassal's support of their heir
		scope:actor = {
			add_opinion = {
				modifier = pleased_opinion
				target = scope:recipient
				opinion = 15
			}
			# Letter event to inform player of positive decision - note, this actually fires event, goes through it, then continues to reallocate resources depending on choice!
			trigger_event = {
				id = mod_fealty_to_heir.0001
			}

			if = {
				limit = { scope:swing_gold = yes }
				scope:actor = {
					pay_short_term_gold = {
						target = scope:recipient
						gold = {
							value = 80
							multiply = scope:recipient.highest_held_title_tier
						}
					}
				} 
			}
			else_if = {
				limit = { scope:swing_prestige = yes }
				scope:actor = {
					add_prestige = {
						value = -160
						multiply = scope:recipient.highest_held_title_tier
					}
				}
				scope:recipient = {
					add_prestige = {
						value = 160
						multiply = scope:recipient.highest_held_title_tier
					}
				}
			}
			else_if = {
				limit = { scope:swing_piety = yes }
				scope:actor = {
					add_piety = {
						value = -120
						multiply = scope:recipient.highest_held_title_tier
					}
				}
				scope:recipient = {
					add_piety = {
						value = 120
						multiply = scope:recipient.highest_held_title_tier
					}
				}
			}
			else_if = {
				limit = { scope:swing_hook = yes }
				scope:actor = {
					use_hook = scope:recipient
				}
			}
			else_if = {
				limit = { scope:swing_hof = yes }
				scope:actor = {
					pay_short_term_gold = {
						target = scope:actor.faith.religious_head
						gold = {
							value = 30
							multiply = scope:recipient.highest_held_title_tier
						}
					}
					add_piety = {
						value = -75
						multiply = scope:recipient.highest_held_title_tier
					}
				}
			}


		}
		# Heir
		scope:actor.player_heir = {
			add_opinion = {
				modifier = pleased_opinion
				target = scope:recipient
				opinion = 25
			}

			add_hook = {
				target = scope:recipient
				type = loyalty_hook
			}
			
		 #	create_alliance = {
		 #		target = scope:recipient
		 #		allied_through_owner = scope:actor.player_heir
		 #		allied_through_target = scope:recipient
		 #	}
		}

		# Vassal
		scope:recipient = {
			add_opinion = {
				target = scope:actor
				modifier = pleased_opinion
				opinion = 10
			}
		}

	}
	
	auto_accept = {
		
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:swing_hook = yes
			scope:actor = { has_strong_hook = scope:recipient }
		}
	}

	on_decline = {
		scope:actor = {
			
			trigger_event = {
				id = mod_fealty_to_heir.0002
			}

			if = {
				limit = { is_ai = no }
				add_opinion = {
					target = scope:recipient
					modifier = trust_opinion
					opinion = -30
				}
			}
		}

	}

	
	ai_accept = {

		base = -50
		#############################################################
		# Additional acceptance modifiers - greater specificity
		#############################################################

		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.6
			desc = AI_OPINION_REASON
		}
		# More likely to accept/decline if vassal likes/dislikes heir
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor.player_heir
			multiplier = 0.4
			desc = AI_OPINION_REASON
		}
		# Vassal is imprisoned by scope:actor
		modifier = {
			scope:recipient = {
				is_imprisoned_by = scope:actor
			}
			add = {
				value = 50
			}
			desc = m_vassal_imprisoned_by_you.tt.accept_chance
		}
		# More likely to accept if shared HOF is involved
		modifier = {
			add = 10
			desc = m_leige_uses_hof_interaction.tt.accept_chance
			scope:swing_hof = yes
		}

		# Vassal is in debt
		modifier = {
			add = 60
			desc = PT_AI_IS_IN_DEBT
			scope:swing_gold = yes
			debt_level >= 1
		}
		# Vassal is greedy
		modifier = {
			add = {
				value = scope:recipient.ai_greed
				min = -75
				max = 75
			}
			desc = gpt_ss_gold
			scope:swing_gold = yes
		}
		# Vassal is zealous and is recieving piety
		modifier = {
			add = {
				value = scope:recipient.ai_zeal
				min = -75
				max = 75
			}
			desc = gpt_ss_piety
			scope:swing_piety = yes
		}
		# Vassal is zealous and interaction is hof
		modifier = {
			add = 50
			desc = gpt_ss_hof
			scope:swing_hof = yes
		}
		modifier = {
			add = 35
			desc = m_zealous_likes_hof.tt
			scope:swing_hof = yes
			has_trait = zealous
		}
		# Weak hook is being used
		modifier = {
			add = 50
			desc = m_leige_uses_weak_hook_interaction.tt.accept_chance
			scope:swing_hook = yes
		}
		# Vassal is ambitious and is recieving prestige
		modifier = {
			add = {
				value = scope:recipient.ai_sociability
				min = -75
				max = 75
			}
			desc = gpt_ss_prestige
			scope:swing_prestige = yes
		}
		# Cynical vassal dislikes religious interaction: hof
		modifier = {
			add = -15
			desc = m_cynical_dislikes_hof.tt
			scope:swing_hof = yes
			has_trait = cynical
		}

		modifier = {
			add = {
				value = scope:recipient.ai_honor
				min = -50
				max = 50
			}
			desc = gpt_ss_honor
		}

		modifier = {
			add = {
				value = scope:actor.player_heir.age
				multiply = -2
			}
			desc = GPT_HEIR_AGE
		}

		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 = 4
				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
		}
		
		# 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 = 20
			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
		}

		# Feud
		modifier = {
			add = -25
			scope:recipient = {
				house_has_feud_relation_with_trigger = { TARGET = scope:actor }
			}
			desc = MARRY_FAMILY_FEUD_REASON
		}
		
		modifier = {
			add = 25
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = 50
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
	}

	#  AI off unless requested by users oe further development. REASON: As little disruption to vanilla game flow as possible 
	# ai_potential = {
	# 	always = no
	# }
	
	# ai_targets = {
	# 	ai_recipients = dynasty
	# 	max = 20
	# }
	
	# ai_targets = {
	# 	ai_recipients = family
	# 	max = 10
	# }
	
	# ai_targets = {
	# 	ai_recipients = spouses
	# }
	# ai_frequency = 72
	
	# ai_will_do = {
	# 	base = 0
		
	# 	modifier = {
	# 		add = 100
	# 		any_ally = {
	# 			count < 2
	# 		}
	# 		scope:recipient = { # Don't propose alliances to tiny insignificant rulers just because
	# 			OR = {
	# 				current_military_strength >= scope:actor.eighty_percent_of_current_military_strength
	# 				primary_title.tier >= scope:actor.primary_title.tier
	# 				AND = {
	# 					primary_title.tier >= tier_duchy
	# 					scope:actor.primary_title.tier < tier_empire
	# 				}
	# 			}
	# 		}
	# 	}
		
	# 	modifier = { # Clans always want alliances with Vassals
	# 		add = 100
	# 		scope:actor = {
	# 			government_has_flag = government_is_clan
	# 		}
	# 		scope:recipient = {
	# 			is_vassal_of = scope:actor
	# 		}
	# 	}
		
	# 	modifier = { # The AI shouldn't propose to their Lieges
	# 		factor = 0
	# 		is_liege_or_above_of = scope:recipient
	# 	}
		
	# 	modifier = { # The AI shouldn't propose to their Vassals unless Clan
	# 		factor = 0
	# 		scope:actor = {
	# 			NOT = { government_has_flag = government_is_clan }
	# 		}
	# 		scope:recipient = {
	# 			is_vassal_of = scope:actor
	# 		}
	# 	}
	# }


}
