//#CLIENTSIDE
function onMouseDown()
{
if (leftmousebutton)
{
temp.targetPlayer = players[testplayer(mousex, mousey)];
temp.targetNPC = npcs[testnpc(mousex, mousey)];
if (temp.targetNPC != null)
{
temp.target = {temp.targetNPC.x + 1.5, temp.targetNPC.y + 2.0, temp.targetNPC.z + 1};
}
elseif (temp.targetPlayer.account != null && temp.targetPlayer.account != player.account)
{
temp.target = {temp.targetPlayer.x + 1.5, temp.targetPlayer.y + 2.0, temp.targetPlayer.z + 1};
}
else
{
temp.target = {mousex, mousey, getz(mousex, mousey)};
}
shootAt(player.x, player.y, player.z, temp.target[0], temp.target[1], temp.target[2], "arrow", "barrow0.png");
}
}
function shootAt(temp.ox, temp.oy, temp.oz, temp.tx, temp.ty, temp.tz, temp.gani, temp.ganiparam)
{
temp.timeStep = 0.05;
temp.origin = {temp.ox, temp.oy, temp.oz};
temp.target = {temp.tx, temp.ty, temp.tz};
temp.dx = (temp.target[0] - 1.5) - temp.origin[0];
temp.dy = (temp.target[1] - 2.0) - temp.origin[1];
temp.dz = temp.target[2] - temp.origin[2];
temp.horizontalDistance = (temp.dx^2 + temp.dy^2) ^ 0.5; //Pythagoras' theorem
temp.verticalDistance = temp.dz;
temp.angle = getangle(temp.dx, temp.dy);
//Look for 'ballistic trajectories' to find the equations used here.
//Two lines of magic incantations to prevent an accuracy error. (Possibly caused by the projectile only being updated every 0.05 seconds)
temp.precognition_horizontalVelocity = 20;
temp.timeToTarget = shared.roundTo(temp.horizontalDistance / temp.precognition_horizontalVelocity, temp.timeStep); //Constant horizontal velocity.
//Alternatively: temp.timeToTarget = 1; //Constant time to reach target.
temp.horizontalVelocity = (temp.horizontalDistance / temp.timeToTarget);
temp.verticalVelocity = 0.5 * (gravity / temp.timeStep) * temp.timeToTarget + (temp.verticalDistance / temp.timeToTarget);
//Trigonometry
if (temp.horizontalVelocity != 0) {
temp.zangle = arctan(temp.verticalVelocity / temp.horizontalVelocity);
}
else {
//Happens when the origin and the target position are the same.
temp.zangle = pi / 2;
}
temp.power = temp.verticalVelocity / sin(temp.zangle) * temp.timeStep;
shoot(temp.origin[0], temp.origin[1], temp.origin[2],
temp.angle, temp.zangle, temp.power,
temp.gani, temp.ganiparam);
}