
send_plnmg_exp_fleet_out = {
    user_scope = megastructure
    scope = galactic_object

    #button_clickable似乎视同button_visible
    button_visible = {
        owner? = {
            not = { has_country_flag = plnmg_exp_big_panel_running }
            has_technology = tech_planetmega_transfer
        }
    }

    ai = {
        ai_minister = none
        weight = { base = 0 }
    }

    icon = GFX_ship_class_conduit
    icon_selected = GFX_ship_class_conduit

    on_completed = on_plnmg_exp_fleet_sent
    context_menu_name = "send_plnmg_exp_fleet_out_name"
    activity_key = "send_plnmg_exp_fleet_out_name"
    tooltip = send_plnmg_exp_fleet_out_name
    cooldown = 1
    required_progress = 1

    begin_anim_state = "working"
    progress_anim_state = "working_looping"

    # cost = { #not works in user_scope=megastructure
    #     energy = 1000
    #     minerals = 1000
    #     alloys = 500
    #     influence = 200
    # }
}

plnmg_pmcar_split = {
    user_scope = fleet
    scope = planet

    possible = {
        planet_size > 1
        is_colony = no
        custom_tooltip = {
            fail_text = reach_plnmg_pmcar_split_limit
            from = {
                or = {
                    not = { is_variable_set = planet_sizes_carried }
                    check_variable = { which = planet_sizes_carried value < 20 }
                    and = {
                        check_variable = { which = planet_sizes_carried value < 50 }
                        any_owned_ship = {
                            check_modifier_value = {
                                modifier = plnmg_plntmatter_carrier_gen
                                value >= 2
                            }
                        }
                    }
                    and = {
                        check_variable = { which = planet_sizes_carried value < 100 }
                        any_owned_ship = {
                            check_modifier_value = {
                                modifier = plnmg_plntmatter_carrier_gen
                                value >= 3
                            }
                        }
                    }
                }
            }
        }
        or = {
            custom_tooltip = {
                fail_text = requires_targeting_uninhabitable_regular_planet
                or = {
                    uninhabitable_regular_planet = yes
                    #habitable_planet = yes
                    is_planet_class = pc_nuked
                }
            }
            and = {
                custom_tooltip = {
                    fail_text = requires_plnmg_plntmatter_carrier_gen_2
                    from = {
                        any_owned_ship = {
                            check_modifier_value = {
                                modifier = plnmg_plntmatter_carrier_gen
                                value >= 2
                            }
                        }
                    }
                }
                custom_tooltip = {
                    fail_text = requires_targeting_common_star
                    is_plnmg_common_star = yes
                }
            }
            # or = {
            #     is_planet_class = pc_black_hole
            #     is_planet_class = pc_rift_star
            #     is_planet_class = pc_astral_scar
            # }
        }
        nor = {
            has_modifier = harvested_resources_mining
            has_modifier = planetmega_ca_harvested
        }
        or = {
            custom_tooltip = {
                fail_text = no_space_owner
                not = { exists = space_owner }
            }
            space_owner = {
                or = {
                    custom_tooltip = {
                        fail_text = space_owner_is_not_us
                        is_same_value = from.controller
                    }
                    custom_tooltip = {
                        fail_text = is_not_hostile
                        is_hostile = from.controller
                    }
                    has_overlord = from.controller
                    opinion_level = { who = from.controller level > good }
                }
            }
        }
    }

    on_completed = on_plnmg_pmcar_split_done
    tooltip = plnmg_pmcar_split_button
    icon = GFX_fleet_action_button_arkship_strip_mine
    icon_selected = GFX_fleet_action_button_arkship_strip_mine
    activity_key = plnmg_pmcar_split_progress_activity
    context_menu_name = plnmg_pmcar_split_menu_title
    progress_activity_key = plnmg_pmcar_split_progress_activity

    cooldown = 45
    required_progress = 15
    required_progress_key = plnmg_pmcar_split_progress_needed
    slot = 1

    cost = { energy = 500 }

    ai = { # this = planet, from = fleet
        weight = {
            base = @ai_action_weight_15
            inline_script = {
                script = scripted_actions/ai_distance_modifiers
                LOCAL_SYSTEM_CHECK = "any_system_planet = { is_same_value = ROOT }"
            }
            inline_script = scripted_actions/ai_general_automation_modifiers
            modifier = {
                factor = 0
                distance = { source = FROM use_bypasses = yes max_jumps >= @ai_action_weight_5 }
            }
        }

        ai_minister = interior
        max_jumps = @ai_action_weight_5 #并不真正生效，需要在weight内自己再写个modifier entry
        good_enough_weight = @ai_action_weight_30
    }
    automation = {
        priority	= 1
        default_on	= yes
        tooltip		= plnmg_pmcar_split_tooltip
        options = {
            inline_script = scripted_actions/ai_general_automation_options
        }
    }
}

plnmg_pmcar_append = {
    user_scope = fleet
    scope = planet

    possible = {
        custom_tooltip = {
            fail_text = requires_planet_sizes_pmcar_carried
            from = {
                is_variable_set = planet_sizes_carried
                check_variable = { which = planet_sizes_carried value > 0 }
            }
        }
        custom_tooltip = {
            fail_text = requires_targeting_regular_planet
            or = {
                uninhabitable_regular_planet = yes
                habitable_planet = yes
                is_planet_class = pc_nuked
            }
        }
        or = {
            custom_tooltip = {
                success_text = no_space_owner
                not = { exists = owner_or_space_owner }
            }
            owner_or_space_owner = {
                or = {
                    is_same_value = from.controller
                    #opinion_level = { who = from.controller level >= excellent }
                    has_overlord = from.controller
                    has_subject = from.controller
                    and = {
                        custom_tooltip = {
                            fail_text = is_not_primitive
                            is_country_type = primitive
                        }
                        custom_tooltip = {
                            fail_text = primitives_not_in_borders
                            prev.space_owner? = {
                                or = {
                                    is_same_value = from.controller
                                    has_overlord = from.controller
                                    has_subject = from.controller
                                }
                            }
                        }
                        custom_tooltip = {
                            fail_text = primitives_not_full_interference
                            from.controller = { has_policy_flag = interference_aggressive }
                        }
                    }
                }
            }
        }
    }

    on_completed = on_plnmg_pmcar_append_done
    tooltip = plnmg_pmcar_append_button
    icon = GFX_fleet_action_button_arkship_reprocess_planet #temp
    icon_selected = GFX_fleet_action_button_arkship_reprocess_planet #temp
    activity_key = plnmg_pmcar_append_progress_activity
    context_menu_name = plnmg_pmcar_append_menu_title
    progress_activity_key = plnmg_pmcar_append_progress_activity

    cooldown = 45
    required_progress = 15
    required_progress_key = plnmg_pmcar_split_progress_needed
    slot = 2

    cost = { energy = 500 }
}

### override vanilla scripted actions ###

# arkship_empty_spine = {
#     user_scope = fleet
#     scope = galactic_object
#
#     on_completed = on_arkship_empty_spine
#     tooltip = arkship_empty_spine_button
#     icon = GFX_fleet_action_button_arkship_empty_spine
#     icon_selected = GFX_fleet_action_button_arkship_empty_spine
#     context_menu_name = arkship_empty_spine
#
#     possible = {
#         always = yes
#     }
#
#     hide_from_context_menu = {
#         always = yes
#     }
#
#     button_visible = {
#         custom_tooltip = {
#             fail_text = arkship_empty_spine_tt
#             starbase? = {
#                 NOR = {
#                     has_starbase_module = arkship_spinal_devastator_mount
#                     has_starbase_module = arkship_capital_shipyard
#                     has_starbase_module = arkship_planetary_refinery
#                     has_starbase_module = arkship_stellar_igniter
#                     has_starbase_module = arkship_system_scanner
#                     has_starbase_module = arkship_exodus_jump
#                 }
#             }
#         }
#     }
#
#     button_clickable = {
#         custom_tooltip = {
#             fail_text = arkship_empty_spine_tt
#             always = no
#         }
#     }
#
#     slot = 5
# }

### test

# test_plnmg_megastructure_relocate = {
#     user_scope = megastructure
#     scope = galactic_object
#
#     #button_clickable似乎视同button_visible
#     button_visible = {
#         #is_megastructure_relocatable = yes
#     }
#
#     ai = {
#         ai_minister = none #military
#         weight = {
#             base = 0
#             # modifier = {
#             #     weight = 10
#             #     is_inside_border = from.owner
#             #     space_owner = { is_same_value = prev.owner }
#             #     from.solar_system = { any_fleet_in_system = { controller = { is_hostile = root.from.owner } } }
#             # }
#         }
#     }
#
#     icon = GFX_ship_class_conduit
#     icon_selected = GFX_ship_class_conduit
#
#     on_completed = on_test_plnmg_megastructure_relocated
#     #i.e. planetmega_easter.999 is triggered
#     context_menu_name = "test_plnmg_megastructure_relocate"
#     activity_key = "test_plnmg_megastructure_relocate"
#     tooltip = test_plnmg_megastructure_relocate
#     cooldown = 1
#     required_progress = 1
#
#     begin_anim_state = "working"
#     progress_anim_state = "working_looping"
# }
