Load from FIle
#4

pawn Код:
new total_vehicles_from_files=0;

total_vehicles_from_files += LoadStaticVehiclesFromFile("vehiclelists/bone.txt");
total_vehicles_from_files += LoadStaticVehiclesFromFile("vehiclelists/flint.txt");
total_vehicles_from_files += LoadStaticVehiclesFromFile("vehiclelists/tierra.txt");

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|%f|%f|%f|%f|%d|%d",vehicletype,
      //SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2);
     
   AddStaticVehicleEx(vehicletype,SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2,-1);
   
   vehicles_loaded++;
 }
 
 fclose(file_ptr);
 printf("Loaded %d vehicles from: %s",vehicles_loaded,filename);
 return vehicles_loaded;
}

// Tokenise by a delimiter
// Return string and index of the end determined by the
// provided delimiter in delim
token_by_delim(const string[], return_str[], delim, start_index)
{
 new x=0;
 while(string[start_index] != EOS && string[start_index] != delim) {
   return_str

  * = string[start_index];

   x++;
   start_index++;
 }
 return_str

  * = EOS;

 if(string[start_index] == EOS) start_index = (-1);
 return start_index;
}
the best place to put it is in a .inc file that you will include in you gamemode
you can use like loadvehicle.inc
then on your gamemode you will add at the start
pawn Код:
#include <loadvehicle>
Reply


Messages In This Thread
Load from FIle - by silentmarine - 11.05.2009, 03:58
Re: Load from FIle - by yom - 11.05.2009, 04:46
Re: Load from FIle - by silentmarine - 11.05.2009, 23:28
Re: Load from FIle - by matrix_smq - 12.05.2009, 08:00
Re: Load from FIle - by silentmarine - 13.05.2009, 01:15
Re: Load from FIle - by silentmarine - 13.05.2009, 01:24
Re: Load from FIle - by Andom - 13.05.2009, 06:33

Forum Jump:


Users browsing this thread: 1 Guest(s)