From c32dc902af24782e311f7bd2ad11998ad091ed6d Mon Sep 17 00:00:00 2001 From: Jayly <65847850+JaylyDev@users.noreply.github.com> Date: Sun, 2 Jun 2024 15:56:10 +0100 Subject: [PATCH] fix dummy move through walls during respawn --- assets/behavior_pack/entities/dummy.json | 20 ------- src/debug/statsDisplay.ts | 5 +- src/dummyEntity/dummyEntity.ts | 26 +++++++++ src/dummyEntity/navigationDetect.ts | 10 ++++ src/index.ts | 2 + src/terminator-death/index.ts | 12 +++-- src/terminator/escapeFromDanger.ts | 67 ++++++++++++++++++++++++ src/terminator/initialization.ts | 65 +---------------------- src/terminator/terminator.ts | 3 +- 9 files changed, 119 insertions(+), 91 deletions(-) create mode 100644 src/dummyEntity/dummyEntity.ts create mode 100644 src/terminator/escapeFromDanger.ts diff --git a/assets/behavior_pack/entities/dummy.json b/assets/behavior_pack/entities/dummy.json index f1a322a..c6574cf 100644 --- a/assets/behavior_pack/entities/dummy.json +++ b/assets/behavior_pack/entities/dummy.json @@ -121,26 +121,6 @@ "add": { "component_groups": ["dummy:force_spawn_entity"] } - }, - "dummy:request_spawning_steve": { - "set_property": { - "dummy:task_type": 1 - } - }, - "dummy:request_pathfind": { - "set_property": { - "dummy:task_type": 2 - } - }, - "dummy:request_spawning_alex": { - "set_property": { - "dummy:task_type": 3 - } - }, - "dummy:request_spawning_custom": { - "set_property": { - "dummy:task_type": 4 - } } } } diff --git a/src/debug/statsDisplay.ts b/src/debug/statsDisplay.ts index c997316..847b4d2 100644 --- a/src/debug/statsDisplay.ts +++ b/src/debug/statsDisplay.ts @@ -17,10 +17,9 @@ export function onHealthDebug(enabled: boolean) { objective: healthObjective, }); runId = system.runInterval(() => { - const terminators = overworld.getEntities({ + for (const terminator of overworld.getEntities({ type: "entity:terminator", - }); - for (const terminator of terminators) { + })) { const health = terminator.getComponent( EntityHealthComponent.componentId ) as EntityHealthComponent; diff --git a/src/dummyEntity/dummyEntity.ts b/src/dummyEntity/dummyEntity.ts new file mode 100644 index 0000000..a7159a5 --- /dev/null +++ b/src/dummyEntity/dummyEntity.ts @@ -0,0 +1,26 @@ +import { world, system, Vector3 } from "@minecraft/server"; +import { MinecraftDimensionTypes } from "@minecraft/vanilla-data"; + +export enum TaskType { + SpawnSteve = 1, + PathFind, + SpawnAlex, + SpawnCustom, +} + +const overworld = world.getDimension("overworld"); + +system.runInterval(() => { + for (const dummyEntity of overworld.getEntities({ type: "entity:dummy" })) { + const spawnLocation = dummyEntity.getDynamicProperty( + "dummy:spawn_location" + ) as Vector3 | undefined; + const spawnDimension = dummyEntity.getDynamicProperty( + "dummy:spawn_dimension" + ) as MinecraftDimensionTypes | undefined; + if (!spawnLocation || !spawnDimension) continue; + dummyEntity.teleport(spawnLocation, { + dimension: world.getDimension(spawnDimension), + }); + } +}); diff --git a/src/dummyEntity/navigationDetect.ts b/src/dummyEntity/navigationDetect.ts index 808e2e5..78729c4 100644 --- a/src/dummyEntity/navigationDetect.ts +++ b/src/dummyEntity/navigationDetect.ts @@ -1,5 +1,7 @@ import { Entity, system, world } from "@minecraft/server"; import { terminatorDie } from "../terminator-events/onTerminatorDie"; +import { debugEnabled } from "../config"; +import { Vector3Utils } from "@minecraft/math"; const overworld = world.getDimension("overworld"); @@ -20,6 +22,12 @@ system.runInterval(() => { maxDistance: 8, }) ) { + if (debugEnabled) + console.warn( + `Terminator navigated to location (${Vector3Utils.toString( + dummyEntity.location + )})` + ); dummyEntity.remove(); terminator.triggerEvent("terminator:remove_escape"); } @@ -35,4 +43,6 @@ terminatorDie.subscribe(({ deadEntity }) => { entity.getDynamicProperty("terminator:navigator") === deadEntity.id ); dummyEntity?.remove(); + if (debugEnabled) + console.warn(`Terminator (id: ${deadEntity.id}) died during navigation.`); }); diff --git a/src/index.ts b/src/index.ts index 55d8ef8..7d7d1d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,9 +9,11 @@ import "./terminator/ridingTransport"; import "./terminator-death/index"; import "./terminator/suffocation"; import "./terminator/rangedAttack"; +import "./terminator/escapeFromDanger"; import "./commands/index"; import "./guide/index"; import "./dummyEntity/navigationDetect"; +import "./dummyEntity/dummyEntity"; // Debug Features if (debugEnabled) { diff --git a/src/terminator-death/index.ts b/src/terminator-death/index.ts index 111c00b..15b0d38 100644 --- a/src/terminator-death/index.ts +++ b/src/terminator-death/index.ts @@ -7,6 +7,7 @@ import { terminatorDie } from "../terminator-events/onTerminatorDie"; import { sendDeathMessageCallback } from "./deathMessage"; import { MinecraftColor } from "../minecraft-color"; import { dropEntityInventory } from "./dropInventory"; +import { TaskType } from "../dummyEntity/dummyEntity"; enum TerminatorVariant { SteveDefault = 0, @@ -44,12 +45,17 @@ terminatorDie.subscribe((event) => { deadEntity.location ); dummyEntity.runCommand("fog @a remove respawn_lore"); + dummyEntity.setDynamicProperty("dummy:spawn_location", deadEntity.location); + dummyEntity.setDynamicProperty( + "dummy:spawn_dimension", + deadEntity.dimension.id + ); if (isSteveVariant) - dummyEntity.triggerEvent("dummy:request_spawning_steve"); + dummyEntity.setProperty("dummy:task_type", TaskType.SpawnSteve); else if (isAlexVariant) - dummyEntity.triggerEvent("dummy:request_spawning_alex"); + dummyEntity.setProperty("dummy:task_type", TaskType.SpawnAlex); else if (isCustomVariant) - dummyEntity.triggerEvent("dummy:request_spawning_custom"); + dummyEntity.setProperty("dummy:task_type", TaskType.SpawnCustom); } // Second Death else if ( diff --git a/src/terminator/escapeFromDanger.ts b/src/terminator/escapeFromDanger.ts new file mode 100644 index 0000000..27e65ae --- /dev/null +++ b/src/terminator/escapeFromDanger.ts @@ -0,0 +1,67 @@ +import { + EntityHealthComponent, + TicksPerSecond, + system, + world, +} from "@minecraft/server"; +import { MinecraftEffectTypes } from "@minecraft/vanilla-data"; +import { debugEnabled } from "../config"; +import { getSpreadLocation } from "../dummyEntity/spreadDummies"; +import { TerminatorEntity } from "./terminator"; + +const overworld = world.getDimension("overworld"); + +// detect when terminator's health is below 20hp (max 60hp) +system.runInterval(() => { + for (const entity of overworld.getEntities({ type: "entity:terminator" })) { + const health = entity.getComponent("health") as EntityHealthComponent; + const dangerEscapeTriggered = + (entity.getDynamicProperty("terminator:escape_triggered") as + | boolean + | undefined) ?? false; + + if (!health.isValid() || dangerEscapeTriggered) continue; + if ( + health.currentValue < 20 && + !entity.hasTag("terminatorNoRegeneration") + ) { + entity.addEffect(MinecraftEffectTypes.Regeneration, 6 * TicksPerSecond, { + amplifier: 4, + showParticles: false, + }); + entity.addEffect(MinecraftEffectTypes.Absorption, 24 * TicksPerSecond, { + amplifier: 3, + showParticles: false, + }); + entity.addEffect(MinecraftEffectTypes.Resistance, 60 * TicksPerSecond, { + amplifier: 0, + showParticles: false, + }); + entity.addEffect( + MinecraftEffectTypes.FireResistance, + 60 * TicksPerSecond, + { + amplifier: 0, + showParticles: false, + } + ); + + // Trigger terminator to pathfind to a random location within 32-48 block radius + const terminator = new TerminatorEntity(entity); + const locationOffsetXZ = { + x: Math.floor(Math.random() * 32 - 17 + terminator.location.x), + z: Math.floor(Math.random() * 32 - 17 + terminator.location.z), + }; + const locationOffset = getSpreadLocation( + locationOffsetXZ, + entity.dimension + ); + if (debugEnabled) + terminator.chat( + `I'm going to ${locationOffset.x}, ${locationOffset.y}, ${locationOffset.z}!` + ); + terminator.navigateToLocation(locationOffset); + terminator.setDynamicProperty("terminator:escape_triggered", true); + } + } +}); diff --git a/src/terminator/initialization.ts b/src/terminator/initialization.ts index 2cf9624..6f03e22 100644 --- a/src/terminator/initialization.ts +++ b/src/terminator/initialization.ts @@ -1,16 +1,6 @@ -import { - EntityHealthComponent, - RawText, - TicksPerSecond, - system, - world, -} from "@minecraft/server"; +import { RawText, system, world } from "@minecraft/server"; import { terminatorSpawn } from "../terminator-events/onTerminatorSpawn"; -import { MinecraftEffectTypes } from "@minecraft/vanilla-data"; import { MinecraftColor } from "../minecraft-color"; -import { TerminatorEntity } from "./terminator"; -import { getSpreadLocation } from "../dummyEntity/spreadDummies"; -import { debugEnabled } from "../config"; // naming tag terminatorSpawn.subscribe(({ entity }) => { @@ -44,56 +34,3 @@ terminatorSpawn.subscribe(({ entity }) => { world.playSound("mob.terminator.spawn", entity.location); }, 2); }); - -// detect when terminator's health is below 20hp (max 60hp) -terminatorSpawn.subscribe(({ entity }) => { - const health = entity.getComponent("health") as EntityHealthComponent; - let triggered = false; - - const id = system.runInterval(() => { - if (!health.isValid() || triggered) { - system.clearRun(id); - return; - } - if ( - health.currentValue < 20 && - !entity.hasTag("terminatorNoRegeneration") - ) { - entity.addEffect(MinecraftEffectTypes.Regeneration, 6 * TicksPerSecond, { - amplifier: 4, - showParticles: false, - }); - entity.addEffect(MinecraftEffectTypes.Absorption, 24 * TicksPerSecond, { - amplifier: 3, - showParticles: false, - }); - entity.addEffect(MinecraftEffectTypes.Resistance, 60 * TicksPerSecond, { - amplifier: 0, - showParticles: false, - }); - entity.addEffect( - MinecraftEffectTypes.FireResistance, - 60 * TicksPerSecond, - { - amplifier: 0, - showParticles: false, - } - ); - - // Trigger terminator to pathfind to a random location within 32-48 block radius - const terminator = new TerminatorEntity(entity); - const locationOffsetXZ = { - x: Math.floor(Math.random() * 32 - 17 + terminator.location.x), - z: Math.floor(Math.random() * 32 - 17 + terminator.location.z), - }; - const locationOffset = getSpreadLocation( - locationOffsetXZ, - entity.dimension - ); - if (debugEnabled) terminator.chat(`I'm going to ${locationOffset.x}, ${locationOffset.y}, ${locationOffset.z}!`); - terminator.navigateToLocation(locationOffset); - - triggered = true; - } - }); -}); diff --git a/src/terminator/terminator.ts b/src/terminator/terminator.ts index ee15517..e0c9ce0 100644 --- a/src/terminator/terminator.ts +++ b/src/terminator/terminator.ts @@ -31,6 +31,7 @@ import { UnbreakableBlocks, } from "../config"; import { MinecraftColor } from "../minecraft-color"; +import { TaskType } from "../dummyEntity/dummyEntity"; /** * Represents the state of a terminator entity in the world. @@ -377,7 +378,7 @@ export class TerminatorEntity implements Entity { */ navigateToLocation(location: Vector3): void { const dummyEntity = this.dimension.spawnEntity("entity:dummy", location); - dummyEntity.triggerEvent("dummy:request_pathfind"); + dummyEntity.setProperty("dummy:task_type", TaskType.PathFind); dummyEntity.setDynamicProperty("terminator:navigator", this.id); this.triggerEvent("terminator:escape"); }