# character just about to die in root scope
# if a killer is know, it's set as scope:killer
#Triggered by code

on_death = {
	effect = {
		play_music_cue = "mx_cue_death"
		every_parent = {
			sound_foundations_random_removal_effect = yes
		}
		if = {
			limit = {
				any_player = { this = root } #is_ai = no
				exists = player_heir
			}
			player_heir = {
				add_character_flag = {
					flag = tutorial_reactive_advice_succession
				}
				#MOD: was hoping to avoid this... will look for a better alternative soon
				add_character_flag = mgr_force_limit_modifiers
			}
		}
		if = {
			limit = {
				exists = global_var:mongol_empire_was_broken_up
				exists = global_var:handed_out_mongolia_in_mongol_succession # If Mongolia wasn't properly granted, something has gone wrong, and the primary_heir should receive the Mongol Empire as expected
				any_held_title = {
					this = title:e_mongol_empire
				}
			}
			destroy_title = title:e_mongol_empire
		}
		if = { #fixes error logs when player_heir doesn't exist?
			limit = { exists = player_heir } 
			player_heir = { apply_limit_modifiers_effect = yes }
		}
		#MOD: Confederate Partition vassal effect
		if = {
			limit = { 
				has_realm_law = confederate_partition_succession_law
			}
			confed_partition_succ_vassal_effect = yes
			player_heir = { add_character_flag = shatter_empire }
		}
		#MOD: Reveal secret bastards on death
		if = {
			limit = {
				has_game_rule = reveal_secret_bastards
				exists = real_father
				OR = {
					NOT = { father = real_father }
					NOT = { exists = father }
				}
			}
			mgr_reveal_secret_bastard_effect = yes
		}
		invalidate_claimant_factions_on_death_effect = yes
	}
	events = {
		death_management.0096	# Updates marriage opinions of other spouses if relevant.
		death_management.0097	# Sets a flag if the dead one was someone you loved (to make suicide available)
		death_management.0098 	# Removes rejected_from_marriage_bed_modifier if spouse dies
		death_management.0099 	# If spouse is pregnant, save as variable
		death_management.0001 	# Finds characters which would care about death for notifications etc.
		intrigue_dread.4012		# Interrupts this event chain for the imprisoner when their prisoner dies.
		stewardship_duty.1065 	# Removes the patron buff from their employer when this character dies.
		stewardship_duty.1067 	# When you die removes your clients fron your court.
		great_holy_war.0024		# Replace recipient for ongoing GHW.
		great_holy_war.0028 	# Beneficiary died, reset and replace.
		great_holy_war.0080 	# ghw_sponsor died, replace.
		great_holy_war.0083		# ghw fallback beneficiary dies.
		religious_decision.0301 # ancestor died and needs to be buried.
		religious_decision.0311 # Dynasty member has died and you can raise a runestone
		game_rule.2				# Exclave Independence
		martial_authority.2057	# Right-Hand Person Dies 
		martial_authority.2055  # Right-handed Person (person with right-hand) Died
	}
}

# Root = character
# Triggered when someone is about to die from a natural death but is given a second chance by meeting
# the has_natural_death_second_chance scripted rule
on_natural_death_second_chance = {
	effect = {
		add_character_flag = {
			flag = know_thyself_perk_delay_period
			days = 390
		}
		add_character_flag = know_thyself_has_triggered_delay
		trigger_event = death_management.9998
		trigger_event = {
			id = death_management.9999
			days = { 340 380 }
		}
	}
}
