Help with gas station script
#1

I'll have following code, that should be tuned also for that it gets that nearest gas station gas price. Gas price for each station is defined in file in this format:

-2244.1365,-2560.6294,31.6276,28,City

(X,Y,Z,PRICE,OWNER)

However I don't know how to do it, so plz help with that.

pawn Код:
#define MAX_FUELSTATIONS 50 // Max fuelstations

//----------------------------------------------------------
// Fuelstaions
//----------------------------------------------------------
enum FuelSTInfo
{
    nblank,
    ownerfuel[45],
    Float:stX,
    Float:stY,
    Float:stZ,
    fuelcost
};

new stfuelcoords[MAX_FUELSTATIONS][FuelSTInfo];

stock LoadFuelStaions()
{
    new SplitDiv[5][MAX_FUELSTATIONS];
    new filestring[256];
    new fueltext[55];
    new File: file = fopen(FUEL_FILE, io_read);
        for(new stid = 0; stid < MAX_FUELSTATIONS; stid++)
        {
            fread(file, filestring);
            split(filestring, SplitDiv, ',');
            stfuelcoords[stid][stX] = floatstr(SplitDiv[0]);
            stfuelcoords[stid][stY] = floatstr(SplitDiv[1]);
            stfuelcoords[stid][stZ] = floatstr(SplitDiv[2]);
            stfuelcoords[stid][fuelcost] = strval(SplitDiv[3]);
            strmid(stfuelcoords[stid][ownerfuel], SplitDiv[4], 0, strlen(SplitDiv[4]), 255);
            format(fueltext, sizeof(fueltext), "PRICE: %iЂ/L || OWNER: %s", stfuelcoords[stid][fuelcost], stfuelcoords[stid][ownerfuel]);
            CreateDynamicMapIcon(stfuelcoords[stid][stX], stfuelcoords[stid][stY], stfuelcoords[stid][stZ], 51, 0, -1, -1, -1, 100.0);
            CreateDynamic3DTextLabel(fueltext, ADS_COLOR, stfuelcoords[stid][stX], stfuelcoords[stid][stY], stfuelcoords[stid][stZ], DRAW_DISRANCE_3DTEXT, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, TEST_LOS, -1, -1, -1, 100.0);
        }
        fclose(file);
}

stock CloseToGas(playerid)
{
    new Float:maxdis = 15.0;
    new Float:ppos[3];
    GetPlayerPos(playerid, ppos[0], ppos[1], ppos[2]);
    for(new i = 0;i<MAX_FUELSTATIONS;i++)
    {
        if (ppos[0] >= floatsub(stfuelcoords[i][stX], maxdis) && ppos[0] <= floatadd(stfuelcoords[i][stX], maxdis)
        && ppos[1] >= floatsub(stfuelcoords[i][stY], maxdis) && ppos[1] <= floatadd(stfuelcoords[i][stY], maxdis)
        && ppos[2] >= floatsub(stfuelcoords[i][stZ], maxdis) && ppos[2] <= floatadd(stfuelcoords[i][stZ], maxdis))
        {
            return i;
        }
    }
    maxdis = 20.0;
    for(new i = 16;i<MAX_FUELSTATIONS;i++)
    {
        if (ppos[0] >= floatsub(stfuelcoords[i][stX], maxdis) && ppos[0] <= floatadd(stfuelcoords[i][stX], maxdis)
        && ppos[1] >= floatsub(stfuelcoords[i][stY], maxdis) && ppos[1] <= floatadd(stfuelcoords[i][stY], maxdis)
        && ppos[2] >= floatsub(stfuelcoords[i][stZ], maxdis) && ppos[2] <= floatadd(stfuelcoords[i][stZ], maxdis))
        {
            return i;
        }
    }
    return 999;
}
Reply
#2

Add codes?

Try to maybe duplicate some codes and tweak, if you get any errors let us know since we maybe cannot do it, its sort of asking for a filterscript after all you want the loading format differently rewroten from you have, or did i misunderstood something?
Reply
#3

If the CloseToGas function works, you can use it for getting nearest gas station's id. Let it came out to be "gasid" Then
pawn Код:
new Rate, String[256];
Rate = stfuelcoords[gasid][fuelcost];
Now you can set a checkpoint for the player to the gas station
pawn Код:
SetPlayerCheckpoint(playerid, stfuelcoords[gasid][stX], stfuelcoords[gasid][stY], stfuelcoords[gasid][stZ], 5.0);
Reply
#4

Yeah, CloseToGas gets called from

