# called for the newborn child
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:father is the family father (not the biological)
# scope:real_father is the biological father
# scope:is_bastard, true if a known bastard
on_birth_child = {
	events = {
		middle_east_decisions.0012 #Saoshyant Descendants.
		major_decisions.0101 #Divine/Consecrated Blood Descendants.
		birth.8001 #Reincarnation
		birth.8011 #Born in the Purple
		trait_specific.0500 #To handle genetic traits not expressed until later in life
		debug.0010 # Disease data tracking
	}
	effect = {
		# Make sure that children aren't Lowborn if one of their parents are, in fact, of noble stock
		if = {
			limit = {
				exists = real_father.house
				scope:real_father.house = { 
					has_house_modifier = pcm_house_super_modifier					
				}
			}
			add_trait = super
		}
			
		
		if = {
			limit = {
				scope:is_bastard = no
				scope:child = {
					is_lowborn = yes
				}
				OR = {
					AND = {
						exists = scope:father
						scope:father = {
							is_lowborn = no
						}
					}
					scope:mother = {
						is_lowborn = no
					}
				}
			}
			if = {
				limit = {
					NOT = {
						exists = scope:father
					}
					exists = mother.house
				}
				set_house = mother.house
			}
			else_if = {
				limit = {
					exists = scope:father
					exists = mother.house
					scope:father = {
						OR = {
							AND = {
								is_spouse_of = scope:mother
								matrilinear_marriage = yes
							}
							is_concubine_of = scope:mother
						}
					}
				}
				set_house = mother.house
			}
			else_if = {
				limit = {
					exists = scope:father
					exists = father.house
				}
				set_house = father.house
			}
			else = {
				set_house = mother.house
			}
		}
	
		#Add bastardy traits and set correct House
		if = {
			limit = {
				scope:is_bastard = yes
			}
			add_bastard_trait_based_on_faith_effect = yes

			if = {
				limit = {
					scope:mother = {
						has_character_flag = pregnancy_real_father_of_bastard_is_known_flag
					}
				}
				#Set father and house
				set_father = scope:real_father
			}
			set_parent_house_effect = yes

			#Set conditions for any twins
			scope:mother = {
				every_child = {
					limit = {
						is_twin_of = scope:child
					}
					add_bastard_trait_based_on_faith_effect = yes
					if = {
						limit = {
							scope:mother = {
								has_character_flag = pregnancy_real_father_of_bastard_is_known_flag
							}
						}
						#Set father and house
						set_father = scope:real_father
					}
					set_parent_house_effect = yes
				}
			}
		}

		#Add house hook
		if = {
			limit = {
				is_alive = yes
				exists = scope:child.house
				scope:child.house.house_head = { is_alive =yes }
				NOT = {
					scope:child = scope:child.house.house_head # If the house dies out, a posthumous child can revive it
				}
				#No need to check if the hook can be added, the kid was just born
			}
			scope:child.house.house_head = {
				add_hook_no_toast = {
					type = house_head_hook
					target = scope:child
				}
			}
		}

		if = {
			limit = {
				exists = scope:father
				exists = scope:mother
				scope:mother = { is_concubine_of = scope:father }
			}
			add_trait = child_of_concubine_female
		}
		if = {
			limit = {
				exists = scope:father
				exists = scope:mother
				scope:father = { is_concubine_of = scope:mother }
			}
			add_trait = child_of_concubine_male
		}

		hidden_effect = {
			every_parent = {
				limit = { has_perk = sound_foundations_perk	}
				sound_foundations_skill_gain_effect = yes
			}
		}

		# Blood legacy 4 extra genetic trait chance effect
		hidden_effect = {
			if = {
				limit = {
					exists = scope:child.dynasty
					scope:child.dynasty = { has_dynasty_perk = blood_legacy_4 }
					OR = { # The child must have a real parent of the Dynasty...
						scope:mother.dynasty = scope:child.dynasty
						scope:real_father.dynasty = scope:child.dynasty
					}
				}
				if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_beauty_good_1
						}
						NOR = {
							has_trait = beauty_good
							has_trait = beauty_bad
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = beauty_good_1
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_intellect_good_1
						}
						NOR = {
							has_trait = intellect_good
							has_trait = intellect_bad
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = intellect_good_1
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_physique_good_1
						}
						NOR = {
							has_trait = physique_good
							has_trait = physique_bad
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = physique_good_1
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_fecund
						}
						NOR = {
							has_trait = infertile
							has_trait = fecund
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = fecund
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_giant
						}
						NOR = {
							has_trait = giant
							has_trait = dwarf
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = giant
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_dwarf
						}
						NOR = {
							has_trait = giant
							has_trait = dwarf
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = dwarf
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_albino
						}
						NOT = {
							has_trait = albino
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = albino
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_scaly
						}
						NOT = {
							has_trait = scaly
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = scaly
					}
				}
			}
		}

		hidden_effect = { # Track El Cid's Lineage (FP2) for achievement
			if = {
				limit = { 
					OR = {
						scope:real_father = { has_character_flag = fp2_el_cid_blood_relation_legacy }
						scope:mother = { has_character_flag = fp2_el_cid_blood_relation_legacy }
					}
				}				
				scope:child = { add_character_flag = fp2_el_cid_blood_relation_legacy }
			}
		}
	}
}