﻿# character having its birthday in root scope
# tested after age has increased
on_birthday = {
	on_actions = {
		on_specific_birthday
		on_birthday_childhood
		on_birthday_adulthood
		on_graceful_aging_birthday
		
		# AGOT Added, update dragon on aging
		on_dragon_birthday
	}
}

on_specific_birthday = {
	first_valid_on_action = {
		#AGOT Added, Targ Marriage AI
		on_1st_birthday
		on_3rd_birthday
		on_6th_birthday
		on_10th_birthday
		#AGOT Added, Targ Marriage AI and choosing patron
		on_14th_birthday
		on_15th_birthday
		on_16th_birthday
	}
}

on_birthday_childhood = {
	trigger = {
		is_adult = no
		age >= childhood_education_start_age
	}
	on_actions = {
		on_birthday_education_events
		on_action_add_sexuality
	}
	effect = {
		if = {
			limit = {
				any_parent = {
					is_landed = yes
					highest_held_title_tier >= tier_duchy
					any_memory = {
						has_memory_type = ascended_throne_memory
						has_variable = childhood_memory
						save_temporary_scope_as = throne_memory_temp
					}
				}
			}
			random_parent = {
				limit = {
					is_landed = yes
					highest_held_title_tier >= tier_duchy
					any_memory = {
						has_memory_type = ascended_throne_memory
						has_variable = childhood_memory
						save_temporary_scope_as = throne_memory_temp
					}
				}
				trigger_event = bp2_yearly.4003 #This event has further triggers, as well as a cooldown, and may still fail.
			}
		}
		if = {
			limit = {
				culture = {
					has_cultural_parameter = strong_traits_more_common
				}
				NOR = {
					has_trait = strong
					has_trait = weak
					has_trait = physique_bad
				}
			}
			random = {
				chance = {
					value = 0
					if = {
						limit = {
							prowess > 0
						}
						add = prowess
						divide = 5
					}
				}
				add_trait = strong
			}
		}
		random = { # Chance for relationships for unlanded children/barons
			chance = {
				value = 10
				if = {
					limit = {
						is_hostage = yes
					}
					add = 15 #Hostages are forced to form relationships quickly
				}
				if = {
					limit = {
						OR = {
							has_sexuality = heterosexual
							has_sexuality = homosexual
							has_sexuality = bisexual
						}
						num_of_relation_crush <= 0
					}
					add = 25
				}
			}
			if = {
				limit = {
					is_available_ai_child = yes
					OR = {
						is_ruler = no
						highest_held_title_tier = tier_barony
					}
					exists = host
				}
				random_list = {
					30 = { # Gain a Crush
						trigger = {
							OR = {
								num_of_relation_crush <= 0
								has_trait = lustful
							}
							NOR = {
								has_trait = chaste
								has_trait = celibate
							}
							OR = {
								has_sexuality = heterosexual
								has_sexuality = bisexual
								has_sexuality = homosexual
							}
						}
						save_good_character_effect = {
							RELATION = crush
							SCOPE = crush
						}
						if = {
							limit = {
								exists = scope:crush
							}
							set_relation_crush = scope:crush
							random = {
								chance = 50
								scope:crush = {
									if = {
										limit = {
											is_ai = yes
											OR = {
												has_sexuality = heterosexual
												has_sexuality = bisexual
												has_sexuality = homosexual
											}
											matching_gender_and_sexuality_trigger = {
												CHARACTER_1 = scope:crush
												CHARACTER_2 = root
											}
											NOT = {
												has_relation_crush = root
											}
										}
										set_relation_crush = root
									}
									else_if = {
										limit = {
											NOT = {
												has_relation_crush = root
											}
										}
										set_variable = {
											name = unrequited_crush
											value = root
										}
									}
								}
							}
						}
					}
					10 = {
						trigger = {
							OR = {
								num_of_relation_friend < 1
								has_trait = gregarious
							}
							NOR = {
								has_trait = shy
								has_trait = callous
							}
						}
						save_good_character_effect = {
							RELATION = friend
							SCOPE = friend
						}
						if = {
							limit = {
								exists = scope:friend
								scope:friend = {
									num_of_relation_friend < 1
								}
							}
							set_relation_friend = { reason = friend_childhood_random target = scope:friend }
							add_stress = minor_stress_loss
						}
					}
					10 = {
						trigger = {
							num_of_relation_bully <= 0
						}
						save_good_character_effect = {
							RELATION = bully
							SCOPE = bully
						}
						if = {
							limit = {
								exists = scope:bully
							}
							set_relation_bully = scope:bully
							add_stress = minor_stress_gain
						}
					}
					10 = {
						trigger = {
							OR = {
								num_of_relation_victim <= 0
								has_trait = sadistic
							}
							NOR = {
								has_trait = forgiving
								has_trait = compassionate
							}
						}
						save_good_character_effect = {
							RELATION = victim
							SCOPE = victim
						}
						if = {
							limit = {
								exists = scope:victim
							}
							set_relation_victim = scope:victim
							add_stress = minor_stress_loss
						}
					}
				}
			}
		}
	}
	random_events = {
		900 = 0
		150 = childhood.2100 # Learn the Language of your Guardian
		150 = childhood.2200 # Learn the Court Language of your Guardian
		150 = childhood.2300 # Learn the Language of your parent
	}
	on_actions = {
		reincarnation_toy_pulse
	}
}
reincarnation_toy_pulse = {
	random_events = {
		chance_to_happen = 14
		100 = bp2_yearly.8090 # Take the toy test to become a reincarnation (14 in 1,464 is roughly 1% in 100. This event can fire between the ages of 4 and 15, meaning there are 12 chances of this happening)
	}
}

