/** * A script with a command for specifying tokens to follow other tokens. * Simply select the tokens in the marching order and enter the "!follow" * commandwith either a direction for the marching order of the selected tokens * or the name of a token for the selected tokens to follow behind. * * E.G. "!follow west" will make the selected tokens follow each other in order * from east to west, with the westmost token being the leader and the eastmost * token being the caboose. * Alternatively if we want the selected tokens to follow a character named * "Drizt", enter the command "!follow Drizt". * * !follow [north|south|east|west|name of leader token] * * To make a token stop following the token in front of it, just manually move * the token anywhere. */ var MarchingOrder = (function() { /** * Makes a token follow another token. * @param {Graphic} leader * @param {Graphic} follower */ var follow = function(leader, follower) { if(!leader || !follower) return; // unbind all of follower's following links. unfollow(follower); var prevFollower = leader.follower; follower.leader = leader; follower.follower = prevFollower; leader.follower = follower; if(prevFollower) { prevFollower.leader = follower; } }; /** * Makes a token stop following other tokens. * @param {Graphic} token */ var unfollow = function(token) { if(token.leader) { token.leader.follower = token.follower; } if(token.follower) { token.follower.leader = token.leader; } token.leader = null; token.follower = null; }; /** * Checks if the chat message starts with some API command. * @param {Msg} msg The chat message for the API command. * @param {String} cmdName * @return {Boolean} */ var _msgStartsWith = function(msg, cmdName) { var msgTxt = msg.content; return (msg.type == 'api' && msgTxt.indexOf(cmdName) !== -1); }; /** * Gets the argument of the API command. * @param {Msg} msg * @return {String} */ var _getCmdArg = function(msg) { var msgTxt = msg.content; var index = msgTxt.indexOf(' '); if(index >= 0) { index++; return msgTxt.substring(index); } else { return null; } }; /** * Tries to parse an API command argument as a compass direction. * @param {String} msgTxt * @return {String} The compass direction, or null if it couldn't be parsed. */ var _parseDirection = function(msgTxt) { if(msgTxt.indexOf("north") !== -1) { return "north"; } else if(msgTxt.indexOf("south") !== -1) { return "south"; } else if(msgTxt.indexOf("west") !== -1) { return "west"; } else if(msgTxt.indexOf("east") !== -1) { return "east"; } else { return null; } }; /** * Returns a list of the tokens selected by the player. * @param {Msg} msg * @return {Graphic[]} */ var _getSelectedTokens = function(msg) { var tokens = []; var curPageID = Campaign().get("playerpageid"); var selected = msg.selected; // Not actually tokens. for(var i=0; i