GetEnemiesInRange function... functionality

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
Alkaiser
Posts: 222
Joined: Sat Sep 25, 2010 2:03 pm

GetEnemiesInRange function... functionality

#1 Post 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.
User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: GetEnemiesInRange function... functionality

#2 Post 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
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan
Post Reply