SA-MP Forums Archive
Formula Issue - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Formula Issue (/showthread.php?tid=375949)



Formula Issue - nmader - 08.09.2012

Basically I was working on a /fuel script and none of the mathematics seem to be working properly:

pawn Код:
command(fuel, playerid, vehicleid, params[])
{
    new fuelamount;
    new neededfuel = MAX_FUEL - Vehicles[vehicleid][Fuel];
    if(sscanf(params, "d", fuelamount))
    {
        SendClientMessage(playerid, ERROR, "SYNTAX: /fuel [amount]");
    }
    else
    {
        if(IsPlayerInAnyVehicle(playerid))
        {
            new calculation = REFUEL_COST / fuelamount;
            if(fuelamount > neededfuel)
            {
                SendClientMessage(playerid, ERROR, "Your vehicle cannot hold that much fuel!");
            }
            else
            {
                if(Player[playerid][Money] < calculation)
                {
                    SendClientMessage(playerid, ERROR, "You do not have enough money to refuel your vehicle.");
                }
                else
                {
                    new calculation2 = Player[playerid][Money] -= calculation;
                    new timertime = fuelamount * 1000;
                    SetPlayerMoney(playerid, calculation2);
                    TogglePlayerControllable(playerid, false);
                    SetTimerEx("Fueling", timertime, false, "i", playerid);
                    new ifuel = Vehicles[vehicleid][Fuel] + fuelamount;
                    Vehicles[vehicleid][Fuel] = ifuel;
                }
            }
        }
    }
    return 1;
}
I am completely clueless on what I am doing wrong. ??


AW: Formula Issue - Nero_3D - 08.09.2012

I expect that REFUEL_COST is for 100%, isnt it ?

Than its an easy cross-multiplication
Код:
100% fuel = REFULE_COST
fuelamount = calculation
pawn Код:
new calculation = (fuelamount * REFULE_COST) / 100;
Also I noticed that you create variables before you need it
pawn Код:
//
    new neededfuel = /* */; // not needed here
    if() { } else {
        if() {
            new calculation = /* */; // not needed here
            // neededfuel first time used
            if() { } else {
                // calculation first time used
                if() { } else {
                }
            }
        }
    }



Re: Formula Issue - nmader - 08.09.2012

I am still getting warnings from those lines stating "Tag Mismatch" which is why the script hadn't been working before.


AW: Formula Issue - Nero_3D - 08.09.2012

show the warnings and mark the line with it, also nearly most warnings doesnt affect the result


Re: Formula Issue - nmader - 08.09.2012

pawn Код:
C:\Documents and Settings\Nmader\Desktop\Lost Life Roleplay\gamemodes\Lost.pwn(19021) : warning 213: tag mismatch
C:\Documents and Settings\Nmader\Desktop\Lost Life Roleplay\gamemodes\Lost.pwn(19040) : warning 213: tag mismatch
pawn Код:
command(fuel, playerid, vehicleid, params[])
{
    new fuelamount;
    if(sscanf(params, "d", fuelamount))
    {
        SendClientMessage(playerid, ERROR, "SYNTAX: /fuel [amount]");
    }
    else
    {
        if(IsPlayerInAnyVehicle(playerid))
        {
            new neededfuel = (MAX_FUEL - Vehicles[vehicleid][Fuel]); //line 19021
            if(fuelamount > neededfuel)
            {
                SendClientMessage(playerid, ERROR, "Your vehicle cannot hold that much fuel!");
            }
            else
            {
                new calculation = (fuelamount * REFUEL_COST) / 100;
                if(Player[playerid][Money] < calculation)
                {
                    SendClientMessage(playerid, ERROR, "You do not have enough money to refuel your vehicle.");
                }
                else
                {
                    new calculation2 = (Player[playerid][Money] - calculation);
                    new timertime = (fuelamount * 1000);
                    SetPlayerMoney(playerid, calculation2);
                    TogglePlayerControllable(playerid, false);
                    SetTimerEx("Fueling", timertime, false, "i", playerid);
                    new ifuel = (Vehicles[vehicleid][Fuel] + fuelamount); //line 19040
                    Vehicles[vehicleid][Fuel] = ifuel;
                }
            }
        }
    }
    return 1;
}



AW: Formula Issue - Nero_3D - 08.09.2012

all variables need to be int, most probably Vehicle[vehicleid][Fuel] is a float and MAX_FUEL could also be a float


Re: Formula Issue - SuperViper - 08.09.2012

Replace

pawn Код:
new ifuel = (Vehicles[vehicleid][Fuel] + fuelamount); //line 19040
Vehicles[vehicleid][Fuel] = ifuel;
with

pawn Код:
Vehicles[vehicleid][Fuel] = floatround(Vehicles[vehicleid][Fuel] + fuelamount);