﻿namespace = domcom_guard

# Immediate warning after an AI courtship/Tantalize breakthrough against the human player's lawful
# partner. This is intentionally not proof of sex and does not create Cucked state.
domcom_guard.100 = {
    type = character_event
    title = domcom_guard.100.t
    desc = domcom_guard.100.desc
    theme = intrigue
    left_portrait = { character = root animation = personality_rational }
    right_portrait = { character = scope:domcom_guard_suitor animation = flirtation }
    trigger = {
        exists = scope:domcom_guard_partner
        exists = scope:domcom_guard_suitor
        scope:domcom_guard_partner = { is_alive = yes }
        scope:domcom_guard_suitor = { is_alive = yes is_ai = yes }
    }
    option = { name = domcom_guard.100.a }
}

# Delayed autonomous escalation. Runs from the pursued partner and opens the player's confrontation only
# if the AI suitor is still pursuing them and the one-year protection is not active.
domcom_guard.105 = {
    hidden = yes
    trigger = {
        is_alive = yes
        age >= 18
        has_variable = domcom_pending_ai_suitor
    }
    immediate = {
        save_scope_as = domcom_guard_partner
        if = {
            limit = { has_character_flag = domcom_household_guarded }
            domcom_clear_pending_ai_courtship_effect = yes
        }
        else_if = {
            limit = { var:domcom_pending_ai_suitor = { is_alive = yes is_ai = yes age >= 18 } }
            var:domcom_pending_ai_suitor = { save_scope_as = domcom_guard_suitor }
            if = {
                limit = {
                    OR = {
                        has_relation_crush = scope:domcom_guard_suitor
                        has_relation_lover = scope:domcom_guard_suitor
                        has_relation_soulmate = scope:domcom_guard_suitor
                        AND = {
                            has_variable = domcom_obedience_patron
                            var:domcom_obedience_patron = scope:domcom_guard_suitor
                        }
                    }
                }
                if = {
                    limit = {
                        exists = primary_spouse
                        primary_spouse = {
                            is_alive = yes
                            age >= 18
                            is_ai = no
                            NOT = { this = scope:domcom_guard_suitor }
                        }
                    }
                    primary_spouse = { save_scope_as = domcom_guardian_spouse }
                }
                else = {
                    every_consort = {
                        limit = {
                            is_alive = yes
                            age >= 18
                            is_ai = no
                            NOT = { this = scope:domcom_guard_suitor }
                        }
                        save_scope_as = domcom_guardian_spouse
                    }
                }

                if = {
                    limit = { exists = scope:domcom_guardian_spouse }
                    scope:domcom_guardian_spouse = {
                        set_variable = { name = domcom_guard_partner value = scope:domcom_guard_partner }
                        set_variable = { name = domcom_guard_suitor value = scope:domcom_guard_suitor }
                        trigger_event = { id = domcom_guard.110 }
                    }
                }
                else = { domcom_clear_pending_ai_courtship_effect = yes }
            }
            else = { domcom_clear_pending_ai_courtship_effect = yes }
        }
        else = { domcom_clear_pending_ai_courtship_effect = yes }
    }
}

