Adding vehicles from file
#1

Hello How could I add all vehicles from a file and how could I save them in a file, lets say im in a car and if I type

/savethis

it will save car chords to file

and then in my gamemode (OnGameModeInit) it will check in that file and add all vehicles that exist in there to map?
Reply
#2

This was taken completely from iNorton's Base Roleplay Script. I did not write this.



Command
pawn Код:
CMD:addveh(playerid, params[])
{
#pragma unused params
    if(PlayerInfo[playerid][Adminlevel] < 5) return SendClientMessage(playerid,COLOR_ERROR,"[ERROR]: You are not authorized to use this command ");
    if(IsPlayerInAnyVehicle(playerid))
    {
        new
            filestring[264],
            Float:X,
            Float:Y,
            Float:Z,
            Float:Rot;
            GetPlayerPos(playerid,X,Y,Z);
            GetVehicleZAngle(GetPlayerVehicleID(playerid), Rot);
            new
                vid = GetVehicleModel(GetPlayerVehicleID(playerid));
            new
                File:pos=fopen("vehicles.ini", io_append);
            format(filestring, 256, "\n%d,%f,%f,%f,%f,-1,-1 ;", vid, X, Y, Z, Rot);
            fwrite(pos, filestring);
            fclose(pos);
            SendClientMessage(playerid,GREEN,"[SUCCESS]: You have successfully added a vehicle!");
    }
    else
    {
        SendClientMessage(playerid,COLOR_ERROR,"[ERROR]: You are not in a vehicle!.");
    }
    return 1;
}
Loading vehicles
pawn Код:
stock LoadStaticVehiclesFromFile(const filename[])
{
    new File:file_ptr;
        new line[256];
        new var_from_line[64];
        new vehicletype;
        new Float:SpawnX;
        new Float:SpawnY;
        new Float:SpawnZ;
        new Float:SpawnRot;
        new Color1, Color2;
        new index;
        new vehicles_loaded;

        file_ptr = fopen(filename,filemode:io_read);
        if(!file_ptr) return 0;

        vehicles_loaded = 0;

    while(fread(file_ptr,line,256) > 0)
    {
        index = 0;

// Read type
            index = token_by_delim(line,var_from_line,',',index);
            if(index == (-1)) continue;
            vehicletype = strval(var_from_line);
            if(vehicletype < 400 || vehicletype > 611) continue;

// Read X, Y, Z, Rotation
            index = token_by_delim(line,var_from_line,',',index+1);
            if(index == (-1)) continue;
            SpawnX = floatstr(var_from_line);

            index = token_by_delim(line,var_from_line,',',index+1);
            if(index == (-1)) continue;
            SpawnY = floatstr(var_from_line);

            index = token_by_delim(line,var_from_line,',',index+1);
            if(index == (-1)) continue;
            SpawnZ = floatstr(var_from_line);

            index = token_by_delim(line,var_from_line,',',index+1);
            if(index == (-1)) continue;
            SpawnRot = floatstr(var_from_line);

// Read Color1, Color2
            index = token_by_delim(line,var_from_line,',',index+1);
            if(index == (-1)) continue;
            Color1 = strval(var_from_line);

            index = token_by_delim(line,var_from_line,';',index+1);
            Color2 = strval(var_from_line);

//printf("%d,%d,%f,%f,%f,%f,%d,%d",total_vehicles_from_files+vehicles_loaded+1,vehicletype,SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2);

                                                  // respawn 30 minutes
            AddStaticVehicleEx(vehicletype,SpawnX,SpawnY,SpawnZ+1,SpawnRot,Color1,Color2,(30*60));
            vehicles_loaded++;
    }
    fclose(file_ptr);
    printf("Loaded %d vehicles from: %s",vehicles_loaded,filename);
    return vehicles_loaded;
}
OnGameModeInit()
pawn Код:
public OnGameModeInit()
{
    LoadStaticVehiclesFromFile("vehicles.ini");
    return 1;
}
Goodluck.

EDIT:


Almost forgot, you need the sscanf and ZCMD includes installed in order for this to even begin to work.
Reply
#3

I have sscanf, and ZCMD I dont need, I can just redo it to use CommandText

Thx man.

Rep++
Reply
#4

Ok I almost finished, but I have one more question. This is how vehicles save
pawn Код:
1,490,1193.171142,-1327.144287,13.501708,353.794891,-1,-1 ;
First number is car ID, is there a chance to respawn all of those cars with ID 1 for example?
SO remove them and add them again?
Reply
#5

You can make a command to loop through all the vehicles and respawn them.
pawn Код:
if(!strcmp(cmdtext,"/respawncars",true))
{
    for(new i=0; i<MAX_VEHICLES; i++)
    {
        SetVehicleToRespawn(i);
    }
    SendClientMessage(playerid, -1, "SERVER: Cars Respawned");
    return 1;
}
Reply
#6

But I only want to respawn vehicles that are in that file vehicles.ini, for every vehicle to respawn.

or maybe any chance by changing the stock line to:
pawn Код:
new TestCars = AddStaticVehicle(vehicletype,SpawnX,SpawnY,SpawnZ+1,SpawnRot,Color1,Color2);
and then when you type a command it deletes all of them and does this after: LoadStaticVehiclesFromFile("vehicles.ini");?

How can I do it?
Reply
#7

Why would you only want to respawn the cars from the .ini file? I'm not sure how that works though.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)