﻿
domcom_assign_attraction_preferences_effect = {
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_body } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_body value = flag:none } }
            12 = { set_variable = { name = domcom_pref_body value = flag:lean_mild } }
            12 = { set_variable = { name = domcom_pref_body value = flag:average_mild } }
            11 = { set_variable = { name = domcom_pref_body value = flag:heavy_mild } }
            7 = { set_variable = { name = domcom_pref_body value = flag:lean_strong } }
            7 = { set_variable = { name = domcom_pref_body value = flag:average_strong } }
            6 = { set_variable = { name = domcom_pref_body value = flag:heavy_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_age } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_age value = flag:none } }
            12 = { set_variable = { name = domcom_pref_age value = flag:young_mild } }
            12 = { set_variable = { name = domcom_pref_age value = flag:prime_mild } }
            11 = { set_variable = { name = domcom_pref_age value = flag:mature_mild } }
            7 = { set_variable = { name = domcom_pref_age value = flag:young_strong } }
            7 = { set_variable = { name = domcom_pref_age value = flag:prime_strong } }
            6 = { set_variable = { name = domcom_pref_age value = flag:mature_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_culture } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_culture value = flag:none } }
            18 = { set_variable = { name = domcom_pref_culture value = flag:same_mild } }
            17 = { set_variable = { name = domcom_pref_culture value = flag:different_mild } }
            10 = { set_variable = { name = domcom_pref_culture value = flag:same_strong } }
            10 = { set_variable = { name = domcom_pref_culture value = flag:different_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_heritage } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_heritage value = flag:none } }
            18 = { set_variable = { name = domcom_pref_heritage value = flag:same_mild } }
            17 = { set_variable = { name = domcom_pref_heritage value = flag:different_mild } }
            10 = { set_variable = { name = domcom_pref_heritage value = flag:same_strong } }
            10 = { set_variable = { name = domcom_pref_heritage value = flag:different_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_beauty } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_beauty value = flag:none } }
            12 = { set_variable = { name = domcom_pref_beauty value = flag:beautiful_mild } }
            12 = { set_variable = { name = domcom_pref_beauty value = flag:plain_mild } }
            11 = { set_variable = { name = domcom_pref_beauty value = flag:unconventional_mild } }
            7 = { set_variable = { name = domcom_pref_beauty value = flag:beautiful_strong } }
            7 = { set_variable = { name = domcom_pref_beauty value = flag:plain_strong } }
            6 = { set_variable = { name = domcom_pref_beauty value = flag:unconventional_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_intellect } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_intellect value = flag:none } }
            12 = { set_variable = { name = domcom_pref_intellect value = flag:brilliant_mild } }
            12 = { set_variable = { name = domcom_pref_intellect value = flag:ordinary_mild } }
            11 = { set_variable = { name = domcom_pref_intellect value = flag:simple_mild } }
            7 = { set_variable = { name = domcom_pref_intellect value = flag:brilliant_strong } }
            7 = { set_variable = { name = domcom_pref_intellect value = flag:ordinary_strong } }
            6 = { set_variable = { name = domcom_pref_intellect value = flag:simple_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_prowess } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_prowess value = flag:none } }
            12 = { set_variable = { name = domcom_pref_prowess value = flag:low_mild } }
            12 = { set_variable = { name = domcom_pref_prowess value = flag:balanced_mild } }
            11 = { set_variable = { name = domcom_pref_prowess value = flag:high_mild } }
            7 = { set_variable = { name = domcom_pref_prowess value = flag:low_strong } }
            7 = { set_variable = { name = domcom_pref_prowess value = flag:balanced_strong } }
            6 = { set_variable = { name = domcom_pref_prowess value = flag:high_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_status } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_status value = flag:none } }
            12 = { set_variable = { name = domcom_pref_status value = flag:unlanded_mild } }
            12 = { set_variable = { name = domcom_pref_status value = flag:landed_mild } }
            11 = { set_variable = { name = domcom_pref_status value = flag:royal_mild } }
            7 = { set_variable = { name = domcom_pref_status value = flag:unlanded_strong } }
            7 = { set_variable = { name = domcom_pref_status value = flag:landed_strong } }
            6 = { set_variable = { name = domcom_pref_status value = flag:royal_strong } }
        }
    }
    if = {
        limit = { age >= 18 NOT = { has_variable = domcom_pref_personality } }
        random_list = {
            45 = { set_variable = { name = domcom_pref_personality value = flag:none } }
            7 = { set_variable = { name = domcom_pref_personality value = flag:gentle_mild } }
            7 = { set_variable = { name = domcom_pref_personality value = flag:forceful_mild } }
            7 = { set_variable = { name = domcom_pref_personality value = flag:cunning_mild } }
            7 = { set_variable = { name = domcom_pref_personality value = flag:social_mild } }
            7 = { set_variable = { name = domcom_pref_personality value = flag:reserved_mild } }
            4 = { set_variable = { name = domcom_pref_personality value = flag:gentle_strong } }
            4 = { set_variable = { name = domcom_pref_personality value = flag:forceful_strong } }
            4 = { set_variable = { name = domcom_pref_personality value = flag:cunning_strong } }
            4 = { set_variable = { name = domcom_pref_personality value = flag:social_strong } }
            4 = { set_variable = { name = domcom_pref_personality value = flag:reserved_strong } }
        }
    }
}

# Full sexual-attraction profile. Physical data remains managed by the original profile code; the new
# preference categories are deliberately lazy.
domcom_initialize_attraction_profile_effect = {
    domcom_initialize_profile_effect = yes
    domcom_assign_attraction_preferences_effect = yes
}

# Add a full Carnalitas sex-evidence patron to this potential cuck and mirror the target onto the patron
# so AI patrons can later act on the evidence without a world scan. Requires scope:domcom_sex_patron.
domcom_record_full_sex_evidence_effect = {
    save_scope_as = domcom_sex_cuck_target
    # Absolute invariant: a character can never be recorded as their own cucker/evidence Patron.
    if = {
        limit = {
            exists = scope:domcom_sex_patron
            NOT = { this = scope:domcom_sex_patron }
            NOT = { is_consort_of = scope:domcom_sex_patron }
        }
        if = {
            limit = {
                NOT = {
                    any_in_list = {
                        variable = domcom_sex_evidence_patrons
                        this = scope:domcom_sex_patron
                    }
                }
            }
            add_to_variable_list = { name = domcom_sex_evidence_patrons target = scope:domcom_sex_patron }
        }
        scope:domcom_sex_patron = {
            if = {
                limit = {
                    NOT = {
                        any_in_list = {
                            variable = domcom_sex_cuck_targets
                            this = scope:domcom_sex_cuck_target
                        }
                    }
                }
                add_to_variable_list = { name = domcom_sex_cuck_targets target = scope:domcom_sex_cuck_target }
            }
        }
    }
}

# Ordinary-lover betrayal is remembered but is intentionally weaker than spouse, concubine/consort, or
# soulmate evidence. It can help an Alpha Check but does not by itself create the public Cucked state.
domcom_record_lover_betrayal_evidence_effect = {
    if = {
        limit = {
            exists = scope:domcom_sex_patron
            NOT = { this = scope:domcom_sex_patron }
            NOT = { is_consort_of = scope:domcom_sex_patron }
            NOT = {
                any_in_list = {
                    variable = domcom_betrayal_evidence_patrons
                    this = scope:domcom_sex_patron
                }
            }
        }
        add_to_variable_list = { name = domcom_betrayal_evidence_patrons target = scope:domcom_sex_patron }
    }
}

domcom_record_direct_dominion_sex_effect = {
    save_scope_as = domcom_direct_sex_partner

    # Hard stop before any evidence code. Sex between lawful consorts is never cuckoldry, regardless of
    # plural-marriage layout, Invite-to-Bed style, or which secondary hook called this effect.
    if = {
        limit = {
            exists = scope:domcom_direct_sex_patron
            NOT = { this = scope:domcom_direct_sex_patron }
            NOT = { is_consort_of = scope:domcom_direct_sex_patron }
        }

        # Direction A: the direct sex partner's spouse/consort/soulmate is betrayed by the Patron.
        scope:domcom_direct_sex_patron = { save_scope_as = domcom_sex_patron }
        every_consort = {
            limit = {
                is_alive = yes age >= 18
                NOT = { this = scope:domcom_direct_sex_patron }
            }
            domcom_record_full_sex_evidence_effect = yes
            scope:domcom_direct_sex_patron = { save_scope_as = domcom_owner }
            domcom_register_cuck_generic_effect = yes
        }
        every_relation = {
            type = soulmate
            limit = {
                is_alive = yes age >= 18
                NOT = { this = scope:domcom_direct_sex_patron }
                NOT = { is_consort_of = scope:domcom_direct_sex_partner }
            }
            domcom_record_full_sex_evidence_effect = yes
            scope:domcom_direct_sex_patron = { save_scope_as = domcom_owner }
            domcom_register_cuck_generic_effect = yes
        }
        every_relation = {
            type = lover
            limit = {
                is_alive = yes age >= 18
                NOT = { this = scope:domcom_direct_sex_patron }
                NOT = { is_consort_of = scope:domcom_direct_sex_partner }
                NOT = { has_relation_soulmate = scope:domcom_direct_sex_partner }
            }
            domcom_record_lover_betrayal_evidence_effect = yes
        }

        # Direction B: the Patron's own spouse/consort/soulmate is betrayed by the direct sex partner.
        scope:domcom_direct_sex_partner = { save_scope_as = domcom_sex_patron }
        scope:domcom_direct_sex_patron = {
            every_consort = {
                limit = {
                    is_alive = yes age >= 18
                    NOT = { this = scope:domcom_direct_sex_partner }
                }
                domcom_record_full_sex_evidence_effect = yes
                scope:domcom_direct_sex_partner = { save_scope_as = domcom_owner }
                domcom_register_cuck_generic_effect = yes
            }
            every_relation = {
                type = soulmate
                limit = {
                    is_alive = yes age >= 18
                    NOT = { this = scope:domcom_direct_sex_partner }
                    NOT = { is_consort_of = scope:domcom_direct_sex_patron }
                }
                domcom_record_full_sex_evidence_effect = yes
                scope:domcom_direct_sex_partner = { save_scope_as = domcom_owner }
                domcom_register_cuck_generic_effect = yes
            }
            every_relation = {
                type = lover
                limit = {
                    is_alive = yes age >= 18
                    NOT = { this = scope:domcom_direct_sex_partner }
                    NOT = { is_consort_of = scope:domcom_direct_sex_patron }
                    NOT = { has_relation_soulmate = scope:domcom_direct_sex_patron }
                }
                domcom_record_lover_betrayal_evidence_effect = yes
            }
        }
    }
}

# Soft Carnalitas hook. This effect is called only when Carnalitas fires carn_on_sex_v2. Dominion itself
# never requires Carnalitas to exist.
domcom_record_carnalitas_sex_effect = {
    # v2.4.0: grade the experience for Endowment Mastery through Carnalitas' public sex hook. The
    # Mastery bridge is separate from cuck evidence and never changes physical measurements.
    domcom_process_carnalitas_mastery_effect = yes
    domcom_process_carnalitas_breast_mastery_effect = yes

    if = {
        limit = {
            age >= 18
            scope:carn_sex_partner = { age >= 18 is_alive = yes }
        }
        save_scope_as = domcom_sex_char_1
        scope:carn_sex_partner = { save_scope_as = domcom_sex_char_2 }

        # Carnalitas can be fired by other mods as a secondary hook. Apply the same invariant here at
        # the outermost level so lawful spouses/concubines cannot enter either betrayal direction.
        if = {
            limit = {
                NOT = { this = scope:domcom_sex_char_2 }
                NOT = { is_consort_of = scope:domcom_sex_char_2 }
            }

            # Character 1's intimate partners are potentially betrayed by character 2.
            scope:domcom_sex_char_2 = { save_scope_as = domcom_sex_patron }
            every_consort = {
                limit = {
                    is_alive = yes age >= 18
                    NOT = { this = scope:domcom_sex_char_2 }
                }
                domcom_record_full_sex_evidence_effect = yes
                scope:domcom_sex_char_2 = { save_scope_as = domcom_owner }
                domcom_register_cuck_generic_effect = yes
            }
            every_relation = {
                type = soulmate
                limit = {
                    is_alive = yes age >= 18
                    NOT = { this = scope:domcom_sex_char_2 }
                    NOT = { is_consort_of = scope:domcom_sex_char_1 }
                }
                domcom_record_full_sex_evidence_effect = yes
                scope:domcom_sex_char_2 = { save_scope_as = domcom_owner }
                domcom_register_cuck_generic_effect = yes
            }
            every_relation = {
                type = lover
                limit = {
                    is_alive = yes age >= 18
                    NOT = { this = scope:domcom_sex_char_2 }
                    NOT = { is_consort_of = scope:domcom_sex_char_1 }
                    NOT = { has_relation_soulmate = scope:domcom_sex_char_1 }
                }
                domcom_record_lover_betrayal_evidence_effect = yes
            }

            # Character 2's intimate partners are potentially betrayed by character 1.
            scope:domcom_sex_char_1 = { save_scope_as = domcom_sex_patron }
            scope:domcom_sex_char_2 = {
                every_consort = {
                    limit = {
                        is_alive = yes age >= 18
                        NOT = { this = scope:domcom_sex_char_1 }
                    }
                    domcom_record_full_sex_evidence_effect = yes
                    scope:domcom_sex_char_1 = { save_scope_as = domcom_owner }
                    domcom_register_cuck_generic_effect = yes
                }
                every_relation = {
                    type = soulmate
                    limit = {
                        is_alive = yes age >= 18
                        NOT = { this = scope:domcom_sex_char_1 }
                        NOT = { is_consort_of = scope:domcom_sex_char_2 }
                    }
                    domcom_record_full_sex_evidence_effect = yes
                    scope:domcom_sex_char_1 = { save_scope_as = domcom_owner }
                    domcom_register_cuck_generic_effect = yes
                }
                every_relation = {
                    type = lover
                    limit = {
                        is_alive = yes age >= 18
                        NOT = { this = scope:domcom_sex_char_1 }
                        NOT = { is_consort_of = scope:domcom_sex_char_2 }
                        NOT = { has_relation_soulmate = scope:domcom_sex_char_2 }
                    }
                    domcom_record_lover_betrayal_evidence_effect = yes
                }
            }
        }
    }
}

domcom_marital_preference_yearly_effect = {
    save_scope_as = domcom_compatibility_target
    every_spouse = {
        limit = { is_alive = yes age >= 18 }
        domcom_initialize_attraction_profile_effect = yes
        if = {
            limit = { NOT = { is_attracted_to_gender_of = scope:domcom_compatibility_target } }
            set_variable = { name = domcom_last_spouse_compatibility value = -90 }
        }
        else = { set_variable = { name = domcom_last_spouse_compatibility value = domcom_pair_compatibility_value } }
        if = {
            limit = { var:domcom_last_spouse_compatibility <= -65 }
            add_character_modifier = { modifier = domcom_extreme_marital_aversion_modifier years = 2 }
            if = {
                limit = {
                    is_ai = yes
                    NOT = { is_imprisoned = yes }
                    OR = { has_trait = sadistic has_trait = callous has_trait = vengeful has_trait = deceitful has_trait = wrathful }
                    NOT = { has_character_flag = domcom_marital_murder_attempt_recent }
                }
                random = {
                    chance = 15
                    add_character_flag = { flag = domcom_marital_murder_attempt_recent years = 5 }
                    start_scheme = { type = murder target_character = scope:domcom_compatibility_target }
                }
            }
        }
        else_if = {
            limit = { var:domcom_last_spouse_compatibility >= 90 }
            add_character_modifier = { modifier = domcom_extreme_marital_fixation_modifier years = 2 }
        }
    }
}
