﻿# What the mod remembers about each family tie, and renewing it at succession.
#
# Lists kept on the human ruler:
#   nmt_choice_alliance_v1 / nmt_choice_truce_v1 / nmt_choice_neither_v1
#       the choice made for each in-law ruler; copied to the heir at succession
#   nmt_inherited_alliance_v1 / _truce_v1 / _neither_v1
#       the predecessor's choices, used once by the succession review, then cleared
#   nmt_known_ties_v1
#       partners (spouses/betrothed) the betrothal sweep has already looked at
#   nmt_waiting_truce_v1
#       in-laws who chose Truce while another truce was running (see nmt_apply_truce_effect)

# ---------------------------------------------------------------------------
# Recorded choices
# ---------------------------------------------------------------------------
# this = the human ruler, $RULER$ = in-law ruler, $CHOICE$ = alliance / truce / neither
nmt_record_choice_effect = {
	nmt_forget_choice_effect = { RULER = $RULER$ }
	add_to_variable_list = { name = nmt_choice_$CHOICE$_v1 target = $RULER$ }
}

# this = the human ruler, $RULER$ = in-law ruler
nmt_forget_choice_effect = {
	remove_list_variable = { name = nmt_choice_alliance_v1 target = $RULER$ }
	remove_list_variable = { name = nmt_choice_truce_v1 target = $RULER$ }
	remove_list_variable = { name = nmt_choice_neither_v1 target = $RULER$ }
}

# this = the human ruler (scope:nmt_player is saved by the refresh).
# Runs once per character. Choices made before this version were not recorded, so work
# them out from what is in place now: the opinion marker means Alliance or Truce
# (allied = Alliance), no marker means Neither. Direct ruler unions are never recorded.
nmt_choice_migration_effect = {
	if = {
		limit = {
			NOT = { has_variable = nmt_choice_migrated_v1 }
		}
		set_variable = nmt_choice_migrated_v1
		if = {
			limit = { has_variable_list = nmt_partners_v2 }
			every_in_list = {
				variable = nmt_partners_v2
				limit = { is_alive = yes }
				save_scope_as = nmt_migrate_ruler
				scope:nmt_player = {
					if = {
						limit = {
							NOT = { nmt_is_direct_partner_trigger = { RULER = scope:nmt_migrate_ruler } }
							NOT = { is_target_in_variable_list = { name = nmt_choice_alliance_v1 target = scope:nmt_migrate_ruler } }
							NOT = { is_target_in_variable_list = { name = nmt_choice_truce_v1 target = scope:nmt_migrate_ruler } }
							NOT = { is_target_in_variable_list = { name = nmt_choice_neither_v1 target = scope:nmt_migrate_ruler } }
						}
						if = {
							limit = {
								nmt_has_marriage_opinion_trigger = { TARGET = scope:nmt_migrate_ruler }
								is_allied_to = scope:nmt_migrate_ruler
							}
							nmt_record_choice_effect = { RULER = scope:nmt_migrate_ruler CHOICE = alliance }
						}
						else_if = {
							limit = { nmt_has_marriage_opinion_trigger = { TARGET = scope:nmt_migrate_ruler } }
							nmt_record_choice_effect = { RULER = scope:nmt_migrate_ruler CHOICE = truce }
						}
						else = {
							nmt_record_choice_effect = { RULER = scope:nmt_migrate_ruler CHOICE = neither }
						}
					}
				}
			}
		}
	}
}

