﻿total_skill = {
	value = diplomacy
	add = martial
	add = stewardship
	add = intrigue
	add = learning
	add = prowess
}

intellect_diplomacy_exp = {
	value = intrigue
	add = learning
	divide = 2
}
intellect_martial_exp = {
	value = stewardship
	add = learning
	divide = 2
}
intellect_stewardship_exp = {
	value = stewardship
	add = intrigue
	add = learning
	divide = 3
}
intellect_intrigue_exp = {
	value = diplomacy
	add = stewardship
	add = learning
	divide = 3
}
intellect_learning_exp = {
	value = diplomacy
	add = martial
	add = stewardship
	add = intrigue
	divide = 4
}

# should be used in a landed_title scope
ai_county_payout_highest = {
	value = title_province.monthly_income
	multiply = 2
	min = 0.1
}

# should be used in a landed_title scope
ai_county_payout_high = {
	value = title_province.monthly_income
	multiply = 1.4
	min = 0.1
}

# should be used in a landed_title scope
ai_county_payout = {
	value = title_province.monthly_income
	multiply = 0.9
	min = 0.1
}

# should be used in a landed_title scope
ai_county_payout_low = {
	value = title_province.monthly_income
	multiply = 0.5
	min = 0.1
}

# should be used in a landed_title scope
ai_county_payout_lowest = {
	value = title_province.monthly_income
	multiply = 0.2
	min = 0.1
}

immediate_family_count = {
	every_close_family_member = {
		even_if_dead = yes
		limit = { NOT = { is_grandparent_of = prev } }
		add = 1
	}
	every_spouse = { add = 1 }
	if = { # count up to 1 dead spouse
		limit = { any_former_spouse = { even_if_dead = yes } }
		add = 1
	}
}

immediate_family_alive_percentage = { # 0.0 < x < 1.0(all alive) < if grandparents are also alive
	if = { # to avoid dividing by zero
		limit = { immediate_family_count = 0 }
		value = 0 # no family means all family is dead in this case!
	}
	else = {
		every_close_family_member = { add = 1 } # grandparents are bonus
		every_spouse = { add = 1 }
		divide = immediate_family_count
	}
}

###### Health ######

aging_factor = {
	# base; range 0.96(age 30) < 1.16(age 50) < x < inf
	value = { #1.36
		value = effective_age
		divide = 100
		add = 0.66
	}
	if = { # you're stacked!
		limit = {
			is_ai = no
			effective_age >= 60
			health > 3.5
		}
		add = {
			value = effective_age
			add = -60
			if = {
				limit = { effective_age < 80 }
				divide = 200
			}
			else_if = {
				limit = { effective_age < 90 }
				divide = 100
			}
			else = {
				divide = 50
			}
		}
	}
	# stress; range 0 < x < 3
	add = {
		value = stress
		divide = 100
	}
	# depression; range 0.3 < x < inf
	if = {
		limit = { has_trait = depressed }
		add = {
			value = effective_age
			divide = 100
		}
	}
	# infirm; flat 1
	if = {
		limit = { has_trait = infirm }
		add = 1
	}
	# whole_of_body; flat -0.15
	if = {
		limit = { has_trait = whole_of_body }
		add = -0.15
	}
	# graceful_aging; flat -0.1
	if = {
		limit = {
			exists = dynasty
			dynasty = { has_dynasty_perk = kin_legacy_5 }
		}
		add = -0.1
	}
	# weight; range 0 < x < 1
	if = { # overweight
		limit = { current_weight > 30 }
		add = {
			value = current_weight
			add = -30
			divide = 70
		}
	}
	else_if = { # underweight
		limit = { current_weight < -30 }
		add = {
			value = current_weight
			add = 30
			divide = -70
		}
	}
	# active lifestyle; range 0 < x < -0.25
	add = {
		value = 0
		if = {
			limit = { has_trait = athletic }
			add = -0.1
		}
		if = {
			limit = { has_trait = tourney_participant }
			add = -0.1
		}
		if = {
			limit = { has_trait = lifestyle_hunter }
			add = -0.1
		}
		if = {
			limit = { has_trait = lifestyle_blademaster }
			add = -0.1
		}
		if = {
			limit = { has_trait = whole_of_body }
			add = -0.15
		}
		min = -0.25
	}
	# loneliness kills; range -0.3 < x < 0.5
	if = {
		limit = { years_from_game_start > 10 } # give it some time
		add = {
			# assuming 0 relation
			value = { # punish the ill-represented characters less
				add = highest_held_title_tier
				divide = 8
			}
			every_relation = { # 5 to recover, more = bonus
				type = soulmate
				type = lover
				type = potential_lover
				type = crush
				type = blood_brother
				type = best_friend
				type = friend
				type = potential_friend
				type = favorite_child
				type = elder
				type = disciple
				type = mentor
				type = student
				type = guardian
				type = ward
				add = -0.1
			}
			every_relation = {
				type = soulmate
				type = best_friend
				add = -0.2
			}
			if = {
				limit = { has_trait = lifestyle_reveler }
				add = -0.1
			}
			if = {
				limit = { has_trait = diplomat }
				add = -0.1
			}
			min = -0.3 # max bonus
		}
	}
	# alcohol addict: flat 0.15
	if = {
		limit = { has_trait = drunkard }
		add = 0.15
	}
	# family loss; max-range from -0.2 < x < 1.5(all dead)
	if = {
		# only if you had any to start with; protect the ill-represented lowborn, wanderers, and new or low nobles
		limit = {
			save_temporary_scope_value_as = {
				name = family_count
				value = immediate_family_count
			}
			scope:family_count > 0 # to avoid dividing by zero
		}
		add = {
			value = {
				value = -0.2
				multiply = scope:family_count
				min = -1.7
			}
			multiply = {
				# not calling immediate_family_count to save calculating extra immediate_family_count
				every_close_family_member = { add = 1 } # grandparents are bonus
				every_spouse = { add = 1 }
				divide = scope:family_count
			}
			add = {
				value = 0.15
				multiply = scope:family_count
				max = 1.5
			}
		}
	}
	min = 1
}

gauge_physique = {
	value = health
	subtract = fine_health
	floor = yes
	if = { # genentic
		limit = { has_trait = physique_good_1 }
		add = 1
	}
	else_if = {
		limit = { has_trait = physique_good_2 }
		add = 3
	}
	else_if = {
		limit = { has_trait = physique_good_3 }
		add = 9
	}
	else_if = {
		limit = { has_trait = physique_bad_1 }
		add = -1
	}
	else_if = {
		limit = { has_trait = physique_bad_2 }
		add = -3
	}
	else_if = {
		limit = { has_trait = physique_bad_3 }
		add = -9
	}
	if = { # lifestyles
		limit = { has_trait = athletic }
		add = 2
	}
	if = {
		limit = { has_trait = strong }
		add = 1
	}
	if = {
		limit = { has_trait = weak }
		add = -1
	}
	if = {
		limit = { has_trait = lifestyle_hunter }
		add = 1
	}
	if = {
		limit = { has_trait = lifestyle_reveler }
		add = -1
	}
	if = {
		limit = { has_trait = drunkard }
		add = -2
	}
	# mental will
	if = {
		limit = { has_trait = depressed }
		add = -4
	}
	if = {
		limit = { stress > normal_baseline_stress } # 75
		add = {
			value = stress
			subtract = normal_baseline_stress
			multiply = -0.1
		}
	}
	else = {
		add = {
			value = normal_baseline_stress
			subtract = stress
			multiply = 0.05
		}
	}
	if = { # overweight
		limit = { current_weight > 30 }
		add = {
			value = current_weight
			add = -30
			multiply = -0.2
		}
	}
	else_if = { # underweight
		limit = { current_weight < -30 }
		add = {
			value = current_weight
			add = 30
			multiply = 0.2
		}
	}
	if = { # learned health perk
		limit = { has_perk = iron_constitution_perk }
		add = health
	}
	floor = yes
}

gauge_hygiene = {
	value = 0
	if = { # dungeon prisoners
		limit = {
			is_in_prison_type = dungeon
			time_in_prison_type = { days > 30 }
		}
		add = {
			value = days_in_prison
			add = -30
			multiply = 0.1
			floor = yes
			max = 40
		}
	}
	if = { # wanderer
		limit = {
			is_pool_character = yes
			NOR = {
				exists = liege
				exists = host
			}
		}
		add = 4
	}
	if = { # broke in travel
		limit = {
			is_travelling = yes
			debt_level >= 0
		}
		add = debt_level
	}
	else = { # simply broke
		add = {
			value = debt_level
			multiply = 0.33
			floor = yes
		}
	}
	if = { # lazy about it
		limit = { has_trait = lazy }
		add = 2
	}
	if = {
		limit = {
			has_perk = wash_your_hands_perk
			NOT = { is_in_prison_type = dungeon }
		}
		multiply = 0.5
		floor = yes
		add = -5
	}
	multiply = -1
}

treatment_value = {
	value = 0
	if = {
		limit = { exists = court_owner.court_position:court_physician_court_position }
		court_owner.court_position:court_physician_court_position = {
			add = aptitude:court_physician_court_position
		}
	}
	if = {
		limit = { has_character_modifier = botched_fertility_treatment }
		add = { -3 -1 }
	}
	else_if = {
		limit = { has_character_modifier = very_successful_fertility_treatment }
		add = { 1 3 }
	}
}

age_of_decline = {
	value = 40 # base
	multiply = {
		value = health
		subtract = fine_health
		min = 0
		divide = 4
		add = 1
	}
	floor = yes
	max = 80
}