Spacom addons

View zones, editor unlock

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Spacom addons
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  View zones, editor unlock
// @match        https://spacom.ru/?act=game/map*
// @match        https://spacom.ru/?act=game/design*
// @run-at       document-end
// ==/UserScript==

(function (w) {
	
	function waitFor(obj, prop, callback) {
		var token = setInterval(function () {
			if (obj[prop] !== undefined) {
				clearInterval(token);
				callback(obj[prop]);
			}
		}, 0);
	}

	function createMapButton (css) {
		var last = $("#radar + div");
		var next = $('<div><i class="fa '+css+' fa-2x"></i></div>').css({
			"z-index": last.css("z-index"),
			"position": last.css("position"),
			"cursor": last.css("cursor"),
			"color": last.css("color"),
			"right": last.css("right"),
			"bottom": (parseInt(last.css("bottom")) + 40) + "px"
		});
		last.before(next);
		return next;
	}
	
	if (!w.Addons) {
		w.Addons = {};
	}

	Addons.ViewZones = {
		button: null,
		circles: null,
		enabled: false,
		createCircles: function () {
			this.circles = {};
			for (var id in map.fleets) {
				var fleet = map.fleets[id];
				if (fleet.owner == "own" && (fleet.turn == 0 || fleet.start_turn - fleet.turn == 0)) {
					var center;
					if (fleet.turn == 0) {
						center = getCenterXY(fleet.x, fleet.y);
					} else {
						center = {x: fleet.start_x, y: fleet.start_y};
					}
					this.circles[id] = new fabric.Circle({
						left: center.x,
						top: center.y,
						radius: fleet.view_radius * box_size,
						fill: 'rgb(40,100,40)',
						opacity: 0.2,
						originX: 'center',
						originY: 'center',
						selectable: false,
						visible:false
					});
					
					scene.add(this.circles[id]);
					scene.sendToBack(this.circles[id]);
				}
			}
		},
		draw: function () {
			if (!this.circles) {
				if (this.enabled) {
					this.createCircles();
				} else {
					return;
				}
			}
			
			var left = current_x;
			var top = current_y;
			var right = current_x + base_width / current_scale;
			var bottom = current_y + base_height / current_scale;
			
			for (var id in this.circles) {
				var circle = this.circles[id];
				var show = this.enabled;
				
				if (show) {
					var raduis = circle.getRadiusX();
					show = this.inBox(
						circle.getLeft(), circle.getTop(),
						left  - raduis, top    - raduis,
						right + raduis, bottom + raduis
					);
				}
				
				circle.set({visible:show});
			}
		},
		show: function (flag) {
			this.enabled = flag;
			this.draw();
			scene.renderAll();
		},
		toggle: function () {
			this.show(!this.enabled);
		},
		inBox: function (x, y, left, top, right, bottom) {
			return (x > left && x < right && y > top && y < bottom);
		},
		init: function () {
			var self = this;
			this.button = createMapButton("fa-eye");
			this.button.on("click", this.toggle.bind(this));
			
			var renewMap = map.renewMap;
			map.renewMap = function () {
				renewMap();
				self.draw();
			};
		}
	};
		
	Addons.AllLevels = {
		enable: function () {
			for (var i in design.template_components) {
				design.template_components[i].max_level = 100;
			}
			design.draw();
		},
		disable: function () {
			for (var i in design.template_components) {
				design.template_components[i].max_level = design.template_components[i]._max_level;
			}
			design.draw();
		},
		init: function () {
			var self = this;
			$("#details_list").prepend('<span><input id="all_levels" type="checkbox"> все уровни</span>');
			$("#all_levels").on("change", function () {
				if ($(this).is(":checked")) {
					self.enable();
				} else {
					self.disable();
				}
			});
			for (var i in design.template_components) {
				design.template_components[i]._max_level = design.template_components[i].max_level;
			}
		}
	};
			
	if (w.map) {
		Addons.ViewZones.init();
	} else if (w.Design) {
		waitFor(w, "design", function (design) {
			Addons.AllLevels.init();
		});
	}
})(unsafeWindow);