A direct copy from my filterscript (other pieces removed from this timer as they aren't relevant), which holds the fuel-system, don't copy this literally.
Also, some variables are still included but may not be relevant to this code as well, as they are used by other systems in this timer.
pawn Код:
// This function is called per player by the "GlobalTimer1000" timer
PlayerTimer1000(playerid)
{
// Setup local variables
new vid, vModel, JailMsg[40], JailMins, JailSecs, Query[128];
new engine, lights, alarm, doors, bonnet, boot, objective, Float:Consumption;
new FuelStatus[30], Float:VehicleHealth, TextDrawMsg[50];
new Msg[128];
// Get the ID of the player's vehicle and the model of the vehicle
vid = GetPlayerVehicleID(playerid);
if (vid != 0)
vModel = GetVehicleModel(vid);
// **********************************************************************************************************************************************************
// Fuel system
// **********************************************************************************************************************************************************
// If the player is inside a vehicle
if(vid != 0)
{
// Check if the player is the driver of the vehicle (prevent fuel-consumption for every passenger to restrict fuel-consumption to driver only)
if (GetPlayerVehicleSeat(playerid) == 0)
{
// Check if the vehicle consumes fuel
if (Vehicle_ConsumesFuel(vModel) == 1)
{
// Get the status of all vehicle parts
GetVehicleParamsEx(vid, engine, lights, alarm, doors, bonnet, boot, objective);
// Only consume fuel if the engine is turned on
if (engine == 1)
{
// Check if the speed is above 10 kph and if the vehicle didn't run out of fuel
if ((APlayerData[playerid][PlayerSpeed] > 10) && (AVehicleData[vid][Fuel] > 0.0))
// Calculate the fuel-consumption for this vehicle-model based on his speed
Consumption = AVehicleInfo[(vModel - 400)][FuelConsumption] * (APlayerData[playerid][PlayerSpeed] / 100.0) * GlobalConsumptionMultiplier;
else // The vehicle is driving slower than 10 kph
// Calculate the fuel-consumption for this vehicle-model as if it were driving 5 kph (5 kph divided by 100.0)
Consumption = AVehicleInfo[(vModel - 400)][FuelConsumption] * 0.05 * GlobalConsumptionMultiplier;
// Consume fuel
AVehicleData[vid][Fuel] = AVehicleData[vid][Fuel] - Consumption;
}
// If the vehicle ran out of fuel, turn off the engine and lights and set fuel to 0.0 (prevents displaying negative values)
if (AVehicleData[vid][Fuel] <= 0.0)
{
AVehicleData[vid][Fuel] = 0.0;
SetVehicleParamsEx(vid, 0, 0, alarm, doors, bonnet, boot, objective);
}
}
}
// Check if the vehicle consumes fuel
if (Vehicle_ConsumesFuel(vModel) == 0)
{
// Preset the fuel-readout to inform the player this vehicle doesn't consume fuel
format(FuelStatus, 30, "~b~N/A");
}
else // The vehicle consumes fuel, so construct the fuel-gauge
{
// Calculate the scale for the fuel-readout, generate the readout
switch (floatround(((AVehicleData[vid][Fuel] / VehicleModel_GetMaxFuel(vModel)) * 10.0), floatround_round))
{
case 0: format(FuelStatus, 30, "~r~%s", "IIIIIIIII"); // Fuel is between 0% and 5% full
case 1: format(FuelStatus, 30, "~g~%s~r~%s", "I", "IIIIIIIII"); // Fuel is between 5% and 15% full
case 2: format(FuelStatus, 30, "~g~%s~r~%s", "II", "IIIIIIII"); // Fuel is between 15% and 25% full
case 3: format(FuelStatus, 30, "~g~%s~r~%s", "III", "IIIIIII"); // Fuel is between 25% and 35% full
case 4: format(FuelStatus, 30, "~g~%s~r~%s", "IIII", "IIIIII"); // Fuel is between 35% and 45% full
case 5: format(FuelStatus, 30, "~g~%s~r~%s", "IIIII", "IIIII"); // Fuel is between 45% and 55% full
case 6: format(FuelStatus, 30, "~g~%s~r~%s", "IIIIII", "IIII"); // Fuel is between 55% and 65% full
case 7: format(FuelStatus, 30, "~g~%s~r~%s", "IIIIIII", "III"); // Fuel is between 65% and 75% full
case 8: format(FuelStatus, 30, "~g~%s~r~%s", "IIIIIIII", "II"); // Fuel is between 75% and 85% full
case 9: format(FuelStatus, 30, "~g~%s~r~%s", "IIIIIIIII", "I"); // Fuel is between 85% and 95% full
case 10: format(FuelStatus, 30, "~g~%s", "IIIIIIIIII"); // Fuel is between 95% and 100% full
}
}
// Update the vehicle-fuel textdraw
format(TextDrawMsg, 50, "~w~Fuel: %s", FuelStatus);
PlayerTextDrawSetString(playerid, APlayerData[playerid][TDVehicleFuel], TextDrawMsg);
}
}
The "Vehicle_ConsumesFuel" function just returns "1" if the vehicle-model holds a fuel-tank larger than 0,25 litres.
"GlobalConsumptionMultiplier" is just a global float value where admins can multiply the default fuel consumption for all vehicle-models at once and is set to 1.0 by default.
Calculating the playerspeed is done using this code:
pawn Код:
// Get ID of the player's vehicle, as well as the speed (of either the vehicle or the player on foot)
vid = GetPlayerVehicleID(playerid);
if (vid != 0)
GetVehicleVelocity(vid, SpeedX, SpeedY, SpeedZ);
else
GetPlayerVelocity(playerid, SpeedX, SpeedY, SpeedZ);
// Calculate speed of the player in kph
SpeedFloat = floatsqroot(((SpeedX * SpeedX) + (SpeedY * SpeedY)) + (SpeedZ * SpeedZ)) * 179.0; // kph
// Convert the float value to an int value, also calculate the speed in mph
SpeedKph = floatround(SpeedFloat, floatround_round);
SpeedMph = floatround((SpeedFloat / 1.6), floatround_round);
// Also save the speed for the player
APlayerData[playerid][PlayerSpeed] = SpeedKph;