forked from annayudovin/PDN-ScrollGeneratorPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configs.cs
630 lines (506 loc) · 23.1 KB
/
Configs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace ScrollGeneratorEffect
{
internal static class Configs
{
public static readonly float rootLift = 12f;
public static readonly float childProportion = 0.75f;
public static readonly float unscaledMinRadius = 20f;
public static readonly float unscaledInitRadius = 50f;
public static readonly int unscaledNodeBuffer = 5; //pixel distance separating tree nodes
public static readonly int unscaledRootBuffer = 12; //distance between twin root node radii
public static readonly float unchangedSproutAngle = 5f / 4f * (float)Math.PI;
public static readonly float unchangedSproutMaxAngle = 11f / 12f * (float)Math.PI;
public static int maxNodes = 200;
public static float twinRatio = 1f;
public static float scale = 1f;
public static float minRadius = 20f;
public static float initRadius = 50f;
public static float nodeBuffer = 5; //pixel distance separating tree nodes
public static float rootBuffer = 12; //distance between twin root node radii
public static float nodeHalo = 0.05f; //angle distance separating tree nodes, similar function to nodeBuffer
public static int maxLeaves = 7;
public static float minRootBase = 0.01f;//more precise values initialized later
public static float startAt = 0.785f; //more precise values initialized later
public static float endAt = 0.785f; //more precise values initialized later
public static float rootAngle = 0f;
//used by DIVOPTION=2 to create a smooth/gradual growth pattern
public static readonly int upperLim = 100;
public static readonly int lowerLim = 20;
private static int availSlotSpaces = 6; //number of option fractions adjusted to fit, either 5 or 6
public static float sproutAngle = 5f / 4f * (float)Math.PI; //1.25*PI, or approx. 225 degrees
public static float sproutMaxAngle = (float)Math.PI;
public static float sproutAdjustment = 0;
public static float spreadFactor = 0;
public static float shiftFactor = 0;
public static bool LGMAX = false; //LargerMax interface option, controls availSlotSpaces
public static int DIVOPTION = 1; //fraction collection
public static bool TWIN = true; //root curl
public static bool RANDTWIN = false; //random twinRatio
public static bool RANDANG = false; //root curl angle
public static bool RANDLRG = false; //random large curl
public static bool MORERND = false; //extra guess for large curl
public static bool RANDSZ = false; //randomize all node sizes
public static bool RANDNUM = false; //number of leaves per node
public static bool GRAD = true; //graduated sizes
public static bool SMTOLG = true; //small-to-large
public static bool GROWTINY = false; //grow large leaves on too-small nodes
//mainly for graduated radius functionality
//also helps spacing "sparce" leaf configurations
public static List<int> useSlots = new();
public static List<float> slotSizes = new();
public static List<float> slotAngles = new();
public static List<float> slotRadMults = new();
public static bool LOG = false;
public static bool BigEnoughToGrow(float rad)
{
return rad >= 2 / 5 * minRadius || (GRAD && SMTOLG);
}
public static bool RadTooSmall(float rad)
{
return rad <= minRadius / 3;
}
public static bool CanDecreaseLeafArc(bool isTwin)
{
return (!isTwin || twinRatio == 1) && !RANDSZ && GRAD && SMTOLG && slotRadMults.Min() < 0.3;
}
public static List<float> CreateFractions(List<int> nums, List<int> denoms, float numAdd = 0f)
{
List<float> floatNums = nums.Select(x => (float)x).ToList();
return CreateFractions(floatNums, denoms, numAdd);
}
public static List<float> CreateFractions(List<float> nums, List<int> denoms, float numAdd = 0f)
{
if (nums.Count != denoms.Count || nums.Count < 2) { return new List<float> { }; }
int fractCnt = availSlotSpaces;
if (DIVOPTION == 1) { fractCnt = availSlotSpaces + 1; }
if (numAdd >= nums.Min()) { numAdd = 0; }
List<float> fracts = new();
for (int idx = 0; idx < fractCnt; idx++)
{
float adjNum = nums[idx] - numAdd;
float adjFract = adjNum / denoms[idx];
fracts.Add(adjFract);
}
return fracts;
}
public static Tuple<List<float>, List<float>, List<float>> EvenSplit()
{
float baseFract = ((float)Math.Tau - endAt - ((float)Math.PI / 8f) - nodeHalo + spreadFactor) / availSlotSpaces;
float radMult = (float)Math.Sin(baseFract / 2f) / (1 - (float)Math.Sin(baseFract / 2f));
//maximum number of these that can fit at !LGMAX is 5
//and 4 at LGMAX. Reducing the size looks worse
//adjusting slot start angles instead
List<float> radMults = Enumerable.Repeat(radMult, availSlotSpaces - 1).ToList();
List<float> slotAngls;
List<float> slotSizes;
if (LGMAX)
{
float slotSize = 1.071f; //experimentally determined
slotSizes = Enumerable.Repeat(slotSize, 4).ToList();
slotAngls = new List<float> { 1.262f, 2.333f, 3.404f, 4.475f }; //experimentally determined
}
else
{
float slotSize = 0.897f; //experimentally determined
slotSizes = Enumerable.Repeat(slotSize, 5).ToList();
slotAngls = new List<float> { 1.153f, 2.051f, 2.948f, 3.846f, 4.744f }; //experimentally determined
}
if (spreadFactor != 0)
{
float sltTotl = slotAngls.Sum();
float desiredTotl = sltTotl + spreadFactor;
float adjustment = desiredTotl / sltTotl;
List<float> adjustedSlotAngls = slotAngls.Select(x => x * adjustment).ToList();
slotAngls = adjustedSlotAngls;
List<float> adjustedSlotSizes = slotSizes.Select(x => x * adjustment).ToList();
slotSizes = adjustedSlotSizes;
}
return Tuple.Create(radMults, slotSizes, slotAngls);
}
public static int FindNextSlot(float tstAngl)
{
int leafSlot = 0;
for (int idx = slotAngles.Count - 1; idx > 0; idx--)
{
float slotAngl = slotAngles[idx];
if (tstAngl > slotAngl)
{
leafSlot = idx;
break;
}
}
return leafSlot;
}
public static float GetGoodRandomRad(float parentRad, Random? rnd = null)
{
rnd ??= new Random();
int localMax = Math.Max(maxLeaves, 3);
float rad = 0;
float max = slotRadMults.Max();
float min = slotRadMults.Min();
for (int tr = 0; tr < localMax; tr++)
{
rad = parentRad * Random(min, max, rnd);
if (Math.Round(rad) > minRadius / 2) { break; }
}
return rad;
}
public static float GetGoodRandomRad(float parentRad, int slot, Random? rnd = null)
{
rnd ??= new Random(); //if rnd is null, create a new Random object
bool goHigh = false;
if (useSlots.Count == 2)
{
if (slot == useSlots.Min()) { goHigh = true; }
}
else if (useSlots.Count % 2 == 1) //odd number of slots in use
{
if (slot % 2 == 1) { goHigh = true; }
}
else
{
if (slot % 2 == 0) { goHigh = true; }
}
int end = 0;
int mid = slotRadMults.Count / 2;
if (goHigh)
{
end = slotRadMults.Count - 1; //this is the important bit
int coin = rnd.Next(1, 2);
mid -= coin % 2;
if (coin % 2 == 0) { parentRad = Math.Max(parentRad, initRadius); }
}
else
{
int coin = rnd.Next(1, 2);
mid += coin % 2;
}
int sizeSlot = rnd.Next(Math.Min(end, mid), Math.Max(end, mid));
float rad = parentRad * slotRadMults[sizeSlot];
if (Math.Round(rad) < minRadius / 2) { return GetGoodRandomRad(parentRad, rnd); }
//else:
return rad;
}
public static List<int> GetSlotList(int take)
{
int numSlots = slotSizes.Count;
List<int> slotLst;
if (take == 2)
{
if (!GRAD) { return LGMAX ? new List<int> { 2, 3 } : new List<int> { 2, 4 }; }
if (LGMAX) { return SMTOLG ? new List<int> { 1, 2 } : new List<int> { 2, 3 }; }
else
{
if (SMTOLG) { return new List<int> { 1, 3 }; }
else { return DIVOPTION == 1 ? new List<int> { 3, 5 } : new List<int> { 2, 4 }; }
}
}
//if !GRAD, the maximum number of slots is 5, and take==2 case is already covered
if (!GRAD) { return Enumerable.Range(1, take).ToList(); }
//for DIVOPTION != 1, aiming the following pattern as the number of leaves decreases:
//remove smallest slot, remove largest slot, remove smallest slot
//for DIVOPTION == 1, the pattern is to remove the smallest two slots first,
//then the largest, then the smallest again, as the number of leaves decreases
if (LGMAX) { numSlots += 1; }
if (!SMTOLG) { numSlots += 1; }
if (DIVOPTION == 1)
{
//in effect, when DIVOPTION == 1
//numSlots = 6 for SMTOLG, and numSlots = 9 for !SMTOLG
if (!SMTOLG) { numSlots += 1; }
else { numSlots -= 1; }
}
int strt = (numSlots - take) / 2;
slotLst = Enumerable.Range(strt, take).ToList();
return slotLst;
}
/*initializers and initializer utility methods--------------------------------------------------*/
public static void InitAll(Random? rnd = null)
{
rnd ??= new Random(); //if rnd is null, create a new Random object
twinRatio = RANDTWIN || RANDSZ ? Random(0.6f, 1f, rnd) : twinRatio;
availSlotSpaces = LGMAX ? 5 : 6;
sproutAngle = unchangedSproutAngle + sproutAdjustment;
sproutMaxAngle = unchangedSproutMaxAngle + sproutAdjustment;
minRadius = unscaledMinRadius * scale;
initRadius = unscaledInitRadius * scale;
nodeBuffer = unscaledNodeBuffer * scale;
rootBuffer = unscaledRootBuffer * scale;
endAt = (float)Math.PI / 4f; //initialize global values for start and end arcs
InitStartAt();
if (RANDNUM) { maxLeaves = 8; } //establish independence of these options, regardless of interface
if (maxLeaves < 2) { maxLeaves = 2; } //otherwise no hope of growing much of a "tree"
InitSegments();
}
public static void InitSegments()
{
//fibNums 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
//triangleNums 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136,
//primeQuads {5, 7, 11, 13}, {11, 13, 17, 19}
List<int> triangleNums = new() { 10, 15, 21, 28, 36, 45, 55, 66 };
List<int> fibNums = new() { 5, 8, 13, 21, 34, 55, 89, 144 };
List<int> fibNums2 = new() { 13, 21, 34, 55, 89, 144, 233, 377 };
List<float> fracts = new();
if (DIVOPTION == 1)
{ fracts = CreateFractions(Enumerable.Range(5, 8).ToList(), triangleNums); }
//"even" growth rate using globals upperLim & lowerLim - unevenSplit() takes it from here
if (DIVOPTION == 2) { fracts = ModeratedFractions(); }
if (DIVOPTION == 3)
{ fracts = CreateFractions(Enumerable.Range(1, 8).ToList(), fibNums); }
if (DIVOPTION == 4)
{
fracts = CreateFractions(Enumerable.Repeat(1, 8).ToList(),
Enumerable.Range(4, 8).ToList());
}
if (DIVOPTION == 5)
{
List<float> inverses = CreateFractions(fibNums, Enumerable.Range(3, 8).ToList());
while (fibNums2.Count > inverses.Count) { fibNums2.RemoveAt(fibNums2.Count - 1); }
fracts = CreateFractions(inverses, fibNums2);
}
Tuple<List<float>, List<float>, List<float>> results = !GRAD && !RANDSZ ? EvenSplit() : UnevenSplit(fracts);
slotRadMults = results.Item1;
slotSizes = results.Item2;
slotAngles = results.Item3;
slotRadMults.Sort();
slotSizes.Sort();
slotAngles.Sort();
AdjustSlotAngles();
if (GRAD && SMTOLG)
{
slotRadMults.Reverse();
slotSizes.Reverse();
}
//initialize list of slots to use when number of leaves varies
int maxSlots = slotSizes.Count;
if (maxLeaves < maxSlots) { useSlots = GetSlotList(maxLeaves); }
else { useSlots = Enumerable.Range(0, maxSlots).ToList(); } //initialize it to all available slots
useSlots.Reverse();
}
public static void AdjustSlotAngles()
{
if (!GRAD) { return; }
float adjstr = (float)Math.Tau - slotAngles.Max();
if (SMTOLG || !TWIN)
{
float minLeafArc = 0.1f + (float)Math.Asin(slotRadMults.Min() / (1 + slotRadMults.Min()));
adjstr -= 1.5f * minLeafArc;
}
else { adjstr -= 1.5f * slotSizes.Min(); }
List<float> adjustedSlotAngls = slotAngles.Select(x => x + adjstr).ToList();
slotAngles = adjustedSlotAngls;
}
public static void InitStartAt()
{
if (!TWIN) { startAt = 0.1f; }
else
{
if (GRAD)
{
if (SMTOLG && !LGMAX) { startAt = 0.1f; }
if (SMTOLG && LGMAX) { startAt = 0; }
}
else
{ startAt = (float)Math.Tau / 6f; }
}
}
public static string LogProperties()
{
string lg = "\n---------\n";
lg += $"maxNodes: {maxNodes}; ";
lg += $"maxLeaves: {maxLeaves}; ";
lg += $"RANDNUM: {RANDNUM}; \n";
lg += $"initRadius: {initRadius:F3}; ";
lg += $"minRadius: {minRadius:F3}; ";
lg += $"childProportion: {childProportion:F3}; ";
lg += $"sproutAdjustment: {sproutAdjustment:F3}; ";
lg += $"spreadFactor: {spreadFactor:F3}; ";
lg += $"shiftFactor: {shiftFactor:F3}; ";
lg += $"startAt: {startAt:F3}; ";
lg += $"endAt: {endAt:F3}; \n";
lg += $"TWIN: {TWIN}; ";
lg += $"twinRatio: {twinRatio:F3}; ";
lg += $"rootAngle: {rootAngle:F3}; ";
lg += $"RANDANG: {RANDANG}; ";
lg += $"RANDLRG: {RANDLRG}; ";
lg += $"RANDSZ: {RANDSZ}; \n";
lg += $"GRAD: {GRAD}; ";
lg += $"SMTOLG: {SMTOLG}; ";
lg += $"LGMAX: {LGMAX}; ";
lg += $"DIVOPTION #{DIVOPTION} ";
lg += $"GROWTINY: {GROWTINY}; \n";
lg += "---Slot Radius Multipliers:\n";
lg += string.Join(", ", slotRadMults.Select(x => x.ToString("F3")));
lg += "\n---Slot Sizes:\n";
lg += string.Join(", ", slotSizes.Select(x => x.ToString("F3")));
lg += "\n---Slot Angles:\n";
lg += string.Join(", ", slotAngles.Select(x => x.ToString("F3")));
lg += "\n---Slots In Use:\n";
lg += string.Join(", ", useSlots);
lg += "\n\n";
return lg;
}
public static List<float> ModeratedFractions()
{
int localMax = availSlotSpaces;
List<float> fracts = new();
float baseFract = ((float)Math.Tau - (1.5f * endAt)) / localMax;
float totlGrowth = Math.Abs(upperLim - lowerLim);
float fractGrowth = 0.01f * totlGrowth / localMax; //convert percent to fraction
for (int idx = 0; idx < localMax; idx++)
{
fracts.Add(baseFract);
baseFract += fractGrowth;
}
return fracts;
}
public static float NextLeafRad(float parentRad, int leafSlot = -1, Random? rnd = null)
{
rnd ??= new Random();
if (leafSlot == -1) //leafSlot matters to options below
{
return RANDSZ ? GetGoodRandomRad(parentRad, rnd) : parentRad * childProportion;
}
if (RANDLRG)
{
int lrgSlot = rnd.Next(0, slotSizes.Count); //guess current slot
int coin = 1;
//reduce the frequency of extra-large nodes when leaf number is large
if (slotSizes.Count > 3){ coin = rnd.Next(0, 2); } //or flip a coin
if (lrgSlot == leafSlot && coin > 0){ return Random(initRadius * 0.5f, initRadius * 0.9f, rnd); }
//else: if didn't guess, proceed to other options as normally
}
if (RANDSZ)
{
return GRAD ? GetGoodRandomRad(parentRad, leafSlot, rnd) : GetGoodRandomRad(parentRad, rnd);
}
//else:
return parentRad * slotRadMults[leafSlot];
}
//tree/node helper methods--------------------------------------------------------------
public static int NumLeaves(Random? rnd = null) //only used with RANDNUM
{
rnd ??= new Random(); //if rnd is null, create a new Random object
int maxSlots = availSlotSpaces;
if (DIVOPTION == 1) { maxSlots = availSlotSpaces + 1; }
return rnd.Next(2, maxSlots);
}
public static List<float> ProportionFractions(List<float> fracts)
{
// add up all fracts
// take ratio of total to desired proportion of whole
// multiply each fract by INVERSE of this ratio
float frTotl = fracts.Sum();
// 1.5 * Math.PI/4 = 1.178; Math.PI/4 + Math.PI/6 = 1.309
//Math.PI/6 is greater than min root base in all cases
float desiredTotl = (float)Math.Tau - endAt - (float)Math.PI / 6f;
if (SMTOLG)
{
if (DIVOPTION == 1) { desiredTotl -= 0.15f; }
if (DIVOPTION == 2) { desiredTotl -= 0.25f; }
if (DIVOPTION == 3 && !LGMAX) { desiredTotl -= 0.35f; }
if (DIVOPTION == 4)
{
if (!LGMAX) { desiredTotl -= 0.2f; }
else { desiredTotl -= 0.1f; }
}
if (DIVOPTION == 5) { desiredTotl -= 0.075f; }
}
else
{
if (DIVOPTION == 1)
{
if (!LGMAX) { desiredTotl += 0.1f; }
else { desiredTotl += 0.225f; }
}
if (DIVOPTION == 2)
{
if (!LGMAX) { desiredTotl += 0.03f; }
else { desiredTotl -= 0.17f; }
}
if (DIVOPTION == 3)
{
if (!LGMAX) { desiredTotl -= 0.23f; }
else { desiredTotl -= 0.17f; }
}
if (DIVOPTION >= 4) { desiredTotl -= 0.125f; }
}
int extra = 1;
if (LGMAX) { extra = 2; }
desiredTotl += spreadFactor;
float adjustment = (desiredTotl - (fracts.Count * extra * nodeHalo)) / frTotl;
List<float> newFracts = fracts.Select(x => x * adjustment).ToList();
return newFracts;
}
public static float Random(float min = 0f, float max = 0f, Random? rnd = null)
{
rnd ??= new Random(); //if rnd is null, create a new Random object
float _max = (float)Math.Max(max, min);
float _min = (float)Math.Min(max, min);
return ((float)rnd.NextDouble() * (_max - _min)) + _min;
}
//also updates minRootBase
public static float RootBaseArc(int rootIdx, float rootDist) //returns relative arc angle
{
float rootBase = startAt;
minRootBase = 0.01f;
if (TWIN)
{
float otherRootRad = initRadius;
if (rootIdx == 0) { otherRootRad = initRadius * twinRatio; }
if (GRAD && !RANDSZ)
{
float minMult = slotRadMults.Min();
float minLeaf = otherRootRad * minMult;
minRootBase = (float)Math.Asin(otherRootRad / rootDist);
if (SMTOLG)
{
float leafArc = (float)Math.Asin(minLeaf / (otherRootRad + minLeaf));
rootBase += leafArc;
minRootBase = rootBase;
}
else
{
if (minMult > 0.33) { minLeaf += nodeBuffer; }
rootBase = (float)Math.Asin((otherRootRad + minLeaf) / rootDist);
}
}
}
minRootBase = 0.5f * minRootBase - 2 * nodeHalo;
return rootBase;
}
public static Tuple<List<float>, List<float>, List<float>> UnevenSplit(List<float> fracts)
{
if (fracts.Count < 2) { return Tuple.Create(new List<float> { }, new List<float> { }, new List<float> { }); }
List<float> adjFracts = ProportionFractions(fracts);
adjFracts.Sort();
List<float> spreadFracts = new();
List<float> radMults = new();
foreach (float adjFract in adjFracts)
{
float radMult = (float)Math.Sin(adjFract / 2f) / (1f - (float)Math.Sin(adjFract / 2f));
radMults.Add(radMult);
spreadFracts.Add(adjFract + nodeHalo);
}
spreadFracts.Sort();
adjFracts.Sort();
if (SMTOLG)
{
spreadFracts.Reverse();
adjFracts.Reverse();
}
List<float> fractAngls = new();
float totl = 0;
foreach (float adjFract in adjFracts)
{
totl += adjFract;
fractAngls.Add(totl);
}
return Tuple.Create(radMults, spreadFracts, fractAngls);
}
}
}