﻿# Rota Fortunae — effects. Character scope unless stated otherwise.
# Realm state is stored on the ruler's primary title so it survives successions.

###########################################################################
# State bootstrap
###########################################################################

# Title scope: write a fresh realm state. $PHASE$ 1-4, $FORTUNE$ 0-100.
rf_write_state_effect = {
	set_variable = { name = rf_phase value = $PHASE$ }
	set_variable = { name = rf_fortune value = $FORTUNE$ }
	set_variable = { name = rf_phase_years value = $YEARS$ }
	set_variable = { name = rf_fortune_delta value = 0 }
	if = { limit = { NOT = { exists = var:rf_menace } } set_variable = { name = rf_menace value = 0 } }
	if = {
		limit = { exists = holder.house }
		set_variable = { name = rf_dyn_house value = holder.house }
	}
	set_variable = { name = rf_dyn_years value = 0 }
}

# Weighted random state, used at game start and for realms that appear later.
rf_random_state_effect = {
	random_list = {
		35 = { rf_write_state_effect = { PHASE = 1 FORTUNE = 60 YEARS = 3 } }
		30 = { rf_write_state_effect = { PHASE = 2 FORTUNE = 70 YEARS = 8 } }
		28 = { rf_write_state_effect = { PHASE = 3 FORTUNE = 40 YEARS = 6 } }
		7 = { rf_write_state_effect = { PHASE = 4 FORTUNE = 20 YEARS = 2 } }
	}
}

# Character scope: make sure this realm has a state. Moves it from a former primary title if needed.
rf_ensure_state_effect = {
	if = {
		limit = { NOT = { exists = primary_title.var:rf_fortune } }
		primary_title = { save_scope_as = rf_new_title }
		if = {
			limit = {
				any_held_title = {
					this != scope:rf_new_title
					exists = var:rf_fortune
				}
			}
			random_held_title = {
				limit = {
					this != scope:rf_new_title
					exists = var:rf_fortune
				}
				save_scope_as = rf_old_title
			}
			rf_move_state_effect = yes
		}
		else = {
			primary_title = {
				rf_write_state_effect = { PHASE = 1 FORTUNE = 55 YEARS = 0 }
			}
		}
		rf_apply_phase_modifier_effect = yes
	}
}

# Uses scope:rf_old_title and scope:rf_new_title.
rf_move_state_effect = {
	scope:rf_new_title = {
		set_variable = { name = rf_menace value = scope:rf_old_title.var:rf_menace }
		set_variable = { name = rf_fortune value = scope:rf_old_title.var:rf_fortune }
		set_variable = { name = rf_phase value = scope:rf_old_title.var:rf_phase }
		set_variable = { name = rf_phase_years value = scope:rf_old_title.var:rf_phase_years }
		set_variable = { name = rf_fortune_delta value = 0 }
		set_variable = { name = rf_dyn_years value = 0 }
		if = {
			limit = { exists = scope:rf_old_title.var:rf_dyn_years }
			set_variable = { name = rf_dyn_years value = scope:rf_old_title.var:rf_dyn_years }
		}
		if = {
			limit = { exists = scope:rf_old_title.var:rf_dyn_house }
			set_variable = { name = rf_dyn_house value = scope:rf_old_title.var:rf_dyn_house }
		}
		if = {
			limit = { exists = scope:rf_old_title.var:rf_coalition_against }
			set_variable = { name = rf_coalition_against value = scope:rf_old_title.var:rf_coalition_against }
			# The target list must point at the new title.
			var:rf_coalition_against = {
				if = {
					limit = { has_variable_list = rf_coalition }
					remove_list_variable = { name = rf_coalition target = scope:rf_old_title }
					add_to_variable_list = { name = rf_coalition target = scope:rf_new_title }
				}
			}
		}
		if = {
			limit = { exists = scope:rf_old_title.var:rf_coalition_cooldown }
			set_variable = { name = rf_coalition_cooldown value = yes years = 10 }
		}
		if = {
			limit = { exists = scope:rf_old_title.var:rf_coalition_war }
			set_variable = { name = rf_coalition_war value = scope:rf_old_title.var:rf_coalition_war }
		}
	}
	scope:rf_old_title = {
		if = {
			limit = { has_variable_list = rf_recent_conquests }
			every_in_list = {
				variable = rf_recent_conquests
				scope:rf_new_title = { add_to_variable_list = { name = rf_recent_conquests target = prev } }
			}
			clear_variable_list = rf_recent_conquests
		}
		if = {
			limit = { has_variable_list = rf_coalition }
			every_in_list = {
				variable = rf_coalition
				scope:rf_new_title = { add_to_variable_list = { name = rf_coalition target = prev } }
				set_variable = { name = rf_coalition_against value = scope:rf_new_title }
			}
			clear_variable_list = rf_coalition
		}
		rf_clear_title_state_effect = yes
	}
}

# Title scope.
rf_clear_title_state_effect = {
	remove_variable = rf_menace
	remove_variable = rf_fortune
	remove_variable = rf_phase
	remove_variable = rf_phase_years
	remove_variable = rf_fortune_delta
	remove_variable = rf_dyn_years
	remove_variable = rf_dyn_house
	remove_variable = rf_coalition_against
	remove_variable = rf_coalition_cooldown
	remove_variable = rf_coalition_war
	remove_variable = rf_memento_mori
	remove_variable = rf_fd_events
}

###########################################################################
# Phase modifiers
###########################################################################

rf_remove_phase_modifiers_effect = {
	if = { limit = { has_character_modifier = rf_regnabo_gentle } remove_character_modifier = rf_regnabo_gentle }
	if = { limit = { has_character_modifier = rf_regnabo_normal } remove_character_modifier = rf_regnabo_normal }
	if = { limit = { has_character_modifier = rf_regnabo_harsh } remove_character_modifier = rf_regnabo_harsh }
	if = { limit = { has_character_modifier = rf_regno_gentle } remove_character_modifier = rf_regno_gentle }
	if = { limit = { has_character_modifier = rf_regno_normal } remove_character_modifier = rf_regno_normal }
	if = { limit = { has_character_modifier = rf_regno_harsh } remove_character_modifier = rf_regno_harsh }
	if = { limit = { has_character_modifier = rf_regnavi_gentle } remove_character_modifier = rf_regnavi_gentle }
	if = { limit = { has_character_modifier = rf_regnavi_normal } remove_character_modifier = rf_regnavi_normal }
	if = { limit = { has_character_modifier = rf_regnavi_harsh } remove_character_modifier = rf_regnavi_harsh }
	if = { limit = { has_character_modifier = rf_sine_regno_gentle } remove_character_modifier = rf_sine_regno_gentle }
	if = { limit = { has_character_modifier = rf_sine_regno_normal } remove_character_modifier = rf_sine_regno_normal }
	if = { limit = { has_character_modifier = rf_sine_regno_harsh } remove_character_modifier = rf_sine_regno_harsh }
}

