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