﻿#############################
# STANDARD ILLNESS TRIGGERS #
#############################

has_any_illness = { #Checks the game for every disease
	has_trait_with_flag = illness
}

has_any_debilitating_illness = { #Check for any illness that would warrant the sick animation
	has_trait_with_flag = debilitating_illness
}

is_debilitating_illness_trait = { #Check for any illness that would warrant the sick animation
	has_trait_flag = debilitating_illness
}

can_get_non_epidemic_disease_trigger = { #Diseases shouldn't stack. Don't add a non-epidemic disease if you have another, unless that disease is lover's pox
	NOT = { has_trait_with_flag = blocks_getting_non_epidemic_disease }
}

##############################################
# SITUATIONAL EVENT CONTENT ILLNESS TRIGGERS #
##############################################

# Will this character suffer age-related health issues, factoring how healthy they actually are vs. what their age is?
age_ranked_health_vulnerability_threshold_trigger = {
	OR = {
		# If you're already having problems...
		has_trait = infirm
		# Otherwise, we check health crossed against age.
		## Characters that are this old are vulnerable regardless of health.
		age >= 80
		## Else, the higher your health is, the older you need to be to suffer age-related health problems.
		AND = {
			health <= good_health
			age >= 70
		}
		AND = {
			health <= medium_health
			age >= 60
		}
		AND = {
			health <= fine_health
			age >= 50
		}
		AND = {
			health <= poor_health
			age >= 40
		}
		AND = {
			health <= dying_health
			age >= 30
		}
	}
	NOT = { has_trait = immortal }
}

# Might this character succumb to a stroke, aneurysm, or other blood pressure-related injury?
potential_victim_of_apoplexy_trigger = {
	OR = {
		has_trait = wrathful
		# Impatient/irritable characters tend towards this when pushed out of their comfort zone.
		AND = {
			OR = {
				has_trait = impatient
				has_trait = irritable
			}
			stress >= low_stress
		}
		# Non-calm characters may be at risk if stressed.
		AND = {
			stress_level >= 1
			NOT = { has_trait = calm }
		}
		# But everyone is vulnerable eventually.
		stress_level >= 2
	}
}

# Does the character have a trait that might shorten their life exptectancy, exacerbate unfortunate physical circumstances, or otherwise complicate medical conditions?
has_weak_physical_health_traits_trigger = {
	# We discount these if you're in truly rude health.
	health < excellent_health
	OR = {
		# General expected reduction in physical health.
		has_trait = physique_bad
		has_trait = weak
		# Induces & exacerbates secondary physical complications.
		has_trait = hunchbacked
		has_trait = giant
		has_trait = inbred
		# Marfan's Syndrome can be medicated well in the modern day, but not in our period.
		has_trait = spindly
		# Haemophilia is both easier to medicate and easier to treat the consequences of in the modern day, but could really mess you up in the medieval period.
		has_trait = bleeder
	}
	# Deliberately excluded:
	## dwarf
	### Unlike gigantism, dwarfism doesn't generally reduce life expectancy or induce complications except in that it can be more likely to occur if you have other conditions. For our purposes, those separate conditions are separate traits.
	## scaly
	### Ichthyosis does not reduce life expectancy & has a minimal effect on most (though not all) secondary conditions.
	## albino
	### Zero effect on life exptectancy.
	## wheezing
	### Asthma does reduce your life exptectancy, but not to the same degree as the other bad physical traits.
}

# Does this character have some physical feature that would be easily mocked?
## Remember to generally try and filter out compassionate characters from bullying people over their appearance & to take caution when using this in your narrative.
has_easily_mocked_physical_attribute_trigger = {
	OR = {
		# Weight.
		current_weight > overweight_value
		current_weight < underweight_value
		# Genetic traits.
		has_trait = beauty_bad
		has_trait = clubfooted
		has_trait = hunchbacked
		has_trait = dwarf
		has_trait = giant
		has_trait = inbred
		has_trait = spindly
		has_trait = scaly
		has_trait = albino
		# Illness traits.
		## Generally stuff with obvious, preferably permanent, outward physical symptoms.
		has_trait = great_pox
		has_trait = lovers_pox
		has_trait = leper
		has_trait = gout_ridden
		# Physical traits.
		has_trait = wounded
		has_trait = maimed
		has_trait = one_eyed
		has_trait = one_legged
		has_trait = disfigured
		has_trait = blind
		is_eunuch_trigger = yes
	}
}

