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