﻿#grab_epidemic_disease_effect
#remove_isolated_capital_modifiers_effect

#grabs your most important epidemic disease and saves it as a scope
#refer to it in loc as [disease_scope.GetNameNoTooltip( GetPlayer )|l]
grab_epidemic_disease_effect = {
	if = {
		limit = { has_trait = typhus }
		trait:typhus = { save_scope_as = disease_scope }
	}
	else_if = {
		limit = { has_trait = measles }
		trait:measles = { save_scope_as = disease_scope }
	}
	else_if = {
		limit = { has_trait = bubonic_plague }
		trait:bubonic_plague = { save_scope_as = disease_scope }
	}
	else_if = {
		limit = { has_trait = dysentery }
		trait:dysentery = { save_scope_as = disease_scope }
	}
	else_if = {
		limit = { has_trait = consumption }
		trait:consumption = { save_scope_as = disease_scope }
	}
	else_if = {
		limit = { has_trait = smallpox }
		trait:smallpox = { save_scope_as = disease_scope }
	}
	else = { trait:ergotism = { save_scope_as = disease_scope } }
}

# Sends a notification event to the relevant people when a province is infected
notify_holders_and_above = {
	save_scope_as = infected_county
	if = {
		limit = {
			# Just so we don't fire multiple alerts for the Black Death
			scope:infected_county = {
				NOT = {
					any_county_province_epidemic = {
						intensity = apocalyptic
						epidemic_type = epidemic_type:bubonic_plague
					}
				}
			}
		}
		if = {
			limit = {
				scope:infected_county = {
					any_county_province_epidemic = {
						intensity = apocalyptic
						NOT = { epidemic_type = epidemic_type:bubonic_plague }
					}
				}
			}
			holder.top_liege = {
				trigger_event = epidemic_events.1101
				every_vassal_or_below = { #Toast notif
					limit = { 
						is_ai = no
						NOT = {
							is_target_in_variable_list = {
								name = plagues_notified
								target = scope:epidemic
							}
						}
					}
					add_to_variable_list = {
						name = plagues_notified
						target = scope:epidemic
					}
					send_interface_toast = {
						type = msg_epidemic_outbreak
						title = epidemic_events.alert_title
						left_icon = scope:infected_county
						custom_tooltip = epidemic_events.alert_description
					}
				}
			}
			holder = {
				if = {
					limit = { 
						is_ai = no
						notified_of_plague_trigger = no
					}
					trigger_event = epidemic_events.1101
				}
				every_liege_or_above = {
					limit = { 
						is_ai = no
						notified_of_plague_trigger = no
					}
					trigger_event = epidemic_events.1101
				}
				every_vassal_or_below = {
					limit = { 
						is_ai = no
						notified_of_plague_trigger = no
					}
					trigger_event = epidemic_events.1101
				}
			}
		}
		else_if = {
			# If it's minor we send you a toast unless it's in your domain
			limit = {
				scope:infected_county = {
					any_county_province_epidemic = {
						NOR = {
							epidemic_type = epidemic_type:bubonic_plague
							outbreak_intensity >= major
						}
					}
					holder = {
						this != top_liege
					}
				}
			}
			if = {
				limit = {
					holder.top_liege = { 
						notified_of_plague_trigger = no
					}
				}
				holder.top_liege = {
					send_interface_toast = {
						type = msg_epidemic_outbreak
						title = epidemic_events.alert_title
						left_icon = scope:infected_county
						custom_tooltip = epidemic_events.alert_description
					}
					add_to_variable_list = {
						name = plagues_notified
						target = scope:epidemic
					}
					every_vassal_or_below = { #Toast notif
						limit = {
							is_ai = no
							this != scope:infected_county.holder
							notified_of_plague_trigger = no
						}
						add_to_variable_list = {
							name = plagues_notified
							target = scope:epidemic
						}
						send_interface_toast = {
							type = msg_epidemic_outbreak
							title = epidemic_events.alert_title
							left_icon = scope:infected_county
							custom_tooltip = epidemic_events.alert_description
						}
					}
				}
			}
			if = {
				limit = {
					holder = {
						is_ai = no
						notified_of_plague_trigger = no
					}
				}
				holder = {
					trigger_event = epidemic_events.1100
					every_liege_or_above = {
						limit = { 
							is_ai = no
							notified_of_plague_trigger = no
						}
						send_interface_toast = {
							type = msg_epidemic_outbreak
							title = epidemic_events.alert_title
							left_icon = scope:infected_county
							custom_tooltip = epidemic_events.alert_description
						}
					}
					every_vassal_or_below = {
						limit = { 
							is_ai = no
							notified_of_plague_trigger = no
						}
						send_interface_toast = {
							type = msg_epidemic_outbreak
							title = epidemic_events.alert_title
							left_icon = scope:infected_county
							custom_tooltip = epidemic_events.alert_description
						}
					}
				}
			}
		}
		else = { # If Major or Apocalyptic but not the Black Death
			holder.top_liege = {
				trigger_event = epidemic_events.1100
				every_vassal_or_below = { #Toast notif
					limit = {
						is_ai = no
						notified_of_plague_trigger = no
					}
					add_to_variable_list = {
						name = plagues_notified
						target = scope:epidemic
					}
					send_interface_toast = {
						type = msg_epidemic_outbreak
						title = epidemic_events.alert_title
						left_icon = scope:infected_county
						custom_tooltip = epidemic_events.alert_description
					}
				}
			}
			holder = {
				if = {
					limit = { 
						is_ai = no
						notified_of_plague_trigger = no
					}
					trigger_event = epidemic_events.1100
				}
				every_liege_or_above = {
					limit = { 
						is_ai = no
						NOT = {
							is_target_in_variable_list = {
								name = plagues_notified
								target = scope:epidemic
							}
						}
					}
					trigger_event = epidemic_events.1100
				}
				every_vassal_or_below = {
					limit = { 
						is_ai = no
						notified_of_plague_trigger = no
					}
					trigger_event = epidemic_events.1100
				}
			}
		}
	}
}
# Start Black Death event chain
start_black_death_chain_effect = {
	holder ?= {
		save_scope_as = black_death_holder
	}
	every_ruler = {
		if = {
			limit = { 
				is_ai = no
				in_diplomatic_range = scope:black_death_holder
				NOT = {
					any_owned_story = {
						type = story_cycle_black_death
					}
				}
			}
			trigger_event = epidemic_events.5999
		}
		every_liege_or_above = {
			limit = { 
				is_ai = no 
				in_diplomatic_range = scope:black_death_holder
				NOT = {
					any_owned_story = {
						type = story_cycle_black_death
					}
				}
			}
			trigger_event = epidemic_events.5999
		}
	}
}

