BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

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