BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

mobs.js 10.1 KiB Download Raw
1import { spriteImageName } from "./tileSprites.js";
2import { centralExchangeStaffCount } from "../market/exchange.js";
3import { RangedItemId } from "./rangedItems.js";
4
5export const MobId = Object.freeze({
6 deer: "deer",
7 pigeon: "pigeon",
8 rat: "rat",
9 chicken: "chicken",
10 humanoid: "humanoid",
11 /** Editor/spawner identity for Central Exchange staff (always humanoid bodies). */
12 centralExchange: "central-exchange"
13});
14
15/** @typedef {{ itemId: number, chance: number }} MobDrop */
16/** @typedef {{ isPlayer: boolean, text: string }} DialogueLine */
17/** @typedef {{ trigger: string, text: string, available?: (ctx: object) => boolean }} DialogueResponse */
18
19/**
20 * Target mob content schema (rendering fields stay alongside gameplay fields for now).
21 * - displayName: shown name
22 * - health / maxHealth: hit points
23 * - attackSkill: one skill level for hit chance and damage
24 * - drops: { itemId, chance } roll on death
25 * - sells: default item IDs for shop stock (overridable per spawner)
26 * - dialogue: branching trees with triggers, lines, responses, and action hooks (later)
27 * - usePlayerBody: draw male/female character body instead of a mob sprite
28 */
29export const MOB_DEFINITIONS = Object.freeze({
30 [MobId.deer]: Object.freeze({
31 mobTypeId: MobId.deer,
32 key: "deer",
33 assetFolder: "mobs",
34 displayName: "Deer",
35 health: 8,
36 maxHealth: 8,
37 attackSkill: 2,
38 attackCooldownMs: 1000,
39 drops: Object.freeze([]),
40 sells: Object.freeze([]),
41 dialogue: Object.freeze([]),
42 persistent: false,
43 flying: false,
44 freeFlight: false,
45 ambient: false,
46 talkable: false,
47 untargetable: false,
48 aggressive: false,
49 gait: "walk",
50 sex: "male",
51 frames: 1,
52 framesPerRow: 1,
53 animationSpeed: 0
54 }),
55 [MobId.pigeon]: Object.freeze({
56 mobTypeId: MobId.pigeon,
57 key: "pigeon",
58 assetFolder: "pigeons",
59 displayName: "Pigeon",
60 health: 1,
61 maxHealth: 1,
62 canFight: false,
63 drops: Object.freeze([]),
64 sells: Object.freeze([]),
65 dialogue: Object.freeze([]),
66 persistent: false,
67 flying: true,
68 freeFlight: true,
69 ambient: false,
70 talkable: false,
71 untargetable: true,
72 aggressive: false,
73 gait: "walk",
74 sex: "male",
75 frames: 2,
76 framesPerRow: 2,
77 animationSpeed: 0.18,
78 defaultState: "pigeon-flying",
79 flightSpeedTilesPerSec: 2.5
80 }),
81 [MobId.rat]: Object.freeze({
82 mobTypeId: MobId.rat,
83 key: "rat",
84 assetFolder: "mobs",
85 displayName: "Rat",
86 health: 12,
87 maxHealth: 12,
88 attackSkill: 3,
89 attackCooldownMs: 900,
90 drops: Object.freeze([]),
91 sells: Object.freeze([]),
92 dialogue: Object.freeze([]),
93 persistent: false,
94 flying: false,
95 freeFlight: false,
96 ambient: false,
97 talkable: false,
98 untargetable: false,
99 /** Auto-engage nearby players when in range with line of sight. */
100 aggressive: true,
101 aggroRangeTiles: 5,
102 gait: "walk",
103 sex: "male",
104 frames: 1,
105 framesPerRow: 1,
106 animationSpeed: 0
107 }),
108 [MobId.chicken]: Object.freeze({
109 mobTypeId: MobId.chicken,
110 key: "chicken",
111 assetFolder: "mobs",
112 displayName: "Chicken",
113 health: 5,
114 maxHealth: 5,
115 attackSkill: 1,
116 attackCooldownMs: 1200,
117 drops: Object.freeze([
118 { itemId: RangedItemId.feather, chance: 1 }
119 ]),
120 sells: Object.freeze([]),
121 dialogue: Object.freeze([]),
122 persistent: false,
123 flying: false,
124 freeFlight: false,
125 ambient: false,
126 talkable: false,
127 untargetable: false,
128 aggressive: false,
129 gait: "walk",
130 sex: "female",
131 frames: 1,
132 framesPerRow: 1,
133 animationSpeed: 0
134 }),
135 [MobId.humanoid]: Object.freeze({
136 mobTypeId: MobId.humanoid,
137 key: "humanoid",
138 assetFolder: "characters",
139 displayName: "Humanoid",
140 health: 10,
141 maxHealth: 10,
142 canFight: false,
143 drops: Object.freeze([]),
144 sells: Object.freeze([]),
145 dialogue: Object.freeze([]),
146 persistent: false,
147 flying: false,
148 freeFlight: false,
149 ambient: false,
150 talkable: true,
151 untargetable: false,
152 aggressive: false,
153 usePlayerBody: true,
154 gait: "walk",
155 sex: "male",
156 frames: 1,
157 framesPerRow: 1,
158 animationSpeed: 0
159 }),
160 [MobId.centralExchange]: Object.freeze({
161 mobTypeId: MobId.centralExchange,
162 key: "central-exchange",
163 assetFolder: "characters",
164 displayName: "Central Exchange staff",
165 health: 10,
166 maxHealth: 10,
167 canFight: false,
168 drops: Object.freeze([]),
169 sells: Object.freeze([]),
170 dialogue: Object.freeze([]),
171 persistent: false,
172 flying: false,
173 freeFlight: false,
174 ambient: false,
175 talkable: false,
176 untargetable: false,
177 aggressive: false,
178 usePlayerBody: true,
179 centralExchange: true,
180 gait: "walk",
181 sex: "male",
182 frames: 1,
183 framesPerRow: 1,
184 animationSpeed: 0
185 })
186});
187
188export function mobDefinition (mobTypeId)
189{
190 return MOB_DEFINITIONS[mobTypeId] ?? null;
191}
192
193export function mobSpriteName (mobTypeId)
194{
195 const def = mobDefinition(mobTypeId);
196 const key = def?.key ?? def?.mobTypeId ?? "deer";
197 if (def?.usePlayerBody)
198 {
199 // Editor palette preview; live entities use appearance.sex → bodySprite().
200 return spriteImageName("character", "player-body-male", "player-body-male");
201 }
202 if (def?.sprite)
203 { return def.sprite; }
204 const state = def?.defaultState ?? key;
205 return spriteImageName("mob", key, state);
206}
207
208export function defaultSpawnerConfig (spawnerId, mobTypeId = MobId.deer)
209{
210 const humanoid = mobTypeId === MobId.humanoid;
211 const centralExchange = mobTypeId === MobId.centralExchange;
212 return {
213 spawnerId,
214 mobTypeId,
215 maxAlive: centralExchange
216 ? centralExchangeStaffCount()
217 : (humanoid ? 1 : 3),
218 spawnRangeTiles: (humanoid || centralExchange) ? 1 : 4,
219 maxWanderRangeTiles: (humanoid || centralExchange) ? 1 : 8,
220 sex: "male",
221 displayName: humanoid ? "Shopkeeper" : "",
222 equipment: emptySpawnerEquipment(),
223 sells: [],
224 buysItems: false,
225 hiddenDiscount: 0.9,
226 centralExchange
227 };
228}
229
230function emptySpawnerEquipment ()
231{
232 return {
233 head: null,
234 chest: null,
235 legs: null,
236 boots: null,
237 weapon: null,
238 offhand: null
239 };
240}
241
242/**
243 * Spawner equipment entry: legacy itemId number, or `{ itemId, dyeColor? }` for dyed clothing.
244 * Normalized form keeps a plain number when undyed (world-save compatible).
245 */
246function normalizeEquipmentEntry (value)
247{
248 if (value == null || value === "" || value === "0")
249 { return null; }
250 let itemId = 0;
251 let dyeColor = null;
252 if (typeof value === "object")
253 {
254 itemId = Number(value.itemId) | 0;
255 dyeColor = normalizeDyeColor(value.dyeColor);
256 }
257 else
258 { itemId = Number(value) | 0; }
259 if (!Number.isFinite(itemId) || itemId <= 0)
260 { return null; }
261 if (!dyeColor)
262 { return itemId; }
263 return Object.freeze({ itemId, dyeColor });
264}
265
266function normalizeDyeColor (raw)
267{
268 if (!raw || typeof raw !== "object")
269 { return null; }
270 const r = Number(raw.r);
271 const g = Number(raw.g);
272 const b = Number(raw.b);
273 if (![r, g, b].every(Number.isFinite))
274 { return null; }
275 const asByte = r > 1 || g > 1 || b > 1;
276 return Object.freeze({
277 r: Math.min(1, Math.max(0, asByte ? r / 255 : r)),
278 g: Math.min(1, Math.max(0, asByte ? g / 255 : g)),
279 b: Math.min(1, Math.max(0, asByte ? b / 255 : b))
280 });
281}
282
283function normalizeEquipment (raw)
284{
285 const next = emptySpawnerEquipment();
286 if (!raw || typeof raw !== "object")
287 { return next; }
288 for (const slot of Object.keys(next))
289 { next[slot] = normalizeEquipmentEntry(raw[slot]); }
290 return next;
291}
292
293/** Resolve authored spawner equipment into itemId (+ optional dye). */
294export function spawnerEquipmentSlot (entry)
295{
296 if (entry == null)
297 { return { itemId: 0, dyeColor: null }; }
298 if (typeof entry === "object")
299 {
300 return {
301 itemId: Number(entry.itemId) | 0,
302 dyeColor: entry.dyeColor ?? null
303 };
304 }
305 return { itemId: Number(entry) | 0, dyeColor: null };
306}
307
308function normalizeSells (raw)
309{
310 if (!Array.isArray(raw))
311 { return []; }
312 const lines = [];
313 const seen = new Set();
314 for (const entry of raw)
315 {
316 let itemId;
317 let quantity = 0;
318 if (entry && typeof entry === "object")
319 {
320 itemId = Number(entry.itemId);
321 quantity = Math.max(0, Number(entry.quantity) || 0) | 0;
322 }
323 else
324 { itemId = Number(entry); }
325 if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId | 0))
326 { continue; }
327 seen.add(itemId | 0);
328 lines.push({ itemId: itemId | 0, quantity });
329 }
330 return lines;
331}
332
333/** Clamp wander ≥ spawn; coerce numeric fields; keep humanoid shop/appearance options. */
334export function normalizeSpawnerConfig (config, spawnerId = "spawner")
335{
336 const mobTypeId = config?.mobTypeId ?? MobId.deer;
337 const centralExchange = mobTypeId === MobId.centralExchange || !!config?.centralExchange;
338 const effectiveMob = centralExchange ? MobId.centralExchange : mobTypeId;
339 const base = defaultSpawnerConfig(spawnerId, effectiveMob);
340 const merged = { ...base, ...(config ?? {}), spawnerId: config?.spawnerId || spawnerId, mobTypeId: effectiveMob };
341 const spawnRangeTiles = Math.max(1, Math.min(32, Number(merged.spawnRangeTiles) || base.spawnRangeTiles));
342 const maxWanderRangeTiles = Math.max(
343 spawnRangeTiles,
344 Math.min(64, Number(merged.maxWanderRangeTiles) || spawnRangeTiles)
345 );
346 const sex = merged.sex === "female" ? "female" : "male";
347 const hiddenDiscount = Math.min(1, Math.max(0.01, Number(merged.hiddenDiscount) || base.hiddenDiscount));
348 const displayName = String(merged.displayName ?? "").trim().slice(0, 48);
349 const staffCap = centralExchangeStaffCount();
350 return {
351 spawnerId: String(merged.spawnerId),
352 mobTypeId: effectiveMob,
353 maxAlive: centralExchange
354 ? staffCap
355 : Math.max(1, Math.min(20, Number(merged.maxAlive) || base.maxAlive)),
356 spawnRangeTiles,
357 maxWanderRangeTiles,
358 sex,
359 displayName,
360 equipment: normalizeEquipment(merged.equipment),
361 sells: centralExchange ? [] : normalizeSells(merged.sells),
362 buysItems: centralExchange ? false : !!merged.buysItems,
363 hiddenDiscount,
364 centralExchange
365 };
366}
367
368export function listAmbientMobTypes ()
369{
370 return Object.values(MOB_DEFINITIONS).filter((def) => def.ambient);
371}
372
373/** Every mob type that can be authored as a world-editor spawner. */
374export function listSpawnableMobTypes ()
375{
376 return Object.values(MOB_DEFINITIONS);
377}
378
379export function isHumanoidMob (mobTypeId)
380{
381 return mobDefinition(mobTypeId)?.usePlayerBody === true;
382}
383
384export function isCentralExchangeSpawner (configOrMobTypeId)
385{
386 if (configOrMobTypeId && typeof configOrMobTypeId === "object")
387 { return !!configOrMobTypeId.centralExchange || configOrMobTypeId.mobTypeId === MobId.centralExchange; }
388 return configOrMobTypeId === MobId.centralExchange;
389}