﻿# Bonds of Diplomacy: core marriage-tie logic.
# Only additive hooks and scripted effects. Do not replace vanilla marriage logic.
# New adult weddings are handled in on_marriage directly; betrothals are swept
# by the quarterly playable pulse (there is no verified CK3 acceptance hook).
# Completed choice history is recorded when the player selects an option.
# Pending choice history is recorded at queue time to prevent duplicate popups.

# ---------------------------------------------------------------------------
# Scheduling (cleanup refresh), one per human ruler even if several hooks fire
# ---------------------------------------------------------------------------
# this = a character involved in a marriage/divorce/death/betrothal change
nmt_schedule_refresh_effect = {
	if = {
		limit = {
			is_ai = no
			is_ruler = yes
			is_alive = yes
		}
		nmt_queue_refresh_effect = yes
	}
	every_close_family_member = {
		limit = {
			is_ai = no
			is_ruler = yes
			is_alive = yes
		}
		nmt_queue_refresh_effect = yes
	}
}

# this = a human ruler
nmt_queue_refresh_effect = {
	if = {
		limit = {
			NOT = { has_variable = nmt_refresh_pending }
		}
		set_variable = {
			name = nmt_refresh_pending
			days = 3
		}
		trigger_event = {
			on_action = nmt_refresh_player_truces
			days = 2
		}
	}
}

# ---------------------------------------------------------------------------
# Wedding hook: find the human ruler on each side and treat this as a NEW tie
# ---------------------------------------------------------------------------
# this = one of the two newlyweds, $OTHER$ = the other one
nmt_wedding_side_effect = {
	save_scope_as = nmt_family
	$OTHER$ = {
		save_scope_as = nmt_other
	}
	if = {
		limit = {
			is_ai = no
			is_ruler = yes
			is_alive = yes
		}
		nmt_queue_wedding_effect = yes
	}
	every_close_family_member = {
		limit = {
			is_ai = no
			is_ruler = yes
			is_alive = yes
		}
		nmt_queue_wedding_effect = yes
	}
}

# Do NOT delay this through a second on_action. The named scopes nmt_family and
# nmt_other are available during the wedding hook; a separate delayed on_action can
# lose them. Find the ruler now and queue the choice EVENT one day later (with saved scopes).
nmt_queue_wedding_effect = {
	# Players this wedding reached; the on_marriage hook uses it to send at most one
	# "no eligible ruler" notice per player. Duplicate choices are prevented by the
	# pending/completed lists in nmt_ask_about_ruler_effect.
	if = {
		limit = { NOT = { is_in_list = nmt_wedding_checked_players } }
		add_to_temporary_list = nmt_wedding_checked_players
	}
	set_variable = nmt_trace_wedding
	nmt_new_tie_effect = yes
}

# this = a newlywed. Remember which court they are in RIGHT NOW (before any move), for 30 days.
nmt_snapshot_court_effect = {
	if = {
		limit = {
			NOT = { has_variable = nmt_prev_court }
			exists = court_owner
		}
		court_owner = {
			save_scope_as = nmt_snapshot_owner
		}
		set_variable = {
			name = nmt_prev_court
			value = scope:nmt_snapshot_owner
			days = 30
		}
	}
}

# this = the human ruler. Uses saved scopes nmt_family (their side) and nmt_other (the new partner).
nmt_new_tie_effect = {
	save_scope_as = nmt_player
	if = {
		limit = {
			exists = scope:nmt_family
			scope:nmt_family = {
				nmt_spouse_reaches_player_trigger = { PLAYER = scope:nmt_player }
			}
		}
		set_variable = nmt_trace_scan
		if = {
			limit = { has_variable = nmt_verbose_debug }
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_debug_checking_toast
			}
		}
		scope:nmt_other = {
			save_scope_as = nmt_through_target
		}
		scope:nmt_family = {
			save_scope_as = nmt_through_owner
		}
		scope:nmt_other = {
			nmt_pick_ruler_effect = { ACTION = nmt_ask_ruler_effect }
		}
	}
}

# ---------------------------------------------------------------------------
# Picking the in-law ruler for a partner (this = the partner)
#   0. the ruler already remembered for this partner (nmt_tie_ruler), while they qualify;
#   1. the partner themselves, if a ruler;
#   2. the ruler of the court the partner was remembered coming from (nmt_prev_court);
#   3. the ruler whose court the partner currently lives in;
#   4. otherwise the highest-ranking ruling close-family member of the partner.
# $ACTION$ is run on the ruler that is picked (and only if they qualify).
# Step 0 keeps a tie with the same ruler for good: without it, a later scan (after the
# partner moved courts and nmt_prev_court expired) could pick another relative, overwrite
# nmt_tie_ruler and make the cleanup end the original ruler's truce.
# ---------------------------------------------------------------------------
nmt_pick_ruler_effect = {
	if = {
		limit = {
			exists = var:nmt_tie_ruler
			var:nmt_tie_ruler = {
				nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
			}
		}
		var:nmt_tie_ruler = {
			$ACTION$ = yes
		}
	}
	else_if = {
		limit = {
			is_ruler = yes
			nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
		}
		$ACTION$ = yes
	}
	else_if = {
		limit = {
			exists = var:nmt_prev_court
			var:nmt_prev_court = {
				nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
			}
		}
		var:nmt_prev_court = {
			$ACTION$ = yes
		}
	}
	else_if = {
		limit = {
			exists = court_owner
			court_owner = {
				nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
			}
		}
		court_owner = {
			$ACTION$ = yes
		}
	}
	else = {
		ordered_close_family_member = {
			limit = {
				is_ruler = yes
				is_alive = yes
				nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
			}
			order_by = {
				value = primary_title.tier
			}
			max = 1
			check_range_bounds = no
			$ACTION$ = yes
		}
	}
}

# this = the picked ruler. Remember them on the spouse, then ask the player once.
nmt_ask_ruler_effect = {
	if = {
		limit = {
			nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
		}
		save_scope_as = nmt_ruler
		# Record eligibility before policy/history checks. A previously handled
		# ruler is still eligible and must not produce a misleading failure toast.
		scope:nmt_player = {
			if = {
				limit = { NOT = { is_in_list = nmt_wedding_eligible_players } }
				add_to_temporary_list = nmt_wedding_eligible_players
			}
		}
		scope:nmt_player = {
			if = {
				limit = { has_variable = nmt_diagnostic_scanning }
				set_variable = nmt_diag_found_ruler
			}
		}
		scope:nmt_through_target = {
			set_variable = {
				name = nmt_tie_ruler
				value = scope:nmt_ruler
			}
		}
		scope:nmt_player = {
			nmt_mark_tie_known_effect = { PARTNER = scope:nmt_through_target }
		}
		scope:nmt_player = {
			if = {
				limit = { has_variable = nmt_verbose_debug }
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = nmt_debug_found_toast
					left_icon = scope:nmt_ruler
				}
			}
			nmt_ask_about_ruler_effect = { RULER = scope:nmt_ruler }
		}
	}
}

# this = human ruler, $RULER$ = in-law ruler.
# Decides what happens for a new tie with this ruler: direct ruler union, a renewed
# choice after succession, a Manual popup, or one of the automatic policies.
# - A manual choice is only recorded as completed when the player clicks an option,
#   so a missed popup can be offered again by a later review.
# - Manual is tested first, as its own branch, not as the last else of the automatic
#   ladder. nmt_policy_manual_choice wins over stale automatic flags, and no policy
#   at all also means Manual.
# - Only new ties are affected: rulers already in nmt_partners_v2 are skipped, so a
#   policy change never creates duplicate pacts.
nmt_ask_about_ruler_effect = {
	# Direct ruler unions keep the game's own alliance under every policy.
	if = {
		limit = { nmt_direct_ruler_union_trigger = { RULER = $RULER$ } }
		nmt_handle_direct_ruler_union_effect = { RULER = $RULER$ }
	}
	else = {
		set_variable = nmt_trace_found
		if = {
			limit = { is_target_in_variable_list = { name = nmt_partners_v2 target = $RULER$ } }
			set_variable = nmt_trace_reviewed
		}
		if = {
			limit = { is_target_in_variable_list = { name = nmt_pending_partners_v2 target = $RULER$ } }
			set_variable = nmt_trace_pending
		}
		# Diagnostic: completed agreements are deliberately not offered twice to the
		# same foreign ruler, even if the policy has changed since the first wedding.
		if = {
			limit = {
				has_variable = nmt_verbose_debug
				is_target_in_variable_list = { name = nmt_partners_v2 target = $RULER$ }
			}
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_v1102_already_reviewed_toast
				left_icon = $RULER$
			}
		}
		if = {
			limit = {
				has_variable = nmt_verbose_debug
				is_target_in_variable_list = { name = nmt_pending_partners_v2 target = $RULER$ }
			}
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_v1103_already_queued_toast
				left_icon = $RULER$
			}
		}

		# SUCCESSION: the previous ruler already chose for this in-law, so renew that
		# choice without a popup. Recording the ruler in nmt_partners_v2 makes the
		# policy branches below skip it.
		if = {
			limit = {
				NOT = { is_target_in_variable_list = { name = nmt_partners_v2 target = $RULER$ } }
				NOT = { is_target_in_variable_list = { name = nmt_pending_partners_v2 target = $RULER$ } }
				OR = {
					is_target_in_variable_list = { name = nmt_inherited_alliance_v1 target = $RULER$ }
					is_target_in_variable_list = { name = nmt_inherited_truce_v1 target = $RULER$ }
					is_target_in_variable_list = { name = nmt_inherited_neither_v1 target = $RULER$ }
				}
			}
			set_variable = nmt_trace_auto
			add_to_variable_list = { name = nmt_partners_v2 target = $RULER$ }
			if = {
				limit = { is_target_in_variable_list = { name = nmt_inherited_alliance_v1 target = $RULER$ } }
				nmt_apply_alliance_effect = {
					RULER = $RULER$
					PLAYER = scope:nmt_player
					OWNER = scope:nmt_through_owner
					TARGET = scope:nmt_through_target
				}
				nmt_notify_renewed_effect = { RULER = $RULER$ CHOICE = alliance }
			}
			else_if = {
				limit = { is_target_in_variable_list = { name = nmt_inherited_truce_v1 target = $RULER$ } }
				nmt_apply_truce_effect = { RULER = $RULER$ PLAYER = scope:nmt_player }
				nmt_notify_renewed_effect = { RULER = $RULER$ CHOICE = truce }
			}
			else = {
				nmt_apply_neither_effect = { RULER = $RULER$ PLAYER = scope:nmt_player }
				nmt_notify_renewed_effect = { RULER = $RULER$ CHOICE = neither }
			}
		}

		# MANUAL: an explicit Manual setting, or no automatic policy at all.
		# Lock the ruler as pending, then queue the popup for tomorrow.
		if = {
			limit = {
				NOT = { is_target_in_variable_list = { name = nmt_partners_v2 target = $RULER$ } }
				NOT = { is_target_in_variable_list = { name = nmt_pending_partners_v2 target = $RULER$ } }
				OR = {
					has_variable = nmt_policy_manual_choice
					NOT = {
						OR = {
							has_variable = nmt_policy_auto_alliance
							has_variable = nmt_policy_auto_truce
							has_variable = nmt_policy_auto_neither
						}
					}
				}
			}
			add_to_variable_list = {
				name = nmt_pending_partners_v2
				target = $RULER$
			}
			if = {
				limit = { has_variable = nmt_diagnostic_scanning }
				set_variable = nmt_diag_prompted
			}
			else = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = nmt_info_choice_queued_title
					custom_tooltip = nmt_info_choice_queued_details
					left_icon = $RULER$
				}
			}
			set_variable = nmt_trace_queued
			trigger_event = { id = nmt_marriage.0001 days = 1 }
		}

		# Automatic modes are completely separate from the manual event scheduler.
		# On taking an automatic action, recording the completed ruler prevents
		# the subsequent branches from firing, including on duplicate wedding hooks.
		if = {
			limit = {
				NOT = { has_variable = nmt_policy_manual_choice }
				has_variable = nmt_policy_auto_alliance
				NOT = { is_target_in_variable_list = { name = nmt_partners_v2 target = $RULER$ } }
				NOT = { is_target_in_variable_list = { name = nmt_pending_partners_v2 target = $RULER$ } }
			}
			set_variable = nmt_trace_auto
			add_to_variable_list = { name = nmt_partners_v2 target = $RULER$ }
			nmt_apply_alliance_effect = {
				RULER = $RULER$
				PLAYER = scope:nmt_player
				OWNER = scope:nmt_through_owner
				TARGET = scope:nmt_through_target
			}
			nmt_notify_auto_alliance_effect = { RULER = $RULER$ }
		}
		if = {
			limit = {
				NOT = { has_variable = nmt_policy_manual_choice }
				has_variable = nmt_policy_auto_truce
				NOT = { is_target_in_variable_list = { name = nmt_partners_v2 target = $RULER$ } }
				NOT = { is_target_in_variable_list = { name = nmt_pending_partners_v2 target = $RULER$ } }
			}
			set_variable = nmt_trace_auto
			add_to_variable_list = { name = nmt_partners_v2 target = $RULER$ }
			nmt_apply_truce_effect = { RULER = $RULER$ PLAYER = scope:nmt_player }
			nmt_notify_auto_truce_effect = { RULER = $RULER$ }
		}
		if = {
			limit = {
				NOT = { has_variable = nmt_policy_manual_choice }
				has_variable = nmt_policy_auto_neither
				NOT = { is_target_in_variable_list = { name = nmt_partners_v2 target = $RULER$ } }
				NOT = { is_target_in_variable_list = { name = nmt_pending_partners_v2 target = $RULER$ } }
			}
			set_variable = nmt_trace_auto
			add_to_variable_list = { name = nmt_partners_v2 target = $RULER$ }
			nmt_apply_neither_effect = { RULER = $RULER$ PLAYER = scope:nmt_player }
			nmt_notify_auto_neither_effect = { RULER = $RULER$ }
		}

	}
}

# Exactly one selected policy for this human ruler.
nmt_clear_diplomacy_policy_effect = {
	remove_variable = nmt_policy_manual_choice
	remove_variable = nmt_policy_auto_alliance
	remove_variable = nmt_policy_auto_truce
	remove_variable = nmt_policy_auto_neither
}

# Event choice recording: from the player, $RULER$ = other ruler.
nmt_mark_choice_completed_effect = {
	# Release the per-target pending lock when one of the three options is chosen.
	remove_list_variable = {
		name = nmt_pending_partners_v2
		target = $RULER$
	}
	if = {
		limit = {
			NOT = {
				is_target_in_variable_list = {
					name = nmt_partners_v2
					target = $RULER$
				}
			}
		}
		add_to_variable_list = {
			name = nmt_partners_v2
			target = $RULER$
		}
	}
}

# ---------------------------------------------------------------------------
# Full refresh for one human ruler (cleanup + betrothal sweep)
# ---------------------------------------------------------------------------
# this = the human ruler
nmt_refresh_effect = {
	save_scope_as = nmt_player
	# Keep pending choices across refreshes: delivered does not mean answered.
	# Completion or on_trigger_fail releases the individual ruler's lock.
	# Only the explicit test-save reset may clear all pending choices.
	nmt_choice_migration_effect = yes
	nmt_betrothal_sweep_effect = yes
	nmt_discover_effect = yes
	nmt_cleanup_effect = yes
	nmt_prune_known_ties_effect = yes
	# Cancel marriage alliances the player declined (see nmt_alliance_effects.txt).
	nmt_declined_migration_effect = yes
	# Refused war calls from a betrothal alliance the player declined cost nothing.
	nmt_excuse_refused_calls_effect = yes
	nmt_start_excuse_watch_effect = yes
	nmt_declined_alliance_sweep_effect = yes
	# Cancel alliances the game added with a ruler the player did not deal with.
	nmt_extra_alliance_sweep_effect = yes
	nmt_waiting_truce_sweep_effect = yes
	nmt_truce_repair_effect = yes
}