# Before Dominion-generated AI sex with the player's spouse, the player gets a real CK3 contest.
domcom_guard.110 = {
    type = character_event
    title = domcom_guard.110.t
    desc = domcom_guard.110.desc
    theme = rival_relation
    trigger = {
        has_variable = domcom_guard_partner
        has_variable = domcom_guard_suitor
        var:domcom_guard_partner = {
            is_alive = yes
            age >= 18
            NOT = { has_character_flag = domcom_household_guarded }
            is_consort_of = root
        }
        var:domcom_guard_suitor = { is_alive = yes is_ai = yes age >= 18 NOT = { this = root } }
    }
    immediate = {
        hidden_effect = {
            var:domcom_guard_partner = { save_scope_as = domcom_guard_partner }
            var:domcom_guard_suitor = { save_scope_as = domcom_guard_suitor }
        }
    }
    left_portrait = { character = root animation = anger }
    right_portrait = { character = scope:domcom_guard_suitor animation = personality_bold }

    option = {
        name = domcom_guard.110.a
        trigger = {
            is_imprisoned = no
            can_start_single_combat_trigger = yes
            can_start_single_combat_banned_checks_trigger = no
            scope:domcom_guard_suitor = {
                is_imprisoned = no
                can_start_single_combat_trigger = yes
                can_start_single_combat_banned_checks_trigger = no
            }
        }
        custom_tooltip = domcom_guard.110.a.tt
        configure_start_single_combat_effect = {
            SC_INITIATOR = root
            SC_ATTACKER = root
            SC_DEFENDER = scope:domcom_guard_suitor
            FATALITY = no
            FIXED = no
            LOCALE = terrain_scope
            OUTPUT_EVENT = domcom_guard.120
            INVALIDATION_EVENT = domcom_guard.190
        }
    }

    option = {
        name = domcom_guard.110.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_guard_suitor = {
                can_start_board_game_eligibility_checks_trigger = yes
                bg_can_start_board_game_is_scope_banned_checks_trigger = no
            }
        }
        custom_tooltip = domcom_guard.110.b.tt
        configure_start_board_game_effect = {
            BG_INITIATOR = root
            BG_ATTACKER = root
            BG_DEFENDER = scope:domcom_guard_suitor
            SYSTEM = chess
            LOCALE = random_nice_day
            OUTPUT_EVENT = domcom_guard.121
            INVALIDATION_EVENT = domcom_guard.190
        }
    }

    option = {
        name = domcom_guard.110.c
        custom_tooltip = domcom_guard.110.c.tt
        hidden_effect = {
            add_character_flag = domcom_guard_stood_aside
            domcom_household_guard_failure_effect = yes
        }
    }

    # Technical fallback: if CK3 currently forbids both minigames, nobody gets sex for free. The pending
    # attempt waits a month and tries again instead of treating combat ineligibility as a player loss.
    option = {
        name = domcom_guard.110.d
        trigger = {
            NOT = {
                AND = {
                    is_imprisoned = no
                    can_start_single_combat_trigger = yes
                    can_start_single_combat_banned_checks_trigger = no
                    scope:domcom_guard_suitor = {
                        is_imprisoned = no
                        can_start_single_combat_trigger = yes
                        can_start_single_combat_banned_checks_trigger = no
                    }
                }
            }
            NOT = {
                AND = {
                    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_guard_suitor = {
                        can_start_board_game_eligibility_checks_trigger = yes
                        bg_can_start_board_game_is_scope_banned_checks_trigger = no
                    }
                }
            }
        }
        custom_tooltip = domcom_guard.110.d.tt
        hidden_effect = {
            scope:domcom_guard_partner = { trigger_event = { id = domcom_guard.105 days = 30 } }
            remove_variable = domcom_guard_partner
            remove_variable = domcom_guard_suitor
        }
    }
}

# Nonfatal duel result.
domcom_guard.120 = {
    hidden = yes
    immediate = {
        if = {
            limit = { scope:sc_victor = root }
            domcom_household_guard_success_effect = yes
        }
        else = { domcom_household_guard_failure_effect = yes }
    }
}

# Chess/strategy-board result.
domcom_guard.121 = {
    hidden = yes
    immediate = {
        if = {
            limit = { scope:bg_victor = root }
            domcom_household_guard_success_effect = yes
        }
        else = { domcom_household_guard_failure_effect = yes }
    }
}

# The spouse accepted after the player lost/waived the confrontation. This is the point at which the
# Cucked state is legitimate and the player is explicitly told who and with whom.
domcom_guard.140 = {
    type = character_event
    title = domcom_guard.140.t
    desc = {
        first_valid = {
            triggered_desc = { trigger = { has_character_flag = domcom_last_cuck_stood_aside } desc = domcom_guard.140.desc.aside }
            desc = domcom_guard.140.desc
        }
    }
    theme = seduction
    trigger = {
        has_variable = domcom_last_cuck_patron
        has_variable = domcom_last_cuck_partner
        var:domcom_last_cuck_patron = { is_alive = yes }
        var:domcom_last_cuck_partner = { is_alive = yes }
    }
    immediate = {
        hidden_effect = {
            var:domcom_last_cuck_patron = { save_scope_as = domcom_guard_suitor }
            var:domcom_last_cuck_partner = { save_scope_as = domcom_guard_partner }
        }
    }
    left_portrait = { character = scope:domcom_guard_partner animation = shame }
    right_portrait = { character = scope:domcom_guard_suitor animation = personality_bold }
    option = {
        name = domcom_guard.140.a
        hidden_effect = { remove_character_flag = domcom_last_cuck_stood_aside }
    }
}

# Even after the defender loses, the partner can still refuse. No cuck state is created.
domcom_guard.141 = {
    type = character_event
    title = domcom_guard.141.t
    desc = domcom_guard.141.desc
    theme = seduction
    trigger = { exists = scope:domcom_guard_partner exists = scope:domcom_guard_suitor }
    left_portrait = { character = scope:domcom_guard_partner animation = personality_bold }
    right_portrait = { character = scope:domcom_guard_suitor animation = shame }
    option = { name = domcom_guard.141.a }
}

# Minigame invalidation never counts as a loss. Delay the pending attempt and try again later.
domcom_guard.190 = {
    hidden = yes
    immediate = {
        if = {
            limit = {
                has_variable = domcom_guard_partner
                var:domcom_guard_partner = { is_alive = yes has_variable = domcom_pending_ai_suitor }
            }
            var:domcom_guard_partner = { trigger_event = { id = domcom_guard.105 days = 30 } }
        }
        if = { limit = { has_variable = domcom_guard_partner } remove_variable = domcom_guard_partner }
        if = { limit = { has_variable = domcom_guard_suitor } remove_variable = domcom_guard_suitor }
        remove_character_flag = domcom_guard_stood_aside
    }
}
