
###planetmega_intersys_strike

plnmg_itss_PLANET_KILLER_CRACKER = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_pk_cracker }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_CRACKER = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_CRACKER } }
}
plnmg_itss_PLANET_KILLER_SHIELDER = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_pk_shielder }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_SHIELDER = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_SHIELDER } }
}
plnmg_itss_PLANET_KILLER_NEUTRON = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_pk_neutron }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_NEUTRON = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_NEUTRON } }
}
plnmg_itss_PLANET_KILLER_GODRAY = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_pk_godray }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_GODRAY = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_GODRAY } }
}
plnmg_itss_PLANET_KILLER_NANOBOTS = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_pk_nanobots }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_NANOBOTS = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_NANOBOTS } }
}
plnmg_itss_PLANET_KILLER_DELUGE = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_ascension_perk = ap_hydrocentric }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_DELUGE = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_DELUGE } }
}
plnmg_itss_PLANET_KILLER_TOXIFIER = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_plnmg_pk_toxifier }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_TOXIFIER = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_TOXIFIER } }
}
plnmg_itss_PLANET_KILLER_DEVOLUTION = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_archaeo_pk_devolving_beam }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_DEVOLUTION = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_DEVOLUTION } }
}
plnmg_itss_PLANET_KILLER_MAW_AEG = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = {
            has_technology = tech_plnmg_optimized_colossus
            any_owned_ship = { has_component = PLANET_KILLER_MAW }
        }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_MAW = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_MAW_AEG } }
}
plnmg_itss_PLANET_KILLER_SMELTER = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4plnt_ptt = yes
    }
    allow = {
        from = { has_technology = tech_pk_smelter }
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4plnt_allow = yes
        can_destroy_planet_with_PLANET_KILLER_SMELTER = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_SMELTER } }
}

plnmg_itss_PLANET_KILLER_STAR_CRACKER = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4star_ptt = yes
    }
    allow = {
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        from = {
            or = {
                has_menace_perk = menp_star_eater
                has_ascension_perk = ap_become_the_crisis
                hidden_trigger = { has_country_flag = can_build_star_eaters }
            }
        }
        is_plnmg_itss_weapon4star_allow = yes
        can_destroy_planet_with_PLANET_KILLER_STAR_CRACKER = yes
    }
    effect = { plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_STAR_CRACKER } }
}
plnmg_itss_PLANET_KILLER_STAR_CRACKER_LITE_PLNMG = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4star_ptt = yes
    }
    allow = {
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        from = {
            has_technology = tech_plnmg_btc_1
            nor = {
                has_menace_perk = menp_star_eater
                has_ascension_perk = ap_become_the_crisis
            }
        }
        is_plnmg_itss_weapon4star_allow = yes
        can_destroy_planet_with_PLANET_KILLER_STAR_CRACKER = yes
    }
    effect = {
        plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_STAR_CRACKER_LITE_PLNMG }
    }
}
plnmg_itss_PLANET_KILLER_PLNMG_STAR_OSCILLATOR = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4star_ptt = yes
    }
    allow = {
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        is_plnmg_itss_weapon4star_allow = yes
        custom_tooltip = {
            #fail_text = is_not_star #WTF why PDX add “右斜杠n” specifically for this tooltip???
            fail_text = is_not_star_plnmg
            solar_system = { not = { is_star_class = sc_rift_star } }
        }
        custom_tooltip = {
            fail_text = star_owner_is_not_hostile
            or = {
                not = { exists = space_owner }
                space_owner = { is_hostile = from }
            }
        }
    }
    effect = {
        plnmg_itss_specific_plnt_weapon_effect = { TYPE = PLANET_KILLER_PLNMG_STAR_OSCILLATOR }
    }
}

plnmg_itss_MEGASTRUCTURE_RUINER = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4others_ptt = yes
    }
    allow = {
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        custom_tooltip = {
            fail_text = is_not_megastructure
            is_scope_type = megastructure
        }
        custom_tooltip = {
            fail_text = is_not_hostile
            or = {
                not = { exists = owner }
                owner = { is_hostile = from }
            }
        }
    }
    effect = {
        hidden_effect = {
            if = {
                limit = { has_megastructure_flag = plnmg_itss_MEGASTRUCTURE_RUINER_of_@from }
                remove_megastructure_flag = plnmg_itss_MEGASTRUCTURE_RUINER_of_@from
            }
            else = { set_megastructure_flag = plnmg_itss_MEGASTRUCTURE_RUINER_of_@from }
        }
    }
}

