﻿adventurer_claim_cb = {
    icon = claim_cb
    group = claim
    mutually_exclusive_titles = {
        NOT = {
            # If pushing personal claims, you can press multiple claims with the Divine Right innovation (High Medieval Era).
            trigger_if = {
                limit = {
                    scope:attacker = scope:claimant
                }
                scope:attacker = {
                    OR = {
                        culture = {
                            has_innovation = innovation_chronicle_writing
                        }
                        AND = {
                            government_has_flag = government_is_landless_adventurer
                            has_realm_law = camp_purpose_legitimists
                            has_variable = legitimist_claimed_title
                            var:legitimist_claimed_title = {
                                OR = {
                                    holder = scope:defender
                                    de_jure_liege.holder = scope:defender
                                }
                            }
                        }
                    }
                }
            }
            # If pushing claims for someone else, you need the Rightful Ownership innovation (Late Medieval Era).
            trigger_else = {
                scope:attacker = {
                    culture = {
                        has_innovation = innovation_divine_right
                    }
                }
            }
        }
    }

    # This file intentionally mirrors the vanilla claim_cb with only the small change above.
    # Root is the title
    # scope:claimant is the claimant
    # scope:attacker is the attacker
    # scope:defender is the defender
    allowed_for_character = {
        # This CB is only for Rise and Fall-created adventurers; without this
        # top-level gate, `target_titles = all` can expose blank claim rows.
        is_landless_adventurer = yes
        has_character_flag = riseandfall_newadventurer
    }

    is_allowed_claim_title = {
        scope:attacker = {
            is_landless_adventurer = yes
            has_character_flag = riseandfall_newadventurer
        }
        # This custom AI path can target implicit hegemony claims, so do not rely on
        # the vanilla `claim` target-title generator to expose every valid title.
        scope:claimant = {
            OR = {
                has_claim_on = root
                has_strong_implicit_claim_on = root
                has_weak_implicit_claim_on = root
            }
        }
        trigger_if = {
            limit = {
                scope:attacker = {
                    is_ai = yes
                    ai_has_conqueror_personality = yes
                }
            }
            tier >= tier_duchy
        }
        custom_description = {
            text = "claimant_titles_held_by_you_or_vassal"
            NOR = {
                holder = scope:attacker
                holder = {
                    target_is_liege_or_above = scope:attacker
                }
            }
        }
        scope:claimant = {
            NOT = { has_trait = incapable }
            is_hostage = no
            trigger_if = {
                limit = { NOT = { scope:claimant = scope:attacker } } # You can always press your own claims
                is_imprisoned = no
            }
            custom_description = {
                text = "claimant_already_pressing_their_claim"
                object = root
                NAND = {
                    liege = scope:attacker
                    any_character_war = {
                        casus_belli = {
                            any_target_title = {
                                this = root
                            }
                        }
                    }
                }
            }
        }
        scope:attacker = {
            custom_description = {
                text = "claimant_titles_held_by_attacker_vassals"
                NOT = {
                    any_vassal_or_below = {
                        has_title = root
                    }
                }
            }
        }
        trigger_if = {
            limit = { NOT = { scope:claimant = scope:attacker } }
            trigger_if = {
                limit = {
                    is_weak_claimant_due_to_gender_trigger = { FAITH = scope:attacker.faith CHARACTER = scope:claimant }
                }
                trigger_if = {
                    limit = {
                        scope:claimant = { is_adult = yes }
                    }
                    save_temporary_scope_as = treat_claim_as_weak
                }
                trigger_else = {
                    always = no
                }
            }
            trigger_else_if = {
                limit = {
                    scope:claimant = { is_adult = no }
                }
                save_temporary_scope_as = treat_claim_as_weak
            }
            trigger_else = {
                always = yes
            }
        }
        trigger_if = {
            limit = { exists = scope:treat_claim_as_weak }
            holder = {
                custom_description = {
                    text = "claim_target_position_is_weak"
                    object = scope:attacker
                    OR = {
                        has_trait = incapable
                        is_adult = no
                        is_weak_claimant_due_to_gender_trigger = { FAITH = scope:attacker.faith CHARACTER = root.holder }
                    }
                }
            }
        }
        trigger_if = {
            limit = {
                scope:defender.culture = { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
            }
            scope:claimant = {
                custom_description = {
                    text = "byzantine_claimant_is_disfigured"
                    object = scope:claimant
                    is_eunuch_trigger = no
                    NOR = {
                        has_trait = incapable
                        has_trait = blind
                        has_trait = disfigured
                        has_trait = maimed
                        has_trait = one_eyed
                        has_trait = one_legged
                        has_trait = clubfooted
                        has_trait = hunchbacked
                        has_trait = dwarf
                    }
                }
            }
        }
        trigger_if = {
            limit = {
                scope:defender.culture = { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
            }
            scope:claimant = {
                custom_description = {
                    text = "byzantine_claimant_is_celibate"
                    object = scope:defender
                    is_eunuch_trigger = no
                    NOR = {
                        has_trait = celibate
                    }
                }
            }
        }
    }

    allowed_for_character_display_regardless = {
        trigger_if = {
            limit = {
                scope:attacker = {
                    government_has_flag = government_is_landless_adventurer
                }
            }
            scope:attacker = {
                has_realm_law = camp_purpose_legitimists
            }
        }
    }

    cost = {
        piety = {
            value = 0
        }
        prestige = {
            value = 0
        }
    }

    # Use the claim target generator so the Declare War UI gets a valid
    # claimant/title pair instead of enumerating every title as a blank claim.
    target_titles = claim

    on_declaration = {
        on_declared_war = yes
        if = {
            limit = {
                scope:defender = { is_sibling_of = scope:attacker }
            }
            scope:attacker = {
                add_achievement_flag_effect = { FLAG = achievement_bad_blood_flag }
            }
        }
    }

    should_invalidate = {
        OR = {
            NOT = {
                any_in_list = {
                    list = target_titles
                    any_in_de_jure_hierarchy = {
                        tier >= tier_county
                        exists = holder
                        holder = {
                            OR = {
                                this = scope:defender
                                target_is_liege_or_above = scope:defender
                            }
                        }
                    }
                }
            }
            AND = {
                NOT = { scope:claimant = scope:attacker }
                scope:claimant = {
                    OR = {
                        is_alive = no
                        NAND = {
                            is_vassal_or_below_of = scope:attacker
                            is_courtier_of = scope:attacker
                        }
                    }
                }
            }
        }
    }

    on_invalidated_desc = {
        first_valid = {
            triggered_desc = {
                trigger = {
                    NOT = { scope:claimant = scope:attacker }
                    scope:claimant = { is_alive = no }
                }
                desc = msg_claim_war_invalidated_claimant_unavailable_dead_message
            }
            triggered_desc = {
                trigger = {
                    scope:claimant = {
                        NOR = {
                            this = scope:attacker
                            NOR = {
                                is_vassal_or_below_of = scope:attacker
                                is_courtier_of = scope:attacker
                            }
                        }
                    }
                }
                desc = msg_claim_war_invalidated_claimant_unavailable_not_vassal_message
            }
            desc = msg_claim_war_invalidated_message
        }
    }
    on_invalidated = {
    }

    on_victory_desc = {
        first_valid = {
            triggered_desc = {
                trigger = {
                    scope:claimant = { is_local_player = yes }
                }
                desc = claim_cb_victory_desc_attacker_claimant
            }
            triggered_desc = {
                trigger = { scope:attacker = { is_local_player = yes } }
                desc = claim_cb_victory_desc_attacker
            }
            desc = claim_cb_victory_desc
        }
    }

    on_victory = {
        # Find the highest-tier claimed title (required by ep3_landless_invasion_titles_taken_effect)
        ordered_in_list = {
            list = target_titles
            order_by = tier
            save_scope_as = target
        }

        scope:attacker = {
            if = {
                limit = {
                    is_ai = no
                    has_realm_law = camp_purpose_legitimists
                    any_in_list = {
                        list = target_titles
                        tier >= tier_kingdom
                    }
                }
                add_achievement_global_variable_effect = {
                    VARIABLE = finished_birthright_achievement
                    VALUE = yes
                }
            }
            if = {
                limit = {
                    is_landless_adventurer = yes
                    NOT = { government_has_flag = government_is_nomadic }
                }
                hidden_effect = {
                    if = {
                        limit = {
                            NOT = { has_trait = adventurer }
                        }
                        add_trait = adventurer
                    }
                    save_scope_as = the_adventurer
                    every_courtier = {
                        set_variable = {
                            name = former_camp_leader
                            value = scope:the_adventurer
                        }
                        add_trait = adventurer_follower
                    }
                }
            }
            # Start gradual title-based low-stability chance recovery after winning.
            riseandfall_set_low_stability_cooldown_se = yes
        }

        scope:attacker = { show_pow_release_message_effect = yes }

        add_legitimacy_attacker_victory_effect = yes
        add_influence_attacker_victory_effect = yes
        scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }

        scope:attacker = {
            if = {
                limit = {
                    scope:defender = { is_alive = yes }
                    NOT = { scope:defender = { is_imprisoned = yes } }
                }
                imprison = {
                    target = scope:defender
                    type = dungeon
                }
            }
        }

        scope:defender ?= {
            if = {
                limit = {
                    is_independent_ruler = yes
                    government_has_mechanic = administrative
                    exists = scope:target
                }
                every_held_title = {
                    limit = {
                        is_noble_family_title = no
                        tier >= tier_county
                        OR = {
                            this = scope:target
                            target_is_de_jure_liege_or_above = scope:target
                        }
                    }
                    add_to_list = target_titles
                }
            }
        }

        # Use vanilla's ep3 landless invasion effect for full realm transfer:
        # transfers the entire realm (including non-de-jure territory) via
        # change_title_holder_include_vassals, matching vanilla claim_cb behaviour.
        scope:claimant = {
            if = {
                limit = { is_landless_adventurer = yes }
                # The ep3 effect relies on a new_capital being defined
                ordered_in_list = {
                    list = target_titles
                    order_by = tier
                    save_scope_as = new_capital
                }
                ep3_landless_invasion_titles_taken_effect = {
                    TITLE_GIVER = scope:defender
                    TITLE_RECEIVER = scope:claimant
                    TITLE = scope:target
                    TITLE_LIST = target_titles
                }
            }
        }

        create_title_and_vassal_change = {
            type = conquest_claim
            save_scope_as = change
            add_claim_on_loss = yes
        }

        scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }

        setup_claim_cb = {
            titles = target_titles
            attacker = scope:attacker
            defender = scope:defender
            claimant = scope:claimant
            change = scope:change
        }

        resolve_title_and_vassal_change = scope:change

        if = {
            limit = {
                NOT = { scope:claimant = scope:attacker }
                scope:claimant.primary_title.tier < scope:attacker.primary_title.tier
                scope:claimant = {
                    NOT = { target_is_liege_or_above = scope:attacker }
                }
            }

            create_title_and_vassal_change = {
                type = conquest_claim
                save_scope_as = change_two
                add_claim_on_loss = yes
            }
            scope:claimant = {
                change_liege = {
                    liege = scope:attacker
                    change = scope:change_two
                }
            }
            resolve_title_and_vassal_change = scope:change_two
        }

        if = {
            limit = {
                NOT = { scope:claimant = scope:attacker }
                scope:attacker = {
                    can_add_hook = {
                        type = favor_hook
                        target = scope:claimant
                    }
                }
            }
            scope:attacker = {
                add_hook = {
                    target = scope:claimant
                    type = favor_hook
                }
            }
        }

        modify_all_participants_fame_values = {
            WINNER = scope:attacker
            LOSER = scope:defender
            FAME_BASE = scope:cb_prestige_factor
            IS_RELIGIOUS_WAR = no
            WINNER_FAME_SCALE = 10
            LOSER_FAME_SCALE = -10
            WINNER_ALLY_FAME_SCALE = 10
            LOSER_ALLY_FAME_SCALE = 10
        }

        add_truce_attacker_victory_effect = yes
        scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
        laamp_as_mercenary_payout_tooltip_effect = yes
    }

    on_white_peace_desc = {
        first_valid = {
            triggered_desc = {
                trigger = { scope:defender = { is_local_player = yes } }
                desc = claim_cb_white_peace_desc_defender
            }
            desc = claim_cb_white_peace_desc
        }
    }

    on_white_peace = {
        scope:attacker = {
            show_pow_release_message_effect = yes
            stress_impact = {
                ambitious = medium_stress_impact_gain
                arrogant = medium_stress_impact_gain
            }
        }

        scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

        every_in_list = {
            list = target_titles
            if = {
                limit = { scope:claimant = { has_weak_claim_on = prev } }
                scope:claimant = {
                    make_claim_strong = prev
                }
            }
        }

        setup_claim_cb = {
            titles = target_titles
            attacker = scope:attacker
            defender = scope:defender
            claimant = scope:claimant
            victory = no
        }

        scope:attacker = {
            add_prestige = {
                value = scope:cb_prestige_factor
                multiply = -5.0
            }
        }

        modify_allies_of_participants_fame_values = {
            WINNER = scope:attacker
            FAME_BASE = scope:cb_prestige_factor
            IS_RELIGIOUS_WAR = no
            WINNER_ALLY_FAME_SCALE = 10
            LOSER_ALLY_FAME_SCALE = 10
        }

        scope:defender = {
            stress_impact = {
                arrogant = medium_stress_impact_gain
            }
        }

        if = {
            limit = {
                NOT = { scope:claimant = scope:attacker }
                scope:attacker = {
                    can_add_hook = { type = favor_hook target = scope:claimant }
                }
            }
            scope:attacker = { add_hook = { target = scope:claimant type = favor_hook } }
        }

        add_truce_white_peace_effect = yes
        laamp_as_mercenary_payout_tooltip_effect = yes
    }

    on_defeat_desc = {
        first_valid = {
            triggered_desc = {
                trigger = { scope:defender = { is_local_player = yes } }
                desc = claim_cb_defeat_desc_defender
            }
            triggered_desc = {
                trigger = { scope:claimant = { is_local_player = yes } }
                desc = claim_cb_defeat_desc_attacker_claimant
            }
            triggered_desc = {
                trigger = { scope:attacker = { is_local_player = yes } }
                desc = claim_cb_defeat_desc_attacker
            }
            desc = claim_cb_defeat_desc
        }
    }

    on_defeat = {
        scope:attacker = { show_pow_release_message_effect = yes }
        add_legitimacy_attacker_defeat_effect = yes
        add_influence_attacker_defeat_effect = yes
        scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

        scope:defender = {
            if = {
                limit = {
                    scope:attacker = { is_alive = yes }
                    NOT = { scope:attacker = { is_imprisoned = yes } }
                }
                imprison = {
                    target = scope:attacker
                    type = dungeon
                }
            }
        }

        every_in_list = {
            list = target_titles
            scope:claimant = { remove_claim = prev }
        }

        # Remove all claims the attacker has on kingdom-tier titles held by the defender or their vassals
        scope:defender = {
            # Defender's own held titles (kingdom+)
            every_held_title = {
                limit = { tier >= tier_kingdom }
                scope:attacker = { remove_claim = prev }
            }

            # Titles held by every vassal (recursive), includes vassal kingdoms
            every_vassal_or_below = {
                every_held_title = {
                    limit = { tier >= tier_kingdom }
                    scope:attacker = { remove_claim = prev }
                }
            }
        }

        if = {
            limit = {
                NOT = { scope:claimant = scope:attacker }
                scope:attacker = {
                    can_add_hook = { type = favor_hook target = scope:claimant }
                }
            }
            scope:attacker = { add_hook = { target = scope:claimant type = favor_hook } }
        }

        setup_claim_cb = { titles = target_titles attacker = scope:attacker defender = scope:defender claimant = scope:claimant victory = no }

        modify_all_participants_fame_values = {
            WINNER = scope:defender
            LOSER = scope:attacker
            FAME_BASE = scope:cb_prestige_factor
            IS_RELIGIOUS_WAR = no
            WINNER_FAME_SCALE = scale_10_war_defender_win
            LOSER_FAME_SCALE = -10
            WINNER_ALLY_FAME_SCALE = 10
            LOSER_ALLY_FAME_SCALE = 10
        }

        add_truce_attacker_defeat_effect = yes

        scope:attacker = { save_temporary_scope_as = loser }
        on_lost_aggression_war_discontent_loss = yes
        laamp_as_mercenary_payout_tooltip_effect = yes
    }

    on_primary_attacker_death = inherit
    on_primary_defender_death = inherit

    transfer_behavior = transfer

    attacker_allies_inherit = yes
    defender_allies_inherit = yes

    war_name = "RISEANDFALL_ADVENTURER_CLAIM_WAR_NAME"
    my_war_name = "RISEANDFALL_ADVENTURER_CLAIM_WAR_NAME_MY"
    war_name_base = "RISEANDFALL_ADVENTURER_CLAIM_WAR_NAME_BASE"
    cb_name = "RISEANDFALL_ADVENTURER_CLAIM_CB_NAME"
    interface_priority = 60

    should_show_war_goal_subview = yes

    use_de_jure_wargoal_only = yes
    # Require a majority of the target before ticking warscore starts, and give
    # adventurer claim wars more time to resolve through battles and sieges.
    attacker_wargoal_percentage = 0.51
    defender_wargoal_percentage = 0.51
    attacker_ticking_warscore_delay = { months = 36 }    # Delay ticking War Score for three years
    defender_ticking_warscore_delay = { months = 36 }
    attacker_ticking_warscore = 0.2                     # Increase daily ticking War Score
    defender_ticking_warscore = 0.2
    attacker_score_from_battles_scale = 40
    defender_score_from_battles_scale = 50
    max_attacker_score_from_battles = 100
    max_defender_score_from_battles = 100
    max_attacker_score_from_occupation = 150
    max_defender_score_from_occupation = 150
    max_ai_diplo_distance_to_title = 1000
    attacker_score_from_occupation_scale = 100         # Restore occupation scale to vanilla
    defender_score_from_occupation_scale = 100         # Restore occupation scale to vanilla
    landless_attacker_needs_armies = yes 				# If set to no, being landless and having no armies doesn't automatically give the other side 100% warscore

    white_peace_possible = no # If set to "no", white peaces can't happen; only victory, defeat, or invalidation

    ai_score_mult = { value = 1 }
}
