//
// Source code of SCP Dystopia: Redefined is licensed under CC-BY-SA 3.0
// (https://creativecommons.org/licenses/by-sa/3.0/)
//
import { world, system, GameMode } from "@minecraft/server";
import { isMultiple, isStringArrayContains } from "./utils";
import { ID_PLAYER, ID_SCP049, SCP008_1_TYPES } from "./globalconstants";
import { simulateScp008 } from "./scp008";
import { simulateScp002 } from "./scp002";
import { simulateScp513 } from "./scp513";
import { showBuildConfirmationGuide } from "./../facility_preview";
// Global variables
const START_TICK = 120;
const overworld = world.getDimension("overworld");
const creativePlayersEqo = {
    gameMode: GameMode.creative
};
let currentTick = 0;
// -------------------------
function initialize() {
    try {
        overworld.runCommandAsync('function scpdtr_func/system/script_init');
    }
    catch (e) { }
}
function resetFacingOfSomeEntities() {
    try {
        overworld.runCommand("execute as @e[family=reset_facing_regularly] at @s run tp @s ~~~ facing ~~~1");
    }
    catch (e) { }
}
function removePenaltiesFromCreativePlayers() {
    try {
        const players = overworld.getPlayers(creativePlayersEqo);
        players.forEach(player => {
            player.removeTag("dtr_scp513_ringing");
            player.removeTag("dtr_scp008_infected");
        });
    }
    catch (ex) { }
}
function killBullets() {
    overworld.runCommandAsync("function scpdtr_func/user/kill_bullets");
}
function mainTick() {
    try {
        if (currentTick === START_TICK)
            initialize();
        if (currentTick > START_TICK) {
            if (isMultiple(currentTick, 80))
                resetFacingOfSomeEntities();
            if (isMultiple(currentTick, 80))
                removePenaltiesFromCreativePlayers();
            if (isMultiple(currentTick, 95))
                killBullets();
            if (isMultiple(currentTick, 35))
                showBuildConfirmationGuide();
            simulateScp008(currentTick);
            simulateScp002();
            simulateScp513();
        }
        currentTick++;
    }
    catch (e) { }
    system.run(mainTick);
}
system.run(mainTick);
// Event subscribes
world.afterEvents.entitySpawn.subscribe((e) => {
    try {
        if (e.entity.typeId == ID_PLAYER) {
            if (!e.entity.hasTag("dtr_received_welcome_book")) {
                e.entity.addTag("dtr_received_welcome_book");
            }
        }
        if (e.entity.typeId == "lc:dtr_blastdoor_destroyer") {
            e.entity.runCommandAsync("event entity @e[type=lc:dtr_blastdoor,r=2] door_destroy");
            e.entity.runCommandAsync("playsound place.copper @a[r=10] ~~~");
        }
    }
    catch (ex) { }
});
world.afterEvents.entityHurt.subscribe((e) => {
    try {
        // Stop if damaging entity is null
        if (e.damageSource.damagingEntity == null)
            return;
        const damagingEntity = e.damageSource.damagingEntity;
        const hurtEntity = e.hurtEntity;
        // SCP-008 infection
        if (hurtEntity.typeId == ID_PLAYER &&
            isStringArrayContains(SCP008_1_TYPES, damagingEntity.typeId)) {
            hurtEntity.runCommandAsync("tag @s add dtr_scp008_infected");
        }
    }
    catch (ex) { }
});
world.afterEvents.entityDie.subscribe((e) => {
    try {
        // Run a function for dead players
        if (e.deadEntity.typeId == ID_PLAYER) {
            e.deadEntity.runCommandAsync("function scpdtr_func/system/player_death");
        }
        if (e.deadEntity.typeId == "lc:dtr_blastdoor") {
            e.deadEntity.runCommandAsync("function scpdtr_func/system/blastdoor_barriers_opened");
        }
        // Stop if damaging entity is null
        if (e.damageSource.damagingEntity == null)
            return;
        const damagingEntity = e.damageSource.damagingEntity;
        const deadEntity = e.deadEntity;
        // Spawn SCP-049-2 if killed by SCP-049
        if (damagingEntity.typeId == ID_SCP049 && deadEntity.typeId == ID_PLAYER) {
            deadEntity.runCommandAsync("summon lc:dtr_steve_scp049_2 ~~~");
        }
    }
    catch (ex) { }
});
// Entity door 1 death
world.afterEvents.entityDie.subscribe((e) => {
    try {
        if (e.deadEntity.typeId == "lc:dtr_door1") {
            e.deadEntity.runCommandAsync("setblock ~~0~ air");
            e.deadEntity.runCommandAsync("setblock ~~1~ air");
        }
    }
    catch (ex) { }
});

//# sourceMappingURL=../../../../_scpdtr_scriptingDebug/scpdtr/copyPasteEverytime/main.js.map
