﻿#####################################################################
# EFFECTS
#####################################################################

spawn_earthquake_in_region_effect = {
	random_county_in_region = {
		region = $REGION$
		limit = { 
			development_level >= 10
			NOT = {
				any_county_situation = {
					situation_type = natural_disaster_earthquake
				}
			}
		}
		alternative_limit = { 
			development_level >= 5
			NOT = {
				any_county_situation = {
					situation_type = natural_disaster_earthquake
				}
			}
		}
		alternative_limit = { 
			development_level >= 2
			NOT = {
				any_county_situation = {
					situation_type = natural_disaster_earthquake
				}
			}
		}
		weight = {
			base = 1
			modifier = {
				any_county_province = { terrain = mountains }
				add = 1
			}
			modifier = {
				any_county_province = { terrain = desert_mountains }
				add = 1
			}
			# Distribution
			modifier = {
				save_temporary_scope_as = county_temp
				any_in_global_list = {
					variable = ongoing_earthquakes
					var:epicenter_county.kingdom ?= scope:county_temp.kingdom
				}
				factor = 0.5
			}
			modifier = {
				save_temporary_scope_as = county_temp
				any_in_global_list = {
					variable = ongoing_earthquakes
					var:epicenter_county.duchy ?= scope:county_temp.duchy
				}
				factor = 0.5
			}
			modifier = {
				save_temporary_scope_as = county_temp
				any_in_global_list = {
					variable = ongoing_earthquakes
					var:epicenter_county ?= scope:county_temp
				}
				factor = 0.1
			}
		}

		spawn_earthquake_in_county_effect = { COUNTY = this }
	}
}

spawn_earthquake_in_county_effect = {
	$COUNTY$ = { save_scope_as = epicenter_county }

	# Track globally for exclusivity and distribution
	start_situation = {
		type = natural_disaster_earthquake
		save_scope_as = situation
	}

	scope:situation ?= {
		set_situation_center_province = scope:epicenter_county.title_province
		add_to_global_variable_list = {
			name = ongoing_earthquakes
			target = this
		}
		set_variable = {
			name = fatalities
			value = 0
		}

		# Potentially overridden below based on region
		save_scope_value_as = {
			name = min_severity
			value = 1
		}
		save_scope_value_as = {
			name = max_severity
			value = 3
		}

		scope:epicenter_county.title_province ?= {
			switch = {
				trigger = geographical_region
				geographical_region:world_europe_west_britannia = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_europe_north = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_middle_east_arabia = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_india_deccan = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_india_rajastan = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:custom_lanka = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_africa_west = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_steppe_west = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_siberia = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_asia_korea = { save_scope_value_as = { name = max_severity value = 1 } }
				geographical_region:world_asia_borneo = { save_scope_value_as = { name = max_severity value = 1 } }
			}
		}
		scope:epicenter_county.title_province ?= {
			switch = {
				trigger = geographical_region
				geographical_region:world_europe_south = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:world_asia_minor = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:world_middle_east = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:world_tibet = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:world_burma = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:custom_arakan_mountains = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:world_asia = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:custom_bactria = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:world_himalaya = { save_scope_value_as = { name = min_severity value = 2 } }
			}
		}

		set_variable = {
			name = severity
			value = {
				add = {
					fixed_range = {
						min = 1
						max = 3
					}
					min = scope:min_severity
					max = scope:max_severity
				}
			}
		}

		set_variable = {
			name = epicenter_county
			value = scope:epicenter_county
		}

		situation_sub_region:disaster_area = { add_dejure_title_to_sub_region = scope:epicenter_county }
		# Add neighbours to sub_region
		scope:epicenter_county = {
			every_neighboring_county = {
				add_to_temporary_list = neighbouring_counties
				save_temporary_scope_as = neighbouring_county
				scope:situation.situation_sub_region:disaster_area = { add_dejure_title_to_sub_region = scope:neighbouring_county }
				# Cleanup for simpler debugging
				clear_saved_scope = neighbouring_county
			}
		}
		log_natural_disaster = "started"
	}
}

spawn_flood_in_region_effect = {
	$REGION$ = { save_scope_as = river_region }

	random_county_in_region = {
		region = $REGION$
		limit = { development_level >= 10 }
		alternative_limit = { development_level >= 5 }
		alternative_limit = { development_level >= 2 }
		weight = {
			base = 1
			modifier = {
				any_county_province = { terrain = farmlands }
				add = 1
			}
			modifier = {
				any_county_province = { terrain = plains }
				add = 1
			}
			# Distribution
			modifier = {
				save_temporary_scope_as = county_temp
				any_in_global_list = {
					variable = ongoing_floods
					var:epicenter_county.kingdom ?= scope:county_temp.kingdom
				}
				factor = 0.5
			}
			modifier = {
				save_temporary_scope_as = county_temp
				any_in_global_list = {
					variable = ongoing_floods
					var:epicenter_county.duchy ?= scope:county_temp.duchy
				}
				factor = 0.5
			}
			modifier = {
				save_temporary_scope_as = county_temp
				any_in_global_list = {
					variable = ongoing_floods
					var:epicenter_county ?= scope:county_temp
				}
				factor = 0.1
			}
		}
		spawn_flood_in_county_effect = { COUNTY = this }
	}
}

spawn_flood_in_county_effect = {
	$COUNTY$ = { save_scope_as = flood_county }

	if = {
		limit = { NOT = { exists = scope:river_region } }
		random_in_global_list = {
			variable = flood_region_list
			limit = {
				save_temporary_scope_as = region_temp
				NOT = {
					any_in_global_list = {
						variable = ongoing_floods
						scope:region_temp = this
					}
				}
				scope:flood_county.title_province = { geographical_region =  scope:region_temp }
			}
			save_scope_as = river_region
		}
	}

	start_situation = {
		type = natural_disaster_flood
		save_scope_as = situation
	}

	# Track globally for exclusivity and distribution
	scope:situation ?= {
		set_situation_center_province = scope:flood_county.title_province
		add_to_global_variable_list = {
			name = ongoing_floods
			target = this
		}

		# Save region for exclusivity checks and Situation naming
		set_variable = {
			name = river_region
			value = scope:river_region
		}
		
		set_variable = {
			name = fatalities
			value = 0
		}

		# Potentially overridden below based on region
		save_scope_value_as = {
			name = min_severity
			value = 1
		}
		save_scope_value_as = {
			name = max_severity
			value = 3
		}

		scope:flood_county.title_province ?= {
			switch = {
				trigger = geographical_region
				geographical_region:yellow_river_region = { save_scope_value_as = { name = min_severity value = 2 } }
				geographical_region:nile_river_region = { save_scope_value_as = { name = min_severity value = 2 } }
			}
		}

		set_variable = {
			name = severity
			value = {
				add = {
					fixed_range = {
						min = 1
						max = 3
					}
					min = scope:min_severity
					max = scope:max_severity
				}
			}
		}

		random_situation_sub_region = { add_geographical_region = scope:river_region }
		log_natural_disaster = "started"
	}
}

# Requires the following scopes:
# this = character
# scope:situation
# scope:situation_sub_region
natural_disaster_ruler_start_effect = {
	save_scope_as = ruler
	scope:situation.situation_center_province ?= {
		if = {
			limit = { scope:ruler = county.holder.top_liege }
			save_scope_as = disaster_province
		}
	}

	if = {
		limit = { NOT = { exists = scope:disaster_province } }
		scope:situation_sub_region = {
			random_situation_sub_region_county = {
				limit = { scope:ruler = holder.top_liege }
				title_province = {
					save_scope_as = disaster_province
				}
			}
		}
	}
	tgp_activate_catalyst_against_hegemon_effect = {
		HEGEMON = scope:ruler.top_liege
		CATALYST = catalyst_hegemon_natural_disaster
	}
	scope:situation = {
		switch = {
			trigger = situation_type
			natural_disaster_earthquake = {
				scope:ruler = { natural_disaster_start_great_project_effect = { TYPE = earthquake } }
			}
			natural_disaster_flood = {
				scope:ruler = { natural_disaster_start_great_project_effect = { TYPE = flood } }
			}
		}
	}
	scope:great_project ?= {
		set_variable = {
			name = situation
			value = scope:situation
		}
		scope:situation = { # Nested to prevent errors from non-existence
			add_to_variable_list = {
				name = great_projects
				target = scope:great_project
			}
		}
	}
}

natural_disaster_start_great_project_effect = {
	natural_disaster_save_disaster_province_effect = yes
	if = { # make sure this ruler doesn't already have a great project for this disaster 
		limit = {
			NOT = {
				any_great_project = {
					great_project_type = great_project_$TYPE$
					great_project_owner = scope:ruler
					var:situation = scope:situation
				}
			}
		}
		scope:disaster_province ?= {
			plan_great_project = {
				great_project_type = great_project_$TYPE$
				founder = scope:ruler
				save_scope_as = great_project
			}
		}
	}
}

great_projects_ensure_unique_disaster_recovery_projects_effect = {
	save_scope_as = title_holder
	every_character_situation = {
		limit = { tgp_natural_disaster_situation_is_disaster_trigger = yes }
		every_great_project = {
			limit = {
				OR = {
					great_project_type = great_project_earthquake
					great_project_type = great_project_flood
				}
				great_project_owner = scope:title_holder
				var:situation = prev
			}
			add_to_list = recovery_projects
		}
		random_in_list = { # find the one to keep
			list = recovery_projects
			limit = { great_project_founder = scope:title_holder }
			alternative_limit = { great_project_founder.house = scope:title_holder.house }
			alternative_limit = { always = yes } # basically random
			save_scope_as = project_to_keep
		}
		every_in_list = { # destroy the rest
			list = recovery_projects
			limit = { this != scope:project_to_keep }
			cancel_great_project = this
		}
		every_in_list = { # clear list for next situation
			list = recovery_projects
			remove_from_list = recovery_projects
		}
	}
}

natural_disaster_vassal_recovery_start_effect = {
	show_as_tooltip = {
		custom_tooltip = natural_disaster_vassal_recovery_start_tt
	}
}

natural_disaster_vassal_start_effect = {
	show_as_tooltip = {
		root.top_liege = {
			if = {
				limit = { has_legitimacy = yes }
				add_legitimacy = natural_disaster_legitimacy_loss_value
			}
		}
		scope:existing_great_project.great_project_location ?= {
			plan_great_project = {
				great_project_type = natural_disaster_$TYPE$
				founder = root.top_liege
			}
		}
		
	}
}

# Effect to manage spacing of warning events
natural_disaster_fire_warning_event_effect = {
	trigger_event = {
		id = natural_disaster.9001
		days = 20
	}
}

# Effect to manage spacing of impact events
natural_disaster_fire_spaced_impact_events_effect = {
	natural_disaster_impact_effect = yes

	# Event for travellers through the affected region
	every_situation_sub_region = {
		every_situation_sub_region_county = {
			every_county_province = {
				every_character_in_location = {
					trigger_event = natural_disaster_flavor_events.0005
				}
			}
		}
	}

	# Later random impact/death events
	trigger_event = {
		id = natural_disaster.9010 # Random
		days = 7
	}
	trigger_event = {
		id = natural_disaster.9010 # Random
		days = 14
	}
	trigger_event = {
		id = natural_disaster.9010 # Random
		days = 21
	}
}

try_remove_disaster_vars_effect = {
	if = {
		limit = {
			NOT = {
				any_character_situation = {
					tgp_natural_disaster_situation_is_disaster_trigger = yes
				}
			}
		}
		if =  {
			limit = { has_diarchy_type = temporary_regency }
			end_diarchy = yes
		}
		remove_variable = natural_disaster_received_first_warning
		remove_variable = natural_disaster_5102_count
		if = {
			limit = { has_variable = natural_disaster_contribution_discount }
			remove_variable = natural_disaster_contribution_discount
		}
	}
}

