BlueGit

timfa questward

Files

questward / server / src / spells / tinders.js

tinders.js

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