SNIPPET : findNextNearestInGroup

Go down

SNIPPET : findNextNearestInGroup Empty SNIPPET : findNextNearestInGroup

Post by TemplarGFX on Fri 08 Mar 2013, 03:03

hey Everyone,

Some of you may find this handy :

Code:
function findNextNearestInGroup(groupName, unitName, goalName, minDist, distType, closerThanGoal)
  local size, goal_distance, closest_name, closest_distance, member, valid, distance_to_unit, distance_to_goal
  --[[
    groupName      : Name of the group to search through
    unitName      : Name of unit to find closest group member to
    goalName      : Name of the entity that is this units goal
    minDist        : Minimum distance a member and unit/goal
    distType = 1  : Ignore members closer than minDist to unitName
    distType = 2  : Ignore members closer than minDist to goalName
    distType = 3  : Ignore members closer than minDist to unitName or goalName
    closerThanGoal : Ignore members that are further than goalName from unitName
  --]]
  size = OFP:getGroupSize(groupName) - 1
  goal_distance = OFP:getDistance(unitName, goalName)
  closest_name = ""
  closest_distance = 123456789
  for i=0, size do
    member = OFP:getGroupMember(groupName, i)
    valid = false
    distance_to_unit = OFP:getDistance(unitName, member)
    distance_to_goal = OFP:getDistance(goalName, member)
    if closerThanGoal ==  true then
      if distance_to_goal <= goal_distance then
        valid = true
      end
    else
      if distType == 1 then
        if distance_to_unit > minDist then
          valid = true
        end
      end
      if distType == 2 then
        if distance_to_goal > minDist then
          valid = true
        end
      end
      if distType == 3 then
        if distance_to_goal > minDist and distance_to_unit > minDist then
          valid = true
        end
      end
    end
    if valid == true then
      if distance_to_unit < closest_distance then
        closest_name = member
        closest_distance = distance_to_unit
      end
    end
  end
  return closest_name
end

TemplarGFX
Veteran

Points : 159
Reputation : 18
Join date : 2013-02-20

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum