public
Last active

TagPro Neomacro UserScript by Popcorn. Description in a comment in the code below ↓

  • Download Gist
neomacro.user.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/*
* TagPro Neomacro | UserScript by Popcorn.
* ========================================
* Are you tired of overly complex chat macro systems? Have you forgotten whether you bound "Yes" to I or U?
* This is Neomacro -- TagPro macro system that uses arrow keys, ctrl and 0 (zero) (you use WASD to control
* the game, don't you?) to create powerful and easy-to-remember macro codes.
*
* 1.0 update: Redesigned some macros, be sure to take a look.
* 1.1 update: Simplified code, added more comments
* 1.2 update: Goodbye, backspace and ctrl+something!
* 1.3 update: Added special handling for direction macros. Introduced 0 (zero) as a direction-giving key.
* Added more direction macros.
*
* It may contain bugs. If you notice one, please leave comment below ↓↓.
*
* Install this UserScript by pressing the <> in the upper right corner of the code ↗
*
* Find the macro combinations by scrolling down the script
* (they're inside the config section, starting with "var teamMacros =", "var directionMacros ="
* and "var globalMacros =") ↓
*
* Enter the combination by pressing the keys one by one (←↑→↓ arrow keys and ● ctrl).
*
* Some combinations have aliases: for example, ←↓ is the same as ↓←, and →←→ is the same as ←→←.
*
* Here's the macro dictionary. It will help you remember the codes:
*
* Directions to enemy FC: two arrow presses, for example:
* ↑↑ Enemy FC on upper side (↑)
* ↑→ or →↑ Enemy FC on upper right (↗)
* Or a zero press:
* 0 Enemy FC in the middle (●)
*
* (Directions to something else see below)
*
* "Yes" and "No" codes are similar to head movements (shaking/nodding):
* yes ↑↓↑ or ↓↑↓
* no ←→← or →←→
*
* Words used in sentences:
* nouns verbs
* --------- ---------------
* I/me ↓ defend ↓
* enemy ↑ attack ↑
* trick/tactic ←
* use button →
*
* Indicative sentences: (noun)●(verb)
* examples: ↓●↓ I'll defend.
* ↑●← Enemy's trying to trick us!
*
* Questions: ●(verb)
* Be aware about ●↑ Where's enemy FC?
* examples: ●↓ Is our flag safe?
* ●← What's our plan? How many defenders?
* ●→ Is the button safe?
*
* Giving commands: (verb)●●
* examples: ↓●● Defend our flag!
* ←●● Trick the enemy!
*
* Giving directions: (noun)0(direction)
* ↑0↑↑ Enemy on top. (↑)
* ↓0↓↓ I'm coming bottom. (↓)
* ←0←↑ Upper left is safe. (↖)
* Directions to enemy FC are unprefixed, as mentioned before.
*
* Emotes (messages chosen randomly):
* ●●↑ happy
* ●●↓ angry
* ●●← really bad jokes
* ●●→ promote Neomacro ('Cos it's best, that's why.)
*
* Have fun, don't forget to report bugs, and don't ever forget t**ALL GLORY TO THE HYPNOTOAD**
*/
 
// ==UserScript==
// @name TagPro Neomacro
// @namespace http://tiny.cc/neomacro
// @description Based on https://gist.github.com/steppin/5292526 by http://www.reddit.com/user/contact_lens_linux/
// @include http://tagpro-*.koalabeast.com:*
// @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @author Popcorn
// @version 1.3
// ==/UserScript==
 
// Body of this function is injected into the HTML page
function Script()
{
/**************************
* *
* START OF CONFIG SECTION *
* *
**************************/
 
// These are JS keycodes (event.keyCode) with corresponding character
// Find them here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
var buttons = {
37:"←",
38:"↑",
39:"→",
40:"↓",
17:"●",
96:"0"
};
 
// All generated messages are prefixed with this
var messagePrefix = "➜ ";
 
// Maximal interval beteween two keypresses (ms)
var keypressLimit = 1000;
 
// Minimal interval beteween two messages sent (ms)
var messageLimit = 300;
 
// All macros support random messages - for example, you can replace
// "↑↓↑": "Yes"
// with
// "↑↓↑": ["Yes", "Yeah", "Yea", "Whatever..."]
 
// Macros sent to your team only (the T-key chat)
var teamMacros = {
// Yes/No
"←→←": "No",
"↑↓↑": "Yes",
 
// Indicative sentences
"↓●↑": "I'll attack!",
"↓●↓": "I'll defend!",
"↓●←": "I'll trick the enemies!",
"↓●→": "I'll control the button!",
"↑●↑": "Enemy's attacking!",
"↑●↓": "Enemy's defending!",
"↑●←": "Enemy's trying to trick us!",
"↑●→": "Enemy's controlling the button!",
// Questions
"●↓" : "Is our flag safe?",
"●↑" : "Where is enemy FC?",
"●←" : "What's our plan? How many defenders?",
"●→" : "Who's controlling the button?",
 
// Commands
"↓●●" : "Defend our flag!",
"↑●●" : "Attack!",
"←●●" : "Trick the enemy!",
"→●●" : "Control the button!",
};
 
// Prefixes for giving directions. Two arrow presses indicating the direction comes after them.
// First macro is empty: it's triggered just by two arrow presses
// [Direction], [direction] and [arrow] are wildcards.
// Sent to your team only.
var directionMacros = {
"" : "Enemy FC on [direction]. ([arrow])",
"↑0" : "Enemy on [direction]. ([arrow])",
"↓0" : "I'm coming [direction]. ([arrow])",
"←0" : "[Direction] is safe. ([arrow])",
};
 
// Macros sent to everyone (Enter-key chat)
var globalMacros = {
// Positive emotions
"●●↑" : [
"Yea!",
"We're good!",
"YES WE CAN",
"Dream team.",
"Well played."
],
 
// Angry emotions
"●●↓" : [
"Shazbot!",
"Oh noez!",
"I've let you win this time.",
"C'mon team!",
"This made me angry."
],
 
// Bad jokes
"●●←" : [
"ALL GLORY TO THE HYPNOTOAD",
"So game. Much flag. Many ball. Amaze.",
"I have a UDP joke for you, but I'm afraid you won't get it.",
"I'm a linguist. I love ambiguity more than most people.",
"What's green and eats nuts? Syphilis.", // by /u/EstherHarshom
"A blind man walks into a bar. And a table. And a chair.", // by /u/VinciFox
"Life without women would be a pain in the ass.", // by /u/-Minnow-
"Where did Sally go during the bombing? Everywhere.", // by /u/My_Name_Is_Not_Chris
],
 
// Use often. Very often.
"●●→" : "Neomacro! TagPro macro system. http://tiny.cc/neomacro",
};
 
// Aliases for some combinations. Alias comes first, real code second.
// You can setup more aliases for a macro.
// If you setup the same alias as the macro itself (for example, "←←":"←←"), the universe will explode
var aliases = {
"→←→":"←→←", // No
"↓↑↓":"↑↓↑", // Yes
"●●●":"0" // Backwards compatibility
};
 
/************************
* *
* END OF CONFIG SECTION *
* *
************************/
 
// Why would you change this?
var directions = {
"↓←": ["lower left", "↙"],
"←←": ["left", "←"],
"↑←": ["upper left", "↖"],
"↑↑": ["top", "↑"],
"↑→": ["upper right", "↗"],
"→→": ["right", "→"],
"↓→": ["lower right", "↘"],
"↓↓": ["bottom", "↓"],
"0": ["middle", "●"]
};
 
directions["←↓"] = directions["↓←"];
directions["←↑"] = directions["↑←"];
directions["→↓"] = directions["↓→"];
directions["→↑"] = directions["↑→"];
 
// Because the game's keypress handlers are more prioritized, they're circumvented using a dummy input
var handlerbtn = document.getElementById("macrohandlerbutton");
handlerbtn.focus();
handlerbtn.addEventListener('keydown', keydownHandler, false);
handlerbtn.addEventListener('keyup', keyupHandler, false);
 
document.addEventListener('keydown', documentKeydown, false);
function documentKeydown(event)
{
if(!tagpro.disableControls)
{
handlerbtn.focus(); // The handler button should be always fucused
 
// Disables backspace and all ctrl interactions -- prevents leaving page by accident
if((event.keyCode==8 || event.ctrlKey) && !tagpro.disableControls)
{
event.preventDefault();
event.stopPropagation();
return false;
}
}
}
 
// Relasing arrow key tricks TagPro to think that you relased WASD-key too, even if you didn't
// This code prevents that from happening
function keyupHandler(event)
{
if(event.keyCode in buttons && !tagpro.disableControls)
{
event.preventDefault();
event.stopPropagation();
}
}
 
// Main macro keypresses handler
var lastKey = 0;
var currentMacro = "";
function keydownHandler(event)
{
if(!(event.keyCode in buttons) || tagpro.disableControls)
return;
 
event.preventDefault();
event.stopPropagation();
 
var now = new Date();
if((now - lastKey) > keypressLimit)
{
currentMacro = "";
}
lastKey = now;
 
currentMacro += buttons[event.keyCode];
 
var message = getMacro(currentMacro);
if(message)
{
chat(message);
currentMacro = "";
}
}
 
// Utility function to get corresponding message for macro code
function getMacro(x)
{
function capitalize(s)
{
return s[0].toUpperCase() + s.slice(1);
}
 
function isDirectionMacro(s)
{
return s.substring(0,s.length-2) in directionMacros && s.substring(s.length-2, s.length) in directions;
}
 
function getDirectionMacro(s)
{
var direction = directions[s.substring(s.length-2, s.length)];
return select(directionMacros[s.substring(0,s.length-2)])
.replace(/\[Direction\]/g, capitalize(direction[0]))
.replace(/\[direction\]/g, direction[0])
.replace(/\[arrow\]/g, direction[1]);
}
 
if(isDirectionMacro(x))
return {text:getDirectionMacro(x), global:0};
 
// When array is supplied, return random message - ideal to add variety to your messages
function select(x)
{
if(typeof x == "object")
return x[Math.floor(Math.random()*x.length)];
return x;
}
 
if(x in teamMacros)
return {text:select(teamMacros[x]), global:0};
 
if(x in globalMacros)
return {text:select(globalMacros[x]), global:1};
if(x in aliases)
return getMacro(aliases[x]);
return false;
}
 
// This functions does what expected - sends a chat message
var lastMessage = 0;
function chat(chatMessage)
{
var now = new Date();
var timeDiff = now - lastMessage;
if (timeDiff > messageLimit)
{
tagpro.socket.emit("chat",
{
message: messagePrefix + chatMessage.text,
toAll: chatMessage.global
});
lastMessage = new Date();
}
else if (timeDiff >= 0)
{
setTimeout(chat, messageLimit - timeDiff, chatMessage);
}
}
}
 
// This dummy input will handle macro keypresses
var btn = document.createElement("input");
btn.style.opacity = 0;
btn.style.position = "absolute";
btn.style.top = "-100px";
btn.style.left = "-100px";
btn.id = "macrohandlerbutton";
document.body.appendChild(btn);
 
// Create a script node holding this source code
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = '(' + Script + ')();';
document.body.appendChild(script);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.