stock GetClosestVehicle(playerid, Float:range){ new Float:p_X; new Float:p_Y; new Float:p_Z; new Float:Distance; new Float:PretendentDistance = range +1; new Pretendent; GetPlayerPos(playerid, p_X, p_Y, p_Z); for(new vehicleid=1; vehicleid < MAX_VEHICLES; vehicleid++){ Distance = GetVehicleDistanceFromPoint(vehicleid, p_X, p_Y, p_Z); if(Distance <= range && Distance <= PretendentDistance){ Pretendent = vehicleid; PretendentDistance = Distance; } } return Pretendent; } new vehicleid = GetClosestVehicle(playerid, 10.0);
native IsValidVehicle(vehicleid); // According to the wiki, it doesn't exist in the default sa-mp includes, so you have to add it. GetClosestVehicle(playerid, Float: range) { new vehicleid = -1, Float: last = range, Float: current, Float: x, Float: y, Float: z; GetPlayerPos(playerid, x, y, z); for (new i = 1, j = GetVehiclePoolSize(); i <= j; i++) // for (new i = 1; i < MAX_VEHICLES; i++) { if (!IsValidVehicle(i)) continue; current = GetVehicleDistanceFromPoint(i, x, y, z); if (current < last) { vehicleid = i; last = current; } } return vehicleid; }
native IsValidVehicle(vehicleid);
GetClosestVehicle(playerid, Float:range) {
new Float:VehiclePos[4], nearestID;
nearestID = 65535;
for(new vehicleid = 0; vehicleid < MAX_VEHICLES; vehicleid++) {
if(IsValidVehicle(vehicleid)) {
GetVehiclePos(vehicleid, VehiclePos[1], VehiclePos[2], VehiclePos[3]);
if(IsPlayerInRangeOfPoint(playerid, range, VehiclePos[1], VehiclePos[2], VehiclePos[3])) {
nearestID = vehicleid;
break;
}
}
}
return nearestID;
}
You should start using GetVehiclePoolSize (Unless you're not using SA-MP 0.3.7)
It retrieves the highest vehicle ID, so instead of 2000 (MAX_VEHICLES), you'd be using something much lower if there isn't a lot of vehicles. I just wrote this, so I'm not sure if it has any bugs or not. Код:
native IsValidVehicle(vehicleid); // According to the wiki, it doesn't exist in the default sa-mp includes, so you have to add it. GetClosestVehicle(playerid, Float: range) { new vehicleid = -1, Float: last = range, Float: current, Float: x, Float: y, Float: z; GetPlayerPos(playerid, x, y, z); for (new i = 1, j = GetVehiclePoolSize(); i <= j; i++) // for (new i = 1; i < MAX_VEHICLES; i++) { if (!IsValidVehicle(i)) continue; current = GetVehicleDistanceFromPoint(i, x, y, z); if (current < last) { vehicleid = i; last = current; } } return vehicleid; } |
If enabling camera target (EnablePlayerCameraTarget), retrieving the closest vehicle (GetPlayerCameraTargetVehicle) and then disabling the camera target would be possible, that would be great and we wouldn't even need a loop. Unfortunately, it has to be enabled on connect otherwise it returns INVALID_VEHICLE_ID (just tested it).
|