BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

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 { 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 */
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 meleeAttack: Object.freeze({
155 mode: "melee",
156 rangeTiles: 1,
157 hitSkillId: SkillId.swordfighting,
158 damageSkillId: SkillId.strength,
159 attackType: AttackType.slashing
160 }),
161 gait: "walk",
162 sex: "male",
163 frames: 1,
164 framesPerRow: 1,
165 animationSpeed: 0
166 }),
167 [MobId.chicken]: Object.freeze({
168 mobTypeId: MobId.chicken,
169 key: "chicken",
170 assetFolder: "mobs",
171 displayName: "Chicken",
172 health: 5,
173 maxHealth: 5,
174 attackSkill: 1,
175 attackCooldownMs: 1200,
176 drops: Object.freeze([
177 { itemId: RangedItemId.feather, chance: 1, quantityMin: 1, quantityMax: 3 },
178 { itemId: ItemId.egg, chance: 0.2 }
179 ]),
180 sells: Object.freeze([]),
181 dialogue: Object.freeze([]),
182 persistent: false,
183 flying: false,
184 freeFlight: false,
185 ambient: false,
186 talkable: false,
187 untargetable: false,
188 aggressive: false,
189 gait: "walk",
190 sex: "female",
191 frames: 1,
192 framesPerRow: 1,
193 animationSpeed: 0
194 }),
195 [MobId.sheep]: Object.freeze({
196 mobTypeId: MobId.sheep,
197 key: "sheep",
198 assetFolder: "mobs",
199 displayName: "Sheep",
200 health: 8,
201 maxHealth: 8,
202 attackSkill: 1,
203 attackCooldownMs: 1200,
204 drops: Object.freeze([
205 { itemId: LeatherItemId.softLeather, chance: 0.5 },
206 { itemId: CraftingItemId.rawMutton, chance: 1 }
207 ]),
208 sells: Object.freeze([]),
209 dialogue: Object.freeze([]),
210 persistent: false,
211 flying: false,
212 freeFlight: false,
213 ambient: false,
214 talkable: false,
215 untargetable: false,
216 aggressive: false,
217 gait: "walk",
218 sex: "female",
219 frames: 1,
220 framesPerRow: 1,
221 animationSpeed: 0,
222 defaultState: "fleece",
223 shearable: Object.freeze({
224 clothTier: ClothTier.wool,
225 fleeceState: "fleece",
226 shornState: "shorn"
227 })
228 }),
229 [MobId.cashmereGoat]: Object.freeze({
230 mobTypeId: MobId.cashmereGoat,
231 key: "cashmere-goat",
232 assetFolder: "mobs",
233 displayName: "Goat",
234 health: 10,
235 maxHealth: 10,
236 attackSkill: 2,
237 attackCooldownMs: 1100,
238 drops: Object.freeze([
239 { itemId: LeatherItemId.softLeather, chance: 0.6 },
240 { itemId: CraftingItemId.rawGoat, chance: 1 }
241 ]),
242 sells: Object.freeze([]),
243 dialogue: Object.freeze([]),
244 persistent: false,
245 flying: false,
246 freeFlight: false,
247 ambient: false,
248 talkable: false,
249 untargetable: false,
250 aggressive: false,
251 gait: "walk",
252 sex: "female",
253 frames: 1,
254 framesPerRow: 1,
255 animationSpeed: 0,
256 defaultState: "fleece",
257 shearable: Object.freeze({
258 clothTier: ClothTier.cashmere,
259 fleeceState: "fleece",
260 shornState: "shorn"
261 })
262 }),
263 [MobId.shade]: Object.freeze({
264 mobTypeId: MobId.shade,
265 key: "shade",
266 assetFolder: "mobs",
267 displayName: "Shade",
268 health: 10,
269 maxHealth: 10,
270 maxHealthFromLevel: "levelPlus10",
271 attackSkill: 5,
272 attackCooldownMs: 900,
273 drops: Object.freeze([
274 { itemId: EssenceItemId[MagicElement.dark], chance: 1 }
275 ]),
276 sells: Object.freeze([]),
277 dialogue: Object.freeze([]),
278 persistent: false,
279 flying: false,
280 freeFlight: false,
281 ambient: false,
282 talkable: false,
283 untargetable: false,
284 /** Spawner shades are hostile; summoned shades opt out via Component.summon. */
285 aggressive: true,
286 aggroRangeTiles: 5,
287 bloomBody: true,
288 gait: "walk",
289 sex: "male",
290 /** Body sheet is static; bloom flickers on its own. */
291 frames: 1,
292 framesPerRow: 1,
293 animationSpeed: 0,
294 bloomFrames: 2,
295 bloomFramesPerRow: 2,
296 /** Match elemental sprite bloom cadence. */
297 bloomAnimationSpeed: 0.5,
298 defaultState: "shade"
299 }),
300 [MobId.sprite]: Object.freeze({
301 mobTypeId: MobId.sprite,
302 key: "sprite",
303 assetFolder: "mobs",
304 displayName: "Sprite",
305 health: 35,
306 maxHealth: 35,
307 maxHealthFromLevel: "levelPlus10",
308 attackSkill: 25,
309 attackCooldownMs: 1100,
310 drops: Object.freeze([]),
311 /** Essence drop is resolved from the creature's chosen element on death. */
312 essenceDropChance: 1,
313 sells: Object.freeze([]),
314 dialogue: Object.freeze([]),
315 persistent: false,
316 flying: false,
317 freeFlight: false,
318 ambient: false,
319 talkable: false,
320 untargetable: false,
321 aggressive: true,
322 aggroRangeTiles: 6,
323 preferSpellAttack: true,
324 lightRadiusTiles: 2,
325 randomElementOnSpawn: true,
326 bloomBody: true,
327 gait: "walk",
328 sex: "male",
329 frames: 2,
330 framesPerRow: 2,
331 animationSpeed: 0.5
332 }),
333 [MobId.fairy]: Object.freeze({
334 mobTypeId: MobId.fairy,
335 key: "fairy",
336 assetFolder: "mobs",
337 displayName: "Fairy",
338 health: 40,
339 maxHealth: 40,
340 maxHealthFromLevel: "levelPlus10",
341 attackSkill: 55,
342 attackCooldownMs: 1000,
343 drops: Object.freeze([
344 { itemId: ClothItemId.fairySilk, chance: 0.7 }
345 ]),
346 sells: Object.freeze([]),
347 dialogue: Object.freeze([]),
348 persistent: false,
349 flying: true,
350 freeFlight: false,
351 ambient: false,
352 talkable: false,
353 untargetable: false,
354 aggressive: true,
355 aggroRangeTiles: 5,
356 /** Prefer sprite-style elemental bolts over melee. */
357 preferSpellAttack: true,
358 /** Cast a random-element sprite bolt each attack (no fixed spawn element). */
359 randomElementSpell: true,
360 /** On hit, flee until at least this many tiles from the hit source (then stop until hit again). */
361 fleeOnHitTiles: 2,
362 /** Dimmer than elemental sprites (radius 2). */
363 lightRadiusTiles: 1,
364 bloomBody: true,
365 /** Shirt/skirt/hair use dual-channel body tints rolled on spawn. */
366 dualChannelAppearance: true,
367 gait: "walk",
368 sex: "female",
369 frames: 2,
370 framesPerRow: 2,
371 animationSpeed: 0.45,
372 defaultState: "fairy"
373 }),
374 [MobId.humanoid]: Object.freeze({
375 mobTypeId: MobId.humanoid,
376 key: "humanoid",
377 assetFolder: "characters",
378 displayName: "Humanoid",
379 health: 10,
380 maxHealth: 10,
381 /** HP = spawner level + 10 (same floor as a starting player's Health). */
382 maxHealthFromLevel: "levelPlus10",
383 drops: Object.freeze([]),
384 sells: Object.freeze([]),
385 dialogue: Object.freeze([]),
386 persistent: false,
387 flying: false,
388 freeFlight: false,
389 ambient: false,
390 talkable: true,
391 untargetable: false,
392 aggressive: false,
393 usePlayerBody: true,
394 gait: "walk",
395 sex: "male",
396 frames: 1,
397 framesPerRow: 1,
398 animationSpeed: 0
399 }),
400 [MobId.townGuard]: Object.freeze({
401 mobTypeId: MobId.townGuard,
402 key: "town-guard",
403 assetFolder: "characters",
404 displayName: "Town Guard",
405 health: 10,
406 maxHealth: 10,
407 maxHealthFromLevel: "levelPlus10",
408 drops: Object.freeze([]),
409 sells: Object.freeze([]),
410 dialogue: Object.freeze([]),
411 persistent: false,
412 flying: false,
413 freeFlight: false,
414 ambient: false,
415 talkable: false,
416 untargetable: false,
417 aggressive: false,
418 usePlayerBody: true,
419 townGuard: true,
420 gait: "walk",
421 sex: "male",
422 frames: 1,
423 framesPerRow: 1,
424 animationSpeed: 0
425 }),
426 [MobId.centralExchange]: Object.freeze({
427 mobTypeId: MobId.centralExchange,
428 key: "central-exchange",
429 assetFolder: "characters",
430 displayName: "Central Exchange staff",
431 health: 10,
432 maxHealth: 10,
433 maxHealthFromLevel: "levelPlus10",
434 drops: Object.freeze([]),
435 sells: Object.freeze([]),
436 dialogue: Object.freeze([]),
437 persistent: false,
438 flying: false,
439 freeFlight: false,
440 ambient: false,
441 talkable: false,
442 untargetable: false,
443 aggressive: false,
444 usePlayerBody: true,
445 centralExchange: true,
446 gait: "walk",
447 sex: "male",
448 frames: 1,
449 framesPerRow: 1,
450 animationSpeed: 0
451 })
452});
453
454export function mobDefinition (mobTypeId)
455{
456 return MOB_DEFINITIONS[mobTypeId] ?? null;
457}
458
459export function mobSpriteName (mobTypeId, visualState = null)
460{
461 const def = mobDefinition(mobTypeId);
462 const key = def?.spriteKey ?? def?.key ?? def?.mobTypeId ?? "deer";
463 if (def?.usePlayerBody)
464 {
465 // Editor palette preview; live entities use appearance.sex → bodySprite().
466 return spriteImageName("character", "player-body-male", "player-body-male");
467 }
468 if (def?.sprite)
469 { return def.sprite; }
470 const state = visualState ?? def?.defaultState ?? key;
471 return spriteImageName("mob", key, state);
472}
473
474/** Whether this mob definition can be sheared for cloth fibre. */
475export function mobIsShearable (mobTypeId)
476{
477 return !!mobDefinition(mobTypeId)?.shearable;
478}
479
480export function defaultSpawnerConfig (spawnerId, mobTypeId = MobId.deer)
481{
482 const humanoid = isHumanoidMob(mobTypeId) || mobTypeId === MobId.humanoid;
483 const townGuard = mobTypeId === MobId.townGuard;
484 const centralExchange = mobTypeId === MobId.centralExchange;
485 const bodyNpc = humanoid || townGuard || centralExchange;
486 const level = townGuard
487 ? 30
488 : (mobTypeId === MobId.goblin
489 ? 5
490 : (mobTypeId === MobId.sprite
491 ? 25
492 : (mobTypeId === MobId.shade
493 ? 28
494 : (mobTypeId === MobId.fairy ? 70 : 1))));
495 const bounty = townGuard ? 500 : 0;
496 return {
497 spawnerId,
498 mobTypeId,
499 maxAlive: centralExchange
500 ? centralExchangeStaffCount()
501 : (bodyNpc ? 1 : 3),
502 /** Seconds after death before this spawner may refill that slot (0 = next spawner tick). */
503 respawnDelaySeconds: 25,
504 spawnRangeTiles: bodyNpc ? 1 : 4,
505 maxWanderRangeTiles: bodyNpc ? 1 : 8,
506 level,
507 bounty,
508 /** Humanoid spawners only: auto-engage players in aggro range. Forces bounty 0. */
509 aggressive: false,
510 sex: "male",
511 displayName: townGuard ? "Town Guard" : (humanoid ? "Shopkeeper" : ""),
512 equipment: townGuard
513 ? {
514 head: ArmourItemId.ironHelmet,
515 chest: ArmourItemId.ironChestplate,
516 legs: ArmourItemId.bronzeGreaves,
517 boots: ArmourItemId.ironBoots,
518 weapon: WoodItemId.oakQuarterstaff,
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}