﻿# Relationship and Alpha support effects. domcom_obedience remains the save-safe internal key.

# The numbered Slave identity is a terminal dynastic submission state. Pet/Devotion remains the
# relationship route that can create it; these effects are keyed to the Slave identity itself.
domcom_apply_absolute_slave_state_effect = {
    save_scope_as = domcom_absolute_slave_subject
    add_character_flag = domcom_dynasty_slave

    if = {
        limit = { NOT = { has_character_modifier = domcom_absolute_slave_modifier } }
        add_character_modifier = domcom_absolute_slave_modifier
    }

    if = {
        limit = {
            has_variable = domcom_pet_patron
            var:domcom_pet_patron = { exists = dynasty }
        }
        var:domcom_pet_patron = { save_scope_as = domcom_absolute_slave_patron }
    }
    else_if = {
        limit = { exists = scope:domcom_tantalize_patron }
        scope:domcom_tantalize_patron = { save_scope_as = domcom_absolute_slave_patron }
    }

    if = {
        limit = { exists = scope:domcom_absolute_slave_patron }
        add_opinion = {
            target = scope:domcom_absolute_slave_patron
            modifier = domcom_pet_obedience_opinion
        }

        if = {
            limit = {
                NOT = { has_variable = domcom_pet_dynasty }
                exists = scope:domcom_absolute_slave_patron.dynasty
            }
            set_variable = {
                name = domcom_pet_dynasty
                value = scope:domcom_absolute_slave_patron.dynasty
            }
        }

        if = {
            limit = {
                exists = scope:domcom_absolute_slave_patron.dynasty
                NOT = { has_character_flag = domcom_slave_dynasty_loyalty_initialized }
            }
            scope:domcom_absolute_slave_patron.dynasty = {
                every_dynasty_member = {
                    limit = { is_alive = yes }
                    save_temporary_scope_as = domcom_absolute_slave_dynasty_member
                    scope:domcom_absolute_slave_subject = {
                        add_opinion = {
                            target = scope:domcom_absolute_slave_dynasty_member
                            modifier = domcom_slave_dynasty_loyalty_opinion
                        }
                    }
                }
            }
            add_character_flag = domcom_slave_dynasty_loyalty_initialized
        }
    }

    if = {
        limit = { NOT = { has_character_flag = domcom_slave_family_network_initialized } }
        every_close_or_extended_family_member = {
            limit = {
                is_alive = yes
                NOT = { has_character_modifier = domcom_slave_family_compromised_modifier }
            }
            add_character_modifier = domcom_slave_family_compromised_modifier
        }
        add_character_flag = domcom_slave_family_network_initialized
    }
}


domcom_clear_devotion_stage_modifiers_effect = {
    if = { limit = { has_character_modifier = domcom_obedience_interested_modifier } remove_character_modifier = domcom_obedience_interested_modifier }
    if = { limit = { has_character_modifier = domcom_obedience_tempted_modifier } remove_character_modifier = domcom_obedience_tempted_modifier }
    if = { limit = { has_character_modifier = domcom_obedience_attached_modifier } remove_character_modifier = domcom_obedience_attached_modifier }
    if = { limit = { has_character_modifier = domcom_obedience_submissive_modifier } remove_character_modifier = domcom_obedience_submissive_modifier }
    if = { limit = { has_character_modifier = domcom_obedience_devoted_modifier } remove_character_modifier = domcom_obedience_devoted_modifier }
    if = { limit = { has_character_modifier = domcom_devotion_ready_modifier } remove_character_modifier = domcom_devotion_ready_modifier }
}

