| 1 | import { MessageType, GAIT, CHUNK_SIZE } from "/shared/protocol/constants.js"; |
| 2 | import { BlockType } from "/shared/definitions/tiles.js"; |
| 3 | import { GameSocket, defaultSocketUrl } from "../net/socket.js"; |
| 4 | import { loadSpriteCatalog, preloadCatalogImages, applySpriteSheets, useNearestNeighbour } from "../render/sprites.js"; |
| 5 | import { WorldView } from "../render/worldView.js"; |
| 6 | import { initSocial } from "./social.js"; |
| 7 | import { initChat } from "./chat.js"; |
| 8 | import { initInventory } from "./inventory.js"; |
| 9 | import { initSkills } from "./skills.js"; |
| 10 | import { initCrafting } from "./crafting.js"; |
| 11 | import { initAlchemy } from "./alchemy.js"; |
| 12 | import { initStamina } from "./stamina.js"; |
| 13 | import { initContextMenu } from "./contextMenu.js"; |
| 14 | import { initDeath } from "./death.js"; |
| 15 | import { CraftingStationId } from "/shared/definitions/craftingStations.js"; |
| 16 | import { isOreBoulderState } from "/shared/definitions/materialTiers.js"; |
| 17 | import { formatQuantity } from "/shared/items/formatQuantity.js"; |
| 18 | |
| 19 | const game = new Game(); |
| 20 | const hud = document.getElementById("hud"); |
| 21 | const overlay = document.getElementById("overlay"); |
| 22 | const statusEl = document.getElementById("status"); |
| 23 | const characterList = document.getElementById("character-list"); |
| 24 | const errorEl = document.getElementById("error"); |
| 25 | |
| 26 | let socket; |
| 27 | let session = null; |
| 28 | let playing = false; |
| 29 | let view = null; |
| 30 | let scene = null; |
| 31 | let gait = GAIT.walk; |
| 32 | let catalog = { grounds: {}, blocks: {} }; |
| 33 | let social; |
| 34 | let chat; |
| 35 | let inventory; |
| 36 | let skills; |
| 37 | let crafting; |
| 38 | let alchemy; |
| 39 | let staminaUi; |
| 40 | let contextMenu; |
| 41 | let death; |
| 42 | let pendingPickup = null; |
| 43 | /** @type {{ kind: "bank" | "cauldron" | "furnace" | "mine", tileX: number, tileY: number, getCheques?: boolean } | { kind: "talk", targetEntityId: number } | null } */ |
| 44 | let pendingInteract = null; |
| 45 | /** Framework can report mousePressed for two frames; latch so one physical click is one command. */ |
| 46 | let leftClickConsumed = false; |
| 47 | |
| 48 | function showError (message) |
| 49 | { |
| 50 | errorEl.textContent = message ?? ""; |
| 51 | } |
| 52 | |
| 53 | function renderCharacters (characters) |
| 54 | { |
| 55 | characterList.innerHTML = ""; |
| 56 | for (const character of characters) |
| 57 | { |
| 58 | const row = document.createElement("div"); |
| 59 | row.className = "character-row"; |
| 60 | const pending = character.deletionScheduledAt |
| 61 | ? ` (deleting ${new Date(character.deletionScheduledAt).toLocaleString()})` |
| 62 | : ""; |
| 63 | row.innerHTML = `<strong>${character.firstName} ${character.lastName}</strong> · ${character.sex}${pending}`; |
| 64 | const play = document.createElement("button"); |
| 65 | play.textContent = "Play"; |
| 66 | play.disabled = !!character.deletionScheduledAt; |
| 67 | play.addEventListener("click", () => socket.send({ type: MessageType.selectCharacter, characterId: character.characterId })); |
| 68 | const del = document.createElement("button"); |
| 69 | del.textContent = character.deletionScheduledAt ? "Cancel delete" : "Delete"; |
| 70 | del.addEventListener("click", () => |
| 71 | { |
| 72 | socket.send({ |
| 73 | type: character.deletionScheduledAt ? MessageType.cancelDeleteCharacter : MessageType.scheduleDeleteCharacter, |
| 74 | characterId: character.characterId |
| 75 | }); |
| 76 | }); |
| 77 | row.append(play, del); |
| 78 | characterList.append(row); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function uiBlocked () |
| 83 | { |
| 84 | return !!(social?.isBlocking() || chat?.isBlocking() || inventory?.isBlocking() |
| 85 | || crafting?.isBlocking() || alchemy?.isBlocking() || contextMenu?.isOpen() || death?.isBlocking()); |
| 86 | } |
| 87 | |
| 88 | function clearPendingPickup () |
| 89 | { |
| 90 | pendingPickup = null; |
| 91 | } |
| 92 | |
| 93 | function clearPendingInteract () |
| 94 | { |
| 95 | pendingInteract = null; |
| 96 | } |
| 97 | |
| 98 | function clearAllPendingActions () |
| 99 | { |
| 100 | clearPendingPickup(); |
| 101 | clearPendingInteract(); |
| 102 | } |
| 103 | |
| 104 | function approachAdjacentTile (tileX, tileY) |
| 105 | { |
| 106 | const local = view.entities.get(view.localEntityId); |
| 107 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 108 | { return true; } |
| 109 | let best = null; |
| 110 | let bestDist = Infinity; |
| 111 | for (let dy = -1; dy <= 1; dy++) |
| 112 | { |
| 113 | for (let dx = -1; dx <= 1; dx++) |
| 114 | { |
| 115 | if (dx === 0 && dy === 0) |
| 116 | { continue; } |
| 117 | const destX = tileX + dx; |
| 118 | const destY = tileY + dy; |
| 119 | const dist = local |
| 120 | ? chebyshev(local.tileX, local.tileY, destX, destY) |
| 121 | : 0; |
| 122 | if (dist < bestDist) |
| 123 | { |
| 124 | bestDist = dist; |
| 125 | best = { tileX: destX, tileY: destY }; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | if (best) |
| 130 | { socket.send({ type: MessageType.moveTo, tileX: best.tileX, tileY: best.tileY }); } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | function requestOpenBank (tileX, tileY, { getCheques = false } = {}) |
| 135 | { |
| 136 | clearAllPendingActions(); |
| 137 | pendingInteract = { kind: "bank", tileX, tileY, getCheques: !!getCheques }; |
| 138 | const local = view?.entities.get(view.localEntityId); |
| 139 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 140 | { flushPendingInteract(); } |
| 141 | else |
| 142 | { approachAdjacentTile(tileX, tileY); } |
| 143 | } |
| 144 | |
| 145 | function requestOpenCauldron (tileX, tileY) |
| 146 | { |
| 147 | clearAllPendingActions(); |
| 148 | pendingInteract = { kind: "cauldron", tileX, tileY }; |
| 149 | const local = view?.entities.get(view.localEntityId); |
| 150 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 151 | { flushPendingInteract(); } |
| 152 | else |
| 153 | { approachAdjacentTile(tileX, tileY); } |
| 154 | } |
| 155 | |
| 156 | function requestOpenFurnace (tileX, tileY) |
| 157 | { |
| 158 | clearAllPendingActions(); |
| 159 | pendingInteract = { kind: "furnace", tileX, tileY }; |
| 160 | const local = view?.entities.get(view.localEntityId); |
| 161 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 162 | { flushPendingInteract(); } |
| 163 | else |
| 164 | { approachAdjacentTile(tileX, tileY); } |
| 165 | } |
| 166 | |
| 167 | function requestMine (tileX, tileY) |
| 168 | { |
| 169 | clearAllPendingActions(); |
| 170 | pendingInteract = { kind: "mine", tileX, tileY }; |
| 171 | const local = view?.entities.get(view.localEntityId); |
| 172 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 173 | { flushPendingInteract(); } |
| 174 | else |
| 175 | { approachAdjacentTile(tileX, tileY); } |
| 176 | } |
| 177 | |
| 178 | function requestTalk (targetEntityId) |
| 179 | { |
| 180 | clearPendingPickup(); |
| 181 | pendingInteract = { kind: "talk", targetEntityId }; |
| 182 | const target = view.entities.get(targetEntityId); |
| 183 | if (!target) |
| 184 | { |
| 185 | clearPendingInteract(); |
| 186 | return; |
| 187 | } |
| 188 | if (approachAdjacentTile(target.tileX, target.tileY)) |
| 189 | { flushPendingInteract(); } |
| 190 | } |
| 191 | |
| 192 | function flushPendingInteract () |
| 193 | { |
| 194 | if (!pendingInteract || !view) |
| 195 | { return; } |
| 196 | const local = view.entities.get(view.localEntityId); |
| 197 | if (!local) |
| 198 | { return; } |
| 199 | |
| 200 | if (pendingInteract.kind === "bank") |
| 201 | { |
| 202 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 203 | { return; } |
| 204 | const getCheques = !!pendingInteract.getCheques; |
| 205 | socket.send({ type: MessageType.openBank }); |
| 206 | if (getCheques) |
| 207 | { inventory?.getBlankCheques(); } |
| 208 | clearPendingInteract(); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (pendingInteract.kind === "cauldron") |
| 213 | { |
| 214 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 215 | { return; } |
| 216 | socket.send({ type: MessageType.openCauldron }); |
| 217 | clearPendingInteract(); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | if (pendingInteract.kind === "furnace") |
| 222 | { |
| 223 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 224 | { return; } |
| 225 | socket.send({ type: MessageType.openFurnace }); |
| 226 | clearPendingInteract(); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | if (pendingInteract.kind === "mine") |
| 231 | { |
| 232 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 233 | { return; } |
| 234 | socket.send({ |
| 235 | type: MessageType.mine, |
| 236 | tileX: pendingInteract.tileX, |
| 237 | tileY: pendingInteract.tileY |
| 238 | }); |
| 239 | clearPendingInteract(); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | if (pendingInteract.kind === "talk") |
| 244 | { |
| 245 | const target = view.entities.get(pendingInteract.targetEntityId); |
| 246 | if (!target) |
| 247 | { |
| 248 | clearPendingInteract(); |
| 249 | return; |
| 250 | } |
| 251 | if (chebyshev(local.tileX, local.tileY, target.tileX, target.tileY) > 1) |
| 252 | { |
| 253 | // Target may have moved — keep chasing. |
| 254 | approachAdjacentTile(target.tileX, target.tileY); |
| 255 | return; |
| 256 | } |
| 257 | socket.send({ type: MessageType.interact, targetEntityId: pendingInteract.targetEntityId }); |
| 258 | clearPendingInteract(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | function requestPickup (ground) |
| 263 | { |
| 264 | if (!ground) |
| 265 | { return; } |
| 266 | clearPendingInteract(); |
| 267 | pendingPickup = { |
| 268 | groundItemId: ground.groundItemId, |
| 269 | tileX: ground.tileX, |
| 270 | tileY: ground.tileY |
| 271 | }; |
| 272 | const local = view.entities.get(view.localEntityId); |
| 273 | if (local && local.tileX === ground.tileX && local.tileY === ground.tileY) |
| 274 | { |
| 275 | flushPendingPickup(); |
| 276 | return; |
| 277 | } |
| 278 | socket.send({ type: MessageType.moveTo, tileX: ground.tileX, tileY: ground.tileY }); |
| 279 | } |
| 280 | |
| 281 | function flushPendingPickup () |
| 282 | { |
| 283 | if (!pendingPickup || !view) |
| 284 | { return; } |
| 285 | const item = view.groundItems.get(pendingPickup.groundItemId); |
| 286 | if (!item) |
| 287 | { |
| 288 | clearPendingPickup(); |
| 289 | return; |
| 290 | } |
| 291 | const local = view.entities.get(view.localEntityId); |
| 292 | if (!local) |
| 293 | { return; } |
| 294 | if (local.tileX !== pendingPickup.tileX || local.tileY !== pendingPickup.tileY) |
| 295 | { return; } |
| 296 | socket.send({ type: MessageType.pickupItem, groundItemId: pendingPickup.groundItemId }); |
| 297 | clearPendingPickup(); |
| 298 | } |
| 299 | |
| 300 | function requestAttack (targetEntityId) |
| 301 | { |
| 302 | clearAllPendingActions(); |
| 303 | socket.send({ type: MessageType.attack, targetEntityId }); |
| 304 | } |
| 305 | |
| 306 | function isAttackableEntity (entity) |
| 307 | { |
| 308 | if (!entity || entity.untargetable) |
| 309 | { return false; } |
| 310 | if (entity.canFight) |
| 311 | { return true; } |
| 312 | // Snapshots can omit canFight; mobs are still attackable via LMB. |
| 313 | return !!entity.mobTypeId && !entity.talkable; |
| 314 | } |
| 315 | |
| 316 | function handleLeftClick (mouseX, mouseY) |
| 317 | { |
| 318 | const entity = view.entityAtScreen(mouseX, mouseY); |
| 319 | if (entity && entity.entityId !== view.localEntityId && !entity.untargetable) |
| 320 | { |
| 321 | clearPendingPickup(); |
| 322 | if (entity.characterId) |
| 323 | { |
| 324 | clearPendingInteract(); |
| 325 | social.inspect(entity); |
| 326 | return; |
| 327 | } |
| 328 | if (entity.talkable) |
| 329 | { |
| 330 | requestTalk(entity.entityId); |
| 331 | return; |
| 332 | } |
| 333 | if (isAttackableEntity(entity)) |
| 334 | { |
| 335 | requestAttack(entity.entityId); |
| 336 | return; |
| 337 | } |
| 338 | // Clicked an entity we don't act on — do not fall through to walk (that cancels combat). |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | const tile = view.screenToTile(mouseX, mouseY); |
| 343 | const ground = view.groundItemAtTile(tile.tileX, tile.tileY); |
| 344 | if (ground) |
| 345 | { requestPickup(ground); } |
| 346 | else if (isBankTile(view, tile.tileX, tile.tileY)) |
| 347 | { requestOpenBank(tile.tileX, tile.tileY); } |
| 348 | else if (isCauldronTile(view, tile.tileX, tile.tileY)) |
| 349 | { requestOpenCauldron(tile.tileX, tile.tileY); } |
| 350 | else if (isFurnaceTile(view, tile.tileX, tile.tileY)) |
| 351 | { requestOpenFurnace(tile.tileX, tile.tileY); } |
| 352 | else if (isOreBoulderTile(view, tile.tileX, tile.tileY)) |
| 353 | { requestMine(tile.tileX, tile.tileY); } |
| 354 | else |
| 355 | { |
| 356 | clearAllPendingActions(); |
| 357 | socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY }); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | function handleRightClick (event, mouseX, mouseY) |
| 362 | { |
| 363 | event.preventDefault(); |
| 364 | const tile = view.screenToTile(mouseX, mouseY); |
| 365 | const items = []; |
| 366 | const walkTileX = tile.tileX; |
| 367 | const walkTileY = tile.tileY; |
| 368 | |
| 369 | items.push({ |
| 370 | label: "Walk here", |
| 371 | onSelect: () => |
| 372 | { |
| 373 | clearAllPendingActions(); |
| 374 | socket.send({ type: MessageType.moveTo, tileX: walkTileX, tileY: walkTileY }); |
| 375 | } |
| 376 | }); |
| 377 | |
| 378 | for (const entity of view.entitiesAtTile(tile.tileX, tile.tileY)) |
| 379 | { |
| 380 | if (entity.entityId === view.localEntityId) |
| 381 | { continue; } |
| 382 | const targetName = entityLabel(entity); |
| 383 | items.push({ |
| 384 | label: `Follow ${targetName}`, |
| 385 | onSelect: () => |
| 386 | { |
| 387 | clearAllPendingActions(); |
| 388 | socket.send({ type: MessageType.follow, targetEntityId: entity.entityId }); |
| 389 | } |
| 390 | }); |
| 391 | if (entity.untargetable) |
| 392 | { continue; } |
| 393 | if (entity.talkable) |
| 394 | { |
| 395 | items.push({ |
| 396 | label: `Talk to ${targetName}`, |
| 397 | onSelect: () => requestTalk(entity.entityId) |
| 398 | }); |
| 399 | } |
| 400 | if (entity.characterId) |
| 401 | { |
| 402 | items.push({ |
| 403 | label: `${targetName} info`, |
| 404 | onSelect: () => |
| 405 | { |
| 406 | clearAllPendingActions(); |
| 407 | social.inspect(entity); |
| 408 | } |
| 409 | }); |
| 410 | } |
| 411 | items.push({ |
| 412 | label: `Attack ${targetName}`, |
| 413 | onSelect: () => requestAttack(entity.entityId) |
| 414 | }); |
| 415 | } |
| 416 | |
| 417 | for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY)) |
| 418 | { |
| 419 | const name = groundLabel(ground); |
| 420 | items.push({ |
| 421 | label: `Pick up ${name}`, |
| 422 | onSelect: () => requestPickup(ground) |
| 423 | }); |
| 424 | } |
| 425 | |
| 426 | if (isBankTile(view, tile.tileX, tile.tileY)) |
| 427 | { |
| 428 | items.push({ |
| 429 | label: "Bank", |
| 430 | onSelect: () => requestOpenBank(tile.tileX, tile.tileY) |
| 431 | }); |
| 432 | items.push({ |
| 433 | label: "Get Blank Cheques", |
| 434 | onSelect: () => requestOpenBank(tile.tileX, tile.tileY, { getCheques: true }) |
| 435 | }); |
| 436 | } |
| 437 | |
| 438 | if (isCauldronTile(view, tile.tileX, tile.tileY)) |
| 439 | { |
| 440 | items.push({ |
| 441 | label: "Alchemy", |
| 442 | onSelect: () => requestOpenCauldron(tile.tileX, tile.tileY) |
| 443 | }); |
| 444 | } |
| 445 | |
| 446 | if (isFurnaceTile(view, tile.tileX, tile.tileY)) |
| 447 | { |
| 448 | items.push({ |
| 449 | label: "Smelt", |
| 450 | onSelect: () => requestOpenFurnace(tile.tileX, tile.tileY) |
| 451 | }); |
| 452 | } |
| 453 | |
| 454 | if (isOreBoulderTile(view, tile.tileX, tile.tileY)) |
| 455 | { |
| 456 | items.push({ |
| 457 | label: "Mine", |
| 458 | onSelect: () => requestMine(tile.tileX, tile.tileY) |
| 459 | }); |
| 460 | } |
| 461 | |
| 462 | contextMenu.show(event.clientX, event.clientY, items); |
| 463 | } |
| 464 | |
| 465 | function entityLabel (entity) |
| 466 | { |
| 467 | const first = (entity?.firstName ?? "").trim(); |
| 468 | const last = (entity?.lastName ?? "").trim(); |
| 469 | if (first && last) |
| 470 | { return `${first} ${last}`; } |
| 471 | return first || "target"; |
| 472 | } |
| 473 | |
| 474 | function groundLabel (ground) |
| 475 | { |
| 476 | const name = (ground?.displayName ?? "").trim() || "item"; |
| 477 | const qty = ground?.quantity ?? 1; |
| 478 | if (qty <= 1) |
| 479 | { return name; } |
| 480 | return `${name} (${formatQuantity(qty)})`; |
| 481 | } |
| 482 | |
| 483 | function startPlay (payload) |
| 484 | { |
| 485 | playing = true; |
| 486 | overlay.hidden = true; |
| 487 | hud.hidden = false; |
| 488 | document.getElementById("you").textContent = `${payload.firstName} ${payload.lastName} (${session.authSubject})`; |
| 489 | gait = payload.gait ?? GAIT.walk; |
| 490 | document.getElementById("gait").textContent = gait; |
| 491 | staminaUi?.show(); |
| 492 | if (payload.stamina != null) |
| 493 | { staminaUi?.apply(payload.stamina); } |
| 494 | if (!scene) |
| 495 | { |
| 496 | scene = new Scene(0, 0, window.innerWidth, window.innerHeight); |
| 497 | game.loadScene(scene); |
| 498 | useNearestNeighbour(scene, game.images); |
| 499 | view = new WorldView(game, scene, catalog); |
| 500 | const ticker = new GameObject(0, 0, null, 10); |
| 501 | ticker.update = (dt) => |
| 502 | { |
| 503 | const leftPressed = GameInput.mousePressed(GameInput.MouseButtons.Left); |
| 504 | const leftHeld = GameInput.mouseHeld(GameInput.MouseButtons.Left); |
| 505 | if (!leftPressed && !leftHeld) |
| 506 | { leftClickConsumed = false; } |
| 507 | else if (!leftClickConsumed && playing && !uiBlocked()) |
| 508 | { |
| 509 | leftClickConsumed = true; |
| 510 | const canvas = scene.canvas; |
| 511 | const rect = canvas.getBoundingClientRect(); |
| 512 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 513 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 514 | handleLeftClick(mouseX, mouseY); |
| 515 | } |
| 516 | if (GameInput.isPressed(82) && !uiBlocked()) |
| 517 | { |
| 518 | const next = gait === GAIT.run ? GAIT.walk : GAIT.run; |
| 519 | if (next === GAIT.run && (staminaUi?.getCurrent?.() ?? 1) <= 0) |
| 520 | { return; } |
| 521 | socket.send({ type: MessageType.setGait, gait: next }); |
| 522 | // Walk always applies; run waits for authoritative STAMINA (may reject if empty). |
| 523 | if (next === GAIT.walk) |
| 524 | { |
| 525 | gait = next; |
| 526 | document.getElementById("gait").textContent = gait; |
| 527 | } |
| 528 | } |
| 529 | if (playing && !death?.isBlocking()) |
| 530 | { |
| 531 | flushPendingPickup(); |
| 532 | flushPendingInteract(); |
| 533 | social?.updateNametag(view, scene, view.localEntityId); |
| 534 | chat?.update(view, scene); |
| 535 | } |
| 536 | view.update(dt); |
| 537 | }; |
| 538 | scene.addObject(ticker); |
| 539 | window.addEventListener("resize", () => |
| 540 | { |
| 541 | scene.size.x = window.innerWidth; |
| 542 | scene.size.y = window.innerHeight; |
| 543 | scene.g_fixSize(); |
| 544 | useNearestNeighbour(scene, game.images); |
| 545 | }); |
| 546 | window.addEventListener("wheel", (event) => |
| 547 | { |
| 548 | if (!playing || uiBlocked()) |
| 549 | { return; } |
| 550 | view.setZoom(view.zoom + (event.deltaY < 0 ? 0.25 : -0.25)); |
| 551 | document.getElementById("zoom").textContent = `${view.zoom.toFixed(2)}×`; |
| 552 | }, { passive: true }); |
| 553 | scene.canvas.addEventListener("contextmenu", (event) => |
| 554 | { |
| 555 | if (!playing || uiBlocked()) |
| 556 | { return; } |
| 557 | const rect = scene.canvas.getBoundingClientRect(); |
| 558 | handleRightClick(event, event.clientX - rect.left, event.clientY - rect.top); |
| 559 | }); |
| 560 | } |
| 561 | view.setSettings(payload.settings ?? view.settings); |
| 562 | chat?.syncSettings(); |
| 563 | view.worldHour = payload.worldTime?.hour ?? 12; |
| 564 | view.setLocalEntity( |
| 565 | payload.entityId, |
| 566 | payload.tileX, |
| 567 | payload.tileY, |
| 568 | payload.gait, |
| 569 | payload.sex, |
| 570 | payload.firstName, |
| 571 | payload.lastName, |
| 572 | payload.characterId, |
| 573 | payload.layers |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | async function main () |
| 578 | { |
| 579 | catalog = await loadSpriteCatalog(); |
| 580 | await preloadCatalogImages(game, catalog); |
| 581 | applySpriteSheets(game.images, catalog); |
| 582 | social = initSocial({ |
| 583 | socket: { send (payload) { socket.send(payload); } }, |
| 584 | MessageType |
| 585 | }); |
| 586 | contextMenu = initContextMenu(); |
| 587 | inventory = initInventory({ |
| 588 | socket: { send (payload) { socket.send(payload); } }, |
| 589 | MessageType, |
| 590 | contextMenu, |
| 591 | resolveSpriteUrl (name) |
| 592 | { |
| 593 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 594 | }, |
| 595 | getFriends () |
| 596 | { |
| 597 | return social?.getFriends?.() ?? []; |
| 598 | }, |
| 599 | getLocalName () |
| 600 | { |
| 601 | const you = document.getElementById("you")?.textContent ?? ""; |
| 602 | return you.split("(")[0].trim() || "Unknown"; |
| 603 | } |
| 604 | }); |
| 605 | skills = initSkills(); |
| 606 | crafting = initCrafting({ |
| 607 | socket: { send (payload) { socket.send(payload); } }, |
| 608 | MessageType, |
| 609 | getInventorySlots () |
| 610 | { |
| 611 | return inventory?.getInventorySlots?.() ?? []; |
| 612 | }, |
| 613 | getSkillLevel (skillId) |
| 614 | { |
| 615 | return skills?.skillLevel?.(skillId) ?? 1; |
| 616 | } |
| 617 | }); |
| 618 | alchemy = initAlchemy({ |
| 619 | socket: { send (payload) { socket.send(payload); } }, |
| 620 | MessageType, |
| 621 | getInventorySlots () |
| 622 | { |
| 623 | return inventory?.getInventorySlots?.() ?? []; |
| 624 | } |
| 625 | }); |
| 626 | staminaUi = initStamina(); |
| 627 | chat = initChat({ |
| 628 | socket: { send (payload) { socket.send(payload); } }, |
| 629 | MessageType, |
| 630 | getSettings: () => view?.settings |
| 631 | }); |
| 632 | death = initDeath({ |
| 633 | socket: { send (payload) { socket.send(payload); } }, |
| 634 | MessageType |
| 635 | }); |
| 636 | game.startGame(); |
| 637 | |
| 638 | socket = new GameSocket(defaultSocketUrl(), (message) => |
| 639 | { |
| 640 | switch (message.type) |
| 641 | { |
| 642 | case MessageType.session: |
| 643 | session = message; |
| 644 | statusEl.textContent = `Signed in as ${message.authSubject} (stub admin)`; |
| 645 | renderCharacters(message.characters); |
| 646 | break; |
| 647 | case MessageType.characterList: |
| 648 | renderCharacters(message.characters); |
| 649 | if (!playing) |
| 650 | { overlay.hidden = false; } |
| 651 | break; |
| 652 | case MessageType.enterWorld: |
| 653 | startPlay(message); |
| 654 | inventory?.setLocalCharacterId(message.characterId); |
| 655 | break; |
| 656 | case MessageType.chunkSnapshot: |
| 657 | view?.applySnapshot(message); |
| 658 | break; |
| 659 | case MessageType.chunkDelta: |
| 660 | for (const tile of message.tiles ?? []) |
| 661 | { |
| 662 | view?.updateAuthoredTile( |
| 663 | message.chunkX, |
| 664 | message.chunkY, |
| 665 | tile.localX, |
| 666 | tile.localY, |
| 667 | { ground: tile.ground, block: tile.block, light: tile.light } |
| 668 | ); |
| 669 | } |
| 670 | break; |
| 671 | case MessageType.unsubscribeChunk: |
| 672 | view?.removeChunk(message.chunkX, message.chunkY); |
| 673 | break; |
| 674 | case MessageType.entitySpawn: |
| 675 | view?.spawnEntity(message); |
| 676 | break; |
| 677 | case MessageType.entityMove: |
| 678 | view?.moveEntity(message); |
| 679 | if (message.entityId === view?.localEntityId && message.gait) |
| 680 | { |
| 681 | gait = message.gait; |
| 682 | document.getElementById("gait").textContent = gait; |
| 683 | } |
| 684 | break; |
| 685 | case MessageType.entityDespawn: |
| 686 | view?.despawnEntity(message.entityId); |
| 687 | break; |
| 688 | case MessageType.entityAppearance: |
| 689 | view?.setAppearance(message); |
| 690 | break; |
| 691 | case MessageType.groundItemAppear: |
| 692 | view?.upsertGroundItem(message); |
| 693 | break; |
| 694 | case MessageType.groundItemDespawn: |
| 695 | view?.despawnGroundItem(message.groundItemId); |
| 696 | break; |
| 697 | case MessageType.inventory: |
| 698 | inventory?.handleInventory(message); |
| 699 | crafting?.refresh(); |
| 700 | alchemy?.refresh(); |
| 701 | break; |
| 702 | case MessageType.skills: |
| 703 | skills?.render(message.skills); |
| 704 | crafting?.refresh(); |
| 705 | break; |
| 706 | case MessageType.stamina: |
| 707 | staminaUi?.apply(message); |
| 708 | if (message.gait) |
| 709 | { |
| 710 | gait = message.gait; |
| 711 | document.getElementById("gait").textContent = gait; |
| 712 | } |
| 713 | break; |
| 714 | case MessageType.bankSnapshot: |
| 715 | inventory?.handleBank(message); |
| 716 | break; |
| 717 | case MessageType.cauldronSnapshot: |
| 718 | alchemy?.handleSnapshot(message); |
| 719 | break; |
| 720 | case MessageType.pigeonDispatched: |
| 721 | inventory?.handlePigeonDispatched(message); |
| 722 | break; |
| 723 | case MessageType.pigeonDelivered: |
| 724 | inventory?.handlePigeonDelivered(message); |
| 725 | break; |
| 726 | case MessageType.characterSearchResult: |
| 727 | inventory?.handleCharacterSearch(message); |
| 728 | break; |
| 729 | case MessageType.craftResult: |
| 730 | crafting?.handleCraftResult(message); |
| 731 | break; |
| 732 | case MessageType.worldStatus: |
| 733 | if (view) |
| 734 | { |
| 735 | view.setSettings(message.settings ?? view.settings); |
| 736 | view.worldHour = message.worldTime?.hour ?? view.worldHour; |
| 737 | chat?.syncSettings(); |
| 738 | } |
| 739 | break; |
| 740 | case MessageType.chatBubble: |
| 741 | chat?.showBubble(message); |
| 742 | break; |
| 743 | case MessageType.friends: |
| 744 | social?.handleFriends(message); |
| 745 | break; |
| 746 | case MessageType.combatSwing: |
| 747 | view?.spawnCombatSwing(message); |
| 748 | break; |
| 749 | case MessageType.gatherSwing: |
| 750 | case MessageType.tileShove: |
| 751 | view?.spawnGatherSwing(message); |
| 752 | break; |
| 753 | case MessageType.stationOpen: |
| 754 | if (message.stationId) |
| 755 | { crafting?.openStation(message.stationId); } |
| 756 | break; |
| 757 | case MessageType.itemBubble: |
| 758 | chat?.showItemBubble(message); |
| 759 | break; |
| 760 | case MessageType.gatherComplete: |
| 761 | crafting?.refresh(); |
| 762 | break; |
| 763 | case MessageType.combatHit: |
| 764 | view?.updateEntityHealth(message.targetId, message.health, message.maxHealth); |
| 765 | view?.flashDamage(message.targetId); |
| 766 | view?.spawnDamageNumber(message.targetId, message.damage); |
| 767 | break; |
| 768 | case MessageType.playerDied: |
| 769 | clearAllPendingActions(); |
| 770 | contextMenu?.hide(); |
| 771 | death?.show(); |
| 772 | break; |
| 773 | case MessageType.playerRespawned: |
| 774 | death?.hide(); |
| 775 | view?.updateEntityHealth(message.entityId, message.health, message.maxHealth); |
| 776 | if (message.stamina != null || message.maxStamina != null) |
| 777 | { |
| 778 | staminaUi?.apply({ |
| 779 | current: message.stamina ?? staminaUi.getCurrent?.(), |
| 780 | max: message.maxStamina ?? staminaUi.getMax?.() |
| 781 | }); |
| 782 | } |
| 783 | break; |
| 784 | case MessageType.error: |
| 785 | showError(message.message); |
| 786 | social?.showError(message.message); |
| 787 | break; |
| 788 | case MessageType.forceLogout: |
| 789 | returnToMenu(message.reason); |
| 790 | break; |
| 791 | } |
| 792 | }, { |
| 793 | onClose (event) |
| 794 | { |
| 795 | if (playing) |
| 796 | { returnToMenu(event.reason || "Disconnected from server."); } |
| 797 | } |
| 798 | }); |
| 799 | await socket.connect(); |
| 800 | if (typeof window !== "undefined") |
| 801 | { |
| 802 | window.__craftDev = (stationId = CraftingStationId.craftingBench) => |
| 803 | { |
| 804 | crafting?.openStation(stationId); |
| 805 | }; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | function returnToMenu (reason = "") |
| 810 | { |
| 811 | playing = false; |
| 812 | clearAllPendingActions(); |
| 813 | social?.reset(); |
| 814 | chat?.reset(); |
| 815 | inventory?.reset(); |
| 816 | crafting?.reset(); |
| 817 | alchemy?.reset(); |
| 818 | staminaUi?.hide(); |
| 819 | skills?.hide(); |
| 820 | contextMenu?.hide(); |
| 821 | death?.reset(); |
| 822 | if (view) |
| 823 | { |
| 824 | for (const key of [...(view.chunks?.keys?.() ?? [])]) |
| 825 | { |
| 826 | const [chunkX, chunkY] = key.split(",").map(Number); |
| 827 | view.removeChunk(chunkX, chunkY); |
| 828 | } |
| 829 | } |
| 830 | overlay.hidden = false; |
| 831 | hud.hidden = true; |
| 832 | if (reason) |
| 833 | { statusEl.textContent = reason; } |
| 834 | } |
| 835 | |
| 836 | document.getElementById("create-form").addEventListener("submit", (event) => |
| 837 | { |
| 838 | event.preventDefault(); |
| 839 | showError(""); |
| 840 | socket.send({ |
| 841 | type: MessageType.createCharacter, |
| 842 | firstName: document.getElementById("first-name").value, |
| 843 | lastName: document.getElementById("last-name").value, |
| 844 | sex: document.getElementById("sex").value |
| 845 | }); |
| 846 | }); |
| 847 | |
| 848 | document.getElementById("leave").addEventListener("click", () => |
| 849 | { |
| 850 | socket.send({ type: MessageType.leaveWorld }); |
| 851 | returnToMenu(); |
| 852 | }); |
| 853 | |
| 854 | main().catch((error) => |
| 855 | { |
| 856 | showError(error.message); |
| 857 | console.error(error); |
| 858 | }); |
| 859 | |
| 860 | function isBankTile (view, tileX, tileY) |
| 861 | { |
| 862 | return blockTypeAt(view, tileX, tileY) === BlockType.bank; |
| 863 | } |
| 864 | |
| 865 | function isCauldronTile (view, tileX, tileY) |
| 866 | { |
| 867 | return blockTypeAt(view, tileX, tileY) === BlockType.cauldron; |
| 868 | } |
| 869 | |
| 870 | function isFurnaceTile (view, tileX, tileY) |
| 871 | { |
| 872 | return blockTypeAt(view, tileX, tileY) === BlockType.furnace; |
| 873 | } |
| 874 | |
| 875 | function isOreBoulderTile (view, tileX, tileY) |
| 876 | { |
| 877 | const block = blockAt(view, tileX, tileY); |
| 878 | return block?.typeId === BlockType.boulder && isOreBoulderState(block.state); |
| 879 | } |
| 880 | |
| 881 | function blockTypeAt (view, tileX, tileY) |
| 882 | { |
| 883 | return blockAt(view, tileX, tileY)?.typeId ?? null; |
| 884 | } |
| 885 | |
| 886 | function blockAt (view, tileX, tileY) |
| 887 | { |
| 888 | const chunkX = Math.floor(tileX / CHUNK_SIZE); |
| 889 | const chunkY = Math.floor(tileY / CHUNK_SIZE); |
| 890 | const snapshot = view.chunks.get(`${chunkX},${chunkY}`); |
| 891 | if (!snapshot) |
| 892 | { return null; } |
| 893 | const localX = tileX - chunkX * CHUNK_SIZE; |
| 894 | const localY = tileY - chunkY * CHUNK_SIZE; |
| 895 | return snapshot.blocks[localY * CHUNK_SIZE + localX] ?? null; |
| 896 | } |
| 897 | |
| 898 | function chebyshev (ax, ay, bx, by) |
| 899 | { |
| 900 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 901 | } |