GetEnemiesInRange function... functionality
Posted: Sun Oct 10, 2010 9:18 pm
I put together the following function from code I cannibalized from other posts.
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.
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