Something like this should do the trick.
GameObject * getClosestVehicle(GameObject *sourceObject)
{
GenericSLNode *x = SmartGameObjList->HeadNode;
float closestDist = FLT_MAX;
Vector3 sourceDist = Commands->Get_Position(sourceObject);
GameObject *closestVeh = NULL;
while (x)
{
GameObject *o = (GameObject *)x->NodeData;
if (o && o != sourceObject && Is_Vehicle(o))
{
Vector3 TargetPos = Commands->Get_Position(o);
float distance = Commands->Get_Distance(sourceDist, TargetPos);
if (distance < closestDist)
{
closestDist = distance;
closestVeh = o;
}
}
x = x->NodeNext;
}
return closestVeh;
}
NOTE: This code might not be 100% because I wrote it down quickly in Notepad.
[Updated on: Sun, 08 May 2011 13:00]
Report message to a moderator