﻿# On actions for child birth and pregnancy notification events, triggered automatically by the game

# called for the mother
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:real_father is the biological father
# scope:father is the family father (not the biological)
on_birth_mother = {
	#AGOT Disabled
	# trigger = {
	# 	NOT = { exists = var:historical_pregnancy }
	# }
	events = {
		birth.0001 #No one dies - hidden event that selects visible birth event based on bastard status and complication rolls
		birth.3011 #Mother dies
		#AGOT Added
		agot_birth_events.0002 #Child born a scaly stillbirth
		agot_birth_events.0100 #One stillborn, one live twin
		agot_realm.0040
	}
	effect = {
		#AGOT Added, handle canon child birth
		scope:mother = {
			if = {
				limit = { has_character_flag = agot_is_pregnant }
				scope:child = { agot_canon_children_birth_effect = yes }
			}
		}

		scope:mother = {
			if = {
				limit = {
					is_playable_character = yes
					exists = scope:mother.location
					exists = scope:child.location
					scope:mother.location = scope:child.location
				}
				trigger_event = {
					id = bp2_yearly.4000
 	  				days = 3
				}
				# If you have a legend maybe we change it?
				if = {
					limit = {
						has_legend_chapter_trigger = { CHAPTER = motivation }
						NOT = {
							promoted_legend ?= {
								has_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_mother
								}
							}
						}
					}
					random = {
						chance = 10
						send_interface_toast = {
							title = legend_chapter_change_toast
							left_icon = scope:mother
							right_icon = scope:child
							promoted_legend = {
								set_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_mother
								}
							}
						}
					}
				}
			}
		}

		random = { # legend_spread_events.8170 follow-up
			chance = 10
			scope:mother = {
				trigger_event = {
					id = legend_spread_events.8171
					months = 3
				}
			}
			scope:father ?= {
				trigger_event = {
					id = legend_spread_events.8171
					months = 3
				}
			}
		}

		scope:mother.current_travel_plan ?= { 
			if = {
				limit = {
					travel_plan_owner = { is_ruler = yes }
				}
				add_companion = scope:child
			}
		}

		# Gain piety from Sacred Childbirth doctrine
		if = {
			limit = {
				faith = { has_doctrine_parameter = piety_from_childbirth_active }
			}
			send_interface_toast = {
				type = event_generic_neutral
				title = piety_from_childbirth_toast
				left_icon = scope:child
				right_icon = scope:mother
				if = {
					limit = {
						scope:child = { has_any_negative_bastard_trait_trigger = no }
					}
					add_piety = {
						value = major_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = major_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = major_piety_gain
						}
					}
				}
				else = {
					add_piety = {
						value = minor_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = minor_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = minor_piety_gain
						}
					}
				}
			}
		}

		scope:child = {
			if = {
				limit = { has_trait = twin }
				random_sibling = {
					limit = { is_twin_of = scope:child }
					save_scope_as = child_2
				}
			}
		}

		#Gain Piety from the Mandala Creator Aspect
		if = {
			limit = {
				government_has_flag = government_is_mandala
				house ?= { 
					has_house_aspiration_parameter = births_give_piety
					this = scope:child.house
				}
				is_landed = yes
			}
			if = {
				limit = { exists = scope:child_2 }
				send_interface_message = {
					type = msg_mandala_creator_birth_good
					title = mandala_creator_children_born
					left_icon = scope:child
					right_icon = scope:child_2
					add_piety = {
						value = major_piety_gain
						multiply = 2
						if = {
							limit = {
								any_child = {
									count > 2
									is_twin_of = scope:child
								}
							}
							add = major_piety_gain
						}
					}
				}
			}
			else = {
				send_interface_message = {
					type = msg_mandala_creator_birth_good
					title = mandala_creator_child_born
					left_icon = scope:child
					right_icon = scope:mother
					add_piety = major_piety_gain
				}
			}
		}

		if = {
			limit = {
				NOR = {
					any_child = {
						even_if_dead = yes
						this != scope:child
					}
					exists = scope:child_2
				}
			}
			create_character_memory = {
				type = first_born
				participants = {
					child = scope:child
				}
			}
		}
		else = {
			if = {
				limit = { exists = scope:child_2 }
				create_character_memory = {
					type = twins_born
					participants = {
						child = scope:child
						child_2 = scope:child_2
					}
				}
			}
			else = {
				create_character_memory = {
					type = child_born
					participants = {
						child = scope:child
					}
				}
			}
		}

		scope:mother = {
			if = {
				limit = {
					has_character_modifier = oath_of_children
				}
				hidden_effect = {
					remove_character_modifier = oath_of_children
					add_character_modifier = oath_of_children
				}
			}
		}
		#AGOT Disabled
		# if = {
		# 	limit = {
		# 		title:h_china.holder ?= {
		# 			OR = {
		# 				scope:mother.top_liege ?= this
		# 				scope:father.top_liege ?= this
		# 			}
		# 		}
		# 		OR = {
		# 			scope:mother.location.county ?= {
		# 				holder = {
		# 					OR = {
		# 						scope:mother = this
		# 						scope:father ?= this
		# 					}
		# 					has_treasury = yes
		# 				}
		# 				has_county_modifier = tgp_minister_project_conduct_census_fertility_effort_modifier
		# 			}
		# 			scope:father.location.county ?= {
		# 				holder = {
		# 					OR = {
		# 						scope:mother ?= this
		# 						scope:father = this
		# 					}
		# 					has_treasury = yes
		# 				}
		# 				has_county_modifier = tgp_minister_project_conduct_census_fertility_effort_modifier
		# 			}
		# 		}
		# 	}
		# 	scope:mother = {
		# 		send_interface_message = {
		# 			type = event_toast_effect_good
		# 			title = great_project_fertility_effort_received_t
		# 			left_icon = title:e_minister_of_revenue
		# 			right_icon = scope:child
		# 			if = {
		# 				limit = {
		# 					title:e_minister_of_revenue.holder ?= {
		# 						this != scope:mother
		# 						this != scope:father
		# 						treasury > conduct_census_fertility_effort_payout_value
		# 					}
		# 				}
		# 				title:e_minister_of_revenue.holder = {
		# 					save_scope_as = minister
		# 				}
		# 			}
		# 			else_if = {
		# 				limit = {
		# 					any_liege_or_above = {
		# 						this != scope:mother
		# 						this != scope:father
		# 						is_ai = yes
		# 						treasury > conduct_census_fertility_effort_payout_value
		# 					}
		# 				}
		# 				ordered_liege_or_above = {
		# 					order_by = {
		# 						value = highest_held_title_tier
		# 						multiply = -1
		# 					}
		# 					limit = {
		# 						this != scope:mother
		# 						this != scope:father
		# 						is_ai = yes
		# 						treasury > conduct_census_fertility_effort_payout_value
		# 					}
		# 					save_scope_as = minister
		# 				}
		# 			}
		# 			else = {
		# 				if = {
		# 					limit = {
		# 						has_treasury = yes
		# 					}
		# 					add_treasury = conduct_census_fertility_effort_payout_value
		# 				}
		# 				else = {
		# 					scope:father ?= {
		# 						add_treasury = conduct_census_fertility_effort_payout_value
		# 					}
		# 				}
		# 			}
		# 			scope:minister ?= {
		# 				send_interface_message = {
		# 					type = event_toast_effect_good
		# 					title = great_project_fertility_effort_paid_t
		# 					left_icon = title:e_minister_of_revenue
		# 					right_icon = scope:child
		# 					if = {
		# 						limit = {
		# 							scope:mother = {
		# 								has_treasury = yes
		# 							}
		# 						}
		# 						pay_short_term_treasury = {
		# 							target = scope:mother
		# 							treasury = conduct_census_fertility_effort_payout_value
		# 						}
		# 						scope:mother.location.county = {
		# 							remove_county_modifier = tgp_minister_project_conduct_census_fertility_effort_modifier
		# 						}
		# 					}
		# 					else = {
		# 						pay_short_term_treasury = {
		# 							target = scope:father
		# 							treasury = conduct_census_fertility_effort_payout_value
		# 						}
		# 						scope:father.location.county ?= {
		# 							remove_county_modifier = tgp_minister_project_conduct_census_fertility_effort_modifier
		# 						}
		# 					}
		# 				}
		# 			}
		# 		}
		# 	}
		# 	scope:father ?= {
		# 		send_interface_message = {
		# 			type = event_toast_effect_good
		# 			title = great_project_fertility_effort_received_t
		# 			left_icon = title:e_minister_of_revenue
		# 			right_icon = scope:child
		# 			show_as_tooltip = {
		# 				if = {
		# 					limit = {
		# 						exists = scope:minister
		# 					}
		# 					scope:minister = {
		# 						if = {
		# 							limit = {
		# 								scope:mother = {
		# 									has_treasury = yes
		# 								}
		# 							}
		# 							pay_short_term_treasury = {
		# 								target = scope:mother
		# 								treasury = conduct_census_fertility_effort_payout_value
		# 							}
		# 						}
		# 						else = {
		# 							pay_short_term_treasury = {
		# 								target = scope:father
		# 								treasury = conduct_census_fertility_effort_payout_value
		# 							}
		# 						}
		# 					}
		# 				}
		# 				else = {
		# 					if = {
		# 						limit = {
		# 							scope:mother = {
		# 								has_treasury = yes
		# 							}
		# 						}
		# 						scope:mother = {
		# 							add_treasury = conduct_census_fertility_effort_payout_value
		# 							location.county = {
		# 								remove_county_modifier = tgp_minister_project_conduct_census_fertility_effort_modifier
		# 							}
		# 						}
		# 					}
		# 					else = {
		# 						add_treasury = conduct_census_fertility_effort_payout_value	
		# 						location.county = {
		# 							remove_county_modifier = tgp_minister_project_conduct_census_fertility_effort_modifier
		# 						}
		# 					}
		# 				}
		# 			}
		# 		}
		# 	}
		# }
	}
}

