namespace = planetmega

country_event = {
    id = planetmega.1000 #in test that only able to be triggered via console
    hide_window = yes #effect add_research_option = tech_planetmega_ultimate
    is_triggered_only = yes
    trigger = {
        always = no
        # is_ai = no
        # is_ironman = no
        # mid_game_years_passed > 0
        # has_all_required_planetmega_advanced_techs = yes
        # nor = {
        #     has_country_flag = planetmega_1000_triggered
        #     has_country_flag = planetmega_ultimate_enabled
        # }
    }
    immediate = {
        reroll_random = yes
        random_list = {
            99 = {}
            1 = {
                modifier = { factor = value:planetmega_1000_mid_year_factor }
                complex_trigger_modifier = {
                    trigger = count_playable_country
                    parameters = { limit = { is_ai = no } }
                    mode = factor
                }
                modifier = { #more determination put in ultimate tech
                    or = {
                        has_global_flag = endgame_crisis_early_start
                        crisis_happened_and_defeated = yes
                        is_player_crisis = yes
                        has_ascension_perk = ap_defender_of_the_galaxy
                        has_ascension_perk = ap_defender_of_the_galaxy_nomads
                        has_ascension_perk = ap_enigmatic_engineering
                    }
                    factor = 2
                }
                modifier = { #less energy put in ultimate tech
                    or = {
                        has_global_flag = ai_invasion_ongoing
                        has_global_flag = synth_queen_ongoing
                        has_global_flag = ongoing_prethoryn_invasion
                        and = { #has PDX added a ongoing-check flag?
                            has_global_flag = extradimensional_invasion_happened
                            not = { has_global_flag = extradimensional_invasion_defeated }
                        }
                        any_playable_country = { is_level_5_player_crisis = yes }
                    }
                    factor = 0.5
                }
                modifier = { round = yes }
                modifier = { min = 1 }
                set_country_flag = planetmega_1000_triggered
                add_research_option = tech_planetmega_ultimate
            }
        }
    }
}

country_event = {
    id = planetmega.1001
    is_triggered_only = yes
    title = planetmega.1001.name
    desc = planetmega.1001.desc
    desc = {
        exclusive_trigger = { is_gestalt = yes }
        text = planetmega.1001.desc_gestalt
    }
    picture = GFX_evt_mysterious_signal
    trigger = {
        is_ai = no
        last_increased_tech = tech_planetmega_ultimate
    }

    option = {
        name = planetmega.1001.spiritualist
        trigger = { is_spiritualist = yes }
    }
    option = {
        name = planetmega.1001.shroud
        exclusive_trigger = {
            or = {
                can_access_shroud = yes
                has_breached_shroud = yes
            }
        }
    }
    option = {
        name = INTERESTING
        trigger = { is_spiritualist = no }
    }
    option = {
        name = NOTIME
        hidden_effect = { set_country_flag = dont_get_this_special_project }
    }

    after = {
        if = {
            limit = { hidden_trigger = { not = { has_country_flag = dont_get_this_special_project } } }
            begin_event_chain = { event_chain = planetmega_ultimate_chain target = this }
            enable_special_project = { name = PLANETMEGA_PROJECT_ULTIMATE_REGULATION owner = this }
        }
        hidden_effect = {
            if = {
                limit = { has_country_flag = dont_get_this_special_project }
                remove_country_flag = dont_get_this_special_project
            }
        }
    }
}

country_event = {
    id = planetmega.1002
    is_triggered_only = yes
    title = planetmega.1002.name
    desc = planetmega.1002.desc
    picture = GFX_evt_mysterious_signal
    event_chain = planetmega_ultimate_chain
    option = { name = GOOD }
    after = { enable_special_project = { name = PLANETMEGA_PROJECT_ULTIMATE_OVERVIEW owner = this } }
}

### ultimate tech unlock chain (META events) ###
# written by deepseek-v4-pro via Claude Code

