# ============================================================
# ORGANIC AI BRAIN - EFFECTS
# ============================================================

# Performance Mode Effects
qyv_ai_enable_performance_mode = {
    set_global_flag = qyv_performance_mode_active
    # Disable Calculating depth features
    remove_game_rule = { rule = qyv_ai_intrigue option = on }
    remove_game_rule = { rule = qyv_ai_military option = on }
    remove_game_rule = { rule = qyv_ai_dread option = on }
    remove_game_rule = { rule = qyv_ai_economy option = on }
    remove_game_rule = { rule = qyv_ai_memory option = on }
    remove_game_rule = { rule = qyv_ai_religion option = on }
    set_game_rule = { rule = qyv_ai_strategic_depth option = adaptive }
}

qyv_ai_disable_performance_mode = {
    remove_global_flag = qyv_performance_mode_active
}

# Debug Decision Effects
qyv_ai_debug_print_brain_state = {
    log = "=== QYVARIA AI BRAIN STATE ==="
    log = "Ruler: [ROOT.GetName] ([ROOT.GetTitle])"
    log = "Strategy Flags: [ROOT.GetCharacterFlags]"
    log = "Realm Size: [ROOT.GetRealmSize]"
    log = "Military Strength: [ROOT.GetMilitaryStrength]"
    log = "Gold: [ROOT.GetGold]"
    log = "Prestige: [ROOT.GetPrestige]"
    log = "Piety: [ROOT.GetPiety]"
    log = "Dread: [ROOT.GetDread]"
    log = "Intrigue: [ROOT.GetIntrigue]"
    log = "Active Schemes: [ROOT.GetActiveSchemes]"
    log = "Character Flags: [ROOT.GetCharacterFlags]"
    log = "Province Flags: [ROOT.GetProvinceFlags]"
    log = "Global Flags: [ROOT.GetGlobalFlags]"
    log = "=== END BRAIN STATE ==="
}

# Counter-Intelligence Effects
qyv_ai_counter_intelligence = {
    scope:enemy_spymaster = {
        add_trait = incompetent
        add_opinion = { target = root modifier = counter_intelligence_years = 10 }
    }
    scope:enemy_spy = {
        remove_spy_network = { target = root }
        imprison = { target = root }
    }
    root = {
        add_dread = 5
        add_opinion = { target = scope:enemy_spymaster modifier = counter_intelligence_years = 5 }
    }
}

# Scheme Timing Optimization Effects
qyv_ai_scheme_before_war = {
    any_neighbor_independent_ruler = {
        limit = {
            military_strength > root.military_strength * 1.2
            NOT = { is_allied_to = root }
            NOT = { has_truce_with = root }
        }
        save_scope_as = war_target
        if = {
            limit = {
                qyv_ai_should_scheme_murder = { target = scope:war_target }
            }
            qyv_ai_scheme_murder = { target = scope:war_target }
        }
        if = {
            limit = {
                qyv_ai_should_scheme_hook = { target = scope:war_target }
            }
            qyv_ai_scheme_hook = { target = scope:war_target }
        }
        if = {
            limit = {
                qyv_ai_should_scheme_claim = { target = scope:war_target }
            }
            qyv_ai_scheme_claim = { target = scope:war_target }
        }
    }
}

qyv_ai_scheme_during_succession_crisis = {
    if = {
        limit = { heir = { is_child = yes } }
        any_child = {
            limit = { is_heir = yes }
            if = {
                limit = { qyv_ai_should_scheme_hook = { target = scope:heir } }
                qyv_ai_scheme_hook = { target = scope:heir }
            }
        }
    }
    if = {
        limit = { qyv_has_bad_heir = root }
        any_child = {
            limit = { is_heir = yes }
            qyv_ai_scheme_murder = { target = scope:heir }
        }
    }
}

