| 1 | import { spriteImageName } from "./tileSprites.js"; |
| 2 | import { centralExchangeStaffCount } from "../market/exchange.js"; |
| 3 | |
| 4 | export 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 | */ |
| 27 | export 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 | |
| 161 | export function mobDefinition (mobTypeId) |
| 162 | { |
| 163 | return MOB_DEFINITIONS[mobTypeId] ?? null; |
| 164 | } |
| 165 | |
| 166 | export 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 | |
| 181 | export 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 | |
| 203 | function 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 | |
| 215 | /** |
| 216 | * Spawner equipment entry: legacy itemId number, or `{ itemId, dyeColor? }` for dyed clothing. |
| 217 | * Normalized form keeps a plain number when undyed (world-save compatible). |
| 218 | */ |
| 219 | function normalizeEquipmentEntry (value) |
| 220 | { |
| 221 | if (value == null || value === "" || value === "0") |
| 222 | { return null; } |
| 223 | let itemId = 0; |
| 224 | let dyeColor = null; |
| 225 | if (typeof value === "object") |
| 226 | { |
| 227 | itemId = Number(value.itemId) | 0; |
| 228 | dyeColor = normalizeDyeColor(value.dyeColor); |
| 229 | } |
| 230 | else |
| 231 | { itemId = Number(value) | 0; } |
| 232 | if (!Number.isFinite(itemId) || itemId <= 0) |
| 233 | { return null; } |
| 234 | if (!dyeColor) |
| 235 | { return itemId; } |
| 236 | return Object.freeze({ itemId, dyeColor }); |
| 237 | } |
| 238 | |
| 239 | function normalizeDyeColor (raw) |
| 240 | { |
| 241 | if (!raw || typeof raw !== "object") |
| 242 | { return null; } |
| 243 | const r = Number(raw.r); |
| 244 | const g = Number(raw.g); |
| 245 | const b = Number(raw.b); |
| 246 | if (![r, g, b].every(Number.isFinite)) |
| 247 | { return null; } |
| 248 | const asByte = r > 1 || g > 1 || b > 1; |
| 249 | return Object.freeze({ |
| 250 | r: Math.min(1, Math.max(0, asByte ? r / 255 : r)), |
| 251 | g: Math.min(1, Math.max(0, asByte ? g / 255 : g)), |
| 252 | b: Math.min(1, Math.max(0, asByte ? b / 255 : b)) |
| 253 | }); |
| 254 | } |
| 255 | |
| 256 | function normalizeEquipment (raw) |
| 257 | { |
| 258 | const next = emptySpawnerEquipment(); |
| 259 | if (!raw || typeof raw !== "object") |
| 260 | { return next; } |
| 261 | for (const slot of Object.keys(next)) |
| 262 | { next[slot] = normalizeEquipmentEntry(raw[slot]); } |
| 263 | return next; |
| 264 | } |
| 265 | |
| 266 | /** Resolve authored spawner equipment into itemId (+ optional dye). */ |
| 267 | export function spawnerEquipmentSlot (entry) |
| 268 | { |
| 269 | if (entry == null) |
| 270 | { return { itemId: 0, dyeColor: null }; } |
| 271 | if (typeof entry === "object") |
| 272 | { |
| 273 | return { |
| 274 | itemId: Number(entry.itemId) | 0, |
| 275 | dyeColor: entry.dyeColor ?? null |
| 276 | }; |
| 277 | } |
| 278 | return { itemId: Number(entry) | 0, dyeColor: null }; |
| 279 | } |
| 280 | |
| 281 | function normalizeSells (raw) |
| 282 | { |
| 283 | if (!Array.isArray(raw)) |
| 284 | { return []; } |
| 285 | const lines = []; |
| 286 | const seen = new Set(); |
| 287 | for (const entry of raw) |
| 288 | { |
| 289 | let itemId; |
| 290 | let quantity = 0; |
| 291 | if (entry && typeof entry === "object") |
| 292 | { |
| 293 | itemId = Number(entry.itemId); |
| 294 | quantity = Math.max(0, Number(entry.quantity) || 0) | 0; |
| 295 | } |
| 296 | else |
| 297 | { itemId = Number(entry); } |
| 298 | if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId | 0)) |
| 299 | { continue; } |
| 300 | seen.add(itemId | 0); |
| 301 | lines.push({ itemId: itemId | 0, quantity }); |
| 302 | } |
| 303 | return lines; |
| 304 | } |
| 305 | |
| 306 | /** Clamp wander ≥ spawn; coerce numeric fields; keep humanoid shop/appearance options. */ |
| 307 | export function normalizeSpawnerConfig (config, spawnerId = "spawner") |
| 308 | { |
| 309 | const mobTypeId = config?.mobTypeId ?? MobId.deer; |
| 310 | const centralExchange = mobTypeId === MobId.centralExchange || !!config?.centralExchange; |
| 311 | const effectiveMob = centralExchange ? MobId.centralExchange : mobTypeId; |
| 312 | const base = defaultSpawnerConfig(spawnerId, effectiveMob); |
| 313 | const merged = { ...base, ...(config ?? {}), spawnerId: config?.spawnerId || spawnerId, mobTypeId: effectiveMob }; |
| 314 | const spawnRangeTiles = Math.max(1, Math.min(32, Number(merged.spawnRangeTiles) || base.spawnRangeTiles)); |
| 315 | const maxWanderRangeTiles = Math.max( |
| 316 | spawnRangeTiles, |
| 317 | Math.min(64, Number(merged.maxWanderRangeTiles) || spawnRangeTiles) |
| 318 | ); |
| 319 | const sex = merged.sex === "female" ? "female" : "male"; |
| 320 | const hiddenDiscount = Math.min(1, Math.max(0.01, Number(merged.hiddenDiscount) || base.hiddenDiscount)); |
| 321 | const displayName = String(merged.displayName ?? "").trim().slice(0, 48); |
| 322 | const staffCap = centralExchangeStaffCount(); |
| 323 | return { |
| 324 | spawnerId: String(merged.spawnerId), |
| 325 | mobTypeId: effectiveMob, |
| 326 | maxAlive: centralExchange |
| 327 | ? staffCap |
| 328 | : Math.max(1, Math.min(20, Number(merged.maxAlive) || base.maxAlive)), |
| 329 | spawnRangeTiles, |
| 330 | maxWanderRangeTiles, |
| 331 | sex, |
| 332 | displayName, |
| 333 | equipment: normalizeEquipment(merged.equipment), |
| 334 | sells: centralExchange ? [] : normalizeSells(merged.sells), |
| 335 | buysItems: centralExchange ? false : !!merged.buysItems, |
| 336 | hiddenDiscount, |
| 337 | centralExchange |
| 338 | }; |
| 339 | } |
| 340 | |
| 341 | export function listAmbientMobTypes () |
| 342 | { |
| 343 | return Object.values(MOB_DEFINITIONS).filter((def) => def.ambient); |
| 344 | } |
| 345 | |
| 346 | /** Every mob type that can be authored as a world-editor spawner. */ |
| 347 | export function listSpawnableMobTypes () |
| 348 | { |
| 349 | return Object.values(MOB_DEFINITIONS); |
| 350 | } |
| 351 | |
| 352 | export function isHumanoidMob (mobTypeId) |
| 353 | { |
| 354 | return mobDefinition(mobTypeId)?.usePlayerBody === true; |
| 355 | } |
| 356 | |
| 357 | export function isCentralExchangeSpawner (configOrMobTypeId) |
| 358 | { |
| 359 | if (configOrMobTypeId && typeof configOrMobTypeId === "object") |
| 360 | { return !!configOrMobTypeId.centralExchange || configOrMobTypeId.mobTypeId === MobId.centralExchange; } |
| 361 | return configOrMobTypeId === MobId.centralExchange; |
| 362 | } |