[HELP] fuel system
#2

I'll give you an example of a basic fuel system

first we create 2 variables, one that stores the current fuel of a vehicle and one that stores the maximum fuel of a vehicle
pawn Код:
enum VData
{
    mFuel,
    cFuel
}
new VehicleData[MAX_VEHICLES][VData];
whereas mFuel is the max fuel and cFuel the current fuel.
Now we set a value for these variables when creating a vehicle.
When doing it at OnGameModeInit it could look like this
pawn Код:
public OnGameModeInit() //it doesn't need to be at OnGameModeInit, depends where you create the vehicle
{
    new vehicleid = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    return 1;
}
Optional you might want to refuel it automatically when it respawns
pawn Код:
public OnVehicleSpawn(vehicleid)
{
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel];
    return 1;
}
now there are different ways to change the fuel amount... What you need for each is a timer...
this will be started at "OnGameModeInit", too
pawn Код:
SetTimer("FuelChange", 1000, true);
all in all OnGameModeInit could look like this
pawn Код:
public OnGameModeInit() //it doesn't need to be at OnGameModeInit, depends where you create the vehicle
{
    SetTimer("FuelChange", 1000, true);
   
    new vehicleid = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    new vehicleid2 = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid2][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid2][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    return 1;
}
now the actual updating of the fuel...

you can run through all players or all vehicles inside of the FuelChange callback...
Looping through vehicles would be more realistic, as you would really get EACH vehicle, not only the USED vehicles. To keep it more simple, I'll now use version to loop through all players, as this does not require a working engine on/off system
pawn Код:
forward FuelChange();
public FuelChange()
{
    for(new playerid=0; playerid<GetMaxPlayers(); i++)
    {
        if(IsPlayerConnected(playerid))
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                new vehicleid = GetPlayerVehicleID(playerid);
                if(GetPlayerVehicleSeat(playerid) == 0) //wouldn't make much sense if he was passenger
                {
                    VehicleData[vehicleid][cFuel]--; //reduce the fuel amount by 1 (EACH SECOND)
                }
            }
        }
    }
    return 1;
}
This version will reduce the vehicle fuel each second by 1. It would be more realistic to make it use different values for different speeds....
this could look similar to this
remember, this is just another version
pawn Код:
enum VData
{
    mFuel,
    cFuel,
    cuFuel
}
new VehicleData[MAX_VEHICLES][VData];

public OnGameModeInit() //it doesn't need to be at OnGameModeInit, depends where you create the vehicle
{
    SetTimer("FuelChange", 500, true);
   
    new vehicleid = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    VehicleData[vehicleid][cuFuel] = 0;
    return 1;
}

public OnVehicleSpawn(vehicleid)
{
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel];
    VehicleData[vehicleid][cuFuel] = 0;
    return 1;
}

forward FuelChange();
public FuelChange()
{
    for(new playerid=0; playerid<GetMaxPlayers(); i++)
    {
        if(IsPlayerConnected(playerid))
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                new vehicleid = GetPlayerVehicleID(playerid);
                if(GetPlayerVehicleSeat(playerid) == 0) //wouldn't make much sense if he was passenger
                {
                    new speed = GetVehicleSpeed(vehicleid);
                    if(speed < 10)
                        VehicleData[vehicleid][cuFuel] += 1;
                    else if(speed < 30)
                        VehicleData[vehicleid][cuFuel] += 2;
                    else if(speed < 70)
                        VehicleData[vehicleid][cuFuel] += 3;
                    else if(speed < 130)
                        VehicleData[vehicleid][cuFuel] += 4;
                    else if(speed < 180)
                        VehicleData[vehicleid][cuFuel] += 5;
                    else if(speed < 220)
                        VehicleData[vehicleid][cuFuel] += 6;
                    else if(speed >= 220)
                        VehicleData[vehicleid][cuFuel] += 7;
                    if(VehicleData[vehicleid][cuFuel] >= 120)
                    {
                        VehicleData[vehicleid][cuFuel] = 0;
                        VehicleData[vehicleid][cFuel]--;
                    }
                }
            }
        }
    }
    return 1;
}

stock GetVehicleSpeed(vehicleid)
{
    new Float:Pos[3], rst;
    GetVehiclePos(vehicleid, Pos[0], Pos[1], Pos[2]);
    rst = floatround((250 * ((Pos[0] * Pos[0]) + (Pos[1] * Pos[1]) + (Pos[2] * Pos[2]))), floatround_round);
    return rst;
}
there are quite a lot ways to realise this.
Remember that this is just a basic layout that you could and should expand more and make it fit to your imaginations..
just that you get an idea how you could start it
(I did not compile this but just wrote it up in the browser, therefor I can't promise there won't be any errors due to typos)
Reply


Messages In This Thread
[HELP] fuel system - by Luca12 - 02.10.2014, 20:22
Re: [HELP] fuel system - by Sascha - 02.10.2014, 20:55
Re: [HELP] fuel system - by Luca12 - 02.10.2014, 21:06
Re: [HELP] fuel system - by Sascha - 02.10.2014, 21:11
Re: [HELP] fuel system - by Luca12 - 02.10.2014, 21:20
Re: [HELP] fuel system - by Sascha - 02.10.2014, 21:24
Re: [HELP] fuel system - by Luca12 - 02.10.2014, 21:27
Re: [HELP] fuel system - by Sascha - 02.10.2014, 21:32
Re: [HELP] fuel system - by Luca12 - 02.10.2014, 21:46
Re: [HELP] fuel system - by JJack - 03.10.2014, 03:02

Forum Jump:


Users browsing this thread: 1 Guest(s)