Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Falling
- A Roll20 script to indicate a falling token's number of feet below ground.
- On Github: https://github.com/blawson69
- Contact me: https://app.roll20.net/users/1781274/ben-l
- Like this script? Become a patron:
- https://www.patreon.com/benscripts
- */
- var Falling = Falling || (function() {
- 'use strict';
- var version = '2.2.1',
- debugMode = false,
- MARKERS,
- ALT_MARKERS = [{name:'red', tag: 'red', url:"#C91010"}, {name: 'blue', tag: 'blue', url: "#1076C9"}, {name: 'green', tag: 'green', url: "#2FC910"}, {name: 'brown', tag: 'brown', url: "#C97310"}, {name: 'purple', tag: 'purple', url: "#9510C9"}, {name: 'pink', tag: 'pink', url: "#EB75E1"}, {name: 'yellow', tag: 'yellow', url: "#E5EB75"}, {name: 'dead', tag: 'dead', url: "X"}],
- styles = {
- box: 'background-color: #fff; border: 1px solid #000; padding: 8px 10px; border-radius: 6px; margin-left: -40px; margin-right: 0px;',
- title: 'padding: 0 0 10px 0; color: ##591209; font-size: 1.5em; font-weight: bold; font-variant: small-caps; font-family: "Times New Roman",Times,serif;',
- button: 'background-color: #000; border-width: 0px; border-radius: 5px; padding: 5px 8px; margin: 8px 0; color: #fff; text-align: center;',
- buttonWrapper: 'text-align: center; margin: 10px 0 4px; clear: both;',
- textButton: 'background-color: transparent; border: none; padding: 0; color: #591209; text-decoration: underline;',
- code: 'display: inline; font-family: "Courier New", Courier, monospace; background-color: #ddd; color: #000; padding: 1px 6px;',
- },
- checkInstall = function () {
- if (!_.has(state, 'Falling')) state['Falling'] = state['Falling'] || {};
- if (typeof state['Falling'].fallMarker == 'undefined') state['Falling'].fallMarker = 'fluffy-wing';
- var chatter = ['Ready for takeoff','Clear the runway','Fasten your seatbelts','Tray tables up','We may experience turbulance'];
- MARKERS = JSON.parse(Campaign().get("token_markers"));
- log('--> Falling v' + version + ' <-- ' + chatter[randomInteger(_.size(chatter)-1)] + '...');
- if (debugMode) {
- var d = new Date();
- showDialog('Debug Mode', 'Falling v' + version + ' loaded at ' + d.toLocaleTimeString() + '<br><a style=\'' + styles.textButton + '\' href="!fall-config">Show config</a>', 'GM');
- }
- },
- handleInput = function(msg) {
- if (msg.type !== "api") {
- return;
- }
- var parms = msg.content.split(/\s+/i);
- switch (parms[0]) {
- case '!fall-config':
- if (playerIsGM(msg.playerid)) showConfig();
- break;
- case '!fall-markers':
- if (playerIsGM(msg.playerid)) showMarkers();
- break;
- case '!fall-set':
- if (playerIsGM(msg.playerid)) {
- let marker = (parms[1]) ? parms[1] : '';
- setMarker(marker);
- }
- break;
- case '!fall':
- if (!msg.selected) {
- showDialog('', 'No tokens were selected!', msg.who);
- break;
- }
- var msgparts = msg.content.split(/\s+/), height = 0;
- if (msgparts[1]) msgparts[1] = msgparts[1].replace(/[^\d]+/,'');
- if (msgparts[1] && msgparts[1] !== 0) height = parseInt(msgparts[1]);
- _.each(msg.selected, function(obj) {
- var token = getObj(obj._type, obj._id);
- if (token && token.get("type") === "graphic") {
- token.set('status_' + state['Falling'].fallMarker, false);
- if (height > 0) {
- var oldMarkers = token.get("statusmarkers"), newMarkers = [];
- var newHeight = height.toString().split('');
- _.each(newHeight, function(num) {
- newMarkers.push(state['Falling'].fallMarker + '@' + num);
- });
- newMarkers = oldMarkers + ',' + newMarkers.join(',');
- token.set({statusmarkers: newMarkers});
- }
- }
- });
- }
- },
- showConfig = function () {
- var marker_style = 'margin: 5px 10px 0 0; display: block; float: left;';
- var message = '<span style=\'' + styles.code + '\'>!fall <number></span> sets the Falling token marker on the selected token(s) with a marker for every digit. '
- + 'Sending <span style=\'' + styles.code + '\'>!fall 45</span> will place one Falling marker with a "4" and another with a "5" on the token.<br><br>'
- + 'No digits after <span style=\'' + styles.code + '\'>!fall</span> will "land" the token by removing all Falling markers.';
- var curr_marker = _.find(MARKERS, function (x) { return x.tag == state['Falling'].fallMarker; });
- if (typeof curr_marker == 'undefined') curr_marker = _.find(ALT_MARKERS, function (x) { return x.tag == state['Falling'].fallMarker; });
- message += '<hr><h4>Falling Marker</h4>' + getMarker(curr_marker, marker_style);
- if (typeof curr_marker == 'undefined') message += '<b style="color: #c00;">Warning:</b> The token marker "' + state['Falling'].fallMarker + '" is invalid!';
- else message += 'This is the current Falling token marker. You may change it below.';
- message += '<div style="' + styles.buttonWrapper + '"><a style="' + styles.button + '" href="!fall-markers" title="This may result in a very long list...">Choose Marker</a></div>';
- message += '<div style="text-align: center;"><a style="' + styles.textButton + '" href="!fall-set ?{Token Marker|}">Set manually</a></div>';
- showDialog('Help Menu', message);
- },
- showMarkers = function () {
- var message = '<table style="border: 0; width: 100%;" cellpadding="0" cellspacing="2">';
- _.each(ALT_MARKERS, function (marker) {
- message += '<tr><td style="height: 24px;">' + getMarker(marker, 'margin-right: 10px;') + '</td><td style="white-space: nowrap; width: 100%;">' + marker.name + '</td>';
- if (marker.tag == state['Falling'].fallMarker) {
- message += '<td style="text-align: center;">Current</td>';
- } else {
- message += '<td style="text-align: center; white-space: nowrap; padding: 2px;"><a style="' + styles.button + '" href="!fall-set ' + marker.tag + '">Set Marker</a></td>';
- }
- message += '</tr>';
- });
- _.each(MARKERS, function (icon) {
- message += '<tr><td style="height: 24px;">' + getMarker(icon, 'margin-right: 10px;') + '</td><td style="white-space: nowrap; width: 100%;">' + icon.name + '</td>';
- if (icon.tag == state['Falling'].fallMarker) {
- message += '<td style="text-align: center;">Current</td>';
- } else {
- message += '<td style="text-align: center; white-space: nowrap; padding: 2px;"><a style="' + styles.button + '" href="!fall-set ' + icon.tag.replace('::','=') + '">Set Marker</a></td>';
- }
- message += '</tr>';
- });
- message += '<tr><td colspan="3" style="text-align: center; padding: 7px;"><a style="' + styles.button + '" href="!fall-config">◄ Back</a></td></tr>';
- message += '</table>';
- showDialog('Choose Falling Marker', message);
- },
- setMarker = function (marker) {
- marker = marker.replace('=', '::');
- var status_markers = _.pluck(MARKERS, 'tag');
- _.each(_.pluck(ALT_MARKERS, 'tag'), function (x) { status_markers.push(x); });
- if (_.find(status_markers, function (tmp) {return tmp === marker; })) {
- state['Falling'].fallMarker = marker;
- } else {
- showDialog('Error', 'The token marker "' + marker + '" is invalid. Please try again.');
- }
- showConfig();
- },
- getMarker = function (marker, style = '') {
- var marker_style = 'width: 24px; height: 24px;' + style;
- var return_marker = '<img src="" width="24" height="24" style="' + marker_style + ' border: 1px solid #ccc;" alt=" " />';
- if (typeof marker != 'undefined' && typeof marker.tag != 'undefined') {
- var status_markers = _.pluck(MARKERS, 'tag'),
- alt_marker = _.find(ALT_MARKERS, function (x) { return x.tag == marker.tag; });
- if (_.find(status_markers, function (x) { return x == marker.tag; })) {
- var icon = _.find(MARKERS, function (x) { return x.tag == marker.tag; });
- return_marker = '<img src="' + icon.url + '" width="24" height="24" style="' + marker_style + '" />';
- } else if (typeof alt_marker !== 'undefined') {
- if (alt_marker.url === 'X') {
- marker_style += 'color: #C91010; font-size: 30px; line-height: 24px; font-weight: bold; text-align: center; padding-top: 0px; overflow: hidden;';
- return_marker = '<div style="' + marker_style + '">X</div>';
- } else {
- marker_style += 'background-color: ' + alt_marker.url + '; border: 1px solid #fff; border-radius: 50%;';
- return_marker = '<div style="' + marker_style + '"></div>';
- }
- }
- }
- return return_marker;
- },
- showDialog = function (title, content, whom = 'GM') {
- var whisperTo = '', gm = /\(GM\)/i;
- whisperTo = (gm.test(whom)) ? 'GM' : '"' + whom + '" ';
- title = (title == '') ? '' : '<div style=\'' + styles.title + '\'>' + title + '</div>';
- var body = '<div style=\'' + styles.box + '\'>' + title + '<div>' + content + '</div></div>';
- sendChat('Falling','/w GM ' + whisperTo + body, null, {noarchive:true});
- },
- registerEventHandlers = function() {
- on('chat:message', handleInput);
- };
- return {
- CheckInstall: checkInstall,
- RegisterEventHandlers: registerEventHandlers
- };
- }());
- on("ready",function(){
- 'use strict';
- Falling.CheckInstall();
- Falling.RegisterEventHandlers();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement