﻿search_for_recent_dynasty_founders = {
	if = {
		limit = {
			has_game_rule = use_real_father
			exists = real_father
			real_father = {
				OR = {
					has_trait_rank = { trait = dynastylineages rank > 0 } 
					has_trait_rank = { trait = linefounder rank > 0 } 
				}
			}
		}
		real_father = { save_scope_as = recent_parent_founder }
		search_for_recent_parent_founder = { PARENT = real_father LINE = dynastylineages FOUNDER = linefounder }
	}
	else_if = {
		limit = {
			has_game_rule = use_father
			exists = father
			father = {
				OR = {
					has_trait_rank = { trait = dynastylineages rank > 0 } 
					has_trait_rank = { trait = linefounder rank > 0 }  
				}
			}
		}
		father = { save_scope_as = recent_parent_founder }
		search_for_recent_parent_founder = { PARENT = father LINE = dynastylineages FOUNDER = linefounder }
	}
	else_if = {
		limit = {
			exists = mother
			mother = {
				OR = {
					has_trait_rank = { trait = dynastylineages rank > 0 } 
					has_trait_rank = { trait = linefounder rank > 0 } 
				}
			}
		}
		mother = { save_scope_as = recent_parent_founder }
		if = {
			limit = {
				has_game_rule = use_real_father
			}
			search_for_recent_parent_founder = { PARENT = real_father LINE = dynastylineages FOUNDER = linefounder }
		}
		else = {
			search_for_recent_parent_founder = { PARENT = father LINE = dynastylineages FOUNDER = linefounder }
		}
	}
	else = {
		save_scope_as = recent_parent_founder
	}
	remove_descendants = { LINE = founder }
}

search_for_recent_paternal_founders = {
	if = {
		limit = {
			has_game_rule = use_real_father
			exists = real_father
			real_father = {
				OR = {
					has_trait_rank = { trait = patrilineages rank > 0 } 
					has_trait_rank = { trait = patrilinefounder rank > 0 } 
				}
			}
		}
		real_father = { save_scope_as = recent_parent_founder }
		search_for_recent_parent_founder = { PARENT = real_father LINE = patrilineages FOUNDER = patrilinefounder }
	}
	else_if = {
		limit = {
			has_game_rule = use_father
			exists = father
			father = {
				OR = {
					has_trait_rank = { trait = patrilineages rank > 0 } 
					has_trait_rank = { trait = patrilinefounder rank > 0 } 
				}
			}
		}
		father = { save_scope_as = recent_parent_founder }
		search_for_recent_parent_founder = { PARENT = father LINE = patrilineages FOUNDER = patrilinefounder }
	}
	else = {
		save_scope_as = recent_parent_founder
	}
	remove_descendants = { LINE = patriline }
}

search_for_recent_maternal_founders = {
	if = {
		limit = {
			exists = mother
			mother = {
				OR = {
					has_trait_rank = { trait = matrilineages rank > 0 } 
					has_trait_rank = { trait = matrilinefounder rank > 0 } 
				}
			}
		}
		mother = { save_scope_as = recent_parent_founder }
		search_for_recent_parent_founder = { PARENT = mother LINE = matrilineages FOUNDER = matrilinefounder }
	}
	else = {
		save_scope_as = recent_parent_founder
	}
	remove_descendants = { LINE = matriline }
}


#####################
# GENERAL FUNCTIONS #
#####################

# LOOP: Search for dynasty/house founder
search_for_recent_founder = {
	scope:recent_parent_founder = {
		if = { 
			limit = { 
				exists = $PARENT$
				$PARENT$ = {
					OR = {
						has_trait_rank = {
							trait = $LINE$
							rank > 0
						} 
						has_trait_rank = {
							trait = $FOUNDER$
							rank > 0
						} 
					}
				}
			}
			$PARENT$ = {save_scope_as = recent_parent_founder }
		}
	}
}

search_for_recent_parent_founder = {
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
	search_for_recent_founder = { PARENT = $PARENT$ LINE = $LINE$ FOUNDER = $FOUNDER$ }
}

#####################
# REMOVAL - GENERAL #
#####################

remove_descendants = { # Needs LINE 

	save_scope_as = actor

	remove_$LINE$_children = yes

	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	remove_$LINE$_descendant = yes
	
	clear_variable_list = parent_list
	clear_variable_list = descendant_list
}

#####################
# REMOVAL - DYNASTY #
#####################
remove_founder_children = {
	scope:recent_parent_founder = {
		remove_trait_rank_effect = { GROUP = dynastylineages }
		remove_trait_rank_effect = { GROUP = linefounder }
		remove_from_line_head_effect = { LINE = dynastylineages }
		every_child = {
			even_if_dead = yes
			remove_trait_rank_effect = { GROUP = dynastylineages }
			remove_trait_rank_effect = { GROUP = linefounder }
			save_scope_as = this_child
			scope:actor = {
				add_to_variable_list = {
					name = descendant_list
					target = scope:this_child 
				}
			}
		}
	}
}

remove_founder_descendant = {
	if = {
		limit = {
			has_variable_list = descendant_list
		}
		every_in_list = {
			variable = descendant_list
			save_scope_as = this_parent
			scope:actor = {
				add_to_variable_list = {
					name = parent_list
					target = scope:this_parent
				}
			}
		}
		clear_variable_list = descendant_list
		every_in_list = {
			variable = parent_list
			every_child = {
				even_if_dead = yes
				remove_trait_rank_effect = { GROUP = dynastylineages }
				remove_trait_rank_effect = { GROUP = linefounder }
				save_scope_as = this_child
				scope:actor = {
					add_to_variable_list = {
						name = descendant_list
						target = scope:this_child 
					}
				}
			}
		}
		clear_variable_list = parent_list
	}
}

#########################
# REMOVAL - PATRILINES #
#########################

remove_patriline_children = {
	scope:recent_parent_founder = {
		remove_trait_rank_effect = { GROUP = patrilineages }
		remove_trait_rank_effect = { GROUP = patrilinefounder }
		remove_from_line_head_effect = { LINE = patrilineages }
		every_child = {
			even_if_dead = yes
			limit = { 
				is_male = yes
			}
			remove_trait_rank_effect = { GROUP = patrilineages }
			remove_trait_rank_effect = { GROUP = patrilinefounder }
			save_scope_as = this_child
			scope:actor = {
				add_to_variable_list = {
					name = descendant_list
					target = scope:this_child 
				}
			}
		}
	}
}

remove_patriline_descendant = {
	if = {
		limit = {
			has_variable_list = descendant_list
		}
		every_in_list = {
			variable = descendant_list
			save_scope_as = this_parent
			scope:actor = {
				add_to_variable_list = {
					name = parent_list
					target = scope:this_parent
				}
			}
		}
		clear_variable_list = descendant_list
		every_in_list = {
			variable = parent_list
			every_child = {
				even_if_dead = yes
				limit = { 
					is_male = yes
				}
				remove_trait_rank_effect = { GROUP = patrilineages }
				remove_trait_rank_effect = { GROUP = patrilinefounder }
				save_scope_as = this_child
				scope:actor = {
					add_to_variable_list = {
						name = descendant_list
						target = scope:this_child 
					}
				}
			}
		}
		clear_variable_list = parent_list
	}
}

#########################
# REMOVAL - MATRILINES #
#########################

remove_matriline_children = {
	scope:recent_parent_founder = {
		remove_trait_rank_effect = { GROUP = matrilineages }
		remove_trait_rank_effect = { GROUP = matrilinefounder }
		remove_from_line_head_effect = { LINE = matrilineages }
		every_child = {
			even_if_dead = yes
			remove_trait_rank_effect = { GROUP = matrilineages }
			remove_trait_rank_effect = { GROUP = matrilinefounder }
			if = {
				limit = { 
					is_female = yes
				}
				save_scope_as = this_child
				scope:actor = {
					add_to_variable_list = {
						name = descendant_list
						target = scope:this_child 
					}
				}
			}
		}
	}
}

remove_matriline_descendant = {
	if = {
		limit = {
			has_variable_list = descendant_list
		}
		every_in_list = {
			variable = descendant_list
			save_scope_as = this_parent
			scope:actor = {
				add_to_variable_list = {
					name = parent_list
					target = scope:this_parent
				}
			}
		}
		clear_variable_list = descendant_list
		every_in_list = {
			variable = parent_list
			every_child = {
				even_if_dead = yes
				remove_trait_rank_effect = { GROUP = matrilineages }
				remove_trait_rank_effect = { GROUP = matrilinefounder }
				if = {
					limit = { 
						is_female = yes
					}
					save_scope_as = this_child
					scope:actor = {
						add_to_variable_list = {
							name = descendant_list
							target = scope:this_child 
						}
					}
				}
			}
		}
		clear_variable_list = parent_list
	}
}

### GOODLINEAGES / BADLINEAGES

remove_goodline_children = {
	scope:recent_parent_founder = {
		remove_trait_rank_effect = { GROUP = goodlineages }
		remove_trait_rank_effect = { GROUP = goodlinefounder }
		remove_from_line_head_effect = { LINE = goodlineages }
		every_child = {
			even_if_dead = yes
			remove_trait_rank_effect = { GROUP = goodlineages }
			remove_trait_rank_effect = { GROUP = goodlinefounder }
			save_scope_as = this_child
			scope:actor = {
				add_to_variable_list = {
					name = descendant_list
					target = scope:this_child 
				}
			}
		}
	}
}

remove_goodline_descendant = {
	if = {
		limit = {
			has_variable_list = descendant_list
		}
		every_in_list = {
			variable = descendant_list
			save_scope_as = this_parent
			scope:actor = {
				add_to_variable_list = {
					name = parent_list
					target = scope:this_parent
				}
			}
		}
		clear_variable_list = descendant_list
		every_in_list = {
			variable = parent_list
			every_child = {
				even_if_dead = yes
				remove_trait_rank_effect = { GROUP = goodlineages }
				remove_trait_rank_effect = { GROUP = goodlinefounder }
				save_scope_as = this_child
				scope:actor = {
					add_to_variable_list = {
						name = descendant_list
						target = scope:this_child 
					}
				}
			}
		}
		clear_variable_list = parent_list
	}
}


remove_badline_children = {
	scope:recent_parent_founder = {
		remove_trait_rank_effect = { GROUP = badlineages }
		remove_trait_rank_effect = { GROUP = badlinefounder }
		remove_from_line_head_effect = { LINE = badlineages }
		every_child = {
			even_if_dead = yes
			remove_trait_rank_effect = { GROUP = badlineages }
			remove_trait_rank_effect = { GROUP = badlinefounder }
			save_scope_as = this_child
			scope:actor = {
				add_to_variable_list = {
					name = descendant_list
					target = scope:this_child 
				}
			}
		}
	}
}

remove_badline_descendant = {
	if = {
		limit = {
			has_variable_list = descendant_list
		}
		every_in_list = {
			variable = descendant_list
			save_scope_as = this_parent
			scope:actor = {
				add_to_variable_list = {
					name = parent_list
					target = scope:this_parent
				}
			}
		}
		clear_variable_list = descendant_list
		every_in_list = {
			variable = parent_list
			every_child = {
				even_if_dead = yes
				remove_trait_rank_effect = { GROUP = badlineages }
				remove_trait_rank_effect = { GROUP = badlinefounder }
				save_scope_as = this_child
				scope:actor = {
					add_to_variable_list = {
						name = descendant_list
						target = scope:this_child 
					}
				}
			}
		}
		clear_variable_list = parent_list
	}
}



###### REMOVAL - FAMILY MARKS ######

## REMOVE MARKS
remove_familymarks_decendants = {
	remove_trait = self_family_mark
	remove_trait = close_family_mark
	remove_trait = extended_family_mark
	remove_trait = super_extended_family_mark
	remove_from_line_head_effect = { LINE = familymarks }
	# First try closest including dead
	every_close_or_extended_family_member = {
		even_if_dead = yes
		remove_trait = self_family_mark
		remove_trait = close_family_mark
		remove_trait = extended_family_mark
		remove_trait = super_extended_family_mark
		# Now, in case some generations have passed since last we marked relatives...
		every_close_or_extended_family_member = {
			even_if_dead = yes
			remove_trait = self_family_mark
			remove_trait = close_family_mark
			remove_trait = extended_family_mark
			remove_trait = super_extended_family_mark
			every_close_or_extended_family_member = {
				even_if_dead = yes
				remove_trait = self_family_mark
				remove_trait = close_family_mark
				remove_trait = extended_family_mark
				remove_trait = super_extended_family_mark
				every_close_or_extended_family_member = {
					even_if_dead = yes
					remove_trait = self_family_mark
					remove_trait = close_family_mark
					remove_trait = extended_family_mark
					remove_trait = super_extended_family_mark
				}
			}
		}
	}
	if = {
		limit = {
			has_game_rule = use_real_father
		}
		every_living_character = {
			remove_trait = self_family_mark
			remove_trait = close_family_mark
			remove_trait = extended_family_mark
			remove_trait = super_extended_family_mark
		}
	}
}

