| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 3 | import { CraftingItemId } from "../../../shared/definitions/craftingItems.js"; |
| 4 | import { ItemFamilyId } from "../../../shared/definitions/tierFamilies.js"; |
| 5 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 6 | import { cookedFishItemId } from "../../../shared/definitions/fishItems.js"; |
| 7 | import { SkillId } from "../../../shared/definitions/skills.js"; |
| 8 | import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js"; |
| 9 | import { rollSkill } from "../../../shared/combat/rolls.js"; |
| 10 | import { firestartingDifficulty, campfireDurationMs } from "../../../shared/definitions/craftingStations.js"; |
| 11 | import { MATERIAL_DISPLAY_NAME } from "../../../shared/definitions/materialTiers.js"; |
| 12 | import { stackQuantity } from "../../../shared/items/inventory.js"; |
| 13 | import { attachOrigins, craftOrigin, CraftOriginVerb, normalizeOrigins, splitOrigins } from "../../../shared/items/origin.js"; |
| 14 | import |
| 15 | { |
| 16 | placeOrMergeGroundStack, |
| 17 | publicGroundItem, |
| 18 | removeGroundItem |
| 19 | } from "../items/bags.js"; |
| 20 | import { tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 21 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 22 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 23 | |
| 24 | const TINDERS_COOK_BURN_CHANCE = 0.4; |
| 25 | const COOK_DIFFICULTY = 1; |
| 26 | |
| 27 | function woodTierFromLogItemId (itemId) |
| 28 | { |
| 29 | const def = itemDefinition(itemId); |
| 30 | if (!def || def.familyId !== ItemFamilyId.log) |
| 31 | { return null; } |
| 32 | return def.tier ?? null; |
| 33 | } |
| 34 | |
| 35 | function groundItemsAt (world, tileX, tileY) |
| 36 | { |
| 37 | const out = []; |
| 38 | for (const item of world.groundItems?.values?.() ?? []) |
| 39 | { |
| 40 | if (item.tileX === tileX && item.tileY === tileY) |
| 41 | { out.push(item); } |
| 42 | } |
| 43 | return out; |
| 44 | } |
| 45 | |
| 46 | function blockAt (world, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 47 | { |
| 48 | const dim = normalizeDimensionId(dimensionId); |
| 49 | const chunkX = tileToChunk(tileX); |
| 50 | const chunkY = tileToChunk(tileY); |
| 51 | const data = world.chunkData?.(chunkX, chunkY, dim); |
| 52 | if (!data) |
| 53 | { return null; } |
| 54 | const index = tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX); |
| 55 | return data.blocks?.[index] ?? null; |
| 56 | } |
| 57 | |
| 58 | function placeCampfire (world, tileX, tileY, woodTier, litByName, now, dimensionId = OVERWORLD_ID) |
| 59 | { |
| 60 | const dim = normalizeDimensionId(dimensionId); |
| 61 | const chunkX = tileToChunk(tileX); |
| 62 | const chunkY = tileToChunk(tileY); |
| 63 | const localX = tileToLocal(tileX); |
| 64 | const localY = tileToLocal(tileY); |
| 65 | const data = world.chunkData(chunkX, chunkY, dim); |
| 66 | const index = localY * CHUNK_SIZE + localX; |
| 67 | const expiresAt = now + campfireDurationMs(woodTier); |
| 68 | const block = { |
| 69 | typeId: BlockType.campfire, |
| 70 | state: woodTier, |
| 71 | config: { woodTier, litAt: now, expiresAt, litByName } |
| 72 | }; |
| 73 | data.blocks[index] = block; |
| 74 | if (!world.campfireExpires) |
| 75 | { world.campfireExpires = new Map(); } |
| 76 | world.campfireExpires.set(`${dim}:${tileX},${tileY}`, { |
| 77 | tileX, |
| 78 | tileY, |
| 79 | dimensionId: dim, |
| 80 | woodTier, |
| 81 | expiresAt, |
| 82 | litByName |
| 83 | }); |
| 84 | return { |
| 85 | type: "chunk-delta", |
| 86 | chunkX, |
| 87 | chunkY, |
| 88 | dimensionId: dim, |
| 89 | tileX, |
| 90 | tileY, |
| 91 | localX, |
| 92 | localY, |
| 93 | block |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | function consumeOneGroundStack (world, item, events) |
| 98 | { |
| 99 | const qty = stackQuantity(item.stack?.quantity); |
| 100 | if (qty <= 1) |
| 101 | { |
| 102 | removeGroundItem(world, item.groundItemId); |
| 103 | events.push({ type: "ground-item-despawn", ...publicGroundItem(item) }); |
| 104 | return; |
| 105 | } |
| 106 | const split = splitOrigins(normalizeOrigins(item.stack), 1); |
| 107 | item.stack.quantity = qty - 1; |
| 108 | item.stack.origins = split.remain; |
| 109 | delete item.stack.origin; |
| 110 | events.push({ |
| 111 | type: "ground-item-appear", |
| 112 | ...publicGroundItem(item), |
| 113 | stack: item.stack, |
| 114 | droppedAt: item.droppedAt, |
| 115 | source: item.source, |
| 116 | expiresAt: item.expiresAt |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | function cookableRawDef (itemId) |
| 121 | { |
| 122 | const def = itemDefinition(itemId); |
| 123 | if (!def?.cookable || def.cookState !== "raw") |
| 124 | { return null; } |
| 125 | return def; |
| 126 | } |
| 127 | |
| 128 | function cookedItemIdForRaw (rawDef) |
| 129 | { |
| 130 | if (rawDef.fishSpeciesId) |
| 131 | { return cookedFishItemId(rawDef.fishSpeciesId); } |
| 132 | if (rawDef.cookedItemId) |
| 133 | { return rawDef.cookedItemId; } |
| 134 | if (rawDef.itemId === CraftingItemId.rawMeat) |
| 135 | { return CraftingItemId.cookedMeat; } |
| 136 | return CraftingItemId.cookedMeat; |
| 137 | } |
| 138 | |
| 139 | function spawnGroundStack (world, tileX, tileY, stack, now, events) |
| 140 | { |
| 141 | const placed = placeOrMergeGroundStack(world, { |
| 142 | tileX, |
| 143 | tileY, |
| 144 | stack, |
| 145 | now, |
| 146 | source: "tinders", |
| 147 | despawnDurationMs: 1000 * 60 * 30 |
| 148 | }); |
| 149 | events.push(...placed.events); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * True while Tinders still has work on this tile (unlit logs or raw food). |
| 154 | * Used to stop auto-cast once a campfire is lit or nothing remains. |
| 155 | */ |
| 156 | export function tindersTileStillNeedsCast (world, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 157 | { |
| 158 | const dim = normalizeDimensionId(dimensionId); |
| 159 | if (blockAt(world, tileX, tileY, dim)?.typeId === BlockType.campfire) |
| 160 | { return false; } |
| 161 | const items = groundItemsAt(world, tileX, tileY); |
| 162 | if (items.some((item) => woodTierFromLogItemId(item.stack?.itemId))) |
| 163 | { return true; } |
| 164 | if (items.some((item) => cookableRawDef(item.stack?.itemId))) |
| 165 | { return true; } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Tinders: Mind roll lights ground logs (as firestarting) or cooks raw meat/fish (40% burn). |
| 171 | */ |
| 172 | export function applyTindersAtTile (world, impact, now) |
| 173 | { |
| 174 | const events = []; |
| 175 | const tileX = impact.targetTileX | 0; |
| 176 | const tileY = impact.targetTileY | 0; |
| 177 | const casterHandle = world.ecs.handleOf(impact.casterEntityId); |
| 178 | const casterPos = casterHandle != null |
| 179 | ? world.ecs.get(casterHandle, Component.position) |
| 180 | : null; |
| 181 | const dim = normalizeDimensionId( |
| 182 | impact.dimensionId ?? casterPos?.dimensionId ?? OVERWORLD_ID |
| 183 | ); |
| 184 | const mind = impact.levels?.mind | 0 || 0; |
| 185 | const rng = world.rng ?? Math.random; |
| 186 | const items = groundItemsAt(world, tileX, tileY); |
| 187 | |
| 188 | const logItem = items.find((item) => woodTierFromLogItemId(item.stack?.itemId)); |
| 189 | if (logItem) |
| 190 | { |
| 191 | const woodTier = woodTierFromLogItemId(logItem.stack.itemId); |
| 192 | const difficulty = firestartingDifficulty(woodTier); |
| 193 | if (mind < difficulty) |
| 194 | { |
| 195 | const label = MATERIAL_DISPLAY_NAME[woodTier] ?? woodTier; |
| 196 | events.push({ |
| 197 | type: "command-error", |
| 198 | sessionId: null, |
| 199 | message: `Your mind is too weak to light ${label} logs.` |
| 200 | }); |
| 201 | return events; |
| 202 | } |
| 203 | if (blockAt(world, tileX, tileY, dim)) |
| 204 | { |
| 205 | events.push({ |
| 206 | type: "command-error", |
| 207 | sessionId: null, |
| 208 | message: "There is no room for a fire there." |
| 209 | }); |
| 210 | return events; |
| 211 | } |
| 212 | const roll = rollSkill(mind, rng); |
| 213 | if (roll < difficulty) |
| 214 | { |
| 215 | events.push({ |
| 216 | type: "command-error", |
| 217 | sessionId: null, |
| 218 | message: "The sparks fail to catch." |
| 219 | }); |
| 220 | return events; |
| 221 | } |
| 222 | consumeOneGroundStack(world, logItem, events); |
| 223 | const appearance = casterHandle != null |
| 224 | ? world.ecs.get(casterHandle, Component.appearance) |
| 225 | : null; |
| 226 | const litByName = [appearance?.firstName, appearance?.lastName] |
| 227 | .filter(Boolean) |
| 228 | .join(" ") |
| 229 | .trim() || "someone"; |
| 230 | events.push(placeCampfire(world, tileX, tileY, woodTier, litByName, now, dim)); |
| 231 | return events; |
| 232 | } |
| 233 | |
| 234 | const meatItem = items.find((item) => cookableRawDef(item.stack?.itemId)); |
| 235 | if (meatItem) |
| 236 | { |
| 237 | const rawDef = cookableRawDef(meatItem.stack.itemId); |
| 238 | const roll = rollSkill(mind, rng); |
| 239 | if (roll < COOK_DIFFICULTY) |
| 240 | { |
| 241 | events.push({ |
| 242 | type: "command-error", |
| 243 | sessionId: null, |
| 244 | message: "The meat fails to cook." |
| 245 | }); |
| 246 | return events; |
| 247 | } |
| 248 | const burned = rng() < TINDERS_COOK_BURN_CHANCE; |
| 249 | const outId = burned |
| 250 | ? (rawDef.burntItemId ?? CraftingItemId.burntMeat) |
| 251 | : cookedItemIdForRaw(rawDef); |
| 252 | consumeOneGroundStack(world, meatItem, events); |
| 253 | const casterHandle = world.ecs.handleOf(impact.casterEntityId); |
| 254 | const appearance = casterHandle != null |
| 255 | ? world.ecs.get(casterHandle, Component.appearance) |
| 256 | : null; |
| 257 | const cookName = [appearance?.firstName, appearance?.lastName] |
| 258 | .filter(Boolean) |
| 259 | .join(" ") |
| 260 | .trim() || "someone"; |
| 261 | const origin = craftOrigin( |
| 262 | burned ? CraftOriginVerb.ruined : CraftOriginVerb.made, |
| 263 | cookName |
| 264 | ); |
| 265 | const stack = attachOrigins({ |
| 266 | itemId: outId, |
| 267 | quantity: 1, |
| 268 | origins: [{ origin, quantity: 1 }] |
| 269 | }); |
| 270 | spawnGroundStack(world, tileX, tileY, stack, now, events); |
| 271 | |
| 272 | const skills = casterHandle != null ? world.ecs.get(casterHandle, Component.skills) : null; |
| 273 | if (skills?.xp && impact.casterCharacterId) |
| 274 | { |
| 275 | const result = applySkillXp(skills, SkillId.cooking, COOK_DIFFICULTY); |
| 276 | if (result.changed) |
| 277 | { |
| 278 | events.push({ |
| 279 | type: "skills-updated", |
| 280 | entityId: impact.casterEntityId, |
| 281 | characterId: impact.casterCharacterId, |
| 282 | ...publicSkillsPayload(skills), |
| 283 | stored: storedSkillXp(skills), |
| 284 | levelUps: result.levelUp ? [result.levelUp] : [] |
| 285 | }); |
| 286 | } |
| 287 | } |
| 288 | return events; |
| 289 | } |
| 290 | |
| 291 | events.push({ |
| 292 | type: "command-error", |
| 293 | sessionId: null, |
| 294 | message: "There is nothing here to light or cook." |
| 295 | }); |
| 296 | return events; |
| 297 | } |