namespace = stellar_manip_utility

# Global flag for mod detection
event = {
    id = stellar_manip_utility.1
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        NOT = {
            has_global_flag = gigas_stellar_manip_exists
        }
    }
    immediate = {
        set_global_flag = gigas_stellar_manip_exists
    }
}

# Stellar Mass Harvesting - Star Class Change
planet_event = {
    id = stellar_manip_utility.2
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        is_scope_valid = yes
    }

    immediate = {
        remove_carrier_flag = has_megastructure
        remove_carrier_flag = megastructure
        switch = {
            trigger = is_planet_class
            pc_o_star = {
                change_pc = pc_b_star
                remove_modifier = giga_o_star_modifier
                clear_deposits = yes
                solar_system = {
                    if = {
                        limit = {
                            has_star_flag = giga_o_star_system
                        }
                        set_star_class = sc_b
                        every_ambient_object = {
                            limit = {
                                is_ambient_object_type = giga_o_star_nebula_1
                            }
                            destroy_ambient_object = this
                        }
                    }
                }
            }
            pc_giga_o_star = {
                change_pc = pc_b_star
                remove_modifier = giga_o_star_modifier
                clear_deposits = yes
                solar_system = {
                    if = {
                        limit = {
                            has_star_flag = giga_o_star_system
                        }
                        set_star_class = sc_b
                        every_ambient_object = {
                            limit = {
                                is_ambient_object_type = giga_o_star_nebula_1
                            }
                            destroy_ambient_object = this
                        }
                    }
                }
            }
            pc_o_super_star = {
                change_pc = pc_b_star
                remove_modifier = giga_o_star_modifier
                clear_deposits = yes
                solar_system = {
                    if = {
                        limit = {
                            has_star_flag = giga_o_star_system
                        }
                        set_star_class = sc_b
                        every_ambient_object = {
                            limit = {
                                is_ambient_object_type = giga_o_star_nebula_1
                            }
                            destroy_ambient_object = this
                        }
                    }
                }
            }
            pc_o_hyper_star = {
                change_pc = pc_b_star
                remove_modifier = giga_o_star_modifier
                clear_deposits = yes
                solar_system = {
                    if = {
                        limit = {
                            has_star_flag = giga_o_star_system
                        }
                        set_star_class = sc_b
                        every_ambient_object = {
                            limit = {
                                is_ambient_object_type = giga_o_star_nebula_1
                            }
                            destroy_ambient_object = this
                        }
                    }
                }
            }
            pc_b_star = {
                change_pc = pc_a_star
                solar_system = {
                    if = {
                        limit = {
                            giga_is_bitrinary = no
                        }
                        set_star_class = sc_a
                    }
                }
            }
            pc_m_giant_star = {
                change_pc = pc_a_star
                solar_system = {
                    if = {
                        limit = {
                            giga_is_bitrinary = no
                        }
                        set_star_class = sc_a
                    }
                }
            }
            pc_a_star = {
                change_pc = pc_f_star
                solar_system = {
                    if = {
                        limit = {
                            giga_is_bitrinary = no
                        }
                        set_star_class = sc_f
                    }
                }
            }
            pc_f_star = {
                change_pc = pc_g_star
                solar_system = {
                    if = {
                        limit = {
                            giga_is_bitrinary = no
                        }
                        set_star_class = sc_g
                    }
                }
            }
            pc_g_star = {
                change_pc = pc_k_star
                solar_system = {
                    if = {
                        limit = {
                            giga_is_bitrinary = no
                        }
                        set_star_class = sc_k
                    }
                }
            }
            pc_k_star = {
                change_pc = pc_m_star
                solar_system = {
                    if = {
                        limit = {
                            giga_is_bitrinary = no
                        }
                        set_star_class = sc_m
                    }
                }
            }
            pc_m_star = {
                change_pc = pc_t_star
                solar_system = {
                    if = {
                        limit = {
                            giga_is_bitrinary = no
                        }
                        set_star_class = sc_t
                    }
                }
            }
        }
        save_event_target_as = star_manip_planet
        if = {
            #automatic harvesting
            limit = {
                from.owner = {
                    has_policy_flag = policy_automatic_harvest
                }
            }

            if = {
                #harvested down to class T
                limit = {
                    is_planet_class = pc_t_star
                }
                space_owner = {
                    country_event = {
                        id = stellar_manip_dialogue.6
                    }
                }
                stellar_manip_remove_applicable_harvester_mega = yes
                destroy_situation = from
            }

            else = {
                if = {
                    #check for energy
                    limit = {
                        space_owner = {
                            has_resource = {
                                type = energy
                                amount >= 5000
                            }
                        }
                    }
                    space_owner = {
                        add_resource = {
                            energy = -5000
                        }
                        set_country_flag = auto_harvester
                    }
                    stellar_manip_remove_applicable_harvester_mega = yes
                    solar_system = {
                        spawn_megastructure = {
                            type = stellar_manipulator_2
                            owner = ROOT.space_owner
                            planet = ROOT
                        }
                    }

                    from.owner = {
                        start_situation = {
                            type = situation_stellar_extraction
                            target = root
                        }
                    }
                    destroy_situation = from

                }
                else = {
                    #no energy
                    stellar_manip_remove_applicable_harvester_mega = yes
                    space_owner = {
                        country_event = {
                            id = stellar_manip_dialogue.7
                        }
                        add_resource = {
                            alloys = 5000
                        }
                    }
                    destroy_situation = from
                }

            }
        }
        else = {
            #manual harvesting
            space_owner = {
                country_event = {
                    id = stellar_manip_dialogue.5
                }
            }
            stellar_manip_remove_applicable_harvester_mega = yes
            destroy_situation = from
        }

    }
}

# Stellar Mass Injection - Star Class Change
planet_event = {
    id = stellar_manip_utility.3
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        is_scope_valid = yes
    }
    immediate = {
        remove_carrier_flag = has_megastructure
        remove_carrier_flag = megastructure
        save_event_target_as = star_manip_planet

        #Star class change
        if = {
            limit = {
                giga_is_b_star_for_megas = yes
            }

            solar_system = {
                stellar_manip_create_o_star = yes
            }

        }
        else_if = {
            limit = {
                giga_is_m_giant_star_for_megas = yes
            }

            solar_system = {
                set_star_class = sc_b
            }
        }

        else_if = {
            limit = {
                giga_is_a_star_for_megas = yes
            }

            solar_system = {
                set_star_class = sc_m_giant
            }
        }

        else_if = {
            limit = {
                giga_is_f_star_for_megas = yes
            }

            solar_system = {
                set_star_class = sc_a
            }
        }

        else_if = {
            limit = {
                giga_is_g_star_for_megas = yes
            }

            solar_system = {
                set_star_class = sc_f
            }
        }

        else_if = {
            limit = {
                giga_is_k_star_for_megas = yes
            }

            solar_system = {
                set_star_class = sc_g
            }
        }

        else_if = {
            limit = {
                giga_is_m_star_for_megas = yes
            }

            solar_system = {
                set_star_class = sc_k
            }
        }

        # Other stuff
        if = {
            #automatic injection
            limit = {
                from.owner = {
                    has_policy_flag = policy_automatic_injection
                }
            }
            if = {
                #injected up to class O
                limit = {
                    giga_is_o_star_for_megas = yes
                }
                space_owner = {
                    country_event = {
                        id = stellar_manip_dialogue.9
                    }
                }
                stellar_manip_remove_applicable_injector_mega = yes
            }
            else = {
                if = {
                    #check for energy
                    limit = {
                        space_owner = {
                            has_resource = {
                                type = energy
                                amount > 5000
                            }
                        }
                    }
                    space_owner = {
                        add_resource = {
                            energy = -5000
                        }
                        set_country_flag = auto_injector
                    }
                    stellar_manip_remove_applicable_injector_mega = yes
                    solar_system = {
                        spawn_megastructure = {
                            type = stellar_manipulator_3
                            owner = ROOT.space_owner
                            planet = ROOT
                        }
                    }
                    from.owner = {
                        start_situation = {
                            type = situation_stellar_infusion
                            target = root
                        }
                    }

                }
                else = {
                    #no energy
                    stellar_manip_remove_applicable_injector_mega = yes
                    space_owner = {
                        country_event = {
                            id = stellar_manip_dialogue.10
                        }
                        add_resource = {
                            alloys = 5000
                        }
                    }
                }
            }
        }
        else = {
            #manual injection
            space_owner = {
                country_event = {
                    id = stellar_manip_dialogue.8
                }
            }
            stellar_manip_remove_applicable_injector_mega = yes
        }
        destroy_situation = from
    }
}

# Stellar Mass Harvesting Progress Initializer
situation_event = {
    id = stellar_manip_utility.4
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        target = {
            OR = {
                is_variable_set = stellar_manip_harvesting_progress
                is_variable_set = stellar_manip_injection_progress
            }

        }
    }
    immediate = {
        # If you have previously harvested plasma from the star, you pick up where you left off
        if = {
            limit = {
                target = {
                    is_variable_set = stellar_manip_harvesting_progress
                }
            }
            add_situation_progress = target.stellar_manip_harvesting_progress
            target = { clear_variable = stellar_manip_harvesting_progress }
        }

        # If you have previously injected plasma into the star, you'll have to undo the progress made in the injection situation
        if = {
            limit = {
                target = {
                    is_variable_set = stellar_manip_injection_progress
                }
            }
            target = {
                multiply_variable = {
                    which = stellar_manip_injection_progress
                    value = -1
                }
            }
            add_situation_progress = target.stellar_manip_injection_progress
            target = { clear_variable = stellar_manip_injection_progress }
        }
    }
}

# Stellar Mass Injection Progress Initializer
situation_event = {
    id = stellar_manip_utility.5
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        target = {
            OR = {
                is_variable_set = stellar_manip_injection_progress
                is_variable_set = stellar_manip_harvesting_progress
            }
        }
    }
    immediate = {
        # If you have previously injected plasma into the star, you pick up where you left off
        if = {
            limit = {
                target = {
                    is_variable_set = stellar_manip_injection_progress
                }
            }
            add_situation_progress = target.stellar_manip_injection_progress
        }

        # If you have previously harvested plasma from the star, you'll have to undo the progress made in the harvesting situation
        if = {
            limit = {
                target = {
                    is_variable_set = stellar_manip_harvesting_progress
                }
            }
            target = {
                multiply_variable = {
                    which = stellar_manip_harvesting_progress
                    value = -1
                }
            }
            add_situation_progress = target.stellar_manip_harvesting_progress
            target = { clear_variable = stellar_manip_harvesting_progress }
        }
    }
}

# Abort manipulation (Harvesting)
situation_event = {
    id = stellar_manip_utility.6
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        current_situation_approach = approach_abort_stellar_manipulation
    }

    immediate = {
        target = {
            set_variable = {
                which = stellar_manip_harvesting_progress
                value = prev.trigger:situation_progress
            }
            subtract_variable = { # You start at 100 so 100 is removed
                which = stellar_manip_harvesting_progress
                value = 100
            }
        }
        abort_situation = this
    }
}

# Abort manipulation (Injection)
situation_event = {
    id = stellar_manip_utility.7
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        current_situation_approach = approach_abort_stellar_manipulation
    }

    immediate = {
        target = {
            set_variable = {
                which = stellar_manip_injection_progress
                value = prev.trigger:situation_progress
            }
            subtract_variable = { # You start at 100 so 100 is removed
                which = stellar_manip_injection_progress
                value = 100
            }

        }
        abort_situation = this
    }
}

planet_event = {
    id = stellar_manip_utility.8
    hide_window = yes
    is_triggered_only = yes

    immediate = {
        from.owner = {
            if = {
                limit = {
                    has_technology = tech_gigas_stellar_manip_1
                }
                add_resource = { sr_stellar_plasma = @supernovae_plasma_production }
            }
        }
    }
}
