﻿
############
# PATRILINES
############
## Only executes if real father of NEWBORN has trait
# Added remove and checks for paternal_lineage, just in case selected trait category or even 100% inheritance doesn't work as expected...
on_birth_child_patrilines_effect = {
	if = {
		limit = {
			is_female = no
		}
		# First remove trait. Supposedly not needed - should never appear spontaneously, should be inherited 100% from real father, and never remain hidden after removal.
		# Don't trust hardcoded inheritance rules anyway. Now if the trait category or behaviour changes, it should still work as expected.
		remove_trait_rank_effect = { GROUP = patrilineages }

		save_father_or_real_father_or_player_effect = yes

		if = {
			limit = {
				exists = scope:line_father
			}
			# Break loop before it starts if unnecessary (Change Log 3)
			if = {
				limit = {
					scope:line_father = {
						OR = {
							has_trait_rank = {
								trait = patrilinefounder
								rank > 0
							} 
							has_trait_rank = {
								trait = patrilineages
								rank > 0
							}
						}
					}
				}
				if = {
					limit = { father_has_patriline_trait = { RANK = 1 } }
					set_trait_rank = { trait = patrilineages rank = 1 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 2 } }
					set_trait_rank = { trait = patrilineages rank = 2 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 3 } }
					set_trait_rank = { trait = patrilineages rank = 3 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 4 } }
					set_trait_rank = { trait = patrilineages rank = 4 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 5 } }
					set_trait_rank = { trait = patrilineages rank = 5 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 6 } }
					set_trait_rank = { trait = patrilineages rank = 6 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 7 } }
					set_trait_rank = { trait = patrilineages rank = 7 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 8 } }
					set_trait_rank = { trait = patrilineages rank = 8 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 9 } }
					set_trait_rank = { trait = patrilineages rank = 9 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 10 } }
					set_trait_rank = { trait = patrilineages rank = 10 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 11 } }
					set_trait_rank = { trait = patrilineages rank = 11 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 12 } }
					set_trait_rank = { trait = patrilineages rank = 12 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 13 } }
					set_trait_rank = { trait = patrilineages rank = 13 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 14 } }
					set_trait_rank = { trait = patrilineages rank = 14 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 15 } }
					set_trait_rank = { trait = patrilineages rank = 15 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 16 } }
					set_trait_rank = { trait = patrilineages rank = 16 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 17 } }
					set_trait_rank = { trait = patrilineages rank = 17 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 18 } }
					set_trait_rank = { trait = patrilineages rank = 18 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 19 } }
					set_trait_rank = { trait = patrilineages rank = 19 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 20 } }
					set_trait_rank = { trait = patrilineages rank = 20 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 21 } }
					set_trait_rank = { trait = patrilineages rank = 21 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 22 } }
					set_trait_rank = { trait = patrilineages rank = 22 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 23 } }
					set_trait_rank = { trait = patrilineages rank = 23 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 24 } }
					set_trait_rank = { trait = patrilineages rank = 24 }
				}
				else_if = {
					limit = { father_has_patriline_trait = { RANK = 25 } }
					set_trait_rank = { trait = patrilineages rank = 25 }
				}
				else = {
					set_trait_rank = { trait = patrilineages rank = 26 }
				}
			}
		}
	}
}

############
# MATRILINES
############
# Executes if mother of NEWBORN is founder.
# Added remove and checks for maternal_lineage, just in case selected trait category or even 100% inheritance doesn't work as expected...
on_birth_child_matrilines_effect = {
	# First remove trait.
	remove_trait_rank_effect = { GROUP = matrilineages }
	if = {
		limit = {
			exists = mother
		}
		# Break loop before it starts if unnecessary (Change Log 3)
		if = {
			limit = {
				mother = {
					OR = {
						has_trait_rank = {
							trait = matrilinefounder
							rank > 0
						}
						has_trait_rank = {
							trait = matrilineages
							rank > 0
						}
					}
				}
			}
			if = {
				limit = { mother_has_matriline_trait = { RANK = 1 } }
				set_trait_rank = { trait = matrilineages rank = 1 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 2 } }
				set_trait_rank = { trait = matrilineages rank = 2 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 3 } }
				set_trait_rank = { trait = matrilineages rank = 3 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 4 } }
				set_trait_rank = { trait = matrilineages rank = 4 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 5 } }
				set_trait_rank = { trait = matrilineages rank = 5 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 6 } }
				set_trait_rank = { trait = matrilineages rank = 6 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 7 } }
				set_trait_rank = { trait = matrilineages rank = 7 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 8 } }
				set_trait_rank = { trait = matrilineages rank = 8 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 9 } }
				set_trait_rank = { trait = matrilineages rank = 9 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 10 } }
				set_trait_rank = { trait = matrilineages rank = 10 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 11 } }
				set_trait_rank = { trait = matrilineages rank = 11 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 12 } }
				set_trait_rank = { trait = matrilineages rank = 12 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 13 } }
				set_trait_rank = { trait = matrilineages rank = 13 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 14 } }
				set_trait_rank = { trait = matrilineages rank = 14 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 15 } }
				set_trait_rank = { trait = matrilineages rank = 15 }
			}
			else_if = {
				limit = { mother_has_matriline_trait = { RANK = 16 } }
				set_trait_rank = { trait = matrilineages rank = 16 }
			}
			else = {
				set_trait_rank = { trait = matrilineages rank = 17 }
			}
		}
	}
}

############
## LINEAGES
############
# Executes if mother or father of NEWBORN are family lineage founders 
# ADDED dynasty lineage: IMPORTANT because else no precedence in inheritance is set, AI chooses randomly. It also guarantees expected behaviour.
on_birth_child_dynastylines_effect = {
	# Remove inherited trait from all, to avoid inheritance of wrong family lineage, hidden inheritance, etc.
	remove_trait_rank_effect = { GROUP = dynastylineages }

	save_father_or_real_father_or_player_effect = yes
	# Break loop before it starts if unnecessary (Change Log 3)
	if = {
		limit = {
			OR = {
				AND = {
					exists = scope:line_father
					father_has_line_trait = yes
				}
				AND = {
					exists = mother
					mother_has_line_trait = yes
				}
			}
		}
		
		# Save appropriate recipient
		save_father_or_real_father_or_player_effect = yes
		save_appropriate_dynasty_parent = yes

		if = {
			limit = { parent_has_line_trait = { RANK = 1 } }
			set_trait_rank = { trait = dynastylineages rank = 1 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 2 } }
			set_trait_rank = { trait = dynastylineages rank = 2 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 3 } }
			set_trait_rank = { trait = dynastylineages rank = 3 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 4 } }
			set_trait_rank = { trait = dynastylineages rank = 4 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 5 } }
			set_trait_rank = { trait = dynastylineages rank = 5 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 6 } }
			set_trait_rank = { trait = dynastylineages rank = 6 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 7 } }
			set_trait_rank = { trait = dynastylineages rank = 7 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 8 } }
			set_trait_rank = { trait = dynastylineages rank = 8 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 9 } }
			set_trait_rank = { trait = dynastylineages rank = 9 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 10 } }
			set_trait_rank = { trait = dynastylineages rank = 10 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 11 } }
			set_trait_rank = { trait = dynastylineages rank = 11 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 12 } }
			set_trait_rank = { trait = dynastylineages rank = 12 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 13 } }
			set_trait_rank = { trait = dynastylineages rank = 13 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 14 } }
			set_trait_rank = { trait = dynastylineages rank = 14 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 15 } }
			set_trait_rank = { trait = dynastylineages rank = 15 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 16 } }
			set_trait_rank = { trait = dynastylineages rank = 16 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 17 } }
			set_trait_rank = { trait = dynastylineages rank = 17 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 18 } }
			set_trait_rank = { trait = dynastylineages rank = 18 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 19 } }
			set_trait_rank = { trait = dynastylineages rank = 19 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 20 } }
			set_trait_rank = { trait = dynastylineages rank = 20 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 21 } }
			set_trait_rank = { trait = dynastylineages rank = 21 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 22 } }
			set_trait_rank = { trait = dynastylineages rank = 22 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 23 } }
			set_trait_rank = { trait = dynastylineages rank = 23 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 24 } }
			set_trait_rank = { trait = dynastylineages rank = 24 }
		}
		else_if = {
			limit = { parent_has_line_trait = { RANK = 25 } }
			set_trait_rank = { trait = dynastylineages rank = 25 }
		}
		else = {
			set_trait_rank = { trait = dynastylineages rank = 26 }
		}
	}
}

############
## FAMILY MARKS
############
# Executes if mother or father of NEWBORN are close/extended family members
# Applies "extended_family_mark" to children of close family members, and "super_extended_family_mark" to children of extended family members
on_birth_child_familymarks_effect = {
	save_father_or_real_father_or_player_effect = yes
	if = {
		limit = {
			OR = {
				AND = { # father or paternal grandparents
					exists = scope:line_father
					scope:line_father = { 
						OR = {
							has_trait = self_family_mark
							trigger_if = {
								limit = { 
									has_game_rule = use_real_father
								}
								AND = {
									exists = real_father
									real_father = { has_trait = self_family_mark }
								}
							}
							trigger_else = {
								AND = {
									exists = father
									father = { has_trait = self_family_mark }
								}
							}
							AND = {
								exists = mother
								mother = { has_trait = self_family_mark }
							}
						}
					}
				}
				AND = { # mother or mathernal grandparents
					exists = mother
					mother = { 
						OR = {
							has_trait = self_family_mark 
							trigger_if = {
								limit = { 
									has_game_rule = use_real_father
								}
								AND = {
									exists = real_father
									real_father = { has_trait = self_family_mark }
								}
							}
							trigger_else = {
								AND = {
									exists = father
									father = { has_trait = self_family_mark }
								}
							}
							AND = {
								exists = mother
								mother = { has_trait = self_family_mark }
							}
						}
					}
				}
			}
		}

		# NEEDS extra "remove_trait" because these traits are flagged "genetic" and keep being inherited from inactive stage...
		remove_trait = self_family_mark
		remove_trait = extended_family_mark
		remove_trait = super_extended_family_mark

		add_trait = close_family_mark
	}

	else_if = {
		limit = {
			OR = {
				AND = {
					exists = scope:line_father
					scope:line_father = {
						has_trait = close_family_mark
					}
				}
				AND = {
					exists = mother
					mother = {
						has_trait = close_family_mark
					}
				}
			}
		}
		# Remove ALL OTHER traits to avoid weird inheritance of genetically inactive traits...
		remove_trait = self_family_mark
		remove_trait = close_family_mark
		remove_trait = super_extended_family_mark

		add_trait = extended_family_mark
	}
	
	else_if = {
		limit = {
			OR = {
				AND = {
					exists = scope:line_father
					scope:line_father = {
						has_trait = extended_family_mark
					}
				}
				AND = {
					exists = mother
					mother = {
						has_trait = extended_family_mark
					}
				}
			}
		}

		# Remove ALL OTHER traits to avoid weird inheritance of genetically inactive traits...
		remove_trait = self_family_mark
		remove_trait = close_family_mark
		remove_trait = extended_family_mark

		add_trait = super_extended_family_mark
	}
}

#################
# OTHER LINEAGES 
#################
# Marking others' lineages
on_birth_child_otherlines_effect = {
	# Remove inherited trait from all, to avoid inheritance of wrong family lineage, hidden inheritance, etc.
	remove_trait_rank_effect = { GROUP = goodlineages }
	remove_trait_rank_effect = { GROUP = badlineages }

	save_father_or_real_father_or_player_effect = yes
	# Break loop before it starts if unnecessary (Change Log 3)
	if = {
		limit = {
			OR = {
				AND = {
					exists = scope:line_father
					scope:line_father = {
						OR = {
							has_trait_rank = {
								trait = goodlinefounder
								rank > 0
							}
							has_trait_rank = {
								trait = goodlineages
								rank > 0
							}
						}
					}
				}
				AND = {
					exists = mother
					mother = {
						OR = {
							has_trait_rank = {
								trait = goodlinefounder
								rank > 0
							}
							has_trait_rank = {
								trait = goodlineages
								rank > 0
							}
						}
					}
				}
			}
		}
		set_trait_rank = { trait = goodlineages rank = 1 }
	}

	if = {
		limit = {
			OR = {
				AND = {
					exists = scope:line_father
					scope:line_father = {
						OR = {
							has_trait_rank = {
								trait = badlinefounder
								rank > 0
							}
							has_trait_rank = {
								trait = badlineages
								rank > 0
							}
						}
					}
				}
				AND = {
					exists = mother
					mother = {
						OR = {
							has_trait_rank = {
								trait = badlinefounder
								rank > 0
							}
							has_trait_rank = {
								trait = badlineages
								rank > 0
							}
						}
					}
				}
			}
		}
		set_trait_rank = { trait = badlineages rank = 1 }
	}
}

