﻿# Marriage ties: hooks. Everything here ADDS to vanilla on_actions; nothing is replaced.

on_marriage = { on_actions = { nmt_on_marriage } }
on_divorce = { on_actions = { nmt_on_divorce } }
on_death = { on_actions = { nmt_on_death } }
on_betrothal_broken = { on_actions = { nmt_on_betrothal_broken } }
on_war_started = { on_actions = { nmt_on_war_started nmt_on_war_started_excuse } }
quarterly_playable_pulse = { on_actions = { nmt_quarterly_pulse } }
on_join_court = { on_actions = { nmt_on_join_court } }
on_alliance_added = { on_actions = { nmt_on_alliance_added } }

# root = none, scope:first = character, scope:second = character
nmt_on_alliance_added = {
	effect = {
		# AI Marriage Diplomacy: an alliance made by a wedding may appear before the
		# wedding hook runs; this marker lets the AI still decide about it.
		if = {
			limit = {
				has_game_rule = nmt_ai_diplomacy_on
				scope:first = { is_ai = yes }
				scope:second = { is_ai = yes }
			}
			scope:first = {
				set_variable = {
					name = nmt_ai_fresh_alliance_with
					value = scope:second
					days = 3
				}
			}
			scope:second = {
				set_variable = {
					name = nmt_ai_fresh_alliance_with
					value = scope:first
					days = 3
				}
			}
		}
	}
}

# root = character, scope:spouse = character
nmt_on_marriage = {
	effect = {
		# Local notification scopes only; do not reuse discovery's spouse scopes,
		# which can be overwritten while both sides are examined.
		root = { save_scope_as = nmt_info_newlywed_a }
		scope:spouse = { save_scope_as = nmt_info_newlywed_b }
		# Remember where each newlywed is living right now (the bride may move courts a moment later)
		root = { nmt_snapshot_court_effect = yes }
		scope:spouse = { nmt_snapshot_court_effect = yes }
		# Treat this wedding as a NEW tie for any human ruler on either side...
		root = { nmt_wedding_side_effect = { OTHER = scope:spouse } }
		scope:spouse = { nmt_wedding_side_effect = { OTHER = root } }
		# Both sides have finished discovery. Suppress this notice if either
		# side found a valid ruler for the same player. Lists last only this chain.
		every_in_list = {
			list = nmt_wedding_checked_players
			limit = { NOT = { is_in_list = nmt_wedding_eligible_players } }
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_info_no_eligible_title
				custom_tooltip = nmt_info_no_eligible_details
				left_icon = root
				right_icon = scope:spouse
			}
		}
		# ...and queue the normal cleanup refresh.
		root = { nmt_schedule_refresh_effect = yes }
		scope:spouse = { nmt_schedule_refresh_effect = yes }
		# 'AI Marriage Diplomacy' game rule (nmt_ai_diplomacy_effects.txt).
		if = {
			limit = { has_game_rule = nmt_ai_diplomacy_on }
			nmt_ai_marriage_diplomacy_effect = yes
		}
	}
}

# Delayed from nmt_ai_schedule_cancel_effect. root = the deciding AI ruler.
# Cancels the marriage alliances the ruler chose Truce or Neither for.
nmt_ai_cancel_alliances = {
	effect = {
		save_scope_as = nmt_ai_decider
		remove_variable = nmt_ai_cancel_pending
		if = {
			limit = { has_variable_list = nmt_ai_cancel_alliance_v1 }
			every_in_list = {
				variable = nmt_ai_cancel_alliance_v1
				limit = {
					is_alive = yes
					is_allied_to = scope:nmt_ai_decider
					# Breaking it would also break a betrothal between the families.
					NOT = { nmt_betrothal_links_trigger = { OTHER = scope:nmt_ai_decider } }
				}
				save_scope_as = nmt_ai_other
				scope:nmt_ai_decider = {
					break_alliance = scope:nmt_ai_other
				}
			}
			clear_variable_list = nmt_ai_cancel_alliance_v1
		}
	}
}

# root = character, scope:spouse = character
nmt_on_divorce = {
	effect = {
		# The remembered court belonged to this marriage; a later betrothal must not use it.
		root = { remove_variable = nmt_prev_court }
		scope:spouse = { remove_variable = nmt_prev_court }
		root = { nmt_schedule_refresh_effect = yes }
		scope:spouse = { nmt_schedule_refresh_effect = yes }
	}
}

# root = the character who died
nmt_on_death = {
	effect = {
		# A widow(er)'s remembered court belonged to this marriage; a later betrothal
		# (within its 30 days) must not use it to pick the in-law ruler.
		every_spouse = { remove_variable = nmt_prev_court }
		if = {
			limit = {
				OR = {
					is_married = yes
					is_betrothed = yes
				}
			}
			nmt_schedule_refresh_effect = yes
		}
	}
}

# root = character, scope:second = character
nmt_on_betrothal_broken = {
	effect = {
		root = { nmt_schedule_refresh_effect = yes }
		scope:second = { nmt_schedule_refresh_effect = yes }
	}
}

# Every quarter: catches betrothals (no hook exists for them) and any missed cleanups.
nmt_quarterly_pulse = {
	trigger = {
		is_ai = no
		is_ruler = yes
	}
	effect = {
		nmt_refresh_effect = yes
	}
}

# Delayed refresh for a human ruler (the delay lets the marriage/divorce finish first).
nmt_refresh_player_truces = {
	trigger = {
		is_ai = no
		is_ruler = yes
		is_alive = yes
	}
	effect = {
		remove_variable = nmt_refresh_pending
		nmt_refresh_effect = yes
	}
}

# If a war starts between two rulers who hold marriage ties, end the truce cleanly
# so there is no double truce when the war ends. The attacker may also pay for it
# ('Attacking In-Laws' game rule).
# root = casus belli, scope:attacker, scope:defender
nmt_on_war_started = {
	trigger = {
		scope:attacker = {
			OR = {
				nmt_has_marriage_opinion_trigger = { TARGET = scope:defender }
				has_opinion_modifier = { target = scope:defender modifier = nmt_ai_marriage_bond_opinion }
			}
		}
	}
	effect = {
		if = {
			limit = { NOT = { has_game_rule = nmt_inlaw_war_penalty_off } }
			scope:attacker = {
				nmt_inlaw_war_penalty_effect = { TARGET = scope:defender }
			}
		}
		scope:attacker = {
			cancel_truce_both_ways = scope:defender
			nmt_remove_marriage_opinion_effect = { TARGET = scope:defender }
			# A marriage truce still waiting to start is off as well.
			remove_list_variable = { name = nmt_waiting_truce_v1 target = scope:defender }
		}
		scope:defender = {
			nmt_remove_marriage_opinion_effect = { TARGET = scope:attacker }
			remove_list_variable = { name = nmt_waiting_truce_v1 target = scope:attacker }
		}
	}
}

# root = character who joined a court, scope:new_employer, scope:old_employer (may be unset)
# Remember the court a courtier came from when they join a HUMAN ruler's court (kept 30 days).
nmt_on_join_court = {
	trigger = {
		exists = scope:old_employer
		scope:new_employer = { is_ai = no }
	}
	effect = {
		set_variable = {
			name = nmt_prev_court
			value = scope:old_employer
			days = 30
		}
		if = {
			limit = { is_married = yes }
			scope:new_employer = {
				if = {
					limit = { has_variable = nmt_verbose_debug }
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = nmt_debug_join_toast
						left_icon = scope:old_employer
					}
				}
			}
		}
	}
}

# A war started: human allies of either side who chose Truce or Neither with that ruler
# (betrothal alliance) may be called in. Watch for a refused call (nmt_alliance_effects.txt).
nmt_on_war_started_excuse = {
	effect = {
		scope:attacker = {
			every_ally = {
				limit = { is_ai = no }
				nmt_start_excuse_watch_effect = yes
			}
		}
		scope:defender = {
			every_ally = {
				limit = { is_ai = no }
				nmt_start_excuse_watch_effect = yes
			}
		}
	}
}

# Repeats every 7 days while needed (nmt_start_excuse_watch_effect). root = the human ruler.
nmt_excuse_watch = {
	trigger = {
		is_ai = no
		is_alive = yes
	}
	effect = {
		remove_variable = nmt_excuse_watch
		nmt_excuse_refused_calls_effect = yes
		nmt_start_excuse_watch_effect = yes
	}
}
