BlueGit

timfa questward

Files

questward / server / src / spells / tinders.js

tinders.js

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