Skip to content

Instantly share code, notes, and snippets.

@Anduh
Created May 21, 2021 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anduh/b4bd6323478665efa6d29754f8fbc17d to your computer and use it in GitHub Desktop.
Save Anduh/b4bd6323478665efa6d29754f8fbc17d to your computer and use it in GitHub Desktop.
Splatter v2 - Rll20 API code from the forums(orig. 2014)
// See: https://app.roll20.net/forum/permalink/874303/ (2014)
(function () {
var oldCreateObj = createObj;
createObj = function () {
var obj = oldCreateObj.apply(this, arguments);
if (obj && !obj.fbpath) {
obj.fbpath = obj.changed._fbpath.replace(/([^\/]*\/){4}/, "/");
}
return obj;
};
}());
// TODO: This script pretty much assumes square-ish/round tokens. Width is the desicive factor in many places.
// helper functions
function getSplatterImageUrl (splatterType) {
var table,
urls = [],
ret;
table = findObjs( {_type:"rollabletable", name: "SplatterTable"});
log("Found these tables, using the first one: ");
log(table);
if(table.length !== 1) {
sendChat("Splatter","No table SplatterTable found.");
throw "Splatter, no table";
}
table = table[0];
var currentPageGraphics = findObjs({
_type: "tableitem",
_rollabletableid: table.get("_id")
});
_.each(currentPageGraphics, function(obj) {
var i;
if(obj.get("name").indexOf(splatterType) === 0) {
for(i = 0 ; i < obj.get("weight"); ++i) {
urls.push(obj.get("avatar"));
}
}
});
if(urls.length === 0) {
sendChat("Splatter","No splatter types of \""+splatterType+"\" found in table SplatterTable.")
throw "Splatter, no suitable splatterType";
}
ret = urls[randomInteger(urls.length)-1];
log("Found image: "+ret);
ret = ret.replace("max.png","thumb.png").replace("med.png","thumb.png");
log("Using image: "+ret);
return ret;
}
// Splat functions
function toDegrees(angle) { return angle * (180 / Math.PI); }
function toRadians(angle) { return angle * (Math.PI / 180); }
// Splatter's location is [top,left], and moved to random direction r pixels
function createSplat2(pageId, top, left, r, splatterType) {
var randomDirection = randomInteger(360) - 1,
createResult = createObj("graphic", {
_subtype: "token",
_pageid: pageId,
imgsrc: getSplatterImageUrl(splatterType),
top: top + Math.sin(toRadians(randomDirection)) * r,
left: left + Math.cos(toRadians(randomDirection)) * r,
width: 70,
height: 70,
rotation: randomInteger(360) - 1,
layer: "map",
flipv: randomInteger(2) === 2 ? true : false,
fliph: randomInteger(2) === 2 ? true : false,
controlledby: ""
}),
splatterEnlargeFunction = function (splatterDrawing, targetWidth, increment) {
var sx = splatterDrawing.get("width"),
sy = splatterDrawing.get("height");
log("sx: "+sx+", targetWidth: "+targetWidth);
if (sx < targetWidth) {
log("Enlarging splatter drawing");
splatterDrawing.set({width: sx + increment, height: sy + increment});
setTimeout(function () {splatterEnlargeFunction(splatterDrawing, targetWidth, increment); }, 60);
}
};
toFront(createResult);
setTimeout(function () {
splatterEnlargeFunction(createResult, 2*r, 9);
}, 100);
}
// Script to add a splat of blood to a token
on("chat:message", function (msg) {
var cmdName = "!splatter",
msgTxt = msg.content,
targetTokenId,
target,
params;
log("msgTxt: "+msgTxt);
if (msg.type === "api" && msgTxt.indexOf(cmdName) !== -1) {
params = msgTxt.split(" ");
targetTokenId = params[1];
splatterType = params[2];
if(splatterType === undefined || splatterType.length === 0) { splatterType = "blood"; }
log("Splatting some \""+splatterType+"\" for token "+targetTokenId);
if (_.isString(targetTokenId) && targetTokenId.length > 0) {
target = getObj("graphic", targetTokenId);
log("target: ");log(target);
try {
createSplat2(target.get("_pageid"), target.get("top"), target.get("left"), target.get("width") / 2, splatterType);
} catch(e) {
log(e);
}
}
}
});
// TODO: Listen to "lastmove" for a bloodtrail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment