| 1 | /** Shared condition kinds + ConditionSet evaluation (server provides snapshot). */ |
| 2 | |
| 3 | import { OVERWORLD_ID, normalizeDimensionId } from "../world/dimensions.js"; |
| 4 | import { isMetaSkillId } from "../definitions/skills.js"; |
| 5 | import { legacyQuestStepNumber } from "./questProgress.js"; |
| 6 | |
| 7 | export const ConditionKind = Object.freeze({ |
| 8 | hasItem: "hasItem", |
| 9 | atTile: "atTile", |
| 10 | nearTile: "nearTile", |
| 11 | inRect: "inRect", |
| 12 | atChunk: "atChunk", |
| 13 | inDimension: "inDimension", |
| 14 | hasVisitedDimension: "hasVisitedDimension", |
| 15 | questState: "questState", |
| 16 | /** @deprecated Read on load and converted. Prefer hasCompletedStep. */ |
| 17 | questStepAtLeast: "questStepAtLeast", |
| 18 | hasCompletedStep: "hasCompletedStep", |
| 19 | dialogueHeard: "dialogueHeard", |
| 20 | dialogueTrigger: "dialogueTrigger", |
| 21 | hpAtLeast: "hpAtLeast", |
| 22 | staminaAtLeast: "staminaAtLeast", |
| 23 | hpPercentAtLeast: "hpPercentAtLeast", |
| 24 | staminaPercentAtLeast: "staminaPercentAtLeast", |
| 25 | skillLevelAtLeast: "skillLevelAtLeast", |
| 26 | killCount: "killCount", |
| 27 | killMobFromSpawner: "killMobFromSpawner", |
| 28 | interactBlock: "interactBlock" |
| 29 | }); |
| 30 | |
| 31 | /** Required threshold for skillLevelAtLeast (meta levels may exceed 99). */ |
| 32 | export function skillRequirementThreshold (condition) |
| 33 | { |
| 34 | const level = Math.max(1, Number(condition?.level) || 1) | 0; |
| 35 | if (isMetaSkillId(condition?.skillId)) |
| 36 | { return Math.min(99_999, level); } |
| 37 | return Math.min(99, level); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * How clauses in a ConditionSet combine. |
| 42 | * - allOf: every clause must pass (legacy default) |
| 43 | * - oneOrMoreOf: at least one clause must pass |
| 44 | * - onlyOneOf: exactly one clause must pass |
| 45 | * - noneOf: fail if any clause passes (inverted one-or-more) |
| 46 | */ |
| 47 | export const ConditionSetMode = Object.freeze({ |
| 48 | allOf: "allOf", |
| 49 | oneOrMoreOf: "oneOrMoreOf", |
| 50 | onlyOneOf: "onlyOneOf", |
| 51 | noneOf: "noneOf" |
| 52 | }); |
| 53 | |
| 54 | export const CONDITION_SET_MODE_LABELS = Object.freeze({ |
| 55 | [ConditionSetMode.allOf]: "All of", |
| 56 | [ConditionSetMode.oneOrMoreOf]: "One or more of", |
| 57 | [ConditionSetMode.onlyOneOf]: "Only one of", |
| 58 | [ConditionSetMode.noneOf]: "None of" |
| 59 | }); |
| 60 | |
| 61 | export function normalizeConditionSetMode (raw) |
| 62 | { |
| 63 | const mode = String(raw ?? "").trim(); |
| 64 | if (Object.values(ConditionSetMode).includes(mode)) |
| 65 | { return mode; } |
| 66 | return ConditionSetMode.allOf; |
| 67 | } |
| 68 | |
| 69 | export function emptyConditionSet () |
| 70 | { |
| 71 | return { mode: ConditionSetMode.allOf, all: [] }; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param {object | Array | null | undefined} raw |
| 76 | * @param {{ quests?: Function | Array | Map } | Function | null} [options] |
| 77 | * Quest catalog so legacy questStepAtLeast / numeric questState can convert on load. |
| 78 | */ |
| 79 | export function normalizeConditionSet (raw, options = null) |
| 80 | { |
| 81 | const lookup = questLookupFromOptions(options); |
| 82 | const all = []; |
| 83 | const list = Array.isArray(raw?.all) ? raw.all : (Array.isArray(raw) ? raw : []); |
| 84 | for (const entry of list) |
| 85 | { |
| 86 | const clause = normalizeConditionClause(entry, lookup); |
| 87 | if (clause) |
| 88 | { all.push(clause); } |
| 89 | } |
| 90 | return { |
| 91 | mode: normalizeConditionSetMode(raw?.mode), |
| 92 | all |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | function questLookupFromOptions (options) |
| 97 | { |
| 98 | const quests = typeof options === "function" ? options : options?.quests; |
| 99 | if (typeof quests === "function") |
| 100 | { return quests; } |
| 101 | if (!quests) |
| 102 | { return null; } |
| 103 | const map = new Map(); |
| 104 | const list = quests instanceof Map ? [...quests.values()] : quests; |
| 105 | for (const quest of list ?? []) |
| 106 | { |
| 107 | const questId = String(quest?.questId ?? "").trim(); |
| 108 | if (questId) |
| 109 | { map.set(questId, quest); } |
| 110 | } |
| 111 | return (questId) => map.get(String(questId ?? "")) ?? null; |
| 112 | } |
| 113 | |
| 114 | function normalizeConditionClause (entry, lookup) |
| 115 | { |
| 116 | if (!entry || typeof entry !== "object") |
| 117 | { return null; } |
| 118 | const condition = entry.condition && typeof entry.condition === "object" |
| 119 | ? entry.condition |
| 120 | : entry; |
| 121 | const kind = String(condition.kind ?? "").trim(); |
| 122 | if (!kind) |
| 123 | { return null; } |
| 124 | return { |
| 125 | not: !!entry.not, |
| 126 | condition: convertLegacyQuestCondition( |
| 127 | normalizeConditionFields({ ...condition, kind }), |
| 128 | lookup |
| 129 | ) |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * questStepAtLeast step 1 → quest started. |
| 135 | * questStepAtLeast step N (N > 1) → hasCompletedStep of step N-1. |
| 136 | * Past the end of a known quest → quest completed (the only way the old check passed). |
| 137 | * Numeric questState → that step's id, when the quest def is available. |
| 138 | * Unknown quests keep questStepAtLeast so a later load can still convert. |
| 139 | */ |
| 140 | export function convertLegacyQuestCondition (condition, lookup) |
| 141 | { |
| 142 | if (!condition || typeof condition !== "object") |
| 143 | { return condition; } |
| 144 | if (condition.kind === ConditionKind.questState) |
| 145 | { |
| 146 | const index = legacyQuestStepNumber(condition.state); |
| 147 | if (index == null || !lookup) |
| 148 | { return condition; } |
| 149 | const step = lookup(condition.questId)?.steps?.[index - 1]; |
| 150 | if (!step?.stepId) |
| 151 | { return condition; } |
| 152 | return { ...condition, state: step.stepId }; |
| 153 | } |
| 154 | if (condition.kind !== ConditionKind.questStepAtLeast) |
| 155 | { return condition; } |
| 156 | const stepNum = Math.max(1, Number(condition.step) || 1); |
| 157 | const questId = String(condition.questId ?? "").trim().slice(0, 80); |
| 158 | if (stepNum <= 1) |
| 159 | { return { kind: ConditionKind.questState, questId, state: "started" }; } |
| 160 | const def = lookup?.(questId) ?? null; |
| 161 | const steps = def?.steps ?? null; |
| 162 | if (!steps) |
| 163 | { |
| 164 | return { kind: ConditionKind.questStepAtLeast, questId, step: stepNum }; |
| 165 | } |
| 166 | const prev = steps[stepNum - 2]; |
| 167 | if (prev?.stepId) |
| 168 | { |
| 169 | return { |
| 170 | kind: ConditionKind.hasCompletedStep, |
| 171 | questId, |
| 172 | stepId: String(prev.stepId) |
| 173 | }; |
| 174 | } |
| 175 | return { kind: ConditionKind.questState, questId, state: "completed" }; |
| 176 | } |
| 177 | |
| 178 | /** Ensure place conditions carry dimensionId (default overworld). */ |
| 179 | function normalizeConditionFields (condition) |
| 180 | { |
| 181 | const kind = condition.kind; |
| 182 | if ( |
| 183 | kind === ConditionKind.atTile |
| 184 | || kind === ConditionKind.nearTile |
| 185 | || kind === ConditionKind.inRect |
| 186 | || kind === ConditionKind.atChunk |
| 187 | || kind === ConditionKind.interactBlock |
| 188 | || kind === ConditionKind.inDimension |
| 189 | || kind === ConditionKind.hasVisitedDimension |
| 190 | ) |
| 191 | { |
| 192 | return { |
| 193 | ...condition, |
| 194 | dimensionId: normalizeDimensionId(condition.dimensionId ?? OVERWORLD_ID) |
| 195 | }; |
| 196 | } |
| 197 | if (kind === ConditionKind.hasCompletedStep) |
| 198 | { |
| 199 | return { |
| 200 | kind, |
| 201 | questId: String(condition.questId ?? "").trim().slice(0, 80), |
| 202 | stepId: String(condition.stepId ?? "").trim().slice(0, 64) |
| 203 | }; |
| 204 | } |
| 205 | if (kind === ConditionKind.questState) |
| 206 | { |
| 207 | return { |
| 208 | ...condition, |
| 209 | kind, |
| 210 | questId: String(condition.questId ?? "").trim().slice(0, 80) |
| 211 | }; |
| 212 | } |
| 213 | return condition; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param {ReturnType<typeof normalizeConditionSet>} set |
| 218 | * @param {object} ctx — snapshot from server (see evaluateCondition) |
| 219 | */ |
| 220 | export function evaluateConditionSet (set, ctx) |
| 221 | { |
| 222 | const clauses = set?.all ?? []; |
| 223 | if (clauses.length === 0) |
| 224 | { return true; } |
| 225 | let passCount = 0; |
| 226 | for (const clause of clauses) |
| 227 | { |
| 228 | const raw = evaluateCondition(clause.condition, ctx); |
| 229 | const ok = clause.not ? !raw : raw; |
| 230 | if (ok) |
| 231 | { passCount += 1; } |
| 232 | } |
| 233 | const mode = normalizeConditionSetMode(set?.mode); |
| 234 | switch (mode) |
| 235 | { |
| 236 | case ConditionSetMode.oneOrMoreOf: |
| 237 | return passCount >= 1; |
| 238 | case ConditionSetMode.onlyOneOf: |
| 239 | return passCount === 1; |
| 240 | case ConditionSetMode.noneOf: |
| 241 | return passCount === 0; |
| 242 | case ConditionSetMode.allOf: |
| 243 | default: |
| 244 | return passCount === clauses.length; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | function sameDimension (condition, ctx) |
| 249 | { |
| 250 | const want = normalizeDimensionId(condition.dimensionId ?? OVERWORLD_ID); |
| 251 | const have = normalizeDimensionId(ctx.dimensionId ?? OVERWORLD_ID); |
| 252 | return want === have; |
| 253 | } |
| 254 | |
| 255 | export function evaluateCondition (condition, ctx) |
| 256 | { |
| 257 | if (!condition || typeof condition !== "object") |
| 258 | { return false; } |
| 259 | const kind = condition.kind; |
| 260 | switch (kind) |
| 261 | { |
| 262 | case ConditionKind.hasItem: |
| 263 | return (ctx.countItem?.(condition.itemId) ?? 0) >= Math.max(1, Number(condition.quantity) || 1); |
| 264 | case ConditionKind.atTile: |
| 265 | return sameDimension(condition, ctx) |
| 266 | && ctx.tileX === (Number(condition.tileX) | 0) |
| 267 | && ctx.tileY === (Number(condition.tileY) | 0); |
| 268 | case ConditionKind.nearTile: |
| 269 | { |
| 270 | if (!sameDimension(condition, ctx)) |
| 271 | { return false; } |
| 272 | const range = Math.max(1, Number(condition.range) || 1); |
| 273 | const dx = Math.abs(ctx.tileX - (Number(condition.tileX) | 0)); |
| 274 | const dy = Math.abs(ctx.tileY - (Number(condition.tileY) | 0)); |
| 275 | return Math.max(dx, dy) <= range; |
| 276 | } |
| 277 | case ConditionKind.inRect: |
| 278 | { |
| 279 | if (!sameDimension(condition, ctx)) |
| 280 | { return false; } |
| 281 | const minX = Math.min(Number(condition.minTileX) | 0, Number(condition.maxTileX) | 0); |
| 282 | const maxX = Math.max(Number(condition.minTileX) | 0, Number(condition.maxTileX) | 0); |
| 283 | const minY = Math.min(Number(condition.minTileY) | 0, Number(condition.maxTileY) | 0); |
| 284 | const maxY = Math.max(Number(condition.minTileY) | 0, Number(condition.maxTileY) | 0); |
| 285 | return ctx.tileX >= minX && ctx.tileX <= maxX && ctx.tileY >= minY && ctx.tileY <= maxY; |
| 286 | } |
| 287 | case ConditionKind.atChunk: |
| 288 | return sameDimension(condition, ctx) |
| 289 | && ctx.chunkX === (Number(condition.chunkX) | 0) |
| 290 | && ctx.chunkY === (Number(condition.chunkY) | 0); |
| 291 | case ConditionKind.inDimension: |
| 292 | return normalizeDimensionId(ctx.dimensionId ?? OVERWORLD_ID) |
| 293 | === normalizeDimensionId(condition.dimensionId ?? OVERWORLD_ID); |
| 294 | case ConditionKind.hasVisitedDimension: |
| 295 | return !!ctx.hasVisitedDimension?.( |
| 296 | normalizeDimensionId(condition.dimensionId ?? OVERWORLD_ID) |
| 297 | ); |
| 298 | case ConditionKind.questState: |
| 299 | return questStatesEqual( |
| 300 | ctx.questState?.(condition.questId), |
| 301 | condition.state, |
| 302 | condition.questId, |
| 303 | ctx |
| 304 | ); |
| 305 | case ConditionKind.hasCompletedStep: |
| 306 | return !!ctx.hasCompletedStep?.(condition.questId, condition.stepId); |
| 307 | case ConditionKind.questStepAtLeast: |
| 308 | return evaluateLegacyQuestStepAtLeast(condition, ctx); |
| 309 | case ConditionKind.dialogueHeard: |
| 310 | return !!ctx.dialogueHeard?.(condition.dialogueId, condition.stepId); |
| 311 | case ConditionKind.dialogueTrigger: |
| 312 | return !!ctx.dialogueTriggerLatched?.(condition.dialogueId, condition.trigger); |
| 313 | case ConditionKind.hpAtLeast: |
| 314 | return (ctx.hp ?? 0) >= Math.max(0, Number(condition.amount) || 0); |
| 315 | case ConditionKind.staminaAtLeast: |
| 316 | return (ctx.stamina ?? 0) >= Math.max(0, Number(condition.amount) || 0); |
| 317 | case ConditionKind.hpPercentAtLeast: |
| 318 | { |
| 319 | const max = Math.max(1, ctx.maxHp ?? 1); |
| 320 | const pct = ((ctx.hp ?? 0) / max) * 100; |
| 321 | return pct >= Math.min(100, Math.max(0, Number(condition.percent) || 0)); |
| 322 | } |
| 323 | case ConditionKind.staminaPercentAtLeast: |
| 324 | { |
| 325 | const max = Math.max(1, ctx.maxStamina ?? 1); |
| 326 | const pct = ((ctx.stamina ?? 0) / max) * 100; |
| 327 | return pct >= Math.min(100, Math.max(0, Number(condition.percent) || 0)); |
| 328 | } |
| 329 | case ConditionKind.skillLevelAtLeast: |
| 330 | return (ctx.skillLevel?.(condition.skillId) ?? 1) >= skillRequirementThreshold(condition); |
| 331 | case ConditionKind.killCount: |
| 332 | case ConditionKind.killMobFromSpawner: |
| 333 | return (ctx.killCount?.(condition) ?? 0) >= Math.max(1, Number(condition.count) || 1); |
| 334 | case ConditionKind.interactBlock: |
| 335 | return sameDimension(condition, ctx) && !!ctx.interactBlockDone?.(condition); |
| 336 | default: |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | function questStatesEqual (actual, expected, questId, ctx) |
| 342 | { |
| 343 | const want = resolveExpectedQuestState(expected, questId, ctx); |
| 344 | if (want === "started") |
| 345 | { return actual != null && actual !== "not-started"; } |
| 346 | if (want === "not-started") |
| 347 | { return actual == null || actual === "not-started"; } |
| 348 | if (want === "completed") |
| 349 | { return actual === "completed"; } |
| 350 | return String(actual ?? "") === String(want ?? ""); |
| 351 | } |
| 352 | |
| 353 | /** Numeric questState ("on step N") maps through the quest's step list when known. */ |
| 354 | function resolveExpectedQuestState (expected, questId, ctx) |
| 355 | { |
| 356 | if (expected === "not-started" || expected === "completed" || expected === "started") |
| 357 | { return expected; } |
| 358 | const index = legacyQuestStepNumber(expected); |
| 359 | if (index != null) |
| 360 | { |
| 361 | const steps = ctx?.questSteps?.(questId); |
| 362 | if (Array.isArray(steps) && steps[index - 1]) |
| 363 | { return steps[index - 1]; } |
| 364 | } |
| 365 | return expected; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Unconverted questStepAtLeast, same rule as convertLegacyQuestCondition: |
| 370 | * step 1 = started, step N = has completed step N-1, past the end = quest completed. |
| 371 | */ |
| 372 | function evaluateLegacyQuestStepAtLeast (condition, ctx) |
| 373 | { |
| 374 | const stepNum = Math.max(1, Number(condition?.step) || 1); |
| 375 | const questId = condition?.questId; |
| 376 | const actual = ctx.questState?.(questId); |
| 377 | if (stepNum <= 1) |
| 378 | { return actual != null && actual !== "not-started"; } |
| 379 | const steps = ctx.questSteps?.(questId); |
| 380 | if (!Array.isArray(steps)) |
| 381 | { return false; } |
| 382 | const prev = steps[stepNum - 2]; |
| 383 | if (!prev) |
| 384 | { return actual === "completed"; } |
| 385 | return !!ctx.hasCompletedStep?.(questId, prev); |
| 386 | } |
| 387 | |
| 388 | export const EffectKind = Object.freeze({ |
| 389 | giveItem: "giveItem", |
| 390 | giveXp: "giveXp", |
| 391 | takeItem: "takeItem", |
| 392 | startQuest: "startQuest" |
| 393 | }); |
| 394 | |
| 395 | export function normalizeEffects (raw) |
| 396 | { |
| 397 | if (!Array.isArray(raw)) |
| 398 | { return []; } |
| 399 | const out = []; |
| 400 | for (const entry of raw) |
| 401 | { |
| 402 | if (!entry || typeof entry !== "object") |
| 403 | { continue; } |
| 404 | const kind = String(entry.kind ?? "").trim(); |
| 405 | if (!Object.values(EffectKind).includes(kind)) |
| 406 | { continue; } |
| 407 | if (kind === EffectKind.giveItem) |
| 408 | { |
| 409 | const itemId = Number(entry.itemId); |
| 410 | if (!Number.isFinite(itemId) || itemId <= 0) |
| 411 | { continue; } |
| 412 | out.push({ |
| 413 | kind, |
| 414 | itemId: itemId | 0, |
| 415 | quantity: Math.max(1, Math.min(99_999, Number(entry.quantity) || 1)) | 0, |
| 416 | toBank: !!entry.toBank |
| 417 | }); |
| 418 | continue; |
| 419 | } |
| 420 | if (kind === EffectKind.giveXp) |
| 421 | { |
| 422 | const skillId = String(entry.skillId ?? "").trim(); |
| 423 | const amount = Math.max(0, Math.min(10_000_000, Number(entry.amount) || 0)) | 0; |
| 424 | if (!skillId || amount <= 0) |
| 425 | { continue; } |
| 426 | out.push({ kind, skillId, amount }); |
| 427 | continue; |
| 428 | } |
| 429 | if (kind === EffectKind.takeItem) |
| 430 | { |
| 431 | const itemId = Number(entry.itemId); |
| 432 | if (!Number.isFinite(itemId) || itemId <= 0) |
| 433 | { continue; } |
| 434 | out.push({ |
| 435 | kind, |
| 436 | itemId: itemId | 0, |
| 437 | quantity: Math.max(1, Math.min(99_999, Number(entry.quantity) || 1)) | 0 |
| 438 | }); |
| 439 | continue; |
| 440 | } |
| 441 | if (kind === EffectKind.startQuest) |
| 442 | { |
| 443 | const questId = String(entry.questId ?? "").trim().slice(0, 80); |
| 444 | if (!questId) |
| 445 | { continue; } |
| 446 | out.push({ kind, questId }); |
| 447 | } |
| 448 | } |
| 449 | return out; |
| 450 | } |