VPA-Keyboard

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

目前為 2016-06-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        VPA-Keyboard
// @author      Quapla
// @copyright   Basecode: Kero van Gelder, 2016 - latest: Quapla
// @license     Lesser Gnu Public License, version 3
// @downloadURL
// @description For planets.nu -- Add VPA-key strokes, and commands
// @namespace   quapla/VPA-Keyboard
// @include     http://planets.nu/*
// @include     http://play.planets.nu/*
// @include     http://test.planets.nu/*
// @version     1.0.5
// @grant       none
// ==/UserScript==

// Reference:   Kero van Gelder's Keyboard: http://chmeee.org/ext/planets.nu/Keyboard.user.js

// Changes by Quapla:
// Use Arrows to select ships
// --> LEFT/RIGHT numerical previous/next
// --> UP/DOWN previous/next at same position as planet -> base -> ships (numerical) -> again planet
// --> '#' sets all ships at position "ready" and moves to next ship (not ready yet)

sharedContent.prototype.planetSurvey_ori = sharedContent.prototype.planetSurvey;
sharedContent.prototype.planetSurvey = function (planetId) {
	vgap.currentPlanetSurvey = planetId;
	this.planetSurvey_ori(planetId);
}

sharedContent.prototype.editNote_ori = sharedContent.prototype.editNote;
sharedContent.prototype.editNote = function (noteId, noteType) {
	vgap.currentNoteId = noteId;
	vgap.currentNoteType = noteType;
	this.editNote_ori(noteId, noteType);
	$("#EditNote").focus();
}

vgapShipScreen.prototype.shipMission_ori = vgapShipScreen.prototype.shipMission;
vgapShipScreen.prototype.shipMission = function () {
	this.shipMissionOpen = true;
	this.shipMission_ori();
}

vgapShipScreen.prototype.selectMission_ori = vgapShipScreen.prototype.selectMission;
vgapShipScreen.prototype.selectMission = function (missionId) {
	this.selectMission_ori(missionId);
	this.shipMissionOpen = false;
}

vgapShipScreen.prototype.primaryEnemy_ori = vgapShipScreen.prototype.primaryEnemy;
vgapShipScreen.prototype.primaryEnemy = function () {
	this.primaryEnemyOpen = true;
	this.primaryEnemy_ori();
}
vgapShipScreen.prototype.selectEnemy_ori = vgapShipScreen.prototype.selectEnemy;
vgapShipScreen.prototype.selectEnemy = function (enemyId) {
	this.selectEnemy_ori(enemyId);
	this.primaryEnemyOpen = false;
}
vgaPlanets.prototype.closeMore_ori = vgaPlanets.prototype.closeMore;
vgaPlanets.prototype.closeMore = function (callback) {
	vgap.closeMore_ori(callback);
	this.primaryEnemyOpen = false;
	vgap.currentPlanetSurvey = null;
	vgap.shipScreen.shipMissionOpen = false;
}

var hotkey_ori = vgaPlanets.prototype.hotkey;
vgaPlanets.prototype.hotkey = function (event) {
	if (vgap.shipScreen.primaryEnemyOpen) {
		var enemy = 0;
		if (event.keyCode >= 48 && event.keyCode <= 57)
			enemy = event.keyCode - 48;
		if (event.keyCode >= 65 && event.keyCode <= 90)
			enemy = event.keyCode - 65 + 10;
		if (enemy <= vgap.players.length) {
			vgap.shipScreen.selectEnemy(enemy);
			return;
		}
	}

	if (vgap.shipScreen.shipMissionOpen) {
		var missionId = -1;
		if (event.keyCode == 67)
			missionId = 9; // 'c', Cloak
		else if (event.keyCode == 69)
			missionId = 0; // 'e', Explore
		else if (event.keyCode == 73)
			missionId = 7; // 'i', Intercept
		else if (event.keyCode == 75)
			missionId = 3; // 'k', Kill
		else if (event.keyCode == 76)
			missionId = 2; // 'l', Lay Mines
		else if (event.keyCode == 77)
			missionId = 1; // 'm', Mine Sweep
		else if (event.keyCode == 83 && event.shiftKey)
			missionId = 8; // 'S', Special / Super Spy
		else if (event.keyCode == 83)
			missionId = 4; // 's', Sensor Sweep
		else if (event.keyCode == 84)
			missionId = 6; // 't', Tow
		if (missionId >= 0) {
			vgap.shipScreen.selectMission(missionId);
			return;
		}
	}

	if (vgap.editNoteOpen) {
		if (event.keyCode == 13 && event.ctrlKey) {
			this.saveNote(vgap.currentNoteId, vgap.currentNoteType);
			return;
		}
	}

	if (event.keyCode == 27) {
		if (this.findObjectFeatureActivated)
			vgap.map.hideFindObjectFeature();
		else if (vgap.moreOpen)
			this.closeMore();
		else if (this.lcOpen)
			this.deselectAll();
		else
			this.hotkey_ori(event);
		return;
	}

	if (vgap.hotkeysOn) {
		switch (event.keyCode) {
		case 8:
			if ((this.starbaseScreenOpen || this.planetScreenOpen || this.shipScreenOpen) && !this.moreOpen) {
				vgap.rPrev();
				return;
			}
		case 78: // 'n'
			if (event.shiftKey) {
				if (this.planetScreenOpen) {
					shtml.editNote(vgap.planetScreen.planet.id, 1);
					return;
				} else if (vgap.currentPlanetSurvey > 0) { // ship screen is open, but unowned planet is selected
					shtml.editNote(vgap.currentPlanetSurvey, 1);
					return
				} else if (this.shipScreenOpen) {
					shtml.editNote(vgap.shipScreen.ship.id, 2);
					return;
				} else if (this.starbaseScreenOpen) {
					shtml.editNote(vgap.starbaseScreen.starbase.id, 3);
					return;
				}
			}
			break;
		case 88: // 'x'
			this.map.clearTools();
			return;
		case 90: // 'z'
			this.map.zoomFarInOrOut_OnWaypointOrPlanet();
			return;

			//case 188: // '<' if (event.shiftKey)
		case 38: // Arrow up
			vgap.unrotateActiveObject();
			return;
			// case 190: // '>'
		case 40: // Arrow-Down if (event.shiftKey)
			vgap.rotateActiveObject();
			return;
		case 39: // Arrow right // if (ev.keyCode == 39 && this.shipScreenOpen || this.planetScreenOpen)
			this.rNext();
			return;
		case 37: // Arrow left // if (ev.keyCode == 39 && this.shipScreenOpen || this.planetScreenOpen)
			this.rPrev();
			return;

		case 163: // '#' set ships/planet ready
			{
				var item = 0;
				var item2 = 0;
				if (this.planetScreenOpen) {
					var item = vgap.planetScreen.planet.id;
					vgap.planetScreen.planet.readystatus = 1;
				} else if (this.starbaseScreenOpen) {
					item = vgap.starbaseScreen.starbase.id;
					vgap.starbaseScreen.starbase.readystatus = 1;
				} else if (this.shipScreenOpen) {
					var ship = vgap.shipScreen.ship;
					item = ship.id;
					ship.readystatus = 1; // Set ready
					var ships = vgap.shipsAt(ship.x, ship.y)
						var index = ships.indexOf(ship);
					for (var i = 0; i < ships.length; i++) {
						if (ships[i].ownerid == vgap.player.id)
							if (ships[i].readystatus == 0) ships[i].readystatus = 1;
					}
				}
				do {
					this.rNext();
					if (this.planetScreenOpen) {
						if (vgap.planetScreen.planet.readystatus < 1)
							break;  // This is the next "unready" one...
						item2 = vgap.planetScreen.planet.id;
					} else if (this.starbaseScreenOpen) {
						if (vgap.starbaseScreen.starbase.readystatus < 1)
							break;
						item2 = vgap.starbaseScreen.starbase.id;
					} else if (this.shipScreenOpen) {
						if (vgap.shipScreen.ship.readystatus < 1)
							break;
						item2 = vgap.shipScreen.ship.id;
					}
				} while (item != item2); // until
				return;
			}
		}
		//r
		if (event.keyCode == 89) { //y
			if (this.planetScreenOpen)
				vgap.planetScreen.setPlanetsReadyCheckBox();
			else if (this.starbaseScreenOpen)
				vgap.starbaseScreen.setStarbaseReadyCheckBox();
			else if (this.shipScreenOpen)
				vgap.shipScreen.setShipReadyCheckBox();
		}
	}
	hotkey_ori.apply(this, arguments);
	//this.hotkey_ori(event);
}

vgapMap.prototype.zoomFarInOrOut_OnWaypointOrPlanet = function () {
	var zoomedOut = (this.zoom < 29);
	if (zoomedOut) {
		this.zoomFactorBeforeZoomingInDeeply = this.zoom;
		this.setZoom(42);
	} else {
		this.setZoom(this.zoomFactorBeforeZoomingInDeeply);
	}
	var x,
	y;
	if (this.activeShip) {
		x = this.activeShip.targetx;
		y = this.activeShip.targety;
	} else if (this.activePlanet) {
		x = this.activePlanet.x;
		y = this.activePlanet.y;
	} else {
		x = this.centerX;
		y = this.centerY;
	}

	this.centerX = x;
	this.centerY = y;
	this.canvas.x = x - $(window).width() / 2 / this.zoom;
	this.canvas.y = y - $(window).height() / 2 / this.zoom;
	this.draw();
}

vgaPlanets.prototype.rotateActiveObject = function () {
	if (vgap.planetScreenOpen) {
		var planet = vgap.map.activePlanet;
		if (planet.isbase) {
			vgap.map.selectStarbase(planet.id);
		} else {
			var ships = vgap.shipsAt(planet.x, planet.y);
			if (ships.length > 0)
				vgap.map.selectShip(ships[0].id);
			// else only a planet
		}
	} else if (vgap.starbaseScreenOpen) {
		var planet = vgap.map.activePlanet;
		var ships = vgap.shipsAt(planet.x, planet.y);
		if (ships.length > 0) {
			vgap.map.selectShip(ships[0].id);
		} else {
			vgap.map.selectPlanet(planet.id);
		}
	} else if (vgap.shipScreenOpen) {
		var ship = vgap.map.activeShip;
		var ships = vgap.shipsAt(ship.x, ship.y)
			var index = ships.indexOf(ship);
		if (index == ships.length - 1) {
			var planet = vgap.planetAt(ship.x, ship.y)
				if (planet) {
					vgap.map.selectPlanet(planet.id);
				} else if (ships.length > 1) {
					vgap.map.selectShip(ships[0].id);
				} // else Cannot rotate 1 ship
		} else {
			vgap.map.selectShip(ships[index + 1].id);
		}
	}
}

vgaPlanets.prototype.unrotateActiveObject = function () {
	if (vgap.planetScreenOpen) {
		var planet = vgap.map.activePlanet;
		var ships = vgap.shipsAt(planet.x, planet.y);
		if (ships.length > 0) {
			vgap.map.selectShip(ships[ships.length - 1].id);
		} else if (planet.isbase) {
			vgap.map.selectStarbase(planet.id);
		} // else only a planet
	} else if (vgap.starbaseScreenOpen) {
		var planet = vgap.map.activePlanet;
		vgap.map.selectPlanet(planet.id);
	} else if (vgap.shipScreenOpen) {
		var ship = vgap.map.activeShip;
		var ships = vgap.shipsAt(ship.x, ship.y)
			var index = ships.indexOf(ship);
		if (index == 0) {
			var planet = vgap.planetAt(ship.x, ship.y)
				if (planet) {
					if (planet.isbase) {
						vgap.map.selectStarbase(planet.id);
					} else {
						vgap.map.selectPlanet(planet.id);
					}
				} else {
					if (ships.length > 1)
						vgap.map.selectShip(ships[ships.length - 1].id);
				}
		} else {
			vgap.map.selectShip(ships[index - 1].id);
		}
	}
}