﻿namespace = natural_disaster

scripted_effect natural_disaster_warning_tooltip_effect = {
	scope:situation = {
		switch = {
			trigger = situation_type
			natural_disaster_earthquake = { custom_tooltip = natural_disaster.8001.a.tt.earthquake }
			natural_disaster_flood = { custom_tooltip = natural_disaster.8001.a.tt.flood }
		}
	}
}

scripted_effect save_natural_disaster_wilderness_scope_effect = {
	scope:situation = {
		random_situation_county = {
			limit = { holder = root }
			alternative_limit = { holder.liege = root }
			alternative_limit = {
				holder = { is_vassal_or_below_of = root }
			}
			random_county_province = {
				limit = {
					trigger_if = {
						limit = { scope:situation = { situation_type = natural_disaster_earthquake } } 
						terrain = mountains
					}
					trigger_else = { terrain = floodplains }
				}
				alternative_limit = {
					trigger_if = {
						limit = { scope:situation = { situation_type = natural_disaster_earthquake } }
						terrain = desert_mountains
					}
					trigger_else = { terrain = wetlands }
				}
				alternative_limit = { always = yes }
				save_scope_as = background_wilderness_scope
			}
		}
	}
}

scripted_effect natural_disaster_remove_realm_effect = {
	scope:situation = {
		every_situation_county = {
			limit = { holder.top_liege = scope:ruler }
			custom = every_realm_disaster_county_custom
			save_scope_as = situation_county
			# Clear negative modifiers
			custom_tooltip = {
				text = natural_disaster_remove_realm_modifier_tt
				remove_county_modifier = tgp_natural_disaster_earthquake_1_county_modifier
				remove_county_modifier = tgp_natural_disaster_earthquake_2_county_modifier
				remove_county_modifier = tgp_natural_disaster_earthquake_3_county_modifier
				remove_county_modifier = tgp_natural_disaster_flood_1_county_modifier
				remove_county_modifier = tgp_natural_disaster_flood_2_county_modifier
				remove_county_modifier = tgp_natural_disaster_flood_3_county_modifier
				remove_county_modifier = tgp_natural_disaster_fortification_county_modifier
				remove_county_modifier = tgp_natural_disaster_exodus_1_county_modifier
				remove_county_modifier = tgp_natural_disaster_exodus_2_county_modifier
				remove_county_modifier = tgp_natural_disaster_exodus_3_county_modifier
				remove_county_modifier = tgp_natural_disaster_fertility_1_county_modifier
				remove_county_modifier = tgp_natural_disaster_fertility_2_county_modifier
				remove_county_modifier = tgp_natural_disaster_fertility_3_county_modifier
			}
			# Remove title from sub region
			hidden_effect = {
				scope:situation.situation_sub_region:disaster_area = { 
					remove_dejure_title_from_sub_region = scope:situation_county
				}
			}
			# Fertility modifier for floods
			if = {
				limit = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				add_county_modifier = {
					modifier = tgp_natural_disaster_flood_fertility_county_modifier
					duration_desc = until_the_disaster_is_over
					years = 10
				}
			}
		}
	}
}

# Deletes situation after last player closes great project end event (if any)
scripted_effect attempt_delete_natural_disaster_situation_effect = {
	scope:situation ?= {
		if = {
			limit = {
				any_situation_county = { count = 0 }
				NOT = {
					any_situation_participant = {
						NOT = { this = root }
						is_ai = no
					}
				}
			}
			end_situation = yes
		}
	}
}


# Notification for going into isolation due to disaster
natural_disaster.0110 = {
	type = character_event
	title = natural_disaster.0110.t
	desc = natural_disaster.0110.desc
	theme = disaster
	left_portrait = {
		character = root
		animation = fear
	}

	option = { #Into isolation we go
		name = natural_disaster.0110.a
		add_legitimacy_effect = { LEGITIMACY = minor_legitimacy_loss }
		add_prestige = major_prestige_loss
		isolate_family_decision_effect = yes
		ai_chance = {
			base = 100
		}
	}
}
natural_disaster.0111 = {
	type = character_event
	title = natural_disaster.0111.t
	desc = natural_disaster.0111.desc
	theme = disaster
	left_portrait = {
		character = root
		animation = personality_content
	}
	trigger = {
		has_character_modifier = isolating_modifier
	}

	option = { #Out of isolation we come
		name = natural_disaster.0111.a
		unisolate_family_decision_effect = yes
		ai_chance = {
			base = 100
		}
	}
}

# Disaster ends by Great Project
natural_disaster.4001 = { # Independent rulers
	type = character_event 
	title = natural_disaster.4001.t
	desc = natural_disaster.4001.desc
	theme = realm
	left_portrait = {
		character = root 
		triggered_animation = {
			trigger = { scope:situation = { situation_type = natural_disaster_flood } }
			animation = survey
		}
		animation = war_over_win
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}

	immediate = {
		save_scope_as = ruler
		natural_disaster_remove_realm_effect = yes
	}
	
	option = {
		name = natural_disaster.4001.a
	}

	after = {
		attempt_delete_natural_disaster_situation_effect = yes
	}
}

natural_disaster.4002 = { # Vassals
	type = character_event 
	title = natural_disaster.4001.t
	desc = natural_disaster.4001.desc
	theme = realm
	left_portrait = {
		character = root 
		triggered_animation = {
			trigger = { scope:situation = { situation_type = natural_disaster_flood } }
			animation = survey
		}
		animation = war_over_win
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}

	immediate = {
		show_as_tooltip = {
			natural_disaster_remove_realm_effect = yes
		}
	}
	
	option = {
		name = natural_disaster.4002.a
	}

	after = {
		attempt_delete_natural_disaster_situation_effect = yes
	}
}

#####################################
#
### IMPACT - 5001-6000
#
#####################################

# Random

scripted_effect natural_disaster_5001_effect = {
	save_scope_as = recipient
	scope:situation = {
		situation_center_province.county = {
			save_scope_as = epicenter_county
		}
		switch = {
			trigger = situation_type
			natural_disaster_earthquake = {
				scope:epicenter_county = { 
					show_as_tooltip = { change_development_level = natural_disaster_earthquake_epicenter_development_loss_value }
					hidden_effect = {
						if = {
							limit = { holder.top_liege = scope:ruler }
							change_development_level = natural_disaster_earthquake_epicenter_development_loss_value
						}
					}
				}
				every_situation_county = {
					limit = {
						holder.top_liege = scope:ruler
						NOT = { this = scope:epicenter_county }
					}
					change_development_level = natural_disaster_earthquake_development_loss_value
				}
			}
			natural_disaster_flood = {
				show_as_tooltip = {
					every_situation_county = {
						limit = { holder = scope:recipient }
						change_development_level = natural_disaster_flood_development_loss_value
						if = {
							limit = { uses_county_fertility = yes }
							change_county_fertility = natural_disaster_flood_fertility_loss_value
						}
					}
				}
				hidden_effect = {
					every_situation_county = {
						limit = { holder.top_liege = scope:ruler }
						change_development_level = natural_disaster_flood_development_loss_value
						if = {
							limit = { uses_county_fertility = yes }
							change_county_fertility = natural_disaster_flood_fertility_loss_value
						}
					}
				}
				custom_tooltip = natural_disaster_5001_flood_tt
			}
		}
	}
	if = {
		limit = {
			scope:ruler = {
				has_title = title:h_china
			}
		}
		situation:dynastic_cycle = {
			trigger_situation_catalyst = catalyst_hegemon_natural_disaster_recovered
		}
	}
}

scripted_effect inform_participating_vassals_effect = {
	scope:situation = {
		every_situation_participant = {
			limit = {
				is_ai = no
				is_vassal_or_below_of = root
			}
			trigger_event = natural_disaster.$EVENT_ID$
		}
	}
}

natural_disaster.5001 = { # Development - independent rulers
	type = character_event 
	title = natural_disaster.5001.t
	desc = {
		desc = natural_disaster.5001.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5001.desc.flood
			}
			desc = natural_disaster.5001.desc.earthquake
		}
	}
	
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	left_portrait = {
		character = root 
		animation = stress
	}

	immediate = {
		# Inform vassals
		inform_participating_vassals_effect = { EVENT_ID = 5002 }
		# Effect
		save_scope_as = ruler
		natural_disaster_5001_effect = yes
	}
	
	option = {
		name = natural_disaster.5001.a
		custom_tooltip = natural_disaster_5001_flood_tt
	}
}

natural_disaster.5002 = { # Development - vassals
	type = character_event 
	title = natural_disaster.5001.t
	desc = {
		desc = natural_disaster.5001.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5001.desc.flood
			}
			desc = natural_disaster.5001.desc.earthquake
		}
	}
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	left_portrait = {
		character = root 
		animation = stress
	}

	immediate = {
		show_as_tooltip = { natural_disaster_5001_effect = yes }
	}
	
	option = {
		name = natural_disaster.5001.a
	}
}

scripted_effect natural_disaster_5011_effect = {
	every_in_list = {
		list = target_counties
		add_county_modifier = {
			modifier = tgp_natural_disaster_fortification_county_modifier
			duration_desc = until_the_disaster_is_over
			years = 25
		}
	}
}

natural_disaster.5011 = { # Fortification - independent rulers
	type = character_event 
	title = natural_disaster.5011.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5011.desc.flood
			}
			desc = natural_disaster.5011.desc.earthquake
		}
		desc = natural_disaster.5011.desc
	}
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	left_portrait = {
		character = root 
		animation = stress
	}

	immediate = {
		# Inform vassals
		inform_participating_vassals_effect = { EVENT_ID = 5012 }
		# Effect
		natural_disaster_5011_effect = yes
	}
	
	option = {
		name = natural_disaster.5011.a
	}
}

natural_disaster.5012 = { # Fortifications - vassals
	type = character_event 
	title = natural_disaster.5011.t
		desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5011.desc.flood
			}
			desc = natural_disaster.5011.desc.earthquake
		}
		desc = natural_disaster.5011.desc
	}
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	left_portrait = {
		character = root 
		animation = stress
	}

	immediate = {
		show_as_tooltip = {
			natural_disaster_5011_effect = yes
		}
	}
	
	option = {
		name = natural_disaster.5011.a
	}
}

natural_disaster.5013 = { # Fortifications - Fix random values
	hidden = yes
	type = character_event

	trigger = {
		NOR = {
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_landless_adventurer
		}
	}

	immediate = {
		save_scope_as = ruler
		scope:situation = {
			save_scope_value_as = {
				name = realm_counties
				value = {
					value = 0
					every_situation_county = {
						limit = { holder.top_liege = scope:ruler }
						add = 1
					}
				}
			}
			save_scope_value_as = {
				name = damaged_fortifications_count
				value = {
					value = scope:realm_counties
					divide = 3
					min = 1
					max = 5
				}
			}
			while = {
				count = scope:damaged_fortifications_count
				random_situation_county = {
					limit = { holder.top_liege = scope:ruler }
					weight = {
						base = 1
						modifier = { add = title_province.fort_level }
						modifier = {
							any_county_province = { terrain = floodplains }
							add = 10
						}
						modifier = {
							any_county_province = { terrain = wetlands }
							add = 10
						}
					}
					add_to_list = target_counties
				}
			}
		}
		trigger_event = natural_disaster.5011
	}
}

scripted_effect natural_disaster_5021_effect = {
	scope:situation ?= {
		every_situation_county = {
			custom = every_disaster_county_custom
			limit = { holder.top_liege = root.top_liege }
			if = {
				limit = { scope:situation.var:severity >= 2.5 }
				add_county_modifier = {
					modifier = tgp_natural_disaster_exodus_3_county_modifier
					duration_desc = until_the_disaster_is_over
					years = 25
				}
			}
			else_if = {
				limit = { scope:situation.var:severity >= 1.5 }
				add_county_modifier = {
					modifier = tgp_natural_disaster_exodus_2_county_modifier
					duration_desc = until_the_disaster_is_over
					years = 25
				}
			}
			else = {
				add_county_modifier = {
					modifier = tgp_natural_disaster_exodus_1_county_modifier
					duration_desc = until_the_disaster_is_over
					years = 25
				}
			}
		}
	}
}

natural_disaster.5021 = { # Exodus - independent rulers
	type = character_event 
	title = natural_disaster.5021.t
	desc = natural_disaster.5021.desc
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	left_portrait = {
		character = root 
		animation = worry
	}

	immediate = {
		# Inform vassals
		inform_participating_vassals_effect = { EVENT_ID = 5022 }
		# Effect
		natural_disaster_5021_effect = yes
	}
	
	option = {
		name = natural_disaster.5021.a
	}
}

natural_disaster.5022 = { # Exodus - vassals
	type = character_event 
	title = natural_disaster.5021.t
	desc = natural_disaster.5022.desc
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	left_portrait = {
		character = root 
		animation = shock
	}

	immediate = {
		show_as_tooltip = {
			natural_disaster_5021_effect = yes
		}
	}
	
	option = {
		name = natural_disaster.5022.a
	}
}

scripted_effect natural_disaster_5031_effect = {
	scope:situation ?= {
		every_situation_county = {
			custom = every_disaster_county_custom
			limit = {
				uses_county_fertility = yes
				holder.top_liege = root.top_liege
			}
			if = {
				limit = { scope:situation.var:severity >= 2.5 }
				add_county_modifier = {
					modifier = tgp_natural_disaster_fertility_3_county_modifier
					duration_desc = until_the_disaster_is_over
					years = 25
				}
			}
			else_if = {
				limit = { scope:situation.var:severity >= 1.5 }
				add_county_modifier = {
					modifier = tgp_natural_disaster_fertility_2_county_modifier
					duration_desc = until_the_disaster_is_over
					years = 25
				}
			}
			else = {
				add_county_modifier = {
					modifier = tgp_natural_disaster_fertility_1_county_modifier
					duration_desc = until_the_disaster_is_over
					years = 25
				}
			}
		}
	}
}

natural_disaster.5031 = { # Fertility - independent rulers
	type = character_event 
	title = natural_disaster.5031.t
	desc = natural_disaster.5031.desc
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	left_portrait = {
		character = root
		animation = disbelief
	}

	trigger = {
		scope:situation = { situation_type = natural_disaster_flood }
		government_has_flag = government_is_nomadic
	}

	immediate = {
		# Inform vassals
		inform_participating_vassals_effect = { EVENT_ID = 5032 }
		# Effect
		natural_disaster_5031_effect = yes
	}
	
	option = {
		name = natural_disaster.5031.a
	}
}

natural_disaster.5032 = { # Fertility - vassals
	type = character_event 
	title = natural_disaster.5031.t
	desc = natural_disaster.5031.desc
	theme = disaster
	override_background = {
		trigger = {
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	left_portrait = {
		character = root 
		animation = shock
	}

	immediate = {
		show_as_tooltip = {
			natural_disaster_5031_effect = yes
		}
	}
	
	option = {
		name = natural_disaster.5031.a
	}
}

### DEATHS AND DAMAGE

natural_disaster.5101 = { # You May Die. Players only, AI always dies
	type = character_event 
	title = natural_disaster.5101.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5101.desc.flood
			}
			desc = natural_disaster.5101.desc.earthquake
		}
		first_valid = {
			triggered_desc = {
				trigger = { 
					is_travelling = yes
				}
				desc = natural_disaster.5101.travel
			}
			triggered_desc = {
				trigger = { is_commanding_army = yes }
				desc = natural_disaster.5101.army
			}
			triggered_desc = {
				trigger = { exists = involved_activity }
				desc = natural_disaster.5101.activity
			}
			triggered_desc = {
				trigger = {
					OR = {
						government_has_flag = government_is_landless_adventurer
						government_has_flag = government_is_nomadic
					}
				}
				desc = natural_disaster.5101.tent
			}
			triggered_desc = {
				trigger = { is_landed = no }
				desc = natural_disaster.5101.estate
			}
			desc = natural_disaster.5101.court
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5101.desc.flood_end
			}
			desc = natural_disaster.5101.desc.earthquake_end
		}
	}
	theme = disaster
	override_background = {
		trigger = {
			exists = scope:travel
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = {
			scope:situation = { situation_type = natural_disaster_flood }
			exists = scope:travel
		}
		reference = bp3_riverside
	}
	override_background = {
		trigger = { exists = scope:army }
		reference = army_camp
	}
	override_background = {
		trigger = { exists = scope:activity }
		reference = ep2_activity
	}
	override_background = {
		trigger = {
			scope:situation = { situation_type = natural_disaster_flood }
			NOR = {
				exists = scope:army
				exists = scope:travel
			}
		}
		reference = throne_room
	}
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	left_portrait = {
		character = root
		animation = aggressive_unarmed
	}
	right_portrait = {
		character = scope:at_risk_character
		animation = fear
	}

	immediate = {
		if = {
			limit = { is_travelling = yes }
			save_scope_as = travel
		}
		else_if = {
			limit = { is_in_army = yes }
			save_scope_as = army
		}
		else_if = {
			limit = { exists = involved_activity }
			save_scope_as = activity
		}

		ordered_courtier = {
			limit = {
				is_close_family_of = root
				is_ai = yes
				NOT = { is_in_list = killed_characters }
			}
			order_by = {
				value = 0
				if = {
					limit = { this = root.primary_heir }
					add = 100
				}
				else_if = {
					limit = { is_child_of = root }
					add = 50
				}
				else_if = {
					limit = { is_close_family_of = root }
					add = 25
				}
				else_if = {
					limit = { is_of_major_interest_trigger = { CHARACTER = root } }
					add = 10
				}
				else_if = {
					limit = { is_of_minor_interest_trigger = { CHARACTER = root } }
					add = 5
				}
			}
			save_scope_as = at_risk_character
		}
	}
	
	# Try to save both (requires at_risk_character)
	option = {
		name = natural_disaster.5101.a
		trigger = { exists = scope:at_risk_character }
		add_internal_flag = dangerous

		duel = {
			skill = prowess
			value = high_skill_rating
			# Success: both survive, likely wounded
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.0
					min = -39
				}
				desc = natural_disaster.5101.a.tt.success
				send_interface_toast = {
					type = event_toast_effect_good
					title = natural_disaster.5101.a.tt.success
					left_icon = root
				}
				add_trait = wounded_1
				scope:at_risk_character = { add_trait = wounded_1 }
			}
			# Failure: one of you dies
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.0
					min = -19
				}
				desc = natural_disaster.5101.a.tt.failure
				random_list = {
					50 = {
						send_interface_toast = {
							type = event_toast_effect_bad
							title = natural_disaster.5101.a.tt.failure_root
							left_icon = root
						}
						natural_disaster_death_effect = { VICTIM = root }
						scope:at_risk_character = { add_trait = wounded_1 }
					}
					50 = {
						send_interface_toast = {
							type = event_toast_effect_bad
							title = natural_disaster.5101.a.tt.failure_secondary
							left_icon = scope:at_risk_character
						}
						natural_disaster_death_effect = { VICTIM = scope:at_risk_character }
						add_trait = wounded_1
					}
				}
			}
		}

		stress_impact = {
			brave = major_stress_impact_loss
			craven = minor_stress_impact_gain
		}
	}

	# Each faces death alone (fallback, always available)
	option = {
		name = natural_disaster.5101.b
		add_internal_flag = dangerous

		duel = {
			skill = prowess
			value = medium_skill_rating
			# Success: you survive, wounded
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.0
					min = -39
				}
				desc = natural_disaster.5101.b.tt.success
				send_interface_toast = {
					type = event_toast_effect_good
					title = natural_disaster.5101.b.tt.success
					left_icon = root
				}
				add_trait = wounded_1
			}
			# Failure: you die
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.0
					min = -19
				}
				desc = natural_disaster.5101.b.tt.failure
				send_interface_toast = {
					type = event_toast_effect_bad
					title = natural_disaster.5101.b.tt.failure
					left_icon = root
				}
				natural_disaster_death_effect = { VICTIM = root }
			}
		}

		scope:at_risk_character ?= {
			custom_tooltip = {
				text = natural_disaster.5101.b.tt.at_risk_character_duel
				duel = {
					skill = prowess
					value = medium_skill_rating
					# Success: they survive, wounded
					50 = {
						compare_modifier = {
							value = scope:duel_value
							multiplier = 3.0
							min = -39
						}
						add_trait = wounded_1
					}
					# Failure: they die
					50 = {
						compare_modifier = {
							value = scope:duel_value
							multiplier = -3.0
							min = -19
						}
						natural_disaster_death_effect = { VICTIM = root }
					}
				}
			}
		}

		stress_impact = {
			brave = minor_stress_impact_gain
			craven = minor_stress_impact_loss
		}
	}

	# Option C: Sacrifice yourself to save the other (requires at_risk_character)
	option = {
		name = natural_disaster.5101.c
		trigger = { exists = scope:at_risk_character }
		add_internal_flag = dangerous

		dynasty = { add_dynasty_prestige = massive_dynasty_prestige_value }
		natural_disaster_death_effect = { VICTIM = root }
	}

	# Option D: Sacrifice the other to save your own skin (requires at_risk_character)
	option = {
		name = natural_disaster.5101.d
		trigger = { exists = scope:at_risk_character }

		stress_impact = {
			craven = minor_stress_impact_loss
			brave = major_stress_impact_gain
		}
		add_prestige = major_prestige_loss
		add_piety = major_piety_loss
		add_trait = craven
		natural_disaster_death_effect = { VICTIM = scope:at_risk_character }
	}

	after = {
		if = {
			limit = { is_alive = yes }
			natural_disaster_survival_memory_effect = yes
		}
		scope:at_risk_character = {
			if = {
				limit = { is_alive = yes }
				natural_disaster_survival_memory_effect = yes
			}
		}
	}
}

natural_disaster.5102 = { # Deaths in travel/army/activity/court/remote in realm.
	type = character_event 
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:local_death }
				desc = natural_disaster.5102.t.affected
			}
			desc = natural_disaster.5102.t.not_affected
		}
		triggered_desc = {
			trigger = { 
				has_variable = natural_disaster_5102_count
				var:natural_disaster_5102_count = 1
			}
			desc = natural_disaster.5102.t.second_round
		}
		triggered_desc = {
			trigger = { 
				has_variable = natural_disaster_5102_count
				var:natural_disaster_5102_count = 2
			}
			desc = natural_disaster.5102.t.third_round
		}
	}

	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { 
					exists = scope:travel
					exists = scope:local_death
				}
				desc = natural_disaster.5102.travel
			}
			triggered_desc = {
				trigger = { 
					exists = scope:army 
					exists = scope:local_death
				}
				desc = natural_disaster.5102.army
			}
			triggered_desc = {
				trigger = { 
					exists = involved_activity 
					exists = scope:local_death
				}
				desc = natural_disaster.5102.activity
			}
			triggered_desc = {
				trigger = {
					OR = {
						government_has_flag = government_is_landless_adventurer
						government_has_flag = government_is_nomadic
					}
					exists = scope:local_death
				}
				desc = natural_disaster.5102.tent
			}
			triggered_desc = {
				trigger = { 
					is_landed = no 
					exists = scope:local_death
				}
				desc = natural_disaster.5102.estate
			}
			triggered_desc = {
				trigger = { 
					exists = scope:local_death
				}
				desc = natural_disaster.5102.court
			}
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { 
						situation_type = natural_disaster_flood 
						exists = scope:local_death
					}
				}
				desc = natural_disaster.5102.desc.flood
			}
			triggered_desc = {
				trigger = { 
					scope:situation = { 
						situation_type = natural_disaster_earthquake 
						exists = scope:local_death
					}
				}
				desc = natural_disaster.5102.desc.earthquake
			}
		}
		# Remote notification when outside affected area
		triggered_desc = {
			trigger = {
				scope:situation = { 
					NOT = { exists = scope:local_death }
					situation_type = natural_disaster_flood
				}
			}
			desc = natural_disaster.5102.desc_remote.flood
		}
		triggered_desc = {
			trigger = {
				scope:situation = { 
					NOT = { exists = scope:local_death }
					situation_type = natural_disaster_earthquake
				}
			}
			desc = natural_disaster.5102.desc_remote.earthquake
		}
	}
	theme = disaster
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				exists = scope:local_death
			}
			animation = shock
		}
		animation = sadness
	}
	lower_right_portrait = scope:killed_courtier_1
	lower_center_portrait = scope:killed_courtier_2
	lower_left_portrait = scope:killed_courtier_3
	override_background = {
		trigger = { exists = scope:travel }
		reference = terrain_travel
	}
	override_background = {
		trigger = { exists = scope:army }
		reference = army_camp
	}
	override_background = {
		trigger = { exists = scope:activity }
		reference = ep2_activity
	}
	override_background = { reference = throne_room }
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	cooldown = { days = 1 }

	immediate = {
		if = {
			limit = { is_travelling = yes }
			save_scope_as = travel
		}
		else_if = {
			limit = { is_in_army = yes }
			save_scope_as = army
		}
		else_if = {
			limit = { exists = involved_activity }
			save_scope_as = activity
		}
		ordered_in_list = {
			list = killed_characters
			limit = {
				OR = {
					this = root.primary_heir
					is_child_of = root
					is_close_family_of = root
					root = { is_of_major_interest_trigger = { CHARACTER = this } }
					root = { is_of_minor_interest_trigger = { CHARACTER = this } }
				}
			}
			order_by = {
				value = 0
				if = {
					limit = { this = root.primary_heir }
					add = 100
				}
				else_if = {
					limit = { is_child_of = root }
					add = 50
				}
				else_if = {
					limit = { is_close_family_of = root }
					add = 25
				}
				else_if = {
					limit = {
						is_of_major_interest_trigger = { CHARACTER = root }
					}
					add = 10
				}
				else_if = {
					limit = {
						is_of_minor_interest_trigger = { CHARACTER = root }
					}
					add = 5
				}
			}
			max = 4
			check_range_bounds = no
			switch = {
				trigger = exists
				scope:killed_courtier_2 = { save_scope_as = killed_courtier_3 }
				scope:killed_courtier_1 = { save_scope_as = killed_courtier_2 }
				fallback = { save_scope_as = killed_courtier_1 }
			}
		}
		show_as_tooltip = {
			every_in_list = {
				list = killed_characters
				save_temporary_scope_as = victim_temp
				natural_disaster_death_effect = { VICTIM = scope:victim_temp }
			}
			commanding_army ?= {
				if = {
					limit = { is_in_list = damaged_armies }
					natural_disaster_army_damage_effect = yes
				}
			}
		}
		if = {
			limit = {
				exists = scope:local_death
			}
			natural_disaster_survival_memory_effect = yes
		}
	}
	
	option = {
		name = natural_disaster.5102.a
	}

	after = {
		set_variable = { # variable to track how many times this has fired, for localization only
			name = natural_disaster_5102_count
			value = {
				if = {
					limit = { exists = var:natural_disaster_5102_count }
					add = var:natural_disaster_5102_count
				}
				add = 1
			}
		}
	}
}

natural_disaster.5111 = { # Commanded army damaged.
	type = character_event 
	title = natural_disaster.5111.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5111.desc.flood
			}
			desc = natural_disaster.5111.desc.earthquake
		}
	}
	theme = disaster
	override_background = { reference = army_camp }
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	left_portrait = {
		character = root
		animation = fear
	}
	right_portrait = {
		character = scope:knight
		animation = shock
	}

	immediate = {
		commanding_army = {
			save_scope_as = army
			army_owner = {
				random_knight = {
					limit = {
						NOT = { this = root }
						knight_army ?= scope:army
					}
					save_scope_as = knight
				}
			}
			show_as_tooltip = { natural_disaster_army_damage_effect = yes }
		}
		show_as_tooltip = { natural_disaster_impact_effect = yes }
		natural_disaster_survival_memory_effect = yes
	}
	
	option = {
		name = natural_disaster.5111.a
	}
}

# Start

natural_disaster.5901 = { # Start - independent rulers
	type = character_event 
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5901.t.flood
			}
			desc = natural_disaster.5901.t.earthquake
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5901.desc.flood
			}
			desc = natural_disaster.5901.desc.earthquake
		}
	}
	theme = disaster
	override_background = {
		trigger = {
			exists = scope:travel
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = bp3_riverside
	}
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = rain
	}
	override_effect_2d = {
		trigger = { scope:situation = { situation_type = natural_disaster_earthquake } }
		reference = smoke
	}
	left_portrait = {
		character = root 
		triggered_animation = {
			trigger = {
				scope:situation = {
					situation_type = natural_disaster_earthquake
					any_situation_county = { this = ROOT.location.county }
				}
			}
			animation = fear
		}
		animation = shock
	}

	trigger = {
		is_alive = yes
		NOR = {	
			is_in_list = informees
			primary_title.previous_holder ?= { is_in_list = informees }
		}
	}

	on_trigger_fail = {
		# Inform vassals
		inform_participating_vassals_effect = { EVENT_ID = 5902 }
	}

	immediate = {
		save_scope_as = ruler
		scope:ruler = {
			if = {
				limit = { has_legitimacy = yes }
				add_legitimacy = natural_disaster_legitimacy_loss_value
			}
		}
		# Inform vassals
		inform_participating_vassals_effect = { EVENT_ID = 5902 }
		# Effect
		show_as_tooltip = { natural_disaster_impact_effect = yes }
	}
	
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:situation = { situation_type = natural_disaster_flood }
						}
						desc = natural_disaster.5901.a.flood
					}
					desc = natural_disaster.5901.a.earthquake
				}
			}
		}
	}
}

natural_disaster.5902 = { # Start - vassals
	type = character_event 
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.5901.t.flood
			}
			desc = natural_disaster.5901.t.earthquake
		}
	}
	desc = natural_disaster.5902.desc
	theme = disaster
	override_background = {
		trigger = {
			exists = scope:travel
			culture = {
				has_graphical_east_asia_culture_group_trigger = yes
			}
			scope:situation = {
				situation_type = natural_disaster_flood
			}
		}
		reference = tgp_overflowing_river
	}
	override_background = {
		trigger = { scope:situation = { situation_type = natural_disaster_flood } }
		reference = wilderness_scope
	}
	left_portrait = {
		character = root 
		animation = shock
	}
	lower_right_portrait = scope:ruler

	trigger = {
		NOR = {
			is_in_list = informees
			primary_title.previous_holder ?= { is_in_list = informees }
		}
	}

	immediate = {
		show_as_tooltip = {
			scope:ruler = {
				if = {
					limit = { has_legitimacy = yes }
					add_legitimacy = natural_disaster_legitimacy_loss_value
				}
			}
			natural_disaster_impact_effect = yes
		}
	}
	
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:situation = { situation_type = natural_disaster_flood }
						}
						desc = natural_disaster.5901.a.flood
					}
					desc = natural_disaster.5901.a.earthquake
				}
			}
		}
	}
}

#####################################
#
### RECOVERY - 6001-7000
#
#####################################

natural_disaster.6901 = { # Start - independent rulers
	type = character_event 
	title = natural_disaster.6901.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.6901.desc.flood
			}
			desc = natural_disaster.6901.desc.earthquake
		}
		desc = natural_disaster.6901.desc
	}
	theme = disaster
	override_background = { reference = throne_room }
	left_portrait = {
		character = root 
		animation = sadness
	}

	immediate = {
		inform_participating_vassals_effect = { EVENT_ID = 6902 }
	}
	
	option = {
		name = natural_disaster.6901.a
	}
}

natural_disaster.6902 = { # Start - vassals
	type = character_event 
	title = natural_disaster.6901.t
	desc = natural_disaster.6902.desc
	theme = disaster
	left_portrait = {
		character = root 
		animation = sadness
	}
	lower_right_portrait = scope:ruler

	immediate = {
		natural_disaster_vassal_recovery_start_effect = yes
	}
	
	option = {
		name = natural_disaster.6902.a
	}
}

scripted_effect natural_disaster_manage_from_home = {
	if = {
		limit = { has_active_diarchy = no }
		custom_tooltip = natural_disaster.7001.a.tt
		try_start_diarchy = temporary_regency
	}
	else = {
		custom_tooltip = natural_disaster.7001.a.tt.already_in_diarchy
	}
	custom_tooltip = natural_disaster.7001.a.tt.discount
	set_variable = {
		name = natural_disaster_contribution_discount
		value = 0.1
	}
}

#####################################
#
### WARNING - 7001-8000
#
#####################################

natural_disaster.7001 = { # Tremors - Earthquake
	type = character_event 
	title = natural_disaster.7001.t
	desc = natural_disaster.7001.desc
	theme = disaster
	override_background = { reference = wilderness_scope } # trigger = { current_location != scope:epicenter_county.title_province }
	left_portrait = {
		character = root 
		animation = worry
	}

	trigger = {
		scope:situation = { situation_type = natural_disaster_earthquake }
	}

	immediate = {
		save_natural_disaster_wilderness_scope_effect = yes
	}

	option = {
		name = natural_disaster.7001.a # Regency to commit fully to planning
		show_as_unavailable = {
			is_landed = yes 
			NOT = { has_variable = natural_disaster_received_first_warning } 
		}
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
		}
		add_stress = minor_stress_impact_gain
		natural_disaster_manage_from_home = yes
		natural_disaster_warning_tooltip_effect = yes
		
		ai_chance = 0
	}

	option = {
		name = natural_disaster.7001.b
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
			custom_tooltip = {
				text = disaster_is_not_in_your_capital
				scope:situation = {
					tpg_character_in_any_situation_sub_region_trigger = yes
				}
			}
		}

		if = {
			limit = { is_ai = no }
			custom_tooltip = {
				text = natural_disaster.7001.b.tt
				decision:isolate_family_decision = {
					open_view_data = {
						view = decision_detail
						player = root
					}
				}
			}
		}
		else = {
			isolate_family_decision_effect = yes
		}
		natural_disaster_warning_tooltip_effect = yes

		ai_chance = 0
	}

	option = {
		name = natural_disaster.7001.c
		natural_disaster_warning_tooltip_effect = yes
	}

	after = {
		set_variable = natural_disaster_received_first_warning
	}
}

