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