Fuel System
#1

Hey guys, working on a fuel system however I have a minor bug. When refueling regardless of how much fuel the vehicle has you will always pay $100, since Fuel cost is 1 and fuel max is 100, it's not getting the variable of fuel before filling up, just curious how would I get the fuel variable and then start fueling from there, with the variable number of fuel being anything from 1 to 100. I might be way over thinking this but I just wanted to ask. Here is my fueling callback.

PHP код:
public MainTimer()
{
    new 
string[128], Float:xFloat:yFloat:z;
    for(new 
i=0MAX_PLAYERSi++)
    {
        if(
IsPlayerConnected(i))
        {
            if(
GetPlayerState(i) == PLAYER_STATE_DRIVER)
            {
                new 
vehicleid GetPlayerVehicleID(i);
                if(!
IsBicycle(vehicleid) && CFuel[vehicleid] > 0)
                {
                    
CFuel[vehicleid] -= GetPlayerSpeed(i)/1000.0;
                    if(
CFuel[vehicleid] <= 0)
                    {
                        
ToggleEngine(vehicleidVEHICLE_PARAMS_OFF);
                        
GameTextForPlayer(i"~r~out of fuel"30003);
                        
SendClientMessage(iCOLOR_LIGHTRED"This vehicle is out of fuel!");
                    }
                }
            }
            if(
RefuelTime[i] > && GetPVarInt(i"FuelStation"))
            {
                new 
vehicleid GetPlayerVehicleID(i);
                
CFuel[vehicleid] += 2.0;
                
RefuelTime[i]--;
                if(
RefuelTime[i] == 0)
                {
                    if(
CFuel[vehicleid] == FuelMax) return SendClientMessage(iCOLOR_RED"Error: Your vehicle cannot hold anymore fuel.");
                    if(
CFuel[vehicleid] >= 100.0CFuel[vehicleid] = 100.0;
                    new 
stationid GetPVarInt(i"FuelStation");
                    new 
cost floatround(CFuel[vehicleid]-GetPVarFloat(i"Fuel"))*FUEL_PRICE;
                    if(
GetPlayerState(i) != PLAYER_STATE_DRIVER || CFuel[vehicleid] >= 100.0 || GetPlayerMoney(i) < cost || !IsPlayerInRangeOfPoint(i10.0FuelStationPos[stationid][0], FuelStationPos[stationid][1], FuelStationPos[stationid][2]))
                    {
                           if(
PlayerInfo[i][pAdmin] > 2)
                        {
                            new 
msg[24];
                            
format(msgsizeof(msg), "STATION ID %d"stationid);
                            
SendClientMessage(iCOLOR_WHITEmsg);
                        }
                        if(
GetPlayerMoney(i) < costcost GetPlayerMoney(i);
                        
GivePlayerCash(i, -cost);
                        
format(stringsizeof(string), "~r~-$%d"cost);
                        
GameTextForPlayer(istring20003);
                        
format(stringsizeof(string), "You have paid $%d for the fuel."cost);
                        
SendClientMessage(iCOLOR_YELLOWstring);
                        
SetPVarInt(i"FuelStation"0);
                        
SetPVarFloat(i"Fuel"0.0);
                    }
                    else
                    {
                        
RefuelTime[i] = 5;
                        
format(stringsizeof(string), "~w~refueling...~n~~r~-$%d"cost);
                        
GameTextForPlayer(istring20003);
                    }
                }
            }
            if(
TrackCar[i])
            {
                
GetVehiclePos(TrackCar[i], xyz);
                
SetPlayerCheckpoint(ixyz3);
            }
        }
    }
    return 
1;

Reply
#2

First, use this
Quote:

for(new i = 0, highestPlayerid = GetPlayerPoolSize(); i <= highestPlayerid; i++)

instead of this
Код:
for(new i=0; i < MAX_PLAYERS; i++)
Say you have 6 players (ID 0, 1, 2, 8, 9, 10) online (ID 3, 4, 5, 6, 7 are not taken), the first block of code will only run 11 times. The second block of code will always run 1000 times (if MAX_PLAYERS is not changed from the default 1000). So the first code is more efficient. Mind that the first block uses <= instead of <.

To answer your question:
If I understand your code right, the "Fuel" PVar is the amount of fuel you have added.
If that is indeed the case, the cause of the issue is that you forgot to increase the fuel PVar value anywhere.

EDIT:
If that is indeed the problem, this
Код:
new cost = floatround(CFuel[vehicleid]-GetPVarFloat(i, "Fuel"))*FUEL_PRICE;
should be changed to this
Код:
new cost = floatround(100.0-GetPVarFloat(i, "Fuel"))*FUEL_PRICE;
Reply
#3

Thanks for the information, but that still doesn't fix the problem. Do you have any other ideas?

EDIT:

I managed to fix it, it wasn't anything to do with the maintimer, I defined "Fuel" as "CFuel" everywhere else in my script. Thanks for the offer though.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)