Skip to content

Instantly share code, notes, and snippets.

@leberechtreinhold
Last active December 2, 2016 17:04
Show Gist options
  • Save leberechtreinhold/bc763a3187dad4b4b33244c5a36a90d7 to your computer and use it in GitHub Desktop.
Save leberechtreinhold/bc763a3187dad4b4b33244c5a36a90d7 to your computer and use it in GitHub Desktop.
Better management for your VGA games through better notes
// ==UserScript==
// @name BetterNotesVGAPlanets
// @description Augments the notes functionality
// @include http://planets.nu/*
// @copyright 2016, Aitor Jiménez
// @license MIT (https://opensource.org/licenses/mit-license.php)
// @namespace leberecht
// @version 0.2
// ==/UserScript==
/*
Better notes
------------
This plugins eases a bit the management of your VGA empire by adding more functionality
to the notes. First, notes will appear in the planet screen, below the graphical
display, so they are easier to see. And more importantly, you can specify some
options in the note.
At the end of the note you can add the following
" { command = value; command2 = value2 } "
The brackets indicate that it's a section that will be read by the plugin, and the
commands allow for some functionality. The commands are separated by a semicolon.
Notes are only updated when loading the map from the dashboard (the standard plugin
structure has no 'official' way to get a callback on note input that I know of)
Example
-------
In a planet, put this note:
"My planet description goes
here, with blabla text.
{ show_radius = 81; }"
This will create a red (default) radius around the planet, which will show you
easily which other planets are in range of a jump. You can use this to mark
frontiers.
Supported commands
------------------
- show_radius
Value: A number, integer, between 9 and 3*81 (three times the max jump size)
Creates a circle around the planet, by default 1px thick and red, of the radius
given.
*/
function wrapper () { // wrapper for injection
if (vgap.version < 3.0) {
console.log("[BetterNotes] Requires at least NU version 3.0. Plugin disabled.");
return;
}
var plugin_version = 0.1;
var plugin = {
// PREFERENCES
CIRCLE_COLOR: '#ff0000',
CIRCLE_THICKNESS: 1,
DEBUG_MODE: false,
// HOOKS
loadplanet: function() {
plugin.log("[BetterNotes] loadplanet called");
plugin.add_notes_section();
},
showmap: function() {
plugin.log("[BetterNotes] showmap called");
if (plugin.DEBUG_MODE) { debugger; }
g_extranotes = {};
for (var i=0; i<vgap.planets.length; i++) {
var planet = vgap.planets[i];
var note = planet.note;
if (note !== undefined && $.type(note.body) === "string" && note.body !== "") {
plugin.parse_note(note.body, planet);
}
}
},
draw: function() {
plugin.log("[BetterNotes] draw called");
for (var i=0; i<vgap.planets.length; i++) {
var planet = vgap.planets[i];
if (plugin.planet_should_draw(planet)) {
var notes = plugin.g_extranotes[planet.id];
if (notes["show_radius"]) {
console.log("Showing radius for planet " + plugin.planet_str(planet));
var x = vgap.map.screenX(planet.x);
var y = vgap.map.screenY(planet.y);
var ctx = vgap.map.ctx;
var radius = plugin.g_extranotes[planet.id]["radius_to_show"] * vgap.map.zoom;
var stroke_style = plugin.CIRCLE_COLOR;
var thickness = plugin.CIRCLE_THICKNESS;
vgap.map.drawCircle(ctx, x, y, radius, stroke_style, thickness);
}
}
}
},
// VARS
g_extranotes: {},
// FUNCTIONS
add_notes_section: function() {
$('#Buildingsbar').before('<div id="NotesBar" class="SepBar"></div>');
$('#NotesBar').append('<div id="TitleNotesBar" class="SepTitle">Information</div>');
$('#NotesBar').after('<div class="SepContainer" id="NotesContainer" style="display: block;"></div>');
$('#NotesContainer').append('<p>' + vgap.planetScreen.planet.note.body + '</p>');
},
parse_note: function(text, planet) {
var parse_area = /{[\s\S]*}/m.exec(text);
if (parse_area === undefined || parse_area === null || parse_area.length < 1)
{
return;
}
var planetmetadata = parse_area[0].replace('{','').replace('}','').trim().split(';');
if (planetmetadata === undefined || planetmetadata === null || planetmetadata.length < 1)
{
return;
}
for (var i=0; i<planetmetadata.length; i++) {
var metadata = planetmetadata[i];
var command = metadata.trim().split('=');
if (command !== null && command.length == 2) {
var command_id = command[0].trim();
var command_value = command[1].trim();
plugin.execute_command(command_id, command_value, planet.id);
}
else if (metadata === "") {
// Don't log, it's normal due to the ending of split
}
else {
plugin.log("[BetterNotes] Error in metadata of planet " + plugin.planet_str(planet));
}
}
},
execute_command: function(id, value, planet_id) {
if (plugin.g_extranotes[planet_id] === undefined) {
plugin.g_extranotes[planet_id] = {};
}
if (id === "show_name" && value.toLowerCase() === "true") {
plugin.g_extranotes[planet_id]["show_names"] = true;
}
if (id === "show_radius" && plugin.is_integer(value)) {
var radius = parseInt(Number(value));
if (radius < 9 || radius > 3 * 81) {
plugin.log("[BetterNotes] You specified a radius too big or too small for planet " + plugin.planet_str(planet));
}
else {
plugin.g_extranotes[planet_id]["show_radius"] = true;
plugin.g_extranotes[planet_id]["radius_to_show"] = parseInt(Number(value));
}
}
},
planet_should_draw: function(planet) {
var has_notes = plugin.g_extranotes[planet.id] !== undefined;
var is_visible = vgap.map.isVisible(planet.x, planet.y, vgap.map.planetRad(planet));
return has_notes && is_visible;
},
is_integer: function(value) {
return !isNaN(value) && parseInt(Number(value)) == value && !isNaN(parseInt(value, 10));
},
planet_str: function(planet) {
return "[" + planet.id + "]: " + planet.name;
},
log: function(text) {
if (plugin.DEBUG_MODE) {
console.log(text);
}
}
};
vgap.registerPlugin(plugin, 'BetterNotes');
}
var script = document.createElement("script");
script.type = "application/javascript";
script.textContent = "(" + wrapper + ")();";
document.body.appendChild(script);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment