﻿# character having its birthday in root scope
# tested after age has increased
on_birthday = {
	on_actions = {
		on_reveal_genetic_traits
		on_childhood_trait_change
		on_remarriage_status
		on_intellect_rebalance
	}
}

on_reveal_genetic_traits = {
	trigger = {
		NOT = { has_game_rule = child_development_disabled }
		age >= 2
		age <= 16
	}
	effect = {
		if = {
			limit = { has_variable = qol_child_pure_blooded }
			random = {
				chance = adult_age_max_chance
				make_qol_trait_active = { TRAIT = pure_blooded }
			}
		}
		else_if = {
			limit = { has_variable = qol_child_inbred }
			random = {
				chance = adult_age_max_chance
				make_qol_trait_active = { TRAIT = inbred }
			}
		}
	}
	on_actions = {
		on_special_reveal
		on_early_age_reveal
		delay = { days = { 1 90 } }
		on_childhood_reveal
		delay = { days = { 91 180 } }
		on_adolescent_reveal
	}
}

on_special_reveal = {
	effect = {
		if = {
			limit = {
				has_variable = qol_child_physique
				OR = {
					age >= 6
					health < fine_health
				}
			}
			random = {
				chance = adult_age_max_chance_health_considered
				make_qol_trait_group_active = { TRAIT = physique }
			}
		}
	}
}

on_early_age_reveal = {
	effect = {
		if = {
			limit = { has_variable = qol_child_intellect }
			random = {
				chance = adult_age_max_chance
				make_qol_trait_group_active = { TRAIT = intellect }
				#if = {
				#	limit = { NOT = { has_game_rule = intellect_rebalance_disabled } }
					set_intellect_trait_xp = yes
				#}
			}
		}
		if = {
			limit = { has_variable = qol_child_lisping }
			random = {
				chance = early_age_max_chance
				make_qol_trait_active = { TRAIT = lisping }
			}
		}
		if = {
			limit = { has_variable = qol_child_stuttering }
			random = {
				chance = early_age_max_chance
				make_qol_trait_active = { TRAIT = stuttering }
			}
		}
	}
}

on_childhood_reveal = {
	trigger = {
		age >= 6
	}
	effect = {
		if = {
			limit = { has_variable = qol_child_dwarf }
			random = {
				chance = adolescent_max_chance
				make_qol_trait_active = { TRAIT = dwarf }
			}
		}
		if = {
			limit = { has_variable = qol_child_giant }
			random = {
				chance = adolescent_max_chance
				make_qol_trait_active = { TRAIT = giant }
			}
		}
	}
}

on_adolescent_reveal = {
	trigger = {
		age >= 12
	}
	effect = {
		if = {
			limit = { has_variable = qol_child_beauty }
			random = {
				chance = adult_age_max_chance
				make_qol_trait_group_active = { TRAIT = beauty }
			}
		}
		if = {
			limit = { has_variable = qol_child_fecund }
			random = {
				chance = adult_age_max_chance
				make_qol_trait_active = { TRAIT = fecund }
			}
		}
	}
}

on_childhood_trait_change = {
	trigger = {
		has_game_rule = keep_childhood_trait_enabled
		OR = {
			has_trait = rowdy
			has_trait = charming
			has_trait = curious
			has_trait = pensive
			has_trait = bossy
		}
	}
	effect = {
		save_scope_as = passed_check
		random = {
			chance = {
				value = -144
				if = {
					limit = { age > 12 }
					add = {
						value = age
						multiply = 12
						max = 204
					}
				}

				#add = -12
				#if = { #12, 13, 14
				#	limit = { age < 15 }
				#	divide = 6
				#}
				#else = { #15, 16, 17
				#	multiply = 10
				#}
				if = {
					limit = {
						has_trait = diplomat
						OR = {
							has_trait = charming
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = august
						OR = {
							has_trait = charming
							has_trait = bossy
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_reveler
						OR = {
							has_trait = rowdy
							has_trait = charming
							has_trait = bossy
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_blademaster
						OR = {
							has_trait = rowdy
							has_trait = charming
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_hunter
						OR = {
							has_trait = rowdy
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = strategist
						OR = {
							has_trait = curious
							has_trait = pensive
							has_trait = bossy
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = overseer
						OR = {
							has_trait = rowdy
							has_trait = pensive
							has_trait = bossy
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = gallant
						has_trait = rowdy
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = architect
						OR = {
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = administrator
						OR = {
							has_trait = pensive
							has_trait = bossy
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = schemer
						OR = {
							has_trait = rowdy
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = seducer
						OR = {
							has_trait = rowdy
							has_trait = charming
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = scholar
						OR = {
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = theologian
						OR = {
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_mystic
						OR = {
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_physician
						OR = {
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_herbalist
						OR = {
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_gardener
						OR = {
							has_trait = charming
							has_trait = curious
							has_trait = pensive
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lifestyle_traveler
						OR = {
							has_trait = charming
							has_trait = curious
						}
					}
					add = -1
				}
				if = {
					limit = {
						has_trait = lustful
						OR = {
							has_trait = rowdy
							has_trait = charming
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = greedy
						OR = {
							has_trait = rowdy
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = generous
						has_trait = charming
					}
					add = -2
				}
				if = {
					limit = { has_trait = diligent }
					add = -2
				}
				if = {
					limit = {
						has_trait = wrathful
						OR = {
							has_trait = rowdy
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = calm
						NOT = { has_trait = rowdy }
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = patient
						NOT = { has_trait = rowdy }
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = arrogant
						OR = {
							has_trait = rowdy
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = humble
						OR = {
							has_trait = charming
							has_trait = curious
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = deceitful
						OR = {
							has_trait = rowdy
							has_trait = charming
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = brave
						OR = {
							has_trait = rowdy
							has_trait = charming
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = gregarious
						OR = {
							has_trait = charming
							has_trait = curious
						}
					}
					add = -2
				}
				if = {
					limit = { has_trait = ambitious }
					add = -2
				}
				if = {
					limit = {
						has_trait = just
						OR = {
							has_trait = charming
							has_trait = pensive
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = cynical
						OR = {
							has_trait = rowdy
							has_trait = curious
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = compassionate
						OR = {
							has_trait = charming
							has_trait = curious
						}
					}
					add = -2
				}
				if = {
					limit = {
						OR = {
							has_trait = callous
							has_trait = sadistic
						}
						has_trait = rowdy
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = stubborn
						OR = {
							has_trait = rowdy
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = fickle
						OR = {
							has_trait = rowdy
							has_trait = curious
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = eccentric
						has_trait = curious
					}
					add = -2
				}
				if = {
					limit = {
						has_trait = vengeful
						OR = {
							has_trait = rowdy
							has_trait = bossy
						}
					}
					add = -2
				}
				if = {
					limit = {
						OR = {
							has_trait = rowdy
							has_trait = charming
						}
					}
					if = {
						limit = { has_trait = education_martial_prowess_1 }
						add = -1
					}
					else_if = {
						limit = { has_trait = education_martial_prowess_2 }
						add = -2
					}
					else_if = {
						limit = { has_trait = education_martial_prowess_3 }
						add = -3
					}
					else_if = {
						limit = { has_trait = education_martial_prowess_4 }
						add = -4
					}
				}
				if = {
					limit = {
						OR = {
							has_trait = pensive
							has_trait = bossy
						}
					}
					if = {
						limit = { has_trait = education_republican_knowledge_1 }
						add = -1
					}
					else_if = {
						limit = { has_trait = education_republican_knowledge_2 }
						add = -2
					}
					else_if = {
						limit = { has_trait = education_republican_knowledge_3 }
						add = -3
					}
					else_if = {
						limit = { has_trait = education_republican_knowledge_4 }
						add = -4
					}
				}
				if = {
					limit = { has_level_1_education_trait_trigger = yes }
					add = -1
				}
				else_if = {
					limit = { has_level_2_education_trait_trigger = yes }
					add = -2
				}
				else_if = {
					limit = { has_level_3_education_trait_trigger = yes }
					add = -3
				}
				else_if = {
					limit = { has_level_4_education_trait_trigger = yes }
					add = -4
				}
				else = { # includes children without education trait, yes
					add = -5
				}
				if = {
					limit = { has_education_affinity_childhood_trait_trigger = yes }
					multiply = 0.8
				}
				if = {
					limit = { has_trait = rowdy }
					add = {
						value = martial
						add = intrigue
						add = prowess
						divide = -15
						floor = yes
					}
					if = {
						limit = {
							has_trait_xp = {
								trait = rowdy
								value >= 100
							}
						}
						add = 20
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = rowdy
								value >= 75
							}
						}
						add = 15
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = rowdy
								value >= 50
							}
						}
						add = 10
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = rowdy
								value >= 25
							}
						}
						add = 5
					}
				}
				if = {
					limit = { has_trait = charming }
					add = {
						value = intrigue
						add = diplomacy
						add = learning
						divide = -15
						floor = yes
					}
					if = {
						limit = {
							has_trait_xp = {
								trait = charming
								value >= 100
							}
						}
						add = 20
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = charming
								value >= 75
							}
						}
						add = 15
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = charming
								value >= 50
							}
						}
						add = 10
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = charming
								value >= 25
							}
						}
						add = 5
					}
				}
				if = {
					limit = { has_trait = curious }
					add = {
						value = diplomacy
						add = learning
						add = stewardship
						divide = -15
						floor = yes
					}
					if = {
						limit = {
							has_trait_xp = {
								trait = curious
								value >= 100
							}
						}
						add = 20
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = curious
								value >= 75
							}
						}
						add = 15
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = curious
								value >= 50
							}
						}
						add = 10
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = curious
								value >= 25
							}
						}
						add = 5
					}
				}
				if = {
					limit = { has_trait = pensive }
					add = {
						value = learning
						add = stewardship
						add = intrigue
						divide = -15
						floor = yes
					}
					if = {
						limit = {
							has_trait_xp = {
								trait = pensive
								value >= 100
							}
						}
						add = 20
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = pensive
								value >= 75
							}
						}
						add = 15
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = pensive
								value >= 50
							}
						}
						add = 10
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = pensive
								value >= 25
							}
						}
						add = 5
					}
				}
				if = {
					limit = { has_trait = bossy }
					add = {
						value = stewardship
						add = martial
						add = diplomacy
						divide = -15
						floor = yes
					}
					if = {
						limit = {
							has_trait_xp = {
								trait = bossy
								value >= 100
							}
						}
						add = 20
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = bossy
								value >= 75
							}
						}
						add = 15
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = bossy
								value >= 50
							}
						}
						add = 10
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = bossy
								value >= 25
							}
						}
						add = 5
					}
				}
				if = {
					limit = {
						OR = {
							is_ruler = yes
							any_heir_title = {
								place_in_line_of_succession = {
									target = root
									value <= 4
								}
							}
						}
					}
					add = -2
				}
				if = {
					limit = { has_variable = qol_child_intellect }
					add = {
						value = var:qol_child_intellect
						multiply = -1
					}
				}
				else = {
					if = {
						limit = { has_trait = intellect_good_3 }
						add = -3
					}
					else_if = {
						limit = { has_trait = intellect_good_2 }
						add = -2
					}
					else_if = {
						limit = { has_trait = intellect_good_1 }
						add = -1
					}
					else_if = {
						limit = { has_trait = intellect_bad_1 }
						add = 1
					}
					else_if = {
						limit = { has_trait = intellect_bad_2 }
						add = 2
					}
					else_if = {
						limit = { has_trait = intellect_bad_3 }
						add = 3
					}
				}
				if = {
					limit = { has_trait = charming }
					if = {
						limit = { has_variable = qol_child_beauty }
						add = {
							value = var:qol_child_beauty
							multiply = -1
						}
					}
					else = {
						if = {
							limit = { has_trait = beauty_good_3 }
							add = -3
						}
						else_if = {
							limit = { has_trait = beauty_good_2 }
							add = -2
						}
						else_if = {
							limit = { has_trait = beauty_good_1 }
							add = -1
						}
						else_if = {
							limit = { has_trait = beauty_bad_1 }
							add = 1
						}
						else_if = {
							limit = { has_trait = beauty_bad_2 }
							add = 2
						}
						else_if = {
							limit = { has_trait = beauty_bad_3 }
							add = 3
						}
					}
				}
				if = {
					limit = {
						OR = {
							has_trait = rowdy
							has_trait = charming
						}
					}
					if = {
						limit = { has_variable = qol_child_physique }
						add = {
							value = var:qol_child_physique
							multiply = -1
						}
					}
					else = {
						if = {
							limit = { has_trait = physique_good_3 }
							add = -3
						}
						else_if = {
							limit = { has_trait = physique_good_2 }
							add = -2
						}
						else_if = {
							limit = { has_trait = physique_good_1 }
							add = -1
						}
						else_if = {
							limit = { has_trait = physique_bad_1 }
							add = 1
						}
						else_if = {
							limit = { has_trait = physique_bad_2 }
							add = 2
						}
						else_if = {
							limit = { has_trait = physique_bad_3 }
							add = 3
						}
					}
				}
				if = {
					limit = { has_trait = infirm }
					add = 10
				}
				if = {
					limit = { has_trait = incapable }
					add = 20
				}
				floor = yes
				min = 0
				max = 100
			}
			mr_QOL_remove_childhoood_traits_effect = yes
			clear_saved_scope = passed_check
		}
		scope:passed_check ?= {
			mr_QOL_improve_childhoood_traits_effect = yes
		}
	}
}

on_remarriage_status = {
	trigger = {
		debug_only = yes
	}
	effect = {
		if = {
			limit = { Not = { has_character_modifier = cannot_remarry } }
			add_character_modifier = cannot_remarry
		}
		if = {
			limit = { can_remarry_trigger = yes }
			remove_character_modifier = cannot_remarry
		}
	}
}