import { rgb } from "./materialTiers.js"; import { ItemCategory } from "./itemCategories.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]; } /** Stable colour from world tile coords (legacy / uncoloured flower nodes). */ export function flowerColorIdForTile (tileX, tileY) { const x = tileX | 0; const y = tileY | 0; const hash = Math.imul(x, 73856093) ^ Math.imul(y, 19349663) ^ Math.imul(x ^ y, 83492791); return FLOWER_COLOR_ORDER[(hash >>> 0) % FLOWER_COLOR_ORDER.length]; } /** Block visual state for a flower colour (catalog tint alias). */ export function flowerBlockState (colorId) { return FLOWER_COLOR_ORDER.includes(colorId) ? colorId : FlowerColorId.white; } /** Colour id encoded in a flower block state, or null if unknown/legacy. */ export function flowerColorIdFromBlockState (state) { return FLOWER_COLOR_ORDER.includes(state) ? state : null; } 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 flower colours weighted by quantity. * Yields one dye bottle per flower (all bottles share the mixed colour). */ export function mixFlowersForDye (batches) { const rows = (batches ?? []).filter((row) => row?.instance?.kind === "flower" && row.instance.color && (row.quantity | 0) > 0); if (!rows.length) { return { ok: false, reason: "Add flowers to make dye." }; } let totalQty = 0; let mixR = 0; let mixG = 0; let mixB = 0; const colorIds = new Set(); for (const row of rows) { const qty = Math.max(1, row.quantity | 0); totalQty += qty; mixR += row.instance.color.r * qty; mixG += row.instance.color.g * qty; mixB += row.instance.color.b * qty; if (row.instance.colorId) { colorIds.add(row.instance.colorId); } } if (totalQty <= 0) { return { ok: false, reason: "Add flowers to make dye." }; } const color = Object.freeze({ r: mixR / totalQty, g: mixG / totalQty, b: mixB / totalQty }); const colorId = colorIds.size === 1 ? [...colorIds][0] : null; const instance = dyeItemInstance(color, DEFAULT_DYE_FILL, colorId); return { ok: true, quantity: totalQty, color, instance, outputs: Array.from({ length: totalQty }, () => ({ quantity: 1, instance })) }; } function colorKey (color) { return [ Math.round((color?.r ?? 0) * 1000), Math.round((color?.g ?? 0) * 1000), Math.round((color?.b ?? 0) * 1000) ].join(","); } /** Stack identity for dyes: exact RGB only (fill ignored). */ export function dyeColorKey (instance) { if (instance?.kind !== "dye" || !instance.color) { return null; } return colorKey(instance.color); } /** Flowers stack like potions; one cauldron dye slot cannot hold more than this. */ export const FLOWER_MAX_STACK = 16; export const FLOWER_ITEM_DEFINITION = Object.freeze({ itemId: DyeItemId.flower, key: "flower", displayName: "Flower", description: "A colourful flower. Crush into dye at a cauldron.", seedValue: 2, stackable: true, maxStackSize: FLOWER_MAX_STACK, 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 colour for cloth and clothing.", seedValue: 4, stackable: true, maxStackSize: 16, uniqueInstance: false, category: ItemCategory.ingredient, iconKey: "potion", defaultState: "potion", dye: true });