﻿namespace = ansf

scripted_effect check_for_possible_relatives = {
	# first clear any previously existing list
	clear_variable_list = potential_grandparents
	clear_variable_list = potential_piblings
	clear_variable_list = potential_relatives
	
	# first check for potential grandparents
	every_ruler = {
		limit = {
			is_landed_or_landless_administrative = yes
			NOR = {
				is_ai = no
				has_government = mercenary_government
				has_government = republic_government
				has_government = theocracy_government
				has_government = holy_order_government
				has_trait = eunuch
				has_trait = celibate
			}
			# must be same culture or one closely related
			culture = { has_same_culture_heritage = $COMPARE$.culture }
			# unless this is TARGET or the other parent isn't TARGET's exact culture
			# (one of the parents needs to be the player's culture!)
			trigger_if = {
				limit = {
					OR = {
						NOT = { $COMPARE$ = scope:target }
						scope:ansf_family_member = scope:target
						AND = {
							exists = scope:target.father
							scope:ansf_family_member = { is_female = yes }
							NOT = { scope:target.father.culture = scope:target.culture }
						}
						AND = {
							exists = scope:target.mother
							scope:ansf_family_member = { is_male = yes }
							NOT = { scope:target.mother.culture = scope:target.culture }
						}
					}
				}
				culture = $COMPARE$.culture
			}
			# same faith or astray
			faith = {
				OR = {
					this = $COMPARE$.faith
					faith_hostility_level = { target = $COMPARE$.faith value = faith_astray_level }
				}
			}
			# unless this is TARGET or the other parent isn't TARGET's exact faith
			# (one of the parents needs to be the player's faith!)
			trigger_if = {
				limit = {
					OR = {
						NOT = { $COMPARE$ = scope:target }
						scope:ansf_family_member = scope:target
						AND = {
							exists = scope:target.father
							scope:ansf_family_member = { is_female = yes }
							NOT = { scope:target.father.faith = scope:target.faith }
						}
						AND = {
							exists = scope:target.mother
							scope:ansf_family_member = { is_male = yes }
							NOT = { scope:target.mother.faith = scope:target.faith }
						}
					}
				}
				faith = $COMPARE$.faith
			}
			# from a different dynasty than their spouse
			trigger_if = {
				limit = {
					scope:ansf_family_member = { is_female = yes }
					NOT = { scope:ansf_family_member = scope:target }
				}
				NOT = { dynasty = scope:target.father.dynasty }
			}
			# inlaws different dynasties than my family
			trigger_if = {
				limit = {
					NOT = { $COMPARE$ = scope:target }
				}
				NOR = {
					dynasty = scope:target.dynasty
					dynasty = scope:target.father.dynasty
					dynasty = scope:target.mother.dynasty
				}
			}
			# old enough to be the parent's parent
			age_minus_childbirth_start >= scope:ansf_family_member.age
			# for faiths without bastards, already has an heir
			trigger_if = {
				limit = { faith = { has_doctrine_parameter = bastards_none } }
				exists = primary_heir
				primary_heir = {
					is_child_of = prev
					trigger_if = {
						limit = { scope:ansf_family_member = { is_male = yes } }
						is_male = yes
						age > scope:ansf_family_member.age
					}
					trigger_else = {
						OR = {
							is_male = yes
							age > scope:ansf_family_member.age
						}
					}
				}
			}
		}
		root = {
			add_to_variable_list = {
				name = potential_grandparents
				target = prev
			}
		}
		root = {
			add_to_variable_list = {
				name = potential_relatives
				target = prev
			}
		}
	}
	# check the player title's previous holder for a potential grandparent
	if = {
		limit = {
			scope:target = {
				is_landed = yes
				exists = primary_title.previous_holder
				NOT = {
					root = {
						is_target_in_variable_list = {
							name = potential_grandparents
							target = root.primary_title.previous_holder
						}
					}
				}
			}
		}
		scope:target.primary_title.previous_holder = {
			if = {
				limit = {
					NOR = {
						is_ai = no
						has_government = mercenary_government
						has_government = republic_government
						has_government = theocracy_government
						has_government = holy_order_government
						has_trait = eunuch
						has_trait = celibate
					}
					# must be same culture or one closely related
					culture = { has_same_culture_heritage = scope:target.culture }
					# unless this is TARGET or the other parent isn't TARGET's exact culture
					# (one of the parents needs to be the player's culture!)
					trigger_if = {
						limit = {
							OR = {
								NOT = { $COMPARE$ = scope:target }
								scope:ansf_family_member = scope:target
								AND = {
									exists = scope:target.father
									scope:ansf_family_member = { is_female = yes }
									NOT = { scope:target.father.culture = scope:target.culture }
								}
								AND = {
									exists = scope:target.mother
									scope:ansf_family_member = { is_male = yes }
									NOT = { scope:target.mother.culture = scope:target.culture }
								}
							}
						}
						culture = $COMPARE$.culture
					}
					# same faith or astray
					faith = {
						OR = {
							this = scope:target.faith
							faith_hostility_level = { target = scope:target.faith value = faith_astray_level }
						}
					}
					# unless this is TARGET or the other parent isn't TARGET's exact faith
					# (one of the parents needs to be the player's faith!)
					trigger_if = {
						limit = {
							OR = {
								NOT = { $COMPARE$ = scope:target }
								scope:ansf_family_member = scope:target
								AND = {
									exists = scope:target.father
									scope:ansf_family_member = { is_female = yes }
									NOT = { scope:target.father.faith = scope:target.faith }
								}
								AND = {
									exists = scope:target.mother
									scope:ansf_family_member = { is_male = yes }
									NOT = { scope:target.mother.faith = scope:target.faith }
								}
							}
						}
						faith = $COMPARE$.faith
					}
					# from a different dynasty than their spouse
					trigger_if = {
						limit = {
							scope:ansf_family_member = { is_female = yes }
							NOT = { scope:ansf_family_member = scope:target }
						}
						NOT = { dynasty = scope:target.father.dynasty }
					}
					# inlaws different dynasties than my family
					trigger_if = {
						limit = {
							NOT = { $COMPARE$ = scope:target }
						}
						NOR = {
							dynasty = scope:target.dynasty
							dynasty = scope:target.father.dynasty
							dynasty = scope:target.mother.dynasty
						}
					}
					# old enough to be the parent's parent
					age_including_death_minus_childbirth_start >= scope:ansf_family_member.age
					trigger_if = {
						limit = {
							is_playable_character = yes
							faith = { has_doctrine_parameter = bastards_none }
						}
						exists = primary_heir
						primary_heir = {
							is_child_of = prev
							trigger_if = {
								limit = { scope:ansf_family_member = { is_male = yes } }
								is_male = yes
								age > scope:ansf_family_member.age
							}
							trigger_else = {
								OR = {
									is_male = yes
									age > scope:ansf_family_member.age
								}
							}
						}
					}
					trigger_else_if = {
						limit = { faith = { has_doctrine_parameter = bastards_none } }
						any_child = {
							trigger_if = {
								limit = { scope:ansf_family_member = { is_male = yes } }
								is_male = yes
								age > scope:ansf_family_member.age
							}
							trigger_else_if = {
								limit = { scope:ansf_family_member = { is_female = yes } }
								OR = {
									is_male = yes
									age > scope:ansf_family_member.age
								}
							}
							trigger_else = {
								always = yes
							}
						}
					}
					trigger_else = {
						always = yes
					}
				}
				root = {
					add_to_variable_list = {
						name = potential_grandparents
						target = prev
					}
				}
				root = {
					add_to_variable_list = {
						name = potential_relatives
						target = prev
					}
				}
			}
		}
	}
	
	# then check for potential uncles/aunts
	every_ruler = {
		limit = {
			is_landed_or_landless_administrative = yes
			NOR = {
				is_ai = no
				has_government = mercenary_government
				has_government = republic_government
				has_government = theocracy_government
				has_government = holy_order_government
			}
			# must be same culture or one closely related
			culture = { has_same_culture_heritage = $COMPARE$.culture }
			# unless this is TARGET or the other parent isn't TARGET's exact culture
			# (one of the parents needs to be the player's culture!)
			trigger_if = {
				limit = {
					OR = {
						NOT = { $COMPARE$ = scope:target }
						scope:ansf_family_member = scope:target
						AND = {
							exists = scope:target.father
							scope:ansf_family_member = { is_female = yes }
							NOT = { scope:target.father.culture = scope:target.culture }
						}
						AND = {
							exists = scope:target.mother
							scope:ansf_family_member = { is_male = yes }
							NOT = { scope:target.mother.culture = scope:target.culture }
						}
					}
				}
				culture = $COMPARE$.culture
			}
			# same faith or astray
			faith = {
				OR = {
					this = $COMPARE$.faith
					faith_hostility_level = { target = $COMPARE$.faith value = faith_astray_level }
				}
			}
			# unless this is TARGET or the other parent isn't TARGET's exact faith
			# (one of the parents needs to be the player's faith!)
			trigger_if = {
				limit = {
					OR = {
						NOT = { $COMPARE$ = scope:target }
						scope:ansf_family_member = scope:target
						AND = {
							exists = scope:target.father
							scope:ansf_family_member = { is_female = yes }
							NOT = { scope:target.father.faith = scope:target.faith }
						}
						AND = {
							exists = scope:target.mother
							scope:ansf_family_member = { is_male = yes }
							NOT = { scope:target.mother.faith = scope:target.faith }
						}
					}
				}
				faith = $COMPARE$.faith
			}
			# from a different dynasty than their spouse
			trigger_if = {
				limit = {
					scope:ansf_family_member = { is_female = yes }
					NOT = { scope:ansf_family_member = scope:target }
				}
				NOT = { dynasty = scope:target.father.dynasty }
			}
			# inlaws different dynasties than my family
			trigger_if = {
				limit = {
					NOT = { $COMPARE$ = scope:target }
				}
				NOR = {
					dynasty = scope:target.dynasty
					dynasty = scope:target.father.dynasty
					dynasty = scope:target.mother.dynasty
				}
			}
			# is older than the parent but not too old
			trigger_if = {
				limit = { sex_same_as = scope:ansf_family_member }
				age > scope:ansf_family_member.age
			}
			OR = {
				age <= scope:ansf_family_member.age_plus_10
				any_sibling = {
					even_if_dead = yes
					age_including_death_age <= scope:ansf_family_member.age_plus_10
				}
			}
			# has an existing father who can be the parent's father
			exists = father
			father = {
				NOT = {
					root = {
						is_target_in_variable_list = {
							name = potential_relatives
							target = prev
						}
					}
				}
				age_including_death_minus_childbirth_start >= scope:ansf_family_member.age
				trigger_if = {
					limit = { is_alive = no }
					days_since_death < scope:ansf_family_member.age_in_days
				}
			}
			# for faiths without bastards, already has an heir
			trigger_if = {
				limit = { faith = { has_doctrine_parameter = bastards_none } }
				exists = primary_heir
				primary_heir = {
					is_child_of = prev
					trigger_if = {
						limit = { scope:ansf_family_member = { is_male = yes } }
						is_male = yes
					}
				}
			}
		}
		root = {
			add_to_variable_list = {
				name = potential_piblings
				target = prev
			}
		}
		if = {
			limit = {
				NOT = {
					root = {
						is_target_in_variable_list = {
							name = potential_relatives
							target = prev
						}
					}
				}
			}
			root = {
				add_to_variable_list = {
					name = potential_relatives
					target = prev
				}
			}
		}
	}
	# check the player title's previous holder for a potential uncle/aunt
	if = {
		limit = {
			scope:target = {
				is_landed = yes
				exists = primary_title.previous_holder
				NOT = {
					root = {
						is_target_in_variable_list = {
							name = potential_piblings
							target = root.primary_title.previous_holder
						}
					}
				}
			}
		}
		scope:target.primary_title.previous_holder = {
			if = {
				limit = {
					NOR = {
						is_ai = no
						has_government = mercenary_government
						has_government = republic_government
						has_government = theocracy_government
						has_government = holy_order_government
					}
					# must be same culture or one closely related
					culture = { has_same_culture_heritage = scope:target.culture }
					# unless this is TARGET or the other parent isn't TARGET's exact culture
					# (one of the parents needs to be the player's culture!)
					trigger_if = {
						limit = {
							OR = {
								NOT = { $COMPARE$ = scope:target }
								scope:ansf_family_member = scope:target
								AND = {
									exists = scope:target.father
									scope:ansf_family_member = { is_female = yes }
									NOT = { scope:target.father.culture = scope:target.culture }
								}
								AND = {
									exists = scope:target.mother
									scope:ansf_family_member = { is_male = yes }
									NOT = { scope:target.mother.culture = scope:target.culture }
								}
							}
						}
						culture = $COMPARE$.culture
					}
					# same faith or astray
					faith = {
						OR = {
							this = scope:target.faith
							faith_hostility_level = { target = scope:target.faith value = faith_astray_level }
						}
					}
					# unless this is TARGET or the other parent isn't TARGET's exact faith
					# (one of the parents needs to be the player's faith!)
					trigger_if = {
						limit = {
							OR = {
								NOT = { $COMPARE$ = scope:target }
								scope:ansf_family_member = scope:target
								AND = {
									exists = scope:target.father
									scope:ansf_family_member = { is_female = yes }
									NOT = { scope:target.father.faith = scope:target.faith }
								}
								AND = {
									exists = scope:target.mother
									scope:ansf_family_member = { is_male = yes }
									NOT = { scope:target.mother.faith = scope:target.faith }
								}
							}
						}
						faith = $COMPARE$.faith
					}
					# from a different dynasty than their spouse
					trigger_if = {
						limit = {
							scope:ansf_family_member = { is_female = yes }
							NOT = { scope:ansf_family_member = scope:target }
						}
						NOT = { dynasty = scope:target.father.dynasty }
					}
					# inlaws different dynasties than my family
					trigger_if = {
						limit = {
							NOT = { $COMPARE$ = scope:target }
						}
						NOR = {
							dynasty = scope:target.dynasty
							dynasty = scope:target.father.dynasty
							dynasty = scope:target.mother.dynasty
						}
					}
					# is older than the parent but not too old
					trigger_if = {
						limit = { sex_same_as = scope:ansf_family_member }
						age_including_death_age > scope:ansf_family_member.age
					}
					OR = {
						age_including_death_age <= scope:ansf_family_member.age_plus_10
						any_sibling = {
							even_if_dead = yes
							age_including_death_age <= scope:ansf_family_member.age_plus_10
						}
					}
					age_including_death_age <= scope:ansf_family_member.age_plus_10
					# has an existing father who can be the parent's father
					exists = father
					father = {
						NOT = {
							root = {
								is_target_in_variable_list = {
									name = potential_relatives
									target = prev
								}
							}
						}
						age_including_death_minus_childbirth_start >= scope:ansf_family_member.age
						trigger_if = {
							limit = { is_alive = no }
							days_since_death < scope:ansf_family_member.age_in_days
						}
					}
					# for faiths without bastards, already has an heir
					trigger_if = {
						limit = {
							is_playable_character = yes
							faith = { has_doctrine_parameter = bastards_none }
						}
						exists = primary_heir
						primary_heir = {
							is_child_of = prev
							trigger_if = {
								limit = { scope:ansf_family_member = { is_male = yes } }
								is_male = yes
							}
						}
					}
					trigger_else_if = {
						limit = {
							any_heir_title = {}
							faith = { has_doctrine_parameter = bastards_none }
						}
						any_child = {
							trigger_if = {
								limit = { scope:ansf_family_member = { is_male = yes } }
								is_male = yes
								age > scope:ansf_family_member.age
							}
							trigger_else = {
								OR = {
									is_male = yes
									age > scope:ansf_family_member.age
								}
							}
						}
					}
					trigger_else = {
						always = yes
					}
				}
				root = {
					add_to_variable_list = {
						name = potential_piblings
						target = prev
					}
				}
				if = {
					limit = {
						NOT = {
							root = {
								is_target_in_variable_list = {
									name = potential_relatives
									target = prev
								}
							}
						}
					}
					root = {
						add_to_variable_list = {
							name = potential_relatives
							target = prev
						}
					}
				}
			}
		}
	}
}

scripted_effect recruit_to_court_effect = {
	if = {
		limit = {
			exists = scope:random_family
			scope:random_family = yes
			$NEW_LIEGE$ = { is_ai = no }
		}
		$NEW_COURTIER$ = { add_to_list = new_player_courtiers }
	}
	else_if = {
		limit = {
			$NEW_COURTIER$ = {
				NOT = { is_courtier_of = $NEW_LIEGE$ }
			}
		}
		$NEW_LIEGE$ = { add_courtier = $NEW_COURTIER$ }
	}
}

scripted_effect connect_to_relative_effect = {
	if = {
		limit = {
			root = { has_variable = will_be_bastard }
		}
		scope:ansf_family_member = {
			ansf_add_bastard_trait_based_on_faith_effect = yes
		}
	}
	else_if = {
		limit = {
			root = { has_variable = will_be_legitimate_bastard }
		}
		scope:ansf_family_member = {
			ansf_add_legit_bastard_trait_based_on_faith_effect = yes
		}
	}
	if = {
		limit = {
			NOR = {
				root.var:actual_grandfather ?= scope:actual_relative
				root.var:actual_grandmother ?= scope:actual_relative
				root = { has_variable = will_be_bastard }
			}
			OR = {
				faith = { NOT = { has_doctrine_parameter = bastards_always } }
				NOT = { exists = scope:actual_relative.mother }
				root = { has_variable = actual_grandmother }
			}
			scope:actual_relative = {
				trigger_if = {
					limit = { has_realm_law = male_only_law }
					scope:ansf_family_member = { is_male = yes }
				}
				trigger_else_if = {
					limit = { has_realm_law = female_only_law }
					scope:ansf_family_member = { is_female = yes }
				}
				trigger_else = {
					always = yes
				}
			}
		}
		scope:ansf_family_member = {
			if = {
				limit = {
					scope:actual_relative = {
						any_held_title = { title_tier >= county }
					}
				}
				custom_tooltip = ansf_get_claims_held_titles
				hidden_effect = {
					scope:actual_relative = {
						every_held_title = {
							limit = { tier >= tier_county }
							scope:ansf_family_member = { add_pressed_claim = prev }
						}
					}
				}
			}
			if = {
				limit = {
					scope:actual_relative = {
						any_claim = {}
					}
				}
				custom_tooltip = ansf_get_claimed_claimed_titles
				hidden_effect = {
					scope:actual_relative = {
						every_claim = {
							if = {
								limit = {
									scope:actual_relative = { has_strong_claim_on = prev }
								}
								scope:ansf_family_member = { add_pressed_claim = prev }
							}
							else = {
								scope:ansf_family_member = { add_unpressed_claim = prev }
							}
						}
					}
				}
			}
		}
	}
	if = {
		limit = {
			scope:actual_relative = {
				is_playable_character = no
				is_courtier = no
			}
		}
		scope:actual_relative = {
			every_traveling_family_member = {
				limit = {
					is_playable_character = no
					is_courtier = no
				}
				scope:target_liege = { add_courtier = prev }
			}
		}
	}
}

scripted_effect post_creation_sibling_effect = {
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_clone
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:target
		if = {
			limit = { is_male = yes }
			add_character_flag = ansf_family_male
		}
		else = {
			add_character_flag = ansf_family_female
		}
	}
	
	#traits
	ansf_validate_character_traits = yes
	if = {
		limit = {
			exists = scope:random_family
			scope:random_family = yes
		}
		every_character_trait = {
			limit = { ansf_is_physical_trait = yes }
			random = {
				chance = 50
				scope:ansf_family_member = { remove_trait = prev }
			}
		}
	}
	
	#add the twin trait if a same-age sibling exists
	if = {
		limit = {
			any_sibling = {
				even_if_dead = yes
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
		}
		add_trait = twin
		remove_character_flag ?= ansf_family_male
		remove_character_flag ?= ansf_family_female
		random_sibling = {
			limit = {
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age = scope:ansf_family_member.age
			}
			scope:ansf_family_member = {
				copy_inheritable_appearance_from = prev
			}
		}
	}
	
	#make a bastard if needed
	if = {
		limit = {
			exists = scope:ansf_family_member.mother
			exists = scope:ansf_family_member.father
			NOT = {
				scope:ansf_family_member.mother = {
					any_spouse = {
						even_if_dead = yes
						this = scope:ansf_family_member.father
					}
				}
			}
		}
		scope:ansf_family_member = {
			ansf_add_bastard_trait_based_on_faith_effect = yes
		}
	}
	
	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		if = {
			limit = {
				root = { is_ai = no }
			}
			root = {
				ansf_remove_all_gui_variables = yes
				set_variable = { name = ansf_family_character value = scope:ansf_family_member }
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age value = scope:ansf_family_member.age }
				set_variable = ansf_family_character_education_random
				set_variable = { name = ansf_family_character_age_min value = age_legitimate_sibling_min }
				set_variable = { name = ansf_family_character_age_max value = age_legitimate_sibling_max }
			}
		}
	}
}

scripted_effect reverse_inherited_trait_check_effect = {
	if = {
		limit = {
			has_trait = beauty_good
			scope:target = { has_trait = beauty_bad }
		}
		random = {
			chance = 50
			remove_trait = beauty_good_1
			remove_trait = beauty_good_2
			remove_trait = beauty_good_3
		}
	}
	if = {
		limit = {
			has_trait = beauty_bad
			scope:target = { has_trait = beauty_good }
		}
		random = {
			chance = 50
			remove_trait = beauty_bad_1
			remove_trait = beauty_bad_2
			remove_trait = beauty_bad_3
		}
	}
	if = {
		limit = {
			has_trait = intellect_good
			scope:target = { has_trait = intellect_bad }
		}
		random = {
			chance = 50
			remove_trait = intellect_good_1
			remove_trait = intellect_good_2
			remove_trait = intellect_good_3
		}
	}
	if = {
		limit = {
			has_trait = intellect_bad
			scope:target = { has_trait = intellect_good }
		}
		random = {
			chance = 50
			remove_trait = intellect_bad_1
			remove_trait = intellect_bad_2
			remove_trait = intellect_bad_3
		}
	}
	if = {
		limit = {
			has_trait = physique_good
			scope:target = { has_trait = physique_bad }
		}
		random = {
			chance = 50
			remove_trait = physique_good_1
			remove_trait = physique_good_2
			remove_trait = physique_good_3
		}
	}
	if = {
		limit = {
			has_trait = physique_bad
			scope:target = { has_trait = physique_good }
		}
		random = {
			chance = 50
			remove_trait = physique_bad_1
			remove_trait = physique_bad_2
			remove_trait = physique_bad_3
		}
	}
	scope:target = {
		random_character_trait = {
			limit = {
				ansf_is_inherited_trait = yes
				NOT = { scope:ansf_family_member = { has_trait = prev } }
				trigger_if = {
					limit = { scope:ansf_family_member = { is_female = yes } }
					NOT = { scope:target.father = { has_trait = prev } }
				}
			}
			random = {
				chance = 50
				modifier = {
					add = -25
					scope:ansf_family_member = {
						any_character_trait = { ansf_is_inherited_trait = yes }
					}
				}
				modifier = {
					add = 25
					scope:ansf_family_member = {
						NOT = { any_character_trait = { ansf_is_inherited_trait = yes } }
					}
				}
				scope:ansf_family_member = { add_trait = prev }
			}
		}
	}
}

scripted_effect post_creation_parent_effect = {
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			OR = {
				root = {
					has_variable = ansf_family_character_is_clone
					has_variable = ansf_family_character_generate
				}
				AND = {
					is_male = yes
					exists = scope:target.mother
					scope:target.mother = { has_character_flag = is_different_DNA }
				}
				AND = {
					is_female = yes
					exists = scope:target.father
					scope:target.father = { has_character_flag = is_different_DNA }
				}
			}
		}
		copy_inheritable_appearance_from = scope:target
		if = {
			limit = { is_male = yes }
			add_character_flag = ansf_family_male
		}
		else = {
			add_character_flag = ansf_family_female
		}
	}
	else = {
		add_character_flag = { flag = is_different_DNA days = 365 }
	}
	if = {
		limit = { exists = scope:will_make_parents }
		random_list = {
			50 = {
				modifier = {
					add = 50
					is_male = yes
					exists = scope:target.mother
					scope:target.mother = { has_character_flag = is_different_DNA }
				}
				modifier = {
					add = 50
					is_female = yes
					exists = scope:target.father
					scope:target.father = { has_character_flag = is_different_DNA }
				}
				copy_inheritable_appearance_from = scope:target
				if = {
					limit = { is_male = yes }
					add_character_flag = ansf_family_male
				}
				else = {
					add_character_flag = ansf_family_female
				}
			}
			50 = {
				modifier = {
					add = -50
					is_male = yes
					exists = scope:target.mother
					scope:target.mother = { has_character_flag = is_different_DNA }
				}
				modifier = {
					add = -50
					is_female = yes
					exists = scope:target.father
					scope:target.father = { has_character_flag = is_different_DNA }
				}
				add_character_flag = { flag = is_different_DNA days = 365 }
			}
		}
	}
	
	#traits
	ansf_validate_character_traits = yes
	if = {
		limit = {
			exists = scope:random_family
			scope:random_family = yes
		}
		every_character_trait = {
			limit = { ansf_is_physical_trait = yes }
			random = {
				chance = 50
				scope:ansf_family_member = { remove_trait = prev }
			}
		}
	}
	
	#parents get a chance to have an inheritable trait belonging to the player
	#and losing an inheritable trait that's opposed
	reverse_inherited_trait_check_effect = yes
	
	#peasant leaders
	if = {
		limit = { exists = scope:is_peasant_leader }
		scope:ansf_family_member = { set_to_lowborn = yes }
	}
	if = {
		limit = {
			exists = scope:father_is_peasant_leader
			scope:ansf_family_member = { is_male = yes }
		}
		scope:ansf_family_member = { add_trait = peasant_leader }
	}
	
	#check for possible relatives
	if = {
		limit = {
			NOR = {
				exists = scope:is_peasant_leader
				AND = {
					exists = scope:father_is_peasant_leader
					scope:ansf_family_member = { is_male = yes }
				}
			}
		}
		check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
	}

	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		if = {
			limit = { root = { is_ai = no } }
			root = {
				ansf_remove_all_gui_variables = yes
				set_variable = { name = ansf_family_character value = scope:ansf_family_member }
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age value = scope:ansf_family_member.age }
			}
			if = {
				limit = {
					exists = scope:father_is_peasant_leader
					is_male = yes
				}
				root = {
					set_variable = ansf_cannot_change_education
					set_variable = ansf_family_character_education_martial
				}
			}
			else = {
				root = {
					set_variable = ansf_family_character_education_random
				}
			}
			if = {
				limit = { is_male = yes }
				root = {
					set_variable = { name = ansf_family_character_age_min value = age_compared_to_target_plus_twenty }
					set_variable = { name = ansf_family_character_age_max value = age_compared_to_target_plus_thirty }
				}
			}
			else = {
				root = {
					set_variable = { name = ansf_family_character_age_min value = age_compared_to_target_plus_childbirth_start }
					set_variable = { name = ansf_family_character_age_max value = age_compared_to_target_father }
				}
			}
			if = {
				limit = {
					is_male = yes
					is_playable_character = no
				}
				root = {
					set_variable = ansf_has_culture_faith_template
				}
			}
			if = {
				limit = {
					is_female = yes
					is_playable_character = no
					exists = scope:target.father
					scope:target = {
						culture = father.culture
						NOR = {
							has_trait = bastard
							has_trait = legitimized_bastard
							has_trait = wild_oat
							has_trait = bastard_founder
						}
					}
				}
				root = {
					set_variable = ansf_has_culture_faith_template
				}
			}
			if = {
				limit = {
					OR = {
						AND = {
							is_male = yes
							exists = scope:target.mother
							scope:target.mother = { has_character_flag = is_different_DNA }
						}
						AND = {
							is_female = yes
							exists = scope:target.father
							scope:target.father = { has_character_flag = is_different_DNA }
						}
					}
				}
				root = {
					set_variable = ansf_family_character_must_be_clone
					set_variable = ansf_family_character_is_clone
				}
			}
		}
	}
}

