This repository has been archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Generator.cs
795 lines (691 loc) · 36.3 KB
/
Generator.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/**
* Stellarator - Creates procedural systems for Kopernicus
* Copyright (c) 2016 Thomas P.
* Licensed under the Terms of the MIT License
*/
namespace Stellarator
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using Accrete;
using ConfigNodeParser;
using Database;
using DynamicExpresso;
using Kopernicus.Configuration;
using ProceduralQuadSphere;
using ProceduralQuadSphere.Unity;
using XnaGeometry;
using Color = ProceduralQuadSphere.Unity.Color;
public class Generator
{
//TODO: Refactor Generator.cs >> Thomas told me (MartinX3)
/// <summary>
/// The random number generator
/// </summary>
public static Random Random { get; private set; }
/// <summary>
/// The seed the system is using
/// </summary>
private static Int32 Seed { get; set; }
/// <summary>
/// The Kerbin equivalent for the system
/// </summary>
private static Planet Kerbin { get; set; }
/// <summary>
/// All planetary bodies in the system
/// </summary>
private static List<Planet> AllBodies { get; set; }
/// <summary>
/// This is the core of the whole app. It generates a solar system, based on a seed.
/// </summary>
public static void Generate(String seed, String folder, Boolean systematicNames)
{
// Log
Console.WriteLine("Generating the solar system...");
Console.WriteLine();
// Create a new Solar System using libaccrete
SolarSystem system = new SolarSystem(false, true, s => { });
SolarSystem.Generate(ref system, seed.GetHashCode(), Int32.MaxValue);
Seed = seed.GetHashCode();
// Reuse the random component
Random = system.random;
// All Nodes
List<ConfigNode> nodes = new List<ConfigNode>();
// Save the main config
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/systems/" + folder);
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/systems/" + folder + "/Configs");
Utility.Save(new ConfigNode("!Body,*"), "@Kopernicus:AFTER[KOPERNICUS]",
Directory.GetCurrentDirectory() + "/systems/" + folder + "/System.cfg",
String.Format(Templates.Header, seed));
// Sun
nodes.Add(GenerateSun(system, systematicNames));
// Select Kerbin
// Select Kerbin
List<Planet> allBodies = new List<Planet>();
FlattenSystem(system.Bodies, ref allBodies);
AllBodies = allBodies;
List<Planet> startBodies = AllBodies.Where(p => !p.gas_giant && (p.surface_pressure > 0)).SelectMany(x => (x.radius > 2500 ? new List<Planet>() { x, x, x } : new List<Planet>() { x })).ToList();
Kerbin = startBodies[Random.Next(0, startBodies.Count)];
// Define Roman Numerals and letters
const String moons = "abcdefghijklmnopqrstuvwxyz";
String[] rN =
{
"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
"XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX",
"XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX",
"XXXI", "XXXII", "XXXIII", "XXXIV", "XXXV", "XXXVI", "XXXVII", "XXXVIII", "XXXIX", "XL",
"XLI", "XLII", "XLIII", "XLIV", "XLV", "XLVI", "XLVII", "XLVIII", "XLIX", "L"
};
// Iterate over all bodies in the generated system
for (Int32 i = 0; i < system.Bodies.Length; i++)
{
ConfigNode node = GenerateBody(system[i], folder,
systematicName: systematicNames
? nodes[0].GetValue("cbNameLater") + " " + rN[i]
: null);
nodes.Add(node);
for (Int32 j = 0; j < system[i].BodiesOrbiting.Length; j++)
{
String name = node.HasValue("cbNameLater") ? node.GetValue("cbNameLater") : node.GetValue("name");
nodes.Add(GenerateBody(system[i][j], folder, node.GetValue("name"),
systematicNames ? name + moons[j] : null));
}
}
// Log
Console.WriteLine("Saving the system");
// Save the config
foreach (ConfigNode node in nodes)
{
String name = node.HasValue("cbNameLater") ? node.GetValue("cbNameLater") : node.GetValue("name");
Utility.Save(node, "@Kopernicus:AFTER[KOPERNICUS]",
Directory.GetCurrentDirectory() + "/systems/" + folder + "/Configs/" + name + ".cfg",
String.Format(Templates.Header, seed));
// Log
Console.WriteLine($"Saved {name}");
}
}
/// <summary>
/// Go over all bodies recuresively, add them to a flat list
/// </summary>
/// <param name="planets">List of planetary bodies</param>
/// <param name="list">Array of all planetary bodies, including all oribiting bodies</param>
private static void FlattenSystem(Planet[] planets, ref List<Planet> list)
{
if (planets == null)
{
return;
}
foreach (var planet in planets)
{
list.Add(planet);
FlattenSystem(planet.BodiesOrbiting, ref list);
}
}
/// <summary>
/// Creates the ConfigNode for the SunBody
/// </summary>
private static ConfigNode GenerateSun(SolarSystem system, Boolean systematicNames)
{
// Create the Body node
String name = Random.Next(0, 100) < (systematicNames ? 50 : 1)
? Utility.SystematicStarName()
: Utility.GenerateStarName();
ConfigNode node = new ConfigNode("Body");
node.AddValue("name", "Sun");
node.AddValue("cbNameLater", name);
// Template
ConfigNode template = new ConfigNode("Template");
node.AddConfigNode(template);
template.AddValue("name", "Sun");
// Properties
ConfigNode properties = new ConfigNode("Properties");
node.AddConfigNode(properties);
properties.AddValue("radius", "" + (system.stellar_radius_ratio * 261600000));
properties.AddValue("mass", "" + (system.stellar_mass_ratio * 1.75656696858329E+28));
properties.AddValue("timewarpAltitudeLimits",
String.Join(" ",
Templates.SunTimewarplimits.Select(i => (Int32) (i * system
.stellar_radius_ratio
))));
//properties.AddValue("useTheInName", "False");
// Log
Console.WriteLine($"Generated root body named {name}");
// Scaled Space
ConfigNode scaled = new ConfigNode("ScaledVersion");
node.AddConfigNode(scaled);
// Load database stuff
ConfigNode starDatabase = Utility.Load("stars");
ConfigNode data = starDatabase.GetNode(system.type.star_class);
data = Random.Choose(data.nodes);
StarPrefab star = Parser.CreateObjectFromConfigNode<StarPrefab>(data);
// Materials
ConfigNode mat = new ConfigNode("Material");
scaled.AddConfigNode(mat);
mat.AddValue("emitColor0", Parser.WriteColor(star.Material.EmitColor0));
mat.AddValue("emitColor1", Parser.WriteColor(star.Material.EmitColor1));
mat.AddValue("sunspotPower", "" + star.Material.SunspotPower);
mat.AddValue("sunspotColor", Parser.WriteColor(star.Material.SunspotColor));
mat.AddValue("rimColor", Parser.WriteColor(star.Material.RimColor));
mat.AddValue("rimPower", "" + star.Material.RimPower);
mat.AddValue("rimBlend", "" + star.Material.RimBlend);
// Light Node
ConfigNode light = new ConfigNode("Light");
scaled.AddConfigNode(light);
light.AddValue("sunlightColor", Parser.WriteColor(star.Light.SunlightColor));
light.AddValue("sunlightIntensity", "" + star.Light.SunlightIntensity);
light.AddValue("sunlightShadowStrength", "" + star.Light.SunlightShadowStrength);
light.AddValue("scaledSunlightColor", Parser.WriteColor(star.Light.ScaledSunlightColor));
light.AddValue("scaledSunlightIntensity", "" + star.Light.ScaledSunlightIntensity);
light.AddValue("IVASunColor", Parser.WriteColor(star.Light.IvaSunColor));
light.AddValue("IVASunIntensity", "" + star.Light.IvaSunIntensity);
light.AddValue("ambientLightColor", Parser.WriteColor(star.Light.AmbientLightColor));
light.AddValue("sunLensFlareColor", Parser.WriteColor(star.Light.SunLensFlareColor));
light.AddValue("givesOffLight", "" + star.Light.GivesOffLight);
light.AddValue("luminosity", "" + (system.stellar_luminosity_ratio * 1360));
// TODO: Coronas
// Log
Console.WriteLine($"Generated scaled space for {name}");
Console.WriteLine();
// Return it
return node;
}
/// <summary>
/// Generates the parameters for a planet
/// </summary>
private static ConfigNode GenerateBody(Planet planet, String folder, String referenceBody = "Sun",
String systematicName = null)
{
String name = systematicName ?? Utility.GenerateName();
ConfigNode node = new ConfigNode("Body");
// Name
if (planet != Kerbin)
{
node.AddValue("name", name);
}
else
{
node.AddValue("name", "Kerbin");
node.AddValue("cbNameLater", name);
}
// Random Main Menu Body
node.AddValue("randomMainMenuBody", "" + (referenceBody == "Sun"));
// Template
ConfigNode template = new ConfigNode("Template");
node.AddConfigNode(template);
if (planet != Kerbin)
{
if (planet.gas_giant)
{
template.AddValue("name", "Jool");
}
else
{
template.AddValue("name", Utility.GetTemplate(planet.surface_pressure > 0.00001, false));
template.AddValue("removeAllPQSMods", "True");
if (planet.surface_pressure <= 0.00001)
{
template.AddValue("removeAtmosphere", "True");
}
template.AddValue("removeOcean", "True");
// TODO: Handle atmospheres and ocean
}
}
else
{
template.AddValue("name", "Kerbin");
template.AddValue("removePQSMods",
"PQSLandControl, QuadEnhanceCoast, VertexHeightMap, VertexHeightNoiseVertHeightCurve2, VertexRidgedAltitudeCurve, VertexSimplexHeightAbsolute");
template.AddValue("removeAtmosphere", "True");
template.AddValue("removeOcean", "True");
}
// Properties
ConfigNode properties = new ConfigNode("Properties");
node.AddConfigNode(properties);
properties.AddValue("radius", "" + (planet.radius * 100));
properties.AddValue("geeASL", "" + planet.surface_grav);
properties.AddValue("rotationPeriod", "" + (planet.day * 60 * 60));
properties.AddValue("tidallyLocked", "" + planet.resonant_period);
properties.AddValue("initialRotation", "" + Random.Next(0, 361));
properties.AddValue("albedo", "" + planet.albedo);
properties.AddValue("timewarpAltitudeLimits",
String.Join(" ",
Templates.KerbinTimewarplimits.Select(i =>
(Int32) (i * ((planet.radius *
100) / 600000)))));
//properties.AddValue("useTheInName", "False");
// Log
Console.WriteLine($"Generated a planet named {name}. GasGiant: {planet.gas_giant}. Template: {template.GetValue("name")}");
// Color
Color planetColor = Utility.GenerateColor();
// Orbit
ConfigNode orbit = new ConfigNode("Orbit");
node.AddConfigNode(orbit);
orbit.AddValue("referenceBody", referenceBody);
orbit.AddValue("eccentricity", "" + planet.e);
orbit.AddValue("semiMajorAxis", "" + (planet.a * 100 * Constants.KM_PER_AU));
orbit.AddValue("longitudeOfAscendingNode", "" + Random.Range(0, 181));
orbit.AddValue("meanAnomalyAtEpochD", "" + Random.Range(0, 181));
if (planet.gas_giant)
{
orbit.AddValue("color", Parser.WriteColor(Utility.AlterColor(planetColor)));
}
// Inclination
orbit.AddValue("inclination", "" + Random.Range(-3, 5));
// Log
Console.WriteLine($"Generated orbit around {referenceBody} for {name}");
// Scaled Space
ConfigNode scaled = new ConfigNode("ScaledVersion");
node.AddConfigNode(scaled);
// Material
ConfigNode mat = new ConfigNode("Material");
scaled.AddConfigNode(mat);
if (!planet.gas_giant)
{
mat.AddValue("texture", folder + "/PluginData/" + name + "_Texture.png");
mat.AddValue("normals", folder + "/PluginData/" + name + "_Normals.png");
}
if (planet.gas_giant)
{
// Texture
String[] files = Directory.GetFiles(Directory.GetCurrentDirectory() + "/data/textures/");
String texture = Random.Choose(files);
String textureName = Path.GetFileNameWithoutExtension(texture) + ".png";
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/");
File.Copy(texture,
Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/" + textureName, true);
mat.AddValue("texture", folder + "/PluginData/" + textureName);
// Load the Image
Color average;
using (Bitmap image = new Bitmap(Directory.GetCurrentDirectory() + "/systems/" + folder +
"/PluginData/" +
textureName))
{
average = Utility.GetAverageColor(image);
}
// Scale
mat.AddValue("mainTexScale",
(Random.Next(0, 2) == 1 ? 1 : -1) + "," + (Random.Next(0, 2) == 1 ? 1 : -1));
mat.AddValue("color", Parser.WriteColor(Utility.ReColor(Utility.AlterColor(planetColor), average)));
ConfigNode gradient = new ConfigNode("Gradient");
mat.AddConfigNode(gradient);
gradient.AddValue("0.0", Parser.WriteColor(Utility.AlterColor(planetColor)));
gradient.AddValue("0.6", "0.0549,0.0784,0.141,1");
gradient.AddValue("1.0", "0.0196,0.0196,0.0196,1");
}
// Log
Console.WriteLine($"Generated scaled space for {name}");
// Atmosphere
if (planet.surface_pressure > 0.00001)
{
ConfigNode atmosphere = new ConfigNode("Atmosphere");
node.AddConfigNode(atmosphere);
atmosphere.AddValue("enabled", "True");
atmosphere.AddValue("oxygen", "" + Random.Boolean(10));
atmosphere.AddValue("atmosphereDepth", "" + (planet.radius * 100 * Random.Range(0.1, 0.16)));
atmosphere.AddValue("atmosphereMolarMass", "" + (planet.molecule_weight / 1000));
atmosphere.AddValue("staticPressureASL", "" + ((planet.surface_pressure / 1000) * 101.324996948242));
atmosphere.AddValue("temperatureSeaLevel", "" + planet.surface_temp);
if (planet.gas_giant)
{
atmosphere.AddValue("ambientColor", Parser.WriteColor(Utility.AlterColor(planetColor)));
atmosphere.AddValue("lightColor",
Parser.WriteColor(Utility.AlterColor(Utility.LightColor(planetColor))));
}
GenerateAtmosphereCurves(ref atmosphere, planet.gas_giant ? "Jool" : Utility.GetTemplate(true, true));
// Log
Console.WriteLine($"Generated atmosphere for {name}");
// Ocean
if (!planet.gas_giant)
{
if (planet.surface_temp < planet.boil_point && (Random.Next(0, 100) < 35))
{
ConfigNode ocean = new ConfigNode("Ocean");
node.AddConfigNode(ocean);
ocean.AddValue("density", "" + planet.hydrosphere * (Random.NextDouble() * 2));
ocean.AddValue("minLevel", "1");
ocean.AddValue("maxLevel", "6");
ocean.AddValue("minDetailDistance", "8");
ocean.AddValue("maxQuadLengthsPerFrame", "0.03");
Color waterColor = Utility.GenerateColor();
ocean.AddValue("oceanColor", Parser.WriteColor(waterColor));
ConfigNode oceanmat = new ConfigNode("Material");
ocean.AddConfigNode(oceanmat);
oceanmat.AddValue("colorFromSpace", Parser.WriteColor(waterColor));
oceanmat.AddValue("color", Parser.WriteColor(waterColor));
ConfigNode oceanfallbackmat = new ConfigNode("FallbackMaterial");
ocean.AddConfigNode(oceanfallbackmat);
oceanfallbackmat.AddValue("colorFromSpace", Parser.WriteColor(waterColor));
oceanfallbackmat.AddValue("color", Parser.WriteColor(waterColor));
Console.WriteLine($"Generated ocean for {name}");
}
}
}
// Rings :D
if (planet.gas_giant && (Random.Next(0, 100) < 5))
{
ConfigNode rings = new ConfigNode("Rings");
node.AddConfigNode(rings);
ConfigNode ringDatatbase = Utility.Load("rings");
ConfigNode data = Random.Choose(ringDatatbase.nodes);
RingPrefab def = Parser.CreateObjectFromConfigNode<RingPrefab>(data);
foreach (RingPrefab.Ring r in def.Rings)
{
ConfigNode ring = new ConfigNode("Ring");
rings.AddConfigNode(ring);
ring.AddValue("innerRadius", "" + (planet.radius * 0.1 * r.InnerRadius));
ring.AddValue("outerRadius", "" + (planet.radius * 0.1 * r.OuterRadius));
ring.AddValue("angle", "" + r.Angle);
ring.AddValue("color", Parser.WriteColor(Utility.AlterColor(planetColor)));
ring.AddValue("lockRotation", "" + r.LockRotation);
ring.AddValue("unlit", "False");
}
// Log
Console.WriteLine($"Generated rings around {name}");
}
// PQS
if (!planet.gas_giant)
{
Color average;
List<Color> biomes;
GeneratePQS(ref node, name, folder, planet, planetColor, out average, out biomes);
// Biomes
ConfigNode bio = new ConfigNode("Biomes");
properties.AddConfigNode(bio);
for (int i = 0; i < biomes.Count; i++)
{
ConfigNode biome = new ConfigNode("Biome");
bio.AddConfigNode(biome);
biome.AddValue("name", "Biome " + (i + 1));
biome.AddValue("value", "1");
biome.AddValue("color", Parser.WriteColor(biomes[i]));
}
Console.WriteLine($"Generated biomes for {name}");
// Apply colors
orbit.AddValue("color", Parser.WriteColor(Utility.AlterColor(average)));
if (node.HasNode("Atmosphere"))
{
ConfigNode atmosphere = node.GetNode("Atmosphere");
atmosphere.AddValue("ambientColor", Parser.WriteColor(Utility.AlterColor(average)));
atmosphere.AddValue("lightColor",
Parser.WriteColor(Utility.AlterColor(Utility.LightColor(average))));
}
ConfigNode gradient = new ConfigNode("Gradient");
mat.AddConfigNode(gradient);
gradient.AddValue("0.0", Parser.WriteColor(Utility.AlterColor(average)));
gradient.AddValue("0.6", "0.0549,0.0784,0.141,1");
gradient.AddValue("1.0", "0.0196,0.0196,0.0196,1");
}
// Log
Console.WriteLine($"Generation of body {name} finished!");
Console.WriteLine();
// Return
return node;
}
/// <summary>
/// Generate the science values for the given planet, based on planet stats
/// </summary>
/// <param name="planet"></param>
/// <param name="config"></param>
/// <returns></returns>
private static ConfigNode GenerateScienceValues(Planet planet, ConfigNode config)
{
if (planet == Kerbin)
{
return null;
}
ConfigNode scienceValues = new ConfigNode("ScienceValues");
scienceValues.AddValue("spaceAltitudeThreshold", "" + (int)(planet.radius * 100 / 2));
if (config.HasNode("Atmosphere"))
{
scienceValues.AddValue("flyingAltitudeThreshold", "" + (int)(double.Parse(config.GetNode("Atmosphere").GetValue("atmosphereDepth")) / 4));
}
double globalMultiplier = 5 + planet.Rank(AllBodies, Kerbin);
if (planet.surface_grav < 0.3)
{
globalMultiplier *= 0.85;
}
else if (planet.surface_grav < 0.7)
{
}
else if (planet.surface_grav < 1.3)
{
globalMultiplier *= 1.5;
}
else
{
globalMultiplier *= 2;
}
globalMultiplier += (0.5 * Math.Abs(double.Parse(config.GetNode("Orbit").GetValue("inclination"))));
globalMultiplier *= Math.Min(1.5, 1 + planet.e * 5);
scienceValues.AddValue("recoveryValue", "" + (int)globalMultiplier);
scienceValues.AddValue("inSpaceHighDataValue", "" + (int)globalMultiplier);
scienceValues.AddValue("inSpaceLowDataValue", "" + (int)(globalMultiplier + 1));
if (planet.gas_giant)
{
scienceValues.AddValue("flyingHighDataValue", "" + (int)(globalMultiplier + 2));
scienceValues.AddValue("flyingLowDataValue", "" + (int)(globalMultiplier + 3));
}
else
{
if (config.HasNode("Atmosphere"))
{
double altitude = double.Parse(config.GetNode("Atmosphere").GetValue("atmosphereDepth"));
double pressure = double.Parse(config.GetNode("Atmosphere").GetValue("staticPressureASL"));
globalMultiplier += Math.Min(4, (altitude / 100000d) * (pressure / 100d));
scienceValues.AddValue("flyingHighDataValue", "" + (int)globalMultiplier);
scienceValues.AddValue("flyingLowDataValue", "" + (int)globalMultiplier);
}
if (config.HasNode("Ocean"))
{
scienceValues.AddValue("splashedDataValue", "" + (int)(globalMultiplier + 2));
}
scienceValues.AddValue("landedDataValue", "" + (int)(globalMultiplier + 2));
}
return scienceValues;
}
/// <summary>
/// Adds static atmosphere curves
/// </summary>
/// <param name="atmosphere"></param>
/// <param name="template"></param>
private static void GenerateAtmosphereCurves(ref ConfigNode atmosphere, String template)
{
ConfigNode pressure =
ConfigNode.Load(Directory.GetCurrentDirectory() + "/data/curves/" + template + "Pressure.cfg");
pressure.name = "pressureCurve";
atmosphere.AddConfigNode(pressure);
atmosphere.AddValue("pressureCurveIsNormalized", "True");
ConfigNode temperature =
ConfigNode.Load(Directory.GetCurrentDirectory() + "/data/curves/" + template + "Temperature.cfg");
temperature.name = "temperatureCurve";
atmosphere.AddConfigNode(temperature);
atmosphere.AddValue("temperatureCurveIsNormalized", "True");
}
/// <summary>
/// Generates a PQS Setup + the Scaled Space Maps needed
/// </summary>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
private static void GeneratePQS(ref ConfigNode node, String name, String folder, Planet planet, Color planetColor,
out Color average, out List<Color> biomes)
{
// Log
Console.WriteLine("Preparing to load PQS data");
// Create the node
ConfigNode pqs = new ConfigNode("PQS");
// TODO: Material Settings?
// Create a node for the mods
ConfigNode mods = new ConfigNode("Mods");
pqs.AddConfigNode(mods);
// Load the PQSDatabase and select a setup
ConfigNode pqsDatabase = Utility.Load("pqs");
List<PQSPreset> data = pqsDatabase.nodes.Select(n => Parser.CreateObjectFromConfigNode<PQSPreset>(n))
.ToList();
data = data.Where(d => ((planet.radius * 100) > d.MinRadius) && ((planet.radius * 100) < d.MaxRadius))
.ToList();
PQSPreset setup = data[Random.Next(0, data.Count)];
// Setup the interpreter
Interpreter interpreter = new Interpreter()
.SetVariable("planet", planet, typeof(Planet))
.SetVariable("pqsVersion", setup, typeof(PQSPreset))
.SetVariable("Random", Random, typeof(Random))
.SetVariable("Seed", Seed, typeof(Int32))
.SetVariable("Color", Utility.GenerateColor(), typeof(Color))
.Reference(typeof(Parser))
.Reference(typeof(Generator))
.Reference(typeof(Utility))
.Reference(typeof(Utils));
// Transfer the mod nodes and evaluate expressions
foreach (ConfigNode modNode in setup.Mods.nodes)
{
mods.AddConfigNode(Utility.Eval(modNode, interpreter));
}
// Create a new PQSObject
PQS pqsVersion = new PQS(planet.radius * 100);
List<PQSMod> patchedmods = new List<PQSMod>();
// Log
Console.WriteLine($"Created PQS Object for {name}");
// Get all loaded types
List<Type> types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).ToList();
// Load mods from Config
foreach (ConfigNode mod in mods.nodes)
{
// get the mod type
if (types.Count(t => t.Name == mod.name) == 0)
{
continue;
}
Type loaderType = types.FirstOrDefault(t => t.Name == mod.name);
String testName = mod.name != "LandControl" ? "PQSMod_" + mod.name : "PQSLandControl";
Type modType = types.FirstOrDefault(t => t.Name == testName);
if ((loaderType == null) || (modType == null))
{
continue;
}
// Do any PQS mods already exist on this PQS matching this mod?
IEnumerable<PQSMod> existingmods = pqsVersion.mods.Where(m => m.GetType() == modType);
// Create the loader
Object loader = Activator.CreateInstance(loaderType);
// Reflection, because C# being silly... :/
MethodInfo createNew = loaderType.GetMethod("Create", new[] {typeof(PQS)});
MethodInfo create = loaderType.GetMethod("Create", new[] {modType});
IList<PQSMod> pqsmods = existingmods as IList<PQSMod> ?? existingmods.ToList();
if (pqsmods.Any())
{
// Attempt to find a PQS mod we can edit that we have not edited before
PQSMod existingMod = pqsmods.FirstOrDefault(m => !patchedmods.Contains(m) &&
(!mod.HasValue("name") ||
(mod.HasValue("index")
? (pqsmods.ToList().IndexOf(m) ==
Int32.Parse(mod.GetValue("index"))) &&
(m.name == mod.GetValue("name"))
: m.name == mod.GetValue("name"))));
if (existingMod != null)
{
create.Invoke(loader, new Object[] {existingMod});
Parser.LoadObjectFromConfigurationNode(loader, mod);
patchedmods.Add(existingMod);
}
else
{
createNew.Invoke(loader, new Object[] {pqsVersion});
Parser.LoadObjectFromConfigurationNode(loader, mod);
}
}
else
{
createNew.Invoke(loader, new Object[] {pqsVersion});
Parser.LoadObjectFromConfigurationNode(loader, mod);
}
// Log
Console.WriteLine($"Created a new instance of {loaderType.Name} on {name}");
}
// Size
Int32 width = pqsVersion.radius >= 600000 ? 4096 : pqsVersion.radius <= 100000 ? 1024 : 2048;
// Biome colors
int numBiomes = Random.Next(2, 10);
biomes = new List<Color>(numBiomes);
for (int i = 0; i < numBiomes; i++)
{
biomes.Add(Utility.GenerateColor());
}
// Export ScaledSpace Maps
using (UnsafeBitmap diffuse = new UnsafeBitmap(width, width / 2))
{
using (UnsafeBitmap height = new UnsafeBitmap(width, width / 2))
{
using (UnsafeBitmap biomeMap = new UnsafeBitmap(width, width / 2))
{
Console.WriteLine("Exporting Scaled Space maps from the PQS. This could take a while...");
// Iterate over the PQS
pqsVersion.SetupSphere();
diffuse.LockBitmap();
height.LockBitmap();
biomeMap.LockBitmap();
for (Int32 i = 0; i < width; i++)
{
for (Int32 j = 0; j < (width / 2); j++)
{
// Create a VertexBuildData
VertexBuildData builddata = new VertexBuildData
{
directionFromCenter =
Quaternion.CreateFromAngleAxis((360d / width) * i,
Vector3.Up) *
Quaternion
.CreateFromAngleAxis(90d - ((180d / (width / 2.0)) * j),
Vector3.Right) *
Vector3.Forward,
vertHeight = pqsVersion.radius
};
// Build the maps
pqsVersion.OnVertexBuildHeight(builddata);
pqsVersion.OnVertexBuild(builddata);
builddata.vertColor.a = 1f;
Single h = Mathf.Clamp01((Single)((builddata.vertHeight - pqsVersion.radius) *
(1d / pqsVersion.radiusMax)));
Single h1 = Mathf.Clamp01((Single)((builddata.vertHeight - pqsVersion.radius) *
(1d / (pqsVersion.radiusMax != 0 ? pqsVersion.radiusMax : planet.radius))));
diffuse.SetPixel(i, j, builddata.vertColor);
height.SetPixel(i, j, new Color(h, h, h));
biomeMap.SetPixel(i, j, biomes[(int)(h1 * (numBiomes - 1))]);
}
}
diffuse.UnlockBitmap();
height.UnlockBitmap();
biomeMap.UnlockBitmap();
// Save the textures
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/");
using (UnsafeBitmap normals = Utility.BumpToNormalMap(height, 9))
{
// TODO: Implement something to make strength dynamic
diffuse.Bitmap
.Save(Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/" + name + "_Texture.png",
ImageFormat.Png);
height.Bitmap
.Save(Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/" + name + "_Height.png",
ImageFormat.Png); // In case you need it :)
normals.Bitmap
.Save(Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/" + name + "_Normals.png",
ImageFormat.Png);
biomeMap.Bitmap
.Save(Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/" + name + "_Biomes.png",
ImageFormat.Png);
}
}
}
// Log
Console.WriteLine($"Saved maps to {Directory.GetCurrentDirectory() + "/systems/" + folder + "/PluginData/"}");
// Finish
node.AddConfigNode(pqs);
// Colors
average = Utility.GetAverageColor(diffuse.Bitmap);
}
}
}
}