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