Add best route function. Calculates best route to destination.

This commit is contained in:
Rob L 2016-07-26 15:59:21 -04:00
parent 301e579287
commit 2169fc0db3
2 changed files with 53 additions and 3 deletions

View File

@ -1,6 +1,13 @@
{
"ATS_Navcomp_DB": {
"version": 2.11,
"gates": [
{ "name":"Sherman's Planet / USB Galadriel"},
{ "name":"Dark Phoenix City / USB Milliways"},
{ "name":"New Risa / USB Lothlorien"},
{ "name":"Lily Yo II / USB Celestine Gate"},
{ "name":"Kildare V / Kildare IX / Kildare XI / USB Stormwatch"}
],
"empires": [
{
"name": "Cardassian",
@ -201,7 +208,7 @@
{ "name":"ch'Nahir", "x": -9507.044174, "y": -128.000000, "z": -19.100000, "cochranes":1298, "market":-1 },
{ "name":"Mol'Rihan <New Romulus>", "x": -9429.742, "y": -147.389, "z": -8.21, "cochranes":1475.271911805, "market":-1 },
{ "name":"ch'Rihan", "x": -9384.244174, "y": -158.800000, "z": -1.800000, "cochranes":1298, "market":-1 }
],
"stations":[
{ "name":"RIS Sienovan B", "x": -9581.352300, "y": -314.942000, "z": 13.693000, "cochranes":1298, "market":-1 },
@ -217,7 +224,7 @@
"name": "GFA",
"desc":"The Galactic Ferengi Alliance is a sparse cluster of planets, centered around Ferenganor, which is colonized by the Ferengi and governed by the Ferengi Commerce Authority (or FCA). As an intensely capitalist society, the alliance is inclusive of most races which are capable of turning a profit. ",
"color": "#FFFF00",
"borders":[
{ "name":"GFA", "x": -8977.944000, "y": 260.000000, "z":-50.000000, "radius":100 }
],

View File

@ -23,6 +23,7 @@ function loadData(_callback) {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
jsonEmpire = JSON.parse(xmlhttp.responseText)['ATS_Navcomp_DB']['empires'];
jsonGate = JSON.parse(xmlhttp.responseText)['ATS_Navcomp_DB']['gates'];
_callback();
}
@ -325,5 +326,47 @@ function calcEndpointByHeading(heading,startvec = new THREE.Vector3(0,0,0)) {
var finalvec = new THREE.Vector3();
calcvec.add(startvec);
return calcvec;
}
function calcBestRoute(pointa,pointb) {
var route = [{}];
delete route['0']; // WTF? We shouldn't need to do this. I hate JS....
// Calculate direct route.
route['Direct'] = { 'stops': [pointb], 'distance': calcDist(pointa, pointb)};
// Find route via stargate.
var distance_a = {};
var distance_b = {};
var near_a,near_b;
// Find gate closest to point a
jsonGate.forEach(function(name) { distance_a[name.name] = calcDist(pointa,name.name) ;});
var dist_a_sorted = Object.keys(distance_a).sort(function(a,b) {return distance_a[a]-distance_a[b]});
var near_a = dist_a_sorted[0];
// Dump out right now if A->nearest gate > direct. Save the compute cycles.
if(distance_a[near_a] > route['Direct'].distance) {
return route['Direct'];
}
// Find gate closest to point b
jsonGate.forEach(function(name) { distance_b[name.name] = calcDist(pointb,name.name) ;});
var dist_b_sorted = Object.keys(distance_b).sort(function(a,b) {return distance_b[a]-distance_b[b]});
var near_b = dist_b_sorted[0];
// Dump out right now if B->nearest gate > direct or the same fucking gate. Save the compute cycles.
if(distance_a[near_b] > route['Direct'].distance || near_a == near_b) {
return route['Direct'];
}
// Assemble the gate travel plan. With our powers unite, we are shitty code!
gate_distance = distance_a[near_a] + distance_b[near_b];
route['Gate'] = {'stops': [near_a,near_b,pointb], 'distance':gate_distance}
// Sort all routes by distance traveled.
console.log(Object.keys(route))
var route_keys_sorted = Object.keys(route).sort(function(a,b) {return route[a].distance-route[b].distance});
return route[route_keys_sorted[0]];
}