# called for the father
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:real_father is the biological father
# scope:father is the family father (not the biological)
on_birth_father = {
	events = {
		#AGOT Added
		agot_realm.0040
	}
	effect = {
		scope:father = {
			if = {
				limit = {
					is_playable_character = yes
					exists = scope:father.location
					exists = scope:child.location
					scope:father.location = scope:child.location
				}
				trigger_event = {
					id = bp2_yearly.4000
 	  				days = 3
				}
				# If you have a legend maybe we change it?
				if = {
					limit = {
						has_legend_chapter_trigger = { CHAPTER = motivation }
						NOT = {
							promoted_legend ?= {
								has_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_father
								}
							}
						}
					}
					random = {
						chance = 10
						send_interface_toast = {
							title = legend_chapter_change_toast
							left_icon = scope:father
							right_icon = scope:child
							promoted_legend = {
								set_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_father
								}
							}
						}
					}
				}
			}
		}

		# Gain piety from Sacred Childbirth doctrine
		if = {
			limit = {
				faith = { has_doctrine_parameter = piety_from_childbirth_active }
			}
			send_interface_toast = {
				type = event_generic_neutral
				title = piety_from_childbirth_toast
				left_icon = scope:child
				right_icon = scope:mother
				if = {
					limit = {
						scope:child = { has_any_negative_bastard_trait_trigger = no }
					}
					add_piety = {
						value = major_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = major_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = major_piety_gain
						}
					}
				}
				else = {
					add_piety = {
						value = minor_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = minor_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = minor_piety_gain
						}
					}
				}
			}
		}

		scope:child = {
			if = {
				limit = { has_trait = twin }
				random_sibling = {
					limit = { is_twin_of = scope:child }
					save_scope_as = child_2
				}
				
			}
		}

		#Gain Piety from the Mandala Creator Aspect
		if = {
			limit = {
				government_has_flag = government_is_mandala
				house ?= { 
					has_house_aspiration_parameter = births_give_piety
					this = scope:child.house
				}
				is_landed = yes
			}
			if = {
				limit = { exists = scope:child_2 }
				send_interface_message = {
					type = msg_mandala_creator_birth_good
					title = mandala_creator_children_born
					left_icon = scope:child
					right_icon = scope:child_2
					add_piety = {
						value = major_piety_gain
						multiply = 2
						if = {
							limit = {
								any_child = {
									count > 2
									is_twin_of = scope:child
								}
							}
							add = major_piety_gain
						}
					}
				}
			}
			else = {
				send_interface_message = {
					type = msg_mandala_creator_birth_good
					title = mandala_creator_child_born
					left_icon = scope:child
					right_icon = scope:father
					add_piety = major_piety_gain
				}
			}
		}
		
		if = {
			limit = {
				NOR = {
					any_child = {
						even_if_dead = yes
						this != scope:child
					}
					exists = scope:child_2
				}
			}
			create_character_memory = {
				type = first_born
				participants = {
					child = scope:child
				}
			}
		}
		else = {
			if = {
				limit = { exists = scope:child_2 }
				create_character_memory = {
					type = twins_born
					participants = {
						child = scope:child
						child_2 = scope:child_2
					}
				}
			}
			else = {
				create_character_memory = {
					type = child_born
					participants = {
						child = scope:child
					}
				}
			}
		}

		scope:father = {
			if = {
				limit = {
					has_character_modifier = oath_of_children
				}
				hidden_effect = {
					remove_character_modifier = oath_of_children
					add_character_modifier = oath_of_children
				}
			}
		}
	}
}

