/** * Shop / tooltip pricing from seedValue and optional player-market last sale. * Matches design §27.6 — lerp toward lastTradePrice over shopPriceLerpDurationMs. */ import { itemDefinition } from "../definitions/items.js"; import { ItemCategory } from "../definitions/itemCategories.js"; import { DEFAULT_WORLD_SETTINGS } from "../protocol/constants.js"; export function lerp (a, b, t) { return a + (b - a) * t; } /** * @param {object|null} lastSale `{ lastTradePrice, lastTradeAt, priceFrom }` */ export function undiscountedMarketValue (itemId, lastSale = null, now = Date.now(), lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs) { const def = itemDefinition(itemId); const seed = Math.max(1, Number(def?.seedValue) || 1); if (!lastSale?.lastTradeAt || lastSale.lastTradePrice == null) { return seed; } const duration = Math.max(1, Number(lerpDurationMs) || DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs); const t = Math.min(1, Math.max(0, (now - lastSale.lastTradeAt) / duration)); const from = lastSale.priceFrom != null ? Number(lastSale.priceFrom) : seed; const to = Number(lastSale.lastTradePrice); if (!Number.isFinite(from) || !Number.isFinite(to)) { return seed; } return lerp(from, to, t); } /** Integer shop purchase price (server authority). */ export function shopUnitPrice (itemId, { lastSale = null, now = Date.now(), lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs, hiddenDiscount = 1 } = {}) { const undiscounted = undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs); const discount = Math.min(1, Math.max(0.01, Number(hiddenDiscount) || 1)); return Math.max(1, Math.round(undiscounted * discount)); } /** NPC shops sell ingredients (and items explicitly flagged). */ export function canSellInNpcShop (itemId) { const def = itemDefinition(itemId); if (!def) { return false; } if (def.npcShopAllowed === false) { return false; } if (def.npcShopAllowed === true) { return true; } return def.category === ItemCategory.ingredient || def.category === "ingredient"; }