BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

mobs.js 21.8 KiB Download Raw
1import { spriteImageName } from "./tileSprites.js";
2import { centralExchangeStaffCount } from "../market/exchange.js";
3import { LeatherItemId } from "./leatherItems.js";
4import { CraftingItemId } from "./craftingItems.js";
5import { RangedItemId } from "./rangedItems.js";
6import { ArmourItemId } from "./armourItems.js";
7import { WoodItemId } from "./woodItems.js";
8import { ItemId } from "./items.js";
9import { EQUIPMENT_SLOTS } from "../items/inventory.js";
10import { spellDefinition } from "./spells.js";
11import { ClothTier } from "./materialTiers.js";
12import { ClothItemId } from "./clothItems.js";
13import { EssenceItemId } from "./essenceItems.js";
14import { AttackType, MagicElement } from "./attackTypes.js";
15import { SkillId } from "./skills.js";
16
17export const MobId = Object.freeze({
18 deer: "deer",
19 pigeon: "pigeon",
20 rat: "rat",
21 goblin: "goblin",
22 chicken: "chicken",
23 sheep: "sheep",
24 cashmereGoat: "cashmere-goat",
25 humanoid: "humanoid",
26 /** Humanoid guards that respond to bounty kills within wander range. */
27 townGuard: "town-guard",
28 /** Editor/spawner identity for Central Exchange staff (always humanoid bodies). */
29 centralExchange: "central-exchange",
30 /** Temporary Shade summon (spell). */
31 shade: "shade",
32 /** Elemental sprite — random element on spawn, drops essence, emits light. */
33 sprite: "sprite",
34 /** Fairy — drops fairy silk fibre on death. */
35 fairy: "fairy"
36});
37
38/** @typedef {{ itemId: number, chance: number, quantity?: number, quantityMin?: number, quantityMax?: number }} MobDrop */
39/** @typedef {{ isPlayer: boolean, text: string }} DialogueLine */
40/** @typedef {{ trigger: string, text: string, available?: (ctx: object) => boolean }} DialogueResponse */
41
42/**
43 * Target mob content schema (rendering fields stay alongside gameplay fields for now).
44 * - displayName: shown name
45 * - health / maxHealth: hit points
46 * - attackSkill: one skill level for hit chance and damage
47 * - drops: { itemId, chance } roll on death
48 * - sells: default item IDs for shop stock (overridable per spawner)
49 * - dialogue: branching trees with triggers, lines, responses, and action hooks (later)
50 * - usePlayerBody: draw male/female character body instead of a mob sprite
51 * - canOpenDoors: optional override; default = humanoid/usePlayerBody can open,
52 * other mobs only pass through already-open (held) doors
53 */
54export const MOB_DEFINITIONS = Object.freeze({
55 [MobId.deer]: Object.freeze({
56 mobTypeId: MobId.deer,
57 key: "deer",
58 assetFolder: "mobs",
59 displayName: "Deer",
60 health: 8,
61 maxHealth: 8,
62 attackSkill: 2,
63 attackCooldownMs: 1000,
64 drops: Object.freeze([
65 { itemId: LeatherItemId.softLeather, chance: 1 },
66 { itemId: CraftingItemId.rawMeat, chance: 1 }
67 ]),
68 sells: Object.freeze([]),
69 dialogue: Object.freeze([]),
70 persistent: false,
71 flying: false,
72 freeFlight: false,
73 ambient: false,
74 talkable: false,
75 untargetable: false,
76 aggressive: false,
77 gait: "walk",
78 sex: "male",
79 frames: 1,
80 framesPerRow: 1,
81 animationSpeed: 0
82 }),
83 [MobId.pigeon]: Object.freeze({
84 mobTypeId: MobId.pigeon,
85 key: "pigeon",
86 assetFolder: "pigeons",
87 displayName: "Pigeon",
88 health: 1,
89 maxHealth: 1,
90 canFight: false,
91 drops: Object.freeze([]),
92 sells: Object.freeze([]),
93 dialogue: Object.freeze([]),
94 persistent: false,
95 flying: true,
96 freeFlight: true,
97 ambient: false,
98 talkable: false,
99 untargetable: true,
100 aggressive: false,
101 gait: "walk",
102 sex: "male",
103 frames: 2,
104 framesPerRow: 2,
105 animationSpeed: 0.18,
106 defaultState: "pigeon-flying",
107 flightSpeedTilesPerSec: 2.5
108 }),
109 [MobId.rat]: Object.freeze({
110 mobTypeId: MobId.rat,
111 key: "rat",
112 assetFolder: "mobs",
113 displayName: "Rat",
114 health: 12,
115 maxHealth: 12,
116 attackSkill: 1,
117 attackCooldownMs: 900,
118 drops: Object.freeze([]),
119 sells: Object.freeze([]),
120 dialogue: Object.freeze([]),
121 persistent: false,
122 flying: false,
123 freeFlight: false,
124 ambient: false,
125 talkable: false,
126 untargetable: false,
127 /** Auto-engage nearby players when in range with line of sight. */
128 aggressive: true,
129 aggroRangeTiles: 5,
130 elementAffinities: Object.freeze({ fire: "weak" }),
131 gait: "walk",
132 sex: "male",
133 frames: 1,
134 framesPerRow: 1,
135 animationSpeed: 0
136 }),
137 [MobId.goblin]: Object.freeze({
138 mobTypeId: MobId.goblin,
139 key: "goblin",
140 assetFolder: "mobs",
141 displayName: "Goblin",
142 health: 16,
143 maxHealth: 16,
144 attackSkill: 5,
145 attackCooldownMs: 850,
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: false,
154 untargetable: false,
155 aggressive: true,
156 aggroRangeTiles: 5,
157 meleeAttack: Object.freeze({
158 mode: "melee",
159 rangeTiles: 1,
160 hitSkillId: SkillId.swordfighting,
161 damageSkillId: SkillId.strength,
162 attackType: AttackType.slashing
163 }),
164 gait: "walk",
165 sex: "male",
166 frames: 1,
167 framesPerRow: 1,
168 animationSpeed: 0
169 }),
170 [MobId.chicken]: Object.freeze({
171 mobTypeId: MobId.chicken,
172 key: "chicken",
173 assetFolder: "mobs",
174 displayName: "Chicken",
175 health: 5,
176 maxHealth: 5,
177 attackSkill: 1,
178 attackCooldownMs: 1200,
179 drops: Object.freeze([
180 { itemId: RangedItemId.feather, chance: 1, quantityMin: 1, quantityMax: 3 },
181 { itemId: ItemId.egg, chance: 0.2 }
182 ]),
183 sells: Object.freeze([]),
184 dialogue: Object.freeze([]),
185 persistent: false,
186 flying: false,
187 freeFlight: false,
188 ambient: false,
189 talkable: false,
190 untargetable: false,
191 aggressive: false,
192 gait: "walk",
193 sex: "female",
194 frames: 1,
195 framesPerRow: 1,
196 animationSpeed: 0
197 }),
198 [MobId.sheep]: Object.freeze({
199 mobTypeId: MobId.sheep,
200 key: "sheep",
201 assetFolder: "mobs",
202 displayName: "Sheep",
203 health: 8,
204 maxHealth: 8,
205 attackSkill: 1,
206 attackCooldownMs: 1200,
207 drops: Object.freeze([
208 { itemId: LeatherItemId.softLeather, chance: 0.5 },
209 { itemId: CraftingItemId.rawMutton, chance: 1 }
210 ]),
211 sells: Object.freeze([]),
212 dialogue: Object.freeze([]),
213 persistent: false,
214 flying: false,
215 freeFlight: false,
216 ambient: false,
217 talkable: false,
218 untargetable: false,
219 aggressive: false,
220 gait: "walk",
221 sex: "female",
222 frames: 1,
223 framesPerRow: 1,
224 animationSpeed: 0,
225 defaultState: "fleece",
226 shearable: Object.freeze({
227 clothTier: ClothTier.wool,
228 fleeceState: "fleece",
229 shornState: "shorn"
230 })
231 }),
232 [MobId.cashmereGoat]: Object.freeze({
233 mobTypeId: MobId.cashmereGoat,
234 key: "cashmere-goat",
235 assetFolder: "mobs",
236 displayName: "Goat",
237 health: 10,
238 maxHealth: 10,
239 attackSkill: 2,
240 attackCooldownMs: 1100,
241 drops: Object.freeze([
242 { itemId: LeatherItemId.softLeather, chance: 0.6 },
243 { itemId: CraftingItemId.rawGoat, chance: 1 }
244 ]),
245 sells: Object.freeze([]),
246 dialogue: Object.freeze([]),
247 persistent: false,
248 flying: false,
249 freeFlight: false,
250 ambient: false,
251 talkable: false,
252 untargetable: false,
253 aggressive: false,
254 gait: "walk",
255 sex: "female",
256 frames: 1,
257 framesPerRow: 1,
258 animationSpeed: 0,
259 defaultState: "fleece",
260 shearable: Object.freeze({
261 clothTier: ClothTier.cashmere,
262 fleeceState: "fleece",
263 shornState: "shorn"
264 })
265 }),
266 [MobId.shade]: Object.freeze({
267 mobTypeId: MobId.shade,
268 key: "shade",
269 assetFolder: "mobs",
270 displayName: "Shade",
271 health: 10,
272 maxHealth: 10,
273 maxHealthFromLevel: "levelPlus10",
274 attackSkill: 5,
275 attackCooldownMs: 900,
276 drops: Object.freeze([
277 { itemId: EssenceItemId[MagicElement.dark], chance: 1 }
278 ]),
279 sells: Object.freeze([]),
280 dialogue: Object.freeze([]),
281 persistent: false,
282 flying: false,
283 freeFlight: false,
284 ambient: false,
285 talkable: false,
286 untargetable: false,
287 /** Spawner shades are hostile; summoned shades opt out via Component.summon. */
288 aggressive: true,
289 aggroRangeTiles: 5,
290 bloomBody: true,
291 gait: "walk",
292 sex: "male",
293 /** Body sheet is static; bloom flickers on its own. */
294 frames: 1,
295 framesPerRow: 1,
296 animationSpeed: 0,
297 bloomFrames: 2,
298 bloomFramesPerRow: 2,
299 /** Match elemental sprite bloom cadence. */
300 bloomAnimationSpeed: 0.5,
301 defaultState: "shade"
302 }),
303 [MobId.sprite]: Object.freeze({
304 mobTypeId: MobId.sprite,
305 key: "sprite",
306 assetFolder: "mobs",
307 displayName: "Sprite",
308 health: 35,
309 maxHealth: 35,
310 maxHealthFromLevel: "levelPlus10",
311 attackSkill: 25,
312 attackCooldownMs: 1100,
313 drops: Object.freeze([]),
314 /** Essence drop is resolved from the creature's chosen element on death. */
315 essenceDropChance: 1,
316 sells: Object.freeze([]),
317 dialogue: Object.freeze([]),
318 persistent: false,
319 flying: false,
320 freeFlight: false,
321 ambient: false,
322 talkable: false,
323 untargetable: false,
324 aggressive: true,
325 aggroRangeTiles: 6,
326 preferSpellAttack: true,
327 lightRadiusTiles: 2,
328 randomElementOnSpawn: true,
329 bloomBody: true,
330 gait: "walk",
331 sex: "male",
332 frames: 2,
333 framesPerRow: 2,
334 animationSpeed: 0.5
335 }),
336 [MobId.fairy]: Object.freeze({
337 mobTypeId: MobId.fairy,
338 key: "fairy",
339 assetFolder: "mobs",
340 displayName: "Fairy",
341 health: 40,
342 maxHealth: 40,
343 maxHealthFromLevel: "levelPlus10",
344 attackSkill: 55,
345 attackCooldownMs: 1000,
346 drops: Object.freeze([
347 { itemId: ClothItemId.fairySilk, chance: 0.7 }
348 ]),
349 sells: Object.freeze([]),
350 dialogue: Object.freeze([]),
351 persistent: false,
352 flying: true,
353 freeFlight: false,
354 ambient: false,
355 talkable: false,
356 untargetable: false,
357 aggressive: true,
358 aggroRangeTiles: 5,
359 /** Prefer sprite-style elemental bolts over melee. */
360 preferSpellAttack: true,
361 /** Cast a random-element sprite bolt each attack (no fixed spawn element). */
362 randomElementSpell: true,
363 /** On hit, flee until at least this many tiles from the hit source (then stop until hit again). */
364 fleeOnHitTiles: 2,
365 /** Dimmer than elemental sprites (radius 2). */
366 lightRadiusTiles: 1,
367 bloomBody: true,
368 /** Shirt/skirt/hair use dual-channel body tints rolled on spawn. */
369 dualChannelAppearance: true,
370 gait: "walk",
371 sex: "female",
372 frames: 2,
373 framesPerRow: 2,
374 animationSpeed: 0.45,
375 defaultState: "fairy"
376 }),
377 [MobId.humanoid]: Object.freeze({
378 mobTypeId: MobId.humanoid,
379 key: "humanoid",
380 assetFolder: "characters",
381 displayName: "Humanoid",
382 health: 10,
383 maxHealth: 10,
384 /** HP = spawner level + 10 (same floor as a starting player's Health). */
385 maxHealthFromLevel: "levelPlus10",
386 drops: Object.freeze([]),
387 sells: Object.freeze([]),
388 dialogue: Object.freeze([]),
389 persistent: false,
390 flying: false,
391 freeFlight: false,
392 ambient: false,
393 talkable: true,
394 untargetable: false,
395 aggressive: false,
396 usePlayerBody: true,
397 gait: "walk",
398 sex: "male",
399 frames: 1,
400 framesPerRow: 1,
401 animationSpeed: 0
402 }),
403 [MobId.townGuard]: Object.freeze({
404 mobTypeId: MobId.townGuard,
405 key: "town-guard",
406 assetFolder: "characters",
407 displayName: "Town Guard",
408 health: 10,
409 maxHealth: 10,
410 maxHealthFromLevel: "levelPlus10",
411 drops: Object.freeze([]),
412 sells: Object.freeze([]),
413 dialogue: Object.freeze([]),
414 persistent: false,
415 flying: false,
416 freeFlight: false,
417 ambient: false,
418 talkable: false,
419 untargetable: false,
420 aggressive: false,
421 usePlayerBody: true,
422 townGuard: true,
423 gait: "walk",
424 sex: "male",
425 frames: 1,
426 framesPerRow: 1,
427 animationSpeed: 0
428 }),
429 [MobId.centralExchange]: Object.freeze({
430 mobTypeId: MobId.centralExchange,
431 key: "central-exchange",
432 assetFolder: "characters",
433 displayName: "Central Exchange staff",
434 health: 10,
435 maxHealth: 10,
436 maxHealthFromLevel: "levelPlus10",
437 drops: Object.freeze([]),
438 sells: Object.freeze([]),
439 dialogue: Object.freeze([]),
440 persistent: false,
441 flying: false,
442 freeFlight: false,
443 ambient: false,
444 talkable: false,
445 untargetable: false,
446 aggressive: false,
447 usePlayerBody: true,
448 centralExchange: true,
449 gait: "walk",
450 sex: "male",
451 frames: 1,
452 framesPerRow: 1,
453 animationSpeed: 0
454 })
455});
456
457export function mobDefinition (mobTypeId)
458{
459 return MOB_DEFINITIONS[mobTypeId] ?? null;
460}
461
462export function mobSpriteName (mobTypeId, visualState = null)
463{
464 const def = mobDefinition(mobTypeId);
465 const key = def?.spriteKey ?? def?.key ?? def?.mobTypeId ?? "deer";
466 if (def?.usePlayerBody)
467 {
468 // Editor palette preview; live entities use appearance.sex → bodySprite().
469 return spriteImageName("character", "player-body-male", "player-body-male");
470 }
471 if (def?.sprite)
472 { return def.sprite; }
473 const state = visualState ?? def?.defaultState ?? key;
474 return spriteImageName("mob", key, state);
475}
476
477/** Whether this mob definition can be sheared for cloth fibre. */
478export function mobIsShearable (mobTypeId)
479{
480 return !!mobDefinition(mobTypeId)?.shearable;
481}
482
483export function defaultSpawnerConfig (spawnerId, mobTypeId = MobId.deer)
484{
485 const humanoid = isHumanoidMob(mobTypeId) || mobTypeId === MobId.humanoid;
486 const townGuard = mobTypeId === MobId.townGuard;
487 const centralExchange = mobTypeId === MobId.centralExchange;
488 const bodyNpc = humanoid || townGuard || centralExchange;
489 const level = townGuard
490 ? 30
491 : (mobTypeId === MobId.goblin
492 ? 5
493 : (mobTypeId === MobId.sprite
494 ? 25
495 : (mobTypeId === MobId.shade
496 ? 28
497 : (mobTypeId === MobId.fairy ? 70 : 1))));
498 const bounty = townGuard ? 500 : 0;
499 return {
500 spawnerId,
501 mobTypeId,
502 maxAlive: centralExchange
503 ? centralExchangeStaffCount()
504 : (bodyNpc ? 1 : 3),
505 /** Seconds after death before this spawner may refill that slot (0 = next spawner tick). */
506 respawnDelaySeconds: 25,
507 spawnRangeTiles: bodyNpc ? 1 : 4,
508 maxWanderRangeTiles: bodyNpc ? 1 : 8,
509 level,
510 bounty,
511 /** Humanoid spawners only: auto-engage players in aggro range. Forces bounty 0. */
512 aggressive: false,
513 sex: "male",
514 displayName: townGuard ? "Town Guard" : (humanoid ? "Shopkeeper" : ""),
515 equipment: townGuard
516 ? {
517 head: ArmourItemId.ironHelmet,
518 chest: ArmourItemId.ironChestplate,
519 legs: ArmourItemId.bronzeGreaves,
520 boots: ArmourItemId.ironBoots,
521 weapon: WoodItemId.oakQuarterstaff,
522 offhand: null
523 }
524 : emptySpawnerEquipment(),
525 sells: [],
526 drops: [],
527 spells: [],
528 buysItems: false,
529 hiddenDiscount: 0.9,
530 centralExchange,
531 townGuard,
532 dialogueId: null
533 };
534}
535
536function emptySpawnerEquipment ()
537{
538 const equipment = {};
539 for (const slot of EQUIPMENT_SLOTS)
540 { equipment[slot] = null; }
541 return equipment;
542}
543
544/**
545 * Spawner equipment entry: legacy itemId number, or `{ itemId, dyeColor? }` for dyed clothing.
546 * Normalized form keeps a plain number when undyed (world-save compatible).
547 */
548function normalizeEquipmentEntry (value)
549{
550 if (value == null || value === "" || value === "0")
551 { return null; }
552 let itemId = 0;
553 let dyeColor = null;
554 if (typeof value === "object")
555 {
556 itemId = Number(value.itemId) | 0;
557 dyeColor = normalizeDyeColor(value.dyeColor);
558 }
559 else
560 { itemId = Number(value) | 0; }
561 if (!Number.isFinite(itemId) || itemId <= 0)
562 { return null; }
563 if (!dyeColor)
564 { return itemId; }
565 return Object.freeze({ itemId, dyeColor });
566}
567
568function normalizeDyeColor (raw)
569{
570 if (!raw || typeof raw !== "object")
571 { return null; }
572 const r = Number(raw.r);
573 const g = Number(raw.g);
574 const b = Number(raw.b);
575 if (![r, g, b].every(Number.isFinite))
576 { return null; }
577 const asByte = r > 1 || g > 1 || b > 1;
578 return Object.freeze({
579 r: Math.min(1, Math.max(0, asByte ? r / 255 : r)),
580 g: Math.min(1, Math.max(0, asByte ? g / 255 : g)),
581 b: Math.min(1, Math.max(0, asByte ? b / 255 : b))
582 });
583}
584
585function normalizeEquipment (raw)
586{
587 const next = emptySpawnerEquipment();
588 if (!raw || typeof raw !== "object")
589 { return next; }
590 for (const slot of Object.keys(next))
591 { next[slot] = normalizeEquipmentEntry(raw[slot]); }
592 return next;
593}
594
595/** Resolve authored spawner equipment into itemId (+ optional dye). */
596export function spawnerEquipmentSlot (entry)
597{
598 if (entry == null)
599 { return { itemId: 0, dyeColor: null }; }
600 if (typeof entry === "object")
601 {
602 return {
603 itemId: Number(entry.itemId) | 0,
604 dyeColor: entry.dyeColor ?? null
605 };
606 }
607 return { itemId: Number(entry) | 0, dyeColor: null };
608}
609
610function normalizeSells (raw)
611{
612 if (!Array.isArray(raw))
613 { return []; }
614 const lines = [];
615 const seen = new Set();
616 for (const entry of raw)
617 {
618 let itemId;
619 let quantity = 0;
620 if (entry && typeof entry === "object")
621 {
622 itemId = Number(entry.itemId);
623 quantity = Math.max(0, Number(entry.quantity) || 0) | 0;
624 }
625 else
626 { itemId = Number(entry); }
627 if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId | 0))
628 { continue; }
629 seen.add(itemId | 0);
630 lines.push({ itemId: itemId | 0, quantity });
631 }
632 return lines;
633}
634
635/**
636 * Spawner loot lines. Chance is 0–1 (UI edits as 0–100%).
637 * Quantity is rolled inclusively between quantityMin and quantityMax when the drop hits.
638 * @returns {ReadonlyArray<{ itemId: number, chance: number, quantityMin: number, quantityMax: number }>}
639 */
640function normalizeDrops (raw)
641{
642 if (!Array.isArray(raw))
643 { return []; }
644 const lines = [];
645 const seen = new Set();
646 for (const entry of raw)
647 {
648 if (!entry || typeof entry !== "object")
649 { continue; }
650 const itemId = Number(entry.itemId) | 0;
651 if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId))
652 { continue; }
653 seen.add(itemId);
654 let chance = Number(entry.chance);
655 if (!Number.isFinite(chance))
656 { chance = 1; }
657 // Accept editor percent (0–100) or fraction (0–1).
658 if (chance > 1)
659 { chance = chance / 100; }
660 chance = Math.min(1, Math.max(0, chance));
661 let quantityMin = Math.max(0, Math.floor(Number(
662 entry.quantityMin ?? entry.quantity ?? 1
663 )));
664 let quantityMax = Math.max(0, Math.floor(Number(
665 entry.quantityMax ?? entry.quantityMin ?? entry.quantity ?? quantityMin
666 )));
667 if (!Number.isFinite(quantityMin))
668 { quantityMin = 1; }
669 if (!Number.isFinite(quantityMax))
670 { quantityMax = quantityMin; }
671 if (quantityMax < quantityMin)
672 { quantityMax = quantityMin; }
673 lines.push(Object.freeze({ itemId, chance, quantityMin, quantityMax }));
674 }
675 return lines;
676}
677
678function normalizeSpells (raw)
679{
680 if (!Array.isArray(raw))
681 { return []; }
682 const lines = [];
683 const seen = new Set();
684 for (const entry of raw)
685 {
686 if (!entry || typeof entry !== "object")
687 { continue; }
688 const spellId = String(entry.spellId ?? "").trim();
689 // Drop unknown / retired ids (e.g. legacy spark) so spawners stay loadable.
690 if (!spellId || seen.has(spellId) || !spellDefinition(spellId))
691 { continue; }
692 seen.add(spellId);
693 // Cast chance is derived from mob level at spawn time — not stored.
694 lines.push({ spellId });
695 }
696 return lines;
697}
698
699/** Clamp wander ≥ spawn; coerce numeric fields; keep humanoid shop/appearance options. */
700export function normalizeSpawnerConfig (config, spawnerId = "spawner")
701{
702 const mobTypeId = config?.mobTypeId ?? MobId.deer;
703 const centralExchange = mobTypeId === MobId.centralExchange || !!config?.centralExchange;
704 const townGuard = !centralExchange && (mobTypeId === MobId.townGuard || !!config?.townGuard);
705 const effectiveMob = centralExchange
706 ? MobId.centralExchange
707 : (townGuard ? MobId.townGuard : mobTypeId);
708 const base = defaultSpawnerConfig(spawnerId, effectiveMob);
709 const merged = { ...base, ...(config ?? {}), spawnerId: config?.spawnerId || spawnerId, mobTypeId: effectiveMob };
710 const spawnRangeTiles = Math.max(1, Math.min(32, Number(merged.spawnRangeTiles) || base.spawnRangeTiles));
711 const maxWanderRangeTiles = Math.max(
712 spawnRangeTiles,
713 Math.min(64, Number(merged.maxWanderRangeTiles) || spawnRangeTiles)
714 );
715 const sex = merged.sex === "female" ? "female" : "male";
716 const hiddenDiscount = Math.min(1, Math.max(0.01, Number(merged.hiddenDiscount) || base.hiddenDiscount));
717 const displayName = String(merged.displayName ?? "").trim().slice(0, 48);
718 const staffCap = centralExchangeStaffCount();
719 const level = Math.max(1, Math.min(99, Number(merged.level) || base.level || 1)) | 0;
720 const bodyNpc = isHumanoidMob(effectiveMob) || centralExchange || townGuard;
721 const aggressive = bodyNpc ? !!merged.aggressive : false;
722 const bounty = aggressive ? 0 : (Math.max(0, Number(merged.bounty) || 0) | 0);
723 return {
724 spawnerId: String(merged.spawnerId),
725 mobTypeId: effectiveMob,
726 maxAlive: centralExchange
727 ? staffCap
728 : Math.max(1, Math.min(20, Number(merged.maxAlive) || base.maxAlive)),
729 respawnDelaySeconds: Math.max(
730 0,
731 Math.min(
732 86_400,
733 Math.floor(
734 Number(
735 merged.respawnDelaySeconds != null && merged.respawnDelaySeconds !== ""
736 ? merged.respawnDelaySeconds
737 : base.respawnDelaySeconds
738 ) || 0
739 )
740 )
741 ),
742 spawnRangeTiles,
743 maxWanderRangeTiles,
744 level,
745 bounty,
746 aggressive,
747 sex,
748 displayName,
749 equipment: normalizeEquipment(merged.equipment),
750 sells: centralExchange ? [] : normalizeSells(merged.sells),
751 drops: bodyNpc ? normalizeDrops(merged.drops) : [],
752 spells: normalizeSpells(merged.spells),
753 buysItems: centralExchange ? false : !!merged.buysItems,
754 hiddenDiscount,
755 centralExchange,
756 townGuard,
757 dialogueId: bodyNpc
758 ? (String(merged.dialogueId ?? "").trim().slice(0, 64) || null)
759 : null
760 };
761}
762
763export function listAmbientMobTypes ()
764{
765 return Object.values(MOB_DEFINITIONS).filter((def) => def.ambient);
766}
767
768/** Every mob type that can be authored as a world-editor spawner. */
769export function listSpawnableMobTypes ()
770{
771 return Object.values(MOB_DEFINITIONS);
772}
773
774export function isHumanoidMob (mobTypeId)
775{
776 return mobDefinition(mobTypeId)?.usePlayerBody === true;
777}
778
779export function isTownGuardMob (mobTypeId)
780{
781 return mobTypeId === MobId.townGuard || !!mobDefinition(mobTypeId)?.townGuard;
782}
783
784export function isCentralExchangeSpawner (configOrMobTypeId)
785{
786 if (configOrMobTypeId && typeof configOrMobTypeId === "object")
787 { return !!configOrMobTypeId.centralExchange || configOrMobTypeId.mobTypeId === MobId.centralExchange; }
788 return configOrMobTypeId === MobId.centralExchange;
789}
790
791/** Max HP for a mob at the given spawner level. */
792export function mobMaxHealthForLevel (def, level)
793{
794 const lv = Math.max(1, Math.floor(Number(level) || 1));
795 const formula = def?.maxHealthFromLevel;
796 if (formula === "levelPlus10" || (!formula && def?.usePlayerBody))
797 { return lv + 10; }
798 const base = Math.max(1, Number(def?.health ?? def?.maxHealth ?? 8) || 8);
799 return Math.max(1, base + (lv - 1));
800}
801
802/** Display label for hover / UI: `Lv.5 Deer`. */
803export function mobLevelDisplayName (level, displayName)
804{
805 const lv = Math.max(1, Math.floor(Number(level) || 1));
806 const name = String(displayName ?? "").trim() || "Creature";
807 return `Lv.${lv} ${name}`;
808}