| 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, publicSkillsPayload, 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 | minClothTier: plant.minClothTier ?? null |
| 474 | }); |
| 475 | if (!clippers.ok) |
| 476 | { throw userError(clippers.reason); } |
| 477 | |
| 478 | let herbId = null; |
| 479 | let herbTier = null; |
| 480 | if (plant.kind === "herb") |
| 481 | { |
| 482 | const gate = resolveHerbGather({ herbId: block.state, skills }); |
| 483 | if (!gate.ok) |
| 484 | { throw userError(gate.reason); } |
| 485 | herbId = gate.herb.herbId; |
| 486 | herbTier = gate.herb.tier; |
| 487 | } |
| 488 | |
| 489 | const toolTier = clippers.tool?.tier ?? null; |
| 490 | const durationMs = herbalismDurationMs(plant.kind, toolTier, herbTier); |
| 491 | const toolItemId = clippers.tool?.stack?.itemId ?? null; |
| 492 | const prefixEvents = []; |
| 493 | let flowerColorId = null; |
| 494 | if (plant.kind === "flower") |
| 495 | { |
| 496 | flowerColorId = flowerColorIdFromBlockState(block.state); |
| 497 | if (!flowerColorId) |
| 498 | { |
| 499 | // Legacy / uncoloured placements: lock a colour before the pick starts. |
| 500 | flowerColorId = flowerColorIdForTile(tileX, tileY); |
| 501 | const delta = setBlockState( |
| 502 | world, |
| 503 | tileX, |
| 504 | tileY, |
| 505 | BlockType.flower, |
| 506 | flowerBlockState(flowerColorId) |
| 507 | ); |
| 508 | if (delta) |
| 509 | { prefixEvents.push(delta); } |
| 510 | } |
| 511 | } |
| 512 | const started = beginGatherSwing(world, handle, entityId, position, { |
| 513 | kind: "plant", |
| 514 | plantKind: plant.kind, |
| 515 | herbId, |
| 516 | herbTier, |
| 517 | tileX, |
| 518 | tileY, |
| 519 | typeId: block.typeId, |
| 520 | flowerColorId, |
| 521 | toolTier, |
| 522 | toolItemId, |
| 523 | durationMs |
| 524 | }, now); |
| 525 | return { |
| 526 | ...started, |
| 527 | events: [...prefixEvents, ...(started.events ?? [])] |
| 528 | }; |
| 529 | } |
| 530 | |
| 531 | function pushBagUpdate (events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps = []) |
| 532 | { |
| 533 | privates.push({ |
| 534 | characterId: character.characterId, |
| 535 | bag: bagView(world.ecs, handle) |
| 536 | }); |
| 537 | if (skillsChanged) |
| 538 | { |
| 539 | events.push({ |
| 540 | type: "skills-updated", |
| 541 | entityId, |
| 542 | characterId: character.characterId, |
| 543 | ...publicSkillsPayload(skills), |
| 544 | stored: storedSkillXp(skills), |
| 545 | levelUps |
| 546 | }); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | function completeMining (world, handle, entityId, character, gather, events, privates, now) |
| 551 | { |
| 552 | const oreItemId = oreItemIdForMaterial(gather.materialId); |
| 553 | const def = itemDefinition(oreItemId); |
| 554 | const inventory = world.ecs.get(handle, Component.inventory); |
| 555 | if (!oreItemId || !def || !inventory) |
| 556 | { |
| 557 | clearGather(world.ecs, handle); |
| 558 | return; |
| 559 | } |
| 560 | const origin = gatherOrigin(GatherOriginVerb.mined, characterDisplayName(world, handle)); |
| 561 | const stack = attachOrigins({ |
| 562 | itemId: oreItemId, |
| 563 | quantity: 1, |
| 564 | origins: [{ origin, quantity: 1 }] |
| 565 | }); |
| 566 | const inserted = insertStack(inventory.slots, stack, def); |
| 567 | if (!inserted.ok || inserted.remainder > 0) |
| 568 | { |
| 569 | clearGather(world.ecs, handle); |
| 570 | events.push({ |
| 571 | type: "command-error", |
| 572 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 573 | message: "Your inventory is full." |
| 574 | }); |
| 575 | return; |
| 576 | } |
| 577 | inventory.slots = inserted.slots; |
| 578 | |
| 579 | const skills = world.ecs.get(handle, Component.skills); |
| 580 | let skillsChanged = false; |
| 581 | const levelUps = []; |
| 582 | if (skills?.xp) |
| 583 | { |
| 584 | const result = applySkillXp(skills, SkillId.mining, miningXpForMaterial(gather.materialId)); |
| 585 | if (result.changed) |
| 586 | { skillsChanged = true; } |
| 587 | if (result.levelUp) |
| 588 | { levelUps.push(result.levelUp); } |
| 589 | } |
| 590 | |
| 591 | const exhausted = setBoulderState( |
| 592 | world, |
| 593 | gather.tileX, |
| 594 | gather.tileY, |
| 595 | EXHAUSTED_ORE_BOULDER_STATE |
| 596 | ); |
| 597 | if (exhausted) |
| 598 | { |
| 599 | events.push(exhausted); |
| 600 | ensureOreRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 601 | tileX: gather.tileX, |
| 602 | tileY: gather.tileY, |
| 603 | materialId: gather.materialId, |
| 604 | respawnsAt: now + oreExhaustDurationMs(gather.materialId) |
| 605 | }); |
| 606 | } |
| 607 | |
| 608 | clearGather(world.ecs, handle); |
| 609 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 610 | events.push({ |
| 611 | type: "gather-complete", |
| 612 | characterId: character.characterId, |
| 613 | kind: "mining", |
| 614 | materialId: gather.materialId, |
| 615 | itemId: oreItemId |
| 616 | }); |
| 617 | } |
| 618 | |
| 619 | function completeWoodcutting (world, handle, entityId, character, gather, events, privates, now) |
| 620 | { |
| 621 | const logItemId = logItemIdForWoodTier(gather.woodTier); |
| 622 | const def = itemDefinition(logItemId); |
| 623 | const inventory = world.ecs.get(handle, Component.inventory); |
| 624 | if (!logItemId || !def || !inventory) |
| 625 | { |
| 626 | clearGather(world.ecs, handle); |
| 627 | return; |
| 628 | } |
| 629 | const origin = gatherOrigin(GatherOriginVerb.felled, characterDisplayName(world, handle)); |
| 630 | const stack = attachOrigins({ |
| 631 | itemId: logItemId, |
| 632 | quantity: 1, |
| 633 | origins: [{ origin, quantity: 1 }] |
| 634 | }); |
| 635 | const inserted = insertStack(inventory.slots, stack, def); |
| 636 | if (!inserted.ok || inserted.remainder > 0) |
| 637 | { |
| 638 | clearGather(world.ecs, handle); |
| 639 | events.push({ |
| 640 | type: "command-error", |
| 641 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 642 | message: "Your inventory is full." |
| 643 | }); |
| 644 | return; |
| 645 | } |
| 646 | inventory.slots = inserted.slots; |
| 647 | |
| 648 | const skills = world.ecs.get(handle, Component.skills); |
| 649 | let skillsChanged = false; |
| 650 | const levelUps = []; |
| 651 | if (skills?.xp) |
| 652 | { |
| 653 | const result = applySkillXp(skills, SkillId.woodcutting, woodcuttingXpForWood(gather.woodTier)); |
| 654 | if (result.changed) |
| 655 | { skillsChanged = true; } |
| 656 | if (result.levelUp) |
| 657 | { levelUps.push(result.levelUp); } |
| 658 | } |
| 659 | |
| 660 | const stumpState = treeStumpStateForSeason(seasonIdAt(now)); |
| 661 | const exhausted = setTreeState( |
| 662 | world, |
| 663 | gather.tileX, |
| 664 | gather.tileY, |
| 665 | gather.typeId, |
| 666 | stumpState |
| 667 | ); |
| 668 | if (exhausted) |
| 669 | { |
| 670 | events.push(exhausted); |
| 671 | ensureTreeRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 672 | tileX: gather.tileX, |
| 673 | tileY: gather.tileY, |
| 674 | typeId: gather.typeId, |
| 675 | woodTier: gather.woodTier, |
| 676 | respawnsAt: now + scaleRegrowDurationMs(treeExhaustDurationMs(gather.woodTier), now) |
| 677 | }); |
| 678 | } |
| 679 | |
| 680 | clearGather(world.ecs, handle); |
| 681 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 682 | events.push({ |
| 683 | type: "gather-complete", |
| 684 | characterId: character.characterId, |
| 685 | kind: "woodcutting", |
| 686 | woodTier: gather.woodTier, |
| 687 | itemId: logItemId |
| 688 | }); |
| 689 | } |
| 690 | |
| 691 | function completePlant (world, handle, entityId, character, gather, events, privates, now) |
| 692 | { |
| 693 | const inventory = world.ecs.get(handle, Component.inventory); |
| 694 | if (!inventory) |
| 695 | { |
| 696 | clearGather(world.ecs, handle); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | let itemId = null; |
| 701 | let instance = null; |
| 702 | if (gather.plantKind === "flower") |
| 703 | { |
| 704 | itemId = ItemId.flower; |
| 705 | const live = blockAt(world, gather.tileX, gather.tileY); |
| 706 | const colorId = gather.flowerColorId |
| 707 | ?? flowerColorIdFromBlockState(live?.state) |
| 708 | ?? randomFlowerColorId(); |
| 709 | instance = flowerItemInstance(colorId); |
| 710 | } |
| 711 | else if (gather.plantKind === "cotton") |
| 712 | { |
| 713 | itemId = rawClothItemId(ClothTier.cotton); |
| 714 | } |
| 715 | else if (gather.plantKind === "flax") |
| 716 | { |
| 717 | itemId = rawClothItemId(ClothTier.linen); |
| 718 | } |
| 719 | else if (gather.plantKind === "silk") |
| 720 | { |
| 721 | itemId = rawClothItemId(ClothTier.silk); |
| 722 | } |
| 723 | else if (gather.plantKind === "herb") |
| 724 | { |
| 725 | const herb = herbDefinition(gather.herbId); |
| 726 | itemId = herb?.itemId ?? null; |
| 727 | } |
| 728 | else |
| 729 | { |
| 730 | clearGather(world.ecs, handle); |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | const def = itemDefinition(itemId); |
| 735 | if (!def) |
| 736 | { |
| 737 | clearGather(world.ecs, handle); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | const origin = gatherOrigin(GatherOriginVerb.picked, characterDisplayName(world, handle)); |
| 742 | const stack = attachOrigins({ |
| 743 | itemId, |
| 744 | quantity: 1, |
| 745 | instance, |
| 746 | origins: [{ origin, quantity: 1 }] |
| 747 | }); |
| 748 | const inserted = insertStack(inventory.slots, stack, def); |
| 749 | if (!inserted.ok || inserted.remainder > 0) |
| 750 | { |
| 751 | clearGather(world.ecs, handle); |
| 752 | events.push({ |
| 753 | type: "command-error", |
| 754 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 755 | message: "Your inventory is full." |
| 756 | }); |
| 757 | return; |
| 758 | } |
| 759 | inventory.slots = inserted.slots; |
| 760 | |
| 761 | const skills = world.ecs.get(handle, Component.skills); |
| 762 | let skillsChanged = false; |
| 763 | const levelUps = []; |
| 764 | if (skills?.xp) |
| 765 | { |
| 766 | const result = applySkillXp(skills, SkillId.herbalism, herbalismXpForPick(gather.plantKind, gather.herbTier)); |
| 767 | if (result.changed) |
| 768 | { skillsChanged = true; } |
| 769 | if (result.levelUp) |
| 770 | { levelUps.push(result.levelUp); } |
| 771 | } |
| 772 | |
| 773 | const delta = clearBlock(world, gather.tileX, gather.tileY, gather.typeId); |
| 774 | if (delta) |
| 775 | { events.push(delta); } |
| 776 | ensurePlantRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 777 | tileX: gather.tileX, |
| 778 | tileY: gather.tileY, |
| 779 | typeId: gather.typeId, |
| 780 | // Colour is chosen when the node refreshes (see stepPlantRespawns). |
| 781 | state: gather.plantKind === "flower" |
| 782 | ? null |
| 783 | : (gather.plantKind === "herb" && isHerbId(gather.herbId) |
| 784 | ? gather.herbId |
| 785 | : defaultPlantState(gather.typeId)), |
| 786 | respawnsAt: now + scaleRegrowDurationMs(PLANT_EXHAUST_MS, now) |
| 787 | }); |
| 788 | |
| 789 | clearGather(world.ecs, handle); |
| 790 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 791 | events.push({ |
| 792 | type: "gather-complete", |
| 793 | characterId: character.characterId, |
| 794 | kind: "plant", |
| 795 | plantKind: gather.plantKind, |
| 796 | itemId |
| 797 | }); |
| 798 | } |
| 799 | |
| 800 | export function stepGather (world, now) |
| 801 | { |
| 802 | const events = []; |
| 803 | const privates = []; |
| 804 | events.push(...stepOreRespawns(world, now).events); |
| 805 | events.push(...stepTreeRespawns(world, now).events); |
| 806 | events.push(...stepPlantRespawns(world, now).events); |
| 807 | |
| 808 | for (const entity of world.ecs.query(Component.gather, Component.position, Component.character)) |
| 809 | { |
| 810 | const { handle, gather, position, character, entityId } = entity; |
| 811 | if (world.ecs.has(handle, Component.dead)) |
| 812 | { |
| 813 | clearGather(world.ecs, handle); |
| 814 | continue; |
| 815 | } |
| 816 | if (chebyshev(position.tileX, position.tileY, gather.tileX, gather.tileY) > 1) |
| 817 | { |
| 818 | clearGather(world.ecs, handle); |
| 819 | continue; |
| 820 | } |
| 821 | const block = blockAt(world, gather.tileX, gather.tileY); |
| 822 | |
| 823 | if (gather.kind === "mining") |
| 824 | { |
| 825 | if (!block || block.typeId !== BlockType.boulder |
| 826 | || materialFromOreBoulderState(block.state) !== gather.materialId) |
| 827 | { |
| 828 | clearGather(world.ecs, handle); |
| 829 | continue; |
| 830 | } |
| 831 | } |
| 832 | else if (gather.kind === "woodcutting") |
| 833 | { |
| 834 | if (!block || block.typeId !== gather.typeId || !isChopableTreeBlock(block)) |
| 835 | { |
| 836 | clearGather(world.ecs, handle); |
| 837 | continue; |
| 838 | } |
| 839 | } |
| 840 | else if (gather.kind === "plant") |
| 841 | { |
| 842 | if (!block || block.typeId !== gather.typeId || !plantGatherForBlock(block)) |
| 843 | { |
| 844 | clearGather(world.ecs, handle); |
| 845 | continue; |
| 846 | } |
| 847 | } |
| 848 | else |
| 849 | { |
| 850 | clearGather(world.ecs, handle); |
| 851 | continue; |
| 852 | } |
| 853 | |
| 854 | if (now >= gather.nextSwingAt && now < gather.finishesAt) |
| 855 | { |
| 856 | gather.nextSwingAt = now + GATHER_SWING_INTERVAL_MS; |
| 857 | events.push({ |
| 858 | type: "gather-swing", |
| 859 | attackerId: entityId, |
| 860 | tileX: gather.tileX, |
| 861 | tileY: gather.tileY, |
| 862 | fromTileX: position.tileX, |
| 863 | fromTileY: position.tileY, |
| 864 | toolItemId: gather.toolItemId ?? null, |
| 865 | swipe: false |
| 866 | }); |
| 867 | } |
| 868 | |
| 869 | if (now < gather.finishesAt) |
| 870 | { continue; } |
| 871 | |
| 872 | if (gather.kind === "mining") |
| 873 | { completeMining(world, handle, entityId, character, gather, events, privates, now); } |
| 874 | else if (gather.kind === "woodcutting") |
| 875 | { completeWoodcutting(world, handle, entityId, character, gather, events, privates, now); } |
| 876 | else |
| 877 | { completePlant(world, handle, entityId, character, gather, events, privates, now); } |
| 878 | } |
| 879 | return { events, privates }; |
| 880 | } |
| 881 | |
| 882 | export function handleGatherCommand (world, sessionId, command, now) |
| 883 | { |
| 884 | switch (command.type) |
| 885 | { |
| 886 | case "mine": |
| 887 | return startMine(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 888 | case "chop": |
| 889 | return startChop(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 890 | case "pick": |
| 891 | return startPick(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 892 | case "cancel-gather": |
| 893 | { |
| 894 | const entityId = world.characterEntities.get(command.characterId); |
| 895 | if (entityId == null) |
| 896 | { return { events: [] }; } |
| 897 | const handle = world.ecs.handleOf(entityId); |
| 898 | clearGather(world.ecs, handle); |
| 899 | return { events: [] }; |
| 900 | } |
| 901 | default: |
| 902 | return null; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | function defaultPlantState (typeId) |
| 907 | { |
| 908 | if (typeId === BlockType.herbPlant) |
| 909 | { return "mint"; } |
| 910 | if (typeId === BlockType.flaxPlant) |
| 911 | { return "flax-plant"; } |
| 912 | if (typeId === BlockType.silkCocoon) |
| 913 | { return "silk-cocoon"; } |
| 914 | return "cotton-plant"; |
| 915 | } |