# Grabs a courtier who would plausibly be superstitious
get_superstitious_courtier_effect = {
	if = {
		limit = {
			any_courtier_or_guest = {
				is_available_adult = yes
				OR = {
					learning <= mediocre_skill_rating
					has_trait = intellect_bad
					has_trait = dull
					has_character_modifier = bp1_superstition_modifier
				}
			}
		}
		random_courtier_or_guest = {
			limit = {
				is_available_adult = yes
				OR = {
					learning <= mediocre_skill_rating
					has_trait = intellect_bad
					has_trait = dull
					has_character_modifier = bp1_superstition_modifier
				}
			}
			save_scope_as = superstitious_courtier
		}
	}
	else = {
		random_courtier_or_guest = {
			limit = {
				is_available_adult = yes
			}
			save_scope_as = superstitious_courtier
		}
	}
}

get_random_realm_epidemic = {
	random_sub_realm_county = {
		limit = {
			any_county_province_epidemic = { outbreak_intensity >= $INTENSITY$ }
		}
		weight = {
			base = 1
			modifier = {
				add = 750
				has_province_with_epidemic = { intensity >= apocalyptic }
			}
			modifier = {
				add = 5
				has_province_with_epidemic = { intensity >= major }
			}
		}
		random_county_province = {
			limit = {
				any_province_epidemic = {
					outbreak_intensity >= $INTENSITY$
				}
			}
			weight = {
				base = 1
				modifier = {
					add = 750
					any_province_epidemic = {
						intensity >= apocalyptic
					}
				}
				modifier = {
					add = 5
					any_province_epidemic = {
						intensity >= major
					}
				}
			}
			random_province_epidemic = {
				limit = {
					outbreak_intensity >= $INTENSITY$
				}
				weight = {
					base = 1
					modifier = {
						add = 750
						outbreak_intensity >= apocalyptic
					}
					modifier = {
						add = 5
						outbreak_intensity >= major
					}
				}
				save_scope_as = epidemic_scope
			}
		}
	}
}

get_random_nearby_realm_epidemic = {
	random_sub_realm_county = {
		limit = {
			any_county_province = {
				OR = {
					county.holder = root
					squared_distance = {
						target = root.capital_province
						value <= squared_distance_medium #250 map-pixels. Roughly one Ireland away (top to bottom).
					}
				}
				any_province_epidemic = {
					outbreak_intensity >= $INTENSITY$
				}
			}
		}
		weight = {
			base = 1
			modifier = {
				add = 750
				has_province_with_epidemic = { intensity >= apocalyptic }
			}
			modifier = {
				add = 5
				has_province_with_epidemic = { intensity >= major }
			}
		}
		random_county_province = {
			limit = {
				any_province_epidemic = {
					outbreak_intensity >= $INTENSITY$
				}
			}
			weight = {
				base = 1
				modifier = {
					add = 750
					any_province_epidemic = {
						intensity >= apocalyptic
					}
				}
				modifier = {
					add = 5
					any_province_epidemic = {
						intensity >= major
					}
				}
			}
			random_province_epidemic = {
				limit = {
					outbreak_intensity >= $INTENSITY$
				}
				weight = {
					base = 1
					modifier = {
						add = 750
						outbreak_intensity >= apocalyptic
					}
					modifier = {
						add = 5
						outbreak_intensity >= major
					}
				}
				save_scope_as = epidemic_scope
			}
		}
	}
}

