-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGuideMarker.cs
378 lines (331 loc) · 14.6 KB
/
GuideMarker.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
using System;
using System.Collections.Generic;
using BepInEx.Logging;
using UnityEngine;
namespace Bulldozer
{
[Flags]
public enum GuideMarkTypes
{
None = 0,
Equator = 1,
Meridian = 2,
Tropic = 4,
MinorMeridian = 8,
Pole = 16
}
public class GuideMarker
{
public static ManualLogSource logger;
private static readonly float[] TropicLats =
{
86f + 11f / 60f, 84.5f, 82.5f, 79.1f, 75.25f, 70.0f + 11.0f / 60f, 64.75f, 55f + 29f / 60f, 46.75f, 28.75f
};
// Map GuideMarkType to PluginConfigVariable in a switch statement. For whatever reason having a staticly initted Dictionary resulted in null refs.
// That might be because of the order that things get instantiated
private static int GetCustomColorIndex(GuideMarkTypes guideMarkType)
{
switch (guideMarkType)
{
case GuideMarkTypes.Equator: return PluginConfig.guideLinesEquatorColor.Value;
case GuideMarkTypes.Meridian: return PluginConfig.guideLinesMeridianColor.Value;
case GuideMarkTypes.MinorMeridian: return PluginConfig.guideLinesMinorMeridianColor.Value;
case GuideMarkTypes.Tropic: return PluginConfig.guideLinesTropicColor.Value;
case GuideMarkTypes.Pole: return PluginConfig.guideLinesPoleColor.Value;
default:
Log.logger.LogWarning(
$"no defined plugin config to get custom color for GuideMarkType {guideMarkType}");
return 7;
}
}
public static void AddGuideMarks(PlatformSystem platformSystem, GuideMarkTypes types)
{
if (types == GuideMarkTypes.None)
{
return;
}
if ((types & GuideMarkTypes.Tropic) == GuideMarkTypes.Tropic)
{
PaintTropics(platformSystem);
}
if ((types & GuideMarkTypes.MinorMeridian) == GuideMarkTypes.MinorMeridian)
{
PaintMinorMeridians(platformSystem);
}
if ((types & GuideMarkTypes.Meridian) == GuideMarkTypes.Meridian)
{
PaintMeridians(platformSystem);
}
if ((types & GuideMarkTypes.Equator) == GuideMarkTypes.Equator)
{
PaintEquator(platformSystem);
}
if ((types & GuideMarkTypes.Pole) == GuideMarkTypes.Pole)
{
PaintPoles(platformSystem);
}
}
private static void PaintEquator(PlatformSystem platformSystem)
{
// equator stripe
var coordLineOffset = GetCoordLineOffset(platformSystem.planet);
List<int> indexes = new List<int>();
for (var lon = -179.9f; lon < 180; lon += 0.25f)
{
for (var latOffset = -1; latOffset < 1; latOffset++)
{
var position = GeoUtil.LatLonToPosition(0f + latOffset * coordLineOffset, lon,
platformSystem.planet.radius);
var reformIndexForPosition = platformSystem.GetReformIndexForPosition(position);
if (reformIndexForPosition >= platformSystem.reformData.Length || reformIndexForPosition < 0)
{
logger.LogWarning($"reformIndex = {reformIndexForPosition} is out of bounds, apparently");
continue;
}
indexes.Add(reformIndexForPosition);
}
}
indexes.Sort();
InterpolateMissingIndexes(indexes);
var colorIndex = GetCustomColorIndex(GuideMarkTypes.Equator);
Console.WriteLine($"custom color index for equator {colorIndex}");
foreach (var equatorIndex in indexes)
{
var actual = Math.Max(0, equatorIndex);
try
{
platformSystem.SetReformType(actual, 1);
platformSystem.SetReformColor(actual, colorIndex);
}
catch (Exception e)
{
logger.LogWarning(
$"exception while setting reform at index {equatorIndex} max={platformSystem.reformData.Length} {e.Message}");
}
}
}
private static void PaintMeridians(PlatformSystem platformSystem)
{
var planetRadius = platformSystem.planet.radius;
var coordLineOffset = GetCoordLineOffset(platformSystem.planet);
var indexesToPaint = new List<int>();
var tropicLatitudes = Math.Abs(platformSystem.planet.radius - 200f) < 0.01f
? TropicLats
: GetTropicLatitudes(platformSystem);
for (var lat = -90.0f; lat < 90; lat += coordLineOffset)
{
if (PluginConfig.LatitudeOutOfBounds(lat))
continue;
for (var meridianOffset = 0; meridianOffset < 4; meridianOffset++)
{
var lonOffsetMin = -1;
// this is all to handle a bug where the 4th meridian line would be too skinny near the poles
if (Math.Abs(lat) > tropicLatitudes[5])
{
lonOffsetMin = -3;
}
if (Math.Abs(lat) > tropicLatitudes[4])
{
lonOffsetMin = -3;
}
if (Math.Abs(lat) > tropicLatitudes[2])
{
lonOffsetMin -= 2;
}
if (Math.Abs(lat) > tropicLatitudes[1])
{
lonOffsetMin -= 5;
}
var lonOffsetMax = 2;
HashSet<int> actualIndexes = new HashSet<int>();
for (var lonOffset = lonOffsetMin; lonOffset < lonOffsetMax; lonOffset++)
{
var lon = coordLineOffset * lonOffset + meridianOffset * 90f;
var position = GeoUtil.LatLonToPosition(lat, lon, planetRadius);
var reformIndexForPosition = platformSystem.GetReformIndexForPosition(position);
indexesToPaint.Add(reformIndexForPosition);
actualIndexes.Add(reformIndexForPosition);
}
}
}
indexesToPaint.Sort();
InterpolateMissingIndexes(indexesToPaint);
var customColor = GetCustomColorIndex(GuideMarkTypes.Meridian);
foreach (var meridianIndex in indexesToPaint)
{
var actualIndex = Math.Max(0, meridianIndex);
try
{
platformSystem.SetReformType(actualIndex, 1);
platformSystem.SetReformColor(actualIndex, customColor);
}
catch (Exception e)
{
logger.LogWarning(
$"exception while setting reform at index {actualIndex} max={platformSystem.reformData.Length} {e.Message}");
}
}
}
private static void PaintMinorMeridians(PlatformSystem platformSystem)
{
var planetRadius = platformSystem.planet.radius;
var coordLineOffset = GetCoordLineOffset(platformSystem.planet);
var indexesToPaint = new List<int>();
var interval = PluginConfig.minorMeridianInterval.Value;
var tropicLatitudes = Math.Abs(platformSystem.planet.radius - 200f) < 0.01f
? TropicLats
: GetTropicLatitudes(platformSystem);
for (var lat = -90.0f; lat < 90; lat += coordLineOffset)
{
if (Math.Abs(lat) > Math.Abs(tropicLatitudes[3]))
continue;
if (PluginConfig.LatitudeOutOfBounds(lat))
continue;
for (var meridianOffset = -180; meridianOffset < 180; meridianOffset += interval)
{
var lonOffsetMin = -1;
var lonOffsetMax = 2;
HashSet<int> actualIndexes = new HashSet<int>();
for (var lonOffset = lonOffsetMin; lonOffset < lonOffsetMax; lonOffset++)
{
var lon = coordLineOffset * lonOffset + meridianOffset;
var position = GeoUtil.LatLonToPosition(lat, lon, planetRadius);
var reformIndexForPosition = platformSystem.GetReformIndexForPosition(position);
indexesToPaint.Add(reformIndexForPosition);
actualIndexes.Add(reformIndexForPosition);
}
}
}
indexesToPaint.Sort();
InterpolateMissingIndexes(indexesToPaint);
var customColor = GetCustomColorIndex(GuideMarkTypes.MinorMeridian);
foreach (var meridianIndex in indexesToPaint)
{
var actualIndex = Math.Max(0, meridianIndex);
try
{
platformSystem.SetReformType(actualIndex, 1);
platformSystem.SetReformColor(actualIndex, customColor);
}
catch (Exception e)
{
logger.LogWarning(
$"exception while setting reform at index {actualIndex} max={platformSystem.reformData.Length} {e.Message}");
}
}
}
private static void PaintTropics(PlatformSystem platformSystem)
{
var latitudes = Math.Abs(platformSystem.planet.radius - 200f) < 0.01f
? TropicLats
: GetTropicLatitudes(platformSystem);
var signs = new[] { 1, -1 };
var indexes = new List<int>();
var coordLineOffset = GetCoordLineOffset(platformSystem.planet);
foreach (var latInDegrees in latitudes)
{
if (PluginConfig.LatitudeOutOfBounds(latInDegrees))
continue;
foreach (var sign in signs)
{
for (var lon = -179.9f; lon < 180; lon += coordLineOffset)
{
var position = GeoUtil.LatLonToPosition(latInDegrees * sign, lon, platformSystem.planet.radius);
var reformIndexForSegment = platformSystem.GetReformIndexForPosition(position);
if (reformIndexForSegment >= 0)
{
indexes.Add(reformIndexForSegment);
}
}
}
}
foreach (var ndx in indexes)
{
platformSystem.SetReformType(ndx, 1);
platformSystem.SetReformColor(ndx, GetCustomColorIndex(GuideMarkTypes.Tropic));
}
}
private static void PaintPoles(PlatformSystem platformSystem)
{
var tropicLatitudes = Math.Abs(platformSystem.planet.radius - 200f) < 0.01f
? TropicLats
: GetTropicLatitudes(platformSystem);
// poles will be anything in the first two tropics
var indexes = new List<int>();
var coordLineOffset = GetCoordLineOffset(platformSystem.planet);
for (var lat = -90.0f; lat < 90; lat += coordLineOffset)
{
if (Math.Abs(lat) <= Math.Abs(tropicLatitudes[0]) + coordLineOffset)
continue;
if (PluginConfig.LatitudeOutOfBounds(lat))
continue;
for (var lon = -179.9f; lon < 180; lon += coordLineOffset)
{
var position = GeoUtil.LatLonToPosition(lat, lon, platformSystem.planet.radius);
var reformIndexForSegment = platformSystem.GetReformIndexForPosition(position);
if (reformIndexForSegment >= 0)
{
indexes.Add(reformIndexForSegment);
}
}
}
var color = GetCustomColorIndex(GuideMarkTypes.Pole);
foreach (var ndx in indexes)
{
platformSystem.SetReformType(ndx, 1);
platformSystem.SetReformColor(ndx, color);
}
}
private static float[] GetTropicLatitudes(PlatformSystem platformSystem)
{
var lastLen = 0;
var result = new List<float>();
var coordLineOffset = GetCoordLineOffset(platformSystem.planet);
for (var lat = -89.9f; lat < 90; lat += coordLineOffset)
{
var position = GeoUtil.LatLonToPosition(lat, 0, platformSystem.planet.radius);
position.Normalize();
double latitude = Mathf.Asin(position.y);
var latitudeIndex = (float)(latitude / 6.28318548202515) * platformSystem.segment;
var longitudeSegmentCount =
PlatformSystem.DetermineLongitudeSegmentCount(Mathf.FloorToInt(Mathf.Abs(latitudeIndex)),
platformSystem.segment);
if (lastLen != longitudeSegmentCount)
{
logger.LogDebug($"at lat = {lat} length is {longitudeSegmentCount}, previous len was {lastLen}");
result.Add(lat);
}
lastLen = longitudeSegmentCount;
}
return result.ToArray();
}
public static float GetCoordLineOffset(PlanetData planet, float baseRate = 0.25f)
{
var result = baseRate;
if (planet.radius > 201)
{
// so if we step by 0.25 for a 200 radius planet, step by 0.125 for a 400 radius planet
result /= (planet.radius / 200f);
}
return result;
}
public static void InterpolateMissingIndexes(List<int> indexes)
{
var tempNewIndexes = new List<int>();
for (int i = 1; i < indexes.Count; i++)
{
var curIndex = indexes[i];
var prevIndex = indexes[i - 1];
if (prevIndex != curIndex - 1 && curIndex - prevIndex < 8)
{
// fill in
for (int j = prevIndex + 1; j < curIndex; j++)
{
tempNewIndexes.Add(j);
}
}
}
indexes.AddRange(tempNewIndexes);
}
}
}