﻿# Rise and Fall - Warlord Scripted Effects

# Give every unaligned direct landed vassal a side in the active era. The
# pending marker prevents duplicate popups while a player choice is unresolved.
riseandfall_warlord_notify_vassals = {
    every_vassal = {
        limit = {
            is_landed = yes
            NOT = { riseandfall_is_any_warlord_trigger = yes }
            NOT = { has_variable = riseandfall_warlord_invitation_pending }
        }
        set_variable = { name = riseandfall_warlord_invitation_pending value = yes }
        trigger_event = { id = warlord.1002 }
    }
}

# Active warlords are regional challengers. They never receive claims on their liege.
riseandfall_warlord_become_warlord = {
    save_scope_as = rf_warlord_actor
    if = {
        limit = { is_landed = yes }
        remove_character_flag = rf_warlord_legacy
        remove_variable = rf_warlord_legacy_expiry
        remove_variable = rf_warlord_legacy_expiry_initialized
        remove_variable = riseandfall_warlord_choice_made
        if = { limit = { has_trait = loyalwarlord } remove_trait = loyalwarlord }
        add_trait = warlord
        set_variable = { name = riseandfall_warlord_alignment_switch_cooldown value = yes years = 1 }
    }
    if = {
        limit = { is_independent_ruler = no }
        liege = {
            add_opinion = {
                target = scope:rf_warlord_actor
                modifier = warlord_rebellion_opinion
            }
            remove_opinion = {
                target = scope:rf_warlord_actor
                modifier = warlord_loyalty_opinion
            }
        }
    }
    riseandfall_warlord_reconcile_succession_se = yes
}

# A Puppet Master can raise a temporary Rebel banner without starting or
# extending the liege's realm-wide Warlord Era.
riseandfall_warlord_become_legacy_rebel = {
    riseandfall_warlord_become_warlord = yes
    if = {
        limit = { is_landed = yes has_trait = warlord }
        add_character_flag = rf_warlord_legacy
        set_variable = { name = rf_warlord_legacy_expiry value = 1 years = 5 }
        set_variable = { name = rf_warlord_legacy_expiry_initialized value = 1 }
        # The Rebel banner threatens the puppet ruler's grip for its full cost.
        if = {
            limit = { exists = liege }
            liege = {
                if = {
                    limit = { has_variable = riseandfall_rs_puppet_stability_bonus }
                    change_variable = { name = riseandfall_rs_puppet_stability_bonus add = -20 }
                    clamp_variable = { name = riseandfall_rs_puppet_stability_bonus min = -35 max = 35 }
                    round_variable = { name = riseandfall_rs_puppet_stability_bonus nearest = 1 }
                }
            }
        }
    }
}

riseandfall_warlord_remain_loyal = {
    set_variable = { name = riseandfall_warlord_choice_made value = yes years = 5 }
    riseandfall_warlord_become_loyal = yes
}

riseandfall_warlord_become_loyal = {
    save_scope_as = rf_warlord_actor
    if = {
        limit = { is_landed = yes }
        remove_character_flag = rf_warlord_legacy
        remove_variable = rf_warlord_legacy_expiry
        remove_variable = rf_warlord_legacy_expiry_initialized
        if = { limit = { has_trait = warlord } remove_trait = warlord }
        add_trait = loyalwarlord
        set_variable = { name = riseandfall_warlord_alignment_switch_cooldown value = yes years = 1 }
    }
    if = {
        limit = { is_independent_ruler = no }
        liege = {
            remove_opinion = {
                target = scope:rf_warlord_actor
                modifier = warlord_rebellion_opinion
            }
            remove_opinion = {
                target = scope:rf_warlord_actor
                modifier = warlord_loyalty_opinion
            }
            add_opinion = {
                target = scope:rf_warlord_actor
                modifier = warlord_loyalty_opinion
            }
        }
    }
    riseandfall_warlord_reconcile_succession_se = yes
}

# Retiring from the movement is personal and temporary. retiredwarlord is left
# untouched because compatibility mods use it for independent former warlords.
riseandfall_warlord_abandon_path = {
    clear_saved_scope = rf_warlord_retiring_liege
    save_scope_as = rf_warlord_retiring_actor
    if = {
        limit = {
            is_independent_ruler = no
            exists = liege
        }
        liege = { save_scope_as = rf_warlord_retiring_liege }
    }
    if = { limit = { has_trait = warlord } remove_trait = warlord }
    if = { limit = { has_trait = loyalwarlord } remove_trait = loyalwarlord }
    remove_character_flag = rf_warlord_legacy
    remove_variable = rf_warlord_legacy_expiry
    remove_variable = rf_warlord_legacy_expiry_initialized
    if = {
        limit = { exists = scope:rf_warlord_retiring_liege }
        scope:rf_warlord_retiring_liege = {
            remove_opinion = {
                target = scope:rf_warlord_retiring_actor
                modifier = warlord_rebellion_opinion
            }
            riseandfall_warlord_try_end_era_se = yes
        }
    }
    riseandfall_warlord_reconcile_succession_se = yes
    clear_saved_scope = rf_warlord_retiring_liege
}

