Code: Select all
function round (x) --Round function to convert the co-ordinates to whole numbers
if x >= 0 then
return math.floor (x + 0.5)
end -- if positive
return math.ceil (x - 0.5)
end -- function round
function IsWithinRadius(x,z,radius)
local playerX = round(player.X);
local playerZ = round(player.Z);
local dbug = false;
local hasX, hasZ = false;
if( radius ~= nil and radius == 0 ) then
radius = 30;
end
local topLeft = {};
local topRight = {};
local bottomLeft = {};
local bottomRight = {};
topLeft.X = x - radius;
topLeft.Z = z + radius;
topRight.X = x + radius;
topRight.Z = z + radius;
bottomLeft.X = x - radius;
bottomLeft.Z = z - radius;
bottomRight.X = x + radius;
bottomRight.Z = z - radius;
local xDifference = topRight.X - topLeft.X;
local zDifference = topLeft.Z - bottomLeft.Z;
if( dbug ) then
printf("Player X="..playerX.."\n");
printf("Player Z="..playerZ.."\n");
end
for i = 0,xDifference do
local newX = topLeft.X + i;
if( dbug ) then
printf("X="..newX.."\n");
end
if( newX == playerX ) then
hasX = true;
if( dbug ) then
printf("We found a match in X");
end
end
end
for y = 0,zDifference do
local newZ = bottomLeft.Z + y;
if( dbug ) then
printf("Z="..newZ.."\n");
end
if( newZ == playerZ ) then
hasZ = true;
if( dbug ) then
printf("We found a match in Z");
end
end
end
if( hasX and hasZ ) then
return true;
else
return false;
end
end