VPA-Keyboard

For planets.nu -- Add VPA-key strokes, and commands

  1. // ==UserScript==
  2. // @name VPA-Keyboard
  3. // @author Quapla
  4. // @copyright Basecode: Kero van Gelder, 2016 - latest: Quapla
  5. // @license Lesser Gnu Public License, version 3
  6. // @downloadURL
  7. // @description For planets.nu -- Add VPA-key strokes, and commands
  8. // @namespace quapla/VPA-Keyboard
  9. // @include http://planets.nu/*
  10. // @include http://play.planets.nu/*
  11. // @include http://test.planets.nu/*
  12. // @version 1.0.8
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. // Reference: Kero van Gelder's Keyboard: http://chmeee.org/ext/planets.nu/Keyboard.user.js
  17.  
  18. // Changes by Quapla:
  19. // Use Arrows to select ships
  20. // --> LEFT/RIGHT numerical previous/next
  21. // --> UP/DOWN previous/next at same position as planet -> base -> ships (numerical) -> again planet
  22. // --> '#' sets all ships at position "ready" and moves to next ship (not ready yet)
  23. // --> ' ' toggles move mode to set waypoints with cursors, use SHIFT for 81LY
  24.  
  25. sharedContent.prototype.planetSurvey_ori = sharedContent.prototype.planetSurvey;
  26. sharedContent.prototype.planetSurvey = function (planetId) {
  27. vgap.currentPlanetSurvey = planetId;
  28. this.planetSurvey_ori(planetId);
  29. }
  30.  
  31. sharedContent.prototype.editNote_ori = sharedContent.prototype.editNote;
  32. sharedContent.prototype.editNote = function (noteId, noteType) {
  33. vgap.currentNoteId = noteId;
  34. vgap.currentNoteType = noteType;
  35. this.editNote_ori(noteId, noteType);
  36. $("#EditNote").focus();
  37. }
  38.  
  39. vgapShipScreen.prototype.shipMission_ori = vgapShipScreen.prototype.shipMission;
  40. vgapShipScreen.prototype.shipMission = function () {
  41. this.shipMissionOpen = true;
  42. this.shipMission_ori();
  43. }
  44.  
  45. vgapShipScreen.prototype.selectMission_ori = vgapShipScreen.prototype.selectMission;
  46. vgapShipScreen.prototype.selectMission = function (missionId) {
  47. this.selectMission_ori(missionId);
  48. this.shipMissionOpen = false;
  49. }
  50.  
  51. vgapShipScreen.prototype.primaryEnemy_ori = vgapShipScreen.prototype.primaryEnemy;
  52. vgapShipScreen.prototype.primaryEnemy = function () {
  53. this.primaryEnemyOpen = true;
  54. this.primaryEnemy_ori();
  55. }
  56. vgapShipScreen.prototype.selectEnemy_ori = vgapShipScreen.prototype.selectEnemy;
  57. vgapShipScreen.prototype.selectEnemy = function (enemyId) {
  58. this.selectEnemy_ori(enemyId);
  59. this.primaryEnemyOpen = false;
  60. }
  61. vgaPlanets.prototype.closeMore_ori = vgaPlanets.prototype.closeMore;
  62. vgaPlanets.prototype.closeMore = function (callback) {
  63. vgap.closeMore_ori(callback);
  64. this.primaryEnemyOpen = false;
  65. vgap.currentPlanetSurvey = null;
  66. vgap.shipScreen.shipMissionOpen = false;
  67. }
  68.  
  69. vgapMap.prototype.selectPlanet_ori = vgapMap.prototype.selectPlanet;
  70. vgapMap.prototype.selectPlanet = function (planet) {
  71. vgaPlanets.prototype.Quapla_Move = false;
  72. this.selectPlanet_ori(planet);
  73. }
  74.  
  75. vgapMap.prototype.selectShip_ori = vgapMap.prototype.selectShip;
  76. vgapMap.prototype.selectShip = function (ship) {
  77. vgaPlanets.prototype.Quapla_Move = false;
  78. this.selectShip_ori(ship);
  79. }
  80. vgapMap.prototype.drawPlanetNames = function (e, f) {
  81. if (vgap.map.planetnames) {
  82. for (var a = 0; a < vgap.planets.length; a++) {
  83. var c = vgap.planets[a];
  84. if (this.isVisible(c.x, c.y, this.planetRad(c))) {
  85. var show = c.id + "- " + c.name;
  86. var ctx = vgap.map.ctx;
  87. ctx.textAlign = "center";
  88. //ctx.fillStyle = color;
  89. ctx.fillText(show, this.screenX(c.x), (this.screenY(c.y)-10));
  90. }
  91. }
  92. } else {
  93. //$(".PlanetName").remove()
  94. vgapMap.prototype.reload();
  95. }
  96. /* drawPlanetNames : function (x, y) {
  97. for (var i = 0; i < vgap.planets.length; i++) {
  98. var planet = vgap.planets[i];
  99. var left = this.screenX(planet.x) + 10;
  100. var top = this.screenY(planet.y) - 15;
  101. $("<div class='PlanetName' style='left:" + left + "px;top:" + top + "px;'>" + planet.id + ": " + planet.name + "</div>").appendTo(this.mapCover);
  102. }
  103. */
  104.  
  105. }
  106.  
  107.  
  108. var hotkey_ori = vgaPlanets.prototype.hotkey;
  109. vgaPlanets.prototype.hotkey = function (event) {
  110. //console.log("KC: "+event.keyCode);
  111. if (vgap.shipScreen.primaryEnemyOpen) {
  112. var enemy = 0;
  113. if (event.keyCode >= 48 && event.keyCode <= 57)
  114. enemy = event.keyCode - 48;
  115. if (event.keyCode >= 65 && event.keyCode <= 90)
  116. enemy = event.keyCode - 65 + 10;
  117. if (enemy <= vgap.players.length) {
  118. vgap.shipScreen.selectEnemy(enemy);
  119. return;
  120. }
  121. }
  122.  
  123. if (vgap.shipScreen.shipMissionOpen) {
  124. var missionId = -1;
  125. if (event.keyCode == 67)
  126. missionId = 9; // 'c', Cloak
  127. else if (event.keyCode == 69)
  128. missionId = 0; // 'e', Explore
  129. else if (event.keyCode == 73)
  130. missionId = 7; // 'i', Intercept
  131. else if (event.keyCode == 75)
  132. missionId = 3; // 'k', Kill
  133. else if (event.keyCode == 76)
  134. missionId = 2; // 'l', Lay Mines
  135. else if (event.keyCode == 77)
  136. missionId = 1; // 'm', Mine Sweep
  137. else if (event.keyCode == 83 && event.shiftKey)
  138. missionId = 8; // 'S', Special / Super Spy
  139. else if (event.keyCode == 83)
  140. missionId = 4; // 's', Sensor Sweep
  141. else if (event.keyCode == 84)
  142. missionId = 6; // 't', Tow
  143. if (missionId >= 0) {
  144. vgap.shipScreen.selectMission(missionId);
  145. return;
  146. }
  147. }
  148.  
  149. if (vgap.editNoteOpen) {
  150. if (event.keyCode == 13 && event.ctrlKey) {
  151. this.saveNote(vgap.currentNoteId, vgap.currentNoteType);
  152. return;
  153. }
  154. }
  155.  
  156. if (event.keyCode == 27) {
  157. if (this.findObjectFeatureActivated)
  158. vgap.map.hideFindObjectFeature();
  159. else if (vgap.moreOpen)
  160. this.closeMore();
  161. else if (this.lcOpen)
  162. this.deselectAll();
  163. return;
  164. }
  165.  
  166. /* TAB not working properly in browsers
  167. if (event.keyCode == 9) { //TAB}
  168. if (event.shiftKey) vgap.map.zoomout()
  169. else vgap.map.zoomin()
  170. return;
  171. }
  172. */
  173.  
  174. if (vgap.hotkeysOn) {
  175. switch (event.keyCode) {
  176. case 8:
  177. if ((this.starbaseScreenOpen || this.planetScreenOpen || this.shipScreenOpen) && !this.moreOpen) {
  178. vgap.rPrev();
  179. return;
  180. }
  181. case 78: // 'n'
  182. if (event.shiftKey) {
  183. if (this.planetScreenOpen) {
  184. shtml.editNote(vgap.planetScreen.planet.id, 1);
  185. return;
  186. } else if (vgap.currentPlanetSurvey > 0) { // ship screen is open, but unowned planet is selected
  187. shtml.editNote(vgap.currentPlanetSurvey, 1);
  188. return
  189. } else if (this.shipScreenOpen) {
  190. shtml.editNote(vgap.shipScreen.ship.id, 2);
  191. return;
  192. } else if (this.starbaseScreenOpen) {
  193. shtml.editNote(vgap.starbaseScreen.starbase.id, 3);
  194. return;
  195. }
  196. }
  197. break;
  198. case 88: // 'x'
  199. this.map.clearTools();
  200. return;
  201. case 90: // 'z'
  202. this.map.zoomFarInOrOut_OnWaypointOrPlanet();
  203. return;
  204.  
  205. case 32: //space bar
  206. if (this.planetScreenOpen || this.shipScreenOpen) {
  207. vgaPlanets.prototype.Quapla_Move = !vgaPlanets.prototype.Quapla_Move;
  208. return;
  209. }
  210.  
  211. //case 188: // '<' if (event.shiftKey)
  212. case 38: // Arrow up
  213. if (vgaPlanets.prototype.Quapla_Move)
  214. if (event.shiftKey)
  215. this.map.moveSelect(0, 81);
  216. else
  217. this.map.moveSelect(0, 1);
  218. else
  219. vgap.unrotateActiveObject();
  220. return;
  221. // case 190: // '>'
  222. case 40: // Arrow-Down if (event.shiftKey)
  223. if (vgaPlanets.prototype.Quapla_Move)
  224. if (event.shiftKey)
  225. this.map.moveSelect(0, -81);
  226. else
  227. this.map.moveSelect(0, -1);
  228. else
  229. vgap.rotateActiveObject();
  230. return;
  231. case 39: // Arrow right // if (ev.keyCode == 39 && this.shipScreenOpen || this.planetScreenOpen)
  232. if (vgaPlanets.prototype.Quapla_Move)
  233. if (event.shiftKey)
  234. this.map.moveSelect(81, 0);
  235. else
  236. this.map.moveSelect(1, 0);
  237. else
  238. this.rNext();
  239. return;
  240. case 37: // Arrow left // if (ev.keyCode == 39 && this.shipScreenOpen || this.planetScreenOpen)
  241. if (vgaPlanets.prototype.Quapla_Move)
  242. if (event.shiftKey)
  243. this.map.moveSelect(-81, 0);
  244. else
  245. this.map.moveSelect(-1, 0);
  246. else
  247. this.rPrev();
  248. return;
  249.  
  250. case 163: // '#' set ships/planet ready "Firefox"
  251. case 191: // Opera
  252. {
  253. var item = 0;
  254. var item2 = 0;
  255. if (this.planetScreenOpen) {
  256. var item = vgap.planetScreen.planet.id;
  257. vgap.planetScreen.planet.readystatus = 1;
  258. } else if (this.starbaseScreenOpen) {
  259. item = vgap.starbaseScreen.starbase.id;
  260. vgap.starbaseScreen.starbase.readystatus = 1;
  261. } else if (this.shipScreenOpen) {
  262. var ship = vgap.shipScreen.ship;
  263. item = ship.id;
  264. ship.readystatus = 1; // Set ready
  265. var ships = vgap.shipsAt(ship.x, ship.y)
  266. var index = ships.indexOf(ship);
  267. for (var i = 0; i < ships.length; i++) {
  268. if (ships[i].ownerid == vgap.player.id)
  269. if (ships[i].readystatus == 0)
  270. ships[i].readystatus = 1;
  271. }
  272. }
  273. do {
  274. this.rNext();
  275. if (this.planetScreenOpen) {
  276. if (vgap.planetScreen.planet.readystatus < 1)
  277. break; // This is the next "unready" one...
  278. item2 = vgap.planetScreen.planet.id;
  279. } else if (this.starbaseScreenOpen) {
  280. if (vgap.starbaseScreen.starbase.readystatus < 1)
  281. break;
  282. item2 = vgap.starbaseScreen.starbase.id;
  283. } else if (this.shipScreenOpen) {
  284. if (vgap.shipScreen.ship.readystatus < 1)
  285. break;
  286. item2 = vgap.shipScreen.ship.id;
  287. }
  288. } while (item != item2); // until
  289. return;
  290. }
  291. case 80: // 'P' && (!this.planetScreenOpen && !this.starbaseScreenOpen))
  292. case 112: // 'p'
  293. {
  294. vgap.map.planetnames = !vgap.map.planetnames;
  295. vgap.map.drawPlanetNames();
  296. return;
  297. }
  298. } // End Case
  299. }
  300. hotkey_ori.apply(this, arguments);
  301. //this.hotkey_ori(event);
  302. }
  303.  
  304. vgapMap.prototype.zoomFarInOrOut_OnWaypointOrPlanet = function () {
  305. var zoomedOut = (this.zoom < 29);
  306. if (zoomedOut) {
  307. this.zoomFactorBeforeZoomingInDeeply = this.zoom;
  308. this.setZoom(42);
  309. } else {
  310. this.setZoom(this.zoomFactorBeforeZoomingInDeeply);
  311. }
  312. var x,
  313. y;
  314. if (this.activeShip) {
  315. x = this.activeShip.targetx;
  316. y = this.activeShip.targety;
  317. } else if (this.activePlanet) {
  318. x = this.activePlanet.x;
  319. y = this.activePlanet.y;
  320. } else {
  321. x = this.centerX;
  322. y = this.centerY;
  323. }
  324.  
  325. this.centerX = x;
  326. this.centerY = y;
  327. this.canvas.x = x - $(window).width() / 2 / this.zoom;
  328. this.canvas.y = y - $(window).height() / 2 / this.zoom;
  329. this.draw();
  330. }
  331.  
  332. vgaPlanets.prototype.rotateActiveObject = function () {
  333. if (vgap.planetScreenOpen) {
  334. var planet = vgap.map.activePlanet;
  335. if (planet.isbase) {
  336. vgap.map.selectStarbase(planet.id);
  337. } else {
  338. var ships = vgap.shipsAt(planet.x, planet.y);
  339. if (ships.length > 0)
  340. vgap.map.selectShip(ships[0].id);
  341. // else only a planet
  342. }
  343. } else if (vgap.starbaseScreenOpen) {
  344. var planet = vgap.map.activePlanet;
  345. var ships = vgap.shipsAt(planet.x, planet.y);
  346. if (ships.length > 0) {
  347. vgap.map.selectShip(ships[0].id);
  348. } else {
  349. vgap.map.selectPlanet(planet.id);
  350. }
  351. } else if (vgap.shipScreenOpen) {
  352. var ship = vgap.map.activeShip;
  353. var ships = vgap.shipsAt(ship.x, ship.y)
  354. var index = ships.indexOf(ship);
  355. if (index == ships.length - 1) {
  356. var planet = vgap.planetAt(ship.x, ship.y)
  357. if (planet) {
  358. vgap.map.selectPlanet(planet.id);
  359. } else if (ships.length > 1) {
  360. vgap.map.selectShip(ships[0].id);
  361. } // else Cannot rotate 1 ship
  362. } else {
  363. vgap.map.selectShip(ships[index + 1].id);
  364. }
  365. }
  366. }
  367.  
  368. vgaPlanets.prototype.unrotateActiveObject = function () {
  369. if (vgap.planetScreenOpen) {
  370. var planet = vgap.map.activePlanet;
  371. var ships = vgap.shipsAt(planet.x, planet.y);
  372. if (ships.length > 0) {
  373. vgap.map.selectShip(ships[ships.length - 1].id);
  374. } else if (planet.isbase) {
  375. vgap.map.selectStarbase(planet.id);
  376. } // else only a planet
  377. } else if (vgap.starbaseScreenOpen) {
  378. var planet = vgap.map.activePlanet;
  379. vgap.map.selectPlanet(planet.id);
  380. } else if (vgap.shipScreenOpen) {
  381. var ship = vgap.map.activeShip;
  382. var ships = vgap.shipsAt(ship.x, ship.y)
  383. var index = ships.indexOf(ship);
  384. if (index == 0) {
  385. var planet = vgap.planetAt(ship.x, ship.y)
  386. if (planet) {
  387. if (planet.isbase) {
  388. vgap.map.selectStarbase(planet.id);
  389. } else {
  390. vgap.map.selectPlanet(planet.id);
  391. }
  392. } else {
  393. if (ships.length > 1)
  394. vgap.map.selectShip(ships[ships.length - 1].id);
  395. }
  396. } else {
  397. vgap.map.selectShip(ships[index - 1].id);
  398. }
  399. }
  400. }