| 1 | import { MessageType, GAIT, CHUNK_SIZE, STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js"; |
| 2 | import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js"; |
| 3 | import { BlockType, BLOCK_DEFINITIONS, GROUND_DEFINITIONS, normalizeGround, resourceNodeDisplayName, isChopableTreeBlock, isPickablePlantBlock } from "/shared/definitions/tiles.js"; |
| 4 | import { OVERWORLD_ID } from "/shared/world/dimensions.js"; |
| 5 | import { findPathToNearestGoal } from "/shared/navigation/bfs.js"; |
| 6 | import { GameSocket, defaultSocketUrl } from "../net/socket.js"; |
| 7 | import { fetchAuthStatus, formatAccountLabel, loginUrl, logoutUrl } from "../net/auth.js"; |
| 8 | import { loadSpriteCatalog, preloadCatalogImages, applySpriteSheets, useNearestNeighbour, playImagesFromCatalog } from "../render/sprites.js"; |
| 9 | import { WorldView } from "../render/worldView.js"; |
| 10 | import { initSocial } from "./social.js"; |
| 11 | import { initChat } from "./chat.js"; |
| 12 | import { initInventory } from "./inventory.js"; |
| 13 | import { initSkills } from "./skills.js"; |
| 14 | import { initSpells } from "./spells.js"; |
| 15 | import { initCrafting } from "./crafting.js"; |
| 16 | import { initAlchemy } from "./alchemy.js"; |
| 17 | import { initTipArrows } from "./tipArrows.js"; |
| 18 | import { initStamina } from "./stamina.js"; |
| 19 | import { initHealth } from "./health.js"; |
| 20 | import { initStatusEffects } from "./statusEffects.js"; |
| 21 | import { initContextMenu } from "./contextMenu.js"; |
| 22 | import { initDeath } from "./death.js"; |
| 23 | import { initToasts } from "./toasts.js"; |
| 24 | import { skillLevelUpToastText, skillUnlockMessages } from "/shared/skills/levelUpUnlocks.js"; |
| 25 | import { initQuests } from "./quests.js"; |
| 26 | import { initDialogue } from "./dialogue.js"; |
| 27 | import { initShop } from "./shop.js"; |
| 28 | import { initMarket } from "./market.js"; |
| 29 | import { initBounty } from "./bounty.js"; |
| 30 | import { initBarber } from "./barber.js"; |
| 31 | import { bindHairControls } from "./hairControls.js"; |
| 32 | import { bindCreateClothingControls } from "./createClothingControls.js"; |
| 33 | import { paintCharacterPreview } from "./characterPreview.js"; |
| 34 | import { initGraphicsSettings } from "./graphicsSettings.js"; |
| 35 | import { CraftingStationId } from "/shared/definitions/craftingStations.js"; |
| 36 | import { isOreBoulderState } from "/shared/definitions/materialTiers.js"; |
| 37 | import { formatQuantity } from "/shared/items/formatQuantity.js"; |
| 38 | import { ItemId, itemDefinition } from "/shared/definitions/items.js"; |
| 39 | import { CraftingItemId, TINDERBOX_MAX_CHARGES } from "/shared/definitions/craftingItems.js"; |
| 40 | import { ItemFamilyId } from "/shared/definitions/tierFamilies.js"; |
| 41 | import { countCoinsInSlots } from "/shared/items/inventory.js"; |
| 42 | |
| 43 | const game = new Game(); |
| 44 | const hud = document.getElementById("hud"); |
| 45 | const overlay = document.getElementById("overlay"); |
| 46 | const statusEl = document.getElementById("status"); |
| 47 | const characterList = document.getElementById("character-list"); |
| 48 | const errorEl = document.getElementById("error"); |
| 49 | const assetLoadEl = document.getElementById("asset-load"); |
| 50 | const assetLoadBar = document.getElementById("asset-load-bar"); |
| 51 | const assetLoadLabel = document.getElementById("asset-load-label"); |
| 52 | const createCharacterOpen = document.getElementById("create-character-open"); |
| 53 | const worldTooltip = document.getElementById("world-tooltip"); |
| 54 | |
| 55 | let socket; |
| 56 | let session = null; |
| 57 | let playing = false; |
| 58 | let view = null; |
| 59 | let graphicsSettingsUi = null; |
| 60 | let scene = null; |
| 61 | let gait = GAIT.walk; |
| 62 | let catalog = { grounds: {}, blocks: {} }; |
| 63 | let social; |
| 64 | let chat; |
| 65 | let inventory; |
| 66 | let skills; |
| 67 | let spells; |
| 68 | let crafting; |
| 69 | let alchemy; |
| 70 | let staminaUi; |
| 71 | let healthUi; |
| 72 | let statusEffectsUi; |
| 73 | let contextMenu; |
| 74 | let death; |
| 75 | let toasts; |
| 76 | let questsUi; |
| 77 | let dialogueUi; |
| 78 | let shop; |
| 79 | let playZoomRaf = 0; |
| 80 | let playZoomDelta = 0; |
| 81 | let market; |
| 82 | let bounty; |
| 83 | let barber; |
| 84 | let createHairControls; |
| 85 | let createClothingControls; |
| 86 | let createActiveTab = "appearance"; |
| 87 | let pendingPickup = null; |
| 88 | /** @type {{ kind: "bank" | "doorway" | "cauldron" | "station" | "mine" | "chop", tileX: number, tileY: number, stationId?: string, getCheques?: boolean } | { kind: "talk", targetEntityId: number } | { kind: "market" | "bounty" | "barber", tileX: number, tileY: number } | null } */ |
| 89 | let pendingInteract = null; |
| 90 | /** |
| 91 | * Block/NPC the player currently has a station UI open for. |
| 92 | * Closed automatically when Chebyshev distance exceeds 1. |
| 93 | * @type {null | { kind: "bank" | "station" | "cauldron" | "market" | "bounty" | "barber", tileX: number, tileY: number } | { kind: "shop" | "dialogue", entityId: number }} |
| 94 | */ |
| 95 | let uiFocus = null; |
| 96 | /** Last approach moveTo so we do not re-path every frame (resets server walkCarry). */ |
| 97 | let approachMove = null; |
| 98 | /** Client-tracked follow target; server follow owns gait while this is set. */ |
| 99 | let followTargetId = null; |
| 100 | /** Destination of the active click/approach moveTo for autorun distance checks. */ |
| 101 | let moveDest = null; |
| 102 | /** Entity / tile focus for server-driven approach (attack, cast) — no client moveTo. */ |
| 103 | let autorunFocusEntityId = null; |
| 104 | let autorunFocusTile = null; |
| 105 | /** After stamina hits 0 while autorunning, wait until ≥50% before running again. */ |
| 106 | let autorunWaitingStamina = false; |
| 107 | /** Framework can report mousePressed for two frames; latch so one physical click is one command. */ |
| 108 | let leftClickConsumed = false; |
| 109 | |
| 110 | const AUTORUN_DISTANCE_TILES = 3; |
| 111 | const AUTORUN_STAMINA_RESUME_RATIO = 0.5; |
| 112 | |
| 113 | function sendPlayerMoveTo (tileX, tileY) |
| 114 | { |
| 115 | followTargetId = null; |
| 116 | clearAutorunFocus(); |
| 117 | moveDest = { tileX: tileX | 0, tileY: tileY | 0 }; |
| 118 | socket.send({ type: MessageType.moveTo, tileX: moveDest.tileX, tileY: moveDest.tileY }); |
| 119 | syncAutorun(); |
| 120 | } |
| 121 | |
| 122 | function sendPlayerFollow (targetEntityId) |
| 123 | { |
| 124 | followTargetId = targetEntityId | 0; |
| 125 | moveDest = null; |
| 126 | clearAutorunFocus(); |
| 127 | autorunWaitingStamina = false; |
| 128 | socket.send({ type: MessageType.follow, targetEntityId: followTargetId }); |
| 129 | } |
| 130 | |
| 131 | function clearAutorunFocus () |
| 132 | { |
| 133 | autorunFocusEntityId = null; |
| 134 | autorunFocusTile = null; |
| 135 | } |
| 136 | |
| 137 | function setAutorunFocusEntity (entityId) |
| 138 | { |
| 139 | moveDest = null; |
| 140 | autorunFocusTile = null; |
| 141 | autorunFocusEntityId = entityId | 0; |
| 142 | syncAutorun(); |
| 143 | } |
| 144 | |
| 145 | function setAutorunFocusTile (tileX, tileY) |
| 146 | { |
| 147 | moveDest = null; |
| 148 | autorunFocusEntityId = null; |
| 149 | autorunFocusTile = { tileX: tileX | 0, tileY: tileY | 0 }; |
| 150 | syncAutorun(); |
| 151 | } |
| 152 | |
| 153 | function resolveAutorunDest () |
| 154 | { |
| 155 | if (moveDest) |
| 156 | { return moveDest; } |
| 157 | if (autorunFocusEntityId != null) |
| 158 | { |
| 159 | const target = view?.entities?.get(autorunFocusEntityId); |
| 160 | if (!target) |
| 161 | { return null; } |
| 162 | return { tileX: target.tileX | 0, tileY: target.tileY | 0 }; |
| 163 | } |
| 164 | return autorunFocusTile; |
| 165 | } |
| 166 | |
| 167 | function localEntityIsMoving (local) |
| 168 | { |
| 169 | if (!local) |
| 170 | { return false; } |
| 171 | if (local.lerpStarted != null) |
| 172 | { return true; } |
| 173 | const dx = (local.visualX ?? 0) - (local.targetX ?? local.visualX ?? 0); |
| 174 | const dy = (local.visualY ?? 0) - (local.targetY ?? local.visualY ?? 0); |
| 175 | return (dx * dx + dy * dy) > 0.25; |
| 176 | } |
| 177 | |
| 178 | function requestGaitIfNeeded (next) |
| 179 | { |
| 180 | if (next !== GAIT.walk && next !== GAIT.run) |
| 181 | { return; } |
| 182 | if (next === gait) |
| 183 | { return; } |
| 184 | if (next === GAIT.run && (staminaUi?.getCurrent?.() ?? 0) <= 0) |
| 185 | { return; } |
| 186 | socket.send({ type: MessageType.setGait, gait: next }); |
| 187 | if (next === GAIT.walk) |
| 188 | { |
| 189 | gait = next; |
| 190 | const el = document.getElementById("gait"); |
| 191 | if (el) |
| 192 | { el.textContent = gait; } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | function syncAutorun () |
| 197 | { |
| 198 | if (!playing || followTargetId != null) |
| 199 | { return; } |
| 200 | const settings = graphicsSettingsUi?.getSettings?.() ?? {}; |
| 201 | if (settings.autorun !== "on") |
| 202 | { return; } |
| 203 | const local = view?.entities?.get(view.localEntityId); |
| 204 | const dest = resolveAutorunDest(); |
| 205 | if (!local || !dest) |
| 206 | { return; } |
| 207 | if (moveDest && local.tileX === moveDest.tileX && local.tileY === moveDest.tileY) |
| 208 | { |
| 209 | moveDest = null; |
| 210 | autorunWaitingStamina = false; |
| 211 | requestGaitIfNeeded(GAIT.walk); |
| 212 | return; |
| 213 | } |
| 214 | // Attack / cast approach is server-pathed — only run while tiles are changing. |
| 215 | if (!moveDest && !localEntityIsMoving(local)) |
| 216 | { |
| 217 | requestGaitIfNeeded(GAIT.walk); |
| 218 | return; |
| 219 | } |
| 220 | const dist = chebyshev(local.tileX, local.tileY, dest.tileX, dest.tileY); |
| 221 | const wantRun = dist > AUTORUN_DISTANCE_TILES; |
| 222 | const current = staminaUi?.getCurrent?.() ?? 0; |
| 223 | const max = Math.max(1, staminaUi?.getMax?.() ?? 1); |
| 224 | if (gait === GAIT.run && current <= 0) |
| 225 | { autorunWaitingStamina = true; } |
| 226 | if (autorunWaitingStamina) |
| 227 | { |
| 228 | if (wantRun && current / max >= AUTORUN_STAMINA_RESUME_RATIO) |
| 229 | { |
| 230 | autorunWaitingStamina = false; |
| 231 | requestGaitIfNeeded(GAIT.run); |
| 232 | } |
| 233 | else |
| 234 | { requestGaitIfNeeded(GAIT.walk); } |
| 235 | return; |
| 236 | } |
| 237 | if (wantRun) |
| 238 | { |
| 239 | if (current <= 0) |
| 240 | { |
| 241 | autorunWaitingStamina = true; |
| 242 | requestGaitIfNeeded(GAIT.walk); |
| 243 | } |
| 244 | else |
| 245 | { requestGaitIfNeeded(GAIT.run); } |
| 246 | } |
| 247 | else |
| 248 | { requestGaitIfNeeded(GAIT.walk); } |
| 249 | } |
| 250 | |
| 251 | function consumeWorldClick () |
| 252 | { |
| 253 | leftClickConsumed = true; |
| 254 | } |
| 255 | |
| 256 | function showError (message) |
| 257 | { |
| 258 | errorEl.textContent = message ?? ""; |
| 259 | } |
| 260 | |
| 261 | function setAssetLoading (active, { loaded = 0, total = 0, phase = "" } = {}) |
| 262 | { |
| 263 | if (assetLoadEl) |
| 264 | { assetLoadEl.hidden = !active; } |
| 265 | if (characterList) |
| 266 | { characterList.hidden = active; } |
| 267 | if (createCharacterOpen) |
| 268 | { createCharacterOpen.hidden = active; } |
| 269 | if (!active) |
| 270 | { return; } |
| 271 | if (assetLoadBar && total > 0) |
| 272 | { assetLoadBar.value = Math.round((loaded / total) * 100); } |
| 273 | if (assetLoadLabel) |
| 274 | { |
| 275 | assetLoadLabel.textContent = phase |
| 276 | || (total > 0 ? `Loading assets… ${loaded}/${total}` : "Loading assets…"); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | function renderCharacters (characters) |
| 281 | { |
| 282 | characterList.innerHTML = ""; |
| 283 | for (const character of characters) |
| 284 | { |
| 285 | const row = document.createElement("div"); |
| 286 | row.className = "character-row"; |
| 287 | const pending = character.deletionScheduledAt |
| 288 | ? ` (deleting ${new Date(character.deletionScheduledAt).toLocaleString()})` |
| 289 | : ""; |
| 290 | row.innerHTML = `<strong>${character.firstName} ${character.lastName}</strong> · ${character.sex}${pending}`; |
| 291 | const play = document.createElement("button"); |
| 292 | play.textContent = "Play"; |
| 293 | play.disabled = !!character.deletionScheduledAt; |
| 294 | play.addEventListener("click", () => socket.send({ type: MessageType.selectCharacter, characterId: character.characterId })); |
| 295 | const del = document.createElement("button"); |
| 296 | del.textContent = character.deletionScheduledAt ? "Cancel delete" : "Delete"; |
| 297 | del.addEventListener("click", () => |
| 298 | { |
| 299 | socket.send({ |
| 300 | type: character.deletionScheduledAt ? MessageType.cancelDeleteCharacter : MessageType.scheduleDeleteCharacter, |
| 301 | characterId: character.characterId |
| 302 | }); |
| 303 | }); |
| 304 | row.append(play, del); |
| 305 | characterList.append(row); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | function uiBlocked () |
| 310 | { |
| 311 | return !!(social?.isBlocking() || chat?.isBlocking() || inventory?.isBlocking() |
| 312 | || crafting?.isBlocking() || alchemy?.isBlocking() || shop?.isBlocking() |
| 313 | || market?.isBlocking() || bounty?.isBlocking() || barber?.isBlocking() |
| 314 | || questsUi?.isBlocking() || dialogueUi?.isBlocking() |
| 315 | || contextMenu?.isOpen() || death?.isBlocking() || spells?.isBlocking() |
| 316 | || graphicsSettingsUi?.isBlocking?.() |
| 317 | || pointerOverSpellUi()); |
| 318 | } |
| 319 | |
| 320 | function pointerOverSpellUi () |
| 321 | { |
| 322 | const el = document.elementFromPoint(GameInput.mousePosition.x, GameInput.mousePosition.y); |
| 323 | if (!el) |
| 324 | { return false; } |
| 325 | return !!el.closest("#spells-panel, #spell-favorites-bar, #spells-toggle"); |
| 326 | } |
| 327 | |
| 328 | function applyGraphicsSettings (settings) |
| 329 | { |
| 330 | view?.setGraphicsPrefs(settings); |
| 331 | if (socket && playing) |
| 332 | { |
| 333 | socket.send({ |
| 334 | type: MessageType.setGraphicsSettings, |
| 335 | viewDistance: settings.viewDistance |
| 336 | }); |
| 337 | } |
| 338 | syncAutorun(); |
| 339 | } |
| 340 | |
| 341 | function clearPendingPickup () |
| 342 | { |
| 343 | pendingPickup = null; |
| 344 | } |
| 345 | |
| 346 | function clearPendingInteract () |
| 347 | { |
| 348 | pendingInteract = null; |
| 349 | approachMove = null; |
| 350 | } |
| 351 | |
| 352 | function clearAllPendingActions () |
| 353 | { |
| 354 | clearPendingPickup(); |
| 355 | clearPendingInteract(); |
| 356 | } |
| 357 | |
| 358 | function approachAdjacentTile (tileX, tileY) |
| 359 | { |
| 360 | const local = view.entities.get(view.localEntityId); |
| 361 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 362 | { |
| 363 | approachMove = null; |
| 364 | return true; |
| 365 | } |
| 366 | if (!local) |
| 367 | { return false; } |
| 368 | |
| 369 | const radius = Math.max(1, STATIC_CHUNK_RADIUS | 0) * CHUNK_SIZE; |
| 370 | const path = findPathToNearestGoal( |
| 371 | local.tileX, |
| 372 | local.tileY, |
| 373 | (x, y) => chebyshev(x, y, tileX, tileY) === 1 && isClientApproachable(x, y), |
| 374 | (x, y) => isClientApproachable(x, y), |
| 375 | local.tileX - radius, |
| 376 | local.tileY - radius, |
| 377 | local.tileX + radius, |
| 378 | local.tileY + radius |
| 379 | ); |
| 380 | let best = path.length ? path[path.length - 1] : null; |
| 381 | if (!best) |
| 382 | { |
| 383 | // Fallback if BFS finds nothing (incomplete client map): nearest adjacent guess. |
| 384 | let bestAny = null; |
| 385 | let bestAnyDist = Infinity; |
| 386 | for (let dy = -1; dy <= 1; dy++) |
| 387 | { |
| 388 | for (let dx = -1; dx <= 1; dx++) |
| 389 | { |
| 390 | if (dx === 0 && dy === 0) |
| 391 | { continue; } |
| 392 | const destX = tileX + dx; |
| 393 | const destY = tileY + dy; |
| 394 | const dist = chebyshev(local.tileX, local.tileY, destX, destY); |
| 395 | if (dist < bestAnyDist) |
| 396 | { |
| 397 | bestAnyDist = dist; |
| 398 | bestAny = { tileX: destX, tileY: destY }; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | best = bestAny ? { x: bestAny.tileX, y: bestAny.tileY } : null; |
| 403 | } |
| 404 | if (!best) |
| 405 | { return false; } |
| 406 | const destX = best.x ?? best.tileX; |
| 407 | const destY = best.y ?? best.tileY; |
| 408 | if ( |
| 409 | approachMove |
| 410 | && approachMove.focusX === tileX |
| 411 | && approachMove.focusY === tileY |
| 412 | && approachMove.destX === destX |
| 413 | && approachMove.destY === destY |
| 414 | ) |
| 415 | { return false; } |
| 416 | approachMove = { |
| 417 | focusX: tileX, |
| 418 | focusY: tileY, |
| 419 | destX, |
| 420 | destY |
| 421 | }; |
| 422 | sendPlayerMoveTo(destX, destY); |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | function requestOpenBank (tileX, tileY, { getCheques = false } = {}) |
| 427 | { |
| 428 | clearAllPendingActions(); |
| 429 | pendingInteract = { kind: "bank", tileX, tileY, getCheques: !!getCheques }; |
| 430 | const local = view?.entities.get(view.localEntityId); |
| 431 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 432 | { flushPendingInteract(); } |
| 433 | else |
| 434 | { approachAdjacentTile(tileX, tileY); } |
| 435 | } |
| 436 | |
| 437 | function requestEnterDoorway (tileX, tileY) |
| 438 | { |
| 439 | clearAllPendingActions(); |
| 440 | pendingInteract = { kind: "doorway", tileX, tileY }; |
| 441 | const local = view?.entities.get(view.localEntityId); |
| 442 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 443 | { flushPendingInteract(); } |
| 444 | else |
| 445 | { approachAdjacentTile(tileX, tileY); } |
| 446 | } |
| 447 | |
| 448 | function requestOpenMarket (tileX, tileY) |
| 449 | { |
| 450 | clearAllPendingActions(); |
| 451 | pendingInteract = { kind: "market", tileX, tileY }; |
| 452 | const local = view?.entities.get(view.localEntityId); |
| 453 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 454 | { flushPendingInteract(); } |
| 455 | else |
| 456 | { approachAdjacentTile(tileX, tileY); } |
| 457 | } |
| 458 | |
| 459 | function requestOpenBounty (tileX, tileY) |
| 460 | { |
| 461 | clearAllPendingActions(); |
| 462 | pendingInteract = { kind: "bounty", tileX, tileY }; |
| 463 | const local = view?.entities.get(view.localEntityId); |
| 464 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 465 | { flushPendingInteract(); } |
| 466 | else |
| 467 | { approachAdjacentTile(tileX, tileY); } |
| 468 | } |
| 469 | |
| 470 | function requestOpenBarber (tileX, tileY) |
| 471 | { |
| 472 | clearAllPendingActions(); |
| 473 | pendingInteract = { kind: "barber", tileX, tileY }; |
| 474 | const local = view?.entities.get(view.localEntityId); |
| 475 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 476 | { flushPendingInteract(); } |
| 477 | else |
| 478 | { approachAdjacentTile(tileX, tileY); } |
| 479 | } |
| 480 | |
| 481 | function requestOpenCauldron (tileX, tileY) |
| 482 | { |
| 483 | clearAllPendingActions(); |
| 484 | pendingInteract = { kind: "cauldron", tileX, tileY }; |
| 485 | const local = view?.entities.get(view.localEntityId); |
| 486 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 487 | { flushPendingInteract(); } |
| 488 | else |
| 489 | { approachAdjacentTile(tileX, tileY); } |
| 490 | } |
| 491 | |
| 492 | function requestOpenFurnace (tileX, tileY) |
| 493 | { |
| 494 | requestOpenCraftStation(CraftingStationId.furnace, tileX, tileY); |
| 495 | } |
| 496 | |
| 497 | function requestOpenCraftStation (stationId, tileX, tileY) |
| 498 | { |
| 499 | clearAllPendingActions(); |
| 500 | pendingInteract = { kind: "station", stationId, tileX, tileY }; |
| 501 | const local = view?.entities.get(view.localEntityId); |
| 502 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 503 | { flushPendingInteract(); } |
| 504 | else |
| 505 | { approachAdjacentTile(tileX, tileY); } |
| 506 | } |
| 507 | |
| 508 | function requestStartFire (ground) |
| 509 | { |
| 510 | if (!ground) |
| 511 | { return; } |
| 512 | clearAllPendingActions(); |
| 513 | pendingInteract = { |
| 514 | kind: "start-fire", |
| 515 | groundItemId: ground.groundItemId, |
| 516 | tileX: ground.tileX, |
| 517 | tileY: ground.tileY |
| 518 | }; |
| 519 | const local = view?.entities.get(view.localEntityId); |
| 520 | if (local && chebyshev(local.tileX, local.tileY, ground.tileX, ground.tileY) <= 1) |
| 521 | { flushPendingInteract(); } |
| 522 | else |
| 523 | { approachAdjacentTile(ground.tileX, ground.tileY); } |
| 524 | } |
| 525 | |
| 526 | function requestFish (tileX, tileY) |
| 527 | { |
| 528 | clearAllPendingActions(); |
| 529 | pendingInteract = { kind: "fish", tileX, tileY }; |
| 530 | const local = view?.entities.get(view.localEntityId); |
| 531 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 532 | { flushPendingInteract(); } |
| 533 | else |
| 534 | { approachAdjacentTile(tileX, tileY); } |
| 535 | } |
| 536 | |
| 537 | function requestMine (tileX, tileY) |
| 538 | { |
| 539 | clearAllPendingActions(); |
| 540 | pendingInteract = { kind: "mine", tileX, tileY }; |
| 541 | const local = view?.entities.get(view.localEntityId); |
| 542 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 543 | { flushPendingInteract(); } |
| 544 | else |
| 545 | { approachAdjacentTile(tileX, tileY); } |
| 546 | } |
| 547 | |
| 548 | function requestChop (tileX, tileY) |
| 549 | { |
| 550 | clearAllPendingActions(); |
| 551 | pendingInteract = { kind: "chop", tileX, tileY }; |
| 552 | const local = view?.entities.get(view.localEntityId); |
| 553 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 554 | { flushPendingInteract(); } |
| 555 | else |
| 556 | { approachAdjacentTile(tileX, tileY); } |
| 557 | } |
| 558 | |
| 559 | function requestPick (tileX, tileY) |
| 560 | { |
| 561 | clearAllPendingActions(); |
| 562 | pendingInteract = { kind: "pick", tileX, tileY }; |
| 563 | const local = view?.entities.get(view.localEntityId); |
| 564 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 565 | { flushPendingInteract(); } |
| 566 | else |
| 567 | { approachAdjacentTile(tileX, tileY); } |
| 568 | } |
| 569 | |
| 570 | function requestTalk (targetEntityId, purpose = null) |
| 571 | { |
| 572 | clearAllPendingActions(); |
| 573 | pendingInteract = { kind: "talk", targetEntityId, purpose }; |
| 574 | const target = view?.entities.get(targetEntityId); |
| 575 | if (!target) |
| 576 | { |
| 577 | clearPendingInteract(); |
| 578 | return; |
| 579 | } |
| 580 | if (approachAdjacentTile(target.tileX, target.tileY)) |
| 581 | { flushPendingInteract(); } |
| 582 | } |
| 583 | |
| 584 | function setUiFocus (focus) |
| 585 | { |
| 586 | uiFocus = focus; |
| 587 | } |
| 588 | |
| 589 | function clearUiFocus () |
| 590 | { |
| 591 | uiFocus = null; |
| 592 | } |
| 593 | |
| 594 | /** Close stations, bank, dialogue, and other overlays when the local player dies. */ |
| 595 | function closeCloseablePlayUi () |
| 596 | { |
| 597 | clearUiFocus(); |
| 598 | inventory?.closeBank?.(); |
| 599 | inventory?.closeOverlays?.(); |
| 600 | if (crafting?.isOpen?.()) |
| 601 | { crafting.close(); } |
| 602 | if (alchemy?.isOpen?.()) |
| 603 | { alchemy.close(); } |
| 604 | if (market?.isOpen?.()) |
| 605 | { market.close(); } |
| 606 | if (bounty?.isOpen?.()) |
| 607 | { bounty.close(); } |
| 608 | if (barber?.isOpen?.()) |
| 609 | { barber.close(); } |
| 610 | if (shop?.isOpen?.()) |
| 611 | { shop.close(); } |
| 612 | if (dialogueUi?.isOpen?.()) |
| 613 | { dialogueUi.close(); } |
| 614 | questsUi?.hide?.(); |
| 615 | chat?.close?.(); |
| 616 | social?.closeOverlays?.(); |
| 617 | contextMenu?.hide(); |
| 618 | spells?.handleSpellDisarmed?.(); |
| 619 | graphicsSettingsUi?.close?.(); |
| 620 | } |
| 621 | |
| 622 | /** Close bank / station / NPC UIs when the player walks out of adjacency. */ |
| 623 | function closeOpenUiIfTooFar () |
| 624 | { |
| 625 | if (!uiFocus || !view) |
| 626 | { return; } |
| 627 | const local = view.entities.get(view.localEntityId); |
| 628 | if (!local) |
| 629 | { return; } |
| 630 | |
| 631 | let tooFar = false; |
| 632 | if (uiFocus.entityId != null) |
| 633 | { |
| 634 | const target = view.entities.get(uiFocus.entityId); |
| 635 | tooFar = !target |
| 636 | || chebyshev(local.tileX, local.tileY, target.tileX, target.tileY) > 1; |
| 637 | } |
| 638 | else if (uiFocus.tileX != null && uiFocus.tileY != null) |
| 639 | { |
| 640 | tooFar = chebyshev(local.tileX, local.tileY, uiFocus.tileX, uiFocus.tileY) > 1; |
| 641 | } |
| 642 | if (!tooFar) |
| 643 | { return; } |
| 644 | |
| 645 | const kind = uiFocus.kind; |
| 646 | clearUiFocus(); |
| 647 | switch (kind) |
| 648 | { |
| 649 | case "bank": |
| 650 | if (inventory?.isBankOpen?.()) |
| 651 | { inventory.closeBank(); } |
| 652 | break; |
| 653 | case "station": |
| 654 | if (crafting?.isOpen?.()) |
| 655 | { crafting.close(); } |
| 656 | break; |
| 657 | case "cauldron": |
| 658 | if (alchemy?.isOpen?.()) |
| 659 | { alchemy.close(); } |
| 660 | break; |
| 661 | case "market": |
| 662 | if (market?.isOpen?.()) |
| 663 | { market.close(); } |
| 664 | break; |
| 665 | case "bounty": |
| 666 | if (bounty?.isOpen?.()) |
| 667 | { bounty.close(); } |
| 668 | break; |
| 669 | case "barber": |
| 670 | if (barber?.isOpen?.()) |
| 671 | { barber.close(); } |
| 672 | break; |
| 673 | case "shop": |
| 674 | if (shop?.isOpen?.()) |
| 675 | { shop.close(); } |
| 676 | break; |
| 677 | case "dialogue": |
| 678 | if (dialogueUi?.isOpen?.()) |
| 679 | { dialogueUi.close(); } |
| 680 | break; |
| 681 | default: |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | function flushPendingInteract () |
| 687 | { |
| 688 | if (!pendingInteract || !view) |
| 689 | { return; } |
| 690 | const local = view.entities.get(view.localEntityId); |
| 691 | if (!local) |
| 692 | { return; } |
| 693 | |
| 694 | if (pendingInteract.kind === "bank") |
| 695 | { |
| 696 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 697 | { return; } |
| 698 | setUiFocus({ |
| 699 | kind: "bank", |
| 700 | tileX: pendingInteract.tileX, |
| 701 | tileY: pendingInteract.tileY |
| 702 | }); |
| 703 | const getCheques = !!pendingInteract.getCheques; |
| 704 | socket.send({ type: MessageType.openBank }); |
| 705 | if (getCheques) |
| 706 | { inventory?.getBlankCheques(); } |
| 707 | clearPendingInteract(); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | if (pendingInteract.kind === "doorway") |
| 712 | { |
| 713 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 714 | { return; } |
| 715 | socket.send({ |
| 716 | type: MessageType.enterDoorway, |
| 717 | tileX: pendingInteract.tileX, |
| 718 | tileY: pendingInteract.tileY |
| 719 | }); |
| 720 | clearPendingInteract(); |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | if (pendingInteract.kind === "market") |
| 725 | { |
| 726 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 727 | { return; } |
| 728 | setUiFocus({ |
| 729 | kind: "market", |
| 730 | tileX: pendingInteract.tileX, |
| 731 | tileY: pendingInteract.tileY |
| 732 | }); |
| 733 | socket.send({ type: MessageType.openMarket }); |
| 734 | clearPendingInteract(); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | if (pendingInteract.kind === "bounty") |
| 739 | { |
| 740 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 741 | { return; } |
| 742 | setUiFocus({ |
| 743 | kind: "bounty", |
| 744 | tileX: pendingInteract.tileX, |
| 745 | tileY: pendingInteract.tileY |
| 746 | }); |
| 747 | socket.send({ type: MessageType.openBounty }); |
| 748 | clearPendingInteract(); |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | if (pendingInteract.kind === "barber") |
| 753 | { |
| 754 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 755 | { return; } |
| 756 | setUiFocus({ |
| 757 | kind: "barber", |
| 758 | tileX: pendingInteract.tileX, |
| 759 | tileY: pendingInteract.tileY |
| 760 | }); |
| 761 | socket.send({ type: MessageType.openBarber }); |
| 762 | clearPendingInteract(); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | if (pendingInteract.kind === "cauldron") |
| 767 | { |
| 768 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 769 | { return; } |
| 770 | setUiFocus({ |
| 771 | kind: "cauldron", |
| 772 | tileX: pendingInteract.tileX, |
| 773 | tileY: pendingInteract.tileY |
| 774 | }); |
| 775 | socket.send({ type: MessageType.openCauldron }); |
| 776 | clearPendingInteract(); |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | if (pendingInteract.kind === "station") |
| 781 | { |
| 782 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 783 | { return; } |
| 784 | setUiFocus({ |
| 785 | kind: "station", |
| 786 | tileX: pendingInteract.tileX, |
| 787 | tileY: pendingInteract.tileY |
| 788 | }); |
| 789 | socket.send({ |
| 790 | type: MessageType.openStation, |
| 791 | stationId: pendingInteract.stationId |
| 792 | }); |
| 793 | clearPendingInteract(); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | if (pendingInteract.kind === "mine") |
| 798 | { |
| 799 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 800 | { return; } |
| 801 | socket.send({ |
| 802 | type: MessageType.mine, |
| 803 | tileX: pendingInteract.tileX, |
| 804 | tileY: pendingInteract.tileY |
| 805 | }); |
| 806 | clearPendingInteract(); |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | if (pendingInteract.kind === "start-fire") |
| 811 | { |
| 812 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 813 | { return; } |
| 814 | socket.send({ |
| 815 | type: MessageType.startFire, |
| 816 | groundItemId: pendingInteract.groundItemId |
| 817 | }); |
| 818 | clearPendingInteract(); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | if (pendingInteract.kind === "fish") |
| 823 | { |
| 824 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 825 | { return; } |
| 826 | socket.send({ |
| 827 | type: MessageType.startFish, |
| 828 | tileX: pendingInteract.tileX, |
| 829 | tileY: pendingInteract.tileY |
| 830 | }); |
| 831 | clearPendingInteract(); |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | if (pendingInteract.kind === "chop") |
| 836 | { |
| 837 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 838 | { return; } |
| 839 | socket.send({ |
| 840 | type: MessageType.chop, |
| 841 | tileX: pendingInteract.tileX, |
| 842 | tileY: pendingInteract.tileY |
| 843 | }); |
| 844 | clearPendingInteract(); |
| 845 | return; |
| 846 | } |
| 847 | |
| 848 | if (pendingInteract.kind === "pick") |
| 849 | { |
| 850 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 851 | { return; } |
| 852 | socket.send({ |
| 853 | type: MessageType.pick, |
| 854 | tileX: pendingInteract.tileX, |
| 855 | tileY: pendingInteract.tileY |
| 856 | }); |
| 857 | clearPendingInteract(); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | if (pendingInteract.kind === "talk") |
| 862 | { |
| 863 | const target = view.entities.get(pendingInteract.targetEntityId); |
| 864 | if (!target) |
| 865 | { |
| 866 | clearPendingInteract(); |
| 867 | return; |
| 868 | } |
| 869 | if (chebyshev(local.tileX, local.tileY, target.tileX, target.tileY) > 1) |
| 870 | { |
| 871 | // Target may have moved — keep chasing. |
| 872 | approachAdjacentTile(target.tileX, target.tileY); |
| 873 | return; |
| 874 | } |
| 875 | socket.send({ |
| 876 | type: MessageType.interact, |
| 877 | targetEntityId: pendingInteract.targetEntityId, |
| 878 | purpose: pendingInteract.purpose || undefined |
| 879 | }); |
| 880 | // Shop vs dialogue is decided server-side; both use NPC adjacency. |
| 881 | setUiFocus({ |
| 882 | kind: pendingInteract.purpose === "shop" ? "shop" : "dialogue", |
| 883 | entityId: pendingInteract.targetEntityId |
| 884 | }); |
| 885 | clearPendingInteract(); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | function requestPickup (ground) |
| 890 | { |
| 891 | if (!ground) |
| 892 | { return; } |
| 893 | clearPendingInteract(); |
| 894 | pendingPickup = { |
| 895 | groundItemId: ground.groundItemId, |
| 896 | tileX: ground.tileX, |
| 897 | tileY: ground.tileY |
| 898 | }; |
| 899 | const local = view.entities.get(view.localEntityId); |
| 900 | if (local && local.tileX === ground.tileX && local.tileY === ground.tileY) |
| 901 | { |
| 902 | flushPendingPickup(); |
| 903 | return; |
| 904 | } |
| 905 | sendPlayerMoveTo(ground.tileX, ground.tileY); |
| 906 | } |
| 907 | |
| 908 | function flushPendingPickup () |
| 909 | { |
| 910 | if (!pendingPickup || !view) |
| 911 | { return; } |
| 912 | const item = view.groundItems.get(pendingPickup.groundItemId); |
| 913 | if (!item) |
| 914 | { |
| 915 | clearPendingPickup(); |
| 916 | return; |
| 917 | } |
| 918 | const local = view.entities.get(view.localEntityId); |
| 919 | if (!local) |
| 920 | { return; } |
| 921 | if (local.tileX !== pendingPickup.tileX || local.tileY !== pendingPickup.tileY) |
| 922 | { return; } |
| 923 | socket.send({ type: MessageType.pickupItem, groundItemId: pendingPickup.groundItemId }); |
| 924 | clearPendingPickup(); |
| 925 | } |
| 926 | |
| 927 | function requestAttack (targetEntityId) |
| 928 | { |
| 929 | clearAllPendingActions(); |
| 930 | setAutorunFocusEntity(targetEntityId); |
| 931 | socket.send({ type: MessageType.attack, targetEntityId }); |
| 932 | } |
| 933 | |
| 934 | function isAttackableEntity (entity) |
| 935 | { |
| 936 | if (!entity || entity.untargetable) |
| 937 | { return false; } |
| 938 | if (entity.canFight) |
| 939 | { return true; } |
| 940 | // Snapshots can omit canFight; mobs are still attackable via LMB. |
| 941 | return !!entity.mobTypeId && !entity.talkable; |
| 942 | } |
| 943 | |
| 944 | function canTalkToEntity (entity) |
| 945 | { |
| 946 | return !!(entity && (entity.hasDialogue || entity.talkable || entity.hasShop)); |
| 947 | } |
| 948 | |
| 949 | function isApproachingCombatOrCast () |
| 950 | { |
| 951 | return autorunFocusEntityId != null || autorunFocusTile != null; |
| 952 | } |
| 953 | |
| 954 | function handleLeftClick (mouseX, mouseY) |
| 955 | { |
| 956 | const tile = view.screenToTile(mouseX, mouseY); |
| 957 | const armed = spells?.getArmedSpell?.(); |
| 958 | const approaching = isApproachingCombatOrCast(); |
| 959 | |
| 960 | // Entity-armed: cast only when a valid target is under the cursor. Empty ground |
| 961 | // must fall through so walk cancels cast/attack approach (server moveTo clears it). |
| 962 | if (armed?.target === "entity" && armed.canCast) |
| 963 | { |
| 964 | const snap = nearestSpellEntityAtCursor(mouseX, mouseY); |
| 965 | if (snap && spells.tryCastAtEntity(snap.entityId)) |
| 966 | { |
| 967 | setAutorunFocusEntity(snap.entityId); |
| 968 | return; |
| 969 | } |
| 970 | } |
| 971 | // Tile-armed while approaching: prefer walk-cancel over re-targeting the cast. |
| 972 | else if ( |
| 973 | !approaching |
| 974 | && armed?.target === "tile" |
| 975 | && armed.canCast |
| 976 | && spells.tryCastAtTile(tile.tileX, tile.tileY) |
| 977 | ) |
| 978 | { |
| 979 | setAutorunFocusTile(tile.tileX, tile.tileY); |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | const entity = view.entityAtScreen(mouseX, mouseY); |
| 984 | if (entity && entity.entityId !== view.localEntityId && !entity.untargetable) |
| 985 | { |
| 986 | clearPendingPickup(); |
| 987 | if (entity.characterId) |
| 988 | { |
| 989 | clearPendingInteract(); |
| 990 | social.inspect(entity); |
| 991 | return; |
| 992 | } |
| 993 | // Dialogue (or shop/talkable) wins over attack as the default click. |
| 994 | if (canTalkToEntity(entity)) |
| 995 | { |
| 996 | // Same intent as context-menu Talk / Shop so dialogue pin matches. |
| 997 | const purpose = entity.hasDialogue || (entity.talkable && !entity.hasShop) |
| 998 | ? "talk" |
| 999 | : (entity.hasShop ? "shop" : "talk"); |
| 1000 | requestTalk(entity.entityId, purpose); |
| 1001 | return; |
| 1002 | } |
| 1003 | if (isAttackableEntity(entity)) |
| 1004 | { |
| 1005 | requestAttack(entity.entityId); |
| 1006 | return; |
| 1007 | } |
| 1008 | // Clicked an entity we don't act on — do not fall through to walk (that cancels combat). |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | const ground = view.groundItemAtTile(tile.tileX, tile.tileY); |
| 1013 | if (ground) |
| 1014 | { requestPickup(ground); } |
| 1015 | else if (isBankTile(view, tile.tileX, tile.tileY)) |
| 1016 | { requestOpenBank(tile.tileX, tile.tileY); } |
| 1017 | else if (isDoorwayTile(view, tile.tileX, tile.tileY)) |
| 1018 | { requestEnterDoorway(tile.tileX, tile.tileY); } |
| 1019 | else if (isMarketplaceTile(view, tile.tileX, tile.tileY)) |
| 1020 | { requestOpenMarket(tile.tileX, tile.tileY); } |
| 1021 | else if (isBountyOfficeTile(view, tile.tileX, tile.tileY)) |
| 1022 | { requestOpenBounty(tile.tileX, tile.tileY); } |
| 1023 | else if (isBarberTile(view, tile.tileX, tile.tileY)) |
| 1024 | { requestOpenBarber(tile.tileX, tile.tileY); } |
| 1025 | else if (isCauldronTile(view, tile.tileX, tile.tileY)) |
| 1026 | { requestOpenCauldron(tile.tileX, tile.tileY); } |
| 1027 | else if (isFurnaceTile(view, tile.tileX, tile.tileY)) |
| 1028 | { requestOpenCraftStation(CraftingStationId.furnace, tile.tileX, tile.tileY); } |
| 1029 | else if (isOvenTile(view, tile.tileX, tile.tileY)) |
| 1030 | { requestOpenCraftStation(CraftingStationId.oven, tile.tileX, tile.tileY); } |
| 1031 | else if (isCampfireTile(view, tile.tileX, tile.tileY)) |
| 1032 | { requestOpenCraftStation(CraftingStationId.fireplace, tile.tileX, tile.tileY); } |
| 1033 | else if (isFishingSpotTile(view, tile.tileX, tile.tileY)) |
| 1034 | { requestFish(tile.tileX, tile.tileY); } |
| 1035 | else if (isAnvilTile(view, tile.tileX, tile.tileY)) |
| 1036 | { requestOpenCraftStation(CraftingStationId.anvil, tile.tileX, tile.tileY); } |
| 1037 | else if (isJeweleringStationTile(view, tile.tileX, tile.tileY)) |
| 1038 | { requestOpenCraftStation(CraftingStationId.jeweleringStation, tile.tileX, tile.tileY); } |
| 1039 | else if (isWoodworkingBenchTile(view, tile.tileX, tile.tileY)) |
| 1040 | { requestOpenCraftStation(CraftingStationId.woodworkingBench, tile.tileX, tile.tileY); } |
| 1041 | else if (isCraftingBenchTile(view, tile.tileX, tile.tileY)) |
| 1042 | { requestOpenCraftStation(CraftingStationId.craftingBench, tile.tileX, tile.tileY); } |
| 1043 | else if (isSpinningWheelTile(view, tile.tileX, tile.tileY)) |
| 1044 | { requestOpenCraftStation(CraftingStationId.spinningWheel, tile.tileX, tile.tileY); } |
| 1045 | else if (isLoomTile(view, tile.tileX, tile.tileY)) |
| 1046 | { requestOpenCraftStation(CraftingStationId.loom, tile.tileX, tile.tileY); } |
| 1047 | else if (isTailorBenchTile(view, tile.tileX, tile.tileY)) |
| 1048 | { requestOpenCraftStation(CraftingStationId.tailorBench, tile.tileX, tile.tileY); } |
| 1049 | else if (isLeatherworkingTableTile(view, tile.tileX, tile.tileY)) |
| 1050 | { requestOpenCraftStation(CraftingStationId.leatherworkingStation, tile.tileX, tile.tileY); } |
| 1051 | else if (isOreBoulderTile(view, tile.tileX, tile.tileY)) |
| 1052 | { requestMine(tile.tileX, tile.tileY); } |
| 1053 | else if (isChopableTreeTile(view, tile.tileX, tile.tileY)) |
| 1054 | { requestChop(tile.tileX, tile.tileY); } |
| 1055 | else if (isPickablePlantTile(view, tile.tileX, tile.tileY)) |
| 1056 | { requestPick(tile.tileX, tile.tileY); } |
| 1057 | else |
| 1058 | { |
| 1059 | clearAllPendingActions(); |
| 1060 | sendPlayerMoveTo(tile.tileX, tile.tileY); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | function handleRightClick (event, mouseX, mouseY) |
| 1065 | { |
| 1066 | event.preventDefault(); |
| 1067 | if (spells?.isTargeting?.()) |
| 1068 | { |
| 1069 | spells.cancelTargeting(); |
| 1070 | view?.clearSpellTargetHighlight(); |
| 1071 | return; |
| 1072 | } |
| 1073 | const tile = view.screenToTile(mouseX, mouseY); |
| 1074 | const items = []; |
| 1075 | const walkTileX = tile.tileX; |
| 1076 | const walkTileY = tile.tileY; |
| 1077 | |
| 1078 | items.push({ |
| 1079 | label: "Walk here", |
| 1080 | onSelect: () => |
| 1081 | { |
| 1082 | clearAllPendingActions(); |
| 1083 | sendPlayerMoveTo(walkTileX, walkTileY); |
| 1084 | } |
| 1085 | }); |
| 1086 | |
| 1087 | for (const entity of view.entitiesAtTile(tile.tileX, tile.tileY)) |
| 1088 | { |
| 1089 | if (entity.entityId === view.localEntityId) |
| 1090 | { continue; } |
| 1091 | const targetName = entityLabel(entity); |
| 1092 | items.push({ |
| 1093 | label: `Follow ${targetName}`, |
| 1094 | onSelect: () => |
| 1095 | { |
| 1096 | clearAllPendingActions(); |
| 1097 | sendPlayerFollow(entity.entityId); |
| 1098 | } |
| 1099 | }); |
| 1100 | if (entity.untargetable) |
| 1101 | { continue; } |
| 1102 | if (entity.hasShop) |
| 1103 | { |
| 1104 | items.push({ |
| 1105 | label: `Shop ${targetName}`, |
| 1106 | onSelect: () => requestTalk(entity.entityId, "shop") |
| 1107 | }); |
| 1108 | } |
| 1109 | if (entity.hasDialogue || (entity.talkable && !entity.hasShop)) |
| 1110 | { |
| 1111 | items.push({ |
| 1112 | label: `Talk to ${targetName}`, |
| 1113 | onSelect: () => requestTalk(entity.entityId, "talk") |
| 1114 | }); |
| 1115 | } |
| 1116 | if (entity.characterId) |
| 1117 | { |
| 1118 | items.push({ |
| 1119 | label: `${targetName} info`, |
| 1120 | onSelect: () => |
| 1121 | { |
| 1122 | clearAllPendingActions(); |
| 1123 | social.inspect(entity); |
| 1124 | } |
| 1125 | }); |
| 1126 | } |
| 1127 | items.push({ |
| 1128 | label: `Attack ${targetName}`, |
| 1129 | onSelect: () => requestAttack(entity.entityId) |
| 1130 | }); |
| 1131 | } |
| 1132 | |
| 1133 | for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY)) |
| 1134 | { |
| 1135 | const name = groundLabel(ground); |
| 1136 | items.push({ |
| 1137 | label: `Pick up ${name}`, |
| 1138 | onSelect: () => requestPickup(ground) |
| 1139 | }); |
| 1140 | if (playerHasUsableTinderbox() && isGroundWoodLog(ground)) |
| 1141 | { |
| 1142 | items.push({ |
| 1143 | label: `Start Fire`, |
| 1144 | onSelect: () => requestStartFire(ground) |
| 1145 | }); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if (isBankTile(view, tile.tileX, tile.tileY)) |
| 1150 | { |
| 1151 | items.push({ |
| 1152 | label: "Bank", |
| 1153 | onSelect: () => requestOpenBank(tile.tileX, tile.tileY) |
| 1154 | }); |
| 1155 | items.push({ |
| 1156 | label: "Get Blank Cheques", |
| 1157 | onSelect: () => requestOpenBank(tile.tileX, tile.tileY, { getCheques: true }) |
| 1158 | }); |
| 1159 | } |
| 1160 | |
| 1161 | if (isDoorwayTile(view, tile.tileX, tile.tileY)) |
| 1162 | { |
| 1163 | items.push({ |
| 1164 | label: "Enter", |
| 1165 | onSelect: () => requestEnterDoorway(tile.tileX, tile.tileY) |
| 1166 | }); |
| 1167 | } |
| 1168 | |
| 1169 | if (isMarketplaceTile(view, tile.tileX, tile.tileY)) |
| 1170 | { |
| 1171 | items.push({ |
| 1172 | label: "Central Exchange", |
| 1173 | onSelect: () => requestOpenMarket(tile.tileX, tile.tileY) |
| 1174 | }); |
| 1175 | } |
| 1176 | |
| 1177 | if (isBountyOfficeTile(view, tile.tileX, tile.tileY)) |
| 1178 | { |
| 1179 | items.push({ |
| 1180 | label: "Bounty Office", |
| 1181 | onSelect: () => requestOpenBounty(tile.tileX, tile.tileY) |
| 1182 | }); |
| 1183 | } |
| 1184 | |
| 1185 | if (isBarberTile(view, tile.tileX, tile.tileY)) |
| 1186 | { |
| 1187 | items.push({ |
| 1188 | label: "Barber", |
| 1189 | onSelect: () => requestOpenBarber(tile.tileX, tile.tileY) |
| 1190 | }); |
| 1191 | } |
| 1192 | |
| 1193 | if (isCauldronTile(view, tile.tileX, tile.tileY)) |
| 1194 | { |
| 1195 | items.push({ |
| 1196 | label: "Alchemy", |
| 1197 | onSelect: () => requestOpenCauldron(tile.tileX, tile.tileY) |
| 1198 | }); |
| 1199 | } |
| 1200 | |
| 1201 | if (isFurnaceTile(view, tile.tileX, tile.tileY)) |
| 1202 | { |
| 1203 | items.push({ |
| 1204 | label: "Smelt", |
| 1205 | onSelect: () => requestOpenCraftStation(CraftingStationId.furnace, tile.tileX, tile.tileY) |
| 1206 | }); |
| 1207 | } |
| 1208 | |
| 1209 | if (isOvenTile(view, tile.tileX, tile.tileY)) |
| 1210 | { |
| 1211 | items.push({ |
| 1212 | label: "Cook", |
| 1213 | onSelect: () => requestOpenCraftStation(CraftingStationId.oven, tile.tileX, tile.tileY) |
| 1214 | }); |
| 1215 | } |
| 1216 | |
| 1217 | if (isCampfireTile(view, tile.tileX, tile.tileY)) |
| 1218 | { |
| 1219 | items.push({ |
| 1220 | label: "Cook", |
| 1221 | onSelect: () => requestOpenCraftStation(CraftingStationId.fireplace, tile.tileX, tile.tileY) |
| 1222 | }); |
| 1223 | } |
| 1224 | |
| 1225 | if (isFishingSpotTile(view, tile.tileX, tile.tileY)) |
| 1226 | { |
| 1227 | items.push({ |
| 1228 | label: "Fish", |
| 1229 | onSelect: () => requestFish(tile.tileX, tile.tileY) |
| 1230 | }); |
| 1231 | } |
| 1232 | |
| 1233 | if (isAnvilTile(view, tile.tileX, tile.tileY)) |
| 1234 | { |
| 1235 | items.push({ |
| 1236 | label: "Smith", |
| 1237 | onSelect: () => requestOpenCraftStation(CraftingStationId.anvil, tile.tileX, tile.tileY) |
| 1238 | }); |
| 1239 | } |
| 1240 | |
| 1241 | if (isJeweleringStationTile(view, tile.tileX, tile.tileY)) |
| 1242 | { |
| 1243 | items.push({ |
| 1244 | label: "Jewel", |
| 1245 | onSelect: () => requestOpenCraftStation(CraftingStationId.jeweleringStation, tile.tileX, tile.tileY) |
| 1246 | }); |
| 1247 | } |
| 1248 | |
| 1249 | if (isWoodworkingBenchTile(view, tile.tileX, tile.tileY)) |
| 1250 | { |
| 1251 | items.push({ |
| 1252 | label: "Woodwork", |
| 1253 | onSelect: () => requestOpenCraftStation(CraftingStationId.woodworkingBench, tile.tileX, tile.tileY) |
| 1254 | }); |
| 1255 | } |
| 1256 | |
| 1257 | if (isCraftingBenchTile(view, tile.tileX, tile.tileY)) |
| 1258 | { |
| 1259 | items.push({ |
| 1260 | label: "Craft", |
| 1261 | onSelect: () => requestOpenCraftStation(CraftingStationId.craftingBench, tile.tileX, tile.tileY) |
| 1262 | }); |
| 1263 | } |
| 1264 | |
| 1265 | if (isSpinningWheelTile(view, tile.tileX, tile.tileY)) |
| 1266 | { |
| 1267 | items.push({ |
| 1268 | label: "Spin", |
| 1269 | onSelect: () => requestOpenCraftStation(CraftingStationId.spinningWheel, tile.tileX, tile.tileY) |
| 1270 | }); |
| 1271 | } |
| 1272 | |
| 1273 | if (isLoomTile(view, tile.tileX, tile.tileY)) |
| 1274 | { |
| 1275 | items.push({ |
| 1276 | label: "Weave", |
| 1277 | onSelect: () => requestOpenCraftStation(CraftingStationId.loom, tile.tileX, tile.tileY) |
| 1278 | }); |
| 1279 | } |
| 1280 | |
| 1281 | if (isTailorBenchTile(view, tile.tileX, tile.tileY)) |
| 1282 | { |
| 1283 | items.push({ |
| 1284 | label: "Tailor", |
| 1285 | onSelect: () => requestOpenCraftStation(CraftingStationId.tailorBench, tile.tileX, tile.tileY) |
| 1286 | }); |
| 1287 | } |
| 1288 | |
| 1289 | if (isLeatherworkingTableTile(view, tile.tileX, tile.tileY)) |
| 1290 | { |
| 1291 | items.push({ |
| 1292 | label: "Leatherwork", |
| 1293 | onSelect: () => requestOpenCraftStation(CraftingStationId.leatherworkingStation, tile.tileX, tile.tileY) |
| 1294 | }); |
| 1295 | } |
| 1296 | |
| 1297 | if (isOreBoulderTile(view, tile.tileX, tile.tileY)) |
| 1298 | { |
| 1299 | items.push({ |
| 1300 | label: "Mine", |
| 1301 | onSelect: () => requestMine(tile.tileX, tile.tileY) |
| 1302 | }); |
| 1303 | } |
| 1304 | |
| 1305 | if (isChopableTreeTile(view, tile.tileX, tile.tileY)) |
| 1306 | { |
| 1307 | items.push({ |
| 1308 | label: "Chop", |
| 1309 | onSelect: () => requestChop(tile.tileX, tile.tileY) |
| 1310 | }); |
| 1311 | } |
| 1312 | |
| 1313 | if (isPickablePlantTile(view, tile.tileX, tile.tileY)) |
| 1314 | { |
| 1315 | items.push({ |
| 1316 | label: "Pick", |
| 1317 | onSelect: () => requestPick(tile.tileX, tile.tileY) |
| 1318 | }); |
| 1319 | } |
| 1320 | |
| 1321 | contextMenu.show(event.clientX, event.clientY, items); |
| 1322 | } |
| 1323 | |
| 1324 | function entityLabel (entity) |
| 1325 | { |
| 1326 | const first = (entity?.firstName ?? "").trim(); |
| 1327 | const last = (entity?.lastName ?? "").trim(); |
| 1328 | const name = first && last ? `${first} ${last}` : (first || "target"); |
| 1329 | const level = entity?.level | 0; |
| 1330 | if (entity?.mobTypeId && level > 0) |
| 1331 | { return `Lv.${level} ${name}`; } |
| 1332 | return name; |
| 1333 | } |
| 1334 | |
| 1335 | function groundLabel (ground) |
| 1336 | { |
| 1337 | const name = (ground?.displayName ?? "").trim() || "item"; |
| 1338 | const qty = ground?.quantity ?? 1; |
| 1339 | if (qty <= 1) |
| 1340 | { return name; } |
| 1341 | return `${name} (${formatQuantity(qty)})`; |
| 1342 | } |
| 1343 | |
| 1344 | function startPlay (payload) |
| 1345 | { |
| 1346 | playing = true; |
| 1347 | overlay.hidden = true; |
| 1348 | hud.hidden = false; |
| 1349 | const settingsToggle = document.getElementById("settings-toggle"); |
| 1350 | if (settingsToggle) |
| 1351 | { settingsToggle.hidden = false; } |
| 1352 | document.getElementById("you").textContent = `${payload.firstName} ${payload.lastName} (${session.authSubject})`; |
| 1353 | gait = payload.gait ?? GAIT.walk; |
| 1354 | document.getElementById("gait").textContent = gait; |
| 1355 | staminaUi?.show(); |
| 1356 | healthUi?.show(); |
| 1357 | statusEffectsUi?.show(); |
| 1358 | if (payload.stamina != null) |
| 1359 | { staminaUi?.apply(payload.stamina); } |
| 1360 | if (payload.health != null || payload.maxHealth != null) |
| 1361 | { |
| 1362 | healthUi?.apply({ |
| 1363 | current: payload.health ?? healthUi.getCurrent?.(), |
| 1364 | max: payload.maxHealth ?? healthUi.getMax?.() |
| 1365 | }); |
| 1366 | } |
| 1367 | if (!scene) |
| 1368 | { |
| 1369 | scene = new Scene(0, 0, window.innerWidth, window.innerHeight); |
| 1370 | game.loadScene(scene); |
| 1371 | useNearestNeighbour(scene, game.images); |
| 1372 | } |
| 1373 | if (!view) |
| 1374 | { |
| 1375 | view = new WorldView(game, scene, catalog); |
| 1376 | graphicsSettingsUi?.apply(); |
| 1377 | const ticker = new GameObject(0, 0, null, 10); |
| 1378 | ticker.update = (dt) => |
| 1379 | { |
| 1380 | const leftPressed = GameInput.mousePressed(GameInput.MouseButtons.Left); |
| 1381 | const leftHeld = GameInput.mouseHeld(GameInput.MouseButtons.Left); |
| 1382 | if (!leftPressed && !leftHeld) |
| 1383 | { leftClickConsumed = false; } |
| 1384 | else if (!leftClickConsumed && playing && !uiBlocked()) |
| 1385 | { |
| 1386 | leftClickConsumed = true; |
| 1387 | const canvas = scene.canvas; |
| 1388 | const rect = canvas.getBoundingClientRect(); |
| 1389 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 1390 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 1391 | handleLeftClick(mouseX, mouseY); |
| 1392 | } |
| 1393 | if (GameInput.isPressed(82) && !uiBlocked()) |
| 1394 | { |
| 1395 | const next = gait === GAIT.run ? GAIT.walk : GAIT.run; |
| 1396 | if (next === GAIT.run && (staminaUi?.getCurrent?.() ?? 1) <= 0) |
| 1397 | { return; } |
| 1398 | socket.send({ type: MessageType.setGait, gait: next }); |
| 1399 | // Walk always applies; run waits for authoritative STAMINA (may reject if empty). |
| 1400 | if (next === GAIT.walk) |
| 1401 | { |
| 1402 | gait = next; |
| 1403 | document.getElementById("gait").textContent = gait; |
| 1404 | } |
| 1405 | } |
| 1406 | if (playing && !death?.isBlocking()) |
| 1407 | { |
| 1408 | flushPendingPickup(); |
| 1409 | flushPendingInteract(); |
| 1410 | syncAutorun(); |
| 1411 | social?.updateNametag(view, scene, view.localEntityId); |
| 1412 | chat?.update(view, scene); |
| 1413 | updateSpellTargetHighlight(); |
| 1414 | updateWorldTooltip(); |
| 1415 | } |
| 1416 | else |
| 1417 | { |
| 1418 | hideWorldTooltip(); |
| 1419 | view?.clearSpellTargetHighlight(); |
| 1420 | } |
| 1421 | view.update(dt); |
| 1422 | }; |
| 1423 | scene.addObject(ticker); |
| 1424 | window.addEventListener("resize", () => |
| 1425 | { |
| 1426 | scene.size.x = window.innerWidth; |
| 1427 | scene.size.y = window.innerHeight; |
| 1428 | scene.g_fixSize(); |
| 1429 | useNearestNeighbour(scene, game.images); |
| 1430 | }); |
| 1431 | window.addEventListener("wheel", (event) => |
| 1432 | { |
| 1433 | if (!playing || uiBlocked()) |
| 1434 | { return; } |
| 1435 | playZoomDelta += event.deltaY < 0 ? 0.25 : -0.25; |
| 1436 | if (playZoomRaf) |
| 1437 | { return; } |
| 1438 | playZoomRaf = requestAnimationFrame(() => |
| 1439 | { |
| 1440 | playZoomRaf = 0; |
| 1441 | if (!view || !playZoomDelta) |
| 1442 | { return; } |
| 1443 | view.setZoom(view.zoom + playZoomDelta); |
| 1444 | playZoomDelta = 0; |
| 1445 | document.getElementById("zoom").textContent = `${view.zoom.toFixed(2)}×`; |
| 1446 | }); |
| 1447 | }, { passive: true }); |
| 1448 | scene.canvas.addEventListener("contextmenu", (event) => |
| 1449 | { |
| 1450 | if (!playing || uiBlocked()) |
| 1451 | { return; } |
| 1452 | const rect = scene.canvas.getBoundingClientRect(); |
| 1453 | handleRightClick(event, event.clientX - rect.left, event.clientY - rect.top); |
| 1454 | }); |
| 1455 | } |
| 1456 | view.setSettings(payload.settings ?? view.settings); |
| 1457 | chat?.syncSettings(); |
| 1458 | view.worldHour = payload.worldTime?.hour ?? 12; |
| 1459 | view.setDimensionId(payload.dimensionId ?? OVERWORLD_ID); |
| 1460 | view.setDimensionModes(payload.lightingMode, payload.seasonMode); |
| 1461 | view.setWorldSeason(payload.worldTime?.season); |
| 1462 | view.setLocalEntity( |
| 1463 | payload.entityId, |
| 1464 | payload.tileX, |
| 1465 | payload.tileY, |
| 1466 | payload.gait, |
| 1467 | payload.sex, |
| 1468 | payload.firstName, |
| 1469 | payload.lastName, |
| 1470 | payload.characterId, |
| 1471 | payload.layers, |
| 1472 | payload.health, |
| 1473 | payload.maxHealth, |
| 1474 | payload.skullValue |
| 1475 | ); |
| 1476 | social?.setLocalIdentity?.({ |
| 1477 | accountId: payload.accountId, |
| 1478 | characterId: payload.characterId |
| 1479 | }); |
| 1480 | graphicsSettingsUi?.apply(); |
| 1481 | } |
| 1482 | |
| 1483 | async function main () |
| 1484 | { |
| 1485 | const authStatus = await fetchAuthStatus(); |
| 1486 | updateAuthUi(authStatus); |
| 1487 | if (!authStatus.authenticated) |
| 1488 | { |
| 1489 | statusEl.textContent = "Sign in to play Questward."; |
| 1490 | showError(""); |
| 1491 | return; |
| 1492 | } |
| 1493 | |
| 1494 | setAssetLoading(true, { phase: "Loading catalog…" }); |
| 1495 | catalog = await loadSpriteCatalog(); |
| 1496 | const assetTotal = playImagesFromCatalog(catalog).length; |
| 1497 | setAssetLoading(true, { loaded: 0, total: assetTotal, phase: "Loading assets…" }); |
| 1498 | await preloadCatalogImages(game, catalog, { |
| 1499 | onProgress ({ loaded, total }) |
| 1500 | { |
| 1501 | setAssetLoading(true, { loaded, total }); |
| 1502 | } |
| 1503 | }); |
| 1504 | applySpriteSheets(game.images, catalog); |
| 1505 | if (!scene) |
| 1506 | { |
| 1507 | scene = new Scene(0, 0, window.innerWidth, window.innerHeight); |
| 1508 | game.loadScene(scene); |
| 1509 | } |
| 1510 | setAssetLoading(false); |
| 1511 | statusEl.textContent = "Connecting…"; |
| 1512 | createHairControls = bindHairControls({ |
| 1513 | root: document.getElementById("create-hair"), |
| 1514 | resolveSpriteUrl (name) |
| 1515 | { |
| 1516 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 1517 | }, |
| 1518 | getSex: () => document.getElementById("sex")?.value ?? "male", |
| 1519 | getBag: () => null, |
| 1520 | hideHeadgear: true, |
| 1521 | onChange () |
| 1522 | { |
| 1523 | if (createActiveTab === "clothing") |
| 1524 | { void refreshCreateClothingPreview(); } |
| 1525 | } |
| 1526 | }); |
| 1527 | createClothingControls = bindCreateClothingControls({ |
| 1528 | root: document.getElementById("create-clothing"), |
| 1529 | onChange () |
| 1530 | { |
| 1531 | if (createActiveTab === "clothing") |
| 1532 | { void refreshCreateClothingPreview(); } |
| 1533 | } |
| 1534 | }); |
| 1535 | initCreateTabs(); |
| 1536 | document.getElementById("sex")?.addEventListener("change", () => |
| 1537 | { |
| 1538 | createHairControls?.fillStyles(); |
| 1539 | void createHairControls?.refreshPreview(); |
| 1540 | if (createActiveTab === "clothing") |
| 1541 | { void refreshCreateClothingPreview(); } |
| 1542 | }); |
| 1543 | void createHairControls?.refreshPreview(); |
| 1544 | void refreshCreateClothingPreview(); |
| 1545 | graphicsSettingsUi = initGraphicsSettings({ |
| 1546 | onChange: applyGraphicsSettings, |
| 1547 | onSuppressWorldClick: consumeWorldClick |
| 1548 | }); |
| 1549 | social = initSocial({ |
| 1550 | socket: { send (payload) { socket.send(payload); } }, |
| 1551 | MessageType, |
| 1552 | resolveSpriteUrl (name) |
| 1553 | { |
| 1554 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 1555 | } |
| 1556 | }); |
| 1557 | contextMenu = initContextMenu({ onSuppressWorldClick: consumeWorldClick }); |
| 1558 | const tipArrows = initTipArrows({ |
| 1559 | socket: { send (payload) { socket.send(payload); } }, |
| 1560 | MessageType, |
| 1561 | getInventoryBag () |
| 1562 | { |
| 1563 | return inventory?.getBag?.() ?? null; |
| 1564 | } |
| 1565 | }); |
| 1566 | inventory = initInventory({ |
| 1567 | socket: { send (payload) { socket.send(payload); } }, |
| 1568 | MessageType, |
| 1569 | contextMenu, |
| 1570 | tipArrows, |
| 1571 | resolveSpriteUrl (name) |
| 1572 | { |
| 1573 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 1574 | }, |
| 1575 | getFriends () |
| 1576 | { |
| 1577 | return social?.getFriends?.() ?? []; |
| 1578 | }, |
| 1579 | getLocalName () |
| 1580 | { |
| 1581 | const you = document.getElementById("you")?.textContent ?? ""; |
| 1582 | return you.split("(")[0].trim() || "Unknown"; |
| 1583 | }, |
| 1584 | getSoftClaims () |
| 1585 | { |
| 1586 | return alchemy?.listSoftClaims?.() ?? []; |
| 1587 | }, |
| 1588 | knowsSpell (spellId) |
| 1589 | { |
| 1590 | return !!spells?.knowsSpell?.(spellId); |
| 1591 | } |
| 1592 | }); |
| 1593 | skills = initSkills({ |
| 1594 | getEquipmentSlots () |
| 1595 | { |
| 1596 | return inventory?.getEquipment?.() ?? {}; |
| 1597 | }, |
| 1598 | getSettings () |
| 1599 | { |
| 1600 | return view?.settings ?? null; |
| 1601 | } |
| 1602 | }); |
| 1603 | spells = initSpells({ |
| 1604 | socket: { send (payload) { socket.send(payload); } }, |
| 1605 | MessageType, |
| 1606 | getSkillLevel (skillId) |
| 1607 | { |
| 1608 | return skills?.skillLevel?.(skillId) ?? 1; |
| 1609 | } |
| 1610 | }); |
| 1611 | crafting = initCrafting({ |
| 1612 | socket: { send (payload) { socket.send(payload); } }, |
| 1613 | MessageType, |
| 1614 | getInventorySlots () |
| 1615 | { |
| 1616 | return inventory?.getInventorySlots?.() ?? []; |
| 1617 | }, |
| 1618 | getSkillLevel (skillId) |
| 1619 | { |
| 1620 | return skills?.skillLevel?.(skillId) ?? 1; |
| 1621 | } |
| 1622 | }); |
| 1623 | alchemy = initAlchemy({ |
| 1624 | socket: { send (payload) { socket.send(payload); } }, |
| 1625 | MessageType, |
| 1626 | getInventorySlots () |
| 1627 | { |
| 1628 | return inventory?.getInventorySlots?.() ?? []; |
| 1629 | } |
| 1630 | }); |
| 1631 | alchemy?.setOnSoftClaimsChanged?.(() => inventory?.refreshBagDisplay?.()); |
| 1632 | staminaUi = initStamina(); |
| 1633 | healthUi = initHealth(); |
| 1634 | statusEffectsUi = initStatusEffects(); |
| 1635 | toasts = initToasts(); |
| 1636 | questsUi = initQuests({ |
| 1637 | socket: { send (payload) { socket.send(payload); } }, |
| 1638 | MessageType, |
| 1639 | toasts |
| 1640 | }); |
| 1641 | dialogueUi = initDialogue({ |
| 1642 | socket: { send (payload) { socket.send(payload); } }, |
| 1643 | MessageType, |
| 1644 | getEntity (id) |
| 1645 | { |
| 1646 | if (id === "local") |
| 1647 | { return view?.entities.get(view.localEntityId) ?? null; } |
| 1648 | return view?.entities.get(id) ?? null; |
| 1649 | }, |
| 1650 | captureSpeakerIcon (entityId, size = 64) |
| 1651 | { |
| 1652 | return view?.captureEntityPortrait?.(entityId, { size }) ?? null; |
| 1653 | } |
| 1654 | }); |
| 1655 | shop = initShop({ |
| 1656 | socket: { send (payload) { socket.send(payload); } }, |
| 1657 | MessageType, |
| 1658 | onSuppressWorldClick: consumeWorldClick, |
| 1659 | getCoinCount () |
| 1660 | { |
| 1661 | return countCoinsInSlots(inventory?.getInventorySlots?.() ?? []); |
| 1662 | }, |
| 1663 | getInventorySlots () |
| 1664 | { |
| 1665 | return inventory?.getInventorySlots?.() ?? []; |
| 1666 | } |
| 1667 | }); |
| 1668 | market = initMarket({ |
| 1669 | socket: { send (payload) { socket.send(payload); } }, |
| 1670 | MessageType, |
| 1671 | onSuppressWorldClick: consumeWorldClick, |
| 1672 | getCoinCount () |
| 1673 | { |
| 1674 | return countCoinsInSlots(inventory?.getInventorySlots?.() ?? []); |
| 1675 | }, |
| 1676 | getInventorySlots () |
| 1677 | { |
| 1678 | return inventory?.getInventorySlots?.() ?? []; |
| 1679 | }, |
| 1680 | getLocalCharacterId () |
| 1681 | { |
| 1682 | return inventory?.getLocalCharacterId?.() ?? null; |
| 1683 | } |
| 1684 | }); |
| 1685 | bounty = initBounty({ |
| 1686 | socket: { send (payload) { socket.send(payload); } }, |
| 1687 | MessageType, |
| 1688 | onSuppressWorldClick: consumeWorldClick, |
| 1689 | resolveSpriteUrl (name) |
| 1690 | { |
| 1691 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 1692 | } |
| 1693 | }); |
| 1694 | barber = initBarber({ |
| 1695 | socket: { send (payload) { socket.send(payload); } }, |
| 1696 | MessageType, |
| 1697 | onSuppressWorldClick: consumeWorldClick, |
| 1698 | resolveSpriteUrl (name) |
| 1699 | { |
| 1700 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 1701 | }, |
| 1702 | getInventoryBag () |
| 1703 | { |
| 1704 | return { |
| 1705 | equipment: { slots: inventory?.getEquipment?.() ?? {} }, |
| 1706 | inventory: inventory?.getInventorySlots?.() ?? [] |
| 1707 | }; |
| 1708 | } |
| 1709 | }); |
| 1710 | chat = initChat({ |
| 1711 | socket: { send (payload) { socket.send(payload); } }, |
| 1712 | MessageType, |
| 1713 | getSettings: () => view?.settings |
| 1714 | }); |
| 1715 | death = initDeath({ |
| 1716 | socket: { send (payload) { socket.send(payload); } }, |
| 1717 | MessageType |
| 1718 | }); |
| 1719 | game.startGame(); |
| 1720 | useNearestNeighbour(scene, game.images); |
| 1721 | |
| 1722 | socket = new GameSocket(defaultSocketUrl(), (message) => |
| 1723 | { |
| 1724 | switch (message.type) |
| 1725 | { |
| 1726 | case MessageType.session: |
| 1727 | session = message; |
| 1728 | statusEl.textContent = `Signed in as ${message.username ?? message.authSubject}${message.isAdmin ? " · admin" : ""}`; |
| 1729 | renderCharacters(message.characters); |
| 1730 | break; |
| 1731 | case MessageType.characterList: |
| 1732 | renderCharacters(message.characters); |
| 1733 | if (!playing) |
| 1734 | { overlay.hidden = false; } |
| 1735 | break; |
| 1736 | case MessageType.enterWorld: |
| 1737 | closeCreateModal(); |
| 1738 | startPlay(message); |
| 1739 | inventory?.setLocalCharacterId(message.characterId); |
| 1740 | toasts?.applyRestartStatus?.(message.restartStatus); |
| 1741 | socket.send({ type: MessageType.requestQuests }); |
| 1742 | break; |
| 1743 | case MessageType.chunkSnapshot: |
| 1744 | view?.applySnapshot(message); |
| 1745 | break; |
| 1746 | case MessageType.dimensionChanged: |
| 1747 | if (view) |
| 1748 | { |
| 1749 | view.setDimensionId(message.dimensionId); |
| 1750 | view.setDimensionModes(message.lightingMode, message.seasonMode); |
| 1751 | view.clearAllChunks(); |
| 1752 | if (message.tileX != null && message.tileY != null && view.localEntityId != null) |
| 1753 | { |
| 1754 | view.moveEntity({ |
| 1755 | entityId: view.localEntityId, |
| 1756 | tileX: message.tileX, |
| 1757 | tileY: message.tileY |
| 1758 | }); |
| 1759 | } |
| 1760 | } |
| 1761 | closeOpenUiIfTooFar(); |
| 1762 | break; |
| 1763 | case MessageType.chunkDelta: |
| 1764 | for (const tile of message.tiles ?? []) |
| 1765 | { |
| 1766 | view?.updateAuthoredTile( |
| 1767 | message.chunkX, |
| 1768 | message.chunkY, |
| 1769 | tile.localX, |
| 1770 | tile.localY, |
| 1771 | { ground: tile.ground, block: tile.block, light: tile.light } |
| 1772 | ); |
| 1773 | } |
| 1774 | break; |
| 1775 | case MessageType.unsubscribeChunk: |
| 1776 | view?.removeChunk(message.chunkX, message.chunkY); |
| 1777 | break; |
| 1778 | case MessageType.entitySpawn: |
| 1779 | view?.spawnEntity(message); |
| 1780 | break; |
| 1781 | case MessageType.entityMove: |
| 1782 | view?.moveEntity(message); |
| 1783 | if (message.entityId === view?.localEntityId) |
| 1784 | { |
| 1785 | if (message.gait) |
| 1786 | { |
| 1787 | gait = message.gait; |
| 1788 | document.getElementById("gait").textContent = gait; |
| 1789 | } |
| 1790 | closeOpenUiIfTooFar(); |
| 1791 | } |
| 1792 | else if ( |
| 1793 | uiFocus?.entityId != null |
| 1794 | && message.entityId === uiFocus.entityId |
| 1795 | ) |
| 1796 | { closeOpenUiIfTooFar(); } |
| 1797 | break; |
| 1798 | case MessageType.entityDespawn: |
| 1799 | if (message.entityId === autorunFocusEntityId) |
| 1800 | { clearAutorunFocus(); } |
| 1801 | view?.despawnEntity(message.entityId); |
| 1802 | break; |
| 1803 | case MessageType.entityAppearance: |
| 1804 | view?.setAppearance(message); |
| 1805 | break; |
| 1806 | case MessageType.entitySkull: |
| 1807 | view?.updateEntitySkull(message.entityId, message.skullValue); |
| 1808 | break; |
| 1809 | case MessageType.groundItemAppear: |
| 1810 | view?.upsertGroundItem(message); |
| 1811 | break; |
| 1812 | case MessageType.groundItemDespawn: |
| 1813 | view?.despawnGroundItem(message.groundItemId); |
| 1814 | break; |
| 1815 | case MessageType.inventory: |
| 1816 | inventory?.handleInventory(message); |
| 1817 | skills?.refresh?.(); |
| 1818 | crafting?.refresh(); |
| 1819 | alchemy?.refresh(); |
| 1820 | shop?.refresh(); |
| 1821 | market?.refresh(); |
| 1822 | break; |
| 1823 | case MessageType.skills: |
| 1824 | skills?.render(message.skills); |
| 1825 | crafting?.refresh(); |
| 1826 | spells?.refreshCastGates?.(); |
| 1827 | break; |
| 1828 | case MessageType.spells: |
| 1829 | case MessageType.spellsUpdated: |
| 1830 | spells?.render(message); |
| 1831 | break; |
| 1832 | case MessageType.spellArmed: |
| 1833 | spells?.handleSpellArmed(message); |
| 1834 | break; |
| 1835 | case MessageType.spellDisarmed: |
| 1836 | spells?.handleSpellDisarmed(); |
| 1837 | break; |
| 1838 | case MessageType.spellProjectile: |
| 1839 | view?.spawnSpellProjectileFx(message); |
| 1840 | if (message.casterId === view?.localEntityId) |
| 1841 | { clearAutorunFocus(); } |
| 1842 | break; |
| 1843 | case MessageType.spellFizzle: |
| 1844 | view?.spawnSpellFizzleFx(message); |
| 1845 | if (message.casterId === view?.localEntityId) |
| 1846 | { |
| 1847 | clearAutorunFocus(); |
| 1848 | toasts?.show("Failed to cast spell"); |
| 1849 | } |
| 1850 | break; |
| 1851 | case MessageType.spellCastFlash: |
| 1852 | // Cast-flash dynamic lights skipped (lightmap rebake cost); Light spell uses spellLight*. |
| 1853 | if (message.casterId === view?.localEntityId) |
| 1854 | { clearAutorunFocus(); } |
| 1855 | break; |
| 1856 | case MessageType.spellLightAppear: |
| 1857 | case MessageType.spellLightUpdate: |
| 1858 | view?.setSpellLight(message); |
| 1859 | break; |
| 1860 | case MessageType.spellLightRemove: |
| 1861 | view?.removeSpellLight(message); |
| 1862 | break; |
| 1863 | case MessageType.spellDarkAppear: |
| 1864 | case MessageType.spellDarkUpdate: |
| 1865 | view?.setSpellDark(message); |
| 1866 | break; |
| 1867 | case MessageType.spellDarkRemove: |
| 1868 | view?.removeSpellDark(message); |
| 1869 | break; |
| 1870 | case MessageType.stamina: |
| 1871 | staminaUi?.apply(message); |
| 1872 | if (message.gait) |
| 1873 | { |
| 1874 | gait = message.gait; |
| 1875 | document.getElementById("gait").textContent = gait; |
| 1876 | if (gait === GAIT.walk && (staminaUi?.getCurrent?.() ?? 0) <= 0) |
| 1877 | { autorunWaitingStamina = true; } |
| 1878 | } |
| 1879 | syncAutorun(); |
| 1880 | break; |
| 1881 | case MessageType.statusEffects: |
| 1882 | view?.setLocalBlind(!!message.blind); |
| 1883 | statusEffectsUi?.apply(message); |
| 1884 | break; |
| 1885 | case MessageType.bankSnapshot: |
| 1886 | inventory?.handleBank(message); |
| 1887 | break; |
| 1888 | case MessageType.cauldronSnapshot: |
| 1889 | if (message.open === false && uiFocus?.kind === "cauldron") |
| 1890 | { clearUiFocus(); } |
| 1891 | alchemy?.handleSnapshot(message); |
| 1892 | break; |
| 1893 | case MessageType.pigeonDispatched: |
| 1894 | inventory?.handlePigeonDispatched(message); |
| 1895 | break; |
| 1896 | case MessageType.pigeonDelivered: |
| 1897 | inventory?.handlePigeonDelivered(message); |
| 1898 | break; |
| 1899 | case MessageType.characterSearchResult: |
| 1900 | inventory?.handleCharacterSearch(message); |
| 1901 | break; |
| 1902 | case MessageType.craftResult: |
| 1903 | crafting?.handleCraftResult(message); |
| 1904 | break; |
| 1905 | case MessageType.worldStatus: |
| 1906 | if (view) |
| 1907 | { |
| 1908 | view.setSettings(message.settings ?? view.settings); |
| 1909 | view.worldHour = message.worldTime?.hour ?? view.worldHour; |
| 1910 | view.setWorldSeason(message.worldTime?.season); |
| 1911 | chat?.syncSettings(); |
| 1912 | } |
| 1913 | skills?.refresh?.(); |
| 1914 | toasts?.applyRestartStatus?.(message.restartStatus); |
| 1915 | break; |
| 1916 | case MessageType.restartStatus: |
| 1917 | toasts?.applyRestartStatus?.(message.restartStatus); |
| 1918 | break; |
| 1919 | case MessageType.chatBubble: |
| 1920 | chat?.showBubble(message); |
| 1921 | break; |
| 1922 | case MessageType.friends: |
| 1923 | social?.handleFriends(message); |
| 1924 | break; |
| 1925 | case MessageType.combatSwing: |
| 1926 | view?.spawnCombatSwing(message); |
| 1927 | break; |
| 1928 | case MessageType.combatProjectile: |
| 1929 | view?.spawnPhysicalProjectileFx({ |
| 1930 | ...message, |
| 1931 | sprite: "fx:projectile:arrow" |
| 1932 | }); |
| 1933 | break; |
| 1934 | case MessageType.combatNoArrows: |
| 1935 | toasts?.show("Out of arrows!"); |
| 1936 | break; |
| 1937 | case MessageType.gatherSwing: |
| 1938 | case MessageType.tileShove: |
| 1939 | view?.spawnGatherSwing(message); |
| 1940 | break; |
| 1941 | case MessageType.stationOpen: |
| 1942 | if (message.stationId) |
| 1943 | { crafting?.openStation(message.stationId); } |
| 1944 | break; |
| 1945 | case MessageType.shopOpen: |
| 1946 | if (message.npcEntityId != null) |
| 1947 | { setUiFocus({ kind: "shop", entityId: message.npcEntityId | 0 }); } |
| 1948 | shop?.handleShopOpen(message); |
| 1949 | break; |
| 1950 | case MessageType.shopClosed: |
| 1951 | if (uiFocus?.kind === "shop") |
| 1952 | { clearUiFocus(); } |
| 1953 | shop?.handleShopClosed(message); |
| 1954 | break; |
| 1955 | case MessageType.marketOpen: |
| 1956 | market?.handleOpen(message); |
| 1957 | break; |
| 1958 | case MessageType.marketClosed: |
| 1959 | if (uiFocus?.kind === "market") |
| 1960 | { clearUiFocus(); } |
| 1961 | market?.handleClosed(message); |
| 1962 | break; |
| 1963 | case MessageType.marketTrade: |
| 1964 | market?.handleTrade(message); |
| 1965 | toasts?.show(`Trade: ${message.quantity} @ ${message.unitPrice} coins`); |
| 1966 | break; |
| 1967 | case MessageType.bountySnapshot: |
| 1968 | bounty?.handleSnapshot(message); |
| 1969 | break; |
| 1970 | case MessageType.closeBounty: |
| 1971 | if (uiFocus?.kind === "bounty") |
| 1972 | { clearUiFocus(); } |
| 1973 | bounty?.handleClosed(message); |
| 1974 | break; |
| 1975 | case MessageType.characterBounty: |
| 1976 | bounty?.handleCharacterBounty(message); |
| 1977 | social?.handleCharacterBounty(message); |
| 1978 | break; |
| 1979 | case MessageType.barberSnapshot: |
| 1980 | barber?.handleSnapshot(message); |
| 1981 | break; |
| 1982 | case MessageType.closeBarber: |
| 1983 | if (uiFocus?.kind === "barber") |
| 1984 | { clearUiFocus(); } |
| 1985 | barber?.handleClosed(message); |
| 1986 | break; |
| 1987 | case MessageType.itemBubble: |
| 1988 | chat?.showItemBubble(message); |
| 1989 | break; |
| 1990 | case MessageType.gatherComplete: |
| 1991 | crafting?.refresh(); |
| 1992 | break; |
| 1993 | case MessageType.combatHit: |
| 1994 | { |
| 1995 | const applyHit = () => |
| 1996 | { |
| 1997 | view?.updateEntityHealth(message.targetId, message.health, message.maxHealth); |
| 1998 | view?.flashDamage(message.targetId); |
| 1999 | view?.spawnCombatKnockback?.(message); |
| 2000 | view?.spawnDamageNumber(message.targetId, message.damage); |
| 2001 | if (message.targetId === view?.localEntityId) |
| 2002 | { |
| 2003 | healthUi?.apply({ |
| 2004 | current: message.health, |
| 2005 | max: message.maxHealth |
| 2006 | }); |
| 2007 | } |
| 2008 | }; |
| 2009 | if (!view?.deferCombatHit?.(message, applyHit)) |
| 2010 | { applyHit(); } |
| 2011 | break; |
| 2012 | } |
| 2013 | case MessageType.combatMiss: |
| 2014 | view?.spawnCombatDodge?.(message); |
| 2015 | view?.markProjectileMiss?.(message); |
| 2016 | break; |
| 2017 | case MessageType.combatBlock: |
| 2018 | { |
| 2019 | const applyBlock = () => |
| 2020 | { |
| 2021 | view?.flashBlock?.(message.targetId); |
| 2022 | if (message.health != null) |
| 2023 | { view?.updateEntityHealth(message.targetId, message.health, message.maxHealth); } |
| 2024 | }; |
| 2025 | if (!view?.deferCombatHit?.(message, applyBlock)) |
| 2026 | { applyBlock(); } |
| 2027 | break; |
| 2028 | } |
| 2029 | case MessageType.entityHealth: |
| 2030 | view?.updateEntityHealth(message.entityId, message.health, message.maxHealth); |
| 2031 | if (message.entityId === view?.localEntityId) |
| 2032 | { |
| 2033 | healthUi?.apply({ |
| 2034 | current: message.health, |
| 2035 | max: message.maxHealth |
| 2036 | }); |
| 2037 | } |
| 2038 | break; |
| 2039 | case MessageType.skillLevelUp: |
| 2040 | view?.spawnSkillLevelUp(message.entityId, message); |
| 2041 | if (message.entityId === view?.localEntityId) |
| 2042 | { |
| 2043 | const level = message.level | 0; |
| 2044 | const previousLevel = message.previousLevel == null |
| 2045 | ? Math.max(0, level - 1) |
| 2046 | : message.previousLevel | 0; |
| 2047 | const unlockCount = skillUnlockMessages(message.skillId, previousLevel, level).length; |
| 2048 | toasts?.show(skillLevelUpToastText(message.skillId, level, previousLevel), { |
| 2049 | durationMs: 3500 + unlockCount * 1600 |
| 2050 | }); |
| 2051 | } |
| 2052 | break; |
| 2053 | case MessageType.playerDied: |
| 2054 | clearAllPendingActions(); |
| 2055 | clearAutorunFocus(); |
| 2056 | contextMenu?.hide(); |
| 2057 | spells?.handleSpellDisarmed?.(); |
| 2058 | closeCloseablePlayUi(); |
| 2059 | death?.show(); |
| 2060 | if (message.entityId === view?.localEntityId) |
| 2061 | { |
| 2062 | healthUi?.apply({ current: 0, max: healthUi.getMax?.() }); |
| 2063 | view?.updateEntityHealth(message.entityId, 0, healthUi.getMax?.()); |
| 2064 | } |
| 2065 | break; |
| 2066 | case MessageType.playerRespawned: |
| 2067 | death?.hide(); |
| 2068 | // Drop deferred killing-blow health=0 that may still be queued on a projectile. |
| 2069 | view?.cancelDeferredCombatHits?.(message.entityId); |
| 2070 | view?.updateEntityHealth(message.entityId, message.health, message.maxHealth); |
| 2071 | if (message.skullValue != null) |
| 2072 | { view?.updateEntitySkull(message.entityId, message.skullValue); } |
| 2073 | if (message.entityId === view?.localEntityId) |
| 2074 | { |
| 2075 | healthUi?.apply({ |
| 2076 | current: message.health, |
| 2077 | max: message.maxHealth |
| 2078 | }); |
| 2079 | } |
| 2080 | if (message.stamina != null || message.maxStamina != null) |
| 2081 | { |
| 2082 | staminaUi?.apply({ |
| 2083 | current: message.stamina ?? staminaUi.getCurrent?.(), |
| 2084 | max: message.maxStamina ?? staminaUi.getMax?.() |
| 2085 | }); |
| 2086 | } |
| 2087 | break; |
| 2088 | case MessageType.error: |
| 2089 | if (playing) |
| 2090 | { toasts?.show(message.message); } |
| 2091 | else |
| 2092 | { |
| 2093 | const createPanel = document.getElementById("create-panel"); |
| 2094 | const createError = document.getElementById("create-error"); |
| 2095 | if (createPanel && !createPanel.hidden && createError) |
| 2096 | { createError.textContent = message.message ?? ""; } |
| 2097 | else |
| 2098 | { showError(message.message); } |
| 2099 | } |
| 2100 | social?.showError(message.message); |
| 2101 | break; |
| 2102 | case MessageType.toast: |
| 2103 | if (playing) |
| 2104 | { toasts?.show(message.message); } |
| 2105 | break; |
| 2106 | case MessageType.questsSnapshot: |
| 2107 | questsUi?.applySnapshot(message); |
| 2108 | break; |
| 2109 | case MessageType.questStepComplete: |
| 2110 | questsUi?.handleStepComplete(message); |
| 2111 | break; |
| 2112 | case MessageType.dialogueStep: |
| 2113 | if (message.npcEntityId != null) |
| 2114 | { setUiFocus({ kind: "dialogue", entityId: message.npcEntityId | 0 }); } |
| 2115 | dialogueUi?.handleStep(message); |
| 2116 | break; |
| 2117 | case MessageType.dialogueChoices: |
| 2118 | if (message.npcEntityId != null) |
| 2119 | { setUiFocus({ kind: "dialogue", entityId: message.npcEntityId | 0 }); } |
| 2120 | dialogueUi?.handleChoices(message); |
| 2121 | break; |
| 2122 | case MessageType.dialogueClose: |
| 2123 | if (uiFocus?.kind === "dialogue") |
| 2124 | { clearUiFocus(); } |
| 2125 | dialogueUi?.handleClose(message); |
| 2126 | break; |
| 2127 | case MessageType.forceLogout: |
| 2128 | returnToMenu(message.reason); |
| 2129 | break; |
| 2130 | } |
| 2131 | }, { |
| 2132 | onClose (event) |
| 2133 | { |
| 2134 | if (playing) |
| 2135 | { returnToMenu(event.reason || "Disconnected from server."); } |
| 2136 | } |
| 2137 | }); |
| 2138 | await socket.connect(); |
| 2139 | if (typeof window !== "undefined") |
| 2140 | { |
| 2141 | window.__craftDev = (stationId = CraftingStationId.craftingBench) => |
| 2142 | { |
| 2143 | crafting?.openStation(stationId); |
| 2144 | }; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | function returnToMenu (reason = "") |
| 2149 | { |
| 2150 | playing = false; |
| 2151 | followTargetId = null; |
| 2152 | moveDest = null; |
| 2153 | clearAutorunFocus(); |
| 2154 | autorunWaitingStamina = false; |
| 2155 | clearAllPendingActions(); |
| 2156 | clearUiFocus(); |
| 2157 | social?.reset(); |
| 2158 | chat?.reset(); |
| 2159 | inventory?.reset(); |
| 2160 | crafting?.reset(); |
| 2161 | alchemy?.reset(); |
| 2162 | staminaUi?.hide(); |
| 2163 | healthUi?.hide(); |
| 2164 | statusEffectsUi?.hide(); |
| 2165 | skills?.hide(); |
| 2166 | spells?.hide(); |
| 2167 | contextMenu?.hide(); |
| 2168 | death?.reset(); |
| 2169 | shop?.close({ notifyServer: false }); |
| 2170 | bounty?.close(); |
| 2171 | barber?.close(); |
| 2172 | questsUi?.reset(); |
| 2173 | dialogueUi?.reset(); |
| 2174 | hideWorldTooltip(); |
| 2175 | if (view) |
| 2176 | { view.clearPlaySession(); } |
| 2177 | overlay.hidden = false; |
| 2178 | hud.hidden = true; |
| 2179 | const settingsToggle = document.getElementById("settings-toggle"); |
| 2180 | if (settingsToggle) |
| 2181 | { settingsToggle.hidden = true; } |
| 2182 | graphicsSettingsUi?.close(); |
| 2183 | if (reason) |
| 2184 | { statusEl.textContent = reason; } |
| 2185 | } |
| 2186 | |
| 2187 | document.getElementById("create-character-open")?.addEventListener("click", () => |
| 2188 | { |
| 2189 | showError(""); |
| 2190 | const createError = document.getElementById("create-error"); |
| 2191 | if (createError) |
| 2192 | { createError.textContent = ""; } |
| 2193 | document.getElementById("create-backdrop").hidden = false; |
| 2194 | document.getElementById("create-panel").hidden = false; |
| 2195 | setCreateTab("appearance"); |
| 2196 | void createHairControls?.refreshPreview(); |
| 2197 | }); |
| 2198 | |
| 2199 | function closeCreateModal () |
| 2200 | { |
| 2201 | document.getElementById("create-backdrop").hidden = true; |
| 2202 | document.getElementById("create-panel").hidden = true; |
| 2203 | } |
| 2204 | |
| 2205 | document.getElementById("create-close")?.addEventListener("click", closeCreateModal); |
| 2206 | document.getElementById("create-backdrop")?.addEventListener("click", closeCreateModal); |
| 2207 | |
| 2208 | document.getElementById("create-form").addEventListener("submit", (event) => |
| 2209 | { |
| 2210 | event.preventDefault(); |
| 2211 | showError(""); |
| 2212 | const createError = document.getElementById("create-error"); |
| 2213 | if (createError) |
| 2214 | { createError.textContent = ""; } |
| 2215 | socket.send({ |
| 2216 | type: MessageType.createCharacter, |
| 2217 | firstName: document.getElementById("first-name").value, |
| 2218 | lastName: document.getElementById("last-name").value, |
| 2219 | sex: document.getElementById("sex").value, |
| 2220 | hair: createHairControls?.currentHair?.() ?? null, |
| 2221 | clothing: createClothingControls?.currentClothing?.() ?? null |
| 2222 | }); |
| 2223 | }); |
| 2224 | |
| 2225 | function initCreateTabs () |
| 2226 | { |
| 2227 | const tabs = document.querySelectorAll(".create-tab"); |
| 2228 | for (const tab of tabs) |
| 2229 | { |
| 2230 | tab.addEventListener("click", () => setCreateTab(tab.dataset.createTab)); |
| 2231 | } |
| 2232 | setCreateTab("appearance"); |
| 2233 | } |
| 2234 | |
| 2235 | function setCreateTab (name) |
| 2236 | { |
| 2237 | createActiveTab = name === "clothing" ? "clothing" : "appearance"; |
| 2238 | for (const tab of document.querySelectorAll(".create-tab")) |
| 2239 | { |
| 2240 | const on = tab.dataset.createTab === createActiveTab; |
| 2241 | tab.classList.toggle("active", on); |
| 2242 | tab.setAttribute("aria-selected", on ? "true" : "false"); |
| 2243 | } |
| 2244 | for (const pane of document.querySelectorAll("[data-create-pane]")) |
| 2245 | { pane.hidden = pane.dataset.createPane !== createActiveTab; } |
| 2246 | if (createActiveTab === "clothing") |
| 2247 | { void refreshCreateClothingPreview(); } |
| 2248 | else |
| 2249 | { void createHairControls?.refreshPreview(); } |
| 2250 | } |
| 2251 | |
| 2252 | async function refreshCreateClothingPreview () |
| 2253 | { |
| 2254 | const canvas = document.getElementById("create-clothing-preview"); |
| 2255 | if (!canvas || !createClothingControls) |
| 2256 | { return; } |
| 2257 | try |
| 2258 | { |
| 2259 | await paintCharacterPreview(canvas, { |
| 2260 | sex: document.getElementById("sex")?.value ?? "male", |
| 2261 | bag: createClothingControls.previewBag(), |
| 2262 | hair: createHairControls?.currentHair?.() ?? null, |
| 2263 | hideHeadgear: true, |
| 2264 | resolveSpriteUrl (name) |
| 2265 | { |
| 2266 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 2267 | }, |
| 2268 | scale: 4 |
| 2269 | }); |
| 2270 | } |
| 2271 | catch (error) |
| 2272 | { console.warn(error); } |
| 2273 | } |
| 2274 | |
| 2275 | document.getElementById("leave").addEventListener("click", () => |
| 2276 | { |
| 2277 | socket.send({ type: MessageType.leaveWorld }); |
| 2278 | returnToMenu(); |
| 2279 | }); |
| 2280 | |
| 2281 | main().catch((error) => |
| 2282 | { |
| 2283 | showError(error.message); |
| 2284 | console.error(error); |
| 2285 | }); |
| 2286 | |
| 2287 | function isBankTile (view, tileX, tileY) |
| 2288 | { |
| 2289 | return blockTypeAt(view, tileX, tileY) === BlockType.bank; |
| 2290 | } |
| 2291 | |
| 2292 | function isDoorwayTile (view, tileX, tileY) |
| 2293 | { |
| 2294 | return blockTypeAt(view, tileX, tileY) === BlockType.doorway; |
| 2295 | } |
| 2296 | |
| 2297 | function isMarketplaceTile (view, tileX, tileY) |
| 2298 | { |
| 2299 | return blockTypeAt(view, tileX, tileY) === BlockType.marketplace; |
| 2300 | } |
| 2301 | |
| 2302 | function isBountyOfficeTile (view, tileX, tileY) |
| 2303 | { |
| 2304 | return blockTypeAt(view, tileX, tileY) === BlockType.bountyOffice; |
| 2305 | } |
| 2306 | |
| 2307 | function isBarberTile (view, tileX, tileY) |
| 2308 | { |
| 2309 | return blockTypeAt(view, tileX, tileY) === BlockType.barber; |
| 2310 | } |
| 2311 | |
| 2312 | function isCauldronTile (view, tileX, tileY) |
| 2313 | { |
| 2314 | return blockTypeAt(view, tileX, tileY) === BlockType.cauldron; |
| 2315 | } |
| 2316 | |
| 2317 | function isFurnaceTile (view, tileX, tileY) |
| 2318 | { |
| 2319 | return blockTypeAt(view, tileX, tileY) === BlockType.furnace; |
| 2320 | } |
| 2321 | |
| 2322 | function isOvenTile (view, tileX, tileY) |
| 2323 | { |
| 2324 | return blockTypeAt(view, tileX, tileY) === BlockType.oven; |
| 2325 | } |
| 2326 | |
| 2327 | function isCampfireTile (view, tileX, tileY) |
| 2328 | { |
| 2329 | return blockTypeAt(view, tileX, tileY) === BlockType.campfire; |
| 2330 | } |
| 2331 | |
| 2332 | function isFishingSpotTile (view, tileX, tileY) |
| 2333 | { |
| 2334 | const block = blockAt(view, tileX, tileY); |
| 2335 | if (!block || block.typeId !== BlockType.fishingSpot) |
| 2336 | { return false; } |
| 2337 | return block.state !== "exhausted"; |
| 2338 | } |
| 2339 | |
| 2340 | function isAnvilTile (view, tileX, tileY) |
| 2341 | { |
| 2342 | return blockTypeAt(view, tileX, tileY) === BlockType.anvil; |
| 2343 | } |
| 2344 | |
| 2345 | function isJeweleringStationTile (view, tileX, tileY) |
| 2346 | { |
| 2347 | return blockTypeAt(view, tileX, tileY) === BlockType.jeweleringStation; |
| 2348 | } |
| 2349 | |
| 2350 | function isGroundWoodLog (ground) |
| 2351 | { |
| 2352 | const def = itemDefinition(ground?.itemId ?? ground?.stack?.itemId); |
| 2353 | return def?.familyId === ItemFamilyId.log; |
| 2354 | } |
| 2355 | |
| 2356 | function playerHasUsableTinderbox () |
| 2357 | { |
| 2358 | const slots = inventory?.getInventorySlots?.() ?? []; |
| 2359 | for (const slot of slots) |
| 2360 | { |
| 2361 | if (!slot || slot.itemId !== CraftingItemId.tinderbox) |
| 2362 | { continue; } |
| 2363 | const charges = slot.instance?.kind === "tinderbox" |
| 2364 | ? (slot.instance.charges | 0) |
| 2365 | : TINDERBOX_MAX_CHARGES; |
| 2366 | if (charges > 0) |
| 2367 | { return true; } |
| 2368 | } |
| 2369 | return false; |
| 2370 | } |
| 2371 | |
| 2372 | function isWoodworkingBenchTile (view, tileX, tileY) |
| 2373 | { |
| 2374 | return blockTypeAt(view, tileX, tileY) === BlockType.woodworkingBench; |
| 2375 | } |
| 2376 | |
| 2377 | function isCraftingBenchTile (view, tileX, tileY) |
| 2378 | { |
| 2379 | return blockTypeAt(view, tileX, tileY) === BlockType.craftingBench; |
| 2380 | } |
| 2381 | |
| 2382 | function isSpinningWheelTile (view, tileX, tileY) |
| 2383 | { |
| 2384 | return blockTypeAt(view, tileX, tileY) === BlockType.spinningWheel; |
| 2385 | } |
| 2386 | |
| 2387 | function isLoomTile (view, tileX, tileY) |
| 2388 | { |
| 2389 | return blockTypeAt(view, tileX, tileY) === BlockType.loom; |
| 2390 | } |
| 2391 | |
| 2392 | function isTailorBenchTile (view, tileX, tileY) |
| 2393 | { |
| 2394 | return blockTypeAt(view, tileX, tileY) === BlockType.tailorBench; |
| 2395 | } |
| 2396 | |
| 2397 | function isLeatherworkingTableTile (view, tileX, tileY) |
| 2398 | { |
| 2399 | return blockTypeAt(view, tileX, tileY) === BlockType.leatherworkingTable; |
| 2400 | } |
| 2401 | |
| 2402 | function isOreBoulderTile (view, tileX, tileY) |
| 2403 | { |
| 2404 | const block = blockAt(view, tileX, tileY); |
| 2405 | return block?.typeId === BlockType.boulder && isOreBoulderState(block.state); |
| 2406 | } |
| 2407 | |
| 2408 | function isChopableTreeTile (view, tileX, tileY) |
| 2409 | { |
| 2410 | return isChopableTreeBlock(blockAt(view, tileX, tileY)); |
| 2411 | } |
| 2412 | |
| 2413 | function isPickablePlantTile (view, tileX, tileY) |
| 2414 | { |
| 2415 | return isPickablePlantBlock(blockAt(view, tileX, tileY)); |
| 2416 | } |
| 2417 | |
| 2418 | const SPELL_ENTITY_SNAP_TILES = 2; |
| 2419 | |
| 2420 | function updateSpellTargetHighlight () |
| 2421 | { |
| 2422 | if (!view || !playing || uiBlocked() || !spells?.isTargeting?.()) |
| 2423 | { |
| 2424 | view?.clearSpellTargetHighlight(); |
| 2425 | return; |
| 2426 | } |
| 2427 | const canvas = scene?.canvas; |
| 2428 | if (!canvas) |
| 2429 | { return; } |
| 2430 | const rect = canvas.getBoundingClientRect(); |
| 2431 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 2432 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 2433 | if (mouseX < 0 || mouseY < 0 || mouseX >= rect.width || mouseY >= rect.height) |
| 2434 | { |
| 2435 | view.clearSpellTargetHighlight(); |
| 2436 | return; |
| 2437 | } |
| 2438 | const armed = spells.getArmedSpell?.(); |
| 2439 | const local = view.entities.get(view.localEntityId); |
| 2440 | if (armed?.target === "entity") |
| 2441 | { |
| 2442 | const target = nearestSpellEntityAtCursor(mouseX, mouseY); |
| 2443 | if (!target) |
| 2444 | { |
| 2445 | view.clearSpellTargetHighlight(); |
| 2446 | return; |
| 2447 | } |
| 2448 | const inRange = !local |
| 2449 | || chebyshev(local.tileX, local.tileY, target.tileX, target.tileY) <= COMBAT_RANGE_TILES; |
| 2450 | view.setSpellEntityTargetHighlight(target.entityId, inRange); |
| 2451 | return; |
| 2452 | } |
| 2453 | const tile = view.screenToTile(mouseX, mouseY); |
| 2454 | const inRange = !local |
| 2455 | || chebyshev(local.tileX, local.tileY, tile.tileX, tile.tileY) <= COMBAT_RANGE_TILES; |
| 2456 | view.setSpellTargetHighlight(tile.tileX, tile.tileY, inRange); |
| 2457 | } |
| 2458 | |
| 2459 | /** |
| 2460 | * Nearest spell-targetable entity within {@link SPELL_ENTITY_SNAP_TILES} of the |
| 2461 | * cursor tile (uses both authoritative and visual tile for moving mobs). |
| 2462 | */ |
| 2463 | function nearestSpellEntityAtCursor (mouseX, mouseY) |
| 2464 | { |
| 2465 | if (!view) |
| 2466 | { return null; } |
| 2467 | const cursor = view.screenToTile(mouseX, mouseY); |
| 2468 | const tileSize = view.tileSize(); |
| 2469 | let best = null; |
| 2470 | let bestDist = Infinity; |
| 2471 | let bestScreen = Infinity; |
| 2472 | for (const entity of view.entities.values()) |
| 2473 | { |
| 2474 | if (!isSpellEntityTarget(entity)) |
| 2475 | { continue; } |
| 2476 | const visualTileX = Math.floor((entity.visualX + (entity.stackOffsetX ?? 0)) / tileSize); |
| 2477 | const visualTileY = Math.floor((entity.visualY + (entity.stackOffsetY ?? 0)) / tileSize); |
| 2478 | const dist = Math.min( |
| 2479 | chebyshev(cursor.tileX, cursor.tileY, entity.tileX, entity.tileY), |
| 2480 | chebyshev(cursor.tileX, cursor.tileY, visualTileX, visualTileY) |
| 2481 | ); |
| 2482 | if (dist > SPELL_ENTITY_SNAP_TILES) |
| 2483 | { continue; } |
| 2484 | const box = view.nametagAnchor?.(entity); |
| 2485 | const screenDist = box |
| 2486 | ? Math.hypot(box.x - mouseX, box.y - mouseY) |
| 2487 | : dist; |
| 2488 | if (dist < bestDist || (dist === bestDist && screenDist < bestScreen)) |
| 2489 | { |
| 2490 | best = entity; |
| 2491 | bestDist = dist; |
| 2492 | bestScreen = screenDist; |
| 2493 | } |
| 2494 | } |
| 2495 | return best; |
| 2496 | } |
| 2497 | |
| 2498 | function isSpellEntityTarget (entity) |
| 2499 | { |
| 2500 | if (!entity || entity.entityId === view?.localEntityId) |
| 2501 | { return false; } |
| 2502 | if (entity.untargetable || entity.freeFlight) |
| 2503 | { return false; } |
| 2504 | return isAttackableEntity(entity); |
| 2505 | } |
| 2506 | |
| 2507 | function hideWorldTooltip () |
| 2508 | { |
| 2509 | if (worldTooltip) |
| 2510 | { |
| 2511 | worldTooltip.hidden = true; |
| 2512 | worldTooltip.classList.remove("friend-name"); |
| 2513 | worldTooltip.style.color = ""; |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | /** Cursor tip for gatherable resource nodes (ores now; trees / fish / herbs later). */ |
| 2518 | function updateWorldTooltip () |
| 2519 | { |
| 2520 | if (!worldTooltip || !view || !scene || uiBlocked()) |
| 2521 | { |
| 2522 | hideWorldTooltip(); |
| 2523 | return; |
| 2524 | } |
| 2525 | const canvas = scene.canvas; |
| 2526 | const rect = canvas.getBoundingClientRect(); |
| 2527 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 2528 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 2529 | if (mouseX < 0 || mouseY < 0 || mouseX >= rect.width || mouseY >= rect.height) |
| 2530 | { |
| 2531 | hideWorldTooltip(); |
| 2532 | return; |
| 2533 | } |
| 2534 | const entity = view.entityAtScreen(mouseX, mouseY); |
| 2535 | if (entity && entity.entityId !== view.localEntityId) |
| 2536 | { |
| 2537 | const label = entityLabel(entity); |
| 2538 | worldTooltip.hidden = false; |
| 2539 | worldTooltip.textContent = label; |
| 2540 | const friend = entity.characterId && social?.isFriendCharacter?.(entity.characterId, entity.accountId); |
| 2541 | worldTooltip.classList.toggle("friend-name", !!friend); |
| 2542 | worldTooltip.style.color = friend ? "#e8c84a" : ""; |
| 2543 | worldTooltip.style.left = `${GameInput.mousePosition.x + 14}px`; |
| 2544 | worldTooltip.style.top = `${GameInput.mousePosition.y + 16}px`; |
| 2545 | return; |
| 2546 | } |
| 2547 | const tile = view.screenToTile(mouseX, mouseY); |
| 2548 | const label = resourceNodeDisplayName(blockAt(view, tile.tileX, tile.tileY)); |
| 2549 | if (!label) |
| 2550 | { |
| 2551 | hideWorldTooltip(); |
| 2552 | return; |
| 2553 | } |
| 2554 | worldTooltip.hidden = false; |
| 2555 | worldTooltip.textContent = label; |
| 2556 | worldTooltip.style.left = `${GameInput.mousePosition.x + 14}px`; |
| 2557 | worldTooltip.style.top = `${GameInput.mousePosition.y + 16}px`; |
| 2558 | } |
| 2559 | |
| 2560 | function blockTypeAt (view, tileX, tileY) |
| 2561 | { |
| 2562 | return blockAt(view, tileX, tileY)?.typeId ?? null; |
| 2563 | } |
| 2564 | |
| 2565 | function blockAt (view, tileX, tileY) |
| 2566 | { |
| 2567 | const chunkX = Math.floor(tileX / CHUNK_SIZE); |
| 2568 | const chunkY = Math.floor(tileY / CHUNK_SIZE); |
| 2569 | const snapshot = view.chunks.get(`${chunkX},${chunkY}`); |
| 2570 | if (!snapshot) |
| 2571 | { return null; } |
| 2572 | const localX = tileX - chunkX * CHUNK_SIZE; |
| 2573 | const localY = tileY - chunkY * CHUNK_SIZE; |
| 2574 | return snapshot.blocks[localY * CHUNK_SIZE + localX] ?? null; |
| 2575 | } |
| 2576 | |
| 2577 | function groundTypeAt (view, tileX, tileY) |
| 2578 | { |
| 2579 | const chunkX = Math.floor(tileX / CHUNK_SIZE); |
| 2580 | const chunkY = Math.floor(tileY / CHUNK_SIZE); |
| 2581 | const snapshot = view.chunks.get(`${chunkX},${chunkY}`); |
| 2582 | if (!snapshot) |
| 2583 | { return null; } |
| 2584 | const localX = tileX - chunkX * CHUNK_SIZE; |
| 2585 | const localY = tileY - chunkY * CHUNK_SIZE; |
| 2586 | return normalizeGround(snapshot.grounds?.[localY * CHUNK_SIZE + localX]).typeId; |
| 2587 | } |
| 2588 | |
| 2589 | /** Best-effort client filter for approach destinations (server still authorizes paths). */ |
| 2590 | function isClientApproachable (tileX, tileY) |
| 2591 | { |
| 2592 | if (!view) |
| 2593 | { return true; } |
| 2594 | const groundId = groundTypeAt(view, tileX, tileY); |
| 2595 | const groundDef = groundId != null ? GROUND_DEFINITIONS[groundId] : null; |
| 2596 | if (groundDef && !groundDef.passable) |
| 2597 | { return false; } |
| 2598 | const block = blockAt(view, tileX, tileY); |
| 2599 | if (block?.typeId != null) |
| 2600 | { |
| 2601 | const blockDef = BLOCK_DEFINITIONS[block.typeId]; |
| 2602 | if (blockDef && !blockDef.passable) |
| 2603 | { return false; } |
| 2604 | } |
| 2605 | for (const entity of view.entitiesAtTile(tileX, tileY)) |
| 2606 | { |
| 2607 | if (entity.entityId === view.localEntityId) |
| 2608 | { continue; } |
| 2609 | if (entity.untargetable || entity.freeFlight) |
| 2610 | { continue; } |
| 2611 | return false; |
| 2612 | } |
| 2613 | return true; |
| 2614 | } |
| 2615 | |
| 2616 | function chebyshev (ax, ay, bx, by) |
| 2617 | { |
| 2618 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 2619 | } |
| 2620 | |
| 2621 | function updateAuthUi (authStatus) |
| 2622 | { |
| 2623 | const signIn = document.getElementById("auth-sign-in"); |
| 2624 | const signOut = document.getElementById("auth-sign-out"); |
| 2625 | const exportLink = document.getElementById("auth-export"); |
| 2626 | if (signIn) |
| 2627 | { |
| 2628 | signIn.hidden = !!authStatus?.authenticated; |
| 2629 | signIn.href = loginUrl("/"); |
| 2630 | } |
| 2631 | if (signOut) |
| 2632 | { |
| 2633 | signOut.hidden = !authStatus?.authenticated; |
| 2634 | signOut.href = logoutUrl("/"); |
| 2635 | } |
| 2636 | if (exportLink) |
| 2637 | { exportLink.hidden = !authStatus?.authenticated; } |
| 2638 | if (authStatus?.authenticated && !session) |
| 2639 | { |
| 2640 | statusEl.textContent = `Signed in as ${formatAccountLabel(authStatus)}`; |
| 2641 | } |
| 2642 | } |