| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 3 | import { CraftingStationId, CRAFT_DURATION_MS } from "../../../shared/definitions/craftingStations.js"; |
| 4 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 5 | import { SkillId, skillLevel } from "../../../shared/definitions/skills.js"; |
| 6 | import { craftingStationDefinition } from "../../../shared/definitions/craftingStations.js"; |
| 7 | import { |
| 8 | recipeDefinition, |
| 9 | canStartCraft, |
| 10 | canCraftFitInventory, |
| 11 | hasRecipeInputs |
| 12 | } from "../../../shared/crafting/recipes.js"; |
| 13 | import { craftDurationMs, smeltingXpForMaterial } from "../../../shared/definitions/gathering.js"; |
| 14 | import { takeFromSlot, insertStack, stackQuantity } from "../../../shared/items/inventory.js"; |
| 15 | import { addSkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js"; |
| 16 | import { bagView } from "../items/bags.js"; |
| 17 | import { tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 18 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 19 | import { tileShoveTowardBlock } from "../simulation/tileInteract.js"; |
| 20 | |
| 21 | function userError (message) |
| 22 | { |
| 23 | const error = new Error(message); |
| 24 | error.userFacing = true; |
| 25 | return error; |
| 26 | } |
| 27 | |
| 28 | function ensureCraftSessions (world) |
| 29 | { |
| 30 | if (!world.craftSessions) |
| 31 | { world.craftSessions = new Map(); } |
| 32 | return world.craftSessions; |
| 33 | } |
| 34 | |
| 35 | function nearStationBlock (world, tileX, tileY, blockType) |
| 36 | { |
| 37 | for (let dy = -1; dy <= 1; dy++) |
| 38 | { |
| 39 | for (let dx = -1; dx <= 1; dx++) |
| 40 | { |
| 41 | const data = world.chunkData(tileToChunk(tileX + dx), tileToChunk(tileY + dy)); |
| 42 | const index = tileToLocal(tileY + dy) * CHUNK_SIZE + tileToLocal(tileX + dx); |
| 43 | if (data.blocks[index]?.typeId === blockType) |
| 44 | { return true; } |
| 45 | } |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | function stationBlockType (stationId) |
| 51 | { |
| 52 | if (stationId === CraftingStationId.furnace) |
| 53 | { return BlockType.furnace; } |
| 54 | if (stationId === CraftingStationId.anvil) |
| 55 | { return BlockType.anvil; } |
| 56 | if (stationId === CraftingStationId.woodworkingBench) |
| 57 | { return BlockType.woodworkingBench; } |
| 58 | if (stationId === CraftingStationId.craftingBench) |
| 59 | { return BlockType.craftingBench; } |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | function takeInputs (slots, inputs) |
| 64 | { |
| 65 | let next = slots.map((slot) => (slot ? structuredClone(slot) : null)); |
| 66 | for (const input of inputs ?? []) |
| 67 | { |
| 68 | let remaining = Math.max(0, stackQuantity(input.quantity)); |
| 69 | if (!remaining) |
| 70 | { continue; } |
| 71 | for (let i = 0; i < next.length && remaining > 0; i++) |
| 72 | { |
| 73 | if (!next[i] || next[i].itemId !== input.itemId) |
| 74 | { continue; } |
| 75 | const taken = takeFromSlot(next, i, remaining); |
| 76 | if (!taken.ok) |
| 77 | { continue; } |
| 78 | next = taken.slots; |
| 79 | remaining -= stackQuantity(taken.taken?.quantity); |
| 80 | } |
| 81 | if (remaining > 0) |
| 82 | { return { ok: false, slots }; } |
| 83 | } |
| 84 | return { ok: true, slots: next }; |
| 85 | } |
| 86 | |
| 87 | export function startCraft (world, sessionId, characterId, stationId, recipeId, now) |
| 88 | { |
| 89 | const entityId = world.characterEntities.get(characterId); |
| 90 | if (entityId == null) |
| 91 | { throw userError("Character is not in the world."); } |
| 92 | const handle = world.ecs.handleOf(entityId); |
| 93 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 94 | if (!identity || identity.sessionId !== sessionId) |
| 95 | { throw userError("Character is not in the world."); } |
| 96 | if (world.ecs.has(handle, Component.dead)) |
| 97 | { throw userError("You are dead."); } |
| 98 | |
| 99 | const stationDef = craftingStationDefinition(stationId); |
| 100 | if (!stationDef) |
| 101 | { throw userError("Unknown crafting station."); } |
| 102 | const recipe = recipeDefinition(recipeId); |
| 103 | if (!recipe) |
| 104 | { throw userError("Unknown recipe."); } |
| 105 | const stationIds = Array.isArray(recipe.stationIds) |
| 106 | ? recipe.stationIds |
| 107 | : [recipe.stationId]; |
| 108 | if (!stationIds.includes(stationId)) |
| 109 | { throw userError("That recipe cannot be made here."); } |
| 110 | |
| 111 | const position = world.ecs.get(handle, Component.position); |
| 112 | const blockType = stationBlockType(stationId); |
| 113 | if (blockType && !nearStationBlock(world, position.tileX, position.tileY, blockType)) |
| 114 | { throw userError("You need to stand next to the station."); } |
| 115 | |
| 116 | const inventory = world.ecs.get(handle, Component.inventory); |
| 117 | const skills = world.ecs.get(handle, Component.skills); |
| 118 | const level = skillLevel(skills, stationDef.skillId ?? SkillId.smithing); |
| 119 | if (!canStartCraft(inventory?.slots ?? [], recipe, level)) |
| 120 | { |
| 121 | if ((level | 0) < (recipe.minSkillLevel | 0 || 1)) |
| 122 | { throw userError(`Requires ${stationDef.skillId} ${recipe.minSkillLevel}.`); } |
| 123 | if (!hasRecipeInputs(inventory?.slots ?? [], recipe)) |
| 124 | { throw userError("Missing materials."); } |
| 125 | throw userError("Not enough bag space for the result."); |
| 126 | } |
| 127 | |
| 128 | const sessions = ensureCraftSessions(world); |
| 129 | const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS); |
| 130 | sessions.set(characterId, { |
| 131 | sessionId, |
| 132 | stationId, |
| 133 | recipeId, |
| 134 | finishesAt: now + durationMs |
| 135 | }); |
| 136 | const events = []; |
| 137 | if (blockType) |
| 138 | { |
| 139 | const shove = tileShoveTowardBlock(world, handle, entityId, blockType); |
| 140 | if (shove) |
| 141 | { events.push(shove); } |
| 142 | } |
| 143 | events.push({ |
| 144 | type: "item-bubble", |
| 145 | entityId, |
| 146 | itemId: recipe.output.itemId, |
| 147 | quantity: Math.max(1, recipe.output.quantity | 0 || 1) |
| 148 | }); |
| 149 | events.push({ |
| 150 | type: "craft-started", |
| 151 | sessionId, |
| 152 | characterId, |
| 153 | recipeId, |
| 154 | durationMs |
| 155 | }); |
| 156 | return { events }; |
| 157 | } |
| 158 | |
| 159 | export function openStation (world, sessionId, characterId, stationId) |
| 160 | { |
| 161 | const entityId = world.characterEntities.get(characterId); |
| 162 | if (entityId == null) |
| 163 | { throw userError("Character is not in the world."); } |
| 164 | const handle = world.ecs.handleOf(entityId); |
| 165 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 166 | if (!identity || identity.sessionId !== sessionId) |
| 167 | { throw userError("Character is not in the world."); } |
| 168 | if (world.ecs.has(handle, Component.dead)) |
| 169 | { throw userError("You are dead."); } |
| 170 | |
| 171 | const stationDef = craftingStationDefinition(stationId); |
| 172 | if (!stationDef) |
| 173 | { throw userError("Unknown crafting station."); } |
| 174 | const blockType = stationBlockType(stationId); |
| 175 | const position = world.ecs.get(handle, Component.position); |
| 176 | if (blockType && !nearStationBlock(world, position.tileX, position.tileY, blockType)) |
| 177 | { throw userError("You need to stand next to the station."); } |
| 178 | |
| 179 | const events = []; |
| 180 | if (blockType) |
| 181 | { |
| 182 | const shove = tileShoveTowardBlock(world, handle, entityId, blockType); |
| 183 | if (shove) |
| 184 | { events.push(shove); } |
| 185 | } |
| 186 | events.push({ |
| 187 | type: "station-open", |
| 188 | sessionId, |
| 189 | characterId, |
| 190 | stationId |
| 191 | }); |
| 192 | return { events }; |
| 193 | } |
| 194 | |
| 195 | export function cancelCraft (world, characterId) |
| 196 | { |
| 197 | ensureCraftSessions(world).delete(characterId); |
| 198 | return { events: [] }; |
| 199 | } |
| 200 | |
| 201 | export function stepCrafting (world, now) |
| 202 | { |
| 203 | const sessions = ensureCraftSessions(world); |
| 204 | const events = []; |
| 205 | const privates = []; |
| 206 | for (const [characterId, session] of [...sessions.entries()]) |
| 207 | { |
| 208 | if (now < session.finishesAt) |
| 209 | { continue; } |
| 210 | sessions.delete(characterId); |
| 211 | const entityId = world.characterEntities.get(characterId); |
| 212 | if (entityId == null) |
| 213 | { |
| 214 | events.push({ |
| 215 | type: "craft-result", |
| 216 | sessionId: session.sessionId, |
| 217 | recipeId: session.recipeId, |
| 218 | ok: false, |
| 219 | error: "Character left the world." |
| 220 | }); |
| 221 | continue; |
| 222 | } |
| 223 | const handle = world.ecs.handleOf(entityId); |
| 224 | if (world.ecs.has(handle, Component.dead)) |
| 225 | { |
| 226 | events.push({ |
| 227 | type: "craft-result", |
| 228 | sessionId: session.sessionId, |
| 229 | recipeId: session.recipeId, |
| 230 | ok: false, |
| 231 | error: "You are dead." |
| 232 | }); |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | const recipe = recipeDefinition(session.recipeId); |
| 237 | const stationDef = craftingStationDefinition(session.stationId); |
| 238 | const inventory = world.ecs.get(handle, Component.inventory); |
| 239 | const skills = world.ecs.get(handle, Component.skills); |
| 240 | const position = world.ecs.get(handle, Component.position); |
| 241 | const blockType = stationBlockType(session.stationId); |
| 242 | const level = skillLevel(skills, stationDef?.skillId ?? SkillId.smithing); |
| 243 | |
| 244 | if (!recipe || !stationDef || !inventory |
| 245 | || (blockType && !nearStationBlock(world, position.tileX, position.tileY, blockType)) |
| 246 | || !canCraftFitInventory(inventory.slots, recipe) |
| 247 | || (level | 0) < (recipe.minSkillLevel | 0 || 1)) |
| 248 | { |
| 249 | events.push({ |
| 250 | type: "craft-result", |
| 251 | sessionId: session.sessionId, |
| 252 | recipeId: session.recipeId, |
| 253 | ok: false, |
| 254 | error: "Could not finish crafting." |
| 255 | }); |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | const taken = takeInputs(inventory.slots, recipe.inputs); |
| 260 | if (!taken.ok) |
| 261 | { |
| 262 | events.push({ |
| 263 | type: "craft-result", |
| 264 | sessionId: session.sessionId, |
| 265 | recipeId: session.recipeId, |
| 266 | ok: false, |
| 267 | error: "Missing materials." |
| 268 | }); |
| 269 | continue; |
| 270 | } |
| 271 | const outDef = itemDefinition(recipe.output.itemId); |
| 272 | const added = insertStack(taken.slots, { |
| 273 | itemId: recipe.output.itemId, |
| 274 | quantity: Math.max(1, recipe.output.quantity | 0 || 1) |
| 275 | }, outDef); |
| 276 | if (!added.ok || added.remainder > 0) |
| 277 | { |
| 278 | events.push({ |
| 279 | type: "craft-result", |
| 280 | sessionId: session.sessionId, |
| 281 | recipeId: session.recipeId, |
| 282 | ok: false, |
| 283 | error: "Not enough bag space for the result." |
| 284 | }); |
| 285 | continue; |
| 286 | } |
| 287 | inventory.slots = added.slots; |
| 288 | |
| 289 | let skillsChanged = false; |
| 290 | if (skills?.xp && recipe.xpMaterial && stationDef?.skillId) |
| 291 | { |
| 292 | if (addSkillXp(skills, stationDef.skillId, smeltingXpForMaterial(recipe.xpMaterial))) |
| 293 | { skillsChanged = true; } |
| 294 | } |
| 295 | |
| 296 | privates.push({ characterId, bag: bagView(world.ecs, handle) }); |
| 297 | if (skillsChanged) |
| 298 | { |
| 299 | events.push({ |
| 300 | type: "skills-updated", |
| 301 | characterId, |
| 302 | skills: publicSkillsView(skills), |
| 303 | stored: storedSkillXp(skills) |
| 304 | }); |
| 305 | } |
| 306 | events.push({ |
| 307 | type: "craft-result", |
| 308 | sessionId: session.sessionId, |
| 309 | recipeId: session.recipeId, |
| 310 | ok: true |
| 311 | }); |
| 312 | } |
| 313 | return { events, privates }; |
| 314 | } |
| 315 | |
| 316 | export function handleCraftCommand (world, sessionId, command, now) |
| 317 | { |
| 318 | switch (command.type) |
| 319 | { |
| 320 | case "open-station": |
| 321 | return openStation(world, sessionId, command.characterId, command.stationId); |
| 322 | case "start-craft": |
| 323 | return startCraft( |
| 324 | world, |
| 325 | sessionId, |
| 326 | command.characterId, |
| 327 | command.stationId, |
| 328 | command.recipeId, |
| 329 | now |
| 330 | ); |
| 331 | case "cancel-craft": |
| 332 | return cancelCraft(world, command.characterId); |
| 333 | default: |
| 334 | return null; |
| 335 | } |
| 336 | } |