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