﻿
quarterly_playable_pulse = {
    on_actions = {
        ddm_baby_mode_ai_pulse
        ddm_prime_education_ai_pulse
        ddm_household_quarterly_pulse
        ddm_proscription_quarterly_pulse
        # 2.6.30 performance: automatic education used to simulate a monthly cadence from this
        # quarterly anchor. Because the feature's candidate search is diplomatic-range wide, that
        # could mean twelve world-character scans per year for large dynasties. One quarterly pass
        # is enough to catch unattended children without making education a constant background tax.
        ddm_auto_education_quarterly_pulse
    }
}

# Run once after loading/starting so an old save does not have to wait for its next quarterly anchor
# before unattended dynasty children are discovered.
on_game_start_after_lobby = {
    on_actions = { ddm_auto_education_initial_scan }
}

ddm_auto_education_quarterly_pulse = {
    trigger = {
        is_alive = yes
        is_ai = no
    }
    effect = {
        ddm_auto_education_monthly_effect = yes
    }
}

# Legacy alias retained for old references; it is no longer scheduled by DDM itself.
ddm_auto_education_monthly_pulse = {
    # Retired schedule. Queued calls drain; initial and quarterly scans own upkeep.
    trigger = { always = no }
}

# on_game_start_after_lobby has no root scope, so iterate only actual players.
ddm_auto_education_initial_scan = {
    effect = {
        every_player = {
            limit = { is_alive = yes is_ai = no }
            ddm_auto_education_monthly_effect = yes
        }
    }
}



# Kidnap quota resets once per ruler-year. The four-base/eight-after-failures accounting is otherwise
# purely local to the ruler and does not require a global scan.
yearly_playable_pulse = {
    on_actions = {
        ddm_kidnap_yearly_quota_reset
        ddm_love_proscription_yearly_reset_pulse
        ddm_personal_proscription_yearly_reset_pulse
        ddm_ai_love_proscription_yearly_pulse
        ddm_habsburg_creed_yearly_pulse
    }
}

ddm_kidnap_yearly_quota_reset = {
    effect = {
        remove_variable = ddm_kidnap_attempt_allowance_year
        remove_variable = ddm_kidnap_attempts_year
        remove_variable = ddm_kidnap_successes_year
        remove_variable = ddm_kidnap_failures_year
    }
}


# Stagger the non-trivial AI target search across the year instead of evaluating every ruler
# on the same yearly date. Each eligible AI still considers kidnapping about once per year.
random_yearly_playable_pulse = {
    on_actions = { ddm_ai_kidnap_yearly_pulse }
}

# Optional AI reciprocity for Dynastic Kidnapping. This is genuinely yearly and randomly staggered in 2.6.31;
# the old quarterly schedule made a nominally rare action recur four times as often as intended.
ddm_ai_kidnap_yearly_pulse = {
    trigger = {
        is_alive = yes
        is_ai = yes
        NOT = { has_game_rule = ddm_kidnap_disabled }
        NOT = { has_game_rule = ddm_kidnap_player_only }
    }
    effect = { ddm_ai_kidnap_yearly_effect = yes }
}

# Legacy alias for save/mod references. DDM itself no longer schedules this quarterly.
ddm_ai_kidnap_quarterly_pulse = {
    trigger = { always = no }
}


# Household metadata is cheap and player-local. The quarterly pass keeps recognized-consort bonuses in
# sync with changing skills and repairs stale concubine slots without world scans.
ddm_household_quarterly_pulse = {
    trigger = { is_alive = yes is_ai = no }
    effect = { ddm_household_maintenance_effect = yes }
}

on_birth_child = {
    on_actions = { ddm_household_birth_hook ddm_habsburg_birth_hook }
}

ddm_household_birth_hook = {
    effect = { ddm_household_on_birth_effect = yes }
}


# Habsburg pedigree tiers are cheap to keep current at birth: only the newborn's four-generation tree is scanned.
ddm_habsburg_birth_hook = {
    effect = {
        save_scope_as = ddm_habsburg_newborn
        every_player = {
            limit = { is_alive = yes is_ai = no exists = dynasty }
            save_scope_as = ddm_habsburg_birth_player
            if = {
                limit = { scope:ddm_habsburg_newborn = { exists = dynasty dynasty ?= scope:ddm_habsburg_birth_player.dynasty } }
                scope:ddm_habsburg_newborn = { ddm_habsburg_apply_consanguinity_tier_effect = yes }
            }
        }
    }
}



# Immediate player alert when a new hostile war creates a proscription-eligible rebel leader.
# scope:defender / scope:attacker / scope:war are supplied by CK3's on_war_started code on_action.
on_war_started = {
    on_actions = { ddm_proscription_war_started_alert ddm_ai_proscription_war_started }
}

ddm_proscription_war_started_alert = {
    effect = {
        if = {
            limit = {
                exists = scope:defender
                exists = scope:attacker
                scope:defender = { is_alive = yes is_ai = no is_landed = yes }
            }
            scope:defender = { save_scope_as = ddm_proscriber }
            if = {
                limit = {
                    scope:attacker = {
                        is_alive = yes
                        ddm_is_proscription_rebel_leader_trigger = yes
                        NOT = { has_variable = ddm_proscribed_by }
                        NOT = { has_character_flag = ddm_bounty_hunter_immunity }
                        NOT = { has_character_flag = ddm_proscription_recently_revoked }
                    }
                }
                scope:defender = {
                    add_character_flag = ddm_proscription_target_available_notified
                    send_interface_toast = {
                        type = event_toast_effect_neutral
                        title = DDM_PROSCRIPTION_TARGET_AVAILABLE_TOAST
                        left_icon = scope:defender
                        right_icon = scope:attacker
                    }
                }
            }
        }
    }
}