# ---------------------------------------------------------------------------
# Betrothal sweep: newly discovered and previously missed betrothals are
# offered a choice even on the first sweep. Partner history avoids repeats.
# ---------------------------------------------------------------------------
# this = the human ruler (scope:nmt_player is already saved)
nmt_betrothal_sweep_effect = {
	nmt_sweep_relative_effect = yes
	every_close_family_member = {
		limit = { is_alive = yes }
		nmt_sweep_relative_effect = yes
	}
	if = {
		limit = {
			NOT = { has_variable = nmt_baseline_done }
		}
		set_variable = nmt_baseline_done
	}
}

# this = the player or one of their close relatives
nmt_sweep_relative_effect = {
	if = {
		limit = { is_betrothed = yes }
		save_scope_as = nmt_family
		betrothed = {
			save_scope_as = nmt_other
			if = {
				limit = {
					NOT = {
						scope:nmt_player = {
							is_target_in_variable_list = { name = nmt_known_ties_v1 target = scope:nmt_other }
						}
					}
				}
				scope:nmt_player = {
					nmt_mark_tie_known_effect = { PARTNER = scope:nmt_other }
					# First scan now offers a choice rather than silently discarding it.
					nmt_new_tie_effect = yes
				}
			}
		}
	}
}

# ---------------------------------------------------------------------------
# Discovery (for cleanup only; never asks): which rulers are still linked right now?
# ---------------------------------------------------------------------------
# this = the human ruler (scope:nmt_player is already saved)
nmt_discover_effect = {
	nmt_check_relative_effect = yes
	every_close_family_member = {
		limit = { is_alive = yes }
		nmt_check_relative_effect = yes
	}
}

# this = the player or one of their close relatives
nmt_check_relative_effect = {
	# Needed if a partner's in-law ruler has to be picked again (see nmt_try_partner_effect).
	save_scope_as = nmt_family
	every_spouse = {
		nmt_try_partner_effect = yes
	}
	if = {
		limit = { is_betrothed = yes }
		betrothed = {
			nmt_try_partner_effect = yes
		}
	}
}

# this = a spouse/betrothed of the player or of a close relative of the player
nmt_try_partner_effect = {
	# Remembered for nmt_prune_known_ties_effect: this partner is still tied to the family.
	add_to_temporary_list = nmt_current_partners
	# The remembered in-law ruler died or no longer qualifies (lost their land, joined
	# the player's realm...). Forget them and pick again, so a successor is offered a
	# choice. The old ruler is then dropped by the cleanup as usual.
	if = {
		limit = {
			exists = var:nmt_tie_ruler
			NOT = {
				var:nmt_tie_ruler = {
					nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
				}
			}
		}
		remove_variable = nmt_tie_ruler
		save_scope_as = nmt_other
		scope:nmt_player = {
			nmt_new_tie_effect = yes
		}
	}
	if = {
		limit = { exists = var:nmt_tie_ruler }
		var:nmt_tie_ruler = {
			nmt_found_ruler_effect = yes
		}
	}
	else = {
		nmt_pick_ruler_effect = { ACTION = nmt_found_ruler_effect }
	}
}

# this = a ruler that is still linked to the player's family
nmt_found_ruler_effect = {
	if = {
		limit = {
			nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player }
		}
		add_to_temporary_list = nmt_found_rulers
	}
}

# ---------------------------------------------------------------------------
# Option effects (called from the event; root = the human ruler)
# ---------------------------------------------------------------------------

# Small prestige reward (scales with the in-law's rank), at most once every 5 years.
# Size comes from the 'Marriage Prestige' game rule: Off, Low (10), Normal (25, the
# default and the original value) or High (50), each multiplied by the in-law's rank.
nmt_prestige_reward_effect = {
	if = {
		limit = {
			NOT = { has_variable = nmt_prestige_cooldown }
			NOT = { has_game_rule = nmt_prestige_reward_off }
			# Renewing the previous ruler's bonds at succession is not a new marriage.
			NOT = { has_variable = nmt_renewing_bonds }
		}
		add_prestige = {
			value = 0
			if = {
				limit = { has_game_rule = nmt_prestige_reward_low }
				add = 10
			}
			else_if = {
				limit = { has_game_rule = nmt_prestige_reward_high }
				add = 50
			}
			else = {
				add = 25
			}
			multiply = $RULER$.primary_title.tier
		}
		set_variable = {
			name = nmt_prestige_cooldown
			years = 5
		}
	}
}

