-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRegionMap.cs
196 lines (171 loc) · 6.03 KB
/
RegionMap.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
using System;
using System.Net;
using System.Text.Json;
namespace EliteDangerousRegionMap
{
public class SystemData
{
public string Name { get; set; }
public long ID64 { get; set; }
public double? X { get; set; }
public double? Y { get; set; }
public double? Z { get; set; }
public Region Region { get; set; }
public SystemBoxel Boxel { get; set; }
}
public class Region
{
public int Id { get; set; }
public string Name { get; set; }
}
public class SystemBoxel
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Region Region { get; set; }
}
public static partial class RegionMap
{
private class EDSMSystem
{
public string name { get; set; }
public long id64 { get; set; }
public EDSMSystemCoords coords { get; set; }
}
private class EDSMSystemCoords
{
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
}
private const double x0 = -49985;
private const double y0 = -40985;
private const double z0 = -24105;
public static Region FindRegion(double x, double y, double z)
{
var px = (int)((x - x0) * 83 / 4096);
var pz = (int)((z - z0) * 83 / 4096);
if (px < 0 || pz < 0 || pz >= RegionMapLines.Length)
{
return default;
}
else
{
var row = RegionMapLines[pz];
var rx = 0;
var pv = 0;
foreach (var (rl, rv) in row)
{
if (px < rx + rl)
{
pv = rv;
break;
}
else
{
rx += rl;
}
}
if (pv == 0)
{
return null;
}
else
{
return new Region { Id = pv, Name = RegionNames[pv] };
}
}
}
public static SystemBoxel FindRegionForBoxel(long id64)
{
int masscode = (int)(id64 & 7);
double x = (((id64 >> (30 - masscode * 2)) & (0x3FFF >> masscode)) << masscode) * 10 + x0;
double y = (((id64 >> (17 - masscode)) & (0x1FFF >> masscode)) << masscode) * 10 + y0;
double z = (((id64 >> 3) & (0x3FFF >> masscode)) << masscode) * 10 + z0;
return new SystemBoxel
{
X = x,
Y = y,
Z = z,
Region = FindRegion(x, y, z)
};
}
public static SystemData[] FindRegionsForSystems(string sysname)
{
var url = $"https://www.edsm.net/api-v1/systems?systemName={Uri.EscapeDataString(sysname)}&coords=1&showId=1";
var client = new WebClient();
var jsonstr = client.DownloadString(url);
var systems = JsonSerializer.Deserialize<EDSMSystem[]>(jsonstr);
var sysregions = new SystemData[systems.Length];
for (int i = 0; i < systems.Length; i++)
{
var system = systems[i];
var sysdata = sysregions[i] = new SystemData
{
Name = system.name,
ID64 = system.id64
};
if (system.coords != null)
{
double x = system.coords.x;
double y = system.coords.y;
double z = system.coords.z;
sysdata.X = x;
sysdata.Y = y;
sysdata.Z = z;
sysdata.Region = FindRegion(x, y, z);
}
if (system.id64 != 0)
{
sysdata.Boxel = FindRegionForBoxel((long)system.id64);
}
}
return sysregions;
}
private static void Main(string[] args)
{
if (args.Length == 0)
{
var exe = System.Reflection.Assembly.GetExecutingAssembly().Location;
Console.WriteLine($"Usage: {exe} \"System 1\" [...]");
return;
}
foreach (var name in args)
{
foreach (var sysdata in FindRegionsForSystems(name))
{
if (sysdata.X != null)
{
var x = sysdata.X;
var y = sysdata.Y;
var z = sysdata.Z;
if (sysdata.Region != null)
{
Console.WriteLine($"System {sysdata.Name} at ({x},{y},{z}) is in region {sysdata.Region.Id} ({sysdata.Region.Name})");
}
else
{
Console.WriteLine($"System {sysdata.Name} at ({x},{y},{z}) is outside the region map");
}
}
if (sysdata.Boxel != null && sysdata.Boxel.Region.Id != sysdata.Region?.Id)
{
var boxel = sysdata.Boxel;
var x = boxel.X;
var y = boxel.Y;
var z = boxel.Z;
if (boxel.Region != null)
{
Console.WriteLine($"Boxel of system {sysdata.Name} at ({x},{y},{z}) is in region {boxel.Region.Id} ({boxel.Region.Name})");
}
else
{
Console.WriteLine($"Boxel of system {sysdata.Name} at ({x},{y},{z}) is outside the region map");
}
}
}
}
}
}
}