﻿
change_illness_progress = {
	if = {
		limit = { has_trait = $TRAIT$ }
		add_trait_xp = {
			trait = $TRAIT$
			value = {
				value = $VALUE$
				switch = { # flat progression
					trigger = has_game_rule
					decline_of_health_harsh = { add = 5 }
					decline_of_health_severe = { add = 10 }
				}
				# how was the treatment?
				switch = {
					trigger = has_character_modifier
					safe_disease_treatment_success_high_modifier = { add = -1 }
					#safe_disease_treatment_success_low_modifier = { add = -1 }
					safe_disease_treatment_failure_modifier = { add = 2 }
					risky_disease_treatment_success_high_modifier = { add = -2 }
					#risky_disease_treatment_success_low_modifier = { add = -2 }
					risky_disease_treatment_failure_modifier = { add = 3 }
				}
				# any wounds?
				switch = {
					trigger = has_character_modifier
					safe_wound_treatment_success_high_modifier = { add = -1 }
					#safe_wound_treatment_success_low_modifier = { add = -1 }
					safe_wound_treatment_failure_modifier = { add = 2 }
					risky_wound_treatment_success_modifier = { add = -2 }
					risky_wound_treatment_failure_modifier = { add = 3 }
				}
				min = 0
			}
		}
	}
}

# gets illness stage if beyond initial stage
# returns var:illness_stage
get_illness_stage = {
	if = {
		limit = {
			has_trait_xp = {
				trait = $TRAIT$
				value >= 80
			}
		}
		set_variable = { name = illness_stage value = 5 }
	}
	else_if = {
		limit = {
			has_trait_xp = {
				trait = $TRAIT$
				value >= 60
			}
		}
		set_variable = { name = illness_stage value = 4 }
	}
	else_if = {
		limit = {
			has_trait_xp = {
				trait = $TRAIT$
				value >= 40
			}
		}
		set_variable = { name = illness_stage value = 3 }
	}
	else_if = {
		limit = {
			has_trait_xp = {
				trait = $TRAIT$
				value >= 20
			}
		}
		set_variable = { name = illness_stage value = 2 }
	}
	else = {
		set_variable = { name = illness_stage value = 1 }
	}
	switch = { # gets a head start
		trigger = has_game_rule
		decline_of_health_harsh = {
			change_variable = {
				name = illness_stage
				add = 1
			}
		}
		decline_of_health_severe = {
			change_variable = {
				name = illness_stage
				add = 2
			}
		}
	}
	# gets worse stage if location is currented infected by $TRAIT$
	location ?= {
		random_province_epidemic = {
			limit = { epidemic_type.epidemic_trait = trait:$TRAIT$ }
			save_temporary_scope_value_as = {
				name = afflicting_chance
				value = {
					value = 50
					switch = {
						trigger = has_game_rule
						decline_of_health_harsh = { add = 25 }
						decline_of_health_severe = { add = 50 }
					}
					divide = {
						value = prev.epidemic_resistance
						min = 1 # don't divide by 0
					}
					switch = {
						trigger = outbreak_intensity
						minor = { multiply = 1 }
						major = { multiply = 2 }
						apocalyptic = { multiply = 3 }
					}
				}
			}
			save_temporary_scope_value_as = {
				name = afflicting_degree
				value = {
					switch = {
						trigger = outbreak_intensity
						minor = { value = 1 }
						major = { value = 2 }
						apocalyptic = { value = 3 }
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:afflicting_chance
			exists = scope:afflicting_degree
		}
		random = {
			chance = scope:afflicting_chance
			change_variable = {
				name = illness_stage
				add = scope:afflicting_degree
			}
		}
	}
}