# Multi-Scheme Coordination Effects
qyv_ai_run_parallel_schemes = {
    if = {
        limit = { qyv_ai_should_scheme_murder = { target = scope:scheme_target_1 } }
        qyv_ai_scheme_murder = { target = scope:scheme_target_1 }
    }
    if = {
        limit = { qyv_ai_should_scheme_abduct = { target = scope:scheme_target_2 } }
        qyv_ai_scheme_abduct = { target = scope:scheme_target_2 }
    }
    else_if = {
        limit = { qyv_ai_should_scheme_hook = { target = scope:scheme_target_2 } }
        qyv_ai_scheme_hook = { target = scope:scheme_target_2 }
    }
    else_if = {
        limit = { qyv_ai_should_scheme_claim = { target = scope:scheme_target_2 } }
        qyv_ai_scheme_claim = { target = scope:scheme_target_2 }
    }
    root = { add_stress = 10 }
}

# Performance Mode Toggle
qyv_ai_toggle_performance_mode = {
    if = {
        limit = { qyv_ai_performance_mode_active = root }
        qyv_ai_enable_performance_mode = yes
    }
    else = {
        qyv_ai_disable_performance_mode = yes
    }
}

# ============================================================
# GREAT POWER STABILITY EFFECTS
# ============================================================

# Great powers spend on vassal-opinion buildings when factions are forming.
# Boosting opinion is cheaper than fighting revolts. Uses the existing
# build_random_building channel already wired in qyv_econ.0600.
qyv_ai_build_vassal_opinion_buildings = {
    if = {
        limit = {
            qyv_ai_is_great_power = root
            qyv_ai_has_faction_trouble = root
            gold >= 500
            NOT = { is_at_war = yes }
        }
        any_owned_province = {
            limit = { holding = { can_build = yes } }
            holding = { build_random_building = yes }
        }
    }
}

# Great powers revoke titles from hostile vassals when they have a hook -
# a vassal with a hook cannot rebel. Uses the existing revoke_title channel
# already wired in qyv_ai_dread_effects.txt.
qyv_ai_revoke_hostile_vassal_titles = {
    if = {
        limit = {
            qyv_ai_is_great_power = root
            qyv_ai_has_faction_trouble = root
            gold >= 200
        }
        any_vassal = {
            limit = {
                opinion_of_liege < -30
                primary_title.tier >= tier_county
            }
            save_scope_as = hostile_vassal
        }
        if = {
            limit = { exists = scope:hostile_vassal }
            scope:hostile_vassal = {
                qyv_ai_revoke_title = yes
            }
        }
    }
}

# Great powers counter-intelligence is always-on when factions are forming.
# The existing qyv_ai_counter_intelligence effect requires intrigue >= 10;
# great powers should run it regardless of intrigue score.
qyv_ai_great_power_counter_intelligence = {
    if = {
        limit = {
            qyv_ai_is_great_power = root
            qyv_ai_has_faction_trouble = root
        }
        qyv_ai_counter_intelligence = yes
    }
}

# ============================================================
# TIER 2 EFFECTS (v1.20.6)
# ============================================================

# Appoint a regent when the heir is a child
qyv_ai_appoint_regent_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_appoint_regent = root
            }
            set_character_flag = regent_appointed
        }
    }
}

# Tighten vassal contracts when a rich vassal has low opinion
qyv_ai_tighten_vassal_contracts_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_tighten_contracts = root
            }
            any_vassal = {
                limit = {
                    monthly_income >= 20
                    opinion_of_liege < 30
                }
                vassal_contract = {
                    increase_levies = 0.2
                    increase_taxes = 0.2
                }
            }
        }
    }
}

# Loosen vassal contracts when a poor vassal is in debt
qyv_ai_loosen_vassal_contracts_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_has_poor_vassal = root
            }
            any_vassal = {
                limit = {
                    has_negative_gold = yes
                }
                vassal_contract = {
                    decrease_levies = 0.2
                    decrease_taxes = 0.2
                }
            }
        }
    }
}

