﻿# EOTM on_action hooks.
#
# V1 automation is limited to the current player ruler death path. The five
# vanilla lifestyles run first; Wanderer is isolated and DLC-gated.

on_birth_child = {
	on_actions = {
		eotm_on_birth_child
	}
}

on_game_start_after_lobby = {
	on_actions = {
		eotm_on_game_start_after_lobby_profile_assurance
		eotm_on_game_start_after_lobby_lifestyle_point_test
	}
}

on_marriage = {
	on_actions = {
		eotm_on_marriage_profile_assurance
	}
}

on_concubinage = {
	on_actions = {
		eotm_on_concubinage_profile_assurance
	}
}

on_set_relation_friend = {
	on_actions = {
		eotm_on_set_relation_friend_profile_insight
	}
}

on_death = {
	on_actions = {
		eotm_on_living_legacy_source_death_cleanup
		eotm_on_player_death_learning_inheritance
		eotm_on_player_death_martial_inheritance
		eotm_on_player_death_diplomacy_inheritance
		eotm_on_player_death_stewardship_inheritance
		eotm_on_player_death_intrigue_inheritance
		eotm_on_player_death_wanderer_inheritance
		eotm_on_player_death_try_child_pending_activation_immediate
		eotm_on_player_death_same_dynasty_fallback_inheritance
	}
}

on_title_gain = {
	on_actions = {
		eotm_on_title_gain_living_succession_abdication
		eotm_on_title_gain_living_succession_fallback
		eotm_on_title_gain_activate_pending_unsafe_perks
		eotm_on_title_gain_lifestyle_point_test
	}
}

on_perks_refunded = {
	on_actions = {
		eotm_on_perks_refunded_lifestyle_auto_spend
	}
}

on_16th_birthday = {
	on_actions = {
		eotm_on_16th_birthday_activate_pending_inheritance
	}
}

on_birthday = {
	on_actions = {
		eotm_on_birthday_activate_pending_inheritance_fallback
		eotm_on_birthday_show_pending_inheritance_report_fallback
	}
}

quarterly_playable_pulse = {
	on_actions = {
		eotm_quarterly_current_player_branch_profile_assurance
		eotm_quarterly_player_pending_inheritance_fallback
	}
}

eotm_on_birth_child = {
	effect = {
		if = {
			limit = {
				OR = {
					scope:mother = { is_ai = no }
					scope:real_father ?= { is_ai = no }
					scope:father ?= { is_ai = no }
				}
			}
			eotm_backfill_inheritance_profile_if_missing_with_birth_report = yes
		}
	}
}

eotm_on_game_start_after_lobby_profile_assurance = {
	effect = {
		every_player = {
			if = {
				limit = { is_ruler = yes }
				set_variable = { name = eotm_was_player_ruler_source value = 1 }
			}
			set_variable = { name = eotm_family_profile_hook_source value = 1 }
			eotm_assure_shared_v1_profiles_for_player_family_and_friends = yes
			remove_variable = eotm_family_profile_hook_source
			eotm_show_deferred_inheritance_notice_effect = yes
			if = {
				limit = {
					age >= 16
					eotm_has_any_pending_adulthood_inheritance = yes
				}
				eotm_try_activate_pending_inheritance_on_adulthood_fallback = yes
			}
			else_if = {
				limit = { age >= 16 }
				eotm_show_pending_inheritance_report_window_effect = yes
			}
		}
	}
}

eotm_on_game_start_after_lobby_lifestyle_point_test = {
	effect = {
		every_player = {
			if = {
				limit = { is_ai = no }
				trigger_event = { id = eotm_report_events.0200 }
			}
		}
	}
}

eotm_on_perks_refunded_lifestyle_auto_spend = {
	effect = {
		if = {
			limit = {
				is_ai = no
				has_variable = eotm_lifestyle_auto_spend_test_enabled
			}
			debug_log = "EOTM_LIFESTYLE_AUTO_SPEND_REFUND_CHECK_SILENT"
			set_variable = { name = eotm_lifestyle_auto_spend_suppress_toasts value = 1 }
			trigger_event = { id = eotm_report_events.0204 }
		}
	}
}

