From 98d4a24714a62e4c0a28690f78194564cc4bf73c Mon Sep 17 00:00:00 2001 From: parabola949 Date: Mon, 26 Aug 2013 14:33:59 -0500 Subject: [PATCH] Plugin add - Farm Find v1.1.1 --- plugins/layer-farms-find.user.js | 299 +++++++++++++++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 plugins/layer-farms-find.user.js diff --git a/plugins/layer-farms-find.user.js b/plugins/layer-farms-find.user.js new file mode 100644 index 00000000..05a16959 --- /dev/null +++ b/plugins/layer-farms-find.user.js @@ -0,0 +1,299 @@ +// ==UserScript== +// @id iitc-plugin-farms@parabola949 +// @name IITC plugin: Show farms by level +// @category Layer +// @version 1.1.1.20130826.142308 +// @namespace https://github.com/jonatkins/ingress-intel-total-conversion +// @updateURL http://userscripts.org/scripts/source/175900.meta.js +// @downloadURL http://userscripts.org/scripts/source/175900.user.js +// @description [parabola949-2013-08-26] Finds farms by minimum level +// @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== + +//CHANGELOG +/* + * +v1.1.1 +Changed circle stroke weight and opacity, making it easier to see when zoomed out. + +v1.1.0 +Changed radius function and center function - +Center is now the midpoint of the max and min long and lat coords +Radius is distance from center to furthest portal (i.e., now the circle will be limited to portals in the farm) + +v1.0.2 +Bugfix - Didn't work without Barcode Name Decoder plugin due to namespaces..... oops + +v1.0.1 +Bugfix - minimum level was actually off by one, due to using > instead of >= + +v1.0.0 +Initial release + +*/ + +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.farmFind = function() {}; + +window.plugin.farmFind.getNearbyPortalCount = function(portal){ + + var circle = new google.maps.Circle(); + var center = new google.maps.LatLng(portal.getLatLng().lat, portal.getLatLng().lng); + circle.setCenter(center); + circle.setRadius(window.plugin.farmFind.Radius); + + + var nearby8Portals = 0; + + $.each(window.portals, function(i, otherPortal) { + var thisPortal = new google.maps.LatLng(otherPortal.getLatLng().lat, otherPortal.getLatLng().lng); + if (circle.getBounds().contains(thisPortal)) + if (getPortalLevel(otherPortal.options.details) >= window.plugin.farmFind.minLevel) nearby8Portals++; + }); + //console.log(nearby8Portals); + return nearby8Portals; +}; + +window.plugin.farmFind.checkPortals = function(){ + possibleFarmPortals = []; + window.plugin.farmFind.levelLayerGroup.clearLayers(); + //console.log(window.portals.length); + $.each(window.portals, function(i, portal) { + + if (window.plugin.farmFind.getNearbyPortalCount(portal) > window.plugin.farmFind.minNearby) + { + //console.log("Farm identified"); + possibleFarmPortals.push(portal); + } + }); + console.log("Farm Portals: " + possibleFarmPortals.length); + var farms = []; + for (i = 0; i< possibleFarmPortals.length;i++) + { + //console.log("Checking portal " + i); + var thisPortal = possibleFarmPortals[i]; + var alreadyInFarm = false; + for (x = 0; x< farms.length;x++) + { + //console.log(alreadyInFarm); + if (thisPortal in farms[x]) alreadyInFarm = true; + } + + //console.log("Portal " + i + " already in farm: " + alreadyInFarm); + + if (!alreadyInFarm) + { + var portalsInFarm = []; + var circle = new google.maps.Circle(); + var center = new google.maps.LatLng(thisPortal.getLatLng().lat, thisPortal.getLatLng().lng); + circle.setCenter(center); + circle.setRadius(window.plugin.farmFind.Radius); + portalsInFarm.push(thisPortal); + for (p = 0; p< possibleFarmPortals.length;p++) + { + //console.log("Checking distance from portal " + p); + var portalLoc = new google.maps.LatLng(possibleFarmPortals[p].getLatLng().lat, possibleFarmPortals[p].getLatLng().lng); + + var farmIndex = 0; + if (circle.getBounds().contains(portalLoc) && possibleFarmPortals[p] != thisPortal) + { + var alreadyInAnotherFarm = false; + for (x = 0; x< farms.length;x++) + { + //console.log(alreadyInFarm); + for (o = 0; o< farms[x].length; o++) + { + if (possibleFarmPortals[p] == farms[x][o]) + { + //console.log("Portal " + p + " in farm " + (x+1) + " at index " + o); + alreadyInAnotherFarm = true; + farmIndex = x; + } + } + + + } + + //console.log("Already in another farm: " + alreadyInAnotherFarm); + + if (alreadyInAnotherFarm == false) + { + //console.log("Farm " + (farms.length + 1) + " adding portal " + p); + portalsInFarm.push(possibleFarmPortals[p]); + } + else + { + //hmmm... portal is in range, but portal is in another farm, so let's extend this farm + for(prt = 0; prt < portalsInFarm.length; prt++) + { + farms[farmIndex].push(portalsInFarm[prt]); + } + + //console.log("Farm " + (farmIndex + 1) + " now has " + farms[farmIndex].length + " portals"); + p = 6000; + } + } + } + + if (!alreadyInAnotherFarm) + { + farms.push(portalsInFarm); + //console.log("Farm " + (farms.length) + ": " + portalsInFarm.length + " portals"); + }; + } + } + + //console.log(farms.length); + for (i = 0; i < farms.length; i++) + console.log("Farm " + (i+1) + ": " + farms[i].length + " portals"); + + + + window.plugin.farmFind.drawnItems = new L.FeatureGroup(); + + + for (farm = 0; farm < farms.length; farm++) + { + window.plugin.farmFind.drawCircle(farms[farm]); + } + + +}; + +window.plugin.farmFind.drawCircle = function(farm) +{ + var latArray = []; + var lngArray = []; + //console.log("Find Center"); + for (p = 0; p < farm.length; p++) + { + latArray.push(farm[p].getLatLng().lat); + lngArray.push(farm[p].getLatLng().lng); + } + + var north = Math.max.apply(null, lngArray); + var south = Math.min.apply(null, lngArray); + var east = Math.max.apply(null, latArray); + var west = Math.min.apply(null, latArray); + + var center = new google.maps.LatLng(((east + west) / 2), ((north + south) / 2)); + //console.log(center); + //console.log("Find Radius"); + var radius = 0; + for (p = 0; p < farm.length; p++) + { + var temp = center.distanceFrom(farm[p].getLatLng()); + if (temp > radius) + radius = temp; + } + + //console.log(radius); + //((20 - map._zoom) * 1000) / map._zoom + + var latlng = new L.LatLng(center.lat(), center.lng()); + //console.log("latlng: " + latlng); + var optCircle = {color:'red',opacity:0.7,fill:true,fillColor:'red',fillOpacity:0.7,weight:15,clickable:false}; + var circle = new L.Circle(latlng, radius, optCircle); + //console.log("circle: " + circle); + circle.addTo(window.plugin.farmFind.levelLayerGroup); +}; + + google.maps.LatLng.prototype.distanceFrom = function(newLatLng) { + + // setup our variables + var lat1 = this.lat(); + var radianLat1 = lat1 * ( Math.PI / 180 ); + + var lng1 = this.lng(); + var radianLng1 = lng1 * ( Math.PI / 180 ); + + var lat2 = newLatLng.lat; + var radianLat2 = lat2 * ( Math.PI / 180 ); + + var lng2 = newLatLng.lng; + var radianLng2 = lng2 * ( Math.PI / 180 ); + + // sort out the radius, MILES or KM? + var earth_radius = 6378.1; // (km = 6378.1) OR (miles = 3959) - radius of the earth + + // sort our the differences + var diffLat = ( radianLat1 - radianLat2 ); + var diffLng = ( radianLng1 - radianLng2 ); + // put on a wave (hey the earth is round after all) + var sinLat = Math.sin( diffLat / 2 ); + var sinLng = Math.sin( diffLng / 2 ); + + // maths - borrowed from http://www.opensourceconnections.com/wp-content/uploads/2009/02/clientsidehaversinecalculation.html + var a = Math.pow(sinLat, 2.0) + Math.cos(radianLat1) * Math.cos(radianLat2) * Math.pow(sinLng, 2.0); + + // work out the distance + var distance = earth_radius * 2 * Math.asin(Math.min(1, Math.sqrt(a))); + + // return the distance + return distance * 1000; +}; + + window.plugin.farmFind.setupCSS = function() { + $("