Ye sure I haven't quite finished it, theres a few tweaks needed but it works for the most part. I will add that I only a started javascript last week befor that I have only done c properly so sorry for the bad syntax. //commands for GM to attack character and announce damage
//--------------------------------------------------------//
//For the !attackfunctionn to work 4variabless need to be passed in this order
// 1. Defender Token_id
// 2. Defence type used (ac/fort/ref/will)
// 3. The modifier to hit
// 4. The attack roll (xdx+mod : 2d8+5)
//This is the macro I use:
// !ATTACK @{target|token_id} ?{Defence|ac} ?{mod|0} ?{Attack|xdx+mod}
//--------------------------------------------------------//
on("chat:message", function(msg) {
// Exit if not an api command
if (msg.type != "api") return;
// Get the API Chat Command
var command = msg.content.split(" ");
if(command[0] == '!ATTACK')
{
log(command);
//target character
var targetToken = getObj("graphic", command[2]);
var targetCharacter = targetToken.get("represents");
//algarithem to find targes defence
var targetFullDefence = 0;
var targetDefence = getAttrByName(targetCharacter, command[3]);
targetDefence = targetDefence.split("+");
do
{
//buffer is used to store remaning callculations
var buffer = "";
var i = 0;
targetDefence.forEach(function(opts) {
//eseption to find half level
if(targetDefence[i].search("@{halflevel}") != -1)
{
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "level"))/2);
}
//eseption to find def-highest ((dex/con/char/dex) - 10)/2
else if(targetDefence[i].search("highest}") != -1)
{
if(targetDefence[i].search("ac") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "dexterity") - 10)/2);
else if(targetDefence[i].search("fort") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "constitution") - 10)/2);
else if(targetDefence[i].search("will") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "charisma") - 10)/2);
else if(targetDefence[i].search("ref") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "dexterity") - 10)/2);
else
log("unusual value for 'highest'} :"+targetDefence[i]); //catchs invaled input
}
//check if an attribute needs to be found and find it
else if(targetDefence[i].search("@{") != -1)
{
targetDefence[i] = targetDefence[i].replace("@{", "");
targetDefence[i] = targetDefence[i].replace("}", "");
var temp = getAttrByName(targetCharacter, targetDefence[i]);
//if temp is another attrabut store it in buffer
if(temp.search("@{") != -1)
{
buffer += temp;
buffer += " ";
}
else // if temp is a value add it to defence
{
//only add it to defence if the valse is grater then 0 this also cathes null values
if(temp > 0)
{
targetFullDefence += Number(temp);
}
}
}
else //if the target is anything else it must be a number so add it to defence
{
targetFullDefence += Number(targetDefence[i]);
}
i++;
});
//seperate buffer into an array then save it the defence
buffer = buffer.replace("+", " ");
buffer = buffer.split(" ");
targetDefence = buffer;
} while(buffer.length > 1); //if buffer is grater then 1 it must have more values to find (there is pertental bug)
log("Target Def = "+targetFullDefence);
//roll fot attack
var attack = randomInteger(20) + Number(command[4]);
//if attack if higher then defence attack hit
log(attack+">"+targetFullDefence+"(AC)")
if(attack > targetFullDefence)
{
sendChat("AS:","/w GM Target Hit");
var damage = command[5];
var damageTotal = 0;
damage = damage.split("+");
var i = 0;
log(damage)
damage.forEach(function(opts) {
if(damage[i].search("d") != -1)
{
var buffer = damage[i].split("d");
for(i = 0; i < buffer[0]; i++)
damageTotal = randomInteger(Number(buffer[1]));
}
else
{
damageTotal += Number(damage[i]);
}
});
sendChat("","/w GM "+damageTotal+" Damage Delt");
}
else //else miss
{
sendChat("(AttckScript)","/w GM Attacker Missed");
}
}
})
This is the whole command in the while loop I use to translate the command into the numbers I put this exeption in to avoind the error: else if(targetDefence[i].search("highest}") != -1)
{
if(targetDefence[i].search("ac") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "dexterity") - 10)/2);
else if(targetDefence[i].search("fort") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "constitution") - 10)/2);
else if(targetDefence[i].search("will") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "charisma") - 10)/2);
else if(targetDefence[i].search("ref") != -1)
targetFullDefence += Math.floor(Number(getAttrByName(targetCharacter, "dexterity") - 10)/2);
else
log("unusual value for 'highest'} :"+targetDefence[i]); //catchs invaled input
}
This find the defence used then calculates the ability modifier this avoids having to directly look at "ac-highest"