﻿ep_snapshot = {
    if = {
        limit = { NOT = { ep_subject_already_recorded = yes } }
    save_scope_as = ep_subject
    ep_ensure_archivist = yes
    set_global_variable = { name = ep_person_$SLOT$ value = this }
    ep_build_bundle = yes
    if = {
        limit = { exists = scope:ep_snapshot }
        set_global_variable = { name = ep_record_$SLOT$ value = scope:ep_snapshot }
        global_var:ep_archive_title = { add_to_variable_list = { name = ep_archive_records target = scope:ep_snapshot } }
        scope:ep_snapshot = { set_variable = { name = ep_slot value = $SLOT$ } set_variable = { name = ep_stage value = 1 } }
        if = { limit = { ep_eligible_posthumous = yes } scope:ep_snapshot = { set_variable = ep_posthumous } }
        if = {
            limit = { has_any_nickname = yes }
            if = { limit = { NOT = { ep_eligible_posthumous = yes } } scope:ep_snapshot = { set_variable = { name = ep_stage value = 2 } set_variable = ep_keep_original } }
            if = { limit = { ep_original_is_prefix = yes } scope:ep_snapshot = { set_variable = ep_prefix } }
            if = { limit = { ep_original_is_bad = yes } scope:ep_snapshot = { set_variable = ep_bad } }
        }
        add_to_global_variable_list = { name = ep_recorded_subjects target = scope:ep_subject }
        add_to_global_variable_list = { name = ep_queue target = scope:ep_snapshot }
        change_global_variable = { name = ep_count add = 1 }
        debug_log = "EP:QUEUED/;/[GetGlobalVariable('ep_count').GetValue]/;/[THIS.Char.GetID]"
        debug_log = "EP:PACKED_RECORD/;/[ep_snapshot.MakeScope.Var('ep_packed_memory_count').GetValue]/;/[ep_snapshot.MakeScope.Var('ep_packed_trait_count').GetValue]"
    }
    else = { debug_log = "EP:SNAPSHOT_FAILED/;/[THIS.Char.GetID]" }
    }
    else = { debug_log = "EP:DUPLICATE_SUBJECT_SKIPPED/;/[THIS.Char.GetID]" }
}

ep_bind_slot = {
    set_global_variable = { name = ep_active_slot value = $SLOT$ }
    set_global_variable = { name = ep_active_person value = global_var:ep_person_$SLOT$ }
    global_var:ep_active_record = { ep_bind_archive_title = yes }
    if = { limit = { exists = global_var:ep_name_$SLOT$ } global_var:ep_name_$SLOT$ = { ep_bind_archive_title = yes } }
    if = {
        limit = { NOT = { global_var:ep_active_record = { has_variable = ep_packed_record } } }
        every_in_global_list = { variable = ep_data_$SLOT$ ep_bind_archive_title = yes }
    }
    if = {
        limit = { exists = global_var:ep_name_$SLOT$ }
        set_global_variable = { name = ep_active_name value = global_var:ep_name_$SLOT$ }
    }
    else = { remove_global_variable = ep_active_name }
    # The successor is the letter sender; the deceased is the subject/AI.
    debug_log = "VOTC:IN/;/init/;/[THIS.Char.GetID]/;/[THIS.Char.GetFullNameNicknamedNoTooltipRegnal]/;/[GetGlobalVariable('ep_active_person').Char.Custom('ep_wire_subject_id')]/;/[GetGlobalVariable('ep_active_person').Char.GetFullNameNicknamedNoTooltipRegnal]/;/[GetCurrentDate.GetStringShort]/;/talk_scene_ep_memorial/;/archive/;/archive/;/[GetCurrentDate.GetDateAsTotalDays]"
    ep_log_character = yes
    ep_log_troops = yes
    debug_log = "[GetGlobalVariable('ep_active_record').Artifact.GetDesc]"
    if = {
        limit = { NOT = { global_var:ep_active_record = { has_variable = ep_packed_record } } }
        every_in_global_list = { variable = ep_data_$SLOT$ debug_log = "[THIS.Artifact.GetDesc]" }
    }
    # The default desktop letter description requires a troops object even
    # for the deceased. Its current (post-death) army is not life evidence.
    if = {
        limit = { global_var:ep_active_record = { has_variable = ep_isolated_history } }
        debug_log = "VOTC:IN/;/levies_vassals/;/[GetGlobalVariable('ep_active_person').Char.Custom('ep_wire_subject_id')]/;/0"
        debug_log = "VOTC:IN/;/troops_eob/;/[GetGlobalVariable('ep_active_person').Char.Custom('ep_wire_subject_id')]/;/"
    }
    else = { global_var:ep_active_person = { ep_log_troops = yes } }
}

ep_choose_thread = {
    set_global_variable = { name = votc_letter_9 value = yes }
    set_global_variable = { name = message_second_scope_letter_9 value = global_var:ep_active_person }
    set_global_variable = ep_waiting
    debug_log = "VOTC:DATE/;/[GetCurrentDate.GetDateAsTotalDays]"
    if = {
        limit = { global_var:ep_active_record = { var:ep_stage = 1 } }
        if = {
            limit = { global_var:ep_active_record = { has_variable = ep_posthumous } }
            debug_log = ep_request_posthumous
        }
        else = { debug_log = ep_request_name }
    }
    else = { debug_log = ep_request_summary }
    trigger_event = ep_legacy.2
}

ep_tick_effect = {
    ep_ensure_archivist = yes
    ep_seed_recorded_subjects = yes
    ep_cleanup_orphan_bundles = yes
    if = {
        limit = { exists = global_var:ep_migrating NOT = { exists = global_var:ep_transfer_open } }
        set_global_variable = ep_transfer_open
        trigger_event = ep_legacy.3
    }
    if = {
        limit = {
            NOT = { exists = global_var:ep_active_record }
            NOT = { exists = global_var:ep_waiting }
            NOT = { exists = global_var:ep_migrating }
            NOT = { exists = global_var:ep_native_upgrade_checked }
            ep_votc_idle = yes
        }
        ep_upgrade_dispatch = yes
    }
    if = {
        limit = {
            NOT = { exists = global_var:ep_waiting }
            NOT = { exists = global_var:ep_migrating }
            ep_votc_idle = yes
        }
        if = {
            limit = { NOT = { exists = global_var:ep_active_record } }
            ordered_in_global_list = {
                variable = ep_queue
                position = 0
                set_global_variable = { name = ep_active_record value = this }
            }
        }
        if = {
            limit = { exists = global_var:ep_active_record }
            ep_dispatch = yes
        }
    }
}


ep_capture_slot = {
    # The reply is stored in a native global flag string and the GUI has
    # verified the readback. Artifacts are transport only, never final storage.
    if = {
        limit = { global_var:ep_active_record = { var:ep_stage = 1 } }
        set_global_variable = ep_native_name_ready_$SLOT$
        if = { limit = { exists = global_var:ep_name_$SLOT$ } destroy_artifact = global_var:ep_name_$SLOT$ }
        remove_global_variable = ep_name_$SLOT$
        remove_global_variable = ep_active_name
        global_var:ep_active_record = {
            set_variable = { name = ep_stage value = 2 }
            remove_variable = ep_prefix
            remove_variable = ep_bad
            if = { limit = { exists = global_var:ep_language_prefix } set_variable = ep_prefix }
        }
    }
    else = {
        set_global_variable = ep_native_summary_ready_$SLOT$
        if = {
            limit = { NOT = { global_var:ep_active_record = { has_variable = ep_keep_original } } }
            global_var:ep_active_person = {
                remove_nickname = yes
                if = {
                    limit = { global_var:ep_active_record = { has_variable = ep_prefix } }
                    if = { limit = { global_var:ep_active_record = { has_variable = ep_bad } } give_nickname = ep_legacy_$SLOT$_prefix_bad }
                    else = { give_nickname = ep_legacy_$SLOT$_prefix }
                }
                else = {
                    if = { limit = { global_var:ep_active_record = { has_variable = ep_bad } } give_nickname = ep_legacy_$SLOT$_suffix_bad }
                    else = { give_nickname = ep_legacy_$SLOT$_suffix }
                }
            }
        }
        if = { limit = { exists = global_var:ep_name_$SLOT$ } destroy_artifact = global_var:ep_name_$SLOT$ }
        if = { limit = { exists = global_var:ep_summary_$SLOT$ } destroy_artifact = global_var:ep_summary_$SLOT$ }
        remove_global_variable = ep_name_$SLOT$
        remove_global_variable = ep_summary_$SLOT$
        global_var:ep_active_record = { set_variable = { name = ep_stage value = 3 } }
        remove_list_global_variable = { name = ep_queue target = global_var:ep_active_record }
        if = {
            limit = { NOT = { global_var:ep_active_record = { has_variable = ep_packed_record } } }
            every_in_global_list = { variable = ep_data_$SLOT$ destroy_artifact = this }
            clear_global_variable_list = ep_data_$SLOT$
        }
        global_var:ep_archive_title = { remove_list_variable = { name = ep_archive_records target = global_var:ep_active_record } }
        debug_log = "EP:COMPLETED_NATIVE/;/[GetGlobalVariable('ep_active_slot').GetValue]/;/[GetGlobalVariable('ep_active_person').Char.GetID]"
        ep_remove_duplicate_bundles_for_subject = yes
        destroy_artifact = global_var:ep_record_$SLOT$
        remove_global_variable = ep_record_$SLOT$
        remove_global_variable = ep_active_record
        remove_global_variable = ep_active_slot
        remove_global_variable = ep_active_person
        remove_global_variable = ep_active_name
    }
    destroy_artifact = global_var:votc_latest_letter
    remove_global_variable = votc_latest_letter
    remove_global_variable = ep_waiting
    ep_clear_transfer = yes
}
