qyv_tribute_gift_effects = {
    # AI sends tribute to stronger neighbor
    qyv_send_tribute = {
        scope:threat = {
            add_gold = 150
            add_opinion = { target = root modifier = qyv_receiving_tribute years = 5 }
            add_character_modifier = { modifier = qyv_receiving_tribute_from_payer years = 5 }
        }
        root = {
            add_gold = -150
            add_character_modifier = { modifier = qyv_paying_tribute years = 5 }
            set_character_flag = qyv_paying_tribute_to_target
            set_character_flag = qyv_tribute_active
        }
    }

    # AI sends diplomatic gift to improve relations
    qyv_send_diplomatic_gift = {
        scope:gift_target = {
            add_gold = 300
            add_opinion = { target = root modifier = qyv_received_diplomatic_gift years = 5 }
            add_character_modifier = { modifier = qyv_received_gift_recently years = 5 }
        }
        root = {
            add_gold = -300
            add_character_modifier = { modifier = qyv_sent_diplomatic_gift years = 5 }
        }
    }

    # Player sends tribute to AI
    qyv_player_send_tribute = {
        scope:tribute_target = {
            add_gold = 250
            add_opinion = { target = root modifier = qyv_receiving_tribute years = 5 }
            add_character_modifier = { modifier = qyv_receiving_tribute_from_payer years = 5 }
        }
        root = {
            add_gold = -250
            add_character_modifier = { modifier = qyv_paying_tribute years = 5 }
            set_character_flag = qyv_paying_tribute_to_target
        }
    }

    # Stop tribute payments
    qyv_stop_tribute = {
        root = {
            remove_character_flag = qyv_paying_tribute_to_target
            remove_character_modifier = qyv_paying_tribute
        }
        scope:tribute_target = {
            remove_character_modifier = qyv_receiving_tribute_from_payer
        }
    }

    # Renew tribute automatically
    qyv_renew_tribute = {
        if = {
            limit = {
                root = {
                    gold >= 200
                    NOT = { in_debt = yes }
                    has_character_flag = qyv_paying_tribute_to_target
                }
                scope:tribute_target = {
                    is_ruler = yes
                    is_independent_ruler = yes
                }
            }
            root = {
                add_gold = -150
            }
            scope:tribute_target = {
                add_gold = 150
                add_opinion = { target = root modifier = qyv_receiving_tribute years = 5 }
                add_character_modifier = { modifier = qyv_receiving_tribute_from_payer years = 5 }
            }
        }
        else = {
            root = { qyv_stop_tribute = yes }
        }
    }
}
# ============================================================
# DIPLOMACY EXPANSION EFFECTS (v1.20.12)
# Hostages, alliances, troop requests, gold reserves,
# genetic optimization, culture/faith unity.
# ============================================================

# --- Hostages ---
# AI takes hostages from defeated rivals to secure peace
qyv_ai_take_hostage = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_take_hostage = root
            }
            any_war_participant = {
                limit = {
                    is_at_war_with = root
                    any_family_member = {
                        limit = {
                            is_ruler = no
                            is_child = no
                            is_married = no
                            is_hostage = no
                            age >= 10
                        }
                    }
                }
                save_scope_as = defeated_rival
            }
            scope:defeated_rival.any_family_member = {
                limit = {
                    is_ruler = no
                    is_child = no
                    is_married = no
                    is_hostage = no
                    age >= 10
                }
                save_scope_as = hostage_candidate
            }
            if = {
                limit = { exists = scope:defeated_rival exists = scope:hostage_candidate }
                root = {
                    take_hostage = scope:hostage_candidate
                }
            }
        }
    }
}

# AI gives hostages to stronger neighbors to buy peace
qyv_ai_give_hostage = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_give_hostage = root
            }
            any_family_member = {
                limit = {
                    is_ruler = no
                    is_child = no
                    is_married = no
                    age >= 10
                    NOT = { is_hostage = yes }
                }
                save_scope_as = hostage_to_give
            }
            if = {
                limit = { exists = scope:hostage_to_give }
                scope:hostage_to_give = {
                    set_character_flag = qyv_given_as_hostage
                }
                root = {
                    set_character_flag = qyv_gave_hostage_for_peace
                }
            }
        }
    }
}

# --- Alliances ---
# AI offers alliances to sensible neighbors
qyv_ai_offer_alliance = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_offer_alliance = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_ally = no
                    has_truce = no
                    is_rival = no
                    military_strength < root.military_strength * 3.0
                    military_strength > root.military_strength * 0.3
                    OR = {
                        same_culture = yes
                        same_faith = yes
                        opinion_of_self >= 0
                    }
                }
                save_scope_as = alliance_target
            }
            if = {
                limit = { exists = scope:alliance_target }
                scope:alliance_target = {
                    ai_diplomacy_stance = { target = root stance = friend }
                }
                root = {
                    set_character_flag = qyv_offered_alliance
                }
            }
        }
    }
}

# --- Troop Requests ---
# Small nations can ask local powers for troops
qyv_ai_request_troops = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_request_troops = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    is_ally = no
                    military_strength > root.military_strength * 1.5
                    has_truce = no
                }
                save_scope_as = potential_ally
            }
            if = {
                limit = { exists = scope:potential_ally }
                scope:potential_ally = {
                    set_character_flag = qyv_requested_troops
                }
                root = {
                    set_character_flag = qyv_requested_troops_sent
                }
            }
        }
    }
}

# --- Gold Reserves ---
# AI keeps 500+ gold reserve, scaled by realm size
qyv_ai_enforce_gold_reserve = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_keep_gold_reserve = root
            }
            # Don't spend below reserve — stop all non-essential spending
            root = {
                set_character_flag = qyv_gold_reserve_active
            }
        }
    }
}

# --- Genetic Optimization ---
# AI prioritizes good genetics for heirs when no alliance needed
qyv_ai_prioritize_genetics = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_prioritize_genetics = root
            }
            has_heir = yes
            heir = {
                set_character_flag = qyv_genetics_priority
            }
        }
    }
}

# --- Culture/Faith Unity ---
# Same-culture/same-faith realms get opinion bonus to ally
qyv_ai_unite_by_culture_faith = {
    effect = {
        if = {
            limit = {
                qyv_ai_should_unite_by_culture_faith = root
            }
            any_neighbor_independent_ruler = {
                limit = {
                    OR = {
                        same_culture = yes
                        same_faith = yes
                    }
                    is_ally = no
                    has_truce = no
                    is_rival = no
                    military_strength < root.military_strength * 2.0
                }
                save_scope_as = unity_target
            }
            if = {
                limit = { exists = scope:unity_target }
                scope:unity_target = {
                    ai_diplomacy_stance = { target = root stance = friend }
                }
            }
        }
    }
}
