﻿### vanilla interactions - modified ###

declare_war_interaction = {
	category = interaction_category_diplomacy
	common_interaction = yes
	special_interaction = declare_war_interaction
	interface = declare_war
	interface_priority = 70
	popup_on_receive = yes
	pause_on_receive = no # this is handled by the interface instead

	## mr_mod replace
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					has_game_rule = surprise_war_enabled
					# scope:actor has an army in scope:recipient's realm
					scope:recipient = { is_landed = yes }
					scope:actor = {
						is_landed = yes
						has_army_in_ruler_realm = { RULER = scope:recipient }
					}
				}
				desc = declare_surprise_war_desc
			}
			desc = declare_war_desc
		}
	}
	## end

	is_shown = {
		scope:recipient != scope:actor
		scope:recipient = {
			is_playable_character = yes
		}
		scope:actor = {
			NOT = {
				is_at_war_with = scope:recipient
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			custom_tooltip = {
				text = sworn_peace_tt
				NOT = { has_character_flag = sworn_peace }
			}
			NOT = { has_trait = incapable }
			can_attack_in_hierarchy = scope:recipient
			## mr_mod replace
			trigger_if = {
				limit = {
					NOT = { government_has_flag = can_start_war_with_raised_troops }
					OR = {
						has_game_rule = surprise_war_disabled
						AND = { # don't let ai get itself into more wars than it can handle
							is_ai = yes
							any_character_war = {} # one is enough for ai, lol
						}
					}
				}
				has_raised_armies = no
			}
			## end
			trigger_else = {
				custom_description = {
					text = army_blocks_defender_army
					NOT = {
						any_army = {
							location.county.holder ?= scope:recipient
						}
					}
				}
			}
			has_any_display_cb_on = scope:recipient
			NOT = {
				is_allied_in_war = scope:recipient
			}
			custom_description = {
				text = "is_not_bankrupt"
				is_in_debt = no
			}
			NOT = {
				custom_description = {
					text = "is_in_an_activity"
					exists = involved_activity
				}
			}
			trigger_if = {
				limit = {
					culture = { has_cultural_parameter = cannot_attack_allies }
				}
				NOT = {
					is_allied_to = scope:recipient
				}
			}
			trigger_if = {
				limit = {
					culture = { has_cultural_parameter = cannot_attack_truces }
				}
				NOT = {
					has_truce = scope:recipient
				}
			}
			is_imprisoned = no
			trigger_if = {
				limit = {
					has_variable_list = subjugation_offer_under_consideration
				}
				custom_description = {
					text = is_not_considering_offer_of_subjugation
					NOT = {
						is_target_in_variable_list = {
							name = subjugation_offer_under_consideration
							target = scope:recipient
						}
					}
				}
			}
		}
		scope:recipient = {
			custom_tooltip = {
				text = is_a_herder_recipient_tt
				NOT = { government_has_flag = government_is_herder } # herders can't fight
			}
			NOT = { is_tributary_of_suzerain_or_above = scope:actor } # can't fight your tributaries or your tributaries' tributaries
			NOT = { has_strong_hook = scope:actor }
			trigger_if = {
				limit = { is_imprisoned = yes }
				imprisoner != scope:actor
			}
		}
		scope:actor = {
			trigger_if = {
				limit = {
					NOT = { government_allows = administrative }
					liege ?= {
						has_realm_law_flag = vassal_all_wars_banned_permanent
						this != scope:actor
					}
				}
				custom_tooltip = {
					text = admin_all_vassal_wars_banned
					always = no
				}
			}
			trigger_if = {
				limit = {
					government_allows = administrative
					top_liege = scope:recipient.top_liege
					scope:recipient = { government_allows = administrative }
					top_liege != scope:recipient
					NOT = {
						top_participant_group:dynastic_cycle ?= {
							has_participant_group_parameter = dynastic_cycle_vassal_internal_wars_allowed
						}
					}
				}
				custom_tooltip = {
					text = admin_cannot_declare_internal_wars
					always = no
				}
			}
			trigger_if = { # Admin - Laws only allow frontier and naval themes to declare war
				limit = {
					government_allows = administrative
					top_liege != this
					top_liege = {
						this != scope:recipient.top_liege
						has_realm_law_flag = admin_vassal_wars_restricted
					}
				}
				custom_tooltip = {
					text = admin_all_vassal_wars_banned
					OR = {
						vassal_contract_has_flag = admin_theme_frontier
						vassal_contract_has_flag = admin_theme_naval
					}
				}
			}
			trigger_if = { # Admin - Laws only allow frontier and naval themes to declare war IF they have permission
				limit = {
					government_allows = administrative
					OR = {
						vassal_contract_has_flag = admin_theme_frontier
						vassal_contract_has_flag = admin_theme_naval
					}
					top_liege = {
						this != scope:recipient.top_liege
						has_realm_law_flag = admin_vassal_wars_permission_only
						NOT = { has_realm_law_flag = admin_vassal_wars_banned }
					}
				}
				custom_tooltip = {
					text = admin_vassal_wars_permission_only_desc
					has_variable = admin_permission_to_declare_war
				}
			}
			trigger_if = { # Admin - Laws prevent all vassals from declaring war
				limit = {
					government_allows = administrative
					OR = {
						vassal_contract_has_flag = admin_theme_frontier
						vassal_contract_has_flag = admin_theme_naval
					}
					top_liege = {
						this != scope:recipient.top_liege
						has_realm_law_flag = admin_vassal_wars_banned
					}
				}
				custom_tooltip = {
					text = admin_all_vassal_wars_banned
					always = no
				}
			}
			trigger_if = {
				limit = {
					scope:recipient = {
						has_subject_contract_group = tributary_celestial
					}
					is_independent_ruler = no # to avoid duplicating tooltips already applying to the top_liege
				}
				NOT = {
					scope:recipient = {
						is_tributary_of = scope:actor.top_liege
					}
				}
			}
		}
		scope:actor = {
			trigger_if = {
				limit = { has_government = landless_adventurer_government }
				custom_tooltip = {
					text = camp_located_in_defender_realm.tt
					domicile.domicile_location.county.holder = {
						NOR = {
							this = scope:recipient
							any_liege_or_above = { this = scope:recipient }
						}
					}
				}
			}
		}
		trigger_if = {
			limit = {
				scope:actor = {
					is_confederation_member = yes
				}
			}
			NOT = {
				scope:recipient = {
					is_member_of_confederation = scope:actor.confederation
				}
			}
		}
	}

	has_valid_target_showing_failures_only = {
		custom_description = {
			text = "declare_war_hook_on_liege"
			OR = {
				war_declarer_needs_hook_on_liege = no
				always = scope:hook
			}
		}
		scope:actor = {
			NOR = {
				is_at_war_with = scope:recipient
				custom_description = { #A vassal cannot go to war against someone their Liege is already at war with
					text = liege_is_at_war_with_recipient
					object = scope:recipient
					liege = {
						is_at_war_with = scope:recipient
					}
				}
			}
		}
	}

	send_options_exclusive = no
	send_option = {
		is_shown = {
			war_declarer_needs_hook_on_liege = yes
		}
		is_valid = {
			scope:actor = {
				has_usable_hook = liege
			}
		}
		flag = hook
		localization = WAR_LIEGE_HOOK
		can_invalidate_interaction = yes
	}
	should_use_extra_icon = {
		war_declarer_needs_hook_on_liege = yes
		scope:actor = { has_usable_hook = liege }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	
	on_accept = {
		scope:actor = {
			## mr_mod
			if = {
				limit = {
					has_game_rule = surprise_war_enabled
					# scope:actor has an army in scope:recipient's realm
					scope:recipient = { is_landed = yes }
					is_landed = yes
					has_army_in_ruler_realm = { RULER = scope:recipient }
				}
				trigger_event = {
					id = QOL_surprise_war.0001
					days = { 4 10 }
				}
			}
			## end

			#Feedback!
			hidden_effect = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = declare_war_interaction_notification

					left_icon = scope:actor					
					right_icon = scope:recipient

					custom_tooltip = declare_war_interaction_notification_tooltip

					show_as_tooltip = {
						if = {
							limit = { always = scope:hook }
							use_hook = liege
						}
					}
				}
			}

			#Has the instigator promised a vassal a war (vassal.2601)?
			if = {
				limit = {
					exists = var:promised_war
					var:promised_war = {
						is_alive = yes
						this != scope:recipient
					}
				}
				trigger_event = {
					id = vassal.2610
					days = { 7 10 }
				}
			}

			if = {
				limit = { always = scope:hook }
				use_hook = liege
			}

			#Send break up event if they are your lover
			if = {
				limit = {
					has_relation_lover = scope:recipient
					NOT = { has_relation_rival = scope:recipient } #To enable really strange love stories
					any_character_war = {
						casus_belli = {
							primary_attacker = scope:actor
							primary_defender = scope:recipient
							any_target_title = { }
						}
					}
				}
				scope:recipient = {
					trigger_event = {
						id = lover.0103
						days = { 14 30 }
					}
				}
			}

			#Invalidate any wars your vassals have going against recipient atm
			scope:recipient = {
				if = {
					limit = {
						any_character_war = {
							primary_attacker = {
								is_vassal_or_below_of = scope:actor
								trigger_if = { #Ai should never invalidate a player's war!
									limit = {
										this = { is_ai = no }
									}
									scope:actor = {
										is_ai = no
									}
								}
							}
						}
					}
					every_character_war = {
						limit = {
							primary_attacker = {
								is_vassal_or_below_of = scope:actor
								trigger_if = { #Ai should never invalidate a player's war!
									limit = {
										this = { is_ai = no }
									}
									scope:actor = {
										is_ai = no
									}
								}
							}
						}
						show_as_tooltip = { end_war = invalidated } #Actually ended in the event
						primary_attacker = { trigger_event = war_event.1001 }
					}
				}
			}
			#Notify defender's vassals if the war is targeting one or more of their titles
			scope:recipient = {
				if = {
					limit = {
						any_vassal = {
							save_temporary_scope_as = attacked_vassal
							any_character_war = {
								casus_belli = {
									primary_attacker = scope:actor
									primary_defender = scope:recipient
									any_target_title = {
										holder = {
											OR = {
												this = scope:attacked_vassal
												is_vassal_or_below_of = scope:attacked_vassal
											}
										}
									}
								}
							}
						}
					}
					every_vassal = {
						save_temporary_scope_as = attacked_vassal
						limit = {
							any_character_war = {
								casus_belli = {
									primary_attacker = scope:actor
									primary_defender = scope:recipient
									any_target_title = {
										holder = {
											OR = {
												this = scope:attacked_vassal
												is_vassal_or_below_of = scope:attacked_vassal
											}
										}
									}
								}
							}
						}
						send_interface_toast = {
							type = msg_war_declared_on_liege
							title = declare_war_interaction_notification

							left_icon = scope:actor					
							right_icon = scope:recipient

							custom_tooltip = declare_war_interaction_notification_tooltip
						}
					}
				}
			}
			
			# If you are admin and had permission to start war, spend said permission
			if = {
				limit = {
					has_variable = admin_permission_to_declare_war
				}
				remove_variable = admin_permission_to_declare_war
			}
		}
	}
	
	auto_accept = yes
}

