﻿# GENERAL

check_global_trait_level_assign_value_effect = { # needs TRAIT; Saves scope:trait_levek
	if = {
		limit = {
			check_global_trait_not_set_trigger = { GROUP = $TRAIT$ }
		}
		save_scope_value_as = { name = trait_level value = 1 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 1 }
		save_scope_value_as = { name = trait_level value = 2 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 2 }
		save_scope_value_as = { name = trait_level value = 3 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 3 }
		save_scope_value_as = { name = trait_level value = 4 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 4 }
		save_scope_value_as = { name = trait_level value = 5 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 5 }
		save_scope_value_as = { name = trait_level value = 6 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 6 }
		save_scope_value_as = { name = trait_level value = 7 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 7 }
		save_scope_value_as = { name = trait_level value = 8 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 8 }
		save_scope_value_as = { name = trait_level value = 9 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 9 }
		save_scope_value_as = { name = trait_level value = 10 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 10 }
		save_scope_value_as = { name = trait_level value = 11 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 11 }
		save_scope_value_as = { name = trait_level value = 12 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 12 }
		save_scope_value_as = { name = trait_level value = 13 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 13 }
		save_scope_value_as = { name = trait_level value = 14 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 14 }
		save_scope_value_as = { name = trait_level value = 15 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 15 }
		save_scope_value_as = { name = trait_level value = 16 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 16 }
		save_scope_value_as = { name = trait_level value = 17 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 17 }
		save_scope_value_as = { name = trait_level value = 18 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 18 }
		save_scope_value_as = { name = trait_level value = 19 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 19 }
		save_scope_value_as = { name = trait_level value = 20 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 20 }
		save_scope_value_as = { name = trait_level value = 21 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 21 }
		save_scope_value_as = { name = trait_level value = 22 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 22 }
		save_scope_value_as = { name = trait_level value = 23 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 23 }
		save_scope_value_as = { name = trait_level value = 24 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 24 }
		save_scope_value_as = { name = trait_level value = 25 }
	}
	else = {
		save_scope_value_as = { name = trait_level value = 26 }
	}
	# Set global variable
	set_global_variable = { 
		name = $TRAIT$ 
		value = scope:trait_level 
	}
}

check_global_trait_level_assign_value_lesser_effect = { # needs TRAIT; Saves scope:trait_levek
	if = {
		limit = {
			check_global_trait_not_set_trigger = { GROUP = $TRAIT$ }
		}
		save_scope_value_as = { name = trait_level value = 1 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 1 }
		save_scope_value_as = { name = trait_level value = 2 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 2 }
		save_scope_value_as = { name = trait_level value = 3 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 3 }
		save_scope_value_as = { name = trait_level value = 4 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 4 }
		save_scope_value_as = { name = trait_level value = 5 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 5 }
		save_scope_value_as = { name = trait_level value = 6 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 6 }
		save_scope_value_as = { name = trait_level value = 7 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 7 }
		save_scope_value_as = { name = trait_level value = 8 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 8 }
		save_scope_value_as = { name = trait_level value = 9 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 9 }
		save_scope_value_as = { name = trait_level value = 10 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 10 }
		save_scope_value_as = { name = trait_level value = 11 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 11 }
		save_scope_value_as = { name = trait_level value = 12 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 12 }
		save_scope_value_as = { name = trait_level value = 13 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 13 }
		save_scope_value_as = { name = trait_level value = 14 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 14 }
		save_scope_value_as = { name = trait_level value = 15 }
	}
	else_if = {
		limit = { global_var:$TRAIT$ = 15 }
		save_scope_value_as = { name = trait_level value = 16 }
	}
	else = {
		save_scope_value_as = { name = trait_level value = 17 }
	}
	# Set global variable
	set_global_variable = { 
		name = $TRAIT$ 
		value = scope:trait_level 
	}
}

# Remove trait
remove_trait_rank_effect = { # needs GROUP
	change_trait_rank = {
		trait = $GROUP$
		rank = -50
	}
}

# Mark trait
mark_trait_rank_effect = { # needs GROUP
	set_trait_rank = { 
		trait = $GROUP$
		rank = scope:trait_level
	}
}

# Remove + add trait
line_level_effect = { # needs LINE
	remove_trait_rank_effect = { GROUP = $LINE$ }
	mark_trait_rank_effect = { GROUP = $LINE$ }
}

# Mark head
mark_as_line_head_effect = { # needs LINE
	add_to_global_variable_list = {
		name = $LINE$_head
		target = this
	}
}
# Remove head
remove_from_line_head_effect = { # needs LINE
	remove_list_global_variable = {
		name = $LINE$_head
		target = this
	}
}

# Mark Family members
mark_children_effect = { # needs LINE
	if = { # 1. Children
		limit = { any_child = {} }
		every_child = {
			even_if_dead = yes
			limit = { $LINE$_family_trigger = yes }
			line_level_effect = { LINE = $LINE$ } # Give trait
			if = { # 2. Grandchildren
				limit = { any_child = {} }
				every_child = {
					even_if_dead = yes
					limit = { $LINE$_family_trigger = yes }
					line_level_effect = { LINE = $LINE$ } # Give trait
					if = { # 2. Greatgrandchildren
						limit = { any_child = {} }
						every_child = {
							even_if_dead = yes
							limit = { $LINE$_family_trigger = yes }
							line_level_effect = { LINE = $LINE$ } # Give trait
							if = { # 3. Greatgreatgrandchildren
								limit = { any_child = {} }
								every_child = {
									even_if_dead = yes
									limit = { $LINE$_family_trigger = yes }
									line_level_effect = { LINE = $LINE$ } # Give trait
									if = { # 4. Greatgreatgreatgrandchildren
										limit = { any_child = {} }
										every_child = {
											even_if_dead = yes
											limit = { $LINE$_family_trigger = yes }
											line_level_effect = { LINE = $LINE$ } # Give trait
											if = { # 5. Greatgreatgreatgreatgrandchildren
												limit = { any_child = {} }
												every_child = {
													limit = { $LINE$_family_trigger = yes }
													even_if_dead = yes
													line_level_effect = { LINE = $LINE$ } # Give trait
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

# BASTARDS

## Give trait to living bastards
check_bastards_effect = { # needs LINE
	# Save a variable to check before continuing with next steps
	set_local_variable = { name = marked value = 1 }
	# Start
	every_living_character = {
		limit = { 
			is_bastard_trigger = yes
			is_bastard_$LINE$_trigger = yes
		}
		bastard_level_effect = { LINE = $LINE$ }
	}
	## Give trait to living bastards
	if = {
		limit = { local_var:marked > 1 }
		set_local_variable = { name = marked value = 1 }
		every_living_character = {
			limit = { 
				is_bastard_trigger = yes
				is_bastard_$LINE$_trigger = yes
			}
			bastard_level_effect = { LINE = $LINE$ }
		}
		## Give trait to living bastards
		if = {
			limit = { local_var:marked > 1 }
			set_local_variable = { name = marked value = 1 }
			every_living_character = {
				limit = { 
					is_bastard_trigger = yes
					is_bastard_$LINE$_trigger = yes
				}
				bastard_level_effect = { LINE = $LINE$ }
			}
			## Give trait to living bastards
			if = {
				limit = { local_var:marked > 1 }
				set_local_variable = { name = marked value = 1 }
				every_living_character = {
					limit = { 
						is_bastard_trigger = yes
						is_bastard_$LINE$_trigger = yes
					}
					bastard_level_effect = { LINE = $LINE$ }
				}
			}
		}
	}
}

bastard_level_effect = { # needs LINE
	if = {
		limit = {
			$LINE$_bastard_family_trigger = yes
		}
		line_level_effect = { LINE = $LINE$ }
		mark_children_effect = { LINE = patrilineages }
		# Mark to guess whether there are more children to look for
		change_local_variable = {
			name = marked
			add = 1
		}
	}
}

#### SAVE SCOPES
# Only for newborn bastards of players
save_father_or_real_father_or_player_effect = {
	if = {
		limit = {
			has_game_rule = use_real_father_only_player
			exists = real_father
			real_father = {
				is_ai = no
			}
		}
		real_father = { save_scope_as = line_father }
	}
	else = {
		save_father_or_real_father_effect = yes
	}
}
# All others
save_father_or_real_father_effect = {
	if = {
		limit = {
			has_game_rule = use_real_father
		}
		if = {
			limit = {
				exists = real_father
			}
			real_father = { save_scope_as = line_father }
		}
	}
	else = {
		if = {
			limit = {
				exists = father
			}
			father = { save_scope_as = line_father }
		}
	}
}

# If selected for parents, assign recipient to either real_father or mother
save_appropriate_dynasty_parent = {
	# A. Both parents exist
	if = {
		limit = {
			exists = scope:line_father
			exists = mother
		}
		# A.0. - Follow only latest line
		if = {
			limit = {
				has_game_rule = mark_latest_line
				father_has_line_trait = yes
				mother_has_line_trait = yes
				parents_have_diff_line_trait = yes
			}
			if = {
				limit = {
					exists = scope:higher_ranked
				}
				scope:higher_ranked = {
					save_scope_as = recipient
				}
				clear_saved_scope = higher_ranked
			}
			else = {
				select_higher_parent_line_trait = yes
			}
		}
		# A.1. Both or None have the trait
		else_if = {
			limit = {
				OR = {
					AND = {
						father_has_line_trait = yes
						mother_has_line_trait = yes
					}
					AND = {
						father_has_line_trait = no
						mother_has_line_trait = no
					}
				}
			}
			# 0. Follow game "inheritance" rules
			if = {
				limit = {
					exists = house
					OR = {
						exists = scope:line_father.house
						exists = mother.house
					}
				}
				if = {
					limit = {
						house = scope:line_father.house
					}
					scope:line_father = { save_scope_as = recipient }
				}
				else = {
					mother = { save_scope_as = recipient }
				}
			}
			# 1. Mother takes preference
			else_if = {
				limit = {
					mother_dominates_over_father = yes
				}
				mother = { save_scope_as = recipient }
			}
			# 2. Lowborn but parent(s) are not (mainly for Game Rule real_father)
			else_if = { 
				limit = {
					is_lowborn = yes
					OR = {
						scope:line_father = {
							is_lowborn = no
						}
						mother = {
							is_lowborn = no
						}
					}
				}
				if = {
					limit = {
						exists = mother.house
						mother_dominates_over_father = yes
					}
					mother = { save_scope_as = recipient }
				}
				else_if = {
					limit = {
						exists = scope:line_father.house
					}
					scope:line_father = { save_scope_as = recipient }
				}
				else_if = {
					limit = {
						exists = mother.house
					}
					mother = { save_scope_as = recipient }
				}
				else = {
					scope:line_father = { save_scope_as = recipient }
				}
			}
			else_if = {
				limit = {
					should_mother_give_trait_to_child_trigger = yes
				}
				mother = { save_scope_as = recipient }
			}
			else = {
				scope:line_father = { save_scope_as = recipient }
			}
		}
		# A.2. Only one of them has the trait
		else = {
			if = {
				limit = {
					father_has_line_trait = yes
				}
				scope:line_father = { save_scope_as = recipient }
			}
			else = {
				mother = { save_scope_as = recipient }
			}
		}
	}
	# B. Only one parent exists
	else = {
		if = {
			limit = { # most frequent case
				exists = mother
			}
			mother = { save_scope_as = recipient }
		}
		else = {
			scope:line_father = { save_scope_as = recipient }
		}
	}
}

select_higher_parent_line_trait = {
	if = { limit = { parent_has_diff_line_trait = { RANK = 1 } } select_parent_line_diff = { RANK = 1 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 2 } } select_parent_line_diff = { RANK = 2 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 3 } } select_parent_line_diff = { RANK = 3 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 4 } } select_parent_line_diff = { RANK = 4 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 5 } } select_parent_line_diff = { RANK = 5 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 6 } } select_parent_line_diff = { RANK = 6 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 7 } } select_parent_line_diff = { RANK = 7 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 8 } } select_parent_line_diff = { RANK = 8 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 9 } } select_parent_line_diff = { RANK = 9 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 10 } } select_parent_line_diff = { RANK = 10 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 11 } } select_parent_line_diff = { RANK = 11 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 12 } } select_parent_line_diff = { RANK = 12 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 13 } } select_parent_line_diff = { RANK = 13 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 14 } } select_parent_line_diff = { RANK = 14 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 15 } } select_parent_line_diff = { RANK = 15 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 16 } } select_parent_line_diff = { RANK = 16 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 17 } } select_parent_line_diff = { RANK = 17 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 18 } } select_parent_line_diff = { RANK = 18 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 19 } } select_parent_line_diff = { RANK = 19 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 20 } } select_parent_line_diff = { RANK = 20 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 21 } } select_parent_line_diff = { RANK = 21 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 22 } } select_parent_line_diff = { RANK = 22 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 23 } } select_parent_line_diff = { RANK = 23 } }
	else_if = { limit = { parent_has_diff_line_trait = { RANK = 24 } } select_parent_line_diff = { RANK = 24 } }
	else = { select_parent_line_diff = { RANK = 25 } }
}

select_parent_line_diff = {
	if = {
		limit = {
			mother = {
				OR = {
					has_trait_rank = {
						trait = linefounder
						rank = $RANK$
					}
					has_trait_rank = {
						trait = dynastylineages
						rank = $RANK$
					}
				}
			}
		}
		scope:line_father = {
			save_scope_as = recipient
		}
	}
	else = {
		mother = {
			save_scope_as = recipient
		}
	}
}