# Demobilize MaA during long peace
qyv_ai_demobilize_maa_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_long_peace = root
                NOT = { has_character_flag = war_recently_ended }
            }
            any_men_at_arms = {
                limit = { maintenance > 0 }
                disband_men_at_arms = yes
            }
            set_character_flag = war_recently_ended
        }
    }
}

# Spend excess gold on prestige when secure and at peace
qyv_ai_spend_excess_gold_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_has_excess_gold = root
            }
            add_prestige = 500
            add_gold = -2000
            any_vassal = {
                limit = { opinion_of_liege < 30 }
                add_opinion = { target = root modifier = qyv_received_subsidy years = 3 }
            }
        }
    }
}

# High dread: reduce subsidies since fear replaces gold
qyv_ai_reduce_subsidies_for_dread_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_dread_high = root
            }
            remove_character_flag = qyv_subsidizing_vassals
        }
    }
}

# Stressed great power: delegate to a competent councillor
qyv_ai_delegate_stressed_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_stressed = root
            }
            add_stress = -20
            scope:spymaster = {
                add_trait = competent
            }
        }
    }
}

# Old great power: designate successor and secure vassal loyalty
qyv_ai_designate_successor_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_old_ruler = root
            }
            set_character_flag = successor_designated
            any_vassal = {
                limit = {
                    primary_title.tier >= tier_duchy
                    opinion_of_liege < 30
                }
                add_opinion = { target = root modifier = qyv_successor_designated years = 5 }
            }
        }
    }
}

# Newly conquered province: build a fort immediately
qyv_ai_fortify_new_conquest_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_newly_conquered = root
            }
            any_owned_province = {
                limit = {
                    recently_conquered = yes
                    holding = { can_build = yes building_type = fort }
                }
                holding = { build_building = fort_1 }
            }
        }
    }
}

# Multiple heirs: switch to elective to pick the best
qyv_ai_switch_to_elective_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_has_multiple_heirs = root
            }
            set_crown_law = feudal_elective_succession
        }
    }
}

# Single heir: lock in primogeniture to prevent partition
qyv_ai_lock_primogeniture_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_has_single_heir = root
            }
            set_crown_law = primogeniture_succession
        }
    }
}

# Neighbor at war: raid their provinces to weaken them
qyv_ai_raid_neighbor_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_neighbor_at_war = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_at_war = yes
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                }
                raid = yes
            }
        }
    }
}

# Prepare claim before truce expires
qyv_ai_prepare_claim_before_truce_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_prepare_claim_before_truce = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    has_truce_with = root
                    truce_days_left < 365
                }
                save_scope_as = truce_neighbor
            }
            if = {
                limit = { exists = scope:truce_neighbor }
                fabricate_claim_on = scope:truce_neighbor
            }
        }
    }
}

# Renew alliance with a gift
qyv_ai_renew_alliance_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_renew_alliance = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_allied_to = root
                    alliance_days_left < 365
                    opinion_of_self >= 10
                }
                qyv_send_diplomatic_gift = { gift_target = scope:neighbor }
            }
        }
    }
}

# Target rival's heir when they are aging
qyv_ai_target_rival_heir_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_target_rival_heir = root
            }
            any_rival = {
                limit = {
                    is_independent_ruler = yes
                    age >= 50
                    has_heir = yes
                }
                save_scope_as = aging_rival
            }
            if = {
                limit = { exists = scope:aging_rival }
                scope:aging_rival = {
                    fabricate_claim_on = scope:heir
                }
            }
        }
    }
}

# ============================================================
# DIFFICULTY SYSTEM EFFECTS (v1.20.7)
# ============================================================

