﻿#On actions related to health events

on_decline_of_health = {
	trigger = {
		NOT = { has_game_rule = decline_of_health_disabled }
	}
	effect = {
		# exemptions for aging
		if = {
			limit = {
				NOT = { has_character_flag = mr_qol_aging_exempted }
				OR = {
					# AGOT dragons
					has_character_flag = owned_dragon
					exists = var:current_rider
					# can't simply check has_trait since it would spam errors without AGOT mod loaded
				}
			}
			add_character_flag = mr_qol_aging_exempted
		}
	}
	on_actions = {
		decline_of_health_pulse
		delay = { days = { 1 89 } }
		illness_progression
		delay = { days = { 90 179 } }
		illness_progression
		delay = { days = { 180 269 } }
		illness_progression
		delay = { days = { 270 365 } }
		illness_progression
	}
}

illness_progression = {
	effect = {
		## illness progression
		random_list = {
			2 = {
				modifier = { # young child
					effective_age < 8
					add = {
						value = -8
						add = effective_age
					}
				}
				modifier = { # elderly
					effective_age > age_of_decline
					add = {
						value = age_of_decline
						subtract = effective_age
					}
				}
				modifier = { # injuries
					has_trait = wounded_1
					add = -2
				}
				modifier = {
					has_trait = wounded_2
					add = -4
				}
				modifier = {
					has_trait = wounded_3
					add = -6
				}
				modifier = {
					has_trait = maimed
					add = -6
				}
				modifier = { # estimated physique
					always = yes
					add = gauge_physique
				}
				modifier = { # estimated hygiene
					always = yes
					add = gauge_hygiene
				}
				modifier = { # physician and treatment
					always = yes
					add = treatment_value
				}
				modifier = {
					OR = {
						has_game_rule = decline_of_health_harsh
						has_game_rule = decline_of_health_severe
					}
					add = -10
				}
			}
			1 = {
				trigger = { has_trait = ill }
				modifier = { # in season
					current_month > 9
					add = {
						value = current_month
						add = -9
					}
				}
				modifier = {
					current_month < 6
					add = {
						value = 6
						subtract = current_month
					}
				}
				get_illness_stage = { TRAIT = ill }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 0 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = ill
					VALUE = scope:ill_progress
				}
			}
			3 = {
				trigger = { has_trait = pneumonic }
				modifier = { # in season
					current_month > 9
					add = {
						value = current_month
						add = -9
					}
				}
				modifier = {
					current_month < 6
					add = {
						value = 6
						subtract = current_month
					}
				}
				get_illness_stage = { TRAIT = pneumonic }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 3 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 0 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = pneumonic
					VALUE = scope:ill_progress
				}
			}
			1 = {
				trigger = { has_trait = great_pox }
				get_illness_stage = { TRAIT = great_pox }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
						}
					}
				}
				change_illness_progress = {
					TRAIT = great_pox
					VALUE = scope:ill_progress
				}
			}
			1 = {
				trigger = { has_trait = lovers_pox }
				get_illness_stage = { TRAIT = lovers_pox }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
						}
					}
				}
				change_illness_progress = {
					TRAIT = lovers_pox
					VALUE = scope:ill_progress
				}
			}
			1 = {
				trigger = { has_trait = leper }
				get_illness_stage = { TRAIT = leper }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 3 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
						}
					}
				}
				change_illness_progress = {
					TRAIT = leper
					VALUE = scope:ill_progress
				}
			}
			1 = {
				trigger = { has_trait = gout_ridden }
				get_illness_stage = { TRAIT = gout_ridden }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 3 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
						}
					}
				}
				change_illness_progress = {
					TRAIT = gout_ridden
					VALUE = scope:ill_progress
				}
			}
			3 = {
				trigger = { has_trait = consumption }
				get_illness_stage = { TRAIT = consumption }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 3 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 0 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = consumption
					VALUE = scope:ill_progress
				}
			}
			3 = {
				trigger = { has_trait = cancer }
				modifier = { # young child
					effective_age < 8
					add = {
						value = -8
						add = effective_age
					}
				}
				modifier = { # elderly
					effective_age > age_of_decline
					add = {
						value = effective_age
						subtract = age_of_decline
					}
				}
				get_illness_stage = { TRAIT = cancer }
				save_scope_value_as = {
					name = ill_progress
					value = { # chronic
						value = { 1 1.2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 0 var:illness_stage }
							add = { 0 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = cancer
					VALUE = scope:ill_progress
				}
			}
			1 = {
				trigger = { has_trait = typhus }
				get_illness_stage = { TRAIT = typhus }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 4 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
						}
					}
				}
				change_illness_progress = {
					TRAIT = typhus
					VALUE = scope:ill_progress
				}
			}
			5 = {
				trigger = { has_trait = bubonic_plague }
				get_illness_stage = { TRAIT = bubonic_plague }
				save_scope_value_as = {
					name = ill_progress
					value = { # rapid
						value = { 1.2 3.2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
							multiply = { 1 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = bubonic_plague
					VALUE = scope:ill_progress
				}
			}
			3 = {
				trigger = { has_trait = smallpox }
				get_illness_stage = { TRAIT = smallpox }
				save_scope_value_as = {
					name = ill_progress
					value = { # rapid
						value = { 1.2 2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
							multiply = { 1 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = smallpox
					VALUE = scope:ill_progress
				}
			}
			3 = {
				trigger = { has_trait = measles }
				get_illness_stage = { TRAIT = measles }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1.5 3.5 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = measles
					VALUE = scope:ill_progress
				}
			}
			2 = {
				trigger = { has_trait = dysentery }
				get_illness_stage = { TRAIT = dysentery }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = dysentery
					VALUE = scope:ill_progress
				}
			}
			1 = {
				trigger = { has_trait = ergotism }
				get_illness_stage = { TRAIT = ergotism }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 1 2 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
						}
					}
				}
				change_illness_progress = {
					TRAIT = ergotism
					VALUE = scope:ill_progress
				}
			}
			1 = {
				trigger = { has_trait = sickly }
				get_illness_stage = { TRAIT = sickly }
				save_scope_value_as = {
					name = ill_progress
					value = {
						value = { 2 3 } # base
						if = {
							limit = { exists = var:illness_stage }
							add = { 1 var:illness_stage }
							multiply = { 1 var:illness_stage }
						}
						floor = yes
					}
				}
				change_illness_progress = {
					TRAIT = sickly
					VALUE = scope:ill_progress
				}
			}
		}
		random = { # fever?
			chance = {
				value = 1
				if = {
					limit = { exists = var:illness_stage }
					add = var:illness_stage
				}
				multiply = 10
				max = 100
			}
			add_character_modifier = {
				modifier = mr_qol_fever_modifier
				days = { 2 14 }
			}
		}
		remove_variable = illness_stage
		clear_saved_scope = ill_progress

		random = {
			chance = 25
			trigger_event = {
				on_action = illness_progression
				days = { 15 30 }
			}
		}
	}
}

decline_of_health_pulse = {
	# estimated fastest death: ~5 years
	# estimated aging_factor for 30 years: ~1.36
	effect = {
		switch = { # reset modifier stack
			trigger = has_character_modifier
			mr_qol_aging = { remove_all_character_modifier_instances = mr_qol_aging }
			mr_qol_aging_harsh = { remove_all_character_modifier_instances = mr_qol_aging_harsh }
			mr_qol_aging_severe = { remove_all_character_modifier_instances = mr_qol_aging_severe }
		}
		# calculate mr_qol_aging
		if = {
			limit = {
				NOT = { has_character_flag = mr_qol_aging_exempted } # exemption for long-living species (from other mods)
				is_immortal = no
				trigger_if = {
					limit = { is_ai = yes }
					effective_age >= 50
				}
				trigger_else_if = {
					limit = { NOT = { has_game_rule = decline_of_health_severe } }
					effective_age >= 45
				}
				trigger_else = {
					effective_age >= 30
				}
			}
			if = {
				limit = { NOT = { has_variable = mr_qol_aging } }
				set_variable = {
					name = mr_qol_aging
					value = 0.1
				}
			}
			if = { # maxed out
				limit = {
					trigger_if = {
						limit = { has_game_rule = decline_of_health_normal }
						var:mr_qol_aging < 10000
					}
					trigger_else_if = {
						limit = { has_game_rule = decline_of_health_harsh }
						var:mr_qol_aging < 100000
					}
					trigger_else = {
						var:mr_qol_aging < 1000000
					}
				}
				change_variable = {
					name = mr_qol_aging
					multiply = aging_factor
				}
			}
		}
		else_if = {
			limit = { has_variable = mr_qol_aging }
			remove_variable = mr_qol_aging
		}
		# set modifiers
		if = {
			limit = { has_variable = mr_qol_aging }
			set_variable = {
				name = mr_qol_aging_copy
				value = {
					value = var:mr_qol_aging
					floor = yes
				}
			}
			while = {
				limit = { var:mr_qol_aging_copy > 1 }
				switch = {
					trigger = has_game_rule
					decline_of_health_normal = { add_character_modifier = mr_qol_aging }
					decline_of_health_harsh = { add_character_modifier = mr_qol_aging_harsh }
					decline_of_health_severe = { add_character_modifier = mr_qol_aging_severe }
				}
				change_variable = {
					name = mr_qol_aging_copy
					multiply = 0.5
				}
			}
			remove_variable = mr_qol_aging_copy
		}
	}
}