-
-
Save palikhov/62bef4f85dc230611827a47102fb0505 to your computer and use it in GitHub Desktop.
Roll20 API Script to create aoe templates at tokens.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//TemplateGenerator | |
//make the macro seen in other file. | |
//Make a rollable table with template shapes, and names being: Circle, Cone, Square, Line. Table name is: TemplateShapes | |
on("ready", function() { | |
var getCleanImgsrc = function (imgsrc) { | |
var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^\?]*)(\?[^?]+)?$/); | |
if(parts) { | |
return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`); | |
} | |
return; | |
}; | |
on("chat:message", function (msg) { | |
if (msg.type === "api" && msg.content.split(' ')[0] === "!TemplateGenerator") { | |
MakeShape(msg); | |
} | |
/*if (msg.type === "api" && msg.content.split(' ')[0] === "!ClearTemplates") { | |
ClearShapes(); | |
}*/ | |
}); | |
function MakeShape(msg) | |
{ | |
let campaign = findObjs({type: "campaign"})[0]; | |
let pageID = campaign.get("playerpageid"); | |
let gridScale = 5 / parseFloat(findObjs({_type: 'page', _id: pageID})[0].get('scale_number')); | |
if(!msg.selected) | |
{ | |
sendChat("", "/w " + msg.who + " no token selected for generation point."); | |
return; | |
} | |
let playerToken = getObj(msg.selected[0]._type, msg.selected[0]._id); | |
let shapeName = msg.content.split(' ')[1]; | |
let shapeTable = findObjs({_type: 'rollabletable', name: 'TemplateShapes'})[0]; | |
let shapeItem = findObjs({_type: 'tableitem', _rollabletableid: shapeTable.id, name: shapeName})[0]; | |
let shapeSize = msg.content.split(' ')[2] * gridScale; | |
if (shapeName == "Circle") {shapeSize *= 2; } | |
let playerID = msg.playerid; | |
if (shapeName != 'Line' && shapeName != 'Cone') | |
{ | |
createObj('graphic', { | |
imgsrc: getCleanImgsrc(shapeItem.get('avatar')), | |
name: shapeName, | |
left: playerToken.get('left'), | |
top: playerToken.get('top'), | |
_pageid: pageID, | |
layer: "objects", | |
height: parseInt(shapeSize)*14, | |
width: parseInt(shapeSize)*14, | |
controlledby: playerID | |
}); | |
} | |
else if (shapeName === 'Line'|| shapeName === 'Cone') | |
{ | |
let lineOffsetTop = 0; | |
let lineOffsetLeft = 0; | |
let lineAngle = 0; | |
if (msg.content.split(' ')[3]) | |
{ | |
lineAngle = parseInt(msg.content.split(' ')[3]); | |
lineAngleRad = lineAngle*Math.PI/180; | |
if (lineAngle == 0 || lineAngle == 90 || lineAngle == 180 || lineAngle == 270) | |
{ | |
lineOffsetTop = ((5*gridScale+ parseInt(shapeSize))/2)*Math.sin(lineAngleRad); | |
lineOffsetLeft = ((5*gridScale+ parseInt(shapeSize))/2)*Math.cos(lineAngleRad); | |
} | |
else | |
{ | |
lineOffsetTop = ((7*gridScale+ parseInt(shapeSize))/2)*Math.sin(lineAngleRad); | |
lineOffsetLeft = ((7*gridScale+ parseInt(shapeSize))/2)*Math.cos(lineAngleRad); | |
} | |
} | |
if (shapeName === 'Line') | |
{ | |
createObj('graphic', { | |
imgsrc: getCleanImgsrc(shapeItem.get('avatar')), | |
name: shapeName, | |
left: playerToken.get('left') + lineOffsetLeft*14, | |
top: playerToken.get('top') - lineOffsetTop*14, | |
_pageid: pageID, | |
layer: "objects", | |
height: 70 * gridScale, | |
width: parseInt(shapeSize)*14, | |
controlledby: playerID, | |
rotation: -1*lineAngle | |
}); | |
} | |
if (shapeName === 'Cone') | |
{ | |
createObj('graphic', { | |
imgsrc: getCleanImgsrc(shapeItem.get('avatar')), | |
name: shapeName, | |
left: playerToken.get('left') + lineOffsetLeft*14, | |
top: playerToken.get('top') - lineOffsetTop*14, | |
_pageid: pageID, | |
layer: "objects", | |
height: parseInt(shapeSize)*14, | |
width: parseInt(shapeSize)*14, | |
controlledby: playerID, | |
rotation: -1*(lineAngle + 90) | |
}); | |
} | |
} | |
} | |
/*function ClearShapes() | |
{ | |
let campaign = findObjs({type: "campaign"})[0]; | |
let pageID = campaign.get("playerpageid"); | |
let circles = findObjs({type: 'graphic', _pageid: pageID, name: 'Circle'}); | |
for (var i = 0; i< circles.length; i++) | |
{ | |
circles[i].remove(); | |
} | |
}*/ | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
&{template:npcaction} {{name=Size and Shape}} {{rname=Shape Maker}} {{description=**Circle Radius** | |
[5 ft](!TemplateGenerator Circle 5) | [10 ft](!TemplateGenerator Circle 10) | [20 ft](!TemplateGenerator Circle 20) | [30 ft](!TemplateGenerator Circle 30) | [40 ft](!TemplateGenerator Circle 40) | |
________________________ | |
**Square Side** | |
[10 ft](!TemplateGenerator Square 10) | [15 ft](!TemplateGenerator Square 15) | [20 ft](!TemplateGenerator Square 20) | [40 ft](!TemplateGenerator Square 40) | |
________________________ | |
**Cones** | |
[60](!TemplateGenerator Cone 60 135) -------- [60](!TemplateGenerator Cone 60 90) -------- [60](!TemplateGenerator Cone 60 45) | |
----[30](!TemplateGenerator Cone 30 135) ---- [30](!TemplateGenerator Cone 30 90) ---- [30](!TemplateGenerator Cone 30 45)---- | |
--------[15](!TemplateGenerator Cone 15 135) [15](!TemplateGenerator Cone 15 90) [15](!TemplateGenerator Cone 15 45)--------- | |
[6](!TemplateGenerator Cone 60 180) [3](!TemplateGenerator Cone 30 180) [15](!TemplateGenerator Cone 15 180)🧙[15](!TemplateGenerator Cone 15 0) [3](!TemplateGenerator Cone 30 0) [6](!TemplateGenerator Cone 60 0) | |
--------[15](!TemplateGenerator Cone 15 225) [15](!TemplateGenerator Cone 15 270) [15](!TemplateGenerator Cone 15 315)--------- | |
----[30](!TemplateGenerator Cone 30 225) ---- [30](!TemplateGenerator Cone 30 270) ---- [30](!TemplateGenerator Cone 30 315)---- | |
[60](!TemplateGenerator Cone 60 225) -------- [60](!TemplateGenerator Cone 60 270) -------- [60](!TemplateGenerator Cone 60 315) | |
________________________ | |
**Lines** | |
[100](!TemplateGenerator Line 100 135)------[100](!TemplateGenerator Line 100 90)------[100](!TemplateGenerator Line 100 45) | |
----[60](!TemplateGenerator Line 60 135) --- [60](!TemplateGenerator Line 60 90) ---- [60](!TemplateGenerator Line 60 45)---- | |
--------[30](!TemplateGenerator Line 30 135) [30](!TemplateGenerator Line 30 90) [30](!TemplateGenerator Line 30 45)--------- | |
[10](!TemplateGenerator Line 100 180) [6](!TemplateGenerator Line 60 180) [3](!TemplateGenerator Line 30 180)🧙[3](!TemplateGenerator Line 30 0) [6](!TemplateGenerator Line 60 0) [10](!TemplateGenerator Line 100 0) | |
--------[30](!TemplateGenerator Line 30 225) [30](!TemplateGenerator Line 30 270) [30](!TemplateGenerator Line 30 315)--------- | |
----[60](!TemplateGenerator Line 60 225) --- [60](!TemplateGenerator Line 60 270) --- [60](!TemplateGenerator Line 60 315)---- | |
[100](!TemplateGenerator Line 100 225)------[100](!TemplateGenerator Line 100 270)------[100](!TemplateGenerator Line 100 315) | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment