|
Hello.
I wish to make a fuel consumption system. Every 100 km, it retire eg 5 liters of gas. But I also want to play with the vehicle speed, if the speed is low, it does not remove as much fuel than if the vehicle was 130 KM / H How to proceed? THX |
|
You should make a timer evry 1 minute which loops through all vehicles with engine on, so the timer should give all vehicles -1l of gas.
|
// 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);
}
}
// 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;
VehicleParams[VehInfo[vid][ModelID]-400][CONSO]
PlayerTimer1000(playerid)
{
CheckSpeed(playerid);
// Setup local variables
new Query[128];
new engine, lights, alarm, doors, bonnet, boot, objective;
new FuelStatus[30], Float:VehicleHealth, TextDrawMsg[50];
new Msg[128];
// Obtenir le model + l'id du veh
new vid = GetPlayerVehicleID(playerid);
if(vid != 0)
{
new vModel = GetVehicleModel(vid);
}
// Joueur а l'intйrieur d'un veh
if(vid != 0)
{
// Si le joueur est conducteur
if(GetPlayerVehicleSeat(playerid) == 0)
{
// Si le veh n'est pas un vйlo
if(!IsABike(vid))
{
GetVehicleParamsEx(vid, engine, lights, alarm, doors, bonnet, boot, objective);
// Moteur = 1
if (engine == 1)
{
// Check if the speed is above 10 kph and if the vehicle didn't run out of fuel
if((VitessePl[playerid] > 10) && (VehInfo[vid][Essence] > 0))
{
new Float:Conso = VehicleParams[VehInfo[vid][ModelID]-400][CONSO] * (VitessePl[playerid]/100.0) * 1.0;
}
else
{ // En dessous de 10 KM/H
new Float:Conso = VehicleParams[VehInfo[vid][ModelID]-400][CONSO] * 0.05 * 1.0;
// Consume fuel
VehInfo[vid][Essence] = VehInfo[vid][Essence] - Conso;
}
// Panne d'essence
if(VehInfo[vid][Essence] <= 0.0)
{
VehInfo[vid][Essence] = 0.0;
SetVehicleParamsEx(vid, 0, 0, alarm, doors, bonnet, boot, objective);
}
}
}
if(VehicleParams[VehInfo[vid][ModelID]-400][CONSO] != 0)
{
format(string, sizeof(string), "%d", VehInfo[vid][Essence]);
PlayerTextDrawSetString(playerid, Textdraw5[playerid], string);
}
}
}
stock CheckSpeed(playerid)
{
new vid = GetPlayerVehicleID(playerid),
Float:Vitesse[3];
if (vid != 0)
{
GetVehicleVelocity(vid, Vitesse[0], Vitesse[1], Vitesse[2]);
}
else
{
GetPlayerVelocity(playerid, Vitesse[0], Vitesse[1], Vitesse[2]);
}
// Calculate speed of the player in kph
SpeedFloat = floatsqroot(((Vitesse[0] * Vitesse[0]) + (Vitesse[1] * Vitesse[1])) + (Vitesse[2] * Vitesse[2])) * 179.0; // kph
// Convert the float value to an int value, also calculate the speed in mph
SpeedKph = floatround(SpeedFloat, floatround_round);
// Also save the speed for the player
VitessePl[playerid] = SpeedKph;
}
// Top of script
new GTimer1000;
// OnGameModeInit
GTimer1000 = SetTimer("GlobalTimer1000", 1000, true);
// OnGameModeExit
KillTimer(GTimer1000);
// Somewhere in the script
forward GlobalTimer1000();
public GlobalTimer1000()
{
// Loop through all players and only run the timer for each player who's logged in
for (new playerid; playerid < MAX_PLAYERS; playerid++)
if (APlayerData[playerid][LoggedIn] == true)
PlayerTimer1000(playerid);
}