on_birthday_adulthood = {
	trigger = {
		is_adult = yes
	}
	random_events = {
		100 = 0
		1 = bp1_yearly.3008 # Surprise Birthday Party Prelude
		1 = bp1_yearly.3006 # Reflecting on the surprise party years later
	}
}

on_graceful_aging_birthday = {
	trigger = {
		exists = dynasty
		dynasty = { has_dynasty_perk = kin_legacy_5 }
		age >= kin_legacy_5_start_age # Start at 40 - This trigger is here to prevent the calculation running for everyone
		save_temporary_scope_value_as = {
			name = age_check
			value = kin_legacy_5_age_modulo
		}
		scope:age_check = 0
	}
	events = {
		dynasty_legacy.0001 # Gain a random skill point
	}
}

on_dragon_birthday = {
	trigger = {
		has_trait = dragon
	}
	effect = {
		agot_apply_dragon_aging_effect = { DRAGON = this }
		# For keeping track of dragon age as var - used to modify genes correctly
		# Update Story Cycle
		if = {
			limit = {
				agot_ruins_has_dragon_storage_system = yes
			}
			title:c_ruins.holder = {
				every_owned_story = {
					limit = {
						story_type = story_dragon_variable_storage
						var:dragon_id ?= root
					}
					save_scope_as = dragon_var_story_val
				}
			}
			scope:dragon_var_story_val = {
				change_variable = {
					name = dragon_age
					add = 1
				}
			}
		}
	}
	on_actions = {
		on_dragon_specific_birthday
	}
}

# AGOT Added
on_dragon_specific_birthday = {
	first_valid_on_action = { 
		on_dragon_3rd_birthday
		on_dragon_14th_birthday
	}
}

# AGOT Added
on_dragon_3rd_birthday = {
	trigger = {
		age = 3
		has_trait = dragon
	}
	events = {
		agot_dragon_pits.1300
	}
}

on_dragon_14th_birthday = {
	trigger = {
		age = 14
		has_trait = dragon
	}
	events = {
		agot_dragon_maintenance.0001 # Assign Education Trait
	}
}