• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

function isNear (cid, key, dist, value)

Strack

Member
Joined
May 15, 2009
Messages
199
Reaction score
14
well that's nothing special, its only a function which cheks if the player is near to something (near another player, near an item, near an npc, near a monster)

add to data/lib/050-function:
Lua:
function isNear (cid, key, dist, value) --by strack
local pos=getCreaturePosition(cid)
local players = getPlayersOnline()
local areaPosition =
{
        {x=pos.x-dist, y=pos.y-dist, z=pos.z, stackpos = 255},
        {x=pos.x+dist, y=pos.y+dist, z=pos.z, stackpos = 255}
}

if key == "item" then
   for i=1, 255 do
	for areax = (pos.x - dist), (pos.x + dist) do
		for areay = (pos.y - dist), (pos.y + dist) do
			local area = {x = areax, y = areay, z = pos.z, stackpos = i}
			if getThingFromPos(area).itemid == value then
				return true
			end
		end
	end
   end
	
elseif key == "player" then
	if value ~=nil then
		local jug = getCreatureByName(value)
			if isPlayer(jug) == TRUE then
				if isInRange(getCreaturePosition(jug), areaPosition[1], areaPosition[2]) == TRUE then
                   return TRUE
				end
			end
	else
		for _, pid in ipairs(players) do
				if isInRange(getCreaturePosition(pid), areaPosition[1], areaPosition[2]) == TRUE then
                   return TRUE
				end
		end				
	end
elseif key == "npc" then
	if value ~=nil then
		local getNpc = getCreatureByName(value)
		if isNpc(getNpc) == TRUE then
			if isInRange(getCreaturePosition(getNpc), areaPosition[1], areaPosition[2]) == TRUE then
				return true
			end
		end
	end
elseif key == "monster" then
	if value ~=nil then
		local getNpc = getCreatureByName(value)
		if isMonster(getNpc) == TRUE then
			if isInRange(getCreaturePosition(getNpc), areaPosition[1], areaPosition[2]) == TRUE then
				return true
			end
		end
	else	
	local monsters = {}
	pos=getCreaturePosition(cid)
	for x=1,17 do
		for y=1,17 do
			getpos={x=pos.x-9+x,y=pos.y-9+y,z=pos.z,stackpos=STACKPOS_TOP_CREATURE}
			if isCreature(getTopCreature(getpos).uid) == TRUE then
			table.insert(monsters, getTopCreature(getpos).uid)
			end
		end
	end
	
	for i=1, #monsters do
		if isMonster(monsters[i]) == TRUE then
			if isInRange(getCreaturePosition(monsters[i]), areaPosition[1], areaPosition[2]) == TRUE then
                   return TRUE
			end
		end
	end
	end
end
end

I know it could be shorter but it runs perfectly :p

explanation:
Lua:
if isNear (cid, "type", distance, "value")
--type = monster, npc, player or item
--distance = distance between cid and the 'target'
--value = if its item, that's the id, else thats the name //(can be null on names)


examples of use:
Lua:
if isNear (cid, "monster", 2, "demon")
-- checks if the monster, 'demon' is at 2 sql of distance of less

Lua:
if isNear (cid, "monster", 2)
--checks if there is any monster at a distance of 2

Lua:
if isNear (cid, "item", 2, 2299)
-- chekcs if the itemid 2299 is at distance 2 or less

Lua:
if isNear (cid, "player", 2)
--checks for any player at that distance

Lua:
if isNear (cid, "player", 2, "pepito")
--checks for a player with name 'pepito' at the distance of 2 or less

Lua:
if isNear (cid, "npc", 2, "lola")
--checks for the npc 'lola' at that distance


Hope you like it ;)
 
Last edited:
Out of curiosity the distance calculation as it is would be a square area and not circle, correct?
 
Back
Top