﻿@designate_heir_cost = 1000

nht_send_volunteer_soldier_interaction = {
	interface_priority = 30
	category = interaction_category_diplomacy
	common_interaction = yes
	desc = nht_send_volunteer_soldier_interaction_desc
	icon = nht_levy

	greeting = positive
	notification_text = NHT_SEND_VOLUNTEER_SOLDIER_NOTIFICATION
	popup_on_receive = yes
	ai_maybe = yes

	cooldown_against_recipient = { years = 15 }

	is_shown = {
		# Scope:actor needs to have a specific perk	
		scope:actor = {
			NOT = { government_has_flag = government_is_landless_adventurer }
		}
		scope:actor = {
			is_ruler = yes
			OR = {
				has_perk = nht_joint_forces_perk
				has_game_rule = nht_send_volunteer_soldier_interaction_no_requi
			}
		}
		# Scope:recipient must be landed, and neither over or under scope:actor. and at war
		scope:recipient = {
			is_at_war = yes
			is_ruler = yes			
			NOR = {
				this = scope:actor
				is_at_war_with = scope:actor
				is_allied_to = scope:actor
				any_liege_or_above = { this = scope:actor }
				any_vassal_or_below = { this = scope:actor }				
			}
		}
	}

	is_valid_showing_failures_only = {
		# Scope:actor must be an available (adult or child).
		scope:actor = { is_available = yes }
		
		# Make sure that scope:actor is going into this with a clean, no-recent/active/forbidden hostilities mindset.
		scope:actor = {
			NOR = {
				# Involved characters can't be on opposite sides in an active war.
				is_at_war_with = scope:recipient
				# Allied characters aren't allowed to do this because it already in war. 
				is_allied_to = scope:recipient
			}
			is_imprisoned = no
		}
	}

	# Hooks can be used to force this through.
	## We don't force-force this on/for the player (so no pain but less benefits), but AI regard weak hooks as compulsory from/for other AI.
	send_options_exclusive = no
	send_option = {
		is_valid = {
			scope:actor = { has_usable_hook = scope:recipient }
		}
		flag = hook
		localization = SCHEME_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	on_auto_accept = {
		scope:actor = {
			nht_send_volunteer_solider_interaction_levy_give_modifier_effect = yes
			if = {
				limit = {
					scope:hook = yes
					has_hook = scope:recipient
				}
				use_hook = scope:recipient
			}
		}	
		scope:recipient = {
			nht_send_volunteer_solider_interaction_levy_receive_modifier_effect = yes
		}	
	}
	
	
	on_accept = {
		scope:actor = {
			nht_send_volunteer_solider_interaction_levy_give_modifier_effect = yes
		}	
		scope:recipient = {
			nht_send_volunteer_solider_interaction_levy_receive_modifier_effect = yes
			add_opinion = {
				modifier = nht_accepted_volunteer_opinion
				target = scope:actor
				opinion = 20
			}
		}
		# Make sure the AI doesn't spam players
		scope:recipient = {
			if = {
				limit = {
					is_ai = no
				}
				add_character_flag = {
					flag = nht_send_volunteer_block
					years = 5
				}
			}
		}		
	}

	on_decline = {
		# Scope:actor gains some stress over the whole ordeal.
		scope:recipient = {
			nht_send_volunteer_solider_interaction_refused_help_modifier_effect = yes
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = no
				}
				add_character_flag = {
					flag = nht_send_volunteer_block
					years = 5
				}
			}
		}
	}

	# AI
	## Standard Acceptance stuff
	ai_accept = {
		# Try to make it 0 for most interactions.
		base = 80

		#Stuck in a long lasting war
		modifier = {
			add = 100
			scope:recipient = {
				days_of_continuous_war >= 3650
			}
			desc = INTERACTION_NHT_ENDLESS_WAR
		}	
	
	
		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 = -10
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_astray_level
							}
						}
					}
					add = 5
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_hostile_level
							}
						}
					}
					subtract = 20
				}
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value >= faith_evil_level
							}
						}
					}
					subtract = 30
				}
				if = {
					limit = {
						scope:recipient.ai_zeal <= -25
					}
					divide = 2
				}
			}
			desc = MARRY_DIFFERENT_FAITH_REASON
		}	
		### Traits Modifiers
		modifier = {
			add = 20
			scope:recipient = {
				has_trait = trusting
			}
			desc = INTERACTION_TRUSTING
		}
		#Don't want any help
		modifier = {
			add = -20
			scope:recipient = {
				has_trait = arrogant
			}
			desc = INTERACTION_ARROGANT
		}
		#Crazy mf
		modifier = {
			add = -20
			scope:recipient = {
				has_trait = paranoid
			}
			desc = INTERACTION_PARANOID
		}
				
		## Opinion
		# AI take opinion into account, but reduced so they'll at least occasionally accept from folks they dislike.
		opinion_modifier = {
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.5
			desc = AI_OPINION_REASON
		}
		# 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"
		}
		# Feud
		modifier = {
			add = -25
			scope:recipient = {
				house_has_feud_relation_with_trigger = { TARGET = scope:actor }
			}
			desc = MARRY_FAMILY_FEUD_REASON
		}		
		
		

		# Boldness always plays a factor.
		modifier = {
			add = {
				value = ai_boldness
				multiply = -0.5
			}
			NOT = { ai_boldness = 0 }
			desc = AI_VALUE_MODIFIER_BOLDNESS
		}
		# Ambitious AI like freedom.
		modifier = {
			add = -50
			has_trait = ambitious
			desc = PT_AI_LIKES_FREEDOM_TO_WAR_REASON
		}


		### Kings need a lot more.
		modifier = {
			add = -20
			scope:recipient = {
				highest_held_title_tier = tier_kingdom
			}
			desc = NHT_IS_KING_DONT_WANT_HELP
		}
		### Emperors take quite a bit to get fussed.
		modifier = {
			add = -30
			scope:recipient = {
				highest_held_title_tier = tier_empire
			}
			desc = NHT_IS_EMPEROR_DONT_WANT_HELP
		}
		
		# Relative power. If felt treathen by actor
		## Scope:actor has far fewer levies than scope:recipient.
		modifier = {
			add = 20
			scope:actor.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_far_weaker_value
			desc = PT_AI_RECIPIENT_IS_MUCH_STRONGER
		}
		## Scope:actor has fewer levies than scope:recipient.
		modifier = {
			add = 10
			scope:actor.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_far_weaker_value
			scope:actor.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_somewhat_weaker_value
			desc = PT_AI_RECIPIENT_IS_SOMEWHAT_STRONGER
		}
		## Scope:actor has roughly equal levies with scope:recipient.
		modifier = {
			add = 10
			scope:actor.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_somewhat_weaker_value
			scope:actor.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_somewhat_stronger_value
			desc = PT_AI_RECIPIENT_IS_ROUGHLY_EQUAL
		}
		## Scope:actor has more levies than scope:recipient.
		modifier = {
			add = -5
			scope:actor.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_somewhat_stronger_value
			scope:actor.max_military_strength <= scope:recipient.purchase_truce_interaction_recipient_far_stronger_value
			desc = PT_AI_RECIPIENT_IS_SOMEWHAT_WEAKER
		}
		## Scope:actor has far more levies than scope:recipient.
		modifier = {
			add = -10
			scope:actor.max_military_strength > scope:recipient.purchase_truce_interaction_recipient_far_stronger_value
			desc = PT_AI_RECIPIENT_IS_MUCH_WEAKER
		}
	}

	## Performance-enhancement	
	ai_potential = {
		is_landed = yes
		is_at_war = yes
		is_imprisoned = no
		# Matches the regular minimum triggers.
		has_perk = nht_joint_forces_perk
	}

	ai_target_quick_trigger = { adult = yes }
	
	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = peer_vassals		
		max = 3
	}

	## Frequency
	ai_frequency = 60
	
	ai_will_do = {
		base = 0

		modifier = {
			add = -500
			scope:recipient = {
				has_opinion_modifier = { 
					target = scope:actor
					modifier = nht_accepted_volunteer_opinion
				}
			}
		}
		# Factor for opinion.
		modifier = {
			add = -300
			scope:actor = {
				opinion = {
					target = scope:recipient
					value < very_high_negative_opinion
				}
			}
		}
		# Some personalities & traits just don't want to fight.
		## Cravens (who aren't also ambitious).
		modifier = {
			add = -50
			has_trait = craven
			NOT = { has_trait = ambitious }
		}
		## Those with chronically low boldness (who aren't also ambitious).
		modifier = {
			add = -25
			ai_boldness <= very_high_negative_ai_value
			NOT = { has_trait = ambitious }
		}
		## Pacifists.
		modifier = {
			add = -25
			faith = { has_doctrine_parameter = pacifist_opinion_active }
		}
		modifier = {
			add = -25
			culture = { has_cultural_parameter = reduced_cost_for_conversion_to_pacifist_faith }
		}
		## Wrathful or ambitious characters don't like to bother.
		modifier = {
			add = 25
			has_trait = ambitious
		}
		modifier  = {
			add = 25
			has_trait = wrathful
		}

		modifier = { # Will not do if AI is at war
			add = -5000
			is_at_war = yes
		}
		## AI Don't do it more than once at a time.
		modifier = {
			factor = 0
			scope:actor = {
				has_character_modifier = nht_is_giving_soldiers_modifier
			}			
		}		
		## Don't send help to dead men.
		modifier = {
			factor = 0
			OR = {
				health <= poor_health
				scope:recipient = { health <= poor_health }
			}
		}		
		## Don't spam players when the decline block is active
		modifier = {
			factor = 0
			scope:recipient = {
				has_character_flag = nht_send_volunteer_block
			}
		}
	}
}