plnmg_itss_FLEET_DESTROYER = {
    potential = {
        can_be_destroyed_with_any_plnmg_itss_ptt = yes
        is_plnmg_itss_weapon4others_ptt = yes
    }
    allow = {
        can_be_destroyed_with_any_plnmg_itss_allow = yes
        custom_tooltip = {
            fail_text = is_not_fleet
            is_scope_type = fleet
        }
        custom_tooltip = {
            fail_text = is_not_hostile
            or = {
                not = { exists = controller }
                controller = { is_hostile = from }
            }
        }
    }
    effect = {
        hidden_effect = {
            if = {
                limit = { has_fleet_flag = plnmg_itss_FLEET_DESTROYER_of_@from }
                remove_fleet_flag = plnmg_itss_FLEET_DESTROYER_of_@from
            }
            else = { set_fleet_flag = plnmg_itss_FLEET_DESTROYER_of_@from }
        }
    }
}

plnmg_itss_plnt_boom_effect = {
    potential = { is_scope_type = planet }
    allow = {
        custom_tooltip = {
            fail_text = not_any_plnmg_itss_weapon_enabled
            plnmg_itss_plnt_weapon_enabled_if_any = yes
        }
        event_target:fire_launcher_of_@from = {
            owner = {
                resource_stockpile_compare = {
                    resource = energy
                    value >= @plntITSS_rechargeBase
                    mult = prev.value:sv_plnmg_itss_recharge_cost_mult
                }
            }
        }
    }
    effect = {
        hidden_effect = {
            switch = {
                trigger = has_planet_flag
                plnmg_itss_PLANET_KILLER_CRACKER_of_@from = {
                    create_plnmg_colossus_hit_se = { ao = colossus_shatter_planet_hit_object }
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_CRACKER
                    }
                }
                plnmg_itss_PLANET_KILLER_SHIELDER_of_@from = {
                    create_plnmg_colossus_hit_se = { ao = colossus_shield_planet_hit_object }
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_SHIELDER
                    }
                }
                plnmg_itss_PLANET_KILLER_NEUTRON_of_@from = {
                    create_plnmg_colossus_hit_se = { ao = colossus_neutron_planet_hit_object }
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_NEUTRON
                    }
                }
                plnmg_itss_PLANET_KILLER_GODRAY_of_@from = {
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_GODRAY
                    }
                    if = {
                        limit = { is_artificial = yes }
                        create_plnmg_colossus_hit_se = { ao = colossus_god_ray_megastructure_hit_object }
                    }
                    else = { create_plnmg_colossus_hit_se = { ao = colossus_god_ray_hit_object } }
                }
                plnmg_itss_PLANET_KILLER_NANOBOTS_of_@from = {
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_NANOBOTS
                    }
                    if = {
                        limit = { is_artificial = yes }
                        create_plnmg_colossus_hit_se = { ao = colossus_assimilate_megastructure_hit_object }
                    }
                    else = { create_plnmg_colossus_hit_se = { ao = colossus_assimilate_hit_object } }
                }
                plnmg_itss_PLANET_KILLER_DELUGE_of_@from = {
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_DELUGE
                    }
                    if = {
                        limit = { is_artificial = yes }
                        create_plnmg_colossus_hit_se = { ao = deluge_planet_hit_object }
                    }
                    else = { create_plnmg_colossus_hit_se = { ao = deluge_megastructure_hit_object } }
                }
                plnmg_itss_PLANET_KILLER_TOXIFIER_of_@from = {
                    create_plnmg_colossus_hit_se = { ao = toxifier_planet_hit_object }
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_TOXIFIER
                    }
                }
                plnmg_itss_PLANET_KILLER_DEVOLUTION_of_@from = {
                    create_plnmg_colossus_hit_se = { ao = colossus_neutron_planet_hit_object }
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_DEVOLUTION
                    }
                }
                plnmg_itss_PLANET_KILLER_MAW_AEG_of_@from = {
                    # sorry but this do not have a proper entity
                    plnmg_itss_colossus_fire_no_check = { event = planetmega_pk.50 }
                }
                plnmg_itss_PLANET_KILLER_SMELTER_of_@from = {
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_SMELTER
                    }
                    if = {
                        limit = { is_artificial = yes }
                        create_plnmg_colossus_hit_se = { ao = infernal_colossus_planet_hit_object }
                    }
                    else = { create_plnmg_colossus_hit_se = { ao = infernal_colossus_megastructure_hit_object } }
                }
                ###
                plnmg_itss_PLANET_KILLER_STAR_CRACKER_of_@from = {
                    create_plnmg_colossus_system_hit_se = {
                        ao = star_eater_hit_object planets = yes megastructures = yes
                    }
                    plnmg_itss_colossus_fire_no_check = {
                        on_action = on_destroy_planet_with_PLANET_KILLER_STAR_CRACKER
                    }
                }
                plnmg_itss_PLANET_KILLER_STAR_CRACKER_LITE_PLNMG_of_@from = {
                    create_plnmg_colossus_system_hit_se = {
                        ao = star_eater_hit_object planets = yes megastructures = yes
                    }
                    plnmg_itss_colossus_fire_no_check = { event = planetmega_pk.80 }
                }
                plnmg_itss_PLANET_KILLER_PLNMG_STAR_OSCILLATOR_of_@from = {
                    plnmg_itss_colossus_fire_no_check = { event = planetmega_pk.20 }
                }
                default = { from = { country_event = { id = planetmega_pk.41 } } }
            }
            remove_all_plnmg_itss_plnt_weapon_flags = yes
        }
    }
}

plnmg_itss_mega_boom_effect = {
    potential = { is_scope_type = megastructure }
    allow = {
        custom_tooltip = {
            fail_text = not_any_plnmg_itss_weapon_enabled
           or = {
               has_megastructure_flag = plnmg_itss_MEGASTRUCTURE_RUINER_of_@from
           }
        }
        if = {
            limit = { hidden_trigger = { planet? = { is_star = yes } } }
            event_target:fire_launcher_of_@from = {
                owner = {
                    resource_stockpile_compare = {
                        resource = energy
                        value >= @plntITSS_rechargeBase
                        mult = prev.value:sv_plnmg_itss_recharge_cost_mult
                    }
                }
            }
        }
        else = {
            event_target:fire_launcher_of_@from = {
                owner = {
                    resource_stockpile_compare = {
                        resource = energy
                        value >= @plntITSS_rechargeSmall
                        mult = prev.value:sv_plnmg_itss_recharge_cost_mult
                    }
                }
            }
        }
    }
    effect = {
        hidden_effect = {
            if = {
                limit = { hidden_trigger = { planet? = { is_star = yes } } }
                event_target:fire_launcher_of_@from = {
                    owner = {
                        add_resource = {
                            energy = @plntITSS_rechargeBase_neg
                            mult = prev.value:sv_plnmg_itss_recharge_cost_mult
                        }
                    }
                }
            }
            else = {
                event_target:fire_launcher_of_@from = {
                    owner = {
                        add_resource = {
                            energy = @plntITSS_rechargeSmall_neg
                            mult = prev.value:sv_plnmg_itss_recharge_cost_mult
                        }
                    }
                }
            }
            switch = {
                trigger = has_megastructure_flag
                plnmg_itss_MEGASTRUCTURE_RUINER_of_@from = {
                    if = {
                        limit = { exists = owner }
                        from = { save_event_target_as = mega_ruiner }
                        solar_system = { save_event_target_as = location_system }
                        owner = { country_event = { id = planetmega_pk.45 } }
                    }
                    create_plnmg_colossus_hit_se = { ao = colossus_shatter_planet_hit_object }
                    set_ruined_megastructure = yes
                }
                default = { from = { country_event = { id = planetmega_pk.41 } } }
            }
            if = {
                limit = { has_megastructure_flag = plnmg_itss_MEGASTRUCTURE_RUINER_of_@from }
                remove_megastructure_flag = plnmg_itss_MEGASTRUCTURE_RUINER_of_@from
            }
        }
    }
}

plnmg_itss_fleet_boom_effect = {
    potential = { is_scope_type = fleet }
    allow = {
        custom_tooltip = {
            fail_text = not_any_plnmg_itss_weapon_enabled
            or = {
                has_fleet_flag = plnmg_itss_FLEET_DESTROYER_of_@from
            }
        }
        event_target:fire_launcher_of_@from = {
            owner = {
                resource_stockpile_compare = {
                    resource = energy
                    value >= 1
                    mult = prev.value:sv_plnmg_itss_fleet_recharge_cost_mult
                }
            }
        }
    }
    effect = {
        hidden_effect = {
            switch = {
                trigger = has_fleet_flag
                plnmg_itss_FLEET_DESTROYER_of_@from = {
                    every_owned_ship = { damage_ship = { amount = value:sv_plnmg_total_ship_hp attacker = from } }
                }
                default = { from = { country_event = { id = planetmega_pk.41 } } }
            }
            event_target:fire_launcher_of_@from = {
                owner = {
                    add_resource = {
                        energy = -1
                        mult = prev.value:sv_plnmg_itss_fleet_recharge_cost_mult
                    }
                }
            }
            if = {
                limit = { has_fleet_flag = plnmg_itss_FLEET_DESTROYER_of_@from }
                remove_fleet_flag = plnmg_itss_FLEET_DESTROYER_of_@from
            }
        }
    }
}

plnmg_itss_clear_all_effect = {
    potential = { always = yes }
    allow = { always = yes }
    effect = {
        #custom_tooltip = plnmg_itss_clear_all_desc
        hidden_effect = {
            if = {
                limit = { is_scope_type = planet }
                remove_all_plnmg_itss_plnt_weapon_flags = yes
                break = yes
            }
            if = {
                limit = { is_scope_type = megastructure }
                remove_megastructure_flag = plnmg_itss_MEGASTRUCTURE_RUINER_of_@from
                break = yes
            }
            if = {
                limit = { is_scope_type = fleet }
                remove_fleet_flag = plnmg_itss_FLEET_DESTROYER_of_@from
            }
        }
    }
}

plnmg_itss_selection_switch = {
    potential = { always = yes }
    allow = { always = yes }
    effect = {
        hidden_effect = {
            from = {
                if = {
                    limit = { not = { is_variable_set = plnmg_itss_selection_index } }
                    set_variable = { which = plnmg_itss_selection_index value = 1 }
                }
                else_if = {
                    limit = { check_variable = { which = plnmg_itss_selection_index value = 1 } }
                    set_variable = { which = plnmg_itss_selection_index value = 2 }
                }
                else_if = {
                    limit = { check_variable = { which = plnmg_itss_selection_index value = 2 } }
                    set_variable = { which = plnmg_itss_selection_index value = 3 }
                }
                else = { clear_variable = plnmg_itss_selection_index }
            }
        }
    }
}

###district_plnmg_expansion_ui
# this = planet

open_plnmg_effect_panel = {
    #potential = { not = { uses_district_set = cosmogenesis_world } }
    #allow = { from = { has_technology = tech_planetmega_improved } }
    potential = { check_if_owner_is_plnmg_from = yes }
    allow = { always = yes }
    effect = { carrier_event = { id = planetmega_effect.100 } }
}

open_plnmg_extra_galaxy_effect = {
    potential = { check_if_owner_is_plnmg_from = yes }
    allow = {
        has_deposit = d_plnmg_extra_galaxy
        from = { has_technology = tech_planetmega_transfer_prereq }
    }
    effect = { carrier_event = { id = planetmega_effect.80 } }
}
open_plnmg_omnipotence_effect = {
    potential = { check_if_owner_is_plnmg_from = yes  }
    allow = {
        has_deposit = d_plnmg_omnipotence
        from = { can_build_plnmg_omnipotence = yes }
    }
    effect = { carrier_event = { id = planetmega_effect.70 } }
}
open_plnmg_colossus_assist_effect = {
    potential = { check_if_owner_is_plnmg_from = yes }
    allow = {
        has_deposit = d_plnmg_colossus_assist
        custom_tooltip = {
            fail_text = plnmg_already_has_megabuild_listed
            not = { exists = event_target:fire_launcher_of_@from }
        }
        from = { has_technology = tech_planetmega_colossus }
    }
    effect = {
        tooltip = { from = { country_event = { id = planetmega_pk.40 } } }
        hidden_effect = { carrier_event = { id = planetmega_pk.39 } }
    }
}
open_plnmg_nano_storm_effect = {
    potential = { check_if_owner_is_plnmg_from = yes }
    allow = {
        has_deposit = d_plnmg_nano_storm
        from = { has_technology = tech_plnmg_nano_field_cntr }
    }
    effect = { carrier_event = { id = planetmega_effect.90 } }
}
open_plnmg_world_shaper_effect = {
    potential = { check_if_owner_is_plnmg_from = yes }
    allow = {
        has_deposit = d_plnmg_world_shaper
        #from = { has_technology = tech_planetmega_advanced }
    }
    effect = { carrier_event = { id = planetmega_effect.140 } }
}

display_plnmg_exp_extra_galaxy_info = {
    potential = { has_zone = { zone = zone_plnmg_exp_extra_galaxy } }
    allow = {
        custom_tooltip = {
            text = exp_plnmg_extra_galaxy_info_loc
            always = no
        }
    }
    #effect = {  }
}
display_plnmg_exp_world_shaper_info = {
    potential = { has_zone = { zone = zone_plnmg_exp_world_shaper } }
    allow = { always = yes }
    #effect = {}
}
display_plnmg_exp_nano_storm_info = {
    potential = { has_zone = { zone = zone_plnmg_exp_nano_storm } }
    allow = {
        custom_tooltip = {
            text = exp_plnmg_nano_storm_info_loc
            always = no
        }
    }
    #effect = {  }
}
