Quote:
Originally Posted by Chompy
They aren't for checking distances tho
it's to draw pretty lights
But, the only thing I mislike in those scripts is that you edit variables with the same name as the function params  Only that, other then that they are very nice 
|
Only small adjustments are required in order to check if a particular baddy have a clear line of sight to the intended target.
PHP Code:
function lineOfSight(x0, y0, x1, y1)
{
// Things can screw up if we don't use integers.
x0 = int(x0); y0 = int(y0); x1 = int(x1); y1 = int(y1);
dx = abs(x1 - x0) << 1;
dy = abs(y1 - y0) << 1;
if (x1 > x0) ix = 1;
else ix = -1;
if (y1 > y0) iy = 1;
else iy = -1;
if (onwall(x0, y0)) return false;
if (dx >= dy) {
error = dy - (dx >> 1);
while (x0 != x1) {
if (error >= 0) {
if (error != 0 || (ix > 0)) {
y0 += iy;
error -= dx;
}
}
x0 += ix;
error += dy;
if (onwall(x0, y0)) return false;
}
}
else {
error = dx - (dy >> 1);
while (y0 != y1) {
if (error >= 0) {
if (error != 0 || iy > 0) {
x0 += ix;
error -= dy;
}
}
y0 += iy;
error += dx;
if (onwall(x0, y0)) return false;
}
}
// Success
return true;
}
If you want distance with that, add a counter or employ trigonometry in a separate function.