# First anomaly — quantum fluctuations contain structured data
country_event = {
    id = planetmega.1003
    is_triggered_only = yes
    title = planetmega.1003.name
    desc = planetmega.1003.desc
    desc = {
        exclusive_trigger = { is_gestalt = yes }
        text = planetmega.1003.desc_gestalt
    }
    picture = GFX_evt_mysterious_signal
    show_sound = event_mysterious_signal
    event_chain = planetmega_ultimate_chain

    option = {
        name = planetmega.1003.investigate
        hidden_effect = { country_event = { id = planetmega.1009 days = 60 random = 120 } }
    }
    option = {
        name = planetmega.1003.dismiss
        hidden_effect = {
            #remove_country_flag = planetmega_ultimate_event_chain
            country_event = { id = planetmega.1003 days = 1800 random = 3600 }
        }
    }
    option = { name = planetmega.1003.abandon }
}

# Breakthrough — the data is source code
country_event = {
    id = planetmega.1009
    is_triggered_only = yes
    title = planetmega.1009.name
    desc = planetmega.1009.desc
    desc = {
        exclusive_trigger = { is_gestalt = yes }
        text = planetmega.1009.desc_gestalt
    }
    picture = GFX_evt_mysterious_signal
    show_sound = event_ancient_drone
    event_chain = planetmega_ultimate_chain

    option = {
        name = planetmega.1009.continue
        country_event = { id = planetmega.1010 days = 10 random = 30 }
    }
}

# THE CHOICE — paths, including one crashes the game
country_event = {
    id = planetmega.1010
    is_triggered_only = yes
    title = planetmega.1010.name
    desc = planetmega.1010.desc
    desc = {
        exclusive_trigger = { is_gestalt = yes }
        text = planetmega.1010.desc_gestalt
    }
    picture = GFX_evt_mysterious_signal
    event_chain = planetmega_ultimate_chain

    option = {
        name = planetmega.1010.safe
        trigger = { always = no }
        start_situation = { type = planetmega_ultimate_research }
    }
    option = {
        name = planetmega.1010.worship
        trigger = { is_spiritualist = yes }
        add_modifier = { modifier = planetmega_ultimate_worship }
        end_event_chain = planetmega_ultimate_chain
    }

    option = {
        name = planetmega.1010.edit
        custom_tooltip = planetmega.1010.edit.tt
        hidden_effect = { country_event = { id = planetmega.1011 days = 10 } }
    }
    option = {
        name = planetmega.1010.seal
        custom_tooltip = planetmega.1010.seal.tt
        hidden_effect = { end_event_chain = planetmega_ultimate_chain }
    }
}

# Reality Compilation Error — the narrative before the crash
country_event = {
    id = planetmega.1011
    is_triggered_only = yes
    title = planetmega.1011.name
    desc = planetmega.1011.desc
    picture = GFX_evt_planetmega_omnipotence
    show_sound = event_arkship_in_crisis
    event_chain = planetmega_ultimate_chain
    immediate = {
        date_distortion = yes
        every_country = { country_event = { id = planetmega.1012 } }
    }
    option = {
        name = planetmega.1011.continue
        custom_tooltip = planetmega.1011.tt
        hidden_effect = { country_event = { id = planetmega.1013 } }
    }
}
country_event = {
    id = planetmega.1012
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        num_owned_colonies > 0
        is_nomadic = no
        is_fallen_empire = no
        is_planetmega_crisis = no
    }
    immediate = {
        every_owned_colony = {
            limit = {
                NOR = {
                    is_capital = yes
                    is_planet_class = pc_cosmogenesis_world
                }
            }
            destroy_colony = yes
            random_list = {
                1 = {
                    modifier = { factor = 0 is_artificial = yes }
                    change_pc = pc_shattered
                }
                1 = { change_pc = pc_black_hole }
                3 = {}
            }
        }
        every_system_within_border = {
            random_list = {
                1 = {}
                1 = {
                    modifier = {
                        factor = 0
                        any_system_planet = { has_building = yes }
                    }
                    random_system_planet = { add_deposit = random }
                    every_fleet_in_system = {
                        limit = { is_mobile = no }
                        destroy_fleet = this
                    }
                }
            }
        }
        every_owned_fleet = {
            limit = { is_mobile = yes }
            random = { chance = 30 destroy_fleet = this }
        }
    }
}
country_event = {
    id = planetmega.1013
    is_triggered_only = yes
    title = BLANK_STRING
    desc = BLANK_STRING
    diplomatic = yes
    diplomatic_title = BLANK_STRING
    custom_gui = AntiMetaPhysicBlankGUI #not exists, intentional to make the game CTD
    option = { name = OK }
    after = { hidden_effect = { date_distortion = no } }
}

