﻿# 'AI Marriage Diplomacy' game rule: when two AI rulers' families marry, the higher-ranked
# ruler picks Alliance, Truce or Neither, like a player would.
# AI bonds are not tracked like the player's: an AI truce and its opinion bonus are simply
# timed (15 years, or 10 with the 'Up to 10 Years' truce length), and there is no cleanup.

# this = root of on_marriage, scope:spouse = the other newlywed
nmt_ai_marriage_diplomacy_effect = {
	root = { nmt_ai_find_side_ruler_effect = { SAVE = nmt_ai_side_a } }
	scope:spouse = { nmt_ai_find_side_ruler_effect = { SAVE = nmt_ai_side_b } }
	if = {
		limit = {
			exists = scope:nmt_ai_side_a
			exists = scope:nmt_ai_side_b
			scope:nmt_ai_side_a = { nmt_ai_bond_pair_trigger = { OTHER = scope:nmt_ai_side_b } }
			# Direct ruler union: the game's alliance stays, as it does for players.
			NOT = {
				AND = {
					root = scope:nmt_ai_side_a
					scope:spouse = scope:nmt_ai_side_b
				}
			}
		}
		if = {
			limit = { scope:nmt_ai_side_b.primary_title.tier > scope:nmt_ai_side_a.primary_title.tier }
			scope:nmt_ai_side_b = { save_scope_as = nmt_ai_decider }
			scope:nmt_ai_side_a = { save_scope_as = nmt_ai_other }
		}
		else = {
			scope:nmt_ai_side_a = { save_scope_as = nmt_ai_decider }
			scope:nmt_ai_side_b = { save_scope_as = nmt_ai_other }
		}
		scope:nmt_ai_decider = { nmt_ai_choose_bond_effect = yes }
	}
}

# this = a newlywed. The ruler on their side: themselves, or their highest-ranked
# ruling close relative.
nmt_ai_find_side_ruler_effect = {
	if = {
		limit = { nmt_ai_side_ruler_trigger = yes }
		save_scope_as = $SAVE$
	}
	else = {
		ordered_close_family_member = {
			limit = { nmt_ai_side_ruler_trigger = yes }
			order_by = primary_title.tier
			max = 1
			check_range_bounds = no
			save_scope_as = $SAVE$
		}
	}
}

# this = the deciding AI ruler, scope:nmt_ai_other = the other side's ruler
nmt_ai_choose_bond_effect = {
	random_list = {
		# Alliance: keep the alliance the game makes for the marriage.
		50 = {
			modifier = { add = 25 has_trait = ambitious }
			modifier = { add = 20 has_trait = gregarious }
			modifier = { add = 20 has_trait = trusting }
			# Needs friends right now.
			modifier = { add = 20 is_at_war = yes }
			# A stronger in-law is worth having on your side.
			modifier = { add = 15 scope:nmt_ai_other.primary_title.tier > scope:nmt_ai_decider.primary_title.tier }
			modifier = {
				factor = 0.3
				opinion = { target = scope:nmt_ai_other value < -20 }
			}
		}
		# Truce: peace with the in-laws, but no wars fought for them.
		30 = {
			modifier = { add = 30 has_trait = paranoid }
			modifier = { add = 20 has_trait = content }
			modifier = { add = 15 has_trait = craven }
			modifier = { add = 15 has_trait = shy }
			nmt_ai_bond_truce_effect = yes
		}
		# Neither.
		15 = {
			modifier = { add = 25 has_trait = arrogant }
			modifier = { add = 15 has_trait = deceitful }
			modifier = { add = 10 has_trait = callous }
			modifier = {
				add = 40
				opinion = { target = scope:nmt_ai_other value < -20 }
			}
			modifier = {
				add = 15
				NOT = { faith = scope:nmt_ai_other.faith }
			}
			nmt_ai_schedule_cancel_effect = yes
		}
	}
}

# this = the deciding AI ruler
nmt_ai_bond_truce_effect = {
	# Never replace a truce that is already running (e.g. from a war).
	if = {
		limit = {
			NOT = { has_truce = scope:nmt_ai_other }
			NOT = { scope:nmt_ai_other = { has_truce = scope:nmt_ai_decider } }
		}
		if = {
			limit = { has_game_rule = nmt_truce_length_10 }
			nmt_ai_add_bond_effect = { YEARS = 10 }
		}
		else = {
			nmt_ai_add_bond_effect = { YEARS = 15 }
		}
	}
	nmt_ai_schedule_cancel_effect = yes
}

# this = the deciding AI ruler
nmt_ai_add_bond_effect = {
	add_truce_both_ways = {
		character = scope:nmt_ai_other
		years = $YEARS$
		name = NMT_TRUCE_MARRIAGE
	}
	add_opinion = {
		target = scope:nmt_ai_other
		modifier = nmt_ai_marriage_bond_opinion
		years = $YEARS$
	}
	scope:nmt_ai_other = {
		add_opinion = {
			target = scope:nmt_ai_decider
			modifier = nmt_ai_marriage_bond_opinion
			years = $YEARS$
		}
	}
}

# this = the deciding AI ruler. The game makes the marriage alliance a little after the
# wedding hook, so it is cancelled a few days later (nmt_ai_cancel_alliances).
nmt_ai_schedule_cancel_effect = {
	if = {
		limit = {
			NOT = { is_target_in_variable_list = { name = nmt_ai_cancel_alliance_v1 target = scope:nmt_ai_other } }
		}
		add_to_variable_list = { name = nmt_ai_cancel_alliance_v1 target = scope:nmt_ai_other }
	}
	if = {
		limit = {
			NOT = { has_variable = nmt_ai_cancel_pending }
		}
		set_variable = {
			name = nmt_ai_cancel_pending
			days = 5
		}
		trigger_event = {
			on_action = nmt_ai_cancel_alliances
			days = 3
		}
	}
}
