BlueGit

timfa questward

Files

questward / shared / definitions / dyes.js

dyes.js

dyes.js 5.1 KiB Download Raw
1import { rgb } from "./materialTiers.js";
2import { ItemCategory } from "./itemCategories.js";
3
4/** Flower / dye palette (paint-style mixing). */
5export const FlowerColorId = Object.freeze({
6 red: "red",
7 blue: "blue",
8 cyan: "cyan",
9 violet: "violet",
10 yellow: "yellow",
11 magenta: "magenta",
12 white: "white",
13 black: "black",
14 pink: "pink"
15});
16
17export const FLOWER_COLOR_ORDER = Object.freeze([
18 FlowerColorId.red,
19 FlowerColorId.blue,
20 FlowerColorId.cyan,
21 FlowerColorId.violet,
22 FlowerColorId.yellow,
23 FlowerColorId.magenta,
24 FlowerColorId.white,
25 FlowerColorId.black,
26 FlowerColorId.pink
27]);
28
29export const FLOWER_COLOR_RGB = Object.freeze({
30 [FlowerColorId.red]: rgb(220, 40, 40),
31 [FlowerColorId.blue]: rgb(40, 80, 220),
32 [FlowerColorId.cyan]: rgb(40, 200, 220),
33 [FlowerColorId.violet]: rgb(140, 50, 200),
34 [FlowerColorId.yellow]: rgb(240, 210, 40),
35 [FlowerColorId.magenta]: rgb(220, 40, 180),
36 [FlowerColorId.white]: rgb(245, 245, 250),
37 [FlowerColorId.black]: rgb(28, 28, 32),
38 [FlowerColorId.pink]: rgb(255, 140, 180)
39});
40
41export const FLOWER_COLOR_DISPLAY = Object.freeze({
42 [FlowerColorId.red]: "Red",
43 [FlowerColorId.blue]: "Blue",
44 [FlowerColorId.cyan]: "Cyan",
45 [FlowerColorId.violet]: "Violet",
46 [FlowerColorId.yellow]: "Yellow",
47 [FlowerColorId.magenta]: "Magenta",
48 [FlowerColorId.white]: "White",
49 [FlowerColorId.black]: "Black",
50 [FlowerColorId.pink]: "Pink"
51});
52
53/** Item IDs for flower petals and bottled dye (potion-look). */
54export const DyeItemId = Object.freeze({
55 flower: 158,
56 dye: 159
57});
58
59export const DEFAULT_DYE_FILL = 4;
60
61export function flowerColorRgb (colorId)
62{
63 return FLOWER_COLOR_RGB[colorId] ?? FLOWER_COLOR_RGB[FlowerColorId.white];
64}
65
66export function randomFlowerColorId (rng = Math.random)
67{
68 const index = Math.floor(rng() * FLOWER_COLOR_ORDER.length) % FLOWER_COLOR_ORDER.length;
69 return FLOWER_COLOR_ORDER[index];
70}
71
72/** Block visual state for a flower colour (catalog tint alias). */
73export function flowerBlockState (colorId)
74{
75 return FLOWER_COLOR_ORDER.includes(colorId) ? colorId : FlowerColorId.white;
76}
77
78/** Colour id encoded in a flower block state, or null if unknown/legacy. */
79export function flowerColorIdFromBlockState (state)
80{
81 return FLOWER_COLOR_ORDER.includes(state) ? state : null;
82}
83
84export function flowerItemInstance (colorId)
85{
86 const id = FLOWER_COLOR_ORDER.includes(colorId) ? colorId : FlowerColorId.white;
87 const color = flowerColorRgb(id);
88 return Object.freeze({
89 kind: "flower",
90 colorId: id,
91 color: Object.freeze({ r: color.r, g: color.g, b: color.b })
92 });
93}
94
95export function dyeItemInstance (color, fillLevel = DEFAULT_DYE_FILL, colorId = null)
96{
97 const fill = Math.max(1, Math.min(5, fillLevel | 0 || DEFAULT_DYE_FILL));
98 return Object.freeze({
99 kind: "dye",
100 colorId: colorId ?? null,
101 color: Object.freeze({
102 r: Number(color?.r) || 0,
103 g: Number(color?.g) || 0,
104 b: Number(color?.b) || 0
105 }),
106 fillLevel: fill
107 });
108}
109
110/**
111 * Paint-style mix of flower colours weighted by quantity.
112 * Yields one dye bottle per flower (all bottles share the mixed colour).
113 */
114export function mixFlowersForDye (batches)
115{
116 const rows = (batches ?? []).filter((row) =>
117 row?.instance?.kind === "flower" && row.instance.color && (row.quantity | 0) > 0);
118 if (!rows.length)
119 { return { ok: false, reason: "Add flowers to make dye." }; }
120
121 let totalQty = 0;
122 let mixR = 0;
123 let mixG = 0;
124 let mixB = 0;
125 const colorIds = new Set();
126 for (const row of rows)
127 {
128 const qty = Math.max(1, row.quantity | 0);
129 totalQty += qty;
130 mixR += row.instance.color.r * qty;
131 mixG += row.instance.color.g * qty;
132 mixB += row.instance.color.b * qty;
133 if (row.instance.colorId)
134 { colorIds.add(row.instance.colorId); }
135 }
136 if (totalQty <= 0)
137 { return { ok: false, reason: "Add flowers to make dye." }; }
138
139 const color = Object.freeze({
140 r: mixR / totalQty,
141 g: mixG / totalQty,
142 b: mixB / totalQty
143 });
144 const colorId = colorIds.size === 1 ? [...colorIds][0] : null;
145 const instance = dyeItemInstance(color, DEFAULT_DYE_FILL, colorId);
146 return {
147 ok: true,
148 quantity: totalQty,
149 color,
150 instance,
151 outputs: Array.from({ length: totalQty }, () => ({
152 quantity: 1,
153 instance
154 }))
155 };
156}
157
158function colorKey (color)
159{
160 return [
161 Math.round((color?.r ?? 0) * 1000),
162 Math.round((color?.g ?? 0) * 1000),
163 Math.round((color?.b ?? 0) * 1000)
164 ].join(",");
165}
166
167/** Stack identity for dyes: exact RGB only (fill ignored). */
168export function dyeColorKey (instance)
169{
170 if (instance?.kind !== "dye" || !instance.color)
171 { return null; }
172 return colorKey(instance.color);
173}
174
175export const FLOWER_ITEM_DEFINITION = Object.freeze({
176 itemId: DyeItemId.flower,
177 key: "flower",
178 displayName: "Flower",
179 description: "A colourful flower. Mix into dye at a cauldron.",
180 seedValue: 2,
181 stackable: true,
182 maxStackSize: 99,
183 uniqueInstance: false,
184 category: ItemCategory.ingredient,
185 iconKey: "flower",
186 defaultState: "flower",
187 flower: true
188});
189
190export const DYE_ITEM_DEFINITION = Object.freeze({
191 itemId: DyeItemId.dye,
192 key: "dye",
193 displayName: "Dye",
194 description: "Bottled dye for cloth and clothing. Same colours stack up to 16.",
195 seedValue: 4,
196 stackable: true,
197 maxStackSize: 16,
198 uniqueInstance: false,
199 category: ItemCategory.ingredient,
200 iconKey: "potion",
201 defaultState: "potion",
202 dye: true
203});