# called for the biological father
# only called if father and real father are different
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:real_father is the biological father
# scope:father is the family father (not the biological)
on_birth_real_father = {
	events = {

	}
}

# 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 = {
		#AGOT Disabled
		#middle_east_decisions.0012 #Saoshyant Descendants.
		major_decisions.0101 #Divine/Consecrated Blood Descendants.
		birth.8001 #Reincarnation
		#AGOT Disabled
		#birth.8011 #Born in the Purple
		trait_specific.0500 #To handle genetic traits not expressed until later in life
		debug.0010 # Disease data tracking
		#AGOT Added
		delay = { days = 1 }
		agot_dragon_egg_cradling.2010
		season_flavor_events.00015 #Nicknames for Seasons of Ice and Fire
	}
	effect = {
		#AGOT Added, fraternal twin handling
		if = {
			limit = {
				scope:mother = {
					OR = {
						has_character_flag = needs_twin_one_female
						has_character_flag = needs_twin_one_male
					}
				}
			}
			if = {
				limit = {
					scope:mother = { has_character_flag = needs_twin_one_female }
					is_female = yes
				}
				scope:mother = {
					set_variable = {
						name = twin_one
						value = root
					}
					remove_character_flag = needs_twin_one_female
				}
			}
			else_if = {
				limit = {
					scope:mother = { has_character_flag = needs_twin_one_male }
					is_female = no
				}
				scope:mother = {
					set_variable = {
						name = twin_one
						value = root
					}
					remove_character_flag = needs_twin_one_male
				}
			}
		}
		else_if = {
			limit = {
				scope:mother = {
					OR = {
						has_character_flag = needs_twin_two_female
						has_character_flag = needs_twin_two_male
					}
				}
			}
			if = {
				limit = {
					scope:mother = { has_character_flag = needs_twin_two_female }
					is_female = yes
				}
				scope:mother = {
					set_variable = {
						name = twin_two
						value = root
					}
					remove_character_flag = needs_twin_two_female
				}
			}
			else_if = {
				limit = {
					scope:mother = { has_character_flag = needs_twin_two_male }
					is_female = no
				}
				scope:mother = {
					set_variable = {
						name = twin_two
						value = root
					}
					remove_character_flag = needs_twin_two_male
				}
			}
		}

		# MPO: Nomadic Philosophy Inheritance
		if = {
			limit = {
				NOT = { has_trait = nomadic_philosophy }
				OR = {
					scope:mother = {
						has_trait = nomadic_philosophy
						NOT = { has_character_flag = nomphil_exodus_first_gen }
					}
					scope:father ?= {
						has_trait = nomadic_philosophy
						NOT = { has_character_flag = nomphil_exodus_first_gen }
					}
				}
			}
			add_trait = nomadic_philosophy
			if = {
				limit = {
					NOR = {
						location.county ?= {
							any_county_situation = { this = situation:the_great_steppe }
						}
						top_liege ?= {
							government_has_flag = government_is_nomadic
							government_has_flag = government_is_steppe_admin
						}
					}
				}
				add_character_flag = nomphil_exodus_first_gen
			}
		}
		if = {
			limit = {
				scope:mother = {
					has_character_flag = blessed_baby
				}
			}
			random_list = {
				23 = {
					add_trait = intellect_good_2
				}
				10 = {
					add_trait = intellect_good_3
				}
				23 = {
					add_trait = physique_good_2
				}
				10 = {
					add_trait = physique_good_3
				}
				23 = {
					add_trait = beauty_good_2
				}			
				10 = {
					add_trait = beauty_good_3
				}
			}
		}

		#AGOT Added
		# A bastard of the High Septon should be set to the mothers house if there is one
		if = {
			limit = { scope:father ?= { house ?= house:house_Most_Devout } }
			if = {
				limit = { scope:mother = { is_lowborn = no } }
				set_house = mother.house
			}
			else = { set_to_lowborn = yes }
		}

		# Make sure that children aren't Lowborn if one of their parents are, in fact, of noble stock
		if = {
			limit = {
				OR = {
					scope:is_bastard = no
					faith = { has_doctrine_parameter = bastards_none }
				}
				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
					#AGOT Added
					NOT = { mother.house = house:house_Most_Devout }
					NOT = { mother = { has_trait = bastard } }
				}
				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
						}
					}
					#AGOT Added
					NOT = { mother.house = house:house_Most_Devout }
					NOT = { mother = { has_trait = bastard } }
				}
				set_house = mother.house
			}
			else_if = {
				limit = {
					exists = scope:father
					exists = father.house
					#AGOT Added
					NOT = { father.house = house:house_Most_Devout }
					NOT = { father = { has_trait = bastard } }
				}
				set_house = father.house
			}
			#AGOT Modified
			# else = {
			# 	set_house = mother.house
			# }
			else_if = {
				limit = {
					NOT = { mother.house = house:house_Most_Devout }
					NOT = { mother = { has_trait = bastard } }
				}
				set_house = mother.house
			}
		}

		#AGOT Added, Make sure children are lowborn if both their parents are bastards
		if = {
			limit = {
				scope:mother = {
					has_trait = bastard
				}
				trigger_if = {
					limit = { exists = scope:father }
					scope:father = {
						has_trait = bastard
					}
				}
			}
			set_to_lowborn = yes
		}

		#AGOT Added, no NW babies
		if = {
			limit = {
				liege_or_court_owner = { has_trait = nightswatch }
			}
			if = {
				limit = {
					exists = scope:mother.liege_or_court_owner
				}
				scope:mother.liege_or_court_owner = { add_courtier ?= scope:child }
			}
			else_if = {
				limit = {
					exists = scope:mother.location
				}
				move_to_pool_at = scope:mother.location
			}
			else = {
				move_to_pool_at = title:c_winterfell.holder.capital_province
			}
		}

		#AGOT Added - Set Birthplace
		hidden_effect = {
			if = {
				limit = {
					is_alive = yes
				}
				set_variable = {
					name = agot_birthplace
					value = location
				}
				if = {
					limit = { has_trait = twin }
					random_sibling = {
						limit = { is_twin_of = scope:child }
						save_scope_as = child_2
						scope:child_2 = {
							if = {
								limit = { has_variable = agot_birthplace }
								remove_variable = agot_birthplace

								set_variable = {
									name = agot_birthplace
									value = scope:child.var:agot_birthplace
								}
							}
						}
					}
				}
			}
		}

		#AGOT Added, Set Bestow Princely Titles
		agot_on_birth_bestow_scion_status_effect = { CHILD = scope:child }

		#AGOT Added, Learn Languages
		agot_learn_appropiate_languages = yes

		#Add bastardy traits and set correct House
		if = {
			limit = {
				scope:is_bastard = yes
			}
			add_bastard_trait_based_on_faith_effect = yes

			#AGOT Added, Bastard Surnames
			if = {
				limit = {
					exists = var:agot_birthplace
				}
				agot_add_birthplace_bastard_nickname_effect = {
					BIRTHPLACE = var:agot_birthplace
				}
			}
			else = {
				agot_add_birthplace_bastard_nickname_effect = {
					BIRTHPLACE = location
				}
			}

			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

					#AGOT Added, Bastard Surnames
					if = {
						limit = {
							exists = scope:child.var:agot_birthplace
						}
						agot_add_birthplace_bastard_nickname_effect = {
							BIRTHPLACE = scope:child.var:agot_birthplace
						}
					}
					else = {
						agot_add_birthplace_bastard_nickname_effect = {
							BIRTHPLACE = scope:child.location
						}
					}

					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
				}
			}
		}

		if = {
			limit = {
				is_alive = yes
				faith = {
					has_doctrine_parameter = parents_get_hooks_on_children
				}
			}
			if = {
				limit = {
					scope:child.father ?= {
						is_alive = yes
						faith = {
							has_doctrine_parameter = parents_get_hooks_on_children
						}						
					}
				}
				scope:child.father = {
					add_hook = {
						target = scope:child
						type = filial_piety_hook
					}
				}
			}
			if = {
				limit = {
					scope:child.mother ?= {
						is_alive = yes
						faith = {
							has_doctrine = tenet_filial_piety
						}						
					}
				}
				scope:child.mother = {
					add_hook = {
						target = scope:child
						type = filial_piety_hook
					}
				}
			}
		}

		#Add house hook
		if = {
			limit = {
				is_alive = yes
				exists = scope:child.house
				scope:child.house.house_head = { 
					is_alive = yes
					can_add_hook = {
						target = scope:child
						type = house_head_hook
					}
				}
				NOT = {
					scope:child = scope:child.house.house_head # If the house dies out, a posthumous child can revive it
				}
			}
			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
		}
		#create wet_nurse and nursed_child relation between the newborn and wet nurse court position character
		if = {
			limit = {
				scope:child.liege ?= {
					any_courtier = {
						has_court_position = wet_nurse_court_position
						save_temporary_scope_as = wet_nurse
					}
				}
				NOT = { any_relation = { type = wet_nurse } }
			}
			set_relation_wet_nurse = scope:wet_nurse
		}
		# Born hostages should be released if their dynasty is the same as their warden
		if = {
			limit = {
				is_hostage = yes
				dynasty = warden.dynasty
			}
			save_scope_as = unhostaged_child
			warden = { save_scope_as = warden }
			return_hostage = yes
			scope:warden = { add_courtier = root }
		}
		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
					}
				}
			}
		}

		#AGOT Disabled
		# 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 }
		# 	}
		# }
		#Enters into blood brotherhood family bond
		scope:child = {
			if = {
				limit = {
					any_parent = {
						has_blood_brother = yes
						any_relation = {
							type = blood_brother
							NOT = {
								is_close_family_of = scope:child
							}
						}
					}
				}
				every_parent = {
					limit = {
						has_blood_brother = yes
						any_relation = {
							type = blood_brother
							NOT = {
								is_close_family_of = scope:child
							}
						}	
					}
					random_relation = {
						type = blood_brother
						save_scope_as = other_blood_brother
					}
				}
				mpo_blood_brother_new_family_member_opinion_effect = yes
			}
		}
		#AGOT Disabled
		# if = {
		# 	limit = {
		# 		any_parent ?= {
		# 			culture = {
		# 				has_cultural_tradition = tradition_stratified_society
		# 			}
		# 			merit_level >= 5
		# 		}
		# 		is_faith_dominant_gender = yes
		# 	}
		# 	change_merit_level = 1
		# 	if = {
		# 		limit = {
		# 			NOR = { 
		# 				has_character_modifier = tgp_passed_provincial_exam_modifier
		# 				has_character_modifier = tgp_bypassed_provincial_exam_modifier
		# 				has_character_modifier = tgp_nepotism_bypass_exams_modifier
		# 			}
		# 		}
		# 		add_character_modifier = {
		# 			modifier = tgp_nepotism_bypass_exams_modifier
		# 		}
		# 	}
		# }

		#AGOT Added
		agot_birth_effects = yes
	}
}

# called for the mother when a pregnancy reaches the "revealed" status
# scope:mother is the pregnant mother
# scope:father is the assumed father
# scope:real_father is the biological father
on_pregnancy_mother = {
	events = {
		pregnancy.0001 # Universal hidden maintenance event
		pregnancy.1001 # Mother gives birth to current husband's child.
		pregnancy.1101 # Mother gives birth to ex-husband's child.
		pregnancy.2001 # Unwed mother gives birth to bastard child.
		pregnancy.2002 # Married mother gives birth to bastard child.
		pregnancy.2050 # Married mother in same-sex relationship.
	}
}

# called for the father when a pregnancy reaches the "revealed" status
# scope:mother is the pregnant mother
# scope:father is the assumed father
# scope:real_father is the biological father
on_pregnancy_father = {
	events = {
	
	}
}

# called for the mother when a pregnancy ends prematurely
# scope:mother is the pregnant mother
# scope:father is the assumed father
# scope:real_father is the biological father
on_pregnancy_ended_mother = {
	events = {
	
	}
}