situation_event = {
    id = planetmega.1015
    is_triggered_only = yes
    title = planetmega.1015.name
    desc = planetmega.1015.desc
    picture = GFX_evt_mysterious_signal
    show_sound = event_mysterious_signal
    event_chain = planetmega_ultimate_chain
    situation = this

    option = {
        name = planetmega.1015.continue
        allow = { is_ironman = no }
        custom_tooltip = planetmega.1010.warning.tt
        hidden_effect = {
            reroll_random = yes
            random_list = {
                9 = { owner = { country_event = { id = planetmega.1011 days = 10 } } }
                1 = { add_situation_progress = 10 }
            }
        }
    }
    option = {
        name = planetmega.1015.restart
        allow = { is_ironman = no }
        owner = { start_situation = { type = planetmega_ultimate_research } }
        tooltip = { destroy_situation = this }
        hidden_effect = { situation_event = { id = situation.1180 } }
    }
    option = {
        name = planetmega.1015.abandon
        owner = { end_event_chain = planetmega_ultimate_chain }
        tooltip = { destroy_situation = this }
        hidden_effect = { situation_event = { id = situation.1180 } }
    }
}

# Safe path — permission granted, tech unlocked
country_event = {
    id = planetmega.1020
    is_triggered_only = yes
    title = planetmega.1020.name
    desc = planetmega.1020.desc
    picture = GFX_evt_mysterious_signal
    show_sound = event_mystic_reveal_light
    event_chain = planetmega_ultimate_chain

    option = {
        name = planetmega.1020.accept
        end_event_chain = planetmega_ultimate_chain
        custom_tooltip = planetmega.1020.effect
        hidden_effect = { set_country_flag = planetmega_ultimate_enabled }
    }
}
country_event = {
    id = planetmega.1021
    is_triggered_only = yes
    title = planetmega.1021.name
    event_chain = planetmega_ultimate_chain

    picture = {
        trigger = { not = { has_country_flag = alter_plnmg_1021_desc } }
        picture = GFX_evt_mysterious_signal
    }
    picture = {
        trigger = { has_country_flag = alter_plnmg_1021_desc }
        picture = GFX_evt_worrying_signal
    }

    desc = {
        trigger = {
            nor = {
                has_country_flag = planetmega_ultimate_enabled
                has_country_flag = alter_plnmg_1021_desc
            }
        }
        text = planetmega.1021.desc
    }
    desc = {
        trigger = {
            not = { has_country_flag = planetmega_ultimate_enabled }
            has_country_flag = alter_plnmg_1021_desc
        }
        text = planetmega.1021.desc_alter
    }
    desc = {
        trigger = { has_country_flag = planetmega_ultimate_enabled }
        text = planetmega.1021.desc_upper
    }

    immediate = {
        initialize_mys_variable = { VAR = planetmega_1022_trigger_var }
        change_variable = { which = planetmega_1022_trigger_var value = 1 }
        reroll_random = yes
        random = {
            chance = 10
            change_variable = { which = planetmega_1022_trigger_var value = 1 }
        }
    }

    option = {
        name = {
            trigger = { not = { has_country_flag = planetmega_ultimate_enabled } }
            text = planetmega.1011.continue
        }
        name = {
            trigger = { has_country_flag = planetmega_ultimate_enabled }
            text = planetmega.1021.accept
        }
        if = {
            limit = { hidden_trigger = { has_country_flag = planetmega_ultimate_enabled } }
            end_event_chain = planetmega_ultimate_chain
            add_research_option = tech_planetmega_ultimate_sp
            hidden_effect = {
                clear_variable = planetmega_1022_trigger_var
                if = {
                    limit = { has_country_flag = alter_plnmg_1021_desc }
                    remove_country_flag = alter_plnmg_1021_desc
                }
            }
        }
        else = {
            hidden_effect = {
                country_event = { id = planetmega.1021 }
                if = {
                    limit = { has_country_flag = alter_plnmg_1021_desc }
                    remove_country_flag = alter_plnmg_1021_desc
                }
                else = {
                    random_list = {
                        20 = {}
                        1 = { set_country_flag = alter_plnmg_1021_desc }
                    }
                }
                if = {
                    limit = { check_variable = { which = planetmega_1022_trigger_var value > 49 } }
                    clear_variable = planetmega_1022_trigger_var
                    country_event = { id = planetmega.1022 }
                }
            }
        }
    }
}
country_event = { #VIR revived!
    id = planetmega.1022
    title = planetmega.1022.name
    desc = {
        trigger = { not = { has_country_flag = planetmega_1022_triggered_twice } }
        text = planetmega.1022.desc
        show_sound = event_planetmega_1022_advisor
    }
    desc = {
        trigger = { has_country_flag = planetmega_1022_triggered_twice }
        text = planetmega.1022.desc.3rd
    }
    is_triggered_only = yes
    is_advisor_event = yes

    trigger = {
        is_ai = no
        or = {
            not = { has_country_flag = planetmega_1022_triggered_once }
            and = {
                check_variable = { which = planetmega_1022_trigger_var value > 299 }
                not = { has_country_flag = planetmega_1022_triggered_twice }
            }
        }
    }
    immediate = {
        if = {
            limit = { is_advisor_active = no }
            set_advisor_active = yes
        }
        if = {
            limit = { has_country_flag = planetmega_1022_triggered_once }
            set_country_flag = planetmega_1022_triggered_twice
        }
    }

    option = {
        name = OK
        trigger = { is_tutorial_level > 0 }
    }
    option = {
        name = planetmega.1022.WTF
        trigger = {
            is_tutorial_level = 0
            not = { has_country_flag = planetmega_1022_triggered_once }
        }
        custom_tooltip = planetmega.1022.WTF.tt
        add_resource = { minerals = 2 }
        hidden_effect = { set_advisor_active = no }
    }
    option = {
        name = planetmega.1022.WTF_twice
        trigger = {
            is_tutorial_level = 0
            has_country_flag = planetmega_1022_triggered_once
        }
        custom_tooltip = planetmega.1022.WTF.tt
        add_resource = { minerals = 2 }
        hidden_effect = { set_advisor_active = no }
    }
    option = {
        name = planetmega.1011.continue
        exclusive_trigger = { has_country_flag = planetmega_1022_triggered_twice }
        custom_tooltip = planetmega.1010.warning.tt
        hidden_effect = { country_event = { id = planetmega.1013 days = 1 } }
    }
    after = { hidden_effect = { set_country_flag = planetmega_1022_triggered_once } }
}

