Advertisement
Southclaw

Simple race data loading system

Aug 23rd, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.93 KB | None | 0 0
  1.  
  2. /*
  3.     Run it when the game starts.
  4.     This loads the basic data that is needed even when a race isn't active.
  5. */
  6. LoadRaceTrackData()
  7. {
  8.     new
  9.         File:idxFile,
  10.         line[256],
  11.         idx;
  12.  
  13.     if(fexist(RACE_INDEX_FILE))return print("ERROR! Race index file not found!");
  14.  
  15.     idxFile = fopen(RACE_INDEX_FILE, io_read);
  16.    
  17.     while(fread(idxFile, line))
  18.     {
  19.         /*
  20.             An index file line might look like this:
  21.  
  22.             In INI format so there is an = sign to take into account.
  23.             This way, you can easily write back into the file and replace
  24.             lines by using the Race Name as the 'key' in any good INI script.
  25.  
  26.             And remember, we don't have to load it using an INI script,
  27.             just tell SSCANF to look for an '=' after the first string
  28.  
  29.             A name
  30.             B weather
  31.             C time
  32.             D best player
  33.             E best time
  34.             ...
  35.            
  36.             (Look in any of the GTA data files to see where I got this idea from!)
  37.            
  38.             A                   =   B       C       D                   E           maybe some other types of data...
  39.             The Big Ear Race 1  =   7,      12,     [HLF]Southclaw,     573758,     moar numbarz!
  40.         */
  41.         if(sscanf(line, "p<=> s[32] p<,> d d s[24] d",
  42.             // A global array that holds all the race data
  43.             RaceData[idx][race_name],
  44.             RaceData[idx][race_weather],
  45.             RaceData[idx][race_time],
  46.             RaceData[idx][race_bestplayer],
  47.             RaceData[idx][race_besttime] )) printf("ERROR: Race data index %d invalid format", idx);
  48.  
  49.         else idx++; // Each race gets an ID, but the 'else' is to make sure the ID only increments on valid races.
  50.     }
  51.     return 1;
  52. }
  53.  
  54. /*
  55.     In the folder with the race index file in it, you have all the files named after the races too.
  56.     These contain the coordinates for other crap like objects, checkpoints, pickups all that stuff.
  57.     Those two lines above are exactly the same length!
  58.    
  59.     You can either use this load function at the start of each race, then unload all the stuff afterwards
  60.     OR
  61.     Load it all into memory on the above function (In the while loop) while the server is starting.
  62.    
  63.     If there's a lot of data, it would be best to load it at the start, but if there's not much then loading it
  64.     in runtime should be fine.
  65. */
  66.  
  67.  
  68. LoadRaceData(racename[])
  69. {
  70.     new
  71.         /*
  72.             32 is the max race name (You should probably define this as MAX_RACE_NAME I guess)
  73.             The extra is for the file extension and folder
  74.             In this example, I use the folder called "races" and the extension ".dat" so that's an extra 9 characters
  75.         */
  76.  
  77.         filename[32 + 9],
  78.         File:datFile,
  79.         line[256],
  80.         chckpt_idx, // I like variables being the same length if they are related in some way...
  81.         pickup_idx, // Bad habit I think, but it makes it easier to read for me.
  82.         pickup_idx; // I'm sure Y_Less or Slice would disagree and say it's stupid idea!
  83.  
  84.     format(filename, sizeof(filename), "races/%s.dat", racename);
  85.     if(!fexist(filename))return printf("ERROR: Race data file '%s' not found!", filename);
  86.     else datFile = fopen(filename, io_read);
  87.    
  88.     while(fread(datFile, line))
  89.     {
  90.         /*
  91.             I'm assuming you want Checkpoints, Pickups and Objects, here are the parameters for each:
  92.            
  93.             chckpt(x, y, z)
  94.             object(x, y, z, rx, ry, rz, model)
  95.             pickup(x, y, z, type)
  96.            
  97.             I can't think of any more params, but you get the idea (Easy to change this anyways)
  98.         */
  99.         // Checkpoints
  100.         if      (!sscanf(line, "'chckpt(' p<,> f f p<)> f", /* some array you hold checkpoint positions in*/ )) // As you can see here
  101.         {
  102.             // Create it with whatever you create checkpoints with
  103.             chckpt_idx++;
  104.         }
  105.         // Objects
  106.         else if (!sscanf(line, "'object(' p<,> fff fff p<)> d", /* some array you hold object positions in*/ )) // I am quite obsessive about
  107.         {
  108.             // Create the object and do what you want with it
  109.             object_idx++;
  110.         }
  111.         // Pickups
  112.         else if (!sscanf(line, "'pickup(' p<,> fff p<)> d", /* some array you hold object positions in*/ )) // Lines being the same length if they are similar!
  113.         {
  114.             // Create the pickup etc
  115.             pickup_idx++:
  116.         }
  117.         // Unrecognised lines (Blank lines, comments or broken data lines)
  118.         else
  119.         {
  120.             /*
  121.                 If you intend to have blank lines or comments in your data files, leave the else condition out.
  122.                 Otherwise, put an error code message on it!
  123.             */
  124.         }
  125.     }
  126.     return 1;
  127. }
  128.  
  129.  
  130. #endinput
  131.  
  132. An example of a simple race file:
  133.  
  134. index.ini:
  135.  
  136.     Chilliad Race=12, 7, Patrik356b, 5375375
  137.     SomeOtherRace=47, 3, Southclaw, 379537635
  138.  
  139. -end of file-
  140.  
  141.  
  142. Chilliad Race.dat:
  143.  
  144.     ; checkpoints (this is a comment!)
  145.     chckpt(5738.3, 3485.38, 3765.36)
  146.     chckpt(3356.3, 3664.38, 4623.36)
  147.     chckpt(3662.3, 4623.38, 4624.36)
  148.    
  149.     ; some objects
  150.     object(8527.34, 8759.43, 5787.86, 0.0, 90.0, 0.0, 1337)
  151.     object(4652.34, 2676.43, 7437.86, 0.0, 90.0, 0.0, 345)
  152.     object(4326.34, 4326.43, 4363.86, 0.0, 60.0, 90.0, 3756)
  153.     object(3462.34, 7373.43, 4326.86, 180.0, 0.0, 45.0, 2424)
  154.    
  155.     ; some pickups
  156.     pickup(8386.38, 3673.75, 3863.38, 0)
  157.     pickup(3624.38, 4674.75, 4373.38, 1)
  158.  
  159.     ; maybe the pickup types are 0 = nitro 1 = repair
  160.  
  161. -end of file-
  162.  
  163.  
  164.  
  165. Randomly generated coordinates, probably mean nothing!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement