gl_common.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //----------------------------------------------------------
  2. //
  3. // GRAND LARCENY common functions include.
  4. //
  5. //----------------------------------------------------------
  6. stock LoadStaticVehiclesFromFile(const filename[])
  7. {
  8. new File:file_ptr;
  9. new line[256];
  10. new var_from_line[64];
  11. new vehicletype;
  12. new Float:SpawnX;
  13. new Float:SpawnY;
  14. new Float:SpawnZ;
  15. new Float:SpawnRot;
  16. new Color1, Color2;
  17. new index;
  18. new vehicles_loaded;
  19. file_ptr = fopen(filename,filemode:io_read);
  20. if(!file_ptr) return 0;
  21. vehicles_loaded = 0;
  22. while(fread(file_ptr,line,256) > 0)
  23. {
  24. index = 0;
  25. // Read type
  26. index = token_by_delim(line,var_from_line,',',index);
  27. if(index == (-1)) continue;
  28. vehicletype = strval(var_from_line);
  29. if(vehicletype < 400 || vehicletype > 611) continue;
  30. // Read X, Y, Z, Rotation
  31. index = token_by_delim(line,var_from_line,',',index+1);
  32. if(index == (-1)) continue;
  33. SpawnX = floatstr(var_from_line);
  34. index = token_by_delim(line,var_from_line,',',index+1);
  35. if(index == (-1)) continue;
  36. SpawnY = floatstr(var_from_line);
  37. index = token_by_delim(line,var_from_line,',',index+1);
  38. if(index == (-1)) continue;
  39. SpawnZ = floatstr(var_from_line);
  40. index = token_by_delim(line,var_from_line,',',index+1);
  41. if(index == (-1)) continue;
  42. SpawnRot = floatstr(var_from_line);
  43. // Read Color1, Color2
  44. index = token_by_delim(line,var_from_line,',',index+1);
  45. if(index == (-1)) continue;
  46. Color1 = strval(var_from_line);
  47. index = token_by_delim(line,var_from_line,';',index+1);
  48. Color2 = strval(var_from_line);
  49. //printf("%d,%d,%f,%f,%f,%f,%d,%d",total_vehicles_from_files+vehicles_loaded+1,vehicletype,SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2);
  50. AddStaticVehicleEx(vehicletype,SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2,(3*60)); // respawn 3 minutes
  51. vehicles_loaded++;
  52. }
  53. fclose(file_ptr);
  54. printf("Loaded %d vehicles from: %s",vehicles_loaded,filename);
  55. return vehicles_loaded;
  56. }
  57. //----------------------------------------------------------
  58. stock strtok(const string[], &index)
  59. {
  60. new length = strlen(string);
  61. while ((index < length) && (string[index] <= ' '))
  62. {
  63. index++;
  64. }
  65. new offset = index;
  66. new result[20];
  67. while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  68. {
  69. result[index - offset] = string[index];
  70. index++;
  71. }
  72. result[index - offset] = EOS;
  73. return result;
  74. }
  75. //------------------------------------------------
  76. stock strrest(const string[], &index)
  77. {
  78. new length = strlen(string);
  79. while ((index < length) && (string[index] <= ' '))
  80. {
  81. index++;
  82. }
  83. new offset = index;
  84. new result[128];
  85. while ((index < length) && ((index - offset) < (sizeof(result) - 1)))
  86. {
  87. result[index - offset] = string[index];
  88. index++;
  89. }
  90. result[index - offset] = EOS;
  91. return result;
  92. }
  93. //----------------------------------------------------------
  94. // Tokenise by a delimiter
  95. // Return string and index of the end determined by the
  96. // provided delimiter in delim
  97. stock token_by_delim(const string[], return_str[], delim, start_index)
  98. {
  99. new x=0;
  100. while(string[start_index] != EOS && string[start_index] != delim) {
  101. return_str[x] = string[start_index];
  102. x++;
  103. start_index++;
  104. }
  105. return_str[x] = EOS;
  106. if(string[start_index] == EOS) start_index = (-1);
  107. return start_index;
  108. }
  109. //----------------------------------------------------------
  110. stock isNumeric(const string[])
  111. {
  112. new length=strlen(string);
  113. if (length==0) return false;
  114. for (new i = 0; i < length; i++)
  115. {
  116. if (
  117. (string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
  118. || (string[i]=='-' && i!=0) // A '-' but not at first.
  119. || (string[i]=='+' && i!=0) // A '+' but not at first.
  120. ) return false;
  121. }
  122. if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
  123. return true;
  124. }
  125. //----------------------------------------------------------
  126. stock IsKeyJustDown(key, newkeys, oldkeys)
  127. {
  128. if((newkeys & key) && !(oldkeys & key)) return 1;
  129. return 0;
  130. }
  131. //----------------------------------------------------------