# here should be a country event
### end of event chain

carrier_event = {
    id = planetmega.1050
    is_triggered_only = yes
    title = planetmega.1050.name
    desc = planetmega.1050.desc
    picture = GFX_evt_giant_building_in_progress
    location = this
    trigger = { owner? = { is_ai = no } }

    option = {
        name = d_plnmg_reincarnation
        trigger = {
            nor = {
                has_deposit = d_plnmg_reincarnation
                has_carrier_flag = planet_has_plnmg_reincarnation
            }
            not = { any_targeting_situation = { has_situation_flag = building_plnmg_reincarnation } }
            owner = { has_technology = tech_planetmega_reincarnation }
        }
        allow = {
            pop_amount >= 10000
        }
        custom_tooltip = plnmg_reincarnation_build_tooltip
        owner = {
            start_situation = {
                type = planet_megastructure_constuti
                target = prev
                effect = { set_situation_flag = building_plnmg_reincarnation }
            }
        }
    }
    option = { name = CANCEL }
    after = { hidden_effect = { remove_carrier_flag = planet_has_plnmg_megabuild_listed } }
}

situation_event = {
    id = planetmega.1055
    is_triggered_only = yes
    title = planetmega.1055.name
    desc = planetmega.1055.desc
    picture = {
        trigger = { has_situation_flag = building_plnmg_reincarnation }
        picture = GFX_evt_planetmega_reincarnation
    }
    picture = {
        trigger = { not = { has_situation_flag = building_plnmg_reincarnation } }
        picture = GFX_evt_giant_building_in_progress
    }
    situation = this
    location = target
    trigger = { exists = owner }
    immediate = { set_situation_locked = yes }

    option = {
        name = planetmega.5.a
        allow = {
            owner = {
                resource_stockpile_compare = {
                    resource = alloys
                    value >= 25000
                    mult = value:ground_megastructure_estimate_mult
                }
                resource_stockpile_compare = {
                    resource = energy
                    value >= 6000
                    mult = value:ground_megastructure_estimate_mult
                }
                resource_stockpile_compare = {
                    resource = influence
                    value >= 1000
                    mult = value:ground_megastructure_estimate_mult
                }
            }
        }
        owner = {
            add_resource = {
                alloys = 25000
                energy = 6000
                influence = 1000
                mult = value:ground_megastructure_cost_mult
            }
        }
        hidden_effect = {
            set_situation_locked = no
            target = { set_carrier_flag = planet_building_groundmega }
        }
    }
    option = {
        name = planetmega.5.b
        hidden_effect = {
            set_situation_locked = no
            destroy_situation = this
        }
    }
}

#owari
situation_event = {
    id = planetmega.1090
    hide_window = yes
    is_triggered_only = yes
    situation = this
    immediate = {
        target = {
            if = {
                limit = {
                    has_carrier_flag = planet_building_groundmega
                    check_variable_arithmetic = {
                        which = value:plnmg_mega_build_count_value
                        subtract = 1
                        value <= 0
                    }
                }
                remove_carrier_flag = planet_building_groundmega
            }
        }
        situation_event = { id = planetmega.1100 }
        if = {
            limit = { situation_is_plnmg_flagged = no }
            situation_event = { id = situation.1180 }
        }
    }
}

situation_event = {
    id = planetmega.1100
    is_triggered_only = yes
    title = planetmega.1100.name
    desc = planetmega.1100.desc
    picture = GFX_evt_planetmega_reincarnation
    situation = this
    location = target
    trigger = { has_situation_flag = building_plnmg_reincarnation }
    immediate = {
        set_situation_locked = yes
        target = { add_plnmg_mega = { TYPE = plnmg_reincarnation on_actions = yes } }
        owner = {
            country_event = { id = planetmega.1101 }
            if = {
                limit = {
                    is_ai = no
                    not = { has_country_flag = built_planetmega_reincarnation }
                }
                set_country_flag = built_planetmega_reincarnation
            }
        }
    }

    option = {
        name = FASCINATING
        tooltip = { target = { add_deposit = d_plnmg_reincarnation } }
    }
    after = {
        hidden_effect = {
            set_situation_locked = no
            destroy_situation = this
        }
    }
}
country_event = {
    id = planetmega.1101
    is_triggered_only = yes
    hide_window = yes
    trigger = { has_technology = tech_planetmega_reincarnation }
    immediate = {
        set_variable = { which = var_plnmg_reincarnation_count value = value:sv_plnmg_reincarnation_count }
    }
}

