
choose_plnmg_rebellion_planet = {
    # this = target country or one of its sector
    # root = espionage_operation
    random_owned_colony = {
        limit = {
            NOR = {
                is_plnmg_rebellion_fomented = yes
                has_carrier_flag = rebel_planet@root.owner
            }
        }
        weights = {
            base = 1
            modifier = {
                factor = 0.5
                planet_stability > 90
            }
            modifier = {
                factor = 0.5
                planet_stability > 80
            }
            modifier = {
                factor = 0.5
                planet_stability > 70
            }
            modifier = {
                add = 5
                planet_stability < 50
            }
            modifier = {
                add = 7.5
                planet_stability < 40
            }
            modifier = {
                add = 10
                planet_stability < 30
            }
            modifier = {
                add = 10
                planet_stability < 20
            }
        }
        set_carrier_flag = rebel_planet@root.owner
    }
    reroll_random = yes
}

steal_plnmg_pops_as_subjects = {
    optimize_memory
    # various things must exist, see planetmega_exp.11x events for details
    [[mult]
        # if mult then value should be [0.1] otherwise bugs would occur
        multiply_variable = { which = ought_stolen_pop_of_@event_target:victim_country value = $mult$ }
    ]
    [[divide]
        # if divide then value should be [1,+inf) otherwise bugs would occur
        divide_variable = { which = ought_stolen_pop_of_@event_target:victim_country value = $divide$ }
    ]
    round_variable = ought_stolen_pop_of_@event_target:victim_country
    subtract_variable = { which = ought_stolen_pop_of_@event_target:victim_country value = 5 }
    if = {
        limit = {
            check_variable = { which = ought_stolen_pop_of_@event_target:victim_country value > 0 }
            any_owned_colony = {
                has_carrier_flag = plnmg_bodyraid_tt
                if = {
                    limit = { owner = { is_gestalt = no } }
                    has_deposit = d_plnmg_advanced_agriculture_edifice
                }
                else_if = {
                    limit = { owner = { is_hive_empire = yes } }
                    has_deposit = d_plnmg_gene_farm_hive
                }
            }
        }
        while = {
            count = ought_stolen_pop_of_@event_target:victim_country
            from.target = {
                random_owned_pop_group = {
                    limit = { is_plnmg_able_to_be_stolen = yes }
                    weights = {
                        base = 1
                        modifier = {
                            mult = 5
                            is_robotic_species = no
                        }
                        modifier = {
                            mult = 10
                            has_psionic_species_trait = yes
                        }
                    }
                    kill_single_pop = yes
                }
            }
            root = {
                if = {
                    limit = { has_ascension_perk = ap_become_the_crisis }
                    complete_crisis_objective = crisobj_purge_pops
                }
            }
        }
        random_owned_colony = {
            limit = {
                has_carrier_flag = plnmg_bodyraid_tt
                if = {
                    limit = { owner = { is_gestalt = no } }
                    has_deposit = d_plnmg_advanced_agriculture_edifice
                }
                if = {
                    limit = { owner = { is_hive_empire = yes } }
                    has_deposit = d_plnmg_gene_farm_hive
                }
            }
            save_event_target_as = steal_subjects_planet
            initialize_mys_variable = { VAR = num_plnmg_usable_bodies }
            change_variable = {
                which = num_plnmg_usable_bodies
                value = prev.ought_stolen_pop_of_@event_target:victim_country
            }
        }
        set_variable = { which = actual_stolen_pops value = ought_stolen_pop_of_@event_target:victim_country }
        clear_variable = ought_stolen_pop_of_@event_target:victim_country
        country_event = { id = planetmega_exp.115 days = 3 }
    }
    else = {
        clear_variable = ought_stolen_pop_of_@event_target:victim_country
        if = {
            limit = {
                not = {
                    any_owned_colony = {
                        has_carrier_flag = plnmg_bodyraid_tt
                        if = {
                            limit = { owner = { is_gestalt = no } }
                            has_deposit = d_plnmg_advanced_agriculture_edifice
                        }
                        if = {
                            limit = { owner = { is_hive_empire = yes } }
                            has_deposit = d_plnmg_gene_farm_hive
                        }
                    }
                }
            }
            destroy_espionage_operation = from
            country_event = { id = planetmega_pk.111 }
        }
    }
}

#以下为agreement域

refresh_planetmega_quota_modifier = {
    target = {
        remove_modifier = planetmega_quota_1_subject_mod
        remove_modifier = planetmega_quota_2_subject_mod
        remove_modifier = planetmega_quota_3_subject_mod
    }
    owner = {
        remove_modifier = planetmega_quota_1_overlord_mod
        remove_modifier = planetmega_quota_2_overlord_mod
        remove_modifier = planetmega_quota_3_overlord_mod
    }
    if = {
        limit = {
            has_term_value = {
                term = planetmega_engineering_aid
                value = plnmg_aid_term_overlord_helper
            }
        }
        if = {
            limit = {
                has_term_value = {
                    term = planetmega_quota
                    value = planetmega_quota_1
                }
            }
            target = { add_modifier = { modifier = planetmega_quota_1_subject_mod } }
        }
        else_if = {
            limit = {
                has_term_value = {
                    term = planetmega_quota
                    value = planetmega_quota_2
                }
            }
            target = { add_modifier = { modifier = planetmega_quota_2_subject_mod } }
        }
        else_if = {
            limit = {
                has_term_value = {
                    term = planetmega_quota
                    value = planetmega_quota_3
                }
            }
            target = { add_modifier = { modifier = planetmega_quota_3_subject_mod } }
        }
    }
    else_if = {
        limit = {
            owner = {
                any_agreement = {
                    has_term_value = {
                        term = planetmega_engineering_aid
                        value = plnmg_aid_term_subject_corvee
                    }
                }
            }
        }
        owner = {
            if = {
                limit = {
                    any_agreement = {
                        has_term_value = {
                            term = planetmega_quota
                            value = planetmega_quota_3
                        }
                    }
                }
                add_modifier = { modifier = planetmega_quota_3_overlord_mod }
            }
            else_if = {
                limit = {
                    any_agreement = {
                        has_term_value = {
                            term = planetmega_quota
                            value = planetmega_quota_2
                        }
                    }
                }
                add_modifier = { modifier = planetmega_quota_2_overlord_mod }
            }
            else_if = {
                limit = {
                    any_agreement = {
                        has_term_value = {
                            term = planetmega_quota
                            value = planetmega_quota_1
                        }
                    }
                }
                add_modifier = { modifier = planetmega_quota_1_overlord_mod }
            }
        }
    }
}
