| 1 | /** |
| 2 | * Build evaluateConditionSet context from ECS entity + conditionAccess snapshot. |
| 3 | * Used on simulation workers (no stateDb). |
| 4 | */ |
| 5 | import { Component } from "../ecs/components.js"; |
| 6 | import { countItemInSlots } from "../../../shared/items/inventory.js"; |
| 7 | import { resolveSkillRequirementLevel } from "../../../shared/combat/combatLevel.js"; |
| 8 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 9 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 10 | import { doorHasRequirements, doorRequirementsMet } from "../../../shared/world/doorRequirements.js"; |
| 11 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 12 | import { tileToChunk, tileToLocal, tileKey } from "../world/coordinates.js"; |
| 13 | |
| 14 | function killProgressKey (condition) |
| 15 | { |
| 16 | if (condition?.spawnerId) |
| 17 | { return `spawner:${condition.spawnerId}`; } |
| 18 | return `mob:${condition.mobTypeId}`; |
| 19 | } |
| 20 | |
| 21 | function interactProgressKey (condition) |
| 22 | { |
| 23 | if (condition?.tileX != null) |
| 24 | { return `tile:${condition.tileX},${condition.tileY}`; } |
| 25 | return `block:${condition.blockTypeId}`; |
| 26 | } |
| 27 | |
| 28 | function floorDiv (value, size) |
| 29 | { |
| 30 | return Math.floor(value / size); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Serializable snapshot stored on Component.conditionAccess. |
| 35 | * @param {import("../persistence/stateDb.js").StateDb} stateDb |
| 36 | * @param {number} characterId |
| 37 | */ |
| 38 | export function buildConditionAccessSnapshot (stateDb, characterId) |
| 39 | { |
| 40 | const questStates = Object.create(null); |
| 41 | const questProgress = Object.create(null); |
| 42 | for (const row of stateDb.listQuestStates(characterId)) |
| 43 | { |
| 44 | questStates[row.questId] = row.state; |
| 45 | questProgress[row.questId] = row.progress ?? {}; |
| 46 | } |
| 47 | const dialogueHeard = (stateDb.listDialogueHeard(characterId) ?? []).map( |
| 48 | (row) => `${row.dialogue_id}\0${row.step_id}` |
| 49 | ); |
| 50 | const dialogueTriggers = (stateDb.listDialogueTriggers?.(characterId) ?? []).map( |
| 51 | (row) => `${row.dialogue_id}\0${row.trigger_id}` |
| 52 | ); |
| 53 | const visitedDimensions = (stateDb.listVisitedDimensions(characterId) ?? []).map( |
| 54 | (row) => normalizeDimensionId(row.dimensionId) |
| 55 | ); |
| 56 | return { |
| 57 | questStates, |
| 58 | questProgress, |
| 59 | dialogueHeard, |
| 60 | dialogueTriggers, |
| 61 | visitedDimensions |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param {object} world — WorkerWorld-like (ecs + chunkData) |
| 67 | * @param {number} entityId |
| 68 | */ |
| 69 | export function buildEntityConditionContext (world, entityId) |
| 70 | { |
| 71 | const handle = world.ecs.handleOf(entityId); |
| 72 | if (handle == null) |
| 73 | { return null; } |
| 74 | const position = world.ecs.get(handle, Component.position); |
| 75 | if (!position) |
| 76 | { return null; } |
| 77 | const tileX = position.tileX | 0; |
| 78 | const tileY = position.tileY | 0; |
| 79 | const dimensionId = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 80 | const inventory = world.ecs.get(handle, Component.inventory)?.slots ?? []; |
| 81 | const health = world.ecs.get(handle, Component.health); |
| 82 | const stamina = world.ecs.get(handle, Component.stamina); |
| 83 | const skills = world.ecs.get(handle, Component.skills); |
| 84 | const access = world.ecs.get(handle, Component.conditionAccess) ?? { |
| 85 | questStates: {}, |
| 86 | questProgress: {}, |
| 87 | dialogueHeard: [], |
| 88 | dialogueTriggers: [], |
| 89 | visitedDimensions: [] |
| 90 | }; |
| 91 | const heard = new Set(access.dialogueHeard ?? []); |
| 92 | const triggers = new Set(access.dialogueTriggers ?? []); |
| 93 | const visited = new Set(access.visitedDimensions ?? []); |
| 94 | const questStates = access.questStates ?? {}; |
| 95 | const questProgress = access.questProgress ?? {}; |
| 96 | return { |
| 97 | tileX, |
| 98 | tileY, |
| 99 | chunkX: floorDiv(tileX, CHUNK_SIZE), |
| 100 | chunkY: floorDiv(tileY, CHUNK_SIZE), |
| 101 | dimensionId, |
| 102 | hp: health?.current ?? 0, |
| 103 | maxHp: health?.max ?? 1, |
| 104 | stamina: stamina?.current ?? 0, |
| 105 | maxStamina: stamina?.max ?? 1, |
| 106 | countItem: (itemId) => countItemInSlots(inventory, itemId | 0), |
| 107 | questState: (questId) => questStates[String(questId)] ?? "not-started", |
| 108 | dialogueHeard: (dialogueId, stepId) => heard.has(`${dialogueId}\0${stepId}`), |
| 109 | dialogueTriggerLatched: (dialogueId, trigger) => triggers.has(`${dialogueId}\0${trigger}`), |
| 110 | skillLevel: (skillId) => resolveSkillRequirementLevel(skills ?? {}, skillId), |
| 111 | hasVisitedDimension: (dimId) => visited.has(normalizeDimensionId(dimId)), |
| 112 | killCount: (condition) => |
| 113 | { |
| 114 | const key = killProgressKey(condition); |
| 115 | let total = 0; |
| 116 | for (const progress of Object.values(questProgress)) |
| 117 | { total += progress?.counters?.[key] | 0; } |
| 118 | return total; |
| 119 | }, |
| 120 | interactBlockDone: (condition) => |
| 121 | { |
| 122 | const key = interactProgressKey(condition); |
| 123 | for (const progress of Object.values(questProgress)) |
| 124 | { |
| 125 | if (progress?.interacts?.[key]) |
| 126 | { return true; } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | function readDoorBlock (world, tileX, tileY, dimensionId) |
| 134 | { |
| 135 | const data = world.chunkData?.(tileToChunk(tileX), tileToChunk(tileY), dimensionId); |
| 136 | if (!data) |
| 137 | { return null; } |
| 138 | const index = tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX); |
| 139 | return data.blocks?.[index] ?? null; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Players must meet door requirements; mobs ignore gates. |
| 144 | * Empty requirements always allow. |
| 145 | */ |
| 146 | export function entityDoorRequirementsAllow (world, entityId, tileX, tileY) |
| 147 | { |
| 148 | const handle = world.ecs.handleOf(entityId); |
| 149 | if (handle == null) |
| 150 | { return true; } |
| 151 | const position = world.ecs.get(handle, Component.position); |
| 152 | const dimensionId = normalizeDimensionId(position?.dimensionId ?? OVERWORLD_ID); |
| 153 | const block = readDoorBlock(world, tileX, tileY, dimensionId); |
| 154 | if (!block || (block.typeId | 0) !== BlockType.door || !doorHasRequirements(block)) |
| 155 | { return true; } |
| 156 | if (!world.ecs.get(handle, Component.character)) |
| 157 | { return true; } |
| 158 | const ctx = buildEntityConditionContext(world, entityId); |
| 159 | return doorRequirementsMet(block, ctx); |
| 160 | } |
| 161 | |
| 162 | export { tileKey }; |