rf_add_phase_modifier_effect = {
	if = {
		limit = { has_game_rule = rf_wheel_gentle }
		add_character_modifier = rf_$PHASE$_gentle
	}
	else_if = {
		limit = { has_game_rule = rf_wheel_harsh }
		add_character_modifier = rf_$PHASE$_harsh
	}
	else = {
		add_character_modifier = rf_$PHASE$_normal
	}
}

rf_apply_phase_modifier_effect = {
	rf_remove_phase_modifiers_effect = yes
	if = {
		limit = {
			rf_wheel_enabled_trigger = yes
			rf_is_realm_trigger = yes
			exists = primary_title.var:rf_phase
		}
		if = { limit = { rf_phase_value = 1 } rf_add_phase_modifier_effect = { PHASE = regnabo } }
		else_if = { limit = { rf_phase_value = 2 } rf_add_phase_modifier_effect = { PHASE = regno } }
		else_if = { limit = { rf_phase_value = 3 } rf_add_phase_modifier_effect = { PHASE = regnavi } }
		else_if = { limit = { rf_phase_value = 4 } rf_add_phase_modifier_effect = { PHASE = sine_regno } }
	}
}

###########################################################################
# Wheel of Fortune
###########################################################################

# Title scope: add $AMOUNT$ to the fortune events accumulator (applied next yearly pulse).
rf_fortune_event_effect = {
	if = {
		limit = { exists = var:rf_fortune }
		if = {
			limit = { NOT = { exists = var:rf_fd_events } }
			set_variable = { name = rf_fd_events value = 0 }
		}
		change_variable = { name = rf_fd_events add = $AMOUNT$ }
	}
}

# Title scope: immediate fortune change (decisions, war results, events).
rf_change_fortune_effect = {
	if = {
		limit = { exists = var:rf_fortune }
		change_variable = { name = rf_fortune add = $AMOUNT$ }
		if = { limit = { var:rf_fortune > 100 } set_variable = { name = rf_fortune value = 100 } }
		if = { limit = { var:rf_fortune < 0 } set_variable = { name = rf_fortune value = 0 } }
	}
}

# Title scope.
rf_change_menace_effect = {
	if = {
		limit = { NOT = { exists = var:rf_menace } }
		set_variable = { name = rf_menace value = 0 }
	}
	change_variable = { name = rf_menace add = $AMOUNT$ }
	if = { limit = { var:rf_menace > 100 } set_variable = { name = rf_menace value = 100 } }
	if = { limit = { var:rf_menace < 0 } set_variable = { name = rf_menace value = 0 } }
}

rf_yearly_fortune_effect = {
	set_local_variable = { name = rf_d_phase value = { value = rf_fortune_phase_inertia_value multiply = rf_wheel_pace_value } }
	set_local_variable = { name = rf_d_ruler value = { value = rf_fortune_ruler_value multiply = rf_wheel_pace_value } }
	set_local_variable = { name = rf_d_vassals value = { value = rf_fortune_vassals_value multiply = rf_wheel_pace_value } }
	set_local_variable = { name = rf_d_debt value = { value = rf_fortune_debt_value multiply = rf_wheel_pace_value } }
	set_local_variable = { name = rf_d_size value = { value = rf_fortune_size_value multiply = rf_wheel_pace_value } }
	set_local_variable = { name = rf_d_hubris value = { value = rf_fortune_hubris_value multiply = rf_wheel_pace_value } }
	set_local_variable = { name = rf_d_dynasty value = { value = rf_fortune_dynasty_value multiply = rf_wheel_pace_value } }
	set_local_variable = { name = rf_d_events value = { value = rf_fortune_events_value multiply = rf_wheel_pace_value } }
	random_list = {
		1 = { set_local_variable = { name = rf_d_luck value = -1.5 } }
		2 = { set_local_variable = { name = rf_d_luck value = -1 } }
		3 = { set_local_variable = { name = rf_d_luck value = -0.5 } }
		4 = { set_local_variable = { name = rf_d_luck value = 0 } }
		3 = { set_local_variable = { name = rf_d_luck value = 0.5 } }
		2 = { set_local_variable = { name = rf_d_luck value = 1 } }
		1 = { set_local_variable = { name = rf_d_luck value = 1.5 } }
	}
	primary_title = {
		set_variable = { name = rf_fd_phase value = local_var:rf_d_phase }
		set_variable = { name = rf_fd_ruler value = local_var:rf_d_ruler }
		set_variable = { name = rf_fd_vassals value = local_var:rf_d_vassals }
		set_variable = { name = rf_fd_debt value = local_var:rf_d_debt }
		set_variable = { name = rf_fd_size value = local_var:rf_d_size }
		set_variable = { name = rf_fd_hubris value = local_var:rf_d_hubris }
		set_variable = { name = rf_fd_dynasty value = local_var:rf_d_dynasty }
		set_variable = { name = rf_fd_luck value = local_var:rf_d_luck }
		set_variable = { name = rf_fd_events_last value = local_var:rf_d_events }
		set_variable = { name = rf_fortune_delta value = var:rf_fd_phase }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_ruler }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_vassals }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_debt }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_size }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_hubris }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_dynasty }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_events_last }
		change_variable = { name = rf_fortune_delta add = var:rf_fd_luck }
		set_variable = { name = rf_fd_events value = 0 }
		rf_change_fortune_effect = { AMOUNT = var:rf_fortune_delta }
		change_variable = { name = rf_phase_years add = 1 }
	}
	rf_check_phase_transition_effect = yes
}