# Repairs old Pets without changing a valid existing Pet number.
domcom_repair_pet_identity_effect = {
    save_scope_as = domcom_pet_identity_subject
    if = {
        limit = { has_character_flag = domcom_pet }
        if = { limit = { NOT = { has_variable = domcom_obedience } } set_variable = { name = domcom_obedience value = 1000 } }
        if = { limit = { var:domcom_obedience < 1000 } set_variable = { name = domcom_obedience value = 1000 } }
        if = { limit = { NOT = { has_character_modifier = domcom_pet_modifier } } add_character_modifier = domcom_pet_modifier }

        if = {
            limit = { NOT = { has_variable = domcom_pet_patron } has_variable = domcom_obedience_patron }
            set_variable = { name = domcom_pet_patron value = var:domcom_obedience_patron }
        }
        if = {
            limit = { NOT = { has_variable = domcom_obedience_patron } has_variable = domcom_pet_patron }
            set_variable = { name = domcom_obedience_patron value = var:domcom_pet_patron }
        }

        if = {
            limit = {
                has_variable = domcom_pet_patron
                var:domcom_pet_patron = { is_alive = yes exists = dynasty }
            }
            var:domcom_pet_patron = {
                dynasty = { save_scope_as = domcom_pet_dynasty_scope }
            }
            if = { limit = { NOT = { has_variable = domcom_pet_dynasty } } set_variable = { name = domcom_pet_dynasty value = scope:domcom_pet_dynasty_scope } }

            scope:domcom_pet_dynasty_scope = {
                if = { limit = { NOT = { has_variable = domcom_pet_counter } } set_variable = { name = domcom_pet_counter value = 0 } }
            }
            if = {
                limit = { has_variable = domcom_pet_number }
                save_scope_value_as = { name = domcom_existing_pet_number value = var:domcom_pet_number }
                scope:domcom_pet_dynasty_scope = {
                    if = {
                        limit = { var:domcom_pet_counter < scope:domcom_existing_pet_number }
                        set_variable = { name = domcom_pet_counter value = scope:domcom_existing_pet_number }
                    }
                }
            }
            if = {
                limit = { NOT = { has_variable = domcom_pet_number } }
                scope:domcom_pet_dynasty_scope = { change_variable = { name = domcom_pet_counter add = 1 } }
                set_variable = { name = domcom_pet_number value = scope:domcom_pet_dynasty_scope.var:domcom_pet_counter }
            }
            if = { limit = { NOT = { has_nickname = nick_domcom_dynasty_slave } } give_nickname = nick_domcom_dynasty_slave }
        }
        else_if = {
            limit = { has_variable = domcom_pet_patron }
            var:domcom_pet_patron = { save_scope_as = domcom_pet_repair_patron }
            scope:domcom_pet_repair_patron = {
                if = { limit = { NOT = { has_variable = domcom_personal_pet_counter } } set_variable = { name = domcom_personal_pet_counter value = 0 } }
            }
            if = {
                limit = { has_variable = domcom_pet_number }
                save_scope_value_as = { name = domcom_existing_pet_number value = var:domcom_pet_number }
                scope:domcom_pet_repair_patron = {
                    if = {
                        limit = { var:domcom_personal_pet_counter < scope:domcom_existing_pet_number }
                        set_variable = { name = domcom_personal_pet_counter value = scope:domcom_existing_pet_number }
                    }
                }
            }
            if = {
                limit = { NOT = { has_variable = domcom_pet_number } }
                scope:domcom_pet_repair_patron = { change_variable = { name = domcom_personal_pet_counter add = 1 } }
                set_variable = { name = domcom_pet_number value = scope:domcom_pet_repair_patron.var:domcom_personal_pet_counter }
            }
            if = { limit = { NOT = { has_nickname = nick_domcom_personal_slave } } give_nickname = nick_domcom_personal_slave }
        }

        # Registry membership is independent of the cached Pet flag. Older saves can retain the terminal
        # identity while silently losing the Patron-local list entry, so reassert it idempotently.
        if = {
            limit = { has_variable = domcom_pet_patron var:domcom_pet_patron = { is_alive = yes } }
            var:domcom_pet_patron = {
                if = {
                    limit = {
                        OR = {
                            NOT = { has_variable_list = domcom_pet_registry }
                            NOT = { any_in_list = { variable = domcom_pet_registry this = scope:domcom_pet_identity_subject } }
                        }
                    }
                    add_to_variable_list = { name = domcom_pet_registry target = scope:domcom_pet_identity_subject }
                }
            }
        }

        # Run after Patron/dynasty/number/registry repair so old saves receive the full state in one pass.
        domcom_apply_absolute_slave_state_effect = yes

        # Retroactively apply the one-generation child obligation for Slaves created by older versions.
        if = {
            limit = { has_variable = domcom_pet_patron var:domcom_pet_patron = { is_alive = yes } }
            var:domcom_pet_patron = { save_scope_as = domcom_inherited_submission_patron }
            every_child = {
                limit = { is_alive = yes NOT = { this = scope:domcom_inherited_submission_patron } }
                domcom_apply_first_generation_slave_obligation_effect = yes
            }
        }
    }
}

# Run from a Patron. Normal upkeep stays bounded to relationship registries.
domcom_repair_pet_registry_for_patron_effect = {
    save_scope_as = domcom_pet_repair_patron

    # Direct relationship repair comes first so an old save with a Lover/Soulmate already sitting at
    # 1000 Devotion terminalizes immediately when the Ledger is refreshed, even if an older build never
    # placed that character in Dominion's Tantalized registry. This remains bounded to the Patron's own
    # relationship graph rather than scanning the world.
    every_relation = {
        type = lover
        type = soulmate
        limit = {
            is_alive = yes
            age >= 18
            has_variable = domcom_obedience
            var:domcom_obedience >= 1000
            OR = {
                NOT = { has_variable = domcom_obedience_patron }
                var:domcom_obedience_patron = scope:domcom_pet_repair_patron
            }
        }
        save_temporary_scope_as = domcom_terminal_relation_subject
        if = { limit = { NOT = { has_variable = domcom_obedience_patron } } set_variable = { name = domcom_obedience_patron value = scope:domcom_pet_repair_patron } }
        if = { limit = { NOT = { has_variable = domcom_pet_patron } } set_variable = { name = domcom_pet_patron value = scope:domcom_pet_repair_patron } }
        if = {
            limit = { NOT = { has_character_flag = domcom_tantalized_registered } }
            add_character_flag = domcom_tantalized_registered
            scope:domcom_pet_repair_patron = { add_to_variable_list = { name = domcom_tantalized_registry target = scope:domcom_terminal_relation_subject } }
        }
        scope:domcom_pet_repair_patron = { save_scope_as = domcom_tantalize_patron }
        domcom_refresh_obedience_stage_effect = yes
    }

    # Already-numbered legacy subjects: repair identity and permanent state.
    if = {
        limit = { has_variable_list = domcom_pet_registry }
        every_in_list = {
            variable = domcom_pet_registry
            limit = {
                is_alive = yes
                has_character_flag = domcom_pet
                OR = {
                    AND = { has_variable = domcom_pet_patron var:domcom_pet_patron = scope:domcom_pet_repair_patron }
                    AND = {
                        NOT = { has_variable = domcom_pet_patron }
                        OR = {
                            NOT = { has_variable = domcom_obedience_patron }
                            var:domcom_obedience_patron = scope:domcom_pet_repair_patron
                        }
                    }
                }
            }
            if = { limit = { NOT = { has_variable = domcom_pet_patron } } set_variable = { name = domcom_pet_patron value = scope:domcom_pet_repair_patron } }
            if = { limit = { NOT = { has_variable = domcom_obedience_patron } } set_variable = { name = domcom_obedience_patron value = scope:domcom_pet_repair_patron } }
            domcom_repair_pet_identity_effect = yes
        }
    }

    # Existing saves may contain 1000/1000 Devotion without the old terminal flag. A relationship registry
    # owned by this Patron is enough to reconstruct the missing Patron pointer and terminalize safely.
    if = {
        limit = { has_variable_list = domcom_tantalized_registry }
        every_in_list = {
            variable = domcom_tantalized_registry
            limit = {
                is_alive = yes
                has_variable = domcom_obedience
                var:domcom_obedience >= 1000
                OR = {
                    NOT = { has_variable = domcom_obedience_patron }
                    var:domcom_obedience_patron = scope:domcom_pet_repair_patron
                }
            }
            if = { limit = { NOT = { has_variable = domcom_obedience_patron } } set_variable = { name = domcom_obedience_patron value = scope:domcom_pet_repair_patron } }
            if = { limit = { NOT = { has_variable = domcom_pet_patron } } set_variable = { name = domcom_pet_patron value = scope:domcom_pet_repair_patron } }
            scope:domcom_pet_repair_patron = { save_scope_as = domcom_tantalize_patron }
            domcom_refresh_obedience_stage_effect = yes
        }
    }

    if = {
        limit = { has_variable_list = domcom_intimate_registry }
        every_in_list = {
            variable = domcom_intimate_registry
            limit = {
                is_alive = yes
                has_variable = domcom_obedience
                var:domcom_obedience >= 1000
                OR = {
                    NOT = { has_variable = domcom_obedience_patron }
                    var:domcom_obedience_patron = scope:domcom_pet_repair_patron
                }
            }
            if = { limit = { NOT = { has_variable = domcom_obedience_patron } } set_variable = { name = domcom_obedience_patron value = scope:domcom_pet_repair_patron } }
            if = { limit = { NOT = { has_variable = domcom_pet_patron } } set_variable = { name = domcom_pet_patron value = scope:domcom_pet_repair_patron } }
            scope:domcom_pet_repair_patron = { save_scope_as = domcom_tantalize_patron }
            domcom_refresh_obedience_stage_effect = yes
        }
    }
}

# Existing Lovers/Soulmates enter Devotion naturally. Active Deepen Communion is much faster.
domcom_devotion_monthly_patron_effect = {
    save_scope_as = domcom_devotion_patron
    every_relation = {
        type = lover
        type = soulmate
        limit = {
            is_alive = yes
            age >= 18
            OR = {
                NOT = { has_variable = domcom_obedience_patron }
                var:domcom_obedience_patron = scope:domcom_devotion_patron
            }
        }
        save_temporary_scope_as = domcom_devotion_partner

        if = { limit = { NOT = { has_variable = domcom_obedience_patron } } set_variable = { name = domcom_obedience_patron value = scope:domcom_devotion_patron } }
        if = { limit = { NOT = { has_variable = domcom_obedience } } set_variable = { name = domcom_obedience value = 0 } }
        if = {
            limit = { NOT = { has_character_flag = domcom_tantalized_registered } }
            add_character_flag = domcom_tantalized_registered
            scope:domcom_devotion_patron = { add_to_variable_list = { name = domcom_tantalized_registry target = scope:domcom_devotion_partner } }
        }

        if = {
            limit = { NOT = { has_character_flag = domcom_pet } var:domcom_obedience < 1000 }
            # 2.0: passive Devotion must be capable of producing terminal subjects within an ordinary
            # campaign. Lovers now gain 4/month and Soulmates 7/month. Established Dominion leverage
            # adds modest acceleration without replacing active Deepen Communion.
            if = { limit = { has_relation_soulmate = scope:domcom_devotion_patron } change_variable = { name = domcom_obedience add = 7 } }
            else = { change_variable = { name = domcom_obedience add = 4 } }
            if = {
                limit = {
                    has_character_flag = domcom_dominated
                    has_variable = domcom_dominance_patron
                    var:domcom_dominance_patron = scope:domcom_devotion_patron
                }
                change_variable = { name = domcom_obedience add = 2 }
            }
            if = {
                limit = {
                    any_spouse = {
                        is_alive = yes
                        has_character_flag = domcom_cucked
                        OR = {
                            AND = { has_variable = domcom_cuck_patron var:domcom_cuck_patron = scope:domcom_devotion_patron }
                            AND = { has_variable = domcom_patron var:domcom_patron = scope:domcom_devotion_patron }
                            any_in_list = { variable = domcom_cuck_patrons this = scope:domcom_devotion_patron }
                        }
                    }
                }
                change_variable = { name = domcom_obedience add = 2 }
            }
            if = { limit = { var:domcom_obedience > 1000 } set_variable = { name = domcom_obedience value = 1000 } }
            domcom_refresh_obedience_stage_effect = yes
        }
        else_if = {
            limit = { NOT = { has_character_flag = domcom_pet } var:domcom_obedience >= 1000 }
            domcom_refresh_obedience_stage_effect = yes
        }

        if = {
            limit = { var:domcom_obedience >= 400 }
            random = {
                chance = 8
                every_secret = { reveal_to = scope:domcom_devotion_patron }
            }
        }
        if = {
            limit = { var:domcom_obedience >= 600 }
            random = {
                chance = 6
                every_spouse = { every_secret = { reveal_to = scope:domcom_devotion_patron } }
                if = { limit = { exists = liege } liege = { every_secret = { reveal_to = scope:domcom_devotion_patron } } }
            }
        }
        if = {
            limit = { var:domcom_obedience >= 800 }
            scope:domcom_devotion_patron = {
                if = { limit = { has_character_modifier = domcom_devoted_network_modifier } remove_character_modifier = domcom_devoted_network_modifier }
                add_character_modifier = { modifier = domcom_devoted_network_modifier days = 35 }
            }
        }

        every_spouse = {
            limit = {
                is_alive = yes
                has_character_flag = domcom_cucked
                OR = {
                    AND = { has_variable = domcom_cuck_patron var:domcom_cuck_patron = scope:domcom_devotion_patron }
                    AND = { has_variable = domcom_patron var:domcom_patron = scope:domcom_devotion_patron }
                    any_in_list = { variable = domcom_cuck_patrons this = scope:domcom_devotion_patron }
                }
            }
            if = { limit = { NOT = { has_variable = domcom_cuck_xp } } set_variable = { name = domcom_cuck_xp value = 100 } }
            if = { limit = { scope:domcom_devotion_partner = { has_character_flag = domcom_pet } } change_variable = { name = domcom_cuck_xp add = 8 } }
            else_if = { limit = { scope:domcom_devotion_partner = { var:domcom_obedience >= 800 } } change_variable = { name = domcom_cuck_xp add = 5 } }
            else_if = { limit = { scope:domcom_devotion_partner = { var:domcom_obedience >= 400 } } change_variable = { name = domcom_cuck_xp add = 3 } }
            else = { change_variable = { name = domcom_cuck_xp add = 1 } }
            if = { limit = { var:domcom_cuck_xp > 1000 } set_variable = { name = domcom_cuck_xp value = 1000 } }
            domcom_refresh_cuck_stage_effect = yes
            domcom_check_final_collapse_effect = yes
        }
    }
}

# AI Patrons actively deepen one established intimate bond on the yearly pulse. This deliberately
# bypasses the player-facing interaction/event chain so AI can create organic terminal Devotion without
# event spam. Passive monthly progression still does most of the work.
domcom_ai_deepen_devotion_effect = {
    save_scope_as = domcom_ai_devotion_patron
    every_relation = {
        type = lover
        type = soulmate
        limit = {
            is_alive = yes
            age >= 18
            NOT = { has_character_flag = domcom_pet }
            OR = {
                NOT = { has_variable = domcom_obedience_patron }
                var:domcom_obedience_patron = scope:domcom_ai_devotion_patron
            }
            OR = {
                NOT = { has_variable = domcom_obedience }
                var:domcom_obedience < 1000
            }
        }
        add_to_temporary_list = domcom_ai_devotion_candidates
    }
    random_in_list = {
        list = domcom_ai_devotion_candidates
        save_temporary_scope_as = domcom_ai_devotion_partner
        if = { limit = { NOT = { has_variable = domcom_obedience_patron } } set_variable = { name = domcom_obedience_patron value = scope:domcom_ai_devotion_patron } }
        if = { limit = { NOT = { has_variable = domcom_obedience } } set_variable = { name = domcom_obedience value = 0 } }
        if = { limit = { has_relation_soulmate = scope:domcom_ai_devotion_patron } change_variable = { name = domcom_obedience add = 120 } }
        else = { change_variable = { name = domcom_obedience add = 80 } }
        if = {
            limit = {
                OR = {
                    AND = { has_character_flag = domcom_dominated has_variable = domcom_dominance_patron var:domcom_dominance_patron = scope:domcom_ai_devotion_patron }
                    any_spouse = {
                        has_character_flag = domcom_cucked
                        OR = {
                            AND = { has_variable = domcom_cuck_patron var:domcom_cuck_patron = scope:domcom_ai_devotion_patron }
                            AND = { has_variable = domcom_patron var:domcom_patron = scope:domcom_ai_devotion_patron }
                        }
                    }
                }
            }
            change_variable = { name = domcom_obedience add = 30 }
        }
        if = { limit = { var:domcom_obedience > 1000 } set_variable = { name = domcom_obedience value = 1000 } }
        domcom_refresh_obedience_stage_effect = yes
    }
}

