import { rgb } from "./materialTiers.js"; import { ItemCategory } from "./itemCategories.js"; import { POTION_MAX_STACK } from "./alchemy.js"; /** Flower / dye palette (paint-style mixing). */ export const FlowerColorId = Object.freeze({ red: "red", blue: "blue", cyan: "cyan", violet: "violet", yellow: "yellow", magenta: "magenta", white: "white", black: "black", pink: "pink" }); export const FLOWER_COLOR_ORDER = Object.freeze([ FlowerColorId.red, FlowerColorId.blue, FlowerColorId.cyan, FlowerColorId.violet, FlowerColorId.yellow, FlowerColorId.magenta, FlowerColorId.white, FlowerColorId.black, FlowerColorId.pink ]); export const FLOWER_COLOR_RGB = Object.freeze({ [FlowerColorId.red]: rgb(220, 40, 40), [FlowerColorId.blue]: rgb(40, 80, 220), [FlowerColorId.cyan]: rgb(40, 200, 220), [FlowerColorId.violet]: rgb(140, 50, 200), [FlowerColorId.yellow]: rgb(240, 210, 40), [FlowerColorId.magenta]: rgb(220, 40, 180), [FlowerColorId.white]: rgb(245, 245, 250), [FlowerColorId.black]: rgb(28, 28, 32), [FlowerColorId.pink]: rgb(255, 140, 180) }); export const FLOWER_COLOR_DISPLAY = Object.freeze({ [FlowerColorId.red]: "Red", [FlowerColorId.blue]: "Blue", [FlowerColorId.cyan]: "Cyan", [FlowerColorId.violet]: "Violet", [FlowerColorId.yellow]: "Yellow", [FlowerColorId.magenta]: "Magenta", [FlowerColorId.white]: "White", [FlowerColorId.black]: "Black", [FlowerColorId.pink]: "Pink" }); /** Item IDs for flower petals and bottled dye (potion-look). */ export const DyeItemId = Object.freeze({ flower: 158, dye: 159 }); export const DEFAULT_DYE_FILL = 4; export function flowerColorRgb (colorId) { return FLOWER_COLOR_RGB[colorId] ?? FLOWER_COLOR_RGB[FlowerColorId.white]; } export function randomFlowerColorId (rng = Math.random) { const index = Math.floor(rng() * FLOWER_COLOR_ORDER.length) % FLOWER_COLOR_ORDER.length; return FLOWER_COLOR_ORDER[index]; } export function flowerItemInstance (colorId) { const id = FLOWER_COLOR_ORDER.includes(colorId) ? colorId : FlowerColorId.white; const color = flowerColorRgb(id); return Object.freeze({ kind: "flower", colorId: id, color: Object.freeze({ r: color.r, g: color.g, b: color.b }) }); } export function dyeItemInstance (color, fillLevel = DEFAULT_DYE_FILL, colorId = null) { const fill = Math.max(1, Math.min(5, fillLevel | 0 || DEFAULT_DYE_FILL)); return Object.freeze({ kind: "dye", colorId: colorId ?? null, color: Object.freeze({ r: Number(color?.r) || 0, g: Number(color?.g) || 0, b: Number(color?.b) || 0 }), fillLevel: fill }); } /** * Paint-style mix of two dye colours weighted by fill contribution. * Output bottle count = sum of inserted dye bottles. Total fill is poured * sequentially (fill to 5, overflow into the next bottle, …). */ export function mixDyeBatches (batches) { const rows = (batches ?? []).filter((row) => row?.instance?.kind === "dye"); if (rows.length < 2) { return { ok: false, reason: "Need two different dye colours to mix." }; } const byColor = new Map(); for (const row of rows) { const key = colorKey(row.instance.color); const qty = Math.max(1, row.quantity | 0); const fill = Math.max(1, Math.min(5, row.instance.fillLevel | 0 || DEFAULT_DYE_FILL)); const prev = byColor.get(key) ?? { color: row.instance.color, quantity: 0, fillUnits: 0 }; prev.quantity += qty; prev.fillUnits += qty * fill; byColor.set(key, prev); } if (byColor.size < 2) { return { ok: false, reason: "Need two different dye colours to mix." }; } if (byColor.size > 2) { return { ok: false, reason: "Mix exactly two dye colours at a time." }; } let totalQty = 0; let totalFill = 0; let mixR = 0; let mixG = 0; let mixB = 0; for (const entry of byColor.values()) { totalQty += entry.quantity; totalFill += entry.fillUnits; mixR += entry.color.r * entry.fillUnits; mixG += entry.color.g * entry.fillUnits; mixB += entry.color.b * entry.fillUnits; } if (totalFill <= 0 || totalQty <= 0) { return { ok: false, reason: "Nothing to mix." }; } const color = Object.freeze({ r: mixR / totalFill, g: mixG / totalFill, b: mixB / totalFill }); // Prefer ceil(fill/5) bottles when fill would overflow the input bottle count. const packedBottles = Math.max(totalQty, Math.ceil(totalFill / 5)); const outputs = []; let remaining = totalFill; for (let i = 0; i < packedBottles; i++) { const bottlesLeft = packedBottles - i; const fillLevel = Math.max(1, Math.min(5, remaining - (bottlesLeft - 1))); remaining -= fillLevel; outputs.push({ quantity: 1, instance: dyeItemInstance(color, fillLevel) }); } return { ok: true, outputs, color, quantity: packedBottles, extraBottles: Math.max(0, packedBottles - totalQty) }; } function colorKey (color) { return [ Math.round((color?.r ?? 0) * 1000), Math.round((color?.g ?? 0) * 1000), Math.round((color?.b ?? 0) * 1000) ].join(","); } export const FLOWER_ITEM_DEFINITION = Object.freeze({ itemId: DyeItemId.flower, key: "flower", displayName: "Flower", description: "A colourful flower. Brew into dye at a cauldron.", seedValue: 2, stackable: true, maxStackSize: 99, uniqueInstance: false, category: ItemCategory.ingredient, iconKey: "flower", defaultState: "flower", flower: true }); export const DYE_ITEM_DEFINITION = Object.freeze({ itemId: DyeItemId.dye, key: "dye", displayName: "Dye", description: "Bottled dye for cloth and clothing. Mix colours in a cauldron.", seedValue: 4, stackable: true, maxStackSize: POTION_MAX_STACK, uniqueInstance: false, category: ItemCategory.ingredient, iconKey: "potion", defaultState: "potion", dye: true });