﻿warlord_conquest_cb = {
    icon = county_conquest_cb
    group = conquest

    combine_into_one = yes
    should_show_war_goal_subview = yes
    mutually_exclusive_titles = { always = yes }
    
    

    # Allow vassals with warlord trait to use this CB against other vassals
    allowed_for_character = {
        riseandfall_is_any_warlord_trigger = yes
        is_independent_ruler = no  # Must be a vassal
        NOR = {
            government_has_flag = government_is_landless_adventurer
            government_has_flag = government_is_nomadic
        }
    }

    allowed_against_character = {
    # Warlords can attack fellow vassals under the same liege
    is_vassal_or_below_of = scope:attacker.liege
    NOT = { this = scope:attacker }
    NOT = {
        AND = {
            has_trait = loyalwarlord
            scope:attacker = { has_trait = loyalwarlord }
        }
    }
    }
    target_titles = all
    target_title_tier = kingdom
    target_de_jure_regions_above = yes
    ignore_effect = change_title_holder

    

    valid_to_start = {
        # Warlords no larger than their target use this one-kingdom skirmish.
        # A larger Warlord uses the separate realm-absorption CB.
        scope:attacker.sub_realm_size <= scope:defender.sub_realm_size
        scope:target = {
            is_landless_type_title = no
            any_in_de_jure_hierarchy = {
                tier = tier_county
                neighboring_county_or_viking_conquest_trigger = { CHARACTER = root }
                holder ?= {
                    OR = {
                        this = scope:defender
                        is_vassal_or_below_of = scope:defender
                    }
                }
            }
        }
        scope:target = {
            trigger_if = {
                limit = {
                    scope:attacker = { is_ai = yes }
                }
                any_in_de_jure_hierarchy = {
                    continue = { tier > tier_county }
                    count >= 1
                    tier = tier_county
                    holder ?= {
                        OR = {
                            this = scope:defender
                            is_vassal_or_below_of = scope:defender
                        }
                    }
                }
            }
        }
    }

    allowed_for_character_display_regardless = {
        # Struggle constraint
        custom_tooltip = {
            text = struggle_involvement_cb_check_failure
            struggle_blocks_invasion_conquest_cb_trigger = yes
        }
    }

    should_invalidate = {
        OR = {
            scope:attacker = {
                NOT = { riseandfall_is_any_warlord_trigger = yes }
            }
            NOT = {
                any_in_list = {
                    list = target_titles
                    any_in_de_jure_hierarchy = {
                        tier = tier_county
                        holder = {
                            OR = {
                                this = scope:defender
                                target_is_liege_or_above = scope:defender
                            }
                        }
                    }
                }
            }
        }
    }

    on_invalidated_desc = msg_county_conquest_war_invalidated_message

    on_invalidated = {
        scope:attacker = { remove_variable = rf_warlord_prewar_sub_realm_size }
        scope:defender = { remove_variable = rf_warlord_prewar_sub_realm_size }
    }

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

    on_declaration = {
        on_declared_war = yes
        scope:attacker = {
            set_variable = {
                name = rf_warlord_prewar_sub_realm_size
                value = sub_realm_size
            }
        }
        scope:defender = {
            set_variable = {
                name = rf_warlord_prewar_sub_realm_size
                value = sub_realm_size
            }
        }
    }

    on_victory_desc = { desc = RISEANDFALL_WARLORD_CONQUEST_ON_VICTORY_DESC }

    on_victory = {
        scope:attacker = { show_pow_release_message_effect = yes }
        
        # Legitimacy
        add_legitimacy_attacker_victory_effect = yes

        #EP2 accolade glory gain for winning against higher ranked enemy
        scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }

        # The selected kingdom is resolved through vanilla's complete-subrealm
        # transfer path. This keeps eligible vassals intact and does not use an
        # occupation percentage to decide whether their realm changes sides.
        riseandfall_apply_de_jure_war_goal_victory_se = yes
        riseandfall_resolve_warlord_defeat_se = yes

        scope:attacker = {
            ordered_in_list = {
                list = titles_taken
                limit = {
                    holder = scope:attacker
                    tier > scope:attacker.primary_title.tier
                }
                order_by = tier
                max = 1
                save_temporary_scope_as = rf_warlord_highest_taken_title
            }
        }
        if = {
            limit = { exists = scope:rf_warlord_highest_taken_title }
            scope:attacker = {
                set_primary_title_to = scope:rf_warlord_highest_taken_title
            }
        }

        # EP3 Laamp possibility warning
        scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }

        # Prestige Progress for the Attacker (higher tier attacker gets more prestige)
        if = {
            limit = {
                scope:attacker = { exists = primary_title }
                scope:defender = { exists = primary_title }
                scope:attacker.primary_title.tier > scope:defender.primary_title.tier
            }
            scope:attacker = {
                add_prestige_experience = {
                    value = medium_prestige_value
                }
            }
        }
        else = {
            every_in_list = {
                list = target_titles
                scope:attacker = {
                    add_prestige_experience = minor_prestige_value
                }
            }
        }

        # Allies on both sides get full prestige value for helping in the war, based on their war participation.
        modify_allies_of_participants_fame_values = {
            WINNER = scope:attacker
            FAME_BASE = minor_prestige_value
            IS_RELIGIOUS_WAR = no
            WINNER_ALLY_FAME_SCALE = 1
            LOSER_ALLY_FAME_SCALE = 1
        }

        # Truce
        add_truce_attacker_victory_effect = yes

        # A smaller Warlord victory gets only a one-year reprieve. Override the
        # vanilla five-year truce after the standard victory effects run.
        if = {
            limit = {
                scope:attacker = { has_variable = rf_warlord_prewar_sub_realm_size }
                scope:defender = { has_variable = rf_warlord_prewar_sub_realm_size }
                scope:attacker.var:rf_warlord_prewar_sub_realm_size < scope:defender.var:rf_warlord_prewar_sub_realm_size
            }
            scope:attacker = {
                hidden_effect = {
                    add_truce_one_way = {
                        character = scope:defender
                        days = 365
                        override = yes
                        war = root.war
                        result = victory
                    }
                }
            }
        }

        # FP1: note the victory for future memorialisation via stele (if applicable).
        scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }

        # EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
        laamp_as_mercenary_payout_tooltip_effect = yes

        scope:attacker = { remove_variable = rf_warlord_prewar_sub_realm_size }
        scope:defender = { remove_variable = rf_warlord_prewar_sub_realm_size }
    }

    on_white_peace_desc = {
        first_valid = {
            triggered_desc = {
                trigger = { scope:defender = { is_local_player = yes } }
                desc = ducal_conquest_cb_white_peace_desc_defender
            }
            desc = ducal_conquest_cb_white_peace_desc
        }
    }

    on_white_peace = {
        scope:attacker = { show_pow_release_message_effect = yes }
        # Prestige loss for the attacker

        #EP2 accolade glory gain for doing pretty good against higher ranked enemy
        scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

        scope:attacker = {
            add_prestige = {
                value = minor_prestige_value
                multiply = -1.0
            }
        }

        # Allies on both sides get full prestige value for helping in the war, based on their war participation.
        modify_allies_of_participants_fame_values = {
            WINNER = scope:attacker # not important as the scale are identical
            FAME_BASE = medium_prestige_value
            IS_RELIGIOUS_WAR = no
            WINNER_ALLY_FAME_SCALE = 1
            LOSER_ALLY_FAME_SCALE = 1
        }

        add_truce_white_peace_effect = yes

        # EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
        laamp_as_mercenary_payout_tooltip_effect = yes

        scope:attacker = { remove_variable = rf_warlord_prewar_sub_realm_size }
        scope:defender = { remove_variable = rf_warlord_prewar_sub_realm_size }
    }

    on_defeat_desc = {
        first_valid = {
            triggered_desc = {
                trigger = { scope:defender = { is_local_player = yes } }
                desc = ducal_conquest_cb_defeat_desc_defender
            }
            triggered_desc = {
                trigger = {
                    scope:attacker = {
                        is_local_player = yes
                        has_targeting_faction = yes
                    }
                }
                desc = ducal_conquest_cb_defeat_desc_attacker
            }
            desc = ducal_conquest_cb_defeat_desc
        }
    }

    on_defeat = {
        scope:attacker = { show_pow_release_message_effect = yes }
        
        # Legitimacy
        add_legitimacy_attacker_defeat_effect = yes

        #EP2 accolade glory gain for winning against higher ranked enemy
        scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

        # Prestige loss for the attacker
        scope:attacker = {
            pay_short_term_gold_reparations_effect = {
                GOLD_VALUE = 2
            }
            add_prestige = {
                value = medium_prestige_value
                multiply = -1.0
            }
        }

        # Prestige for Defender
        scope:defender = {
            add_prestige_war_defender_effect = {
                PRESTIGE_VALUE = medium_prestige_value
            }
        }

        # Allies on both sides get full prestige value for helping in the war, based on their war participation.
        modify_allies_of_participants_fame_values = {
            WINNER = scope:defender
            FAME_BASE = minor_prestige_value
            IS_RELIGIOUS_WAR = no
            WINNER_ALLY_FAME_SCALE = 1
            LOSER_ALLY_FAME_SCALE = 1
        }

        add_truce_attacker_defeat_effect = yes

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

        # EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
        laamp_as_mercenary_payout_tooltip_effect = yes

        scope:attacker = { remove_variable = rf_warlord_prewar_sub_realm_size }
        scope:defender = { remove_variable = rf_warlord_prewar_sub_realm_size }
    }

    transfer_behavior = transfer

    on_primary_attacker_death = inherit
    on_primary_defender_death = inherit

    attacker_allies_inherit = yes
    defender_allies_inherit = yes

    war_name = "WARLORD_CONQUEST_WAR_NAME"
    war_name_base = "WARLORD_CONQUEST_WAR_NAME_BASE"
    cb_name = "WARLORD_CONQUEST_CB_NAME"

    interface_priority = 79

    use_de_jure_wargoal_only = yes
    ticking_war_score_targets_entire_realm = no
    # A foothold contests passive ticking, while defenders need near-total
    # control of the selected kingdom to win by denial alone.
    attacker_wargoal_percentage = 0.01
    defender_wargoal_percentage = 0.95

    attacker_ticking_warscore_delay = { months = 36 }
    defender_ticking_warscore_delay = { months = 36 }
    attacker_ticking_warscore = 0.03
    defender_ticking_warscore = 0.03

    attacker_score_from_occupation_scale = 50
    defender_score_from_occupation_scale = 50
    attacker_score_from_battles_scale = 100
    defender_score_from_battles_scale = 100

    max_attacker_score_from_battles = 90
    max_defender_score_from_battles = 90
    max_attacker_score_from_occupation = 100
    max_defender_score_from_occupation = 100

    # Keep the vanilla/intent behavior flags
    full_occupation_by_defender_gives_victory = yes
    full_occupation_by_attacker_gives_victory = yes
    landless_attacker_needs_armies = no
    allow_hostages = yes

    max_ai_diplo_distance_to_title = 1500

    ai_score_mult = {
        value = 1
        #Declaring war on the frankokratia leader can really ruin the whole thing
        if = {
            limit = {
                scope:defender = {
                    any_owned_story = {
                        story_type = frankokratia_story
                        NOT = { has_variable = franko_war_complete }
                    }
                }
            }
            multiply = 0
        }
    }
}
