BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

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