############################
# VARIOUS DISEASE TRIGGERS #
############################

#Takes argument disease
can_contract_disease_trigger = {
	save_temporary_scope_as = sick_character_check

	# Is not immune to disease
	NOT = { has_character_flag = immune_to_disease }

	#Doesn't already have the trait
	NOT = { has_trait = $DISEASE$ }

	#Only get measles once
	trigger_if = {
		limit = { trait:$DISEASE$ = trait:measles }
		NOT = { has_character_flag = measles_immunity }
	}

	#Only get bubonic plague once
	trigger_if = {
		limit = { trait:$DISEASE$ = trait:bubonic_plague }
		NOT = { has_character_flag = bubonic_immunity }
	}

	#Young characters do not get gout
	trigger_if = {
		limit = { trait:$DISEASE$ = trait:gout_ridden }
		current_weight >= 50
		is_young_character = no
	}

	save_temporary_scope_value_as = {
		name = disease_type_for_trigger
		value = flag:$DISEASE$
	}
	
	trigger_if = { #Lover's pox can only be contracted if you are sexually active
		limit = {
			scope:disease_type_for_trigger = flag:lovers_pox
			is_imprisoned = yes
		}
		imprisoner = {
			OR = {
				AND = {
					scope:sick_character_check = { is_in_prison_type = dungeon }
					any_prisoner = {
						OR = {
							has_relation_lover = scope:sick_character_check
							any_consort = {
								this = scope:sick_character_check
							}
						}
					}
				}
				AND = {
					scope:sick_character_check = { is_in_prison_type = house_arrest }
					OR = {
						any_prisoner = {
							OR = {
								has_relation_lover = scope:sick_character_check
								any_consort = {
									this = scope:sick_character_check
								}
							}
						}
						any_courtier = {
							OR = {
								has_relation_lover = scope:sick_character_check
								any_consort = {
									this = scope:sick_character_check
								}
							}
						}
					}
				}
			}
		}
	}

	# Check to see that there is someone for you to have sex with if you are traveling
	trigger_if = {
		limit = {
			scope:disease_type_for_trigger = flag:lovers_pox
			is_travelling = yes
			# Do not block Goliardic Shenanigans!
			involved_activity ?= {
				NOT = { has_activity_type = activity_adult_education }
			}
		}
		current_travel_plan = {
			any_entourage_character = {
				OR = {
					has_relation_lover = scope:sick_character_check
					any_consort = {
						this = scope:sick_character_check
					}
				}
			}
		}
	}

	#Secondary requirements (mutually exclusive diseases or immunity)
	OR = {
		NOR = { #No secondary requirement
			scope:disease_type_for_trigger = flag:ill
			scope:disease_type_for_trigger = flag:smallpox
			scope:disease_type_for_trigger = flag:bubonic_plague
		}
		AND = {
			scope:disease_type_for_trigger = flag:ill
			NOT = { has_trait = pneumonic }
		}
		AND = {
			scope:disease_type_for_trigger = flag:smallpox
			NOT = { has_character_flag = disease_immunity_smallpox }
		}
		AND = {
			scope:disease_type_for_trigger = flag:measles
			NOT = { has_character_flag = disease_immunity_measles }
		}
		AND = {
			scope:disease_type_for_trigger = flag:bubonic_plague
			NOT = { has_character_flag = disease_immunity_bubonic_plague }
		}
	}

	#No disease stacking
	OR = {
		can_get_non_epidemic_disease_trigger = yes
		OR = {
			scope:disease_type_for_trigger = flag:bubonic_plague
			scope:disease_type_for_trigger = flag:smallpox
		}
		AND = {
			scope:disease_type_for_trigger = flag:pneumonic
			has_trait = ill
		}	
	}

	#Cooldown is over
	NOR = {
		has_character_flag = contraction_cooldown_general #Haven't had a disease for 2 years
		#No specific cooldowns:
		#Ill, you can get it often
		#Smallpox and bubonic plague, after one time you are immune
		#Lover's pox and leper because you never recover
		AND = {
			has_character_flag = contraction_cooldown_pneumonic
			scope:disease_type_for_trigger = flag:pneumonic
		}
		AND = {
			has_character_flag = contraction_cooldown_gout_ridden
			scope:disease_type_for_trigger = flag:gout_ridden
		}
		AND = {
			has_character_flag = contraction_cooldown_typhus
			scope:disease_type_for_trigger = flag:typhus
		}
		AND = {
			has_character_flag = contraction_cooldown_consumption
			scope:disease_type_for_trigger = flag:consumption
		}
		AND = {
			has_character_flag = contraction_cooldown_cancer
			scope:disease_type_for_trigger = flag:cancer
		}
		AND = {
			has_character_flag = contraction_cooldown_great_pox
			scope:disease_type_for_trigger = flag:great_pox
		}
		AND = {
			has_character_flag = contraction_cooldown_dysentery
			scope:disease_type_for_trigger = flag:dysentery
		}
		AND = {
			has_character_flag = contraction_cooldown_ergotism
			scope:disease_type_for_trigger = flag:ergotism
		}
	}
}

has_any_disease_trigger = {
	OR = {
		has_trait = ill
 		has_trait = lovers_pox
 		has_trait = great_pox
 		has_trait = early_great_pox
		has_trait = bubonic_plague
		has_trait = smallpox
		has_trait = measles
		has_trait = ergotism
		has_trait = dysentery
		has_trait = consumption
		has_trait = typhus
		has_trait = cancer
		has_trait = pneumonic
		has_trait = leper
		has_trait = gout_ridden
 	}
}

has_std_trigger = {
 	OR = {
 		has_trait = lovers_pox
 		has_trait = great_pox
 		has_trait = early_great_pox
 	}
}

has_epidemic_disease_trigger = {
	OR = {
		has_trait = bubonic_plague
		has_trait = smallpox
		has_trait = measles
		has_trait = ergotism
		has_trait = dysentery
		has_trait = consumption
		has_trait = typhus
	}
}

has_contagious_disease_trigger = {  #Only diseases handled by the disease_outbreak_pulse and the disease_recovery_pulse
	OR = {
		has_std_trigger = yes
		has_trait = bubonic_plague
		has_trait = smallpox
		has_trait = measles
		has_trait = dysentery
		has_trait = consumption
		has_trait = typhus
	}
}

has_yearly_recovery_disease_trigger = {
	OR = {
		has_trait = consumption
		has_trait = cancer
		has_trait = great_pox
		has_trait = gout_ridden
	}
}

has_quarterly_recovery_disease_trigger = {
	OR = {
		
		has_trait = ill
		has_trait = pneumonic
		has_trait = typhus
		has_trait = smallpox
		has_trait = bubonic_plague
	}
}

has_short_disease_trigger = {
	OR = {
		has_trait = ill
		has_trait = typhus
		has_trait = smallpox
		has_trait = bubonic_plague
		has_trait = ergotism
		has_trait = measles
		has_trait = dysentery
	}
}

has_serious_disease_trigger = {
	OR = {
		has_trait = bubonic_plague
		has_trait = great_pox
		has_trait = leper
		has_trait = typhus
		has_trait = smallpox
		has_trait = cancer
		has_trait = pneumonic
		has_trait = consumption
		has_trait = measles
		has_trait = dysentery
		has_trait = ergotism
	}
}
has_insect_carried_disease_trigger = {
	OR = {
		has_trait = bubonic_plague
		has_trait = typhus
	}
}

############################
# COURT PHYSICIAN TRIGGERS #
############################

court_physician_available_trigger = {
	exists = court_owner
	court_owner = {
		employs_court_position = court_physician_court_position
		any_court_position_holder = {
			type = court_physician_court_position
			is_physically_able = yes
		}
	}
	# If you're traveling, we check that there is a court physician with you
	court_physician_available_when_traveling_trigger = yes
}

court_physician_available_when_traveling_trigger = {
	trigger_if = {
		limit = { is_travelling = yes }
		current_travel_plan = { 
			any_entourage_character = {
				has_court_position = court_physician_court_position
			}
		}
	}
}

can_be_court_physician_of = {
	is_courtier_of = $EMPLOYER$
	is_adult = yes
	basic_is_available_ai = yes
	NOT = { has_trait = incapable }
}

can_punish_court_physician = {
	save_temporary_scope_as = court_physician_punisher
	$PHYSICIAN$ = {
		is_alive = yes
		target_is_liege_or_above = scope:court_physician_punisher
	}
}

character_can_easily_self_treat_trigger = {
	OR = {
		# Understand medicine implicitly.
		has_trait = lifestyle_physician
		has_trait = lifestyle_herbalist
		has_trait = devoted
		## Truly, obscenely high learning means your general knowledge is good enough for anything even if you aren't specifically an expert on it.
		learning >= 35
		# Understands medicine if learning is high enough.
		AND = {
			learning >= high_skill_rating
			OR = {
				has_trait = lifestyle_gardener
				has_trait = lifestyle_traveler
			}
		}
		# Might understand medicine if learning is really, really high.
		AND = {
			learning >= very_high_skill_rating
			OR = {
				has_trait = intrigue_court_2
				has_trait = scholarly_court_2
			}
		}
		AND = {
			learning >= extremely_high_skill_rating
			OR = {
				has_trait = intrigue_court_1
				has_trait = scholarly_court_1
			}
		}
	}
}

######################
# TREATMENT TRIGGERS #
######################

has_treatable_disease_trigger = {
	OR = {
		has_trait = consumption
		has_trait = cancer
		has_trait = great_pox
		has_trait = gout_ridden
		has_trait = ill
		has_trait = pneumonic
		has_trait = typhus
		has_trait = smallpox
		has_trait = bubonic_plague
		has_trait = leper
		has_trait = measles
		has_trait = dysentery
		has_trait = ergotism
	}
}

has_wounds_trigger = {
	OR = {
		has_trait_rank = {
			trait = wounded
			rank >= 2
		}
		has_character_modifier = infected_wound_modifier
		has_character_modifier = gangrene_modifier
	}
}

has_good_disease_treatment_trigger = {
	OR = {
		has_character_modifier = safe_disease_treatment_success_high_modifier
		has_character_modifier = safe_disease_treatment_success_low_modifier
		has_character_modifier = risky_disease_treatment_success_high_modifier
		has_character_modifier = risky_disease_treatment_success_low_modifier
	}
}

has_recent_treatment_trigger = {
	OR = {
		has_character_modifier = safe_disease_treatment_success_high_modifier
		has_character_modifier = safe_disease_treatment_success_low_modifier
		has_character_modifier = safe_disease_treatment_failure_modifier
		has_character_modifier = risky_disease_treatment_success_high_modifier
		has_character_modifier = risky_disease_treatment_success_low_modifier
		has_character_modifier = risky_disease_treatment_failure_modifier
	}
}

has_recent_wound_treatment_trigger = {
	OR = {
		has_character_modifier = safe_wound_treatment_success_high_modifier
		has_character_modifier = safe_wound_treatment_success_low_modifier
		has_character_modifier = safe_wound_treatment_failure_modifier
		has_character_modifier = risky_wound_treatment_success_modifier
		has_character_modifier = risky_wound_treatment_failure_modifier
	}
}


has_amputation_type_disease_trigger = {
	exists = scope:disease_type
	OR = {
		scope:disease_type = flag:gout_ridden
		scope:disease_type = flag:leper
		scope:disease_type = flag:cancer
	}
}