eotm_on_marriage_profile_assurance = {
	effect = {
		if = {
			limit = { is_ai = no }
			set_variable = { name = eotm_family_profile_hook_source value = 3 }
			set_variable = { name = eotm_family_profile_relationship value = 1 }
			set_variable = { name = eotm_family_profile_assurance_source value = 1 }
			eotm_assure_shared_v1_base_profile_on_character = yes
			scope:spouse = {
				set_variable = { name = eotm_family_profile_hook_source value = 3 }
				set_variable = { name = eotm_family_profile_relationship value = 2 }
				set_variable = { name = eotm_family_profile_assurance_source value = 2 }
				eotm_assure_shared_v1_base_profile_on_character = yes
				remove_variable = eotm_family_profile_relationship
				remove_variable = eotm_family_profile_reason
				remove_variable = eotm_family_profile_category
				remove_variable = eotm_family_profile_hook_source
				remove_variable = eotm_family_profile_assurance_source
			}
			eotm_assure_shared_v1_profiles_for_player_family_and_friends = yes
			remove_variable = eotm_family_profile_relationship
			remove_variable = eotm_family_profile_reason
			remove_variable = eotm_family_profile_category
			remove_variable = eotm_family_profile_hook_source
			remove_variable = eotm_family_profile_assurance_source
		}
		else_if = {
			limit = { scope:spouse = { is_ai = no } }
			scope:spouse = {
				set_variable = { name = eotm_family_profile_hook_source value = 3 }
				set_variable = { name = eotm_family_profile_relationship value = 1 }
				set_variable = { name = eotm_family_profile_assurance_source value = 1 }
				eotm_assure_shared_v1_base_profile_on_character = yes
				eotm_assure_shared_v1_profiles_for_player_family_and_friends = yes
				remove_variable = eotm_family_profile_relationship
				remove_variable = eotm_family_profile_reason
				remove_variable = eotm_family_profile_category
				remove_variable = eotm_family_profile_hook_source
				remove_variable = eotm_family_profile_assurance_source
			}
			set_variable = { name = eotm_family_profile_hook_source value = 3 }
			set_variable = { name = eotm_family_profile_relationship value = 2 }
			set_variable = { name = eotm_family_profile_assurance_source value = 2 }
			eotm_assure_shared_v1_base_profile_on_character = yes
			remove_variable = eotm_family_profile_relationship
			remove_variable = eotm_family_profile_reason
			remove_variable = eotm_family_profile_category
			remove_variable = eotm_family_profile_hook_source
			remove_variable = eotm_family_profile_assurance_source
		}
	}
}

eotm_on_concubinage_profile_assurance = {
	effect = {
		if = {
			limit = { is_ai = no }
			set_variable = { name = eotm_family_profile_hook_source value = 4 }
			set_variable = { name = eotm_family_profile_relationship value = 1 }
			set_variable = { name = eotm_family_profile_assurance_source value = 1 }
			eotm_assure_shared_v1_base_profile_on_character = yes
			scope:concubine = {
				set_variable = { name = eotm_family_profile_hook_source value = 4 }
				set_variable = { name = eotm_family_profile_relationship value = 3 }
				set_variable = { name = eotm_family_profile_assurance_source value = 3 }
				eotm_assure_shared_v1_base_profile_on_character = yes
				remove_variable = eotm_family_profile_relationship
				remove_variable = eotm_family_profile_reason
				remove_variable = eotm_family_profile_category
				remove_variable = eotm_family_profile_hook_source
				remove_variable = eotm_family_profile_assurance_source
			}
			eotm_assure_shared_v1_profiles_for_player_family_and_friends = yes
			remove_variable = eotm_family_profile_relationship
			remove_variable = eotm_family_profile_reason
			remove_variable = eotm_family_profile_category
			remove_variable = eotm_family_profile_hook_source
			remove_variable = eotm_family_profile_assurance_source
		}
		else_if = {
			limit = { scope:concubine = { is_ai = no } }
			scope:concubine = {
				set_variable = { name = eotm_family_profile_hook_source value = 4 }
				set_variable = { name = eotm_family_profile_relationship value = 1 }
				set_variable = { name = eotm_family_profile_assurance_source value = 1 }
				eotm_assure_shared_v1_base_profile_on_character = yes
				eotm_assure_shared_v1_profiles_for_player_family_and_friends = yes
				remove_variable = eotm_family_profile_relationship
				remove_variable = eotm_family_profile_reason
				remove_variable = eotm_family_profile_category
				remove_variable = eotm_family_profile_hook_source
				remove_variable = eotm_family_profile_assurance_source
			}
			set_variable = { name = eotm_family_profile_hook_source value = 4 }
			set_variable = { name = eotm_family_profile_relationship value = 3 }
			set_variable = { name = eotm_family_profile_assurance_source value = 3 }
			eotm_assure_shared_v1_base_profile_on_character = yes
			remove_variable = eotm_family_profile_relationship
			remove_variable = eotm_family_profile_reason
			remove_variable = eotm_family_profile_category
			remove_variable = eotm_family_profile_hook_source
			remove_variable = eotm_family_profile_assurance_source
		}
	}
}

