| 1 | import { GAIT, VISIBLE_CHUNK_RADIUS, DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js"; |
| 2 | import { |
| 3 | chunkKey, |
| 4 | chunkToRegion, |
| 5 | parseChunkKey, |
| 6 | regionKey, |
| 7 | tileKey, |
| 8 | tileToChunk, |
| 9 | tileToLocal, |
| 10 | tileToRegion |
| 11 | } from "../world/coordinates.js"; |
| 12 | import { cloneChunkData, createEmptyChunkData, isChunkTilePassable } from "../world/chunkData.js"; |
| 13 | import { closedNeighborEdges } from "../world/chunkFlags.js"; |
| 14 | import { EcsWorld } from "../ecs/world.js"; |
| 15 | import { Component } from "../ecs/components.js"; |
| 16 | import { deserializeEntity, publicEntityView, serializeEntity } from "../ecs/serialize.js"; |
| 17 | import { chunkBoundsAround, findPath } from "../navigation/astar.js"; |
| 18 | import { WorkerMessage } from "./protocol.js"; |
| 19 | import { collectSpawnRequests } from "./spawners.js"; |
| 20 | import { mobDefinition } from "../../../shared/definitions/mobs.js"; |
| 21 | import { |
| 22 | bagView, |
| 23 | applyBagView, |
| 24 | expireGroundItems, |
| 25 | groundItemsInChunk, |
| 26 | groundItemsInRegion, |
| 27 | handleItemCommand, |
| 28 | removeGroundItem, |
| 29 | setGroundItemsForRegion |
| 30 | } from "../items/bags.js"; |
| 31 | import { |
| 32 | clearEngage, |
| 33 | engageTarget, |
| 34 | ensureMobCombat, |
| 35 | isUntargetable, |
| 36 | stepCombat |
| 37 | } from "../combat/service.js"; |
| 38 | import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js"; |
| 39 | import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js"; |
| 40 | |
| 41 | export class WorkerWorld |
| 42 | { |
| 43 | constructor (workerId) |
| 44 | { |
| 45 | this.workerId = workerId; |
| 46 | this.ecs = new EcsWorld(); |
| 47 | this.chunks = new Map(); |
| 48 | this.ownedRegions = new Set(); |
| 49 | this.occupancy = new Map(); |
| 50 | this.ghosts = new Map(); |
| 51 | this.ghostOccupancy = new Map(); |
| 52 | this.characterEntities = new Map(); |
| 53 | this.frozen = new Set(); |
| 54 | this.groundItems = new Map(); |
| 55 | this.groundByTile = new Map(); |
| 56 | this.settings = { ...DEFAULT_WORLD_SETTINGS }; |
| 57 | this.activeRevisionId = 1; |
| 58 | } |
| 59 | |
| 60 | handle (message) |
| 61 | { |
| 62 | switch (message.type) |
| 63 | { |
| 64 | case WorkerMessage.configure: |
| 65 | return this.#configure(message); |
| 66 | case WorkerMessage.assignRegion: |
| 67 | return this.#assignRegion(message); |
| 68 | case WorkerMessage.dropRegion: |
| 69 | return this.#dropRegion(message); |
| 70 | case WorkerMessage.replaceRegionChunks: |
| 71 | return this.#replaceRegionChunks(message); |
| 72 | case WorkerMessage.setReplicas: |
| 73 | return this.#setReplicas(message); |
| 74 | case WorkerMessage.setSimulatingChunks: |
| 75 | return this.#setSimulatingChunks(message); |
| 76 | case WorkerMessage.setGhosts: |
| 77 | return this.#setGhosts(message); |
| 78 | case WorkerMessage.spawn: |
| 79 | return this.#spawn(message); |
| 80 | case WorkerMessage.despawn: |
| 81 | return this.#despawn(message.entityId); |
| 82 | case WorkerMessage.despawnCharacter: |
| 83 | return this.#despawnCharacter(message.characterId); |
| 84 | case WorkerMessage.command: |
| 85 | return this.#command(message); |
| 86 | case WorkerMessage.stepMovement: |
| 87 | return this.#stepMovement(message.now); |
| 88 | case WorkerMessage.stepSystems: |
| 89 | return this.#stepSystems(message.now); |
| 90 | case WorkerMessage.stepSpawners: |
| 91 | return { spawnRequests: collectSpawnRequests(this) }; |
| 92 | case WorkerMessage.setGroundItems: |
| 93 | setGroundItemsForRegion(this, message.regionX, message.regionY, message.items, tileToRegion); |
| 94 | return { ok: true }; |
| 95 | case WorkerMessage.inspectBag: |
| 96 | return this.#inspectBag(message.entityId); |
| 97 | case WorkerMessage.inspectSkills: |
| 98 | return this.#inspectSkills(message.entityId); |
| 99 | case WorkerMessage.applyBag: |
| 100 | return this.#applyBag(message.entityId, message.bag); |
| 101 | case WorkerMessage.updateFlight: |
| 102 | return this.#updateFlight(message.entityId, message.flight, message.now); |
| 103 | case WorkerMessage.chunkIsSimulating: |
| 104 | return this.chunkIsSimulating(message.tileX, message.tileY); |
| 105 | case WorkerMessage.applyTransfer: |
| 106 | return this.#applyTransfer(message); |
| 107 | case WorkerMessage.commitTransfer: |
| 108 | return this.#commitTransfer(message.entityId); |
| 109 | case WorkerMessage.abortTransfer: |
| 110 | return this.#abortTransfer(message.entityId); |
| 111 | case WorkerMessage.snapshotChunk: |
| 112 | return this.#chunkSnapshot(message.chunkX, message.chunkY); |
| 113 | case WorkerMessage.listLoaders: |
| 114 | return this.#listLoaders(); |
| 115 | case WorkerMessage.listPublicEntities: |
| 116 | return this.#listPublicEntities(); |
| 117 | case WorkerMessage.entitiesInRegion: |
| 118 | return this.#entitiesInRegion(message.regionX, message.regionY); |
| 119 | case WorkerMessage.inspect: |
| 120 | return publicEntityView(this.ecs, message.entityId); |
| 121 | default: |
| 122 | throw new Error(`Unknown worker message ${message.type}`); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | ownsRegion (regionX, regionY) |
| 127 | { |
| 128 | return this.ownedRegions.has(regionKey(regionX, regionY)); |
| 129 | } |
| 130 | |
| 131 | requireOwnedEntity (sessionId, characterId) |
| 132 | { |
| 133 | return this.#requireOwnedEntity(sessionId, characterId); |
| 134 | } |
| 135 | |
| 136 | chunkData (chunkX, chunkY) |
| 137 | { |
| 138 | return this.#chunkData(chunkX, chunkY); |
| 139 | } |
| 140 | |
| 141 | occupied (tileX, tileY) |
| 142 | { |
| 143 | return this.#occupied(tileX, tileY); |
| 144 | } |
| 145 | |
| 146 | #configure (message) |
| 147 | { |
| 148 | if (message.settings) |
| 149 | { this.settings = { ...this.settings, ...message.settings }; } |
| 150 | if (message.activeRevisionId != null) |
| 151 | { this.activeRevisionId = message.activeRevisionId; } |
| 152 | return { ok: true }; |
| 153 | } |
| 154 | |
| 155 | #assignRegion (message) |
| 156 | { |
| 157 | this.ownedRegions.add(regionKey(message.regionX, message.regionY)); |
| 158 | for (const chunk of message.chunks ?? []) |
| 159 | { |
| 160 | this.chunks.set(chunkKey(chunk.chunkX, chunk.chunkY), { |
| 161 | data: cloneChunkData(chunk.data), |
| 162 | simulating: false, |
| 163 | replica: false |
| 164 | }); |
| 165 | } |
| 166 | return { ok: true }; |
| 167 | } |
| 168 | |
| 169 | #replaceRegionChunks (message) |
| 170 | { |
| 171 | for (const chunk of message.chunks ?? []) |
| 172 | { |
| 173 | const key = chunkKey(chunk.chunkX, chunk.chunkY); |
| 174 | const existing = this.chunks.get(key); |
| 175 | this.chunks.set(key, { |
| 176 | data: cloneChunkData(chunk.data), |
| 177 | simulating: existing?.simulating ?? false, |
| 178 | replica: existing?.replica ?? false |
| 179 | }); |
| 180 | } |
| 181 | return { ok: true }; |
| 182 | } |
| 183 | |
| 184 | #dropRegion (message) |
| 185 | { |
| 186 | const dropped = []; |
| 187 | for (const record of this.#entitiesInRegion(message.regionX, message.regionY)) |
| 188 | { |
| 189 | const components = record.payload?.components; |
| 190 | if (components?.creature && !components?.character && components?.health) |
| 191 | { components.health.current = components.health.max; } |
| 192 | dropped.push(record); |
| 193 | this.#destroyEntity(record.entityId); |
| 194 | } |
| 195 | const groundItems = groundItemsInRegion(this, message.regionX, message.regionY, tileToRegion); |
| 196 | for (const item of groundItems) |
| 197 | { removeGroundItem(this, item.groundItemId); } |
| 198 | for (const [key, chunk] of [...this.chunks.entries()]) |
| 199 | { |
| 200 | const { chunkX, chunkY } = parseChunkKey(key); |
| 201 | if (chunk.replica) |
| 202 | { continue; } |
| 203 | if (chunkToRegion(chunkX) === message.regionX && chunkToRegion(chunkY) === message.regionY) |
| 204 | { this.chunks.delete(key); } |
| 205 | } |
| 206 | this.ownedRegions.delete(regionKey(message.regionX, message.regionY)); |
| 207 | return { dropped, groundItems }; |
| 208 | } |
| 209 | |
| 210 | #setReplicas (message) |
| 211 | { |
| 212 | const keep = new Set(); |
| 213 | for (const chunk of message.chunks ?? []) |
| 214 | { |
| 215 | const key = chunkKey(chunk.chunkX, chunk.chunkY); |
| 216 | keep.add(key); |
| 217 | const existing = this.chunks.get(key); |
| 218 | if (existing && !existing.replica) |
| 219 | { continue; } |
| 220 | this.chunks.set(key, { |
| 221 | data: cloneChunkData(chunk.data), |
| 222 | simulating: false, |
| 223 | replica: true |
| 224 | }); |
| 225 | } |
| 226 | for (const [key, chunk] of [...this.chunks.entries()]) |
| 227 | { |
| 228 | if (chunk.replica && !keep.has(key)) |
| 229 | { this.chunks.delete(key); } |
| 230 | } |
| 231 | return { ok: true }; |
| 232 | } |
| 233 | |
| 234 | #setSimulatingChunks (message) |
| 235 | { |
| 236 | const simulating = new Set(message.chunkKeys ?? []); |
| 237 | for (const [key, chunk] of this.chunks) |
| 238 | { |
| 239 | if (chunk.replica) |
| 240 | { |
| 241 | chunk.simulating = false; |
| 242 | continue; |
| 243 | } |
| 244 | chunk.simulating = simulating.has(key); |
| 245 | } |
| 246 | return { ok: true }; |
| 247 | } |
| 248 | |
| 249 | #setGhosts (message) |
| 250 | { |
| 251 | this.ghosts.clear(); |
| 252 | this.ghostOccupancy.clear(); |
| 253 | for (const ghost of message.ghosts ?? []) |
| 254 | { |
| 255 | this.ghosts.set(ghost.entityId, ghost); |
| 256 | this.ghostOccupancy.set(tileKey(ghost.tileX, ghost.tileY), ghost.entityId); |
| 257 | } |
| 258 | return { ok: true }; |
| 259 | } |
| 260 | |
| 261 | #spawn (message) |
| 262 | { |
| 263 | const entityId = deserializeEntity(this.ecs, message); |
| 264 | const handle = this.ecs.handleOf(entityId); |
| 265 | const position = this.ecs.get(handle, Component.position); |
| 266 | if (!position) |
| 267 | { |
| 268 | this.ecs.destroy(handle); |
| 269 | throw new Error("Spawned entity is missing position."); |
| 270 | } |
| 271 | const flying = this.#isFlying(handle); |
| 272 | if (!flying) |
| 273 | { |
| 274 | const occupant = this.#occupied(position.tileX, position.tileY); |
| 275 | if (occupant && occupant !== entityId) |
| 276 | { |
| 277 | this.ecs.destroy(handle); |
| 278 | const error = new Error("That tile is occupied."); |
| 279 | error.userFacing = true; |
| 280 | throw error; |
| 281 | } |
| 282 | this.#occupy(entityId, position.tileX, position.tileY); |
| 283 | } |
| 284 | const creature = this.ecs.get(handle, Component.creature); |
| 285 | const def = creature ? mobDefinition(creature.mobTypeId) : null; |
| 286 | if (def?.freeFlight) |
| 287 | { ensureFlightComponent(this.ecs, handle, position, def); } |
| 288 | const character = this.ecs.get(handle, Component.character); |
| 289 | if (character?.characterId) |
| 290 | { this.characterEntities.set(character.characterId, entityId); } |
| 291 | return publicEntityView(this.ecs, entityId); |
| 292 | } |
| 293 | |
| 294 | #despawn (entityId) |
| 295 | { |
| 296 | return this.#destroyEntity(entityId); |
| 297 | } |
| 298 | |
| 299 | #despawnCharacter (characterId) |
| 300 | { |
| 301 | const entityId = this.characterEntities.get(characterId); |
| 302 | if (!entityId) |
| 303 | { return null; } |
| 304 | return this.#destroyEntity(entityId); |
| 305 | } |
| 306 | |
| 307 | #destroyEntity (entityId) |
| 308 | { |
| 309 | const handle = this.ecs.handleOf(entityId); |
| 310 | if (handle == null) |
| 311 | { return null; } |
| 312 | const view = publicEntityView(this.ecs, entityId); |
| 313 | const position = this.ecs.get(handle, Component.position); |
| 314 | const character = this.ecs.get(handle, Component.character); |
| 315 | if (position && !this.#isFlying(handle)) |
| 316 | { this.#vacate(entityId, position.tileX, position.tileY); } |
| 317 | if (character?.characterId) |
| 318 | { this.characterEntities.delete(character.characterId); } |
| 319 | this.frozen.delete(entityId); |
| 320 | this.ecs.destroy(handle); |
| 321 | return view; |
| 322 | } |
| 323 | |
| 324 | #command (message) |
| 325 | { |
| 326 | const { sessionId, command } = message; |
| 327 | switch (command.type) |
| 328 | { |
| 329 | case "move-to": |
| 330 | this.#moveTo(sessionId, command.characterId, command.tileX, command.tileY); |
| 331 | return { events: [] }; |
| 332 | case "set-gait": |
| 333 | this.#setGait(sessionId, command.characterId, command.gait); |
| 334 | return { events: [] }; |
| 335 | case "attack": |
| 336 | return this.#attack(sessionId, command.characterId, command.targetEntityId, message.now ?? Date.now()); |
| 337 | case "cancel-attack": |
| 338 | return this.#cancelAttack(sessionId, command.characterId); |
| 339 | case "interact": |
| 340 | return this.#interact(sessionId, command.characterId, command.targetEntityId); |
| 341 | case "follow": |
| 342 | return this.#follow(sessionId, command.characterId, command.targetEntityId); |
| 343 | case "cancel-follow": |
| 344 | return this.#cancelFollow(sessionId, command.characterId); |
| 345 | default: |
| 346 | { |
| 347 | const result = handleItemCommand(this, sessionId, command, message.now ?? Date.now()); |
| 348 | return result ?? { events: [] }; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | #inspectBag (entityId) |
| 354 | { |
| 355 | const handle = this.ecs.handleOf(entityId); |
| 356 | if (handle == null) |
| 357 | { return null; } |
| 358 | return bagView(this.ecs, handle); |
| 359 | } |
| 360 | |
| 361 | #inspectSkills (entityId) |
| 362 | { |
| 363 | const handle = this.ecs.handleOf(entityId); |
| 364 | if (handle == null) |
| 365 | { return null; } |
| 366 | const skills = this.ecs.get(handle, Component.skills); |
| 367 | if (!skills?.xp) |
| 368 | { return null; } |
| 369 | return skills; |
| 370 | } |
| 371 | |
| 372 | #applyBag (entityId, bag) |
| 373 | { |
| 374 | const handle = this.ecs.handleOf(entityId); |
| 375 | if (handle == null) |
| 376 | { return null; } |
| 377 | return applyBagView(this.ecs, handle, bag); |
| 378 | } |
| 379 | |
| 380 | #updateFlight (entityId, flightPatch, now = Date.now()) |
| 381 | { |
| 382 | const handle = this.ecs.handleOf(entityId); |
| 383 | if (handle == null) |
| 384 | { return null; } |
| 385 | let flight = this.ecs.get(handle, Component.flight); |
| 386 | if (!flight) |
| 387 | { flight = this.ecs.add(handle, Component.flight, { ...flightPatch }); } |
| 388 | else |
| 389 | { Object.assign(flight, flightPatch); } |
| 390 | const position = this.ecs.get(handle, Component.position); |
| 391 | const movement = this.ecs.get(handle, Component.movement); |
| 392 | const fromTileX = position.tileX; |
| 393 | const fromTileY = position.tileY; |
| 394 | const tileX = Math.floor(flight.x); |
| 395 | const tileY = Math.floor(flight.y); |
| 396 | position.tileX = tileX; |
| 397 | position.tileY = tileY; |
| 398 | const destRegionX = tileToRegion(tileX); |
| 399 | const destRegionY = tileToRegion(tileY); |
| 400 | if (!this.ownsRegion(destRegionX, destRegionY)) |
| 401 | { |
| 402 | this.frozen.add(entityId); |
| 403 | return { |
| 404 | transferRequest: { |
| 405 | type: "transfer-request", |
| 406 | entityId, |
| 407 | fromTileX, |
| 408 | fromTileY, |
| 409 | destTileX: tileX, |
| 410 | destTileY: tileY, |
| 411 | toRegionX: destRegionX, |
| 412 | toRegionY: destRegionY |
| 413 | } |
| 414 | }; |
| 415 | } |
| 416 | const dt = flight.lastAt && now > flight.lastAt |
| 417 | ? Math.min(0.25, (now - flight.lastAt) / 1000) |
| 418 | : 1 / 20; |
| 419 | return { |
| 420 | event: { |
| 421 | type: "entity-move", |
| 422 | entityId, |
| 423 | fromTileX, |
| 424 | fromTileY, |
| 425 | tileX, |
| 426 | tileY, |
| 427 | worldX: flight.x, |
| 428 | worldY: flight.y, |
| 429 | gait: movement?.gait ?? "walk", |
| 430 | lerpMs: Math.max(50, Math.round(dt * 1000)), |
| 431 | freeFlight: true |
| 432 | } |
| 433 | }; |
| 434 | } |
| 435 | |
| 436 | #moveTo (sessionId, characterId, tileX, tileY) |
| 437 | { |
| 438 | const { handle, entityId } = this.#requireOwnedEntity(sessionId, characterId); |
| 439 | if (this.frozen.has(entityId)) |
| 440 | { return; } |
| 441 | clearEngage(this.ecs, entityId); |
| 442 | clearFollow(this.ecs, entityId); |
| 443 | const position = this.ecs.get(handle, Component.position); |
| 444 | const movement = this.ecs.get(handle, Component.movement); |
| 445 | let path = this.ecs.get(handle, Component.path); |
| 446 | if (!path) |
| 447 | { path = this.ecs.add(handle, Component.path, { tiles: [] }); } |
| 448 | const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS); |
| 449 | path.tiles = findPath( |
| 450 | position.tileX, |
| 451 | position.tileY, |
| 452 | tileX, |
| 453 | tileY, |
| 454 | (x, y) => this.walkableFor(entityId, x, y), |
| 455 | bounds.minX, |
| 456 | bounds.minY, |
| 457 | bounds.maxX, |
| 458 | bounds.maxY |
| 459 | ); |
| 460 | movement.walkCarry = 0; |
| 461 | } |
| 462 | |
| 463 | #attack (sessionId, characterId, targetEntityId, now) |
| 464 | { |
| 465 | const { handle, entityId } = this.#requireOwnedEntity(sessionId, characterId); |
| 466 | const targetHandle = this.ecs.handleOf(targetEntityId); |
| 467 | if (targetHandle == null) |
| 468 | { |
| 469 | const error = new Error("Target is not here."); |
| 470 | error.userFacing = true; |
| 471 | throw error; |
| 472 | } |
| 473 | if (targetEntityId === entityId) |
| 474 | { |
| 475 | const error = new Error("Cannot attack yourself."); |
| 476 | error.userFacing = true; |
| 477 | throw error; |
| 478 | } |
| 479 | if (isUntargetable(this.ecs, targetHandle)) |
| 480 | { |
| 481 | const error = new Error("That cannot be attacked."); |
| 482 | error.userFacing = true; |
| 483 | throw error; |
| 484 | } |
| 485 | const health = this.ecs.get(targetHandle, Component.health); |
| 486 | if (!health) |
| 487 | { |
| 488 | const error = new Error("That cannot be attacked."); |
| 489 | error.userFacing = true; |
| 490 | throw error; |
| 491 | } |
| 492 | if (!this.ecs.get(handle, Component.combat)) |
| 493 | { this.ecs.add(handle, Component.combat, { targetEntityId: null, nextSwingAt: 0 }); } |
| 494 | clearFollow(this.ecs, entityId); |
| 495 | engageTarget(this.ecs, entityId, targetEntityId, now); |
| 496 | if (ensureMobCombat(this.ecs, targetEntityId)) |
| 497 | { engageTarget(this.ecs, targetEntityId, entityId, now); } |
| 498 | const path = this.ecs.get(handle, Component.path); |
| 499 | if (path) |
| 500 | { path.tiles = []; } |
| 501 | return { events: [] }; |
| 502 | } |
| 503 | |
| 504 | #cancelAttack (sessionId, characterId) |
| 505 | { |
| 506 | const { entityId } = this.#requireOwnedEntity(sessionId, characterId); |
| 507 | clearEngage(this.ecs, entityId); |
| 508 | return { events: [] }; |
| 509 | } |
| 510 | |
| 511 | #interact (sessionId, characterId, targetEntityId) |
| 512 | { |
| 513 | this.#requireOwnedEntity(sessionId, characterId); |
| 514 | const targetHandle = this.ecs.handleOf(targetEntityId); |
| 515 | if (targetHandle == null) |
| 516 | { |
| 517 | const error = new Error("Target is not here."); |
| 518 | error.userFacing = true; |
| 519 | throw error; |
| 520 | } |
| 521 | if (isUntargetable(this.ecs, targetHandle)) |
| 522 | { |
| 523 | const error = new Error("Nothing to say."); |
| 524 | error.userFacing = true; |
| 525 | throw error; |
| 526 | } |
| 527 | const creature = this.ecs.get(targetHandle, Component.creature); |
| 528 | const def = creature ? mobDefinition(creature.mobTypeId) : null; |
| 529 | if (!def?.talkable) |
| 530 | { |
| 531 | const error = new Error("They have nothing to say."); |
| 532 | error.userFacing = true; |
| 533 | throw error; |
| 534 | } |
| 535 | return { events: [] }; |
| 536 | } |
| 537 | |
| 538 | #follow (sessionId, characterId, targetEntityId) |
| 539 | { |
| 540 | const { handle, entityId } = this.#requireOwnedEntity(sessionId, characterId); |
| 541 | const targetHandle = this.ecs.handleOf(targetEntityId); |
| 542 | if (targetHandle == null) |
| 543 | { |
| 544 | const error = new Error("Target is not here."); |
| 545 | error.userFacing = true; |
| 546 | throw error; |
| 547 | } |
| 548 | if (targetEntityId === entityId) |
| 549 | { |
| 550 | const error = new Error("Cannot follow yourself."); |
| 551 | error.userFacing = true; |
| 552 | throw error; |
| 553 | } |
| 554 | if (!this.ecs.get(targetHandle, Component.position)) |
| 555 | { |
| 556 | const error = new Error("That cannot be followed."); |
| 557 | error.userFacing = true; |
| 558 | throw error; |
| 559 | } |
| 560 | clearEngage(this.ecs, entityId); |
| 561 | setFollow(this.ecs, entityId, targetEntityId); |
| 562 | const path = this.ecs.get(handle, Component.path); |
| 563 | if (path) |
| 564 | { path.tiles = []; } |
| 565 | return { events: [] }; |
| 566 | } |
| 567 | |
| 568 | #cancelFollow (sessionId, characterId) |
| 569 | { |
| 570 | const { entityId } = this.#requireOwnedEntity(sessionId, characterId); |
| 571 | clearFollow(this.ecs, entityId); |
| 572 | return { events: [] }; |
| 573 | } |
| 574 | |
| 575 | #setGait (sessionId, characterId, gait) |
| 576 | { |
| 577 | if (gait !== GAIT.walk && gait !== GAIT.run) |
| 578 | { |
| 579 | const error = new Error("Gait must be walk or run."); |
| 580 | error.userFacing = true; |
| 581 | throw error; |
| 582 | } |
| 583 | const { handle } = this.#requireOwnedEntity(sessionId, characterId); |
| 584 | this.ecs.get(handle, Component.movement).gait = gait; |
| 585 | } |
| 586 | |
| 587 | #requireOwnedEntity (sessionId, characterId) |
| 588 | { |
| 589 | const entityId = this.characterEntities.get(characterId); |
| 590 | if (!entityId) |
| 591 | { |
| 592 | const error = new Error("Character is not in the world."); |
| 593 | error.userFacing = true; |
| 594 | throw error; |
| 595 | } |
| 596 | const handle = this.ecs.handleOf(entityId); |
| 597 | const identity = handle != null ? this.ecs.get(handle, Component.networkIdentity) : null; |
| 598 | if (!identity || identity.sessionId !== sessionId) |
| 599 | { |
| 600 | const error = new Error("Character is not in the world."); |
| 601 | error.userFacing = true; |
| 602 | throw error; |
| 603 | } |
| 604 | return { handle, entityId }; |
| 605 | } |
| 606 | |
| 607 | #stepSystems (now = Date.now()) |
| 608 | { |
| 609 | const events = []; |
| 610 | const transferRequests = []; |
| 611 | const flight = stepFreeFlight(this, now); |
| 612 | for (const event of flight) |
| 613 | { |
| 614 | if (event.type === "transfer-request") |
| 615 | { |
| 616 | const payload = serializeEntity(this.ecs, event.entityId); |
| 617 | transferRequests.push({ |
| 618 | entityId: event.entityId, |
| 619 | fromTileX: event.fromTileX, |
| 620 | fromTileY: event.fromTileY, |
| 621 | destTileX: event.destTileX, |
| 622 | destTileY: event.destTileY, |
| 623 | toRegionX: event.toRegionX, |
| 624 | toRegionY: event.toRegionY, |
| 625 | payload |
| 626 | }); |
| 627 | continue; |
| 628 | } |
| 629 | events.push(event); |
| 630 | } |
| 631 | events.push(...stepCombat(this, now)); |
| 632 | stepFollow(this); |
| 633 | return { events, transferRequests }; |
| 634 | } |
| 635 | |
| 636 | #stepMovement (now = Date.now()) |
| 637 | { |
| 638 | const events = expireGroundItems(this, now); |
| 639 | const transferRequests = []; |
| 640 | this.#queueAmbientWander(); |
| 641 | for (const entity of this.ecs.query(Component.position, Component.movement)) |
| 642 | { |
| 643 | const { handle, entityId, position, movement } = entity; |
| 644 | if (this.ecs.has(handle, Component.flight)) |
| 645 | { continue; } |
| 646 | const path = this.ecs.get(handle, Component.path); |
| 647 | const tiles = path?.tiles; |
| 648 | if (this.frozen.has(entityId) || !tiles?.length) |
| 649 | { continue; } |
| 650 | if (!this.chunkIsSimulating(position.tileX, position.tileY)) |
| 651 | { continue; } |
| 652 | |
| 653 | const isWalk = movement.gait !== GAIT.run; |
| 654 | if (isWalk) |
| 655 | { |
| 656 | movement.walkCarry = (movement.walkCarry + 1) % 2; |
| 657 | if (movement.walkCarry !== 0) |
| 658 | { continue; } |
| 659 | } |
| 660 | |
| 661 | const next = tiles[0]; |
| 662 | const destRegionX = tileToRegion(next.x); |
| 663 | const destRegionY = tileToRegion(next.y); |
| 664 | if (!this.ownsRegion(destRegionX, destRegionY)) |
| 665 | { |
| 666 | this.frozen.add(entityId); |
| 667 | transferRequests.push({ |
| 668 | entityId, |
| 669 | fromTileX: position.tileX, |
| 670 | fromTileY: position.tileY, |
| 671 | destTileX: next.x, |
| 672 | destTileY: next.y, |
| 673 | toRegionX: destRegionX, |
| 674 | toRegionY: destRegionY, |
| 675 | payload: serializeEntity(this.ecs, entityId) |
| 676 | }); |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | if (!this.walkableFor(entityId, next.x, next.y)) |
| 681 | { continue; } |
| 682 | |
| 683 | const fromTileX = position.tileX; |
| 684 | const fromTileY = position.tileY; |
| 685 | const flying = this.#isFlying(handle); |
| 686 | if (!flying) |
| 687 | { this.#vacate(entityId, fromTileX, fromTileY); } |
| 688 | position.tileX = next.x; |
| 689 | position.tileY = next.y; |
| 690 | tiles.shift(); |
| 691 | if (!flying) |
| 692 | { this.#occupy(entityId, position.tileX, position.tileY); } |
| 693 | recordFollowTrail(this.ecs, entityId, fromTileX, fromTileY); |
| 694 | consumeFollowTrail(this.ecs, entityId, position.tileX, position.tileY); |
| 695 | events.push(this.#moveEvent(entityId, fromTileX, fromTileY)); |
| 696 | } |
| 697 | return { events, transferRequests }; |
| 698 | } |
| 699 | |
| 700 | #queueAmbientWander () |
| 701 | { |
| 702 | for (const entity of this.ecs.query(Component.position, Component.movement, Component.creature)) |
| 703 | { |
| 704 | const def = mobDefinition(entity.creature.mobTypeId); |
| 705 | if (!def?.ambient || def.freeFlight) |
| 706 | { continue; } |
| 707 | const path = this.ecs.get(entity.handle, Component.path); |
| 708 | if (!path) |
| 709 | { continue; } |
| 710 | if (path.tiles?.length) |
| 711 | { continue; } |
| 712 | if (!this.chunkIsSimulating(entity.position.tileX, entity.position.tileY)) |
| 713 | { continue; } |
| 714 | if (Math.random() > 0.35) |
| 715 | { continue; } |
| 716 | const dirs = [ |
| 717 | { x: 1, y: 0 }, { x: -1, y: 0 }, { x: 0, y: 1 }, { x: 0, y: -1 }, |
| 718 | { x: 1, y: 1 }, { x: 1, y: -1 }, { x: -1, y: 1 }, { x: -1, y: -1 } |
| 719 | ]; |
| 720 | const dir = dirs[Math.floor(Math.random() * dirs.length)]; |
| 721 | const nextX = entity.position.tileX + dir.x; |
| 722 | const nextY = entity.position.tileY + dir.y; |
| 723 | if (!this.walkableFor(entity.entityId, nextX, nextY)) |
| 724 | { continue; } |
| 725 | path.tiles = [{ x: nextX, y: nextY }]; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | #applyTransfer (message) |
| 730 | { |
| 731 | const payload = message.payload; |
| 732 | const entityId = payload.entityId; |
| 733 | if (this.ecs.hasEntity(entityId)) |
| 734 | { return { accepted: false, reason: "already-present" }; } |
| 735 | |
| 736 | const position = payload.components?.position; |
| 737 | const destTileX = message.destTileX ?? position?.tileX; |
| 738 | const destTileY = message.destTileY ?? position?.tileY; |
| 739 | if (destTileX == null || destTileY == null) |
| 740 | { return { accepted: false, reason: "missing-destination" }; } |
| 741 | if (!this.ownsRegion(tileToRegion(destTileX), tileToRegion(destTileY))) |
| 742 | { return { accepted: false, reason: "not-owner" }; } |
| 743 | |
| 744 | const creature = payload.components?.creature; |
| 745 | const flying = !!mobDefinition(creature?.mobTypeId)?.flying; |
| 746 | if (flying) |
| 747 | { |
| 748 | const data = this.#chunkData(tileToChunk(destTileX), tileToChunk(destTileY)); |
| 749 | if (!data?.open) |
| 750 | { return { accepted: false, reason: "blocked" }; } |
| 751 | } |
| 752 | else if (!this.isEnterable(destTileX, destTileY) || this.#occupied(destTileX, destTileY)) |
| 753 | { return { accepted: false, reason: "blocked" }; } |
| 754 | |
| 755 | deserializeEntity(this.ecs, payload); |
| 756 | const handle = this.ecs.handleOf(entityId); |
| 757 | const livePosition = this.ecs.get(handle, Component.position); |
| 758 | const path = this.ecs.get(handle, Component.path); |
| 759 | livePosition.tileX = destTileX; |
| 760 | livePosition.tileY = destTileY; |
| 761 | if (path?.tiles?.length && path.tiles[0].x === destTileX && path.tiles[0].y === destTileY) |
| 762 | { path.tiles.shift(); } |
| 763 | if (!flying) |
| 764 | { this.#occupy(entityId, destTileX, destTileY); } |
| 765 | const character = this.ecs.get(handle, Component.character); |
| 766 | if (character?.characterId) |
| 767 | { this.characterEntities.set(character.characterId, entityId); } |
| 768 | this.frozen.delete(entityId); |
| 769 | return { |
| 770 | accepted: true, |
| 771 | entity: publicEntityView(this.ecs, entityId) |
| 772 | }; |
| 773 | } |
| 774 | |
| 775 | #commitTransfer (entityId) |
| 776 | { |
| 777 | if (!this.ecs.hasEntity(entityId)) |
| 778 | { return null; } |
| 779 | return this.#destroyEntity(entityId); |
| 780 | } |
| 781 | |
| 782 | #abortTransfer (entityId) |
| 783 | { |
| 784 | this.frozen.delete(entityId); |
| 785 | return publicEntityView(this.ecs, entityId); |
| 786 | } |
| 787 | |
| 788 | #listLoaders () |
| 789 | { |
| 790 | return this.ecs.query(Component.position, Component.chunkLoader).map((entity) => ({ |
| 791 | entityId: entity.entityId, |
| 792 | tileX: entity.position.tileX, |
| 793 | tileY: entity.position.tileY, |
| 794 | chunkLoader: { ...entity.chunkLoader } |
| 795 | })); |
| 796 | } |
| 797 | |
| 798 | #listPublicEntities () |
| 799 | { |
| 800 | return this.ecs.query(Component.position).map((entity) => publicEntityView(this.ecs, entity.entityId)).filter(Boolean); |
| 801 | } |
| 802 | |
| 803 | #entitiesInRegion (regionX, regionY) |
| 804 | { |
| 805 | const records = []; |
| 806 | for (const entity of this.ecs.query(Component.position)) |
| 807 | { |
| 808 | if (tileToRegion(entity.position.tileX) !== regionX || tileToRegion(entity.position.tileY) !== regionY) |
| 809 | { continue; } |
| 810 | records.push({ |
| 811 | ...publicEntityView(this.ecs, entity.entityId), |
| 812 | persistent: this.ecs.has(entity.handle, Component.persistent), |
| 813 | payload: serializeEntity(this.ecs, entity.entityId) |
| 814 | }); |
| 815 | } |
| 816 | return records; |
| 817 | } |
| 818 | |
| 819 | #chunkSnapshot (chunkX, chunkY) |
| 820 | { |
| 821 | const data = this.#chunkData(chunkX, chunkY); |
| 822 | const entities = []; |
| 823 | for (const entity of this.ecs.query(Component.position, Component.appearance)) |
| 824 | { |
| 825 | if (tileToChunk(entity.position.tileX) !== chunkX || tileToChunk(entity.position.tileY) !== chunkY) |
| 826 | { continue; } |
| 827 | entities.push(publicEntityView(this.ecs, entity.entityId)); |
| 828 | } |
| 829 | return { |
| 830 | chunkX, |
| 831 | chunkY, |
| 832 | open: data.open, |
| 833 | closedEdges: closedNeighborEdges(chunkX, chunkY, (x, y) => this.#chunkData(x, y).open), |
| 834 | grounds: data.grounds, |
| 835 | blocks: data.blocks, |
| 836 | light: data.light, |
| 837 | entities, |
| 838 | groundItems: groundItemsInChunk(this, chunkX, chunkY) |
| 839 | }; |
| 840 | } |
| 841 | |
| 842 | #moveEvent (entityId, fromTileX, fromTileY) |
| 843 | { |
| 844 | const view = publicEntityView(this.ecs, entityId); |
| 845 | return { |
| 846 | type: "entity-move", |
| 847 | entityId, |
| 848 | fromTileX, |
| 849 | fromTileY, |
| 850 | tileX: view.tileX, |
| 851 | tileY: view.tileY, |
| 852 | worldX: view.worldX, |
| 853 | worldY: view.worldY, |
| 854 | gait: view.gait, |
| 855 | freeFlight: !!view.freeFlight |
| 856 | }; |
| 857 | } |
| 858 | |
| 859 | chunkIsSimulating (tileX, tileY) |
| 860 | { |
| 861 | const chunk = this.chunks.get(chunkKey(tileToChunk(tileX), tileToChunk(tileY))); |
| 862 | return !!chunk && !chunk.replica && chunk.simulating; |
| 863 | } |
| 864 | |
| 865 | chunkData (chunkX, chunkY) |
| 866 | { |
| 867 | return this.#chunkData(chunkX, chunkY); |
| 868 | } |
| 869 | |
| 870 | walkableFor (entityId, tileX, tileY) |
| 871 | { |
| 872 | const handle = this.ecs.handleOf(entityId); |
| 873 | if (this.#isFlying(handle)) |
| 874 | { |
| 875 | const data = this.#chunkData(tileToChunk(tileX), tileToChunk(tileY)); |
| 876 | return !!data?.open; |
| 877 | } |
| 878 | if (!this.isEnterable(tileX, tileY)) |
| 879 | { return false; } |
| 880 | const occupant = this.#occupied(tileX, tileY); |
| 881 | return !occupant || occupant === entityId; |
| 882 | } |
| 883 | |
| 884 | destroyEntity (entityId) |
| 885 | { |
| 886 | return this.#destroyEntity(entityId); |
| 887 | } |
| 888 | |
| 889 | occupied (tileX, tileY) |
| 890 | { |
| 891 | return this.#occupied(tileX, tileY); |
| 892 | } |
| 893 | |
| 894 | #chunkIsSimulating (tileX, tileY) |
| 895 | { |
| 896 | return this.chunkIsSimulating(tileX, tileY); |
| 897 | } |
| 898 | |
| 899 | #chunkData (chunkX, chunkY) |
| 900 | { |
| 901 | const loaded = this.chunks.get(chunkKey(chunkX, chunkY)); |
| 902 | if (loaded) |
| 903 | { return loaded.data; } |
| 904 | return createEmptyChunkData(this.settings.defaultGroundTypeId); |
| 905 | } |
| 906 | |
| 907 | isEnterable (tileX, tileY) |
| 908 | { |
| 909 | const data = this.#chunkData(tileToChunk(tileX), tileToChunk(tileY)); |
| 910 | if (!data.open) |
| 911 | { return false; } |
| 912 | return isChunkTilePassable(data, tileToLocal(tileX), tileToLocal(tileY)); |
| 913 | } |
| 914 | |
| 915 | #walkableFor (entityId, tileX, tileY) |
| 916 | { |
| 917 | return this.walkableFor(entityId, tileX, tileY); |
| 918 | } |
| 919 | |
| 920 | #isFlying (handle) |
| 921 | { |
| 922 | if (handle == null) |
| 923 | { return false; } |
| 924 | const creature = this.ecs.get(handle, Component.creature); |
| 925 | return !!mobDefinition(creature?.mobTypeId)?.flying; |
| 926 | } |
| 927 | |
| 928 | #occupy (entityId, tileX, tileY) |
| 929 | { |
| 930 | this.occupancy.set(tileKey(tileX, tileY), entityId); |
| 931 | } |
| 932 | |
| 933 | #vacate (entityId, tileX, tileY) |
| 934 | { |
| 935 | const key = tileKey(tileX, tileY); |
| 936 | if (this.occupancy.get(key) === entityId) |
| 937 | { this.occupancy.delete(key); } |
| 938 | } |
| 939 | |
| 940 | #occupied (tileX, tileY) |
| 941 | { |
| 942 | return this.occupancy.get(tileKey(tileX, tileY)) |
| 943 | ?? this.ghostOccupancy.get(tileKey(tileX, tileY)) |
| 944 | ?? null; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | export function replicaNeededForOwnedChunks (ownedChunkKeys) |
| 949 | { |
| 950 | const needed = new Set(); |
| 951 | for (const key of ownedChunkKeys) |
| 952 | { |
| 953 | const { chunkX, chunkY } = parseChunkKey(key); |
| 954 | for (let dy = -1; dy <= 1; dy++) |
| 955 | { |
| 956 | for (let dx = -1; dx <= 1; dx++) |
| 957 | { |
| 958 | if (dx === 0 && dy === 0) |
| 959 | { continue; } |
| 960 | needed.add(chunkKey(chunkX + dx, chunkY + dy)); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | return needed; |
| 965 | } |