# when our ship is destroyed, reincarnation hall begins to work
# This = owner of ship 1 (destroyed)
# From = owner of ship 2 (combatant)
# FromFrom = ship 1
# FromFromFrom = ship 2
country_event = {
    id = planetmega.1110
    hide_window = yes
    is_triggered_only = yes
    trigger = {
        has_country_flag = built_planetmega_reincarnation
        not = { has_global_flag = curse_of_ricnt_eternity }
        fromfrom = {
            not = { exists = colony } #to prevent bugs
            or = {
                is_ship_class = shipclass_military
                #is_ship_class = shipclass_military_special
                is_ship_size = colossus
                is_ship_class = shipclass_military_station
            }
            or = {
                not = { has_ship_flag = reincarnated_ship }
                check_variable = { which = ship_ricnt_revival_count value < modifier:plnmg_ricnt_ship_times_add }
            }
        }
        nand = { #to prevent effect repeatedly
            is_reanimator = yes
            fromfrom = { can_be_reanimated = yes }
        }
    }

    immediate = {
        if = {
            limit = {
                resource_stockpile_compare = {
                    resource = energy value >= @plnmg_ricnt_energy_cost
                    mult = value:sv_plnmg_ricnt_energy_cost_mult
                }
            }
            add_resource = {
                energy = @plnmg_ricnt_energy_cost_neg
                mult = value:sv_plnmg_ricnt_energy_cost_mult
            }
            fromfrom = {
                fleet = {
                    create_ship = {
                        name = prev
                        design = prev.design
                        effect = {
                            save_event_target_as = reincarnated_ship
                            set_ship_flag = reincarnated_ship
                            set_variable = {
                                which = ship_ricnt_revival_count
                                value = value:former_ship_ricnt_revival_count
                            }
                        }
                    }
                }
            }
            ### notices
            create_message = {
                type = MESSAGE_PLNMG_RICNT_SHIP #MESSAGE_PLNMG_RICNT_SHIP_FAILED
                localization = MESSAGE_PLNMG_RICNT_SHIP_DESC
                days = 30
                target = fromfrom.fleet
                variable = { type = name localization = SHIP scope = fromfrom }
            }
            from = {
                create_message = {
                    type = MESSAGE_PLNMG_RICNT_ENEMY_SHIP
                    localization = MESSAGE_PLNMG_RICNT_ENEMY_SHIP_DESC
                    days = 30
                    target = root.fromfrom.fleet
                    variable = { type = name localization = SHIP scope = root.fromfrom.fleet }
                }
            }
        }
        else = { # can't effect and make notice event
            country_event = {
                id = planetmega.1111
                scopes = { from = from fromfrom = fromfrom fromfromfrom = fromfromfrom }
            }
        }
    }
}

#notice event for no effect
country_event = {
    id = planetmega.1111
    is_triggered_only = yes
    title = planetmega.1111.name
    desc = planetmega.1111.desc
    picture = GFX_evt_ghost_ship
    show_sound = event_mysterious_signal
    location = fromfrom
    option = { name = OOPS }
}


