| 1 | import { MessageType, GAIT, CHUNK_SIZE } from "/shared/protocol/constants.js"; |
| 2 | import { BlockType, BLOCK_DEFINITIONS, GROUND_DEFINITIONS, normalizeGround, resourceNodeDisplayName, isChopableTreeBlock, isPickablePlantBlock } from "/shared/definitions/tiles.js"; |
| 3 | import { GameSocket, defaultSocketUrl } from "../net/socket.js"; |
| 4 | import { loadSpriteCatalog, preloadCatalogImages, applySpriteSheets, useNearestNeighbour } from "../render/sprites.js"; |
| 5 | import { WorldView } from "../render/worldView.js"; |
| 6 | import { initSocial } from "./social.js"; |
| 7 | import { initChat } from "./chat.js"; |
| 8 | import { initInventory } from "./inventory.js"; |
| 9 | import { initSkills } from "./skills.js"; |
| 10 | import { initCrafting } from "./crafting.js"; |
| 11 | import { initAlchemy } from "./alchemy.js"; |
| 12 | import { initStamina } from "./stamina.js"; |
| 13 | import { initContextMenu } from "./contextMenu.js"; |
| 14 | import { initDeath } from "./death.js"; |
| 15 | import { initToasts } from "./toasts.js"; |
| 16 | import { initShop } from "./shop.js"; |
| 17 | import { initMarket } from "./market.js"; |
| 18 | import { CraftingStationId } from "/shared/definitions/craftingStations.js"; |
| 19 | import { isOreBoulderState } from "/shared/definitions/materialTiers.js"; |
| 20 | import { formatQuantity } from "/shared/items/formatQuantity.js"; |
| 21 | import { ItemId } from "/shared/definitions/items.js"; |
| 22 | import { countCoinsInSlots } from "/shared/items/inventory.js"; |
| 23 | |
| 24 | const game = new Game(); |
| 25 | const hud = document.getElementById("hud"); |
| 26 | const overlay = document.getElementById("overlay"); |
| 27 | const statusEl = document.getElementById("status"); |
| 28 | const characterList = document.getElementById("character-list"); |
| 29 | const errorEl = document.getElementById("error"); |
| 30 | const worldTooltip = document.getElementById("world-tooltip"); |
| 31 | |
| 32 | let socket; |
| 33 | let session = null; |
| 34 | let playing = false; |
| 35 | let view = null; |
| 36 | let scene = null; |
| 37 | let gait = GAIT.walk; |
| 38 | let catalog = { grounds: {}, blocks: {} }; |
| 39 | let social; |
| 40 | let chat; |
| 41 | let inventory; |
| 42 | let skills; |
| 43 | let crafting; |
| 44 | let alchemy; |
| 45 | let staminaUi; |
| 46 | let contextMenu; |
| 47 | let death; |
| 48 | let toasts; |
| 49 | let shop; |
| 50 | let market; |
| 51 | let pendingPickup = null; |
| 52 | /** @type {{ kind: "bank" | "cauldron" | "station" | "mine" | "chop", tileX: number, tileY: number, stationId?: string, getCheques?: boolean } | { kind: "talk", targetEntityId: number } | null } */ |
| 53 | let pendingInteract = null; |
| 54 | /** Last approach moveTo so we do not re-path every frame (resets server walkCarry). */ |
| 55 | let approachMove = null; |
| 56 | /** Framework can report mousePressed for two frames; latch so one physical click is one command. */ |
| 57 | let leftClickConsumed = false; |
| 58 | |
| 59 | function consumeWorldClick () |
| 60 | { |
| 61 | leftClickConsumed = true; |
| 62 | } |
| 63 | |
| 64 | function showError (message) |
| 65 | { |
| 66 | errorEl.textContent = message ?? ""; |
| 67 | } |
| 68 | |
| 69 | function renderCharacters (characters) |
| 70 | { |
| 71 | characterList.innerHTML = ""; |
| 72 | for (const character of characters) |
| 73 | { |
| 74 | const row = document.createElement("div"); |
| 75 | row.className = "character-row"; |
| 76 | const pending = character.deletionScheduledAt |
| 77 | ? ` (deleting ${new Date(character.deletionScheduledAt).toLocaleString()})` |
| 78 | : ""; |
| 79 | row.innerHTML = `<strong>${character.firstName} ${character.lastName}</strong> · ${character.sex}${pending}`; |
| 80 | const play = document.createElement("button"); |
| 81 | play.textContent = "Play"; |
| 82 | play.disabled = !!character.deletionScheduledAt; |
| 83 | play.addEventListener("click", () => socket.send({ type: MessageType.selectCharacter, characterId: character.characterId })); |
| 84 | const del = document.createElement("button"); |
| 85 | del.textContent = character.deletionScheduledAt ? "Cancel delete" : "Delete"; |
| 86 | del.addEventListener("click", () => |
| 87 | { |
| 88 | socket.send({ |
| 89 | type: character.deletionScheduledAt ? MessageType.cancelDeleteCharacter : MessageType.scheduleDeleteCharacter, |
| 90 | characterId: character.characterId |
| 91 | }); |
| 92 | }); |
| 93 | row.append(play, del); |
| 94 | characterList.append(row); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | function uiBlocked () |
| 99 | { |
| 100 | return !!(social?.isBlocking() || chat?.isBlocking() || inventory?.isBlocking() |
| 101 | || crafting?.isBlocking() || alchemy?.isBlocking() || shop?.isBlocking() |
| 102 | || market?.isBlocking() || contextMenu?.isOpen() || death?.isBlocking()); |
| 103 | } |
| 104 | |
| 105 | function clearPendingPickup () |
| 106 | { |
| 107 | pendingPickup = null; |
| 108 | } |
| 109 | |
| 110 | function clearPendingInteract () |
| 111 | { |
| 112 | pendingInteract = null; |
| 113 | approachMove = null; |
| 114 | } |
| 115 | |
| 116 | function clearAllPendingActions () |
| 117 | { |
| 118 | clearPendingPickup(); |
| 119 | clearPendingInteract(); |
| 120 | } |
| 121 | |
| 122 | function approachAdjacentTile (tileX, tileY) |
| 123 | { |
| 124 | const local = view.entities.get(view.localEntityId); |
| 125 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 126 | { |
| 127 | approachMove = null; |
| 128 | return true; |
| 129 | } |
| 130 | let bestWalkable = null; |
| 131 | let bestWalkableDist = Infinity; |
| 132 | let bestAny = null; |
| 133 | let bestAnyDist = Infinity; |
| 134 | for (let dy = -1; dy <= 1; dy++) |
| 135 | { |
| 136 | for (let dx = -1; dx <= 1; dx++) |
| 137 | { |
| 138 | if (dx === 0 && dy === 0) |
| 139 | { continue; } |
| 140 | const destX = tileX + dx; |
| 141 | const destY = tileY + dy; |
| 142 | const dist = local |
| 143 | ? chebyshev(local.tileX, local.tileY, destX, destY) |
| 144 | : 0; |
| 145 | if (dist < bestAnyDist) |
| 146 | { |
| 147 | bestAnyDist = dist; |
| 148 | bestAny = { tileX: destX, tileY: destY }; |
| 149 | } |
| 150 | if (isClientApproachable(destX, destY) && dist < bestWalkableDist) |
| 151 | { |
| 152 | bestWalkableDist = dist; |
| 153 | bestWalkable = { tileX: destX, tileY: destY }; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | const best = bestWalkable ?? bestAny; |
| 158 | if (!best) |
| 159 | { return false; } |
| 160 | if ( |
| 161 | approachMove |
| 162 | && approachMove.focusX === tileX |
| 163 | && approachMove.focusY === tileY |
| 164 | && approachMove.destX === best.tileX |
| 165 | && approachMove.destY === best.tileY |
| 166 | ) |
| 167 | { return false; } |
| 168 | approachMove = { |
| 169 | focusX: tileX, |
| 170 | focusY: tileY, |
| 171 | destX: best.tileX, |
| 172 | destY: best.tileY |
| 173 | }; |
| 174 | socket.send({ type: MessageType.moveTo, tileX: best.tileX, tileY: best.tileY }); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | function requestOpenBank (tileX, tileY, { getCheques = false } = {}) |
| 179 | { |
| 180 | clearAllPendingActions(); |
| 181 | pendingInteract = { kind: "bank", tileX, tileY, getCheques: !!getCheques }; |
| 182 | const local = view?.entities.get(view.localEntityId); |
| 183 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 184 | { flushPendingInteract(); } |
| 185 | else |
| 186 | { approachAdjacentTile(tileX, tileY); } |
| 187 | } |
| 188 | |
| 189 | function requestOpenMarket (tileX, tileY) |
| 190 | { |
| 191 | clearAllPendingActions(); |
| 192 | pendingInteract = { kind: "market", tileX, tileY }; |
| 193 | const local = view?.entities.get(view.localEntityId); |
| 194 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 195 | { flushPendingInteract(); } |
| 196 | else |
| 197 | { approachAdjacentTile(tileX, tileY); } |
| 198 | } |
| 199 | |
| 200 | function requestOpenCauldron (tileX, tileY) |
| 201 | { |
| 202 | clearAllPendingActions(); |
| 203 | pendingInteract = { kind: "cauldron", tileX, tileY }; |
| 204 | const local = view?.entities.get(view.localEntityId); |
| 205 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 206 | { flushPendingInteract(); } |
| 207 | else |
| 208 | { approachAdjacentTile(tileX, tileY); } |
| 209 | } |
| 210 | |
| 211 | function requestOpenFurnace (tileX, tileY) |
| 212 | { |
| 213 | requestOpenCraftStation(CraftingStationId.furnace, tileX, tileY); |
| 214 | } |
| 215 | |
| 216 | function requestOpenCraftStation (stationId, tileX, tileY) |
| 217 | { |
| 218 | clearAllPendingActions(); |
| 219 | pendingInteract = { kind: "station", stationId, tileX, tileY }; |
| 220 | const local = view?.entities.get(view.localEntityId); |
| 221 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 222 | { flushPendingInteract(); } |
| 223 | else |
| 224 | { approachAdjacentTile(tileX, tileY); } |
| 225 | } |
| 226 | |
| 227 | function requestMine (tileX, tileY) |
| 228 | { |
| 229 | clearAllPendingActions(); |
| 230 | pendingInteract = { kind: "mine", tileX, tileY }; |
| 231 | const local = view?.entities.get(view.localEntityId); |
| 232 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 233 | { flushPendingInteract(); } |
| 234 | else |
| 235 | { approachAdjacentTile(tileX, tileY); } |
| 236 | } |
| 237 | |
| 238 | function requestChop (tileX, tileY) |
| 239 | { |
| 240 | clearAllPendingActions(); |
| 241 | pendingInteract = { kind: "chop", tileX, tileY }; |
| 242 | const local = view?.entities.get(view.localEntityId); |
| 243 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 244 | { flushPendingInteract(); } |
| 245 | else |
| 246 | { approachAdjacentTile(tileX, tileY); } |
| 247 | } |
| 248 | |
| 249 | function requestPick (tileX, tileY) |
| 250 | { |
| 251 | clearAllPendingActions(); |
| 252 | pendingInteract = { kind: "pick", tileX, tileY }; |
| 253 | const local = view?.entities.get(view.localEntityId); |
| 254 | if (local && chebyshev(local.tileX, local.tileY, tileX, tileY) <= 1) |
| 255 | { flushPendingInteract(); } |
| 256 | else |
| 257 | { approachAdjacentTile(tileX, tileY); } |
| 258 | } |
| 259 | |
| 260 | function requestTalk (targetEntityId) |
| 261 | { |
| 262 | clearAllPendingActions(); |
| 263 | pendingInteract = { kind: "talk", targetEntityId }; |
| 264 | const target = view?.entities.get(targetEntityId); |
| 265 | if (!target) |
| 266 | { |
| 267 | clearPendingInteract(); |
| 268 | return; |
| 269 | } |
| 270 | if (approachAdjacentTile(target.tileX, target.tileY)) |
| 271 | { flushPendingInteract(); } |
| 272 | } |
| 273 | |
| 274 | function flushPendingInteract () |
| 275 | { |
| 276 | if (!pendingInteract || !view) |
| 277 | { return; } |
| 278 | const local = view.entities.get(view.localEntityId); |
| 279 | if (!local) |
| 280 | { return; } |
| 281 | |
| 282 | if (pendingInteract.kind === "bank") |
| 283 | { |
| 284 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 285 | { return; } |
| 286 | const getCheques = !!pendingInteract.getCheques; |
| 287 | socket.send({ type: MessageType.openBank }); |
| 288 | if (getCheques) |
| 289 | { inventory?.getBlankCheques(); } |
| 290 | clearPendingInteract(); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | if (pendingInteract.kind === "market") |
| 295 | { |
| 296 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 297 | { return; } |
| 298 | socket.send({ type: MessageType.openMarket }); |
| 299 | clearPendingInteract(); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | if (pendingInteract.kind === "cauldron") |
| 304 | { |
| 305 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 306 | { return; } |
| 307 | socket.send({ type: MessageType.openCauldron }); |
| 308 | clearPendingInteract(); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | if (pendingInteract.kind === "station") |
| 313 | { |
| 314 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 315 | { return; } |
| 316 | socket.send({ |
| 317 | type: MessageType.openStation, |
| 318 | stationId: pendingInteract.stationId |
| 319 | }); |
| 320 | clearPendingInteract(); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | if (pendingInteract.kind === "mine") |
| 325 | { |
| 326 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 327 | { return; } |
| 328 | socket.send({ |
| 329 | type: MessageType.mine, |
| 330 | tileX: pendingInteract.tileX, |
| 331 | tileY: pendingInteract.tileY |
| 332 | }); |
| 333 | clearPendingInteract(); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | if (pendingInteract.kind === "chop") |
| 338 | { |
| 339 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 340 | { return; } |
| 341 | socket.send({ |
| 342 | type: MessageType.chop, |
| 343 | tileX: pendingInteract.tileX, |
| 344 | tileY: pendingInteract.tileY |
| 345 | }); |
| 346 | clearPendingInteract(); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (pendingInteract.kind === "pick") |
| 351 | { |
| 352 | if (chebyshev(local.tileX, local.tileY, pendingInteract.tileX, pendingInteract.tileY) > 1) |
| 353 | { return; } |
| 354 | socket.send({ |
| 355 | type: MessageType.pick, |
| 356 | tileX: pendingInteract.tileX, |
| 357 | tileY: pendingInteract.tileY |
| 358 | }); |
| 359 | clearPendingInteract(); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | if (pendingInteract.kind === "talk") |
| 364 | { |
| 365 | const target = view.entities.get(pendingInteract.targetEntityId); |
| 366 | if (!target) |
| 367 | { |
| 368 | clearPendingInteract(); |
| 369 | return; |
| 370 | } |
| 371 | if (chebyshev(local.tileX, local.tileY, target.tileX, target.tileY) > 1) |
| 372 | { |
| 373 | // Target may have moved — keep chasing. |
| 374 | approachAdjacentTile(target.tileX, target.tileY); |
| 375 | return; |
| 376 | } |
| 377 | socket.send({ type: MessageType.interact, targetEntityId: pendingInteract.targetEntityId }); |
| 378 | clearPendingInteract(); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | function requestPickup (ground) |
| 383 | { |
| 384 | if (!ground) |
| 385 | { return; } |
| 386 | clearPendingInteract(); |
| 387 | pendingPickup = { |
| 388 | groundItemId: ground.groundItemId, |
| 389 | tileX: ground.tileX, |
| 390 | tileY: ground.tileY |
| 391 | }; |
| 392 | const local = view.entities.get(view.localEntityId); |
| 393 | if (local && local.tileX === ground.tileX && local.tileY === ground.tileY) |
| 394 | { |
| 395 | flushPendingPickup(); |
| 396 | return; |
| 397 | } |
| 398 | socket.send({ type: MessageType.moveTo, tileX: ground.tileX, tileY: ground.tileY }); |
| 399 | } |
| 400 | |
| 401 | function flushPendingPickup () |
| 402 | { |
| 403 | if (!pendingPickup || !view) |
| 404 | { return; } |
| 405 | const item = view.groundItems.get(pendingPickup.groundItemId); |
| 406 | if (!item) |
| 407 | { |
| 408 | clearPendingPickup(); |
| 409 | return; |
| 410 | } |
| 411 | const local = view.entities.get(view.localEntityId); |
| 412 | if (!local) |
| 413 | { return; } |
| 414 | if (local.tileX !== pendingPickup.tileX || local.tileY !== pendingPickup.tileY) |
| 415 | { return; } |
| 416 | socket.send({ type: MessageType.pickupItem, groundItemId: pendingPickup.groundItemId }); |
| 417 | clearPendingPickup(); |
| 418 | } |
| 419 | |
| 420 | function requestAttack (targetEntityId) |
| 421 | { |
| 422 | clearAllPendingActions(); |
| 423 | socket.send({ type: MessageType.attack, targetEntityId }); |
| 424 | } |
| 425 | |
| 426 | function isAttackableEntity (entity) |
| 427 | { |
| 428 | if (!entity || entity.untargetable) |
| 429 | { return false; } |
| 430 | if (entity.canFight) |
| 431 | { return true; } |
| 432 | // Snapshots can omit canFight; mobs are still attackable via LMB. |
| 433 | return !!entity.mobTypeId && !entity.talkable; |
| 434 | } |
| 435 | |
| 436 | function handleLeftClick (mouseX, mouseY) |
| 437 | { |
| 438 | const entity = view.entityAtScreen(mouseX, mouseY); |
| 439 | if (entity && entity.entityId !== view.localEntityId && !entity.untargetable) |
| 440 | { |
| 441 | clearPendingPickup(); |
| 442 | if (entity.characterId) |
| 443 | { |
| 444 | clearPendingInteract(); |
| 445 | social.inspect(entity); |
| 446 | return; |
| 447 | } |
| 448 | if (entity.talkable) |
| 449 | { |
| 450 | requestTalk(entity.entityId); |
| 451 | return; |
| 452 | } |
| 453 | if (isAttackableEntity(entity)) |
| 454 | { |
| 455 | requestAttack(entity.entityId); |
| 456 | return; |
| 457 | } |
| 458 | // Clicked an entity we don't act on — do not fall through to walk (that cancels combat). |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | const tile = view.screenToTile(mouseX, mouseY); |
| 463 | const ground = view.groundItemAtTile(tile.tileX, tile.tileY); |
| 464 | if (ground) |
| 465 | { requestPickup(ground); } |
| 466 | else if (isBankTile(view, tile.tileX, tile.tileY)) |
| 467 | { requestOpenBank(tile.tileX, tile.tileY); } |
| 468 | else if (isMarketplaceTile(view, tile.tileX, tile.tileY)) |
| 469 | { requestOpenMarket(tile.tileX, tile.tileY); } |
| 470 | else if (isCauldronTile(view, tile.tileX, tile.tileY)) |
| 471 | { requestOpenCauldron(tile.tileX, tile.tileY); } |
| 472 | else if (isFurnaceTile(view, tile.tileX, tile.tileY)) |
| 473 | { requestOpenCraftStation(CraftingStationId.furnace, tile.tileX, tile.tileY); } |
| 474 | else if (isAnvilTile(view, tile.tileX, tile.tileY)) |
| 475 | { requestOpenCraftStation(CraftingStationId.anvil, tile.tileX, tile.tileY); } |
| 476 | else if (isWoodworkingBenchTile(view, tile.tileX, tile.tileY)) |
| 477 | { requestOpenCraftStation(CraftingStationId.woodworkingBench, tile.tileX, tile.tileY); } |
| 478 | else if (isCraftingBenchTile(view, tile.tileX, tile.tileY)) |
| 479 | { requestOpenCraftStation(CraftingStationId.craftingBench, tile.tileX, tile.tileY); } |
| 480 | else if (isSpinningWheelTile(view, tile.tileX, tile.tileY)) |
| 481 | { requestOpenCraftStation(CraftingStationId.spinningWheel, tile.tileX, tile.tileY); } |
| 482 | else if (isLoomTile(view, tile.tileX, tile.tileY)) |
| 483 | { requestOpenCraftStation(CraftingStationId.loom, tile.tileX, tile.tileY); } |
| 484 | else if (isTailorBenchTile(view, tile.tileX, tile.tileY)) |
| 485 | { requestOpenCraftStation(CraftingStationId.tailorBench, tile.tileX, tile.tileY); } |
| 486 | else if (isOreBoulderTile(view, tile.tileX, tile.tileY)) |
| 487 | { requestMine(tile.tileX, tile.tileY); } |
| 488 | else if (isChopableTreeTile(view, tile.tileX, tile.tileY)) |
| 489 | { requestChop(tile.tileX, tile.tileY); } |
| 490 | else if (isPickablePlantTile(view, tile.tileX, tile.tileY)) |
| 491 | { requestPick(tile.tileX, tile.tileY); } |
| 492 | else |
| 493 | { |
| 494 | clearAllPendingActions(); |
| 495 | socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY }); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | function handleRightClick (event, mouseX, mouseY) |
| 500 | { |
| 501 | event.preventDefault(); |
| 502 | const tile = view.screenToTile(mouseX, mouseY); |
| 503 | const items = []; |
| 504 | const walkTileX = tile.tileX; |
| 505 | const walkTileY = tile.tileY; |
| 506 | |
| 507 | items.push({ |
| 508 | label: "Walk here", |
| 509 | onSelect: () => |
| 510 | { |
| 511 | clearAllPendingActions(); |
| 512 | socket.send({ type: MessageType.moveTo, tileX: walkTileX, tileY: walkTileY }); |
| 513 | } |
| 514 | }); |
| 515 | |
| 516 | for (const entity of view.entitiesAtTile(tile.tileX, tile.tileY)) |
| 517 | { |
| 518 | if (entity.entityId === view.localEntityId) |
| 519 | { continue; } |
| 520 | const targetName = entityLabel(entity); |
| 521 | items.push({ |
| 522 | label: `Follow ${targetName}`, |
| 523 | onSelect: () => |
| 524 | { |
| 525 | clearAllPendingActions(); |
| 526 | socket.send({ type: MessageType.follow, targetEntityId: entity.entityId }); |
| 527 | } |
| 528 | }); |
| 529 | if (entity.untargetable) |
| 530 | { continue; } |
| 531 | if (entity.hasShop) |
| 532 | { |
| 533 | items.push({ |
| 534 | label: `Shop ${targetName}`, |
| 535 | onSelect: () => requestTalk(entity.entityId) |
| 536 | }); |
| 537 | } |
| 538 | else if (entity.talkable) |
| 539 | { |
| 540 | items.push({ |
| 541 | label: `Talk to ${targetName}`, |
| 542 | onSelect: () => requestTalk(entity.entityId) |
| 543 | }); |
| 544 | } |
| 545 | if (entity.characterId) |
| 546 | { |
| 547 | items.push({ |
| 548 | label: `${targetName} info`, |
| 549 | onSelect: () => |
| 550 | { |
| 551 | clearAllPendingActions(); |
| 552 | social.inspect(entity); |
| 553 | } |
| 554 | }); |
| 555 | } |
| 556 | items.push({ |
| 557 | label: `Attack ${targetName}`, |
| 558 | onSelect: () => requestAttack(entity.entityId) |
| 559 | }); |
| 560 | } |
| 561 | |
| 562 | for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY)) |
| 563 | { |
| 564 | const name = groundLabel(ground); |
| 565 | items.push({ |
| 566 | label: `Pick up ${name}`, |
| 567 | onSelect: () => requestPickup(ground) |
| 568 | }); |
| 569 | } |
| 570 | |
| 571 | if (isBankTile(view, tile.tileX, tile.tileY)) |
| 572 | { |
| 573 | items.push({ |
| 574 | label: "Bank", |
| 575 | onSelect: () => requestOpenBank(tile.tileX, tile.tileY) |
| 576 | }); |
| 577 | items.push({ |
| 578 | label: "Get Blank Cheques", |
| 579 | onSelect: () => requestOpenBank(tile.tileX, tile.tileY, { getCheques: true }) |
| 580 | }); |
| 581 | } |
| 582 | |
| 583 | if (isMarketplaceTile(view, tile.tileX, tile.tileY)) |
| 584 | { |
| 585 | items.push({ |
| 586 | label: "Central Exchange", |
| 587 | onSelect: () => requestOpenMarket(tile.tileX, tile.tileY) |
| 588 | }); |
| 589 | } |
| 590 | |
| 591 | if (isCauldronTile(view, tile.tileX, tile.tileY)) |
| 592 | { |
| 593 | items.push({ |
| 594 | label: "Alchemy", |
| 595 | onSelect: () => requestOpenCauldron(tile.tileX, tile.tileY) |
| 596 | }); |
| 597 | } |
| 598 | |
| 599 | if (isFurnaceTile(view, tile.tileX, tile.tileY)) |
| 600 | { |
| 601 | items.push({ |
| 602 | label: "Smelt", |
| 603 | onSelect: () => requestOpenCraftStation(CraftingStationId.furnace, tile.tileX, tile.tileY) |
| 604 | }); |
| 605 | } |
| 606 | |
| 607 | if (isAnvilTile(view, tile.tileX, tile.tileY)) |
| 608 | { |
| 609 | items.push({ |
| 610 | label: "Smith", |
| 611 | onSelect: () => requestOpenCraftStation(CraftingStationId.anvil, tile.tileX, tile.tileY) |
| 612 | }); |
| 613 | } |
| 614 | |
| 615 | if (isWoodworkingBenchTile(view, tile.tileX, tile.tileY)) |
| 616 | { |
| 617 | items.push({ |
| 618 | label: "Woodwork", |
| 619 | onSelect: () => requestOpenCraftStation(CraftingStationId.woodworkingBench, tile.tileX, tile.tileY) |
| 620 | }); |
| 621 | } |
| 622 | |
| 623 | if (isCraftingBenchTile(view, tile.tileX, tile.tileY)) |
| 624 | { |
| 625 | items.push({ |
| 626 | label: "Craft", |
| 627 | onSelect: () => requestOpenCraftStation(CraftingStationId.craftingBench, tile.tileX, tile.tileY) |
| 628 | }); |
| 629 | } |
| 630 | |
| 631 | if (isSpinningWheelTile(view, tile.tileX, tile.tileY)) |
| 632 | { |
| 633 | items.push({ |
| 634 | label: "Spin", |
| 635 | onSelect: () => requestOpenCraftStation(CraftingStationId.spinningWheel, tile.tileX, tile.tileY) |
| 636 | }); |
| 637 | } |
| 638 | |
| 639 | if (isLoomTile(view, tile.tileX, tile.tileY)) |
| 640 | { |
| 641 | items.push({ |
| 642 | label: "Weave", |
| 643 | onSelect: () => requestOpenCraftStation(CraftingStationId.loom, tile.tileX, tile.tileY) |
| 644 | }); |
| 645 | } |
| 646 | |
| 647 | if (isTailorBenchTile(view, tile.tileX, tile.tileY)) |
| 648 | { |
| 649 | items.push({ |
| 650 | label: "Tailor", |
| 651 | onSelect: () => requestOpenCraftStation(CraftingStationId.tailorBench, tile.tileX, tile.tileY) |
| 652 | }); |
| 653 | } |
| 654 | |
| 655 | if (isOreBoulderTile(view, tile.tileX, tile.tileY)) |
| 656 | { |
| 657 | items.push({ |
| 658 | label: "Mine", |
| 659 | onSelect: () => requestMine(tile.tileX, tile.tileY) |
| 660 | }); |
| 661 | } |
| 662 | |
| 663 | if (isChopableTreeTile(view, tile.tileX, tile.tileY)) |
| 664 | { |
| 665 | items.push({ |
| 666 | label: "Chop", |
| 667 | onSelect: () => requestChop(tile.tileX, tile.tileY) |
| 668 | }); |
| 669 | } |
| 670 | |
| 671 | if (isPickablePlantTile(view, tile.tileX, tile.tileY)) |
| 672 | { |
| 673 | items.push({ |
| 674 | label: "Pick", |
| 675 | onSelect: () => requestPick(tile.tileX, tile.tileY) |
| 676 | }); |
| 677 | } |
| 678 | |
| 679 | contextMenu.show(event.clientX, event.clientY, items); |
| 680 | } |
| 681 | |
| 682 | function entityLabel (entity) |
| 683 | { |
| 684 | const first = (entity?.firstName ?? "").trim(); |
| 685 | const last = (entity?.lastName ?? "").trim(); |
| 686 | if (first && last) |
| 687 | { return `${first} ${last}`; } |
| 688 | return first || "target"; |
| 689 | } |
| 690 | |
| 691 | function groundLabel (ground) |
| 692 | { |
| 693 | const name = (ground?.displayName ?? "").trim() || "item"; |
| 694 | const qty = ground?.quantity ?? 1; |
| 695 | if (qty <= 1) |
| 696 | { return name; } |
| 697 | return `${name} (${formatQuantity(qty)})`; |
| 698 | } |
| 699 | |
| 700 | function startPlay (payload) |
| 701 | { |
| 702 | playing = true; |
| 703 | overlay.hidden = true; |
| 704 | hud.hidden = false; |
| 705 | document.getElementById("you").textContent = `${payload.firstName} ${payload.lastName} (${session.authSubject})`; |
| 706 | gait = payload.gait ?? GAIT.walk; |
| 707 | document.getElementById("gait").textContent = gait; |
| 708 | staminaUi?.show(); |
| 709 | if (payload.stamina != null) |
| 710 | { staminaUi?.apply(payload.stamina); } |
| 711 | if (!scene) |
| 712 | { |
| 713 | scene = new Scene(0, 0, window.innerWidth, window.innerHeight); |
| 714 | game.loadScene(scene); |
| 715 | useNearestNeighbour(scene, game.images); |
| 716 | view = new WorldView(game, scene, catalog); |
| 717 | const ticker = new GameObject(0, 0, null, 10); |
| 718 | ticker.update = (dt) => |
| 719 | { |
| 720 | const leftPressed = GameInput.mousePressed(GameInput.MouseButtons.Left); |
| 721 | const leftHeld = GameInput.mouseHeld(GameInput.MouseButtons.Left); |
| 722 | if (!leftPressed && !leftHeld) |
| 723 | { leftClickConsumed = false; } |
| 724 | else if (!leftClickConsumed && playing && !uiBlocked()) |
| 725 | { |
| 726 | leftClickConsumed = true; |
| 727 | const canvas = scene.canvas; |
| 728 | const rect = canvas.getBoundingClientRect(); |
| 729 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 730 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 731 | handleLeftClick(mouseX, mouseY); |
| 732 | } |
| 733 | if (GameInput.isPressed(82) && !uiBlocked()) |
| 734 | { |
| 735 | const next = gait === GAIT.run ? GAIT.walk : GAIT.run; |
| 736 | if (next === GAIT.run && (staminaUi?.getCurrent?.() ?? 1) <= 0) |
| 737 | { return; } |
| 738 | socket.send({ type: MessageType.setGait, gait: next }); |
| 739 | // Walk always applies; run waits for authoritative STAMINA (may reject if empty). |
| 740 | if (next === GAIT.walk) |
| 741 | { |
| 742 | gait = next; |
| 743 | document.getElementById("gait").textContent = gait; |
| 744 | } |
| 745 | } |
| 746 | if (playing && !death?.isBlocking()) |
| 747 | { |
| 748 | flushPendingPickup(); |
| 749 | flushPendingInteract(); |
| 750 | social?.updateNametag(view, scene, view.localEntityId); |
| 751 | chat?.update(view, scene); |
| 752 | updateWorldTooltip(); |
| 753 | } |
| 754 | else |
| 755 | { hideWorldTooltip(); } |
| 756 | view.update(dt); |
| 757 | }; |
| 758 | scene.addObject(ticker); |
| 759 | window.addEventListener("resize", () => |
| 760 | { |
| 761 | scene.size.x = window.innerWidth; |
| 762 | scene.size.y = window.innerHeight; |
| 763 | scene.g_fixSize(); |
| 764 | useNearestNeighbour(scene, game.images); |
| 765 | }); |
| 766 | window.addEventListener("wheel", (event) => |
| 767 | { |
| 768 | if (!playing || uiBlocked()) |
| 769 | { return; } |
| 770 | view.setZoom(view.zoom + (event.deltaY < 0 ? 0.25 : -0.25)); |
| 771 | document.getElementById("zoom").textContent = `${view.zoom.toFixed(2)}×`; |
| 772 | }, { passive: true }); |
| 773 | scene.canvas.addEventListener("contextmenu", (event) => |
| 774 | { |
| 775 | if (!playing || uiBlocked()) |
| 776 | { return; } |
| 777 | const rect = scene.canvas.getBoundingClientRect(); |
| 778 | handleRightClick(event, event.clientX - rect.left, event.clientY - rect.top); |
| 779 | }); |
| 780 | } |
| 781 | view.setSettings(payload.settings ?? view.settings); |
| 782 | chat?.syncSettings(); |
| 783 | view.worldHour = payload.worldTime?.hour ?? 12; |
| 784 | view.setWorldSeason(payload.worldTime?.season); |
| 785 | view.setLocalEntity( |
| 786 | payload.entityId, |
| 787 | payload.tileX, |
| 788 | payload.tileY, |
| 789 | payload.gait, |
| 790 | payload.sex, |
| 791 | payload.firstName, |
| 792 | payload.lastName, |
| 793 | payload.characterId, |
| 794 | payload.layers |
| 795 | ); |
| 796 | } |
| 797 | |
| 798 | async function main () |
| 799 | { |
| 800 | catalog = await loadSpriteCatalog(); |
| 801 | await preloadCatalogImages(game, catalog); |
| 802 | applySpriteSheets(game.images, catalog); |
| 803 | social = initSocial({ |
| 804 | socket: { send (payload) { socket.send(payload); } }, |
| 805 | MessageType |
| 806 | }); |
| 807 | contextMenu = initContextMenu({ onSuppressWorldClick: consumeWorldClick }); |
| 808 | inventory = initInventory({ |
| 809 | socket: { send (payload) { socket.send(payload); } }, |
| 810 | MessageType, |
| 811 | contextMenu, |
| 812 | resolveSpriteUrl (name) |
| 813 | { |
| 814 | return catalog?.images?.find((entry) => entry.name === name)?.url ?? null; |
| 815 | }, |
| 816 | getFriends () |
| 817 | { |
| 818 | return social?.getFriends?.() ?? []; |
| 819 | }, |
| 820 | getLocalName () |
| 821 | { |
| 822 | const you = document.getElementById("you")?.textContent ?? ""; |
| 823 | return you.split("(")[0].trim() || "Unknown"; |
| 824 | } |
| 825 | }); |
| 826 | skills = initSkills(); |
| 827 | crafting = initCrafting({ |
| 828 | socket: { send (payload) { socket.send(payload); } }, |
| 829 | MessageType, |
| 830 | getInventorySlots () |
| 831 | { |
| 832 | return inventory?.getInventorySlots?.() ?? []; |
| 833 | }, |
| 834 | getSkillLevel (skillId) |
| 835 | { |
| 836 | return skills?.skillLevel?.(skillId) ?? 1; |
| 837 | } |
| 838 | }); |
| 839 | alchemy = initAlchemy({ |
| 840 | socket: { send (payload) { socket.send(payload); } }, |
| 841 | MessageType, |
| 842 | getInventorySlots () |
| 843 | { |
| 844 | return inventory?.getInventorySlots?.() ?? []; |
| 845 | } |
| 846 | }); |
| 847 | staminaUi = initStamina(); |
| 848 | toasts = initToasts(); |
| 849 | shop = initShop({ |
| 850 | socket: { send (payload) { socket.send(payload); } }, |
| 851 | MessageType, |
| 852 | onSuppressWorldClick: consumeWorldClick, |
| 853 | getCoinCount () |
| 854 | { |
| 855 | return countCoinsInSlots(inventory?.getInventorySlots?.() ?? []); |
| 856 | }, |
| 857 | getInventorySlots () |
| 858 | { |
| 859 | return inventory?.getInventorySlots?.() ?? []; |
| 860 | } |
| 861 | }); |
| 862 | market = initMarket({ |
| 863 | socket: { send (payload) { socket.send(payload); } }, |
| 864 | MessageType, |
| 865 | onSuppressWorldClick: consumeWorldClick, |
| 866 | getCoinCount () |
| 867 | { |
| 868 | return countCoinsInSlots(inventory?.getInventorySlots?.() ?? []); |
| 869 | }, |
| 870 | getInventorySlots () |
| 871 | { |
| 872 | return inventory?.getInventorySlots?.() ?? []; |
| 873 | } |
| 874 | }); |
| 875 | chat = initChat({ |
| 876 | socket: { send (payload) { socket.send(payload); } }, |
| 877 | MessageType, |
| 878 | getSettings: () => view?.settings |
| 879 | }); |
| 880 | death = initDeath({ |
| 881 | socket: { send (payload) { socket.send(payload); } }, |
| 882 | MessageType |
| 883 | }); |
| 884 | game.startGame(); |
| 885 | |
| 886 | socket = new GameSocket(defaultSocketUrl(), (message) => |
| 887 | { |
| 888 | switch (message.type) |
| 889 | { |
| 890 | case MessageType.session: |
| 891 | session = message; |
| 892 | statusEl.textContent = `Signed in as ${message.authSubject} (stub admin)`; |
| 893 | renderCharacters(message.characters); |
| 894 | break; |
| 895 | case MessageType.characterList: |
| 896 | renderCharacters(message.characters); |
| 897 | if (!playing) |
| 898 | { overlay.hidden = false; } |
| 899 | break; |
| 900 | case MessageType.enterWorld: |
| 901 | startPlay(message); |
| 902 | inventory?.setLocalCharacterId(message.characterId); |
| 903 | break; |
| 904 | case MessageType.chunkSnapshot: |
| 905 | view?.applySnapshot(message); |
| 906 | break; |
| 907 | case MessageType.chunkDelta: |
| 908 | for (const tile of message.tiles ?? []) |
| 909 | { |
| 910 | view?.updateAuthoredTile( |
| 911 | message.chunkX, |
| 912 | message.chunkY, |
| 913 | tile.localX, |
| 914 | tile.localY, |
| 915 | { ground: tile.ground, block: tile.block, light: tile.light } |
| 916 | ); |
| 917 | } |
| 918 | break; |
| 919 | case MessageType.unsubscribeChunk: |
| 920 | view?.removeChunk(message.chunkX, message.chunkY); |
| 921 | break; |
| 922 | case MessageType.entitySpawn: |
| 923 | view?.spawnEntity(message); |
| 924 | break; |
| 925 | case MessageType.entityMove: |
| 926 | view?.moveEntity(message); |
| 927 | if (message.entityId === view?.localEntityId && message.gait) |
| 928 | { |
| 929 | gait = message.gait; |
| 930 | document.getElementById("gait").textContent = gait; |
| 931 | } |
| 932 | break; |
| 933 | case MessageType.entityDespawn: |
| 934 | view?.despawnEntity(message.entityId); |
| 935 | break; |
| 936 | case MessageType.entityAppearance: |
| 937 | view?.setAppearance(message); |
| 938 | break; |
| 939 | case MessageType.groundItemAppear: |
| 940 | view?.upsertGroundItem(message); |
| 941 | break; |
| 942 | case MessageType.groundItemDespawn: |
| 943 | view?.despawnGroundItem(message.groundItemId); |
| 944 | break; |
| 945 | case MessageType.inventory: |
| 946 | inventory?.handleInventory(message); |
| 947 | crafting?.refresh(); |
| 948 | alchemy?.refresh(); |
| 949 | shop?.refresh(); |
| 950 | market?.refresh(); |
| 951 | break; |
| 952 | case MessageType.skills: |
| 953 | skills?.render(message.skills); |
| 954 | crafting?.refresh(); |
| 955 | break; |
| 956 | case MessageType.stamina: |
| 957 | staminaUi?.apply(message); |
| 958 | if (message.gait) |
| 959 | { |
| 960 | gait = message.gait; |
| 961 | document.getElementById("gait").textContent = gait; |
| 962 | } |
| 963 | break; |
| 964 | case MessageType.bankSnapshot: |
| 965 | inventory?.handleBank(message); |
| 966 | break; |
| 967 | case MessageType.cauldronSnapshot: |
| 968 | alchemy?.handleSnapshot(message); |
| 969 | break; |
| 970 | case MessageType.pigeonDispatched: |
| 971 | inventory?.handlePigeonDispatched(message); |
| 972 | break; |
| 973 | case MessageType.pigeonDelivered: |
| 974 | inventory?.handlePigeonDelivered(message); |
| 975 | break; |
| 976 | case MessageType.characterSearchResult: |
| 977 | inventory?.handleCharacterSearch(message); |
| 978 | break; |
| 979 | case MessageType.craftResult: |
| 980 | crafting?.handleCraftResult(message); |
| 981 | break; |
| 982 | case MessageType.worldStatus: |
| 983 | if (view) |
| 984 | { |
| 985 | view.setSettings(message.settings ?? view.settings); |
| 986 | view.worldHour = message.worldTime?.hour ?? view.worldHour; |
| 987 | view.setWorldSeason(message.worldTime?.season); |
| 988 | chat?.syncSettings(); |
| 989 | } |
| 990 | break; |
| 991 | case MessageType.chatBubble: |
| 992 | chat?.showBubble(message); |
| 993 | break; |
| 994 | case MessageType.friends: |
| 995 | social?.handleFriends(message); |
| 996 | break; |
| 997 | case MessageType.combatSwing: |
| 998 | view?.spawnCombatSwing(message); |
| 999 | break; |
| 1000 | case MessageType.gatherSwing: |
| 1001 | case MessageType.tileShove: |
| 1002 | view?.spawnGatherSwing(message); |
| 1003 | break; |
| 1004 | case MessageType.stationOpen: |
| 1005 | if (message.stationId) |
| 1006 | { crafting?.openStation(message.stationId); } |
| 1007 | break; |
| 1008 | case MessageType.shopOpen: |
| 1009 | shop?.handleShopOpen(message); |
| 1010 | break; |
| 1011 | case MessageType.shopClosed: |
| 1012 | shop?.handleShopClosed(message); |
| 1013 | break; |
| 1014 | case MessageType.marketOpen: |
| 1015 | market?.handleOpen(message); |
| 1016 | break; |
| 1017 | case MessageType.marketClosed: |
| 1018 | market?.handleClosed(message); |
| 1019 | break; |
| 1020 | case MessageType.marketTrade: |
| 1021 | market?.handleTrade(message); |
| 1022 | toasts?.show(`Trade: ${message.quantity} @ ${message.unitPrice} coins`); |
| 1023 | break; |
| 1024 | case MessageType.itemBubble: |
| 1025 | chat?.showItemBubble(message); |
| 1026 | break; |
| 1027 | case MessageType.gatherComplete: |
| 1028 | crafting?.refresh(); |
| 1029 | break; |
| 1030 | case MessageType.combatHit: |
| 1031 | view?.updateEntityHealth(message.targetId, message.health, message.maxHealth); |
| 1032 | view?.flashDamage(message.targetId); |
| 1033 | view?.spawnDamageNumber(message.targetId, message.damage); |
| 1034 | break; |
| 1035 | case MessageType.skillLevelUp: |
| 1036 | view?.spawnSkillLevelUp(message.entityId, message); |
| 1037 | if (message.entityId === view?.localEntityId) |
| 1038 | { |
| 1039 | const name = message.displayName ?? message.skillId ?? "Skill"; |
| 1040 | toasts?.show(`${name} level ${message.level}!`); |
| 1041 | } |
| 1042 | break; |
| 1043 | case MessageType.playerDied: |
| 1044 | clearAllPendingActions(); |
| 1045 | contextMenu?.hide(); |
| 1046 | death?.show(); |
| 1047 | break; |
| 1048 | case MessageType.playerRespawned: |
| 1049 | death?.hide(); |
| 1050 | view?.updateEntityHealth(message.entityId, message.health, message.maxHealth); |
| 1051 | if (message.stamina != null || message.maxStamina != null) |
| 1052 | { |
| 1053 | staminaUi?.apply({ |
| 1054 | current: message.stamina ?? staminaUi.getCurrent?.(), |
| 1055 | max: message.maxStamina ?? staminaUi.getMax?.() |
| 1056 | }); |
| 1057 | } |
| 1058 | break; |
| 1059 | case MessageType.error: |
| 1060 | if (playing) |
| 1061 | { toasts?.show(message.message); } |
| 1062 | else |
| 1063 | { showError(message.message); } |
| 1064 | social?.showError(message.message); |
| 1065 | break; |
| 1066 | case MessageType.toast: |
| 1067 | if (playing) |
| 1068 | { toasts?.show(message.message); } |
| 1069 | break; |
| 1070 | case MessageType.forceLogout: |
| 1071 | returnToMenu(message.reason); |
| 1072 | break; |
| 1073 | } |
| 1074 | }, { |
| 1075 | onClose (event) |
| 1076 | { |
| 1077 | if (playing) |
| 1078 | { returnToMenu(event.reason || "Disconnected from server."); } |
| 1079 | } |
| 1080 | }); |
| 1081 | await socket.connect(); |
| 1082 | if (typeof window !== "undefined") |
| 1083 | { |
| 1084 | window.__craftDev = (stationId = CraftingStationId.craftingBench) => |
| 1085 | { |
| 1086 | crafting?.openStation(stationId); |
| 1087 | }; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | function returnToMenu (reason = "") |
| 1092 | { |
| 1093 | playing = false; |
| 1094 | clearAllPendingActions(); |
| 1095 | social?.reset(); |
| 1096 | chat?.reset(); |
| 1097 | inventory?.reset(); |
| 1098 | crafting?.reset(); |
| 1099 | alchemy?.reset(); |
| 1100 | staminaUi?.hide(); |
| 1101 | skills?.hide(); |
| 1102 | contextMenu?.hide(); |
| 1103 | death?.reset(); |
| 1104 | shop?.close({ notifyServer: false }); |
| 1105 | hideWorldTooltip(); |
| 1106 | if (view) |
| 1107 | { |
| 1108 | for (const key of [...(view.chunks?.keys?.() ?? [])]) |
| 1109 | { |
| 1110 | const [chunkX, chunkY] = key.split(",").map(Number); |
| 1111 | view.removeChunk(chunkX, chunkY); |
| 1112 | } |
| 1113 | } |
| 1114 | overlay.hidden = false; |
| 1115 | hud.hidden = true; |
| 1116 | if (reason) |
| 1117 | { statusEl.textContent = reason; } |
| 1118 | } |
| 1119 | |
| 1120 | document.getElementById("create-form").addEventListener("submit", (event) => |
| 1121 | { |
| 1122 | event.preventDefault(); |
| 1123 | showError(""); |
| 1124 | socket.send({ |
| 1125 | type: MessageType.createCharacter, |
| 1126 | firstName: document.getElementById("first-name").value, |
| 1127 | lastName: document.getElementById("last-name").value, |
| 1128 | sex: document.getElementById("sex").value |
| 1129 | }); |
| 1130 | }); |
| 1131 | |
| 1132 | document.getElementById("leave").addEventListener("click", () => |
| 1133 | { |
| 1134 | socket.send({ type: MessageType.leaveWorld }); |
| 1135 | returnToMenu(); |
| 1136 | }); |
| 1137 | |
| 1138 | main().catch((error) => |
| 1139 | { |
| 1140 | showError(error.message); |
| 1141 | console.error(error); |
| 1142 | }); |
| 1143 | |
| 1144 | function isBankTile (view, tileX, tileY) |
| 1145 | { |
| 1146 | return blockTypeAt(view, tileX, tileY) === BlockType.bank; |
| 1147 | } |
| 1148 | |
| 1149 | function isMarketplaceTile (view, tileX, tileY) |
| 1150 | { |
| 1151 | return blockTypeAt(view, tileX, tileY) === BlockType.marketplace; |
| 1152 | } |
| 1153 | |
| 1154 | function isCauldronTile (view, tileX, tileY) |
| 1155 | { |
| 1156 | return blockTypeAt(view, tileX, tileY) === BlockType.cauldron; |
| 1157 | } |
| 1158 | |
| 1159 | function isFurnaceTile (view, tileX, tileY) |
| 1160 | { |
| 1161 | return blockTypeAt(view, tileX, tileY) === BlockType.furnace; |
| 1162 | } |
| 1163 | |
| 1164 | function isAnvilTile (view, tileX, tileY) |
| 1165 | { |
| 1166 | return blockTypeAt(view, tileX, tileY) === BlockType.anvil; |
| 1167 | } |
| 1168 | |
| 1169 | function isWoodworkingBenchTile (view, tileX, tileY) |
| 1170 | { |
| 1171 | return blockTypeAt(view, tileX, tileY) === BlockType.woodworkingBench; |
| 1172 | } |
| 1173 | |
| 1174 | function isCraftingBenchTile (view, tileX, tileY) |
| 1175 | { |
| 1176 | return blockTypeAt(view, tileX, tileY) === BlockType.craftingBench; |
| 1177 | } |
| 1178 | |
| 1179 | function isSpinningWheelTile (view, tileX, tileY) |
| 1180 | { |
| 1181 | return blockTypeAt(view, tileX, tileY) === BlockType.spinningWheel; |
| 1182 | } |
| 1183 | |
| 1184 | function isLoomTile (view, tileX, tileY) |
| 1185 | { |
| 1186 | return blockTypeAt(view, tileX, tileY) === BlockType.loom; |
| 1187 | } |
| 1188 | |
| 1189 | function isTailorBenchTile (view, tileX, tileY) |
| 1190 | { |
| 1191 | return blockTypeAt(view, tileX, tileY) === BlockType.tailorBench; |
| 1192 | } |
| 1193 | |
| 1194 | function isOreBoulderTile (view, tileX, tileY) |
| 1195 | { |
| 1196 | const block = blockAt(view, tileX, tileY); |
| 1197 | return block?.typeId === BlockType.boulder && isOreBoulderState(block.state); |
| 1198 | } |
| 1199 | |
| 1200 | function isChopableTreeTile (view, tileX, tileY) |
| 1201 | { |
| 1202 | return isChopableTreeBlock(blockAt(view, tileX, tileY)); |
| 1203 | } |
| 1204 | |
| 1205 | function isPickablePlantTile (view, tileX, tileY) |
| 1206 | { |
| 1207 | return isPickablePlantBlock(blockAt(view, tileX, tileY)); |
| 1208 | } |
| 1209 | |
| 1210 | function hideWorldTooltip () |
| 1211 | { |
| 1212 | if (worldTooltip) |
| 1213 | { worldTooltip.hidden = true; } |
| 1214 | } |
| 1215 | |
| 1216 | /** Cursor tip for gatherable resource nodes (ores now; trees / fish / herbs later). */ |
| 1217 | function updateWorldTooltip () |
| 1218 | { |
| 1219 | if (!worldTooltip || !view || !scene || uiBlocked()) |
| 1220 | { |
| 1221 | hideWorldTooltip(); |
| 1222 | return; |
| 1223 | } |
| 1224 | const canvas = scene.canvas; |
| 1225 | const rect = canvas.getBoundingClientRect(); |
| 1226 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 1227 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 1228 | if (mouseX < 0 || mouseY < 0 || mouseX >= rect.width || mouseY >= rect.height) |
| 1229 | { |
| 1230 | hideWorldTooltip(); |
| 1231 | return; |
| 1232 | } |
| 1233 | const entity = view.entityAtScreen(mouseX, mouseY); |
| 1234 | if (entity && entity.entityId !== view.localEntityId) |
| 1235 | { |
| 1236 | hideWorldTooltip(); |
| 1237 | return; |
| 1238 | } |
| 1239 | const tile = view.screenToTile(mouseX, mouseY); |
| 1240 | const label = resourceNodeDisplayName(blockAt(view, tile.tileX, tile.tileY)); |
| 1241 | if (!label) |
| 1242 | { |
| 1243 | hideWorldTooltip(); |
| 1244 | return; |
| 1245 | } |
| 1246 | worldTooltip.hidden = false; |
| 1247 | worldTooltip.textContent = label; |
| 1248 | worldTooltip.style.left = `${GameInput.mousePosition.x + 14}px`; |
| 1249 | worldTooltip.style.top = `${GameInput.mousePosition.y + 16}px`; |
| 1250 | } |
| 1251 | |
| 1252 | function blockTypeAt (view, tileX, tileY) |
| 1253 | { |
| 1254 | return blockAt(view, tileX, tileY)?.typeId ?? null; |
| 1255 | } |
| 1256 | |
| 1257 | function blockAt (view, tileX, tileY) |
| 1258 | { |
| 1259 | const chunkX = Math.floor(tileX / CHUNK_SIZE); |
| 1260 | const chunkY = Math.floor(tileY / CHUNK_SIZE); |
| 1261 | const snapshot = view.chunks.get(`${chunkX},${chunkY}`); |
| 1262 | if (!snapshot) |
| 1263 | { return null; } |
| 1264 | const localX = tileX - chunkX * CHUNK_SIZE; |
| 1265 | const localY = tileY - chunkY * CHUNK_SIZE; |
| 1266 | return snapshot.blocks[localY * CHUNK_SIZE + localX] ?? null; |
| 1267 | } |
| 1268 | |
| 1269 | function groundTypeAt (view, tileX, tileY) |
| 1270 | { |
| 1271 | const chunkX = Math.floor(tileX / CHUNK_SIZE); |
| 1272 | const chunkY = Math.floor(tileY / CHUNK_SIZE); |
| 1273 | const snapshot = view.chunks.get(`${chunkX},${chunkY}`); |
| 1274 | if (!snapshot) |
| 1275 | { return null; } |
| 1276 | const localX = tileX - chunkX * CHUNK_SIZE; |
| 1277 | const localY = tileY - chunkY * CHUNK_SIZE; |
| 1278 | return normalizeGround(snapshot.grounds?.[localY * CHUNK_SIZE + localX]).typeId; |
| 1279 | } |
| 1280 | |
| 1281 | /** Best-effort client filter for approach destinations (server still authorizes paths). */ |
| 1282 | function isClientApproachable (tileX, tileY) |
| 1283 | { |
| 1284 | if (!view) |
| 1285 | { return true; } |
| 1286 | const groundId = groundTypeAt(view, tileX, tileY); |
| 1287 | const groundDef = groundId != null ? GROUND_DEFINITIONS[groundId] : null; |
| 1288 | if (groundDef && !groundDef.passable) |
| 1289 | { return false; } |
| 1290 | const block = blockAt(view, tileX, tileY); |
| 1291 | if (block?.typeId != null) |
| 1292 | { |
| 1293 | const blockDef = BLOCK_DEFINITIONS[block.typeId]; |
| 1294 | if (blockDef && !blockDef.passable) |
| 1295 | { return false; } |
| 1296 | } |
| 1297 | for (const entity of view.entitiesAtTile(tileX, tileY)) |
| 1298 | { |
| 1299 | if (entity.entityId === view.localEntityId) |
| 1300 | { continue; } |
| 1301 | if (entity.untargetable || entity.freeFlight) |
| 1302 | { continue; } |
| 1303 | return false; |
| 1304 | } |
| 1305 | return true; |
| 1306 | } |
| 1307 | |
| 1308 | function chebyshev (ax, ay, bx, by) |
| 1309 | { |
| 1310 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 1311 | } |