# Expected scope: independent liege. Rebel strength is their subrealms; loyal
# strength is the liege's personal domain plus all Loyal Warlord subrealms.
riseandfall_warlord_refresh_crisis_strength_se = {
    set_variable = { name = rf_warlord_crisis_count value = 0 }
    set_variable = { name = rf_warlord_crisis_territory value = 0 }
    set_variable = { name = rf_warlord_rebel_count value = 0 }
    set_variable = { name = rf_warlord_loyal_count value = 0 }
    set_variable = { name = rf_warlord_rebel_territory value = 0 }
    set_variable = { name = rf_warlord_loyal_territory value = 0 }
    set_variable = { name = rf_warlord_crisis_realm_size value = realm_size }
    if = {
        limit = { var:rf_warlord_crisis_realm_size < 1 }
        set_variable = { name = rf_warlord_crisis_realm_size value = 1 }
    }
    save_scope_as = rf_warlord_crisis_liege
    every_held_title = {
        limit = { tier = tier_county }
        scope:rf_warlord_crisis_liege = {
            change_variable = { name = rf_warlord_loyal_territory add = 1 }
        }
    }
    every_vassal = {
        limit = {
            is_landed = yes
            riseandfall_is_active_warlord_trigger = yes
        }
        save_scope_as = rf_warlord_crisis_member
        scope:rf_warlord_crisis_liege = {
            change_variable = { name = rf_warlord_crisis_count add = 1 }
            change_variable = { name = rf_warlord_crisis_territory add = scope:rf_warlord_crisis_member.sub_realm_size }
            if = {
                limit = { scope:rf_warlord_crisis_member = { has_trait = warlord } }
                change_variable = { name = rf_warlord_rebel_count add = 1 }
                change_variable = { name = rf_warlord_rebel_territory add = scope:rf_warlord_crisis_member.sub_realm_size }
            }
            else = {
                change_variable = { name = rf_warlord_loyal_count add = 1 }
                change_variable = { name = rf_warlord_loyal_territory add = scope:rf_warlord_crisis_member.sub_realm_size }
            }
        }
    }
    set_variable = { name = rf_warlord_crisis_territory_pct value = var:rf_warlord_crisis_territory }
    change_variable = { name = rf_warlord_crisis_territory_pct multiply = 100 }
    change_variable = { name = rf_warlord_crisis_territory_pct divide = var:rf_warlord_crisis_realm_size }
    round_variable = { name = rf_warlord_crisis_territory_pct nearest = 1 }
    clamp_variable = { name = rf_warlord_crisis_territory_pct min = 0 max = 100 }
    set_variable = { name = rf_warlord_rebel_territory_pct value = var:rf_warlord_rebel_territory }
    change_variable = { name = rf_warlord_rebel_territory_pct multiply = 100 }
    change_variable = { name = rf_warlord_rebel_territory_pct divide = var:rf_warlord_crisis_realm_size }
    round_variable = { name = rf_warlord_rebel_territory_pct nearest = 1 }
    clamp_variable = { name = rf_warlord_rebel_territory_pct min = 0 max = 100 }
    set_variable = { name = rf_warlord_loyal_territory_pct value = var:rf_warlord_loyal_territory }
    change_variable = { name = rf_warlord_loyal_territory_pct multiply = 100 }
    change_variable = { name = rf_warlord_loyal_territory_pct divide = var:rf_warlord_crisis_realm_size }
    round_variable = { name = rf_warlord_loyal_territory_pct nearest = 1 }
    clamp_variable = { name = rf_warlord_loyal_territory_pct min = 0 max = 100 }
    remove_variable = rf_warlord_crisis_territory
    remove_variable = rf_warlord_rebel_territory
    remove_variable = rf_warlord_loyal_territory
    remove_variable = rf_warlord_crisis_realm_size
}

