UN COD PUȚIN MAI OPTIMIZAT
#include <zcmd>
#define COLOR_LIGHTRED 0xFF6347AA
#define COLOR_GREEN 0x0ea142FF
stock GetClosestVehicle2(playerid, Float:dis)
{
new Float:X, Float:Y, Float:Z;
if (GetPlayerPos(playerid, X, Y, Z))
{
new vehicleid = INVALID_VEHICLE_ID;
dis *= dis; // Evităm operațiunea de radical prin compararea dis^2 cu temp^2
for (new v, Float:VX, Float:VY, Float:VZ; v < MAX_VEHICLES; v++)
{
if (GetVehiclePos(v, VX, VY, VZ))
{
VX -= X; VY -= Y; VZ -= Z;
new temp = VX * VX + VY * VY + VZ * VZ;
if (temp < dis)
{
dis = temp;
vehicleid = v;
}
}
}
dis = floatpower(dis, 0.5); // Redundant în acest context, puteți renunța la această linie
return vehicleid;
}
return INVALID_VEHICLE_ID;
}
CMD:closestcar(playerid, params[])
{
if(!IsPlayerAdmin(playerid))
{
SendClientMessage(playerid, COLOR_GREEN, "Nu poti folosi comanda pentru ca nu esti admin!");
return 1;
}
if(IsPlayerInAnyVehicle(playerid))
{
SendClientMessage(playerid, COLOR_LIGHTRED, "Esti deja intr-o masina!");
return 1;
}
if(GetPlayerState(playerid) != PLAYER_STATE_ONFOOT)
{
SendClientMessage(playerid, COLOR_LIGHTRED, "Nu esti in picioare langa o masina!");
return 1;
}
new Float:closestDistance = 50.0;
new cCar = GetClosestVehicle2(playerid, closestDistance);
if(cCar != INVALID_VEHICLE_ID)
{
PutPlayerInVehicle(playerid, cCar, 0);
}
else
{
SendClientMessage(playerid, COLOR_LIGHTRED, "Nu ai gasit nicio masina in apropiere!");
}
return 1;
}