natural_disaster_death_damage_roll_effect = {
	save_scope_as = situation
	every_situation_county = {
		every_county_province = {
			every_character_in_location = { add_to_list = killable_characters }
			every_army_in_location = { add_to_list = damageable_armies }
		}
	}
	# Risk of characters to die or their armies to get damaged
	every_in_list = {
		list = killable_characters
		if = {
			limit = { is_pool_character = yes }
			add_to_list = killed_characters				
			commanding_army ?= { add_to_list = damaged_armies }
			knight_army ?= { add_to_list = damaged_armies }
		}
		else = {
			random = {
				chance = natural_disaster_base_death_chance_value
				add_to_list = killed_characters
				commanding_army ?= { add_to_list = damaged_armies }
				knight_army ?= { add_to_list = damaged_armies }
			}
		}
	}
	# Risk of armies to get damaged if they weren't already from their leader commander/knight dying
	every_in_list = {
		list = damageable_armies
		limit = {
			NOT = { is_in_list = damaged_armies }
		}
		random = { # 25% to 50% base risk of getting damaged depending on size, plus more from type and severity
			chance = 25
			modifier = {
				add = {
					value = army_size
					divide = 1000
					max = 25
				}
			}
			modifier = { # Earthquakes are more deadly
				scope:situation = { situation_type = natural_disaster_earthquake }
				factor = 1.5
			}
			modifier = { # Earthquake epicenters are more deadly
				scope:situation.var:epicenter_county ?= location.county
				factor = 1.5
			}
			add_to_list = damaged_armies
		}
	}

	# EVENTS
	# Inform players they may die.
	every_player = {
		limit = { is_in_list = killed_characters }
		add_to_list = informees
		trigger_event = natural_disaster.5101
	}

	# Otherwise inform players of any deaths.
	every_in_list = {
		list = killed_characters
		save_scope_as = killed_character
		every_player = {
			if = {
				limit = { 
					scope:killed_character = { is_travelling = yes }
					this = current_travel_plan.travel_plan_owner
				}
				save_scope_as = message_receiver_temp
			}
			else_if = {
				limit = { 
					scope:killed_character = { is_in_army = yes }
					this = knight_army.army_commander
				}
				save_scope_as = message_receiver_temp
			}
			else_if = {
				limit = { 
					scope:killed_character = { exists = involved_activity }
					involved_activity ?= scope:killed_character.involved_activity
				}
				save_scope_as = message_receiver_temp
			}
			else_if = {
				limit = { 
					scope:killed_character = { liege_or_court_owner = prev }
				}
				save_scope_as = message_receiver_temp
			}
			else_if = {
				limit = {
					OR = {
						is_of_major_interest_trigger = { CHARACTER = scope:killed_character }
						is_of_minor_interest_trigger = { CHARACTER = scope:killed_character }
					}
				}
				save_scope_as = message_receiver_temp
			}
		}
		scope:message_receiver_temp ?= {
		    if = {
		    	limit = {
		    		any_character_situation = { this = scope:situation } # we literally do not care if we aren't subject to the disaster
					NOT = { is_in_list = informees }
				}
				add_to_list = informees

				if = {
					limit = {
						any_in_list = {
							list = killed_characters
							location = prev.location
						}
					}
					save_scope_as = local_death
				}

				trigger_event = {
					id = natural_disaster.5102
				}
			}
		}
	}

	# Inform players their army is damaged.
	every_in_list = {
		list = damaged_armies
		army_commander ?= {
			if = {
				limit = {
					is_ai = no
					NOT = { is_in_list = informees }
				}
				add_to_list = informees
				trigger_event = natural_disaster.5111
			}
		}
	}
	# Otherwise inform players of any damaged armies.
	every_in_list = {
		list = damaged_armies
		limit = {
			NOR = {
				army_commander = { is_in_list = informees }
				save_temporary_scope_as = army_temp
				any_in_list = {
					list = killed_characters
					knight_army ?= scope:army_temp
				}
			}
		}
		save_scope_as = army
		army_owner = {
			if = {
				limit = {
					NOT = { this = scope:army.army_commander }
				}
				send_interface_message = {
					type = msg_natural_disaster_army_damaged_bad
					title = natural_disaster_army_damaged_title
					right_icon = scope:army.army_commander
					show_as_tooltip = {
						scope:army = { natural_disaster_army_damage_effect = yes }
						every_in_list = {
							list = killed_characters
							limit = { knight_army ?= scope:army }
							save_scope_as = victim_temp
							natural_disaster_death_effect = { VICTIM = scope:victim_temp }
						}
					}
				}
			}
			every_war_ally = {
				send_interface_message = {
					type = msg_natural_disaster_army_damaged_bad
					title = natural_disaster_allied_army_damaged_title
					right_icon = scope:army.army_commander
					show_as_tooltip = {
						scope:army = { natural_disaster_army_damage_effect = yes }
						every_in_list = {
							list = killed_characters
							limit = { knight_army ?= scope:army }
							save_scope_as = victim_temp
							natural_disaster_death_effect = { VICTIM = scope:victim_temp }
						}
					}
				}
			}
			every_war_enemy = {
				send_interface_message = {
					type = msg_natural_disaster_army_damaged_good
					title = natural_disaster_enemy_army_damaged_title
					right_icon = scope:army.army_commander
					show_as_tooltip = {
						scope:army = { natural_disaster_army_damage_effect = yes }
						every_in_list = {
							list = killed_characters
							limit = { knight_army ?= scope:army }
							save_scope_as = victim_temp
							natural_disaster_death_effect = { VICTIM = scope:victim_temp }
						}
					}
				}
			}
		}
	}
	# Actually kill people.
	every_in_list = {
		list = killed_characters
		limit = { 
			is_ai = yes # Players who die will do so in events
			is_alive = yes
		}
		save_scope_as = victim_temp
		natural_disaster_death_effect = { VICTIM = scope:victim_temp }
	}
	# Actually damage armies.
	every_in_list = {
		list = damaged_armies
		save_scope_as = army_temp
		natural_disaster_army_damage_effect = yes
	}
	debug_log_scopes = yes
}

