﻿# One persistent institution title. A living successor to this title is the
# warehouse. Do not create a second camp from the pre-succession death hook.
ep_reset_archivist_identity = {
    save_scope_as = ep_reset_archivist
    every_spouse = { divorce = scope:ep_reset_archivist }
    if = { limit = { exists = betrothed } break_betrothal = betrothed }
    if = { limit = { is_concubine = yes exists = concubinist } concubinist = { remove_concubine = scope:ep_reset_archivist } }
    every_concubine = { scope:ep_reset_archivist = { remove_concubine = prev } }
    every_child = {
        # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
        #tiger-ignore(key=unknown-field, text="clear_father")
        if = { limit = { father = scope:ep_reset_archivist } clear_father = yes }
        # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
        #tiger-ignore(key=unknown-field, text="clear_mother")
        if = { limit = { mother = scope:ep_reset_archivist } clear_mother = yes }
        # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
        #tiger-ignore(key=unknown-field, text="clear_real_father")
        if = { limit = { real_father = scope:ep_reset_archivist } clear_real_father = yes }
        # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
        #tiger-ignore(key=unknown-field, text="clear_real_mother")
        if = { limit = { real_mother = scope:ep_reset_archivist } clear_real_mother = yes }
    }
    # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
    #tiger-ignore(key=unknown-field, text="clear_father")
    clear_father = yes
    # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
    #tiger-ignore(key=unknown-field, text="clear_mother")
    clear_mother = yes
    # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
    #tiger-ignore(key=unknown-field, text="clear_real_father")
    clear_real_father = yes
    # Native CK3 1.20 API; populated parent/child links validated in-game. Tiger targets 1.19.
    #tiger-ignore(key=unknown-field, text="clear_real_mother")
    clear_real_mother = yes
    set_to_lowborn = yes
    every_character_trait = { save_scope_as = ep_removed_trait prev = { remove_trait = scope:ep_removed_trait } }
    remove_nickname = yes
    change_first_name = ep_archivist_name
    add_trait = immortal
    add_trait = incapable
    set_variable = ep_archivist_identity_reset_051
    debug_log = "EP:ARCHIVIST_IDENTITY_RESET/;/[THIS.Char.GetID]"
}

ep_archivist_properties = {
    if = { limit = { NOT = { has_government = nomad_government } } change_government = nomad_government }
    if = { limit = { NOT = { has_variable = ep_archivist_identity_reset_051 } } ep_reset_archivist_identity = yes }
    change_first_name = ep_archivist_name
    if = { limit = { NOT = { has_trait = immortal } } add_trait = immortal }
    if = { limit = { NOT = { has_trait = incapable } } add_trait = incapable }
    set_variable = can_not_marry
    if = { limit = { NOT = { has_character_modifier = ep_archivist_guard_modifier } } add_character_modifier = { modifier = ep_archivist_guard_modifier years = -1 } }
}

ep_initialize_archive_anchor = {
    if = {
        limit = { NOT = { exists = global_var:ep_archive_anchor } }
        # Recover a 0.5.0 original archive inherited by an untreated native heir.
        every_living_character = {
            limit = { ep_safe_archive_holder = yes NOT = { has_character_modifier = ep_archivist_guard_modifier } }
            every_held_title = {
                limit = { has_variable = ep_county_archive_v050 is_nomad_title = yes NOT = { exists = global_var:ep_archive_anchor } }
                set_global_variable = { name = ep_archive_anchor value = this }
            }
        }
        if = { limit = { NOT = { exists = global_var:ep_archive_anchor } exists = global_var:ep_archive_title } set_global_variable = { name = ep_archive_anchor value = global_var:ep_archive_title } }
    }
}

ep_create_archivist_character = {
    create_character = {
        name = ep_archivist_name age = 55 gender_female_chance = 0
        location = root.location culture = culture:greek faith = root.faith dynasty = none random_traits = no
        trait = immortal trait = incapable save_scope_as = ep_new_archivist
    }
    set_global_variable = { name = ep_archivist value = scope:ep_new_archivist }
    global_var:ep_archivist = { ep_archivist_properties = yes }
    debug_log = "EP:ARCHIVIST_CREATED/;/[GetGlobalVariable('ep_archivist').Char.GetID]"
}

ep_assign_archive_anchor = {
    create_title_and_vassal_change = { type = created save_scope_as = ep_archive_anchor_change add_claim_on_loss = no }
    global_var:ep_archive_anchor = { change_title_holder = { holder = global_var:ep_archivist change = scope:ep_archive_anchor_change } }
    resolve_title_and_vassal_change = scope:ep_archive_anchor_change
    set_global_variable = ep_archive_rebind_needed
}

ep_ensure_archive_title = {
    if = {
        limit = { NOT = { exists = global_var:ep_archive_anchor } }
        global_var:ep_archivist = { every_held_title = { limit = { is_nomad_title = yes tier = tier_county } save_scope_as = ep_existing_archive_camp } }
        if = { limit = { exists = scope:ep_existing_archive_camp } set_global_variable = { name = ep_archive_anchor value = scope:ep_existing_archive_camp } }
        else = {
            create_nomad_title = { name = c_ep_secret_historian holder = global_var:ep_archivist government = nomad_government save_scope_as = ep_new_archive_anchor }
            set_global_variable = { name = ep_archive_anchor value = scope:ep_new_archive_anchor }
        }
        set_global_variable = ep_archive_rebind_needed
    }
    if = { limit = { NOT = { global_var:ep_archive_anchor.holder = global_var:ep_archivist } } ep_assign_archive_anchor = yes }
    if = { limit = { OR = { NOT = { exists = global_var:ep_archive_title } global_var:ep_archive_title != global_var:ep_archive_anchor } } set_global_variable = ep_archive_rebind_needed }
    set_global_variable = { name = ep_archive_title value = global_var:ep_archive_anchor }
    global_var:ep_archive_anchor = {
        if = {
            limit = { NOT = { has_variable = ep_archive_anchor_v051 } }
            set_title_name = c_ep_secret_historian
            set_definitive_form = yes
            set_can_use_nomadic_naming = no
            set_can_be_named_after_dynasty = no
            set_no_automatic_claims = yes
            set_destroy_if_invalid_heir = no
            set_destroy_on_succession = no
            set_delete_on_destroy = no
            set_destroy_on_gain_same_tier = no
            set_variable = ep_county_archive_v050
            set_variable = ep_archive_anchor_v051
            debug_log = "EP:ARCHIVE_ANCHOR_READY/;/[THIS.Title.GetKey]/;/[THIS.Title.GetHolder.GetID]"
        }
        set_variable = { name = ep_archive_holder value = holder }
    }
    # A new caretaker can receive an automatic camp while government changes.
    # Keep the canonical title and remove that spare camp from this caretaker.
    global_var:ep_archivist = {
        every_held_title = {
            limit = { is_nomad_title = yes this != global_var:ep_archive_anchor }
            save_scope_as = ep_spare_archive_camp
            prev = { destroy_title = scope:ep_spare_archive_camp }
        }
    }
}

ep_consolidate_archive_titles = {
    if = {
        limit = { NOT = { exists = global_var:ep_archive_consolidated_v051 } }
        ep_reclaim_archive_custody = yes
        every_living_character = {
            every_held_title = {
                limit = { has_variable = ep_county_archive_v050 is_nomad_title = yes this != global_var:ep_archive_anchor }
                save_scope_as = ep_duplicate_archive_title
                prev = {
                    destroy_title = scope:ep_duplicate_archive_title
                    if = {
                        limit = { this != global_var:ep_archivist is_ai = yes has_character_modifier = ep_archivist_guard_modifier NOT = { any_held_title = { tier >= tier_county } } }
                        death = { death_reason = death_vanished }
                    }
                }
                debug_log = "EP:DUPLICATE_ARCHIVE_TITLE_REMOVED"
            }
        }
        set_global_variable = ep_archive_consolidated_v051
    }
}

ep_ensure_archivist = {
    ep_initialize_archive_anchor = yes
    if = {
        limit = { exists = global_var:ep_archive_anchor.holder global_var:ep_archive_anchor.holder = { is_alive = yes ep_safe_archive_holder = yes } }
        if = { limit = { NOT = { global_var:ep_archivist = global_var:ep_archive_anchor.holder } } set_global_variable = ep_archive_rebind_needed debug_log = "EP:ARCHIVE_NATIVE_SUCCESSOR_ADOPTED/;/[GetGlobalVariable('ep_archive_anchor').Title.GetKey]/;/[GetGlobalVariable('ep_archive_anchor').Title.GetHolder.GetID]" }
        set_global_variable = { name = ep_archivist value = global_var:ep_archive_anchor.holder }
        global_var:ep_archivist = { ep_archivist_properties = yes }
    }
    else_if = {
        limit = { exists = global_var:ep_archivist global_var:ep_archivist = { is_alive = yes ep_safe_archive_holder = yes } }
        global_var:ep_archivist = { ep_archivist_properties = yes }
    }
    else = { ep_create_archivist_character = yes set_global_variable = ep_archive_rebind_needed }
    ep_ensure_archive_title = yes
    if = { limit = { exists = global_var:ep_archive_rebind_needed } ep_reclaim_archive_custody = yes remove_global_variable = ep_archive_rebind_needed }
    ep_consolidate_archive_titles = yes
    remove_global_variable = ep_archive_succession_pending
}

ep_bind_archive_title = {
    set_variable = ep_transport_carrier
    set_variable = suppress_artifact_notifications
    set_variable = { name = artifact_succession_title value = global_var:ep_archive_anchor }
    set_owner = { target = global_var:ep_archive_anchor.holder generate_history = no }
}

ep_reclaim_archive_custody = {
    every_artifact = {
        limit = { has_variable = ep_transport_carrier }
        set_variable = suppress_artifact_notifications
        set_variable = { name = artifact_succession_title value = global_var:ep_archive_anchor }
        set_owner = { target = global_var:ep_archive_anchor.holder generate_history = no }
    }
}
# This runs while the dying subject still exposes its memories and traits.
# Only the archive holder ever owns the outgoing transport artifact.
ep_build_bundle = {
    global_var:ep_archivist = {
        create_artifact = {
            name = ep_no_prior_nickname description = ep_protocol_character
            type = journal visuals = scroll modifier = ep_archive_storage_modifier
            creator = scope:ep_subject max_durability = 100000 durability = 100000
            save_scope_as = ep_snapshot
        }
    }
    scope:ep_snapshot = {
        save_scope_as = ep_bundle
        ep_bind_archive_title = yes
        set_variable = ep_packed_record
        set_variable = ep_isolated_history
        set_variable = { name = ep_packed_memory_count value = 0 }
        set_variable = { name = ep_packed_trait_count value = 0 }
    }
    if = { limit = { has_any_nickname = yes } scope:ep_snapshot = { set_artifact_name = ep_original_nickname set_artifact_description = ep_append_nickname_context } }
    every_memory = {
        save_scope_as = ep_memory
        scope:ep_snapshot = {
            set_artifact_description = ep_append_memory
            change_variable = { name = ep_packed_memory_count add = 1 }
        }
    }
    every_character_trait = {
        save_scope_as = ep_trait
        scope:ep_snapshot = {
            set_artifact_description = ep_append_trait
            change_variable = { name = ep_packed_trait_count add = 1 }
        }
    }
}
