Add route planner

This commit is contained in:
Rob L
2016-07-27 15:43:30 -04:00
parent 2169fc0db3
commit 77a26bf1f6
3 changed files with 82 additions and 30 deletions

View File

@ -12,19 +12,18 @@ $(document).ready(function() {
var selected = $('#findbyselect option:selected').text();
zoomfocus(selected);
});
$('#route_output').change(function() {
var stop=$('#route_output :selected').parent().attr('label');
zoomfocus(stop);
});
$('#calctnd').click(function() {
removeEntity('arrow');
lastInputBox = null;
if ( $('#pointa option:selected').text() != $('#pointb option:selected').text() && (!$('#pointa option:selected').text().match("[=.] (.+) [=.]") && !$('#pointb option:selected').text().match("[=.] (.+) [=.]"))) {
$('#cal_start').html( $('#pointa option:selected').text() );
$('#cal_end').html( $('#pointb option:selected').text() );
$('#cal_speed').html( $('#speed').val() +" " + $('#speedunit option:selected').val() );
var dist = calcDist( $('#pointa option:selected').text(), $('#pointb option:selected').text() );
var eta = calcETA({'speed': $('#speed').val(), 'unit': $('#speedunit option:selected').val()},dist)
$('#cal_eta').html( timeformat(eta) );
$('#cal_dist').html( dist.toFixed(2) + " PC");
drawline(grabPositionByName($('#pointa option:selected').text()),grabPositionByName($('#pointb option:selected').text()));
}
var speed = {'speed': $('#speed').val(), 'unit':$ ('#speedunit option:selected').val() };
populateRoutePlan( $('#pointa option:selected').text() , $('#pointb option:selected').text(),speed );
});
$('#cbs').click(function() {populateFBSelect(); });
$('#cbp').click(function() {populateFBSelect(); });
@ -54,10 +53,11 @@ function populateUserFields() {
var keys = Object.keys(listobjects(types[type]));
var captype = types[type];
captype = captype.capitalize()
option += '<option value="">==== ' + captype + ' ====</option>';
option += '<optgroup label="'+ captype + '">';
keys.sort().forEach(function(element, index, array){
option += '<option value="'+ escapeHTML(element) + '">' + escapeHTML(element) + '</option>';
});
option += '</optgroup>';
}
@ -77,10 +77,14 @@ function populateFBSelect() {
}
var option = '';
for (var type in types) {
var captype = types[type];
captype = captype.capitalize()
option += '<optgroup label="'+ captype + '">';
var keys = Object.keys(listobjects(types[type]));
keys.sort().forEach(function(element, index, array){
option += '<option value="'+ escapeHTML(element) + '">' + escapeHTML(element) + '</option>';
});
option += '</optgroup>';
}
@ -126,3 +130,39 @@ function timeformat(secs) {
// console.log("Input :"+ secs);
return h+":"+(m < 10 ? '0'+m : m)+":"+(s < 10 ? '0'+s : s); //zero padding on minutes and seconds
}
function populateRoutePlan(pointa,pointb,speed) {
if ( pointa != pointb ) {
// Populate legacy (trip total) fields with route info
$('#cal_start').html( pointa );
$('#cal_end').html( pointb );
$('#cal_speed').html( speed.speed + " " + speed.unit );
var route = calcBestRoute(pointa,pointb);
console.log(route)
var dist = route.distance;
var eta = calcETA(speed,dist);
$('#cal_eta').html( timeformat(eta) );
$('#cal_dist').html( dist.toFixed(2) + " PC");
// Populate the route plan select area
lastWaypoint = {'name': pointa, gate:false};
var routeplan;
route.stops.forEach(function(waypoint,index,self) {
routeplan += '<optgroup label="' + waypoint.name + '">';
if(typeof self[index+1] != 'undefined' ) {
if(waypoint.gate && self[index+1].gate) {routeplan += '<option>^---- Gate From</option>'; }
console.log(waypoint.gate)
}
if(waypoint.gate && lastWaypoint.gate) {routeplan += '<option>^---- Gate Exit</option>'; }
if(!waypoint.gate || (!lastWaypoint.gate && waypoint.gate)) {
routeplan += '<option>Distance:' + calcDist(lastWaypoint.name,waypoint.name) + '</option>'
routeplan += '<option>ETA: ' + timeformat(calcETA(speed,calcDist(lastWaypoint.name,waypoint.name))) + '</option>';
drawline(grabPositionByName(lastWaypoint.name),grabPositionByName(waypoint.name));
}
lastWaypoint = waypoint;
});
$('#route_output').html(routeplan);
//drawline(grabPositionByName($('#pointa option:selected').text()),grabPositionByName($('#pointb option:selected').text()));
}
}

View File

@ -251,8 +251,11 @@ function drawline(origin,dest) {
}
function removeEntity(object) {
var selectedObject = scene.getObjectByName(object);
var selectedObject;
while ( selectedObject = scene.getObjectByName(object) ) {
scene.remove( selectedObject );
}
animate();
}
@ -333,19 +336,20 @@ function calcBestRoute(pointa,pointb) {
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)};
route['Direct'] = { 'stops': [{'name':pointb, 'gate': false}], '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) ;});
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) {
if(distance_a[near_a] > route['Direct'].distance || near_a == pointb) {
return route['Direct'];
}
@ -355,17 +359,14 @@ function calcBestRoute(pointa,pointb) {
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'];
if(distance_b[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}
route['Gate'] = {'stops': [{'name':near_a, 'gate':true} ,{'name': near_b, 'gate': true},{'name': pointb, 'gate':false}], 'distance':gate_distance}
// Sort all routes by distance traveled.
console.log(Object.keys(route))
// Sort all routes by distance traveled. Index of zero should be the fastest, in theory any way
var route_keys_sorted = Object.keys(route).sort(function(a,b) {return route[a].distance-route[b].distance});
return route[route_keys_sorted[0]];