### auto_building_order ###

my_building_order_interaction = {

	category = interaction_category_friendly

	desc = my_building_order_interaction_desc

	interface_priority = 120

	is_shown = {
		scope:actor = {
			is_ai = no
			NOT = { has_variable = has_its_own_order }
			this = scope:recipient
		}
	}

	is_valid_showing_failures_only = {
		has_game_rule = auto_building_order_enabled
	}

	on_accept = {
		scope:actor = {
			custom_tooltip = my_building_order_interaction_tooltip
			hidden_effect = {
				set_variable = {
					name = has_its_own_order
					value = yes
				}
			}
		}
	}

	auto_accept = yes
}

auto_building_order_interaction = {

	category = interaction_category_friendly

	desc = auto_building_order_interaction_desc

	interface_priority = 120

	is_shown = {
		scope:actor = {
			is_ai = no
			has_variable = has_its_own_order
		}
	}

	is_valid_showing_failures_only = {
		has_game_rule = auto_building_order_enabled
	}

	on_accept = {
		scope:actor = {
			custom_tooltip = auto_building_order_interaction_tooltip
			hidden_effect = {
				remove_variable = has_its_own_order
			}
		}
	}

	auto_accept = yes
}

abdicate_interaction = {

	category = interaction_category_diplomacy

	desc = abdicate_interaction_desc

	interface_priority = 120

	is_shown = {
		exists = primary_heir
		NOT = { has_game_rule = reasons_to_abdicate_disabled }
		scope:recipient = scope:actor
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			primary_heir = {
				OR = {
					is_adult = yes
					scope:actor = { ai_compassion < low_negative_ai_value }
				}
			}
			custom_description = {
				text = abdicate_interaction_reasons
				OR = {
					AND = {
						age >= 50
						health < fine_health
					}
					has_trait = incapable
					has_trait = impotent
					has_trait = infirm
					trigger_if = {
						limit = { has_game_rule = reasons_to_abdicate_enabled }
						OR = {
							has_trait = content
							has_trait = humble
							trigger_if = {
								limit = { is_ai = yes }
								ai_greed < high_negative_ai_value
							}
							trigger_else = {
								ai_greed < low_positive_ai_value
							}
							trigger_if = {
								limit = { ai_rationality > medium_positive_ai_value }
								OR = {
									diarch ?= primary_heir
									designated_diarch ?= primary_heir
								}
								primary_heir.total_skill > scope:actor.total_skill
							}
						}
						NOR = {
							has_trait = ambitious
							has_trait = arrogant
						}
					}
				}
			}
		}
	}

	on_accept = {
		scope:actor = {
			depose = yes
		}
	}

	auto_accept = yes

	ai_will_do = {
		base = 0
		modifier = {
			stress_level > 0
			add = 10
		}
		modifier = {
			stress_level > 1
			add = 20
		}
		modifier = {
			stress_level > 2
			add = 30
		}
		modifier = {
			OR = {
				has_game_rule = hard_difficulty
				has_game_rule = very_hard_difficulty
			}
			add = {
				value = primary_heir.total_skill
				subtract = scope:actor.total_skill
			}
		}
	}
}

toggle_knighthood_interaction = {

	category = interaction_category_friendly

	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:actor = { has_variable = mr_player_un_knight } }
				desc = enable_knighthood_interaction_desc
			}
			desc = disable_knighthood_interaction_desc
		}
	}

	interface_priority = 120

	is_shown = {
		has_game_rule = player_can_be_knight_enabled
		scope:actor = {
			is_ai = no
			scope:actor = scope:recipient
		}
	}

	is_valid_showing_failures_only = {
	}

	on_accept = {
		scope:actor = {
			if = {
				limit = { has_variable = mr_player_un_knight }
				custom_tooltip = enable_knighthood_interaction_tooltip
				hidden_effect = {
					remove_variable = mr_player_un_knight
				}
			}
			else = {
				custom_tooltip = disable_knighthood_interaction_tooltip
				hidden_effect = {
					set_variable = {
						name = mr_player_un_knight
						value = yes
					}
				}
			}
		}
	}

	auto_accept = yes
}