# Applies one completed Deepen Communion session from the Patron/root.
domcom_finish_deepen_communion_effect = {
    if = {
        limit = { has_variable = domcom_communion_event_target var:domcom_communion_event_target = { is_alive = yes } }
        var:domcom_communion_event_target = { save_scope_as = domcom_deepen_target }
        if = {
            limit = { var:domcom_communion_event_score >= 7 }
            scope:domcom_deepen_target = {
                change_variable = { name = domcom_obedience add = 160 }
                add_opinion = { target = root modifier = domcom_deepen_communion_good_opinion }
            }
        }
        else_if = {
            limit = { var:domcom_communion_event_score >= 4 }
            scope:domcom_deepen_target = {
                change_variable = { name = domcom_obedience add = 110 }
                add_opinion = { target = root modifier = domcom_deepen_communion_good_opinion }
            }
        }
        else_if = {
            limit = { var:domcom_communion_event_score >= 1 }
            scope:domcom_deepen_target = { change_variable = { name = domcom_obedience add = 60 } }
        }
        else = {
            scope:domcom_deepen_target = {
                change_variable = { name = domcom_obedience subtract = 40 }
                add_opinion = { target = root modifier = domcom_deepen_communion_bad_opinion }
            }
        }
        scope:domcom_deepen_target = {
            if = { limit = { has_relation_soulmate = root NOT = { has_character_flag = domcom_pet } } change_variable = { name = domcom_obedience add = 20 } }
            if = { limit = { var:domcom_obedience > 1000 } set_variable = { name = domcom_obedience value = 1000 } }
            if = { limit = { var:domcom_obedience < 0 } set_variable = { name = domcom_obedience value = 0 } }
            domcom_refresh_obedience_stage_effect = yes
        }
    }
}

domcom_cleanup_communion_event_effect = {
    if = {
        limit = { has_variable = domcom_communion_event_target }
        var:domcom_communion_event_target = {
            if = { limit = { has_variable = domcom_communion_event_compatibility } remove_variable = domcom_communion_event_compatibility }
        }
        remove_variable = domcom_communion_event_target
    }
    if = { limit = { has_variable = domcom_communion_related } remove_variable = domcom_communion_related }
    if = { limit = { has_variable = domcom_communion_event_score } remove_variable = domcom_communion_event_score }
    remove_character_flag = domcom_devotion_test_passed
    remove_character_flag = domcom_pet_claim_accepted
}

