BlueGit

timfa questward

Files

questward / server / src / spells / tinders.js

tinders.js

tinders.js 8.0 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, publicSkillsView, 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 } 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 item.stack.quantity = qty - 1;
102 events.push({
103 type: "ground-item-appear",
104 ...publicGroundItem(item),
105 stack: item.stack,
106 droppedAt: item.droppedAt,
107 source: item.source,
108 expiresAt: item.expiresAt
109 });
110}
111
112function cookableRawDef (itemId)
113{
114 const def = itemDefinition(itemId);
115 if (!def?.cookable || def.cookState !== "raw")
116 { return null; }
117 return def;
118}
119
120function cookedItemIdForRaw (rawDef)
121{
122 if (rawDef.fishSpeciesId)
123 { return cookedFishItemId(rawDef.fishSpeciesId); }
124 if (rawDef.itemId === CraftingItemId.rawMeat)
125 { return CraftingItemId.cookedMeat; }
126 return rawDef.cookedItemId ?? CraftingItemId.cookedMeat;
127}
128
129function spawnGroundStack (world, tileX, tileY, stack, now, events)
130{
131 const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
132 const item = {
133 groundItemId,
134 tileX,
135 tileY,
136 stack,
137 droppedAt: now,
138 source: "tinders",
139 expiresAt: now + 1000 * 60 * 30
140 };
141 addGroundItem(world, item);
142 events.push({
143 type: "ground-item-appear",
144 ...publicGroundItem(item),
145 stack: item.stack,
146 droppedAt: item.droppedAt,
147 source: item.source,
148 expiresAt: item.expiresAt
149 });
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 */
156export function tindersTileStillNeedsCast (world, tileX, tileY)
157{
158 if (blockAt(world, tileX, tileY)?.typeId === BlockType.campfire)
159 { return false; }
160 const items = groundItemsAt(world, tileX, tileY);
161 if (items.some((item) => woodTierFromLogItemId(item.stack?.itemId)))
162 { return true; }
163 if (items.some((item) => cookableRawDef(item.stack?.itemId)))
164 { return true; }
165 return false;
166}
167
168/**
169 * Tinders: Mind roll lights ground logs (as firestarting) or cooks raw meat/fish (40% burn).
170 */
171export function applyTindersAtTile (world, impact, now)
172{
173 const events = [];
174 const tileX = impact.targetTileX | 0;
175 const tileY = impact.targetTileY | 0;
176 const mind = impact.levels?.mind | 0 || 0;
177 const rng = world.rng ?? Math.random;
178 const items = groundItemsAt(world, tileX, tileY);
179
180 const logItem = items.find((item) => woodTierFromLogItemId(item.stack?.itemId));
181 if (logItem)
182 {
183 const woodTier = woodTierFromLogItemId(logItem.stack.itemId);
184 const difficulty = firestartingDifficulty(woodTier);
185 if (mind < difficulty)
186 {
187 const label = MATERIAL_DISPLAY_NAME[woodTier] ?? woodTier;
188 events.push({
189 type: "command-error",
190 sessionId: null,
191 message: `Your mind is too weak to light ${label} logs.`
192 });
193 return events;
194 }
195 if (blockAt(world, tileX, tileY))
196 {
197 events.push({
198 type: "command-error",
199 sessionId: null,
200 message: "There is no room for a fire there."
201 });
202 return events;
203 }
204 const roll = rollSkill(mind, rng);
205 if (roll < difficulty)
206 {
207 events.push({
208 type: "command-error",
209 sessionId: null,
210 message: "The sparks fail to catch."
211 });
212 return events;
213 }
214 consumeOneGroundStack(world, logItem, events);
215 const casterHandle = world.ecs.handleOf(impact.casterEntityId);
216 const appearance = casterHandle != null
217 ? world.ecs.get(casterHandle, Component.appearance)
218 : null;
219 const litByName = [appearance?.firstName, appearance?.lastName]
220 .filter(Boolean)
221 .join(" ")
222 .trim() || "someone";
223 events.push(placeCampfire(world, tileX, tileY, woodTier, litByName, now));
224 return events;
225 }
226
227 const meatItem = items.find((item) => cookableRawDef(item.stack?.itemId));
228 if (meatItem)
229 {
230 const rawDef = cookableRawDef(meatItem.stack.itemId);
231 const roll = rollSkill(mind, rng);
232 if (roll < COOK_DIFFICULTY)
233 {
234 events.push({
235 type: "command-error",
236 sessionId: null,
237 message: "The meat fails to cook."
238 });
239 return events;
240 }
241 const burned = rng() < TINDERS_COOK_BURN_CHANCE;
242 const outId = burned
243 ? (rawDef.burntItemId ?? CraftingItemId.burntMeat)
244 : cookedItemIdForRaw(rawDef);
245 consumeOneGroundStack(world, meatItem, events);
246 const casterHandle = world.ecs.handleOf(impact.casterEntityId);
247 const appearance = casterHandle != null
248 ? world.ecs.get(casterHandle, Component.appearance)
249 : null;
250 const cookName = [appearance?.firstName, appearance?.lastName]
251 .filter(Boolean)
252 .join(" ")
253 .trim() || "someone";
254 const origin = craftOrigin(
255 burned ? CraftOriginVerb.ruined : CraftOriginVerb.made,
256 cookName
257 );
258 const stack = attachOrigins({
259 itemId: outId,
260 quantity: 1,
261 origins: [{ origin, quantity: 1 }]
262 });
263 spawnGroundStack(world, tileX, tileY, stack, now, events);
264
265 const skills = casterHandle != null ? world.ecs.get(casterHandle, Component.skills) : null;
266 if (skills?.xp && impact.casterCharacterId)
267 {
268 const result = applySkillXp(skills, SkillId.cooking, COOK_DIFFICULTY);
269 if (result.changed)
270 {
271 events.push({
272 type: "skills-updated",
273 entityId: impact.casterEntityId,
274 characterId: impact.casterCharacterId,
275 skills: publicSkillsView(skills),
276 stored: storedSkillXp(skills),
277 levelUps: result.levelUp ? [result.levelUp] : []
278 });
279 }
280 }
281 return events;
282 }
283
284 events.push({
285 type: "command-error",
286 sessionId: null,
287 message: "There is nothing here to light or cook."
288 });
289 return events;
290}