﻿# Marriage Planner candidate filter. Called from a spouse-candidate character while the subject and
# player are saved as scope:ddm_match_subject / scope:ddm_match_actor. The final legality check deliberately
# delegates gender/incest/recent-divorce/faith/culture rules to CK3's can_marry_character_trigger so other
# mods can redefine those rules without a DDM compatibility patch.

ddm_match_candidate_allowed_trigger = {
    is_alive = yes
    is_married = no
    is_betrothed = no
    is_concubine = no
    can_marry_trigger = yes
    this != scope:ddm_match_subject

    # The ruler's personal Best Match card is an adult spouse search; dynasty planning may recommend
    # minors whenever the active CK3/mod rules consider the pair valid for betrothal.
    trigger_if = {
        limit = { scope:ddm_match_subject = scope:ddm_match_actor }
        is_adult = yes
    }

    # Pair legality comes from the currently loaded CK3/mod marriage/betrothal rules, not a DDM reimplementation.
    can_marry_character_trigger = { CHARACTER = scope:ddm_match_subject }

    # Avoid Close Kin: distant same-dynasty members remain legal, but close/extended relatives are
    # excluded. Allow Dynasty: CK3's own faith/marriage rules decide. Pure-Blood Program: same dynasty
    # only, again subject to vanilla legality.
    trigger_if = {
        limit = { scope:ddm_match_actor.var:ddm_match_dynasty_mode = 0 }
        NOT = { is_close_or_extended_family_of = scope:ddm_match_subject }
    }
    trigger_if = {
        limit = { scope:ddm_match_actor.var:ddm_match_dynasty_mode = 2 }
        exists = dynasty
        dynasty = scope:ddm_match_subject.dynasty
    }
}




# Dominion family-control bridge. DDM does not depend on Dominion: when those flags/variables are absent,
# this simply evaluates false. A child of a character in full Dominion may be planned and directly arranged
# by that Patron, reflecting the political/familial control already represented by Owned.
ddm_match_subject_owned_family_control_trigger = {
    any_parent = {
        has_character_flag = domcom_owned_cuck
        has_variable = domcom_patron
        var:domcom_patron = scope:ddm_match_actor
    }
}


# Direct character-sheet/right-click family authority. Unlike the planner-only helper above, this trigger
# reads the live interaction actor scope so it can be used by a marriage interaction opened on the child.
ddm_patron_owned_child_marriage_trigger = {
    any_parent = {
        has_character_flag = domcom_owned_cuck
        has_variable = domcom_patron
        var:domcom_patron = scope:actor
    }
}

# Whether the player can legitimately arrange this dynasty member's marriage.  `matchmaker` is the
# normal fast path, but vanilla Arrange Marriage deliberately also exposes the player's own children
# when family/realm authority still applies (notably landed children, whose matchmaker is commonly
# themselves).  DDM mirrors that exception so the planner does not hide marriages CK3 itself allows.
ddm_match_subject_player_arrangeable_trigger = {
    OR = {
        this = scope:ddm_match_actor
        matchmaker = scope:ddm_match_actor
        AND = {
            is_child_of = scope:ddm_match_actor
            OR = {
                top_liege = scope:ddm_match_actor
                house.house_head = scope:ddm_match_actor
                dynasty.dynast = scope:ddm_match_actor
            }
        }
        ddm_match_subject_owned_family_control_trigger = yes
    }
}

# Recommendation cache. A spouse-candidate scan is the expensive part of the planner, and sorting never
# changes a score — only which dynasts sit on the page. A dynast's cached recommendations are therefore reused
# whenever it was computed by this planner, under the same marriage philosophy, the same kinship strategy
# and the same scan generation, and whenever every cached name (including Dynasty Plan fallbacks) is still available to marry. Refresh
# Explicit Refresh Marriage Planner raises the generation. If a full Dynasty Scan had already been completed,
# refresh immediately repopulates the whole dynasty so coverage does not collapse back to one page.
ddm_match_recommendations_current_trigger = {
    exists = var:ddm_match_planner_owner
    var:ddm_match_planner_owner = scope:ddm_match_actor
    exists = var:ddm_match_effective_policy
    exists = var:ddm_match_cache_generation
    exists = var:ddm_match_cache_policy
    exists = var:ddm_match_cache_dynasty_mode
    exists = scope:ddm_match_actor.var:ddm_match_scan_generation
    exists = scope:ddm_match_actor.var:ddm_match_dynasty_mode
    var:ddm_match_cache_generation = ddm_match_actor_scan_generation_value
    var:ddm_match_cache_policy = ddm_match_effective_policy_value
    var:ddm_match_cache_dynasty_mode = ddm_match_actor_dynasty_mode_value

    trigger_if = {
        limit = { exists = var:ddm_match_best }
        var:ddm_match_best = { is_alive = yes is_married = no is_betrothed = no }
    }
    trigger_if = {
        limit = { exists = var:ddm_match_second }
        var:ddm_match_second = { is_alive = yes is_married = no is_betrothed = no }
    }
    trigger_if = {
        limit = { exists = var:ddm_match_third }
        var:ddm_match_third = { is_alive = yes is_married = no is_betrothed = no }
    }
    trigger_if = {
        limit = { exists = var:ddm_match_plan_fourth }
        var:ddm_match_plan_fourth = { is_alive = yes is_married = no is_betrothed = no }
    }
    trigger_if = {
        limit = { exists = var:ddm_match_plan_fifth }
        var:ddm_match_plan_fifth = { is_alive = yes is_married = no is_betrothed = no }
    }
    trigger_if = {
        limit = { exists = var:ddm_match_plan_sixth }
        var:ddm_match_plan_sixth = { is_alive = yes is_married = no is_betrothed = no }
    }
    trigger_if = {
        limit = { exists = var:ddm_match_plan_seventh }
        var:ddm_match_plan_seventh = { is_alive = yes is_married = no is_betrothed = no }
    }
    trigger_if = {
        limit = { exists = var:ddm_match_plan_eighth }
        var:ddm_match_plan_eighth = { is_alive = yes is_married = no is_betrothed = no }
    }
}

# Direct-finalization is deliberately stricter than recommendation. It is used for bulk automation and
# the "Direct" pair sort. Row buttons can still open CK3's normal proposal for legal external matches.
ddm_match_cached_pair_autofinalizable_trigger = {
    exists = var:ddm_match_planner_owner
    exists = var:ddm_match_pair_candidate
    has_variable = ddm_match_pair_managed
    is_adult = yes
    matchmaker = var:ddm_match_planner_owner
}

# Whether the row's Arrange Match button can do anything at all. Adult pairs controlled by the player
# can still be finalized immediately. When either side is a minor, DDM opens CK3's own Arrange Marriage
# interaction with the recommended pair preselected so vanilla/mod betrothal rules and terms remain authoritative.
# The managed/external flags are recorded during the scan so this stays a cheap UI check.
ddm_match_selection_actionable_trigger = {
    exists = var:ddm_match_planner_owner
    exists = var:ddm_match_sel
    is_alive = yes
    is_married = no
    is_betrothed = no
    OR = {
        this = var:ddm_match_planner_owner
        matchmaker = var:ddm_match_planner_owner
        AND = {
            is_child_of = var:ddm_match_planner_owner
            OR = {
                top_liege = var:ddm_match_planner_owner
                house.house_head = var:ddm_match_planner_owner
                dynasty.dynast = var:ddm_match_planner_owner
            }
        }
        AND = {
            any_parent = {
                has_character_flag = domcom_owned_cuck
                has_variable = domcom_patron
                var:domcom_patron = root.var:ddm_match_planner_owner
            }
        }
    }
    var:ddm_match_sel = {
        is_alive = yes
        is_married = no
        is_betrothed = no
    }
    OR = {
        has_variable = ddm_match_sel_managed
        has_variable = ddm_match_sel_external
    }
}