# Opinion both ways. The amount comes from the 'Marriage Opinion' game rule: Minimal (+1),
# Low (+5), Normal (+10, the default and the original value) or High (+20). The modifier is
# always given because it is also the marker the cleanup looks for.
nmt_marriage_opinion_effect = {
	if = {
		limit = { has_game_rule = nmt_opinion_bonus_minimal }
		nmt_give_marriage_opinion_effect = { RULER = $RULER$ PLAYER = $PLAYER$ MODIFIER = nmt_marriage_truce_opinion_minimal }
	}
	else_if = {
		limit = { has_game_rule = nmt_opinion_bonus_low }
		nmt_give_marriage_opinion_effect = { RULER = $RULER$ PLAYER = $PLAYER$ MODIFIER = nmt_marriage_truce_opinion_low }
	}
	else_if = {
		limit = { has_game_rule = nmt_opinion_bonus_high }
		nmt_give_marriage_opinion_effect = { RULER = $RULER$ PLAYER = $PLAYER$ MODIFIER = nmt_marriage_truce_opinion_high }
	}
	else = {
		nmt_give_marriage_opinion_effect = { RULER = $RULER$ PLAYER = $PLAYER$ MODIFIER = nmt_marriage_truce_opinion }
	}
}

# Option: truce only. The game needs a length, so it is set very long and the truce is
# cancelled by the cleanup when the last tie ends. The tooltip says so instead of "100 years".
# If another truce (e.g. from a recent war) already exists, it is NOT replaced: the script
# cannot read how long it has left, so cancelling ours later would erase it. Instead the
# ruler waits in nmt_waiting_truce_v1 and ours is added once the old one has run out
# (see nmt_waiting_truce_sweep_effect).
nmt_apply_truce_effect = {
	custom_tooltip = nmt_truce_option_tooltip
	# Recorded first, so the "alliance cancelled" notice can say Truce was chosen.
	hidden_effect = {
		nmt_record_choice_effect = { RULER = $RULER$ CHOICE = truce }
	}
	# The game may have made a marriage alliance by itself; a truce-only choice cancels it.
	nmt_decline_alliance_effect = { RULER = $RULER$ }
	hidden_effect = {
		if = {
			limit = {
				OR = {
					# Someone else's truce is running (ours carries the opinion marker).
					AND = {
						NOT = { nmt_has_marriage_opinion_trigger = { TARGET = $RULER$ } }
						OR = {
							has_truce = $RULER$
							$RULER$ = { has_truce = $PLAYER$ }
						}
					}
					# The game will not add a truce between allies. The alliance is still
					# there for a betrothal (cancelled at the wedding) or because a
					# cancelled alliance only disappears after this effect.
					is_allied_to = $RULER$
				}
			}
			if = {
				limit = {
					NOT = { is_target_in_variable_list = { name = nmt_waiting_truce_v1 target = $RULER$ } }
				}
				add_to_variable_list = { name = nmt_waiting_truce_v1 target = $RULER$ }
			}
		}
		else = {
			nmt_add_marriage_truce_effect = { RULER = $RULER$ }
		}
	}
	nmt_marriage_opinion_effect = { RULER = $RULER$ PLAYER = $PLAYER$ }
	nmt_prestige_reward_effect = { RULER = $RULER$ }
}

# this = the human ruler. Length from the 'Marriage Truce Length' game rule; with
# "While Ties Last" it is 100 years and the cleanup cancels it when the ties end.
# For 10 and 25 years the end year is kept on the ruler (nmt_truce_end_year) for the
# Marriage Diplomacy Overview.
nmt_add_marriage_truce_effect = {
	if = {
		limit = { has_game_rule = nmt_truce_length_10 }
		add_truce_both_ways = {
			character = $RULER$
			years = 10
			name = NMT_TRUCE_MARRIAGE
			override = yes
		}
		$RULER$ = {
			set_variable = {
				name = nmt_truce_end_year
				value = { value = current_year add = 10 }
			}
		}
	}
	else_if = {
		limit = { has_game_rule = nmt_truce_length_25 }
		add_truce_both_ways = {
			character = $RULER$
			years = 25
			name = NMT_TRUCE_MARRIAGE
			override = yes
		}
		$RULER$ = {
			set_variable = {
				name = nmt_truce_end_year
				value = { value = current_year add = 25 }
			}
		}
	}
	else = {
		add_truce_both_ways = {
			character = $RULER$
			years = 100
			name = NMT_TRUCE_MARRIAGE
			override = yes
		}
		$RULER$ = { remove_variable = nmt_truce_end_year }
	}
}

# Option: neither. No alliance (one the game made by itself is cancelled) and no truce.
# The in-law may take it as a snub ('Declining In-Laws' game rule), but not when the
# choice is only being renewed for an heir.
nmt_apply_neither_effect = {
	# Recorded first, so the "alliance cancelled" notice can say Neither was chosen.
	hidden_effect = {
		nmt_record_choice_effect = { RULER = $RULER$ CHOICE = neither }
	}
	nmt_decline_alliance_effect = { RULER = $RULER$ }
	if = {
		limit = {
			NOT = { has_variable = nmt_renewing_bonds }
			NOT = { has_game_rule = nmt_snub_penalty_off }
		}
		$RULER$ = {
			if = {
				limit = { has_game_rule = nmt_snub_penalty_harsh }
				add_opinion = { target = $PLAYER$ modifier = nmt_snubbed_opinion_harsh }
			}
			else = {
				add_opinion = { target = $PLAYER$ modifier = nmt_snubbed_opinion_mild }
			}
		}
	}
}

# this = the human ruler. Removes the snub, e.g. when the bond is upgraded to an alliance.
nmt_remove_snub_effect = {
	$RULER$ = {
		if = {
			limit = { has_opinion_modifier = { target = $PLAYER$ modifier = nmt_snubbed_opinion_mild } }
			remove_opinion = { target = $PLAYER$ modifier = nmt_snubbed_opinion_mild }
		}
		if = {
			limit = { has_opinion_modifier = { target = $PLAYER$ modifier = nmt_snubbed_opinion_harsh } }
			remove_opinion = { target = $PLAYER$ modifier = nmt_snubbed_opinion_harsh }
		}
	}
}

# Option: alliance
nmt_apply_alliance_effect = {
	custom_tooltip = nmt_alliance_option_tooltip
	hidden_effect = {
		nmt_undecline_alliance_effect = { RULER = $RULER$ }
		if = {
			limit = {
				NOT = { is_allied_to = $RULER$ }
			}
			# Once an alliance between two rulers has been broken (e.g. the game's marriage
			# alliance, cancelled for an earlier Truce/Neither choice), the game keeps it
			# blocked and create_alliance has no lasting effect until it is allowed again.
			allow_alliance = $RULER$
			create_alliance = {
				target = $RULER$
				allied_through_owner = $OWNER$
				allied_through_target = $TARGET$
			}
		}
		nmt_record_choice_effect = { RULER = $RULER$ CHOICE = alliance }
	}
	nmt_marriage_opinion_effect = { RULER = $RULER$ PLAYER = $PLAYER$ }
	nmt_prestige_reward_effect = { RULER = $RULER$ }
}

# ---------------------------------------------------------------------------
# Cleanup: end truces whose last marriage/betrothal tie is gone
# ---------------------------------------------------------------------------
# this = the human ruler (scope:nmt_player is already saved)
nmt_cleanup_effect = {
	if = {
		limit = { has_variable_list = nmt_partners_v2 }
		every_in_list = {
			variable = nmt_partners_v2
			limit = {
				OR = {
					is_alive = no
					NOT = { is_in_list = nmt_found_rulers }
				}
			}
			add_to_temporary_list = nmt_voided_partners
		}
		every_in_list = {
			list = nmt_voided_partners
			save_scope_as = nmt_ruler
			scope:nmt_player = {
				nmt_end_truce_with_ruler_effect = { RULER = scope:nmt_ruler }
			}
		}
	}
}

# this = the human ruler, $RULER$ = in-law ruler
nmt_end_truce_with_ruler_effect = {
	# Reset this notice when the existing cleanup loses the last eligible tie.
	remove_list_variable = { name = nmt_direct_union_notices target = $RULER$ }

	if = {
		limit = { nmt_has_marriage_opinion_trigger = { TARGET = $RULER$ } }
		# A ruler still waiting for our truce holds someone else's truce: leave it alone.
		if = {
			limit = {
				has_truce = $RULER$
				NOT = { is_target_in_variable_list = { name = nmt_waiting_truce_v1 target = $RULER$ } }
			}
			cancel_truce_both_ways = $RULER$
			$RULER$ = { remove_variable = nmt_truce_end_year }
		}
		nmt_remove_marriage_opinion_effect = { TARGET = $RULER$ }
		$RULER$ = {
			nmt_remove_marriage_opinion_effect = { TARGET = scope:nmt_player }
		}
		# The reason helps tell a real end of the ties from the mod losing track of one.
		if = {
			limit = { $RULER$ = { is_alive = no } }
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_truce_ended_toast
				custom_tooltip = nmt_truce_ended_dead
				left_icon = $RULER$
			}
		}
		else_if = {
			limit = { $RULER$ = { nmt_valid_truce_pair_trigger = { PLAYER = scope:nmt_player } } }
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_truce_ended_toast
				custom_tooltip = nmt_truce_ended_no_link
				left_icon = $RULER$
			}
		}
		else = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_truce_ended_toast
				custom_tooltip = nmt_truce_ended_not_eligible
				left_icon = $RULER$
			}
		}
	}
	remove_list_variable = {
		name = nmt_partners_v2
		target = $RULER$
	}
	# The tie is gone, so the next tie with this ruler asks again.
	remove_list_variable = {
		name = nmt_declined_alliance_v1
		target = $RULER$
	}
	remove_list_variable = {
		name = nmt_waiting_truce_v1
		target = $RULER$
	}
	nmt_forget_choice_effect = { RULER = $RULER$ }
}

# ---------------------------------------------------------------------------
# Review decision: every manual scan ends in an event explaining the result.
# We only set markers on the player; no gameplay balance changes during scanning.
# ---------------------------------------------------------------------------
nmt_manual_review_effect = {
	save_scope_as = nmt_player
	remove_variable = nmt_diag_any_tie
	remove_variable = nmt_diag_found_ruler
	remove_variable = nmt_diag_prompted
	set_variable = nmt_diagnostic_scanning
	nmt_manual_review_relative_effect = yes
	every_close_family_member = {
		limit = { is_alive = yes }
		nmt_manual_review_relative_effect = yes
	}
	remove_variable = nmt_diagnostic_scanning
	if = {
		limit = { NOT = { has_variable = nmt_diag_any_tie } }
		trigger_event = { id = nmt_marriage.0097 }
	}
	else_if = {
		limit = { NOT = { has_variable = nmt_diag_found_ruler } }
		trigger_event = { id = nmt_marriage.0096 }
	}
	else_if = {
		limit = { NOT = { has_variable = nmt_diag_prompted } }
		trigger_event = { id = nmt_marriage.0095 }
	}
	remove_variable = nmt_diag_any_tie
	remove_variable = nmt_diag_found_ruler
	remove_variable = nmt_diag_prompted
}

# this = player or a close family member
nmt_manual_review_relative_effect = {
	save_scope_as = nmt_family
	every_spouse = {
		scope:nmt_player = { set_variable = nmt_diag_any_tie }
		save_scope_as = nmt_other
		scope:nmt_player = { nmt_new_tie_effect = yes }
	}
	if = {
		limit = { is_betrothed = yes }
		scope:nmt_player = { set_variable = nmt_diag_any_tie }
		betrothed = {
			save_scope_as = nmt_other
			scope:nmt_player = { nmt_new_tie_effect = yes }
		}
	}
}