# Not enabled, as might occur bugs, need more tests
# This = owner
# From = army
# FromFrom = opponent
# FromFromFrom = colony
country_event = {
    id = planetmega.1115
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        always = no
        # has_technology = tech_planetmega_reincarnation
        # is_reanimator = yes
        # from = {
        #     or = {
        #         army_type = giant_undead_army
        #         species? = { is_robotic = no }
        #     }
        # }
    }
    immediate = {
        create_message = {
            type = MESSAGE_PLNMG_RICNT_ARMY
            localization = MESSAGE_PLNMG_RICNT_ARMY_DESC
            days = 30
            target = fromfromfrom
            variable = { type = name localization = ARMY scope = from }
        }
        fromfrom = {
            create_message = {
                type = MESSAGE_PLNMG_RICNT_ENEMY_ARMY
                localization = MESSAGE_PLNMG_RICNT_ENEMY_ARMY_DESC
                days = 30
                target = root.fromfromfrom
                variable = { type = name localization = ARMY scope = root.from }
            }
        }

        if = {
            limit = { from = { army_type = giant_undead_army } }
            fromfromfrom = {
                create_army = {
                    owner = root
                    type = giant_undead_army
                    species = root.from.species
                }
            }
        }
        else_if = {
            limit = { from = { is_defensive_army = yes } }
            fromfromfrom = {
                create_army = {
                    owner = root
                    type = undead_defense_army
                    species = root.from.species
                }
            }
        }
        else = {
            fromfromfrom = {
                create_army = {
                    owner = root
                    type = undead_army
                    species = root.from.species
                }
            }
        }
    }
}