# ---------------------------------------------------------------------------
# Upgrading a Truce/Neither bond to an alliance (nmt_propose_inlaw_alliance_interaction)
# ---------------------------------------------------------------------------
# this = the human ruler, $RULER$ = the in-law ruler who accepted.
# The alliance runs through the married/betrothed couple linking the two families, or
# between the two rulers themselves if no couple is found (vanilla does both, e.g. in
# Negotiate Alliance). The game will not create an alliance while the two have a truce,
# so our marriage truce is cancelled first; the interaction is not valid while someone
# else's truce (nmt_waiting_truce_v1) is running.
# The result is not checked here: a new alliance only shows up some time after
# create_alliance, and an earlier check that undid the upgrade when it did not see the
# alliance yet ended up cancelling the alliance the game then made.
nmt_upgrade_to_alliance_effect = {
	save_scope_as = nmt_player
	$RULER$ = { save_scope_as = nmt_ruler }
	nmt_find_tie_couple_effect = { RULER = $RULER$ }
	hidden_effect = {
		if = {
			limit = {
				OR = {
					has_truce = $RULER$
					$RULER$ = { has_truce = scope:nmt_player }
				}
			}
			cancel_truce_both_ways = $RULER$
		}
	}
	# The prestige was paid to ask; no marriage reward on top.
	set_variable = nmt_renewing_bonds
	if = {
		limit = {
			exists = scope:nmt_couple_owner
			exists = scope:nmt_couple_target
		}
		nmt_apply_alliance_effect = {
			RULER = $RULER$
			PLAYER = scope:nmt_player
			OWNER = scope:nmt_couple_owner
			TARGET = scope:nmt_couple_target
		}
	}
	else = {
		nmt_apply_alliance_effect = {
			RULER = $RULER$
			PLAYER = scope:nmt_player
			OWNER = scope:nmt_player
			TARGET = $RULER$
		}
	}
	remove_variable = nmt_renewing_bonds
	nmt_remove_snub_effect = { RULER = $RULER$ PLAYER = scope:nmt_player }
	hidden_effect = {
		remove_list_variable = { name = nmt_waiting_truce_v1 target = $RULER$ }
	}
	send_interface_toast = {
		type = event_toast_effect_good
		title = nmt_propose_inlaw_alliance_accepted
		left_icon = $RULER$
	}
}

# this = the human ruler. Saves scope:nmt_couple_owner (the player or a close relative)
# and scope:nmt_couple_target (their spouse/betrothed) for a tie remembered as being
# with $RULER$, if there is one.
nmt_find_tie_couple_effect = {
	nmt_find_tie_couple_in_effect = { RULER = $RULER$ }
	every_close_family_member = {
		limit = { is_alive = yes }
		nmt_find_tie_couple_in_effect = { RULER = $RULER$ }
	}
}

# this = the human ruler or a close relative
nmt_find_tie_couple_in_effect = {
	if = {
		limit = {
			NOT = { exists = scope:nmt_couple_target }
		}
		save_scope_as = nmt_couple_candidate
		if = {
			limit = {
				any_spouse = { nmt_partner_tied_to_trigger = { RULER = $RULER$ } }
			}
			random_spouse = {
				limit = { nmt_partner_tied_to_trigger = { RULER = $RULER$ } }
				save_scope_as = nmt_couple_target
			}
			scope:nmt_couple_candidate = { save_scope_as = nmt_couple_owner }
		}
		else_if = {
			limit = {
				is_betrothed = yes
				betrothed = { nmt_partner_tied_to_trigger = { RULER = $RULER$ } }
			}
			betrothed = { save_scope_as = nmt_couple_target }
			scope:nmt_couple_candidate = { save_scope_as = nmt_couple_owner }
		}
	}
}

# ---------------------------------------------------------------------------
# Known ties (replaces the old global nmt_tie_known character flag)
# ---------------------------------------------------------------------------
# this = the human ruler, $PARTNER$ = a spouse/betrothed of the player or a close relative
nmt_mark_tie_known_effect = {
	if = {
		limit = {
			NOT = { is_target_in_variable_list = { name = nmt_known_ties_v1 target = $PARTNER$ } }
		}
		add_to_variable_list = { name = nmt_known_ties_v1 target = $PARTNER$ }
	}
}

# this = the human ruler. Runs in the refresh after nmt_discover_effect, which fills
# nmt_current_partners. A partner whose tie has ended is forgotten, so a later betrothal
# of the same person to another relative is offered again.
nmt_prune_known_ties_effect = {
	if = {
		limit = { has_variable_list = nmt_known_ties_v1 }
		every_in_list = {
			variable = nmt_known_ties_v1
			limit = {
				NOT = { is_in_list = nmt_current_partners }
			}
			add_to_temporary_list = nmt_stale_ties
		}
		every_in_list = {
			list = nmt_stale_ties
			save_scope_as = nmt_stale_partner
			scope:nmt_player = {
				remove_list_variable = { name = nmt_known_ties_v1 target = scope:nmt_stale_partner }
			}
		}
	}
}

