﻿namespace = domcom_alpha

# Full Alpha Check. Alpha Gauntlet remains the fast one-click resolution; this interaction deliberately
# opens CK3's real contest systems when the player chooses a physical or strategic challenge.
domcom_alpha.100 = {
    type = character_event
    title = domcom_alpha.100.t
    desc = domcom_alpha.100.desc
    theme = rival_relation
    trigger = {
        has_variable = domcom_alpha_event_target
        var:domcom_alpha_event_target = { is_alive = yes }
    }
    immediate = {
        hidden_effect = {
     var:domcom_alpha_event_target = { save_scope_as = domcom_alpha_target } 
        }
    }
    left_portrait = { character = root animation = anger }
    right_portrait = { character = scope:domcom_alpha_target animation = personality_bold }

    option = {
        name = domcom_alpha.100.a
        trigger = {
            is_imprisoned = no
            can_start_single_combat_trigger = yes
            can_start_single_combat_banned_checks_trigger = no
            scope:domcom_alpha_target = {
                is_imprisoned = no
                can_start_single_combat_trigger = yes
                can_start_single_combat_banned_checks_trigger = no
            }
        }
        custom_tooltip = domcom_alpha.100.a.tt
        configure_start_single_combat_effect = {
            SC_INITIATOR = root
            SC_ATTACKER = root
            SC_DEFENDER = scope:domcom_alpha_target
            FATALITY = no
            FIXED = no
            LOCALE = terrain_scope
            OUTPUT_EVENT = domcom_alpha.200
            INVALIDATION_EVENT = domcom_alpha.290
        }
    }

    option = {
        name = domcom_alpha.100.b
        trigger = {
            has_fp2_dlc_trigger = yes
            bg_can_start_board_game_trigger = yes
            can_start_board_game_eligibility_checks_trigger = yes
            bg_can_start_board_game_is_scope_banned_checks_trigger = no
            scope:domcom_alpha_target = {
                can_start_board_game_eligibility_checks_trigger = yes
                bg_can_start_board_game_is_scope_banned_checks_trigger = no
            }
        }
        custom_tooltip = domcom_alpha.100.b.tt
        configure_start_board_game_effect = {
            BG_INITIATOR = root
            BG_ATTACKER = root
            BG_DEFENDER = scope:domcom_alpha_target
            SYSTEM = chess
            LOCALE = random_nice_day
            OUTPUT_EVENT = domcom_alpha.210
            INVALIDATION_EVENT = domcom_alpha.290
        }
    }

    option = {
        name = domcom_alpha.100.c
        custom_tooltip = domcom_alpha.100.c.tt
        show_as_tooltip = {
            scope:domcom_alpha_target = {
                random_list = {
                    0 = {
                        modifier = { add = domcom_alpha_check_chance_value }
                        custom_tooltip = domcom_alpha_check_tt_success
                    }
                    0 = {
                        modifier = { add = domcom_alpha_check_failure_weight_value }
                        custom_tooltip = domcom_alpha_check_tt_failure
                    }
                }
            }
        }
        hidden_effect = {
            random = { chance = domcom_alpha_event_final_chance_value add_character_flag = domcom_alpha_event_succeeded }
            if = {
                limit = { has_character_flag = domcom_alpha_event_succeeded }
                save_scope_as = domcom_alpha_patron
                scope:domcom_alpha_target = { domcom_alpha_event_success_effect = yes }
                trigger_event = { id = domcom_alpha.240 }
            }
            else = {
                # A proud/aggressive rival may turn a failed social check into a Counter-Alpha challenge.
                if = {
                    limit = { scope:domcom_alpha_target = { OR = { has_trait = wrathful has_trait = sadistic has_trait = vengeful has_trait = brave has_trait = arrogant } prowess >= 5 } }
                    random = { chance = 40 add_character_flag = domcom_alpha_counter_escalation }
                }
                if = { limit = { has_character_flag = domcom_alpha_counter_escalation } trigger_event = { id = domcom_alpha.220 } }
                else = {
                    add_character_flag = domcom_alpha_event_failed
                    save_scope_as = domcom_alpha_patron
                    scope:domcom_alpha_target = { domcom_alpha_event_failure_effect = yes }
                    trigger_event = { id = domcom_alpha.240 }
                }
            }
        }
    }

    option = {
        name = domcom_alpha.100.d
        custom_tooltip = domcom_alpha.100.d.tt
        hidden_effect = {
            root = {
                remove_interaction_cooldown_against = {
                    interaction = domcom_alpha_check_interaction
                    target = scope:domcom_alpha_target
                }
                if = {
                    limit = { has_variable = domcom_alpha_budget_used var:domcom_alpha_budget_used > 0 }
                    change_variable = { name = domcom_alpha_budget_used subtract = 1 }
                }
                if = {
                    limit = { has_variable = domcom_alpha_attempts var:domcom_alpha_attempts > 0 }
                    change_variable = { name = domcom_alpha_attempts subtract = 1 }
                }
            }
            scope:domcom_alpha_target = {
                if = {
                    limit = {
                        has_variable = domcom_last_alpha_patron
                        var:domcom_last_alpha_patron = root
                    }
                    remove_character_flag = domcom_recent_alpha_challenge
                    remove_variable = domcom_last_alpha_patron
                }
            }
            domcom_alpha_event_cleanup_effect = yes
        }
    }
}

# Outcome of the real nonfatal single-combat Alpha route.
domcom_alpha.200 = {
    hidden = yes
    immediate = {
        if = {
            limit = { scope:sc_victor = root }
            save_scope_as = domcom_alpha_patron
            scope:sc_loser = { domcom_alpha_event_success_effect = yes }
            domcom_alpha_event_cleanup_effect = yes
        }
        else = {
            # Losing the physical contest is a true Alpha failure; no second hidden stress/success roll.
            save_scope_as = domcom_alpha_patron
            scope:sc_victor = { domcom_alpha_event_failure_effect = yes }
            domcom_alpha_event_cleanup_effect = yes
        }
    }
}

# Outcome of the real board-game Alpha route. A sufficiently aggressive winner may demand that the
# defeated player answer a Counter-Alpha physically instead of accepting the social result.
domcom_alpha.210 = {
    hidden = yes
    immediate = {
        if = {
            limit = { scope:bg_victor = root }
            save_scope_as = domcom_alpha_patron
            scope:bg_loser = { domcom_alpha_event_success_effect = yes }
            domcom_alpha_event_cleanup_effect = yes
        }
        else = {
            scope:bg_victor = { save_scope_as = domcom_alpha_target }
            if = {
                limit = { scope:domcom_alpha_target = { OR = { has_trait = wrathful has_trait = sadistic has_trait = vengeful has_trait = brave has_trait = arrogant } prowess >= 5 } }
                random = { chance = 40 add_character_flag = domcom_alpha_counter_escalation }
            }
            if = { limit = { has_character_flag = domcom_alpha_counter_escalation } trigger_event = { id = domcom_alpha.220 } }
            else = {
                save_scope_as = domcom_alpha_patron
                scope:domcom_alpha_target = { domcom_alpha_event_failure_effect = yes }
                domcom_alpha_event_cleanup_effect = yes
            }
        }
    }
}

# Counter-Alpha after a failed non-physical route. The rival's nature determines whether a lethal option
# is even on the table. Both choices use CK3's real single-combat system.
domcom_alpha.220 = {
    type = character_event
    title = domcom_alpha.220.t
    desc = {
        first_valid = {
            triggered_desc = { trigger = { scope:domcom_alpha_target = { OR = { has_trait = wrathful has_trait = sadistic has_trait = vengeful } } } desc = domcom_alpha.220.desc.deadly }
            desc = domcom_alpha.220.desc
        }
    }
    theme = rival_relation
    trigger = { has_variable = domcom_alpha_event_target var:domcom_alpha_event_target = { is_alive = yes } }
    immediate = {
        hidden_effect = {
     var:domcom_alpha_event_target = { save_scope_as = domcom_alpha_target } 
        }
    }
    left_portrait = { character = root animation = anger }
    right_portrait = { character = scope:domcom_alpha_target animation = anger }

    option = {
        name = domcom_alpha.220.a
        trigger = {
            is_imprisoned = no
            can_start_single_combat_trigger = yes
            can_start_single_combat_banned_checks_trigger = no
            scope:domcom_alpha_target = {
                is_imprisoned = no
                can_start_single_combat_trigger = yes
                can_start_single_combat_banned_checks_trigger = no
            }
        }
        custom_tooltip = domcom_alpha.220.a.tt
        configure_start_single_combat_effect = {
            SC_INITIATOR = root
            SC_ATTACKER = root
            SC_DEFENDER = scope:domcom_alpha_target
            FATALITY = no
            FIXED = no
            LOCALE = terrain_scope
            OUTPUT_EVENT = domcom_alpha.225
            INVALIDATION_EVENT = domcom_alpha.290
        }
    }

    option = {
        name = domcom_alpha.220.b
        trigger = {
            scope:domcom_alpha_target = { OR = { has_trait = wrathful has_trait = sadistic has_trait = vengeful } }
            is_imprisoned = no
            can_start_single_combat_trigger = yes
            can_start_single_combat_banned_checks_trigger = no
            scope:domcom_alpha_target = {
                is_imprisoned = no
                can_start_single_combat_trigger = yes
                can_start_single_combat_banned_checks_trigger = no
            }
        }
        custom_tooltip = domcom_alpha.220.b.tt
        configure_start_single_combat_effect = {
            SC_INITIATOR = root
            SC_ATTACKER = root
            SC_DEFENDER = scope:domcom_alpha_target
            FATALITY = yes
            FIXED = no
            LOCALE = terrain_scope
            OUTPUT_EVENT = domcom_alpha.230
            INVALIDATION_EVENT = domcom_alpha.290
        }
    }

    option = {
        name = domcom_alpha.220.c
        custom_tooltip = domcom_alpha.220.c.tt
        add_prestige = -300
        add_character_modifier = { modifier = domcom_counter_alpha_defeat_modifier years = 5 }
        hidden_effect = {
            save_scope_as = domcom_alpha_patron
            scope:domcom_alpha_target = { domcom_alpha_event_failure_effect = yes }
            domcom_alpha_event_cleanup_effect = yes
        }
    }
}