has_disfigurement_type_disease_trigger = {
	exists = scope:disease_type
	OR = {
		scope:disease_type = flag:bubonic_plague
		scope:disease_type = flag:smallpox
		scope:disease_type = flag:measles
		scope:disease_type = flag:dysentery
		scope:disease_type = flag:ergotism
		scope:disease_type = flag:leper
		scope:disease_type = flag:cancer
	}
}


has_short_disease_type_trigger = {
	OR = {
		scope:disease_type = flag:ill 				
		scope:disease_type = flag:pneumonic #3
		scope:disease_type = flag:measles #3
		scope:disease_type = flag:dysentery #3.5
		scope:disease_type = flag:ergotism #3.5
		scope:disease_type = flag:typhus #3.5
		scope:disease_type = flag:smallpox #3.5
		scope:disease_type = flag:bubonic_plague #6.5
	}
}

has_slow_disease_type_trigger = {
	OR = {
		scope:disease_type = flag:consumption #2
		scope:disease_type = flag:cancer #3
		scope:disease_type = flag:great_pox #1		
		scope:disease_type = flag:gout_ridden #1.5 	
		scope:disease_type = flag:leper #1.5	  						
	}
}

has_high_health_penalty_disease_type_trigger = { # >= 3.5
	OR = {
		### TYPHUS
		scope:disease_type = flag:typhus #4.5 + 1
		### SMALLPOX
		scope:disease_type = flag:smallpox #4.5 + 1.5
		### BUBONIC PLAGUE
		scope:disease_type = flag:bubonic_plague #7 + 2
		### DYSENTERY
		scope:disease_type = flag:dysentery #3.5 + 2
		### ERGOTISM
		scope:disease_type = flag:ergotism #3.5
		### CONSUMPTION
		AND = {
			scope:disease_type = flag:consumption #2 + 1 + 2
			is_old_character = yes
		}
		### CANCER
		AND = {
			scope:disease_type = flag:cancer #3 + 1
			is_old_character = yes
		}
		### MEASLES
		AND = {
			scope:disease_type = flag:measles #3 + 3
			is_old_character = yes
		}
		### PNEUMONIA
		AND = {
			scope:disease_type = flag:pneumonic #3 + 2
			is_old_character = yes
		}
	}
}

has_low_health_penalty_disease_type_trigger = { # < 3.5
	OR = {
		## ILL
		scope:disease_type = flag:ill #1 + 1
		### GREAT POX
		scope:disease_type = flag:great_pox #1
		### GOUT
		scope:disease_type = flag:gout_ridden #1
		### LEPROSY
		scope:disease_type = flag:leper #1.5
		### CONSUMPTION
		AND = {
			scope:disease_type = flag:consumption #2 + 1 + 2
			is_old_character = no
		}
		### CANCER
		AND = {
			scope:disease_type = flag:cancer #3 + 1
			is_old_character = no
		}
		### MEASLES
		AND = {
			scope:disease_type = flag:measles #3 + 3
			is_old_character = no
		}
		### PNEUMONIA
		AND = {
			scope:disease_type = flag:pneumonic #3 + 2
			is_old_character = no
		}
	}
}

minor_disease_flag_for_notification_trigger = {
	OR = {
		scope:disease_type = flag:ill
		scope:disease_type = flag:gout_ridden
		scope:disease_type = flag:lovers_pox
		scope:disease_type = flag:early_great_pox
	}
}

sick_enough_for_relative_to_care_trigger = {
	#For this list of diseases, you need to be dying
	trigger_if = {
		limit = {
			exists = scope:disease_type
			minor_disease_flag_for_notification_trigger = yes
		}
		health < death_chance_dying_health
	}
	#For all others we always notify
	trigger_else = {
		always = yes
	}
}

liege_is_responsible_for_treatment_trigger = {
	is_ruler = no
	exists = liege
	OR = {
		trigger_if = {
			limit = { exists = liege.primary_spouse }
			this = liege.primary_spouse
		}
		trigger_if = {
			limit = { exists = liege.player_heir }
			this = liege.player_heir
		}
	}
}

