-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSCANdata.cs
294 lines (267 loc) · 9.6 KB
/
SCANdata.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
/*
* Scientific Committee on Advanced Navigation S.C.A.N. Satellite
* SCANdata - encapsulates scanned data for a body
*
* Copyright (c)2013 damny; see LICENSE.txt for licensing details.
*/
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
namespace SCANsat
{
public class SCANdata
{
protected byte[,] coverage = new byte[360, 180];
protected float[,] heightmap = new float[360, 180];
public CelestialBody body;
public Texture2D map_small = new Texture2D(360, 180, TextureFormat.RGB24, false);
public bool disabled;
public enum SCANtype
{
Nothing = 0,
// no data
AltimetryLoRes = 1,
// low resolution altimetry (limited zoom)
AltimetryHiRes = 2,
// high resolution altimetry (unlimited zoom)
Altimetry = 3,
// both (setting) or either (testing) altimetry
//Slope = 4, // slope data
Biome = 8,
// biome data
Anomaly = 16,
// anomalies (position of anomaly)
AnomalyDetail = 32,
// anomaly detail (name of anomaly, etc.)
Everything = 255
// everything
}
public void registerPass(double lon, double lat, SCANtype type) {
// fudging coordinates a bit because KSP may return them unclipped
int ilon = ((int)(lon + 360 + 180)) % 360;
int ilat = ((int)(lat + 180 + 90)) % 180;
if(ilon < 0 || ilat < 0 || ilon >= 360 || ilat >= 180) return;
coverage[ilon, ilat] |= (byte)type;
}
public bool isCovered(double lon, double lat, SCANtype type) {
int ilon = ((int)(lon + 360 + 180)) % 360;
int ilat = ((int)(lat + 180 + 90)) % 180;
if(ilon < 0 || ilat < 0 || ilon >= 360 || ilat >= 180) return false;
return (coverage[ilon, ilat] & (byte)type) != 0;
}
public bool isCoveredByAll(double lon, double lat, SCANtype type) {
int ilon = ((int)(lon + 360 + 180)) % 360;
int ilat = ((int)(lat + 180 + 90)) % 180;
if(ilon < 0 || ilat < 0 || ilon >= 360 || ilat >= 180) return false;
return (coverage[ilon, ilat] & (byte)type) == (byte)type;
}
public int[] coverage_count = new int[8];
public void updateCoverage() {
for(int i=0; i<6; ++i) {
SCANtype t = (SCANtype)(1 << i);
int cc = 0;
for(int x=0; x<360; ++x) {
for(int y=0; y<180; ++y) {
if((coverage[x, y] & (byte)t) == 0) ++cc;
}
}
coverage_count[i] = cc;
}
}
public int getCoverage(SCANtype type) {
int uncov = 0;
if((type & SCANtype.AltimetryLoRes) != SCANtype.Nothing) uncov += coverage_count[0];
if((type & SCANtype.AltimetryHiRes) != SCANtype.Nothing) uncov += coverage_count[1];
if((type & SCANtype.Biome) != SCANtype.Nothing) uncov += coverage_count[3];
if((type & SCANtype.Anomaly) != SCANtype.Nothing) uncov += coverage_count[4];
if((type & SCANtype.AnomalyDetail) != SCANtype.Nothing) uncov += coverage_count[5];
return uncov;
}
public double getCoveragePercentage(SCANtype type) {
if(type == SCANtype.Nothing) {
type = SCANtype.AltimetryLoRes | SCANtype.AltimetryHiRes | SCANtype.Biome | SCANtype.Anomaly;
}
double cov = getCoverage(type);
if(cov <= 0) {
cov = 100;
} else {
cov = Math.Min(99.9d, 100 - cov * 100d / (360d * 180d * SCANcontroller.countBits((int)type)));
}
return cov;
}
public class SCANanomaly
{
public SCANanomaly (string s, double lon, double lat, PQSMod m)
{
name = s;
longitude = lon;
latitude = lat;
known = false;
mod = m;
}
public bool known;
public bool detail;
public string name;
public double longitude;
public double latitude;
public PQSMod mod;
}
SCANanomaly[] anomalies;
public SCANanomaly[] getAnomalies() {
if(anomalies == null) {
PQSCity[] sites = body.GetComponentsInChildren<PQSCity>(true);
anomalies = new SCANanomaly[sites.Length];
for(int i=0; i<sites.Length; ++i) {
anomalies[i] = new SCANanomaly(sites[i].name, body.GetLongitude(sites[i].transform.position), body.GetLatitude(sites[i].transform.position), sites[i]);
}
}
for(int i=0; i<anomalies.Length; ++i) {
anomalies[i].known = isCovered(anomalies[i].longitude, anomalies[i].latitude, SCANtype.Anomaly);
anomalies[i].detail = isCovered(anomalies[i].longitude, anomalies[i].latitude, SCANtype.AnomalyDetail);
}
return anomalies;
}
public string serialize() {
// convert the byte[,] array into a KSP-savefile-safe variant of Base64
MemoryStream mem = new MemoryStream();
BinaryFormatter binf = new BinaryFormatter();
binf.Serialize(mem, coverage);
string blob = Convert.ToBase64String(CLZF2.Compress(mem.ToArray()));
return blob.Replace("/", "-").Replace("=", "_");
}
public void deserialize(string blob) {
try {
blob = blob.Replace("-", "/").Replace("_", "=");
byte[] bytes = Convert.FromBase64String(blob);
bytes = CLZF2.Decompress(bytes);
MemoryStream mem = new MemoryStream(bytes, false);
BinaryFormatter binf = new BinaryFormatter();
coverage = (byte[,])binf.Deserialize(mem);
} catch(Exception e) {
coverage = new byte[360, 180];
heightmap = new float[360, 180];
throw e;
}
resetImages();
}
public void reset() {
coverage = new byte[360, 180];
heightmap = new float[360, 180];
resetImages();
}
public void resetImages() {
// Just draw a simple grid to initialize the image; the map will appear on top of it
for(int y=0; y<map_small.height; y++) {
for(int x=0; x<map_small.width; x++) {
if((x % 30 == 0 && y % 3 > 0) || (y % 30 == 0 && x % 3 > 0)) {
map_small.SetPixel(x, y, Color.white);
} else {
map_small.SetPixel(x, y, Color.grey);
}
}
}
map_small.Apply();
}
protected Color[] redline;
public void updateImages(SCANtype type) {
if(redline == null) {
redline = new Color[360];
for(int i=0; i<360; i++) redline[i] = Color.red;
}
drawHeightScanline(type);
if(scanline < 179) {
map_small.SetPixels(0, scanline + 1, 360, 1, redline);
}
map_small.Apply();
}
public double getElevation(double lon, double lat) {
if(body.pqsController == null) return 0;
int ilon = ((int)(lon + 360 + 180)) % 360;
int ilat = ((int)(lat + 180 + 90)) % 180;
double rlon = Mathf.Deg2Rad * lon;
double rlat = Mathf.Deg2Rad * lat;
Vector3d rad = new Vector3d(Math.Cos(rlat) * Math.Cos(rlon), Math.Sin(rlat), Math.Cos(rlat) * Math.Sin(rlon));
return Math.Round(body.pqsController.GetSurfaceHeight(rad) - body.pqsController.radius, 1);
}
public int getBiomeIndex(double lon, double lat) {
if(body.BiomeMap == null) return -1;
if(body.BiomeMap.Map == null) return -1;
double u = ((lon + 360 + 180 + 90)) % 360;
double v = ((lat + 180 + 90)) % 180;
if(u < 0 || v < 0 || u >= 360 || v >= 180) return -1;
CBAttributeMap.MapAttribute att = body.BiomeMap.GetAtt(Mathf.Deg2Rad * lat, Mathf.Deg2Rad * lon);
for(int i = 0; i < body.BiomeMap.Attributes.Length; ++i) {
if(body.BiomeMap.Attributes[i] == att) {
return i;
}
}
return -1;
}
public double getBiomeIndexFraction(double lon, double lat) {
if(body.BiomeMap == null) return 0f;
return getBiomeIndex(lon, lat) * 1.0f / body.BiomeMap.Attributes.Length;
}
public CBAttributeMap.MapAttribute getBiome(double lon, double lat) {
if(body.BiomeMap == null) return null;
if(body.BiomeMap.Map == null) return body.BiomeMap.defaultAttribute;
int i = getBiomeIndex(lon, lat);
if(i < 0) return body.BiomeMap.defaultAttribute;
return body.BiomeMap.Attributes[i];
}
public string getBiomeName(double lon, double lat) {
CBAttributeMap.MapAttribute a = getBiome(lon, lat);
if(a == null) return "unknown";
return a.name;
}
protected int scanline = 0;
protected int scanstep = 0;
public void drawHeightScanline(SCANtype type) {
Color[] cols_height_map_small = map_small.GetPixels(0, scanline, 360, 1);
for(int ilon=0; ilon<360; ilon+=1) {
int scheme = 0;
float val = heightmap[ilon, scanline];
if(val == 0 && isCovered(ilon - 180, scanline - 90, SCANtype.Altimetry)) {
if(body.pqsController == null) {
heightmap[ilon, scanline] = 0;
cols_height_map_small[ilon] = Color.Lerp(Color.black, Color.white, UnityEngine.Random.value);
continue;
} else {
// convert to radial vector
double rlon = Mathf.Deg2Rad * (ilon - 180);
double rlat = Mathf.Deg2Rad * (scanline - 90);
Vector3d rad = new Vector3d(Math.Cos(rlat) * Math.Cos(rlon), Math.Sin(rlat), Math.Cos(rlat) * Math.Sin(rlon));
// query terrain controller for elevation at this point
val = (float)Math.Round(body.pqsController.GetSurfaceHeight(rad) - body.pqsController.radius, 1);
if(val == 0) val = -0.001f; // this is terrible
heightmap[ilon, scanline] = val;
}
}
Color c = Color.black;
if(val != 0) {
if(isCovered(ilon - 180, scanline - 90, SCANtype.AltimetryHiRes)) c = SCANmap.heightToColor(val, scheme);
else c = SCANmap.heightToColor(val, 1);
} else {
c = Color.grey;
if(scanline % 30 == 0 && ilon % 3 == 0) {
c = Color.white;
} else if(ilon % 30 == 0 && scanline % 3 == 0) {
c = Color.white;
}
}
if(type != SCANtype.Nothing) {
if(!isCoveredByAll(ilon - 180, scanline - 90, type)) {
c = Color.Lerp(c, Color.black, 0.5f);
}
}
cols_height_map_small[ilon] = c;
}
map_small.SetPixels(0, scanline, 360, 1, cols_height_map_small);
scanline = scanline + 1;
if(scanline >= 180) {
scanstep += 1;
scanline = 0;
}
}
}
}