
namespace = grand_archive
# This = Leader captured
# from = perpetrator country
# fromfrom = victim country
# fromfromfrom = system where it happened
leader_event = {
	id = grand_archive.9000
	is_triggered_only = yes
	hide_window = yes
	immediate = {
        if = {
            limit = {
                fromfromfrom = {
                    any_fleet_in_system = {
                        is_controlled_by = root.from
                        any_controlled_ship = { has_any_leader_capturer_comp_no_pirate = yes }
                    }
                }
            }
            if = {
                limit = {
                    or = {
                        leader_cannot_be_captured = yes
                        fromfromfrom = {
                            any_fleet_in_system = {
                                is_controlled_by = root.fromfrom
                                #any_controlled_ship = { has_any_leader_capturer_comp_no_pirate = yes }
                                any_controlled_ship = { has_any_comp_against_leader_capturer_comp = yes }
                            }
                        }
                    }
                }
                set_owner = fromfrom
            }
            else = {
                set_owner = from
                from = {
                    create_message = {
                        type = MESSAGE_LEADER_CAPTURED_PLNMG_RICNT
                        localization = MESSAGE_LEADER_CAPTURED_PLNMG_RICNT_DESC
                        days = 30
                        target = root.fromfromfrom
                        #recipient = root.from
                        variable = { type = name localization = COUNTRY scope = root.fromfrom }
                        variable = { type = name localization = TARGET_LEADER scope = root }
                        variable = { type = name localization = LOCATION_SYSTEM scope = root.fromfromfrom }
                    }
                }
                fromfrom = {
                    create_message = {
                        type = MESSAGE_PLNMG_FLEET_NO_LEADER
                        localization = MESSAGE_PLNMG_FLEET_NO_LEADER_DESC
                        days = 30
                        target = root.fromfromfrom
                        #recipient = root.fromfrom
                        variable = { type = name localization = LOCATION_SYSTEM scope = root.fromfromfrom }
                    }
                }
            }
        }
        else = {
            if = {
                limit = {
                    or = {
                        leader_cannot_be_captured = yes
                        fromfromfrom = {
                            any_fleet_in_system = {
                                is_controlled_by = root.fromfrom
                                any_controlled_ship = { has_any_comp_against_leader_capturer_comp = yes }
                            }
                        }
                    }
                }
                set_owner = fromfrom
            }
            else = {
                #vanilla content begins
                set_leader_flag = leader_death_events_blocked
                exile_leader_as = captured_leader_by@from
                from = {
                    create_message = {
                        type = MESSAGE_LEADER_CAPTURED
                        localization = MESSAGE_LEADER_CAPTURED_DESC
                        days = 20
                        target = root.fromfromfrom #added
                        variable = {
                            type = name
                            localization = COUNTRY
                            scope = root.fromfrom
                        }
                    }
                }
                fromfrom = {
                    country_event = {
                        id = grand_archive.9005
                        days = 1
                        scopes = {
                            from = root.from
                            fromfrom = root
                            fromfromfrom = root.fromfromfrom
                        }
                    }
                }
            }
        }
	}
}

# 修正damage_random_nearby_enemy_on_death应用事件，尝试修复变量于错误域、无源伤害、错误音效bug
# This = owner of ship 1 (destroyed)
# From = owner of ship 2 (combatant)
# FromFrom = ship 1
# FromFromFrom = ship 2 # can be invalid if damage is done via other methods (like scripted action)
country_event = {
    id = grand_archive.10090
    hide_window = yes
    is_triggered_only = yes
    trigger = {
        fromfrom = {
            check_modifier_value = {
                modifier = damage_random_nearby_enemy_on_death
                value > 0
            }
        }
    }

    immediate = {
        fromfrom.solar_system = {
            random_ship_in_system = {
                limit = {
                    owner? = { is_same_value = root.from }
                    distance = { source = root.fromfrom max_distance <= 30 same_solar_system = yes }
                }
                damage_ship = {
                    amount = root.fromfrom.value:grand_archive_10090_damage_value
                    attacker = root.fromfrom
                }
            }
        }
        if = {
            limit = { fromfrom = { has_catalytic_bladder_mutation = yes } }
            play_sound = { # Sound played on ship explosion
                sound = ga_weapon_catalytic_bladder
                location = fromfrom
            }
        }
    }
}

namespace = colony
### 嘿地底人，你们到处挖洞的日子结束了，把地下空间给我
carrier_event = {
    id = colony.50
    title = "colony.50.name"
    desc = {
        trigger = { owner = { is_subterranean_empire = no } }
        text = "colony.50.desc"
    }
    desc = {
        trigger = { owner = { is_subterranean_empire = yes } }
        text = "colony.50.desc.subterranean"
    }

    picture = GFX_evt_in_the_dark
    show_sound = event_air_raid_siren
    location = ROOT
    trackable = yes
    event_chain = subterranean_civilization_chain

    pre_triggers = {
        is_ai = no
        has_owner = yes
        is_homeworld = no
        original_owner = yes
        has_ground_combat = no
        is_capital = no
        is_occupied_flag = no
    }

    trigger = {
        owner = { is_wilderness_empire = no }
        pop_amount > 0
        carrier_is_type = planet
        NOR = {
            has_global_flag = seismic_disturbance_global
            has_carrier_flag = colony_event
            is_planet_class = pc_nuked
            # is_planet_class = pc_ringworld_habitable
            # is_planet_class = pc_shattered_ring_habitable
            is_ringworld = yes
            # is_planet_class = pc_habitat
            is_artificial = yes
            #is_planet_class = pc_city
            planet_is_ecu_equivalent = yes
            is_plnmg_mod_city_world = yes
            is_planet_class = pc_relic
            is_planet_class = pc_volcanic
            #is_planet_class = pc_ark
        }
    }

    is_triggered_only = yes

    immediate = {
        owner = { save_event_target_as = subterranean_planet_owner }
        set_global_flag = seismic_disturbance_global
        set_carrier_flag = seismic_disturbance
        set_carrier_flag = colony_event
        add_deposit = d_underground_farm_blocked
        add_deposit = d_underground_mine_blocked
        add_deposit = d_underground_generator_blocked
        create_subterranean_nation = { FLAG = subterranean_nation }
        begin_event_chain = {
            event_chain = "subterranean_civilization_chain"
            target = ROOT
        }
    }

    option = {
        name = colony.50.a
        tooltip = {
            begin_event_chain = {
                event_chain = "subterranean_civilization_chain"
                target = ROOT
            }
        }
        enable_special_project = {
            name = "SEISMIC_DISTURBANCE_1_PROJECT" #Establish Communications
            location = this
            owner = root
        }
        enable_special_project = {
            name = "SEISMIC_DISTURBANCE_2_PROJECT" #Preemptive Strike
            location = this
            owner = root
        }

    }
    option = {
        name = distar.216.a.exter
        exclusive_trigger = { owner = { is_homicidal = yes } }
        tooltip = {
            begin_event_chain = {
                event_chain = "subterranean_civilization_chain"
                target = ROOT
            }
        }
        enable_special_project = {
            name = "SEISMIC_DISTURBANCE_2_PROJECT" #Preemptive Strike
            location = this
            owner = root
        }

    }
}

namespace = nomads
# 加上其他on_action，因为通过set_location似乎只能触发on_entering_system(_fleet)
## Fired by on_fleet_arkship_exodus_jump
# THIS = Fleet
# FROM = System jumping to
# FROMFROM = System jumped from
fleet_event = {
    id = nomads.4650
    is_triggered_only = yes
    hide_window = yes
    immediate = {
        random_owned_ship = { save_event_target_as = arkship }
        fromfrom = {
            every_fleet_in_system = {
                limit = {
                    is_mobile = yes
                    controller = { is_same_value = root.controller }
                    not = { any_controlled_ship = { is_disabled = yes } }
                }
                remove_auto_move_target = yes
                set_location = {
                    target = event_target:arkship
                    distance = 20
                    angle = random
                    direction = in_system
                }
                # added by this mod
                # for optimization, on_ship_quantum_catapult is not added
                fire_on_action = {
                    on_action = on_fleet_quantum_catapult
                    scopes = { from = root.from fromfrom = root.fromfrom }
                }
            }
        }
        controller = {
            create_message = {
                type = TOAST_EXODUS_JUMP_COMPLETE
                localization = TOAST_EXODUS_JUMP_COMPLETE_DESC
                days = @toast_message_days
                variable = {
                    type = name
                    localization = ARKSHIP
                    scope = event_target:arkship
                }
                variable = {
                    type = name
                    localization = SYSTEM
                    scope = from
                }
            }
        }
        FROM = { arkship_exodus_jump_system_effects = yes }
    }
}