rf_check_phase_transition_effect = {
	if = {
		limit = { rf_phase_value = 1 rf_fortune_value >= 70 rf_phase_years_value >= 10 }
		rf_enter_phase_effect = { PHASE = 2 }
	}
	else_if = {
		limit = { rf_phase_value = 1 rf_fortune_value <= 25 rf_phase_years_value >= 10 }
		rf_enter_phase_effect = { PHASE = 3 }
	}
	else_if = {
		limit = { rf_phase_value = 2 rf_fortune_value <= 45 rf_phase_years_value >= 15 }
		rf_enter_phase_effect = { PHASE = 3 }
	}
	else_if = {
		limit = { rf_phase_value = 3 rf_fortune_value >= 70 rf_phase_years_value >= 10 }
		rf_enter_phase_effect = { PHASE = 2 }
	}
	else_if = {
		limit = { rf_phase_value = 3 rf_fortune_value <= 10 rf_phase_years_value >= 15 }
		rf_enter_phase_effect = { PHASE = 4 }
	}
	else_if = {
		limit = { rf_phase_value = 4 rf_fortune_value >= 40 rf_phase_years_value >= 5 }
		rf_enter_phase_effect = { PHASE = 1 }
	}
	else_if = {
		# The realm is still coming apart: further breakaways from time to time.
		limit = { rf_phase_value = 4 rf_phase_years_value >= 2 }
		random = { chance = 30 rf_collapse_pressure_effect = { CHANCE = 25 } }
	}
}

rf_enter_phase_effect = {
	primary_title = {
		set_variable = { name = rf_previous_phase value = var:rf_phase }
		set_variable = { name = rf_phase value = $PHASE$ }
		set_variable = { name = rf_phase_years value = 0 }
		if = { limit = { var:rf_phase = 4 } remove_variable = rf_memento_mori }
	}
	rf_apply_phase_modifier_effect = yes
	save_scope_as = rf_turning_realm
	trigger_event = rf_wheel.$PHASE$
	# The great powers' turns are news everywhere.
	if = {
		limit = { realm_size >= 40 }
		every_player = {
			limit = { this != scope:rf_turning_realm }
			send_interface_message = {
				type = event_generic_neutral
				title = rf_msg_wheel_turns_title_$PHASE$
				desc = rf_msg_wheel_turns_desc_$PHASE$
				left_icon = scope:rf_turning_realm
			}
		}
	}
	if = {
		limit = { rf_phase_value = 4 }
		rf_collapse_pressure_effect = { CHANCE = 55 }
	}
}

# Powerful vassals push for independence.
rf_collapse_pressure_effect = {
	save_scope_as = rf_failing_liege
	every_vassal = {
		limit = {
			highest_held_title_tier >= tier_duchy
			is_ai = yes
			NOT = { is_at_war_with = scope:rf_failing_liege }
			can_create_faction = { type = independence_faction target = scope:rf_failing_liege }
		}
		random = {
			chance = $CHANCE$
			create_faction = { type = independence_faction target = scope:rf_failing_liege }
			joined_faction ?= { add_faction_discontent = 40 }
		}
	}
}

###########################################################################
# Menace
###########################################################################

rf_yearly_menace_effect = {
	set_local_variable = { name = rf_m_gain value = rf_menace_structural_value }
	set_local_variable = { name = rf_m_decay value = rf_menace_decay_value }
	primary_title = {
		save_scope_as = rf_pruned_title
		rf_change_menace_effect = { AMOUNT = local_var:rf_m_gain }
		change_variable = { name = rf_menace subtract = local_var:rf_m_decay }
		if = { limit = { var:rf_menace < 0 } set_variable = { name = rf_menace value = 0 } }
		set_variable = { name = rf_menace_last_gain value = local_var:rf_m_gain }
		set_variable = { name = rf_menace_last_decay value = local_var:rf_m_decay }
		set_variable = { name = rf_menace_trend value = var:rf_menace_last_gain }
		change_variable = { name = rf_menace_trend subtract = var:rf_menace_last_decay }
		# Forget conquests older than thirty years.
		if = {
			limit = { has_variable_list = rf_recent_conquests }
			every_in_list = {
				variable = rf_recent_conquests
				limit = {
					OR = {
						NOT = { exists = var:rf_conq_year }
						var:rf_conq_year <= { value = current_year subtract = 30 }
					}
				}
				add_to_temporary_list = rf_expired_conquests
			}
			every_in_list = {
				list = rf_expired_conquests
				remove_variable = rf_conq_year
				remove_variable = rf_conq_from
				scope:rf_pruned_title = { remove_list_variable = { name = rf_recent_conquests target = prev } }
			}
		}
	}
	# Every sovereign within reach grows wary.
	if = {
		limit = { rf_menace_value >= 25 }
		save_scope_as = rf_threat
		every_independent_ruler = {
			limit = {
				this != scope:rf_threat
				rf_is_realm_trigger = yes
				rf_in_coalition_range_trigger = { THREAT = scope:rf_threat }
			}
			if = {
				limit = { has_opinion_modifier = { modifier = rf_wary_of_menace_opinion target = scope:rf_threat } }
				remove_opinion = { modifier = rf_wary_of_menace_opinion target = scope:rf_threat }
			}
			if = {
				limit = { has_opinion_modifier = { modifier = rf_wary_major_opinion target = scope:rf_threat } }
				remove_opinion = { modifier = rf_wary_major_opinion target = scope:rf_threat }
			}
			if = {
				limit = { has_opinion_modifier = { modifier = rf_wary_extreme_opinion target = scope:rf_threat } }
				remove_opinion = { modifier = rf_wary_extreme_opinion target = scope:rf_threat }
			}
			if = {
				limit = { scope:rf_threat.rf_menace_value >= 75 }
				add_opinion = { target = scope:rf_threat modifier = rf_wary_extreme_opinion years = 2 }
			}
			else_if = {
				limit = { scope:rf_threat.rf_menace_value >= 50 }
				add_opinion = { target = scope:rf_threat modifier = rf_wary_major_opinion years = 2 }
			}
			else = {
				add_opinion = { target = scope:rf_threat modifier = rf_wary_of_menace_opinion years = 2 }
			}
		}
	}
}

# on_title_gain hook. root = new holder, scope:title, scope:previous_holder, scope:transfer_type.
rf_on_conquest_effect = {
	top_liege = { save_scope_as = rf_conqueror }
	# The realm the county was taken from (the previous holder's sovereign).
	if = {
		limit = { exists = scope:previous_holder }
		scope:previous_holder = {
			if = {
				limit = { is_alive = yes exists = top_liege }
				top_liege = { save_scope_as = rf_victim }
			}
			else = { save_scope_as = rf_victim }
		}
	}
	if = {
		limit = {
			scope:title = { tier = tier_county }
			scope:rf_conqueror = { rf_is_realm_trigger = yes }
			OR = {
				scope:transfer_type = flag:conquest
				scope:transfer_type = flag:conquest_claim
				scope:transfer_type = flag:conquest_holy_war
			}
			# Wars between one's own vassals do not alarm the neighbours.
			NOT = { scope:rf_victim ?= scope:rf_conqueror }
		}
		if = {
			limit = { scope:transfer_type = flag:conquest }
			save_scope_value_as = { name = rf_conquest_mult value = 1.5 }
		}
		else_if = {
			limit = { scope:transfer_type = flag:conquest_holy_war }
			save_scope_value_as = { name = rf_conquest_mult value = 0.75 }
		}
		else = {
			save_scope_value_as = { name = rf_conquest_mult value = 1 }
		}
		scope:rf_conqueror = { rf_ensure_state_effect = yes }
		set_local_variable = {
			name = rf_gain
			value = {
				value = scope:title.rf_county_menace_gain_value
				multiply = scope:rf_conqueror.rf_menace_speed_mult_value
			}
		}
		if = {
			limit = { exists = scope:rf_conqueror.primary_title.var:rf_sword_years }
			set_local_variable = { name = rf_gain value = { value = local_var:rf_gain multiply = 1.25 } }
		}
		if = {
			limit = {
				exists = scope:rf_victim
				scope:rf_victim = { is_alive = yes is_landed = no }
			}
			change_local_variable = { name = rf_gain add = 5 }
		}
		scope:rf_conqueror.primary_title = {
			rf_change_menace_effect = { AMOUNT = local_var:rf_gain }
			if = {
				limit = {
					NOT = { is_target_in_variable_list = { name = rf_recent_conquests target = scope:title } }
				}
				add_to_variable_list = { name = rf_recent_conquests target = scope:title }
			}
		}
		scope:title = {
			set_variable = { name = rf_conq_year value = current_year }
			if = {
				limit = { exists = scope:rf_victim.primary_title }
				set_variable = { name = rf_conq_from value = scope:rf_victim.primary_title }
			}
			else = { remove_variable = rf_conq_from }
		}
		if = {
			limit = {
				exists = scope:rf_victim
				scope:rf_victim = { is_alive = yes is_landed = yes }
			}
			scope:rf_victim = {
				set_variable = { name = rf_lost_land_to value = scope:rf_conqueror.primary_title years = 30 }
			}
		}
	}
}

###########################################################################
# Coalitions
###########################################################################

# Character scope = new member. $THREAT$ = the threatening ruler.
rf_join_coalition_effect = {
	save_scope_as = rf_joiner
	$THREAT$ = { save_scope_as = rf_coalition_target }
	# Remember whether this joiner turns a lone member into a real coalition.
	if = {
		limit = { scope:rf_coalition_target.rf_coalition_size_value < 2 }
		scope:rf_coalition_target = { add_character_flag = { flag = rf_coalition_was_forming days = 1 } }
	}
	rf_ensure_state_effect = yes
	scope:rf_coalition_target = { rf_ensure_state_effect = yes }
	scope:rf_coalition_target.primary_title = {
		add_to_variable_list = { name = rf_coalition target = scope:rf_joiner.primary_title }
	}
	primary_title = {
		set_variable = { name = rf_coalition_against value = scope:rf_coalition_target.primary_title }
	}
	add_opinion = { target = scope:rf_coalition_target modifier = rf_coalition_enemy_opinion }
	scope:rf_coalition_target = { add_opinion = { target = scope:rf_joiner modifier = rf_coalition_enemy_opinion } }
	scope:rf_coalition_target.primary_title = {
		every_in_list = {
			variable = rf_coalition
			limit = { exists = holder holder != scope:rf_joiner }
			holder = {
				add_opinion = { target = scope:rf_joiner modifier = rf_coalition_ally_opinion }
				scope:rf_joiner = { add_opinion = { target = prev modifier = rf_coalition_ally_opinion } }
				if = {
					limit = { is_ai = no }
					send_interface_message = {
						type = event_generic_good
						title = rf_msg_member_joined_title
						desc = rf_msg_member_joined_desc
						left_icon = scope:rf_joiner
						right_icon = scope:rf_coalition_target
					}
				}
			}
		}
	}
	# The hegemonic crisis opens once a real coalition exists, and widens with each member.
	if = {
		limit = { scope:rf_coalition_target.rf_coalition_size_value >= 2 }
		scope:rf_coalition_target = { rf_crisis_open_or_extend_effect = yes }
	}
	# The target learns of it.
	if = {
		limit = {
			scope:rf_coalition_target = { has_character_flag = rf_coalition_was_forming }
			scope:rf_coalition_target.rf_coalition_size_value >= 2
		}
		scope:rf_coalition_target = {
			remove_character_flag = rf_coalition_was_forming
			trigger_event = rf_coalition.1
		}
	}
	else_if = {
		limit = { scope:rf_coalition_target = { is_ai = no } }
		scope:rf_coalition_target = {
			send_interface_message = {
				type = event_generic_bad
				title = rf_msg_coalition_grows_title
				desc = rf_msg_coalition_grows_desc
				left_icon = scope:rf_joiner
			}
		}
	}
}

# Character scope = member leaving.
rf_leave_coalition_effect = {
	if = {
		limit = { exists = primary_title.var:rf_coalition_against }
		save_scope_as = rf_leaver
		primary_title.var:rf_coalition_against = {
			if = {
				limit = { has_variable_list = rf_coalition }
				remove_list_variable = { name = rf_coalition target = scope:rf_leaver.primary_title }
			}
		}
		primary_title = { remove_variable = rf_coalition_against }
	}
}

# Title scope = the target's title. Dissolves the coalition.
rf_dissolve_coalition_effect = {
	if = {
		limit = { exists = var:rf_crisis }
		var:rf_crisis = {
			situation_sub_region:rf_theatre = { change_phase = { phase = rf_crisis_aftermath } }
			set_variable = { name = rf_crisis_end_year value = { value = current_year add = 5 } }
		}
		remove_variable = rf_crisis
	}
	if = {
		limit = { has_variable_list = rf_coalition }
		every_in_list = {
			variable = rf_coalition
			remove_variable = rf_coalition_against
		}
		clear_variable_list = rf_coalition
	}
	remove_variable = rf_coalition_war
}

# Character scope = coalition target. Remove members that no longer qualify.
rf_coalition_upkeep_effect = {
	save_scope_as = rf_coalition_target
	primary_title = {
		every_in_list = {
			variable = rf_coalition
			limit = {
				OR = {
					NOT = { exists = holder }
					holder = { rf_is_realm_trigger = no }
					holder = { is_allied_to = scope:rf_coalition_target }
					holder = { is_tributary_of = scope:rf_coalition_target }
					NOT = { var:rf_coalition_against ?= scope:rf_coalition_target.primary_title }
					# Out of reach (outside a coalition war): distant realms lose interest.
					AND = {
						NOT = { exists = scope:rf_coalition_target.primary_title.var:rf_coalition_war }
						holder = { NOT = { rf_in_coalition_range_trigger = { THREAT = scope:rf_coalition_target } } }
					}
				}
			}
			add_to_temporary_list = rf_leaving_titles
		}
		every_in_list = {
			list = rf_leaving_titles
			remove_variable = rf_coalition_against
			scope:rf_coalition_target.primary_title = { remove_list_variable = { name = rf_coalition target = prev } }
		}
	}
	# The threat has passed.
	if = {
		limit = {
			rf_menace_value < 25
			NOT = { exists = primary_title.var:rf_coalition_war }
		}
		if = {
			limit = { rf_coalition_size_value >= 1 }
			every_player = {
				limit = { rf_in_coalition_against_trigger = { THREAT = scope:rf_coalition_target } }
				send_interface_message = {
					type = event_generic_neutral
					title = rf_msg_coalition_dissolved_title
					desc = rf_msg_coalition_dissolved_desc
					left_icon = scope:rf_coalition_target
				}
			}
		}
		primary_title = { rf_dissolve_coalition_effect = yes }
	}
}

# Character scope = a candidate, scope:rf_threat set. Joins (AI) or is invited (player).
rf_coalition_candidate_effect = {
	if = {
		limit = { is_ai = yes }
		rf_join_coalition_effect = { THREAT = scope:rf_threat }
	}
	else_if = {
		limit = { NOT = { has_character_flag = rf_coalition_invite_cooldown } }
		add_character_flag = { flag = rf_coalition_invite_cooldown years = 3 }
		trigger_event = rf_coalition.2
	}
}

# Character scope = the threat. Every sovereign within reach considers joining.
rf_coalition_recruitment_effect = {
	save_scope_as = rf_threat
	every_independent_ruler = {
		limit = {
			rf_can_join_coalition_against_trigger = { THREAT = scope:rf_threat }
			rf_in_coalition_range_trigger = { THREAT = scope:rf_threat }
		}
		random = {
			chance = rf_coalition_join_chance_value
			rf_coalition_candidate_effect = yes
		}
	}
}

# Character scope = the threat. The coalition considers striking.
rf_coalition_war_check_effect = {
	if = {
		limit = { rf_coalition_war_ready_trigger = yes }
		save_scope_as = rf_coalition_target
		primary_title = { save_scope_as = rf_target_title }
		primary_title = {
			ordered_in_list = {
				variable = rf_coalition
				limit = {
					exists = holder
					holder = {
						is_imprisoned = no
						NOT = { is_at_war_with = scope:rf_coalition_target }
						NOT = { has_truce = scope:rf_coalition_target }
					}
				}
				order_by = holder.max_military_strength
				position = 0
				holder = { save_scope_as = rf_coalition_leader }
			}
		}
		if = {
			limit = { exists = scope:rf_coalition_leader }
			if = {
				limit = { scope:rf_coalition_leader = { is_ai = yes } }
				random = {
					chance = 40
					scope:rf_coalition_leader = { rf_declare_coalition_war_effect = { TARGET = scope:rf_coalition_target } }
				}
			}
			else_if = {
				limit = { scope:rf_coalition_leader = { NOT = { has_character_flag = rf_coalition_war_offer_cooldown } } }
				scope:rf_coalition_leader = {
					add_character_flag = { flag = rf_coalition_war_offer_cooldown years = 2 }
					trigger_event = rf_coalition.3
				}
			}
		}
	}
}

# Character scope = coalition leader.
rf_declare_coalition_war_effect = {
	$TARGET$.primary_title = {
		set_variable = { name = rf_coalition_war value = yes }
	}
	$TARGET$ = { rf_crisis_set_phase_effect = { PHASE = rf_crisis_war } }
	start_war = {
		casus_belli = rf_coalition_war
		target = $TARGET$
	}
}

# Called from the CB's on_declaration. Brings in every member.
rf_coalition_war_join_members_effect = {
	scope:defender.primary_title = {
		every_in_list = {
			variable = rf_coalition
			limit = {
				exists = holder
				holder = {
					this != scope:attacker
					is_imprisoned = no
					NOT = { is_at_war_with = scope:attacker }
				}
			}
			holder = { save_scope_as = rf_member }
			scope:war ?= {
				if = {
					limit = { NOT = { is_participant = scope:rf_member } }
					add_attacker = scope:rf_member
				}
			}
		}
	}
}

