
sit_dismantle_plnmg_mega_effect = {
    optimize_memory
    # this = situation
    sit_dismantle_plnmg_mega_special = yes
    #t1
    sit_dismantle_plnmg_mega_indiv = { type = great_laboratory }
    sit_dismantle_plnmg_mega_indiv = { type = advanced_forge }
    sit_dismantle_plnmg_mega_indiv = { type = heaven_colossal }
    sit_dismantle_plnmg_mega_indiv = { type = giant_power_battery }
    sit_dismantle_plnmg_mega_indiv = { type = commercial_shikara }
    sit_dismantle_plnmg_mega_indiv = { type = war_headquarter }
    sit_dismantle_plnmg_mega_indiv = { type = centralized_manufactory }
    sit_dismantle_plnmg_mega_indiv = { type = primary_mind_pyramid }
    sit_dismantle_plnmg_mega_indiv = { type = matter_demodulator }
    sit_dismantle_plnmg_mega_indiv = { type = vertical_farm_edifice }
    sit_dismantle_plnmg_mega_indiv = { type = hive_farm_edifice }
    sit_dismantle_plnmg_mega_indiv = { type = admin_axis }
    sit_dismantle_plnmg_mega_indiv = { type = ecodome_complex }
    sit_dismantle_plnmg_mega_indiv = { type = logistics_shikara }
    sit_dismantle_plnmg_mega_indiv = { type = resistance_organ }
    sit_dismantle_plnmg_mega_indiv = { type = hakkou_ichiu_tower }
    #t2
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_singularity_generator }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_bore_of_the_shroud }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_maginot_nexus }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_advanced_agriculture_edifice }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_gene_farm_hive }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_supercom }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_fusion_brain }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_nirvana_factory }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_financial_shikara }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_tms_control }
    #t3
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_colossus_assist }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_extra_galaxy }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_omnipotence }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_nano_storm }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_world_shaper }
    #t1wild
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_wilderness_flora }
    sit_dismantle_plnmg_mega_indiv = { type = plnmg_wilderness_terra }
}
sit_dismantle_plnmg_mega_indiv = {
    optimize_memory
    if = {
        limit = { has_situation_flag = dismantling_$type$ }
        remove_situation_flag = dismantling_$type$
        target = { remove_plnmg_mega = { TYPE = $type$ } }
    }
}
sit_dismantle_plnmg_mega_special = {
    optimize_memory
    target = {
        if = {
            limit = {
                has_carrier_flag = built_capital_pm_admin
                prev = {
                    or = {
                        has_situation_flag = dismantling_admin_axis
                        has_situation_flag = dismantling_primary_mind_pyramid
                        has_situation_flag = dismantling_plnmg_tms_control
                    }
                }
            }
            remove_carrier_flag = built_capital_pm_admin
        }
    }
}

sit_dismantle_plnmg_mega_pre_action = {
    optimize_memory
    # this = situation
    #t1
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = great_laboratory }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = advanced_forge }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = heaven_colossal }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = giant_power_battery }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = commercial_shikara }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = war_headquarter }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = centralized_manufactory }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = primary_mind_pyramid }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = matter_demodulator }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = vertical_farm_edifice }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = hive_farm_edifice }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = admin_axis }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = ecodome_complex }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = logistics_shikara }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = resistance_organ }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = hakkou_ichiu_tower }
    #t2
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_singularity_generator }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_bore_of_the_shroud }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_maginot_nexus }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_advanced_agriculture_edifice }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_gene_farm_hive }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_supercom }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_fusion_brain }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_nirvana_factory }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_financial_shikara }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_tms_control }
    #t3
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_colossus_assist }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_extra_galaxy }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_omnipotence }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_nano_storm }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_world_shaper }
    #t1wild
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_wilderness_flora }
    sit_dismantle_plnmg_mega_pre_action_indiv = { type = plnmg_wilderness_terra }
}
sit_dismantle_plnmg_mega_pre_action_indiv = {
    optimize_memory
    if = {
        limit = { has_situation_flag = dismantling_$type$ }
        target = {
            random_deposit = {
                limit = { is_deposit_type = d_$type$ }
                save_event_target_as = target_deposit
            }
        }
    }
}

sit_plnmg_t0upg_initializer = {
    set_situation_flag = no_cost_check
    add_situation_progress = 750
    fire_on_action = { on_action = on_planetmega_build_begin }
}

#以下的SCOPE应当为PLANET
add_plnmg_mega_flag_indiv = {
    #unused, plz do not use
    set_carrier_flag = planet_has_groundmega
    set_carrier_flag = planet_has_$TYPE$
    set_variable = { which = num_plnmg_mega_owned_count value = 0 }
    every_deposit = {
        limit = { is_plnmg_any_mega = yes }
        set_deposit_flag = keep_deposit_on_terraform
        prev = { change_variable = { which = num_plnmg_mega_owned_count value = 1 } }
    }
    if = {
        limit = { not = { has_modifier = planetmega_has_a_megastructure } }
        add_modifier = { modifier = planetmega_has_a_megastructure days = -1 }
    }
    if = {
        limit = { has_plnmg_improved_mega = yes }
        set_carrier_flag = planet_has_improved_groundmega
    }
    if = {
        limit = { has_plnmg_advanced_mega = yes }
        set_carrier_flag = planet_has_advanced_groundmega
    }
    if = {
        limit = { has_plnmg_ultimate_mega = yes }
        set_carrier_flag = planet_has_ultimate_groundmega
    }
}
add_plnmg_mega = {
    optimize_memory
    #default event_target:target_situation is root, if no root then prev, if no prev then from
    #or you can manually set event_target:target_situation that overrides root
    add_deposit = d_$TYPE$

    #add_plnmg_mega_flag_indiv effect
    set_carrier_flag = planet_has_groundmega
    set_carrier_flag = planet_has_$TYPE$
    set_variable = { which = num_plnmg_mega_owned_count value = 0 }
    every_deposit = {
        limit = { is_plnmg_any_mega = yes }
        set_deposit_flag = keep_deposit_on_terraform
        prev = { change_variable = { which = num_plnmg_mega_owned_count value = 1 } }
    }
    if = {
        limit = { not = { has_modifier = planetmega_has_a_megastructure } }
        add_modifier = { modifier = planetmega_has_a_megastructure days = -1 }
    }
    if = {
        limit = { has_plnmg_improved_mega = yes }
        set_carrier_flag = planet_has_improved_groundmega
    }
    if = {
        limit = { has_plnmg_advanced_mega = yes }
        set_carrier_flag = planet_has_advanced_groundmega
    }
    if = {
        limit = { has_plnmg_ultimate_mega = yes }
        set_carrier_flag = planet_has_ultimate_groundmega
    }

    # specially for war t1 & t2
    if = {
        limit = {
            or = {
                has_deposit = d_war_headquarter
                has_deposit = d_resistance_organ
            }
            has_global_flag = AOW_installed
        }
        if = {
            limit = { has_global_flag = AOW_installed }
            set_carrier_flag = cannot_be_hit_by_aow_starfall_spear
        }
        # if = {
        #     limit = { has_gigastructures = yes }
        #     set_carrier_flag = cant_target_this_planet
        # }
    }
    if = {
        limit = { has_deposit = d_plnmg_maginot_nexus }
        carrier_event = { id = planetmega.906 days = 1 }
        #solar_system = { set_star_flag = ai_dyson_gun_limit }
        if = {
            limit = { has_global_flag = AOW_installed }
            set_carrier_flag = cannot_be_hit_by_aow_starfall_spear
            solar_system = { set_star_flag = cannot_be_hit_by_aow_starfall_spear }
        }
    }
    # specially for all planetmega's labs
    # if = {
    #     limit = {
    #         or = {
    #             has_deposit = d_great_laboratory
    #             has_deposit = d_plnmg_supercom
    #             has_deposit = d_plnmg_fusion_brain
    #         }
    #     }
    # }
    # specially for planetmega's T2Labs
    if = {
        limit = {
            or = {
                has_deposit = d_plnmg_supercom
                has_deposit = d_plnmg_fusion_brain
            }
            nor = {
                has_carrier_flag = plnmg_all_research
                has_carrier_flag = plnmg_all_production
            }
        }
        set_carrier_flag = plnmg_all_research
    }
    # specially for nirvana factory
    if = {
        limit = {
            has_deposit = d_plnmg_nirvana_factory
            nor = {
                has_carrier_flag = plnmg_all_alloys_prod
                has_carrier_flag = plnmg_all_goods_prod
            }
        }
        if = {
            limit = {
                owner? = {
                    or = {
                        has_civic = civic_dystopian_society
                        and = {
                            is_gestalt = yes
                            nor = {
                                has_civic = civic_machine_obsessional_directive
                                has_civic = civic_machine_servitor
                            }
                        }
                    }
                }
            }
            set_carrier_flag = plnmg_all_alloys_prod
        }
        else_if = {
            limit = {
                owner? = {
                    or = {
                        has_civic = civic_machine_obsessional_directive
                        is_pleasure_seeker_empire = yes
                    }
                }
            }
            set_carrier_flag = plnmg_all_goods_prod
        }
    }
    # specially for d_plnmg_nano_storm
    if = {
        limit = {
            has_deposit = d_plnmg_nano_storm
            met_synth_queen = no
            exists = owner
        }
        owner = { country_event = { id = planetmega.984 } }
    }

    [[on_actions]
        random_deposit = {
            limit = { is_deposit_type = d_$TYPE$ }
            save_event_target_as = target_deposit
        }
        if = {
            limit = { not = { exists = event_target:target_situation } }
            if = {
                limit = {
                    root? = {
                        is_scope_type = situation
                        situation_is_plnmg_construction = yes
                    }
                }
                root = { save_event_target_as = target_situation }
            }
            else_if = {
                limit = {
                    prev? = {
                        is_scope_type = situation
                        situation_is_plnmg_construction = yes
                    }
                }
                prev = { save_event_target_as = target_situation }
            }
            else_if = {
                limit = {
                    from? = {
                        is_scope_type = situation
                        situation_is_plnmg_construction = yes
                    }
                }
                from = { save_event_target_as = target_situation }
            }
        }
        if = {
            limit = {
                event_target:target_situation? = {
                    is_situation_type = planet_megastructure_upgrading
                }
            }
            fire_on_action = {
                on_action = on_planetmega_upgraded
                scopes = { from = event_target:target_deposit }
            }
        }
        else_if = {
            limit = { event_target:target_situation? = { situation_is_plnmg_building = yes } }
            fire_on_action = {
                on_action = on_planetmega_built
                scopes = { from = event_target:target_deposit }
            }
        }
        else_if = {
            limit = {
                event_target:target_situation? = {
                    is_situation_type = planet_megastructure_restoration
                }
            }
            fire_on_action = {
                on_action = on_planetmega_restored
                scopes = { from = event_target:target_deposit }
            }
        }
        else = {
            fire_on_action = {
                on_action = on_planetmega_built
                scopes = { from = event_target:target_deposit }
            }
        }
    ]
}

remove_plnmg_mega_flag_indiv = {
    optimize_memory
    #对多个同类型的保底
    set_variable = { which = num_planet_$TYPE$ value = 0 }
    every_deposit = {
        limit = { is_deposit_type = d_$TYPE$ }
        prev = { change_variable = { which = num_planet_$TYPE$ value = 1 } }
    }
    if = {
        limit = {
            check_variable = { which = num_planet_$TYPE$ value <= 0 }
            has_carrier_flag = planet_has_$TYPE$
        }
        remove_carrier_flag = planet_has_$TYPE$
    }
    clear_variable = num_planet_$TYPE$
    #保底结束
    set_variable = { which = num_plnmg_mega_owned_count value = 0 }
    every_deposit = {
        limit = { is_plnmg_any_mega = yes }
        set_deposit_flag = keep_deposit_on_terraform
        prev = { change_variable = { which = num_plnmg_mega_owned_count value = 1 } }
    }
    if = {
        limit = { check_variable = { which = num_plnmg_mega_owned_count value <= 0 } }
        if = {
            limit = { has_carrier_flag = planet_has_groundmega }
            remove_carrier_flag = planet_has_groundmega
        }
        if = {
            limit = { has_modifier = planetmega_has_a_megastructure }
            remove_modifier = planetmega_has_a_megastructure
        }
    }

    if = {
        limit = {
            has_carrier_flag = planet_has_improved_groundmega
            has_plnmg_improved_mega = no
        }
        remove_carrier_flag = planet_has_improved_groundmega
    }
    if = {
        limit = {
            has_carrier_flag = planet_has_advanced_groundmega
            has_plnmg_advanced_mega = no
        }
        remove_carrier_flag = planet_has_advanced_groundmega
    }
    if = {
        limit = {
            has_carrier_flag = planet_has_ultimate_groundmega
            has_plnmg_ultimate_mega = no
        }
        remove_carrier_flag = planet_has_ultimate_groundmega
    }
}
remove_plnmg_mega = {
    optimize_memory

    remove_deposit = d_$TYPE$

    remove_plnmg_mega_flag_indiv = { TYPE = $TYPE$ }

    # specially for war t1 & t2
    if = {
        limit = {
            nor = {
                has_deposit = d_plnmg_maginot_nexus
                has_carrier_flag = planet_has_plnmg_maginot_nexus
            }
            exists = orbital_defence.starbase
        }
        orbital_defence = {
            if = {
                limit = { has_modifier = plnmg_maginot_nexus_ring_buff }
                remove_modifier = plnmg_maginot_nexus_ring_buff
            }
        }
    }
    if = {
        limit = {
            nor = {
                has_carrier_flag = planet_has_war_headquarter
                has_carrier_flag = planet_has_plnmg_maginot_nexus
            }
            has_carrier_flag = cannot_be_hit_by_aow_starfall_spear
            #奇观扩展
            if = {
                limit = { has_global_flag = AOW_installed }
                solar_system = {
                    NOR = {
                        has_megastructure = AOW_MEGA_celestial_shield_0
                        has_megastructure = AOW_MEGA_celestial_shield_1
                        has_megastructure = AOW_MEGA_celestial_shield_2
                        has_megastructure = AOW_MEGA_celestial_shield_3
                        has_megastructure = AOW_MEGA_celestial_shield_4
                        has_megastructure = AOW_MEGA_celestial_shield_create_bunker_rings
                    }
                }
            }
        }
        remove_carrier_flag = cannot_be_hit_by_aow_starfall_spear
    }
    if = {
        limit = { not = { has_carrier_flag = planet_has_plnmg_maginot_nexus } }
        solar_system = {
            # if = {
            #     limit = { has_star_flag = ai_dyson_gun_limit }
            #     remove_star_flag = ai_dyson_gun_limit
            # }
            if = {
                limit = {
                    has_star_flag = cannot_be_hit_by_aow_starfall_spear
                    if = {
                        limit = { has_global_flag = AOW_installed } #奇观扩展
                        NOR = {
                            has_megastructure = AOW_MEGA_celestial_shield_0
                            has_megastructure = AOW_MEGA_celestial_shield_1
                            has_megastructure = AOW_MEGA_celestial_shield_2
                            has_megastructure = AOW_MEGA_celestial_shield_3
                            has_megastructure = AOW_MEGA_celestial_shield_4
                            has_megastructure = AOW_MEGA_celestial_shield_create_bunker_rings
                        }
                    }
                }
                remove_star_flag = cannot_be_hit_by_aow_starfall_spear
            }
        }
    }

    [[on_actions]
        fire_on_action = { on_action = on_planetmega_removed }
    ]
}

rebuild_plnmg_mega_indiv = {
    if = {
        limit = { has_carrier_flag = planet_has_$TYPE$ }
        planet = { remove_plnmg_mega_flag_indiv = { TYPE = $TYPE$ } }
        planet = { add_plnmg_mega = { TYPE = $TYPE$ } }
    }
}
rebuild_plnmg_all_mega_destroyed = {
    optimize_memory
    rebuild_plnmg_mega_indiv = { TYPE = great_laboratory }
    rebuild_plnmg_mega_indiv = { TYPE = advanced_forge }
    rebuild_plnmg_mega_indiv = { TYPE = heaven_colossal }
    rebuild_plnmg_mega_indiv = { TYPE = giant_power_battery }
    rebuild_plnmg_mega_indiv = { TYPE = commercial_shikara }
    rebuild_plnmg_mega_indiv = { TYPE = war_headquarter }
    rebuild_plnmg_mega_indiv = { TYPE = centralized_manufactory }
    rebuild_plnmg_mega_indiv = { TYPE = primary_mind_pyramid }
    rebuild_plnmg_mega_indiv = { TYPE = matter_demodulator }
    rebuild_plnmg_mega_indiv = { TYPE = vertical_farm_edifice }
    rebuild_plnmg_mega_indiv = { TYPE = hive_farm_edifice }
    rebuild_plnmg_mega_indiv = { TYPE = admin_axis }
    rebuild_plnmg_mega_indiv = { TYPE = ecodome_complex }
    rebuild_plnmg_mega_indiv = { TYPE = logistics_shikara }
    rebuild_plnmg_mega_indiv = { TYPE = resistance_organ }
    #
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_singularity_generator }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_bore_of_the_shroud }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_maginot_nexus }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_advanced_agriculture_edifice }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_gene_farm_hive }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_supercom }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_fusion_brain }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_nirvana_factory }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_financial_shikara }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_tms_control }
    #
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_colossus_assist }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_extra_galaxy }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_omnipotence }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_nano_storm }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_world_shaper }
    #
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_wilderness_flora }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_wilderness_terra }
    rebuild_plnmg_mega_indiv = { TYPE = plnmg_reincarnation }
    every_deposit = {
        limit = { is_plnmg_any_mega = yes }
        set_deposit_flag = keep_deposit_on_terraform
    }
}

start_planetmega_construction = {
    # cannot be used with planetmega that is upgraded into
    add_deposit = d_$TYPE$
    if = {
        limit = { last_added_deposit = { is_plnmg_ordinary_mega = yes } }
        owner = {
            start_situation = {
                type = planet_megastructure_construction
                target = prev
                effect = { set_situation_flag = building_$TYPE$ }
            }
        }
    }
    else_if = {
        limit = {
            last_added_deposit = {
                is_plnmg_improved_mega = yes
                is_plnmg_upgraded_mega = no
            }
        }
        owner = {
            start_situation = {
                type = planet_megastructure_constimp
                target = prev
                effect = { set_situation_flag = building_$TYPE$ }
            }
        }
    }
    else_if = {
        limit = { last_added_deposit = { is_plnmg_advanced_mega = yes } }
        owner = {
            start_situation = {
                type = planet_megastructure_constadv
                target = prev
                effect = { set_situation_flag = building_$TYPE$ }
            }
        }
    }
    else_if = {
        limit = { last_added_deposit = { is_plnmg_ultimate_mega = yes } }
        owner = {
            start_situation = {
                type = planet_megastructure_constuti
                target = prev
                effect = { set_situation_flag = building_$TYPE$ }
            }
        }
    }
    remove_deposit = d_$TYPE$
}

restore_plnmg_docked_defence_ship = {
    if = {
        limit = { check_variable = { which = var_plnmg_defence_ship value > 100 } }
        set_variable = { which = var_plnmg_defence_ship_new value = var_plnmg_defence_ship }
        set_variable = { which = var_plnmg_defence_ship_hundred value = 0 }
        while = {
            limit = { check_variable = { which = var_plnmg_defence_ship_new value < 100 } }
            subtract_variable = { which = var_plnmg_defence_ship_new value = 100 }
            change_variable = { which = var_plnmg_defence_ship_hundred value = 1 }
        }
        while = {
            count = var_plnmg_defence_ship_hundred
            while = {
                count = 100
                add_deposit = d_plnmg_docked_defence_ship
            }
        }
        if = {
            limit = { check_variable = { which = var_plnmg_defence_ship_new value > 0 } }
            while = {
                count = var_plnmg_defence_ship_new
                add_deposit = d_plnmg_docked_defence_ship
            }
        }
        clear_variable = var_plnmg_defence_ship_new
        clear_variable = var_plnmg_defence_ship_hundred
    }
    else = {
        while = {
            count = var_plnmg_defence_ship
            add_deposit = d_plnmg_docked_defence_ship
        }
    }
}

restore_planetmega_on_cleared = {
    if = {
        limit = {
            nor = {
                has_deposit = d_$TYPE$
                has_carrier_flag = planet_has_$TYPE$
                any_targeting_situation = { has_situation_flag = building_$TYPE$ }
            }
        }
        add_plnmg_mega = { TYPE = $TYPE$ }
        random_deposit = {
            limit = { is_deposit_type = d_$TYPE$ }
            save_event_target_as = target_deposit
        }
        fire_on_action = {
            on_action = on_planetmega_restored
            scopes = { from = event_target:target_deposit }
        }
    }
}
