BlueGit

timfa questward

Files

questward / shared / definitions / dyes.js

dyes.js

dyes.js 5.6 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/** Stable colour from world tile coords (legacy / uncoloured flower nodes). */
73export function flowerColorIdForTile (tileX, tileY)
74{
75 const x = tileX | 0;
76 const y = tileY | 0;
77 const hash = Math.imul(x, 73856093) ^ Math.imul(y, 19349663) ^ Math.imul(x ^ y, 83492791);
78 return FLOWER_COLOR_ORDER[(hash >>> 0) % FLOWER_COLOR_ORDER.length];
79}
80
81/** Block visual state for a flower colour (catalog tint alias). */
82export function flowerBlockState (colorId)
83{
84 return FLOWER_COLOR_ORDER.includes(colorId) ? colorId : FlowerColorId.white;
85}
86
87/** Colour id encoded in a flower block state, or null if unknown/legacy. */
88export function flowerColorIdFromBlockState (state)
89{
90 return FLOWER_COLOR_ORDER.includes(state) ? state : null;
91}
92
93export function flowerItemInstance (colorId)
94{
95 const id = FLOWER_COLOR_ORDER.includes(colorId) ? colorId : FlowerColorId.white;
96 const color = flowerColorRgb(id);
97 return Object.freeze({
98 kind: "flower",
99 colorId: id,
100 color: Object.freeze({ r: color.r, g: color.g, b: color.b })
101 });
102}
103
104export function dyeItemInstance (color, fillLevel = DEFAULT_DYE_FILL, colorId = null)
105{
106 const fill = Math.max(1, Math.min(5, fillLevel | 0 || DEFAULT_DYE_FILL));
107 return Object.freeze({
108 kind: "dye",
109 colorId: colorId ?? null,
110 color: Object.freeze({
111 r: Number(color?.r) || 0,
112 g: Number(color?.g) || 0,
113 b: Number(color?.b) || 0
114 }),
115 fillLevel: fill
116 });
117}
118
119/**
120 * Paint-style mix of flower colours weighted by quantity.
121 * Yields one dye bottle per flower (all bottles share the mixed colour).
122 */
123export function mixFlowersForDye (batches)
124{
125 const rows = (batches ?? []).filter((row) =>
126 row?.instance?.kind === "flower" && row.instance.color && (row.quantity | 0) > 0);
127 if (!rows.length)
128 { return { ok: false, reason: "Add flowers to make dye." }; }
129
130 let totalQty = 0;
131 let mixR = 0;
132 let mixG = 0;
133 let mixB = 0;
134 const colorIds = new Set();
135 for (const row of rows)
136 {
137 const qty = Math.max(1, row.quantity | 0);
138 totalQty += qty;
139 mixR += row.instance.color.r * qty;
140 mixG += row.instance.color.g * qty;
141 mixB += row.instance.color.b * qty;
142 if (row.instance.colorId)
143 { colorIds.add(row.instance.colorId); }
144 }
145 if (totalQty <= 0)
146 { return { ok: false, reason: "Add flowers to make dye." }; }
147
148 const color = Object.freeze({
149 r: mixR / totalQty,
150 g: mixG / totalQty,
151 b: mixB / totalQty
152 });
153 const colorId = colorIds.size === 1 ? [...colorIds][0] : null;
154 const instance = dyeItemInstance(color, DEFAULT_DYE_FILL, colorId);
155 return {
156 ok: true,
157 quantity: totalQty,
158 color,
159 instance,
160 outputs: Array.from({ length: totalQty }, () => ({
161 quantity: 1,
162 instance
163 }))
164 };
165}
166
167function colorKey (color)
168{
169 return [
170 Math.round((color?.r ?? 0) * 1000),
171 Math.round((color?.g ?? 0) * 1000),
172 Math.round((color?.b ?? 0) * 1000)
173 ].join(",");
174}
175
176/** Stack identity for dyes: exact RGB only (fill ignored). */
177export function dyeColorKey (instance)
178{
179 if (instance?.kind !== "dye" || !instance.color)
180 { return null; }
181 return colorKey(instance.color);
182}
183
184/** Flowers stack like potions; one cauldron dye slot cannot hold more than this. */
185export const FLOWER_MAX_STACK = 16;
186
187export const FLOWER_ITEM_DEFINITION = Object.freeze({
188 itemId: DyeItemId.flower,
189 key: "flower",
190 displayName: "Flower",
191 description: "A colourful flower. Crush into dye at a cauldron.",
192 seedValue: 2,
193 stackable: true,
194 maxStackSize: FLOWER_MAX_STACK,
195 uniqueInstance: false,
196 category: ItemCategory.ingredient,
197 iconKey: "flower",
198 defaultState: "flower",
199 flower: true
200});
201
202export const DYE_ITEM_DEFINITION = Object.freeze({
203 itemId: DyeItemId.dye,
204 key: "dye",
205 displayName: "Dye",
206 description: "Bottled colour for cloth and clothing.",
207 seedValue: 4,
208 stackable: true,
209 maxStackSize: 16,
210 uniqueInstance: false,
211 category: ItemCategory.ingredient,
212 iconKey: "potion",
213 defaultState: "potion",
214 dye: true
215});