BlueGit

timfa questward

Files

questward / server / src / spells / tinders.js

tinders.js

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