| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { BlockType, woodTierForTreeBlock, isChopableTreeBlock, plantGatherForBlock } from "../../../shared/definitions/tiles.js"; |
| 3 | import { itemDefinition, ItemId } from "../../../shared/definitions/items.js"; |
| 4 | import { SkillId } from "../../../shared/definitions/skills.js"; |
| 5 | import { |
| 6 | materialFromOreBoulderState, |
| 7 | oreItemIdForMaterial, |
| 8 | miningDurationMs, |
| 9 | miningXpForMaterial, |
| 10 | oreExhaustDurationMs, |
| 11 | oreBoulderState, |
| 12 | EXHAUSTED_ORE_BOULDER_STATE, |
| 13 | resolveMiningPickaxe, |
| 14 | resolveWoodcuttingAxe, |
| 15 | resolvePlantClippers, |
| 16 | woodcuttingDurationMs, |
| 17 | woodcuttingXpForWood, |
| 18 | treeExhaustDurationMs, |
| 19 | logItemIdForWoodTier, |
| 20 | herbalismDurationMs, |
| 21 | herbalismXpForPick, |
| 22 | PLANT_EXHAUST_MS |
| 23 | } from "../../../shared/definitions/gathering.js"; |
| 24 | import { rawClothItemId } from "../../../shared/definitions/clothItems.js"; |
| 25 | import { ClothTier } from "../../../shared/definitions/materialTiers.js"; |
| 26 | import { |
| 27 | flowerItemInstance, |
| 28 | flowerColorIdFromBlockState, |
| 29 | flowerBlockState, |
| 30 | randomFlowerColorId |
| 31 | } from "../../../shared/definitions/dyes.js"; |
| 32 | import { insertStack } from "../../../shared/items/inventory.js"; |
| 33 | import { attachOrigins, GatherOriginVerb, gatherOrigin } from "../../../shared/items/origin.js"; |
| 34 | import { applySkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js"; |
| 35 | import { seasonIdAt, treeStumpStateForSeason, SeasonId } from "../../../shared/world/season.js"; |
| 36 | import { bagView } from "../items/bags.js"; |
| 37 | import { chebyshev, clearEngage } from "../combat/service.js"; |
| 38 | import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 39 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 40 | |
| 41 | const GATHER_SWING_INTERVAL_MS = 900; |
| 42 | |
| 43 | function userError (message) |
| 44 | { |
| 45 | const error = new Error(message); |
| 46 | error.userFacing = true; |
| 47 | return error; |
| 48 | } |
| 49 | |
| 50 | function characterDisplayName (world, handle) |
| 51 | { |
| 52 | const appearance = world.ecs.get(handle, Component.appearance); |
| 53 | const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim(); |
| 54 | return name || "someone"; |
| 55 | } |
| 56 | |
| 57 | function ensureOreRespawns (world) |
| 58 | { |
| 59 | if (!world.oreRespawns) |
| 60 | { world.oreRespawns = new Map(); } |
| 61 | return world.oreRespawns; |
| 62 | } |
| 63 | |
| 64 | function ensureTreeRespawns (world) |
| 65 | { |
| 66 | if (!world.treeRespawns) |
| 67 | { world.treeRespawns = new Map(); } |
| 68 | return world.treeRespawns; |
| 69 | } |
| 70 | |
| 71 | function ensurePlantRespawns (world) |
| 72 | { |
| 73 | if (!world.plantRespawns) |
| 74 | { world.plantRespawns = new Map(); } |
| 75 | return world.plantRespawns; |
| 76 | } |
| 77 | |
| 78 | function blockAt (world, tileX, tileY) |
| 79 | { |
| 80 | const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY)); |
| 81 | return data?.blocks?.[tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX)] ?? null; |
| 82 | } |
| 83 | |
| 84 | function setBlockState (world, tileX, tileY, typeId, state) |
| 85 | { |
| 86 | const chunkX = tileToChunk(tileX); |
| 87 | const chunkY = tileToChunk(tileY); |
| 88 | const localX = tileToLocal(tileX); |
| 89 | const localY = tileToLocal(tileY); |
| 90 | const data = world.chunkData(chunkX, chunkY); |
| 91 | const index = localY * CHUNK_SIZE + localX; |
| 92 | const prev = data?.blocks?.[index]; |
| 93 | if (!prev || prev.typeId !== typeId) |
| 94 | { return null; } |
| 95 | const block = { typeId: prev.typeId, state, config: prev.config ?? null }; |
| 96 | data.blocks[index] = block; |
| 97 | return { |
| 98 | type: "chunk-delta", |
| 99 | chunkX, |
| 100 | chunkY, |
| 101 | tileX, |
| 102 | tileY, |
| 103 | localX, |
| 104 | localY, |
| 105 | block |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | function setBoulderState (world, tileX, tileY, state) |
| 110 | { |
| 111 | return setBlockState(world, tileX, tileY, BlockType.boulder, state); |
| 112 | } |
| 113 | |
| 114 | function setTreeState (world, tileX, tileY, typeId, state) |
| 115 | { |
| 116 | return setBlockState(world, tileX, tileY, typeId, state); |
| 117 | } |
| 118 | |
| 119 | function clearBlock (world, tileX, tileY, typeId) |
| 120 | { |
| 121 | const chunkX = tileToChunk(tileX); |
| 122 | const chunkY = tileToChunk(tileY); |
| 123 | const localX = tileToLocal(tileX); |
| 124 | const localY = tileToLocal(tileY); |
| 125 | const data = world.chunkData(chunkX, chunkY); |
| 126 | const index = localY * CHUNK_SIZE + localX; |
| 127 | const prev = data?.blocks?.[index]; |
| 128 | if (!prev || prev.typeId !== typeId) |
| 129 | { return null; } |
| 130 | data.blocks[index] = null; |
| 131 | return { |
| 132 | type: "chunk-delta", |
| 133 | chunkX, |
| 134 | chunkY, |
| 135 | tileX, |
| 136 | tileY, |
| 137 | localX, |
| 138 | localY, |
| 139 | block: null |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | function placeBlock (world, tileX, tileY, typeId, state) |
| 144 | { |
| 145 | const chunkX = tileToChunk(tileX); |
| 146 | const chunkY = tileToChunk(tileY); |
| 147 | const localX = tileToLocal(tileX); |
| 148 | const localY = tileToLocal(tileY); |
| 149 | const data = world.chunkData(chunkX, chunkY); |
| 150 | const index = localY * CHUNK_SIZE + localX; |
| 151 | const block = { typeId, state, config: null }; |
| 152 | data.blocks[index] = block; |
| 153 | return { |
| 154 | type: "chunk-delta", |
| 155 | chunkX, |
| 156 | chunkY, |
| 157 | tileX, |
| 158 | tileY, |
| 159 | localX, |
| 160 | localY, |
| 161 | block |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | function pickaxeForOre (world, handle, materialId) |
| 166 | { |
| 167 | const inventory = world.ecs.get(handle, Component.inventory); |
| 168 | const equipment = world.ecs.get(handle, Component.equipment); |
| 169 | const skills = world.ecs.get(handle, Component.skills); |
| 170 | return resolveMiningPickaxe({ |
| 171 | inventorySlots: inventory?.slots, |
| 172 | equipmentSlots: equipment?.slots, |
| 173 | skills, |
| 174 | materialId |
| 175 | }); |
| 176 | } |
| 177 | |
| 178 | function axeForWood (world, handle, woodTier) |
| 179 | { |
| 180 | const inventory = world.ecs.get(handle, Component.inventory); |
| 181 | const equipment = world.ecs.get(handle, Component.equipment); |
| 182 | const skills = world.ecs.get(handle, Component.skills); |
| 183 | return resolveWoodcuttingAxe({ |
| 184 | inventorySlots: inventory?.slots, |
| 185 | equipmentSlots: equipment?.slots, |
| 186 | skills, |
| 187 | woodTier |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | function clearGather (ecs, handle) |
| 192 | { |
| 193 | if (ecs.has(handle, Component.gather)) |
| 194 | { ecs.remove(handle, Component.gather); } |
| 195 | } |
| 196 | |
| 197 | export function cancelGather (ecs, handle) |
| 198 | { |
| 199 | clearGather(ecs, handle); |
| 200 | } |
| 201 | |
| 202 | /** Drop scheduled ore respawns when authored chunk data is replaced or unloaded. */ |
| 203 | export function clearOreRespawnsForChunk (world, chunkX, chunkY) |
| 204 | { |
| 205 | const respawns = world.oreRespawns; |
| 206 | if (!respawns?.size) |
| 207 | { return; } |
| 208 | for (const key of [...respawns.keys()]) |
| 209 | { |
| 210 | const [tileX, tileY] = key.split(",").map(Number); |
| 211 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 212 | { respawns.delete(key); } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** Drop scheduled tree respawns when authored chunk data is replaced or unloaded. */ |
| 217 | export function clearTreeRespawnsForChunk (world, chunkX, chunkY) |
| 218 | { |
| 219 | const respawns = world.treeRespawns; |
| 220 | if (!respawns?.size) |
| 221 | { return; } |
| 222 | for (const key of [...respawns.keys()]) |
| 223 | { |
| 224 | const [tileX, tileY] = key.split(",").map(Number); |
| 225 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 226 | { respawns.delete(key); } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | export function clearPlantRespawnsForChunk (world, chunkX, chunkY) |
| 231 | { |
| 232 | const respawns = world.plantRespawns; |
| 233 | if (!respawns?.size) |
| 234 | { return; } |
| 235 | for (const key of [...respawns.keys()]) |
| 236 | { |
| 237 | const [tileX, tileY] = key.split(",").map(Number); |
| 238 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 239 | { respawns.delete(key); } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Restore exhausted ore boulders whose timers have elapsed. |
| 245 | */ |
| 246 | export function stepOreRespawns (world, now) |
| 247 | { |
| 248 | const respawns = world.oreRespawns; |
| 249 | if (!respawns?.size) |
| 250 | { return { events: [] }; } |
| 251 | const events = []; |
| 252 | for (const [key, entry] of [...respawns.entries()]) |
| 253 | { |
| 254 | if (now < entry.respawnsAt) |
| 255 | { continue; } |
| 256 | respawns.delete(key); |
| 257 | const delta = setBoulderState(world, entry.tileX, entry.tileY, oreBoulderState(entry.materialId)); |
| 258 | if (delta) |
| 259 | { events.push(delta); } |
| 260 | } |
| 261 | return { events }; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Restore exhausted trees whose timers have elapsed (live seasonal look). |
| 266 | */ |
| 267 | export function stepTreeRespawns (world, now) |
| 268 | { |
| 269 | const respawns = world.treeRespawns; |
| 270 | if (!respawns?.size) |
| 271 | { return { events: [] }; } |
| 272 | const events = []; |
| 273 | for (const [key, entry] of [...respawns.entries()]) |
| 274 | { |
| 275 | if (now < entry.respawnsAt) |
| 276 | { continue; } |
| 277 | respawns.delete(key); |
| 278 | const delta = setTreeState( |
| 279 | world, |
| 280 | entry.tileX, |
| 281 | entry.tileY, |
| 282 | entry.typeId, |
| 283 | SeasonId.summer |
| 284 | ); |
| 285 | if (delta) |
| 286 | { events.push(delta); } |
| 287 | } |
| 288 | return { events }; |
| 289 | } |
| 290 | |
| 291 | export function stepPlantRespawns (world, now) |
| 292 | { |
| 293 | const respawns = world.plantRespawns; |
| 294 | if (!respawns?.size) |
| 295 | { return { events: [] }; } |
| 296 | const events = []; |
| 297 | for (const [key, entry] of [...respawns.entries()]) |
| 298 | { |
| 299 | if (now < entry.respawnsAt) |
| 300 | { continue; } |
| 301 | respawns.delete(key); |
| 302 | const existing = blockAt(world, entry.tileX, entry.tileY); |
| 303 | if (existing) |
| 304 | { continue; } |
| 305 | const state = entry.typeId === BlockType.flower |
| 306 | ? flowerBlockState(randomFlowerColorId()) |
| 307 | : (entry.state ?? "cotton-plant"); |
| 308 | const delta = placeBlock(world, entry.tileX, entry.tileY, entry.typeId, state); |
| 309 | if (delta) |
| 310 | { events.push(delta); } |
| 311 | } |
| 312 | return { events }; |
| 313 | } |
| 314 | |
| 315 | function beginGatherSwing (world, handle, entityId, position, gatherFields, now) |
| 316 | { |
| 317 | clearEngage(world.ecs, entityId); |
| 318 | const path = world.ecs.get(handle, Component.path); |
| 319 | if (path) |
| 320 | { path.tiles = []; } |
| 321 | world.ecs.add(handle, Component.gather, { |
| 322 | ...gatherFields, |
| 323 | startedAt: now, |
| 324 | finishesAt: now + gatherFields.durationMs, |
| 325 | nextSwingAt: now |
| 326 | }); |
| 327 | return { |
| 328 | events: [{ |
| 329 | type: "gather-swing", |
| 330 | attackerId: entityId, |
| 331 | tileX: gatherFields.tileX, |
| 332 | tileY: gatherFields.tileY, |
| 333 | fromTileX: position.tileX, |
| 334 | fromTileY: position.tileY, |
| 335 | toolItemId: gatherFields.toolItemId ?? null, |
| 336 | swipe: false |
| 337 | }] |
| 338 | }; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Start mining an ore boulder. Returns events / private bag updates when complete via stepGather. |
| 343 | */ |
| 344 | export function startMine (world, sessionId, characterId, tileX, tileY, now) |
| 345 | { |
| 346 | const entityId = world.characterEntities.get(characterId); |
| 347 | if (entityId == null) |
| 348 | { throw userError("Character is not in the world."); } |
| 349 | const handle = world.ecs.handleOf(entityId); |
| 350 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 351 | if (!identity || identity.sessionId !== sessionId) |
| 352 | { throw userError("Character is not in the world."); } |
| 353 | if (world.ecs.has(handle, Component.dead)) |
| 354 | { throw userError("You are dead."); } |
| 355 | |
| 356 | const position = world.ecs.get(handle, Component.position); |
| 357 | if (chebyshev(position.tileX, position.tileY, tileX, tileY) > 1) |
| 358 | { throw userError("You need to stand next to the rock."); } |
| 359 | |
| 360 | const block = blockAt(world, tileX, tileY); |
| 361 | if (!block || block.typeId !== BlockType.boulder) |
| 362 | { throw userError("There is nothing to mine there."); } |
| 363 | const materialId = materialFromOreBoulderState(block.state); |
| 364 | if (!materialId) |
| 365 | { |
| 366 | throw userError(block.state === EXHAUSTED_ORE_BOULDER_STATE |
| 367 | ? "That rock is exhausted." |
| 368 | : "That rock has no ore."); |
| 369 | } |
| 370 | |
| 371 | const pickaxe = pickaxeForOre(world, handle, materialId); |
| 372 | if (!pickaxe.ok) |
| 373 | { throw userError(pickaxe.reason); } |
| 374 | |
| 375 | const durationMs = miningDurationMs(materialId, pickaxe.tool.tier); |
| 376 | const toolItemId = pickaxe.tool.stack?.itemId ?? null; |
| 377 | return beginGatherSwing(world, handle, entityId, position, { |
| 378 | kind: "mining", |
| 379 | tileX, |
| 380 | tileY, |
| 381 | materialId, |
| 382 | toolTier: pickaxe.tool.tier, |
| 383 | toolItemId, |
| 384 | durationMs |
| 385 | }, now); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Start chopping a tree. Completes via stepGather (stump + log + woodcutting XP). |
| 390 | */ |
| 391 | export function startChop (world, sessionId, characterId, tileX, tileY, now) |
| 392 | { |
| 393 | const entityId = world.characterEntities.get(characterId); |
| 394 | if (entityId == null) |
| 395 | { throw userError("Character is not in the world."); } |
| 396 | const handle = world.ecs.handleOf(entityId); |
| 397 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 398 | if (!identity || identity.sessionId !== sessionId) |
| 399 | { throw userError("Character is not in the world."); } |
| 400 | if (world.ecs.has(handle, Component.dead)) |
| 401 | { throw userError("You are dead."); } |
| 402 | |
| 403 | const position = world.ecs.get(handle, Component.position); |
| 404 | if (chebyshev(position.tileX, position.tileY, tileX, tileY) > 1) |
| 405 | { throw userError("You need to stand next to the tree."); } |
| 406 | |
| 407 | const block = blockAt(world, tileX, tileY); |
| 408 | if (!isChopableTreeBlock(block)) |
| 409 | { |
| 410 | throw userError(block && woodTierForTreeBlock(block.typeId) != null |
| 411 | ? "That tree is already felled." |
| 412 | : "There is nothing to chop there."); |
| 413 | } |
| 414 | const woodTier = woodTierForTreeBlock(block.typeId); |
| 415 | const axe = axeForWood(world, handle, woodTier); |
| 416 | if (!axe.ok) |
| 417 | { throw userError(axe.reason); } |
| 418 | |
| 419 | const durationMs = woodcuttingDurationMs(woodTier, axe.tool.tier); |
| 420 | const toolItemId = axe.tool.stack?.itemId ?? null; |
| 421 | return beginGatherSwing(world, handle, entityId, position, { |
| 422 | kind: "woodcutting", |
| 423 | tileX, |
| 424 | tileY, |
| 425 | typeId: block.typeId, |
| 426 | woodTier, |
| 427 | toolTier: axe.tool.tier, |
| 428 | toolItemId, |
| 429 | durationMs |
| 430 | }, now); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Pick a plant node (flower / cotton; no tools required). |
| 435 | */ |
| 436 | export function startPick (world, sessionId, characterId, tileX, tileY, now) |
| 437 | { |
| 438 | const entityId = world.characterEntities.get(characterId); |
| 439 | if (entityId == null) |
| 440 | { throw userError("Character is not in the world."); } |
| 441 | const handle = world.ecs.handleOf(entityId); |
| 442 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 443 | if (!identity || identity.sessionId !== sessionId) |
| 444 | { throw userError("Character is not in the world."); } |
| 445 | if (world.ecs.has(handle, Component.dead)) |
| 446 | { throw userError("You are dead."); } |
| 447 | |
| 448 | const position = world.ecs.get(handle, Component.position); |
| 449 | if (chebyshev(position.tileX, position.tileY, tileX, tileY) > 1) |
| 450 | { throw userError("You need to stand next to the plant."); } |
| 451 | |
| 452 | const block = blockAt(world, tileX, tileY); |
| 453 | const plant = plantGatherForBlock(block); |
| 454 | if (!plant) |
| 455 | { throw userError("There is nothing to pick there."); } |
| 456 | |
| 457 | const inventory = world.ecs.get(handle, Component.inventory); |
| 458 | const equipment = world.ecs.get(handle, Component.equipment); |
| 459 | const skills = world.ecs.get(handle, Component.skills); |
| 460 | const clippers = resolvePlantClippers({ |
| 461 | inventorySlots: inventory?.slots, |
| 462 | equipmentSlots: equipment?.slots, |
| 463 | skills, |
| 464 | requireTool: !!plant.requiresClippers |
| 465 | }); |
| 466 | if (!clippers.ok) |
| 467 | { throw userError(clippers.reason); } |
| 468 | |
| 469 | const toolTier = clippers.tool?.tier ?? null; |
| 470 | const durationMs = herbalismDurationMs(plant.kind, toolTier); |
| 471 | const toolItemId = clippers.tool?.stack?.itemId ?? null; |
| 472 | const prefixEvents = []; |
| 473 | let flowerColorId = null; |
| 474 | if (plant.kind === "flower") |
| 475 | { |
| 476 | flowerColorId = flowerColorIdFromBlockState(block.state); |
| 477 | if (!flowerColorId) |
| 478 | { |
| 479 | // Legacy / uncoloured placements: lock a colour before the pick starts. |
| 480 | flowerColorId = randomFlowerColorId(); |
| 481 | const delta = setBlockState( |
| 482 | world, |
| 483 | tileX, |
| 484 | tileY, |
| 485 | BlockType.flower, |
| 486 | flowerBlockState(flowerColorId) |
| 487 | ); |
| 488 | if (delta) |
| 489 | { prefixEvents.push(delta); } |
| 490 | } |
| 491 | } |
| 492 | const started = beginGatherSwing(world, handle, entityId, position, { |
| 493 | kind: "plant", |
| 494 | plantKind: plant.kind, |
| 495 | tileX, |
| 496 | tileY, |
| 497 | typeId: block.typeId, |
| 498 | flowerColorId, |
| 499 | toolTier, |
| 500 | toolItemId, |
| 501 | durationMs |
| 502 | }, now); |
| 503 | return { |
| 504 | ...started, |
| 505 | events: [...prefixEvents, ...(started.events ?? [])] |
| 506 | }; |
| 507 | } |
| 508 | |
| 509 | function pushBagUpdate (events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps = []) |
| 510 | { |
| 511 | privates.push({ |
| 512 | characterId: character.characterId, |
| 513 | bag: bagView(world.ecs, handle) |
| 514 | }); |
| 515 | if (skillsChanged) |
| 516 | { |
| 517 | events.push({ |
| 518 | type: "skills-updated", |
| 519 | entityId, |
| 520 | characterId: character.characterId, |
| 521 | skills: publicSkillsView(skills), |
| 522 | stored: storedSkillXp(skills), |
| 523 | levelUps |
| 524 | }); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | function completeMining (world, handle, entityId, character, gather, events, privates, now) |
| 529 | { |
| 530 | const oreItemId = oreItemIdForMaterial(gather.materialId); |
| 531 | const def = itemDefinition(oreItemId); |
| 532 | const inventory = world.ecs.get(handle, Component.inventory); |
| 533 | if (!oreItemId || !def || !inventory) |
| 534 | { |
| 535 | clearGather(world.ecs, handle); |
| 536 | return; |
| 537 | } |
| 538 | const origin = gatherOrigin(GatherOriginVerb.mined, characterDisplayName(world, handle)); |
| 539 | const stack = attachOrigins({ |
| 540 | itemId: oreItemId, |
| 541 | quantity: 1, |
| 542 | origins: [{ origin, quantity: 1 }] |
| 543 | }); |
| 544 | const inserted = insertStack(inventory.slots, stack, def); |
| 545 | if (!inserted.ok || inserted.remainder > 0) |
| 546 | { |
| 547 | clearGather(world.ecs, handle); |
| 548 | events.push({ |
| 549 | type: "command-error", |
| 550 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 551 | message: "Your inventory is full." |
| 552 | }); |
| 553 | return; |
| 554 | } |
| 555 | inventory.slots = inserted.slots; |
| 556 | |
| 557 | const skills = world.ecs.get(handle, Component.skills); |
| 558 | let skillsChanged = false; |
| 559 | const levelUps = []; |
| 560 | if (skills?.xp) |
| 561 | { |
| 562 | const result = applySkillXp(skills, SkillId.mining, miningXpForMaterial(gather.materialId)); |
| 563 | if (result.changed) |
| 564 | { skillsChanged = true; } |
| 565 | if (result.levelUp) |
| 566 | { levelUps.push(result.levelUp); } |
| 567 | } |
| 568 | |
| 569 | const exhausted = setBoulderState( |
| 570 | world, |
| 571 | gather.tileX, |
| 572 | gather.tileY, |
| 573 | EXHAUSTED_ORE_BOULDER_STATE |
| 574 | ); |
| 575 | if (exhausted) |
| 576 | { |
| 577 | events.push(exhausted); |
| 578 | ensureOreRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 579 | tileX: gather.tileX, |
| 580 | tileY: gather.tileY, |
| 581 | materialId: gather.materialId, |
| 582 | respawnsAt: now + oreExhaustDurationMs(gather.materialId) |
| 583 | }); |
| 584 | } |
| 585 | |
| 586 | clearGather(world.ecs, handle); |
| 587 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 588 | events.push({ |
| 589 | type: "gather-complete", |
| 590 | characterId: character.characterId, |
| 591 | kind: "mining", |
| 592 | materialId: gather.materialId, |
| 593 | itemId: oreItemId |
| 594 | }); |
| 595 | } |
| 596 | |
| 597 | function completeWoodcutting (world, handle, entityId, character, gather, events, privates, now) |
| 598 | { |
| 599 | const logItemId = logItemIdForWoodTier(gather.woodTier); |
| 600 | const def = itemDefinition(logItemId); |
| 601 | const inventory = world.ecs.get(handle, Component.inventory); |
| 602 | if (!logItemId || !def || !inventory) |
| 603 | { |
| 604 | clearGather(world.ecs, handle); |
| 605 | return; |
| 606 | } |
| 607 | const origin = gatherOrigin(GatherOriginVerb.felled, characterDisplayName(world, handle)); |
| 608 | const stack = attachOrigins({ |
| 609 | itemId: logItemId, |
| 610 | quantity: 1, |
| 611 | origins: [{ origin, quantity: 1 }] |
| 612 | }); |
| 613 | const inserted = insertStack(inventory.slots, stack, def); |
| 614 | if (!inserted.ok || inserted.remainder > 0) |
| 615 | { |
| 616 | clearGather(world.ecs, handle); |
| 617 | events.push({ |
| 618 | type: "command-error", |
| 619 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 620 | message: "Your inventory is full." |
| 621 | }); |
| 622 | return; |
| 623 | } |
| 624 | inventory.slots = inserted.slots; |
| 625 | |
| 626 | const skills = world.ecs.get(handle, Component.skills); |
| 627 | let skillsChanged = false; |
| 628 | const levelUps = []; |
| 629 | if (skills?.xp) |
| 630 | { |
| 631 | const result = applySkillXp(skills, SkillId.woodcutting, woodcuttingXpForWood(gather.woodTier)); |
| 632 | if (result.changed) |
| 633 | { skillsChanged = true; } |
| 634 | if (result.levelUp) |
| 635 | { levelUps.push(result.levelUp); } |
| 636 | } |
| 637 | |
| 638 | const stumpState = treeStumpStateForSeason(seasonIdAt(now)); |
| 639 | const exhausted = setTreeState( |
| 640 | world, |
| 641 | gather.tileX, |
| 642 | gather.tileY, |
| 643 | gather.typeId, |
| 644 | stumpState |
| 645 | ); |
| 646 | if (exhausted) |
| 647 | { |
| 648 | events.push(exhausted); |
| 649 | ensureTreeRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 650 | tileX: gather.tileX, |
| 651 | tileY: gather.tileY, |
| 652 | typeId: gather.typeId, |
| 653 | woodTier: gather.woodTier, |
| 654 | respawnsAt: now + treeExhaustDurationMs(gather.woodTier) |
| 655 | }); |
| 656 | } |
| 657 | |
| 658 | clearGather(world.ecs, handle); |
| 659 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 660 | events.push({ |
| 661 | type: "gather-complete", |
| 662 | characterId: character.characterId, |
| 663 | kind: "woodcutting", |
| 664 | woodTier: gather.woodTier, |
| 665 | itemId: logItemId |
| 666 | }); |
| 667 | } |
| 668 | |
| 669 | function completePlant (world, handle, entityId, character, gather, events, privates, now) |
| 670 | { |
| 671 | const inventory = world.ecs.get(handle, Component.inventory); |
| 672 | if (!inventory) |
| 673 | { |
| 674 | clearGather(world.ecs, handle); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | let itemId = null; |
| 679 | let instance = null; |
| 680 | if (gather.plantKind === "flower") |
| 681 | { |
| 682 | itemId = ItemId.flower; |
| 683 | const live = blockAt(world, gather.tileX, gather.tileY); |
| 684 | const colorId = gather.flowerColorId |
| 685 | ?? flowerColorIdFromBlockState(live?.state) |
| 686 | ?? randomFlowerColorId(); |
| 687 | instance = flowerItemInstance(colorId); |
| 688 | } |
| 689 | else if (gather.plantKind === "cotton") |
| 690 | { |
| 691 | itemId = rawClothItemId(ClothTier.cotton); |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | clearGather(world.ecs, handle); |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | const def = itemDefinition(itemId); |
| 700 | if (!def) |
| 701 | { |
| 702 | clearGather(world.ecs, handle); |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | const origin = gatherOrigin(GatherOriginVerb.picked, characterDisplayName(world, handle)); |
| 707 | const stack = attachOrigins({ |
| 708 | itemId, |
| 709 | quantity: 1, |
| 710 | instance, |
| 711 | origins: [{ origin, quantity: 1 }] |
| 712 | }); |
| 713 | const inserted = insertStack(inventory.slots, stack, def); |
| 714 | if (!inserted.ok || inserted.remainder > 0) |
| 715 | { |
| 716 | clearGather(world.ecs, handle); |
| 717 | events.push({ |
| 718 | type: "command-error", |
| 719 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 720 | message: "Your inventory is full." |
| 721 | }); |
| 722 | return; |
| 723 | } |
| 724 | inventory.slots = inserted.slots; |
| 725 | |
| 726 | const skills = world.ecs.get(handle, Component.skills); |
| 727 | let skillsChanged = false; |
| 728 | const levelUps = []; |
| 729 | if (skills?.xp) |
| 730 | { |
| 731 | const result = applySkillXp(skills, SkillId.herbalism, herbalismXpForPick(gather.plantKind)); |
| 732 | if (result.changed) |
| 733 | { skillsChanged = true; } |
| 734 | if (result.levelUp) |
| 735 | { levelUps.push(result.levelUp); } |
| 736 | } |
| 737 | |
| 738 | const delta = clearBlock(world, gather.tileX, gather.tileY, gather.typeId); |
| 739 | if (delta) |
| 740 | { events.push(delta); } |
| 741 | ensurePlantRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 742 | tileX: gather.tileX, |
| 743 | tileY: gather.tileY, |
| 744 | typeId: gather.typeId, |
| 745 | // Colour is chosen when the node refreshes (see stepPlantRespawns). |
| 746 | state: gather.plantKind === "flower" ? null : "cotton-plant", |
| 747 | respawnsAt: now + PLANT_EXHAUST_MS |
| 748 | }); |
| 749 | |
| 750 | clearGather(world.ecs, handle); |
| 751 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 752 | events.push({ |
| 753 | type: "gather-complete", |
| 754 | characterId: character.characterId, |
| 755 | kind: "plant", |
| 756 | plantKind: gather.plantKind, |
| 757 | itemId |
| 758 | }); |
| 759 | } |
| 760 | |
| 761 | export function stepGather (world, now) |
| 762 | { |
| 763 | const events = []; |
| 764 | const privates = []; |
| 765 | events.push(...stepOreRespawns(world, now).events); |
| 766 | events.push(...stepTreeRespawns(world, now).events); |
| 767 | events.push(...stepPlantRespawns(world, now).events); |
| 768 | |
| 769 | for (const entity of world.ecs.query(Component.gather, Component.position, Component.character)) |
| 770 | { |
| 771 | const { handle, gather, position, character, entityId } = entity; |
| 772 | if (world.ecs.has(handle, Component.dead)) |
| 773 | { |
| 774 | clearGather(world.ecs, handle); |
| 775 | continue; |
| 776 | } |
| 777 | if (chebyshev(position.tileX, position.tileY, gather.tileX, gather.tileY) > 1) |
| 778 | { |
| 779 | clearGather(world.ecs, handle); |
| 780 | continue; |
| 781 | } |
| 782 | const block = blockAt(world, gather.tileX, gather.tileY); |
| 783 | |
| 784 | if (gather.kind === "mining") |
| 785 | { |
| 786 | if (!block || block.typeId !== BlockType.boulder |
| 787 | || materialFromOreBoulderState(block.state) !== gather.materialId) |
| 788 | { |
| 789 | clearGather(world.ecs, handle); |
| 790 | continue; |
| 791 | } |
| 792 | } |
| 793 | else if (gather.kind === "woodcutting") |
| 794 | { |
| 795 | if (!block || block.typeId !== gather.typeId || !isChopableTreeBlock(block)) |
| 796 | { |
| 797 | clearGather(world.ecs, handle); |
| 798 | continue; |
| 799 | } |
| 800 | } |
| 801 | else if (gather.kind === "plant") |
| 802 | { |
| 803 | if (!block || block.typeId !== gather.typeId || !plantGatherForBlock(block)) |
| 804 | { |
| 805 | clearGather(world.ecs, handle); |
| 806 | continue; |
| 807 | } |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | clearGather(world.ecs, handle); |
| 812 | continue; |
| 813 | } |
| 814 | |
| 815 | if (now >= gather.nextSwingAt && now < gather.finishesAt) |
| 816 | { |
| 817 | gather.nextSwingAt = now + GATHER_SWING_INTERVAL_MS; |
| 818 | events.push({ |
| 819 | type: "gather-swing", |
| 820 | attackerId: entityId, |
| 821 | tileX: gather.tileX, |
| 822 | tileY: gather.tileY, |
| 823 | fromTileX: position.tileX, |
| 824 | fromTileY: position.tileY, |
| 825 | toolItemId: gather.toolItemId ?? null, |
| 826 | swipe: false |
| 827 | }); |
| 828 | } |
| 829 | |
| 830 | if (now < gather.finishesAt) |
| 831 | { continue; } |
| 832 | |
| 833 | if (gather.kind === "mining") |
| 834 | { completeMining(world, handle, entityId, character, gather, events, privates, now); } |
| 835 | else if (gather.kind === "woodcutting") |
| 836 | { completeWoodcutting(world, handle, entityId, character, gather, events, privates, now); } |
| 837 | else |
| 838 | { completePlant(world, handle, entityId, character, gather, events, privates, now); } |
| 839 | } |
| 840 | return { events, privates }; |
| 841 | } |
| 842 | |
| 843 | export function handleGatherCommand (world, sessionId, command, now) |
| 844 | { |
| 845 | switch (command.type) |
| 846 | { |
| 847 | case "mine": |
| 848 | return startMine(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 849 | case "chop": |
| 850 | return startChop(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 851 | case "pick": |
| 852 | return startPick(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 853 | case "cancel-gather": |
| 854 | { |
| 855 | const entityId = world.characterEntities.get(command.characterId); |
| 856 | if (entityId == null) |
| 857 | { return { events: [] }; } |
| 858 | const handle = world.ecs.handleOf(entityId); |
| 859 | clearGather(world.ecs, handle); |
| 860 | return { events: [] }; |
| 861 | } |
| 862 | default: |
| 863 | return null; |
| 864 | } |
| 865 | } |