-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRegionMap.js
More file actions
131 lines (108 loc) · 3.82 KB
/
RegionMap.js
File metadata and controls
131 lines (108 loc) · 3.82 KB
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
'use strict';
const crossfetch = require('cross-fetch');
const regionmapdata = require('./RegionMapData.json');
const regions = regionmapdata.regions;
const regionmap = regionmapdata.regionmap;
const x0 = -49985;
const y0 = -40985;
const z0 = -24105;
function findRegion(x, y, z){
let px = Math.floor((x - x0) * 83 / 4096);
let pz = Math.floor((z - z0) * 83 / 4096);
if (px < 0 || pz < 0 || pz > regionmap.length){
return null;
} else {
let row = regionmap[pz];
let rx = 0;
let pv = 0;
for (var v of row) {
let rl = v[0];
if (px < rx + rl){
pv = v[1];
break;
} else {
rx += rl;
}
}
if (pv == 0){
return { id: 0, name: null };
} else {
return { id: pv, name: regions[pv] };
}
}
}
function findRegionForBoxel(id64){
let masscode = id64 & 7;
let xdiv = 1 << (30 - masscode * 2);
let ydiv = 1 << (17 - masscode)
let zdiv = 1 << 3
let x = ((Math.floor(id64 / xdiv) & (0x3FFF >> masscode)) << masscode) * 10 + x0;
let y = ((Math.floor(id64 / ydiv) & (0x1FFF >> masscode)) << masscode) * 10 + y0;
let z = ((Math.floor(id64 / zdiv) & (0x3FFF >> masscode)) << masscode) * 10 + z0;
return {
x: x,
y: y,
z: z,
region: findRegion(x, y, z)
}
}
async function findRegionsForSystems(sysname){
const response = await crossfetch.fetch('https://www.edsm.net/api-v1/systems?systemName=' + encodeURIComponent(sysname) + '&coords=1&showId=1');
const systems = await response.json();
return systems.map(system => {
let systemdata = {
name: system.name,
id64: system.id64
};
if (system.coords) {
let x = systemdata.x = system.coords.x;
let y = systemdata.y = system.coords.y;
let z = systemdata.z = system.coords.z;
systemdata.region = findRegion(x, y, z);
}
if (system.id64) {
system.boxel = findRegionForBoxel(system.id64);
}
return systemdata;
});
}
async function main(args){
if (args.length == 0) {
console.log("Usage: " + process.argv[0] + " " + process.argv[1] + " \"System Name\" [..]");
return;
}
for (var sysname of args){
for (var sysdata of await findRegionsForSystems(sysname)){
let region = { id: 0, name: null };
if (sysdata.region !== undefined){
region = sysdata.region;
let x = sysdata.x;
let y = sysdata.y;
let z = sysdata.z;
if (region.id != 0){
console.log("System %s at (%f,%f,%f) is in region %d (%s)", sysdata.name, x, y, z, region.id, region.name);
} else {
console.log("System %s at (%f,%f,%f) is outside the region map", sysdata.name, x, y, z);
}
}
if (sysdata.boxel !== undefined && sysdata.boxel.region.id != region.id){
let boxel = sysdata.boxel;
let boxelregion = boxel.region;
let x = boxel.x;
let y = boxel.y;
let z = boxel.z;
if (boxelregion.id != 0){
console.log("Boxel of system %s at (%f,%f,%f) is in region %d (%s)", sysdata.name, x, y, z, boxelregion.id, boxelregion.name);
} else {
console.log("Boxel of system %s at (%f,%f,%f) is outside the region map", sysdata.name, x, y, z);
}
}
}
}
}
module.exports.findRegion = findRegion;
module.exports.findRegionForBoxel = findRegionForBoxel;
module.exports.findRegionsForSystems = findRegionsForSystems;
if (require.main == module){
main(process.argv.slice(2));
}