eotm_on_set_relation_friend_profile_insight = {
	effect = {
		if = {
			limit = {
				is_ai = no
				scope:target ?= { is_ai = yes }
			}
			scope:target = {
				eotm_assign_friendship_insight_profile_and_report_to_root_effect = yes
			}
		}
		else_if = {
			limit = {
				is_ai = yes
				scope:target ?= { is_ai = no }
			}
			eotm_assign_friendship_insight_profile_and_report_to_target_effect = yes
		}
	}
}

eotm_on_title_gain_activate_pending_unsafe_perks = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
			}
			set_variable = { name = eotm_was_player_ruler_source value = 1 }
			set_variable = { name = eotm_family_profile_hook_source value = 5 }
			eotm_assure_shared_v1_profiles_for_player_family_and_friends = yes
			remove_variable = eotm_family_profile_hook_source
			eotm_show_deferred_inheritance_notice_effect = yes
			if = {
				limit = {
					age >= 16
					eotm_has_any_pending_adulthood_inheritance = yes
				}
				eotm_try_activate_pending_inheritance_on_adulthood_fallback = yes
			}
			else_if = {
				limit = { age >= 16 }
				eotm_show_pending_inheritance_report_window_effect = yes
			}
		}
		if = {
			limit = {
				eotm_has_any_pending_unsafe_perk = yes
			}
			eotm_activate_pending_unsafe_perks = yes
		}
	}
}

eotm_on_title_gain_lifestyle_point_test = {
	effect = {
		if = {
			limit = { is_ai = no }
			trigger_event = { id = eotm_report_events.0200 }
		}
	}
}

eotm_on_title_gain_living_succession_fallback = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
			}
			eotm_try_living_succession_from_father_effect = yes
		}
	}
}

eotm_on_title_gain_living_succession_abdication = {
	effect = {
		if = {
			limit = {
				scope:transfer_type = flag:abdication
				exists = scope:previous_holder
			}
			eotm_try_living_succession_from_abdication_effect = yes
		}
	}
}

eotm_on_living_legacy_source_death_cleanup = {
	effect = {
		if = {
			limit = { has_variable = eotm_living_legacy_transferred }
			debug_log = "EOTM_LIVING_SUCCESSION_CLEANUP_START"
			debug_log = "EOTM_LIVING_SUCCESSION_FATHER_DIED"
			debug_log = "EOTM_LIVING_SUCCESSION_DUPLICATE_BLOCKED"
			every_child = {
				limit = { has_variable = eotm_living_legacy_active }
				debug_log = "EOTM_LIVING_SUCCESSION_CHILD_CLEANED"
				eotm_remove_living_father_guidance_effect = yes
			}
			debug_log = "EOTM_LIVING_SUCCESSION_CLEANUP_FINISHED"
			remove_variable = eotm_living_legacy_active
		}
	}
}

eotm_on_16th_birthday_activate_pending_inheritance = {
	effect = {
		if = {
			limit = { is_ai = no }
			set_variable = { name = eotm_family_profile_hook_source value = 6 }
			eotm_assure_shared_v1_profiles_for_player_family_and_friends = yes
			remove_variable = eotm_family_profile_hook_source
		}
		eotm_try_activate_pending_inheritance_on_adulthood = yes
	}
}

eotm_on_birthday_activate_pending_inheritance_fallback = {
	trigger = {
		is_ai = no
		age >= 16
		eotm_has_any_pending_adulthood_inheritance = yes
	}
	effect = {
		eotm_try_activate_pending_inheritance_on_adulthood_fallback = yes
	}
}