# on_war_started hook: a coalition member attacked by its target is defended by all.
rf_coalition_mutual_defense_effect = {
	if = {
		limit = {
			exists = scope:attacker
			exists = scope:defender
			scope:attacker = { rf_has_coalition_against_trigger = yes }
			scope:defender = { rf_in_coalition_against_trigger = { THREAT = scope:attacker } }
		}
		scope:attacker.primary_title = {
			every_in_list = {
				variable = rf_coalition
				limit = {
					exists = holder
					holder = {
						this != scope:defender
						is_imprisoned = no
						NOT = { is_at_war_with = scope:attacker }
					}
				}
				holder = { save_scope_as = rf_member }
				scope:war ?= {
					if = {
						limit = { NOT = { is_participant = scope:rf_member } }
						add_defender = scope:rf_member
					}
				}
				scope:rf_member = {
					if = {
						limit = { is_ai = no }
						send_interface_message = {
							type = event_generic_neutral
							title = rf_msg_called_to_defend_title
							desc = rf_msg_called_to_defend_desc
							left_icon = scope:defender
							right_icon = scope:attacker
						}
					}
				}
			}
		}
	}
}

###########################################################################
# Coalition war outcomes (CB scope: scope:attacker, scope:defender)
###########################################################################

# Title scope = a recent conquest of scope:defender, scope:rf_coalition_leader set.
rf_restore_county_effect = {
	save_scope_as = rf_restored_county
	# 1. The former sovereign still reigns.
	if = {
		limit = {
			exists = var:rf_conq_from.holder
			var:rf_conq_from.holder = {
				is_alive = yes
				is_landed = yes
				this != scope:defender
				NOT = { is_vassal_or_below_of = scope:defender }
			}
		}
		var:rf_conq_from.holder = { save_scope_as = rf_new_owner }
	}
	# 2. A coalition member next door.
	else_if = {
		limit = {
			any_neighboring_county = {
				exists = holder
				holder.top_liege = {
					rf_in_coalition_against_trigger = { THREAT = scope:defender }
				}
			}
		}
		random_neighboring_county = {
			limit = {
				exists = holder
				holder.top_liege = {
					rf_in_coalition_against_trigger = { THREAT = scope:defender }
				}
			}
			holder.top_liege = { save_scope_as = rf_new_owner }
		}
	}
	# 3. A local lord rises.
	else = {
		create_character = {
			location = scope:rf_restored_county.title_province
			culture = scope:rf_restored_county.culture
			faith = scope:rf_restored_county.faith
			template = post_migration_local_warlord_template
			dynasty = generate
			save_scope_as = rf_new_owner
		}
	}
	create_title_and_vassal_change = {
		type = returned
		save_scope_as = rf_change
		add_claim_on_loss = yes
	}
	if = {
		limit = { holder = scope:defender }
		change_title_holder = {
			holder = scope:rf_new_owner
			change = scope:rf_change
			take_baronies = yes
		}
	}
	else = {
		change_title_holder_include_vassals = {
			holder = scope:rf_new_owner
			change = scope:rf_change
			take_baronies = yes
		}
	}
	resolve_title_and_vassal_change = scope:rf_change
	remove_variable = rf_conq_year
	remove_variable = rf_conq_from
}

rf_coalition_victory_effect = {
	scope:defender.primary_title = { save_scope_as = rf_target_title }
	scope:attacker = { save_scope_as = rf_coalition_leader }
	# Return recent conquests still in the defender's realm.
	scope:rf_target_title = {
		if = {
			limit = { has_variable_list = rf_recent_conquests }
			every_in_list = {
				variable = rf_recent_conquests
				limit = {
					exists = holder
					OR = {
						holder = scope:defender
						holder = { is_vassal_or_below_of = scope:defender }
					}
					NOT = { this = scope:defender.capital_county }
				}
				add_to_temporary_list = rf_to_restore
			}
			every_in_list = {
				list = rf_to_restore
				rf_restore_county_effect = yes
			}
			clear_variable_list = rf_recent_conquests
		}
	}
	# Reparations shared between the members.
	scope:defender = { save_scope_value_as = { name = rf_reparations value = rf_reparations_value } }
	scope:rf_target_title = {
		every_in_list = {
			variable = rf_coalition
			limit = { exists = holder }
			holder = {
				save_scope_as = rf_member
				add_prestige = 150
				add_character_modifier = { modifier = rf_coalition_victor_modifier years = 10 }
				scope:defender = {
					pay_short_term_gold = {
						target = scope:rf_member
						gold = {
							value = scope:rf_reparations
							divide = { value = scope:defender.rf_coalition_size_value min = 1 }
						}
					}
					add_truce_both_ways = { character = scope:rf_member years = 15 name = rf_coalition_truce }
				}
			}
		}
	}
	scope:defender = {
		add_prestige = -300
		add_character_modifier = { modifier = rf_humbled_modifier years = 15 }
		primary_title = {
			set_variable = { name = rf_menace value = 0 }
			rf_change_fortune_effect = { AMOUNT = -20 }
			set_variable = { name = rf_coalition_cooldown value = yes years = 10 }
			rf_dissolve_coalition_effect = yes
		}
	}
}

rf_coalition_defeat_effect = {
	scope:defender.primary_title = { save_scope_as = rf_target_title }
	scope:rf_target_title = {
		every_in_list = {
			variable = rf_coalition
			limit = { exists = holder }
			holder = {
				save_scope_as = rf_member
				add_prestige = -100
				pay_short_term_gold = {
					target = scope:defender
					gold = rf_reparations_value
				}
				add_truce_both_ways = { character = scope:defender years = 10 name = rf_coalition_truce }
			}
		}
	}
	scope:defender = {
		add_prestige = 400
		add_character_modifier = { modifier = rf_world_bows_modifier years = 10 }
		primary_title = {
			rf_change_menace_effect = { AMOUNT = -30 }
			rf_change_fortune_effect = { AMOUNT = 10 }
			set_variable = { name = rf_coalition_cooldown value = yes years = 15 }
			rf_dissolve_coalition_effect = yes
		}
	}
}

rf_coalition_white_peace_effect = {
	scope:defender.primary_title = { save_scope_as = rf_target_title }
	scope:rf_target_title = {
		every_in_list = {
			variable = rf_coalition
			limit = { exists = holder }
			holder = { add_truce_both_ways = { character = scope:defender years = 10 name = rf_coalition_truce } }
		}
	}
	scope:defender.primary_title = {
		rf_change_menace_effect = { AMOUNT = -15 }
		set_variable = { name = rf_coalition_cooldown value = yes years = 10 }
		rf_dissolve_coalition_effect = yes
	}
}