nht_declare_as_heir_interaction = {
	icon = icon_nht_declare_as_heir

	category = interaction_category_diplomacy

	desc = nht_declare_as_heir_interaction_desc

	is_shown = {
		# Scope:actor needs to have a specific perk	
		scope:recipient.faith = scope:actor.faith		
		scope:actor = {
			NOT = { government_has_flag = government_is_landless_adventurer }
		}
		scope:actor = {
			is_ruler = yes
			OR = {
				has_perk = nht_inheritance_instinct_perk
				has_game_rule = nht_declare_as_heir_interaction_no_requi
			}
		}
		NOR = {
			scope:recipient = scope:actor.player_heir
			scope:recipient = scope:actor			
			scope:recipient = scope:actor.father
			scope:recipient = scope:actor.mother
			scope:actor = { is_heir_of = scope:recipient }
			scope:recipient = { is_great_grandparent_of = scope:actor }
		}
		NOT = { scope:actor = { has_variable = nht_has_changed_heir } }
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			NOR = {
				has_trait = disinherited
				has_trait = eunuch
				has_trait = devoted
				has_trait = order_member
			}
			is_imprisoned = no
		}
		scope:actor = { 
			prestige >= @designate_heir_cost 
			is_imprisoned = no
		}
	}

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

	on_accept = {
		scope:actor.player_heir = {
			add_trait = nht_disgraced_heir
			
			if = {
				limit = { age <= 8 }
				add_opinion = {
					target = scope:recipient
					modifier = nht_prefered_as_heir_opinion
					opinion = -40
				}
				add_opinion = {
					target = scope:actor
					modifier = nht_disrupted_line_succession_opinion
					opinion = -20
				}				
			}	
			else_if = {
				limit = { age > 8 }
				add_opinion = {
					target = scope:recipient
					modifier = nht_prefered_as_heir_opinion
					opinion = -60
				}
				add_opinion = {
					target = scope:actor
					modifier = nht_disrupted_line_succession_opinion
					opinion = -50
				}	
				set_relation_rival = {
					target = scope:recipient
					reason = rival_was_chosen_as_heir
				}
			}
		}
		scope:actor = {
			if = {
				limit = { is_ai = no }
				set_variable = {
					name = nht_has_changed_heir
					years = 10
				}
			}
			else = {
				set_variable = nht_has_changed_heir
			}	
			send_interface_toast = {
				title = designate_heir_interaction_notification
				left_icon = scope:actor					
				right_icon = scope:recipient
				
				scope:recipient = {			
					add_prestige = 500
					nht_give_controversial_heir_trait_effect = yes
				}
				set_designated_heir_unsafe = scope:recipient
				add_prestige = { subtract = @designate_heir_cost }
			}
			every_close_family_member = {
				limit = { 
					is_child_of = scope:actor
					NOR = { 
						is_player_heir_of = scope:actor	
						this = scope:recipient
					}
				}
				add_opinion = {
					target = scope:recipient
					modifier = nht_disrupted_line_succession_opinion
					opinion = -20
				}
			}			
		}
		
	}

	auto_accept = yes

	## Performance-enhancement	
	ai_potential = {
		is_landed = yes
		is_imprisoned = no
		# Matches the regular minimum triggers.
		has_perk = nht_inheritance_instinct_perk
	}
	
	ai_targets = {
		ai_recipients = children
	}

	## Frequency
	ai_frequency = 60
	
	ai_will_do = {
		base = -1000

		modifier = {
			add = 1020
			scope:actor.player_heir = {
				has_trait = incapable
			}
		}
		# Factor for opinion.
		modifier = {
			add = -300
			scope:actor = {
				opinion = {
					target = scope:recipient
					value < high_negative_opinion
				}
			}
		}		
		## Don't send help to dead men.
		modifier = {
			factor = 0
			OR = {
				health <= poor_health
				scope:recipient = { health <= poor_health }
			}
		}		
	}
}