BlueGit

timfa questward

Files

questward / server / src / spells / tinders.js

tinders.js

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