[UserFunction] IsWithinRadius

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
KillerTHC
Posts: 63
Joined: Tue May 25, 2010 8:49 pm

[UserFunction] IsWithinRadius

#1 Post by KillerTHC » Thu Jul 22, 2010 8:38 pm

I made this function to check if the player is within a radius of a set of co-ordinates. I thought of this because it will allow me to run scripts that die and spawn in 2 different locations depending on where they died, this way I can check if I am at one rez point or the other. This code is not optimized because Lua isn't my first or second language.

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

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 24 guests