# Counter-Alpha nonfatal duel result. Winning the counterchallenge reverses the failed route into a real
# Alpha victory; losing confirms the failure.
domcom_alpha.225 = {
    hidden = yes
    immediate = {
        if = {
            limit = { scope:sc_victor = root }
            save_scope_as = domcom_alpha_patron
            scope:sc_loser = { domcom_alpha_event_success_effect = yes }
        }
        else = {
            save_scope_as = domcom_alpha_patron
            scope:sc_victor = { domcom_alpha_event_failure_effect = yes }
        }
        domcom_alpha_event_cleanup_effect = yes
    }
}

# Lethal Counter-Alpha result. The combat system handles death; if the player survives as victor, the
# Alpha victory is recorded as normal before cleanup.
domcom_alpha.230 = {
    hidden = yes
    immediate = {
        if = {
            limit = { scope:sc_victor = root root = { is_alive = yes } }
            save_scope_as = domcom_alpha_patron
            scope:sc_loser = { domcom_alpha_event_success_effect = yes }
        }
        else_if = {
            limit = { root = { is_alive = yes } exists = scope:sc_victor }
            save_scope_as = domcom_alpha_patron
            scope:sc_victor = { domcom_alpha_event_failure_effect = yes }
        }
        domcom_alpha_event_cleanup_effect = yes
    }
}

# Resolution only for the calculated standing/leverage route.
domcom_alpha.240 = {
    type = character_event
    title = domcom_alpha.240.t
    desc = {
        first_valid = {
            triggered_desc = { trigger = { has_character_flag = domcom_alpha_event_succeeded } desc = domcom_alpha.240.desc.success }
            desc = domcom_alpha.240.desc.failure
        }
    }
    theme = rival_relation
    trigger = { has_variable = domcom_alpha_event_target }
    immediate = {
        hidden_effect = {
     var:domcom_alpha_event_target = { save_scope_as = domcom_alpha_target } 
        }
    }
    left_portrait = { character = root animation = personality_rational }
    right_portrait = { character = scope:domcom_alpha_target animation = personality_rational }
    option = {
        name = domcom_alpha.240.a
        hidden_effect = { domcom_alpha_event_cleanup_effect = yes }
    }
}

# Contest invalidation cleanup.
domcom_alpha.290 = {
    hidden = yes
    immediate = { domcom_alpha_event_cleanup_effect = yes }
}

# Full World Alpha Tournament report. This replaces the narrow result toast so the player can always see
# the actual champion, the character who eliminated them, their bracket result, and their world standing.
domcom_alpha.300 = {
    type = character_event
    title = domcom_alpha.300.t
    theme = rival_relation
    trigger = {
        has_variable = domcom_world_tournament_last_champion
    }
    immediate = {
        hidden_effect = {
            var:domcom_world_tournament_last_champion = { save_scope_as = domcom_world_tournament_result_champion }
            if = {
                limit = { has_variable = domcom_world_tournament_last_eliminator }
                var:domcom_world_tournament_last_eliminator = { save_scope_as = domcom_world_tournament_result_eliminator }
            }
        }
    }
    desc = {
        first_valid = {
            triggered_desc = {
                trigger = { var:domcom_world_tournament_last_placement = 5 }
                desc = domcom_alpha.300.desc.champion
            }
            triggered_desc = {
                trigger = { var:domcom_world_tournament_last_placement = 4 }
                desc = domcom_alpha.300.desc.runner_up
            }
            triggered_desc = {
                trigger = { var:domcom_world_tournament_last_placement = 3 }
                desc = domcom_alpha.300.desc.top_1
            }
            triggered_desc = {
                trigger = { var:domcom_world_tournament_last_placement = 2 }
                desc = domcom_alpha.300.desc.top_5
            }
            triggered_desc = {
                trigger = { var:domcom_world_tournament_last_placement = 1 }
                desc = domcom_alpha.300.desc.top_10
            }
            triggered_desc = {
                trigger = { var:domcom_world_tournament_last_bracket_champion >= 1 }
                desc = domcom_alpha.300.desc.bracket
            }
            desc = domcom_alpha.300.desc.eliminated
        }
    }
    left_portrait = { character = root animation = personality_rational }
    right_portrait = { character = scope:domcom_world_tournament_result_champion animation = personality_bold }

    option = {
        name = domcom_alpha.300.a
    }
}
