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