# Expected scope: a direct vassal. The decision is deliberately recalculated
# only after the one-year alignment cooldown has expired.
riseandfall_warlord_ai_choose_alignment_se = {
    save_scope_as = rf_warlord_actor
    set_variable = { name = rf_warlord_loyal_chance value = 50 }
    liege = {
        if = {
            limit = {
                has_variable = riseandfall_realm_stability_score
                var:riseandfall_realm_stability_score >= 60
            }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_loyal_chance add = 20 } }
        }
        else_if = {
            limit = {
                has_variable = riseandfall_realm_stability_score
                var:riseandfall_realm_stability_score < 20
            }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_loyal_chance add = -35 } }
        }
        else_if = {
            limit = {
                has_variable = riseandfall_realm_stability_score
                var:riseandfall_realm_stability_score < 40
            }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_loyal_chance add = -20 } }
        }
        if = {
            limit = { var:rf_warlord_loyal_territory_pct >= 35 }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_loyal_chance add = 15 } }
        }
        if = {
            limit = { var:rf_warlord_rebel_territory_pct >= 35 }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_loyal_chance add = -15 } }
        }
    }
    # Personal hostility should dominate the alignment decision. Positive
    # opinion is common late-game, so it provides only a small loyalty nudge;
    # deep hostility makes loyalty extremely unlikely even for a cautious or
    # otherwise traditionally loyal vassal.
    if = { limit = { opinion = { target = liege value >= 75 } } change_variable = { name = rf_warlord_loyal_chance add = 15 } }
    else_if = { limit = { opinion = { target = liege value >= 50 } } change_variable = { name = rf_warlord_loyal_chance add = 10 } }
    else_if = { limit = { opinion = { target = liege value >= 25 } } change_variable = { name = rf_warlord_loyal_chance add = 5 } }
    if = { limit = { opinion = { target = liege value <= -75 } } change_variable = { name = rf_warlord_loyal_chance add = -90 } }
    else_if = { limit = { opinion = { target = liege value <= -50 } } change_variable = { name = rf_warlord_loyal_chance add = -55 } }
    else_if = { limit = { opinion = { target = liege value <= -25 } } change_variable = { name = rf_warlord_loyal_chance add = -25 } }
    if = { limit = { has_trait = loyal } change_variable = { name = rf_warlord_loyal_chance add = 30 } }
    if = { limit = { has_trait = content } change_variable = { name = rf_warlord_loyal_chance add = 20 } }
    if = { limit = { has_trait = just } change_variable = { name = rf_warlord_loyal_chance add = 15 } }
    if = { limit = { has_trait = honest } change_variable = { name = rf_warlord_loyal_chance add = 10 } }
    if = { limit = { has_trait = humble } change_variable = { name = rf_warlord_loyal_chance add = 10 } }
    if = { limit = { has_trait = disloyal } change_variable = { name = rf_warlord_loyal_chance add = -35 } }
    if = { limit = { has_trait = ambitious } change_variable = { name = rf_warlord_loyal_chance add = -20 } }
    if = { limit = { has_trait = arbitrary } change_variable = { name = rf_warlord_loyal_chance add = -15 } }
    if = { limit = { has_trait = vengeful } change_variable = { name = rf_warlord_loyal_chance add = -10 } }
    if = { limit = { has_trait = deceitful } change_variable = { name = rf_warlord_loyal_chance add = -10 } }
    # Strength creates a snowball: the closer a vassal is to parity, and the
    # farther above the liege they rise, the less attractive loyalty becomes.
    if = {
        limit = { military_power >= { value = liege.military_power multiply = 5 } }
        change_variable = { name = rf_warlord_loyal_chance add = -65 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 4 } }
        change_variable = { name = rf_warlord_loyal_chance add = -55 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 3 } }
        change_variable = { name = rf_warlord_loyal_chance add = -45 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 2.5 } }
        change_variable = { name = rf_warlord_loyal_chance add = -40 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 2 } }
        change_variable = { name = rf_warlord_loyal_chance add = -35 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 1.5 } }
        change_variable = { name = rf_warlord_loyal_chance add = -25 }
    }
    else_if = {
        limit = { military_power >= liege.military_power }
        change_variable = { name = rf_warlord_loyal_chance add = -15 }
    }
    else = { change_variable = { name = rf_warlord_loyal_chance add = 10 } }
    clamp_variable = { name = rf_warlord_loyal_chance min = 5 max = 95 }
    random = {
        chance = var:rf_warlord_loyal_chance
        riseandfall_warlord_become_loyal = yes
    }
    if = {
        limit = { NOT = { has_variable = riseandfall_warlord_alignment_switch_cooldown } }
        riseandfall_warlord_become_warlord = yes
    }
    remove_variable = rf_warlord_loyal_chance
}