natural_disaster_survival_memory_effect = {
	if = {
		limit = {
			any_memory = {
				memory_type = survived_disaster_memory
				memory_age_years < 1
			}
		}
		# Don't add duplicates
	}
	else = {
		create_character_memory = { type = survived_disaster_memory }
		ordered_memory = {
			order_by = memory_creation_date
			save_scope_as = memory
			scope:situation = {
				switch = {
					trigger = situation_type
					natural_disaster_flood = {
						scope:memory = {
							set_variable = { name = disaster value = flag:flood }
							set_variable = { name = river_region value = scope:situation.var:river_region }
						}
					}
					natural_disaster_earthquake = {
						scope:memory = {
							set_variable = { name = disaster value = flag:earthquake }
							set_variable = { name = epicenter_county value = scope:situation.situation_center_province.county }
						}
					}
				}
			}
			set_variable = { name = location value = memory_owner.location }
		}
	}
}

# Effect to apply the correct death reason to a killed character
natural_disaster_death_effect = {
	scope:situation = {
		change_variable = {
			name = fatalities
			add = 1
		}
		switch = {
			trigger = situation_type
			natural_disaster_earthquake = {
				$VICTIM$ = {
					death = { death_reason = death_consumed_by_earthquake }
				}
			}
			natural_disaster_flood = {
				$VICTIM$ = {
					death = { death_reason = death_swept_away_by_flash_flood }
				}
			}
		}
	}
}

# Effect to damage an army
natural_disaster_army_damage_effect = {
	subtract_supply = natural_disaster_army_supply_loss_value
	deplete_army_by_percentage = natural_disaster_army_deplete_value
}

# Save base useful event scopes
natural_disaster_save_base_scopes_effect = {
	situation_center_province.county ?= { save_scope_as = epicenter_county }
	var:river_region ?= { save_scope_as = river_region }
	scope:situation_sub_region ?= { save_scope_as = situation_sub_region }
}

natural_disaster_save_disaster_province_effect = {
	save_scope_as = disaster_province_ruler
	scope:situation = {
		every_situation_sub_region = {
			every_situation_sub_region_county = {
				limit = { holder.top_liege ?= scope:disaster_province_ruler }
				add_to_list = disaster_counties
			}
		}
		random_in_list = {
			list = disaster_counties
			limit = { situation_center_province.county ?= this }
			alternative_limit = { holder ?= scope:disaster_province_ruler }
			alternative_limit = { holder.liege ?= scope:disaster_province_ruler }
			alternative_limit = { always = yes }
			title_province = { save_scope_as = disaster_province }
		}
	}
}

natural_disaster_impact_effect = {
	scope:situation = {
		switch = {
			trigger = situation_type
			natural_disaster_earthquake = {
				every_situation_county = {
					custom = every_disaster_county_custom
					change_county_control = natural_disaster_earthquake_control_loss_value
					if = {
						limit = { scope:situation.var:severity >= 2.5 }
						add_county_modifier = {
							modifier = tgp_natural_disaster_earthquake_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_earthquake_2_county_modifier
							duration_desc = until_the_disaster_is_over
							years = 25
						}
					}
					else = {
						add_county_modifier = {
							modifier = tgp_natural_disaster_earthquake_1_county_modifier
							duration_desc = until_the_disaster_is_over
							years = 25
						}
					}
				}
			}
			natural_disaster_flood = {
				every_situation_county = {
					custom = every_disaster_county_custom
					change_county_control = natural_disaster_flood_control_loss_value
					if = {
						limit = { scope:situation.var:severity >= 2.5 }
						add_county_modifier = {
							modifier = tgp_natural_disaster_flood_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_flood_2_county_modifier
							duration_desc = until_the_disaster_is_over
							years = 25
						}
					}
					else = {
						add_county_modifier = {
							modifier = tgp_natural_disaster_flood_1_county_modifier
							duration_desc = until_the_disaster_is_over
							years = 25
						}
					}
				}
			}
		}
	}
}

natural_disaster_great_project_mandatory_investment_reward_effect = {
	if = {
		limit = { this = scope:owner }
		if = {
			limit = { is_valid_for_legitimacy_change = yes }
			add_legitimacy = natural_disaster_great_project_mandatory_investment_legitimacy_gain_value
		}
		else = {
			add_prestige = minor_prestige_gain
			add_piety = minor_piety_gain
		}
	}
	else_if = {
		limit = { is_governor = yes }
		change_influence = minor_influence_gain
		increase_governance_effect = { VALUE = 2 }
	}
	else = {
		add_prestige = medium_prestige_gain
		add_piety = medium_prestige_gain
	}
	if = {
		limit = {
			government_allows = merit
			this != title:h_china.holder # Chinese Emperor does not "use" merit
		}
		change_merit = {
			value = medium_merit_gain
		}
	}
}

natural_disaster_assign_councillor_to_manage_effect = {
	if = {
		limit = { government_has_flag = government_is_nomadic }
		random_councillor = {
			limit = { is_kurultai_trigger = yes }
			save_scope_as = councillor
		}
	}
	else_if = {
		limit = { government_has_flag = government_is_landless_adventurer }
		random_entourage_character = {
			save_scope_as = councillor
		}
	}
	else_if = {
		limit = {
			government_has_flag = government_is_celestial
			tgp_has_access_to_ministry_trigger = yes
		}
		cp:minister_works = { save_scope_as = councillor }
	}
	else = {
		cp:councillor_steward = { save_scope_as = councillor }
	}
	$GREAT_PROJECT$ = {
		set_variable = {
			name = managing_councillor
			value = scope:councillor
		}
	}
}

natural_disaster_great_project_optional_investment_reward_effect = {
	if = {
		limit = { this = scope:owner }
		if = {
			limit = { is_valid_for_legitimacy_change = yes }
			add_legitimacy = natural_disaster_great_project_optional_investment_legitimacy_gain_value
		}
		else = {
			add_prestige = medium_prestige_gain
			add_piety = medium_piety_gain
		}
	}
	else_if = {
		limit = { is_governor = yes }
		change_influence = medium_influence_gain
		increase_governance_effect = { VALUE = 5 }
	}
	else = {
		add_prestige = major_prestige_gain
		add_piety = major_prestige_gain
	}
	if = {
		limit = {
			government_allows = merit
			this != title:h_china.holder # Chinese Emperor does not "use" merit
		}
		change_merit = {
			value = major_merit_gain
		}
	}
}

# OPERATION: 'add' or 'remove' modifier
# MODIFIER: which modifier to add/remove
# YEARS: duration in years, or negative value for permanent
natural_disaster_all_counties_investment_modifier_effect = {
	scope:situation ?= {
		every_situation_county = {
			limit = { holder.top_liege = scope:owner }
			custom = every_realm_disaster_county_great_project_custom
			if = { 
				limit = { exists = $YEARS$ }
				$OPERATION$_county_modifier = {
					modifier = $MODIFIER$
					years = $YEARS$
				}
			}
			else = {
				$OPERATION$_county_modifier = {
					modifier = $MODIFIER$
				}
			}
		}
	}
}

natural_disaster_great_project_save_scopes_effect = {
	scope:great_project.var:situation ?= {
		save_scope_as = situation
		situation_sub_region:disaster_area = { save_scope_as = situation_sub_region }
	}
}
