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