import { spriteImageName } from "./tileSprites.js"; import { centralExchangeStaffCount } from "../market/exchange.js"; export const MobId = Object.freeze({ deer: "deer", pigeon: "pigeon", rat: "rat", humanoid: "humanoid", /** Editor/spawner identity for Central Exchange staff (always humanoid bodies). */ centralExchange: "central-exchange" }); /** @typedef {{ itemId: number, chance: number }} MobDrop */ /** @typedef {{ isPlayer: boolean, text: string }} DialogueLine */ /** @typedef {{ trigger: string, text: string, available?: (ctx: object) => boolean }} DialogueResponse */ /** * Target mob content schema (rendering fields stay alongside gameplay fields for now). * - displayName: shown name * - health / maxHealth: hit points * - attackSkill: one skill level for hit chance and damage * - drops: { itemId, chance } roll on death * - sells: default item IDs for shop stock (overridable per spawner) * - dialogue: branching trees with triggers, lines, responses, and action hooks (later) * - usePlayerBody: draw male/female character body instead of a mob sprite */ export const MOB_DEFINITIONS = Object.freeze({ [MobId.deer]: Object.freeze({ mobTypeId: MobId.deer, key: "deer", assetFolder: "mobs", displayName: "Deer", health: 8, maxHealth: 8, attackSkill: 2, attackCooldownMs: 1000, baseDamage: 1, drops: Object.freeze([]), sells: Object.freeze([]), dialogue: Object.freeze([]), persistent: false, flying: false, freeFlight: false, ambient: false, talkable: false, untargetable: false, aggressive: false, gait: "walk", sex: "male", frames: 1, framesPerRow: 1, animationSpeed: 0 }), [MobId.pigeon]: Object.freeze({ mobTypeId: MobId.pigeon, key: "pigeon", assetFolder: "pigeons", displayName: "Pigeon", health: 1, maxHealth: 1, canFight: false, drops: Object.freeze([]), sells: Object.freeze([]), dialogue: Object.freeze([]), persistent: false, flying: true, freeFlight: true, ambient: false, talkable: false, untargetable: true, aggressive: false, gait: "walk", sex: "male", frames: 2, framesPerRow: 2, animationSpeed: 0.18, defaultState: "pigeon-flying", flightSpeedTilesPerSec: 2.5 }), [MobId.rat]: Object.freeze({ mobTypeId: MobId.rat, key: "rat", assetFolder: "mobs", displayName: "Rat", health: 12, maxHealth: 12, attackSkill: 3, attackCooldownMs: 900, baseDamage: 2, drops: Object.freeze([]), sells: Object.freeze([]), dialogue: Object.freeze([]), persistent: false, flying: false, freeFlight: false, ambient: false, talkable: false, untargetable: false, /** Auto-engage nearby players when in range with line of sight. */ aggressive: true, aggroRangeTiles: 5, gait: "walk", sex: "male", frames: 1, framesPerRow: 1, animationSpeed: 0 }), [MobId.humanoid]: Object.freeze({ mobTypeId: MobId.humanoid, key: "humanoid", assetFolder: "characters", displayName: "Humanoid", health: 10, maxHealth: 10, canFight: false, drops: Object.freeze([]), sells: Object.freeze([]), dialogue: Object.freeze([]), persistent: false, flying: false, freeFlight: false, ambient: false, talkable: true, untargetable: false, aggressive: false, usePlayerBody: true, gait: "walk", sex: "male", frames: 1, framesPerRow: 1, animationSpeed: 0 }), [MobId.centralExchange]: Object.freeze({ mobTypeId: MobId.centralExchange, key: "central-exchange", assetFolder: "characters", displayName: "Central Exchange staff", health: 10, maxHealth: 10, canFight: false, drops: Object.freeze([]), sells: Object.freeze([]), dialogue: Object.freeze([]), persistent: false, flying: false, freeFlight: false, ambient: false, talkable: false, untargetable: false, aggressive: false, usePlayerBody: true, centralExchange: true, gait: "walk", sex: "male", frames: 1, framesPerRow: 1, animationSpeed: 0 }) }); export function mobDefinition (mobTypeId) { return MOB_DEFINITIONS[mobTypeId] ?? null; } export function mobSpriteName (mobTypeId) { const def = mobDefinition(mobTypeId); const key = def?.key ?? def?.mobTypeId ?? "deer"; if (def?.usePlayerBody) { // Editor palette preview; live entities use appearance.sex → bodySprite(). return spriteImageName("character", "player-body-male", "player-body-male"); } if (def?.sprite) { return def.sprite; } const state = def?.defaultState ?? key; return spriteImageName("mob", key, state); } export function defaultSpawnerConfig (spawnerId, mobTypeId = MobId.deer) { const humanoid = mobTypeId === MobId.humanoid; const centralExchange = mobTypeId === MobId.centralExchange; return { spawnerId, mobTypeId, maxAlive: centralExchange ? centralExchangeStaffCount() : (humanoid ? 1 : 3), spawnRangeTiles: (humanoid || centralExchange) ? 1 : 4, maxWanderRangeTiles: (humanoid || centralExchange) ? 1 : 8, sex: "male", displayName: humanoid ? "Shopkeeper" : "", equipment: emptySpawnerEquipment(), sells: [], buysItems: false, hiddenDiscount: 0.9, centralExchange }; } function emptySpawnerEquipment () { return { head: null, chest: null, legs: null, boots: null, weapon: null, offhand: null }; } /** * Spawner equipment entry: legacy itemId number, or `{ itemId, dyeColor? }` for dyed clothing. * Normalized form keeps a plain number when undyed (world-save compatible). */ function normalizeEquipmentEntry (value) { if (value == null || value === "" || value === "0") { return null; } let itemId = 0; let dyeColor = null; if (typeof value === "object") { itemId = Number(value.itemId) | 0; dyeColor = normalizeDyeColor(value.dyeColor); } else { itemId = Number(value) | 0; } if (!Number.isFinite(itemId) || itemId <= 0) { return null; } if (!dyeColor) { return itemId; } return Object.freeze({ itemId, dyeColor }); } function normalizeDyeColor (raw) { if (!raw || typeof raw !== "object") { return null; } const r = Number(raw.r); const g = Number(raw.g); const b = Number(raw.b); if (![r, g, b].every(Number.isFinite)) { return null; } const asByte = r > 1 || g > 1 || b > 1; return Object.freeze({ r: Math.min(1, Math.max(0, asByte ? r / 255 : r)), g: Math.min(1, Math.max(0, asByte ? g / 255 : g)), b: Math.min(1, Math.max(0, asByte ? b / 255 : b)) }); } function normalizeEquipment (raw) { const next = emptySpawnerEquipment(); if (!raw || typeof raw !== "object") { return next; } for (const slot of Object.keys(next)) { next[slot] = normalizeEquipmentEntry(raw[slot]); } return next; } /** Resolve authored spawner equipment into itemId (+ optional dye). */ export function spawnerEquipmentSlot (entry) { if (entry == null) { return { itemId: 0, dyeColor: null }; } if (typeof entry === "object") { return { itemId: Number(entry.itemId) | 0, dyeColor: entry.dyeColor ?? null }; } return { itemId: Number(entry) | 0, dyeColor: null }; } function normalizeSells (raw) { if (!Array.isArray(raw)) { return []; } const lines = []; const seen = new Set(); for (const entry of raw) { let itemId; let quantity = 0; if (entry && typeof entry === "object") { itemId = Number(entry.itemId); quantity = Math.max(0, Number(entry.quantity) || 0) | 0; } else { itemId = Number(entry); } if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId | 0)) { continue; } seen.add(itemId | 0); lines.push({ itemId: itemId | 0, quantity }); } return lines; } /** Clamp wander ≥ spawn; coerce numeric fields; keep humanoid shop/appearance options. */ export function normalizeSpawnerConfig (config, spawnerId = "spawner") { const mobTypeId = config?.mobTypeId ?? MobId.deer; const centralExchange = mobTypeId === MobId.centralExchange || !!config?.centralExchange; const effectiveMob = centralExchange ? MobId.centralExchange : mobTypeId; const base = defaultSpawnerConfig(spawnerId, effectiveMob); const merged = { ...base, ...(config ?? {}), spawnerId: config?.spawnerId || spawnerId, mobTypeId: effectiveMob }; const spawnRangeTiles = Math.max(1, Math.min(32, Number(merged.spawnRangeTiles) || base.spawnRangeTiles)); const maxWanderRangeTiles = Math.max( spawnRangeTiles, Math.min(64, Number(merged.maxWanderRangeTiles) || spawnRangeTiles) ); const sex = merged.sex === "female" ? "female" : "male"; const hiddenDiscount = Math.min(1, Math.max(0.01, Number(merged.hiddenDiscount) || base.hiddenDiscount)); const displayName = String(merged.displayName ?? "").trim().slice(0, 48); const staffCap = centralExchangeStaffCount(); return { spawnerId: String(merged.spawnerId), mobTypeId: effectiveMob, maxAlive: centralExchange ? staffCap : Math.max(1, Math.min(20, Number(merged.maxAlive) || base.maxAlive)), spawnRangeTiles, maxWanderRangeTiles, sex, displayName, equipment: normalizeEquipment(merged.equipment), sells: centralExchange ? [] : normalizeSells(merged.sells), buysItems: centralExchange ? false : !!merged.buysItems, hiddenDiscount, centralExchange }; } export function listAmbientMobTypes () { return Object.values(MOB_DEFINITIONS).filter((def) => def.ambient); } /** Every mob type that can be authored as a world-editor spawner. */ export function listSpawnableMobTypes () { return Object.values(MOB_DEFINITIONS); } export function isHumanoidMob (mobTypeId) { return mobDefinition(mobTypeId)?.usePlayerBody === true; } export function isCentralExchangeSpawner (configOrMobTypeId) { if (configOrMobTypeId && typeof configOrMobTypeId === "object") { return !!configOrMobTypeId.centralExchange || configOrMobTypeId.mobTypeId === MobId.centralExchange; } return configOrMobTypeId === MobId.centralExchange; }