# crisis trigger for d_plnmg_reincarnation
event = {
    id = planetmega.1120
    hide_window = yes
    is_triggered_only = yes
    trigger = {
        is_crises_allowed = yes
        exists = event_target:global_event_country
        blocks_disruptive_gameplay = no
        OR = {
            NOT = { has_global_flag = galactic_crisis_happened }
            AND = {
                allowed_crisis_type = all
                NOT = { has_global_flag = galactic_crisis_recently_fired }
            }
        }
        any_playable_country = { has_country_flag = built_planetmega_reincarnation }
    }

    immediate = {
        refresh_global_plnmg_reincarnation_count = yes
        event_target:global_event_country = {
            random_list = {
                50 = {
                    modifier = {
                        factor = 0
                        or = {
                            has_global_flag = endgame_crisis_early_start #From Ultima Vigilis
                            AND = { #Both crisis summoning resolutions active
                                is_active_resolution = resolution_galacticstudies_extradimensional_experimentation
                                is_active_resolution = resolution_galactic_threats_committee
                            }
                        }
                    }
                    modifier = {
                        factor = 0
                        end_game_years_passed >= 0
                        or = {
                            default_endgame_early_start_triggers = yes
                            any_country = {
                                is_country_type = default
                                OR = {
                                    has_technology = tech_jump_drive_1
                                    has_technology = tech_psi_jump_drive_1
                                }
                            }
                        }
                    }
                }
                ### Unbidden (Extradimensionals)
                50 = {
                    modifier = {
                        is_variable_set = var_plnmg_reincarnation_count
                        factor = var_plnmg_reincarnation_count
                    }
                    modifier = {
                        factor = 3
                        allowed_crisis_type = unbidden
                    }
                    modifier = {
                        factor = 0
                        OR = {
                            allowed_crisis_type = contingency
                            allowed_crisis_type = prethoryn
                            allowed_crisis_type = synth_queen
                        }
                        NOT = { has_global_flag = defender_of_the_galaxy_second_crisis }
                    }
                    modifier = { #anti-boring
                        factor = 2
                        OR = {
                            has_global_flag = no_war_in_heaven
                            NOT = {
                                any_country = {
                                    OR = {
                                        is_country_type = fallen_empire
                                        is_country_type = awakened_fallen_empire
                                    }
                                }
                            }
                        }
                    }
                    modifier = {
                        factor = 0
                        has_global_flag = extradimensional_invasion_happened
                    }
                    country_event = { id = crisis.1000 days = 20 random = 330 }
                    set_global_flag = unbidden_triggered_by_plnmg_ricnt #added by this mod
                    set_global_flag = galactic_crisis_happened
                    set_timed_global_flag = { flag = galactic_crisis_recently_fired months = 61 }
                    set_timed_global_flag = { flag = galactic_crisis_early_defeat_tracker_1 months = 25 }
                    set_timed_global_flag = { flag = galactic_crisis_early_defeat_tracker_2 months = 50 }
                }
                ### Synth Queen
                20 = {
                    modifier = {
                        is_variable_set = var_plnmg_reincarnation_count
                        factor = var_plnmg_reincarnation_count
                    }
                    modifier = {
                        factor = 0
                        has_country_flag = only_unbidden_this_time
                    }
                    modifier = {
                        factor = 3
                        allowed_crisis_type = synth_queen
                    }
                    modifier = {
                        factor = 0
                        OR = {
                            allowed_crisis_type = prethoryn
                            allowed_crisis_type = unbidden
                            allowed_crisis_type = contingency
                        }
                        NOT = { has_global_flag = defender_of_the_galaxy_second_crisis }
                    }
                    modifier = { #anti-boring
                        factor = 2
                        OR = {
                            has_global_flag = no_war_in_heaven
                            NOT = {
                                any_country = {
                                    OR = {
                                        is_country_type = fallen_empire
                                        is_country_type = awakened_fallen_empire
                                    }
                                }
                            }
                        }
                    }
                    modifier = {
                        factor = 0
                        has_global_flag = synth_queen_happened
                    }
                    # modifier = { #disables requirement for DLC here
                    #     factor = 0
                    #     has_machine_age_dlc = no
                    # }
                    country_event = { id = crisis.8005 days = 20 random = 330 }
                    set_global_flag = galactic_crisis_happened
                    set_timed_global_flag = { flag = galactic_crisis_recently_fired months = 61 }
                    set_timed_global_flag = { flag = galactic_crisis_early_defeat_tracker_1 months = 25 }
                    set_timed_global_flag = { flag = galactic_crisis_early_defeat_tracker_2 months = 50 }
                }
                ### Contingency (AI)
                10 = {
                    modifier = {
                        is_variable_set = var_plnmg_reincarnation_count
                        factor = var_plnmg_reincarnation_count
                    }
                    modifier = {
                        factor = 0
                        has_country_flag = only_unbidden_this_time
                    }
                    modifier = {
                        factor = 3
                        allowed_crisis_type = contingency
                    }
                    modifier = {
                        factor = 0
                        OR = {
                            allowed_crisis_type = prethoryn
                            allowed_crisis_type = unbidden
                            allowed_crisis_type = synth_queen
                        }
                        NOT = { has_global_flag = defender_of_the_galaxy_second_crisis }
                    }
                    modifier = { #anti-boring
                        factor = 2
                        OR = {
                            has_global_flag = no_war_in_heaven
                            NOT = {
                                any_country = {
                                    OR = {
                                        is_country_type = fallen_empire
                                        is_country_type = awakened_fallen_empire
                                    }
                                }
                            }
                        }
                    }
                    modifier = {
                        factor = 0
                        has_global_flag = ai_invasion_happened
                    }
                    country_event = { id = crisis.2005 days = 20 random = 330 }
                    set_global_flag = galactic_crisis_happened
                    set_timed_global_flag = { flag = galactic_crisis_recently_fired months = 61 }
                    set_timed_global_flag = { flag = galactic_crisis_early_defeat_tracker_1 months = 25 }
                    set_timed_global_flag = { flag = galactic_crisis_early_defeat_tracker_2 months = 50 }
                }
            }
        }
    }
}
country_event = {
    id = planetmega.1121
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        or = {
            is_country_type = extradimensional
            is_country_type = extradimensional_2
            is_country_type = extradimensional_3
        }
    }
    immediate = {
        #plnmg_global_event_country_maintenance = yes
        refresh_global_plnmg_reincarnation_count = yes
        add_modifier = {
            modifier = plnmg_ricnt_enhanced_extradimensional
            mult = event_target:global_event_country.var_plnmg_reincarnation_count
        }
    }
}