# Epidemic treatment effect
epidemic_prevention_effect = {
	$PHYSICIAN$ = { save_scope_as = physician }
	# Apply prevention
	if = {
		limit = {
			$PREVENTION_LEVEL$ = 1
		}
		custom_tooltip = minor_epidemic_treatment.tt
		hidden_effect = {
			# OUTCOME RANDOMIZATION
			random_list = {
				70 = { # Success
					modifier = {
						factor = scope:physician.physician_safe_treatment_skill_factor
						# 1 for an average learning skill character without traits
						# 0.2 min for low learning
						# ~3 for high skill character without traits
						# ~12 for high skill character with best trait (physician_3)
					}
					trigger_event = { id = physician_epidemic_events.1021 days = { 3 14 } } #Safe prevention sucess
				}
				30 = { # Failure
					trigger_event = { id = physician_epidemic_events.1022 days = { 3 14 } } #Safe prevention failure
				}
			}
		}
	}
	if = {
		limit = {
			$PREVENTION_LEVEL$ = 2
		}
		custom_tooltip = moderate_epidemic_treatment.tt
		# OUTCOME RANDOMIZATION
		hidden_effect = {
			random_list = {
				1 = { #Success
					modifier = {
						scope:physician = {
							learning >= mediocre_skill_rating
							learning < medium_skill_rating
						}
						factor = 2
					}
					modifier = {
						scope:physician = {
							learning >= medium_skill_rating
							learning < decent_skill_rating
						}
						factor = 4
					}
					modifier = {
						scope:physician = {
							learning >= decent_skill_rating
							learning < high_skill_rating
						}
						factor = 7
					}
					modifier = {
						scope:physician = {
							learning >= high_skill_rating
						}
						factor = 10
					}
					modifier = {
						scope:physician = { has_trait = lifestyle_physician }
						factor = 2
					}
					modifier = {
						scope:physician = {
							has_trait = lifestyle_physician
							has_trait_xp = {
								trait = lifestyle_physician
								value >= 50
							}
						}
						factor = 5
					}
					modifier = {
						scope:physician = {
							has_trait = lifestyle_physician
							has_trait_xp = {
								trait = lifestyle_physician
								value >= 100
							}
						}
						factor = 5
					}
					trigger_event = { id = physician_epidemic_events.1023 days = { 3 14 } } #success
				}
				10 = { #Failure
					trigger_event = { id = physician_epidemic_events.1024 days = { 3 14 } } #Failure
				}
			}
		}
	}
	if = {
		limit = {
			$PREVENTION_LEVEL$ = 3
		}
		custom_tooltip = mystic_epidemic_treatment.tt
		# OUTCOME RANDOMIZATION
		hidden_effect = {
			random_list = {
				1 = { #Success
					modifier = {
						scope:physician = {
							learning >= mediocre_skill_rating
							learning < medium_skill_rating
						}
						factor = 2
					}
					modifier = {
						scope:physician = {
							learning >= medium_skill_rating
							learning < decent_skill_rating
						}
						factor = 4
					}
					modifier = {
						scope:physician = {
							learning >= decent_skill_rating
							learning < high_skill_rating
						}
						factor = 7
					}
					modifier = {
						scope:physician = {
							learning >= high_skill_rating
						}
						factor = 10
					}
					modifier = {
						scope:physician = { has_trait = lifestyle_mystic }
						factor = 2
					}
					modifier = {
						scope:physician = {
							has_trait = lifestyle_mystic
							has_trait_xp = {
								trait = lifestyle_mystic
								value >= 50
							}
						}
						factor = 5
					}
					modifier = {
						scope:physician = {
							has_trait = lifestyle_mystic
							has_trait_xp = {
								trait = lifestyle_mystic
								value >= 50
							}
						}
						factor = 5
					}

					#Critical or normal success?
					random_list = {
						1 = {
							modifier = {
								scope:physician = { has_trait = lifestyle_physician }
								factor = 1.5
							}
							modifier = {
								scope:physician = {
									has_trait = lifestyle_physician
									has_trait_xp = {
										trait = lifestyle_physician
										value >= 50
									}
								}
								factor = 2
							}
							modifier = {
								scope:physician = {
									has_trait = lifestyle_physician
									has_trait_xp = {
										trait = lifestyle_physician
										value >= 50
									}
								}
								factor = 3
							}
							trigger_event = { id = physician_epidemic_events.1025 days = { 3 14 } } #Critical success
						}
						5 = {
							trigger_event = { id = physician_epidemic_events.1023 days = { 3 14 } } #Normal success
						}
					}
				}
				10 = { #Failure
					#Critical or normal failure?
					random_list = {
						1 = {
							trigger_event = { id = physician_epidemic_events.1026 days = { 3 14 } } #Critical failure
						}
						5 = {
							trigger_event = { id = physician_epidemic_events.1024 days = { 3 14 } } #Failure
						}
					}
				}
			}
		}
	}
}

remove_isolated_capital_modifiers_effect = {
	if = {
		limit = {
			has_county_modifier = isolate_capital_decision_modifier
			NOT = {
				any_county_province = {
					this != root
					any_province_epidemic = {
						this = scope:epidemic
					}
				}
			}
		}
		remove_county_modifier = isolate_capital_decision_modifier
	}
	if = {
		limit = {
			holder.top_liege = {
				any_sub_realm_county = { has_county_modifier = isolate_capital_decision_modifier }
			}
			holder.top_liege = {
				NOT = {
					any_sub_realm_county = {
						any_county_province = {
							this != root
							any_province_epidemic = {
								this = scope:epidemic
							}
						}
					}
				}
			}
		}
		holder.top_liege = {
			every_sub_realm_county = {
				limit = {
					has_county_modifier = isolate_capital_decision_modifier
				}
				remove_county_modifier = isolate_capital_decision_modifier
			}
		}
	}
}

give_epidemic_immunity = {
	if = {
		limit = {
			$EPIDEMIC$.epidemic_type.epidemic_trait = trait:measles
		}
		add_character_flag = measles_immunity
	}
	if = {
		limit = {
			$EPIDEMIC$.epidemic_type.epidemic_trait = trait:bubonic_plague
		}
		add_character_flag = bubonic_immunity
	}
	add_to_variable_list = {
		name = immunised_plagues
		target = $EPIDEMIC$
	}
}

add_plague_county_modifiers = {
	if = {
		limit = {
			any_sub_realm_county = {
				development_level >= 30
			}
		}
		show_as_tooltip = { # Show as tooltip to reduce tooltip size
			every_sub_realm_county = {
				custom = shattered_by_plague_tt
				add_county_modifier = {
					modifier = shattered_by_plague
					years = 10
				}
			}
		}
	}
	if = {
		limit = {
			any_sub_realm_county = {
				AND = {
					development_level >= 20
					development_level < 30
				}
			}
		}
		show_as_tooltip = { # Show as tooltip to reduce tooltip size
			every_sub_realm_county = {
				custom = ravaged_by_plague_tt
				add_county_modifier = {
					modifier = ravaged_by_plague
					years = 10
				}
			}
		}
	}
	if = {
		limit = {
			any_sub_realm_county = {
				AND = {
					development_level >= 15
					development_level < 20
				}
			}
		}
		show_as_tooltip = { # Show as tooltip to reduce tooltip size
			every_sub_realm_county = {
				custom = blighted_by_plague_tt
				add_county_modifier = {
					modifier = blighted_by_plague
					years = 10
				}
			}
		}
	}
	if = {
		limit = {
			any_sub_realm_county = {
				AND = {
					development_level >= 10
					development_level < 15
				}
			}
		}
		show_as_tooltip = { # Show as tooltip to reduce tooltip size
			every_sub_realm_county = {
				custom = struck_by_plague_tt
				add_county_modifier = {
					modifier = struck_by_plague
					years = 10
				}
			}
		}
	}
	hidden_effect = {
		every_sub_realm_county = {
			if = { # Refresh the modifier for new plagues
				limit = {
					has_county_modifier = struck_by_plague
				}
				remove_county_modifier = struck_by_plague
			}
			else_if = { 
				limit = {
					has_county_modifier = blighted_by_plague
				}
				remove_county_modifier = blighted_by_plague
			}
			else_if = { 
				limit = {
					has_county_modifier = ravaged_by_plague
				}
				remove_county_modifier = ravaged_by_plague
			}
			else_if = {
				limit = {
					has_county_modifier = shattered_by_plague
				}
				remove_county_modifier = shattered_by_plague
			}
		}
		every_sub_realm_county = {
			limit = {
				development_level >= 30
			}
			add_county_modifier = {
				modifier = shattered_by_plague
				years = 10
			}
		}
		every_sub_realm_county = {
			limit = {
				AND = {
					development_level >= 20
					development_level < 30
				}
			}
			add_county_modifier = {
				modifier = ravaged_by_plague
				years = 10
			}
		}
		every_sub_realm_county = {
			limit = {
				AND = {
					development_level >= 15
					development_level < 20
				}
			}
			add_county_modifier = {
				modifier = blighted_by_plague
				years = 10
			}
		}
		every_sub_realm_county = {
			limit = {
				AND = {
					development_level >= 10
					development_level < 15
				}
			}
			add_county_modifier = {
				modifier = struck_by_plague
				years = 10
			}
		}
	}
}

plague_recovery_event_effect = {
	scope:epidemic = { save_scope_as = epidemic }
	county.holder.liege = {
		add_to_variable_list = {
			name = formerly_infected_counties
			target = root.county
			years = 10
		}
		if = {
			limit = {
				any_sub_realm_county = {
					any_county_province = {
						count = 1
						any_province_epidemic = {
							count = 1
						}
					}
				}
			}
			trigger_event = {
				id = epidemic_events.0110
				days = 1
			}
		}
	}
}

cant_touch_this_achievement_effect = {
	if = {
		limit = {
			county.holder = {
				OR = {
					this = { is_ai = no }
					any_liege_or_above = { is_ai = no }
				}
			}
			epidemic_resistance >= 100
		}
		county.holder = {
			add_achievement_global_variable_effect = {
				VARIABLE = ce1_cant_touch_this_achievement_unlocked
				VALUE = yes
			}
		}
	}
}

# this = county to apply to
# scope:epidemic = epidemic infecting
# BASE = how much to apply by default for this epidemic type
apply_infection_development_loss = {
	save_temporary_scope_value_as = {
		name = base_loss
		value = $BASE$
	}
	change_development_progress_with_overflow = epidemic_development_loss_value
}

apply_infection_fertility_loss = {
	save_temporary_scope_value_as = {
		name = base_loss
		value = $BASE$
	}
	change_county_fertility = epidemic_fertility_loss_value
}