# ---------------------------------------------------------------------------
# Truces waiting for an older truce to run out, or for an alliance to be cancelled
# ---------------------------------------------------------------------------
# this = the human ruler (scope:nmt_player is saved by the refresh). Runs after the cleanup,
# so rulers whose tie has ended are already off the list.
nmt_waiting_truce_sweep_effect = {
	if = {
		limit = { has_variable_list = nmt_waiting_truce_v1 }
		every_in_list = {
			variable = nmt_waiting_truce_v1
			add_to_temporary_list = nmt_waiting_check
		}
		every_in_list = {
			list = nmt_waiting_check
			save_scope_as = nmt_waiting_ruler
			scope:nmt_player = {
				if = {
					limit = { scope:nmt_waiting_ruler = { is_alive = no } }
					remove_list_variable = { name = nmt_waiting_truce_v1 target = scope:nmt_waiting_ruler }
				}
				# Wait until no truce is left in either direction (war truces can be one-way)
				# and the two are no longer allied (no truce is possible between allies).
				else_if = {
					limit = {
						NOT = { has_truce = scope:nmt_waiting_ruler }
						NOT = { scope:nmt_waiting_ruler = { has_truce = scope:nmt_player } }
						NOT = { is_allied_to = scope:nmt_waiting_ruler }
					}
					remove_list_variable = { name = nmt_waiting_truce_v1 target = scope:nmt_waiting_ruler }
					# The marker is gone if a war broke the bond in the meantime.
					if = {
						limit = { nmt_has_marriage_opinion_trigger = { TARGET = scope:nmt_waiting_ruler } }
						nmt_add_marriage_truce_effect = { RULER = scope:nmt_waiting_ruler }
						send_interface_toast = {
							type = event_toast_effect_neutral
							title = nmt_truce_started_toast
							left_icon = scope:nmt_waiting_ruler
						}
					}
				}
			}
		}
	}
}

# this = the human ruler (scope:nmt_player is saved by the refresh). Runs after the
# waiting-truce sweep.
# Repairs a Truce choice whose truce is missing although the ties still stand (the opinion
# marker is there): e.g. Truce was chosen for a betrothal while still allied, before
# truces waited for the alliance to go. Only with "While Ties Last", since a 10/25-year
# marriage truce is meant to run out.
nmt_truce_repair_effect = {
	if = {
		limit = {
			has_variable_list = nmt_choice_truce_v1
			has_game_rule = nmt_truce_length_ties
		}
		every_in_list = {
			variable = nmt_choice_truce_v1
			limit = {
				is_alive = yes
				nmt_has_marriage_opinion_trigger = { TARGET = scope:nmt_player }
				NOT = { is_allied_to = scope:nmt_player }
				NOT = { has_truce = scope:nmt_player }
			}
			add_to_temporary_list = nmt_truce_repair
		}
		every_in_list = {
			list = nmt_truce_repair
			save_scope_as = nmt_waiting_ruler
			scope:nmt_player = {
				if = {
					limit = {
						NOT = { has_truce = scope:nmt_waiting_ruler }
						NOT = { is_target_in_variable_list = { name = nmt_waiting_truce_v1 target = scope:nmt_waiting_ruler } }
					}
					nmt_add_marriage_truce_effect = { RULER = scope:nmt_waiting_ruler }
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = nmt_truce_started_toast
						left_icon = scope:nmt_waiting_ruler
					}
				}
			}
		}
	}
}

# ---------------------------------------------------------------------------
# Succession: carry the previous ruler's bonds over to the heir
# ---------------------------------------------------------------------------
# this = the new player, scope:previous_player_character = the old one.
# Called from nmt_inherit_diplomacy_policy_effect.
nmt_inherit_bonds_effect = {
	save_scope_as = nmt_heir
	nmt_clear_inherited_choices_effect = yes
	nmt_copy_choice_list_effect = { CHOICE = alliance }
	nmt_copy_choice_list_effect = { CHOICE = truce }
	nmt_copy_choice_list_effect = { CHOICE = neither }
	# Let the succession finish (titles, alliances) before reviewing the heir's ties.
	trigger_event = {
		on_action = nmt_succession_review
		days = 3
	}
}