pawn Код:
stock refuel(playerid)
{
    if(CloseToGas(playerid) != 999 && GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
            gasprice[playerid] = gascost; // gascost amount should be filled in CloseToGas function.
            if(GetPlayerMoney(playerid) >= gasprice[playerid])
            {
             SendClientMessage(playerid,COLOR_YELLOW,"Your vehicle is being tanking up...");
             Filling[playerid] = 1;
             gassing = SetTimer("TankUp", cSetting[tankingspeed], 1);
            }
            if(GetPlayerMoney(playerid) <=  gasprice[playerid])
            {
             SendClientMessage(playerid,COLOR_BRIGHTRED,"You dont have enought money!");
             Filling[playerid] = 0;
            }

    }
    return 1;
}
and refuel gets called from Classic menu.

So you mean most easiest way is to change format to like?
ID,X,Y,Z,PRICE,OWNER

Yeah, and now I am thinkin' how I am gonna get that ID in CloseToGas or what?
Reply
#5

New function to get closest gas station ID. (will only work after you load gas station info from the file)
pawn Код:
stock ClosestGasSt(playerid)
{
    new gasid, float:distance=99999.0, float:x, float:y, float:z;
    GetPlayerPos(playerid, x, y, z)
    for(new i=0; i<MAX_FUELSTATIONS; i++)
    {
        new float:tempdist = GetDistanceBetween(x, y, z, stfuelcoords[i][stX], stfuelcoords[i][stY], stfuelcoords[i][stZ]);
        if(tempdist < distance)
        {
        distance = tempdist;
        gasid = i;
    }
    return gasid;
}
A function to get distance between two points:
pawn Код:
stock GetDistanceBetween(x1, y1, z1, x2, y2, z2)
{
    new float:dist;
    dist = floatsqroot(floatpower(floatsub(x2, x1), 2) + floatpower(floatsub( y2, y1), 2) + floatpower(floatsub( z2, z1), 2));
    return dist;
}
Reply
#6

Ok I got still problem with this.
First GivePlayerMoney(i, -gasprice[i]); doesn't work at all. It doesn't minus money.
And message "and it cost for you %iЂ" returns always 0. "and it cost for you 0Ђ".

Now codes look like this:

pawn Код:
#define MAX_FUELSTATIONS 27 // Max fuelstations

enum FuelSTInfo
{
    fuelstid,
    ownerfuel[125],
    Float:stX,
    Float:stY,
    Float:stZ,
    fuelcost,
    krtrue,
    sttank
};

new stfuelcoords[MAX_FUELSTATIONS][FuelSTInfo];

stock LoadFuelStaions()
{
    new SplitDiv[8][MAX_FUELSTATIONS];
    new filestring[FILESTRING_SIZE];
    new fueltext[55];
    format(pfile, sizeof(pfile), "%s/%s/%s%s", CORE_FOLDER, SETTING_FOLDER, SUB_FILE_fuelstations, CORE_FOLDERS_FILESYSTEM);
    new File: file = fopen(pfile, io_read);
        for(new stid = 0; stid < MAX_FUELSTATIONS; stid++)
        {
            fread(file, filestring);
            split(filestring, SplitDiv, ',');
            stfuelcoords[stid][fuelstid] = strval(SplitDiv[0]);
            stfuelcoords[stid][stX] = floatstr(SplitDiv[1]);
            stfuelcoords[stid][stY] = floatstr(SplitDiv[2]);
            stfuelcoords[stid][stZ] = floatstr(SplitDiv[3]);
            stfuelcoords[stid][fuelcost] = strval(SplitDiv[4]);
            strmid(stfuelcoords[stid][ownerfuel], SplitDiv[5], 0, strlen(SplitDiv[5]), 255);
            stfuelcoords[stid][krtrue] = strval(SplitDiv[6]);
            stfuelcoords[stid][sttank] = strval(SplitDiv[7]);
            format(fueltext, sizeof(fueltext), "COST: %iЂ/Liter || Owner: %s", stfuelcoords[stid][fuelcost], stfuelcoords[stid][ownerfuel]);
            CreateDynamicMapIcon(stfuelcoords[stid][stX], stfuelcoords[stid][stY], stfuelcoords[stid][stZ], 51, 0, -1, -1, -1, 100.0);
            stfuelcoords[stid][fuelstid] = CreateDynamic3DTextLabel(fueltext, FUELST_COLOR, stfuelcoords[stid][stX], stfuelcoords[stid][stY], stfuelcoords[stid][stZ], DRAW_DISRANCE_3DTEXT, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, TEST_LOS, -1, -1, -1, 100.0);
        }
        fclose(file);
}

stock refuel(playerid)
{
    if(CloseToGas(playerid) != 999 && GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
            stfuelcoords[ClosestGasSt(playerid)][fuelcost] = gasprice[playerid];
            if(GetPlayerMoney(playerid) >= gasprice[playerid])
            {
             SendClientMessage(playerid,COLOR_YELLOW,"...");
             Filling[playerid] = 1;
             tankkaus = SetTimer("Tankkaa", cSetting[tankkausnopeus], 1);
            }
            if(GetPlayerMoney(playerid) <= gasprice[playerid])
            {
             SendClientMessage(playerid,COLOR_BRIGHTRED,"....");
             Filling[playerid] = 0;
            }

    }
    return 1;
}

stock GetDistanceBetween(x1, y1, z1, x2, y2, z2)
{
    new float:dist;
    dist = floatsqroot(floatpower(floatsub(x2, x1), 2) + floatpower(floatsub( y2, y1), 2) + floatpower(floatsub( z2, z1), 2));
    return dist;
}


stock ClosestGasSt(playerid)
{
    new gasid, float:distance=99999.0, float:x, float:y, float:z;
    GetPlayerPos(playerid, x, y, z);
    for(new i=0; i<MAX_FUELSTATIONS; i++)
    {
        new float:tempdist = GetDistanceBetween(x, y, z, stfuelcoords[i][stX], stfuelcoords[i][stY], stfuelcoords[i][stZ]);
        if(tempdist < distance)
        {
        distance = tempdist;
        gasid = i;
        }
    }
    return gasid;
}

public Tankkaa()
{
  foreach(Player, i)
   {
   if(IsPlayerConnected(i) && IsPlayerInAnyVehicle(i))
       {
    if(Filling[i] == 1)
        {
        if(Gas[GetPlayerVehicleID(i)] < 100)
        {
        Gas[GetPlayerVehicleID(i)] = (Gas[GetPlayerVehicleID(i)]+1);
        GivePlayerMoney(i, -gasprice[i]);
        gascosted[i] = (gascosted[i] + gasprice[i]);
        }
        if(Gas[GetPlayerVehicleID(i)] > 99 && Filling[i] == 1)
        {
        format(carmess, sizeof(carmess), "%ssi is now full... and it cost for you %iЂ, ...!", VehicleInfo[GetPlayerVehicleID(i)][name2], gascosted[i]);
        SendClientMessage(i, COLOR_GREEN, carmess);
        Gas[GetPlayerVehicleID(i)] = MAX_GAS;
        gasprice[i] = 0;
        gascosted[i] = 0;
        KillTimer(tankkaus);
        Filling[i] = 0;
        }
        if(CloseToGas(i) == 999 && Filling[i] == 1)
        {
        format(carmess, sizeof(carmess), "it cost for you %iЂ.", gascosted[i]);
        SendClientMessage(i, COLOR_BRIGHTRED, ".....");
        SendClientMessage(i, COLOR_BRIGHTRED, carmess);
        gasprice[i] = 0;
        gascosted[i] = 0;
        KillTimer(tankkaus);
        Filling[i] = 0;
        }
        }
        }
       }
  return 1;
}
stfuelcoords[ClosestGasSt(playerid)][fuelcost] should return gas station id like:

stfuelcoords[5][fuelcost]

Fuel station file format is like this:
5,-94.765098,-1174.807861,1.997900,28,City,0,100

GASSTAIONID,X,Y,Z,GASCOST,OWNER,...,STATIONTANKSIZ E

And yes, I know that LoadFuelStaion is working there is no problems there. And it gets called at ongamemodeinit.

Refuel gets called from vehicle menu. (In-car)
Reply
#7

bump
Reply
#8

Quote:
Originally Posted by HosCun
Посмотреть сообщение
pls send me gm i fix it
LOOL!!!

On Topic

make a debug when you load from file maybe its bugging there and check maybe your gas price in file is 0
also you can make a command for a debug that will change gas prices for stations
Reply
#9

Quote:
Originally Posted by [DM]Kane
Посмотреть сообщение
Why are you using gasprice[i] instead of stfuelcoords[gasid][fuelcost]?
Yeah! Now it works perfectly thanks!

Quote:
Originally Posted by HosCun
Посмотреть сообщение
pls send me gm i fix it
Ahahahahaaa
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)