Page 1 of 1

GetEnemiesInRange function... functionality

Posted: Sun Oct 10, 2010 9:18 pm
by Alkaiser
I put together the following function from code I cannibalized from other posts.

Code: Select all

function CPlayer:GetEnemiesInRange(_range)
	_range = _range or 50
	local function isInDist(x1, y1, x2, y2, radius)
		if( x2 >= x1-radius and x2 <= x1+radius and
			y2 >= y1-radius and y2 <= y1+radius ) then

			if( ((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)) <= radius*radius ) then
				return true
			else
				return false
			end
		end
		return false
	end
	local count = 0
	local obj = nil
	local objectList = CObjectList()
	objectList:update()
	for i = 0,objectList:size() do
		obj = objectList:getObject(i)
		if( obj.Type == PT_MONSTER ) then
			if( isInDist(self.X, self.Z, obj.X, obj.Z, _range) ) then
				count = count + 1
			end
		end
	end
	return count
end
I would like it to count only hostile mobs that are within the specified radius, but it seems the PT_MONSTER type specification is too broad. It includes things such as your pet and non mob entities like "Entrance to..." etc. I tried obj.TargetPtr == player.Address, but that always seemed to result in a nil value.

Re: GetEnemiesInRange function... functionality

Posted: Sun Oct 10, 2010 9:57 pm
by rock5
You can try putting in exceptions for your pet and other objects, eg.

Code: Select all

      if( obj.Type == PT_MONSTER and obj.Address ~= player.PetPtr and not string.find(obj.Name,"Entrance to") )then