# Run from an Alpha target with scope:domcom_alpha_patron saved.
domcom_alpha_event_success_effect = {
    save_scope_as = domcom_alpha_target
    scope:domcom_alpha_patron = { save_scope_as = domcom_owner }

    if = {
        limit = { has_character_flag = domcom_alpha_event_has_cuck_evidence }
        if = { limit = { NOT = { has_character_flag = domcom_cucked } } domcom_register_cuck_generic_effect = yes }
        if = {
            limit = {
                has_character_flag = domcom_dominated
                has_variable = domcom_dominance_patron
                var:domcom_dominance_patron = scope:domcom_alpha_patron
            }
            remove_character_flag = domcom_dominated
            remove_character_modifier = domcom_dominated_modifier
            remove_variable = domcom_dominance_patron
        }
        if = {
            limit = {
                NOT = {
                    AND = {
                        has_character_flag = domcom_owned_cuck
                        has_variable = domcom_patron
                        var:domcom_patron = scope:domcom_alpha_patron
                    }
                }
            }
            domcom_own_cuck_generic_effect = yes
        }
        if = { limit = { NOT = { has_variable = domcom_cuck_xp } } set_variable = { name = domcom_cuck_xp value = 0 } }
        change_variable = { name = domcom_cuck_xp add = 100 }
        if = { limit = { var:domcom_cuck_xp > 1000 } set_variable = { name = domcom_cuck_xp value = 1000 } }
        domcom_refresh_cuck_stage_effect = yes
        domcom_check_final_collapse_effect = yes
    }
    else = { domcom_apply_dominated_effect = yes }

    if = {
        limit = {
            OR = {
                AND = { highest_held_title_tier = tier_county scope:domcom_alpha_patron = { is_landed = no } }
                AND = { highest_held_title_tier = tier_duchy scope:domcom_alpha_patron = { OR = { is_landed = no highest_held_title_tier = tier_county } } }
                AND = { highest_held_title_tier = tier_kingdom scope:domcom_alpha_patron = { OR = { is_landed = no highest_held_title_tier = tier_county highest_held_title_tier = tier_duchy } } }
                AND = { highest_held_title_tier >= tier_empire scope:domcom_alpha_patron = { NOT = { highest_held_title_tier >= tier_empire } } }
            }
        }
        scope:domcom_alpha_patron = {
            add_prestige = 300
            add_character_modifier = { modifier = domcom_reverse_alpha_victory_modifier years = 10 }
            change_variable = { name = domcom_reverse_alpha_victories add = 1 }
            if = {
                limit = {
                    OR = {
                        AND = { scope:domcom_alpha_target = { highest_held_title_tier = tier_duchy } is_landed = no }
                        AND = { scope:domcom_alpha_target = { highest_held_title_tier = tier_kingdom } OR = { is_landed = no highest_held_title_tier = tier_county } }
                        AND = { scope:domcom_alpha_target = { highest_held_title_tier >= tier_empire } OR = { is_landed = no highest_held_title_tier = tier_county highest_held_title_tier = tier_duchy } }
                    }
                }
                add_prestige = 300
                add_character_modifier = { modifier = domcom_reverse_alpha_major_upset_modifier years = 10 }
            }
            if = {
                limit = {
                    OR = {
                        AND = { scope:domcom_alpha_target = { highest_held_title_tier = tier_kingdom } is_landed = no }
                        AND = { scope:domcom_alpha_target = { highest_held_title_tier >= tier_empire } OR = { is_landed = no highest_held_title_tier = tier_county } }
                    }
                }
                add_prestige = 400
                add_character_modifier = { modifier = domcom_reverse_alpha_legendary_upset_modifier years = 10 }
            }
        }
        add_opinion = { target = scope:domcom_alpha_patron modifier = domcom_reverse_alpha_shock_opinion }
    }

    scope:domcom_alpha_patron = {
        change_variable = { name = domcom_alpha_successes add = 1 }
        domcom_rep_gain_breaker_small_effect = yes
        send_interface_toast = {
            type = event_toast_effect_good
            title = domcom_alpha_check_success_toast
            left_icon = scope:domcom_alpha_patron
            right_icon = scope:domcom_alpha_target
        }
    }
}

domcom_alpha_event_failure_effect = {
    save_scope_as = domcom_alpha_target
    scope:domcom_alpha_patron = { save_scope_as = domcom_owner domcom_dominion_rejection_stress_effect = yes }
    if = {
        limit = { has_character_flag = domcom_alpha_event_has_cuck_evidence }
        if = { limit = { NOT = { has_character_flag = domcom_cucked } } domcom_register_cuck_generic_effect = yes }
        if = { limit = { NOT = { has_variable = domcom_cuck_xp } } set_variable = { name = domcom_cuck_xp value = 100 } }
        change_variable = { name = domcom_cuck_xp subtract = 50 }
        if = { limit = { var:domcom_cuck_xp < 100 } set_variable = { name = domcom_cuck_xp value = 100 } }
        domcom_refresh_cuck_stage_effect = yes
    }
    add_opinion = { target = scope:domcom_alpha_patron modifier = domcom_resisted_alpha_opinion }
    scope:domcom_alpha_patron = {
        change_variable = { name = domcom_alpha_failures add = 1 }
        domcom_rep_alpha_failure_effect = yes
        send_interface_toast = {
            type = event_toast_effect_bad
            title = domcom_alpha_check_failure_toast
            left_icon = scope:domcom_alpha_patron
            right_icon = scope:domcom_alpha_target
        }
    }
}

domcom_alpha_event_cleanup_effect = {
    if = {
        limit = { has_variable = domcom_alpha_event_target }
        var:domcom_alpha_event_target = {
            if = { limit = { has_variable = domcom_alpha_event_base_chance } remove_variable = domcom_alpha_event_base_chance }
            remove_character_flag = domcom_alpha_event_has_cuck_evidence
        }
        remove_variable = domcom_alpha_event_target
    }
    if = { limit = { has_variable = domcom_alpha_event_score } remove_variable = domcom_alpha_event_score }
    remove_character_flag = domcom_alpha_event_succeeded
    remove_character_flag = domcom_alpha_event_failed
    remove_character_flag = domcom_alpha_counter_escalation
    remove_character_flag = domcom_alpha_fatal_escalation
}
