BlueGit

timfa questward

Files

questward / server / src / spells / tinders.js

tinders.js

magics
timfa Tim Falken · pushed by timfa · 2 months ago
8b1a158b History
tinders.js 6.8 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 } 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, 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 }
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 });
79 return {
80 type: "chunk-delta",
81 chunkX,
82 chunkY,
83 tileX,
84 tileY,
85 localX,
86 localY,
87 block
88 };
89}
90
91function consumeOneGroundStack (world, item, events)
92{
93 const qty = stackQuantity(item.stack?.quantity);
94 if (qty <= 1)
95 {
96 removeGroundItem(world, item.groundItemId);
97 events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });
98 return;
99 }
100 item.stack.quantity = qty - 1;
101 events.push({
102 type: "ground-item-appear",
103 ...publicGroundItem(item),
104 stack: item.stack,
105 droppedAt: item.droppedAt,
106 source: item.source,
107 expiresAt: item.expiresAt
108 });
109}
110
111function cookableRawDef (itemId)
112{
113 const def = itemDefinition(itemId);
114 if (!def?.cookable || def.cookState !== "raw")
115 { return null; }
116 return def;
117}
118
119function cookedItemIdForRaw (rawDef)
120{
121 if (rawDef.fishSpeciesId)
122 { return cookedFishItemId(rawDef.fishSpeciesId); }
123 if (rawDef.itemId === CraftingItemId.rawMeat)
124 { return CraftingItemId.cookedMeat; }
125 return rawDef.cookedItemId ?? CraftingItemId.cookedMeat;
126}
127
128function spawnGroundStack (world, tileX, tileY, stack, now, events)
129{
130 const groundItemId = (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1);
131 const item = {
132 groundItemId,
133 tileX,
134 tileY,
135 stack,
136 droppedAt: now,
137 source: "tinders",
138 expiresAt: now + 1000 * 60 * 30
139 };
140 addGroundItem(world, item);
141 events.push({
142 type: "ground-item-appear",
143 ...publicGroundItem(item),
144 stack: item.stack,
145 droppedAt: item.droppedAt,
146 source: item.source,
147 expiresAt: item.expiresAt
148 });
149}
150
151/**
152 * Tinders: Mind roll lights ground logs (as firestarting) or cooks raw meat/fish (40% burn).
153 */
154export function applyTindersAtTile (world, impact, now)
155{
156 const events = [];
157 const tileX = impact.targetTileX | 0;
158 const tileY = impact.targetTileY | 0;
159 const mind = impact.levels?.mind | 0 || 0;
160 const rng = world.rng ?? Math.random;
161 const items = groundItemsAt(world, tileX, tileY);
162
163 const logItem = items.find((item) => woodTierFromLogItemId(item.stack?.itemId));
164 if (logItem)
165 {
166 const woodTier = woodTierFromLogItemId(logItem.stack.itemId);
167 const difficulty = firestartingDifficulty(woodTier);
168 if (mind < difficulty)
169 {
170 const label = MATERIAL_DISPLAY_NAME[woodTier] ?? woodTier;
171 events.push({
172 type: "command-error",
173 sessionId: null,
174 message: `Your mind is too weak to light ${label} logs.`
175 });
176 return events;
177 }
178 if (blockAt(world, tileX, tileY))
179 {
180 events.push({
181 type: "command-error",
182 sessionId: null,
183 message: "There is no room for a fire there."
184 });
185 return events;
186 }
187 const roll = rollSkill(mind, rng);
188 if (roll < difficulty)
189 {
190 events.push({
191 type: "command-error",
192 sessionId: null,
193 message: "The sparks fail to catch."
194 });
195 return events;
196 }
197 consumeOneGroundStack(world, logItem, events);
198 events.push(placeCampfire(world, tileX, tileY, woodTier, now));
199 return events;
200 }
201
202 const meatItem = items.find((item) => cookableRawDef(item.stack?.itemId));
203 if (meatItem)
204 {
205 const rawDef = cookableRawDef(meatItem.stack.itemId);
206 const roll = rollSkill(mind, rng);
207 if (roll < COOK_DIFFICULTY)
208 {
209 events.push({
210 type: "command-error",
211 sessionId: null,
212 message: "The meat fails to cook."
213 });
214 return events;
215 }
216 const burned = rng() < TINDERS_COOK_BURN_CHANCE;
217 const outId = burned
218 ? (rawDef.burntItemId ?? CraftingItemId.burntMeat)
219 : cookedItemIdForRaw(rawDef);
220 consumeOneGroundStack(world, meatItem, events);
221 const stack = attachOrigins({ itemId: outId, quantity: 1, origins: [] });
222 spawnGroundStack(world, tileX, tileY, stack, now, events);
223
224 const casterHandle = world.ecs.handleOf(impact.casterEntityId);
225 const skills = casterHandle != null ? world.ecs.get(casterHandle, Component.skills) : null;
226 if (skills?.xp && impact.casterCharacterId)
227 {
228 const result = applySkillXp(skills, SkillId.cooking, COOK_DIFFICULTY);
229 if (result.changed)
230 {
231 events.push({
232 type: "skills-updated",
233 entityId: impact.casterEntityId,
234 characterId: impact.casterCharacterId,
235 skills: publicSkillsView(skills),
236 stored: storedSkillXp(skills),
237 levelUps: result.levelUp ? [result.levelUp] : []
238 });
239 }
240 }
241 return events;
242 }
243
244 events.push({
245 type: "command-error",
246 sessionId: null,
247 message: "There is nothing here to light or cook."
248 });
249 return events;
250}