﻿######################
# ON START
######################
## Mods compatibility
on_game_start = {
    on_actions = {
        on_fm_start
    }
}

on_fm_start = {
    effect = {
        set_global_variable = {
			name = fm_is_loaded
			value = yes
		}
		# Just to avoid error logs:
		if = {
			limit = {
				exists = global_var:fm_is_loaded
			}
		}
    }
}

######################
# ON START AFTER LOBBY
######################
## GAME RULE EFFECTS (assign automatically)
on_game_start_after_lobby = {
    on_actions = {
        fm_on_start_mark_lines
    }
}

fm_on_start_mark_lines = {
	effect = {
		random_ruler = {
			limit = { 
				is_ai = no
				OR = {
					has_game_rule = mark_all_lines_father
					has_game_rule = mark_all_lines_mother
				}
			}
			save_scope_as = actor

			# Scoped to Player:

			# A. Mark All Lines (Father's Dynasty)
			if = {
				limit = {
					has_game_rule = mark_all_lines_father
				}
				if = {
					limit = { 
						exists = father 
					}
					# Patrilines
					add_character_flag = active_ancestral_patrilineage_founder
					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
					}

					# Dynasty lines
					add_character_flag = active_ancestral_lineage_founder
					father = { 
						save_scope_as = father_dynasty_founder
						save_scope_as = father_house_founder
					}
					search_for_founders = { PARENT = father }
					# Apply effects
					scope:father_dynasty_founder = { 
						add_trait = lineage_founder
						mark_as_line_head_effect = { LINE = dynastylineages }
					}
					mark_founder_descendants = { PARENT = father_dynasty_founder TRAIT = 0 }

					scope:father_house_founder = { 
						add_trait = lineage_founder1
						mark_as_line_head_effect = { LINE = dynastylineages }
					}
					mark_founder_descendants = { PARENT = father_dynasty_founder TRAIT = 1 }
				}
				# Last, mark our own line
				add_character_flag = active_lineage_founder
				trigger_event = lineages.0003
				if = {
					limit = {
						is_male = yes
					}
					add_character_flag = active_patrilineage_founder
					trigger_event = lineages.0001
				}
				# Matriliens
				if = { 
					limit = { exists = mother }
					add_character_flag = active_ancestral_matrilineage_founder
					mother = { 
						save_scope_as = mother_dynasty_founder
					}
					search_for_maternal_founders = yes
					if = { 
						limit = { exists = scope:mother_dynasty_founder }
						mark_maternal_founder_descendants = yes
					}
				}
				# Mark our own line
				if = {
					limit = {
						is_female = yes
					}
					add_character_flag = active_matrilineage_founder
					trigger_event = lineages.0002
				}
			}

			# B. Mark All Lines (Mother's Dynasty)
			else_if = {
				limit = {
					has_game_rule = mark_all_lines_mother
				}
				if = { 
					limit = {  
						exists = mother 
					}
					# Matrilines
					add_character_flag = active_ancestral_matrilineage_founder
					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 Lines
					add_character_flag = active_ancestral_lineage_founder
					mother = { 
						save_scope_as = mother_dynasty_founder
						save_scope_as = mother_house_founder
					}
					search_for_founders = { PARENT = mother }

					# Apply effects
					scope:mother_dynasty_founder = { 
						add_trait = lineage_founder 
						mark_as_line_head_effect = { LINE = dynastylineages }
					}
					mark_founder_descendants = { PARENT = mother_dynasty_founder TRAIT = 0 }

					scope:mother_house_founder = { 
						add_trait = lineage_founder1
						mark_as_line_head_effect = { LINE = dynastylineages }
					}
					mark_founder_descendants = { PARENT = mother_dynasty_founder TRAIT = 1 }
				}
				# Mark our own line
				add_character_flag = active_lineage_founder
				trigger_event = lineages.0003
				if = {
					limit = {
						is_female = yes
					}
					add_character_flag = active_matrilineage_founder
					trigger_event = lineages.0002
				}
				# Patrilines
				if = { 
					limit = { exists = father }
					add_character_flag = active_ancestral_patrilineage_founder
					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
					}
				}
				# Mark our own patriline
				if = {
					limit = {
						is_male = yes
					}
					add_character_flag = active_patrilineage_founder
					trigger_event = lineages.0001
				}
			}
			
			add_character_flag = active_close_family_marker
			trigger_event = lineages.0101
			add_character_flag = active_extended_family_marker
			trigger_event = lineages.0102
			add_character_flag = active_super_extended_family_marker
			trigger_event = lineages.0103

			# # C. If we didn't choose to mark all, then test everything else
			# else = {
			# 	if = {
			# 		NOT = { has_game_rule = dont_mark_dynasty_lines }

			# 	}
			# }
		}
	}
}