natural_disaster.7011 = { # Animals Flee - Earthquake
	type = character_event 
	title = natural_disaster.7011.t
	desc = natural_disaster.7011.desc
	theme = disaster
	override_background = { reference = wilderness_scope }
	left_portrait = {
		character = root 
		animation = disappointed
	}

	trigger = {
		scope:situation = { situation_type = natural_disaster_earthquake }
	}

	immediate = {
		save_natural_disaster_wilderness_scope_effect = yes
	}
	
	option = {
		name = natural_disaster.7001.a # Regency to commit fully to planning
		show_as_unavailable = {
			is_landed = yes 
			NOT = { has_variable = natural_disaster_received_first_warning } 
		}
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
		}
		add_stress = minor_stress_impact_gain
		natural_disaster_manage_from_home = yes
		natural_disaster_warning_tooltip_effect = yes
		
		ai_chance = 0
	}

	option = {
		name = natural_disaster.7001.b
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
			custom_tooltip = {
				text = disaster_is_not_in_your_capital
				scope:situation = {
					tpg_character_in_any_situation_sub_region_trigger = yes
				}
			}
		}

		if = {
			limit = { is_ai = no }
			custom_tooltip = {
				text = natural_disaster.7001.b.tt
				decision:isolate_family_decision = {
					open_view_data = {
						view = decision_detail
						player = root
					}
				}
			}
		}
		else = {
			isolate_family_decision_effect = yes
		}
		natural_disaster_warning_tooltip_effect = yes

		ai_chance = 0
	}

	option = {
		name = natural_disaster.7001.c
		natural_disaster_warning_tooltip_effect = yes
	}

	after = {
		set_variable = natural_disaster_received_first_warning
	}
}


natural_disaster.7021 = { # Rivers Swell - Flood
	type = character_event 
	title = natural_disaster.7021.t
	desc = natural_disaster.7021.desc
	theme = disaster

	override_background = { reference = bp3_riverside }
	left_portrait = {
		character = root 
		animation = worry
	}

	trigger = {
		scope:situation = { situation_type = natural_disaster_flood }
	}
	
	option = {
		name = natural_disaster.7001.a # Regency to commit fully to planning
		show_as_unavailable = {
			is_landed = yes 
			NOT = { has_variable = natural_disaster_received_first_warning } 
		}
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
		}
		add_stress = minor_stress_impact_gain
		natural_disaster_manage_from_home = yes
		natural_disaster_warning_tooltip_effect = yes
		
		ai_chance = 0
	}

	option = {
		name = natural_disaster.7001.b
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
			custom_tooltip = {
				text = disaster_is_not_in_your_capital
				scope:situation = {
					tpg_character_in_any_situation_sub_region_trigger = yes
				}
			}
		}

		if = {
			limit = { is_ai = no }
			custom_tooltip = {
				text = natural_disaster.7001.b.tt
				decision:isolate_family_decision = {
					open_view_data = {
						view = decision_detail
						player = root
					}
				}
			}
		}
		else = {
			isolate_family_decision_effect = yes
		}
		natural_disaster_warning_tooltip_effect = yes

		ai_chance = 0
	}

	option = {
		name = natural_disaster.7021.a
		natural_disaster_warning_tooltip_effect = yes
	}

	after = {
		set_variable = natural_disaster_received_first_warning
	}
}

natural_disaster.7031 = { # Great Storm - Flood
	type = character_event 
	title = natural_disaster.7031.t
	desc = natural_disaster.7031.desc
	theme = disaster

	override_background = { reference = bp3_riverside }
	override_effect_2d = { reference = rain	}
	left_portrait = {
		character = root 
		animation = disappointed
	}

	trigger = {
		scope:situation = { situation_type = natural_disaster_flood }
	}
	
	option = {
		name = natural_disaster.7001.a # Regency to commit fully to planning
		show_as_unavailable = {
			is_landed = yes 
			NOT = { has_variable = natural_disaster_received_first_warning } 
		}
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
		}
		add_stress = minor_stress_impact_gain
		natural_disaster_manage_from_home = yes
		natural_disaster_warning_tooltip_effect = yes
		
		ai_chance = 0
	}

	option = {
		name = natural_disaster.7001.b
		trigger = {
			is_landed = yes
			NOT = {
				has_variable = natural_disaster_received_first_warning 
				has_character_modifier = isolating_modifier
			}
			custom_tooltip = {
				text = disaster_is_not_in_your_capital
				scope:situation = {
					tpg_character_in_any_situation_sub_region_trigger = yes
				}
			}
		}

		if = {
			limit = { is_ai = no }
			custom_tooltip = {
				text = natural_disaster.7001.b.tt
				decision:isolate_family_decision = {
					open_view_data = {
						view = decision_detail
						player = root
					}
				}
			}
		}
		else = {
			isolate_family_decision_effect = yes
		}
		natural_disaster_warning_tooltip_effect = yes

		ai_chance = 0
	}

	option = {
		name = natural_disaster.7031.a
		natural_disaster_warning_tooltip_effect = yes
	}

	after = {
		set_variable = natural_disaster_received_first_warning
	}
}

#####################################
#
### JOIN - 8001-9000
#
#####################################

# Warning - A player joins a Natural Disaster during the Warning phase
natural_disaster.8001 = {
	type = character_event 
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.8001.t.flood
			}
			desc = natural_disaster.8001.t.earthquake
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.8001.desc.flood
			}
			desc = natural_disaster.8001.desc.earthquake
		}
	}
	theme = disaster
	override_background = { reference = throne_room }
	left_portrait = {
		character = root 
		animation = worry
	}

	trigger = {
		scope:situation = { situation_current_phase = warning }
	}

	immediate = {
		scope:situation = { natural_disaster_save_base_scopes_effect = yes }
	}
	
	option = {
		name = natural_disaster.8001.a
		natural_disaster_warning_tooltip_effect = yes
	}
}

