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