| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { MobId, mobDefinition } from "../../../shared/definitions/mobs.js"; |
| 3 | import { mobCanFight, mobIsAggressive } from "../../../shared/definitions/mobCombat.js"; |
| 4 | import |
| 5 | { |
| 6 | chebyshev, |
| 7 | engageTarget, |
| 8 | ensureMobCombat, |
| 9 | hasCombatLineOfSight |
| 10 | } from "../combat/service.js"; |
| 11 | import { mobComponents } from "../simulation/spawners.js"; |
| 12 | |
| 13 | const SHADE_AGGRO_RANGE = 12; |
| 14 | |
| 15 | /** |
| 16 | * Expire summons and pick combat targets for Shade companions. |
| 17 | * Jadeite fairies are handled by {@link stepJadeiteFairies}. |
| 18 | * @returns {{ events: object[], despawnIds: number[] }} |
| 19 | */ |
| 20 | export function stepSummons (world, now = Date.now()) |
| 21 | { |
| 22 | const events = []; |
| 23 | const despawnIds = []; |
| 24 | for (const entity of world.ecs.query(Component.summon, Component.creature, Component.position)) |
| 25 | { |
| 26 | const { handle, entityId, summon, position } = entity; |
| 27 | if (summon?.kind === "jadeite-fairy") |
| 28 | { continue; } |
| 29 | if (summon.expiresAt == null) |
| 30 | { continue; } |
| 31 | if (now >= Number(summon.expiresAt)) |
| 32 | { |
| 33 | despawnIds.push(entityId); |
| 34 | continue; |
| 35 | } |
| 36 | if (world.ecs.has(handle, Component.dead)) |
| 37 | { continue; } |
| 38 | const combat = world.ecs.get(handle, Component.combat); |
| 39 | if (combat?.targetEntityId != null) |
| 40 | { |
| 41 | const stillValid = resolveSummonTarget(world, combat.targetEntityId, summon.ownerEntityId); |
| 42 | if (stillValid) |
| 43 | { continue; } |
| 44 | combat.targetEntityId = null; |
| 45 | } |
| 46 | |
| 47 | const targetId = pickShadeTarget(world, entityId, handle, position, summon.ownerEntityId); |
| 48 | if (targetId == null) |
| 49 | { continue; } |
| 50 | if (!ensureMobCombat(world.ecs, entityId)) |
| 51 | { continue; } |
| 52 | engageTarget(world.ecs, entityId, targetId, now); |
| 53 | } |
| 54 | return { events, despawnIds }; |
| 55 | } |
| 56 | |
| 57 | function resolveSummonTarget (world, targetId, ownerEntityId) |
| 58 | { |
| 59 | if (targetId == null || targetId === ownerEntityId) |
| 60 | { return false; } |
| 61 | const handle = world.ecs.handleOf(targetId); |
| 62 | if (handle == null) |
| 63 | { return false; } |
| 64 | if (world.ecs.has(handle, Component.dead)) |
| 65 | { return false; } |
| 66 | const health = world.ecs.get(handle, Component.health); |
| 67 | if (health && health.current <= 0) |
| 68 | { return false; } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | function pickShadeTarget (world, shadeId, shadeHandle, shadePos, ownerEntityId) |
| 73 | { |
| 74 | const ownerHandle = world.ecs.handleOf(ownerEntityId); |
| 75 | if (ownerHandle == null || world.ecs.has(ownerHandle, Component.dead)) |
| 76 | { return null; } |
| 77 | |
| 78 | const ownerCombat = world.ecs.get(ownerHandle, Component.combat); |
| 79 | if (ownerCombat?.targetEntityId != null |
| 80 | && ownerCombat.targetEntityId !== shadeId |
| 81 | && resolveSummonTarget(world, ownerCombat.targetEntityId, ownerEntityId)) |
| 82 | { return ownerCombat.targetEntityId; } |
| 83 | |
| 84 | // Anything attacking the owner. |
| 85 | for (const entity of world.ecs.query(Component.combat, Component.creature, Component.position)) |
| 86 | { |
| 87 | if (entity.entityId === shadeId || entity.entityId === ownerEntityId) |
| 88 | { continue; } |
| 89 | if (entity.combat.targetEntityId !== ownerEntityId) |
| 90 | { continue; } |
| 91 | if (world.ecs.has(entity.handle, Component.dead)) |
| 92 | { continue; } |
| 93 | if (!inRangeLos(world, shadePos, entity.position)) |
| 94 | { continue; } |
| 95 | return entity.entityId; |
| 96 | } |
| 97 | |
| 98 | // Aggressive mobs nearby. |
| 99 | let bestId = null; |
| 100 | let bestDist = Infinity; |
| 101 | for (const entity of world.ecs.query(Component.creature, Component.position)) |
| 102 | { |
| 103 | if (entity.entityId === shadeId || entity.entityId === ownerEntityId) |
| 104 | { continue; } |
| 105 | if (world.ecs.has(entity.handle, Component.dead)) |
| 106 | { continue; } |
| 107 | if (world.ecs.get(entity.handle, Component.summon)) |
| 108 | { continue; } |
| 109 | const def = mobDefinition(entity.creature.mobTypeId); |
| 110 | const origin = world.ecs.get(entity.handle, Component.spawnOrigin); |
| 111 | const aggressive = origin ? !!origin.aggressive : mobIsAggressive(def); |
| 112 | if (!aggressive || !mobCanFight(def)) |
| 113 | { continue; } |
| 114 | const dist = chebyshev( |
| 115 | shadePos.tileX, |
| 116 | shadePos.tileY, |
| 117 | entity.position.tileX, |
| 118 | entity.position.tileY |
| 119 | ); |
| 120 | if (dist < 1 || dist > SHADE_AGGRO_RANGE || dist >= bestDist) |
| 121 | { continue; } |
| 122 | if (!inRangeLos(world, shadePos, entity.position)) |
| 123 | { continue; } |
| 124 | bestDist = dist; |
| 125 | bestId = entity.entityId; |
| 126 | } |
| 127 | return bestId; |
| 128 | } |
| 129 | |
| 130 | function inRangeLos (world, fromPos, toPos) |
| 131 | { |
| 132 | const dist = chebyshev(fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY); |
| 133 | if (dist < 1 || dist > SHADE_AGGRO_RANGE) |
| 134 | { return false; } |
| 135 | return hasCombatLineOfSight(world, fromPos.tileX, fromPos.tileY, toPos.tileX, toPos.tileY); |
| 136 | } |
| 137 | |
| 138 | /** Build ECS components for a Shade summon spawn. */ |
| 139 | export function shadeSummonComponents ({ tileX, tileY, level, ownerEntityId, expiresAt, dimensionId }) |
| 140 | { |
| 141 | const components = mobComponents({ |
| 142 | mobTypeId: MobId.shade, |
| 143 | tileX, |
| 144 | tileY, |
| 145 | level, |
| 146 | dimensionId, |
| 147 | displayName: "Shade", |
| 148 | aggressive: false |
| 149 | }); |
| 150 | components.summon = { |
| 151 | ownerEntityId, |
| 152 | expiresAt |
| 153 | }; |
| 154 | return components; |
| 155 | } |
| 156 | |
| 157 | export function publicSummonDespawn (entityId) |
| 158 | { |
| 159 | return { type: "entity-despawn", entityId }; |
| 160 | } |