# Preemptive war: strike first when neighbor is fabricating against you
qyv_ai_preemptive_war_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_preempt = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                    military_strength > root.military_strength * 0.7
                    has_active_scheme = scheme_fabricate_claim
                }
                save_scope_as = preempt_target
            }
            if = {
                limit = { exists = scope:preempt_target }
                declare_war = { target = scope:preempt_target cb = conquest }
            }
        }
    }
}

# Opportunistic raid: strike distracted neighbors
qyv_ai_raid_hard_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_raid_hard = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_at_war = yes
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                }
                save_scope_as = raid_target
            }
            if = {
                limit = { exists = scope:raid_target }
                raid = yes
            }
        }
    }
}

# Aggressive diplomacy: form alliances against the player
qyv_ai_form_anti_player_alliance_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_form_anti_player_alliance = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_player = yes
                    NOT = { is_allied_to = root }
                    NOT = { has_truce_with = root }
                    opinion_of_self >= 10
                }
                save_scope_as = anti_player_ally
            }
            if = {
                limit = { exists = scope:anti_player_ally }
                scope:anti_player_ally = {
                    add_alliance = root
                }
            }
        }
    }
}

# Player targeting: focus on the player when they are strong
qyv_ai_target_player_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_target_player = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_player = yes
                    military_strength > root.military_strength * 0.5
                }
                save_scope_as = player_target
            }
            if = {
                limit = { exists = scope:player_target }
                scope:player_target = {
                    fabricate_claim_on = root
                }
            }
        }
    }
}

# No mercy: never white peace, always demand total surrender
qyv_ai_no_mercy_war_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_never_white_peace = root
            }
            war = {
                defender = { is_ruler = root }
                refuse_white_peace = yes
            }
        }
    }
}

# No mercy: always demand annexation when winning
qyv_ai_demand_annexation_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_demand_annexation = root
            }
            war = {
                attacker = { is_ruler = root }
                set_war_goal = annexation
            }
        }
    }
}

# Brutal: run parallel schemes against multiple targets
qyv_ai_brutal_parallel_schemes_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_brutal_parallel_schemes = root
            }
            qyv_ai_run_parallel_schemes = yes
        }
    }
}

# Brutal: prepare for war even when at peace
qyv_ai_brutal_preparation_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_brutal_preparation = root
            }
            if = {
                limit = { gold >= 1000 }
                raise_men_at_arms = { type = men_at_arms_heavy_infantry }
            }
            if = {
                limit = { qyv_ai_should_build_spy_network = root }
                qyv_ai_build_spy_network = yes
            }
        }
    }
}

# ============================================================
# ELITE AI BEHAVIOURS (v1.20.10)
# Layered, parallel, proactive — how the best CK3 players play.
# ============================================================

# --- War Chaining ---
# While in war 1, prepare claims for war 2 targets.
# Elite players never wait for one war to end before starting the next.
qyv_ai_chain_war_claims_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_chain_war_claims = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    has_truce = no
                    military_strength < root.military_strength * 2.0
                    NOT = { has_claim_on = scope:neighbor }
                }
                save_scope_as = chain_target
            }
            if = {
                limit = { exists = scope:chain_target }
                fabricate_claim_on = scope:chain_target
            }
        }
    }
}

# --- Truce-Aware Expansion ---
# Treat truces as temporary. When truce with enemy A exists,
# prepare claims on enemy B (no truce) instead of waiting.
qyv_ai_expand_around_truces_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_expand_around_truces = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    has_truce = no
                    military_strength < root.military_strength * 1.5
                    NOT = { has_claim_on = scope:neighbor }
                }
                save_scope_as = truce_avoid_target
            }
            if = {
                limit = { exists = scope:truce_avoid_target }
                fabricate_claim_on = scope:truce_avoid_target
            }
        }
    }
}

# --- Dread Economy ---
# Elite players generate dread before they need it.
# When secure and at peace, build dread so fear replaces gold later.
qyv_ai_build_dread_preemptively_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_build_dread_preemptively = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    military_strength < root.military_strength * 1.5
                }
                save_scope_as = dread_target
            }
            if = {
                limit = { exists = scope:dread_target }
                scope:dread_target = {
                    scheme_hook = yes
                }
            }
        }
    }
}

