| 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 | woodworkingXpForDebark, |
| 20 | treeExhaustDurationMs, |
| 21 | logItemIdForWoodTier, |
| 22 | herbalismDurationMs, |
| 23 | herbalismXpForPick, |
| 24 | PLANT_EXHAUST_MS |
| 25 | } from "../../../shared/definitions/gathering.js"; |
| 26 | import { rawClothItemId } from "../../../shared/definitions/clothItems.js"; |
| 27 | import { ClothTier, metalTierForHerbTier } from "../../../shared/definitions/materialTiers.js"; |
| 28 | import { |
| 29 | flowerItemInstance, |
| 30 | flowerColorIdFromBlockState, |
| 31 | flowerBlockState, |
| 32 | flowerColorIdForTile, |
| 33 | randomFlowerColorId |
| 34 | } from "../../../shared/definitions/dyes.js"; |
| 35 | import { herbDefinition, isHerbId } from "../../../shared/definitions/herbs.js"; |
| 36 | import { insertStack, stackQuantity } from "../../../shared/items/inventory.js"; |
| 37 | import { |
| 38 | attachOrigins, |
| 39 | GatherOriginVerb, |
| 40 | gatherOrigin, |
| 41 | craftOrigin, |
| 42 | CraftOriginVerb, |
| 43 | normalizeOrigins, |
| 44 | splitOrigins |
| 45 | } from "../../../shared/items/origin.js"; |
| 46 | import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js"; |
| 47 | import |
| 48 | { |
| 49 | gatherDurationMultiplier, |
| 50 | harvestYieldMultiplier |
| 51 | } from "../../../shared/equipment/jewelryEffects.js"; |
| 52 | import { |
| 53 | seasonIdAt, |
| 54 | treeStumpStateForSeason, |
| 55 | SeasonId, |
| 56 | scaleRegrowDurationMs |
| 57 | } from "../../../shared/world/season.js"; |
| 58 | import { bagView, placeOrMergeGroundStack, publicGroundItem, removeGroundItem } from "../items/bags.js"; |
| 59 | import { clearEngage } from "../combat/service.js"; |
| 60 | import { isAdjacentTile } from "../../../shared/navigation/adjacency.js"; |
| 61 | import { tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 62 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 63 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 64 | import { |
| 65 | barkItemIdForWood, |
| 66 | debarkedLogInstance, |
| 67 | isLogDebarked |
| 68 | } from "../../../shared/definitions/woodItems.js"; |
| 69 | import { ItemFamilyId } from "../../../shared/definitions/tierFamilies.js"; |
| 70 | |
| 71 | const GATHER_SWING_INTERVAL_MS = 900; |
| 72 | /** Fixed strip duration — short axe work on a felled log. */ |
| 73 | export const DEBARK_DURATION_MS = 2000; |
| 74 | |
| 75 | function respawnTileKey (tileX, tileY, dimensionId = OVERWORLD_ID) |
| 76 | { |
| 77 | const dim = normalizeDimensionId(dimensionId); |
| 78 | return `${dim}:${tileX},${tileY}`; |
| 79 | } |
| 80 | |
| 81 | function parseRespawnTileKey (key) |
| 82 | { |
| 83 | const raw = String(key ?? ""); |
| 84 | const colon = raw.indexOf(":"); |
| 85 | if (colon < 0) |
| 86 | { |
| 87 | const [tileX, tileY] = raw.split(",").map(Number); |
| 88 | return { dimensionId: OVERWORLD_ID, tileX, tileY }; |
| 89 | } |
| 90 | return { |
| 91 | dimensionId: normalizeDimensionId(raw.slice(0, colon)), |
| 92 | tileX: Number(raw.slice(colon + 1).split(",")[0]), |
| 93 | tileY: Number(raw.slice(colon + 1).split(",")[1]) |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | function userError (message) |
| 98 | { |
| 99 | const error = new Error(message); |
| 100 | error.userFacing = true; |
| 101 | return error; |
| 102 | } |
| 103 | |
| 104 | function characterDisplayName (world, handle) |
| 105 | { |
| 106 | const appearance = world.ecs.get(handle, Component.appearance); |
| 107 | const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim(); |
| 108 | return name || "someone"; |
| 109 | } |
| 110 | |
| 111 | function ensureOreRespawns (world) |
| 112 | { |
| 113 | if (!world.oreRespawns) |
| 114 | { world.oreRespawns = new Map(); } |
| 115 | return world.oreRespawns; |
| 116 | } |
| 117 | |
| 118 | function ensureTreeRespawns (world) |
| 119 | { |
| 120 | if (!world.treeRespawns) |
| 121 | { world.treeRespawns = new Map(); } |
| 122 | return world.treeRespawns; |
| 123 | } |
| 124 | |
| 125 | function ensurePlantRespawns (world) |
| 126 | { |
| 127 | if (!world.plantRespawns) |
| 128 | { world.plantRespawns = new Map(); } |
| 129 | return world.plantRespawns; |
| 130 | } |
| 131 | |
| 132 | function blockAt (world, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 133 | { |
| 134 | const dim = normalizeDimensionId(dimensionId); |
| 135 | const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY), dim); |
| 136 | return data?.blocks?.[tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX)] ?? null; |
| 137 | } |
| 138 | |
| 139 | function setBlockState (world, tileX, tileY, typeId, state, dimensionId = OVERWORLD_ID) |
| 140 | { |
| 141 | const dim = normalizeDimensionId(dimensionId); |
| 142 | const chunkX = tileToChunk(tileX); |
| 143 | const chunkY = tileToChunk(tileY); |
| 144 | const localX = tileToLocal(tileX); |
| 145 | const localY = tileToLocal(tileY); |
| 146 | const data = world.chunkData(chunkX, chunkY, dim); |
| 147 | const index = localY * CHUNK_SIZE + localX; |
| 148 | const prev = data?.blocks?.[index]; |
| 149 | if (!prev || prev.typeId !== typeId) |
| 150 | { return null; } |
| 151 | const block = { typeId: prev.typeId, state, config: prev.config ?? null }; |
| 152 | data.blocks[index] = block; |
| 153 | return { |
| 154 | type: "chunk-delta", |
| 155 | chunkX, |
| 156 | chunkY, |
| 157 | dimensionId: dim, |
| 158 | tileX, |
| 159 | tileY, |
| 160 | localX, |
| 161 | localY, |
| 162 | block |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | function setBoulderState (world, tileX, tileY, state, dimensionId = OVERWORLD_ID) |
| 167 | { |
| 168 | return setBlockState(world, tileX, tileY, BlockType.boulder, state, dimensionId); |
| 169 | } |
| 170 | |
| 171 | function setTreeState (world, tileX, tileY, typeId, state, dimensionId = OVERWORLD_ID) |
| 172 | { |
| 173 | return setBlockState(world, tileX, tileY, typeId, state, dimensionId); |
| 174 | } |
| 175 | |
| 176 | function clearBlock (world, tileX, tileY, typeId, dimensionId = OVERWORLD_ID) |
| 177 | { |
| 178 | const dim = normalizeDimensionId(dimensionId); |
| 179 | const chunkX = tileToChunk(tileX); |
| 180 | const chunkY = tileToChunk(tileY); |
| 181 | const localX = tileToLocal(tileX); |
| 182 | const localY = tileToLocal(tileY); |
| 183 | const data = world.chunkData(chunkX, chunkY, dim); |
| 184 | const index = localY * CHUNK_SIZE + localX; |
| 185 | const prev = data?.blocks?.[index]; |
| 186 | if (!prev || prev.typeId !== typeId) |
| 187 | { return null; } |
| 188 | data.blocks[index] = null; |
| 189 | return { |
| 190 | type: "chunk-delta", |
| 191 | chunkX, |
| 192 | chunkY, |
| 193 | dimensionId: dim, |
| 194 | tileX, |
| 195 | tileY, |
| 196 | localX, |
| 197 | localY, |
| 198 | block: null |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | function placeBlock (world, tileX, tileY, typeId, state, dimensionId = OVERWORLD_ID) |
| 203 | { |
| 204 | const dim = normalizeDimensionId(dimensionId); |
| 205 | const chunkX = tileToChunk(tileX); |
| 206 | const chunkY = tileToChunk(tileY); |
| 207 | const localX = tileToLocal(tileX); |
| 208 | const localY = tileToLocal(tileY); |
| 209 | const data = world.chunkData(chunkX, chunkY, dim); |
| 210 | const index = localY * CHUNK_SIZE + localX; |
| 211 | const block = { typeId, state, config: null }; |
| 212 | data.blocks[index] = block; |
| 213 | return { |
| 214 | type: "chunk-delta", |
| 215 | chunkX, |
| 216 | chunkY, |
| 217 | dimensionId: dim, |
| 218 | tileX, |
| 219 | tileY, |
| 220 | localX, |
| 221 | localY, |
| 222 | block |
| 223 | }; |
| 224 | } |
| 225 | |
| 226 | function pickaxeForOre (world, handle, materialId) |
| 227 | { |
| 228 | const inventory = world.ecs.get(handle, Component.inventory); |
| 229 | const equipment = world.ecs.get(handle, Component.equipment); |
| 230 | const skills = world.ecs.get(handle, Component.skills); |
| 231 | return resolveMiningPickaxe({ |
| 232 | inventorySlots: inventory?.slots, |
| 233 | equipmentSlots: equipment?.slots, |
| 234 | skills, |
| 235 | materialId |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | function axeForWood (world, handle, woodTier) |
| 240 | { |
| 241 | const inventory = world.ecs.get(handle, Component.inventory); |
| 242 | const equipment = world.ecs.get(handle, Component.equipment); |
| 243 | const skills = world.ecs.get(handle, Component.skills); |
| 244 | return resolveWoodcuttingAxe({ |
| 245 | inventorySlots: inventory?.slots, |
| 246 | equipmentSlots: equipment?.slots, |
| 247 | skills, |
| 248 | woodTier |
| 249 | }); |
| 250 | } |
| 251 | |
| 252 | function clearGather (ecs, handle) |
| 253 | { |
| 254 | if (ecs.has(handle, Component.gather)) |
| 255 | { ecs.remove(handle, Component.gather); } |
| 256 | } |
| 257 | |
| 258 | export function cancelGather (ecs, handle) |
| 259 | { |
| 260 | clearGather(ecs, handle); |
| 261 | } |
| 262 | |
| 263 | /** Drop scheduled ore respawns when authored chunk data is replaced or unloaded. */ |
| 264 | export function clearOreRespawnsForChunk (world, chunkX, chunkY, dimensionId = null) |
| 265 | { |
| 266 | const respawns = world.oreRespawns; |
| 267 | if (!respawns?.size) |
| 268 | { return; } |
| 269 | const dimFilter = dimensionId != null ? normalizeDimensionId(dimensionId) : null; |
| 270 | for (const key of [...respawns.keys()]) |
| 271 | { |
| 272 | const { dimensionId: keyDim, tileX, tileY } = parseRespawnTileKey(key); |
| 273 | if (dimFilter != null && keyDim !== dimFilter) |
| 274 | { continue; } |
| 275 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 276 | { respawns.delete(key); } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** Drop scheduled tree respawns when authored chunk data is replaced or unloaded. */ |
| 281 | export function clearTreeRespawnsForChunk (world, chunkX, chunkY, dimensionId = null) |
| 282 | { |
| 283 | const respawns = world.treeRespawns; |
| 284 | if (!respawns?.size) |
| 285 | { return; } |
| 286 | const dimFilter = dimensionId != null ? normalizeDimensionId(dimensionId) : null; |
| 287 | for (const key of [...respawns.keys()]) |
| 288 | { |
| 289 | const { dimensionId: keyDim, tileX, tileY } = parseRespawnTileKey(key); |
| 290 | if (dimFilter != null && keyDim !== dimFilter) |
| 291 | { continue; } |
| 292 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 293 | { respawns.delete(key); } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | export function clearPlantRespawnsForChunk (world, chunkX, chunkY, dimensionId = null) |
| 298 | { |
| 299 | const respawns = world.plantRespawns; |
| 300 | if (!respawns?.size) |
| 301 | { return; } |
| 302 | const dimFilter = dimensionId != null ? normalizeDimensionId(dimensionId) : null; |
| 303 | for (const key of [...respawns.keys()]) |
| 304 | { |
| 305 | const { dimensionId: keyDim, tileX, tileY } = parseRespawnTileKey(key); |
| 306 | if (dimFilter != null && keyDim !== dimFilter) |
| 307 | { continue; } |
| 308 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 309 | { respawns.delete(key); } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Restore exhausted ore boulders whose timers have elapsed. |
| 315 | */ |
| 316 | export function stepOreRespawns (world, now) |
| 317 | { |
| 318 | const respawns = world.oreRespawns; |
| 319 | if (!respawns?.size) |
| 320 | { return { events: [] }; } |
| 321 | const events = []; |
| 322 | for (const [key, entry] of [...respawns.entries()]) |
| 323 | { |
| 324 | if (now < entry.respawnsAt) |
| 325 | { continue; } |
| 326 | respawns.delete(key); |
| 327 | const dim = normalizeDimensionId(entry.dimensionId ?? OVERWORLD_ID); |
| 328 | const delta = setBoulderState(world, entry.tileX, entry.tileY, oreBoulderState(entry.materialId), dim); |
| 329 | if (delta) |
| 330 | { events.push(delta); } |
| 331 | } |
| 332 | return { events }; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Restore exhausted trees whose timers have elapsed (live seasonal look). |
| 337 | */ |
| 338 | export function stepTreeRespawns (world, now) |
| 339 | { |
| 340 | const respawns = world.treeRespawns; |
| 341 | if (!respawns?.size) |
| 342 | { return { events: [] }; } |
| 343 | const events = []; |
| 344 | for (const [key, entry] of [...respawns.entries()]) |
| 345 | { |
| 346 | if (now < entry.respawnsAt) |
| 347 | { continue; } |
| 348 | respawns.delete(key); |
| 349 | const dim = normalizeDimensionId(entry.dimensionId ?? OVERWORLD_ID); |
| 350 | const delta = setTreeState( |
| 351 | world, |
| 352 | entry.tileX, |
| 353 | entry.tileY, |
| 354 | entry.typeId, |
| 355 | SeasonId.summer, |
| 356 | dim |
| 357 | ); |
| 358 | if (delta) |
| 359 | { events.push(delta); } |
| 360 | } |
| 361 | return { events }; |
| 362 | } |
| 363 | |
| 364 | export function stepPlantRespawns (world, now) |
| 365 | { |
| 366 | const respawns = world.plantRespawns; |
| 367 | if (!respawns?.size) |
| 368 | { return { events: [] }; } |
| 369 | const events = []; |
| 370 | for (const [key, entry] of [...respawns.entries()]) |
| 371 | { |
| 372 | if (now < entry.respawnsAt) |
| 373 | { continue; } |
| 374 | respawns.delete(key); |
| 375 | const dim = normalizeDimensionId(entry.dimensionId ?? OVERWORLD_ID); |
| 376 | const existing = blockAt(world, entry.tileX, entry.tileY, dim); |
| 377 | if (existing) |
| 378 | { continue; } |
| 379 | const state = entry.typeId === BlockType.flower |
| 380 | ? flowerBlockState(randomFlowerColorId()) |
| 381 | : (entry.state ?? defaultPlantState(entry.typeId)); |
| 382 | const delta = placeBlock(world, entry.tileX, entry.tileY, entry.typeId, state, dim); |
| 383 | if (delta) |
| 384 | { events.push(delta); } |
| 385 | } |
| 386 | return { events }; |
| 387 | } |
| 388 | |
| 389 | function beginGatherSwing (world, handle, entityId, position, gatherFields, now) |
| 390 | { |
| 391 | clearEngage(world.ecs, entityId); |
| 392 | const path = world.ecs.get(handle, Component.path); |
| 393 | if (path) |
| 394 | { path.tiles = []; } |
| 395 | world.ecs.add(handle, Component.gather, { |
| 396 | ...gatherFields, |
| 397 | dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID), |
| 398 | startedAt: now, |
| 399 | finishesAt: now + gatherFields.durationMs, |
| 400 | nextSwingAt: now |
| 401 | }); |
| 402 | return { |
| 403 | events: [{ |
| 404 | type: "gather-swing", |
| 405 | attackerId: entityId, |
| 406 | tileX: gatherFields.tileX, |
| 407 | tileY: gatherFields.tileY, |
| 408 | fromTileX: position.tileX, |
| 409 | fromTileY: position.tileY, |
| 410 | toolItemId: gatherFields.toolItemId ?? null, |
| 411 | swipe: false, |
| 412 | dimensionId: normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID) |
| 413 | }] |
| 414 | }; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Start mining an ore boulder. Returns events / private bag updates when complete via stepGather. |
| 419 | */ |
| 420 | export function startMine (world, sessionId, characterId, tileX, tileY, now) |
| 421 | { |
| 422 | const entityId = world.characterEntities.get(characterId); |
| 423 | if (entityId == null) |
| 424 | { throw userError("Character is not in the world."); } |
| 425 | const handle = world.ecs.handleOf(entityId); |
| 426 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 427 | if (!identity || identity.sessionId !== sessionId) |
| 428 | { throw userError("Character is not in the world."); } |
| 429 | if (world.ecs.has(handle, Component.dead)) |
| 430 | { throw userError("You are dead."); } |
| 431 | |
| 432 | const position = world.ecs.get(handle, Component.position); |
| 433 | if (!isAdjacentTile(position.tileX, position.tileY, tileX, tileY)) |
| 434 | { throw userError("You need to stand next to the rock."); } |
| 435 | |
| 436 | const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 437 | const block = blockAt(world, tileX, tileY, dim); |
| 438 | if (!block || block.typeId !== BlockType.boulder) |
| 439 | { throw userError("There is nothing to mine there."); } |
| 440 | const materialId = materialFromOreBoulderState(block.state); |
| 441 | if (!materialId) |
| 442 | { |
| 443 | throw userError(block.state === EXHAUSTED_ORE_BOULDER_STATE |
| 444 | ? "That rock is exhausted." |
| 445 | : "That rock has no ore."); |
| 446 | } |
| 447 | |
| 448 | const pickaxe = pickaxeForOre(world, handle, materialId); |
| 449 | if (!pickaxe.ok) |
| 450 | { throw userError(pickaxe.reason); } |
| 451 | |
| 452 | const durationMs = Math.max( |
| 453 | 1, |
| 454 | Math.floor(miningDurationMs(materialId, pickaxe.tool.tier) |
| 455 | * gatherDurationMultiplier(world.ecs.get(handle, Component.equipment))) |
| 456 | ); |
| 457 | const toolItemId = pickaxe.tool.stack?.itemId ?? null; |
| 458 | return beginGatherSwing(world, handle, entityId, position, { |
| 459 | kind: "mining", |
| 460 | tileX, |
| 461 | tileY, |
| 462 | materialId, |
| 463 | toolTier: pickaxe.tool.tier, |
| 464 | toolItemId, |
| 465 | durationMs |
| 466 | }, now); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Start chopping a tree. Completes via stepGather (stump + log + woodcutting XP). |
| 471 | */ |
| 472 | export function startChop (world, sessionId, characterId, tileX, tileY, now) |
| 473 | { |
| 474 | const entityId = world.characterEntities.get(characterId); |
| 475 | if (entityId == null) |
| 476 | { throw userError("Character is not in the world."); } |
| 477 | const handle = world.ecs.handleOf(entityId); |
| 478 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 479 | if (!identity || identity.sessionId !== sessionId) |
| 480 | { throw userError("Character is not in the world."); } |
| 481 | if (world.ecs.has(handle, Component.dead)) |
| 482 | { throw userError("You are dead."); } |
| 483 | |
| 484 | const position = world.ecs.get(handle, Component.position); |
| 485 | if (!isAdjacentTile(position.tileX, position.tileY, tileX, tileY)) |
| 486 | { throw userError("You need to stand next to the tree."); } |
| 487 | |
| 488 | const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 489 | const block = blockAt(world, tileX, tileY, dim); |
| 490 | if (!isChopableTreeBlock(block)) |
| 491 | { |
| 492 | throw userError(block && woodTierForTreeBlock(block.typeId) != null |
| 493 | ? "That tree is already felled." |
| 494 | : "There is nothing to chop there."); |
| 495 | } |
| 496 | const woodTier = woodTierForTreeBlock(block.typeId); |
| 497 | const axe = axeForWood(world, handle, woodTier); |
| 498 | if (!axe.ok) |
| 499 | { throw userError(axe.reason); } |
| 500 | |
| 501 | const durationMs = Math.max( |
| 502 | 1, |
| 503 | Math.floor(woodcuttingDurationMs(woodTier, axe.tool.tier) |
| 504 | * gatherDurationMultiplier(world.ecs.get(handle, Component.equipment))) |
| 505 | ); |
| 506 | const toolItemId = axe.tool.stack?.itemId ?? null; |
| 507 | return beginGatherSwing(world, handle, entityId, position, { |
| 508 | kind: "woodcutting", |
| 509 | tileX, |
| 510 | tileY, |
| 511 | typeId: block.typeId, |
| 512 | woodTier, |
| 513 | toolTier: axe.tool.tier, |
| 514 | toolItemId, |
| 515 | durationMs |
| 516 | }, now); |
| 517 | } |
| 518 | |
| 519 | function woodTierFromLogItemId (itemId) |
| 520 | { |
| 521 | const def = itemDefinition(itemId); |
| 522 | if (!def || def.familyId !== ItemFamilyId.log) |
| 523 | { return null; } |
| 524 | return def.tier ?? null; |
| 525 | } |
| 526 | |
| 527 | function nearDebarkTarget (position, tileX, tileY) |
| 528 | { |
| 529 | return isAdjacentTile(position.tileX, position.tileY, tileX, tileY) |
| 530 | || (position.tileX === tileX && position.tileY === tileY); |
| 531 | } |
| 532 | |
| 533 | function publishGroundItem (item) |
| 534 | { |
| 535 | return { |
| 536 | type: "ground-item-appear", |
| 537 | ...publicGroundItem(item), |
| 538 | stack: item.stack, |
| 539 | droppedAt: item.droppedAt, |
| 540 | source: item.source, |
| 541 | droppedByCharacterId: item.droppedByCharacterId, |
| 542 | expiresAt: item.expiresAt |
| 543 | }; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Start stripping bark from a ground log (2s axe swings, then bark + debarked flag). |
| 548 | */ |
| 549 | export function startDebark (world, sessionId, characterId, groundItemId, now) |
| 550 | { |
| 551 | const entityId = world.characterEntities.get(characterId); |
| 552 | if (entityId == null) |
| 553 | { throw userError("Character is not in the world."); } |
| 554 | const handle = world.ecs.handleOf(entityId); |
| 555 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 556 | if (!identity || identity.sessionId !== sessionId) |
| 557 | { throw userError("Character is not in the world."); } |
| 558 | if (world.ecs.has(handle, Component.dead)) |
| 559 | { throw userError("You are dead."); } |
| 560 | |
| 561 | const item = world.groundItems.get(groundItemId); |
| 562 | if (!item?.stack) |
| 563 | { throw userError("That item is no longer on the ground."); } |
| 564 | |
| 565 | const woodTier = woodTierFromLogItemId(item.stack.itemId); |
| 566 | if (!woodTier) |
| 567 | { throw userError("That is not a log."); } |
| 568 | if (isLogDebarked(item.stack)) |
| 569 | { throw userError("That log is already stripped of bark."); } |
| 570 | |
| 571 | const position = world.ecs.get(handle, Component.position); |
| 572 | if (!nearDebarkTarget(position, item.tileX, item.tileY)) |
| 573 | { throw userError("Stand next to the log."); } |
| 574 | |
| 575 | const axe = axeForWood(world, handle, woodTier); |
| 576 | if (!axe.ok) |
| 577 | { throw userError(axe.reason || "You need an axe to strip this bark."); } |
| 578 | |
| 579 | const barkId = barkItemIdForWood(woodTier); |
| 580 | if (barkId == null) |
| 581 | { throw userError("That bark cannot be stripped."); } |
| 582 | |
| 583 | const toolItemId = axe.tool.stack?.itemId ?? null; |
| 584 | return beginGatherSwing(world, handle, entityId, position, { |
| 585 | kind: "debark", |
| 586 | tileX: item.tileX, |
| 587 | tileY: item.tileY, |
| 588 | groundItemId, |
| 589 | woodTier, |
| 590 | logItemId: item.stack.itemId, |
| 591 | barkItemId: barkId, |
| 592 | toolTier: axe.tool.tier, |
| 593 | toolItemId, |
| 594 | durationMs: DEBARK_DURATION_MS |
| 595 | }, now); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Pick a plant node (flower / cotton; no tools required). |
| 600 | */ |
| 601 | export function startPick (world, sessionId, characterId, tileX, tileY, now) |
| 602 | { |
| 603 | const entityId = world.characterEntities.get(characterId); |
| 604 | if (entityId == null) |
| 605 | { throw userError("Character is not in the world."); } |
| 606 | const handle = world.ecs.handleOf(entityId); |
| 607 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 608 | if (!identity || identity.sessionId !== sessionId) |
| 609 | { throw userError("Character is not in the world."); } |
| 610 | if (world.ecs.has(handle, Component.dead)) |
| 611 | { throw userError("You are dead."); } |
| 612 | |
| 613 | const position = world.ecs.get(handle, Component.position); |
| 614 | if (!isAdjacentTile(position.tileX, position.tileY, tileX, tileY)) |
| 615 | { throw userError("You need to stand next to the plant."); } |
| 616 | |
| 617 | const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 618 | const block = blockAt(world, tileX, tileY, dim); |
| 619 | const plant = plantGatherForBlock(block); |
| 620 | if (!plant) |
| 621 | { throw userError("There is nothing to pick there."); } |
| 622 | |
| 623 | const inventory = world.ecs.get(handle, Component.inventory); |
| 624 | const equipment = world.ecs.get(handle, Component.equipment); |
| 625 | const skills = world.ecs.get(handle, Component.skills); |
| 626 | |
| 627 | let herbId = null; |
| 628 | let herbTier = null; |
| 629 | let minMetalTier = null; |
| 630 | if (plant.kind === "herb") |
| 631 | { |
| 632 | const gate = resolveHerbGather({ herbId: block.state, skills }); |
| 633 | if (!gate.ok) |
| 634 | { throw userError(gate.reason); } |
| 635 | herbId = gate.herb.herbId; |
| 636 | herbTier = gate.herb.tier; |
| 637 | minMetalTier = metalTierForHerbTier(gate.herb.tier); |
| 638 | } |
| 639 | |
| 640 | const clippers = resolvePlantClippers({ |
| 641 | inventorySlots: inventory?.slots, |
| 642 | equipmentSlots: equipment?.slots, |
| 643 | skills, |
| 644 | requireTool: !!plant.requiresClippers, |
| 645 | minClothTier: plant.minClothTier ?? null, |
| 646 | minMetalTier |
| 647 | }); |
| 648 | if (!clippers.ok) |
| 649 | { throw userError(clippers.reason); } |
| 650 | |
| 651 | const toolTier = clippers.tool?.tier ?? null; |
| 652 | const durationMs = Math.max( |
| 653 | 1, |
| 654 | Math.floor(herbalismDurationMs(plant.kind, toolTier, herbTier) |
| 655 | * gatherDurationMultiplier(world.ecs.get(handle, Component.equipment))) |
| 656 | ); |
| 657 | const toolItemId = clippers.tool?.stack?.itemId ?? null; |
| 658 | const prefixEvents = []; |
| 659 | let flowerColorId = null; |
| 660 | if (plant.kind === "flower") |
| 661 | { |
| 662 | flowerColorId = flowerColorIdFromBlockState(block.state); |
| 663 | if (!flowerColorId) |
| 664 | { |
| 665 | // Legacy / uncoloured placements: lock a colour before the pick starts. |
| 666 | flowerColorId = flowerColorIdForTile(tileX, tileY); |
| 667 | const delta = setBlockState( |
| 668 | world, |
| 669 | tileX, |
| 670 | tileY, |
| 671 | BlockType.flower, |
| 672 | flowerBlockState(flowerColorId), |
| 673 | dim |
| 674 | ); |
| 675 | if (delta) |
| 676 | { prefixEvents.push(delta); } |
| 677 | } |
| 678 | } |
| 679 | const started = beginGatherSwing(world, handle, entityId, position, { |
| 680 | kind: "plant", |
| 681 | plantKind: plant.kind, |
| 682 | herbId, |
| 683 | herbTier, |
| 684 | tileX, |
| 685 | tileY, |
| 686 | typeId: block.typeId, |
| 687 | flowerColorId, |
| 688 | toolTier, |
| 689 | toolItemId, |
| 690 | durationMs |
| 691 | }, now); |
| 692 | return { |
| 693 | ...started, |
| 694 | events: [...prefixEvents, ...(started.events ?? [])] |
| 695 | }; |
| 696 | } |
| 697 | |
| 698 | function pushBagUpdate (events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps = []) |
| 699 | { |
| 700 | privates.push({ |
| 701 | characterId: character.characterId, |
| 702 | bag: bagView(world.ecs, handle) |
| 703 | }); |
| 704 | if (skillsChanged) |
| 705 | { |
| 706 | events.push({ |
| 707 | type: "skills-updated", |
| 708 | entityId, |
| 709 | characterId: character.characterId, |
| 710 | ...publicSkillsPayload(skills), |
| 711 | stored: storedSkillXp(skills), |
| 712 | levelUps |
| 713 | }); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | function completeMining (world, handle, entityId, character, gather, events, privates, now) |
| 718 | { |
| 719 | const oreItemId = oreItemIdForMaterial(gather.materialId); |
| 720 | const def = itemDefinition(oreItemId); |
| 721 | const inventory = world.ecs.get(handle, Component.inventory); |
| 722 | if (!oreItemId || !def || !inventory) |
| 723 | { |
| 724 | clearGather(world.ecs, handle); |
| 725 | return; |
| 726 | } |
| 727 | const origin = gatherOrigin(GatherOriginVerb.mined, characterDisplayName(world, handle)); |
| 728 | const quantity = Math.max(1, Math.floor(1 * harvestYieldMultiplier(world.ecs.get(handle, Component.equipment)))); |
| 729 | const stack = attachOrigins({ |
| 730 | itemId: oreItemId, |
| 731 | quantity, |
| 732 | origins: [{ origin, quantity }] |
| 733 | }); |
| 734 | const inserted = insertStack(inventory.slots, stack, def); |
| 735 | if (!inserted.ok || inserted.remainder > 0) |
| 736 | { |
| 737 | clearGather(world.ecs, handle); |
| 738 | events.push({ |
| 739 | type: "command-error", |
| 740 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 741 | message: "Your inventory is full." |
| 742 | }); |
| 743 | return; |
| 744 | } |
| 745 | inventory.slots = inserted.slots; |
| 746 | |
| 747 | const skills = world.ecs.get(handle, Component.skills); |
| 748 | let skillsChanged = false; |
| 749 | const levelUps = []; |
| 750 | if (skills?.xp) |
| 751 | { |
| 752 | const result = applySkillXp( |
| 753 | skills, |
| 754 | SkillId.mining, |
| 755 | miningXpForMaterial(gather.materialId), |
| 756 | world.ecs.get(handle, Component.equipment) |
| 757 | ); |
| 758 | if (result.changed) |
| 759 | { skillsChanged = true; } |
| 760 | if (result.levelUp) |
| 761 | { levelUps.push(result.levelUp); } |
| 762 | } |
| 763 | |
| 764 | const exhausted = setBoulderState( |
| 765 | world, |
| 766 | gather.tileX, |
| 767 | gather.tileY, |
| 768 | EXHAUSTED_ORE_BOULDER_STATE, |
| 769 | normalizeDimensionId(gather.dimensionId ?? OVERWORLD_ID) |
| 770 | ); |
| 771 | if (exhausted) |
| 772 | { |
| 773 | events.push(exhausted); |
| 774 | const dim = normalizeDimensionId(gather.dimensionId ?? OVERWORLD_ID); |
| 775 | ensureOreRespawns(world).set(respawnTileKey(gather.tileX, gather.tileY, dim), { |
| 776 | tileX: gather.tileX, |
| 777 | tileY: gather.tileY, |
| 778 | dimensionId: dim, |
| 779 | materialId: gather.materialId, |
| 780 | respawnsAt: now + oreExhaustDurationMs(gather.materialId) |
| 781 | }); |
| 782 | } |
| 783 | |
| 784 | clearGather(world.ecs, handle); |
| 785 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 786 | events.push({ |
| 787 | type: "gather-complete", |
| 788 | characterId: character.characterId, |
| 789 | kind: "mining", |
| 790 | materialId: gather.materialId, |
| 791 | itemId: oreItemId |
| 792 | }); |
| 793 | } |
| 794 | |
| 795 | function completeWoodcutting (world, handle, entityId, character, gather, events, privates, now) |
| 796 | { |
| 797 | const logItemId = logItemIdForWoodTier(gather.woodTier); |
| 798 | const def = itemDefinition(logItemId); |
| 799 | const inventory = world.ecs.get(handle, Component.inventory); |
| 800 | if (!logItemId || !def || !inventory) |
| 801 | { |
| 802 | clearGather(world.ecs, handle); |
| 803 | return; |
| 804 | } |
| 805 | const origin = gatherOrigin(GatherOriginVerb.felled, characterDisplayName(world, handle)); |
| 806 | const quantity = Math.max(1, Math.floor(1 * harvestYieldMultiplier(world.ecs.get(handle, Component.equipment)))); |
| 807 | const stack = attachOrigins({ |
| 808 | itemId: logItemId, |
| 809 | quantity, |
| 810 | origins: [{ origin, quantity }] |
| 811 | }); |
| 812 | const inserted = insertStack(inventory.slots, stack, def); |
| 813 | if (!inserted.ok || inserted.remainder > 0) |
| 814 | { |
| 815 | clearGather(world.ecs, handle); |
| 816 | events.push({ |
| 817 | type: "command-error", |
| 818 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 819 | message: "Your inventory is full." |
| 820 | }); |
| 821 | return; |
| 822 | } |
| 823 | inventory.slots = inserted.slots; |
| 824 | |
| 825 | const skills = world.ecs.get(handle, Component.skills); |
| 826 | let skillsChanged = false; |
| 827 | const levelUps = []; |
| 828 | if (skills?.xp) |
| 829 | { |
| 830 | const result = applySkillXp( |
| 831 | skills, |
| 832 | SkillId.woodcutting, |
| 833 | woodcuttingXpForWood(gather.woodTier), |
| 834 | world.ecs.get(handle, Component.equipment) |
| 835 | ); |
| 836 | if (result.changed) |
| 837 | { skillsChanged = true; } |
| 838 | if (result.levelUp) |
| 839 | { levelUps.push(result.levelUp); } |
| 840 | } |
| 841 | |
| 842 | const stumpState = treeStumpStateForSeason(seasonIdAt(now)); |
| 843 | const dim = normalizeDimensionId(gather.dimensionId ?? OVERWORLD_ID); |
| 844 | const exhausted = setTreeState( |
| 845 | world, |
| 846 | gather.tileX, |
| 847 | gather.tileY, |
| 848 | gather.typeId, |
| 849 | stumpState, |
| 850 | dim |
| 851 | ); |
| 852 | if (exhausted) |
| 853 | { |
| 854 | events.push(exhausted); |
| 855 | ensureTreeRespawns(world).set(respawnTileKey(gather.tileX, gather.tileY, dim), { |
| 856 | tileX: gather.tileX, |
| 857 | tileY: gather.tileY, |
| 858 | dimensionId: dim, |
| 859 | typeId: gather.typeId, |
| 860 | woodTier: gather.woodTier, |
| 861 | respawnsAt: now + scaleRegrowDurationMs(treeExhaustDurationMs(gather.woodTier), now) |
| 862 | }); |
| 863 | } |
| 864 | |
| 865 | clearGather(world.ecs, handle); |
| 866 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 867 | events.push({ |
| 868 | type: "gather-complete", |
| 869 | characterId: character.characterId, |
| 870 | kind: "woodcutting", |
| 871 | woodTier: gather.woodTier, |
| 872 | itemId: logItemId |
| 873 | }); |
| 874 | } |
| 875 | |
| 876 | function completeDebark (world, handle, entityId, character, gather, events, privates, now) |
| 877 | { |
| 878 | const item = world.groundItems.get(gather.groundItemId); |
| 879 | if (!item?.stack |
| 880 | || (item.stack.itemId | 0) !== (gather.logItemId | 0) |
| 881 | || isLogDebarked(item.stack) |
| 882 | || item.tileX !== gather.tileX |
| 883 | || item.tileY !== gather.tileY) |
| 884 | { |
| 885 | clearGather(world.ecs, handle); |
| 886 | return; |
| 887 | } |
| 888 | |
| 889 | const qty = stackQuantity(item.stack.quantity); |
| 890 | if (qty <= 0) |
| 891 | { |
| 892 | clearGather(world.ecs, handle); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | const origin = craftOrigin(CraftOriginVerb.made, characterDisplayName(world, handle)); |
| 897 | const split = splitOrigins(normalizeOrigins(item.stack), 1); |
| 898 | const debarkedStack = attachOrigins({ |
| 899 | itemId: item.stack.itemId, |
| 900 | quantity: 1, |
| 901 | instance: debarkedLogInstance(), |
| 902 | origins: split.taken |
| 903 | }); |
| 904 | const tileX = item.tileX; |
| 905 | const tileY = item.tileY; |
| 906 | let reuseGroundItemId = null; |
| 907 | |
| 908 | if (qty === 1) |
| 909 | { |
| 910 | reuseGroundItemId = item.groundItemId; |
| 911 | removeGroundItem(world, item.groundItemId); |
| 912 | events.push({ type: "ground-item-despawn", ...publicGroundItem(item) }); |
| 913 | } |
| 914 | else |
| 915 | { |
| 916 | item.stack = { |
| 917 | itemId: item.stack.itemId, |
| 918 | quantity: qty - 1, |
| 919 | instance: item.stack.instance ?? null, |
| 920 | origins: split.remain |
| 921 | }; |
| 922 | events.push(publishGroundItem(item)); |
| 923 | } |
| 924 | |
| 925 | const placedDebarked = placeOrMergeGroundStack(world, { |
| 926 | tileX, |
| 927 | tileY, |
| 928 | dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID), |
| 929 | stack: debarkedStack, |
| 930 | now, |
| 931 | source: "debark", |
| 932 | droppedByCharacterId: character.characterId, |
| 933 | groundItemId: reuseGroundItemId |
| 934 | }); |
| 935 | events.push(...placedDebarked.events); |
| 936 | |
| 937 | const placedBark = placeOrMergeGroundStack(world, { |
| 938 | tileX, |
| 939 | tileY, |
| 940 | dimensionId: normalizeDimensionId(item.dimensionId ?? OVERWORLD_ID), |
| 941 | stack: attachOrigins({ |
| 942 | itemId: gather.barkItemId, |
| 943 | quantity: 1, |
| 944 | origins: [{ origin, quantity: 1 }] |
| 945 | }), |
| 946 | now, |
| 947 | source: "debark", |
| 948 | droppedByCharacterId: character.characterId |
| 949 | }); |
| 950 | events.push(...placedBark.events); |
| 951 | |
| 952 | const skills = world.ecs.get(handle, Component.skills); |
| 953 | let skillsChanged = false; |
| 954 | const levelUps = []; |
| 955 | if (skills?.xp) |
| 956 | { |
| 957 | const result = applySkillXp( |
| 958 | skills, |
| 959 | SkillId.woodworking, |
| 960 | woodworkingXpForDebark(gather.woodTier), |
| 961 | world.ecs.get(handle, Component.equipment) |
| 962 | ); |
| 963 | if (result.changed) |
| 964 | { skillsChanged = true; } |
| 965 | if (result.levelUp) |
| 966 | { levelUps.push(result.levelUp); } |
| 967 | } |
| 968 | |
| 969 | clearGather(world.ecs, handle); |
| 970 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 971 | events.push({ |
| 972 | type: "gather-complete", |
| 973 | characterId: character.characterId, |
| 974 | kind: "debark", |
| 975 | woodTier: gather.woodTier, |
| 976 | itemId: gather.barkItemId |
| 977 | }); |
| 978 | } |
| 979 | |
| 980 | function completePlant (world, handle, entityId, character, gather, events, privates, now) |
| 981 | { |
| 982 | const inventory = world.ecs.get(handle, Component.inventory); |
| 983 | if (!inventory) |
| 984 | { |
| 985 | clearGather(world.ecs, handle); |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | let itemId = null; |
| 990 | let instance = null; |
| 991 | if (gather.plantKind === "flower") |
| 992 | { |
| 993 | itemId = ItemId.flower; |
| 994 | const dim = normalizeDimensionId(gather.dimensionId ?? OVERWORLD_ID); |
| 995 | const live = blockAt(world, gather.tileX, gather.tileY, dim); |
| 996 | const colorId = gather.flowerColorId |
| 997 | ?? flowerColorIdFromBlockState(live?.state) |
| 998 | ?? randomFlowerColorId(); |
| 999 | instance = flowerItemInstance(colorId); |
| 1000 | } |
| 1001 | else if (gather.plantKind === "cotton") |
| 1002 | { |
| 1003 | itemId = rawClothItemId(ClothTier.cotton); |
| 1004 | } |
| 1005 | else if (gather.plantKind === "flax") |
| 1006 | { |
| 1007 | itemId = rawClothItemId(ClothTier.linen); |
| 1008 | } |
| 1009 | else if (gather.plantKind === "silk") |
| 1010 | { |
| 1011 | itemId = rawClothItemId(ClothTier.silk); |
| 1012 | } |
| 1013 | else if (gather.plantKind === "herb") |
| 1014 | { |
| 1015 | const herb = herbDefinition(gather.herbId); |
| 1016 | itemId = herb?.itemId ?? null; |
| 1017 | } |
| 1018 | else |
| 1019 | { |
| 1020 | clearGather(world.ecs, handle); |
| 1021 | return; |
| 1022 | } |
| 1023 | |
| 1024 | const def = itemDefinition(itemId); |
| 1025 | if (!def) |
| 1026 | { |
| 1027 | clearGather(world.ecs, handle); |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | const origin = gatherOrigin(GatherOriginVerb.picked, characterDisplayName(world, handle)); |
| 1032 | const quantity = Math.max(1, Math.floor(1 * harvestYieldMultiplier(world.ecs.get(handle, Component.equipment)))); |
| 1033 | const stack = attachOrigins({ |
| 1034 | itemId, |
| 1035 | quantity, |
| 1036 | instance, |
| 1037 | origins: [{ origin, quantity }] |
| 1038 | }); |
| 1039 | const inserted = insertStack(inventory.slots, stack, def); |
| 1040 | if (!inserted.ok || inserted.remainder > 0) |
| 1041 | { |
| 1042 | clearGather(world.ecs, handle); |
| 1043 | events.push({ |
| 1044 | type: "command-error", |
| 1045 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 1046 | message: "Your inventory is full." |
| 1047 | }); |
| 1048 | return; |
| 1049 | } |
| 1050 | inventory.slots = inserted.slots; |
| 1051 | |
| 1052 | const skills = world.ecs.get(handle, Component.skills); |
| 1053 | let skillsChanged = false; |
| 1054 | const levelUps = []; |
| 1055 | if (skills?.xp) |
| 1056 | { |
| 1057 | const result = applySkillXp( |
| 1058 | skills, |
| 1059 | SkillId.herbalism, |
| 1060 | herbalismXpForPick(gather.plantKind, gather.herbTier), |
| 1061 | world.ecs.get(handle, Component.equipment) |
| 1062 | ); |
| 1063 | if (result.changed) |
| 1064 | { skillsChanged = true; } |
| 1065 | if (result.levelUp) |
| 1066 | { levelUps.push(result.levelUp); } |
| 1067 | } |
| 1068 | |
| 1069 | const dim = normalizeDimensionId(gather.dimensionId ?? OVERWORLD_ID); |
| 1070 | const delta = clearBlock(world, gather.tileX, gather.tileY, gather.typeId, dim); |
| 1071 | if (delta) |
| 1072 | { events.push(delta); } |
| 1073 | ensurePlantRespawns(world).set(respawnTileKey(gather.tileX, gather.tileY, dim), { |
| 1074 | tileX: gather.tileX, |
| 1075 | tileY: gather.tileY, |
| 1076 | dimensionId: dim, |
| 1077 | typeId: gather.typeId, |
| 1078 | // Colour is chosen when the node refreshes (see stepPlantRespawns). |
| 1079 | state: gather.plantKind === "flower" |
| 1080 | ? null |
| 1081 | : (gather.plantKind === "herb" && isHerbId(gather.herbId) |
| 1082 | ? gather.herbId |
| 1083 | : defaultPlantState(gather.typeId)), |
| 1084 | respawnsAt: now + scaleRegrowDurationMs(PLANT_EXHAUST_MS, now) |
| 1085 | }); |
| 1086 | |
| 1087 | clearGather(world.ecs, handle); |
| 1088 | pushBagUpdate(events, privates, world, handle, entityId, character, skillsChanged, skills, levelUps); |
| 1089 | events.push({ |
| 1090 | type: "gather-complete", |
| 1091 | characterId: character.characterId, |
| 1092 | kind: "plant", |
| 1093 | plantKind: gather.plantKind, |
| 1094 | itemId |
| 1095 | }); |
| 1096 | } |
| 1097 | |
| 1098 | export function stepGather (world, now) |
| 1099 | { |
| 1100 | const events = []; |
| 1101 | const privates = []; |
| 1102 | events.push(...stepOreRespawns(world, now).events); |
| 1103 | events.push(...stepTreeRespawns(world, now).events); |
| 1104 | events.push(...stepPlantRespawns(world, now).events); |
| 1105 | |
| 1106 | for (const entity of world.ecs.query(Component.gather, Component.position, Component.character)) |
| 1107 | { |
| 1108 | const { handle, gather, position, character, entityId } = entity; |
| 1109 | if (world.ecs.has(handle, Component.dead)) |
| 1110 | { |
| 1111 | clearGather(world.ecs, handle); |
| 1112 | continue; |
| 1113 | } |
| 1114 | const nearTarget = gather.kind === "debark" |
| 1115 | ? nearDebarkTarget(position, gather.tileX, gather.tileY) |
| 1116 | : isAdjacentTile(position.tileX, position.tileY, gather.tileX, gather.tileY); |
| 1117 | if (!nearTarget) |
| 1118 | { |
| 1119 | clearGather(world.ecs, handle); |
| 1120 | continue; |
| 1121 | } |
| 1122 | const block = blockAt( |
| 1123 | world, |
| 1124 | gather.tileX, |
| 1125 | gather.tileY, |
| 1126 | normalizeDimensionId(gather.dimensionId ?? OVERWORLD_ID) |
| 1127 | ); |
| 1128 | |
| 1129 | if (gather.kind === "mining") |
| 1130 | { |
| 1131 | if (!block || block.typeId !== BlockType.boulder |
| 1132 | || materialFromOreBoulderState(block.state) !== gather.materialId) |
| 1133 | { |
| 1134 | clearGather(world.ecs, handle); |
| 1135 | continue; |
| 1136 | } |
| 1137 | } |
| 1138 | else if (gather.kind === "woodcutting") |
| 1139 | { |
| 1140 | if (!block || block.typeId !== gather.typeId || !isChopableTreeBlock(block)) |
| 1141 | { |
| 1142 | clearGather(world.ecs, handle); |
| 1143 | continue; |
| 1144 | } |
| 1145 | } |
| 1146 | else if (gather.kind === "plant") |
| 1147 | { |
| 1148 | if (!block || block.typeId !== gather.typeId || !plantGatherForBlock(block)) |
| 1149 | { |
| 1150 | clearGather(world.ecs, handle); |
| 1151 | continue; |
| 1152 | } |
| 1153 | } |
| 1154 | else if (gather.kind === "debark") |
| 1155 | { |
| 1156 | const item = world.groundItems.get(gather.groundItemId); |
| 1157 | if (!item?.stack |
| 1158 | || (item.stack.itemId | 0) !== (gather.logItemId | 0) |
| 1159 | || isLogDebarked(item.stack) |
| 1160 | || item.tileX !== gather.tileX |
| 1161 | || item.tileY !== gather.tileY) |
| 1162 | { |
| 1163 | clearGather(world.ecs, handle); |
| 1164 | continue; |
| 1165 | } |
| 1166 | } |
| 1167 | else |
| 1168 | { |
| 1169 | clearGather(world.ecs, handle); |
| 1170 | continue; |
| 1171 | } |
| 1172 | |
| 1173 | if (now >= gather.nextSwingAt && now < gather.finishesAt) |
| 1174 | { |
| 1175 | gather.nextSwingAt = now + GATHER_SWING_INTERVAL_MS; |
| 1176 | events.push({ |
| 1177 | type: "gather-swing", |
| 1178 | attackerId: entityId, |
| 1179 | tileX: gather.tileX, |
| 1180 | tileY: gather.tileY, |
| 1181 | fromTileX: position.tileX, |
| 1182 | fromTileY: position.tileY, |
| 1183 | toolItemId: gather.toolItemId ?? null, |
| 1184 | swipe: false, |
| 1185 | dimensionId: normalizeDimensionId( |
| 1186 | gather.dimensionId ?? position.dimensionId ?? OVERWORLD_ID |
| 1187 | ) |
| 1188 | }); |
| 1189 | } |
| 1190 | |
| 1191 | if (now < gather.finishesAt) |
| 1192 | { continue; } |
| 1193 | |
| 1194 | if (gather.kind === "mining") |
| 1195 | { completeMining(world, handle, entityId, character, gather, events, privates, now); } |
| 1196 | else if (gather.kind === "woodcutting") |
| 1197 | { completeWoodcutting(world, handle, entityId, character, gather, events, privates, now); } |
| 1198 | else if (gather.kind === "debark") |
| 1199 | { completeDebark(world, handle, entityId, character, gather, events, privates, now); } |
| 1200 | else |
| 1201 | { completePlant(world, handle, entityId, character, gather, events, privates, now); } |
| 1202 | } |
| 1203 | return { events, privates }; |
| 1204 | } |
| 1205 | |
| 1206 | export function handleGatherCommand (world, sessionId, command, now) |
| 1207 | { |
| 1208 | switch (command.type) |
| 1209 | { |
| 1210 | case "mine": |
| 1211 | return startMine(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 1212 | case "chop": |
| 1213 | return startChop(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 1214 | case "pick": |
| 1215 | return startPick(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 1216 | case "debark-log": |
| 1217 | return startDebark(world, sessionId, command.characterId, command.groundItemId | 0, now); |
| 1218 | case "cancel-gather": |
| 1219 | { |
| 1220 | const entityId = world.characterEntities.get(command.characterId); |
| 1221 | if (entityId == null) |
| 1222 | { return { events: [] }; } |
| 1223 | const handle = world.ecs.handleOf(entityId); |
| 1224 | clearGather(world.ecs, handle); |
| 1225 | return { events: [] }; |
| 1226 | } |
| 1227 | default: |
| 1228 | return null; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | function defaultPlantState (typeId) |
| 1233 | { |
| 1234 | if (typeId === BlockType.herbPlant) |
| 1235 | { return "mint"; } |
| 1236 | if (typeId === BlockType.flaxPlant) |
| 1237 | { return "flax-plant"; } |
| 1238 | if (typeId === BlockType.silkCocoon) |
| 1239 | { return "silk-cocoon"; } |
| 1240 | return "cotton-plant"; |
| 1241 | } |