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