scripted_effect post_creation_existing_effect = {
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_different
				has_variable = ansf_family_character_generate
			}
		}
		add_character_flag = { flag = is_different_DNA days = 365 }
	}
	else_if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_clone
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:target
	}
	else_if = {
		limit = { is_male = yes }
		copy_inheritable_appearance_from = scope:target
		add_character_flag = ansf_family_male
	}
	else = {
		copy_inheritable_appearance_from = scope:target
		add_character_flag = ansf_family_female
	}
	
	#traits
	ansf_validate_character_traits = yes
	if = {
		limit = {
			exists = scope:random_family
			scope:random_family = yes
		}
		every_character_trait = {
			limit = { ansf_is_physical_trait = yes }
			random = {
				chance = 50
				scope:ansf_family_member = { remove_trait = prev }
			}
		}
	}
	
	#parents get a chance to have an inheritable trait belonging to the player
	#and losing an inheritable trait that's opposed
	reverse_inherited_trait_check_effect = yes
	
	#add the twin trait if a same-age sibling exists
	if = {
		limit = {
			any_sibling = {
				even_if_dead = yes
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age = scope:ansf_family_member.age
			}
		}
		add_trait = twin
		remove_character_flag ?= ansf_family_male
		remove_character_flag ?= ansf_family_female
		random_sibling = {
			even_if_dead = yes
			limit = {
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
			scope:ansf_family_member = {
				add_character_flag = { flag = is_different_DNA days = 365 }
				copy_inheritable_appearance_from = prev
			}
		}
	}
	
	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		if = {
			limit = { root = { is_ai = no } }
			root = {
				ansf_remove_all_gui_variables = yes
				set_variable = { name = ansf_family_character value = scope:ansf_family_member }
				set_variable = ansf_family_character_is_existing_dynasty
				set_variable = ansf_family_character_cannot_change_age
				set_variable = { name = ansf_family_character_age value = scope:ansf_family_member.age }
				set_variable = ansf_family_character_education_random
			}
			if = {
				limit = { has_trait = twin }
				root = { set_variable = ansf_family_character_cannot_be_clone }
			}
			if = {
				limit = {
					is_female = yes
					scope:target.father = { has_character_flag = is_different_DNA }
				}
				root = { set_variable = ansf_family_character_cannot_be_different }
			}
		}
	}
}

scripted_effect connect_father_to_me_effect = {
	set_father = scope:ansf_family_member
	
	#player is a bastard
	if = {
		limit = { ansf_is_bastard = yes }
		#give them the adulterer/fornicator trait if faith permits
		scope:ansf_family_member = {
			if = {
				limit = { ansf_is_adulterer = no }
				add_adulterer_fornicator_trait_or_nothing_effect = yes
			}
		}
		#add them to court
		if = {
			limit = {
				scope:ansf_family_member = {
					NOT = { is_courtier_of = scope:target_liege }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}
		#forgive any criminal offenses
		hidden_effect = {
			scope:target_liege = {
				consume_all_criminal_reasons_effect = {
					LIEGE = scope:target_liege
					CRIMINAL = scope:ansf_family_member
				}
			}
		}
	}
	#player is not a bastard
	else = {
		#first marry them to the mother, if she already exists
		if = {
			limit = {
				exists = mother
				mother = { is_alive = yes }
			}
			if = {
				limit = {
					OR = {
						exists = scope:is_existing_dynasty
						mother = {
							has_dynasty = yes
							dynasty = scope:target.dynasty
						}
					}
				}
				scope:ansf_family_member = { marry_matrilineal = scope:target.mother }
			}
			else = {
				scope:ansf_family_member = { marry = scope:target.mother }
			}
		}
		#set father for existing siblings, if age appropriate
		hidden_effect = {
			every_sibling = {
				even_if_dead = yes
				limit = {
					age_including_death_plus_childbirth_start <= scope:ansf_family_member.age
					has_dynasty = yes
					dynasty = scope:target.mother.dynasty
					exists = mother
					mother = scope:target.mother
					NOR = {
						exists = father
						exists = real_father
						ansf_is_bastard = yes
					}
				}
				set_father = scope:ansf_family_member
			}
		}
		#add them to appropriate court
		if = {
			limit = {
				exists = scope:is_existing_dynasty
				exists = scope:actual_relative
				scope:actual_relative = {
					is_playable_character = yes
					OR = {
						is_spouse_of = scope:ansf_family_member
						is_child_of = scope:ansf_family_member
					}
				}
			}
			scope:actual_relative = { add_courtier = scope:ansf_family_member }
		}
		else_if = {
			limit = {
				scope:ansf_family_member = {
					NOT = { is_courtier_of = scope:target_liege }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}
	}
}

scripted_effect connect_mother_to_me_effect = {
	set_mother = scope:ansf_family_member
	
	#player is a bastard
	if = {
		limit = { ansf_is_bastard = yes }
		#give them the adulterer/fornicator trait if faith permits
		scope:ansf_family_member = {
			if = {
				limit = { ansf_is_adulterer = no }
				add_adulterer_fornicator_trait_or_nothing_effect = yes
			}
		}
		#add them to court
		if = {
			limit = {
				scope:ansf_family_member = {
					NOT = { is_courtier_of = scope:target_liege }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}
		#forgive any criminal offenses
		hidden_effect = {
			scope:target_liege = {
				consume_all_criminal_reasons_effect = {
					LIEGE = scope:target_liege
					CRIMINAL = scope:ansf_family_member
				}
			}
		}
	}
	#player is not a bastard
	else = {
		#first marry them to the father
		if = {
			limit = {
				exists = father
				father = { is_alive = yes }
			}
			if = {
				limit = {
					scope:ansf_family_member = { has_dynasty = yes }
					OR = {
						father = { has_dynasty = no }
						AND = {
							scope:ansf_family_member.dynasty = scope:target.dynasty
							father = {
								has_dynasty = yes
								NOT = { dynasty = scope:target.dynasty }
							}
						}
						AND = {
							scope:ansf_family_member = { is_playable_character = yes }
							father = { is_playable_character = no }
						}
					}
				}
				scope:ansf_family_member = { marry_matrilineal = scope:target.father }
			}
			else = {
				scope:ansf_family_member = { marry = scope:target.father }
			}
		}
		#set father for existing siblings, if age appropriate
		hidden_effect = {
			every_sibling = {
				even_if_dead = yes
				limit = {
					age_including_death_plus_childbirth_start <= scope:ansf_family_member.age
					has_dynasty = yes
					dynasty = scope:target.father.dynasty
					exists = father
					father = scope:target.father
					NOR = {
						exists = mother
						ansf_is_bastard = yes
					}
				}
				set_mother = scope:ansf_family_member
			}
		}
		#add them to appropriate court
		if = {
			limit = {
				exists = scope:is_existing_dynasty
				exists = scope:actual_relative
				scope:actual_relative = {
					is_playable_character = yes
					OR = {
						is_spouse_of = scope:ansf_family_member
						is_child_of = scope:ansf_family_member
					}
				}
			}
			scope:actual_relative = { add_courtier = scope:ansf_family_member }
		}
		else_if = {
			limit = {
				scope:ansf_family_member = {
					NOT = { is_courtier_of = scope:target_liege }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}
	}
}

scripted_effect begin_family_creation_effect = {
	# first check for lack of a father
	if = {
		limit = {
			scope:target = { has_no_father_or_real_father = yes }
		}
		trigger_event = ansf.1002
	}
	# then check for lack of a mother
	else_if = {
		limit = {
			scope:target = {
				NOT = { exists = mother }
			}
		}
		trigger_event = ansf.1003
	}
	# failing that, go to the main family screen
	else = {
		trigger_event = ansf.1004
	}
}

scripted_effect set_new_target_house_effect = {
	scope:target.house = {
		every_house_member = {
			set_house = $RELATIVE$.house
		}
	}
	$RELATIVE$.house = {
		set_variable = {
			name = ansf_reflected_on_family
			value = yes
		}
	}
}

scripted_effect check_twin_effect = {
	if = {
		limit = {
			$CHARACTER$ = {
				any_sibling = {
					even_if_dead = yes
					NOT = { has_trait = twin }
					mother ?= $CHARACTER$.mother
					father ?= $CHARACTER$.father
					age_including_death_age = $CHARACTER$.age
				}
			}
		}
		$CHARACTER$ = {
			if = {
				limit = { NOT = { has_trait = twin } }
				add_trait = twin
			}
			remove_character_flag ?= ansf_family_male
			remove_character_flag ?= ansf_family_female
			random_sibling = {
				even_if_dead = yes
				limit = {
					NOT = { has_trait = twin }
					mother = $CHARACTER$.mother
					father = $CHARACTER$.father
					age_including_death_age = $CHARACTER$.age
				}
				add_trait = twin
			}
		}
	}
}

scripted_effect increment_random_siblings_effect = {
	if = {
		limit = {
			exists = scope:random_family
			scope:random_family = yes
			has_variable = num_siblings
		}
		change_variable = {
			name = num_siblings
			add = -1
		}
	}
}

scripted_effect remove_family_list_variables = {
	remove_variable ?= portrait1
	remove_variable ?= portrait2
	remove_variable ?= portrait3
	remove_variable ?= portrait4
	remove_variable ?= portrait5
	clear_saved_scope ?= parent_main
	clear_saved_scope ?= parent_other
	clear_saved_scope ?= first_sibling
	clear_saved_scope ?= second_sibling
	clear_saved_scope ?= third_sibling
	clear_saved_scope ?= fourth_sibling
	clear_saved_scope ?= fifth_sibling
	clear_saved_scope ?= sixth_sibling
	clear_saved_scope ?= seventh_sibling
	clear_saved_scope ?= eighth_sibling
}

scripted_effect find_random_template_ruler = {
	remove_variable = culture_faith_template
	if = {
		limit = {
			any_ruler = {
				NOT = { this = scope:target }
				save_temporary_scope_as = potential_template
				OR = {
					culture = scope:target.culture
					culture = { has_same_culture_heritage = scope:target.culture }
					scope:target.culture = {
						cultural_acceptance = {
							target = scope:potential_template.culture
							value >= 25
						}
					}
				}
				OR = {
					faith = scope:target.faith
					scope:target.faith = {
						faith_hostility_level = {
							target = scope:potential_template.faith
							value <= faith_astray_level
						}
					}
				}
				# is a player running the event? If so, we can scan further afield
				trigger_if = {
					limit = {
						root = { is_ai = no }
					}
					in_diplomatic_range = scope:target
				}
				# otherwise, keep the range more limited
				trigger_else = {
					exists = capital_province
					scope:target.location = {
						squared_distance = { target = scope:potential_template.capital_province value < 500000 }
					}
				}
			}
		}
		random_ruler = {
			limit = {
				NOT = { this = scope:target }
				save_temporary_scope_as = potential_template
				OR = {
					culture = scope:target.culture
					culture = { has_same_culture_heritage = scope:target.culture }
					scope:target.culture = {
						cultural_acceptance = {
							target = scope:potential_template.culture
							value >= 25
						}
					}
				}
				OR = {
					faith = scope:target.faith
					scope:target.faith = {
						faith_hostility_level = {
							target = scope:potential_template.faith
							value <= faith_astray_level
						}
					}
				}
				# is a player running the event? If so, we can scan further afield
				trigger_if = {
					limit = {
						root = { is_ai = no }
					}
					in_diplomatic_range = scope:target
				}
				# otherwise, keep the range more limited
				trigger_else = {
					exists = capital_province
					scope:target.location = {
						squared_distance = { target = scope:potential_template.capital_province value < 500000 }
					}
				}
			}
			weight = {
				base = 10
				modifier = {
					add = 3
					culture = scope:target.culture
					NAND = {
						exists = scope:random_spouse_option
						scope:random_spouse_option = yes
					}
				}
				modifier = {
					add = 6
					culture = scope:target.culture
					NAND = {
						exists = scope:random_spouse_option
						scope:random_spouse_option = yes
					}
				}
				modifier = {
					add = 1
					NOT = { culture = scope:target.culture }
					culture = { has_same_culture_heritage = scope:target.culture }
				}
			}
			save_temporary_scope_as = potential_template
			root = {
				set_variable = {
					name = culture_faith_template
					value = scope:potential_template
				}
			}
		}
	}
	else = {
		root = {
			set_variable = {
				name = culture_faith_template
				value = scope:target
			}
		}
	}
}

scripted_effect find_eldest_legitimate_halfsibling = {
	every_in_list = {
		list = legitimate_halfsiblings
		remove_from_list = legitimate_halfsiblings
	}
	$MOTHER$ = {
		every_child = {
			even_if_dead = yes
			limit = {
				NOR = {
					ansf_is_bastard = yes
					has_trait = child_of_concubine_female
					father = $FATHER$
				}
			}
			add_to_list = legitimate_halfsiblings
		}
	}
	$FATHER$ = {
		every_child = {
			even_if_dead = yes
			limit = {
				NOR = {
					ansf_is_bastard = yes
					has_trait = child_of_concubine_female
					mother = $MOTHER$
				}
			}
			add_to_list = legitimate_halfsiblings
		}
	}
	if = {
		limit = {
			list_size = { name = legitimate_halfsiblings value > 0 }
		}
		ordered_in_list = {
			list = legitimate_halfsiblings
			order_by = age_including_death_age
			save_scope_as = mother_father_eldest_child
		}
	}
}

ansf.1001 = { #Who am I?
	type = character_event
	window = ansf_event_window
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.1001.t
			}
			desc = ansf.1001.t.other
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					scope:target = { government_has_flag = government_is_landless_adventurer }
				}
				desc = ansf.1001.desc.adventurer
			}
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.1001.desc
			}
			triggered_desc = {
				trigger = { scope:target = { is_playable_character = yes } }
				desc = ansf.1001.desc.other.ruler
			}
			desc = ansf.1001.desc.other
		}
	}
	theme = family
	center_portrait = {
		character = scope:target
		animation = personality_content
	}
	override_background = {
		trigger = { scope:target = { is_landed = yes } }
		reference = throne_room
	}
	override_background = {
		trigger = { scope:target = { is_landed = no } }
		reference = study
	}
	
	immediate = {
		hidden_effect = {
			scope:target = {
				house = {
					set_variable = {
						name = ansf_reflected_on_family
						value = yes
					}
				}
				if = {
					limit = {
						is_playable_character = yes
						has_no_parents = yes
					}
					save_scope_as = ansf_family_member
					check_for_possible_relatives = { COMPARE = scope:target }
				}
			}
			root = {
				set_variable = {
					name = target
					value = scope:target
				}
			}
			
			# we create two "real" dummy_male and dummy_female just to avoid a glut of errors being dumped into the log every time you discard a character
			ansf_create_dummy_characters = yes
			
			# this pre-loads all the needed traits for the GUI (useful for mods with a lot of extra traits, like AGOT)
			ansf_load_traits = yes
			
			# this is here to prevent error checks
			if = {
				limit = { always = no }
				save_scope_as = selection
				set_global_variable = { name = AGOT_is_loaded value = yes }
			}
		}
	}
	
	option = {
		trigger = {
			scope:target = {
				is_adult = yes
				is_playable_character = yes
				has_no_parents = yes
			}
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:target = root
							scope:target = { government_has_flag = government_is_landless_adventurer }
						}
						desc = ansf.me.peasant.leader.adventurer
					}
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.me.peasant.leader
					}
					desc = ansf.other.peasant.leader
				}
			}
		}
		
		custom_tooltip = ansf.me.peasant.leader.tt
		
		scope:target = {
			if = {
				limit = { NOT = { has_trait = peasant_leader } }
				add_trait_force_tooltip = peasant_leader
			}
		}
		
		save_scope_value_as = {
			name = is_peasant_leader
			value = yes
		}
		trigger_event = ansf.1002
	}
	option = {
		trigger = {
			scope:target = {
				NOT = { has_trait = peasant_leader }
				is_playable_character = yes
				custom_description = {
					text = cannot_change_dynasty
					subject = scope:target
					ansf_can_change_dynasty = yes
				}
				custom_description = {
					text = cannot_have_existing_parents
					subject = scope:target
					has_no_parents = yes
				}
				custom_description = {
					text = cannot_have_family_title
					subject = scope:target
					NOT = {
						any_held_title = { is_noble_family_title = yes }
					}
				}
			}
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:target = root
							scope:target = { government_has_flag = government_is_landless_adventurer }
						}
						desc = ansf.me.father.peasant.adventurer
					}
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.me.father.peasant.leader
					}
					triggered_desc = {
						trigger = { scope:target = { government_has_flag = government_is_landless_adventurer } }
						desc = ansf.other.father.peasant.leader.adventurer
					}
					desc = ansf.other.father.peasant.leader
				}
			}
		}
		show_as_unavailable = {
			scope:target = {
				OR = {
					has_trait = peasant_leader
					is_playable_character = no
					has_no_parents = no
					ansf_can_change_dynasty = no
					any_held_title = { is_noble_family_title = yes }
				}
			}
		}
		
		custom_tooltip = ansf.me.father.peasant.leader.tt

		save_scope_value_as = {
			name = father_is_peasant_leader
			value = yes
		}
		trigger_event = ansf.1002
	}
	option = {
		trigger = {
			scope:target = {
				NOT = { has_trait = peasant_leader }
				custom_description = {
					text = cannot_have_existing_parents
					subject = scope:target
					has_no_parents = yes
				}
				custom_description = {
					text = cannot_change_dynasty
					subject = scope:target
					ansf_can_change_dynasty = yes
				}
				trigger_if = {
					limit = {
						ansf_can_change_dynasty = yes
						has_no_parents = yes
					}
					custom_description = {
						text = no_dynasty_options
						subject = scope:target
						root = {
							has_variable_list = potential_relatives
							variable_list_size = { name = potential_relatives value > 0 }
						}
					}
				}
			}
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.me.existing.family
					}
					desc = ansf.other.existing.family
				}
			}
		}
		show_as_unavailable = {
			OR = {
				scope:target = { has_trait = peasant_leader }
				scope:target = { has_no_parents = no }
				scope:target = { ansf_can_change_dynasty = no }
				root = { NOT = { has_variable_list = potential_relatives } }
				root = { variable_list_size = { name = potential_relatives value < 1 } }
			}	
		}
		scope:target = {
			if = {
				limit = { is_ai = no }
				custom_tooltip = ansf.me.existing.family.tt
			}
			else = {
				custom_tooltip = ansf.other.existing.family.tt
			}
		}
		trigger_event = ansf.1022
	}
	option = {
		trigger = {
			scope:target = {
				is_ai = no
				is_playable_character = yes
				top_liege != this
			}
		}
		name = ansf.me.has.liege

		if = {
			limit = { years_from_game_start < 1 }
			reverse_add_opinion = {
				target = liege
				modifier = friendliness_opinion
				opinion = 25
			}
		}
		
		custom_tooltip = ansf.me.has.liege.tt
		
		begin_family_creation_effect = yes
	}
	option = {
		trigger = {
			scope:target = {
				is_ai = no
				is_playable_character = yes
				top_liege = this
			}
		}
		name = ansf.me.no.changes
		
		custom_tooltip = ansf.me.no.changes.tt
		
		begin_family_creation_effect = yes
	}
	option = {
		trigger = {
			scope:target = {
				is_ai = yes
			}
		}
		name = ansf.other.no.changes
		
		custom_tooltip = ansf.me.no.changes.tt
		
		begin_family_creation_effect = yes
	}
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.me.no
					}
					desc = ansf.other.no
				}
			}
		}
		scope:target = {
			if = {
				limit = { is_ai = no }
				custom_tooltip = ansf.me.no.tt
			}
			else = {
				custom_tooltip = ansf.other.no.tt
			}
			house = { remove_variable = ansf_reflected_on_family }
		}
	}
}

ansf.1002 = { #Is this your father?
	type = character_event
	window = ansf_event_window
	title = ansf.father.event.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					exists = scope:is_existing_dynasty
				}
				desc = ansf.father.event.existing.d
			}
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.father.event.d
			}
			desc = ansf.father.event.other.d
		}
	}
	theme = family
	center_portrait = scope:ansf_family_member
	override_background = { reference = temple }

	immediate = {
		hidden_effect = {
			clear_variable_list = potential_grandparents
			clear_variable_list = potential_piblings
			clear_variable_list = potential_relatives
			if = {
				limit = { has_character_flag = no_creation_override }
				remove_character_flag = no_creation_override
				check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
			}
			else = {
				# use the player's template unless one has already been set
				if = {
					limit = {
						OR = {
							NOT = { has_variable = culture_faith_template }
							AND = {
								has_variable = ansf_family_character_is_clone
								has_variable = ansf_family_character_generate
							}
						}
					}
					set_variable = {
						name = culture_faith_template
						value = scope:target
					}
				}
				scope:new_dummy_male = { save_scope_as = source_parent }
				random_list = {
					20 = {
						modifier = {
							factor = 0
							exists = scope:father_is_peasant_leader
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = { 
								integer_range = {
									min = age_compared_to_target_plus_twenty
									max = age_compared_to_target_plus_thirty
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_diplomacy days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:new_dummy_male } }
						}
						modifier = {
							factor = 5
							exists = scope:is_peasant_leader
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = { 
								integer_range = {
									min = age_compared_to_target_plus_twenty
									max = age_compared_to_target_plus_thirty
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_martial days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							exists = scope:father_is_peasant_leader
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = { 
								integer_range = {
									min = age_compared_to_target_plus_twenty
									max = age_compared_to_target_plus_thirty
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_stewardship days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							exists = scope:father_is_peasant_leader
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = { 
								integer_range = {
									min = age_compared_to_target_plus_twenty
									max = age_compared_to_target_plus_thirty
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_intrigue days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							exists = scope:father_is_peasant_leader
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = { 
								integer_range = {
									min = age_compared_to_target_plus_twenty
									max = age_compared_to_target_plus_thirty
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_learning days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
				}
			}
		}
	}

	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	#Father and he's part of my dynasty
	option = {
		trigger = {
			custom_description = {
				text = cannot_change_dynasty
				subject = scope:target
				scope:target = { ansf_can_change_dynasty = yes }
			}
			custom_description = {
				text = cannot_have_family_title
				subject = scope:target
				scope:target = {
					NOT = {
						any_held_title = { is_noble_family_title = yes }
					}
				}
			}
			scope:ansf_family_member.culture = scope:target.culture
			NOR = {
				exists = scope:is_peasant_leader
				exists = scope:is_existing_dynasty
				scope:ansf_family_member.dynasty ?= scope:target.dynasty
			}
		}
		show_as_unavailable = {
			OR = {
				exists = scope:is_peasant_leader
				exists = scope:is_existing_dynasty
				scope:ansf_family_member.dynasty ?= scope:target.dynasty
				scope:target = { ansf_can_change_dynasty = no }
				scope:target = { any_held_title = { is_noble_family_title = yes } }
				NOT = { scope:ansf_family_member.culture = scope:target.culture }
			}
		}
		name = ansf.yes.dad.house
		
		custom_tooltip = ansf.join.dynasty.tooltip
		
		set_new_target_house_effect = { RELATIVE = scope:ansf_family_member }
		scope:target = { connect_father_to_me_effect = yes }
		
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 100
		}
	}

	#Father, will be different dynasty and will not show up in the dynasty window
	option = {
		trigger = {
			NOT = { exists = scope:father_is_peasant_leader }
		}
		name = ansf.yes.dad
		
		if = {
			limit = {
				NOR = {
					exists = scope:is_peasant_leader
					exists = scope:is_existing_dynasty
				}
				scope:ansf_family_member = {
					has_dynasty = yes
					NOT = { dynasty = scope:target.dynasty }
				}
			}
			custom_tooltip = ansf.yes.dad.tooltip
		}
		
		scope:target = { connect_father_to_me_effect = yes }

		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 10
		}
	}
	
	#Father is of a dynasty existing in the world
	option = {
		trigger = {
			NOR = {
				exists = scope:is_peasant_leader
				exists = scope:father_is_peasant_leader
			}
			custom_description = {
				text = no_dynasty_options
				subject = scope:ansf_family_member
				has_variable_list = potential_relatives
				variable_list_size = { name = potential_relatives value > 0 }
			}
		}
		show_as_unavailable = {
			NOR = {
				exists = scope:is_peasant_leader
				exists = scope:father_is_peasant_leader
				exists = scope:is_existing_dynasty
			}
			OR = {
				NOT = { has_variable_list = potential_relatives }
				variable_list_size = { name = potential_relatives value < 1 }
			}
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.yes.dad.relative
					}
					desc = ansf.yes.dad.relative.other
				}
			}
		}

		custom_tooltip = ansf.existing.dynasty.tooltip
		if = {
			limit = { scope:target = { any_held_title = { is_noble_family_title = yes } } }
			custom_tooltip = ansf.noble.title.warning.tooltip
		}
		
		remove_variable ?= show_ansf_regenerate_window
		
		trigger_event = ansf.1020
		
		ai_chance = {
			base = 0
		}
	}

	#This isn't my father... redo
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.dad.regenerate
					}
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.no.dad
					}
					desc = ansf.no.dad.other
				}
			}
		}
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.reroll
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
	
		trigger_event = ansf.1002
		
		ai_chance = {
			base = 0
		}
	}
}

ansf.1003 = { #Is this your mother?
	type = character_event
	window = ansf_event_window
	title = ansf.mother.event.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					exists = scope:is_existing_dynasty
					scope:target = { has_trait = bastard_founder }
				}
				desc = ansf.mother.event.existing.bastard.d
			}
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.mother.event.d
			}
			desc = ansf.mother.event.other.d
		}
	}
	theme = family
	center_portrait = scope:ansf_family_member
	override_background = { reference = garden }

	immediate = {
		hidden_effect = {
			clear_variable_list = potential_grandparents
			clear_variable_list = potential_piblings
			clear_variable_list = potential_relatives
			if = {
				limit = { has_character_flag = no_creation_override }
				remove_character_flag = no_creation_override
				check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
			}
			else = {
				# use the player's template unless one has already been set
				if = {
					limit = {
						OR = {
							NOT = { has_variable = culture_faith_template }
							AND = {
								has_variable = ansf_family_character_is_clone
								has_variable = ansf_family_character_generate
							}
						}
					}
					set_variable = {
						name = culture_faith_template
						value = scope:target
					}
				}
				scope:new_dummy_female = { save_scope_as = source_parent }
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = age_compared_to_target_plus_childbirth_start
									max = age_compared_to_target_father
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_diplomacy days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:new_dummy_female } }
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = age_compared_to_target_plus_childbirth_start
									max = age_compared_to_target_father
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_martial days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = age_compared_to_target_plus_childbirth_start
									max = age_compared_to_target_father
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_stewardship days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = age_compared_to_target_plus_childbirth_start
									max = age_compared_to_target_father
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_intrigue days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = age_compared_to_target_plus_childbirth_start
									max = age_compared_to_target_father
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = { flag = was_learning days = 365 }
								post_creation_parent_effect = yes
							}
						}
					}
				}
			}
		}
	}

	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	#Lowborn mother for peasant leader fathers
	option = {
		trigger = {
			exists = scope:father_is_peasant_leader
		}
		name = ansf.yes.mom.lowborn
		
		scope:target = { set_mother = scope:ansf_family_member }
		scope:ansf_family_member = {
			set_to_lowborn = yes
			marry = scope:target.father
		}
		
		hidden_effect = {
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}
		
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
	}

	#My Mother, will not get her CoA and will not show up in Dynasty Window
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.yes.mom
					}
					desc = ansf.yes.mom.other
				}
			}
		}
		
		scope:target = { connect_mother_to_me_effect = yes }

		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 10
		}
	}
	
	#My Mother, but she will be the "founder" of our Dynasty
	option = {
		trigger = {
			custom_description = {
				text = cannot_change_dynasty
				subject = scope:target
				scope:target = { ansf_can_change_dynasty = yes }
			}
			custom_description = {
				text = cannot_have_family_title
				subject = scope:target
				scope:target = {
					NOT = {
						any_held_title = { is_noble_family_title = yes }
					}
				}
			}
			custom_description = {
				text = already_joined_father_dynasty
				subject = scope:target
				NOT = { scope:target.father.dynasty ?= scope:target.dynasty }
			}
			scope:ansf_family_member.culture = scope:target.culture
			NOR = {
				exists = scope:is_peasant_leader
				exists = scope:is_existing_dynasty
				scope:ansf_family_member.dynasty ?= scope:target.dynasty
			}
		}
		name = ansf.yes.mom.house
		show_as_unavailable = {
			OR = {
				scope:target.father.dynasty ?= scope:target.dynasty
				scope:target = { ansf_can_change_dynasty = no }
				scope:target = { any_held_title = { is_noble_family_title = yes } }
				exists = scope:is_peasant_leader
				exists = scope:is_existing_dynasty
				scope:ansf_family_member.dynasty ?= scope:target.dynasty
				NOT = { scope:ansf_family_member.culture = scope:target.culture }
			}
		}
		
		custom_tooltip = ansf.join.dynasty.tooltip
		
		set_new_target_house_effect = { RELATIVE = scope:ansf_family_member }
		scope:target = { set_mother = scope:ansf_family_member }
		scope:ansf_family_member = { marry_matrilineal = scope:target.father }
	
		hidden_effect = {
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}

		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 100
		}
	}

	#Mother is of a dynasty existing in the world
	option = {
		trigger = {
			NOT = { exists = scope:is_peasant_leader }
			custom_description = {
				text = no_dynasty_options
				subject = scope:ansf_family_member
				has_variable_list = potential_relatives
				variable_list_size = { name = potential_relatives value > 0 }
			}
		}
		show_as_unavailable = {
			NOR = {
				exists = scope:is_peasant_leader
				exists = scope:is_existing_dynasty
			}
			OR = {
				NOT = { has_variable_list = potential_relatives }
				variable_list_size = { name = potential_relatives value < 1 }
			}
		}
		name = ansf.yes.mom.relative
		custom_tooltip = ansf.existing.dynasty.tooltip
		if = {
			limit = { scope:target = { any_held_title = { is_noble_family_title = yes } } }
			custom_tooltip = ansf.noble.title.warning.tooltip
		}
		
		remove_variable ?= show_ansf_regenerate_window
		
		trigger_event = ansf.1020
		
		ai_chance = {
			base = 0
		}
	}

	#This isn't my mother... redo
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.mom.regenerate
					}
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.no.mom
					}
					desc = ansf.no.mom.other
				}
			}
		}
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.reroll
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}

		trigger_event = ansf.1003
		ai_chance = {
			base = 0
		}
	}
}

ansf.1004 = { #Main Family Screen
	type = character_event
	window = ansf_group_event_window
	title = ansf.family.event.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.family.event.d
			}
			desc = ansf.family.event.other.d
		}
	}
	theme = family
	left_portrait = {
		character ?= scope:parent_other
	}
	center_portrait = {
		character ?= scope:parent_main
	}
	right_portrait = {
		character = scope:target
	}
	lower_left_portrait = {
		character ?= scope:first_sibling
	}
	lower_center_portrait = {
		character ?= scope:second_sibling
	}
	lower_right_portrait = {
		character ?= scope:third_sibling
	}
	override_background = { reference = throne_room }

	immediate = {
		# setting the portraits to appear on-screen
		hidden_effect = {
			scope:target = {
				if = {
					limit = {
						exists = mother
						mother = {
							has_dynasty = yes
							dynasty = scope:target.dynasty
							OR = {
								is_house_head = yes
								AND = {
									is_ruler = yes
									scope:target.father ?= { is_ruler = no }
								}
							}
						}
					}
					mother = { save_scope_as = parent_main }
					father ?= { save_scope_as = parent_other }
				}
				else = {
					mother ?= { save_scope_as = parent_other }
					father ?= { save_scope_as = parent_main }
				}
				if = {
					limit = {
						any_child = {
							NOT = { has_trait = bastard }
							age >= 8
						}
					}
					random_child = {
						limit = {
							NOT = { has_trait = bastard }
							age >= 8
						}
						weight = {
							base = 1
							modifier = {
								add = {
									value = age
									multiply = 2
								}
							}
							modifier = {
								is_player_heir_of = scope:target
								add = 100
							}
						}
						save_scope_as = first_sibling
					}
				}
				else = {
					random_sibling = {
						limit = {
							is_alive = yes
							age >= 8
						}
						weight = {
							base = 1
							modifier = {
								add = {
									value = age
									multiply = 2
								}
							}
							modifier = {
								add = 100
								scope:target = { is_married = yes }
								is_player_heir_of = root
							}
							modifier = {
								factor = 0.5
								ansf_is_bastard = yes
							}
						}
						save_scope_as = first_sibling
					}
				}
				if = {
					limit = { exists = primary_spouse }
					primary_spouse = { save_scope_as = second_sibling }
				}
				else = {
					random_sibling = {
						limit = {
							is_alive = yes
							NAND = {
								exists = scope:first_sibling
								this = scope:first_sibling
							}
							age >= 8
						}
						weight = {
							base = 1
							modifier = {
								add = {
									value = age
									multiply = 4
								}
							}
							modifier = {
								add = 100
								is_player_heir_of = root
							}
							modifier = {
								factor = 0.5
								ansf_is_bastard = yes
							}
						}
						save_scope_as = second_sibling
					}
				}
				random_sibling = {
					limit = {
						is_alive = yes
						NOR = {
							AND = {
								exists = scope:first_sibling
								this = scope:first_sibling
							}
							AND = {
								exists = scope:second_sibling
								this = scope:second_sibling
							}
						}
						age >= 8
					}
					weight = {
						base = 1
						modifier = {
							add = {
								value = age
								multiply = 2
							}
						}
						modifier = {
							factor = 0.5
							ansf_is_bastard = yes
						}
					}
					save_scope_as = third_sibling
				}
				
				# are there more? display up to five
				if = {
					limit = {
						list_size = { name = extra_family value > 0 }
					}
					every_in_list = {
						list = extra_family
						remove_from_list = extra_family
					}
				}
				every_close_or_extended_family_member = {
					limit = {
						NOR = {
							is_parent_of = scope:target
							is_cousin_of = scope:target
							AND = {
								exists = scope:first_sibling
								this = scope:first_sibling
							}
							AND = {
								exists = scope:second_sibling
								this = scope:second_sibling
							}
							AND = {
								exists = scope:third_sibling
								this = scope:third_sibling
							}
						}
					}
					add_to_list = extra_family
				}
				every_former_spouse = {
					even_if_dead = yes
					limit = {
						NOR = {
							is_in_list = extra_family
							is_parent_of = scope:target
							AND = {
								exists = scope:target.primary_spouse
								this = scope:target.primary_spouse
							}
							AND = {
								exists = scope:first_sibling
								this = scope:first_sibling
							}
							AND = {
								exists = scope:second_sibling
								this = scope:second_sibling
							}
							AND = {
								exists = scope:third_sibling
								this = scope:third_sibling
							}
						}
					}
					add_to_list = extra_family
				}
				every_consort = {
					limit = {
						NOR = {
							is_in_list = extra_family
							is_parent_of = scope:target
							AND = {
								exists = scope:target.primary_spouse
								this = scope:target.primary_spouse
							}
							AND = {
								exists = scope:first_sibling
								this = scope:first_sibling
							}
							AND = {
								exists = scope:second_sibling
								this = scope:second_sibling
							}
							AND = {
								exists = scope:third_sibling
								this = scope:third_sibling
							}
						}
					}
					add_to_list = extra_family
				}
				every_relation = {
					type = lover
					limit = {
						NOR = {
							is_in_list = extra_family
							is_parent_of = scope:target
							AND = {
								exists = scope:target.primary_spouse
								this = scope:target.primary_spouse
							}
							AND = {
								exists = scope:first_sibling
								this = scope:first_sibling
							}
							AND = {
								exists = scope:second_sibling
								this = scope:second_sibling
							}
							AND = {
								exists = scope:third_sibling
								this = scope:third_sibling
							}
						}
					}
					add_to_list = extra_family
				}
				if = {
					limit = { exists = primary_spouse }
					primary_spouse = {
						every_close_family_member = {
							limit = {
								NOR = {
									this = scope:target
									is_parent_of = scope:target
									AND = {
										exists = scope:first_sibling
										this = scope:first_sibling
									}
									AND = {
										exists = scope:second_sibling
										this = scope:second_sibling
									}
									AND = {
										exists = scope:third_sibling
										this = scope:third_sibling
									}
								}
							}
							add_to_list = extra_family
						}
					}
				}
				if = {
					limit = {
						list_size = { name = extra_family value > 0 }
					}
					ordered_in_list = {
						list = extra_family
						order_by = {
							add = {
								value = age
								multiply = 2
							}
							if = {
								limit = { is_child_of = scope:target }
								add = 100
							}
							if = {
								limit = { is_player_heir_of = scope:target }
								add = 200
							}
							if = {
								limit = { ansf_is_bastard = yes }
								multiply = 0.5
							}
							if = {
								limit = {
									NOT = { is_close_or_extended_family_of = scope:target }
								}
								multiply = 0.2
							}
							if = {
								limit = {
									is_extended_family_of = scope:target
									is_ruler = no
								}
								multiply = 0.2
							}
							if = {
								limit = {
									is_extended_family_of = scope:target
									is_ruler = yes
								}
								multiply = 0.3
							}
						}
						max = 5
						check_range_bounds = no
						if = {
							limit = {
								NOT = { exists = root.var:portrait1 }
							}
							save_scope_as = fourth_sibling
							root = {
								set_variable = {
									name = portrait1
									value = scope:fourth_sibling
								}
							}
						}
						else_if = {
							limit = {
								NOR = {
									AND = {
										exists = scope:fourth_sibling
										this = scope:fourth_sibling
									}
									exists = root.var:portrait2
								}
							}
							save_scope_as = fifth_sibling
							root = {
								set_variable = {
									name = portrait2
									value = scope:fifth_sibling
								}
							}
						}
						else_if = {
							limit = {
								NOR = {
									AND = {
										exists = scope:fourth_sibling
										this = scope:fourth_sibling
									}
									AND = {
										exists = scope:fifth_sibling
										this = scope:fifth_sibling
									}
									exists = root.var:portrait3
								}
							}
							save_scope_as = sixth_sibling
							root = {
								set_variable = {
									name = portrait3
									value = scope:sixth_sibling
								}
							}
						}
						else_if = {
							limit = {
								NOR = {
									AND = {
										exists = scope:fourth_sibling
										this = scope:fourth_sibling
									}
									AND = {
										exists = scope:fifth_sibling
										this = scope:fifth_sibling
									}
									AND = {
										exists = scope:sixth_sibling
										this = scope:sixth_sibling
									}
									exists = root.var:portrait4
								}
							}
							save_scope_as = seventh_sibling
							root = {
								set_variable = {
									name = portrait4
									value = scope:seventh_sibling
								}
							}
						}
						else_if = {
							limit = {
								NOR = {
									AND = {
										exists = scope:fourth_sibling
										this = scope:fourth_sibling
									}
									AND = {
										exists = scope:fifth_sibling
										this = scope:fifth_sibling
									}
									AND = {
										exists = scope:sixth_sibling
										this = scope:sixth_sibling
									}
									AND = {
										exists = scope:seventh_sibling
										this = scope:seventh_sibling
									}
									exists = root.var:portrait5
								}
							}
							save_scope_as = eighth_sibling
							root = {
								set_variable = {
									name = portrait5
									value = scope:eighth_sibling
								}
							}
						}
					}
				}		
			}
			
			# for random generation, pre-determine how many siblings there will be
			if = {
				limit = {
					exists = scope:random_family
					scope:random_family = yes
					NOT = { has_variable = num_siblings }
				}
				random_list = {
					10 = {
						trigger = {
							exists = scope:will_make_parents
							scope:will_make_parents = yes
						}
						set_variable = { name = num_siblings value = 0 }
					}
					40 = {
						set_variable = { name = num_siblings value = 1 }
					}
					30 = {
						modifier = {
							add = -20
							scope:target = {
								any_sibling = { is_alive = yes }
							}
						}
						set_variable = { name = num_siblings value = 2 }
					}
					20 = {
						modifier = {
							add = -20
							scope:target = {
								any_sibling = { is_alive = yes }
							}
						}
						set_variable = { name = num_siblings value = 3 }
					}
				}
			}
		}
	}

	option = { #Mommy first
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.mother.yes
					}
					desc = ansf.mother.yes.other
				}
			}
		}
		custom_tooltip = ansf.add.mother
		trigger = {
			NOT = { exists = scope:target.mother }
		}
		remove_family_list_variables = yes
		trigger_event = ansf.1003
		
		ai_chance = {
			base = 100
		}
	}

	option = { #Create a spouse
		trigger = {
			scope:target = {
				is_adult = yes
				is_married = no
				can_marry_common_trigger = yes
			}
		}
		name = {
			trigger = {
				scope:target = root
				is_male = yes
			}
			text = ansf.wife.yes
		}
		name = {
			trigger = {
				scope:target = root
				is_female = yes
			}
			text = ansf.husband.yes
		}
		name = {
			trigger = {
				NOT = { scope:target = root }
			}
			text = ansf.spouse.other.yes
		}
		custom_tooltip = ansf.add.spouse
		remove_family_list_variables = yes
		trigger_event = ansf.2200
		
		ai_chance = {
			base = 0
		}
	}
	
	option = { #Yes, I have a brother.
		trigger = {
			scope:target = {
				exists = mother
				trigger_if = {
					limit = {
						exists = scope:is_existing_dynasty
						is_female = yes
					}
					NOR = {
						has_trait = bastard
						has_trait = legitimized_bastard
						has_trait = bastard_founder
					}
				}
			}
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:target = {
								this = root
								any_sibling = {
									is_alive = yes
									is_male = yes
									same_parents_as = { CHARACTER = scope:target }
								}
							}
						}
						desc = ansf.brother.yes.another
					}
					triggered_desc = {
						trigger = {
							scope:target = {
								any_sibling = {
									is_alive = yes
									is_male = yes
									same_parents_as = { CHARACTER = scope:target }
								}
							}
						}
						desc = ansf.brother.other.yes.another
					}
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.brother.yes
					}
					desc = ansf.brother.other.yes
				}
			}
		}
		
		custom_tooltip = ansf.add.brother

		scope:new_dummy_male = { save_scope_as = sibling }
		increment_random_siblings_effect = yes
		remove_family_list_variables = yes
		trigger_event = ansf.1005
		
		ai_chance = {
			base = 45
			modifier = {
				factor = 0
				has_variable = num_siblings
				var:num_siblings < 1
			}
		}
	}
	
	option = { #Yes, I have a sister.
		trigger = {
			exists = scope:target.mother
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:target = {
								this = root
								any_sibling = {
									is_alive = yes
									is_female = yes
									same_parents_as = { CHARACTER = scope:target }
								}
							}
						}
						desc = ansf.sister.yes.another
					}
					triggered_desc = {
						trigger = {
							scope:target = {
								any_sibling = {
									is_alive = yes
									is_female = yes
									same_parents_as = { CHARACTER = scope:target }
								}
							}
						}
						desc = ansf.sister.other.yes.another
					}
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.sister.yes
					}
					desc = ansf.sister.other.yes
				}
			}
		}		
		
		custom_tooltip = ansf.add.sister
		
		scope:new_dummy_female = { save_scope_as = sibling }
		increment_random_siblings_effect = yes
		remove_family_list_variables = yes
		trigger_event = ansf.1005

		ai_chance = {
			base = 45
			modifier = {
				factor = 0
				has_variable = num_siblings
				var:num_siblings < 1
			}
		}
	}

	option = { #Yes, I have a half-brother.
		trigger = {
			exists = scope:target.mother
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:target = {
								any_sibling = {
									is_alive = yes
									is_male = yes
									NOT = {
										same_parents_as = { CHARACTER = scope:target }
									}
								}
							}
						}
						desc = ansf.halfbrother.another.yes
					}
					desc = ansf.halfbrother.yes
				}
			}
		}
		custom_tooltip = ansf.add.halfbrother
		scope:new_dummy_male = { save_scope_as = halfsibling }
		increment_random_siblings_effect = yes
		remove_family_list_variables = yes
		trigger_event = ansf.2100
		
		ai_chance = {
			base = 5
			modifier = {
				factor = 0
				has_variable = num_siblings
				var:num_siblings < 1
			}
		}
	}
	
	option = { #Yes, I have a half-sister.
		trigger = {
			exists = scope:target.mother
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:target = {
								any_sibling = {
									is_alive = yes
									is_female = yes
									NOT = {
										same_parents_as = { CHARACTER = scope:target }
									}
								}
							}
						}
						desc = ansf.halfsister.another.yes
					}
					desc = ansf.halfsister.yes
				}
			}
		}
		custom_tooltip = ansf.add.halfsister
		scope:new_dummy_female = { save_scope_as = halfsibling }
		increment_random_siblings_effect = yes
		remove_family_list_variables = yes
		trigger_event = ansf.2100
		
		ai_chance = {
			base = 5
			modifier = {
				factor = 0
				has_variable = num_siblings
				var:num_siblings < 1
			}
		}
	}
	
	option = { #Create a child
		trigger = {
			scope:target = {
				this = root
				trigger_if = {
					limit = { is_male = yes }
					age > define:NChildbirth|MIN_MALE_REPRODUCTION_AGE
				}
				trigger_else = {
					age > define:NChildbirth|MIN_FEMALE_REPRODUCTION_AGE
				}
			}
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:target = {
								any_child = { is_alive = yes }
							}
						}
						desc = ansf.child.another.yes
					}
					desc = ansf.child.yes
				}
			}
		}
		
		custom_tooltip = ansf.add.child
		remove_family_list_variables = yes
		trigger_event = ansf.2300
		
		ai_chance = {
			base = 0
		}
	}

	option = { #Nevermind
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.family.no
					}
					desc = ansf.family.no.other
				}
			}
		}
		if = {
			limit = {
				scope:target = root
				OR = {
					AND = {
						exists = father
						father = {
							is_alive = yes
							is_ruler = no
						}
					}
					AND = {
						exists = mother
						mother = {
							is_alive = yes
							is_ruler = no
							exists = house
							house = root.house
							is_house_head = yes
						}
					}
				}
			}
			custom_tooltip = ansf.goto.father.fate.tt
			ansf_remove_all_gui_variables = yes
			remove_family_list_variables = yes
			trigger_event = ansf.1010
		}
		else = {
			custom_tooltip = ansf.add.done
			ansf_remove_all_flags_variables = yes
			remove_family_list_variables = yes
		}
		
		if = {
			limit = {
				AND = {
					exists = scope:random_family
					scope:random_family = yes
				}
				scope:target_liege = { is_ai = no }
				any_in_list = {
					list = new_player_courtiers
					NOT = { is_courtier_of = scope:target_liege }
					count > 0
				}
			}
			scope:target_liege = {
				send_interface_message = {
					type = event_generic_neutral
					title = asnf.new.courtiers.tt
					right_icon = scope:target
					every_in_list = {
						list = new_player_courtiers
						limit = { NOT = { is_courtier_of = scope:target_liege } }
						save_temporary_scope_as = new_courtier
						scope:target_liege = { add_courtier = scope:new_courtier }
					}
				}
			}
		}
		
		ai_chance = {
			base = 100
			modifier = {
				factor = 0
				OR = {
					NOT = { exists = scope:target.mother }
					AND = {
						has_variable = num_siblings
						var:num_siblings > 0
					}
				}
			}
		}
	}
}

ansf.1005 = { #Is this your sibling?
	type = character_event
	window = ansf_event_window
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:ansf_family_member = { is_female = yes }
				}
				desc = ansf.sister.event.t
			}
			desc = ansf.brother.event.t
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					scope:ansf_family_member = { is_female = yes }
				}
				desc = ansf.sister.event.d
			}
			triggered_desc = {
				trigger = {
					scope:ansf_family_member = { is_female = yes }
				}
				desc = ansf.sister.event.other.d
			}
			triggered_desc = {
				trigger = {
					scope:target = root
					scope:ansf_family_member = { is_male = yes }
				}
				desc = ansf.brother.event.d
			}
			desc = ansf.brother.event.other.d
		}
	}
	theme = family
	center_portrait = scope:ansf_family_member
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = yes } }
		reference = sitting_room
	}
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = no } }
		reference = courtyard
	}

	immediate = {
		hidden_effect = {
			# we need the eldest legitimate half-sibling
			find_eldest_legitimate_halfsibling = { MOTHER = scope:target.mother FATHER = scope:target.father }
			
			#Find some appropriate options
			random_list = {
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Diplomat
						location = scope:target.location
						culture = scope:target.culture
						faith = scope:target.faith
						gender = scope:sibling
						dynasty = none
						age = {
							integer_range = {
								min = age_legitimate_sibling_min
								max = age_legitimate_sibling_max
							}
						}
						mother = scope:target.mother
						father = scope:target.father						
						template = olympia_diplomat_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = { flag = was_diplomacy days = 365 }
							post_creation_sibling_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						add = -15
						NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:sibling } }
					}
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
						}
					}
					create_character = { #Martial
						location = scope:target.location
						culture = scope:target.culture
						faith = scope:target.faith
						gender = scope:sibling
						dynasty = none
						age = {
							integer_range = {
								min = age_legitimate_sibling_min
								max = age_legitimate_sibling_max
							}
						}
						mother = scope:target.mother
						father = scope:target.father
						template = olympia_martial_template
						prowess = { 10 15 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = { flag = was_martial days = 365 }
							post_creation_sibling_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Steward
						location = scope:target.location
						culture = scope:target.culture
						faith = scope:target.faith
						gender = scope:sibling
						dynasty = none
						age = {
							integer_range = {
								min = age_legitimate_sibling_min
								max = age_legitimate_sibling_max
							}
						}
						mother = scope:target.mother
						father = scope:target.father
						template = olympia_stewardship_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = { flag = was_stewardship days = 365 }
							post_creation_sibling_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Intrigue
						location = scope:target.location
						culture = scope:target.culture
						faith = scope:target.faith
						gender = scope:sibling
						dynasty = none
						age = {
							integer_range = {
								min = age_legitimate_sibling_min
								max = age_legitimate_sibling_max
							}
						}
						mother = scope:target.mother
						father = scope:target.father
						template = olympia_intrigue_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = { flag = was_intrigue days = 365 }
							post_creation_sibling_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Learning
						location = scope:target.location
						culture = scope:target.culture
						faith = scope:target.faith
						gender = scope:sibling
						dynasty = none
						age = {
							integer_range = {
								min = age_legitimate_sibling_min
								max = age_legitimate_sibling_max
							}
						}
						mother = scope:target.mother
						father = scope:target.father
						template = olympia_learning_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = { flag = was_learning days = 365 }
							post_creation_sibling_effect = yes
						}
					}
				}
			}
			
			if = {
				limit = { exists = scope:mother_father_eldest_child }
				clear_saved_scope = mother_father_eldest_child
			}
		}
	}

	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	#This is my sibling
	option = {
		trigger = {
			custom_description = {
				text = ansf_cannot_be_twin
				subject = scope:ansf_family_member
				NOT = {
					scope:ansf_family_member = {
						any_sibling = {
							even_if_dead = yes
							has_trait = twin
							mother = scope:ansf_family_member.mother
							father = scope:ansf_family_member.father
							age_including_death_age = scope:ansf_family_member.age
						}
					}
				}
			}
		}
		name = ansf.yes.sibling		
		show_as_unavailable = {
			scope:ansf_family_member = {
				any_sibling = {
					even_if_dead = yes
					has_trait = twin
					mother = scope:ansf_family_member.mother
					father = scope:ansf_family_member.father
					age_including_death_age = scope:ansf_family_member.age
				}
			}
		}
		
		if = {
			limit = {
				OR = {
					scope:ansf_family_member.father = { is_playable_character = yes }
					scope:ansf_family_member.mother = { is_playable_character = yes }
				}
				scope:target = {
					NAND = {
						has_trait = bastard_founder
						is_playable_character = yes
					}
				}
			}
			scope:ansf_family_member = {
				random_parent = {
					limit = { is_playable_character = yes }
					weight = {
						base = 1
						modifier = {
							add = 100
							dynasty = scope:ansf_family_member.dynasty
						}
					}
					if = {
						limit = {
							NOT = {
								scope:ansf_family_member = { is_courtier_of = prev }
							}
						}
						add_courtier = scope:ansf_family_member
					}
				}
			}
		}
		else = {
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}
		
		scope:ansf_family_member = {
			if = {
				limit = {
					exists = mother
					mother = { has_dynasty = yes }
					OR = {
						ansf_is_bastard = yes
						mother = { matrilinear_marriage = yes }
					}
				}
				set_house = mother.house
			}
			else_if = {
				limit = {
					exists = father
					father = { has_dynasty = yes }
				}
				set_house = father.house
			}
		}
		
		scope:ansf_family_member = {
			random_sibling = {
				even_if_dead = yes
				limit = {
					NOT = { has_trait = twin }
					mother = scope:ansf_family_member.mother
					father = scope:ansf_family_member.father
					age_including_death_age = scope:ansf_family_member.age
				}
				add_trait = twin
			}
		}
		
		clear_saved_scope = sibling
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 100
		}
	}
	
	#This isn't my sibling... redo
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.redosibling
				}
			}
		}
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
		}
		else = {
			custom_tooltip = ansf.reroll
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
			add_internal_flag = special
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.1005
		
		ai_chance = {
			base = 0
		}
	}
	
	#I have no sibling
	option = {
		name = ansf.no.sibling
		custom_tooltip = ansf.sibling.none

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }

		clear_saved_scope = sibling
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.1004

		ai_chance = {
			base = 1
		}
	}
}

ansf.1010 = { #The family news
	type = character_event
	window = ansf_group_event_window
	title = ansf.family.news.t
	desc = ansf.family.news.desc
	theme = family
	
	left_portrait = {
		character = scope:assembled_family_1
		animation = sadness
	}
	center_portrait = {
		character = scope:assembled_family_4
		animation = disbelief
	}
	right_portrait = {
		character = root
		animation = shame
	}
	lower_right_portrait = {
		character = scope:assembled_family_3
		animation = throne_room_conversation_3
	}
	lower_left_portrait = {
		character = scope:assembled_family_2
		animation = worry
	}
	
	override_background = { reference = throne_room }
	
	immediate = {
		# get primary death parent
		if = {
			limit = {
				exists = mother
				mother = {
					is_alive = yes
					is_ruler = no
					exists = house
					house = root.house
					is_house_head = yes
				}
			}
			mother = { save_scope_as = dead_parent }
		}
		else = {
			father = { save_scope_as = dead_parent }
		}
		# get other parent
		random_parent = {
			limit = {
				NOT = { this = scope:dead_parent }
				is_alive = yes
				is_ruler = no
			}
			save_scope_as = other_parent
		}
		# pick two random family members for portraits
		every_close_or_extended_family_member = {
			limit = {
				age >= 8
				NOR = {
					this = scope:dead_parent
					this = scope:other_parent
				}
			}
			add_to_temporary_list = assembled_family
		}
		every_spouse = {
			limit = {
				NOT = { is_in_list = assembled_family }
			}
			add_to_temporary_list = assembled_family
			every_parent = {
				limit = {
					is_in_the_same_court_as = root
					NOT = { is_in_list = assembled_family }
				}
				add_to_temporary_list = assembled_family
			}
		}
		random_in_list = {
			list = assembled_family
			limit = { age >= 8 }
			weight = {
				base = 1
				modifier = {
					add = {
						value = age
						multiply = 2
					}
				}
				modifier = {
					add = 100
					is_player_heir_of = root
				}
				modifier = {
					factor = 0.3
					NOT = { is_close_family_of = root }
				}
			}
			save_scope_as = assembled_family_1
		}
		random_in_list = {
			list = assembled_family
			limit = {
				age >= 8
				NOT = { this = scope:assembled_family_1 }
			}
			weight = {
				base = 1
				modifier = {
					add = {
						value = age
						multiply = 2
					}
				}
				modifier = {
					add = 100
					is_player_heir_of = root
				}
				modifier = {
					factor = 0.3
					NOT = { is_close_family_of = root }
				}
			}
			save_scope_as = assembled_family_2
		}
		random_in_list = {
			list = assembled_family
			limit = {
				age >= 8
				NOR = {
					this = scope:assembled_family_1
					this = scope:assembled_family_2
				}
			}
			weight = {
				base = 1
				modifier = {
					add = {
						value = age
						multiply = 2
					}
				}
				modifier = {
					add = 100
					is_player_heir_of = root
				}
				modifier = {
					factor = 0.3
					NOT = { is_close_family_of = root }
				}
			}
			save_scope_as = assembled_family_3
		}
		random_in_list = {
			list = assembled_family
			limit = {
				age >= 8
				NOR = {
					this = scope:assembled_family_1
					this = scope:assembled_family_2
					this = scope:assembled_family_3
				}
			}
			weight = {
				base = 1
				modifier = {
					add = {
						value = age
						multiply = 2
					}
				}
				modifier = {
					add = 100
					is_player_heir_of = root
				}
				modifier = {
					factor = 0.3
					NOT = { is_close_family_of = root }
				}
			}
			save_scope_as = assembled_family_4
		}
	}
	
	option = {
		name = ansf.househead.dead #Parent is dead
		trigger = {
			exists = scope:dead_parent
		}
		ansf_remove_all_flags_variables = yes
		custom_tooltip = ansf.househead.dead.tt
		trigger_event = ansf.1011
	}
	option = {
		name = ansf.both.dead #Both parents are dead
		trigger = {
			exists = scope:dead_parent
			exists = scope:other_parent
		}
		ansf_remove_all_flags_variables = yes
		scope:dead_parent = {
			death = { death_reason = death_accident }
		}
		scope:other_parent = {
			death = { death_reason = death_accident }
		}
	}
	option = {
		name = ansf.new.househead #Taking over as house head
		trigger = {
			is_house_head = no
			house.house_head = { is_ruler = no }
		}
		ansf_remove_all_flags_variables = yes
		house = { set_house_head = root }
	}
	option = {
		name = ansf.nothing.new #No news
		custom_tooltip = ansf.nothing.new.tt
		ansf_remove_all_flags_variables = yes
	}
	option = {
		name = ansf.go.back #I made a mistake, start over with siblings
		custom_tooltip = ansf.go.back.tooltip
		trigger_event = ansf.1004 #Main Family Menu
	}
}

ansf.1011 = { #How does my parent die?
	type = character_event
	window = ansf_event_window
	title = ansf.death.event.t
	desc = ansf.death.event.d
	theme = death
	center_portrait = {
		character = scope:dead_parent
		animation = sadness
	}
	override_background = { reference = alley_night }
	
	immediate = {
		if = {
			limit = {
				exists = mother
				mother = {
					is_alive = yes
					OR = {
						has_personality_malicious_trigger = yes
						has_trait = adulterer
						father = { has_trait = adulterer }
					}
				}
			}
			mother = { save_scope_as = ansf_mother }
		}
		random_sibling = {
			limit = {
				is_adult = yes
				has_personality_malicious_trigger = yes
			}
			save_scope_as = ansf_sibling
		}
		random_courtier = {
			limit = {
				is_adult = yes
				has_personality_malicious_trigger = yes
			}
			save_scope_as = ansf_courtier
		}
		random_vassal = {
			limit = {
				is_adult = yes
				has_personality_benevolent_trigger = no
				has_personality_submissive_trigger = no
				has_personality_levelheaded_trigger = no
			}
			save_scope_as = ansf_vassal
		}
		random_neighboring_realm_same_rank_owner = {
			limit = {
				is_adult = yes
				has_personality_benevolent_trigger = no
				has_personality_submissive_trigger = no
				has_personality_levelheaded_trigger = no
			}
			save_scope_as = ansf_neighbor_ruler
		}
	}
	
	#Vanished?
	option = {
		name = ansf.death.vanish
		custom_tooltip = ansf.death.vanish.tooltip
		
		hidden_effect = {
			scope:dead_parent = {
				death = { death_reason = death_vanished }
			}
		}
	}
	
	#Random?
	option = {
		name = ansf.death.random
		custom_tooltip = ansf.death.random.tooltip
	
		hidden_effect = {
			scope:dead_parent = {
				random_list = {
					1 = {
						death = { death_reason = death_hunting_accident }
					}
					1 = {
						death = { death_reason = death_horse_riding_accident }
					}
					1 = {
						death = { death_reason = death_drowned }
					}
					1 = {
						death = { death_reason = death_accident }
					}
					1 = {
						add_trait = ill
						death = { death_reason = death_ill }
					}
					1 = {
						death = { death_reason = death_stress }
					}
				}
			}
		}
	}
	
	#I killed him
	option = {
		trigger = {
			is_adult = yes
		}
		name = ansf.death.murder
		custom_tooltip = ansf.death.murder.tooltip
		
		stress_impact = {
			base = medium_stress_gain
			sadistic = medium_stress_loss
			callous = medium_stress_loss
			ambitious = minor_stress_loss
			content = medium_stress_gain
			compassionate = major_stress_gain
		}
		
		hidden_effect = {
			scope:dead_parent = {
				death = {
					death_reason = death_murder
					killer = root
				}
				root = {
					add_secret = {
						type = secret_murder
						target = scope:dead_parent
					}
				}
			}
		}
	}
	
	#Murder?
	option = {
		trigger = {
			OR = {
				exists = scope:ansf_mother
				exists = scope:ansf_sibling
				exists = scope:ansf_courtier
				exists = scope:ansf_vassal
				exists = scope:ansf_neighbor_ruler
			}
		}
		name = ansf.death.mysterious
		custom_tooltip = ansf.death.mysterious.tooltip
		
		hidden_effect = {
			scope:dead_parent = {
				random_list = {
					2 = {
						trigger = {
							exists = scope:ansf_mother
						}
						death = {
							death_reason = death_murder
							killer = scope:ansf_mother
						}
						scope:ansf_mother = {
							add_secret = {
								type = secret_murder
								target = scope:ansf_father
							}
						}
					}
					1 = {
						trigger = {
							exists = scope:ansf_sibling
						}
						death = {
							death_reason = death_murder
							killer = scope:ansf_sibling
						}
						scope:ansf_sibling = {
							add_secret = {
								type = secret_murder
								target = scope:dead_parent
							}
						}
					}
					5 = {
						trigger = {
							exists = scope:ansf_vassal
						}
						death = {
							death_reason = death_murder
							killer = scope:ansf_vassal
						}
						scope:ansf_vassal = {
							add_secret = {
								type = secret_murder
								target = scope:dead_parent
							}
						}
					}
					2 = {
						trigger = {
							exists = scope:ansf_courtier
						}
						death = {
							death_reason = death_murder
							killer = scope:ansf_courtier
						}
						scope:ansf_courtier = {
							add_secret = {
								type = secret_murder
								target = scope:dead_parent
							}
						}
					}
					10 = {
						trigger = {
							exists = scope:ansf_neighbor_ruler
						}
						death = {
							death_reason = death_murder
							killer = scope:ansf_neighbor_ruler
						}
						scope:ansf_neighbor_ruler = {
							add_secret = {
								type = secret_murder
								target = scope:dead_parent
							}
						}
					}
				}
			}
		}
	}
	
	#Ill, but still alive
	option = {
		name = ansf.death.none
		custom_tooltip = ansf.death.none.tooltip
		if = {
			limit = {
				house.house_head = { this = scope:dead_parent }
			}
			house = { set_house_head = root }
		}
		hidden_effect = {
			scope:dead_parent = {
				contract_disease_effect = { DISEASE = cancer TREATMENT_EVENT = yes }
			}
		}
	}
}

ansf.1020 = { #Parent from an existing dynasty
	type = character_event
	window = ansf_dynasty_event_window
	title = ansf.existing.event.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.existing.event.d
			}
			desc = ansf.existing.event.other.d
		}
	}
	theme = family

	center_portrait = {
		character = scope:target
		animation = thinking
	}	
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = yes } }
		reference = garden
	}
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = no } }
		reference = temple
	}

	immediate = {
		set_variable = { name = ansf_family_character value = scope:ansf_family_member }
	}

	# Selection is their father
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_grandparents
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.grandparent
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		custom_tooltip = ansf.yes.existing1.grandparent.tt
		root.var:potential_relative_1 = {
			if = {
				limit = { is_male = yes }
				root = {
					set_variable = {
						name = actual_grandfather
						value = root.var:potential_relative_1
					}
				}
			}
			else = {
				root = {
					set_variable = {
						name = actual_grandmother
						value = root.var:potential_relative_1
					}
				}
			}
			if = {
				limit = {
					should_be_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.bastard.tt
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					any_consort = {
						even_if_dead = yes
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
				}
				random_consort = {
					even_if_dead = yes
					limit = {
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
					if = {
						limit = { is_female = yes }
						root = {
							set_variable = {
								name = actual_grandmother
								value = prev
							}
						}
					}
					else = {
						root = {
							set_variable = {
								name = actual_grandfather
								value = prev
							}
						}
					}
				}
			}
		}
		
		if = {
			limit = {
				NOT = { root.var:potential_relative_1.culture = scope:target.culture }
			}
			custom_tooltip = ansf.yes.existing1.culture.tt
		}
		if = {
			limit = {
				NOT = { root.var:potential_relative_1.faith = scope:target.faith }
			}
			custom_tooltip = ansf.yes.existing1.faith.tt
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.1021
	}

	# Selection is their brother
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_piblings
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.pibling
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		custom_tooltip = ansf.yes.existing1.pibling.tt
		root.var:potential_relative_1 = {
			root = {
				set_variable = {
					name = actual_grandfather
					value = root.var:potential_relative_1.father
				}
			}
			if = {
				limit = {
					should_be_fathers_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.bastard.tt
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					should_be_legitimate_bastard = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.legit.bastard.tt
				root = {
					set_variable = {
						name = will_be_legitimate_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					father = {
						any_consort = {
							even_if_dead = yes
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
					}
				}
				father = {
					random_consort = {
						even_if_dead = yes
						limit = {
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
						root = {
							set_variable = {
								name = actual_grandmother
								value = prev
							}
						}
					}
				}
			}
		}
		
		if = {
			limit = {
				NOT = { root.var:potential_relative_1.culture = scope:target.culture }
			}
			custom_tooltip = ansf.yes.existing1.culture.tt
		}
		if = {
			limit = {
				NOT = { root.var:potential_relative_1.faith = scope:target.faith }
			}
			custom_tooltip = ansf.yes.existing1.faith.tt
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.1021
	}
	
	# Go back
	option = {
		name = ansf.no.existing
		add_character_flag = no_creation_override
		
		remove_variable ?= potential_relative_1
		remove_variable ?= ansf_family_character
		
		custom_tooltip = ansf.no.me.existing.tt
		
		if = {
			limit = { scope:ansf_family_member = { is_male = yes } }
			trigger_event = ansf.1002
		}
		else = {
			trigger_event = ansf.1003
		}
	}
}

ansf.1021 = { #Generating the parent from the new dynasty
	type = character_event
	window = ansf_event_window
	title = ansf.existing.event2.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.existing.grandparent.d
			}
			triggered_desc = {
				trigger = {
					scope:target = root
					NOR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.existing.pibling.d
			}
			triggered_desc = {
				trigger = {
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.existing.grandparent.other.d
			}
			desc = ansf.existing.pibling.other.d
		}
	}
	
	theme = family
	center_portrait = scope:ansf_family_member	
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = yes } }
		reference = garden
	}
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = no } }
		reference = temple
	}

	immediate = {
		hidden_effect = {
			if = {
				limit = {
					has_variable = actual_grandfather
					has_variable = actual_grandmother
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					has_variable = actual_grandfather
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
				}
			}
			else = {
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								set_house = scope:actual_relative.house
								post_creation_existing_effect = yes
							}
						}
					}
				}
			}	
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}
	
	# Join their dynasty
	option = {
		trigger = {
			custom_description = {
				text = cannot_change_dynasty
				subject = scope:target
				scope:target = { ansf_can_change_dynasty = yes }
			}
			custom_description = {
				text = cannot_have_family_title
				subject = scope:target
				scope:target = {
					NOT = {
						any_held_title = { is_noble_family_title = yes }
					}
				}
			}
			trigger_if = {
				limit = { scope:ansf_family_member = { is_female = yes } }
				custom_description = {
					text = already_joined_father_dynasty
					subject = scope:target
					NOT = { scope:target.father.dynasty ?= scope:target.dynasty }
				}
			}
			scope:ansf_family_member.culture = scope:target.culture
			NOR = {
				exists = scope:is_peasant_leader
				exists = scope:is_existing_dynasty
				scope:ansf_family_member.dynasty = scope:target.dynasty
			}
		}
		show_as_unavailable = {
			OR = {
				scope:target = { ansf_can_change_dynasty = no }
				scope:target = { any_held_title = { is_noble_family_title = yes } }
				exists = scope:is_peasant_leader
				exists = scope:is_existing_dynasty
				scope:ansf_family_member.dynasty = scope:target.dynasty
				NOT = { scope:ansf_family_member.culture = scope:target.culture }
				AND = {
					scope:ansf_family_member = { is_female = yes }
					scope:target.father.dynasty ?= scope:target.dynasty
				}
			}
		}
		name = ansf.yes.existing.take.dynasty
		
		custom_tooltip = ansf.take.dynasty.tooltip
		
		connect_to_relative_effect = yes
		set_new_target_house_effect = { RELATIVE = scope:ansf_family_member }
		
		if = {
			limit = { scope:ansf_family_member = { is_male = yes } }
			scope:target = { connect_father_to_me_effect = yes }
		}
		else = {
			scope:target = { connect_mother_to_me_effect = yes }
		}
		
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
	
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandfather
		remove_variable ?= actual_grandmother
		clear_saved_scope ?= source_parent
		trigger_event = ansf.2001
	}
	
	# That's them
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.yes.existing.proceed
					}
					desc = ansf.yes.existing.proceed.other
				}
			}
		}
		
		connect_to_relative_effect = yes
	
		if = {
			limit = { scope:ansf_family_member = { is_male = yes } }
			scope:target = { connect_father_to_me_effect = yes }
		}
		else = {
			scope:target = { connect_mother_to_me_effect = yes }
		}
		
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
	
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandfather
		remove_variable ?= actual_grandmother
		clear_saved_scope ?= source_parent
		trigger_event = ansf.2001
	}
	
	# Redo
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.no.existing.reroll
				}
			}
		}
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.reroll.existing
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
	
		trigger_event = ansf.1021
	}
	
	# Never mind
	option = {
		name = ansf.no.existing.leave
		custom_tooltip = ansf.no.existing.leave.tt
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }

		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandfather
		remove_variable ?= actual_grandmother
		clear_saved_scope ?= source_parent
		ansf_remove_all_gui_variables = yes
		
		if = {
			limit = { scope:ansf_family_member = { is_male = yes } }
			trigger_event = ansf.1002
		}
		else = {
			trigger_event = ansf.1003
		}
	}
}

ansf.1022 = { #I'm from an existing dynasty
	type = character_event
	window = ansf_dynasty_event_window
	title = ansf.me.existing.event.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.me.existing.event.d
			}
			triggered_desc = {
				trigger = { scope:target = { is_landed = yes } }
				desc = ansf.other.ruler.existing.event.d
			}
			desc = ansf.other.existing.event.d
		}
	}
	theme = family

	center_portrait = {
		character = root
		animation = thinking
	}
	override_background = {
		trigger = { scope:target = { is_landed = yes } }
		reference = throne_room
	}
	override_background = {
		trigger = { scope:target = { is_landed = no } }
		reference = study
	}

	immediate = {
		set_variable = { name = ansf_family_character value = scope:ansf_family_member }
	}

	# Selection is my father
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_grandparents
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.me.existing1.grandparent
		
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		#first set them as your parent
		root.var:potential_relative_1 = { add_character_flag = is_different_DNA }
		if = {
			limit = { root.var:potential_relative_1 = { is_male = yes } }
			scope:target = { set_father = root.var:potential_relative_1 }
		}
		else = {
			scope:target = { set_mother = root.var:potential_relative_1 }
		}
		#then check to see if you should be a bastard
		if = {
			limit = {
				root.var:potential_relative_1 = {
					should_be_bastard_trigger = { RELATIVE = scope:target }
				}
			}
			scope:target = { add_trait = bastard_founder }
			custom_tooltip = ansf.yes.me.existing.bastard.tt
			hidden_effect = {
				if = {
					limit = {
						root.var:potential_relative_1 = {
							NOR = {
								has_trait = fornicator
								has_trait = adulterer
							}
						}
					}
					root.var:potential_relative_1 = {
						add_adulterer_fornicator_trait_or_nothing_effect = yes
						if = {
							limit = {
								is_alive = yes
								exists = liege
							}
							consume_all_criminal_reasons_effect = {
								LIEGE = root.var:potential_relative_1.liege
								CRIMINAL = root.var:potential_relative_1
							}
						}
					}
				}
			}
		}
		else = {
			if = {
				limit = {
					root.var:potential_relative_1 = {
						any_consort = {
							even_if_dead = yes
							could_have_been_parent_of = { CHILD = scope:target }
						}
					}
				}
				root.var:potential_relative_1 = {
					random_consort = {
						even_if_dead = yes
						limit = {
							could_have_been_parent_of = { CHILD = scope:target }
						}
						if = {
							limit = { is_female = yes }
							scope:target = { set_mother = prev }
						}
						else = {
							scope:target = { set_father = prev }
						}
					}
				}
				custom_tooltip = ansf.yes.me.existing.bothparents.tt
			}
			else = {
				custom_tooltip = ansf.yes.me.existing.oneparent.tt
			}
			set_new_target_house_effect = { RELATIVE = root.var:potential_relative_1 }
		}
		
		if = {
			limit = {
				root.var:potential_relative_1 = {
					is_playable_character = no
					is_courtier = no
				}
			}
			root.var:potential_relative_1 = {
				every_traveling_family_member = {
					limit = {
						is_playable_character = no
						is_courtier = no
					}
					scope:target_liege = { add_courtier = prev }
				}
			}
		}

		check_twin_effect = { CHARACTER = scope:target }
		
		save_scope_value_as = {
			name = is_existing_dynasty
			value = yes
		}
		
		begin_family_creation_effect = yes
	}
	# Selection is my brother
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_piblings
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.me.existing1.pibling
		
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		#first set their father to be yours
		scope:target = { set_father = root.var:potential_relative_1.father }
		#then check to see if you should be a bastard
		if = {
			limit = {
				root.var:potential_relative_1 = {
					should_be_fathers_bastard_trigger = { RELATIVE = scope:target }
				}
			}
			scope:target = { add_trait = bastard_founder }
			custom_tooltip = ansf.yes.me.existing.bastard.tt
			hidden_effect = {
				if = {
					limit = {
						root.var:potential_relative_1.father = {
							is_alive = yes
							NOR = {
								has_trait = fornicator
								has_trait = adulterer
							}
						}
					}
					root.var:potential_relative_1.father = {
						add_adulterer_fornicator_trait_or_nothing_effect = yes
						if = {
							limit = {
								is_alive = yes
								exists = liege
							}
							consume_all_criminal_reasons_effect = {
								LIEGE = root.var:potential_relative_1.father.liege
								CRIMINAL = root.var:potential_relative_1.father
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				root.var:potential_relative_1 = {
					should_be_legitimate_bastard = { RELATIVE = scope:target }
				}
			}
			scope:target = { add_trait = legitimized_bastard }
			custom_tooltip = ansf.yes.me.existing.oneparent.tt
			set_new_target_house_effect = { RELATIVE = root.var:potential_relative_1 }
			hidden_effect = {
				if = {
					limit = {
						root.var:potential_relative_1.father = {
							NOR = {
								has_trait = fornicator
								has_trait = adulterer
							}
						}
					}
					root.var:potential_relative_1.father = {
						add_adulterer_fornicator_trait_or_nothing_effect = yes
						if = {
							limit = {
								is_alive = yes
								exists = liege
							}
							consume_all_criminal_reasons_effect = {
								LIEGE = root.var:potential_relative_1.father.liege
								CRIMINAL = root.var:potential_relative_1.father
							}
						}
					}
				}
			}
		}
		else = {
			if = {
				limit = {
					root.var:potential_relative_1.father = {
						any_consort = {
							even_if_dead = yes
							could_have_been_parent_of = { CHILD = scope:target }
						}
					}
				}
				root.var:potential_relative_1.father = {
					random_consort = {
						even_if_dead = yes
						limit = {
							could_have_been_parent_of = { CHILD = scope:target }
						}
						scope:target = { set_mother = prev }
					}
				}
				custom_tooltip = ansf.yes.me.existing.bothparents.tt
			}
			else = {
				custom_tooltip = ansf.yes.me.existing.oneparent.tt
			}
			set_new_target_house_effect = { RELATIVE = root.var:potential_relative_1 }
		}
		
		if = {
			limit = {
				root.var:potential_relative_1 = {
					is_playable_character = no
					is_courtier = no
				}
			}
			root.var:potential_relative_1 = {
				every_traveling_family_member = {
					limit = {
						is_playable_character = no
						is_courtier = no
					}
					scope:target_liege = { add_courtier = prev }
				}
			}
		}

		check_twin_effect = { CHARACTER = scope:target }
		
		save_scope_value_as = { name = is_existing_dynasty value = yes }
		remove_variable = potential_relative_1
		remove_variable = ansf_family_character
		clear_saved_scope = actual_relative
		
		begin_family_creation_effect = yes
	}
	# Go back
	option = {
		name = ansf.no.me.existing
		
		custom_tooltip = ansf.no.me.existing.tt
		
		remove_variable = potential_relative_1
		remove_variable = ansf_family_character
		clear_saved_scope = actual_relative
		trigger_event = ansf.1001
	}
}

#######################
#Main Character Screen#
#######################

ansf.2001 = { #Change traits?
	type = character_event
	window = ansf_event_window
	title = ansf.traits.event.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:ansf_family_member = {
						is_sibling_of = scope:target
						age < 13
					}
				}
				desc = ansf.traits.child.event.d
			}
			desc = ansf.traits.event.d
		}
	}
	
	theme = family
	center_portrait = scope:ansf_family_member
	override_background = { reference = throne_room }
	
	widgets = {
		widget = {
			gui = "event_window_widget_edit_traits"
			container = "custom_widgets_container"
		}
	}
	
	immediate = {
		scope:ansf_family_member = { remove_character_flag ?= no_hat }
	}
	
	#Done with traits
	option = {
		name = ansf.done
		custom_tooltip = ansf.done.tooltip
		hidden_effect = {
			scope:ansf_family_member = {
				remove_variable ?= trait_start
				remove_variable ?= current_select_trait
				remove_variable ?= will_add_new_trait
				force_character_skill_recalculation = yes
			}
		}
		
		if = {
			limit = { exists = var:is_otherparent_creation }
			remove_variable = is_otherparent_creation
			trigger_event = ansf.2105
		}
		else_if = {
			limit = { exists = var:is_player_otherparent_creation }
			remove_variable = is_player_otherparent_creation
			trigger_event = ansf.2305
		}
		else_if = {
			limit = {
				exists = scope:spouse_creation
				scope:spouse_creation = flag:father_in_law
			}
			trigger_event = ansf.2202
		}
		else_if = {
			limit = {
				exists = scope:spouse_creation
				scope:spouse_creation = flag:mother_in_law
			}
			trigger_event = ansf.2203
		}
		else_if = {
			limit = {
				exists = scope:custom_spouse_option
				scope:custom_spouse_option = yes
			}
			ansf_remove_all_flags_variables = yes
		}
		else = {
			trigger_event = ansf.1004 #To the family screen
		}
		
		ai_chance = {
			base = 100
		}
	}
}

###################
#Different Parents#
###################

scripted_effect set_bastard_parental_flags = {
	# determine which of the two is the mother and father
	if = {
		limit = {
			scope:ansf_bastard_parent = { is_female = yes }
		}
		scope:ansf_bastard_parent = { save_scope_as = ansf_bastard_mother }
		scope:ansf_bastard_otherparent = { save_scope_as = ansf_bastard_father }
	}
	else = {
		scope:ansf_bastard_parent = { save_scope_as = ansf_bastard_father }
		scope:ansf_bastard_otherparent = { save_scope_as = ansf_bastard_mother }
	}
	# determine which is the "dominant" parent (will pass on dynasty)
	if = {
		limit = { exists = root.var:is_concubine }
		scope:ansf_bastard_father = { save_scope_as = ansf_dominant_parent }
	}
	else_if = {
		limit = {
			exists = root.var:is_former_spouse
			OR = {
				AND = {
					scope:ansf_bastard_mother = { is_ruler = yes }
					scope:ansf_bastard_father = { is_ruler = no }
				}
				AND = {
					scope:ansf_bastard_mother = { has_dynasty = yes }
					scope:ansf_bastard_father = { has_dynasty = no }
				}
			}
		}
		scope:ansf_bastard_mother = { save_scope_as = ansf_dominant_parent }
	}
	else_if = {
		limit = { exists = root.var:is_former_spouse }
		scope:ansf_bastard_father = { save_scope_as = ansf_dominant_parent }
	}
	else_if = {
		limit = { exists = scope:ansf_bastard_mother.house }
		scope:ansf_bastard_mother = { save_scope_as = ansf_dominant_parent }
	}
	else = {
		scope:ansf_bastard_father = { save_scope_as = ansf_dominant_parent }
	}
	# give notifications of what the half-sibling will be
	if = {
		limit = {
			scope:ansf_bastard_father = { has_dynasty = no }
			scope:ansf_bastard_mother = { has_dynasty = no }
		}
		custom_tooltip = ansf.halfsibling.lowborn.tt
	}
	else = {
		custom_tooltip = ansf.halfsibling.notdynasty.tt
	}
	custom_tooltip = ansf.halfsibling.notculture.tt
	custom_tooltip = ansf.halfsibling.notfaith.tt
}

scripted_effect post_creation_bastard_effect = {
	#birth traits
	if = {
		limit = { exists = root.var:is_concubine }
		add_trait = child_of_concubine_female
	}
	else_if = {
		limit = { exists = root.var:is_lover_bastard }
		ansf_add_bastard_trait_based_on_faith_effect = yes
	}
	
	#dynasty
	if = {
		limit = { scope:ansf_dominant_parent = { has_dynasty = yes } }
		set_house = scope:ansf_dominant_parent.house
	}
	
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_clone
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:target
		if = {
			limit = { is_female = yes }
			add_character_flag = ansf_family_female
		}
		else = {
			add_character_flag = ansf_family_male
		}
	}
	else_if = {
		limit = {
			root = {
				has_variable = ansf_family_character_use_father_dna
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:ansf_family_member.father
		if = {
			limit = { is_female = yes }
			add_character_flag = ansf_family_female
		}
		else = {
			add_character_flag = ansf_family_male
		}
	}
	else_if = {
		limit = {
			root = {
				has_variable = ansf_family_character_use_mother_dna
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:ansf_family_member.mother
		if = {
			limit = { is_female = yes }
			add_character_flag = ansf_family_female
		}
		else = {
			add_character_flag = ansf_family_male
		}
	}

	#traits
	ansf_validate_character_traits = yes
	if = {
		limit = {
			exists = scope:random_family
			scope:random_family = yes
		}
		every_character_trait = {
			limit = { ansf_is_physical_trait = yes }
			random = {
				chance = 50
				scope:ansf_family_member = { remove_trait = prev }
			}
		}
	}
	
	#add the twin trait if a same-age sibling exists
	if = {
		limit = {
			any_sibling = {
				even_if_dead = yes
				NOT = { has_trait = twin }
				OR = {
					mother = scope:ansf_family_member.mother
					AND = {
						NOT = { exists = mother }
						scope:ansf_family_member.mother = scope:ansf_bastard_otherparent
						age_including_death_plus_childbirth_start <= scope:ansf_family_member.mother.age
					}
				}
				OR = {
					father = scope:ansf_family_member.father
					AND = {
						NOT = { exists = father }
						scope:ansf_family_member.father = scope:ansf_bastard_otherparent
						age_including_death_plus_childbirth_start <= scope:ansf_family_member.father.age
					}
				}
				age_including_death_age = scope:ansf_family_member.age
			}
		}
		add_trait = twin
		remove_character_flag ?= ansf_family_male
		remove_character_flag ?= ansf_family_female
		random_sibling = {
			even_if_dead = yes
			limit = {
				NOT = { has_trait = twin }
				OR = {
					mother = scope:ansf_family_member.mother
					AND = {
						NOT = { exists = mother }
						scope:ansf_family_member.mother = scope:ansf_bastard_otherparent
						age_including_death_plus_childbirth_start <= scope:ansf_family_member.mother.age
					}
				}
				OR = {
					father = scope:ansf_family_member.father
					AND = {
						NOT = { exists = father }
						scope:ansf_family_member.father = scope:ansf_bastard_otherparent
						age_including_death_plus_childbirth_start <= scope:ansf_family_member.father.age
					}
				}
				age_including_death_age = scope:ansf_family_member.age
			}
			scope:ansf_family_member = {
				copy_inheritable_appearance_from = prev
			}
		}
	}
	
	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		if = {
			limit = {
				root = { is_ai = no }
			}
			root = {
				ansf_remove_all_gui_variables = yes
				set_variable = { name = ansf_family_character value = scope:ansf_family_member }
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age value = scope:ansf_family_member.age }
				set_variable = ansf_family_character_education_random
				set_variable = ansf_family_character_can_use_parent_dna
				set_variable = { name = ansf_family_character_age_min value = age_halfsibling_min }
				set_variable = { name = ansf_family_character_age_max value = age_halfsibling_max }
			}
		}
	}
}

scripted_effect post_creation_otherparent_effect = {
	save_scope_as = ansf_family_member
	#set lowborn status
	if = {
		limit = {
			root = {
				has_variable = ansf_must_be_lowborn
				has_variable = ansf_family_character_generate
			}
		}
		set_to_lowborn = yes
	}
	else_if = {
		limit = {
			NOR = {
				root = {
					has_variable = ansf_must_not_be_lowborn
					has_variable = ansf_family_character_generate
				}
				exists = mother
				exists = father
			}
		}
		random = {
			chance = 50
			set_to_lowborn = yes
		}
	}
	
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_clone
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:target
		if = {
			limit = { is_male = yes }
			add_character_flag = ansf_family_male
		}
		else = {
			add_character_flag = ansf_family_female
		}
	}
	
	#traits
	ansf_validate_character_traits = yes
	every_character_trait = {
		limit = {
			NAND = {
				exists = scope:random_spouse_option
				scope:random_spouse_option = yes
			}
			OR = {
				this = trait:beauty_bad_1
				this = trait:beauty_bad_2
				this = trait:beauty_bad_3
				this = trait:intellect_bad_1
				this = trait:intellect_bad_2
				this = trait:intellect_bad_3
				this = trait:physique_bad_2
				this = trait:physique_bad_3
				this = trait:hunchbacked
				this = trait:dwarf
				this = trait:giant
				this = trait:inbred
				this = trait:spindly
				this = trait:scaly
				this = trait:albino
				this = trait:infertile
			}
		}
		scope:ansf_family_member = { remove_trait = prev }
	}
	if = {
		limit = {
			exists = scope:random_family
			scope:random_family = yes
		}
		every_character_trait = {
			limit = { ansf_is_physical_trait = yes }
			random = {
				chance = 50
				scope:ansf_family_member = { remove_trait = prev }
			}
		}
	}
	else = {
		random_list = {
			20 = {
				trigger = {
					NOT = { has_trait = beauty_good }
				}
				add_trait = beauty_good_1
			}
			20 = {
				trigger = {
					is_female = yes
					NOT = { has_trait = beauty_good }
				}
				add_trait = beauty_good_2
			}
			10 = {
				trigger = {
					is_female = yes
					NOT = { has_trait = beauty_good }
				}
				add_trait = beauty_good_3
			}
			20 = {
				trigger = {
					is_male = yes
					NOT = { has_trait = physique_good }
				}
				add_trait = physique_good_1
			}
			10 = {
				trigger = {
					is_male = yes
					NOT = { has_trait = physique_good }
				}
				add_trait = physique_good_2
			}
			50 = {}
		}
	}

	#relatives
	if = {
		limit = {
			any_sibling = {
				even_if_dead = yes
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
		}
		add_trait = twin
		remove_character_flag ?= ansf_family_male
		remove_character_flag ?= ansf_family_female
		random_sibling = {
			even_if_dead = yes
			limit = {
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
			scope:ansf_family_member = {
				add_character_flag = is_different_DNA
				copy_inheritable_appearance_from = prev
			}
		}
	}	
	check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
	
	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		if = {
			limit = {
				root = { is_ai = no }
			}
			root = {
				ansf_remove_all_gui_variables = yes
				set_variable = { name = ansf_family_character value = scope:ansf_bastard_otherparent }
				set_variable = { name = ansf_family_character_age value = scope:ansf_bastard_otherparent.age }
				set_variable = ansf_family_character_education_random
			}
			if = {
				limit = {
					root = { has_character_flag = otherparent_existing_dynasty }
				}
				root = {
					set_variable = ansf_family_character_cannot_change_age
				}
			}
			else = {
				root = {
					set_variable = ansf_family_character_randomize_age
					set_variable = { name = ansf_family_character_age_min value = age_otherparent_min }
					set_variable = { name = ansf_family_character_age_max value = age_otherparent_max }
					set_variable = ansf_has_culture_faith_template
					set_variable = ansf_can_set_lowborn_status
				}
			}
		}
	}
}

scripted_effect find_bastard_parent_eldest_legitimate_child = {
	every_in_list = {
		list = legitimate_children
		remove_from_list = legitimate_children
	}	
	$PARENT$ = {
		every_child = {
			even_if_dead = yes
			limit = {
				ansf_is_bastard = no
				NOT = { has_trait = child_of_concubine_female }
			}
			add_to_list = legitimate_children
		}
	}
	if = {
		limit = {
			list_size = { name = legitimate_children value > 0 }
		}
		ordered_in_list = {
			list = legitimate_children
			limit = {
				trigger_if = {
					limit = {
						root = { has_character_flag = must_have_both_parents }
						NOT = { exists = mother }
						scope:ansf_bastard_otherparent = { is_female = yes }
					}
					age_including_death_plus_childbirth_start > scope:ansf_bastard_otherparent.age
				}
				trigger_else_if = {
					limit = {
						root = { has_character_flag = must_have_both_parents }
						NOT = { exists = father }
						scope:ansf_bastard_otherparent = { is_male = yes }
					}
					age_including_death_plus_childbirth_start > scope:ansf_bastard_otherparent.age
				}
				trigger_else = {
					always = yes
				}
				
				trigger_if = {
					limit = { exists = scope:ansf_bastard_otherparent }
					NOR = {
						mother ?= scope:ansf_bastard_otherparent
						father ?= scope:ansf_bastard_otherparent
					}
				}
				trigger_else = {
					always = yes
				}
			}
			order_by = age_including_death_age
			save_scope_as = eldest_legitimate_child
		}
	}
}

scripted_effect add_to_potential_parents_list = {
	save_temporary_scope_as = selected_potential_parent
	if = {
		limit = {
			root = {
				NOT = {
					is_target_in_variable_list = {
						name = potential_parents
						target = scope:selected_potential_parent
					}
				}
			}
		}
		root = {
			add_to_variable_list = {
				name = potential_parents
				target = scope:selected_potential_parent
			}
		}
	}
}

ansf.2100 = { #Half-sibling of which parent?
	type = character_event
	window = ansf_event_window
	title = ansf.halfsibling.event.t
	desc = ansf.halfsibling.event.d
	theme = family
	
	right_portrait ?= scope:target.father
	center_portrait ?= scope:target.mother
	override_background = { reference = bedchamber }
	
	option = {
		name = ansf.halfsibling.father
		trigger = { exists = scope:target.father }
		scope:target.father = { save_scope_as = ansf_bastard_parent }
		scope:new_dummy_female = { save_scope_as = ansf_bastard_otherparent }
		trigger_event = ansf.2101
		ai_chance = {
			base = 50
			modifier = {
				add = -45
				exists = scope:target.mother
				scope:target.mother = {
					OR = {
						has_trait = adulterer
						has_trait = fornicator
					}
				}
			}
			modifier = {
				factor = 2
				exists = scope:target.mother
				age < scope:target.mother.age
			}
		}
	}
	option = {
		name = ansf.halfsibling.mother
		trigger = { exists = scope:target.mother }
		scope:target.mother = { save_scope_as = ansf_bastard_parent }
		scope:new_dummy_male = { save_scope_as = ansf_bastard_otherparent }
		trigger_event = ansf.2101
		ai_chance = {
			base = 50
			modifier = {
				add = -45
				exists = scope:target.father
				scope:target.father = {
					OR = {
						has_trait = adulterer
						has_trait = fornicator
					}
				}
			}
			modifier = {
				factor = 2
				exists = scope:target.father
				age < scope:target.father.age
			}
		}
	}
	option = {
		name = ansf.halfsibling.nevermind
		custom_tooltip = ansf.halfsibling.nevermind.tooltip
		clear_saved_scope = halfsibling
		trigger_event = ansf.1004
		
		ai_chance = {
			base = 0
		}
	}
}

ansf.2101 = { #Nature of the relationship?
	type = character_event
	window = ansf_event_window
	title = ansf.halfsibling.nature.event.t
	desc = ansf.halfsibling.nature.event.d
	theme = family
	
	center_portrait = scope:ansf_bastard_parent
	override_background = { reference = bedchamber }
	
	widgets = {
		widget = {
			gui = "event_window_widget_select_parent"
			container = "custom_widgets_container"
		}
	}
	
	immediate = {
		hidden_effect = {
		# create the list for the gui
			clear_variable_list = potential_parents
			scope:ansf_bastard_parent = {
				every_former_spouse = {
					even_if_dead = yes
					limit = { sex_opposite_of = scope:ansf_bastard_parent }
					add_to_potential_parents_list = yes
				}
				every_consort = {
					limit = { sex_opposite_of = scope:ansf_bastard_parent }
					add_to_potential_parents_list = yes
				}
				every_relation = {
					type = lover
					limit = { sex_opposite_of = scope:ansf_bastard_parent }
					add_to_potential_parents_list = yes
				}
				every_child = {
					even_if_dead = yes
					limit = {
						trigger_if = {
							limit = {
								scope:ansf_bastard_parent = { is_male = yes }
							}
							exists = mother
							NOT = { mother = scope:target.mother }
						}
						trigger_else = {
							exists = father
							NOT = { father = scope:target.father }
						}
					}
					if = {
						limit = {
							scope:ansf_bastard_parent = { is_male = yes }
						}
						mother = { add_to_potential_parents_list = yes }
					}
					if = {
						limit = {
							scope:ansf_bastard_parent = { is_female = yes }
						}
						father = { add_to_potential_parents_list = yes }
					}
				}
			}
			if = {
				limit = {
					exists = scope:ansf_bastard_parent.primary_spouse
					is_target_in_variable_list = {
						name = potential_parents
						target = scope:ansf_bastard_parent.primary_spouse
					}
				}
				remove_list_variable = {
					name = potential_parents
					target = scope:ansf_bastard_parent.primary_spouse
				}
			}
		}
	}
	
	option = { # a former spouse
		trigger = {
			custom_description = {
				text = ansf_already_has_ex_spouse
				subject = scope:ansf_bastard_parent
				scope:ansf_bastard_parent = {
					NOT = {
						any_former_spouse = {
							even_if_dead = yes
							count > 0
						}
					}
				}
			}
		}
		name = ansf.halfsibling.exspouse
		show_as_unavailable = {
			scope:ansf_bastard_parent = {
				any_former_spouse = {
					even_if_dead = yes
					count > 0
				}
			}
		}
		
		custom_tooltip = { text = ansf.halfsibling.exspouse.tt }
		custom_tooltip = { text = ansf.halfsibling.expouse.tt.warning }
		scope:ansf_bastard_otherparent = { save_scope_as = source_otherparent }
		set_variable = { name = is_former_spouse value = yes }
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		remove_variable ?= ansf_bastard_parent
		trigger_event = ansf.2102
		
		ai_chance = {
			base = 0
		}
	}
	option = { # a concubine
		trigger = {
			scope:ansf_bastard_parent = {
				is_male = yes
				has_dynasty = yes
				faith = { has_doctrine = doctrine_concubines }
				OR = {
					is_married = no
					matrilinear_marriage = no
				}
			}
		}
		name = ansf.halfsibling.concubine
		
		custom_tooltip = { text = ansf.halfsibling.concubine.tt }
		scope:ansf_bastard_otherparent = { save_scope_as = source_otherparent }
		set_variable = { name = is_concubine value = yes }
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		remove_variable ?= ansf_bastard_parent
		trigger_event = ansf.2102
		
		ai_chance = {
			base = 0
		}
	}
	option = { # a forbidden lover
		name = ansf.halfsibling.lover
		
		custom_tooltip = { text = ansf.halfsibling.lover.tt }
		custom_tooltip = { text = ansf.halfsibling.lover.tt.warning }
		scope:ansf_bastard_otherparent = { save_scope_as = source_otherparent }
		set_variable = { name = is_lover_bastard value = yes }
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		remove_variable ?= ansf_bastard_parent
		trigger_event = ansf.2102
		
		ai_chance = {
			base = 100
		}
	}
	option = { # select an already-existing parent
		trigger = {
			NOT = { exists = global_var:ansf_show_possible_parents }
			has_variable_list = potential_parents
			variable_list_size = { name = potential_parents value > 0 }
		}
		name = ansf.halfsibling.findexisting
		
		custom_tooltip = { text = ansf.halfsibling.findexisting.tt }
		
		# restart the event and prompt the widget to appear
		set_global_variable = { name = ansf_show_possible_parents value = yes }
		set_variable = { name = ansf_bastard_parent value = scope:ansf_bastard_parent }
		trigger_event = ansf.2101
		
		ai_chance = {
			base = 0
		}
	}
	option = { # use a selected existing parent
		trigger = {
			exists = var:existing_parent
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:ansf_bastard_parent = {
								any_child = {
									even_if_dead = yes
									is_child_of = root.var:existing_parent
								}
							}
						}
						desc = ansf.halfsibling.existing
					}
					desc = ansf.halfsibling.existing.first
				}
			}
		}
		
		custom_tooltip = { text = ansf.halfsibling.existing.tt }
		var:existing_parent = { save_scope_as = ansf_bastard_otherparent }
		set_variable = { name = do_not_delete value = scope:ansf_bastard_otherparent }

		if = {
			limit = {
				scope:ansf_bastard_parent = {
					ansf_was_married_to = { SPOUSE = scope:ansf_bastard_otherparent }
				}
			}
			set_variable = { name = is_former_spouse value = yes }
		}
		else_if = {
			limit = {
				scope:ansf_bastard_otherparent = {
					OR = {
						is_concubine_of = scope:ansf_bastard_otherparent
						any_child = {
							even_if_dead = yes
							has_trait = child_of_concubine_female
						}
					}
				}
			}
			set_variable = { name = is_concubine value = yes }
		}
		else = {
			set_variable = { name = is_lover_bastard value = yes }
		}
		
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		remove_variable ?= ansf_bastard_parent
		set_bastard_parental_flags = yes
		trigger_event = ansf.2105
		
		ai_chance = {
			base = 0
		}
	}
	option = { # go back
		name = ansf.halfsibling.nevermind
		custom_tooltip = { text = ansf.halfsibling.nevermind.tooltip }
		clear_saved_scope = halfsibling
		clear_saved_scope = ansf_bastard_parent
		clear_saved_scope = ansf_bastard_otherparent
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		remove_variable ?= ansf_bastard_parent
		trigger_event = ansf.2100
		
		ai_chance = {
			base = 0
		}
	}	
}

ansf.2102 = { #The other parent of your half-sibling?
	type = character_event
	window = ansf_event_window
	title = ansf.halfsibling.parent.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = var:is_concubine }
				desc = ansf.halfsibling.parent.d.concubine
			}
			triggered_desc = {
				trigger = { exists = var:is_former_spouse }
				desc = ansf.halfsibling.parent.d.former.spouse
			}
			desc = ansf.halfsibling.parent.d.lover
		}
	}
	theme = family
	
	right_portrait = {
		character = scope:ansf_bastard_parent
		triggered_animation = {
			trigger = { exists = root.var:is_lover_bastard }
			animation = shame
		}
		animation = interested
	}
	center_portrait = {
		character = scope:ansf_bastard_otherparent
		animation = love
	}
	override_background = { reference = bedchamber }
	
	immediate = {
		hidden_effect = {
			clear_variable_list = potential_grandparents
			clear_variable_list = potential_piblings
			clear_variable_list = potential_relatives
			
			# we need the eldest legitimate child of the main parent
			find_bastard_parent_eldest_legitimate_child = { PARENT = scope:ansf_bastard_parent }
			
			# use the parent's template unless one has already been set
			if = {
				limit = { has_character_flag = no_creation_override }
				remove_character_flag = no_creation_override
				check_for_possible_relatives = { COMPARE = scope:ansf_bastard_otherparent }
			}
			else = {
				# for characters that are a clone of the target, set the culture/faith variable to match
				if = {
					limit = {
						has_variable = ansf_family_character_is_clone
						has_variable = ansf_family_character_generate
					}
					set_variable = {
						name = culture_faith_template
						value = scope:target
					}
				}
				# otherwise, we use a random one if culture/faith hasn't been set
				else_if = {
					limit = {
						NOT = { has_variable = culture_faith_template }
					}
					find_random_template_ruler = yes
				}
				# now create the character
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								post_creation_otherparent_effect = yes
							}
						}
					}
					40 = {
						modifier = {
							add = -35
							NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:source_otherparent } }
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_learning
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
		
			if = {
				limit = { exists = scope:eldest_legitimate_child }
				clear_saved_scope = eldest_legitimate_child
			}
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_bastard_otherparent = { save_scope_as = name_character_target } }
		}
	}
	
	# Yes, but they're dead
	option = {
		name = ansf.halfsibling.parent.yes.dead
		custom_tooltip = { text =  ansf.halfsibling.parent.dead.tt }
		add_character_flag = otherparent_will_be_dead
		
		set_variable = { name = is_otherparent_creation value = yes }
		ansf_remove_all_gui_variables = yes
		set_bastard_parental_flags = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 50
		}
	}
	# Yes, and they're lovers still
	option = {
		trigger = {
			exists = var:is_lover_bastard
			custom_description = {
				text = ansf_already_has_lover
				subject = scope:ansf_bastard_parent
				scope:ansf_bastard_parent = { num_of_relation_lover < 1 }
			}
			scope:ansf_bastard_parent = { is_alive = yes }
		}
		name = ansf.halfsibling.parent.yes.lover.yes
		show_as_unavailable = {
			exists = var:is_lover_bastard
			scope:ansf_bastard_parent = {
				OR = {
					num_of_relation_lover > 0
					is_alive = no
				}
			}
		}
		custom_tooltip = { text =  ansf.halfsibling.parent.lover.remain.tt }
		add_character_flag = otherparent_will_remain_lover
		
		set_variable = { name = is_otherparent_creation value = yes }
		ansf_remove_all_gui_variables = yes
		set_bastard_parental_flags = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 0
		}
	}
	# Yes, and they're my courtier
	option = {
		name = {
			trigger = { exists = var:is_lover_bastard }
			text = ansf.halfsibling.parent.yes.lover.no
		}
		name = {
			trigger = {
				NOT = { exists = var:is_lover_bastard }
			}
			text = ansf.halfsibling.parent.yes.remain
		}
		custom_tooltip = { text =  ansf.halfsibling.parent.remain.tt }
		add_character_flag = otherparent_will_be_courtier
		
		set_variable = { name = is_otherparent_creation value = yes }
		ansf_remove_all_gui_variables = yes
		set_bastard_parental_flags = yes
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 50
		}
	}
	# Connect to dynasty
	option = {
		trigger = {
			custom_description = {
				text = no_dynasty_options
				subject = scope:ansf_bastard_otherparent
				has_variable_list = potential_relatives
				variable_list_size = { name = potential_relatives value > 0 }
			}
		}
		name = ansf.halfsibling.parent.connect
		show_as_unavailable = {
			OR = {
				NOT = { has_variable_list = potential_relatives }
				variable_list_size = { name = potential_relatives value < 1 }
			}
		}

		remove_variable ?= show_ansf_regenerate_window
		trigger_event = ansf.2103
		
		custom_tooltip = { text =  ansf.existing.other.parent.dynasty.tt }
		
		ai_chance = {
			base = 0
		}
	}
	# Regenerate Character
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.halfsibling.parent.regenerate
					}
					desc = ansf.halfsibling.parent.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = { text =  ansf.reroll.paramaters }
			add_internal_flag = special
		}
		else = {
			custom_tooltip = { text =  ansf.spouse.redo.father.tt }
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_bastard_otherparent }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		trigger_event = ansf.2102
		
		ai_chance = {
			base = 0
		}
	}
	# Go back
	option = { # go back
		name = ansf.halfsibling.nevermind
		custom_tooltip = { text = ansf.no.me.existing.tt }
		remove_variable = is_former_spouse
		remove_variable = is_concubine
		remove_variable = is_lover_bastard
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2101
		
		ai_chance = {
			base = 0
		}
	}
}

ansf.2103 = { #Other parent from an existing dynasty
	type = character_event
	window = ansf_dynasty_event_window
	title = ansf.halfsibling.parent.connect.t
	desc = ansf.halfsibling.parent.connect.d
	theme = family

	center_portrait = {
		character = scope:ansf_bastard_parent
		animation = thinking
	}
	override_background = { reference = bedchamber }

	immediate = {
		set_variable = { name = ansf_family_character value = scope:ansf_family_member }
	}

	# Selection is their father
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_grandparents
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.grandparent
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = { text = ansf.yes.existing1.other.grandparent.tt }
		root.var:potential_relative_1 = {
			if = {
				limit = { is_male = yes }
				root = {
					set_variable = {
						name = actual_grandfather
						value = root.var:potential_relative_1
					}
				}
			}
			else = {
				root = {
					set_variable = {
						name = actual_grandmother
						value = root.var:potential_relative_1
					}
				}
			}
			if = {
				limit = {
					should_be_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = { text =  ansf.yes.existing.other.bastard.tt }
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					any_consort = {
						even_if_dead = yes
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
				}
				random_consort = {
					even_if_dead = yes
					limit = {
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
					if = {
						limit = { is_female = yes }
						root = {
							set_variable = {
								name = actual_grandmother
								value = prev
							}
						}
					}
					else = {
						root = {
							set_variable = {
								name = actual_grandfather
								value = prev
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		remove_variable = potential_relative_1
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2104
	}
	
	# Selection is their brother
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_piblings
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.pibling
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = { text = ansf.yes.existing1.other.pibling.tt }
		root.var:potential_relative_1 = {
			root = {
				set_variable = {
					name = actual_grandfather
					value = root.var:potential_relative_1.father
				}
			}
			if = {
				limit = {
					should_be_fathers_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = { text = ansf.yes.existing.other.bastard.tt }
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					should_be_legitimate_bastard = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = { text = ansf.yes.existing.other.legit.bastard.tt }
				root = {
					set_variable = {
						name = will_be_legitimate_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					father = {
						any_consort = {
							even_if_dead = yes
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
					}
				}
				father = {
					random_consort = {
						even_if_dead = yes
						limit = {
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
						root = {
							set_variable = {
								name = actual_grandmother
								value = prev
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		remove_variable = potential_relative_1
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2104
	}
	
	# Go back
	option = {
		name = ansf.no.inlaw.existing
		add_character_flag = no_creation_override
		
		remove_variable = potential_relative_1
		remove_variable = ansf_family_character
		
		custom_tooltip = { text = ansf.no.me.existing.tt }
		
		trigger_event = ansf.2102
	}
}

ansf.2104 = { #Creating the other parent with connection to the existing dynasty
	type = character_event
	window = ansf_event_window
	title = ansf.halfsibling.parent.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.halfsibling.parent.grandparent.d
			}
			desc = ansf.halfsibling.parent.pibling.d
		}
	}
	
	theme = family
	center_portrait = scope:ansf_family_member	
	override_background = { reference = bedchamber }

	immediate = {
		hidden_effect = {
			add_character_flag = otherparent_existing_dynasty
			if = {
				limit = {
					has_variable = actual_grandfather
					has_variable = actual_grandmother
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					has_variable = actual_grandfather
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
			else = {
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
			remove_character_flag = otherparent_existing_dynasty
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	# Yes, but they're dead
	option = {
		name = ansf.halfsibling.parent.yes.dead
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text =  ansf.halfsibling.parent.dead.tt }
		add_character_flag = otherparent_will_be_dead
		
		set_bastard_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_otherparent_creation value = yes }
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 50
		}
	}
	# Yes, and they're lovers still
	option = {
		trigger = {
			exists = var:is_lover_bastard
			custom_description = {
				text = ansf_already_has_lover
				subject = scope:ansf_bastard_parent
				scope:ansf_bastard_parent = { num_of_relation_lover < 1 }
			}
		}
		name = ansf.halfsibling.parent.yes.lover.yes
		show_as_unavailable = {
			exists = var:is_lover_bastard
			scope:ansf_bastard_parent = { num_of_relation_lover > 0 }
		}
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text =  ansf.halfsibling.parent.lover.remain.tt }
		add_character_flag = otherparent_will_remain_lover
		
		set_bastard_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_otherparent_creation value = yes }
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 0
		}
	}
	# Yes, and they're my courtier
	option = {
		trigger = {
			NOT = { exists = var:is_former_spouse }
		}
		name = {
			trigger = { exists = var:is_lover_bastard }
			text = ansf.halfsibling.parent.yes.lover.no
		}
		name = {
			trigger = {
				NOT = { exists = var:is_lover_bastard }
			}
			text = ansf.halfsibling.parent.yes.remain
		}
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text =  ansf.halfsibling.parent.remain.tt }
		add_character_flag = otherparent_will_be_courtier
		
		set_bastard_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_otherparent_creation value = yes }
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 50
		}
	}
	# Yes, but they're at their previous court
	option = {
		trigger = { exists = var:is_former_spouse }
		name = ansf.halfsibling.parent.yes.relative
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text = ansf.halfsibling.parent.relative.tt }
		add_character_flag = otherparent_will_be_courtier
		
		set_bastard_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_otherparent_creation value = yes }
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 50
		}
	}
	# Redo
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.no.existing.reroll
				}
			}
		}

		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = { text = ansf.reroll.paramaters }
			add_internal_flag = special
		}
		else = {
			custom_tooltip = { text = ansf.reroll.inlaw.existing }
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
	
		trigger_event = ansf.2104
	}
	# Never mind
	option = {
		name = ansf.no.existing.leave
		custom_tooltip = ansf.no.existing.leave.tt
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }

		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2102
	}
}

ansf.2105 = { #Is this your half-sibling?
	type = character_event
	window = ansf_event_window
	title = ansf.halfsibling.event3.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					scope:ansf_family_member = { is_female = yes }
				}
				desc = ansf.halfsister.event.d
			}
			triggered_desc = {
				trigger = {
					scope:target = root
					scope:ansf_family_member = { is_male = yes }
				}
				desc = ansf.halfbrother.event.d
			}
			desc = ansf.halfsibling.event3.d
		}
	}
	theme = family
	
	center_portrait = scope:ansf_family_member
	lower_left_portrait = scope:ansf_bastard_otherparent
	lower_right_portrait = scope:ansf_bastard_parent
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = yes } }
		reference = sitting_room
	}
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = no } }
		reference = courtyard
	}
	
	immediate = {
		hidden_effect = {
			# we need the eldest legitimate child of the main parent
			add_character_flag = must_have_both_parents
			find_bastard_parent_eldest_legitimate_child = { PARENT = scope:ansf_bastard_parent }
			#Find some appropriate options
			random_list = {
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Diplomat
						location = scope:target.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:halfsibling
						dynasty = none
						age = {
							integer_range = {
								min = age_halfsibling_min
								max = age_halfsibling_max
							}
						}
						mother = scope:ansf_bastard_mother
						father = scope:ansf_bastard_father
						template = olympia_diplomat_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_diplomacy
							post_creation_bastard_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
						}
					}
					create_character = { #Martial
						location = scope:target.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:halfsibling
						dynasty = none
						age = {
							integer_range = {
								min = age_halfsibling_min
								max = age_halfsibling_max
							}
						}
						mother = scope:ansf_bastard_mother
						father = scope:ansf_bastard_father
						template = olympia_martial_template
						prowess = { 10 15 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_martial
							post_creation_bastard_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Steward
						location = scope:target.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:halfsibling
						dynasty = none
						age = {
							integer_range = {
								min = age_halfsibling_min
								max = age_halfsibling_max
							}
						}
						mother = scope:ansf_bastard_mother
						father = scope:ansf_bastard_father						
						template = olympia_stewardship_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_stewardship
							post_creation_bastard_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Intrigue
						location = scope:target.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:halfsibling
						dynasty = none
						age = {
							integer_range = {
								min = age_halfsibling_min
								max = age_halfsibling_max
							}
						}
						mother = scope:ansf_bastard_mother
						father = scope:ansf_bastard_father						
						template = olympia_intrigue_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_intrigue
							post_creation_bastard_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Learning
						location = scope:target.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:halfsibling
						dynasty = none
						age = {
							integer_range = {
								min = age_halfsibling_min
								max = age_halfsibling_max
							}
						}
						mother = scope:ansf_bastard_mother
						father = scope:ansf_bastard_father						
						template = olympia_learning_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_learning
							post_creation_bastard_effect = yes
						}
					}
				}
			}
			
			remove_character_flag = must_have_both_parents
			if = {
				limit = { exists = scope:eldest_legitimate_child }
				clear_saved_scope = eldest_legitimate_child
			}
		}
	}

	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	#This is my half-sibling
	option = {
		name = ansf.halfsibling.event3.proceed
		trigger = {
			custom_description = {
				text = ansf_cannot_be_twin
				subject = scope:ansf_family_member
				NOT = {
					scope:ansf_family_member = {
						any_sibling = {
							even_if_dead = yes
							has_trait = twin
							OR = {
								mother = scope:ansf_family_member.mother
								AND = {
									NOT = { exists = mother }
									scope:ansf_family_member.mother = scope:ansf_bastard_otherparent
									age_including_death_plus_childbirth_start <= scope:ansf_family_member.mother.age
								}
							}
							OR = {
								father = scope:ansf_family_member.father
								AND = {
									NOT = { exists = father }
									scope:ansf_family_member.father = scope:ansf_bastard_otherparent
									age_including_death_plus_childbirth_start <= scope:ansf_family_member.father.age
								}
							}
							age_including_death_age = scope:ansf_family_member.age
						}
					}
				}
			}
		}
		
		show_as_unavailable = {
			scope:ansf_family_member = {
				any_sibling = {
					even_if_dead = yes
					has_trait = twin
					OR = {
						mother = scope:ansf_family_member.mother
						AND = {
							NOT = { exists = mother }
							scope:ansf_family_member.mother = scope:ansf_bastard_otherparent
							age_including_death_plus_childbirth_start <= scope:ansf_family_member.mother.age
						}
					}
					OR = {
						father = scope:ansf_family_member.father
						AND = {
							NOT = { exists = father }
							scope:ansf_family_member.father = scope:ansf_bastard_otherparent
							age_including_death_plus_childbirth_start <= scope:ansf_family_member.father.age
						}
					}
					age_including_death_age = scope:ansf_family_member.age
				}
			}
		}

		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		# first move the half-sibling to the right court
		if = {
			limit = {
				scope:ansf_family_member = {
					NOT = { is_courtier_of = scope:target_liege }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_family_member }
		}
		
		# make the otherparent a former spouse, if needed
		if = {
			limit = {
				exists = var:is_former_spouse
				scope:ansf_bastard_parent = {
					NOT = { ansf_was_married_to = { SPOUSE = scope:ansf_bastard_otherparent } }
				}
			}
			ansf_make_former_spouse = { PARENT = scope:ansf_bastard_parent FORMER = scope:ansf_bastard_otherparent }
		}
		# make the otherparent a concubine, if needed
		if = {
			limit = {
				exists = var:is_concubine
				has_character_flag = otherparent_will_be_courtier
			}
			scope:ansf_bastard_parent = { make_concubine = scope:ansf_bastard_otherparent }
		}
		# make the otherparent a public lover, if needed
		if = {
			limit = { has_character_flag = otherparent_will_remain_lover }
			scope:ansf_bastard_parent = {
				set_variable = { name = ignore_lover_secret value = scope:ansf_bastard_otherparent days = 2 }
				set_random_lover_reason = { TARGET = scope:ansf_bastard_otherparent }
			}
		}
		# make the otherparent a courtier
		if = {
			limit = {
				exists = var:is_former_spouse
				exists = scope:actual_relative
				has_character_flag = otherparent_will_be_courtier
				scope:ansf_bastard_otherparent = {
					is_close_or_extended_family_of = scope:actual_relative
					NOT = { is_courtier_of = scope:actual_relative }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:actual_relative NEW_COURTIER = scope:ansf_bastard_otherparent }
		}
		else_if = {
			limit = {
				OR = {
					has_character_flag = otherparent_will_be_courtier
					has_character_flag = otherparent_will_remain_lover
				}
				scope:ansf_bastard_otherparent = { is_courtier = no }
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_bastard_otherparent }
		}
		# give both parents adulterer/fornicator traits, if needed
		if = {
			limit = {
				OR = {
					exists = var:is_lover_bastard
					exists = var:otherparent_will_remain_lover
				}
			}
			scope:ansf_bastard_parent = {
				if = {
					limit = { ansf_is_adulterer = no }
					add_adulterer_fornicator_trait_or_nothing_effect = yes
					if = {
						limit = { exists = liege }
						hidden_effect = {
							consume_all_criminal_reasons_effect = {
								LIEGE = scope:ansf_bastard_parent.liege
								CRIMINAL = scope:ansf_bastard_parent
							}
						}
					}
				}
				if = {
					limit = {
						NOR = {
							exists = root.var:otherparent_will_remain_lover
							has_relation_old_flame = scope:ansf_bastard_otherparent
						}
					}
					hidden_effect = { set_relation_old_flame = scope:ansf_bastard_otherparent }
				}
			}
			scope:ansf_bastard_otherparent = {
				if = {
					limit = { ansf_is_adulterer = no }
					add_adulterer_fornicator_trait_or_nothing_effect = yes
					if = {
						limit = { exists = liege }
						hidden_effect = {
							consume_all_criminal_reasons_effect = {
								LIEGE = scope:ansf_bastard_otherparent.liege
								CRIMINAL = scope:ansf_bastard_otherparent
							}
						}
					}
				}
				if = {
					limit = {
						NOR = {
							exists = root.var:otherparent_will_remain_lover
							has_relation_old_flame = scope:ansf_bastard_parent
						}
					}
					hidden_effect = { set_relation_old_flame = scope:ansf_bastard_parent }
				}
			}
		}
		# kill the otherparent, if needed
		if = {
			limit = { has_character_flag = otherparent_will_be_dead }
			scope:ansf_bastard_otherparent = {
				death = { death_reason = death_vanished }
			}
		}
		# reveal a secret lover, if this is a previously existing character
		if = {
			limit = {
				NOR = {
					has_character_flag = otherparent_will_be_dead
					has_character_flag = otherparent_will_remain_lover
					has_character_flag = otherparent_will_be_courtier
				}
				scope:ansf_bastard_parent = {
					any_secret = {
						type = secret_lover
						secret_target = scope:ansf_bastard_otherparent
					}
				}
			}
			scope:ansf_bastard_parent = {
				random_secret = {
					type = secret_lover
					limit = { secret_target = scope:ansf_bastard_otherparent }
					expose_secret = scope:ansf_bastard_parent
				}
			}
		}		
		clear_saved_scope = halfsibling
		clear_saved_scope = ansf_bastard_father
		clear_saved_scope = ansf_bastard_mother
		clear_saved_scope = ansf_dominant_parent
		remove_variable = is_former_spouse
		remove_variable = is_concubine
		remove_variable = is_lover_bastard
		remove_character_flag = otherparent_will_be_dead
		remove_character_flag = otherparent_will_remain_lover
		remove_character_flag = otherparent_will_be_courtier
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2001
		
		ai_chance = {
			base = 100
		}
	}
	
	#No, that's not them
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.halfsibling.event3.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.halfsibling.event3.redo.tooltip
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2105
		
		ai_chance = {
			base = 0
		}
	}
	
	#Oops, go back
	option = {
		name = ansf.halfsibling.nevermind
		custom_tooltip = ansf.halfsibling.nevermind.tooltip
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_disappear_effect = { KILLED = scope:ansf_bastard_otherparent }
		clear_saved_scope = halfsibling
		clear_saved_scope = ansf_bastard_father
		clear_saved_scope = ansf_bastard_mother
		clear_saved_scope = ansf_dominant_parent
		remove_variable = is_former_spouse
		remove_variable = is_concubine
		remove_variable = is_lover_bastard
		remove_character_flag = otherparent_will_be_dead
		remove_character_flag = otherparent_will_remain_lover
		remove_character_flag = otherparent_will_be_courtier
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.1004
		
		ai_chance = {
			base = 1
		}
	}
	
}

######################
#Create Spouse Events#
######################

scripted_effect post_creation_inlaw_effect = {
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_clone
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:target
		if = {
			limit = { is_male = yes }
			add_character_flag = ansf_family_male
		}
		else = {
			add_character_flag = ansf_family_female
		}
	}
	
	#traits
	ansf_validate_character_traits = yes
	if = {
		limit = { is_female = yes }
		every_character_trait = {
			limit = {
				NAND = {
					exists = scope:random_spouse_option
					scope:random_spouse_option = yes
				}
				OR = {
					this = trait:beauty_bad_2
					this = trait:beauty_bad_3
					this = trait:intellect_bad_2
					this = trait:intellect_bad_3
					this = trait:physique_bad_2
					this = trait:physique_bad_3
					this = trait:hunchbacked
					this = trait:dwarf
					this = trait:giant
					this = trait:inbred
					this = trait:spindly
					this = trait:scaly
					this = trait:albino
					this = trait:infertile
				}
			}
			scope:ansf_family_member = { remove_trait = prev }
		}
	}
	
	#relatives
	if = {
		limit = {
			any_sibling = {
				even_if_dead = yes
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
		}
		add_trait = twin
		remove_character_flag ?= ansf_family_male
		remove_character_flag ?= ansf_family_female
		random_sibling = {
			even_if_dead = yes
			limit = {
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
			scope:ansf_family_member = {
				add_character_flag = is_different_DNA
				copy_inheritable_appearance_from = prev
			}
		}
	}	
	if = {
		limit = {
			NAND = {
				exists = scope:spouse_creation
				scope:spouse_creation = flag:existing
			}
		}
		check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
	}
	root = {
		if = {
			limit = {
				scope:ansf_family_member = { is_male = yes }
			}
			set_variable = {
				name = actual_father_in_law
				value = scope:ansf_family_member
			}
		}
		else = {
			set_variable = {
				name = actual_mother_in_law
				value = scope:ansf_family_member
			}
		}
		if = {
			limit = {
				OR = {
					scope:ansf_family_member = { is_male = yes }
					NOT = { has_variable = actual_father_in_law }
					AND = {
						has_variable = actual_father_in_law
						scope:ansf_family_member = {
							is_female = yes
							NOT = { has_trait = bastard }
						}
						var:actual_father_in_law = { has_trait = bastard }
					}
				}
			}
			set_variable = {
				name = dominant_in_law
				value = scope:ansf_family_member
			}
		}
	}
	
	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		root = {
			ansf_remove_all_gui_variables = yes
			set_variable = { name = ansf_family_character value = scope:ansf_family_member }
			set_variable = { name = ansf_family_character_age value = scope:ansf_family_member.age }
			set_variable = ansf_family_character_education_random
		}
		if = {
			limit = {
				root = { has_character_flag = inlaw_existing_dynasty }
			}
			root = {
				set_variable = ansf_family_character_cannot_change_age
			}
		}
		else_if = {
			limit = { is_male = yes }
			root = {
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age_min value = 35 }
				set_variable = { name = ansf_family_character_age_max value = 50 }
				set_variable = ansf_has_culture_faith_template
			}
		}
		else = {
			root = {
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age_min value = 35 }
				set_variable = { name = ansf_family_character_age_max value = target_spouse_mother_in_law_max_age_range }
				set_variable = ansf_has_culture_faith_template
			}
		}
	}
}

scripted_effect post_creation_spouse_effect = {
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_clone
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:target
		if = {
			limit = { is_male = yes }
			add_character_flag = ansf_family_male
		}
		else = {
			add_character_flag = ansf_family_female
		}
	}
	
	#traits
	ansf_validate_character_traits = yes
	every_character_trait = {
		limit = {
			NAND = {
				exists = scope:random_spouse_option
				scope:random_spouse_option = yes
			}
			OR = {
				this = trait:beauty_bad_1
				this = trait:beauty_bad_2
				this = trait:beauty_bad_3
				this = trait:intellect_bad_1
				this = trait:intellect_bad_2
				this = trait:intellect_bad_3
				this = trait:physique_bad_2
				this = trait:physique_bad_3
				this = trait:hunchbacked
				this = trait:dwarf
				this = trait:giant
				this = trait:inbred
				this = trait:spindly
				this = trait:scaly
				this = trait:albino
				this = trait:infertile
			}
		}
		scope:ansf_family_member = { remove_trait = prev }
	}
	if = {
		limit = {
			is_female = yes
			NOR = {
				AND = {
					exists = scope:random_spouse_option
					scope:random_spouse_option = yes
				}
				has_trait = beauty_bad
				has_trait = beauty_good
			}
		}
		random = {
			chance = 50
			add_trait = beauty_good_1
		}
	}
	else_if = {
		limit = {
			is_male = yes
			NOR = {
				AND = {
					exists = scope:random_spouse_option
					scope:random_spouse_option = yes
				}
				has_trait = physique_bad
				has_trait = physique_good
			}
		}
		random = {
			chance = 50
			add_trait = physique_good_1
		}
	}
	
	#relatives
	if = {
		limit = {
			any_sibling = {
				even_if_dead = yes
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
		}
		add_trait = twin
		remove_character_flag ?= ansf_family_male
		remove_character_flag ?= ansf_family_female
		random_sibling = {
			even_if_dead = yes
			limit = {
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
			scope:ansf_family_member = {
				add_character_flag = is_different_DNA
				copy_inheritable_appearance_from = prev
			}
		}
	}	
	if = {
		limit = {
			exists = scope:spouse_creation
			scope:spouse_creation = flag:no_parent_spouse
		}
		check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
	}
	root = { remove_variable ?= culture_faith_template }
	
	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		root = {
			ansf_remove_all_gui_variables = yes
			set_variable = { name = ansf_family_character value = scope:ansf_family_member }
			set_variable = { name = ansf_family_character_age value = scope:ansf_family_member.age }
			set_variable = ansf_family_character_education_random
		}
		if = {
			limit = {
				root = { has_character_flag = spouse_existing_dynasty }
			}
			root = {
				set_variable = ansf_family_character_cannot_change_age
			}
		}
		else_if = {
			limit = {
				root = { has_character_flag = created_spouse_lowborn }
				is_female = yes
			}
			root = {
				remove_character_flag = created_spouse_lowborn
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age_min value = adulthood_start_age }
				set_variable = { name = ansf_family_character_age_max value = root.age }
				set_variable = ansf_cannot_change_education
			}
		}
		else_if = {
			limit = {
				root = { has_character_flag = created_spouse_lowborn }
				is_male = yes
			}
			root = {
				remove_character_flag = created_spouse_lowborn
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age_min value = 18 }
				set_variable = { name = ansf_family_character_age_max value = age_compared_to_target_plus_ten }
				set_variable = ansf_cannot_change_education
			}
		}
		else_if = {
			limit = {
				exists = scope:spouse_creation
				scope:spouse_creation = flag:no_parent_spouse
			}
			root = {
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age_min value = adulthood_start_age }
				set_variable = { name = ansf_family_character_age_max value = age_compared_to_target_plus_four }
				set_variable = ansf_has_culture_faith_template
			}
		}
		else = {
			root = {
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age_min value = adulthood_start_age }
				set_variable = { name = ansf_family_character_age_max value = target_spouse_max_age_range }
			}
		}
	}
}

ansf.2200 = { #Options for creating a new spouse
	type = character_event
	window = ansf_event_window
	title = ansf.2200.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2200.desc
			}
			desc = ansf.2200.other.desc
		}
	}
	theme = family
	
	center_portrait = {
		character = scope:target
		animation = thinking
	}
	override_background = {
		trigger = { scope:target = { is_landed = yes } }
		reference = throne_room
	}
	override_background = {
		trigger = { scope:target = { is_landed = no } }
		reference = sitting_room
	}
	
	immediate = {
		hidden_effect = {
			scope:target = {
				if = {
					limit = { is_male = yes }
					scope:new_dummy_female = { save_scope_as = new_spouse }
				}
				else = {
					scope:new_dummy_male = { save_scope_as = new_spouse }
				}
			}
		}
	}
	
	option = { #Spouse with parents
		name = ansf.spouse.yes.parents
		custom_tooltip = ansf.spouse.parents
		
		trigger_event = ansf.2201
		ai_chance = {
			base = 0
		}
	}
	option = { #Just the spouse
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.spouse.yes.alone
					}
					desc = ansf.spouse.other.yes.alone
				}
			}
		}
		custom_tooltip = ansf.spouse.alone
		
		trigger_event = ansf.2206
		ai_chance = {
			base = 100
		}
	}
	option = { #Lowborn spouse
		name = {
			trigger = { scope:new_spouse = { is_male = yes } }
			text = ansf.spouse.yes.lowborn.husband
		}
		name = {
			trigger = { scope:new_spouse = { is_female = yes } }
			text = ansf.spouse.yes.lowborn.wife
		}
		
		if = {
			limit = { scope:new_spouse = { is_male = yes } }
			custom_tooltip =  ansf.spouse.lowborn.husband
		}
		else = {
			custom_tooltip =  ansf.spouse.lowborn.wife
		}
		
		trigger_event = ansf.2209
		ai_chance = {
			base = 0
		}
	}
	option = { #Never mind
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:target = root }
						desc = ansf.spouse.no
					}
					desc = ansf.spouse.other.no
				}
			}
		}

		if = {
			limit = { scope:target = root }
			custom_tooltip = ansf.spouse.none
			trigger_event = ansf.1004
		}
		else = {
			custom_tooltip = ansf.spouse.other.none
			ansf_remove_all_flags_variables = yes
		}
		ai_chance = {
			base = 0
		}
	}
}

ansf.2201 = { #Is this your father-in-law?
	type = character_event
	window = ansf_event_window
	title = ansf.2201.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2201.desc
			}
			desc = ansf.2201.other.desc
		}
	}
	theme = family
	
	center_portrait = {
		character = scope:ansf_family_member
		outfit_tags = { travel_cloak }
	}
	override_background = {
		trigger = { scope:target = root }
		reference = wilderness
	}
	override_background = {
		trigger = { NOT = { scope:target = root } }
		reference = throne_room
	}
	
	immediate = {
		hidden_effect = {
			clear_variable_list = potential_grandparents
			clear_variable_list = potential_piblings
			clear_variable_list = potential_relatives
			
			if = {
				limit = { has_character_flag = no_creation_override }
				remove_character_flag = no_creation_override
				root = { add_character_flag = in_law_creation }
				check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
				root = { remove_character_flag = in_law_creation }
			}
			else = {
				# find a random template if we're not using the player's
				if = {
					limit = {
						has_variable = ansf_family_character_is_clone
						has_variable = ansf_family_character_generate
					}
					set_variable = {
						name = culture_faith_template
						value = scope:target
					}
				}
				else_if = {
					limit = {
						NOT = { has_variable = culture_faith_template }
					}
					find_random_template_ruler = yes
				}
				# create the father-in-law
				scope:new_dummy_male = { save_scope_as = source_parent }
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_father_in_law_max_age
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								post_creation_inlaw_effect = yes
							}
						}
					}
					40 = {
						modifier = {
							add = -35
							NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:new_dummy_male } }
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_father_in_law_max_age
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_father_in_law_max_age
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_father_in_law_max_age
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 0
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_father_in_law_max_age
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								post_creation_inlaw_effect = yes
							}
						}
					}
				}
			}
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}
	
	option = { #That's him
		name = ansf.spouse.father.yes
		
		scope:target_liege = { add_courtier = scope:ansf_family_member }
		
		scope:ansf_family_member = {
			add_character_flag = {
				flag = was_created
				days = 30
			}
		}
		save_temporary_scope_as = ruler
		save_scope_value_as = {
			name = spouse_creation
			value = flag:father_in_law
		}
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
		
		custom_tooltip = ansf.spouse.will.have.age.range
		custom_tooltip = ansf.spouse.will.join.house
		custom_tooltip = ansf.spouse.will.have.culture
		custom_tooltip = ansf.spouse.will.have.faith
	}
	option = { #He's part of a larger family
		trigger = {
			custom_description = {
				text = no_dynasty_options
				subject = scope:ansf_family_member
				has_variable_list = potential_relatives
				variable_list_size = { name = potential_relatives value > 0 }
			}
		}
		name = ansf.spouse.father.existing
		show_as_unavailable = {
			OR = {
				NOT = { has_variable_list = potential_relatives }
				variable_list_size = { name = potential_relatives value < 1 }
			}
		}		
		save_scope_value_as = {
			name = spouse_creation
			value = flag:existing
		}
		remove_variable ?= show_ansf_regenerate_window
		trigger_event = ansf.2204
		
		custom_tooltip = ansf.existing.spouse.parent.dynasty.tt
	}
	option = { #Redo with same faith/culture
		trigger = {
			scope:ansf_family_member = {
				OR = {
					NOT = { culture = scope:target.culture }
					NOT = { faith = scope:target.faith }
				}
			}
			NAND = {
				has_variable = ansf_family_character_generate
				has_variable = culture_faith_template
			}
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					NOR = {
						culture = scope:target.culture
						faith = scope:target.faith
					}
				}
			}
			text = ansf.spouse.father.redo.same.both
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					culture = scope:target.culture
					NOT = { faith = scope:target.faith }
				}
			}
			text = ansf.spouse.father.redo.same.faith
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					faith = scope:target.faith
					NOT = { culture = scope:target.culture }
				}
			}
			text = ansf.spouse.father.redo.same.culture
		}

		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters.same
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.spouse.redo.father.same.tt
		}
		
		set_variable = {
			name = culture_faith_template
			value = scope:target
		}
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2201
	}
	option = { #Redo
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.spouse.father.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.spouse.redo.father.tt
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2201
	}
	option = { #No father-in-law
		name = ansf.spouse.father.no
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		remove_variable ?= actual_father_in_law
		remove_variable ?= dominant_in_law
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2202
		
		custom_tooltip = ansf.spouse.proceed.spouse.mother.tt
	}
}

ansf.2202 = { #Is this your mother-in-law?
	type = character_event
	window = ansf_event_window
	title = ansf.2202.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2202.desc
			}
			desc = ansf.2202.other.desc
		}
	}
	theme = family
	
	center_portrait = scope:ansf_family_member
	override_background = { reference = relaxing_room }
	
	immediate = {
		hidden_effect = {
			clear_variable_list = potential_grandparents
			clear_variable_list = potential_piblings
			clear_variable_list = potential_relatives
			
			if = {
				limit = { has_character_flag = no_creation_override }
				remove_character_flag = no_creation_override
				check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
			}
			else = {
				# find a random template if we're not using the player's
				if = {
					limit = {
						has_variable = ansf_family_character_is_clone
						has_variable = ansf_family_character_generate
					}
					set_variable = {
						name = culture_faith_template
						value = scope:target
					}
				}
				else_if = {
					limit = {
						NOT = { has_variable = culture_faith_template }
					}
					find_random_template_ruler = yes
				}
				# create the mother-in-law
				scope:new_dummy_female = { save_scope_as = source_parent }
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_mother_in_law_max_age_range
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:new_dummy_female } }
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_mother_in_law_max_age_range
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_mother_in_law_max_age_range
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_mother_in_law_max_age_range
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender_female_chance = 100
							age = {
								integer_range = {
									min = target_spouse_in_law_min_age
									max = target_spouse_mother_in_law_max_age_range
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								post_creation_inlaw_effect = yes
							}
						}
					}
				}
			}
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}
	
	option = { #That's her
		name = ansf.spouse.mother.yes
		
		scope:target_liege = { add_courtier = scope:ansf_family_member }
		if = {
			limit = {
				has_variable = actual_father_in_law
				var:dominant_in_law = scope:ansf_family_member
			}
			scope:ansf_family_member = { marry_matrilineal = root.var:actual_father_in_law }
		}
		else_if = {
			limit = { has_variable = actual_father_in_law }
			scope:ansf_family_member = { marry = root.var:actual_father_in_law }
		}
		
		scope:ansf_family_member = {
			add_character_flag = {
				flag = was_created
				days = 30
			}
		}		
		save_temporary_scope_as = ruler
		save_scope_value_as = {
			name = spouse_creation
			value = flag:mother_in_law
		}
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
		
		custom_tooltip = ansf.spouse.will.have.age.range
		custom_tooltip = ansf.spouse.will.join.house
		custom_tooltip = ansf.spouse.will.have.culture
		custom_tooltip = ansf.spouse.will.have.faith
	}
	option = { #She's part of a larger family
		trigger = {
			custom_description = {
				text = no_dynasty_options
				subject = scope:ansf_family_member
				has_variable_list = potential_relatives
				variable_list_size = { name = potential_relatives value > 0 }
			}
		}
		name = ansf.spouse.mother.existing
		show_as_unavailable = {
			OR = {
				NOT = { has_variable_list = potential_relatives }
				variable_list_size = { name = potential_relatives value < 1 }
			}
		}		
		save_scope_value_as = {
			name = spouse_creation
			value = flag:existing
		}
		remove_variable ?= show_ansf_regenerate_window
		trigger_event = ansf.2204
		
		custom_tooltip = ansf.existing.spouse.parent.dynasty.tt
	}
	option = { #Redo with same faith/culture
		trigger = {
			scope:ansf_family_member = {
				OR = {
					NOT = { culture = scope:target.culture }
					NOT = { faith = scope:target.faith }
				}
			}
			NAND = {
				has_variable = ansf_family_character_generate
				has_variable = culture_faith_template
			}
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					NOR = {
						culture = scope:target.culture
						faith = scope:target.faith
					}
				}
			}
			text = ansf.spouse.father.redo.same.both
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					culture = scope:target.culture
					NOT = { faith = scope:target.faith }
				}
			}
			text = ansf.spouse.father.redo.same.faith
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					faith = scope:target.faith
					NOT = { culture = scope:target.culture }
				}
			}
			text = ansf.spouse.father.redo.same.culture
		}

		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters.same
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.spouse.redo.father.same.tt
		}
		
		set_variable = {
			name = culture_faith_template
			value = scope:target
		}
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2202
	}
	option = { #Redo
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.spouse.father.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.spouse.redo.father.tt
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2202
	}
	option = { #No mother-in-law
		trigger = { has_variable = actual_father_in_law }
		name = ansf.spouse.mother.no
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		remove_variable ?= actual_mother_in_law
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2203
		
		custom_tooltip = ansf.spouse.proceed.spouse.no.mother.tt
	}
}

ansf.2203 = { #Is this your spouse? (created)
	type = character_event
	window = ansf_event_window
	title = ansf.2203.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2203.desc
			}
			desc = ansf.2203.other.desc
		}
	}
	theme = family
	
	center_portrait = scope:ansf_family_member
	lower_right_portrait = root.var:actual_mother_in_law
	lower_left_portrait = root.var:actual_father_in_law
	override_background = {
		trigger = { scope:target = root }
		reference = bedchamber
	}
	
	immediate = {
		hidden_effect = {
			if = {
				limit = {
					has_variable = actual_father_in_law
					has_variable = actual_mother_in_law
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:new_spouse = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:target } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					has_variable = actual_father_in_law
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:new_spouse = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:target } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							father = root.var:actual_father_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
				}
			}
			else = {
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					40 = {
						modifier = {
							add = -35
							NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:new_spouse } }
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:dominant_in_law.culture
							faith = root.var:dominant_in_law.faith
							dynasty = none
							mother = root.var:actual_mother_in_law
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = target_spouse_max_age_range
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = root.var:dominant_in_law.house
								post_creation_spouse_effect = yes
							}
						}
					}
				}
			}	
		
			save_scope_value_as = {
				name = spouse_creation
				value = flag:no
			}
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	option = { #That's them
		name = ansf.spouse.final.yes
		
		scope:target_liege = { add_courtier = scope:ansf_family_member }
		
		scope:target = {
			if = {
				limit = {
					is_female = yes
					is_playable_character = yes
				}
				marry_matrilineal = scope:ansf_family_member
			}
			else = {
				marry = scope:ansf_family_member
			}
		}
		
		remove_variable ?= actual_mother_in_law
		remove_variable ?= actual_father_in_law
		remove_variable ?= dominant_in_law
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
	}
	option = { #Redo
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.spouse.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.spouse.redo.spouse.tt
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2203
	}
	option = { #Never mind
		name = ansf.spouse.cancel
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = {
				has_variable = actual_father_in_law
				var:actual_father_in_law = { has_character_flag = was_created }
			}
			ansf_disappear_effect = { KILLED = root.var:actual_father_in_law }
		}
		if = {
			limit = {
				has_variable = actual_mother_in_law
				var:actual_mother_in_law = { has_character_flag = was_created }
			}
			ansf_disappear_effect = { KILLED = root.var:actual_mother_in_law }
		}
		remove_variable ?= actual_mother_in_law
		remove_variable ?= actual_father_in_law
		remove_variable ?= dominant_in_law
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2200
		
		custom_tooltip = ansf.spouse.cancel.tt
	}
}

