BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

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