# AI rulers may answer genuine rebellion/faction wars with the same political Proscription system.
# This fires only at war start and only against a target that passes the existing rebel-leader trigger,
# so ordinary external wars and Grand Capitulation wars are excluded.
ddm_ai_proscription_war_started = {
    effect = {
        if = {
            limit = {
                exists = scope:defender
                exists = scope:attacker
                scope:defender = { is_alive = yes is_ai = yes is_landed = yes }
            }
            scope:defender = { save_scope_as = ddm_proscriber }
            if = {
                limit = {
                    scope:attacker = {
                        is_alive = yes
                        ddm_is_proscription_rebel_leader_trigger = yes
                        NOT = { has_variable = ddm_proscribed_by }
                        NOT = { has_character_flag = ddm_bounty_hunter_immunity }
                        NOT = { has_character_flag = ddm_proscription_recently_revoked }
                    }
                }
                random = {
                    chance = 35
                    modifier = { add = 20 scope:defender = { dread >= 40 } }
                    modifier = { add = 15 scope:defender = { has_trait = vengeful } }
                    modifier = { add = 10 scope:defender = { has_trait = wrathful } }
                    modifier = { add = -15 scope:defender = { has_trait = forgiving } }
                    scope:attacker = { save_scope_as = ddm_proscription_target }
                    scope:defender = { ddm_issue_proscription_effect = yes }
                }
            }
        }
    }
}

# Proscriptions are player-local and only tick when at least one writ or a lingering climate exists.
monthly_playable_pulse = {
    on_actions = {
        ddm_proscription_availability_pulse
        ddm_proscription_monthly_pulse
        ddm_personal_proscription_auto_pulse
        ddm_love_proscription_monthly_pulse
    }
}

ddm_proscription_availability_pulse = {
    trigger = {
        is_alive = yes
        is_ai = no
        is_landed = yes
    }
    effect = { ddm_proscription_target_availability_effect = yes }
}

ddm_proscription_monthly_pulse = {
    trigger = {
        is_alive = yes
        is_ai = no
        is_landed = yes
        OR = {
            has_variable = ddm_proscription_active_count
            has_variable = ddm_proscription_climate
        }
    }
    effect = { ddm_proscription_monthly_effect = yes }
}

ddm_proscription_quarterly_pulse = {
    trigger = {
        is_alive = yes
        is_ai = no
        is_landed = yes
        OR = {
            has_variable = ddm_proscription_reputation
            has_variable = ddm_proscription_climate
        }
    }
    effect = { ddm_proscription_quarterly_intimidation_effect = yes }
}


# AI rulers may use the same infant safety net. A 50% quarterly adoption chance avoids creating every
# support court on the same day while making adoption very likely during a prolonged infancy.
ddm_baby_mode_ai_pulse = {
    trigger = { is_alive = yes is_ai = yes is_landed = yes age < 6 NOT = { has_character_flag = ddm_baby_mode_resolved } }
    effect = { random = { chance = 50 ddm_activate_baby_mode_effect = yes } }
}




ddm_personal_proscription_yearly_reset_pulse = {
    trigger = { is_alive = yes is_ai = no is_landed = yes highest_held_title_tier >= tier_kingdom }
    effect = { ddm_personal_proscription_yearly_reset_effect = yes }
}

ddm_personal_proscription_auto_pulse = {
    trigger = {
        is_alive = yes
        is_ai = no
        is_landed = yes
        highest_held_title_tier >= tier_kingdom
        has_variable = ddm_personal_proscription_auto
        var:ddm_personal_proscription_auto = 1
    }
    effect = { ddm_personal_proscription_automation_effect = yes }
}

# AI child rulers plan their own education once from birth. This is independent of whether
# Baby Mode was ever used, and therefore also repairs AI infant starts from older saves.
ddm_prime_education_ai_pulse = {
    trigger = { is_alive = yes is_ai = yes is_landed = yes is_adult = no NOT = { has_character_flag = ddm_prime_ai_plan_applied } }
    effect = { ddm_prime_education_ai_effect = yes }
}


# Relationship proscriptions tick monthly for both human and AI rulers. Pressure settings determine
# whether a given target may actually receive 4, 8 or 12 attempts in the current year.
ddm_love_proscription_monthly_pulse = {
    trigger = { is_alive = yes is_landed = yes has_variable_list = ddm_love_proscriptions }
    effect = { ddm_love_proscription_monthly_effect = yes }
}

ddm_love_proscription_yearly_reset_pulse = {
    trigger = { is_alive = yes is_landed = yes has_variable_list = ddm_love_proscriptions }
    effect = { ddm_love_proscription_yearly_reset_effect = yes }
}

ddm_ai_love_proscription_yearly_pulse = {
    trigger = { is_alive = yes is_ai = yes is_landed = yes is_adult = yes }
    effect = { ddm_ai_try_unfaithful_lover_proscription_effect = yes }
}


# Habsburgs' Creed runs once per year for the human ruler, arranging at most twelve adult same-dynasty pairs.
ddm_habsburg_creed_yearly_pulse = {
    trigger = { is_alive = yes is_ai = no has_variable = ddm_habsburg_mode var:ddm_habsburg_mode > 0 exists = dynasty }
    effect = { ddm_habsburg_creed_sweep_effect = yes }
}
