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