VPA-Keyboard

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

目前为 2016-06-03 提交的版本。查看 最新版本

  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.5
  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.  
  24. sharedContent.prototype.planetSurvey_ori = sharedContent.prototype.planetSurvey;
  25. sharedContent.prototype.planetSurvey = function (planetId) {
  26. vgap.currentPlanetSurvey = planetId;
  27. this.planetSurvey_ori(planetId);
  28. }
  29.  
  30. sharedContent.prototype.editNote_ori = sharedContent.prototype.editNote;
  31. sharedContent.prototype.editNote = function (noteId, noteType) {
  32. vgap.currentNoteId = noteId;
  33. vgap.currentNoteType = noteType;
  34. this.editNote_ori(noteId, noteType);
  35. $("#EditNote").focus();
  36. }
  37.  
  38. vgapShipScreen.prototype.shipMission_ori = vgapShipScreen.prototype.shipMission;
  39. vgapShipScreen.prototype.shipMission = function () {
  40. this.shipMissionOpen = true;
  41. this.shipMission_ori();
  42. }
  43.  
  44. vgapShipScreen.prototype.selectMission_ori = vgapShipScreen.prototype.selectMission;
  45. vgapShipScreen.prototype.selectMission = function (missionId) {
  46. this.selectMission_ori(missionId);
  47. this.shipMissionOpen = false;
  48. }
  49.  
  50. vgapShipScreen.prototype.primaryEnemy_ori = vgapShipScreen.prototype.primaryEnemy;
  51. vgapShipScreen.prototype.primaryEnemy = function () {
  52. this.primaryEnemyOpen = true;
  53. this.primaryEnemy_ori();
  54. }
  55. vgapShipScreen.prototype.selectEnemy_ori = vgapShipScreen.prototype.selectEnemy;
  56. vgapShipScreen.prototype.selectEnemy = function (enemyId) {
  57. this.selectEnemy_ori(enemyId);
  58. this.primaryEnemyOpen = false;
  59. }
  60. vgaPlanets.prototype.closeMore_ori = vgaPlanets.prototype.closeMore;
  61. vgaPlanets.prototype.closeMore = function (callback) {
  62. vgap.closeMore_ori(callback);
  63. this.primaryEnemyOpen = false;
  64. vgap.currentPlanetSurvey = null;
  65. vgap.shipScreen.shipMissionOpen = false;
  66. }
  67.  
  68. var hotkey_ori = vgaPlanets.prototype.hotkey;
  69. vgaPlanets.prototype.hotkey = function (event) {
  70. if (vgap.shipScreen.primaryEnemyOpen) {
  71. var enemy = 0;
  72. if (event.keyCode >= 48 && event.keyCode <= 57)
  73. enemy = event.keyCode - 48;
  74. if (event.keyCode >= 65 && event.keyCode <= 90)
  75. enemy = event.keyCode - 65 + 10;
  76. if (enemy <= vgap.players.length) {
  77. vgap.shipScreen.selectEnemy(enemy);
  78. return;
  79. }
  80. }
  81.  
  82. if (vgap.shipScreen.shipMissionOpen) {
  83. var missionId = -1;
  84. if (event.keyCode == 67)
  85. missionId = 9; // 'c', Cloak
  86. else if (event.keyCode == 69)
  87. missionId = 0; // 'e', Explore
  88. else if (event.keyCode == 73)
  89. missionId = 7; // 'i', Intercept
  90. else if (event.keyCode == 75)
  91. missionId = 3; // 'k', Kill
  92. else if (event.keyCode == 76)
  93. missionId = 2; // 'l', Lay Mines
  94. else if (event.keyCode == 77)
  95. missionId = 1; // 'm', Mine Sweep
  96. else if (event.keyCode == 83 && event.shiftKey)
  97. missionId = 8; // 'S', Special / Super Spy
  98. else if (event.keyCode == 83)
  99. missionId = 4; // 's', Sensor Sweep
  100. else if (event.keyCode == 84)
  101. missionId = 6; // 't', Tow
  102. if (missionId >= 0) {
  103. vgap.shipScreen.selectMission(missionId);
  104. return;
  105. }
  106. }
  107.  
  108. if (vgap.editNoteOpen) {
  109. if (event.keyCode == 13 && event.ctrlKey) {
  110. this.saveNote(vgap.currentNoteId, vgap.currentNoteType);
  111. return;
  112. }
  113. }
  114.  
  115. if (event.keyCode == 27) {
  116. if (this.findObjectFeatureActivated)
  117. vgap.map.hideFindObjectFeature();
  118. else if (vgap.moreOpen)
  119. this.closeMore();
  120. else if (this.lcOpen)
  121. this.deselectAll();
  122. else
  123. this.hotkey_ori(event);
  124. return;
  125. }
  126.  
  127. if (vgap.hotkeysOn) {
  128. switch (event.keyCode) {
  129. case 8:
  130. if ((this.starbaseScreenOpen || this.planetScreenOpen || this.shipScreenOpen) && !this.moreOpen) {
  131. vgap.rPrev();
  132. return;
  133. }
  134. case 78: // 'n'
  135. if (event.shiftKey) {
  136. if (this.planetScreenOpen) {
  137. shtml.editNote(vgap.planetScreen.planet.id, 1);
  138. return;
  139. } else if (vgap.currentPlanetSurvey > 0) { // ship screen is open, but unowned planet is selected
  140. shtml.editNote(vgap.currentPlanetSurvey, 1);
  141. return
  142. } else if (this.shipScreenOpen) {
  143. shtml.editNote(vgap.shipScreen.ship.id, 2);
  144. return;
  145. } else if (this.starbaseScreenOpen) {
  146. shtml.editNote(vgap.starbaseScreen.starbase.id, 3);
  147. return;
  148. }
  149. }
  150. break;
  151. case 88: // 'x'
  152. this.map.clearTools();
  153. return;
  154. case 90: // 'z'
  155. this.map.zoomFarInOrOut_OnWaypointOrPlanet();
  156. return;
  157.  
  158. //case 188: // '<' if (event.shiftKey)
  159. case 38: // Arrow up
  160. vgap.unrotateActiveObject();
  161. return;
  162. // case 190: // '>'
  163. case 40: // Arrow-Down if (event.shiftKey)
  164. vgap.rotateActiveObject();
  165. return;
  166. case 39: // Arrow right // if (ev.keyCode == 39 && this.shipScreenOpen || this.planetScreenOpen)
  167. this.rNext();
  168. return;
  169. case 37: // Arrow left // if (ev.keyCode == 39 && this.shipScreenOpen || this.planetScreenOpen)
  170. this.rPrev();
  171. return;
  172.  
  173. case 163: // '#' set ships/planet ready
  174. {
  175. var item = 0;
  176. var item2 = 0;
  177. if (this.planetScreenOpen) {
  178. var item = vgap.planetScreen.planet.id;
  179. vgap.planetScreen.planet.readystatus = 1;
  180. } else if (this.starbaseScreenOpen) {
  181. item = vgap.starbaseScreen.starbase.id;
  182. vgap.starbaseScreen.starbase.readystatus = 1;
  183. } else if (this.shipScreenOpen) {
  184. var ship = vgap.shipScreen.ship;
  185. item = ship.id;
  186. ship.readystatus = 1; // Set ready
  187. var ships = vgap.shipsAt(ship.x, ship.y)
  188. var index = ships.indexOf(ship);
  189. for (var i = 0; i < ships.length; i++) {
  190. if (ships[i].ownerid == vgap.player.id)
  191. if (ships[i].readystatus == 0) ships[i].readystatus = 1;
  192. }
  193. }
  194. do {
  195. this.rNext();
  196. if (this.planetScreenOpen) {
  197. if (vgap.planetScreen.planet.readystatus < 1)
  198. break; // This is the next "unready" one...
  199. item2 = vgap.planetScreen.planet.id;
  200. } else if (this.starbaseScreenOpen) {
  201. if (vgap.starbaseScreen.starbase.readystatus < 1)
  202. break;
  203. item2 = vgap.starbaseScreen.starbase.id;
  204. } else if (this.shipScreenOpen) {
  205. if (vgap.shipScreen.ship.readystatus < 1)
  206. break;
  207. item2 = vgap.shipScreen.ship.id;
  208. }
  209. } while (item != item2); // until
  210. return;
  211. }
  212. }
  213. //r
  214. if (event.keyCode == 89) { //y
  215. if (this.planetScreenOpen)
  216. vgap.planetScreen.setPlanetsReadyCheckBox();
  217. else if (this.starbaseScreenOpen)
  218. vgap.starbaseScreen.setStarbaseReadyCheckBox();
  219. else if (this.shipScreenOpen)
  220. vgap.shipScreen.setShipReadyCheckBox();
  221. }
  222. }
  223. hotkey_ori.apply(this, arguments);
  224. //this.hotkey_ori(event);
  225. }
  226.  
  227. vgapMap.prototype.zoomFarInOrOut_OnWaypointOrPlanet = function () {
  228. var zoomedOut = (this.zoom < 29);
  229. if (zoomedOut) {
  230. this.zoomFactorBeforeZoomingInDeeply = this.zoom;
  231. this.setZoom(42);
  232. } else {
  233. this.setZoom(this.zoomFactorBeforeZoomingInDeeply);
  234. }
  235. var x,
  236. y;
  237. if (this.activeShip) {
  238. x = this.activeShip.targetx;
  239. y = this.activeShip.targety;
  240. } else if (this.activePlanet) {
  241. x = this.activePlanet.x;
  242. y = this.activePlanet.y;
  243. } else {
  244. x = this.centerX;
  245. y = this.centerY;
  246. }
  247.  
  248. this.centerX = x;
  249. this.centerY = y;
  250. this.canvas.x = x - $(window).width() / 2 / this.zoom;
  251. this.canvas.y = y - $(window).height() / 2 / this.zoom;
  252. this.draw();
  253. }
  254.  
  255. vgaPlanets.prototype.rotateActiveObject = function () {
  256. if (vgap.planetScreenOpen) {
  257. var planet = vgap.map.activePlanet;
  258. if (planet.isbase) {
  259. vgap.map.selectStarbase(planet.id);
  260. } else {
  261. var ships = vgap.shipsAt(planet.x, planet.y);
  262. if (ships.length > 0)
  263. vgap.map.selectShip(ships[0].id);
  264. // else only a planet
  265. }
  266. } else if (vgap.starbaseScreenOpen) {
  267. var planet = vgap.map.activePlanet;
  268. var ships = vgap.shipsAt(planet.x, planet.y);
  269. if (ships.length > 0) {
  270. vgap.map.selectShip(ships[0].id);
  271. } else {
  272. vgap.map.selectPlanet(planet.id);
  273. }
  274. } else if (vgap.shipScreenOpen) {
  275. var ship = vgap.map.activeShip;
  276. var ships = vgap.shipsAt(ship.x, ship.y)
  277. var index = ships.indexOf(ship);
  278. if (index == ships.length - 1) {
  279. var planet = vgap.planetAt(ship.x, ship.y)
  280. if (planet) {
  281. vgap.map.selectPlanet(planet.id);
  282. } else if (ships.length > 1) {
  283. vgap.map.selectShip(ships[0].id);
  284. } // else Cannot rotate 1 ship
  285. } else {
  286. vgap.map.selectShip(ships[index + 1].id);
  287. }
  288. }
  289. }
  290.  
  291. vgaPlanets.prototype.unrotateActiveObject = function () {
  292. if (vgap.planetScreenOpen) {
  293. var planet = vgap.map.activePlanet;
  294. var ships = vgap.shipsAt(planet.x, planet.y);
  295. if (ships.length > 0) {
  296. vgap.map.selectShip(ships[ships.length - 1].id);
  297. } else if (planet.isbase) {
  298. vgap.map.selectStarbase(planet.id);
  299. } // else only a planet
  300. } else if (vgap.starbaseScreenOpen) {
  301. var planet = vgap.map.activePlanet;
  302. vgap.map.selectPlanet(planet.id);
  303. } else if (vgap.shipScreenOpen) {
  304. var ship = vgap.map.activeShip;
  305. var ships = vgap.shipsAt(ship.x, ship.y)
  306. var index = ships.indexOf(ship);
  307. if (index == 0) {
  308. var planet = vgap.planetAt(ship.x, ship.y)
  309. if (planet) {
  310. if (planet.isbase) {
  311. vgap.map.selectStarbase(planet.id);
  312. } else {
  313. vgap.map.selectPlanet(planet.id);
  314. }
  315. } else {
  316. if (ships.length > 1)
  317. vgap.map.selectShip(ships[ships.length - 1].id);
  318. }
  319. } else {
  320. vgap.map.selectShip(ships[index - 1].id);
  321. }
  322. }
  323. }