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