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.

387 lines
14 KiB

  1. //
  2. // Admin vehicle spawner using previews. For SA-MP 0.3x and above.
  3. // - Kye 2012
  4. //
  5. #include <a_samp>
  6. #include "../include/gl_common.inc"
  7. #define TOTAL_ITEMS 207
  8. #define SELECTION_ITEMS 21
  9. #define ITEMS_PER_LINE 7
  10. #define HEADER_TEXT "Vehicles"
  11. #define NEXT_TEXT "Next"
  12. #define PREV_TEXT "Prev"
  13. #define DIALOG_BASE_X 75.0
  14. #define DIALOG_BASE_Y 130.0
  15. #define DIALOG_WIDTH 550.0
  16. #define DIALOG_HEIGHT 180.0
  17. #define SPRITE_DIM_X 60.0
  18. #define SPRITE_DIM_Y 70.0
  19. new gTotalItems = TOTAL_ITEMS;
  20. new PlayerText:gCurrentPageTextDrawId[MAX_PLAYERS];
  21. new PlayerText:gHeaderTextDrawId[MAX_PLAYERS];
  22. new PlayerText:gBackgroundTextDrawId[MAX_PLAYERS];
  23. new PlayerText:gNextButtonTextDrawId[MAX_PLAYERS];
  24. new PlayerText:gPrevButtonTextDrawId[MAX_PLAYERS];
  25. new PlayerText:gSelectionItems[MAX_PLAYERS][SELECTION_ITEMS];
  26. new gSelectionItemsTag[MAX_PLAYERS][SELECTION_ITEMS];
  27. new gItemAt[MAX_PLAYERS];
  28. new gItemList[TOTAL_ITEMS] = {
  29. 400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,
  30. 431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,450,451,452,453,454,455,456,457,458,459,460,461,
  31. 462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,
  32. 493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,
  33. 524,525,526,527,528,529,530,531,532,533,534,535,536,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,
  34. 555,556,557,558,559,560,561,562,563,564,565,566,567,568,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,
  35. 586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611
  36. };
  37. //------------------------------------------------
  38. public OnFilterScriptInit()
  39. {
  40. print("\n--Admin Vehicle Spawner Loaded\n");
  41. return 1;
  42. }
  43. //------------------------------------------------
  44. GetNumberOfPages()
  45. {
  46. if((gTotalItems >= SELECTION_ITEMS) && (gTotalItems % SELECTION_ITEMS) == 0)
  47. {
  48. return (gTotalItems / SELECTION_ITEMS);
  49. }
  50. else return (gTotalItems / SELECTION_ITEMS) + 1;
  51. }
  52. //------------------------------------------------
  53. PlayerText:CreateCurrentPageTextDraw(playerid, Float:Xpos, Float:Ypos)
  54. {
  55. new PlayerText:txtInit;
  56. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, "0/0");
  57. PlayerTextDrawUseBox(playerid, txtInit, 0);
  58. PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  59. PlayerTextDrawFont(playerid, txtInit, 1);
  60. PlayerTextDrawSetShadow(playerid, txtInit, 0);
  61. PlayerTextDrawSetOutline(playerid, txtInit, 1);
  62. PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  63. PlayerTextDrawShow(playerid, txtInit);
  64. return txtInit;
  65. }
  66. //------------------------------------------------
  67. // Creates a button textdraw and returns the textdraw ID.
  68. PlayerText:CreatePlayerDialogButton(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height, button_text[])
  69. {
  70. new PlayerText:txtInit;
  71. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, button_text);
  72. PlayerTextDrawUseBox(playerid, txtInit, 1);
  73. PlayerTextDrawBoxColor(playerid, txtInit, 0x000000FF);
  74. PlayerTextDrawBackgroundColor(playerid, txtInit, 0x000000FF);
  75. PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  76. PlayerTextDrawFont(playerid, txtInit, 1);
  77. PlayerTextDrawSetShadow(playerid, txtInit, 0); // no shadow
  78. PlayerTextDrawSetOutline(playerid, txtInit, 0);
  79. PlayerTextDrawColor(playerid, txtInit, 0x4A5A6BFF);
  80. PlayerTextDrawSetSelectable(playerid, txtInit, 1);
  81. PlayerTextDrawAlignment(playerid, txtInit, 2);
  82. PlayerTextDrawTextSize(playerid, txtInit, Height, Width); // The width and height are reversed for centering.. something the game does <g>
  83. PlayerTextDrawShow(playerid, txtInit);
  84. return txtInit;
  85. }
  86. //------------------------------------------------
  87. PlayerText:CreatePlayerHeaderTextDraw(playerid, Float:Xpos, Float:Ypos, header_text[])
  88. {
  89. new PlayerText:txtInit;
  90. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, header_text);
  91. PlayerTextDrawUseBox(playerid, txtInit, 0);
  92. PlayerTextDrawLetterSize(playerid, txtInit, 1.25, 3.0);
  93. PlayerTextDrawFont(playerid, txtInit, 0);
  94. PlayerTextDrawSetShadow(playerid, txtInit, 0);
  95. PlayerTextDrawSetOutline(playerid, txtInit, 1);
  96. PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  97. PlayerTextDrawShow(playerid, txtInit);
  98. return txtInit;
  99. }
  100. //------------------------------------------------
  101. PlayerText:CreatePlayerBackgroundTextDraw(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height)
  102. {
  103. new PlayerText:txtBackground = CreatePlayerTextDraw(playerid, Xpos, Ypos,
  104. " ~n~"); // enough space for everyone
  105. PlayerTextDrawUseBox(playerid, txtBackground, 1);
  106. PlayerTextDrawBoxColor(playerid, txtBackground, 0x00000099);
  107. PlayerTextDrawLetterSize(playerid, txtBackground, 5.0, 5.0);
  108. PlayerTextDrawFont(playerid, txtBackground, 0);
  109. PlayerTextDrawSetShadow(playerid, txtBackground, 0);
  110. PlayerTextDrawSetOutline(playerid, txtBackground, 0);
  111. PlayerTextDrawColor(playerid, txtBackground,0x000000FF);
  112. PlayerTextDrawTextSize(playerid, txtBackground, Width, Height);
  113. PlayerTextDrawBackgroundColor(playerid, txtBackground, 0x00000099);
  114. PlayerTextDrawShow(playerid, txtBackground);
  115. return txtBackground;
  116. }
  117. //------------------------------------------------
  118. // Creates a model preview sprite
  119. PlayerText:CreateModelPreviewTextDraw(playerid, modelindex, Float:Xpos, Float:Ypos, Float:width, Float:height)
  120. {
  121. new PlayerText:txtPlayerSprite = CreatePlayerTextDraw(playerid, Xpos, Ypos, ""); // it has to be set with SetText later
  122. PlayerTextDrawFont(playerid, txtPlayerSprite, TEXT_DRAW_FONT_MODEL_PREVIEW);
  123. PlayerTextDrawColor(playerid, txtPlayerSprite, 0xFFFFFFFF);
  124. PlayerTextDrawBackgroundColor(playerid, txtPlayerSprite, 0x000000EE);
  125. PlayerTextDrawTextSize(playerid, txtPlayerSprite, width, height); // Text size is the Width:Height
  126. PlayerTextDrawSetPreviewModel(playerid, txtPlayerSprite, modelindex);
  127. PlayerTextDrawSetPreviewRot(playerid,txtPlayerSprite, -16.0, 0.0, -55.0);
  128. PlayerTextDrawSetSelectable(playerid, txtPlayerSprite, 1);
  129. PlayerTextDrawShow(playerid,txtPlayerSprite);
  130. return txtPlayerSprite;
  131. }
  132. //------------------------------------------------
  133. DestroyPlayerModelPreviews(playerid)
  134. {
  135. new x=0;
  136. while(x != SELECTION_ITEMS) {
  137. if(gSelectionItems[playerid][x] != PlayerText:INVALID_TEXT_DRAW) {
  138. PlayerTextDrawDestroy(playerid, gSelectionItems[playerid][x]);
  139. gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  140. }
  141. x++;
  142. }
  143. }
  144. //------------------------------------------------
  145. ShowPlayerModelPreviews(playerid)
  146. {
  147. new x=0;
  148. new Float:BaseX = DIALOG_BASE_X;
  149. new Float:BaseY = DIALOG_BASE_Y - (SPRITE_DIM_Y * 0.33); // down a bit
  150. new linetracker = 0;
  151. new itemat = GetPVarInt(playerid, "vspawner_page") * SELECTION_ITEMS;
  152. // Destroy any previous ones created
  153. DestroyPlayerModelPreviews(playerid);
  154. while(x != SELECTION_ITEMS && itemat < gTotalItems) {
  155. if(linetracker == 0) {
  156. BaseX = DIALOG_BASE_X + 25.0; // in a bit from the box
  157. BaseY += SPRITE_DIM_Y + 1.0; // move on the Y for the next line
  158. }
  159. gSelectionItems[playerid][x] = CreateModelPreviewTextDraw(playerid, gItemList[itemat], BaseX, BaseY, SPRITE_DIM_X, SPRITE_DIM_Y);
  160. gSelectionItemsTag[playerid][x] = gItemList[itemat];
  161. BaseX += SPRITE_DIM_X + 1.0; // move on the X for the next sprite
  162. linetracker++;
  163. if(linetracker == ITEMS_PER_LINE) linetracker = 0;
  164. itemat++;
  165. x++;
  166. }
  167. }
  168. //------------------------------------------------
  169. UpdatePageTextDraw(playerid)
  170. {
  171. new PageText[64+1];
  172. format(PageText, 64, "%d/%d", GetPVarInt(playerid,"vspawner_page") + 1, GetNumberOfPages());
  173. PlayerTextDrawSetString(playerid, gCurrentPageTextDrawId[playerid], PageText);
  174. }
  175. //------------------------------------------------
  176. CreateSelectionMenu(playerid)
  177. {
  178. gBackgroundTextDrawId[playerid] = CreatePlayerBackgroundTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y + 20.0, DIALOG_WIDTH, DIALOG_HEIGHT);
  179. gHeaderTextDrawId[playerid] = CreatePlayerHeaderTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y, HEADER_TEXT);
  180. gCurrentPageTextDrawId[playerid] = CreateCurrentPageTextDraw(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y + 15.0);
  181. gNextButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, NEXT_TEXT);
  182. gPrevButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 90.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, PREV_TEXT);
  183. ShowPlayerModelPreviews(playerid);
  184. UpdatePageTextDraw(playerid);
  185. }
  186. //------------------------------------------------
  187. DestroySelectionMenu(playerid)
  188. {
  189. DestroyPlayerModelPreviews(playerid);
  190. PlayerTextDrawDestroy(playerid, gHeaderTextDrawId[playerid]);
  191. PlayerTextDrawDestroy(playerid, gBackgroundTextDrawId[playerid]);
  192. PlayerTextDrawDestroy(playerid, gCurrentPageTextDrawId[playerid]);
  193. PlayerTextDrawDestroy(playerid, gNextButtonTextDrawId[playerid]);
  194. PlayerTextDrawDestroy(playerid, gPrevButtonTextDrawId[playerid]);
  195. gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  196. gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  197. gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  198. gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  199. gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  200. }
  201. //------------------------------------------------
  202. SpawnVehicle_InfrontOfPlayer(playerid, vehiclemodel, color1, color2)
  203. {
  204. new Float:x,Float:y,Float:z;
  205. new Float:facing;
  206. new Float:distance;
  207. GetPlayerPos(playerid, x, y, z);
  208. GetPlayerFacingAngle(playerid, facing);
  209. new Float:size_x,Float:size_y,Float:size_z;
  210. GetVehicleModelInfo(vehiclemodel, VEHICLE_MODEL_INFO_SIZE, size_x, size_y, size_z);
  211. distance = size_x + 0.5;
  212. x += (distance * floatsin(-facing, degrees));
  213. y += (distance * floatcos(-facing, degrees));
  214. facing += 90.0;
  215. if(facing > 360.0) facing -= 360.0;
  216. return CreateVehicle(vehiclemodel, x, y, z + (size_z * 0.25), facing, color1, color2, -1);
  217. }
  218. //------------------------------------------------
  219. HandlePlayerItemSelection(playerid, selecteditem)
  220. {
  221. // In this case we're spawning a vehicle for them
  222. SpawnVehicle_InfrontOfPlayer(playerid, gSelectionItemsTag[playerid][selecteditem], -1, -1);
  223. }
  224. //------------------------------------------------
  225. public OnPlayerConnect(playerid)
  226. {
  227. // Init all of the textdraw related globals
  228. gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  229. gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  230. gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  231. gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  232. gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  233. for(new x=0; x < SELECTION_ITEMS; x++) {
  234. gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  235. }
  236. gItemAt[playerid] = 0;
  237. return 1; // Allow other scripts to keep processing OnPlayerConnect
  238. }
  239. //-------------------------------------------
  240. // Even though only Player* textdraws are used in this script,
  241. // OnPlayerClickTextDraw is still required to handle ESC
  242. public OnPlayerClickTextDraw(playerid, Text:clickedid)
  243. {
  244. if(GetPVarInt(playerid, "vspawner_active") == 0) return 0;
  245. // Handle: They cancelled (with ESC)
  246. if(clickedid == Text:INVALID_TEXT_DRAW) {
  247. DestroySelectionMenu(playerid);
  248. SetPVarInt(playerid, "vspawner_active", 0);
  249. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. //------------------------------------------------
  255. public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
  256. {
  257. if(GetPVarInt(playerid, "vspawner_active") == 0) return 0;
  258. new curpage = GetPVarInt(playerid, "vspawner_page");
  259. // Handle: next button
  260. if(playertextid == gNextButtonTextDrawId[playerid]) {
  261. if(curpage < (GetNumberOfPages() - 1)) {
  262. SetPVarInt(playerid, "vspawner_page", curpage + 1);
  263. ShowPlayerModelPreviews(playerid);
  264. UpdatePageTextDraw(playerid);
  265. PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  266. } else {
  267. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  268. }
  269. return 1;
  270. }
  271. // Handle: previous button
  272. if(playertextid == gPrevButtonTextDrawId[playerid]) {
  273. if(curpage > 0) {
  274. SetPVarInt(playerid, "vspawner_page", curpage - 1);
  275. ShowPlayerModelPreviews(playerid);
  276. UpdatePageTextDraw(playerid);
  277. PlayerPlaySound(playerid, 1084, 0.0, 0.0, 0.0);
  278. } else {
  279. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  280. }
  281. return 1;
  282. }
  283. // Search in the array of textdraws used for the items
  284. new x=0;
  285. while(x != SELECTION_ITEMS) {
  286. if(playertextid == gSelectionItems[playerid][x]) {
  287. HandlePlayerItemSelection(playerid, x);
  288. PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  289. DestroySelectionMenu(playerid);
  290. CancelSelectTextDraw(playerid);
  291. SetPVarInt(playerid, "vspawner_active", 0);
  292. return 1;
  293. }
  294. x++;
  295. }
  296. return 0;
  297. }
  298. //------------------------------------------------
  299. public OnPlayerCommandText(playerid, cmdtext[])
  300. {
  301. new cmd[256+1];
  302. new idx;
  303. if(!IsPlayerAdmin(playerid)) return 0;
  304. cmd = strtok(cmdtext, idx);
  305. if(strcmp("/vspawner", cmd, true) == 0)
  306. {
  307. // If there was a previously created selection menu, destroy it
  308. DestroySelectionMenu(playerid);
  309. SetPVarInt(playerid, "vspawner_active", 1);
  310. //SetPVarInt(playerid, "vspawner_page", 0); // will reset the page back to the first
  311. CreateSelectionMenu(playerid);
  312. SelectTextDraw(playerid, 0xACCBF1FF);
  313. return 1;
  314. }
  315. return 0;
  316. }
  317. //------------------------------------------------