BlueGit

timfa questward

Files

questward / shared / definitions / dyes.js

dyes.js

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