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