# this = the new player (scope:nmt_heir), $CHOICE$ = alliance / truce / neither
nmt_copy_choice_list_effect = {
	if = {
		limit = {
			scope:previous_player_character = { has_variable_list = nmt_choice_$CHOICE$_v1 }
		}
		scope:previous_player_character = {
			every_in_list = {
				variable = nmt_choice_$CHOICE$_v1
				limit = { is_alive = yes }
				save_scope_as = nmt_inherit_ruler
				scope:nmt_heir = {
					add_to_variable_list = { name = nmt_inherited_$CHOICE$_v1 target = scope:nmt_inherit_ruler }
				}
			}
		}
	}
}

# this = the human ruler
nmt_clear_inherited_choices_effect = {
	if = {
		limit = { has_variable_list = nmt_inherited_alliance_v1 }
		clear_variable_list = nmt_inherited_alliance_v1
	}
	if = {
		limit = { has_variable_list = nmt_inherited_truce_v1 }
		clear_variable_list = nmt_inherited_truce_v1
	}
	if = {
		limit = { has_variable_list = nmt_inherited_neither_v1 }
		clear_variable_list = nmt_inherited_neither_v1
	}
}

# this = the new player. Looks at every current family tie: an in-law the predecessor
# already chose for gets that choice back without a popup (in nmt_ask_about_ruler_effect);
# any other eligible tie goes through the normal policy (popup or automatic).
nmt_succession_review_effect = {
	save_scope_as = nmt_player
	# No prestige reward or snub penalty for bonds that are only being renewed.
	set_variable = nmt_renewing_bonds
	nmt_manual_review_relative_effect = yes
	every_close_family_member = {
		limit = { is_alive = yes }
		nmt_manual_review_relative_effect = yes
	}
	remove_variable = nmt_renewing_bonds
	# Set by nmt_manual_review_relative_effect; only the diagnostic review reads it.
	remove_variable = nmt_diag_any_tie
	nmt_clear_inherited_choices_effect = yes
	# Normal refresh, so cleanup and the found-ruler list start out right for the heir.
	nmt_refresh_effect = yes
}

# ---------------------------------------------------------------------------
# Proposal ruler (called from on_accept of arrange_marriage_interaction and
# marry_off_interaction; interaction scopes)
# ---------------------------------------------------------------------------
# The ruler who accepted (or sent) a human's proposal is the one the player dealt with.
# Remember them as the foreign partner's in-law ruler, so the tie stays with that ruler
# even if the partner moves to another court before the wedding.
# nmt_pick_ruler_effect only uses it while that ruler qualifies.
# Runs BEFORE marriage_interaction_on_accept_effect, i.e. before the game adds any alliance.
# The game fulfils a betrothal by sending this interaction again (auto-accepted) to the
# ruler whose court the partner lives at by then. That is not a new deal: the ruler of
# the original proposal is kept, and the wedding ruler is only noted as a possible extra ally.
nmt_remember_proposal_ruler_effect = {
	if = {
		limit = {
			exists = scope:secondary_actor
			exists = scope:secondary_recipient
			scope:secondary_actor = {
				is_betrothed = yes
				betrothed = scope:secondary_recipient
			}
		}
		if = {
			limit = {
				scope:actor = { is_ai = no }
				scope:recipient = { is_ruler = yes }
			}
			scope:secondary_recipient = {
				nmt_note_wedding_ruler_effect = { RULER = scope:recipient PLAYER = scope:actor }
			}
		}
		else_if = {
			limit = {
				scope:recipient = { is_ai = no }
				scope:actor = { is_ruler = yes }
			}
			scope:secondary_actor = {
				nmt_note_wedding_ruler_effect = { RULER = scope:actor PLAYER = scope:recipient }
			}
		}
	}
	else_if = {
		limit = {
			scope:actor = { is_ai = no }
			exists = scope:secondary_recipient
			scope:recipient = { is_ruler = yes }
		}
		scope:secondary_recipient = {
			nmt_remember_proposal_partner_effect = { RULER = scope:recipient PLAYER = scope:actor }
		}
	}
	else_if = {
		limit = {
			scope:recipient = { is_ai = no }
			exists = scope:secondary_actor
			scope:actor = { is_ruler = yes }
		}
		scope:secondary_actor = {
			nmt_remember_proposal_partner_effect = { RULER = scope:actor PLAYER = scope:recipient }
		}
	}
}