eotm_on_birthday_show_pending_inheritance_report_fallback = {
	trigger = {
		is_ai = no
		OR = {
			AND = {
				age < 16
				eotm_has_any_pending_adulthood_inheritance = yes
				NOT = { has_variable = eotm_deferred_inheritance_notice_shown }
			}
			AND = {
				age >= 16
				has_variable = eotm_pending_inheritance_report_window
				NOT = { has_variable = eotm_pending_inheritance_report_window_shown }
			}
		}
	}
	effect = {
		if = {
			limit = { age < 16 }
			eotm_show_deferred_inheritance_notice_effect = yes
		}
		else_if = {
			limit = { eotm_has_any_pending_adulthood_inheritance = yes }
			eotm_try_activate_pending_inheritance_on_adulthood_fallback = yes
		}
		else = {
			eotm_show_pending_inheritance_report_window_effect = yes
		}
	}
}

eotm_quarterly_player_pending_inheritance_fallback = {
	trigger = {
		is_ai = no
		OR = {
			has_variable = eotm_deferred_inheritance_notice_pending
			eotm_has_any_pending_adulthood_inheritance = yes
			AND = {
				has_variable = eotm_pending_inheritance_report_window
				NOT = { has_variable = eotm_pending_inheritance_report_window_shown }
			}
		}
	}
	effect = {
		if = {
			limit = { age < 16 }
			eotm_show_deferred_inheritance_notice_effect = yes
		}
		else_if = {
			limit = { eotm_has_any_pending_adulthood_inheritance = yes }
			eotm_try_activate_pending_inheritance_on_adulthood_fallback = yes
		}
		else_if = {
			limit = {
				has_variable = eotm_pending_inheritance_report_window
				NOT = { has_variable = eotm_pending_inheritance_report_window_shown }
			}
			eotm_show_pending_inheritance_report_window_effect = yes
		}
	}
}

eotm_quarterly_current_player_branch_profile_assurance = {
	trigger = {
		is_ai = no
		is_ruler = yes
	}
	effect = {
		set_variable = { name = eotm_family_profile_hook_source value = 7 }
		eotm_ensure_current_player_family_profiles_effect = yes
		remove_variable = eotm_family_profile_hook_source
	}
}

eotm_on_player_death_learning_inheritance = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
				NOT = { has_variable = eotm_living_legacy_transferred }
			}
			hidden_effect = { }
			hidden_effect = { }
			eotm_execute_learning_death_inheritance = yes
			hidden_effect = { }
		}
	}
}

eotm_on_player_death_martial_inheritance = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
				NOT = { has_variable = eotm_living_legacy_transferred }
			}
			hidden_effect = { }
			eotm_execute_martial_death_inheritance = yes
			hidden_effect = { }
		}
	}
}

eotm_on_player_death_diplomacy_inheritance = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
				NOT = { has_variable = eotm_living_legacy_transferred }
			}
			hidden_effect = { }
			eotm_execute_diplomacy_death_inheritance = yes
			hidden_effect = { }
		}
	}
}

eotm_on_player_death_stewardship_inheritance = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
				NOT = { has_variable = eotm_living_legacy_transferred }
			}
			hidden_effect = { }
			eotm_execute_stewardship_death_inheritance = yes
			hidden_effect = { }
		}
	}
}

eotm_on_player_death_intrigue_inheritance = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
				NOT = { has_variable = eotm_living_legacy_transferred }
			}
			hidden_effect = { }
			eotm_execute_intrigue_death_inheritance = yes
			hidden_effect = { }
		}
	}
}

eotm_on_player_death_wanderer_inheritance = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
				eotm_wanderer_content_available = yes
				NOT = { has_variable = eotm_living_legacy_transferred }
			}
			hidden_effect = { }
			eotm_execute_wanderer_death_inheritance = yes
			hidden_effect = { }
		}
	}
}

eotm_on_player_death_try_child_pending_activation_immediate = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
			}
			every_child = {
				limit = { eotm_has_any_pending_adulthood_inheritance = yes }
				eotm_try_activate_current_player_pending_inheritance_effect = yes
			}
		}
	}
}

eotm_on_player_death_same_dynasty_fallback_inheritance = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				is_ai = no
				NOT = { has_variable = eotm_living_legacy_transferred }
			}
			eotm_prepare_same_dynasty_fallback_inheritance_effect = yes
		}
	}
}