###########################################################################
# Yearly pulse
###########################################################################

rf_yearly_realm_effect = {
	rf_ensure_state_effect = yes
	# Dynastic age.
	primary_title = {
		if = {
			limit = {
				exists = holder.house
				OR = {
					NOT = { exists = var:rf_dyn_house }
					var:rf_dyn_house != holder.house
				}
			}
			set_variable = { name = rf_dyn_house value = holder.house }
			set_variable = { name = rf_dyn_years value = 0 }
		}
		else = {
			change_variable = { name = rf_dyn_years add = 1 }
		}
	}
	rf_yearly_menace_effect = yes
	rf_menace_tier_notify_effect = yes
	if = {
		limit = { rf_wheel_enabled_trigger = yes }
		rf_yearly_fortune_effect = yes
	}
	if = {
		limit = { exists = primary_title.var:rf_crisis }
		rf_crisis_update_effect = yes
	}
	rf_apply_phase_modifier_effect = yes
	if = {
		limit = { rf_coalitions_enabled_trigger = yes }
		if = {
			limit = { primary_title = { has_variable_list = rf_coalition } }
			rf_coalition_upkeep_effect = yes
		}
		if = {
			limit = { rf_can_be_coalition_target_trigger = yes }
			rf_coalition_recruitment_effect = yes
			rf_coalition_war_check_effect = yes
		}
	}
	# A member whose target has vanished, or whose target title is no longer a sovereign's
	# primary title (personal union, demotion), is released.
	if = {
		limit = { exists = primary_title.var:rf_coalition_against }
		if = {
			limit = { NOT = { exists = primary_title.var:rf_coalition_against.holder } }
			rf_leave_coalition_effect = yes
		}
		else_if = {
			limit = {
				NOT = { primary_title.var:rf_coalition_against.holder.primary_title = primary_title.var:rf_coalition_against }
			}
			rf_leave_coalition_effect = yes
		}
		else_if = {
			limit = {
				primary_title.var:rf_coalition_against.holder = { rf_is_realm_trigger = no }
			}
			rf_leave_coalition_effect = yes
		}
	}
}

# Global, once a year: refresh every player's list of the world's menacing powers.
rf_refresh_threat_lists_effect = {
	every_player = {
		if = {
			limit = { has_variable_list = rf_gui_threats }
			clear_variable_list = rf_gui_threats
		}
		save_scope_as = rf_viewer
		ordered_independent_ruler = {
			limit = {
				rf_is_realm_trigger = yes
				rf_menace_value >= 25
			}
			order_by = rf_menace_value
			max = 8
			check_range_bounds = no
			scope:rf_viewer = { add_to_variable_list = { name = rf_gui_threats target = prev } }
		}
	}
}

###########################################################################
# Game start
###########################################################################

# Title scope, historical presets.
rf_preset_effect = {
	if = {
		limit = { exists = holder }
		rf_write_state_effect = { PHASE = $PHASE$ FORTUNE = $FORTUNE$ YEARS = $YEARS$ }
	}
}

rf_game_start_effect = {
	set_global_variable = { name = rf_initialized value = yes }
	every_independent_ruler = {
		limit = { rf_is_realm_trigger = yes }
		primary_title = { rf_random_state_effect = yes }
	}
	# 867: Abbasid anarchy, Macedonian renaissance, the Carolingian partitions.
	if = {
		limit = { current_year < 900 }
		title:e_arabia ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 3 FORTUNE = 30 YEARS = 6 } } } }
		title:e_byzantium ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 1 FORTUNE = 62 YEARS = 0 } } } }
		title:k_east_francia ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 3 FORTUNE = 42 YEARS = 4 } } } }
		title:k_france ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 3 FORTUNE = 38 YEARS = 4 } } } }
	}
	# 1066: Seljuk ascent, the Investiture storm, Byzantium's crisis.
	else_if = {
		limit = { current_year < 1100 }
		title:e_byzantium ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 3 FORTUNE = 35 YEARS = 9 } } } }
		title:e_hre ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 3 FORTUNE = 45 YEARS = 3 } } } }
		title:e_persia ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 1 FORTUNE = 68 YEARS = 4 } } } }
	}
	# 1178: Saladin's rise, Komnenian twilight.
	else_if = {
		limit = { current_year < 1250 }
		title:e_byzantium ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 2 FORTUNE = 52 YEARS = 15 } } } }
		title:k_egypt ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 1 FORTUNE = 70 YEARS = 4 } } } }
		title:e_hre ?= { if = { limit = { exists = holder } holder.primary_title = { rf_preset_effect = { PHASE = 2 FORTUNE = 72 YEARS = 12 } } } }
	}
	every_independent_ruler = {
		limit = { rf_is_realm_trigger = yes }
		rf_apply_phase_modifier_effect = yes
	}
	rf_refresh_great_powers_effect = yes
	rf_refresh_threat_lists_effect = yes
}

###########################################################################
# Menace notifications (players only)
###########################################################################

rf_menace_tier_notify_effect = {
	if = {
		limit = {
			is_ai = no
			exists = primary_title.var:rf_menace_tier_last
			rf_menace_tier_value > primary_title.var:rf_menace_tier_last
		}
		if = {
			limit = { rf_menace_tier_value = 1 }
			send_interface_toast = { type = event_toast_effect_bad title = rf_toast_menace_tier_1 left_icon = root }
		}
		else_if = {
			limit = { rf_menace_tier_value = 2 }
			send_interface_toast = { type = event_toast_effect_bad title = rf_toast_menace_tier_2 left_icon = root }
		}
		else_if = {
			limit = { rf_menace_tier_value = 3 }
			send_interface_toast = { type = event_toast_effect_bad title = rf_toast_menace_tier_3 left_icon = root }
		}
	}
	set_local_variable = { name = rf_tier_now value = rf_menace_tier_value }
	primary_title = { set_variable = { name = rf_menace_tier_last value = local_var:rf_tier_now } }
}

###########################################################################
# Great powers
###########################################################################

