Skip to content

Commit

Permalink
Added "GTA::World::GetNearbyPeds" and "GTA::World::GetNearbyVehicles"
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire committed Apr 30, 2015
1 parent f8ed7a2 commit f9b6637
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
77 changes: 77 additions & 0 deletions source/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,83 @@ namespace GTA
Native::Function::Call(Native::Hash::SET_GRAVITY_LEVEL, value);
}

array<Ped ^> ^World::GetNearbyPeds(Ped ^ped, float radius)
{
return GetNearbyPeds(ped, radius, 10000);
}
array<Ped ^> ^World::GetNearbyPeds(Ped ^ped, float radius, int maxAmount)
{
const Math::Vector3 position = ped->Position;
System::Collections::Generic::List<Ped ^> ^result = gcnew System::Collections::Generic::List<Ped ^>();
int *handles = new int[maxAmount + 1];

try
{
handles[0] = maxAmount;

const int amount = Native::Function::Call<int>(Native::Hash::GET_PED_NEARBY_PEDS, ped->ID, handles, -1);

for (int i = 0; i < amount; ++i)
{
const int index = i + 1;

if (handles[index] != 0 && Native::Function::Call<bool>(Native::Hash::DOES_ENTITY_EXIST, handles[index]))
{
Ped ^currped = gcnew Ped(handles[index]);

if (Math::Vector3::Subtract(position, currped->Position).LengthSquared() < radius * radius)
{
result->Add(currped);
}
}
}
}
finally
{
delete[] handles;
}

return result->ToArray();
}
array<Vehicle ^> ^World::GetNearbyVehicles(Ped ^ped, float radius)
{
return GetNearbyVehicles(ped, radius, 10000);
}
array<Vehicle ^> ^World::GetNearbyVehicles(Ped ^ped, float radius, int maxAmount)
{
const Math::Vector3 position = ped->Position;
System::Collections::Generic::List<Vehicle ^> ^result = gcnew System::Collections::Generic::List<Vehicle ^>();
int *handles = new int[maxAmount * 2 + 2];

try
{
handles[0] = maxAmount;

const int amount = Native::Function::Call<int>(Native::Hash::GET_PED_NEARBY_VEHICLES, ped->ID, handles, -1);

for (int i = 0; i < amount; ++i)
{
const int index = i * 2 + 2;

if (handles[index] != 0 && Native::Function::Call<bool>(Native::Hash::DOES_ENTITY_EXIST, handles[index]))
{
Vehicle ^vehicle = gcnew Vehicle(handles[index]);

if (Math::Vector3::Subtract(position, vehicle->Position).LengthSquared() < radius * radius)
{
result->Add(vehicle);
}
}
}
}
finally
{
delete[] handles;
}

return result->ToArray();
}

Ped ^World::CreatePed(Model model, Math::Vector3 position)
{
return CreatePed(model, position, 0.0f);
Expand Down
5 changes: 5 additions & 0 deletions source/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ namespace GTA
void set(int value);
}

static array<Ped ^> ^GetNearbyPeds(Ped ^ped, float radius);
static array<Ped ^> ^GetNearbyPeds(Ped ^ped, float radius, int maxAmount);
static array<Vehicle ^> ^GetNearbyVehicles(Ped ^ped, float radius);
static array<Vehicle ^> ^GetNearbyVehicles(Ped ^ped, float radius, int maxAmount);

static Ped ^CreatePed(Model model, Math::Vector3 position);
static Ped ^CreatePed(Model model, Math::Vector3 position, float heading);
static Vehicle ^CreateVehicle(Model model, Math::Vector3 position);
Expand Down

0 comments on commit f9b6637

Please sign in to comment.