﻿# Marriage alliances. The game makes alliances for marriages by itself (at the wedding, and
# again for an heir at succession). When the player has chosen Truce or Neither for a ruler
# (nmt_declined_alliance_v1), there is no alliance with that ruler while the ties last:
# any alliance with them is cancelled when the choice is made and at every refresh.
# Direct ruler unions are never touched. To ally after all, the player uses Propose
# Alliance to In-Law, which takes the ruler off the declined list.

# this = the human ruler, $RULER$ = the other ruler.
# Never cancels an alliance the player's recorded choice for this ruler is Alliance
# (e.g. one made with Propose Alliance to In-Law). While a betrothal links the families
# the alliance is left alone, because breaking it would break the betrothal (see
# nmt_betrothal_triggers.txt); the refresh after the wedding cancels it.
nmt_cancel_marriage_alliance_effect = {
	if = {
		limit = {
			is_allied_to = $RULER$
			NOT = { nmt_is_direct_partner_trigger = { RULER = $RULER$ } }
			NOT = { is_target_in_variable_list = { name = nmt_choice_alliance_v1 target = $RULER$ } }
			NOT = { nmt_betrothal_links_trigger = { OTHER = $RULER$ } }
		}
		$RULER$ = { save_scope_as = nmt_cancel_ruler }
		hidden_effect = {
			break_alliance = $RULER$
			# A truce waiting for this alliance to go is added by the next refresh.
			nmt_queue_refresh_effect = yes
		}
		if = {
			limit = { is_target_in_variable_list = { name = nmt_choice_truce_v1 target = $RULER$ } }
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_alliance_cancelled_toast
				custom_tooltip = nmt_alliance_cancelled_truce_details
				left_icon = $RULER$
			}
		}
		else = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = nmt_alliance_cancelled_toast
				custom_tooltip = nmt_alliance_cancelled_details
				left_icon = $RULER$
			}
		}
	}
}

# this = the human ruler, $RULER$ = the other ruler.
# The player wants an alliance with this ruler after all. Removes every copy, in case an
# older version put the ruler on the declined list more than once.
nmt_undecline_alliance_effect = {
	while = {
		limit = { is_target_in_variable_list = { name = nmt_declined_alliance_v1 target = $RULER$ } }
		remove_list_variable = { name = nmt_declined_alliance_v1 target = $RULER$ }
	}
}

# this = the human ruler, $RULER$ = the other ruler.
# Remember that the player does not want an alliance with this ruler, and cancel one
# that has already appeared.
nmt_decline_alliance_effect = {
	hidden_effect = {
		if = {
			limit = {
				NOT = { is_target_in_variable_list = { name = nmt_declined_alliance_v1 target = $RULER$ } }
			}
			add_to_variable_list = {
				name = nmt_declined_alliance_v1
				target = $RULER$
			}
		}
		nmt_cancel_marriage_alliance_effect = { RULER = $RULER$ }
		# A betrothal keeps the alliance for now: watch for war calls from this ruler.
		nmt_start_excuse_watch_effect = yes
	}
}

# this = the human ruler (scope:nmt_player is saved by the refresh).
# Catches alliances the game makes after the choice: a wedding's alliance can appear a little
# after the wedding hook, a betrothal answered earlier only gets its alliance at the wedding,
# and an heir gets the marriage alliances back at succession.
nmt_declined_alliance_sweep_effect = {
	if = {
		limit = {
			has_variable_list = nmt_declined_alliance_v1
		}
		every_in_list = {
			variable = nmt_declined_alliance_v1
			limit = { is_alive = yes }
			save_scope_as = nmt_declined_ruler
			scope:nmt_player = {
				nmt_cancel_marriage_alliance_effect = { RULER = scope:nmt_declined_ruler }
			}
		}
	}
}

# this = the human ruler (scope:nmt_player is saved by the refresh).
# Runs once per character. Rulers already handled before this version have no "declined"
# record, so every handled ruler the player is not allied with counts as declined.
nmt_declined_migration_effect = {
	if = {
		limit = {
			NOT = { has_variable = nmt_declined_migrated_v1 }
		}
		set_variable = nmt_declined_migrated_v1
		if = {
			limit = { has_variable_list = nmt_partners_v2 }
			every_in_list = {
				variable = nmt_partners_v2
				limit = {
					is_alive = yes
					NOT = { is_allied_to = scope:nmt_player }
				}
				save_scope_as = nmt_migrate_ruler
				scope:nmt_player = {
					if = {
						limit = {
							NOT = { is_target_in_variable_list = { name = nmt_declined_alliance_v1 target = scope:nmt_migrate_ruler } }
						}
						add_to_variable_list = {
							name = nmt_declined_alliance_v1
							target = scope:nmt_migrate_ruler
						}
					}
				}
			}
		}
	}
}

# this = the human ruler.
# A ruler the player chose Truce or Neither with keeps the game's betrothal alliance until
# the wedding (see nmt_cancel_marriage_alliance_effect), so they can still call the player
# to war. Refusing such a call is excused: the "rejected call" opinion is removed, the
# house relation damage from vanilla call_ally_interaction is undone, and a letter from
# that ruler explains it (nmt_excuse.0001). The Fame (prestige experience) the game may
# take is not given back: in testing the game took none, so a refund was free Fame.
nmt_excuse_refused_calls_effect = {
	save_scope_as = nmt_excuse_player
	if = {
		limit = { has_variable_list = nmt_declined_alliance_v1 }
		every_in_list = {
			variable = nmt_declined_alliance_v1
			limit = {
				is_alive = yes
				OR = {
					has_opinion_modifier = { target = scope:nmt_excuse_player modifier = rejected_call_to_defensive_war }
					has_opinion_modifier = { target = scope:nmt_excuse_player modifier = rejected_call_to_offensive_war }
				}
			}
			save_scope_as = nmt_excuse_ruler
			remove_opinion = { target = scope:nmt_excuse_player modifier = rejected_call_to_defensive_war }
			remove_opinion = { target = scope:nmt_excuse_player modifier = rejected_call_to_offensive_war }
			# Vanilla damages the relation when the refusing player is house head.
			scope:nmt_excuse_player = {
				if = {
					limit = {
						exists = house
						is_house_head = yes
						exists = scope:nmt_excuse_ruler.house
						NOT = { house = scope:nmt_excuse_ruler.house }
					}
					house = {
						change_house_relation_effect = {
							HOUSE = scope:nmt_excuse_ruler.house
							VALUE = house_relation_improve_medium_value
							REASON = nmt_call_excused
							CHAR = scope:nmt_excuse_ruler
							TARGET_CHAR = scope:nmt_excuse_player
							TITLE = scope:nmt_excuse_player
						}
					}
				}
				trigger_event = nmt_excuse.0001
			}
		}
	}
}

# this = the human ruler. While a ruler the player chose Truce or Neither with is still
# allied (betrothal) and at war, check for refused calls every 7 days, so the refund comes
# soon after the refusal instead of at the next quarterly refresh.
nmt_start_excuse_watch_effect = {
	save_scope_as = nmt_excuse_player
	if = {
		limit = {
			is_ai = no
			NOT = { has_variable = nmt_excuse_watch }
			has_variable_list = nmt_declined_alliance_v1
			any_in_list = {
				variable = nmt_declined_alliance_v1
				is_alive = yes
				is_at_war = yes
				is_allied_to = scope:nmt_excuse_player
			}
		}
		set_variable = {
			name = nmt_excuse_watch
			days = 8
		}
		trigger_event = {
			on_action = nmt_excuse_watch
			days = 7
		}
	}
}