# Global, yearly. Ranks the eight mightiest sovereigns of kingdom tier or higher.
rf_refresh_great_powers_effect = {
	# Forget last year's ranking.
	if = {
		limit = { has_global_variable_list = rf_great_powers }
		every_in_global_list = {
			variable = rf_great_powers
			if = {
				limit = { has_character_modifier = rf_hegemon_modifier }
				remove_character_modifier = rf_hegemon_modifier
			}
			if = {
				limit = { has_character_modifier = rf_great_power_modifier }
				remove_character_modifier = rf_great_power_modifier
			}
			primary_title ?= { remove_variable = rf_gp_rank }
		}
		clear_global_variable_list = rf_great_powers
	}
	set_global_variable = { name = rf_rank_counter value = 0 }
	ordered_independent_ruler = {
		limit = {
			rf_is_realm_trigger = yes
			highest_held_title_tier >= tier_kingdom
		}
		order_by = rf_power_value
		max = 8
		check_range_bounds = no
		change_global_variable = { name = rf_rank_counter add = 1 }
		add_to_global_variable_list = { name = rf_great_powers target = this }
		primary_title = { set_variable = { name = rf_gp_rank value = global_var:rf_rank_counter } }
		if = {
			limit = { global_var:rf_rank_counter = 1 }
			add_character_modifier = rf_hegemon_modifier
			save_scope_as = rf_new_hegemon
		}
		else = {
			add_character_modifier = rf_great_power_modifier
		}
	}
	# A new hegemon is news everywhere.
	if = {
		limit = {
			exists = scope:rf_new_hegemon
			NOT = { global_var:rf_hegemon_title ?= scope:rf_new_hegemon.primary_title }
		}
		set_global_variable = { name = rf_hegemon_title value = scope:rf_new_hegemon.primary_title }
		every_player = {
			send_interface_message = {
				type = event_generic_neutral
				title = rf_msg_new_hegemon_title
				desc = rf_msg_new_hegemon_desc
				left_icon = scope:rf_new_hegemon
			}
		}
	}
	# Each player gets an ordered copy for the panel.
	every_player = {
		if = {
			limit = { has_variable_list = rf_gui_powers }
			clear_variable_list = rf_gui_powers
		}
		save_scope_as = rf_viewer
		every_in_global_list = {
			variable = rf_great_powers
			scope:rf_viewer = { add_to_variable_list = { name = rf_gui_powers target = prev } }
		}
	}
}

###########################################################################
# Hegemonic crisis (a vanilla Situation, one per coalition target)
###########################################################################

# Character scope = the coalition target.
rf_crisis_open_or_extend_effect = {
	save_scope_as = rf_crisis_hegemon
	if = {
		limit = { NOT = { exists = primary_title.var:rf_crisis } }
		start_situation = {
			type = rf_hegemonic_crisis
			save_scope_as = rf_new_crisis
		}
		scope:rf_new_crisis ?= {
			set_variable = { name = rf_hegemon_title value = scope:rf_crisis_hegemon.primary_title }
			set_situation_center_province = scope:rf_crisis_hegemon.capital_province
			# The capital anchors the theatre until the realms are added below.
			situation_sub_region:rf_theatre = { add_dejure_title_to_sub_region = scope:rf_crisis_hegemon.capital_county }
			scope:rf_crisis_hegemon.primary_title = { set_variable = { name = rf_crisis value = scope:rf_new_crisis } }
		}
		every_player = {
			limit = {
				OR = {
					this = scope:rf_crisis_hegemon
					rf_in_coalition_against_trigger = { THREAT = scope:rf_crisis_hegemon }
				}
			}
			send_interface_message = {
				type = event_generic_neutral
				title = rf_msg_crisis_opened_title
				desc = rf_msg_crisis_opened_desc
				left_icon = scope:rf_crisis_hegemon
			}
		}
	}
	rf_crisis_sync_theatre_effect = yes
}

# Character scope = the coalition target. The theatre is exactly the land of the target's realm
# and of its members' realms: on the crisis map the two sides stand out and the rest is grey.
rf_crisis_sync_theatre_effect = {
	save_scope_as = rf_crisis_hegemon
	primary_title.var:rf_crisis ?= {
		save_scope_as = rf_crisis_sit
		situation_sub_region:rf_theatre = { save_scope_as = rf_crisis_region }
	}
	if = {
		limit = { exists = scope:rf_crisis_region }
		# Land that no longer belongs to either side leaves the theatre.
		scope:rf_crisis_region = {
			every_situation_sub_region_county = {
				limit = {
					NOT = {
						holder ?= {
							top_liege = {
								OR = {
									this = scope:rf_crisis_hegemon
									rf_in_coalition_against_trigger = { THREAT = scope:rf_crisis_hegemon }
								}
							}
						}
					}
				}
				scope:rf_crisis_region = { remove_dejure_title_from_sub_region = prev }
			}
		}
		rf_crisis_add_realm_to_theatre_effect = yes
		primary_title = {
			every_in_list = {
				variable = rf_coalition
				limit = { exists = holder }
				holder = { rf_crisis_add_realm_to_theatre_effect = yes }
			}
		}
	}
}

# Character scope = a side of the crisis. Needs scope:rf_crisis_sit and scope:rf_crisis_region.
rf_crisis_add_realm_to_theatre_effect = {
	every_realm_county = {
		limit = {
			NOT = { any_county_situation = { this = scope:rf_crisis_sit } }
		}
		scope:rf_crisis_region = { add_dejure_title_to_sub_region = prev }
	}
}

# Character scope = the coalition target. $PHASE$ = phase key.
rf_crisis_set_phase_effect = {
	primary_title.var:rf_crisis ?= {
		if = {
			limit = { NOT = { situation_current_phase = $PHASE$ } }
			situation_sub_region:rf_theatre = { change_phase = { phase = $PHASE$ } }
		}
	}
}

# Character scope = the coalition target, yearly.
rf_crisis_update_effect = {
	rf_crisis_sync_theatre_effect = yes
	if = {
		limit = { exists = primary_title.var:rf_coalition_war }
		rf_crisis_set_phase_effect = { PHASE = rf_crisis_war }
	}
	else_if = {
		limit = { rf_menace_value >= 75 }
		rf_crisis_set_phase_effect = { PHASE = rf_crisis_escalation }
	}
	else = {
		rf_crisis_set_phase_effect = { PHASE = rf_crisis_tension }
	}
}
