| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { BlockType, woodTierForTreeBlock, isChopableTreeBlock } from "../../../shared/definitions/tiles.js"; |
| 3 | import { itemDefinition } 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 | woodcuttingDurationMs, |
| 16 | woodcuttingXpForWood, |
| 17 | treeExhaustDurationMs, |
| 18 | logItemIdForWoodTier |
| 19 | } from "../../../shared/definitions/gathering.js"; |
| 20 | import { insertStack } from "../../../shared/items/inventory.js"; |
| 21 | import { attachOrigins, GatherOriginVerb, gatherOrigin } from "../../../shared/items/origin.js"; |
| 22 | import { addSkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js"; |
| 23 | import { seasonIdAt, treeStumpStateForSeason, SeasonId } from "../../../shared/world/season.js"; |
| 24 | import { bagView } from "../items/bags.js"; |
| 25 | import { chebyshev, clearEngage } from "../combat/service.js"; |
| 26 | import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 27 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 28 | |
| 29 | const GATHER_SWING_INTERVAL_MS = 900; |
| 30 | |
| 31 | function userError (message) |
| 32 | { |
| 33 | const error = new Error(message); |
| 34 | error.userFacing = true; |
| 35 | return error; |
| 36 | } |
| 37 | |
| 38 | function characterDisplayName (world, handle) |
| 39 | { |
| 40 | const appearance = world.ecs.get(handle, Component.appearance); |
| 41 | const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim(); |
| 42 | return name || "someone"; |
| 43 | } |
| 44 | |
| 45 | function ensureOreRespawns (world) |
| 46 | { |
| 47 | if (!world.oreRespawns) |
| 48 | { world.oreRespawns = new Map(); } |
| 49 | return world.oreRespawns; |
| 50 | } |
| 51 | |
| 52 | function ensureTreeRespawns (world) |
| 53 | { |
| 54 | if (!world.treeRespawns) |
| 55 | { world.treeRespawns = new Map(); } |
| 56 | return world.treeRespawns; |
| 57 | } |
| 58 | |
| 59 | function blockAt (world, tileX, tileY) |
| 60 | { |
| 61 | const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY)); |
| 62 | return data?.blocks?.[tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX)] ?? null; |
| 63 | } |
| 64 | |
| 65 | function setBlockState (world, tileX, tileY, typeId, state) |
| 66 | { |
| 67 | const chunkX = tileToChunk(tileX); |
| 68 | const chunkY = tileToChunk(tileY); |
| 69 | const localX = tileToLocal(tileX); |
| 70 | const localY = tileToLocal(tileY); |
| 71 | const data = world.chunkData(chunkX, chunkY); |
| 72 | const index = localY * CHUNK_SIZE + localX; |
| 73 | const prev = data?.blocks?.[index]; |
| 74 | if (!prev || prev.typeId !== typeId) |
| 75 | { return null; } |
| 76 | const block = { typeId: prev.typeId, state, config: prev.config ?? null }; |
| 77 | data.blocks[index] = block; |
| 78 | return { |
| 79 | type: "chunk-delta", |
| 80 | chunkX, |
| 81 | chunkY, |
| 82 | tileX, |
| 83 | tileY, |
| 84 | localX, |
| 85 | localY, |
| 86 | block |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | function setBoulderState (world, tileX, tileY, state) |
| 91 | { |
| 92 | return setBlockState(world, tileX, tileY, BlockType.boulder, state); |
| 93 | } |
| 94 | |
| 95 | function setTreeState (world, tileX, tileY, typeId, state) |
| 96 | { |
| 97 | return setBlockState(world, tileX, tileY, typeId, state); |
| 98 | } |
| 99 | |
| 100 | function pickaxeForOre (world, handle, materialId) |
| 101 | { |
| 102 | const inventory = world.ecs.get(handle, Component.inventory); |
| 103 | const equipment = world.ecs.get(handle, Component.equipment); |
| 104 | const skills = world.ecs.get(handle, Component.skills); |
| 105 | return resolveMiningPickaxe({ |
| 106 | inventorySlots: inventory?.slots, |
| 107 | equipmentSlots: equipment?.slots, |
| 108 | skills, |
| 109 | materialId |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | function axeForWood (world, handle, woodTier) |
| 114 | { |
| 115 | const inventory = world.ecs.get(handle, Component.inventory); |
| 116 | const equipment = world.ecs.get(handle, Component.equipment); |
| 117 | const skills = world.ecs.get(handle, Component.skills); |
| 118 | return resolveWoodcuttingAxe({ |
| 119 | inventorySlots: inventory?.slots, |
| 120 | equipmentSlots: equipment?.slots, |
| 121 | skills, |
| 122 | woodTier |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | function clearGather (ecs, handle) |
| 127 | { |
| 128 | if (ecs.has(handle, Component.gather)) |
| 129 | { ecs.remove(handle, Component.gather); } |
| 130 | } |
| 131 | |
| 132 | export function cancelGather (ecs, handle) |
| 133 | { |
| 134 | clearGather(ecs, handle); |
| 135 | } |
| 136 | |
| 137 | /** Drop scheduled ore respawns when authored chunk data is replaced or unloaded. */ |
| 138 | export function clearOreRespawnsForChunk (world, chunkX, chunkY) |
| 139 | { |
| 140 | const respawns = world.oreRespawns; |
| 141 | if (!respawns?.size) |
| 142 | { return; } |
| 143 | for (const key of [...respawns.keys()]) |
| 144 | { |
| 145 | const [tileX, tileY] = key.split(",").map(Number); |
| 146 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 147 | { respawns.delete(key); } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** Drop scheduled tree respawns when authored chunk data is replaced or unloaded. */ |
| 152 | export function clearTreeRespawnsForChunk (world, chunkX, chunkY) |
| 153 | { |
| 154 | const respawns = world.treeRespawns; |
| 155 | if (!respawns?.size) |
| 156 | { return; } |
| 157 | for (const key of [...respawns.keys()]) |
| 158 | { |
| 159 | const [tileX, tileY] = key.split(",").map(Number); |
| 160 | if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) |
| 161 | { respawns.delete(key); } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Restore exhausted ore boulders whose timers have elapsed. |
| 167 | */ |
| 168 | export function stepOreRespawns (world, now) |
| 169 | { |
| 170 | const respawns = world.oreRespawns; |
| 171 | if (!respawns?.size) |
| 172 | { return { events: [] }; } |
| 173 | const events = []; |
| 174 | for (const [key, entry] of [...respawns.entries()]) |
| 175 | { |
| 176 | if (now < entry.respawnsAt) |
| 177 | { continue; } |
| 178 | respawns.delete(key); |
| 179 | const delta = setBoulderState(world, entry.tileX, entry.tileY, oreBoulderState(entry.materialId)); |
| 180 | if (delta) |
| 181 | { events.push(delta); } |
| 182 | } |
| 183 | return { events }; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Restore exhausted trees whose timers have elapsed (live seasonal look). |
| 188 | */ |
| 189 | export function stepTreeRespawns (world, now) |
| 190 | { |
| 191 | const respawns = world.treeRespawns; |
| 192 | if (!respawns?.size) |
| 193 | { return { events: [] }; } |
| 194 | const events = []; |
| 195 | for (const [key, entry] of [...respawns.entries()]) |
| 196 | { |
| 197 | if (now < entry.respawnsAt) |
| 198 | { continue; } |
| 199 | respawns.delete(key); |
| 200 | const delta = setTreeState( |
| 201 | world, |
| 202 | entry.tileX, |
| 203 | entry.tileY, |
| 204 | entry.typeId, |
| 205 | SeasonId.summer |
| 206 | ); |
| 207 | if (delta) |
| 208 | { events.push(delta); } |
| 209 | } |
| 210 | return { events }; |
| 211 | } |
| 212 | |
| 213 | function beginGatherSwing (world, handle, entityId, position, gatherFields, now) |
| 214 | { |
| 215 | clearEngage(world.ecs, entityId); |
| 216 | const path = world.ecs.get(handle, Component.path); |
| 217 | if (path) |
| 218 | { path.tiles = []; } |
| 219 | world.ecs.add(handle, Component.gather, { |
| 220 | ...gatherFields, |
| 221 | startedAt: now, |
| 222 | finishesAt: now + gatherFields.durationMs, |
| 223 | nextSwingAt: now |
| 224 | }); |
| 225 | return { |
| 226 | events: [{ |
| 227 | type: "gather-swing", |
| 228 | attackerId: entityId, |
| 229 | tileX: gatherFields.tileX, |
| 230 | tileY: gatherFields.tileY, |
| 231 | fromTileX: position.tileX, |
| 232 | fromTileY: position.tileY, |
| 233 | toolItemId: gatherFields.toolItemId ?? null, |
| 234 | swipe: false |
| 235 | }] |
| 236 | }; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Start mining an ore boulder. Returns events / private bag updates when complete via stepGather. |
| 241 | */ |
| 242 | export function startMine (world, sessionId, characterId, tileX, tileY, now) |
| 243 | { |
| 244 | const entityId = world.characterEntities.get(characterId); |
| 245 | if (entityId == null) |
| 246 | { throw userError("Character is not in the world."); } |
| 247 | const handle = world.ecs.handleOf(entityId); |
| 248 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 249 | if (!identity || identity.sessionId !== sessionId) |
| 250 | { throw userError("Character is not in the world."); } |
| 251 | if (world.ecs.has(handle, Component.dead)) |
| 252 | { throw userError("You are dead."); } |
| 253 | |
| 254 | const position = world.ecs.get(handle, Component.position); |
| 255 | if (chebyshev(position.tileX, position.tileY, tileX, tileY) > 1) |
| 256 | { throw userError("You need to stand next to the rock."); } |
| 257 | |
| 258 | const block = blockAt(world, tileX, tileY); |
| 259 | if (!block || block.typeId !== BlockType.boulder) |
| 260 | { throw userError("There is nothing to mine there."); } |
| 261 | const materialId = materialFromOreBoulderState(block.state); |
| 262 | if (!materialId) |
| 263 | { |
| 264 | throw userError(block.state === EXHAUSTED_ORE_BOULDER_STATE |
| 265 | ? "That rock is exhausted." |
| 266 | : "That rock has no ore."); |
| 267 | } |
| 268 | |
| 269 | const pickaxe = pickaxeForOre(world, handle, materialId); |
| 270 | if (!pickaxe.ok) |
| 271 | { throw userError(pickaxe.reason); } |
| 272 | |
| 273 | const durationMs = miningDurationMs(materialId, pickaxe.tool.tier); |
| 274 | const toolItemId = pickaxe.tool.stack?.itemId ?? null; |
| 275 | return beginGatherSwing(world, handle, entityId, position, { |
| 276 | kind: "mining", |
| 277 | tileX, |
| 278 | tileY, |
| 279 | materialId, |
| 280 | toolTier: pickaxe.tool.tier, |
| 281 | toolItemId, |
| 282 | durationMs |
| 283 | }, now); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Start chopping a tree. Completes via stepGather (stump + log + woodcutting XP). |
| 288 | */ |
| 289 | export function startChop (world, sessionId, characterId, tileX, tileY, now) |
| 290 | { |
| 291 | const entityId = world.characterEntities.get(characterId); |
| 292 | if (entityId == null) |
| 293 | { throw userError("Character is not in the world."); } |
| 294 | const handle = world.ecs.handleOf(entityId); |
| 295 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 296 | if (!identity || identity.sessionId !== sessionId) |
| 297 | { throw userError("Character is not in the world."); } |
| 298 | if (world.ecs.has(handle, Component.dead)) |
| 299 | { throw userError("You are dead."); } |
| 300 | |
| 301 | const position = world.ecs.get(handle, Component.position); |
| 302 | if (chebyshev(position.tileX, position.tileY, tileX, tileY) > 1) |
| 303 | { throw userError("You need to stand next to the tree."); } |
| 304 | |
| 305 | const block = blockAt(world, tileX, tileY); |
| 306 | if (!isChopableTreeBlock(block)) |
| 307 | { |
| 308 | throw userError(block && woodTierForTreeBlock(block.typeId) != null |
| 309 | ? "That tree is already felled." |
| 310 | : "There is nothing to chop there."); |
| 311 | } |
| 312 | const woodTier = woodTierForTreeBlock(block.typeId); |
| 313 | const axe = axeForWood(world, handle, woodTier); |
| 314 | if (!axe.ok) |
| 315 | { throw userError(axe.reason); } |
| 316 | |
| 317 | const durationMs = woodcuttingDurationMs(woodTier, axe.tool.tier); |
| 318 | const toolItemId = axe.tool.stack?.itemId ?? null; |
| 319 | return beginGatherSwing(world, handle, entityId, position, { |
| 320 | kind: "woodcutting", |
| 321 | tileX, |
| 322 | tileY, |
| 323 | typeId: block.typeId, |
| 324 | woodTier, |
| 325 | toolTier: axe.tool.tier, |
| 326 | toolItemId, |
| 327 | durationMs |
| 328 | }, now); |
| 329 | } |
| 330 | |
| 331 | function pushBagUpdate (events, privates, world, handle, character, skillsChanged, skills) |
| 332 | { |
| 333 | privates.push({ |
| 334 | characterId: character.characterId, |
| 335 | bag: bagView(world.ecs, handle) |
| 336 | }); |
| 337 | if (skillsChanged) |
| 338 | { |
| 339 | events.push({ |
| 340 | type: "skills-updated", |
| 341 | characterId: character.characterId, |
| 342 | skills: publicSkillsView(skills), |
| 343 | stored: storedSkillXp(skills) |
| 344 | }); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | function completeMining (world, handle, entityId, character, gather, events, privates, now) |
| 349 | { |
| 350 | const oreItemId = oreItemIdForMaterial(gather.materialId); |
| 351 | const def = itemDefinition(oreItemId); |
| 352 | const inventory = world.ecs.get(handle, Component.inventory); |
| 353 | if (!oreItemId || !def || !inventory) |
| 354 | { |
| 355 | clearGather(world.ecs, handle); |
| 356 | return; |
| 357 | } |
| 358 | const origin = gatherOrigin(GatherOriginVerb.mined, characterDisplayName(world, handle)); |
| 359 | const stack = attachOrigins({ |
| 360 | itemId: oreItemId, |
| 361 | quantity: 1, |
| 362 | origins: [{ origin, quantity: 1 }] |
| 363 | }); |
| 364 | const inserted = insertStack(inventory.slots, stack, def); |
| 365 | if (!inserted.ok || inserted.remainder > 0) |
| 366 | { |
| 367 | clearGather(world.ecs, handle); |
| 368 | events.push({ |
| 369 | type: "command-error", |
| 370 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 371 | message: "Your inventory is full." |
| 372 | }); |
| 373 | return; |
| 374 | } |
| 375 | inventory.slots = inserted.slots; |
| 376 | |
| 377 | const skills = world.ecs.get(handle, Component.skills); |
| 378 | let skillsChanged = false; |
| 379 | if (skills?.xp && addSkillXp(skills, SkillId.mining, miningXpForMaterial(gather.materialId))) |
| 380 | { skillsChanged = true; } |
| 381 | |
| 382 | const exhausted = setBoulderState( |
| 383 | world, |
| 384 | gather.tileX, |
| 385 | gather.tileY, |
| 386 | EXHAUSTED_ORE_BOULDER_STATE |
| 387 | ); |
| 388 | if (exhausted) |
| 389 | { |
| 390 | events.push(exhausted); |
| 391 | ensureOreRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 392 | tileX: gather.tileX, |
| 393 | tileY: gather.tileY, |
| 394 | materialId: gather.materialId, |
| 395 | respawnsAt: now + oreExhaustDurationMs(gather.materialId) |
| 396 | }); |
| 397 | } |
| 398 | |
| 399 | clearGather(world.ecs, handle); |
| 400 | pushBagUpdate(events, privates, world, handle, character, skillsChanged, skills); |
| 401 | events.push({ |
| 402 | type: "gather-complete", |
| 403 | characterId: character.characterId, |
| 404 | kind: "mining", |
| 405 | materialId: gather.materialId, |
| 406 | itemId: oreItemId |
| 407 | }); |
| 408 | } |
| 409 | |
| 410 | function completeWoodcutting (world, handle, entityId, character, gather, events, privates, now) |
| 411 | { |
| 412 | const logItemId = logItemIdForWoodTier(gather.woodTier); |
| 413 | const def = itemDefinition(logItemId); |
| 414 | const inventory = world.ecs.get(handle, Component.inventory); |
| 415 | if (!logItemId || !def || !inventory) |
| 416 | { |
| 417 | clearGather(world.ecs, handle); |
| 418 | return; |
| 419 | } |
| 420 | const origin = gatherOrigin(GatherOriginVerb.felled, characterDisplayName(world, handle)); |
| 421 | const stack = attachOrigins({ |
| 422 | itemId: logItemId, |
| 423 | quantity: 1, |
| 424 | origins: [{ origin, quantity: 1 }] |
| 425 | }); |
| 426 | const inserted = insertStack(inventory.slots, stack, def); |
| 427 | if (!inserted.ok || inserted.remainder > 0) |
| 428 | { |
| 429 | clearGather(world.ecs, handle); |
| 430 | events.push({ |
| 431 | type: "command-error", |
| 432 | sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, |
| 433 | message: "Your inventory is full." |
| 434 | }); |
| 435 | return; |
| 436 | } |
| 437 | inventory.slots = inserted.slots; |
| 438 | |
| 439 | const skills = world.ecs.get(handle, Component.skills); |
| 440 | let skillsChanged = false; |
| 441 | if (skills?.xp && addSkillXp(skills, SkillId.woodcutting, woodcuttingXpForWood(gather.woodTier))) |
| 442 | { skillsChanged = true; } |
| 443 | |
| 444 | const stumpState = treeStumpStateForSeason(seasonIdAt(now)); |
| 445 | const exhausted = setTreeState( |
| 446 | world, |
| 447 | gather.tileX, |
| 448 | gather.tileY, |
| 449 | gather.typeId, |
| 450 | stumpState |
| 451 | ); |
| 452 | if (exhausted) |
| 453 | { |
| 454 | events.push(exhausted); |
| 455 | ensureTreeRespawns(world).set(tileKey(gather.tileX, gather.tileY), { |
| 456 | tileX: gather.tileX, |
| 457 | tileY: gather.tileY, |
| 458 | typeId: gather.typeId, |
| 459 | woodTier: gather.woodTier, |
| 460 | respawnsAt: now + treeExhaustDurationMs(gather.woodTier) |
| 461 | }); |
| 462 | } |
| 463 | |
| 464 | clearGather(world.ecs, handle); |
| 465 | pushBagUpdate(events, privates, world, handle, character, skillsChanged, skills); |
| 466 | events.push({ |
| 467 | type: "gather-complete", |
| 468 | characterId: character.characterId, |
| 469 | kind: "woodcutting", |
| 470 | woodTier: gather.woodTier, |
| 471 | itemId: logItemId |
| 472 | }); |
| 473 | } |
| 474 | |
| 475 | export function stepGather (world, now) |
| 476 | { |
| 477 | const events = []; |
| 478 | const privates = []; |
| 479 | events.push(...stepOreRespawns(world, now).events); |
| 480 | events.push(...stepTreeRespawns(world, now).events); |
| 481 | |
| 482 | for (const entity of world.ecs.query(Component.gather, Component.position, Component.character)) |
| 483 | { |
| 484 | const { handle, gather, position, character, entityId } = entity; |
| 485 | if (world.ecs.has(handle, Component.dead)) |
| 486 | { |
| 487 | clearGather(world.ecs, handle); |
| 488 | continue; |
| 489 | } |
| 490 | if (chebyshev(position.tileX, position.tileY, gather.tileX, gather.tileY) > 1) |
| 491 | { |
| 492 | clearGather(world.ecs, handle); |
| 493 | continue; |
| 494 | } |
| 495 | const block = blockAt(world, gather.tileX, gather.tileY); |
| 496 | |
| 497 | if (gather.kind === "mining") |
| 498 | { |
| 499 | if (!block || block.typeId !== BlockType.boulder |
| 500 | || materialFromOreBoulderState(block.state) !== gather.materialId) |
| 501 | { |
| 502 | clearGather(world.ecs, handle); |
| 503 | continue; |
| 504 | } |
| 505 | } |
| 506 | else if (gather.kind === "woodcutting") |
| 507 | { |
| 508 | if (!block || block.typeId !== gather.typeId || !isChopableTreeBlock(block)) |
| 509 | { |
| 510 | clearGather(world.ecs, handle); |
| 511 | continue; |
| 512 | } |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | clearGather(world.ecs, handle); |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | if (now >= gather.nextSwingAt && now < gather.finishesAt) |
| 521 | { |
| 522 | gather.nextSwingAt = now + GATHER_SWING_INTERVAL_MS; |
| 523 | events.push({ |
| 524 | type: "gather-swing", |
| 525 | attackerId: entityId, |
| 526 | tileX: gather.tileX, |
| 527 | tileY: gather.tileY, |
| 528 | fromTileX: position.tileX, |
| 529 | fromTileY: position.tileY, |
| 530 | toolItemId: gather.toolItemId ?? null, |
| 531 | swipe: false |
| 532 | }); |
| 533 | } |
| 534 | |
| 535 | if (now < gather.finishesAt) |
| 536 | { continue; } |
| 537 | |
| 538 | if (gather.kind === "mining") |
| 539 | { completeMining(world, handle, entityId, character, gather, events, privates, now); } |
| 540 | else |
| 541 | { completeWoodcutting(world, handle, entityId, character, gather, events, privates, now); } |
| 542 | } |
| 543 | return { events, privates }; |
| 544 | } |
| 545 | |
| 546 | export function handleGatherCommand (world, sessionId, command, now) |
| 547 | { |
| 548 | switch (command.type) |
| 549 | { |
| 550 | case "mine": |
| 551 | return startMine(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 552 | case "chop": |
| 553 | return startChop(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); |
| 554 | case "cancel-gather": |
| 555 | { |
| 556 | const entityId = world.characterEntities.get(command.characterId); |
| 557 | if (entityId == null) |
| 558 | { return { events: [] }; } |
| 559 | const handle = world.ecs.handleOf(entityId); |
| 560 | clearGather(world.ecs, handle); |
| 561 | return { events: [] }; |
| 562 | } |
| 563 | default: |
| 564 | return null; |
| 565 | } |
| 566 | } |