﻿namespace = historical_figure_vanilla_birth

#BIRTH: Mother: regular birth
historical_figure_vanilla_birth.1001 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:child = {
						NOT = { has_trait = twin }
						NAND = {
							exists = root.player_heir
							this = root.player_heir
						}
					}
				}
				desc = birth.1001.t
			}
			triggered_desc = {
				trigger = {
					scope:child = {
						NOT = { has_trait = twin }
						AND = {
							exists = root.player_heir
							this = root.player_heir
						}
					}
				}
				desc = birth.1001.heir.t
			}
			triggered_desc = {
				trigger = {
					scope:child = {
						has_trait = twin
						is_female = yes
					}
					scope:child_2 = { is_female = yes }
				}
				desc = birth.1001.daughters.t
			}
			triggered_desc = {
				trigger = {
					scope:child = {
						has_trait = twin
						is_male = yes
					}
					scope:child_2 = { is_male = yes }
				}
				desc = birth.1001.sons.t
			}
			desc = birth.1001.children.t
		}
	}
	desc = {
		#How do I feel?
		random_valid = {
			triggered_desc = { #First child and it was cool
				trigger = {
					NOT = {
						any_child = {
							even_if_dead = yes
							NOR = {
								this = scope:child
								is_twin_of = scope:child
							}
						}
					}
					NOT = { has_trait = craven }
				}
				desc = birth.1001.first_birth_good.desc
			}
			triggered_desc = { #First child and it was scary
				trigger = {
					NOT = {
						any_child = {
							even_if_dead = yes
							NOR = {
								this = scope:child
								is_twin_of = scope:child
							}
						}
					}
					NOT = { has_trait = brave }
				}
				desc = birth.1001.first_birth_scary.desc
			}
			triggered_desc = { #Had a lot of babies
				trigger = {
					any_child = {
						even_if_dead = yes
						count >= 5
					}
				}
				desc = birth.1001.many_births.desc
			}
			triggered_desc = {
				trigger = {
					any_child = {
						even_if_dead = yes
						NOR = {
							this = scope:child
							is_twin_of = scope:child
						}
					}
				}
				desc = birth.1001.not_first_birth.desc
			}
		}
		#What did you get?
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:child = {
						NOT = { has_trait = twin }
					}
				}
				desc = birth.1001.sondaughter.desc
			}
			triggered_desc = {
				trigger = {
					scope:child = {
						has_trait = twin
						is_female = yes
					}
					scope:child_2 = { is_female = yes }
				}
				desc = birth.1001.daughters.desc
			}
			triggered_desc = {
				trigger = {
					scope:child = {
						has_trait = twin
						is_male = yes
					}
					scope:child_2 = { is_male = yes }
				}
				desc = birth.1001.sons.desc
			}
			desc = birth.1001.children.desc
		}
		#Conclusion
		first_valid = {
			# Mystical Birthright
			triggered_desc = {
				trigger = {
					scope:child = { has_character_flag = born_as_taltos }
					faith = scope:child.faith
				}
				desc = birth.1001.taltos.desc
			}
			triggered_desc = {
				trigger = {
					scope:child = { has_character_flag = born_as_taltos }
					NOT = {faith = scope:child.faith}
				}
				desc = birth.1001.taltos_unfaithful.desc
			}
			# Twins
			triggered_desc = {
				trigger = {
					scope:child = { has_trait = twin }
					exists = root.player_heir
					OR = {
						scope:child = root.player_heir
						scope:child_2 = root.player_heir
					}
				}
				desc = birth.1001.twins_heir.desc
			}
			triggered_desc = {
				trigger = {
					scope:child = { has_trait = twin }
				}
				desc = birth.1001.twins.desc
			}
			# Heir
			triggered_desc = {
				trigger = {
					exists = root.player_heir
					scope:child = root.player_heir
					NOT = { sex_opposite_of = scope:child }
				}
				desc = birth.1001.same_gender_heir.desc
			}
			triggered_desc = {
				trigger = {
					exists = root.player_heir
					scope:child = root.player_heir
				}
				desc = birth.1001.other_gender_heir.desc
			}
			# Nothing Special
			triggered_desc = {
				desc = birth.1001.end_fallback.desc
			}
		}
	}
	theme = pregnancy
	override_background = { reference = bedchamber }
	left_portrait = {
		character = root
		animation = newborn
		outfit_tags = { nightgown }
	}
	right_portrait = {
		trigger = { exists = scope:second_adult }
		character = scope:second_adult
		animation = newborn
	}
	lower_left_portrait =  scope:child
	lower_center_portrait =  scope:child_2
	lower_right_portrait =  scope:father

#	trigger = {
#		root = scope:mother
#		scope:child.father = scope:child.real_father
#		scope:child = {
#			NOT = { has_trait = bastard }
#			NOT = { has_trait = legitimized_bastard }
#		}
#		NOT = {
#			any_secret = {
#				secret_type = secret_disputed_heritage
#				secret_target = {
#					this = scope:child
#				}
#			}
#		}
#		birth_will_happen_as_usual_trigger = yes
#	}

	immediate = {
		play_music_cue_once = { TRACK = mx_cue_birth }
		if = {
			limit = {
				exists = var:dead_spouse
			}
			var:dead_spouse = {
				save_scope_as = father
			}
		}
		else = {
			scope:child.father = {
				save_scope_as = father
			}
		}
		scope:child = {
			hidden_effect = { set_father = scope:father }
			if = {
				limit = { has_trait = twin }
				random_sibling = {
					limit = {
						is_twin_of = scope:child
					}
					save_scope_as = child_2
				}
			}
		}
		if = { #To save the child that the father suspects of illegitimacy
			limit = {
				has_character_flag = father_suspects_this_pregnancy
				scope:father = { is_alive = yes }
			}
			remove_character_flag = father_suspects_this_pregnancy
			scope:father = {
				set_variable = {
					name = suspect_this_child_of_illegitimacy
					value = scope:child
				}
			}
		}
		#Look for another adult to hold the twin
		if = {
			limit = { exists = scope:child_2 }
			#Let's save the court physician if you have one
			if = {
				limit = { 
					court_physician_available_trigger = yes
					any_court_position_holder = {
						type = court_physician_court_position
						has_court_event_flag = no
						is_ai = yes
					}
				}
				save_court_physician_as_effect = { SCOPE_NAME = second_adult }
			}
			#Otherwise look for a relative of the mother
			else_if = {
				limit = {
					scope:mother = {
						any_close_or_extended_family_member = {
							is_in_the_same_court_as = root
							is_adult = yes
							NOR = { 
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:mother }
								this = scope:father 
								this = scope:mother
							}
						}
					}
				}
				scope:mother = {
					random_close_or_extended_family_member = {
						limit = {
							is_in_the_same_court_as = root
							is_adult = yes
							has_any_good_relationship_with_character_trigger = { CHARACTER = scope:mother }
							NOR = { 
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
								this = scope:father 
								this = scope:mother
							}
						}
						alternative_limit = {
							is_in_the_same_court_as = root
							is_adult = yes
							NOR = {
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:mother }
								this = scope:father 
								this = scope:mother
							}
						}
						save_scope_as = second_adult
					}
				}
			}
			#Then a relative of the father
			else_if = {
				limit = {
					scope:father = {
						any_close_or_extended_family_member = {
							is_in_the_same_court_as = root
							is_adult = yes
							is_female = yes
							NOR = { 
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:mother }
								this = scope:father 
								this = scope:mother
							}
						}
					}
				}
				scope:father = {
					random_close_or_extended_family_member = {
						limit = {
							is_in_the_same_court_as = root
							is_adult = yes
							is_female = yes
							has_any_good_relationship_with_character_trigger = { CHARACTER = scope:mother }
							NOR = { 
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
								this = scope:father 
								this = scope:mother
							}
						}
						alternative_limit = {
							is_in_the_same_court_as = root
							is_adult = yes
							is_female = yes
							opinion = {
								target = scope:mother
								value >= 10
							}
							NOR = { 
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
								has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:mother }
								this = scope:father 
								this = scope:mother
							}
						}
						save_scope_as = second_adult
					}
				}
			}
			#In lack of that look for a courtier with good relations
			else_if = {
				limit = {
					any_courtier= {
						is_adult = yes
						is_female = yes
						NOR = { 
							has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
							has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:mother }
							this = scope:father 
							this = scope:mother
						}
					}
				}
				random_courtier = {
					limit = {
						is_adult = yes
						is_female = yes
						has_any_good_relationship_with_character_trigger = { CHARACTER = scope:mother }
						NOR = { 
							has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
							this = scope:father 
							this = scope:mother
						}
					}
					alternative_limit = {
						is_adult = yes
						is_female = yes
						opinion = {
							target = scope:mother
							value >= 10
						}
						NOR = { 
							has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:father }
							has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:mother }
							this = scope:father 
							this = scope:mother
						}
					}
					save_scope_as = second_adult
				}
			}
		}
		# Special effects for Taltos
		scope:child = {
			if = {
				limit = {
					faith = {
						has_doctrine_parameter = mystic_birthright_active
						NOT = { has_variable = recent_taltos_born }
					}
				}
				random_list = {
					# 1% chance of super rare Taltos birth for AI characters
					1 = {
						modifier = { # 5% chance for players
							add = 4
							OR = {
								scope:mother = {
									is_ai = no
								}
								scope:father = {
									is_ai = no
								}
							}
						}

						add_character_flag = born_as_taltos
						add_trait = lifestyle_mystic
						hidden_effect = {
							add_trait_xp = {
								trait = lifestyle_mystic
								value = 100
							}
						}
						faith = {
							set_variable = {
								name = recent_taltos_born
								value = yes
								years = 30
							}
						}
						# Notify other players about the birth of a Taltos.
						every_player = {
							limit = {
								faith = scope:child.faith
								NOR = {
									this = scope:father
									this = scope:mother
								}
							}
							trigger_event = birth.1100
						}
					}
					99 = {
						modifier = { # 5% chance for players
							add = -4
							OR = {
								scope:mother = {
									is_ai = no
								}
								scope:father = {
									is_ai = no
								}
							}
						}
						# 99% chance of normal uninteresting birth
					}

				}
			}
		}
	}


	#NAMING WIDGET
	widgets = {
		widget = {
			is_shown = {
				allow_naming_on_birth_of_child_trigger = { CHILD = scope:child }
			}
			gui = "event_window_widget_name_child"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:child = { save_scope_as = name_character_target } }
		}
		widget = {
			is_shown = {
				allow_naming_on_birth_of_child_trigger = { CHILD = scope:child_2 }
			}
			gui = "event_window_widget_name_child"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:child_2 = { save_scope_as = name_character_target } }
		}
	}
	
	option = {
		name = birth.1001.a
		trigger = { scope:child = { NOT = { has_trait = twin } } }
		add_prestige = minor_prestige_gain
	}

	option = {
		name = birth.1001.b
		trigger = { scope:child = { has_trait = twin } }
		add_prestige = minor_prestige_gain
	}
}