# this = the foreign partner, $RULER$ = the ruler of the proposal, $PLAYER$ = the human.
# If the partner lives at another ruler's court, the game may ally the player with that
# ruler too. Remember that ruler (nmt_extra_ally) only if the player is not allied with
# them yet, so an alliance that existed before this proposal is never touched.
nmt_remember_proposal_partner_effect = {
	set_variable = {
		name = nmt_tie_ruler
		value = $RULER$
	}
	remove_variable = nmt_extra_ally
	if = {
		limit = {
			exists = court_owner
			court_owner = {
				is_ruler = yes
				NOT = { this = $RULER$ }
				NOT = { this = $PLAYER$ }
				NOT = { is_allied_to = $PLAYER$ }
			}
		}
		set_variable = {
			name = nmt_extra_ally
			value = court_owner
		}
	}
}

# this = the foreign partner at a betrothal's wedding, $RULER$ = the ruler the game sent
# the wedding to, $PLAYER$ = the human. If the partner moved to another court since the
# proposal, the game may ally the player with that ruler too; note them like
# nmt_remember_proposal_partner_effect does.
nmt_note_wedding_ruler_effect = {
	if = {
		limit = {
			exists = var:nmt_tie_ruler
			NOT = { exists = var:nmt_extra_ally }
			NOT = { var:nmt_tie_ruler = $RULER$ }
			$RULER$ = {
				NOT = { this = $PLAYER$ }
				NOT = { is_allied_to = $PLAYER$ }
			}
		}
		set_variable = {
			name = nmt_extra_ally
			value = $RULER$
		}
	}
}

# ---------------------------------------------------------------------------
# Extra alliances: the game allied the player with the ruler whose court a partner
# lived at, although the player made the marriage with another ruler. After the
# wedding (never while a betrothal depends on it) that alliance is cancelled, unless
# the player has made their own choice about that ruler.
# ---------------------------------------------------------------------------
# this = the human ruler (scope:nmt_player is saved by the refresh)
nmt_extra_alliance_sweep_effect = {
	nmt_extra_alliance_relative_effect = yes
	every_close_family_member = {
		limit = { is_alive = yes }
		nmt_extra_alliance_relative_effect = yes
	}
}

# this = the player or a close relative
nmt_extra_alliance_relative_effect = {
	every_spouse = {
		limit = {
			exists = var:nmt_extra_ally
			var:nmt_extra_ally = { is_alive = yes }
		}
		save_scope_as = nmt_extra_partner
		var:nmt_extra_ally = { save_scope_as = nmt_cancel_ruler }
		if = {
			# The player has their own bond with this ruler: leave the alliance alone.
			limit = {
				scope:nmt_player = { nmt_has_own_bond_with_trigger = { RULER = scope:nmt_cancel_ruler } }
			}
			remove_variable = nmt_extra_ally
		}
		else_if = {
			limit = {
				scope:nmt_player = {
					is_allied_to = scope:nmt_cancel_ruler
					NOT = { nmt_betrothal_links_trigger = { OTHER = scope:nmt_cancel_ruler } }
				}
			}
			remove_variable = nmt_extra_ally
			scope:nmt_player = {
				hidden_effect = {
					break_alliance = scope:nmt_cancel_ruler
				}
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = nmt_extra_alliance_cancelled_toast
					custom_tooltip = nmt_extra_alliance_cancelled_details
					left_icon = scope:nmt_cancel_ruler
				}
			}
		}
	}
}