ansf.2204 = { #In-law from an existing dynasty
	type = character_event
	window = ansf_dynasty_event_window
	title = ansf.2204.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2204.desc
			}
			desc = ansf.2204.other.desc
		}
	}
	theme = family

	center_portrait = {
		character = scope:target
		animation = thinking
	}
	override_background = {
		trigger = { scope:target = { is_landed = yes } }
		reference = throne_room
	}
	override_background = {
		trigger = { scope:target = { is_landed = no } }
		reference = sitting_room
	}

	immediate = {
		set_variable = { name = ansf_family_character value = scope:ansf_family_member }
	}

	# Selection is their father
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_grandparents
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.grandparent
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = ansf.yes.existing1.inlaw.grandparent.tt
		root.var:potential_relative_1 = {
			if = {
				limit = { is_male = yes }
				save_temporary_scope_as = actual_grandfather
				root = {
					set_variable = {
						name = actual_grandfather
						value = scope:actual_grandfather
					}
				}
			}
			else = {
				save_temporary_scope_as = actual_grandmother
				root = {
					set_variable = {
						name = actual_grandmother
						value = scope:actual_grandmother
					}
				}
			}
			if = {
				limit = {
					should_be_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.inlaw.bastard.tt
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					any_consort = {
						even_if_dead = yes
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
				}
				random_consort = {
					even_if_dead = yes
					limit = {
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
					if = {
						limit = { is_female = yes }
						save_temporary_scope_as = actual_grandmother
						root = {
							set_variable = {
								name = actual_grandmother
								value = scope:actual_grandmother
							}
						}
					}
					else = {
						save_temporary_scope_as = actual_grandfather
						root = {
							set_variable = {
								name = actual_grandfather
								value = scope:actual_grandfather
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2205
	}
	
	# Selection is their brother
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_piblings
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.pibling
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = ansf.yes.existing1.inlaw.pibling.tt
		root.var:potential_relative_1 = {
			root = {
				set_variable = {
					name = actual_grandfather
					value = root.var:potential_relative_1.father
				}
			}
			if = {
				limit = {
					should_be_fathers_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.inlaw.bastard.tt
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					should_be_legitimate_bastard = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.inlaw.legit.bastard.tt
				root = {
					set_variable = {
						name = will_be_legitimate_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					father = {
						any_consort = {
							even_if_dead = yes
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
					}
				}
				father = {
					random_consort = {
						even_if_dead = yes
						limit = {
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
						save_temporary_scope_as = actual_grandmother
						root = {
							set_variable = {
								name = actual_grandmother
								value = scope:actual_grandmother
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2205
	}
	
	# Go back
	option = {
		name = ansf.no.inlaw.existing
		add_character_flag = no_creation_override
		
		remove_variable = potential_relative_1
		remove_variable = ansf_family_character
		
		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}
		
		custom_tooltip = ansf.no.me.existing.tt
		
		if = {
			limit = { scope:ansf_family_member = { is_male = yes } }
			trigger_event = ansf.2201
		}
		else = {
			trigger_event = ansf.2202
		}
	}
}

ansf.2205 = { #Create the in-law with connection to an existing dynasty
	type = character_event
	window = ansf_event_window
	title = ansf.2205.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.2205.grandparent.desc
			}
			triggered_desc = {
				trigger = {
					scope:target = root
					NOR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.2205.pibling.desc
			}
			triggered_desc = {
				trigger = {
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.2205.grandparent.other.desc
			}
			desc = ansf.2205.pibling.other.desc
		}
	}
	
	theme = family
	center_portrait = scope:ansf_family_member	
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = yes } }
		reference = relaxing_room
	}
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = no } }
		reference = wilderness
	}

	immediate = {
		hidden_effect = {
			add_character_flag = inlaw_existing_dynasty
			if = {
				limit = {
					has_variable = actual_grandfather
					has_variable = actual_grandmother
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					has_variable = actual_grandfather
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
				}
			}
			else = {
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_inlaw_effect = yes
							}
						}
					}
				}
			}
			remove_character_flag = inlaw_existing_dynasty			
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}
	
	option = { # That's them
		name = ansf.yes.inlaw.existing.proceed
		
		connect_to_relative_effect = yes
		
		scope:target_liege = { add_courtier = scope:ansf_family_member }
		scope:ansf_family_member = {
			add_character_flag = {
				flag = was_created
				days = 30
			}
		}
		save_temporary_scope_as = ruler
		if = {
			limit = { scope:ansf_family_member = { is_male = yes } }
			save_scope_value_as = {
				name = spouse_creation
				value = flag:father_in_law
			}
		}
		else = {
			save_scope_value_as = {
				name = spouse_creation
				value = flag:mother_in_law
			}
			if = {
				limit = { has_variable = actual_father_in_law }
				scope:ansf_family_member = { marry = root.var:actual_father_in_law }
			}
		}
		
		scope:ansf_family_member = {
			random_sibling = {
				even_if_dead = yes
				limit = {
					NOT = { has_trait = twin }
					mother = scope:ansf_family_member.mother
					father = scope:ansf_family_member.father
					age_including_death_age = scope:ansf_family_member.age
				}
				add_trait = twin
			}
		}
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		ansf_remove_all_gui_variables = yes
	
		trigger_event = ansf.2001
		
		custom_tooltip = ansf.spouse.will.have.age.range
		custom_tooltip = ansf.spouse.will.join.house
		custom_tooltip = ansf.spouse.will.have.culture
		custom_tooltip = ansf.spouse.will.have.faith
	}
	
	option = { # Redo
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.no.existing.reroll
				}
			}
		}

		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.reroll.inlaw.existing
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
	
		trigger_event = ansf.2205
	}
	
	option = { # Never mind
		name = ansf.no.existing.leave
		custom_tooltip = ansf.no.existing.leave.tt
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }

		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		ansf_remove_all_gui_variables = yes
		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}
		if = {
			limit = { scope:ansf_family_member = { is_male = yes } }
			trigger_event = ansf.2201
		}
		else = {
			trigger_event = ansf.2202
		}
	}
}

ansf.2206 = { #Is this your spouse? (created with no parents)
	type = character_event
	window = ansf_event_window
	title = ansf.2206.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2206.desc
			}
			desc = ansf.2206.other.desc
		}
	}
	theme = family
	
	center_portrait = scope:ansf_family_member
	override_background = { reference = bedchamber }
	
	immediate = {
		hidden_effect = {
			clear_variable_list = potential_grandparents
			clear_variable_list = potential_piblings
			clear_variable_list = potential_relatives

			save_scope_value_as = {
				name = spouse_creation
				value = flag:no_parent_spouse
			}

			if = {
				limit = { has_character_flag = no_creation_override }
				remove_character_flag = no_creation_override
				check_for_possible_relatives = { COMPARE = scope:ansf_family_member }
			}
			else = {			
			# find a random template if we're not using the player's
				if = {
					limit = {
						has_variable = ansf_family_character_is_clone
						has_variable = ansf_family_character_generate
					}
					set_variable = {
						name = culture_faith_template
						value = scope:target
					}
				}
				else_if = {
					limit = {
						NOT = { has_variable = culture_faith_template }
					}
					find_random_template_ruler = yes
				}
				# create the spouse
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = age_compared_to_target_plus_four
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:new_spouse = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:target } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = age_compared_to_target_plus_four
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = age_compared_to_target_plus_four
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = age_compared_to_target_plus_four
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:new_spouse
							age = {
								integer_range = {
									min = target_spouse_min_age
									max = age_compared_to_target_plus_four
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								post_creation_spouse_effect = yes
							}
						}
					}
				}
			}
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	option = { #That's them
		name = ansf.spouse.final.yes
		
		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}
		
		scope:target_liege = { add_courtier = scope:ansf_family_member }
		
		scope:target = {
			if = {
				limit = {
					is_female = yes
					is_playable_character = yes
				}
				marry_matrilineal = scope:ansf_family_member
			}
			else = {
				marry = scope:ansf_family_member
			}
		}
		
		ansf_remove_all_gui_variables = yes
		
		if = {
			limit = {
				exists = scope:random_spouse_option
				scope:random_spouse_option = yes
			}
			ansf_remove_all_flags_variables = yes
		}
		else = {
			trigger_event = ansf.2001
		}
		
		ai_chance = {
			base = 100
		}
	}
	option = { #They belong to a larger family
		trigger = {
			custom_description = {
				text = no_dynasty_options
				subject = scope:ansf_family_member
				has_variable_list = potential_relatives
				variable_list_size = { name = potential_relatives value > 0 }
			}
		}
		name = ansf.spouse.existing
		show_as_unavailable = {
			OR = {
				NOT = { has_variable_list = potential_relatives }
				variable_list_size = { name = potential_relatives value < 1 }
			}
		}
		custom_tooltip = ansf.existing.spouse.dynasty.tt
		remove_variable ?= show_ansf_regenerate_window
		trigger_event = ansf.2207
		ai_chance = {
			base = 0
		}
	}
	option = { #Redo
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.spouse.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip =  ansf.spouse.redo.spouse2.tt
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2206
		ai_chance = {
			base = 0
		}
	}
	option = { #Redo - same culture/faith
		trigger = {
			scope:ansf_family_member = {
				OR = {
					NOT = { culture = scope:target.culture }
					NOT = { faith = scope:target.faith }
				}
			}
			NAND = {
				has_variable = ansf_family_character_generate
				has_variable = culture_faith_template
			}
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					NOR = {
						culture = scope:target.culture
						faith = scope:target.faith
					}
				}
			}
			text = ansf.spouse.redo.same.both
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					culture = scope:target.culture
					NOT = { faith = scope:target.faith }
				}
			}
			text = ansf.spouse.redo.same.faith
		}
		name = {
			trigger = {
				scope:ansf_family_member = {
					faith = scope:target.faith
					NOT = { culture = scope:target.culture }
				}
			}
			text = ansf.spouse.redo.same.culture
		}

		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters.same
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.spouse.redo.same.tt
		}

		set_variable = {
			name = culture_faith_template
			value = scope:target
		}
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2206
		ai_chance = {
			base = 0
		}
	}
	option = { #Never mind
		name = ansf.spouse.cancel

		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2200
		
		custom_tooltip = ansf.spouse.cancel.tt
		ai_chance = {
			base = 0
		}
	}
}

ansf.2207 = { #Spouse from an existing dynasty
	type = character_event
	window = ansf_dynasty_event_window
	title = ansf.2207.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2207.desc
			}
			desc = ansf.2207.other.desc
		}
	}
	theme = family

	center_portrait = {
		character = scope:target
		animation = thinking
	}
	override_background = { reference = bedchamber }
	
	immediate = {
		set_variable = { name = ansf_family_character value = scope:ansf_family_member }
	}

	# They're part of Dynasty 1
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_grandparents
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.grandparent
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = ansf.yes.existing1.spouse.grandparent.tt
		root.var:potential_relative_1 = {
			if = {
				limit = { is_male = yes }
				save_temporary_scope_as = actual_grandfather
				root = {
					set_variable = {
						name = actual_grandfather
						value = scope:actual_grandfather
					}
				}
			}
			else = {
				save_temporary_scope_as = actual_grandmother
				root = {
					set_variable = {
						name = actual_grandmother
						value = scope:actual_grandmother
					}
				}
			}
			if = {
				limit = {
					should_be_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.spouse.bastard.tt
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					any_consort = {
						even_if_dead = yes
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
				}
				random_consort = {
					even_if_dead = yes
					limit = {
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
					if = {
						limit = { is_female = yes }
						save_temporary_scope_as = actual_grandmother
						root = {
							set_variable = {
								name = actual_grandmother
								value = scope:actual_grandmother
							}
						}
					}
					else = {
						save_temporary_scope_as = actual_grandfather
						root = {
							set_variable = {
								name = actual_grandfather
								value = scope:actual_grandfather
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2208
	}
	
	# Selection is her brother
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_piblings
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.pibling
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = ansf.yes.existing1.spouse.pibling.tt
		root.var:potential_relative_1 = {
			root = {
				set_variable = {
					name = actual_grandfather
					value = root.var:potential_relative_1.father
				}
			}
			if = {
				limit = {
					should_be_fathers_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.spouse.bastard.tt
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					should_be_legitimate_bastard = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = ansf.yes.existing.spouse.legit.bastard.tt
				root = {
					set_variable = {
						name = will_be_legitimate_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					father = {
						any_consort = {
							even_if_dead = yes
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
					}
				}
				father = {
					random_consort = {
						even_if_dead = yes
						limit = {
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
						save_temporary_scope_as = actual_grandmother
						root = {
							set_variable = {
								name = actual_grandmother
								value = scope:actual_grandmother
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2208
	}
		
	# Go back
	option = {
		name = ansf.no.spouse.existing
		add_character_flag = no_creation_override
		
		remove_variable = potential_relative_1
		remove_variable = ansf_family_character
		
		custom_tooltip = ansf.no.me.existing.tt
		
		trigger_event = ansf.2206
	}
}

ansf.2208 = { #Create the spouse with connection to an existing dynasty
	type = character_event
	window = ansf_event_window
	title = ansf.2208.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = root
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.2208.grandparent.desc
			}
			triggered_desc = {
				trigger = {
					scope:target = root
					NOR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.2208.pibling.desc
			}
			triggered_desc = {
				trigger = {
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.2208.grandparent.other.desc
			}
			desc = ansf.2208.pibling.other.desc
		}
	}
	
	theme = family
	center_portrait = scope:ansf_family_member	
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = yes } }
		reference = relaxing_room
	}
	override_background = {
		trigger = { scope:ansf_family_member = { is_female = no } }
		reference = wilderness
	}

	immediate = {
		hidden_effect = {
			add_character_flag = spouse_existing_dynasty
			if = {
				limit = {
					has_variable = actual_grandfather
					has_variable = actual_grandmother
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:target_liege } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					has_variable = actual_grandfather
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:target_liege } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
				}
			}
			else = {
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:target_liege } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_family_member
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_spouse_effect = yes
							}
						}
					}
				}
			}	
			remove_character_flag = spouse_existing_dynasty
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}
	
	option = { # That's them
		name = ansf.yes.spouse.existing.proceed
		
		connect_to_relative_effect = yes
		
		scope:target_liege = { add_courtier = scope:ansf_family_member }

		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}

		scope:target = {
			if = {
				limit = {
					is_female = yes
					is_playable_character = yes
				}
				marry_matrilineal = scope:ansf_family_member
			}
			else = {
				marry = scope:ansf_family_member
			}
		}
		
		scope:ansf_family_member = {
			random_sibling = {
				even_if_dead = yes
				limit = {
					NOT = { has_trait = twin }
					mother = scope:ansf_family_member.mother
					father = scope:ansf_family_member.father
					age_including_death_age = scope:ansf_family_member.age
				}
				add_trait = twin
			}
		}
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent		
		ansf_remove_all_gui_variables = yes
	
		trigger_event = ansf.2001
	}
	
	option = { # Redo
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.no.existing.reroll
				}
			}
		}

		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip =  ansf.reroll.spouse.existing
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
	
		trigger_event = ansf.2208
	}
	
	option = { # Never mind
		name = ansf.no.existing.leave
		custom_tooltip = ansf.no.existing.leave.tt
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }

		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		ansf_remove_all_gui_variables = yes
		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}
		trigger_event = ansf.2200
	}
}

ansf.2209 = { #Is this your spouse? (created lowborn}
	type = character_event
	window = ansf_event_window
	title = ansf.2209.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:target = root }
				desc = ansf.2209.desc
			}
			desc = ansf.2209.other.desc
		}
	}
	theme = family
	
	center_portrait = scope:ansf_family_member
	override_background = { reference = bedchamber }
	
	immediate = {
		hidden_effect = {
			add_character_flag = created_spouse_lowborn
			save_scope_value_as = {
				name = spouse_creation
				value = flag:no_parent_spouse
			}
			
			if = {
				limit = { scope:new_spouse = { is_male = yes } }
				create_character = {
					location = scope:target.location
					culture = scope:target.culture
					faith = scope:target.faith
					gender = scope:new_spouse
					dynasty = none
					age = {
						integer_range = {
							min = target_spouse_lowborn_male_min_age
							max = target_spouse_lowborn_male_max_age
						}
					}
					template = olympia_lowborn_male
					save_scope_as = ansf_family_member
					after_creation = {
						post_creation_spouse_effect = yes
					}
				}
			}
			else = {
				create_character = {
					location = scope:target.location
					culture = scope:target.culture
					faith = scope:target.faith
					gender = scope:new_spouse
					dynasty = none
					age = {
						integer_range = {
							min = target_spouse_min_age
							max = target_spouse_lowborn_female_max_age
						}
					}
					template = olympia_lowborn_female
					save_scope_as = ansf_family_member
					after_creation = {
						post_creation_spouse_effect = yes
					}
				}
			}
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}
	
	option = { #That's them
		name = ansf.spouse.final.yes
		
		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}
		
		scope:target_liege = { add_courtier = scope:ansf_family_member }
		
		scope:target = {
			if = {
				limit = {
					is_female = yes
					has_dynasty = yes
				}
				marry_matrilineal = scope:ansf_family_member
			}
			else = {
				marry = scope:ansf_family_member
			}
		}
		
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2001
	}	
	option = { #Redo
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.spouse.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip =  ansf.spouse.redo.spouse2.tt
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2209
	}
	option = { #Never mind
		name = ansf.spouse.cancel
		
		save_scope_value_as = {
			name = spouse_creation
			value = flag:no
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2200
		
		custom_tooltip = ansf.spouse.cancel.tt
	}
}

######################
#Create Child Events #
######################

scripted_effect set_child_parental_flags = {
	# determine which of the two is the mother and father
	if = {
		limit = { is_female = yes }
		save_scope_as = ansf_mother
		scope:ansf_bastard_otherparent = { save_scope_as = ansf_father }
	}
	else = {
		save_scope_as = ansf_father
		scope:ansf_bastard_otherparent = { save_scope_as = ansf_mother }
	}
	# determine which is the "dominant" parent (will pass on dynasty)
	if = {
		limit = { exists = root.var:is_concubine }
		scope:ansf_father = { save_scope_as = ansf_dominant_parent }
	}
	else_if = {
		limit = {
			exists = root.var:is_former_spouse
			OR = {
				AND = {
					scope:ansf_mother = { is_ruler = yes }
					scope:ansf_father = { is_ruler = no }
				}
				AND = {
					scope:ansf_mother = { has_dynasty = yes }
					scope:ansf_father = { has_dynasty = no }
				}
			}
		}
		scope:ansf_mother = { save_scope_as = ansf_dominant_parent }
	}
	else_if = {
		limit = { exists = root.var:is_former_spouse }
		scope:ansf_father = { save_scope_as = ansf_dominant_parent }
	}
	else_if = {
		limit = { exists = scope:ansf_mother.house }
		scope:ansf_mother = { save_scope_as = ansf_dominant_parent }
	}
	else = {
		scope:ansf_father = { save_scope_as = ansf_dominant_parent }
	}
	# give notifications of what the half-sibling will be
	custom_tooltip = ansf.your.notdynasty.tt
	custom_tooltip = ansf.your.notculture.tt
	custom_tooltip = ansf.your.notfaith.tt
}

scripted_effect post_creation_child_effect = {
	#appearance
	add_character_flag = no_hat
	if = {
		limit = {
			root = {
				has_variable = ansf_family_character_is_clone
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = root
		if = {
			limit = { is_female = yes }
			add_character_flag = ansf_family_female
		}
		else = {
			add_character_flag = ansf_family_male
		}
	}
	else_if = {
		limit = {
			root = {
				has_variable = ansf_family_character_use_father_dna
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:ansf_family_member.father
		if = {
			limit = { is_female = yes }
			add_character_flag = ansf_family_female
		}
		else = {
			add_character_flag = ansf_family_male
		}
	}
	else_if = {
		limit = {
			root = {
				has_variable = ansf_family_character_use_mother_dna
				has_variable = ansf_family_character_generate
			}
		}
		copy_inheritable_appearance_from = scope:ansf_family_member.mother
		if = {
			limit = { is_female = yes }
			add_character_flag = ansf_family_female
		}
		else = {
			add_character_flag = ansf_family_male
		}
	}

	#birth traits
	if = {
		limit = { exists = root.var:is_concubine }
		add_trait = child_of_concubine_female
	}
	else_if = {
		limit = { exists = root.var:is_lover_bastard }
		ansf_add_bastard_trait_based_on_faith_effect = yes
	}
	
	#traits
	assign_quirk_effect = yes
	ansf_validate_character_traits = yes
	
	#add the twin trait if a same-age sibling exists
	if = {
		limit = {
			any_sibling = {
				even_if_dead = yes
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
		}
		add_trait = twin
		remove_character_flag ?= ansf_family_male
		remove_character_flag ?= ansf_family_female
		random_sibling = {
			even_if_dead = yes
			limit = {
				NOT = { has_trait = twin }
				mother = scope:ansf_family_member.mother
				father = scope:ansf_family_member.father
				age_including_death_age = scope:ansf_family_member.age
			}
			scope:ansf_family_member = {
				copy_inheritable_appearance_from = prev
			}
		}
	}
	
	#for the character creation GUI
	if = {
		limit = {
			NOT = { root = { has_variable = ansf_family_character_generate } }
		}
		if = {
			limit = {
				root = { is_ai = no }
			}
			root = {
				ansf_remove_all_gui_variables = yes
				set_variable = { name = ansf_family_character value = scope:ansf_family_member }
				set_variable = ansf_family_character_randomize_age
				set_variable = { name = ansf_family_character_age value = scope:ansf_family_member.age }
				set_variable = ansf_family_character_education_random
				set_variable = ansf_family_character_can_use_parent_dna
				set_variable = ansf_family_character_cannot_be_clone
				set_variable = { name = ansf_family_character_age_min value = age_player_child_min }
				set_variable = { name = ansf_family_character_age_max value = age_player_child_max }
			}
		}
	}
}

ansf.2300 = { #Options for creating a new child
	type = character_event
	window = ansf_event_window
	title = ansf.2300.t
	desc = ansf.2300.desc
	theme = family
	
	center_portrait = {
		character = root
		animation = happiness
	}
	override_background = { reference = bedchamber }
	
	option = { #Legitimate son
		trigger = {
			is_married = yes
			primary_spouse = {
				sex_opposite_of = root
				trigger_if = {
					limit = { is_male = yes }
					age >= define:NChildbirth|MIN_MALE_REPRODUCTION_AGE
				}
				trigger_else = {
					age >= define:NChildbirth|MIN_FEMALE_REPRODUCTION_AGE
				}
			}
		}
		name = ansf.child.son.yes
		custom_tooltip = ansf.add.son.tooltip
		
		scope:new_dummy_male = { save_scope_as = new_child }
		if = {
			limit = { is_female = yes }
			save_scope_as = ansf_mother
			primary_spouse = { save_scope_as = ansf_father }
		}
		else = {
			save_scope_as = ansf_father
			primary_spouse = { save_scope_as = ansf_mother }
		}
		if = {
			limit = {
				is_female = yes
				matrilinear_marriage = yes
			}
			save_scope_as = ansf_dominant_parent
		}
		else_if = {
			limit = {
				is_male = yes
				patrilinear_marriage = yes
			}
			save_scope_as = ansf_dominant_parent
		}
		else = {
			primary_spouse = { save_scope_as = ansf_dominant_parent }
		}

		trigger_event = ansf.2305
	}
	option = { #Legitimate daughter
		trigger = {
			is_married = yes
			primary_spouse = {
				sex_opposite_of = root
				trigger_if = {
					limit = { is_male = yes }
					age >= define:NChildbirth|MIN_MALE_REPRODUCTION_AGE
				}
				trigger_else = {
					age >= define:NChildbirth|MIN_FEMALE_REPRODUCTION_AGE
				}
			}
		}
		name = ansf.child.daughter.yes
		custom_tooltip = ansf.add.daughter.tooltip
		
		scope:new_dummy_female = { save_scope_as = new_child }
		if = {
			limit = { is_female = yes }
			save_scope_as = ansf_mother
			primary_spouse = { save_scope_as = ansf_father }
		}
		else = {
			save_scope_as = ansf_father
			primary_spouse = { save_scope_as = ansf_mother }
		}
		if = {
			limit = {
				is_female = yes
				matrilinear_marriage = yes
			}
			save_scope_as = ansf_dominant_parent
		}
		else_if = {
			limit = {
				is_male = yes
				patrilinear_marriage = yes
			}
			save_scope_as = ansf_dominant_parent
		}
		else = {
			primary_spouse = { save_scope_as = ansf_dominant_parent }
		}
		trigger_event = ansf.2305
	}
	option = { #Son by a different parent
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { is_married = yes }
						desc = ansf.child.son.bastard.yes
					}
					desc = ansf.child.son.bastard.single.yes
				}
			}
		}
		custom_tooltip = ansf.add.son.bastard.tooltip
		
		scope:new_dummy_male = { save_scope_as = new_child }
		if = {
			limit = { is_male = yes }
			scope:new_dummy_female = { save_scope_as = source_otherparent }
		}
		else = {
			scope:new_dummy_male = { save_scope_as = source_otherparent }
		}
		trigger_event = ansf.2301
	}
	option = { #Daughter by a different parent
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { is_married = yes }
						desc = ansf.child.daughter.bastard.yes
					}
					desc = ansf.child.daughter.bastard.single.yes
				}
			}
		}
		custom_tooltip = ansf.add.daughter.bastard.tooltip
		
		scope:new_dummy_female = { save_scope_as = new_child }
		if = {
			limit = { is_male = yes }
			scope:new_dummy_female = { save_scope_as = source_otherparent }
		}
		else = {
			scope:new_dummy_male = { save_scope_as = source_otherparent }
		}
		trigger_event = ansf.2301
	}
	option = { #Never mind
		name = ansf.child.no
		custom_tooltip = ansf.child.no.tooltip
		
		trigger_event = ansf.1004
	}
}

ansf.2301 = { #Nature of your relationship with the other parent?
	type = character_event
	window = ansf_event_window
	title = ansf.2301.t
	desc = ansf.2301.desc
	theme = family
	
	center_portrait = scope:target
	override_background = { reference = bedchamber }
	
	widgets = {
		widget = {
			gui = "event_window_widget_select_parent"
			container = "custom_widgets_container"
		}
	}
	
	immediate = {
		hidden_effect = {
			# create the list for the gui
			clear_variable_list = potential_parents
			every_former_spouse = {
				even_if_dead = yes
				limit = { sex_opposite_of = root }
				add_to_potential_parents_list = yes
			}
			every_consort = {
				limit = { sex_opposite_of = root }
				add_to_potential_parents_list = yes
			}
			every_relation = {
				type = lover
				limit = { sex_opposite_of = root }
				add_to_potential_parents_list = yes
			}
			every_child = {
				even_if_dead = yes
				limit = {
					trigger_if = {
						limit = {
							root = { is_male = yes }
						}
						exists = mother
						NOT = { mother = root.primary_spouse }
					}
					trigger_else = {
						exists = father
						NOT = { father = root.primary_spouse }
					}
				}
				if = {
					limit = {
						root = { is_male = yes }
					}
					mother = { add_to_potential_parents_list = yes }
				}
				if = {
					limit = {
						root = { is_female = yes }
					}
					father = { add_to_potential_parents_list = yes }
				}
			}
			if = {
				limit = {
					exists = root.primary_spouse
					is_target_in_variable_list = {
						name = potential_parents
						target = root.primary_spouse
					}
				}
				remove_list_variable = {
					name = potential_parents
					target = root.primary_spouse
				}
			}
		}
	}
	
	option = { # a former spouse
		trigger = {
			custom_description = {
				text = ansf_already_has_ex_spouse
				subject = root
				NOT = {
					any_former_spouse = {
						even_if_dead = yes
						NOT = { is_spouse_of = root }
						count > 0
					}
				}
			}
		}
		name = ansf.your.exspouse
		show_as_unavailable = {
			any_former_spouse = {
				even_if_dead = yes
				NOT = { is_spouse_of = root }
				count > 0
			}
		}
		
		custom_tooltip = { text = ansf.your.exspouse.tt }
		if = {
			limit = { is_married = yes }
			custom_tooltip = { text = ansf.your.exspouse.tt.married.warning }
		}
		else = {
			custom_tooltip = { text = ansf.your.exspouse.tt.single.warning }
		}
		set_variable = { name = is_former_spouse value = yes }
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		save_scope_as = ansf_bastard_parent
		trigger_event = ansf.2302
		
		ai_chance = {
			base = 0
		}
	}
	option = { # a concubine
		trigger = {
			is_male = yes
			has_dynasty = yes
			faith = { has_doctrine = doctrine_concubines }
			OR = {
				is_married = no
				matrilinear_marriage = no
			}
		}
		name = ansf.your.concubine
		
		custom_tooltip = { text = ansf.your.concubine.tt }
		set_variable = { name = is_concubine value = yes }
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		save_scope_as = ansf_bastard_parent
		trigger_event = ansf.2302
		
		ai_chance = {
			base = 0
		}
	}
	option = { # a forbidden lover
		name = ansf.your.lover
		
		custom_tooltip = { text = ansf.your.lover.tt }
		custom_tooltip = { text = ansf.your.lover.tt.warning }
		set_variable = { name = is_lover_bastard value = yes }
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		save_scope_as = ansf_bastard_parent
		trigger_event = ansf.2302
		
		ai_chance = {
			base = 100
		}
	}
	option = { # select an already-existing parent
		trigger = {
			NOT = { exists = global_var:ansf_show_possible_parents }
			has_variable_list = potential_parents
			variable_list_size = { name = potential_parents value > 0 }
		}
		name = ansf.your.findexisting
		
		custom_tooltip = { text = ansf.your.findexisting.tt }
		
		# restart the event and prompt the widget to appear
		set_global_variable = { name = ansf_show_possible_parents value = yes }
		set_variable = { name = ansf_bastard_parent value = root }
		trigger_event = ansf.2301
		
		ai_chance = {
			base = 0
		}
	}
	option = { # use a selected existing parent
		trigger = {
			exists = var:existing_parent
		}
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							any_child = {
								even_if_dead = yes
								is_child_of = root.var:existing_parent
							}
						}
						desc = ansf.your.existing
					}
					desc = ansf.your.existing.first
				}
			}
		}
		custom_tooltip = { text = ansf.your.existing.tt }
		save_scope_as = ansf_bastard_parent
		var:existing_parent = { save_scope_as = ansf_bastard_otherparent }
		set_variable = { name = do_not_delete value = scope:ansf_bastard_otherparent }

		if = {
			limit = {
				ansf_was_married_to = { SPOUSE = scope:ansf_bastard_otherparent }
			}
			set_variable = { name = is_former_spouse value = yes }
		}
		else_if = {
			limit = {
				scope:ansf_bastard_otherparent = {
					OR = {
						is_concubine_of = root
						any_child = {
							even_if_dead = yes
							has_trait = child_of_concubine_female
						}
					}
				}
			}
			set_variable = { name = is_concubine value = yes }
		}
		else = {
			set_variable = { name = is_lover_bastard value = yes }
		}
		
		set_child_parental_flags = yes
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		set_variable = { name = ansf_bastard_parent value = root }
		trigger_event = ansf.2305
		
		ai_chance = {
			base = 0
		}
	}
	option = { # go back
		name = ansf.halfsibling.nevermind
		custom_tooltip = { text = ansf.your.nevermind.tooltip }
		clear_saved_scope = new_child
		clear_saved_scope = source_otherparent
		remove_global_variable ?= ansf_show_possible_parents
		remove_variable ?= existing_parent
		remove_variable ?= ansf_bastard_parent
		trigger_event = ansf.1004
		
		ai_chance = {
			base = 0
		}
	}	
}

ansf.2302 = { #The other parent of your child?
	type = character_event
	window = ansf_event_window
	title = ansf.2301.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = var:is_concubine }
				desc = ansf.your.parent.d.concubine
			}
			triggered_desc = {
				trigger = { exists = var:is_former_spouse }
				desc = ansf.your.parent.d.former.spouse
			}
			desc = ansf.your.parent.d.lover
		}
	}
	theme = family
	
	right_portrait = {
		character = root
		triggered_animation = {
			trigger = { exists = root.var:is_lover_bastard }
			animation = shame
		}
		animation = interested
	}
	center_portrait = {
		character = scope:ansf_bastard_otherparent
		animation = love
	}
	override_background = { reference = bedchamber }
	
	immediate = {
		hidden_effect = {
			clear_variable_list = potential_grandparents
			clear_variable_list = potential_piblings
			clear_variable_list = potential_relatives
			
			# we need the eldest legitimate child of the main parent
			find_bastard_parent_eldest_legitimate_child = { PARENT = root }
			
			# use the parent's template unless one has already been set
			if = {
				limit = { has_character_flag = no_creation_override }
				remove_character_flag = no_creation_override
				check_for_possible_relatives = { COMPARE = scope:ansf_bastard_otherparent }
			}
			else = {
				# for characters that are a clone of the target, set the culture/faith variable to match
				if = {
					limit = {
						has_variable = ansf_family_character_is_clone
						has_variable = ansf_family_character_generate
					}
					set_variable = {
						name = culture_faith_template
						value = scope:target
					}
				}
				# otherwise, we use a random one if culture/faith hasn't been set
				else_if = {
					limit = {
						NOT = { has_variable = culture_faith_template }
					}
					find_random_template_ruler = yes
				}
				# now create the character
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_diplomat_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								post_creation_otherparent_effect = yes
							}
						}
					}
					40 = {
						modifier = {
							add = -35
							NOT = { ansf_can_be_combatant_based_on_gender_trigger = { GENDER_CHARACTER = scope:source_otherparent } }
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_martial_template
							prowess = { 10 15 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_stewardship_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_intrigue_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = root.var:culture_faith_template.culture
							faith = root.var:culture_faith_template.faith
							gender = scope:source_otherparent
							age = {
								integer_range = {
									min = age_otherparent_min
									max = age_otherparent_max
								}
							}
							template = olympia_learning_template
							prowess = { 2 10 }
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_learning
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
		
			if = {
				limit = { exists = scope:eldest_legitimate_child }
				clear_saved_scope = eldest_legitimate_child
			}
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_bastard_otherparent = { save_scope_as = name_character_target } }
		}
	}
	
	# Yes, but they're dead
	option = {
		name = ansf.halfsibling.parent.yes.dead
		custom_tooltip = { text =  ansf.your.parent.dead.tt }
		add_character_flag = otherparent_will_be_dead
		
		set_variable = { name = is_player_otherparent_creation value = yes }
		ansf_remove_all_gui_variables = yes
		set_child_parental_flags = yes
		trigger_event = ansf.2001
	}
	# Yes, and we're lovers still
	option = {
		trigger = {
			exists = var:is_lover_bastard
			custom_description = {
				text = ansf_already_has_lover
				subject = root
				num_of_relation_lover < 1
			}
		}
		name = ansf.your.parent.yes.lover.yes
		show_as_unavailable = {
			exists = var:is_lover_bastard
			num_of_relation_lover > 0
		}
		custom_tooltip = { text = ansf.your.parent.lover.remain.tt }
		add_character_flag = otherparent_will_remain_lover
		
		set_variable = { name = is_player_otherparent_creation value = yes }
		ansf_remove_all_gui_variables = yes
		set_child_parental_flags = yes
		trigger_event = ansf.2001
	}
	# Yes, and they're my courtier
	option = {
		name = {
			trigger = { exists = var:is_lover_bastard }
			text = ansf.your.parent.yes.lover.no
		}
		name = {
			trigger = {
				NOT = { exists = var:is_lover_bastard }
			}
			text = ansf.your.parent.yes.remain
		}
		custom_tooltip = { text = ansf.your.parent.remain.tt }
		add_character_flag = otherparent_will_be_courtier
		
		set_variable = { name = is_player_otherparent_creation value = yes }
		ansf_remove_all_gui_variables = yes
		set_child_parental_flags = yes
		trigger_event = ansf.2001
	}
	# Connect to dynasty
	option = {
		trigger = {
			custom_description = {
				text = no_dynasty_options
				subject = scope:ansf_bastard_otherparent
				has_variable_list = potential_relatives
				variable_list_size = { name = potential_relatives value > 0 }
			}
		}
		name = ansf.halfsibling.parent.connect
		show_as_unavailable = {
			OR = {
				NOT = { has_variable_list = potential_relatives }
				variable_list_size = { name = potential_relatives value < 1 }
			}
		}

		remove_variable ?= show_ansf_regenerate_window
		trigger_event = ansf.2303
		
		custom_tooltip = { text =  ansf.your.existing.other.parent.dynasty.tt }
	}
	# Regenerate Character
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.halfsibling.parent.regenerate
					}
					desc = ansf.halfsibling.parent.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = { text =  ansf.reroll.paramaters }
			add_internal_flag = special
		}
		else = {
			custom_tooltip = { text =  ansf.spouse.redo.father.tt }
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_bastard_otherparent }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		trigger_event = ansf.2302
	}
	# Go back
	option = { # go back
		name = ansf.halfsibling.nevermind
		custom_tooltip = { text = ansf.no.me.existing.tt }
		remove_variable = is_former_spouse
		remove_variable = is_concubine
		remove_variable = is_lover_bastard
		clear_saved_scope = ansf_bastard_parent
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		ansf_remove_all_gui_variables = yes
		trigger_event = ansf.2301
	}
}

ansf.2303 = { #Other parent from an existing dynasty
	type = character_event
	window = ansf_dynasty_event_window
	title = ansf.2303.t
	desc = ansf.your.parent.connect.d
	theme = family

	center_portrait = {
		character = root
		animation = thinking
	}
	override_background = { reference = bedchamber }

	immediate = {
		set_variable = { name = ansf_family_character value = scope:ansf_family_member }
	}

	# Selection is their father
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_grandparents
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.grandparent
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = { text = ansf.yes.existing1.other.grandparent.tt }
		root.var:potential_relative_1 = {
			if = {
				limit = { is_male = yes }
				root = {
					set_variable = {
						name = actual_grandfather
						value = root.var:potential_relative_1
					}
				}
			}
			else = {
				root = {
					set_variable = {
						name = actual_grandmother
						value = root.var:potential_relative_1
					}
				}
			}
			if = {
				limit = {
					should_be_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = { text =  ansf.yes.existing.other.bastard.tt }
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					any_consort = {
						even_if_dead = yes
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
				}
				random_consort = {
					even_if_dead = yes
					limit = {
						could_have_been_parent_of = { CHILD = scope:ansf_family_member }
					}
					if = {
						limit = { is_female = yes }
						root = {
							set_variable = {
								name = actual_grandmother
								value = prev
							}
						}
					}
					else = {
						root = {
							set_variable = {
								name = actual_grandfather
								value = prev
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		remove_variable = potential_relative_1
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2304
	}
	
	# Selection is their brother
	option = {
		trigger = {
			has_variable = potential_relative_1
			is_target_in_variable_list = {
				name = potential_piblings
				target = root.var:potential_relative_1
			}
		}
		name = ansf.yes.existing1.pibling
		
		scope:ansf_family_member = { save_scope_as = source_parent }
		root.var:potential_relative_1 = { save_scope_as = actual_relative }
		
		custom_tooltip = { text = ansf.yes.existing1.other.pibling.tt }
		root.var:potential_relative_1 = {
			root = {
				set_variable = {
					name = actual_grandfather
					value = root.var:potential_relative_1.father
				}
			}
			if = {
				limit = {
					should_be_fathers_bastard_trigger = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = { text = ansf.yes.existing.other.bastard.tt }
				root = {
					set_variable = {
						name = will_be_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					should_be_legitimate_bastard = { RELATIVE = scope:ansf_family_member }
				}
				custom_tooltip = { text = ansf.yes.existing.other.legit.bastard.tt }
				root = {
					set_variable = {
						name = will_be_legitimate_bastard
						value = flag:yes
					}
				}
			}
			else_if = {
				limit = {
					father = {
						any_consort = {
							even_if_dead = yes
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
					}
				}
				father = {
					random_consort = {
						even_if_dead = yes
						limit = {
							could_have_been_parent_of = { CHILD = scope:ansf_family_member }
						}
						root = {
							set_variable = {
								name = actual_grandmother
								value = prev
							}
						}
					}
				}
			}
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		remove_variable = potential_relative_1
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2304
	}
	
	# Go back
	option = {
		name = ansf.no.inlaw.existing
		add_character_flag = no_creation_override
		
		remove_variable = potential_relative_1
		remove_variable = ansf_family_character
		
		custom_tooltip = { text = ansf.no.me.existing.tt }
		
		trigger_event = ansf.2302
	}
}

ansf.2304 = { #Creating the other parent with connection to the existing dynasty
	type = character_event
	window = ansf_event_window
	title = ansf.2301.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						root.var:actual_grandfather ?= scope:actual_relative
						root.var:actual_grandmother ?= scope:actual_relative
					}
				}
				desc = ansf.your.parent.grandparent.d
			}
			desc = ansf.your.parent.pibling.d
		}
	}
	
	theme = family
	center_portrait = scope:ansf_family_member	
	override_background = { reference = bedchamber }

	immediate = {
		hidden_effect = {
			add_character_flag = otherparent_existing_dynasty
			if = {
				limit = {
					has_variable = actual_grandfather
					has_variable = actual_grandmother
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					40 = {
						modifier = {
							add = -35
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
			else_if = {
				limit = {
					has_variable = actual_grandfather
				}
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							father = root.var:actual_grandfather
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
			else = {
				random_list = {
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Diplomat
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_diplomat_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_diplomacy
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							add = -15
							scope:source_parent = {
								NOT = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actual_relative } }
							}
						}
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
							}
						}
						create_character = { #Martial
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_martial_template
							prowess = { 10 15 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_martial
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Steward
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_stewardship_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_stewardship
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_learning
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Intrigue
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_intrigue_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_intrigue
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
					20 = {
						modifier = {
							factor = 0
							has_variable = ansf_family_character_generate
							OR = {
								has_variable = ansf_family_character_education_intrigue
								has_variable = ansf_family_character_education_stewardship
								has_variable = ansf_family_character_education_diplomacy
								has_variable = ansf_family_character_education_martial
							}
						}
						create_character = { #Learning
							location = scope:target.location
							culture = scope:actual_relative.culture
							faith = scope:actual_relative.faith
							dynasty = none
							mother = root.var:actual_grandmother
							gender = scope:source_parent
							age = age_ansf_family_member_exact
							template = olympia_learning_template
							prowess = { 2 10 }
							random_traits = yes
							save_scope_as = ansf_bastard_otherparent
							after_creation = {
								add_character_flag = was_learning
								set_house = scope:actual_relative.house
								post_creation_otherparent_effect = yes
							}
						}
					}
				}
			}
			remove_character_flag = otherparent_existing_dynasty
		}
	}
	
	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	# Yes, but they're dead
	option = {
		name = ansf.halfsibling.parent.yes.dead
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text = ansf.your.parent.dead.tt }
		add_character_flag = otherparent_will_be_dead
		
		set_child_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_player_otherparent_creation value = yes }
		trigger_event = ansf.2001
	}
	# Yes, and we're lovers still
	option = {
		trigger = {
			exists = var:is_lover_bastard
			custom_description = {
				text = ansf_already_has_lover
				subject = root
				num_of_relation_lover < 1
			}
		}
		name = ansf.your.parent.yes.lover.yes
		show_as_unavailable = {
			exists = var:is_lover_bastard
			num_of_relation_lover > 0
		}
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text = ansf.your.parent.lover.remain.tt }
		add_character_flag = otherparent_will_remain_lover
		
		set_child_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_player_otherparent_creation value = yes }
		trigger_event = ansf.2001
	}
	# Yes, and they're my courtier
	option = {
		trigger = {
			NOT = { exists = var:is_former_spouse }
		}
		name = {
			trigger = { exists = var:is_lover_bastard }
			text = ansf.your.parent.yes.lover.no
		}
		name = {
			trigger = {
				NOT = { exists = var:is_lover_bastard }
			}
			text = ansf.your.parent.yes.remain
		}
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text = ansf.your.parent.remain.tt }
		add_character_flag = otherparent_will_be_courtier
		
		set_child_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_player_otherparent_creation value = yes }
		trigger_event = ansf.2001
	}
	# Yes, but they're at their previous court
	option = {
		trigger = { exists = var:is_former_spouse }
		name = ansf.your.parent.yes.relative
		
		connect_to_relative_effect = yes
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		custom_tooltip = { text = ansf.your.parent.relative.tt }
		add_character_flag = otherparent_will_be_courtier
		
		set_child_parental_flags = yes
		ansf_remove_all_gui_variables = yes
		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		
		set_variable = { name = is_player_otherparent_creation value = yes }
		trigger_event = ansf.2001
	}
	# Redo
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.no.existing.reroll
				}
			}
		}

		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = { text = ansf.reroll.paramaters }
			add_internal_flag = special
		}
		else = {
			custom_tooltip = { text = ansf.reroll.inlaw.existing }
		}

		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
	
		trigger_event = ansf.2304
	}
	# Never mind
	option = {
		name = ansf.no.existing.leave
		custom_tooltip = ansf.no.existing.leave.tt
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }

		remove_variable ?= potential_relative_1
		remove_variable ?= will_be_bastard
		remove_variable ?= will_be_legitimate_bastard
		remove_variable ?= actual_grandmother
		remove_variable ?= actual_grandfather
		clear_saved_scope ?= source_parent
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2302
	}
}

ansf.2305 = { #Is this your child?
	type = character_event
	window = ansf_event_window
	title = ansf.legitimate.child.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:ansf_family_member = { age < 3 }
				}
				desc = ansf.child.infant.desc
			}
			triggered_desc = {
				trigger = {
					scope:ansf_family_member = { age > 2 }
					scope:ansf_family_member = { age < 10 }
				}
				desc = ansf.child.smallchild.desc
			}
			triggered_desc = {
				trigger = {
					scope:ansf_family_member = { is_adult = no }
				}
				desc = ansf.child.teen.desc
			}
			desc = ansf.child.adult.desc
		}
	}
	theme = family
	
	center_portrait = scope:ansf_family_member
	lower_left_portrait = scope:ansf_mother
	lower_right_portrait = scope:ansf_father
	override_background = {
		trigger = { scope:ansf_family_member = { age < 10 } }
		reference = bedchamber
	}
	override_background = {
		trigger = { scope:ansf_family_member = { age > 9 } }
		reference = courtyard
	}
	
	immediate = {
		#find ages of eldest children
		find_bastard_parent_eldest_legitimate_child = { PARENT = root }
		if = {
			limit = { is_married = yes }
			find_eldest_legitimate_halfsibling = { MOTHER = root.primary_spouse FATHER = root }
		}
		else = {
			dummy_male = { save_scope_as = mother_father_eldest_child }
		}
		
		hidden_effect = {
			random_list = {
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Diplomat
						location = root.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:new_child
						dynasty_house = scope:ansf_dominant_parent.house
						age = {
							integer_range = {
								min = age_player_child_min
								max = age_player_child_max
							}
						}
						mother = scope:ansf_mother
						father = scope:ansf_father
						template = olympia_diplomat_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_diplomacy
							post_creation_child_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
						}
					}
					create_character = { #Martial
						location = root.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:new_child
						dynasty_house = scope:ansf_dominant_parent.house
						age = {
							integer_range = {
								min = age_player_child_min
								max = age_player_child_max
							}
						}
						mother = scope:ansf_mother
						father = scope:ansf_father
						template = olympia_martial_template
						prowess = { 10 15 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_martial
							post_creation_child_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Steward
						location = root.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:new_child
						dynasty_house = scope:ansf_dominant_parent.house
						age = {
							integer_range = {
								min = age_player_child_min
								max = age_player_child_max
							}
						}
						mother = scope:ansf_mother
						father = scope:ansf_father					
						template = olympia_stewardship_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_stewardship
							post_creation_child_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_learning
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Intrigue
						location = root.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:new_child
						dynasty_house = scope:ansf_dominant_parent.house
						age = {
							integer_range = {
								min = age_player_child_min
								max = age_player_child_max
							}
						}
						mother = scope:ansf_mother
						father = scope:ansf_father						
						template = olympia_intrigue_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_intrigue
							post_creation_child_effect = yes
						}
					}
				}
				20 = {
					modifier = {
						factor = 0
						has_variable = ansf_family_character_generate
						OR = {
							has_variable = ansf_family_character_education_intrigue
							has_variable = ansf_family_character_education_stewardship
							has_variable = ansf_family_character_education_diplomacy
							has_variable = ansf_family_character_education_martial
						}
					}
					create_character = { #Learning
						location = root.location
						culture = scope:ansf_dominant_parent.culture
						faith = scope:ansf_dominant_parent.faith
						gender = scope:new_child
						dynasty_house = scope:ansf_dominant_parent.house
						age = {
							integer_range = {
								min = age_player_child_min
								max = age_player_child_max
							}
						}
						mother = scope:ansf_mother
						father = scope:ansf_father						
						template = olympia_learning_template
						prowess = { 2 10 }
						random_traits = yes
						save_scope_as = ansf_family_member
						after_creation = {
							add_character_flag = was_learning
							post_creation_child_effect = yes
						}
					}
				}
			}
		}
	}

	widgets = {
		widget = {
			gui = "event_window_widget_name_family"
			container = "dynamic_birth_name"
			controller = name_character
			setup_scope = { scope:ansf_family_member = { save_scope_as = name_character_target } }
		}
	}

	#This is my child
	option = {
		name = ansf.legimate.yes.proceed
		trigger = {
			custom_description = {
				text = ansf_cannot_be_twin
				subject = scope:ansf_family_member
				NOT = {
					scope:ansf_family_member = {
						any_sibling = {
							even_if_dead = yes
							has_trait = twin
							mother = scope:ansf_family_member.mother
							father = scope:ansf_family_member.father
							age_including_death_age = scope:ansf_family_member.age
						}
					}
				}
			}
		}
		
		show_as_unavailable = {
			scope:ansf_family_member = {
				any_sibling = {
					even_if_dead = yes
					has_trait = twin
					mother = scope:ansf_family_member.mother
					father = scope:ansf_family_member.father
					age_including_death_age = scope:ansf_family_member.age
				}
			}
		}
		
		#give any full sibling the twin trait
		check_twin_effect = { CHARACTER = scope:ansf_family_member }
		
		#add the child to your court
		if = {
			limit = {
				scope:ansf_family_member = {
					NOT = { is_courtier_of = root }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = root NEW_COURTIER = scope:ansf_family_member }
		}
		
		# make the otherparent a former spouse, if needed
		if = {
			limit = {
				exists = var:is_former_spouse
				NOT = { ansf_was_married_to = { SPOUSE = scope:ansf_bastard_otherparent } }
			}
			ansf_make_former_spouse = { PARENT = root FORMER = scope:ansf_bastard_otherparent }
		}
		# make the otherparent a concubine, if needed
		if = {
			limit = {
				exists = var:is_concubine
				has_character_flag = otherparent_will_be_courtier
				scope:ansf_bastard_otherparent = {
					NOT = { is_concubine_of = root }
				}
			}
			make_concubine = scope:ansf_bastard_otherparent
		}
		# make the otherparent a public lover, if needed
		if = {
			limit = {
				has_character_flag = otherparent_will_remain_lover
				NOT = { has_relation_lover = scope:ansf_bastard_otherparent }
			}
			set_random_lover_reason = { TARGET = scope:ansf_bastard_otherparent }
			random_secret = {
				type = secret_lover
				limit = { secret_target = scope:ansf_bastard_otherparent }
				hidden_effect = { expose_secret = root }
			}
		}
		# make the otherparent a courtier
		if = {
			limit = {
				exists = var:is_former_spouse
				exists = scope:actual_relative
				has_character_flag = otherparent_will_be_courtier
				scope:ansf_bastard_otherparent = {
					is_close_or_extended_family_of = scope:actual_relative
					NOT = { is_courtier_of = scope:actual_relative }
				}
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:actual_relative NEW_COURTIER = scope:ansf_bastard_otherparent }
		}
		else_if = {
			limit = {
				OR = {
					has_character_flag = otherparent_will_be_courtier
					has_character_flag = otherparent_will_remain_lover
				}
				scope:ansf_bastard_otherparent = { is_courtier = no }
			}
			recruit_to_court_effect = { NEW_LIEGE = scope:target_liege NEW_COURTIER = scope:ansf_bastard_otherparent }
		}
		# give both parents adulterer/fornicator traits, if needed
		if = {
			limit = {
				OR = {
					exists = var:is_lover_bastard
					exists = var:otherparent_will_remain_lover
				}
			}
			if = {
				limit = { ansf_is_adulterer = no }
				add_adulterer_fornicator_trait_or_nothing_effect = yes
				if = {
					limit = { exists = liege }
					hidden_effect = {
						consume_all_criminal_reasons_effect = {
							LIEGE = root.liege
							CRIMINAL = root
						}
					}
				}
			}
			if = {
				limit = {
					NOR = {
						exists = var:otherparent_will_remain_lover
						has_relation_old_flame = scope:ansf_bastard_otherparent
					}
				}
				hidden_effect = { set_relation_old_flame = scope:ansf_bastard_otherparent }
			}
			scope:ansf_bastard_otherparent = {
				if = {
					limit = { ansf_is_adulterer = no }
					add_adulterer_fornicator_trait_or_nothing_effect = yes
					if = {
						limit = { exists = liege }
						hidden_effect = {
							consume_all_criminal_reasons_effect = {
								LIEGE = scope:ansf_bastard_otherparent.liege
								CRIMINAL = scope:ansf_bastard_otherparent
							}
						}
					}
				}
				if = {
					limit = {
						NOR = {
							exists = root.var:otherparent_will_remain_lover
							has_relation_old_flame = root
						}
					}
					hidden_effect = { set_relation_old_flame = root }
				}
			}
		}
		# kill the otherparent, if needed
		if = {
			limit = {
				has_character_flag = otherparent_will_be_dead
				scope:ansf_bastard_otherparent = { is_alive = yes }
			}
			scope:ansf_bastard_otherparent = {
				death = { death_reason = death_vanished }
			}
		}
		# reveal a secret lover, if this is a previously existing character
		if = {
			limit = {
				NOR = {
					has_character_flag = otherparent_will_be_dead
					has_character_flag = otherparent_will_remain_lover
					has_character_flag = otherparent_will_be_courtier
				}
				any_secret = {
					type = secret_lover
					secret_target = scope:ansf_bastard_otherparent
				}
			}
			random_secret = {
				type = secret_lover
				limit = { secret_target = scope:ansf_bastard_otherparent }
				expose_secret = root
			}
		}
		
		
		remove_variable = is_former_spouse
		remove_variable = is_concubine
		remove_variable = is_lover_bastard
		remove_character_flag = otherparent_will_be_dead
		remove_character_flag = otherparent_will_remain_lover
		remove_character_flag = otherparent_will_be_courtier
		clear_saved_scope = new_child
		clear_saved_scope = ansf_mother
		clear_saved_scope = ansf_father
		clear_saved_scope = ansf_bastard_parent
		clear_saved_scope = ansf_dominant_parent
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.2001
	}
	
	#No, that's not them
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = ansf_family_character_generate }
						desc = ansf.no.existing.regenerate
					}
					desc = ansf.halfsibling.event3.redo
				}
			}
		}
		
		if = {
			limit = { has_variable = ansf_family_character_generate }
			custom_tooltip = ansf.reroll.paramaters
			add_internal_flag = special
		}
		else = {
			custom_tooltip = ansf.child.redo.tooltip
		}
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { has_variable = ansf_family_character_generate }
			remove_variable ?= show_ansf_regenerate_window
		}
		else = {
			ansf_remove_all_gui_variables = yes
		}
		
		trigger_event = ansf.2305
	}
	
	#Oops, go back
	option = {
		name = ansf.child.no
		custom_tooltip = ansf.child.no.tooltip
		
		ansf_disappear_effect = { KILLED = scope:ansf_family_member }
		if = {
			limit = { exists = scope:ansf_bastard_otherparent }
			ansf_disappear_effect = { KILLED = scope:ansf_bastard_otherparent }
		}
		
		remove_variable = is_former_spouse
		remove_variable = is_concubine
		remove_variable = is_lover_bastard
		remove_character_flag = otherparent_will_be_dead
		remove_character_flag = otherparent_will_remain_lover
		remove_character_flag = otherparent_will_be_courtier
		clear_saved_scope = new_child
		clear_saved_scope = ansf_mother
		clear_saved_scope = ansf_father
		clear_saved_scope = ansf_bastard_parent
		clear_saved_scope = ansf_dominant_parent
		ansf_remove_all_gui_variables = yes
		
		trigger_event = ansf.1004
	}
}

