- rather than drawing each triangle completely, draw each edge just once. prevents overdraw - use dashed lines for edges - allows existing links to be seen - change to calculate on lat/lng directly, rather than the odd offset-to-positive latE6/lngE6 used before - change the event used for detecting portals added to map - was using one fired before portals added to the map before
164 lines
5.0 KiB
JavaScript
164 lines
5.0 KiB
JavaScript
// ==UserScript==
|
|
// @id max-links@boombuler
|
|
// @name IITC plugin: Max Links
|
|
// @category Layer
|
|
// @version 0.4.0.@@DATETIMEVERSION@@
|
|
// @updateURL @@UPDATEURL@@
|
|
// @downloadURL @@DOWNLOADURL@@
|
|
// @description [@@BUILDNAME@@-@@BUILDDATE@@] Calculates how to link the portals to create the maximum number of fields. Enable from the layer chooser.
|
|
// @include https://www.ingress.com/intel*
|
|
// @include http://www.ingress.com/intel*
|
|
// @match https://www.ingress.com/intel*
|
|
// @match http://www.ingress.com/intel*
|
|
// @grant none
|
|
// ==/UserScript==
|
|
|
|
function wrapper() {
|
|
|
|
// ensure plugin framework is there, even if iitc is not yet loaded
|
|
if(typeof window.plugin !== 'function')
|
|
window.plugin = function() {};
|
|
|
|
// PLUGIN START ////////////////////////////////////////////////////////
|
|
|
|
// use own namespace for plugin
|
|
window.plugin.maxLinks = function() {};
|
|
|
|
// const values
|
|
window.plugin.maxLinks.MAX_DRAWN_LINKS = 400;
|
|
window.plugin.maxLinks.MAX_DRAWN_LINKS_INCREASED_LIMIT = 1000;
|
|
window.plugin.maxLinks.STROKE_STYLE = {
|
|
color: '#FF0000',
|
|
opacity: 1,
|
|
weight: 2,
|
|
clickable: false,
|
|
dashArray: [8,6],
|
|
smoothFactor: 10,
|
|
};
|
|
window.plugin.maxLinks.layer = null;
|
|
|
|
window.plugin.maxLinks._updating = false;
|
|
window.plugin.maxLinks._renderLimitReached = false;
|
|
|
|
window.plugin.maxLinks.Point = function(x,y) {
|
|
this.x=x;
|
|
this.y=y;
|
|
}
|
|
window.plugin.maxLinks.Point.prototype.toString = function() {
|
|
return this.x+","+this.y;
|
|
}
|
|
|
|
window.plugin.maxLinks.updateLayer = function() {
|
|
if (window.plugin.maxLinks._updating ||
|
|
window.plugin.maxLinks.layer === null ||
|
|
!window.map.hasLayer(window.plugin.maxLinks.layer))
|
|
return;
|
|
window.plugin.maxLinks._updating = true;
|
|
window.plugin.maxLinks.layer.clearLayers();
|
|
|
|
var locations = [];
|
|
|
|
$.each(window.portals, function(guid, portal) {
|
|
var loc = portal.options.details.locationE6;
|
|
var nloc = new window.plugin.maxLinks.Point(loc.latE6/1E6, loc.lngE6/1E6);
|
|
locations.push(nloc);
|
|
});
|
|
|
|
var triangles = window.delaunay.triangulate(locations);
|
|
|
|
var drawnLinkCount = 0;
|
|
window.plugin.maxLinks._renderLimitReached = false;
|
|
var renderLimit = window.USE_INCREASED_RENDER_LIMIT ?
|
|
window.plugin.maxLinks.MAX_DRAWN_LINKS_INCREASED_LIMIT :
|
|
window.plugin.maxLinks.MAX_DRAWN_LINKS;
|
|
|
|
var orderedPoints = function(a,b) {
|
|
if(a.x<b.x) return [a,b];
|
|
if(a.x==b.x && a.y<b.y) return [a,b];
|
|
return [b,a];
|
|
}
|
|
var drawnLinks = {};
|
|
|
|
//draw a link, but only if it hasn't already been drawn
|
|
var drawLink = function(a,b) {
|
|
//order the points, so a pair of coordinates in any order is handled in one direction only
|
|
var points = orderedPoints(a,b);
|
|
a=points[0];
|
|
b=points[1];
|
|
|
|
//do we have a line already drawn from a to b?
|
|
if(!(a in drawnLinks)) {
|
|
//no lines from a to anywhere yet - create an empty target array
|
|
drawnLinks[a] = {};
|
|
}
|
|
|
|
if (!(b in drawnLinks[a])) {
|
|
//no line from a to b yet
|
|
|
|
//using drawnLinks[a] as a set - so the stored value is of no importance
|
|
drawnLinks[a][b] = null;
|
|
|
|
var poly = L.polyline([[a.x,a.y],[b.x,b.y]], window.plugin.maxLinks.STROKE_STYLE);
|
|
poly.addTo(window.plugin.maxLinks.layer);
|
|
drawnLinkCount++;
|
|
}
|
|
}
|
|
|
|
$.each(triangles, function(idx, triangle) {
|
|
drawLink(triangle.a,triangle.b);
|
|
drawLink(triangle.b,triangle.c);
|
|
drawLink(triangle.c,triangle.a);
|
|
|
|
// we only check the render limit after drawing all three edges of a triangle, for efficency
|
|
if (drawnLinkCount > renderLimit ) {
|
|
window.plugin.maxLinks._renderLimitReached = true;
|
|
return false; //$.each break
|
|
}
|
|
});
|
|
window.plugin.maxLinks._updating = false;
|
|
window.renderUpdateStatus();
|
|
}
|
|
|
|
window.plugin.maxLinks.setup = function() {
|
|
try { console.log('Loading delaunay JS now'); } catch(e) {}
|
|
@@INCLUDERAW:external/delaunay.js@@
|
|
try { console.log('done loading delaunay JS'); } catch(e) {}
|
|
|
|
window.plugin.maxLinks.layer = L.layerGroup([]);
|
|
|
|
window.addHook('checkRenderLimit', function(e) {
|
|
if (window.map.hasLayer(window.plugin.maxLinks.layer) &&
|
|
window.plugin.maxLinks._renderLimitReached)
|
|
e.reached = true;
|
|
});
|
|
|
|
window.addHook('requestFinished', function(e) {
|
|
window.plugin.maxLinks.updateLayer();
|
|
});
|
|
|
|
window.map.on('layeradd', function(e) {
|
|
if (e.layer === window.plugin.maxLinks.layer)
|
|
window.plugin.maxLinks.updateLayer();
|
|
});
|
|
window.map.on('zoomend moveend', window.plugin.maxLinks.updateLayer);
|
|
window.addLayerGroup('Maximum Links', window.plugin.maxLinks.layer, false);
|
|
}
|
|
var setup = window.plugin.maxLinks.setup;
|
|
|
|
// PLUGIN END //////////////////////////////////////////////////////////
|
|
if(window.iitcLoaded && typeof setup === 'function') {
|
|
setup();
|
|
} else {
|
|
if(window.bootPlugins)
|
|
window.bootPlugins.push(setup);
|
|
else
|
|
window.bootPlugins = [setup];
|
|
}
|
|
|
|
} // wrapper end
|
|
|
|
// inject code into site context
|
|
var script = document.createElement('script');
|
|
script.appendChild(document.createTextNode('('+ wrapper +')();'));
|
|
(document.body || document.head || document.documentElement).appendChild(script);
|