Planets.nu - Sensor Range Plugin

Plugin for Planets.nu which allows to visualize the sensor range of ships and planets on the starmap

  1. // ==UserScript==
  2. // @name Planets.nu - Sensor Range Plugin
  3. // @description Plugin for Planets.nu which allows to visualize the sensor range of ships and planets on the starmap
  4. // @namespace kedalion/sensorRange
  5. // @version 0.31
  6. // @date 2014-12-24
  7. // @author kedalion
  8. // @include http://planets.nu/*
  9. // @include http://play.planets.nu/*
  10. // @include http://test.planets.nu/*
  11. // @resource userscript https://greasyfork.org/en/scripts/7149-planets-nu-sensor-range-plugin
  12. // @homepage http://planets.nu/discussion/utility-script-sensor-range-plugin
  13.  
  14. // ==/UserScript==
  15.  
  16. /*
  17. Change log:
  18. 0.31 Moved script location to Greasyfork.org and added support for Sphere addon [2014-12-24]
  19. 0.3 Moved script location to monkeyguts.com [2014-05-30]
  20. 0.21 Added support for Dark Sense (created by Arcanix) [2014-04-09]
  21. 0.20 Fixed a bug which made sensor range include all planets [2013-08-11]
  22. 0.19 Change sensor range to ignore Safe Passage [2013-08-02]
  23. 0.18 Removed bug causing Firefox to show everything washed out [2013-08-01]
  24. 0.17 Added additional enemy sensor range view [2013-07-30]
  25. 0.15 Changed the sensor range display to joint area instead of individual circles [2013-07-29]
  26. 0.14 Added hotkey 'x' support [2013-07-25]
  27. 0.13 Fixed button behavior (time machine) [2013-07-25]
  28. 0.12 Minor fixes, ensure settings stay through turn changes (time machine) [2013-07-25]
  29. 0.11 Initial release [2013-07-25]
  30. */
  31.  
  32. function wrapper () { // wrapper for injection
  33. if (vgap.version < 3.0) {
  34. console.log("Sensor Range plugin requires at least NU version 3.0. Plugin disabled." );
  35. return;
  36. }
  37. var plugin_version = 0.31;
  38. console.log("Sensor Range plugin version: v" + plugin_version );
  39. /*
  40. * Specify your plugin
  41. * You need to have all those methods defined or errors will be thrown.
  42. * I inserted the print-outs in order to demonstrate when each method is
  43. * being called. Just comment them out once you know your way around.
  44. *
  45. * For any access to plugin class variables and methods from inside these
  46. * reserved methods, "vgap.plugins["nameOfMyPlugin"].my_variable" has to be
  47. * used instead of "this.my_variable".
  48. */
  49. var sensorRangePlugin = {
  50. /*
  51. * processload: executed whenever a turn is loaded: either the current turn or
  52. * an older turn through time machine
  53. */
  54. processload: function() {
  55. //console.log("ProcessLoad: plugin called.");
  56. //check if plugin data is for current game
  57. if (vgap.plugins["sensorRangePlugin"].gameid != vgap.gameId) {
  58. vgap.plugins["sensorRangePlugin"].reset();
  59. }
  60. },
  61. /*
  62. * loaddashboard: executed to rebuild the dashboard content after a turn is loaded
  63. */
  64. loaddashboard: function() {
  65. //console.log("LoadDashboard: plugin called.");
  66. },
  67.  
  68. /*
  69. * showdashboard: executed when switching from starmap to dashboard
  70. */
  71. showdashboard: function() {
  72. //console.log("ShowDashboard: plugin called.");
  73. },
  74. /*
  75. * showsummary: executed when returning to the main screen of the dashboard
  76. */
  77. showsummary: function() {
  78. //console.log("ShowSummary: plugin called.");
  79. },
  80. /*
  81. * loadmap: executed after the first turn has been loaded to create the map
  82. * as far as I can tell not executed again when using time machine
  83. */
  84. loadmap: function() {
  85. //console.log("LoadMap: plugin called.");
  86. vgap.plugins["sensorRangePlugin"].reset();
  87. vgap.plugins["sensorRangePlugin"].addMapTool("Sensor Range", "ShowMinerals", "sensorrange", function () {
  88. if (vgap.plugins["sensorRangePlugin"].enabled) {
  89. if (vgap.plugins["sensorRangePlugin"].mode == 0) {
  90. vgap.plugins["sensorRangePlugin"].enabled = false;
  91. }
  92. } else {
  93. vgap.plugins["sensorRangePlugin"].enabled = true;
  94. }
  95. vgap.plugins["sensorRangePlugin"].mode = 0;
  96. vgap.plugins["sensorRangePlugin"].syncButtons();
  97. vgap.map.draw();
  98. });
  99. vgap.plugins["sensorRangePlugin"].addMapTool("MineDetectRange", "ShowMinerals", "minedetectrange", function () {
  100. if (vgap.plugins["sensorRangePlugin"].enabled) {
  101. if (vgap.plugins["sensorRangePlugin"].mode == 1) {
  102. vgap.plugins["sensorRangePlugin"].enabled = false;
  103. }
  104. } else {
  105. vgap.plugins["sensorRangePlugin"].enabled = true;
  106. }
  107. vgap.plugins["sensorRangePlugin"].mode = 1;
  108. vgap.plugins["sensorRangePlugin"].syncButtons();
  109. vgap.map.draw();
  110. });
  111. vgap.plugins["sensorRangePlugin"].addMapTool("EnemySense", "ShowMinerals", "enemysensorrange", function () {
  112. if (vgap.plugins["sensorRangePlugin"].enabled) {
  113. if (vgap.plugins["sensorRangePlugin"].mode == 2) {
  114. vgap.plugins["sensorRangePlugin"].enabled = false;
  115. }
  116. } else {
  117. vgap.plugins["sensorRangePlugin"].enabled = true;
  118. }
  119. vgap.plugins["sensorRangePlugin"].mode = 2;
  120. vgap.plugins["sensorRangePlugin"].syncButtons();
  121. vgap.map.draw();
  122. });
  123. if (vgap.player.raceid == 8) {
  124. vgap.plugins["sensorRangePlugin"].addMapTool("Dark Sense", "ShowMinerals", "darksenserange", function () {
  125. if (vgap.plugins["sensorRangePlugin"].enabled) {
  126. if (vgap.plugins["sensorRangePlugin"].mode == 3) {
  127. vgap.plugins["sensorRangePlugin"].enabled = false;
  128. }
  129. } else {
  130. vgap.plugins["sensorRangePlugin"].enabled = true;
  131. }
  132. vgap.plugins["sensorRangePlugin"].mode = 3;
  133. vgap.plugins["sensorRangePlugin"].syncButtons();
  134. vgap.map.draw();
  135. });
  136. }
  137. },
  138. /*
  139. * showmap: executed when switching from dashboard to starmap
  140. */
  141. showmap: function() {
  142. //console.log("ShowMap: plugin called.");
  143. },
  144. /*
  145. * draw: executed on any click or drag on the starmap
  146. */
  147. draw: function() {
  148. //console.log("Draw: plugin called.");
  149. if (vgap.plugins["sensorRangePlugin"].enabled) {
  150. if (vgap.plugins["sensorRangePlugin"].mode == 0) {
  151. var ctx = vgap.map.ctx;
  152. //ctx.clearRect(0, 0, vgap.map.canvas.width, vgap.map.canvas.height);
  153. //ctx.globalAlpha = 0.2;
  154. vgap.plugins["sensorRangePlugin"].showSensorRange();
  155. //ctx.globalAlpha = 1.0;
  156. //vgap.plugins["sensorRangePlugin"].drawEssentials(false, ctx, false);
  157. } else if (vgap.plugins["sensorRangePlugin"].mode == 2) {
  158. var ctx = vgap.map.ctx;
  159. vgap.plugins["sensorRangePlugin"].showEnemySensorRange();
  160. } else if (vgap.plugins["sensorRangePlugin"].mode == 3) {
  161. vgap.plugins["sensorRangePlugin"].showDarkSenseRange();
  162. } else {
  163. vgap.plugins["sensorRangePlugin"].showMineDetectRange();
  164. }
  165. }
  166. },
  167. /*
  168. * loadplanet: executed a planet is selected on dashboard or starmap
  169. */
  170. loadplanet: function() {
  171. //console.log("LoadPlanet: plugin called.");
  172. },
  173. /*
  174. * loadstarbase: executed a planet is selected on dashboard or starmap
  175. */
  176. loadstarbase: function() {
  177. //console.log("LoadStarbase: plugin called.");
  178. },
  179. /*
  180. * loadship: executed a planet is selected on dashboard or starmap
  181. */
  182. loadship: function() {
  183. //console.log("LoadShip: plugin called.");
  184. },
  185. version: plugin_version,
  186. plugin_name: "SensorRangePlugin",
  187. enabled: false,
  188. mode: 0, //0: range to detect ships and planets, 1: range of minefield detection, 2: Enemy Sensor Range, 3: Dark Sense
  189. gameid: -1,
  190. oldClearTools: null,
  191. canvas_shipscan: null,
  192. canvas_planetscan: null,
  193.  
  194. reset: function () {
  195. //reset plugin settings
  196. this.enabled = false;
  197. this.mode = 0;
  198. this.gameid = vgap.gameId;
  199. //make sure the buttons represent the actual plugin state
  200. this.syncButtons();
  201. },
  202. syncButtons: function () {
  203. //make sure the buttons represent the actual plugin state
  204. if (document.getElementById("sensorrange") != null) {
  205. var sensor_on = (document.getElementById("sensorrange").className.indexOf("selectedmaptool") != -1);
  206. if ((sensor_on && !this.enabled) || (!sensor_on && this.enabled && this.mode == 0) || (sensor_on && this.enabled && this.mode != 0)) {
  207. $('#sensorrange').toggleClass("selectedmaptool");
  208. }
  209. }
  210. if (document.getElementById("minedetectrange") != null) {
  211. var mine_on = (document.getElementById("minedetectrange").className.indexOf("selectedmaptool") != -1);
  212. if ((mine_on && !this.enabled) || (!mine_on && this.enabled && this.mode == 1) || (mine_on && this.enabled && this.mode != 1)) {
  213. $('#minedetectrange').toggleClass("selectedmaptool");
  214. }
  215. }
  216. if (document.getElementById("enemysensorrange") != null) {
  217. var enemy_on = (document.getElementById("enemysensorrange").className.indexOf("selectedmaptool") != -1);
  218. if ((enemy_on && !this.enabled) || (!enemy_on && this.enabled && this.mode == 2) || (enemy_on && this.enabled && this.mode != 2)) {
  219. $('#enemysensorrange').toggleClass("selectedmaptool");
  220. }
  221. }
  222. if (document.getElementById("darksenserange") != null) {
  223. var dark_on = (document.getElementById("darksenserange").className.indexOf("selectedmaptool") != -1);
  224. if ((dark_on && !this.enabled) || (!dark_on && this.enabled && this.mode == 3) || (dark_on && this.enabled && this.mode != 3)) {
  225. $('#darksenserange').toggleClass("selectedmaptool");
  226. }
  227. }
  228. },
  229. addMapTool: function (text, cls, id, onclick) {
  230. $("<li class='" + cls + "' id='" + id + "'>" + text + "</li>").tclick(onclick).appendTo("#MapTools");
  231. },
  232.  
  233. /**
  234. * Determine if the player has an agreement with another player which will give him information.
  235. * Requires at least 'share intel' from the other player.
  236. * @param int id of target player
  237. * @return bool true if player is delivering information
  238. */
  239. receiveInfoFrom: function (fromId) {
  240. var relation = vgap.getRelation(fromId);
  241. if (relation == null)
  242. return false;
  243. return relation.relationfrom >= 3;
  244. },
  245.  
  246. /**
  247. * Draw the sensor range circles around own and allied ships and planets. Two different colors are used for
  248. * ship and planet scan range. Planetscanrange is omitted if set to infinity (not explore map setting)
  249. */
  250. showEnemySensorRange: function () {
  251. if (this.canvas_planetscan == null) {
  252. this.canvas_planetscan = document.createElement("canvas");
  253. }
  254. this.canvas_planetscan.width = vgap.map.canvas.width;
  255. this.canvas_planetscan.height = vgap.map.canvas.height;
  256. var ctx_planetscan = this.canvas_planetscan.getContext("2d");
  257. ctx_planetscan.clearRect(0, 0, vgap.map.canvas.width, vgap.map.canvas.height);
  258. var color_planetscan = "#FF8000";
  259. var alpha_planetscan = 1.0;
  260. if (this.canvas_shipscan == null) {
  261. this.canvas_shipscan = document.createElement("canvas");
  262. }
  263. this.canvas_shipscan.width = vgap.map.canvas.width;
  264. this.canvas_shipscan.height = vgap.map.canvas.height;
  265. var ctx_shipscan = this.canvas_shipscan.getContext("2d");
  266. ctx_shipscan.clearRect(0, 0, vgap.map.canvas.width, vgap.map.canvas.height);
  267. var color_shipscan = "#F5D0A9";
  268. var alpha_shipscan = 1.0;
  269. //var ctx = vgap.map.ctx;
  270. //ctx.globalAlpha = 0.1;
  271. for (var i = 0; i < vgap.ships.length; i++) {
  272. var ship = vgap.ships[i];
  273. //skip ship if owned by player or ally/neutral -> we want enemies
  274. if (ship.ownerid == vgap.player.id || (vgap.allied(ship.ownerid) && vgap.alliedTo(ship.ownerid))) {
  275. continue;
  276. }
  277.  
  278. //draw a circle for planet visibility around each ship
  279. if (vgap.settings.planetscanrange < 10000) {
  280.  
  281. //console.log("ship #" + ship.id + " planet range");
  282. //check if area is visible
  283. if (vgap.map.isVisible(ship.x, ship.y, vgap.settings.planetscanrange)) {
  284. //ctx_planetscan.strokeStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  285. //ctx_planetscan.lineWidth = 2;
  286. ctx_planetscan.fillStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  287. ctx_planetscan.beginPath();
  288. ctx_planetscan.arc(vgap.map.screenX(ship.x), vgap.map.screenY(ship.y), vgap.settings.planetscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  289. ctx_planetscan.closePath();
  290. ctx_planetscan.fill();
  291. //ctx_planetscan.stroke();
  292. }
  293. }
  294. //draw a circle for ship visibility around each ship
  295. //check if area is visible
  296. if (vgap.map.isVisible(ship.x, ship.y, vgap.settings.shipscanrange)) {
  297. //console.log("ship #" + ship.id + " ship range");
  298. //ctx_shipscan.strokeStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  299. //ctx_shipscan.lineWidth = 2;
  300. ctx_shipscan.fillStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  301. ctx_shipscan.beginPath();
  302. ctx_shipscan.arc(vgap.map.screenX(ship.x), vgap.map.screenY(ship.y), vgap.settings.shipscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  303. ctx_shipscan.closePath();
  304. ctx_shipscan.fill();
  305. //ctx_shipscan.stroke();
  306. }
  307. }
  308. for (var i = 0; i < vgap.planets.length; i++) {
  309. var planet = vgap.planets[i];
  310. //skip planet if not owned by player or ally/neutral
  311. if (planet.ownerid == vgap.player.id || (vgap.allied(planet.ownerid) && vgap.alliedTo(planet.ownerid))) {
  312. continue;
  313. }
  314.  
  315. //skip unowned planets
  316. if (planet.ownerid == 0 ) {
  317. continue;
  318. }
  319. //draw a circle for planet visibility around each ship
  320. if (vgap.settings.planetscanrange < 10000) {
  321.  
  322. //console.log("planet #" + planet.id + " planet range");
  323. //check if area is visible
  324. if (vgap.map.isVisible(planet.x, planet.y, vgap.settings.planetscanrange)) {
  325. //ctx_planetscan.strokeStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  326. //ctx_planetscan.lineWidth = 3;
  327. ctx_planetscan.fillStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  328. ctx_planetscan.beginPath();
  329. ctx_planetscan.arc(vgap.map.screenX(planet.x), vgap.map.screenY(planet.y), vgap.settings.planetscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  330. ctx_planetscan.closePath();
  331. ctx_planetscan.fill();
  332. //ctx_planetscan.stroke();
  333. }
  334. }
  335. //draw a circle for ship visibility around each planet
  336. //check if area is visible
  337. if (vgap.map.isVisible(planet.x, planet.y, vgap.settings.shipscanrange)) {
  338. //console.log("planet #" + planet.id + " ship range");
  339. //ctx_shipscan.strokeStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  340. //ctx_shipscan.lineWidth = 3;
  341. ctx_shipscan.fillStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  342. ctx_shipscan.beginPath();
  343. ctx_shipscan.arc(vgap.map.screenX(planet.x), vgap.map.screenY(planet.y), vgap.settings.shipscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  344. ctx_shipscan.closePath();
  345. ctx_shipscan.fill();
  346. //ctx_shipscan.stroke();
  347. }
  348. }
  349. vgap.map.ctx.globalAlpha = 0.2;
  350. if ( vgap.settings.planetscanrange == 10000 ) {
  351. //console.log("shipscan only1");
  352. vgap.map.ctx.drawImage(this.canvas_shipscan, 0, 0);
  353. //console.log("shipscan only2");
  354. } else if (vgap.settings.shipscanrange > vgap.settings.planetscanrange) {
  355. //console.log("shipsscan first1");
  356. ctx_shipscan.drawImage(this.canvas_planetscan, 0, 0);
  357. vgap.map.ctx.drawImage(this.canvas_shipscan, 0, 0);
  358. //console.log("shipsscan first2");
  359. } else {
  360. //console.log("shipsscan last1");
  361. ctx_planetscan.drawImage(this.canvas_shipscan, 0, 0);
  362. vgap.map.ctx.drawImage(this.canvas_planetscan, 0, 0);
  363. //console.log("shipsscan last2");
  364. }
  365. vgap.map.ctx.globalAlpha = 1.0;
  366. },
  367. /**
  368. * Draw the sensor range circles around own and allied ships and planets. Two different colors are used for
  369. * ship and planet scan range. Planetscanrange is omitted if set to infinity (not explore map setting)
  370. */
  371. showSensorRange: function () {
  372. if (this.canvas_planetscan == null) {
  373. this.canvas_planetscan = document.createElement("canvas");
  374. }
  375. this.canvas_planetscan.width = vgap.map.canvas.width;
  376. this.canvas_planetscan.height = vgap.map.canvas.height;
  377. var ctx_planetscan = this.canvas_planetscan.getContext("2d");
  378. ctx_planetscan.clearRect(0, 0, vgap.map.canvas.width, vgap.map.canvas.height);
  379. var color_planetscan = "#FF8000";
  380. var alpha_planetscan = 1.0;
  381. if (this.canvas_shipscan == null) {
  382. this.canvas_shipscan = document.createElement("canvas");
  383. }
  384. this.canvas_shipscan.width = vgap.map.canvas.width;
  385. this.canvas_shipscan.height = vgap.map.canvas.height;
  386. var ctx_shipscan = this.canvas_shipscan.getContext("2d");
  387. ctx_shipscan.clearRect(0, 0, vgap.map.canvas.width, vgap.map.canvas.height);
  388. var color_shipscan = "#F5D0A9";
  389. var alpha_shipscan = 1.0;
  390. //var ctx = vgap.map.ctx;
  391. //ctx.globalAlpha = 0.1;
  392. for (var i = 0; i < vgap.ships.length; i++) {
  393. var ship = vgap.ships[i];
  394. //skip ship if not owned by player or ally
  395. if (ship.ownerid != vgap.player.id && !this.receiveInfoFrom(ship.ownerid)) { // (vgap.allied(ship.ownerid) && vgap.alliedTo(ship.ownerid))) {
  396. continue;
  397. }
  398.  
  399. //draw a circle for planet visibility around each ship
  400. if (vgap.settings.planetscanrange < 10000) {
  401.  
  402. //console.log("ship #" + ship.id + " planet range");
  403. //check if area is visible
  404. if (vgap.map.isVisible(ship.x, ship.y, vgap.settings.planetscanrange)) {
  405. //ctx_planetscan.strokeStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  406. //ctx_planetscan.lineWidth = 2;
  407. ctx_planetscan.fillStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  408. ctx_planetscan.beginPath();
  409. ctx_planetscan.arc(vgap.map.screenX(ship.x), vgap.map.screenY(ship.y), vgap.settings.planetscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  410. ctx_planetscan.closePath();
  411. ctx_planetscan.fill();
  412. //ctx_planetscan.stroke();
  413. }
  414. }
  415. //draw a circle for ship visibility around each ship
  416. //check if area is visible
  417. if (vgap.map.isVisible(ship.x, ship.y, vgap.settings.shipscanrange)) {
  418. //console.log("ship #" + ship.id + " ship range");
  419. //ctx_shipscan.strokeStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  420. //ctx_shipscan.lineWidth = 2;
  421. ctx_shipscan.fillStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  422. ctx_shipscan.beginPath();
  423. ctx_shipscan.arc(vgap.map.screenX(ship.x), vgap.map.screenY(ship.y), vgap.settings.shipscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  424. ctx_shipscan.closePath();
  425. ctx_shipscan.fill();
  426. //ctx_shipscan.stroke();
  427. }
  428. }
  429. for (var i = 0; i < vgap.planets.length; i++) {
  430. var planet = vgap.planets[i];
  431. //skip planet if not owned by player or ally
  432. if (planet.ownerid != vgap.player.id && !this.receiveInfoFrom(planet.ownerid)) { //!(vgap.allied(planet.ownerid) && vgap.alliedTo(planet.ownerid))) {
  433. continue;
  434. }
  435.  
  436. //draw a circle for planet visibility around each ship
  437. if (vgap.settings.planetscanrange < 10000) {
  438.  
  439. //console.log("planet #" + planet.id + " planet range");
  440. //check if area is visible
  441. if (vgap.map.isVisible(planet.x, planet.y, vgap.settings.planetscanrange)) {
  442. //ctx_planetscan.strokeStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  443. //ctx_planetscan.lineWidth = 3;
  444. ctx_planetscan.fillStyle = colorToRGBA(color_planetscan, alpha_planetscan);
  445. ctx_planetscan.beginPath();
  446. ctx_planetscan.arc(vgap.map.screenX(planet.x), vgap.map.screenY(planet.y), vgap.settings.planetscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  447. ctx_planetscan.closePath();
  448. ctx_planetscan.fill();
  449. //ctx_planetscan.stroke();
  450. }
  451. }
  452. //draw a circle for ship visibility around each planet
  453. //check if area is visible
  454. if (vgap.map.isVisible(planet.x, planet.y, vgap.settings.shipscanrange)) {
  455. //console.log("planet #" + planet.id + " ship range");
  456. //ctx_shipscan.strokeStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  457. //ctx_shipscan.lineWidth = 3;
  458. ctx_shipscan.fillStyle = colorToRGBA(color_shipscan, alpha_shipscan);
  459. ctx_shipscan.beginPath();
  460. ctx_shipscan.arc(vgap.map.screenX(planet.x), vgap.map.screenY(planet.y), vgap.settings.shipscanrange * vgap.map.zoom, 0, Math.PI * 2, false);
  461. ctx_shipscan.closePath();
  462. ctx_shipscan.fill();
  463. //ctx_shipscan.stroke();
  464. }
  465. }
  466. vgap.map.ctx.globalAlpha = 0.2;
  467. if ( vgap.settings.planetscanrange == 10000 ) {
  468. vgap.map.ctx.drawImage(this.canvas_shipscan, 0, 0);
  469. } else if (vgap.settings.shipscanrange > vgap.settings.planetscanrange) {
  470. ctx_shipscan.drawImage(this.canvas_planetscan, 0, 0);
  471. vgap.map.ctx.drawImage(this.canvas_shipscan, 0, 0);
  472. } else {
  473. ctx_planetscan.drawImage(this.canvas_shipscan, 0, 0);
  474. vgap.map.ctx.drawImage(this.canvas_planetscan, 0, 0);
  475. }
  476. vgap.map.ctx.globalAlpha = 1.0;
  477. },
  478. /**
  479. * Draw the mine detection range circles around own ships that have mission 'mine sweep' set.
  480. */
  481. showMineDetectRange: function () {
  482. //console.log("show mine detect range");
  483. var ctx = vgap.map.ctx;
  484. var minefield_detect_range = 200;
  485. for (var i = 0; i < vgap.ships.length; i++) {
  486. var ship = vgap.ships[i];
  487. //skip ship if not owned by player
  488. if (ship.ownerid != vgap.player.id) { // && !(vgap.allied(ship.ownerid) && vgap.alliedTo(ship.ownerid))) {
  489. continue;
  490. }
  491. var alpha = 0.2;
  492. var color = "#81F7F3";
  493. //negative ship ids come from sphere addon. Mission changes are only stored in their positive
  494. //id counter part
  495. if (ship.id < 0) {
  496. var ship_pos = vgap.getShip(-ship.id);
  497. if (ship_pos == null || ship_pos.mission != 1) {
  498. continue;
  499. }
  500. } else {
  501. if (ship.mission != 1) {
  502. continue;
  503. //alpha = 0.05;
  504. //color = "#F7FE2E";
  505. }
  506. }
  507. //draw a circle for ship visibility around each ship
  508. //check if area is visible
  509. if (vgap.map.isVisible(ship.x, ship.y, minefield_detect_range)) {
  510. //console.log("ship #" + ship.id + " mine range");
  511. ctx.strokeStyle = colorToRGBA(color, 0.4);
  512. ctx.lineWidth = 2;
  513. ctx.fillStyle = colorToRGBA(color, alpha);
  514. ctx.beginPath();
  515. ctx.arc(vgap.map.screenX(ship.x), vgap.map.screenY(ship.y), minefield_detect_range * vgap.map.zoom, 0, Math.PI * 2, false);
  516. ctx.closePath();
  517. //ctx.fill();
  518. ctx.stroke();
  519. }
  520. }
  521. },
  522. /**
  523. * Draw the dark sense range circles around own ships that have mission 'dark sense' set.
  524. */
  525. showDarkSenseRange: function () {
  526. //console.log("show dark sense range");
  527. if (vgap.player.raceid != 8) { return; }
  528. var ctx = vgap.map.ctx;
  529. var darksense_range = 200;
  530. for (var i = 0; i < vgap.ships.length; i++) {
  531. var ship = vgap.ships[i];
  532. //skip ship if not owned by player
  533. if (ship.ownerid != vgap.player.id) { // && !(vgap.allied(ship.ownerid) && vgap.alliedTo(ship.ownerid))) {
  534. continue;
  535. }
  536. var alpha = 0.2;
  537. var color = "#909090";
  538. //negative ship ids come from sphere addon. Mission changes are only stored in their positive
  539. //id counter part
  540. if (ship.id < 0) {
  541. var ship_pos = vgap.getShip(-ship.id);
  542. if (ship_pos == null || ship_pos.mission != 8) {
  543. continue;
  544. }
  545. } else {
  546. if (ship.mission != 8) {
  547. continue;
  548. }
  549. }
  550. //draw a circle for ship visibility around each ship
  551. //check if area is visible
  552. if (vgap.map.isVisible(ship.x, ship.y, darksense_range)) {
  553. //console.log("ship #" + ship.id + " mine range");
  554. ctx.strokeStyle = colorToRGBA(color, 0.4);
  555. ctx.lineWidth = 2;
  556. ctx.fillStyle = colorToRGBA(color, alpha);
  557. ctx.beginPath();
  558. ctx.arc(vgap.map.screenX(ship.x), vgap.map.screenY(ship.y), darksense_range * vgap.map.zoom, 0, Math.PI * 2, false);
  559. ctx.closePath();
  560. //ctx.fill();
  561. ctx.stroke();
  562. }
  563. }
  564. }
  565. };
  566. // register your plugin with NU
  567. vgap.registerPlugin(sensorRangePlugin, "sensorRangePlugin");
  568.  
  569. /**
  570. * Overload the clearTools function in order to be able to
  571. * disable the sensor range map through hotkey 'x'
  572. */
  573. vgap.plugins["sensorRangePlugin"].oldClearTools = vgapMap.prototype.clearTools;
  574. vgapMap.prototype.clearTools = function(result) {
  575. vgap.plugins["sensorRangePlugin"].enabled = false;
  576. vgap.plugins["sensorRangePlugin"].syncButtons();
  577. //execute the normal clearTools function
  578. vgap.plugins["sensorRangePlugin"].oldClearTools.apply(this,arguments);
  579. };
  580. } //wrapper for injection
  581.  
  582. var script = document.createElement("script");
  583. script.type = "application/javascript";
  584. script.textContent = "(" + wrapper + ")();";
  585.  
  586. document.body.appendChild(script);
  587. document.body.removeChild(script);