-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlanetRandomizer.cs
277 lines (250 loc) · 13.5 KB
/
PlanetRandomizer.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
using OWML.Utils;
using PacificEngine.OW_CommonResources.Game.Component.Detector;
using PacificEngine.OW_CommonResources.Game;
using PacificEngine.OW_CommonResources.Game.Config;
using PacificEngine.OW_CommonResources.Game.Resource;
using PacificEngine.OW_CommonResources.Game.State;
using PacificEngine.OW_CommonResources.Geometry;
using PacificEngine.OW_CommonResources.Geometry.Orbits;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace PacificEngine.OW_Randomizer
{
public class PlanetRandomizer : AbstractRandomizer
{
private static bool hasUpdate = false;
public static PlanetRandomizer instance { get; } = new PlanetRandomizer();
public new void updateSeed(int seed, RandomizerSeeds.Type type)
{
base.updateSeed(seed, type);
}
public override void Start()
{
Planet.enabledManagement = true;
}
public override void Awake()
{
hasUpdate = false;
Tracker.getTracked(HeavenlyBodies.Player).onAstroUpdateEvent += onParentUpdate;
Tracker.getTracked(HeavenlyBodies.Ship).onAstroUpdateEvent += onParentUpdate;
Tracker.getTracked(HeavenlyBodies.Probe).onAstroUpdateEvent += onParentUpdate;
Tracker.getTracked(HeavenlyBodies.ModelShip).onAstroUpdateEvent += onParentUpdate;
Tracker.getTracked(HeavenlyBodies.NomaiEmberTwinShuttle).onAstroUpdateEvent += onParentUpdate;
Tracker.getTracked(HeavenlyBodies.NomaiBrittleHollowShuttle).onAstroUpdateEvent += onParentUpdate;
}
public override void Update()
{
if (!hasUpdate && GameTimer.FramesSinceAwake > 2)
{
Reset();
hasUpdate = true;
}
base.Update();
}
public override void FixedUpdate()
{
}
protected override void defaultValues()
{
var mapping = Planet.defaultMapping;
Planet.mapping = mapping;
fixMappingSatlite(mapping);
}
protected override void randomizeValues()
{
var defaultMapping = Planet.defaultMapping;
var originalMapping = Planet.mapping;
var mapping = new Dictionary<HeavenlyBody, Planet.Plantoid>();
foreach (var planet in defaultMapping)
{
var currentValue = originalMapping.ContainsKey(planet.Key) ? originalMapping[planet.Key] : null;
var newPlanet = randomPlantoid(defaultMapping, originalMapping, mapping, planet.Key, currentValue, planet.Value);
mapping.Add(planet.Key, newPlanet);
}
Planet.mapping = mapping;
fixMappingSatlite(mapping);
}
private void fixMappingSatlite(Dictionary<HeavenlyBody, Planet.Plantoid> mapping)
{
var body = Position.getBody(HeavenlyBodies.SatiliteMapping);
if (body != null && mapping.ContainsKey(HeavenlyBodies.SatiliteMapping))
{
var mappingValue = mapping[HeavenlyBodies.SatiliteMapping];
foreach(var detector in GravityDetector.getForceDetectors<ForceDetector>(body.gameObject))
{
GameObject.Destroy(detector);
}
GravityDetector.registerConstantForceDetector(body, new HeavenlyBody[] { mappingValue?.state?.parent });
}
}
private Planet.Plantoid randomPlantoid(Dictionary<HeavenlyBody, Planet.Plantoid> defaultMapping, Dictionary<HeavenlyBody, Planet.Plantoid> originalMapping, Dictionary<HeavenlyBody, Planet.Plantoid> newMapping, HeavenlyBody body, Planet.Plantoid currentValue, Planet.Plantoid defaultValue)
{
if (currentValue == null)
{
return null;
}
Planet.Plantoid parent = originalMapping.ContainsKey(currentValue.state.parent) ? originalMapping[currentValue.state.parent] : null;
if (parent == null)
{
parent = defaultMapping.ContainsKey(currentValue.state.parent) ? defaultMapping[currentValue.state.parent] : null;
}
if (body == HeavenlyBodies.GiantsDeep)
{
// Giants Deep Island's majorly glitch out if it isn't Facing Up with no rotational velocity
return new Planet.Plantoid(currentValue.size, currentValue.gravity, currentValue.state?.orbit?.rotation ?? currentValue.state.relative.rotation, currentValue.state?.orbit?.angularVelocity.magnitude ?? currentValue.state.relative.angularVelocity.magnitude, currentValue.state.parent, randomKepler(parent, body, currentValue));
}
else if (body == HeavenlyBodies.SunStation
|| body == HeavenlyBodies.HourglassTwins
|| body == HeavenlyBodies.Attlerock
|| body == HeavenlyBodies.Interloper
|| body == HeavenlyBodies.TimberHearthProbe)
{
// Because of AlignWithTargetBody
return new Planet.Plantoid(currentValue.size, currentValue.gravity, currentValue.state?.orbit?.rotation ?? currentValue.state.relative.rotation, 0f, currentValue.state.parent, randomKepler(parent, body, currentValue));
}
else if (body == HeavenlyBodies.ProbeCannon)
{
// It unspawns if too far from GiantsDeep
return new Planet.Plantoid(currentValue.size, currentValue.gravity, currentValue.state?.orbit?.rotation ?? currentValue.state.relative.rotation, 0f, currentValue.state.parent, randomKepler(parent, body, parent.size.size + currentValue.size.size, 2000f));
}
else if (body == HeavenlyBodies.TimberHearth
|| body == HeavenlyBodies.BrittleHollow
|| body == HeavenlyBodies.HollowLantern
|| body == HeavenlyBodies.DarkBramble
|| body == HeavenlyBodies.SatiliteBacker)
{
return new Planet.Plantoid(currentValue.size, currentValue.gravity, randomQuaternion(), (float)seeds.NextRange(-0.2, 0.2), currentValue.state.parent, randomKepler(parent, body, currentValue));
}
else if (body == HeavenlyBodies.Sun
|| body == HeavenlyBodies.AshTwin
|| body == HeavenlyBodies.EmberTwin
|| body == HeavenlyBodies.SatiliteMapping)
{
return defaultValue;
}
else if (body == HeavenlyBodies.Stranger
|| body == HeavenlyBodies.DreamWorld
|| body == HeavenlyBodies.QuantumMoon
|| body == HeavenlyBodies.EyeOfTheUniverse
|| body == HeavenlyBodies.EyeOfTheUniverse_Vessel)
{
return currentValue;
}
else if (currentValue?.state?.parent?.pseudoHeavenlyBody == true)
{
return defaultValue;
}
else if (parent != null)
{
if (currentValue?.gravity?.isStatic == true)
{
return new Planet.Plantoid(currentValue.size, currentValue.gravity, randomQuaternion(), currentValue.state.relative.angularVelocity.magnitude, currentValue.state.parent, randomPosition(parent, body, currentValue), currentValue.state.relative.velocity);
}
else
{
return new Planet.Plantoid(currentValue.size, currentValue.gravity, randomQuaternion(), (float)seeds.NextRange(-0.2, 0.2), currentValue.state.parent, randomKepler(parent, body, currentValue));
}
}
else
{
return currentValue;
}
}
private Quaternion randomQuaternion()
{
return Quaternion.Euler((float)seeds.NextRange(0.0, 360.0), (float)seeds.NextRange(0.0, 360.0), (float)seeds.NextRange(0.0, 360.0));
}
private KeplerCoordinates randomKepler(Planet.Plantoid parent, HeavenlyBody body, Planet.Plantoid oldState)
{
var minDistance = parent.size.size + oldState.size.size;
var maxDistance = parent.size.influence;
if (oldState?.state?.parent == HeavenlyBodies.Sun)
{
maxDistance = maxDistance / 2f;
}
return randomKepler(parent, body, minDistance, maxDistance);
}
private KeplerCoordinates randomKepler(Planet.Plantoid parent, HeavenlyBody body, float minOrbitalDistance, float maxOrbitalDistance)
{
minOrbitalDistance = Math.Abs(minOrbitalDistance);
maxOrbitalDistance = Math.Abs(maxOrbitalDistance);
if (minOrbitalDistance > maxOrbitalDistance)
{
Helper.helper.Console.WriteLine($"Failed to Randmize {body} - Minimum orbital distance more than maximum orbital distance - Distance: {minOrbitalDistance}-{maxOrbitalDistance} -> {minOrbitalDistance}", OWML.Common.MessageType.Warning);
return KeplerCoordinates.fromMeanAnomaly(Ellipse.fromApogeeAndPerigee(minOrbitalDistance, minOrbitalDistance), (float)seeds.NextRange(-90.0, 90.0), (float)seeds.NextRange(0.0, 360.0), (float)seeds.NextRange(0.0, 360.0), (float)seeds.NextRange(0.0, 360.0));
}
else
{
var minRadius = (float)seeds.NextRange(minOrbitalDistance, maxOrbitalDistance);
var maxRadius = (float)seeds.NextRange(minRadius, maxOrbitalDistance);
return KeplerCoordinates.fromMeanAnomaly(Ellipse.fromApogeeAndPerigee(maxRadius, minRadius), (float)seeds.NextRange(-90.0, 90.0), (float)seeds.NextRange(0.0, 360.0), (float)seeds.NextRange(0.0, 360.0), (float)seeds.NextRange(0.0, 360.0));
}
}
private Vector3 randomPosition(Planet.Plantoid parent, HeavenlyBody body, Planet.Plantoid oldState)
{
var minDistance = parent.size.size + oldState.size.size;
var maxDistance = parent.size.influence;
return randomPosition(body, minDistance, maxDistance);
}
private Vector3 randomPosition(HeavenlyBody body, float minOrbitalDistance, float maxOrbitalDistance)
{
minOrbitalDistance = Math.Abs(minOrbitalDistance);
maxOrbitalDistance = Math.Abs(maxOrbitalDistance);
if (minOrbitalDistance > maxOrbitalDistance)
{
Helper.helper.Console.WriteLine($"Distance: {minOrbitalDistance}-{maxOrbitalDistance} -> {minOrbitalDistance} (Issue: Maximum is above Minimum)", OWML.Common.MessageType.Warning);
return new Vector3((float)seeds.NextRange(-1, 1), (float)seeds.NextRange(-1, 1), (float)seeds.NextRange(-1, 1)).normalized * minOrbitalDistance;
}
else
{
return new Vector3((float)seeds.NextRange(-1, 1), (float)seeds.NextRange(-1, 1), (float)seeds.NextRange(-1, 1)).normalized * (float)seeds.NextRange(minOrbitalDistance, maxOrbitalDistance);
}
}
private void onParentUpdate(HeavenlyBody body, HeavenlyBody oldParent, HeavenlyBody newParent)
{
if (body == HeavenlyBodies.None || oldParent == HeavenlyBodies.None || newParent == HeavenlyBodies.None)
{
return;
}
if (type == RandomizerSeeds.Type.FullUse || type == RandomizerSeeds.Type.SeedlessFullUse)
{
if (oldParent == HeavenlyBodies.Sun)
{
randomizeValues();
}
}
else if (type == RandomizerSeeds.Type.Use || type == RandomizerSeeds.Type.SeedlessUse
|| type == RandomizerSeeds.Type.MinuteUse || type == RandomizerSeeds.Type.SeedlessMinuteUse)
{
if (oldParent != HeavenlyBodies.Sun
&& oldParent != HeavenlyBodies.HourglassTwins)
{
var toUpdate = oldParent;
if (toUpdate == HeavenlyBodies.AshTwin
|| toUpdate == HeavenlyBodies.EmberTwin)
{
toUpdate = HeavenlyBodies.HourglassTwins;
}
var defaultMapping = Planet.defaultMapping;
var originalMapping = Planet.mapping;
var mapping = originalMapping;
if (originalMapping.ContainsKey(toUpdate))
{
var defaultValue = defaultMapping.ContainsKey(toUpdate) ? defaultMapping[toUpdate] : null;
mapping[toUpdate] = randomPlantoid(defaultMapping, originalMapping, mapping, toUpdate, originalMapping[toUpdate], defaultValue);
if (toUpdate == HeavenlyBodies.WhiteHole)
{
toUpdate = HeavenlyBodies.WhiteHoleStation;
defaultValue = defaultMapping.ContainsKey(toUpdate) ? defaultMapping[toUpdate] : null;
mapping[toUpdate] = randomPlantoid(defaultMapping, originalMapping, mapping, toUpdate, originalMapping[toUpdate], defaultValue);
}
Planet.mapping = mapping;
}
}
}
}
}
}