﻿namespace = historical_figure_birth

# Just before the birth
historical_figure_birth.0001 = {
	hidden = yes

	immediate = {

		# Give historical name

		change_first_name = { template_character = scope:template }

		# Save traits

		historical_figure_clear_trait_without_congenital_trait_effect = yes
		historical_figure_copy_congenital_and_personality_trait_effect = { TEMP = scope:template }
		historical_figure_save_education_trait_effect = { TEMP = scope:template TEMP2 = scope:template2 TEMP3 = scope:template3 }
		historical_figure_save_commander_trait_effect = { TEMP = scope:template TEMP2 = scope:template2 TEMP3 = scope:template3 }
		historical_figure_save_trait_effect = { TEMP = scope:template }

		# Special traits and information

		scope:template = {
			historical_figure_clear_trait_without_special_trait_effect = yes
		}
		copy_traits = scope:template

		historical_figure_add_information_effect = yes

		# Save skills

		historical_figure_save_skill_effect = { TEMP = scope:template TEMP2 = scope:template2 TEMP3 = scope:template3 }

		# Trait adjustment

		if = {
			limit = {
				has_trait = twin
			}
			add_character_flag = historical_figure_is_twin
			remove_trait = twin
		}

		historical_figure_birth_extra_effect = yes

		# Trigger birth event

		scope:father = { save_scope_as = real_father }
		save_scope_value_as = { name = is_bastard value = no }

		scope:child = {
			trigger_event = { on_action = on_birth_child }
			trigger_event = { id = historical_figure_birth.0002 days = 1 }
		}
		scope:mother = {
			trigger_event = historical_figure_vanilla_birth.1001
		}
		scope:father = {
			trigger_event = birth.1003
		}

	}

	after = {
		historical_figure_delete_template_character_effect = yes
	}

}


# After the birth
historical_figure_birth.0002 = {
	hidden = yes

	immediate = {
		if = {
			limit = {
				has_character_flag = historical_figure_is_twin
			}
			add_trait = twin
			remove_character_flag = historical_figure_is_twin
		}
	}

}


# On coming of age
historical_figure_birth.0010 = {
	hidden = yes

	immediate = {

		# Restore historical skills and traits

		if = {
			limit = {
				exists = var:historical_figure_id
			}

			historical_figure_load_trait_effect = yes
			historical_figure_load_education_trait_effect = yes
			historical_figure_temporary_save_trait_effect = yes
			historical_figure_clear_trait_effect = yes
			historical_figure_load_skill_effect = yes
			historical_figure_temporary_load_trait_effect = yes

			historical_figure_coming_of_age_extra_effect = yes

		}

	}

}


# On death
historical_figure_birth.0090 = {
	hidden = yes

	immediate = {

		if = {
			limit = {
				exists = var:historical_figure_id
			}

			remove_list_global_variable = {
				name = historical_figure_list
				target = root
			}
			remove_list_global_variable = {
				name = historical_figure_id_list
				target = var:historical_figure_id
			}
		}

	}

}


# Birth from father
historical_figure_birth.1001 = {
	hidden = yes

	immediate = {

		if = {
			limit = {
				scope:father = { is_alive = no }
			}

			# Birth from parentless if father died
			trigger_event = historical_figure_birth.3001
		}
		else_if = {
			limit = {
				has_game_rule = historical_figure_birth_player_only
				any_player = {
					AND = {
						NOT = { scope:father = this }
						NOT = {
							any_consort = {
								scope:father = this
							}
						}
					}
				}
			}

			# do nothing if the rule is player only
			historical_figure_delete_template_character_effect = yes
		}
		else_if = {
			limit = {
				has_game_rule = historical_figure_birth_ai_only
				any_player = {
					OR = {
						scope:father = this
						any_consort = {
							scope:father = this
						}
					}
				}
			}

			# do nothing if the rule is AI only
			historical_figure_delete_template_character_effect = yes
		}
		else = {

			# Choose mother

			scope:father = {
				if = {
					limit = {
						any_consort = {
							is_pregnant = no
						}
					}
					random_consort = {
						limit = {
							is_pregnant = no
						}
						save_scope_as = mother
					}
					save_scope_value_as = { name = is_bastard value = no }
				}
				else = {
					if = {
						limit = {
							exists = scope:father.location
						}
						create_character = {
							gender = female
							location = scope:father.location
							template = generic_peasant_character
							save_scope_as = mother
						}
						save_scope_value_as = { name = is_bastard value = yes }
					}
					else = {
						random_province = {
							save_scope_as = birthplace_mother
						}
						create_character = {
							gender = female
							location = scope:birthplace_mother
							template = generic_peasant_character
							save_scope_as = mother
						}
						save_scope_value_as = { name = is_bastard value = yes }
					}
				}
			}

			# Determine dynasty

			if = {
				limit = {
					scope:father.primary_title.tier > scope:mother.primary_title.tier
				}
				historical_figure_set_dynasty_house_father_effect = yes
			}
			else_if = {
				limit = {
					scope:father.primary_title.tier < scope:mother.primary_title.tier
				}
				historical_figure_set_dynasty_house_mother_effect = yes
			}
			else = {
				historical_figure_set_dynasty_house_father_effect = yes
			}

			# Find birthplace

			if = {
				limit = {
					exists = scope:father.location
				}
				scope:father.location = { save_scope_as = birthplace }
			}
			else = {
				historical_figure_find_birthplace_effect = { FAITH = scope:faith CULTURE = scope:culture }
			}

			# Create character

			if = {
				limit = {
					exists = scope:house
				}
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty_house = scope:house
					faith = scope:father.faith
					culture = scope:father.culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}
			else_if = {
				limit = {
					exists = scope:dynasty
				}
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty = scope:dynasty
					faith = scope:father.faith
					culture = scope:father.culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}
			else = {
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty = none
					faith = scope:father.faith
					culture = scope:father.culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}

			# General birth procedure

			scope:child = {
				trigger_event = historical_figure_birth.0001
			}

		}

	}

}


# Birth from mother
historical_figure_birth.2001 = {
	hidden = yes

	immediate = {

		if = {
			limit = {
				NOT = { scope:priority = flag:none }
			}

			# do nothing if the father exists
			historical_figure_delete_template_character_effect = yes
		}
		else_if = {
			limit = {
				scope:mother = { is_alive = no }
			}

			# Birth from parentless if mother died
			trigger_event = historical_figure_birth.3001
		}
		else_if = {
			limit = {
				has_game_rule = historical_figure_birth_player_only
				any_player = {
					AND = {
						NOT = { scope:mother = this }
						NOT = {
							any_consort = {
								scope:mother = this
							}
						}
					}
				}
			}

			# do nothing if the rule is player only
			historical_figure_delete_template_character_effect = yes
		}
		else_if = {
			limit = {
				has_game_rule = historical_figure_birth_ai_only
				any_player = {
					OR = {
						scope:mother = this
						any_consort = {
							scope:mother = this
						}
					}
				}
			}

			# do nothing if the rule is AI only
			historical_figure_delete_template_character_effect = yes
		}
		else = {

			# Choose father

			scope:mother = {
				if = {
					limit = {
						any_consort = {
							exists = this
						}
					}
					random_consort = {
						save_scope_as = father
					}
					save_scope_value_as = { name = is_bastard value = no }
				}
				else = {
					if = {
						limit = {
							exists = scope:mother.location
						}
						create_character = {
							gender = male
							location = scope:mother.location
							template = generic_peasant_character
							save_scope_as = father
						}
						save_scope_value_as = { name = is_bastard value = yes }
					}
					else = {
						random_province = {
							save_scope_as = birthplace_father
						}
						create_character = {
							gender = male
							location = scope:birthplace_father
							template = generic_peasant_character
							save_scope_as = father
						}
						save_scope_value_as = { name = is_bastard value = yes }
					}
				}
			}

			# Determine dynasty

			if = {
				limit = {
					scope:father.primary_title.tier > scope:mother.primary_title.tier
				}
				historical_figure_set_dynasty_house_father_effect = yes
			}
			else_if = {
				limit = {
					scope:father.primary_title.tier < scope:mother.primary_title.tier
				}
				historical_figure_set_dynasty_house_mother_effect = yes
			}
			else = {
				historical_figure_set_dynasty_house_mother_effect = yes
			}

			# Find birthplace

			if = {
				limit = {
					exists = scope:mother.location
				}
				scope:mother.location = { save_scope_as = birthplace }
			}
			else = {
				historical_figure_find_birthplace_effect = { FAITH = scope:faith CULTURE = scope:culture }
			}

			# Create character

			if = {
				limit = {
					exists = scope:house
				}
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty_house = scope:house
					faith = scope:mother.faith
					culture = scope:mother.culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}
			else_if = {
				limit = {
					exists = scope:dynasty
				}
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty = scope:dynasty
					faith = scope:mother.faith
					culture = scope:mother.culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}
			else = {
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty = none
					faith = scope:mother.faith
					culture = scope:mother.culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}

			# General birth procedure

			scope:child = {
				trigger_event = historical_figure_birth.0001
			}

		}

	}

}


# Birth from palentless
historical_figure_birth.3001 = {
	hidden = yes

	immediate = {

		if = {
			limit = {
				has_game_rule = historical_figure_birth_player_only
			}

			# do nothing if the rule is player only
			historical_figure_delete_template_character_effect = yes
		}
		else = {

			# Find birthplace

			historical_figure_find_birthplace_effect = { FAITH = scope:faith CULTURE = scope:culture }

			# Create parents

			create_character = {
				gender = male
				faith = scope:faith
				culture = scope:culture
				location = scope:birthplace
				template = generic_peasant_character
				save_scope_as = father
			}

			create_character = {
				gender = female
				faith = scope:faith
				culture = scope:culture
				location = scope:birthplace
				template = generic_peasant_character
				save_scope_as = mother
			}

			save_scope_value_as = { name = is_bastard value = no }

			# Determine dynasty

			if = {
				limit = {
					scope:father.primary_title.tier > scope:mother.primary_title.tier
				}
				historical_figure_set_dynasty_house_father_effect = yes
			}
			else_if = {
				limit = {
					scope:father.primary_title.tier < scope:mother.primary_title.tier
				}
				historical_figure_set_dynasty_house_mother_effect = yes
			}
			else = {
				historical_figure_set_dynasty_house_father_effect = yes
			}

			# Create character

			if = {
				limit = {
					exists = scope:house
				}
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty_house = scope:house
					faith = scope:faith
					culture = scope:culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}
			else_if = {
				limit = {
					exists = scope:dynasty
				}
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty = scope:dynasty
					faith = scope:faith
					culture = scope:culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}
			else = {
				create_character = {
					age = 0
					gender = scope:template
					father = scope:father
					mother = scope:mother
					dynasty = none
					faith = scope:faith
					culture = scope:culture
					location = scope:birthplace
					template_character = scope:template
					save_scope_as = child
				}
			}

			# General birth procedure

			scope:child = {
				trigger_event = historical_figure_birth.0001
			}

		}

	}

}


# Initialization
historical_figure_birth.4001 = {
	hidden = yes

	immediate = {

		# Special trait and information

		historical_figure_add_information_effect = yes

		# Save skill and trait

		if = {
			limit = {
				exists = scope:template
				exists = scope:template2
				exists = scope:template3
			}

			historical_figure_clear_trait_without_congenital_and_personality_trait_effect = yes
			historical_figure_save_education_trait_effect = { TEMP = scope:template TEMP2 = scope:template2 TEMP3 = scope:template3 }
			historical_figure_save_commander_trait_effect = { TEMP = scope:template TEMP2 = scope:template2 TEMP3 = scope:template3 }
			historical_figure_save_skill_effect = { TEMP = scope:template TEMP2 = scope:template2 TEMP3 = scope:template3 }
			historical_figure_save_trait_effect = { TEMP = scope:template }
			historical_figure_delete_template_character_effect = yes
		}

	}

}