# --- CB Cost Evaluation ---
# Different CBs have different warscore costs and troop requirements.
# Elite players pick the cheapest viable CB for each target.
qyv_ai_evaluate_cb_cost_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_evaluate_cb_cost = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    has_truce = no
                    military_strength < root.military_strength * 1.5
                }
                save_scope_as = cb_target
            }
            if = {
                limit = { exists = scope:cb_target }
                scope:cb_target = {
                    fabricate_claim_on = yes
                }
            }
        }
    }
}

# --- Succession Timing ---
# Elite players time the succession switch.
# Switch to primogeniture BEFORE inheritance happens, not after.
qyv_ai_time_succession_switch_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_time_succession_switch = root
            }
            set_crown_law = primogeniture_succession
        }
    }
}

# --- Layered Building ---
# Elite players build in ROI order: economy first, then military, then prestige.
qyv_ai_build_economy_first_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_build_economy_first = root
            }
            any_owned_province = {
                limit = {
                    holding = { can_build = yes building_type = farm }
                }
                holding = { build_building = farm_1 }
            }
        }
    }
}

qyv_ai_build_military_second_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_build_military_second = root
            }
            any_owned_province = {
                limit = {
                    holding = { can_build = yes building_type = fort }
                }
                holding = { build_building = fort_1 }
            }
        }
    }
}

qyv_ai_build_prestige_third_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_build_prestige_third = root
            }
            any_owned_province = {
                limit = {
                    holding = { can_build = yes building_type = temple }
                }
                holding = { build_building = temple_1 }
            }
        }
    }
}

# ============================================================
# GRANDMASTER AI (v1.20.11)
# 4 systems that make the AI feel like it reads your mind.
# ============================================================

# --- 1. Player-Behaviour Prediction ---
# Track the player's active fronts and pre-empt them.
qyv_ai_predict_player_front_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_predict_player_front = root
            }
            # Player is targeting us — fortify our borders and prepare counter-claims
            any_owned_title = {
                limit = {
                    has_claim_on = no
                    any_claimant = { is_player = yes }
                }
                save_scope_as = threatened_title
            }
            if = {
                limit = { exists = scope:threatened_title }
                # Fortify the threatened province
                scope:threatened_title = {
                    set_character_flag = qyv_player_targeting_us
                }
            }
        }
    }
}

qyv_ai_preempt_player_front_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_preempt_player_front = root
            }
            any_owned_title = {
                limit = {
                    has_claim_on = no
                    any_claimant = { is_player = yes }
                }
                save_scope_as = preempt_target_title
            }
            if = {
                limit = { exists = scope:preempt_target_title }
                # Fabricate a counter-claim on the player's home realm
                scope:preempt_target_title.liege = {
                    fabricate_claim_on = root
                }
            }
        }
    }
}

# --- 2. Destabilization as a Weapon ---
# Elite players create crises for others. The AI should too.
qyv_ai_destabilize_rival_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_destabilize_rival = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    has_truce = no
                    military_strength < root.military_strength * 1.5
                    qyv_ai_is_fragile = scope:neighbor
                }
                save_scope_as = destabilize_target
            }
            if = {
                limit = { exists = scope:destabilize_target }
                # Support claimants and encourage rebellions in the fragile realm
                scope:destabilize_target = {
                    set_character_flag = qyv_ai_destabilizing
                }
            }
        }
    }
}

qyv_ai_destabilize_faction_trouble_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_destabilize_faction_trouble = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    NOT = { is_allied_to = root }
                    has_truce = no
                    military_strength < root.military_strength * 1.5
                    qyv_ai_is_fragile = scope:neighbor
                }
                save_scope_as = destabilize_target
            }
            if = {
                limit = { exists = scope:destabilize_target }
                scope:destabilize_target = {
                    set_character_flag = qyv_ai_stirring_factions
                }
            }
        }
    }
}

# --- 3. Information as Currency ---
# Secrets and hooks are diplomatic leverage, not just scheme triggers.
qyv_ai_trade_information_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_trade_information = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_ally = no
                    has_truce = no
                    relationship >= 0
                    military_strength < root.military_strength * 2.0
                }
                save_scope_as = info_trade_target
            }
            if = {
                limit = { exists = scope:info_trade_target }
                # Offer a secret in exchange for alliance/non-aggression
                scope:info_trade_target = {
                    set_character_flag = qyv_ai_offered_secret
                }
            }
        }
    }
}

qyv_ai_use_hook_as_leverage_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_use_hook_as_leverage = root
            }
            scope:hook_target = {
                # Demand concessions using the hook
                set_character_flag = qyv_ai_hook_leverage_used
            }
        }
    }
}

# --- 4. Truce-Timing Warfare ---
# Schedule declarations around truce end dates.
qyv_ai_time_truce_expiry_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_time_truce_expiry = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    has_truce = root
                    qyv_ai_truce_expiring_soon = scope:neighbor
                }
                save_scope_as = truce_target
            }
            if = {
                limit = { exists = scope:truce_target }
                # Mark the truce target for immediate action when truce expires
                scope:truce_target = {
                    set_character_flag = qyv_ai_truce_expiring_target
                }
            }
        }
    }
}

qyv_ai_prepare_for_truce_expiry_effect = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_prepare_for_truce_expiry = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    has_truce = root
                    qyv_ai_truce_expiring_soon = scope:neighbor
                    NOT = { has_claim_on = scope:neighbor }
                }
                save_scope_as = truce_target
            }
            if = {
                limit = { exists = scope:truce_target }
                scope:truce_target = {
                    fabricate_claim_on = root
                }
            }
        }
    }
}

# ============================================================
# AI DIPLOMACY EXPANSION EFFECTS (v1.20.15)
# ============================================================

# --- AI Sends Money to Liege ---
qyv_ai_send_money_to_liege = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_send_money_to_liege = root
            }
            root = {
                add_gold = -200
            }
            liege = {
                add_gold = 200
                add_opinion = { target = root modifier = qyv_sent_money_to_liege years = 5 }
            }
            root = {
                set_character_flag = qyv_sent_money_to_liege
            }
        }
    }
}

# AI offers military help to liege
qyv_ai_offer_military_help = {
    effect = {
        if = {
            limit = {
                qyv_ai_offer_military_help = root
            }
            root = {
                set_character_flag = qyv_offered_military_help
            }
            liege = {
                add_opinion = { target = root modifier = qyv_offered_military_help years = 5 }
            }
        }
    }
}

# --- AI Marriage Proposals ---
qyv_ai_propose_marriage = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_propose_marriage = root
            }
            root = {
                set_character_flag = qyv_marriage_proposal_sent
            }
        }
    }
}

# --- AI Diplomacy Improvement ---
qyv_ai_maximize_allies = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_maximize_allies = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_ally = no
                    has_truce = no
                    is_rival = no
                    military_strength < root.military_strength * 4.0
                    OR = {
                        same_culture = yes
                        same_faith = yes
                        opinion_of_self >= 0
                    }
                }
                save_scope_as = ally_candidate
            }
            if = {
                limit = { exists = scope:ally_candidate }
                scope:ally_candidate = {
                    ai_diplomacy_stance = { target = root stance = friend }
                }
                root = {
                    set_character_flag = qyv_maximizing_allies
                }
            }
        }
    }
}

qyv_ai_pursue_diplomacy = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_pursue_diplomacy = root
            }
            root = {
                set_character_flag = qyv_diplomacy_active
            }
        }
    }
}
