| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { SkillId, skillLevel } from "../../../shared/definitions/skills.js"; |
| 3 | import { SpellId, spellDefinition, spellDifficulty } from "../../../shared/definitions/spells.js"; |
| 4 | import |
| 5 | { |
| 6 | canCastSpell, |
| 7 | defaultPlayerSpells, |
| 8 | normalizePlayerSpells, |
| 9 | publicSpellsView, |
| 10 | storedPlayerSpells, |
| 11 | toggleFavorite, |
| 12 | learnSpell |
| 13 | } from "../../../shared/spells/state.js"; |
| 14 | import |
| 15 | { |
| 16 | CAST_FLASH_MS, |
| 17 | COMBAT_RANGE_TILES, |
| 18 | LIGHT_DURATION_MS_PER_MIND, |
| 19 | SPELL_COOLDOWN_MS, |
| 20 | SPELL_LIGHT_RANGE_TILES, |
| 21 | SPELL_PROJECTILE_MS, |
| 22 | castFlashRadiusTiles |
| 23 | } from "../../../shared/spells/constants.js"; |
| 24 | import |
| 25 | { |
| 26 | mobDamageAffinities, |
| 27 | mobElementAffinities, |
| 28 | ElementMultiplier, |
| 29 | resolveMagicAttack, |
| 30 | resolveSpellCast, |
| 31 | resolveSpellPotency |
| 32 | } from "../../../shared/combat/magic.js"; |
| 33 | import { rollSpellDamage } from "../../../shared/combat/rolls.js"; |
| 34 | import { mobDefinition } from "../../../shared/definitions/mobs.js"; |
| 35 | import { awardCombatHitXp, awardDamageHealthXp, awardSpellCastXp, syncPlayerHealth } from "../skills/service.js"; |
| 36 | import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../../../shared/equipment/stats.js"; |
| 37 | import { statusSkillBonuses, applyStatusEffect, emptyStatusEffects, StatusEffectType, publicStatusEffectsView, hasStatusEffect } from "../../../shared/statusEffects/rules.js"; |
| 38 | import |
| 39 | { |
| 40 | chebyshev, |
| 41 | clearEngage, |
| 42 | hasCombatLineOfSight, |
| 43 | queueStandInRangeApproach, |
| 44 | resolveCombatTarget, |
| 45 | applyDamageAndKill |
| 46 | } from "../combat/service.js"; |
| 47 | import { storedSkillXp } from "../../../shared/skills/state.js"; |
| 48 | import { spellCastXpFromDifficulty } from "../../../shared/skills/progression.js"; |
| 49 | import { publicSpellLight, SpellLightRegistry } from "./lights.js"; |
| 50 | import { spellbookInstance, spellbookItemIdForSpell, spellIdFromSpellbookStack } from "../../../shared/definitions/spellItems.js"; |
| 51 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 52 | import { bagView } from "../items/bags.js"; |
| 53 | import { applyTindersAtTile, tindersTileStillNeedsCast } from "./tinders.js"; |
| 54 | |
| 55 | const MAGIC_ATTACK_PROFILE = Object.freeze({ |
| 56 | hitSkillId: SkillId.magic, |
| 57 | damageSkillId: SkillId.mind |
| 58 | }); |
| 59 | |
| 60 | export function ensureSpellComponents (ecs, handle, storedSpells = null) |
| 61 | { |
| 62 | if (!ecs.get(handle, Component.spells)) |
| 63 | { |
| 64 | ecs.add(handle, Component.spells, normalizePlayerSpells(storedSpells ?? defaultPlayerSpells())); |
| 65 | } |
| 66 | if (!ecs.get(handle, Component.spellcasting)) |
| 67 | { |
| 68 | ecs.add(handle, Component.spellcasting, emptySpellcasting()); |
| 69 | } |
| 70 | else |
| 71 | { normalizeSpellcasting(ecs.get(handle, Component.spellcasting)); } |
| 72 | } |
| 73 | |
| 74 | export function emptySpellcasting () |
| 75 | { |
| 76 | return { |
| 77 | armedSpellId: null, |
| 78 | repeatSpellId: null, |
| 79 | repeatTargetEntityId: null, |
| 80 | repeatTileX: null, |
| 81 | repeatTileY: null, |
| 82 | nextCastAt: 0, |
| 83 | pendingCast: null |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | function normalizeSpellcasting (casting) |
| 88 | { |
| 89 | if (!casting) |
| 90 | { return; } |
| 91 | if (!("repeatTileX" in casting)) |
| 92 | { casting.repeatTileX = null; } |
| 93 | if (!("repeatTileY" in casting)) |
| 94 | { casting.repeatTileY = null; } |
| 95 | } |
| 96 | |
| 97 | /** Clear auto-repeat / pending cast state (move, melee, cancel, …). */ |
| 98 | export function clearSpellAutoRepeat (casting) |
| 99 | { |
| 100 | if (!casting) |
| 101 | { return; } |
| 102 | casting.repeatSpellId = null; |
| 103 | casting.repeatTargetEntityId = null; |
| 104 | casting.repeatTileX = null; |
| 105 | casting.repeatTileY = null; |
| 106 | casting.pendingCast = null; |
| 107 | } |
| 108 | |
| 109 | function stopCasterMovement (world, handle, entityId) |
| 110 | { |
| 111 | clearEngage(world.ecs, entityId); |
| 112 | const path = world.ecs.get(handle, Component.path); |
| 113 | if (path?.tiles?.length) |
| 114 | { path.tiles = []; } |
| 115 | const movement = world.ecs.get(handle, Component.movement); |
| 116 | if (movement) |
| 117 | { movement.walkCarry = 0; } |
| 118 | } |
| 119 | |
| 120 | export function handleSpellCommand (world, sessionId, command, now = Date.now()) |
| 121 | { |
| 122 | switch (command.type) |
| 123 | { |
| 124 | case "set-spell-favorite": |
| 125 | return setSpellFavorite(world, sessionId, command, now); |
| 126 | case "set-spell-autocast": |
| 127 | return setSpellAutoCast(world, sessionId, command, now); |
| 128 | case "arm-spell": |
| 129 | return armSpell(world, sessionId, command, now); |
| 130 | case "cancel-arm-spell": |
| 131 | return cancelArmSpell(world, sessionId, command, now); |
| 132 | case "cast-spell-tile": |
| 133 | return castSpellAtTile(world, sessionId, command, now); |
| 134 | case "cast-spell-entity": |
| 135 | return castSpellAtEntity(world, sessionId, command, now); |
| 136 | case "read-spellbook": |
| 137 | return readSpellbook(world, sessionId, command, now); |
| 138 | default: |
| 139 | return null; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | function requirePlayer (world, sessionId, characterId) |
| 144 | { |
| 145 | for (const entity of world.ecs.query(Component.networkIdentity, Component.character)) |
| 146 | { |
| 147 | if (entity.networkIdentity.sessionId !== sessionId) |
| 148 | { continue; } |
| 149 | if (entity.character.characterId !== characterId) |
| 150 | { continue; } |
| 151 | if (world.ecs.has(entity.handle, Component.dead)) |
| 152 | { |
| 153 | const error = new Error("You are dead."); |
| 154 | error.userFacing = true; |
| 155 | throw error; |
| 156 | } |
| 157 | return { handle: entity.handle, entityId: entity.entityId, character: entity.character }; |
| 158 | } |
| 159 | const error = new Error("Character not found."); |
| 160 | error.userFacing = true; |
| 161 | throw error; |
| 162 | } |
| 163 | |
| 164 | function spellsUpdatedEvent (entityId, characterId, spells, skills, levelUps = []) |
| 165 | { |
| 166 | return { |
| 167 | type: "spells-updated", |
| 168 | entityId, |
| 169 | characterId, |
| 170 | spells: publicSpellsView(spells, skills), |
| 171 | stored: storedPlayerSpells(spells), |
| 172 | autoCast: !!spells.autoCast, |
| 173 | levelUps |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | function setSpellFavorite (world, sessionId, command, now) |
| 178 | { |
| 179 | const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId); |
| 180 | ensureSpellComponents(world.ecs, handle); |
| 181 | const skills = world.ecs.get(handle, Component.skills); |
| 182 | const spells = world.ecs.get(handle, Component.spells); |
| 183 | const result = toggleFavorite(spells, command.spellId, skills); |
| 184 | if (result.error) |
| 185 | { |
| 186 | const error = new Error(result.error); |
| 187 | error.userFacing = true; |
| 188 | throw error; |
| 189 | } |
| 190 | Object.assign(spells, result.spells); |
| 191 | const events = []; |
| 192 | if (result.changed) |
| 193 | { |
| 194 | events.push(spellsUpdatedEvent(entityId, character.characterId, spells, skills)); |
| 195 | } |
| 196 | return { events }; |
| 197 | } |
| 198 | |
| 199 | function setSpellAutoCast (world, sessionId, command, now) |
| 200 | { |
| 201 | const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId); |
| 202 | ensureSpellComponents(world.ecs, handle); |
| 203 | const skills = world.ecs.get(handle, Component.skills); |
| 204 | const spells = world.ecs.get(handle, Component.spells); |
| 205 | spells.autoCast = !!command.enabled; |
| 206 | const casting = world.ecs.get(handle, Component.spellcasting); |
| 207 | if (!spells.autoCast) |
| 208 | { clearSpellAutoRepeat(casting); } |
| 209 | return { |
| 210 | events: [spellsUpdatedEvent(entityId, character.characterId, spells, skills)] |
| 211 | }; |
| 212 | } |
| 213 | |
| 214 | function armSpell (world, sessionId, command, now) |
| 215 | { |
| 216 | const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId); |
| 217 | ensureSpellComponents(world.ecs, handle); |
| 218 | const skills = world.ecs.get(handle, Component.skills); |
| 219 | const spells = world.ecs.get(handle, Component.spells); |
| 220 | const check = canCastSpell(skills, spells, command.spellId); |
| 221 | if (!check.ok) |
| 222 | { |
| 223 | const error = new Error(check.reason); |
| 224 | error.userFacing = true; |
| 225 | throw error; |
| 226 | } |
| 227 | const casting = world.ecs.get(handle, Component.spellcasting); |
| 228 | casting.armedSpellId = command.spellId; |
| 229 | casting.pendingCast = null; |
| 230 | /** Re-arming the active auto-repeat spell must not cancel it (favourite bar clicks). */ |
| 231 | if (casting.repeatSpellId != null && casting.repeatSpellId !== command.spellId) |
| 232 | { |
| 233 | casting.repeatSpellId = null; |
| 234 | casting.repeatTargetEntityId = null; |
| 235 | casting.repeatTileX = null; |
| 236 | casting.repeatTileY = null; |
| 237 | } |
| 238 | return { |
| 239 | events: [{ |
| 240 | type: "spell-armed", |
| 241 | entityId, |
| 242 | characterId: character.characterId, |
| 243 | spellId: command.spellId |
| 244 | }] |
| 245 | }; |
| 246 | } |
| 247 | |
| 248 | function cancelArmSpell (world, sessionId, command) |
| 249 | { |
| 250 | const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId); |
| 251 | const casting = world.ecs.get(handle, Component.spellcasting); |
| 252 | if (!casting) |
| 253 | { return { events: [] }; } |
| 254 | casting.armedSpellId = null; |
| 255 | casting.pendingCast = null; |
| 256 | /** |
| 257 | * Do not clear auto-repeat here. Cast already disarms then sets repeat; a late |
| 258 | * cancel-arm (right-click while the client still thought it was armed) used to |
| 259 | * wipe that repeat and leave the player falling back to melee. |
| 260 | */ |
| 261 | return { |
| 262 | events: [{ |
| 263 | type: "spell-disarmed", |
| 264 | entityId, |
| 265 | characterId: character.characterId |
| 266 | }] |
| 267 | }; |
| 268 | } |
| 269 | |
| 270 | function casterMagicLevels (ecs, handle, settings = null) |
| 271 | { |
| 272 | const skills = ecs.get(handle, Component.skills); |
| 273 | const equipment = ecs.get(handle, Component.equipment); |
| 274 | const bonuses = equipmentBonusesFromSlots(equipment?.slots, settings); |
| 275 | const status = statusSkillBonuses(ecs.get(handle, Component.statusEffects), Date.now()); |
| 276 | bonuses[SkillId.mind] = (bonuses[SkillId.mind] ?? 0) + (status.mind ?? 0); |
| 277 | return { |
| 278 | magic: effectiveSkillLevel(skills, SkillId.magic, bonuses), |
| 279 | mind: effectiveSkillLevel(skills, SkillId.mind, bonuses) |
| 280 | }; |
| 281 | } |
| 282 | |
| 283 | function validateCastRange (world, handle, tileX, tileY, now) |
| 284 | { |
| 285 | const pos = world.ecs.get(handle, Component.position); |
| 286 | if (!pos) |
| 287 | { return { ok: false, reason: "You are not in the world." }; } |
| 288 | const dist = chebyshev(pos.tileX, pos.tileY, tileX, tileY); |
| 289 | if (dist > COMBAT_RANGE_TILES) |
| 290 | { return { ok: false, reason: "That is out of range." }; } |
| 291 | if (!hasCombatLineOfSight(world, pos.tileX, pos.tileY, tileX, tileY)) |
| 292 | { return { ok: false, reason: "You cannot see that spot." }; } |
| 293 | return { ok: true }; |
| 294 | } |
| 295 | |
| 296 | function resolvePendingCastTarget (world, pending) |
| 297 | { |
| 298 | if (pending.targetEntityId == null) |
| 299 | { |
| 300 | return { |
| 301 | tileX: pending.tileX | 0, |
| 302 | tileY: pending.tileY | 0, |
| 303 | targetEntityId: null |
| 304 | }; |
| 305 | } |
| 306 | const target = resolveCombatTarget(world, pending.targetEntityId); |
| 307 | if (!target) |
| 308 | { return null; } |
| 309 | return { |
| 310 | tileX: target.pos.tileX, |
| 311 | tileY: target.pos.tileY, |
| 312 | targetEntityId: pending.targetEntityId |
| 313 | }; |
| 314 | } |
| 315 | |
| 316 | function queueSpellApproach (world, handle, entityId, target, minStandDist) |
| 317 | { |
| 318 | const position = world.ecs.get(handle, Component.position); |
| 319 | if (!position) |
| 320 | { return; } |
| 321 | queueStandInRangeApproach( |
| 322 | world, |
| 323 | entityId, |
| 324 | handle, |
| 325 | position, |
| 326 | target.tileX, |
| 327 | target.tileY, |
| 328 | COMBAT_RANGE_TILES, |
| 329 | minStandDist |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | function beginCastOrApproach (world, sessionId, characterId, spellId, target, now, minStandDist = 0) |
| 334 | { |
| 335 | const { handle, entityId } = requirePlayer(world, sessionId, characterId); |
| 336 | ensureSpellComponents(world.ecs, handle); |
| 337 | const casting = world.ecs.get(handle, Component.spellcasting); |
| 338 | stopCasterMovement(world, handle, entityId); |
| 339 | const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now); |
| 340 | if (!rangeCheck.ok) |
| 341 | { |
| 342 | casting.pendingCast = { |
| 343 | spellId, |
| 344 | tileX: target.tileX, |
| 345 | tileY: target.tileY, |
| 346 | targetEntityId: target.targetEntityId ?? null |
| 347 | }; |
| 348 | queueSpellApproach(world, handle, entityId, target, minStandDist); |
| 349 | return { events: [] }; |
| 350 | } |
| 351 | casting.pendingCast = null; |
| 352 | return executeCast(world, sessionId, characterId, spellId, target, now); |
| 353 | } |
| 354 | |
| 355 | function castSpellAtTile (world, sessionId, command, now) |
| 356 | { |
| 357 | const spellId = command.spellId; |
| 358 | const def = spellDefinition(spellId); |
| 359 | if (!def || def.target !== "tile") |
| 360 | { |
| 361 | const error = new Error("That spell needs a different target."); |
| 362 | error.userFacing = true; |
| 363 | throw error; |
| 364 | } |
| 365 | return beginCastOrApproach(world, sessionId, command.characterId, spellId, { |
| 366 | tileX: command.tileX | 0, |
| 367 | tileY: command.tileY | 0, |
| 368 | targetEntityId: null |
| 369 | }, now, 0); |
| 370 | } |
| 371 | |
| 372 | function castSpellAtEntity (world, sessionId, command, now) |
| 373 | { |
| 374 | const spellId = command.spellId; |
| 375 | const def = spellDefinition(spellId); |
| 376 | if (!def || def.target !== "entity") |
| 377 | { |
| 378 | const error = new Error("That spell needs a tile target."); |
| 379 | error.userFacing = true; |
| 380 | throw error; |
| 381 | } |
| 382 | const { entityId: casterEntityId } = requirePlayer(world, sessionId, command.characterId); |
| 383 | const target = resolveCombatTarget(world, command.targetEntityId); |
| 384 | if (!target) |
| 385 | { |
| 386 | const error = new Error("Target is not here."); |
| 387 | error.userFacing = true; |
| 388 | throw error; |
| 389 | } |
| 390 | const selfTarget = command.targetEntityId === casterEntityId; |
| 391 | return beginCastOrApproach(world, sessionId, command.characterId, spellId, { |
| 392 | tileX: target.pos.tileX, |
| 393 | tileY: target.pos.tileY, |
| 394 | targetEntityId: command.targetEntityId |
| 395 | }, now, selfTarget ? 0 : 1); |
| 396 | } |
| 397 | |
| 398 | function executeCast (world, sessionId, characterId, spellId, target, now) |
| 399 | { |
| 400 | const { handle, entityId, character } = requirePlayer(world, sessionId, characterId); |
| 401 | ensureSpellComponents(world.ecs, handle); |
| 402 | const skills = world.ecs.get(handle, Component.skills); |
| 403 | const spells = world.ecs.get(handle, Component.spells); |
| 404 | const casting = world.ecs.get(handle, Component.spellcasting); |
| 405 | const def = spellDefinition(spellId); |
| 406 | const check = canCastSpell(skills, spells, spellId); |
| 407 | if (!check.ok) |
| 408 | { |
| 409 | const error = new Error(check.reason); |
| 410 | error.userFacing = true; |
| 411 | throw error; |
| 412 | } |
| 413 | if (now < (casting.nextCastAt || 0)) |
| 414 | { |
| 415 | const error = new Error("You are not ready to cast yet."); |
| 416 | error.userFacing = true; |
| 417 | throw error; |
| 418 | } |
| 419 | const rangeCheck = validateCastRange(world, handle, target.tileX, target.tileY, now); |
| 420 | if (!rangeCheck.ok) |
| 421 | { |
| 422 | const error = new Error(rangeCheck.reason); |
| 423 | error.userFacing = true; |
| 424 | throw error; |
| 425 | } |
| 426 | |
| 427 | stopCasterMovement(world, handle, entityId); |
| 428 | |
| 429 | const levels = casterMagicLevels(world.ecs, handle, world.settings); |
| 430 | const difficulty = spellDifficulty(def); |
| 431 | const cast = resolveSpellCast({ |
| 432 | magicSkillLevel: levels.magic, |
| 433 | spellDifficulty: difficulty |
| 434 | }); |
| 435 | |
| 436 | const events = []; |
| 437 | const from = world.ecs.get(handle, Component.position); |
| 438 | casting.armedSpellId = null; |
| 439 | casting.nextCastAt = now + SPELL_COOLDOWN_MS; |
| 440 | events.push({ |
| 441 | type: "spell-disarmed", |
| 442 | entityId, |
| 443 | characterId: character.characterId |
| 444 | }); |
| 445 | |
| 446 | // Keep / refresh auto-repeat even on fizzle so we keep trying until cancelled. |
| 447 | rememberAutoRepeat(casting, spells, def, spellId, target); |
| 448 | |
| 449 | if (!cast.success) |
| 450 | { |
| 451 | events.push({ |
| 452 | type: "spell-fizzle", |
| 453 | casterId: entityId, |
| 454 | spellId, |
| 455 | fromTileX: from.tileX, |
| 456 | fromTileY: from.tileY, |
| 457 | tileX: target.tileX, |
| 458 | tileY: target.tileY |
| 459 | }); |
| 460 | return { events }; |
| 461 | } |
| 462 | |
| 463 | const xpAward = awardSpellCastXp(world.ecs, handle, difficulty); |
| 464 | if (xpAward) |
| 465 | { |
| 466 | events.push({ |
| 467 | type: "skills-updated", |
| 468 | entityId, |
| 469 | characterId: character.characterId, |
| 470 | skills: xpAward.skills, |
| 471 | combatLevel: xpAward.combatLevel, |
| 472 | stored: xpAward.stored, |
| 473 | levelUps: xpAward.levelUps ?? [] |
| 474 | }); |
| 475 | } |
| 476 | |
| 477 | const flashRadius = castFlashRadiusTiles(def.tier); |
| 478 | events.push({ |
| 479 | type: "spell-cast-flash", |
| 480 | casterId: entityId, |
| 481 | tileX: from.tileX, |
| 482 | tileY: from.tileY, |
| 483 | radiusTiles: flashRadius, |
| 484 | durationMs: CAST_FLASH_MS, |
| 485 | tier: def.tier |
| 486 | }); |
| 487 | |
| 488 | world.pendingSpellImpacts.push({ |
| 489 | executeAt: now + SPELL_PROJECTILE_MS, |
| 490 | casterEntityId: entityId, |
| 491 | casterCharacterId: character.characterId, |
| 492 | spellId, |
| 493 | targetTileX: target.tileX, |
| 494 | targetTileY: target.tileY, |
| 495 | targetEntityId: target.targetEntityId, |
| 496 | levels |
| 497 | }); |
| 498 | |
| 499 | events.push({ |
| 500 | type: "spell-projectile", |
| 501 | casterId: entityId, |
| 502 | spellId, |
| 503 | targetId: target.targetEntityId ?? null, |
| 504 | fromTileX: from.tileX, |
| 505 | fromTileY: from.tileY, |
| 506 | tileX: target.tileX, |
| 507 | tileY: target.tileY, |
| 508 | durationMs: SPELL_PROJECTILE_MS, |
| 509 | sprite: def.projectileSprite |
| 510 | }); |
| 511 | |
| 512 | return { events }; |
| 513 | } |
| 514 | |
| 515 | function rememberAutoRepeat (casting, spells, def, spellId, target) |
| 516 | { |
| 517 | if (!casting || !def?.autoRepeat) |
| 518 | { |
| 519 | clearSpellAutoRepeat(casting); |
| 520 | return; |
| 521 | } |
| 522 | const continuingEntityRepeat = !!( |
| 523 | casting.repeatSpellId === spellId |
| 524 | && casting.repeatTargetEntityId != null |
| 525 | && target?.targetEntityId != null |
| 526 | && def.target === "entity" |
| 527 | ); |
| 528 | if (!spells?.autoCast && !continuingEntityRepeat) |
| 529 | { |
| 530 | clearSpellAutoRepeat(casting); |
| 531 | return; |
| 532 | } |
| 533 | if (def.target === "entity" && target?.targetEntityId != null) |
| 534 | { |
| 535 | casting.repeatSpellId = spellId; |
| 536 | casting.repeatTargetEntityId = target.targetEntityId | 0; |
| 537 | casting.repeatTileX = null; |
| 538 | casting.repeatTileY = null; |
| 539 | casting.pendingCast = null; |
| 540 | return; |
| 541 | } |
| 542 | if (def.target === "tile") |
| 543 | { |
| 544 | if (!spells?.autoCast) |
| 545 | { |
| 546 | clearSpellAutoRepeat(casting); |
| 547 | return; |
| 548 | } |
| 549 | casting.repeatSpellId = spellId; |
| 550 | casting.repeatTargetEntityId = null; |
| 551 | casting.repeatTileX = target.tileX | 0; |
| 552 | casting.repeatTileY = target.tileY | 0; |
| 553 | casting.pendingCast = null; |
| 554 | return; |
| 555 | } |
| 556 | clearSpellAutoRepeat(casting); |
| 557 | } |
| 558 | |
| 559 | function targetAffinitiesForEntity (world, entityId) |
| 560 | { |
| 561 | if (entityId == null) |
| 562 | { return {}; } |
| 563 | const handle = world.ecs.handleOf(entityId); |
| 564 | if (handle == null) |
| 565 | { return {}; } |
| 566 | const creature = world.ecs.get(handle, Component.creature); |
| 567 | if (!creature) |
| 568 | { return {}; } |
| 569 | return mobDamageAffinities(mobDefinition(creature.mobTypeId)); |
| 570 | } |
| 571 | |
| 572 | function applySpellImpact (world, impact, now) |
| 573 | { |
| 574 | const def = spellDefinition(impact.spellId); |
| 575 | if (!def) |
| 576 | { return []; } |
| 577 | const events = []; |
| 578 | events.push({ |
| 579 | type: "spell-cast-flash", |
| 580 | casterId: impact.casterEntityId, |
| 581 | tileX: impact.targetTileX, |
| 582 | tileY: impact.targetTileY, |
| 583 | radiusTiles: castFlashRadiusTiles(def.tier), |
| 584 | durationMs: CAST_FLASH_MS, |
| 585 | tier: def.tier |
| 586 | }); |
| 587 | |
| 588 | const effect = def.effect ?? impact.spellId; |
| 589 | if (effect === "light" || effect === "dark") |
| 590 | { |
| 591 | events.push(...applyLightOrDarkImpact(world, impact, def, effect, now)); |
| 592 | return events; |
| 593 | } |
| 594 | if (effect === "damage") |
| 595 | { |
| 596 | events.push(...applyDamageSpellImpact(world, impact, def, now)); |
| 597 | return events; |
| 598 | } |
| 599 | if (effect === "poison") |
| 600 | { |
| 601 | events.push(...applyPoisonImpact(world, impact, def, now)); |
| 602 | return events; |
| 603 | } |
| 604 | if (effect === "drain") |
| 605 | { |
| 606 | events.push(...applyDrainImpact(world, impact, now)); |
| 607 | return events; |
| 608 | } |
| 609 | if (effect === "revitalize") |
| 610 | { |
| 611 | events.push(...applyRevitalizeImpact(world, impact, now)); |
| 612 | return events; |
| 613 | } |
| 614 | if (effect === "tinders") |
| 615 | { |
| 616 | events.push(...applyTindersImpact(world, impact, now)); |
| 617 | return events; |
| 618 | } |
| 619 | if (effect === "shade") |
| 620 | { |
| 621 | events.push(...applyShadeImpact(world, impact, now)); |
| 622 | return events; |
| 623 | } |
| 624 | return events; |
| 625 | } |
| 626 | |
| 627 | function mindRollOf (impact) |
| 628 | { |
| 629 | return resolveSpellPotency({ mindSkillLevel: impact.levels.mind }).mindRoll; |
| 630 | } |
| 631 | |
| 632 | function mindDamageRollOf (impact, spellTier = 1) |
| 633 | { |
| 634 | return rollSpellDamage(impact.levels.mind, spellTier); |
| 635 | } |
| 636 | |
| 637 | function applyLightOrDarkImpact (world, impact, def, effect, now) |
| 638 | { |
| 639 | const events = []; |
| 640 | world.spellLights = world.spellLights ?? new SpellLightRegistry(); |
| 641 | const mindRoll = mindRollOf(impact); |
| 642 | const durationMs = Math.max(1, mindRoll) * LIGHT_DURATION_MS_PER_MIND; |
| 643 | const baseRange = mindRoll === 0 |
| 644 | ? SPELL_LIGHT_RANGE_TILES * 0.5 |
| 645 | : SPELL_LIGHT_RANGE_TILES; |
| 646 | const kind = effect === "dark" ? "dark" : "light"; |
| 647 | const { light, extended } = world.spellLights.placeOrExtend({ |
| 648 | tileX: impact.targetTileX, |
| 649 | tileY: impact.targetTileY, |
| 650 | ownerEntityId: impact.casterEntityId, |
| 651 | durationMs, |
| 652 | baseRange, |
| 653 | kind, |
| 654 | now |
| 655 | }); |
| 656 | const appear = kind === "dark" |
| 657 | ? (extended ? "spell-dark-update" : "spell-dark-appear") |
| 658 | : (extended ? "spell-light-update" : "spell-light-appear"); |
| 659 | events.push({ type: appear, ...publicSpellLight(light, now) }); |
| 660 | return events; |
| 661 | } |
| 662 | |
| 663 | function elementalDamageMultiplier (def, affinities) |
| 664 | { |
| 665 | let multiplier = ElementMultiplier.neutral; |
| 666 | const affinity = affinities[def.element ?? "none"]; |
| 667 | if (affinity === "weak") |
| 668 | { multiplier = ElementMultiplier.weak; } |
| 669 | else if (affinity === "resistant") |
| 670 | { multiplier = ElementMultiplier.resistant; } |
| 671 | return multiplier; |
| 672 | } |
| 673 | |
| 674 | function applyDamageSpellImpact (world, impact, def, now) |
| 675 | { |
| 676 | if (impact.targetEntityId == null) |
| 677 | { return []; } |
| 678 | const events = []; |
| 679 | const affinities = targetAffinitiesForEntity(world, impact.targetEntityId); |
| 680 | const mindRoll = mindDamageRollOf(impact, def.tier); |
| 681 | const multiplier = elementalDamageMultiplier(def, affinities); |
| 682 | const damage = Math.max(1, Math.floor(mindRoll * multiplier)); |
| 683 | events.push(...applyDamageAndKill( |
| 684 | world, |
| 685 | impact.casterEntityId, |
| 686 | impact.targetEntityId, |
| 687 | damage, |
| 688 | now |
| 689 | )); |
| 690 | events.push(...awardMagicHitXp(world, impact, damage)); |
| 691 | return events; |
| 692 | } |
| 693 | |
| 694 | function awardMagicHitXp (world, impact, damage) |
| 695 | { |
| 696 | const events = []; |
| 697 | const casterHandle = world.ecs.handleOf(impact.casterEntityId); |
| 698 | if (casterHandle == null || !impact.casterCharacterId) |
| 699 | { return events; } |
| 700 | const xp = awardCombatHitXp(world.ecs, casterHandle, MAGIC_ATTACK_PROFILE, damage); |
| 701 | if (xp) |
| 702 | { |
| 703 | events.push({ |
| 704 | type: "skills-updated", |
| 705 | entityId: impact.casterEntityId, |
| 706 | characterId: impact.casterCharacterId, |
| 707 | skills: xp.skills, |
| 708 | combatLevel: xp.combatLevel, |
| 709 | stored: xp.stored, |
| 710 | levelUps: xp.levelUps ?? [] |
| 711 | }); |
| 712 | } |
| 713 | return events; |
| 714 | } |
| 715 | |
| 716 | function ensureEntityStatusEffects (ecs, handle) |
| 717 | { |
| 718 | let component = ecs.get(handle, Component.statusEffects); |
| 719 | if (!component) |
| 720 | { component = ecs.add(handle, Component.statusEffects, emptyStatusEffects()); } |
| 721 | return component; |
| 722 | } |
| 723 | |
| 724 | function applyPoisonImpact (world, impact, def, now) |
| 725 | { |
| 726 | if (impact.targetEntityId == null) |
| 727 | { return []; } |
| 728 | const target = resolveCombatTarget(world, impact.targetEntityId); |
| 729 | if (!target?.local || target.handle == null) |
| 730 | { return []; } |
| 731 | const affinities = targetAffinitiesForEntity(world, impact.targetEntityId); |
| 732 | const mindRoll = mindRollOf(impact); |
| 733 | const multiplier = elementalDamageMultiplier(def, affinities); |
| 734 | const total = Math.max(1, Math.floor(mindRoll * multiplier * 2)); |
| 735 | const durationMs = total * 1000; |
| 736 | const status = ensureEntityStatusEffects(world.ecs, target.handle); |
| 737 | applyStatusEffect(status, { |
| 738 | type: StatusEffectType.healthDrain, |
| 739 | durationMs, |
| 740 | amountPerSecond: 1 |
| 741 | }, now); |
| 742 | return [{ |
| 743 | type: "status-effects-entity", |
| 744 | entityId: impact.targetEntityId, |
| 745 | effects: publicStatusEffectsView(status, now) |
| 746 | }]; |
| 747 | } |
| 748 | |
| 749 | function applyDrainImpact (world, impact, now) |
| 750 | { |
| 751 | if (impact.targetEntityId == null) |
| 752 | { return []; } |
| 753 | const target = resolveCombatTarget(world, impact.targetEntityId); |
| 754 | if (!target?.local || target.handle == null) |
| 755 | { return []; } |
| 756 | if (!world.ecs.get(target.handle, Component.stamina)) |
| 757 | { return []; } |
| 758 | const mindRoll = mindRollOf(impact); |
| 759 | const durationMs = Math.max(1000, mindRoll * 3 * 1000); |
| 760 | const status = ensureEntityStatusEffects(world.ecs, target.handle); |
| 761 | applyStatusEffect(status, { |
| 762 | type: StatusEffectType.staminaDrain, |
| 763 | durationMs, |
| 764 | amountPerSecond: 1 |
| 765 | }, now); |
| 766 | applyStatusEffect(status, { |
| 767 | type: StatusEffectType.staminaBlock, |
| 768 | durationMs, |
| 769 | amountPerSecond: 0 |
| 770 | }, now); |
| 771 | const character = world.ecs.get(target.handle, Component.character); |
| 772 | const events = [{ |
| 773 | type: "status-effects-entity", |
| 774 | entityId: impact.targetEntityId, |
| 775 | effects: publicStatusEffectsView(status, now) |
| 776 | }]; |
| 777 | if (character?.characterId) |
| 778 | { |
| 779 | events.push({ |
| 780 | type: "status-effects", |
| 781 | characterId: character.characterId, |
| 782 | effects: publicStatusEffectsView(status, now), |
| 783 | blind: hasStatusEffect(status, StatusEffectType.blind, now) |
| 784 | }); |
| 785 | } |
| 786 | return events; |
| 787 | } |
| 788 | |
| 789 | function applyRevitalizeImpact (world, impact, now) |
| 790 | { |
| 791 | if (impact.targetEntityId == null) |
| 792 | { return []; } |
| 793 | const target = resolveCombatTarget(world, impact.targetEntityId); |
| 794 | if (!target?.local || target.handle == null) |
| 795 | { return []; } |
| 796 | if (!world.ecs.get(target.handle, Component.health)) |
| 797 | { return []; } |
| 798 | const mindRoll = mindRollOf(impact); |
| 799 | const healTotal = Math.max(1, mindRoll); |
| 800 | const durationMs = healTotal * 1000; |
| 801 | const status = ensureEntityStatusEffects(world.ecs, target.handle); |
| 802 | applyStatusEffect(status, { |
| 803 | type: StatusEffectType.healthRegen, |
| 804 | durationMs, |
| 805 | amountPerSecond: 1 |
| 806 | }, now); |
| 807 | const character = world.ecs.get(target.handle, Component.character); |
| 808 | const events = [{ |
| 809 | type: "status-effects-entity", |
| 810 | entityId: impact.targetEntityId, |
| 811 | effects: publicStatusEffectsView(status, now) |
| 812 | }]; |
| 813 | if (character?.characterId) |
| 814 | { |
| 815 | events.push({ |
| 816 | type: "status-effects", |
| 817 | characterId: character.characterId, |
| 818 | effects: publicStatusEffectsView(status, now), |
| 819 | blind: hasStatusEffect(status, StatusEffectType.blind, now) |
| 820 | }); |
| 821 | } |
| 822 | return events; |
| 823 | } |
| 824 | |
| 825 | function applyTindersImpact (world, impact, now) |
| 826 | { |
| 827 | const events = applyTindersAtTile(world, impact, now); |
| 828 | const casterHandle = world.ecs.handleOf(impact.casterEntityId); |
| 829 | const sessionId = casterHandle != null |
| 830 | ? world.ecs.get(casterHandle, Component.networkIdentity)?.sessionId |
| 831 | : null; |
| 832 | for (const event of events) |
| 833 | { |
| 834 | if (event.type === "command-error" && sessionId != null) |
| 835 | { event.sessionId = sessionId; } |
| 836 | } |
| 837 | return events; |
| 838 | } |
| 839 | |
| 840 | function applyShadeImpact (world, impact, now) |
| 841 | { |
| 842 | const mindRoll = mindRollOf(impact); |
| 843 | const durationMs = Math.max(1000, mindRoll * 3 * 1000); |
| 844 | const casterHandle = world.ecs.handleOf(impact.casterEntityId); |
| 845 | const mindLevel = impact.levels?.mind | 0 || 1; |
| 846 | const level = Math.max(1, Math.min(99, Math.floor(mindLevel * 0.75))); |
| 847 | return [{ |
| 848 | type: "summon-entity", |
| 849 | ownerEntityId: impact.casterEntityId, |
| 850 | ownerCharacterId: impact.casterCharacterId ?? null, |
| 851 | tileX: impact.targetTileX, |
| 852 | tileY: impact.targetTileY, |
| 853 | level, |
| 854 | expiresAt: now + durationMs, |
| 855 | mobTypeId: "shade" |
| 856 | }]; |
| 857 | } |
| 858 | |
| 859 | export function processSpellImpacts (world, now = Date.now()) |
| 860 | { |
| 861 | const queue = world.pendingSpellImpacts ?? []; |
| 862 | if (!queue.length) |
| 863 | { return []; } |
| 864 | const events = []; |
| 865 | const keep = []; |
| 866 | for (const impact of queue) |
| 867 | { |
| 868 | if (impact.executeAt > now) |
| 869 | { |
| 870 | keep.push(impact); |
| 871 | continue; |
| 872 | } |
| 873 | events.push(...applySpellImpact(world, impact, now)); |
| 874 | } |
| 875 | world.pendingSpellImpacts = keep; |
| 876 | return events; |
| 877 | } |
| 878 | |
| 879 | export function stepPendingSpellCast (world, now = Date.now()) |
| 880 | { |
| 881 | const events = []; |
| 882 | for (const entity of world.ecs.query(Component.spellcasting, Component.character, Component.networkIdentity)) |
| 883 | { |
| 884 | if (world.ecs.has(entity.handle, Component.dead)) |
| 885 | { |
| 886 | if (entity.spellcasting) |
| 887 | { entity.spellcasting.pendingCast = null; } |
| 888 | continue; |
| 889 | } |
| 890 | const casting = entity.spellcasting; |
| 891 | if (!casting?.pendingCast) |
| 892 | { continue; } |
| 893 | const pending = casting.pendingCast; |
| 894 | const target = resolvePendingCastTarget(world, pending); |
| 895 | if (!target) |
| 896 | { |
| 897 | casting.pendingCast = null; |
| 898 | continue; |
| 899 | } |
| 900 | const rangeCheck = validateCastRange(world, entity.handle, target.tileX, target.tileY, now); |
| 901 | if (!rangeCheck.ok) |
| 902 | { |
| 903 | const minStandDist = target.targetEntityId != null ? 1 : 0; |
| 904 | queueSpellApproach(world, entity.handle, entity.entityId, target, minStandDist); |
| 905 | continue; |
| 906 | } |
| 907 | casting.pendingCast = null; |
| 908 | const result = executeCast( |
| 909 | world, |
| 910 | entity.networkIdentity.sessionId, |
| 911 | entity.character.characterId, |
| 912 | pending.spellId, |
| 913 | target, |
| 914 | now |
| 915 | ); |
| 916 | events.push(...(result.events ?? [])); |
| 917 | } |
| 918 | return events; |
| 919 | } |
| 920 | |
| 921 | export function stepSpellAutoCast (world, now = Date.now()) |
| 922 | { |
| 923 | const events = []; |
| 924 | for (const entity of world.ecs.query( |
| 925 | Component.spellcasting, |
| 926 | Component.spells, |
| 927 | Component.character, |
| 928 | Component.networkIdentity |
| 929 | )) |
| 930 | { |
| 931 | if (world.ecs.has(entity.handle, Component.dead)) |
| 932 | { |
| 933 | clearSpellAutoRepeat(entity.spellcasting); |
| 934 | continue; |
| 935 | } |
| 936 | const casting = entity.spellcasting; |
| 937 | const spells = entity.spells; |
| 938 | normalizeSpellcasting(casting); |
| 939 | if (!casting.repeatSpellId) |
| 940 | { continue; } |
| 941 | if (now < (casting.nextCastAt || 0)) |
| 942 | { continue; } |
| 943 | const def = spellDefinition(casting.repeatSpellId); |
| 944 | if (!def?.autoRepeat) |
| 945 | { |
| 946 | clearSpellAutoRepeat(casting); |
| 947 | continue; |
| 948 | } |
| 949 | /** UI auto-cast toggle gates starting repeats; combat retaliate may set repeat without it. */ |
| 950 | if (!spells.autoCast && casting.repeatTargetEntityId == null) |
| 951 | { |
| 952 | clearSpellAutoRepeat(casting); |
| 953 | continue; |
| 954 | } |
| 955 | |
| 956 | const sessionId = entity.networkIdentity.sessionId; |
| 957 | if (!sessionId) |
| 958 | { continue; } |
| 959 | |
| 960 | let target = null; |
| 961 | let minStandDist = 0; |
| 962 | if (casting.repeatTargetEntityId != null) |
| 963 | { |
| 964 | const resolved = resolveCombatTarget(world, casting.repeatTargetEntityId); |
| 965 | if (!resolved || (resolved.health && resolved.health.current <= 0)) |
| 966 | { |
| 967 | clearSpellAutoRepeat(casting); |
| 968 | continue; |
| 969 | } |
| 970 | target = { |
| 971 | tileX: resolved.pos.tileX, |
| 972 | tileY: resolved.pos.tileY, |
| 973 | targetEntityId: casting.repeatTargetEntityId |
| 974 | }; |
| 975 | minStandDist = casting.repeatTargetEntityId === entity.entityId ? 0 : 1; |
| 976 | } |
| 977 | else if (casting.repeatTileX != null && casting.repeatTileY != null) |
| 978 | { |
| 979 | if (!tileAutoCastStillValid(world, casting.repeatSpellId, casting.repeatTileX, casting.repeatTileY)) |
| 980 | { |
| 981 | clearSpellAutoRepeat(casting); |
| 982 | continue; |
| 983 | } |
| 984 | target = { |
| 985 | tileX: casting.repeatTileX | 0, |
| 986 | tileY: casting.repeatTileY | 0, |
| 987 | targetEntityId: null |
| 988 | }; |
| 989 | minStandDist = 0; |
| 990 | } |
| 991 | else |
| 992 | { |
| 993 | clearSpellAutoRepeat(casting); |
| 994 | continue; |
| 995 | } |
| 996 | |
| 997 | const range = validateCastRange(world, entity.handle, target.tileX, target.tileY, now); |
| 998 | if (!range.ok) |
| 999 | { |
| 1000 | queueSpellApproach(world, entity.handle, entity.entityId, target, minStandDist); |
| 1001 | continue; |
| 1002 | } |
| 1003 | |
| 1004 | const result = executeCast( |
| 1005 | world, |
| 1006 | sessionId, |
| 1007 | entity.character.characterId, |
| 1008 | casting.repeatSpellId, |
| 1009 | target, |
| 1010 | now |
| 1011 | ); |
| 1012 | events.push(...(result.events ?? [])); |
| 1013 | } |
| 1014 | return events; |
| 1015 | } |
| 1016 | |
| 1017 | function tileAutoCastStillValid (world, spellId, tileX, tileY) |
| 1018 | { |
| 1019 | const def = spellDefinition(spellId); |
| 1020 | if (!def) |
| 1021 | { return false; } |
| 1022 | if (def.effect === "tinders") |
| 1023 | { return tindersTileStillNeedsCast(world, tileX, tileY); } |
| 1024 | return true; |
| 1025 | } |
| 1026 | |
| 1027 | export function tickSpellLights (world, now = Date.now()) |
| 1028 | { |
| 1029 | if (!world.spellLights) |
| 1030 | { return []; } |
| 1031 | return world.spellLights.tick(world, now); |
| 1032 | } |
| 1033 | |
| 1034 | function readSpellbook (world, sessionId, command, now) |
| 1035 | { |
| 1036 | const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId); |
| 1037 | ensureSpellComponents(world.ecs, handle); |
| 1038 | const inventory = world.ecs.get(handle, Component.inventory); |
| 1039 | const slotIndex = command.slotIndex | 0; |
| 1040 | const slot = inventory?.slots?.[slotIndex]; |
| 1041 | const itemDef = itemDefinition(slot?.itemId); |
| 1042 | const spellId = spellIdFromSpellbookStack(slot, itemDef); |
| 1043 | if (!spellId) |
| 1044 | { |
| 1045 | const error = new Error("That is not a spellbook."); |
| 1046 | error.userFacing = true; |
| 1047 | throw error; |
| 1048 | } |
| 1049 | const spells = world.ecs.get(handle, Component.spells); |
| 1050 | const skills = world.ecs.get(handle, Component.skills); |
| 1051 | const learned = learnSpell(spells, spellId); |
| 1052 | if (!learned.changed) |
| 1053 | { |
| 1054 | const error = new Error("You already know that spell."); |
| 1055 | error.userFacing = true; |
| 1056 | throw error; |
| 1057 | } |
| 1058 | Object.assign(spells, learned.spells); |
| 1059 | inventory.slots[slotIndex] = null; |
| 1060 | const events = [ |
| 1061 | spellsUpdatedEvent(entityId, character.characterId, spells, skills) |
| 1062 | ]; |
| 1063 | return { |
| 1064 | events, |
| 1065 | private: bagView(world.ecs, handle) |
| 1066 | }; |
| 1067 | } |
| 1068 | |
| 1069 | export function createSpellbookStack (spellId) |
| 1070 | { |
| 1071 | const instance = spellbookInstance(spellId); |
| 1072 | const itemId = spellbookItemIdForSpell(spellId); |
| 1073 | if (!instance || itemId == null) |
| 1074 | { return null; } |
| 1075 | return { itemId, quantity: 1, instance }; |
| 1076 | } |