# Impact - A player joins a Natural Disaster during the Impact phase
natural_disaster.8002 = {
	type = character_event 
	title = natural_disaster.8002.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.8002.desc.flood
			}
			desc = natural_disaster.8002.desc.earthquake
		}
		desc = natural_disaster.8002.desc
	}
	theme = disaster
	left_portrait = {
		character = root 
		animation = worry
	}
	override_background = {
		trigger = {
			scope:situation = { situation_type = natural_disaster_flood }
		}
		reference = wilderness_wetlands
	}
	
	override_background = {
		trigger = {
			scope:situation = { situation_type = natural_disaster_earthquake }
		}
		reference = burning_building
	}

	trigger = {
		scope:situation = { situation_current_phase = impact }
	}

	immediate = {
		scope:situation = { natural_disaster_save_base_scopes_effect = yes }
	}
	
	option = {
		name = natural_disaster.8002.a
		if = {
			limit = {
				scope:situation = { situation_type = natural_disaster_flood }
			}
			custom_tooltip = natural_disaster.8002.a.tt
		}
		else = {
			custom_tooltip = natural_disaster.8002.a.tt.earthquake
		}
	}
}

# Recovery - A player joins a Natural Disaster during the Recovery phase
natural_disaster.8003 = {
	type = character_event 
	title = natural_disaster.8003.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:situation = { situation_type = natural_disaster_flood }
				}
				desc = natural_disaster.8003.desc.flood
			}
			desc = natural_disaster.8003.desc.earthquake
		}
		desc = natural_disaster.8003.desc_outro
	}
	theme = disaster
	left_portrait = {
		character = root 
		animation = sadness
	}

	trigger = {
		scope:situation = { situation_current_phase = recovery }
	}

	immediate = {
		scope:situation = { natural_disaster_save_base_scopes_effect = yes }
	}
	
	option = {
		name = natural_disaster.8003.a
		if = {
			limit = {
				scope:situation = { situation_type = natural_disaster_flood }
			}
			custom_tooltip = natural_disaster.8003.a.tt
		}
		else = {
			custom_tooltip = natural_disaster.8003.a.tt.earthquake
		}
	}
}

#####################################
#
### MAINTENANCE - 9001-9999
#
#####################################

# Warning - Fire events for all affected rulers
natural_disaster.9001 = {
	hidden = yes
	scope = situation

	immediate = {
		# Save any/all relevant scopes
		scope:situation = { natural_disaster_save_base_scopes_effect = yes }
		every_situation_participant = {
			trigger_event = { on_action = natural_disaster_warning_events }
		}
	}
}

# Impact - Fire events for independent rulers and travellers
natural_disaster.9010 = {
	hidden = yes
	scope = situation

	immediate = {
		save_scope_as = situation
		trigger_event = { id = natural_disaster.9011 } # Death/Army Damage checks
		situation_participant_group:affected_ruler = {
			every_situation_group_participant = {
				trigger_event = { 
					on_action = natural_disaster_impact_events
					days = 3
				}
			}
		}
	}
}

# Impact - Roll for deaths
natural_disaster.9011 = {
	hidden = yes
	scope = situation

	trigger = {
		OR = { # Disaster type must have a sudden deadly aspect (not droughts)
			situation_type = natural_disaster_earthquake
			situation_type = natural_disaster_flood
		}
	}

	immediate = {
		natural_disaster_death_damage_roll_effect = yes
	}
}

# Select where to spawn an Earthquake
natural_disaster.9901 = { 
	hidden = yes
	scope = none
	immediate = {
		# Select
		every_in_global_list = {
			variable = earthquake_region_list
			save_scope_as = region
			random_list = {
				1 = {
					modifier = { # Higher frequency of high magnitude earthquakes
						OR = {
							this = geographical_region:world_middle_east
							this = geographical_region:world_asia_china
							this = geographical_region:world_asia_japan
							this = geographical_region:world_asia_minor
							this = geographical_region:world_asia_indonesia
							this = geographical_region:world_asia_philippines
							this = geographical_region:world_europe_south
							this = geographical_region:world_africa_north_east
						}
						add = natural_disaster_earthquake_frequent_region_chance_per_year_value
					}
					modifier = { # Lower chance to stack
						save_temporary_scope_as = region_temp
						any_in_global_list = {
							variable = ongoing_earthquakes
							var:epicenter_county.title_province ?= { geographical_region = scope:region_temp }
						}
						add = natural_disaster_earthquake_stacked_region_chance_per_year_value
					}
					add_to_list = potential_earthquake_regions
				}
				199 = {}
			}
		}
		clear_saved_scope = region_temp

		# Randomly select one of the valid regions
		random_in_list = {
			list = potential_earthquake_regions
			save_scope_as = region
			trigger_event = {
				id = natural_disaster.9911
				months = { 0 11 }
			}
		}
	}
}

# Select where to spawn a Flood
natural_disaster.9902 = { 
	hidden = yes
	scope = none
	immediate = {
		# Select
		every_in_global_list = {
			variable = flood_region_list
			limit = {
				save_temporary_scope_as = region_temp
				NOT = { # No ongoing flood in this region
					any_in_global_list = {
						variable = ongoing_floods
						var:river_region ?= scope:region_temp
					}
				}
			}

			random_list = {
				1 = {
					modifier = { # Higher frequency of high magnitude earthquakes
						OR = {
						this = geographical_region:yellow_river_region
						this = geographical_region:yangtze_river_region
						this = geographical_region:pearl_river_region
						this = geographical_region:nile_river_region
						}
						add = natural_disaster_earthquake_frequent_region_chance_per_year_value
					}
					add_to_list = potential_flood_regions
				}
				199 = {}
			}
		}
		clear_saved_scope = region_temp

		# Randomly select one of the valid regions
		random_in_list = {
			list = potential_flood_regions
			save_scope_as = region
			trigger_event = {
				id = natural_disaster.9912
				months = { 0 11 }
			}
		}
	}
}

# Spawn Earthquake in region
natural_disaster.9911 = { 
	hidden = yes
	scope = none
	immediate = {
		spawn_earthquake_in_region_effect = { REGION = scope:region }
	}
}

# Spawn Flood in region
natural_disaster.9912 = { 
	hidden = yes
	scope = none
	immediate = {
		spawn_flood_in_region_effect = { REGION = scope:region }
	}
}
