System fuel cosumption.
#1

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
Reply
#2

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
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
That's a very indepth system if your going to add car speed in. If it's an RP server, I wouldn't bother with it.

On the other hand, making a fuel system is easy enough.

You need to have a timer, that will drop the fuel every few seconds.
Reply
#3

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.
Reply
#4

Quote:
Originally Posted by Gogeta
Посмотреть сообщение
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.
Look all my subject...
Reply
#5

Just make a timer which checks if the player is inside a vehicle as driver, with the engine turned on, and make it run every second.
You could make it run every minute, but players could abuse it by getting out of the vehicle every 55 seconds (when they time it right) and consume 0 fuel.

If he is, multiply your required fuel consumption with the speed, then divide by 100.
That will make your vehicle consume more fuel at higher speeds.
Say you want vehicles to consume 5l per 100km when they drive 100kph.
Your fuel consumption would be:
5 litres per hour => 5/3600 = 0.00138888 litres per second

When you drive 100kph, your vehicle consumes 0.0013888888 litres per second.
When it drives 150 kph, that would be 0.00138888 * 150 / 100 = 0.0020833 litres per second.

Very easy to do, I have a similar system, but a little bit expanded, as every vehicle model has it's own fuel-tank size and consumption values, so planes consume alot more fuel compared to bikes and cars.

Be careful, as someone standing still would have a speed of 0, and he will consume 0 fuel per second.
Vehicles in real-life always consume fuel when the engine is turned on, even when standing still, so I figured vehicles should consume fuel as if they were driving 5kph when they drive slower than that.
If you detect a speed below 5kph, then use 5kph in your calculation, if you want it, or adjust the numbers to your own wishes.

You could also detect if the vehicle has a negative fuel-readout, then turn off the engine.
That's very nice when using planes and helicopters
Reply
#6

Can you give me an explose with a code pawn please?
Reply
#7

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;
Reply
#8

I remember that I made a code for someone asking the same thing, Take 2 minutes from time and try to search it on ******
Reply
#9

Hello !

I tried your code, I modified it a bit to suit my gamemode.

However, it does not work.

I'm doing everything Los Santos, none liter has withdrawn.

pawn Код:
VehicleParams[VehInfo[vid][ModelID]-400][CONSO]
= 1 for Landstalker. (Exemple)

pawn Код:
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;
}
Reply
#10

Have you started the timer?

I have this code to start it for all players:
pawn Код:
// 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);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)