liege_picks_treatment_trigger = {
	save_temporary_scope_as = pick_treatment_check
	exists = liege
	liege_is_responsible_for_treatment_trigger = yes
	trigger_if = {
		limit = { exists = scope:disease_type }
		sick_enough_for_relative_to_care_trigger = yes
	}
}

root_picks_treatment_for_sick_character_trigger = {
	scope:sick_character = { liege_picks_treatment_trigger = yes }
	scope:sick_character = { liege = { this = root } }
}

has_infected_or_gangrenous_wound_trigger = {
	OR = {
		has_character_modifier = gangrene_modifier
		has_character_modifier = infected_wound_modifier
	}
}

inform_about_relative_recovery_trigger = {
	is_ruler = yes
	OR = {
		scope:disease_type = flag:lovers_pox #Because it has an effect (removes rejected from bed modifier)
		scope:disease_type = flag:early_great_pox #Because it has an effect (removes rejected from bed modifier)
		scope:disease_type = flag:great_pox #Because it has an effect (removes rejected from bed modifier)
		AND = {
			is_ai = no
			OR = {
				NOT = { minor_disease_flag_for_notification_trigger = yes } #I.e. it's an important disease
				scope:sick_character = {
					is_target_in_variable_list = { #The character got an event when sick character got sick and needs to be updated
						name = recovery_event_receivers
						target = prev
					}
				}
			}
		}
	}
}

has_health_court_owner_trigger = { #Should match save_health_court_owner_effect
	trigger_if = {
		limit = {
			exists = imprisoner
		}
		always = yes
		imprisoner = { save_temporary_scope_as = trigger_health_court_owner }
	}
	trigger_else_if = {
		limit = { exists = host }
		always = yes
		host = { save_temporary_scope_as = trigger_health_court_owner }
	}
	trigger_else = {
		always = no
	}
}

court_physician_can_fail_on_purpose_trigger = {
	scope:physician = {
		dread_modified_ai_boldness = {
			dreaded_character = scope:sick_character
			value > 0
		}
	}
	OR = {
	 	reverse_opinion = {
	 		target = scope:physician
	 		value < 0
	 	}
		has_relation_rival = scope:physician
		has_relation_potential_rival = scope:physician
	}
	NOT = { scope:physician = scope:sick_character }
}

has_traits_that_negate_athletic_trigger = { #Are we already athletic or does any of our traits contradict athletic?
	OR = {
		has_trait = athletic
		has_trait = lazy
		has_trait = infirm
		has_trait = incapable
	}
}

#Did this character's culture or area have any evidence of practicing trepanning?
native_trepanning_trigger = {
	OR = {
		root.culture = {
			OR = {
				has_cultural_pillar = heritage_east_african
				has_cultural_pillar = heritage_west_slavic
				has_cultural_pillar = heritage_central_germanic
				has_cultural_pillar = heritage_west_germanic
				has_cultural_pillar = heritage_dravidian
				has_cultural_pillar = heritage_indo_aryan
			}
		}
		root.faith = faith:armenian_apostolic
		root.culture = culture:armenian
		has_title = title:k_hungary
		has_title = title:k_cuman
		root.capital_province = {
			OR = {
				geographical_region = ghw_region_mediterranean
				geographical_region = ghw_region_anatolia
				geographical_region = world_europe_west_germania
				geographical_region = world_europe_west
				geographical_region = world_africa_north_east
				geographical_region = custom_slavia
				geographical_region = world_india
			}	
		}
	}
}

#Has this character already developed immunity to the epidemic?
immune_to_epidemic = {
	NOT = {
		is_target_in_variable_list = {
			name = immunised_plagues
			target = $EPIDEMIC$
		}
	}
}

# I'm putting this here because I refuse to make a "hair_triggers.txt" file.
is_bald_trigger = {
	OR = {
		# As it transpires, we actually cannot use this trigger here.
		#has_gene = {
		#	category = gene_baldness
		#	template = male_pattern_baldness
		#}
		has_character_modifier = hajj_halq_modifier
		has_character_modifier = shaven_bald_modifier
	}
}
