﻿namespace = line_character

# Patrilineal Founder
line_character.0001 = {

	type = character_event
	hidden = yes

	immediate = {
		save_scope_as = actor
		if = {
			limit = {
				check_global_trait_not_set_trigger = { GROUP = patrilineages }
			}
			if = { 
				limit = { exists = real_father }
				real_father = { 
					save_scope_as = real_father_dynasty_founder
				}
				search_for_paternal_founders = yes
			}

			if = { 
				limit = { exists = scope:real_father_dynasty_founder }
				mark_paternal_founder_descendants = yes
			}
		}
	}
}

# Matrilineal Founder
line_character.0002 = {

	type = character_event
	hidden = yes

	immediate = {
		save_scope_as = actor

		if = {
			limit = {
				check_global_trait_not_set_trigger = { GROUP = matrilineages }
			}
			if = { 
				limit = { exists = mother }
				mother = { 
					save_scope_as = mother_dynasty_founder
				}
				search_for_maternal_founders = yes
			}

			if = { 
				limit = {  exists = scope:mother_dynasty_founder }
				mark_maternal_founder_descendants = yes
			}
		}
	}
}

# Dynasty Founder
line_character.0003 = {

	type = character_event

	title = line_character.0003.t
	desc = line_character.0003.desc

	theme = learning

	override_background = {
		reference = mausoleum
	}
	
	left_portrait = {
		character = scope:father_dynasty_founder
		animation = personality_honorable
		trigger = {
			exists = scope:father_dynasty_founder
		}
	}

	right_portrait = {
		character = scope:mother_dynasty_founder
		animation = personality_honorable
		trigger = {
			exists = scope:mother_dynasty_founder
		}
		# animation = personality_bold # Ancestors celebrate successes.
		# animation = personality_zealous # Ancestors show piety.
	}

	lower_left_portrait = {
		character = scope:father_house_founder
		animation = personality_honorable
		trigger = {
			exists = scope:father_house_founder
			NOT = {
				scope:father_house_founder = scope:father_dynasty_founder
			}
		}
	}

	lower_right_portrait = {
		character = scope:mother_house_founder
		animation = personality_honorable
		trigger = {
			exists = scope:mother_house_founder
			NOT = {
				scope:mother_house_founder = scope:mother_dynasty_founder
			}
		}
	}
	
	immediate = {

		save_scope_as = actor

		if = { # Last check to avoid weirdness
			limit = {
				check_global_trait_not_set_trigger = { GROUP = dynastylineages }
			}
			if = {
				limit = { 
					exists = father 
				}
				father = { 
					save_scope_as = father_dynasty_founder
					save_scope_as = father_house_founder
				}
				search_for_founders = { PARENT = father }
			}
			if = { 
				limit = {  
					exists = mother 
				}
				mother = { 
					save_scope_as = mother_dynasty_founder
					save_scope_as = mother_house_founder
				}
				search_for_founders = { PARENT = mother }
			}
		}

		# This is much more elegant, but days_since_death doesn't work for death characters on start... 
		# dynasty = {
		# 	ordered_dynasty_member = {
		# 		even_if_dead = yes
		# 		order_by = days_since_death
		# 		limit = { 
		# 			any_ancestor = {  this = scope:actor } 
		# 		}
		# 		# position = 0
		# 		scope:actor = {
		# 			save_scope_as = dynasty_founder
		# 		}
		# 	}
		# }

	}

	option = {
		name = line_character.0003.father_dynasty_founder
		trigger = { exists = scope:father_dynasty_founder }
		# Apply effects
		scope:father_dynasty_founder = { 
			add_trait = lineage_founder
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_dynasty_founder TRAIT = 0 }
		}
	}
	option = {
		name = line_character.0003.father_house_founder
		trigger = { 
			exists = scope:father_house_founder 
			NOT = { scope:father_dynasty_founder = scope:father_house_founder }
		}
		# Apply effects
		scope:father_house_founder = { 
			add_trait = lineage_founder
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_house_founder TRAIT = 0 }
		}
	}
	option = {
		name = line_character.0003.father_dynasty_house_founder
		trigger = { 
			exists = scope:father_dynasty_founder
			exists = scope:father_house_founder 
			NOT = { scope:father_dynasty_founder = scope:father_house_founder }
		}
		# Apply effects
		scope:father_dynasty_founder = { 
			add_trait = lineage_founder
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_dynasty_founder TRAIT = 0 }
		}
		scope:father_house_founder = { 
			add_trait = lineage_founder1
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_dynasty_founder TRAIT = 1 }
		}
	}

	option = {
		name = line_character.0003.mother_dynasty_founder
		trigger = { exists = scope:mother_dynasty_founder }

		# Apply effects
		scope:mother_dynasty_founder = { 
			add_trait = lineage_founder
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_dynasty_founder TRAIT = 0 }
		}
	}
	option = {
		name = line_character.0003.mother_house_founder
		trigger = { 
			exists = scope:mother_house_founder 
			NOT = { scope:mother_dynasty_founder = scope:mother_house_founder }
		}
		# Apply effects
		scope:mother_house_founder = { 
			add_trait = lineage_founder
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_house_founder TRAIT = 0 }
		}
	}
	option = {
		name = line_character.0003.mother_dynasty_house_founder
		trigger = { 
			exists = scope:mother_dynasty_founder
			exists = scope:mother_house_founder 
			NOT = { scope:mother_dynasty_founder = scope:mother_house_founder }
		}
		# Apply effects
		scope:mother_dynasty_founder = { 
			add_trait = lineage_founder
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_dynasty_founder TRAIT = 0 }
		}
		scope:mother_house_founder = { 
			add_trait = lineage_founder1
			mark_as_line_head_effect = { LINE = dynastylineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_dynasty_founder TRAIT = 1 }
		}
	}

	option = {
		name = line_character.0003.skip_for_now
		hidden_effect = {
			remove_character_flag = active_ancestral_lineage_founder
		}
	}
}

########## MARK GOOD LINEAGES
# Good lineage
line_character.2001 = {

	type = character_event

	title = line_character.2001.t
	desc = line_character.2001.desc

	theme = learning

	override_background = {
		reference = mausoleum
	}
	
	left_portrait = {
		character = scope:father_dynasty_founder
		animation = personality_honorable
		trigger = {
			exists = scope:father_dynasty_founder
		}
	}

	right_portrait = {
		character = scope:mother_dynasty_founder
		animation = personality_honorable
		trigger = {
			exists = scope:mother_dynasty_founder
		}
		# animation = personality_bold # Ancestors celebrate successes.
		# animation = personality_zealous # Ancestors show piety.
	}

	lower_left_portrait = {
		character = scope:father_house_founder
		animation = personality_honorable
		trigger = {
			exists = scope:father_house_founder
			NOT = {
				scope:father_house_founder = scope:father_dynasty_founder
			}
		}
	}

	lower_right_portrait = {
		character = scope:mother_house_founder
		animation = personality_honorable
		trigger = {
			exists = scope:mother_house_founder
			NOT = {
				scope:mother_house_founder = scope:mother_dynasty_founder
			}
		}
	}
	
	immediate = {

		save_scope_as = actor

		if = {
			limit = { # Last minute check to avoid weirdness
				check_global_trait_not_set_trigger = { GROUP = goodlineages }
				exists = scope:recipient
			}
			scope:recipient = {
				if = {
					limit = { 
						exists = father 
					}
					father = { 
						save_scope_as = father_dynasty_founder
						save_scope_as = father_house_founder
					}
					search_for_founders = { PARENT = father }
				}
				if = { 
					limit = {  
						exists = mother 
					}
					mother = { 
						save_scope_as = mother_dynasty_founder
						save_scope_as = mother_house_founder
					}
					search_for_founders = { PARENT = mother }
				}
			}
		}
	}

	option = {
		name = line_character.2001.himself
		scope:recipient = {
			add_trait = good_lineage_founder # to show in tooltip
			hidden_effect = {
				trigger_event = lineages.2001
			}
		}
	}

	option = {
		name = line_character.2001.father_dynasty_founder
		trigger = { exists = scope:father_dynasty_founder }
		# Apply effects
		scope:father_dynasty_founder = { 
			add_trait = good_lineage_founder
			mark_as_line_head_effect = { LINE = goodlineages }
		}
		
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_dynasty_founder TRAIT = good }
		}
	}
	option = {
		name = line_character.2001.father_house_founder
		trigger = { 
			exists = scope:father_house_founder 
			NOT = { scope:father_dynasty_founder = scope:father_house_founder }
		}
		# Apply effects
		scope:father_house_founder = { 
			add_trait = good_lineage_founder
			mark_as_line_head_effect = { LINE = goodlineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_house_founder TRAIT = good }
		}
	}

	option = {
		name = line_character.2001.mother_dynasty_founder
		trigger = { exists = scope:mother_dynasty_founder }

		# Apply effects
		scope:mother_dynasty_founder = { 
			add_trait = good_lineage_founder
			mark_as_line_head_effect = { LINE = goodlineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_dynasty_founder TRAIT = good }
		}
	}
	option = {
		name = line_character.2001.mother_house_founder
		trigger = { 
			exists = scope:mother_house_founder 
			NOT = { scope:mother_dynasty_founder = scope:mother_house_founder }
		}
		# Apply effects
		scope:mother_house_founder = { 
			add_trait = good_lineage_founder
			mark_as_line_head_effect = { LINE = goodlineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_house_founder TRAIT = good }
		}
	}

	option = {
		name = line_character.2001.skip_for_now
		hidden_effect = {
		}
	}
}

# Bad lineage
line_character.2002 = {
	
	type = character_event

	title = line_character.2002.t
	desc = line_character.2002.desc

	theme = learning

	override_background = {
		reference = mausoleum
	}
	
	left_portrait = {
		character = scope:father_dynasty_founder
		animation = personality_honorable
		trigger = {
			exists = scope:father_dynasty_founder
		}
	}

	right_portrait = {
		character = scope:mother_dynasty_founder
		animation = personality_honorable
		trigger = {
			exists = scope:mother_dynasty_founder
		}
		# animation = personality_bold # Ancestors celebrate successes.
		# animation = personality_zealous # Ancestors show piety.
	}

	lower_left_portrait = {
		character = scope:father_house_founder
		animation = personality_honorable
		trigger = {
			exists = scope:father_house_founder
			NOT = {
				scope:father_house_founder = scope:father_dynasty_founder
			}
		}
	}

	lower_right_portrait = {
		character = scope:mother_house_founder
		animation = personality_honorable
		trigger = {
			exists = scope:mother_house_founder
			NOT = {
				scope:mother_house_founder = scope:mother_dynasty_founder
			}
		}
	}
	
	immediate = {

		save_scope_as = actor

		if = {
			limit = { # Last minute check to avoid weirdness
				check_global_trait_not_set_trigger = { GROUP = badlineages }
				exists = scope:recipient
			}
			scope:recipient = {
				if = { # Last minute check to avoid weirdness
					limit = {
						check_global_trait_not_set_trigger = { GROUP = badlineages }
					}
					if = {
						limit = { 
							exists = father 
						}
						father = { 
							save_scope_as = father_dynasty_founder
							save_scope_as = father_house_founder
						}
						search_for_founders = { PARENT = father }
					}
					if = { 
						limit = {  
							exists = mother 
						}
						mother = { 
							save_scope_as = mother_dynasty_founder
							save_scope_as = mother_house_founder
						}
						search_for_founders = { PARENT = mother }
					}
				}
			}
		}
	}

	option = {
		name = line_character.2002.himself
		scope:recipient = {
			add_trait = bad_lineage_founder # to show in tooltip
			hidden_effect = {
				trigger_event = lineages.2002
			}
		}
	}

	option = {
		name = line_character.2002.father_dynasty_founder
		trigger = { exists = scope:father_dynasty_founder }
		# Apply effects
		scope:father_dynasty_founder = { 
			add_trait = bad_lineage_founder
			mark_as_line_head_effect = { LINE = badlineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_dynasty_founder TRAIT = bad }
		}
	}
	option = {
		name = line_character.2002.father_house_founder
		trigger = { 
			exists = scope:father_house_founder 
			NOT = { scope:father_dynasty_founder = scope:father_house_founder }
		}
		# Apply effects
		scope:father_house_founder = { 
			add_trait = bad_lineage_founder
			mark_as_line_head_effect = { LINE = badlineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = father_house_founder TRAIT = bad }
		}
	}

	option = {
		name = line_character.2002.mother_dynasty_founder
		trigger = { exists = scope:mother_dynasty_founder }

		# Apply effects
		scope:mother_dynasty_founder = { 
			add_trait = bad_lineage_founder
			mark_as_line_head_effect = { LINE = badlineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_dynasty_founder TRAIT = bad }
		}
	}
	option = {
		name = line_character.2002.mother_house_founder
		trigger = { 
			exists = scope:mother_house_founder 
			NOT = { scope:mother_dynasty_founder = scope:mother_house_founder }
		}
		# Apply effects
		scope:mother_house_founder = { 
			add_trait = bad_lineage_founder
			mark_as_line_head_effect = { LINE = badlineages }
		}
		hidden_effect = {
			mark_founder_descendants = { PARENT = mother_house_founder TRAIT = bad }
		}
	}

	option = {
		name = line_character.2002.skip_for_now
		hidden_effect = {
		}
	}
}