BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

mobs.js 22.3 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 /** When set (and Aggressive is off): mob takes hits but does not retaliate. */
514 passive: false,
515 sex: "male",
516 displayName: townGuard ? "Town Guard" : (humanoid ? "Shopkeeper" : ""),
517 equipment: townGuard
518 ? {
519 head: ArmourItemId.ironHelmet,
520 chest: ArmourItemId.ironChestplate,
521 legs: ArmourItemId.bronzeGreaves,
522 boots: ArmourItemId.ironBoots,
523 weapon: WoodItemId.oakQuarterstaff,
524 offhand: null
525 }
526 : emptySpawnerEquipment(),
527 sells: [],
528 drops: [],
529 spells: [],
530 buysItems: false,
531 hiddenDiscount: 0.9,
532 centralExchange,
533 townGuard,
534 dialogueId: null
535 };
536}
537
538function emptySpawnerEquipment ()
539{
540 const equipment = {};
541 for (const slot of EQUIPMENT_SLOTS)
542 { equipment[slot] = null; }
543 return equipment;
544}
545
546/**
547 * Spawner equipment entry: legacy itemId number, or `{ itemId, dyeColor? }` for dyed clothing.
548 * Normalized form keeps a plain number when undyed (world-save compatible).
549 */
550function normalizeEquipmentEntry (value)
551{
552 if (value == null || value === "" || value === "0")
553 { return null; }
554 let itemId = 0;
555 let dyeColor = null;
556 if (typeof value === "object")
557 {
558 itemId = Number(value.itemId) | 0;
559 dyeColor = normalizeDyeColor(value.dyeColor);
560 }
561 else
562 { itemId = Number(value) | 0; }
563 if (!Number.isFinite(itemId) || itemId <= 0)
564 { return null; }
565 if (!dyeColor)
566 { return itemId; }
567 return Object.freeze({ itemId, dyeColor });
568}
569
570function normalizeDyeColor (raw)
571{
572 if (!raw || typeof raw !== "object")
573 { return null; }
574 const r = Number(raw.r);
575 const g = Number(raw.g);
576 const b = Number(raw.b);
577 if (![r, g, b].every(Number.isFinite))
578 { return null; }
579 const asByte = r > 1 || g > 1 || b > 1;
580 return Object.freeze({
581 r: Math.min(1, Math.max(0, asByte ? r / 255 : r)),
582 g: Math.min(1, Math.max(0, asByte ? g / 255 : g)),
583 b: Math.min(1, Math.max(0, asByte ? b / 255 : b))
584 });
585}
586
587function normalizeEquipment (raw)
588{
589 const next = emptySpawnerEquipment();
590 if (!raw || typeof raw !== "object")
591 { return next; }
592 for (const slot of Object.keys(next))
593 { next[slot] = normalizeEquipmentEntry(raw[slot]); }
594 return next;
595}
596
597/** Resolve authored spawner equipment into itemId (+ optional dye). */
598export function spawnerEquipmentSlot (entry)
599{
600 if (entry == null)
601 { return { itemId: 0, dyeColor: null }; }
602 if (typeof entry === "object")
603 {
604 return {
605 itemId: Number(entry.itemId) | 0,
606 dyeColor: entry.dyeColor ?? null
607 };
608 }
609 return { itemId: Number(entry) | 0, dyeColor: null };
610}
611
612function normalizeSells (raw)
613{
614 if (!Array.isArray(raw))
615 { return []; }
616 const lines = [];
617 const seen = new Set();
618 for (const entry of raw)
619 {
620 let itemId;
621 let quantity = 0;
622 if (entry && typeof entry === "object")
623 {
624 itemId = Number(entry.itemId);
625 quantity = Math.max(0, Number(entry.quantity) || 0) | 0;
626 }
627 else
628 { itemId = Number(entry); }
629 if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId | 0))
630 { continue; }
631 seen.add(itemId | 0);
632 lines.push({ itemId: itemId | 0, quantity });
633 }
634 return lines;
635}
636
637/**
638 * Spawner loot lines. Chance is 0–1 (UI edits as 0–100%).
639 * Quantity is rolled inclusively between quantityMin and quantityMax when the drop hits.
640 * @returns {ReadonlyArray<{ itemId: number, chance: number, quantityMin: number, quantityMax: number }>}
641 */
642function normalizeDrops (raw)
643{
644 if (!Array.isArray(raw))
645 { return []; }
646 const lines = [];
647 const seen = new Set();
648 for (const entry of raw)
649 {
650 if (!entry || typeof entry !== "object")
651 { continue; }
652 const itemId = Number(entry.itemId) | 0;
653 if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId))
654 { continue; }
655 seen.add(itemId);
656 let chance = Number(entry.chance);
657 if (!Number.isFinite(chance))
658 { chance = 1; }
659 // Accept editor percent (0–100) or fraction (0–1).
660 if (chance > 1)
661 { chance = chance / 100; }
662 chance = Math.min(1, Math.max(0, chance));
663 let quantityMin = Math.max(0, Math.floor(Number(
664 entry.quantityMin ?? entry.quantity ?? 1
665 )));
666 let quantityMax = Math.max(0, Math.floor(Number(
667 entry.quantityMax ?? entry.quantityMin ?? entry.quantity ?? quantityMin
668 )));
669 if (!Number.isFinite(quantityMin))
670 { quantityMin = 1; }
671 if (!Number.isFinite(quantityMax))
672 { quantityMax = quantityMin; }
673 if (quantityMax < quantityMin)
674 { quantityMax = quantityMin; }
675 lines.push(Object.freeze({ itemId, chance, quantityMin, quantityMax }));
676 }
677 return lines;
678}
679
680function normalizeSpells (raw)
681{
682 if (!Array.isArray(raw))
683 { return []; }
684 const lines = [];
685 const seen = new Set();
686 for (const entry of raw)
687 {
688 if (!entry || typeof entry !== "object")
689 { continue; }
690 const spellId = String(entry.spellId ?? "").trim();
691 // Drop unknown / retired ids (e.g. legacy spark) so spawners stay loadable.
692 if (!spellId || seen.has(spellId) || !spellDefinition(spellId))
693 { continue; }
694 seen.add(spellId);
695 // Cast chance is derived from mob level at spawn time — not stored.
696 lines.push({ spellId });
697 }
698 return lines;
699}
700
701/** Clamp wander ≥ spawn; coerce numeric fields; keep humanoid shop/appearance options. */
702export function normalizeSpawnerConfig (config, spawnerId = "spawner")
703{
704 const mobTypeId = config?.mobTypeId ?? MobId.deer;
705 const centralExchange = mobTypeId === MobId.centralExchange || !!config?.centralExchange;
706 const townGuard = !centralExchange && (mobTypeId === MobId.townGuard || !!config?.townGuard);
707 const effectiveMob = centralExchange
708 ? MobId.centralExchange
709 : (townGuard ? MobId.townGuard : mobTypeId);
710 const base = defaultSpawnerConfig(spawnerId, effectiveMob);
711 const merged = { ...base, ...(config ?? {}), spawnerId: config?.spawnerId || spawnerId, mobTypeId: effectiveMob };
712 const rawSpawn = Number(merged.spawnRangeTiles);
713 const spawnRangeTiles = Math.max(
714 0,
715 Math.min(32, Number.isFinite(rawSpawn) ? (rawSpawn | 0) : base.spawnRangeTiles)
716 );
717 const rawWander = Number(merged.maxWanderRangeTiles);
718 const maxWanderRangeTiles = Math.max(
719 spawnRangeTiles,
720 Math.min(
721 64,
722 Number.isFinite(rawWander) ? (rawWander | 0) : spawnRangeTiles
723 )
724 );
725 const sex = merged.sex === "female" ? "female" : "male";
726 const hiddenDiscount = Math.min(1, Math.max(0.01, Number(merged.hiddenDiscount) || base.hiddenDiscount));
727 const displayName = centralExchange
728 ? ""
729 : String(merged.displayName ?? "").trim().slice(0, 48);
730 const staffCap = centralExchangeStaffCount();
731 const level = Math.max(1, Math.min(99, Number(merged.level) || base.level || 1)) | 0;
732 const bodyNpc = isHumanoidMob(effectiveMob) || centralExchange || townGuard;
733 const aggressive = bodyNpc ? !!merged.aggressive : false;
734 // Passive only when Aggressive is off (spawner flag or wildlife mob def).
735 const defAggressive = !bodyNpc && !!mobDefinition(effectiveMob)?.aggressive;
736 const passive = !aggressive && !defAggressive && !!merged.passive;
737 const bounty = aggressive ? 0 : (Math.max(0, Number(merged.bounty) || 0) | 0);
738 return {
739 spawnerId: String(merged.spawnerId),
740 mobTypeId: effectiveMob,
741 maxAlive: centralExchange
742 ? staffCap
743 : Math.max(1, Math.min(20, Number(merged.maxAlive) || base.maxAlive)),
744 respawnDelaySeconds: Math.max(
745 0,
746 Math.min(
747 86_400,
748 Math.floor(
749 Number(
750 merged.respawnDelaySeconds != null && merged.respawnDelaySeconds !== ""
751 ? merged.respawnDelaySeconds
752 : base.respawnDelaySeconds
753 ) || 0
754 )
755 )
756 ),
757 spawnRangeTiles,
758 maxWanderRangeTiles,
759 level,
760 bounty,
761 aggressive,
762 passive,
763 sex,
764 displayName,
765 equipment: normalizeEquipment(merged.equipment),
766 sells: centralExchange ? [] : normalizeSells(merged.sells),
767 drops: normalizeDrops(merged.drops),
768 spells: normalizeSpells(merged.spells),
769 buysItems: centralExchange ? false : !!merged.buysItems,
770 hiddenDiscount,
771 centralExchange,
772 townGuard,
773 dialogueId: bodyNpc
774 ? (String(merged.dialogueId ?? "").trim().slice(0, 64) || null)
775 : null
776 };
777}
778
779export function listAmbientMobTypes ()
780{
781 return Object.values(MOB_DEFINITIONS).filter((def) => def.ambient);
782}
783
784/** Every mob type that can be authored as a world-editor spawner. */
785export function listSpawnableMobTypes ()
786{
787 return Object.values(MOB_DEFINITIONS);
788}
789
790export function isHumanoidMob (mobTypeId)
791{
792 return mobDefinition(mobTypeId)?.usePlayerBody === true;
793}
794
795export function isTownGuardMob (mobTypeId)
796{
797 return mobTypeId === MobId.townGuard || !!mobDefinition(mobTypeId)?.townGuard;
798}
799
800export function isCentralExchangeSpawner (configOrMobTypeId)
801{
802 if (configOrMobTypeId && typeof configOrMobTypeId === "object")
803 { return !!configOrMobTypeId.centralExchange || configOrMobTypeId.mobTypeId === MobId.centralExchange; }
804 return configOrMobTypeId === MobId.centralExchange;
805}
806
807/** Max HP for a mob at the given spawner level. */
808export function mobMaxHealthForLevel (def, level)
809{
810 const lv = Math.max(1, Math.floor(Number(level) || 1));
811 const formula = def?.maxHealthFromLevel;
812 if (formula === "levelPlus10" || (!formula && def?.usePlayerBody))
813 { return lv + 10; }
814 const base = Math.max(1, Number(def?.health ?? def?.maxHealth ?? 8) || 8);
815 return Math.max(1, base + (lv - 1));
816}
817
818/** Display label for hover / UI: `Lv.5 Deer`. */
819export function mobLevelDisplayName (level, displayName)
820{
821 const lv = Math.max(1, Math.floor(Number(level) || 1));
822 const name = String(displayName ?? "").trim() || "Creature";
823 return `Lv.${lv} ${name}`;
824}