SAMP Gitlab CI Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
5.9 KiB

5 years ago
  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,%.2f,%.2f,%.2f,%.4f,%d,%d",vehicletype,SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2);
  50. AddStaticVehicleEx(vehicletype,SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2,(30*60)); // respawn 30 minutes
  51. /*new numplate_test[32+1];
  52. format(numplate_test,32,"GRLC{44AA33}%d",vid);
  53. SetVehicleNumberPlate(vid, numplate_test);*/
  54. vehicles_loaded++;
  55. }
  56. fclose(file_ptr);
  57. printf("Loaded %d vehicles from: %s",vehicles_loaded,filename);
  58. return vehicles_loaded;
  59. }
  60. //----------------------------------------------------------
  61. stock strtok(const string[], &index)
  62. {
  63. new length = strlen(string);
  64. while ((index < length) && (string[index] <= ' '))
  65. {
  66. index++;
  67. }
  68. new offset = index;
  69. new result[20];
  70. while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  71. {
  72. result[index - offset] = string[index];
  73. index++;
  74. }
  75. result[index - offset] = EOS;
  76. return result;
  77. }
  78. //------------------------------------------------
  79. stock strrest(const string[], &index)
  80. {
  81. new length = strlen(string);
  82. while ((index < length) && (string[index] <= ' '))
  83. {
  84. index++;
  85. }
  86. new offset = index;
  87. new result[128];
  88. while ((index < length) && ((index - offset) < (sizeof(result) - 1)))
  89. {
  90. result[index - offset] = string[index];
  91. index++;
  92. }
  93. result[index - offset] = EOS;
  94. return result;
  95. }
  96. //----------------------------------------------------------
  97. // Tokenise by a delimiter
  98. // Return string and index of the end determined by the
  99. // provided delimiter in delim
  100. stock token_by_delim(const string[], return_str[], delim, start_index)
  101. {
  102. new x=0;
  103. while(string[start_index] != EOS && string[start_index] != delim) {
  104. return_str[x] = string[start_index];
  105. x++;
  106. start_index++;
  107. }
  108. return_str[x] = EOS;
  109. if(string[start_index] == EOS) start_index = (-1);
  110. return start_index;
  111. }
  112. //----------------------------------------------------------
  113. stock isNumeric(const string[])
  114. {
  115. new length=strlen(string);
  116. if (length==0) return false;
  117. for (new i = 0; i < length; i++)
  118. {
  119. if (
  120. (string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
  121. || (string[i]=='-' && i!=0) // A '-' but not at first.
  122. || (string[i]=='+' && i!=0) // A '+' but not at first.
  123. ) return false;
  124. }
  125. if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
  126. return true;
  127. }
  128. //----------------------------------------------------------
  129. stock IsKeyJustDown(key, newkeys, oldkeys)
  130. {
  131. if((newkeys & key) && !(oldkeys & key)) return 1;
  132. return 0;
  133. }
  134. //----------------------------------------------------------
  135. stock PlaySoundForAll(soundid, Float:x, Float:y, Float:z)
  136. {
  137. for(new i=0; i<MAX_PLAYERS; i++)
  138. {
  139. if(IsPlayerConnected(i))
  140. {
  141. PlayerPlaySound(i, soundid, x, y, z);
  142. }
  143. }
  144. }
  145. //----------------------------------------------------------
  146. stock PlaySoundForPlayersInRange(soundid, Float:range, Float:x, Float:y, Float:z)
  147. {
  148. for(new i=0; i<MAX_PLAYERS; i++)
  149. {
  150. if(IsPlayerConnected(i) && IsPlayerInRangeOfPoint(i,range,x,y,z))
  151. {
  152. PlayerPlaySound(i, soundid, x, y, z);
  153. }
  154. }
  155. }
  156. //----------------------------------------------------------
  157. #define RETURN_USER_FAILURE -1
  158. #define RETURN_USER_MULTIPLE -2
  159. stock ReturnUser(text[])
  160. {
  161. new pos = 0;
  162. new userid = RETURN_USER_FAILURE;
  163. while(text[pos] < 0x21) { // Strip out leading spaces
  164. if(text[pos] == 0) return RETURN_USER_FAILURE; // No passed text
  165. pos++;
  166. }
  167. if(isNumeric(text[pos])) { // Check whole passed string
  168. userid = strval(text[pos]);
  169. if(userid >=0 && userid < MAX_PLAYERS)
  170. {
  171. if(IsPlayerConnected(userid)) return userid;
  172. return RETURN_USER_FAILURE;
  173. }
  174. }
  175. // They entered [part of] a name or the id search failed (check names just incase)
  176. new len = strlen(text[pos]);
  177. new count = 0;
  178. new name[MAX_PLAYER_NAME+1];
  179. for(new i = 0; i < MAX_PLAYERS; i++)
  180. {
  181. if(IsPlayerConnected(i))
  182. {
  183. GetPlayerName(i, name, sizeof(name));
  184. if(strcmp(name, text[pos], true, len) == 0) // Check segment of name
  185. {
  186. if(len == strlen(name)) { // Exact match
  187. return i;
  188. }
  189. else { // Partial match
  190. count++;
  191. userid = i;
  192. }
  193. }
  194. }
  195. }
  196. if(!count) return RETURN_USER_FAILURE;
  197. if(count > 1) return RETURN_USER_MULTIPLE;
  198. return userid;
  199. }
  200. //----------------------------------------------------------