BlueGit

timfa questward

Files

questward / shared / definitions / mobs.js

mobs.js

rweaks
timfa Tim Falken · pushed by timfa · 2 months ago
2b34e2dd History
mobs.js 8.2 KiB Download Raw
1import { spriteImageName } from "./tileSprites.js";
2import { centralExchangeStaffCount } from "../market/exchange.js";
3
4export const MobId = Object.freeze({
5 deer: "deer",
6 pigeon: "pigeon",
7 rat: "rat",
8 humanoid: "humanoid",
9 /** Editor/spawner identity for Central Exchange staff (always humanoid bodies). */
10 centralExchange: "central-exchange"
11});
12
13/** @typedef {{ itemId: number, chance: number }} MobDrop */
14/** @typedef {{ isPlayer: boolean, text: string }} DialogueLine */
15/** @typedef {{ trigger: string, text: string, available?: (ctx: object) => boolean }} DialogueResponse */
16
17/**
18 * Target mob content schema (rendering fields stay alongside gameplay fields for now).
19 * - displayName: shown name
20 * - health / maxHealth: hit points
21 * - attackSkill: one skill level for hit chance and damage
22 * - drops: { itemId, chance } roll on death
23 * - sells: default item IDs for shop stock (overridable per spawner)
24 * - dialogue: branching trees with triggers, lines, responses, and action hooks (later)
25 * - usePlayerBody: draw male/female character body instead of a mob sprite
26 */
27export const MOB_DEFINITIONS = Object.freeze({
28 [MobId.deer]: Object.freeze({
29 mobTypeId: MobId.deer,
30 key: "deer",
31 assetFolder: "mobs",
32 displayName: "Deer",
33 health: 8,
34 maxHealth: 8,
35 attackSkill: 2,
36 attackCooldownMs: 1000,
37 baseDamage: 1,
38 drops: Object.freeze([]),
39 sells: Object.freeze([]),
40 dialogue: Object.freeze([]),
41 persistent: false,
42 flying: false,
43 freeFlight: false,
44 ambient: false,
45 talkable: false,
46 untargetable: false,
47 aggressive: false,
48 gait: "walk",
49 sex: "male",
50 frames: 1,
51 framesPerRow: 1,
52 animationSpeed: 0
53 }),
54 [MobId.pigeon]: Object.freeze({
55 mobTypeId: MobId.pigeon,
56 key: "pigeon",
57 assetFolder: "pigeons",
58 displayName: "Pigeon",
59 health: 1,
60 maxHealth: 1,
61 canFight: false,
62 drops: Object.freeze([]),
63 sells: Object.freeze([]),
64 dialogue: Object.freeze([]),
65 persistent: false,
66 flying: true,
67 freeFlight: true,
68 ambient: false,
69 talkable: false,
70 untargetable: true,
71 aggressive: false,
72 gait: "walk",
73 sex: "male",
74 frames: 2,
75 framesPerRow: 2,
76 animationSpeed: 0.18,
77 defaultState: "pigeon-flying",
78 flightSpeedTilesPerSec: 2.5
79 }),
80 [MobId.rat]: Object.freeze({
81 mobTypeId: MobId.rat,
82 key: "rat",
83 assetFolder: "mobs",
84 displayName: "Rat",
85 health: 12,
86 maxHealth: 12,
87 attackSkill: 3,
88 attackCooldownMs: 900,
89 baseDamage: 2,
90 drops: Object.freeze([]),
91 sells: Object.freeze([]),
92 dialogue: Object.freeze([]),
93 persistent: false,
94 flying: false,
95 freeFlight: false,
96 ambient: false,
97 talkable: false,
98 untargetable: false,
99 /** Auto-engage nearby players when in range with line of sight. */
100 aggressive: true,
101 aggroRangeTiles: 5,
102 gait: "walk",
103 sex: "male",
104 frames: 1,
105 framesPerRow: 1,
106 animationSpeed: 0
107 }),
108 [MobId.humanoid]: Object.freeze({
109 mobTypeId: MobId.humanoid,
110 key: "humanoid",
111 assetFolder: "characters",
112 displayName: "Humanoid",
113 health: 10,
114 maxHealth: 10,
115 canFight: false,
116 drops: Object.freeze([]),
117 sells: Object.freeze([]),
118 dialogue: Object.freeze([]),
119 persistent: false,
120 flying: false,
121 freeFlight: false,
122 ambient: false,
123 talkable: true,
124 untargetable: false,
125 aggressive: false,
126 usePlayerBody: true,
127 gait: "walk",
128 sex: "male",
129 frames: 1,
130 framesPerRow: 1,
131 animationSpeed: 0
132 }),
133 [MobId.centralExchange]: Object.freeze({
134 mobTypeId: MobId.centralExchange,
135 key: "central-exchange",
136 assetFolder: "characters",
137 displayName: "Central Exchange staff",
138 health: 10,
139 maxHealth: 10,
140 canFight: false,
141 drops: Object.freeze([]),
142 sells: Object.freeze([]),
143 dialogue: Object.freeze([]),
144 persistent: false,
145 flying: false,
146 freeFlight: false,
147 ambient: false,
148 talkable: false,
149 untargetable: false,
150 aggressive: false,
151 usePlayerBody: true,
152 centralExchange: true,
153 gait: "walk",
154 sex: "male",
155 frames: 1,
156 framesPerRow: 1,
157 animationSpeed: 0
158 })
159});
160
161export function mobDefinition (mobTypeId)
162{
163 return MOB_DEFINITIONS[mobTypeId] ?? null;
164}
165
166export function mobSpriteName (mobTypeId)
167{
168 const def = mobDefinition(mobTypeId);
169 const key = def?.key ?? def?.mobTypeId ?? "deer";
170 if (def?.usePlayerBody)
171 {
172 // Editor palette preview; live entities use appearance.sex → bodySprite().
173 return spriteImageName("character", "player-body-male", "player-body-male");
174 }
175 if (def?.sprite)
176 { return def.sprite; }
177 const state = def?.defaultState ?? key;
178 return spriteImageName("mob", key, state);
179}
180
181export function defaultSpawnerConfig (spawnerId, mobTypeId = MobId.deer)
182{
183 const humanoid = mobTypeId === MobId.humanoid;
184 const centralExchange = mobTypeId === MobId.centralExchange;
185 return {
186 spawnerId,
187 mobTypeId,
188 maxAlive: centralExchange
189 ? centralExchangeStaffCount()
190 : (humanoid ? 1 : 3),
191 spawnRangeTiles: (humanoid || centralExchange) ? 1 : 4,
192 maxWanderRangeTiles: (humanoid || centralExchange) ? 1 : 8,
193 sex: "male",
194 displayName: humanoid ? "Shopkeeper" : "",
195 equipment: emptySpawnerEquipment(),
196 sells: [],
197 buysItems: false,
198 hiddenDiscount: 0.9,
199 centralExchange
200 };
201}
202
203function emptySpawnerEquipment ()
204{
205 return {
206 head: null,
207 chest: null,
208 legs: null,
209 boots: null,
210 weapon: null,
211 offhand: null
212 };
213}
214
215function normalizeEquipment (raw)
216{
217 const next = emptySpawnerEquipment();
218 if (!raw || typeof raw !== "object")
219 { return next; }
220 for (const slot of Object.keys(next))
221 {
222 const value = raw[slot];
223 if (value == null || value === "" || value === "0")
224 {
225 next[slot] = null;
226 continue;
227 }
228 const itemId = Number(value);
229 next[slot] = Number.isFinite(itemId) && itemId > 0 ? (itemId | 0) : null;
230 }
231 return next;
232}
233
234function normalizeSells (raw)
235{
236 if (!Array.isArray(raw))
237 { return []; }
238 const lines = [];
239 const seen = new Set();
240 for (const entry of raw)
241 {
242 let itemId;
243 let quantity = 0;
244 if (entry && typeof entry === "object")
245 {
246 itemId = Number(entry.itemId);
247 quantity = Math.max(0, Number(entry.quantity) || 0) | 0;
248 }
249 else
250 { itemId = Number(entry); }
251 if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId | 0))
252 { continue; }
253 seen.add(itemId | 0);
254 lines.push({ itemId: itemId | 0, quantity });
255 }
256 return lines;
257}
258
259/** Clamp wander ≥ spawn; coerce numeric fields; keep humanoid shop/appearance options. */
260export function normalizeSpawnerConfig (config, spawnerId = "spawner")
261{
262 const mobTypeId = config?.mobTypeId ?? MobId.deer;
263 const centralExchange = mobTypeId === MobId.centralExchange || !!config?.centralExchange;
264 const effectiveMob = centralExchange ? MobId.centralExchange : mobTypeId;
265 const base = defaultSpawnerConfig(spawnerId, effectiveMob);
266 const merged = { ...base, ...(config ?? {}), spawnerId: config?.spawnerId || spawnerId, mobTypeId: effectiveMob };
267 const spawnRangeTiles = Math.max(1, Math.min(32, Number(merged.spawnRangeTiles) || base.spawnRangeTiles));
268 const maxWanderRangeTiles = Math.max(
269 spawnRangeTiles,
270 Math.min(64, Number(merged.maxWanderRangeTiles) || spawnRangeTiles)
271 );
272 const sex = merged.sex === "female" ? "female" : "male";
273 const hiddenDiscount = Math.min(1, Math.max(0.01, Number(merged.hiddenDiscount) || base.hiddenDiscount));
274 const displayName = String(merged.displayName ?? "").trim().slice(0, 48);
275 const staffCap = centralExchangeStaffCount();
276 return {
277 spawnerId: String(merged.spawnerId),
278 mobTypeId: effectiveMob,
279 maxAlive: centralExchange
280 ? staffCap
281 : Math.max(1, Math.min(20, Number(merged.maxAlive) || base.maxAlive)),
282 spawnRangeTiles,
283 maxWanderRangeTiles,
284 sex,
285 displayName,
286 equipment: normalizeEquipment(merged.equipment),
287 sells: centralExchange ? [] : normalizeSells(merged.sells),
288 buysItems: centralExchange ? false : !!merged.buysItems,
289 hiddenDiscount,
290 centralExchange
291 };
292}
293
294export function listAmbientMobTypes ()
295{
296 return Object.values(MOB_DEFINITIONS).filter((def) => def.ambient);
297}
298
299/** Every mob type that can be authored as a world-editor spawner. */
300export function listSpawnableMobTypes ()
301{
302 return Object.values(MOB_DEFINITIONS);
303}
304
305export function isHumanoidMob (mobTypeId)
306{
307 return mobDefinition(mobTypeId)?.usePlayerBody === true;
308}
309
310export function isCentralExchangeSpawner (configOrMobTypeId)
311{
312 if (configOrMobTypeId && typeof configOrMobTypeId === "object")
313 { return !!configOrMobTypeId.centralExchange || configOrMobTypeId.mobTypeId === MobId.centralExchange; }
314 return configOrMobTypeId === MobId.centralExchange;
315}