# Expected scope: an AI direct vassal during its liege's active Warlord crisis.
riseandfall_warlord_ai_join_crisis_se = {
    # This effect is called from an on_action, where root is unset.
    save_scope_as = rf_warlord_actor
    set_variable = { name = rf_warlord_join_chance value = 5 }
    liege = { save_scope_as = rf_warlord_crisis_liege }
    liege = {
        if = {
            limit = { var:rf_warlord_crisis_territory_pct >= 35 }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_join_chance add = 55 } }
        }
        else_if = {
            limit = { var:rf_warlord_crisis_territory_pct >= 20 }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_join_chance add = 30 } }
        }
        else_if = {
            limit = { var:rf_warlord_crisis_territory_pct >= 10 }
            scope:rf_warlord_actor = { change_variable = { name = rf_warlord_join_chance add = 10 } }
        }
    }
    # A neighboring Warlord offers a practical local reason to join: protection
    # and a potential alliance against the liege or other regional challengers.
    if = {
        limit = {
            any_neighboring_realm_owner = {
                NOT = { this = scope:rf_warlord_actor }
                riseandfall_is_any_warlord_trigger = yes
                target_is_liege_or_above = scope:rf_warlord_crisis_liege
            }
        }
        change_variable = { name = rf_warlord_join_chance add = 20 }
    }
    if = { limit = { has_trait = ambitious } change_variable = { name = rf_warlord_join_chance add = 20 } }
    if = { limit = { has_trait = disloyal } change_variable = { name = rf_warlord_join_chance add = 20 } }
    if = { limit = { has_trait = vengeful } change_variable = { name = rf_warlord_join_chance add = 10 } }
    if = { limit = { has_trait = content } change_variable = { name = rf_warlord_join_chance add = -25 } }
    if = { limit = { has_trait = craven } change_variable = { name = rf_warlord_join_chance add = -20 } }
    if = { limit = { ai_boldness > medium_positive_ai_value } change_variable = { name = rf_warlord_join_chance add = 15 } }
    if = { limit = { ai_boldness < medium_negative_ai_value } change_variable = { name = rf_warlord_join_chance add = -15 } }
    # Hostility is a much stronger reason to enter the crisis than friendship
    # is to remain outside it; high positive opinion is deliberately modest.
    if = { limit = { opinion = { target = liege value <= -75 } } change_variable = { name = rf_warlord_join_chance add = 45 } }
    else_if = { limit = { opinion = { target = liege value <= -50 } } change_variable = { name = rf_warlord_join_chance add = 35 } }
    else_if = { limit = { opinion = { target = liege value <= -25 } } change_variable = { name = rf_warlord_join_chance add = 25 } }
    if = { limit = { opinion = { target = liege value >= 75 } } change_variable = { name = rf_warlord_join_chance add = -10 } }
    else_if = { limit = { opinion = { target = liege value >= 50 } } change_variable = { name = rf_warlord_join_chance add = -8 } }
    else_if = { limit = { opinion = { target = liege value >= 25 } } change_variable = { name = rf_warlord_join_chance add = -5 } }
    if = {
        limit = { military_power >= { value = liege.military_power multiply = 5 } }
        change_variable = { name = rf_warlord_join_chance add = 60 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 4 } }
        change_variable = { name = rf_warlord_join_chance add = 50 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 3 } }
        change_variable = { name = rf_warlord_join_chance add = 40 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 2.5 } }
        change_variable = { name = rf_warlord_join_chance add = 32 }
    }
    else_if = {
        limit = {
            military_power >= { value = liege.military_power multiply = 2 }
            military_power < { value = liege.military_power multiply = 2.5 }
        }
        change_variable = { name = rf_warlord_join_chance add = 25 }
    }
    else_if = {
        limit = { military_power >= { value = liege.military_power multiply = 1.5 } }
        change_variable = { name = rf_warlord_join_chance add = 18 }
    }
    else_if = {
        limit = { military_power >= liege.military_power }
        change_variable = { name = rf_warlord_join_chance add = 10 }
    }
    clamp_variable = { name = rf_warlord_join_chance min = 0 max = 95 }
    random = {
        chance = var:rf_warlord_join_chance
        riseandfall_warlord_ai_choose_alignment_se = yes
    }
    remove_variable = rf_warlord_join_chance
}

# Expected scope: an AI Warlord direct vassal. Reconsider alignment no more
# than once per year, using the same relationship and realm-state evaluator.
riseandfall_warlord_ai_reconsider_se = {
    if = {
        limit = { NOT = { has_variable = riseandfall_warlord_alignment_switch_cooldown } }
        riseandfall_warlord_ai_choose_alignment_se = yes
    }
}
