Advertisement
Fitri_Fahim

Vehspawner

Feb 23rd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.04 KB | None | 0 0
  1. //
  2. // Admin vehicle spawner using previews. For SA-MP 0.3x and above.
  3. // - Kye 2012
  4. //
  5.  
  6. #include <a_samp>
  7. #include "../include/gl_common.inc"
  8.  
  9. #define TOTAL_ITEMS 196
  10. #define SELECTION_ITEMS 21
  11. #define ITEMS_PER_LINE 7
  12.  
  13. #define HEADER_TEXT "Vehicles List"
  14. #define NEXT_TEXT "Next"
  15. #define PREV_TEXT "Preview"
  16.  
  17. #define DIALOG_BASE_X 75.0
  18. #define DIALOG_BASE_Y 130.0
  19. #define DIALOG_WIDTH 550.0
  20. #define DIALOG_HEIGHT 180.0
  21. #define SPRITE_DIM_X 60.0
  22. #define SPRITE_DIM_Y 70.0
  23.  
  24. new gTotalItems = TOTAL_ITEMS;
  25. new PlayerText:gCurrentPageTextDrawId[MAX_PLAYERS];
  26. new PlayerText:gHeaderTextDrawId[MAX_PLAYERS];
  27. new PlayerText:gBackgroundTextDrawId[MAX_PLAYERS];
  28. new PlayerText:gNextButtonTextDrawId[MAX_PLAYERS];
  29. new PlayerText:gPrevButtonTextDrawId[MAX_PLAYERS];
  30. new PlayerText:gSelectionItems[MAX_PLAYERS][SELECTION_ITEMS];
  31. new gSelectionItemsTag[MAX_PLAYERS][SELECTION_ITEMS];
  32. new gItemAt[MAX_PLAYERS];
  33.  
  34.  
  35. new gItemList[TOTAL_ITEMS] = {
  36. 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,426,427,428,429,430,
  37. 431,433,434,435,436,437,438,439,440,442,443,444,445,446,448,450,451,452,453,454,455,456,457,458,459,460,461,
  38. 462,463,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,
  39. 493,494,495,496,497,498,499,500,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,521,522,523,
  40. 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,
  41. 555,556,558,559,560,561,562,563,565,566,567,568,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,
  42. 586,587,588,589,590,591,592,593,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611
  43.  
  44.  
  45. };
  46.  
  47. //------------------------------------------------
  48.  
  49. public OnFilterScriptInit()
  50. {
  51. print("\n--Player Vehicle Spawner Loaded\n");
  52. return 1;
  53. }
  54.  
  55. //------------------------------------------------
  56.  
  57. GetNumberOfPages()
  58. {
  59. if((gTotalItems >= SELECTION_ITEMS) && (gTotalItems % SELECTION_ITEMS) == 0)
  60. {
  61. return (gTotalItems / SELECTION_ITEMS);
  62. }
  63. else return (gTotalItems / SELECTION_ITEMS) + 1;
  64. }
  65.  
  66. //------------------------------------------------
  67.  
  68. PlayerText:CreateCurrentPageTextDraw(playerid, Float:Xpos, Float:Ypos)
  69. {
  70. new PlayerText:txtInit;
  71. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, "0/0");
  72. PlayerTextDrawUseBox(playerid, txtInit, 0);
  73. PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  74. PlayerTextDrawFont(playerid, txtInit, 1);
  75. PlayerTextDrawSetShadow(playerid, txtInit, 0);
  76. PlayerTextDrawSetOutline(playerid, txtInit, 1);
  77. PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  78. PlayerTextDrawShow(playerid, txtInit);
  79. return txtInit;
  80. }
  81.  
  82. //------------------------------------------------
  83. // Creates a button textdraw and returns the textdraw ID.
  84.  
  85. PlayerText:CreatePlayerDialogButton(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height, button_text[])
  86. {
  87. new PlayerText:txtInit;
  88. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, button_text);
  89. PlayerTextDrawUseBox(playerid, txtInit, 1);
  90. PlayerTextDrawBoxColor(playerid, txtInit, 0x000000FF);
  91. PlayerTextDrawBackgroundColor(playerid, txtInit, 0x000000FF);
  92. PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  93. PlayerTextDrawFont(playerid, txtInit, 1);
  94. PlayerTextDrawSetShadow(playerid, txtInit, 0); // no shadow
  95. PlayerTextDrawSetOutline(playerid, txtInit, 0);
  96. PlayerTextDrawColor(playerid, txtInit, 0x4A5A6BFF);
  97. PlayerTextDrawSetSelectable(playerid, txtInit, 1);
  98. PlayerTextDrawAlignment(playerid, txtInit, 2);
  99. PlayerTextDrawTextSize(playerid, txtInit, Height, Width); // The width and height are reversed for centering.. something the game does <g>
  100. PlayerTextDrawShow(playerid, txtInit);
  101. return txtInit;
  102. }
  103.  
  104. //------------------------------------------------
  105.  
  106. PlayerText:CreatePlayerHeaderTextDraw(playerid, Float:Xpos, Float:Ypos, header_text[])
  107. {
  108. new PlayerText:txtInit;
  109. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, header_text);
  110. PlayerTextDrawUseBox(playerid, txtInit, 0);
  111. PlayerTextDrawLetterSize(playerid, txtInit, 1.25, 3.0);
  112. PlayerTextDrawFont(playerid, txtInit, 0);
  113. PlayerTextDrawSetShadow(playerid, txtInit, 0);
  114. PlayerTextDrawSetOutline(playerid, txtInit, 1);
  115. PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  116. PlayerTextDrawShow(playerid, txtInit);
  117. return txtInit;
  118. }
  119.  
  120. //------------------------------------------------
  121.  
  122. PlayerText:CreatePlayerBackgroundTextDraw(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height)
  123. {
  124. new PlayerText:txtBackground = CreatePlayerTextDraw(playerid, Xpos, Ypos,
  125. " ~n~"); // enough space for everyone
  126. PlayerTextDrawUseBox(playerid, txtBackground, 1);
  127. PlayerTextDrawBoxColor(playerid, txtBackground, 0x00000099);
  128. PlayerTextDrawLetterSize(playerid, txtBackground, 5.0, 5.0);
  129. PlayerTextDrawFont(playerid, txtBackground, 0);
  130. PlayerTextDrawSetShadow(playerid, txtBackground, 0);
  131. PlayerTextDrawSetOutline(playerid, txtBackground, 0);
  132. PlayerTextDrawColor(playerid, txtBackground,0x000000FF);
  133. PlayerTextDrawTextSize(playerid, txtBackground, Width, Height);
  134. PlayerTextDrawBackgroundColor(playerid, txtBackground, 0x00000099);
  135. PlayerTextDrawShow(playerid, txtBackground);
  136. return txtBackground;
  137. }
  138.  
  139. //------------------------------------------------
  140. // Creates a model preview sprite
  141.  
  142. PlayerText:CreateModelPreviewTextDraw(playerid, modelindex, Float:Xpos, Float:Ypos, Float:width, Float:height)
  143. {
  144. new PlayerText:txtPlayerSprite = CreatePlayerTextDraw(playerid, Xpos, Ypos, ""); // it has to be set with SetText later
  145. PlayerTextDrawFont(playerid, txtPlayerSprite, TEXT_DRAW_FONT_MODEL_PREVIEW);
  146. PlayerTextDrawColor(playerid, txtPlayerSprite, 0xFFFFFFFF);
  147. PlayerTextDrawBackgroundColor(playerid, txtPlayerSprite, 0x000000EE);
  148. PlayerTextDrawTextSize(playerid, txtPlayerSprite, width, height); // Text size is the Width:Height
  149. PlayerTextDrawSetPreviewModel(playerid, txtPlayerSprite, modelindex);
  150. PlayerTextDrawSetPreviewRot(playerid,txtPlayerSprite, -16.0, 0.0, -55.0);
  151. PlayerTextDrawSetSelectable(playerid, txtPlayerSprite, 1);
  152. PlayerTextDrawShow(playerid,txtPlayerSprite);
  153. return txtPlayerSprite;
  154. }
  155.  
  156. //------------------------------------------------
  157.  
  158. DestroyPlayerModelPreviews(playerid)
  159. {
  160. new x=0;
  161. while(x != SELECTION_ITEMS) {
  162. if(gSelectionItems[playerid][x] != PlayerText:INVALID_TEXT_DRAW) {
  163. PlayerTextDrawDestroy(playerid, gSelectionItems[playerid][x]);
  164. gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  165. }
  166. x++;
  167. }
  168. }
  169.  
  170. //------------------------------------------------
  171.  
  172. ShowPlayerModelPreviews(playerid)
  173. {
  174. new x=0;
  175. new Float:BaseX = DIALOG_BASE_X;
  176. new Float:BaseY = DIALOG_BASE_Y - (SPRITE_DIM_Y * 0.33); // down a bit
  177. new linetracker = 0;
  178.  
  179. new itemat = GetPVarInt(playerid, "vspawner_page") * SELECTION_ITEMS;
  180.  
  181. // Destroy any previous ones created
  182. DestroyPlayerModelPreviews(playerid);
  183.  
  184. while(x != SELECTION_ITEMS && itemat < gTotalItems) {
  185. if(linetracker == 0) {
  186. BaseX = DIALOG_BASE_X + 25.0; // in a bit from the box
  187. BaseY += SPRITE_DIM_Y + 1.0; // move on the Y for the next line
  188. }
  189. gSelectionItems[playerid][x] = CreateModelPreviewTextDraw(playerid, gItemList[itemat], BaseX, BaseY, SPRITE_DIM_X, SPRITE_DIM_Y);
  190. gSelectionItemsTag[playerid][x] = gItemList[itemat];
  191. BaseX += SPRITE_DIM_X + 1.0; // move on the X for the next sprite
  192. linetracker++;
  193. if(linetracker == ITEMS_PER_LINE) linetracker = 0;
  194. itemat++;
  195. x++;
  196. }
  197. }
  198.  
  199. //------------------------------------------------
  200.  
  201. UpdatePageTextDraw(playerid)
  202. {
  203. new PageText[64+1];
  204. format(PageText, 64, "%d/%d", GetPVarInt(playerid,"vspawner_page") + 1, GetNumberOfPages());
  205. PlayerTextDrawSetString(playerid, gCurrentPageTextDrawId[playerid], PageText);
  206. }
  207.  
  208. //------------------------------------------------
  209.  
  210. CreateSelectionMenu(playerid)
  211. {
  212. gBackgroundTextDrawId[playerid] = CreatePlayerBackgroundTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y + 20.0, DIALOG_WIDTH, DIALOG_HEIGHT);
  213. gHeaderTextDrawId[playerid] = CreatePlayerHeaderTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y, HEADER_TEXT);
  214. gCurrentPageTextDrawId[playerid] = CreateCurrentPageTextDraw(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y + 15.0);
  215. gNextButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, NEXT_TEXT);
  216. gPrevButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 90.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, PREV_TEXT);
  217.  
  218. ShowPlayerModelPreviews(playerid);
  219. UpdatePageTextDraw(playerid);
  220. }
  221.  
  222. //------------------------------------------------
  223.  
  224. DestroySelectionMenu(playerid)
  225. {
  226. DestroyPlayerModelPreviews(playerid);
  227.  
  228. PlayerTextDrawDestroy(playerid, gHeaderTextDrawId[playerid]);
  229. PlayerTextDrawDestroy(playerid, gBackgroundTextDrawId[playerid]);
  230. PlayerTextDrawDestroy(playerid, gCurrentPageTextDrawId[playerid]);
  231. PlayerTextDrawDestroy(playerid, gNextButtonTextDrawId[playerid]);
  232. PlayerTextDrawDestroy(playerid, gPrevButtonTextDrawId[playerid]);
  233.  
  234. gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  235. gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  236. gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  237. gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  238. gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  239. }
  240.  
  241. //------------------------------------------------
  242.  
  243. SpawnVehicle_InfrontOfPlayer(playerid, vehiclemodel, color1, color2)
  244. {
  245. new Float:x,Float:y,Float:z;
  246. new Float:facing;
  247. new Float:distance;
  248.  
  249. GetPlayerPos(playerid, x, y, z);
  250. GetPlayerFacingAngle(playerid, facing);
  251.  
  252. new Float:size_x,Float:size_y,Float:size_z;
  253. GetVehicleModelInfo(vehiclemodel, VEHICLE_MODEL_INFO_SIZE, size_x, size_y, size_z);
  254.  
  255. distance = size_x + 0.5;
  256.  
  257. x += (distance * floatsin(-facing, degrees));
  258. y += (distance * floatcos(-facing, degrees));
  259.  
  260. facing += 90.0;
  261. if(facing > 360.0) facing -= 360.0;
  262.  
  263. return CreateVehicle(vehiclemodel, x, y, z + (size_z * 0.25), facing, color1, color2, -1);
  264. }
  265.  
  266. //------------------------------------------------
  267.  
  268. HandlePlayerItemSelection(playerid, selecteditem)
  269. {
  270. // In this case we're spawning a vehicle for them
  271. SpawnVehicle_InfrontOfPlayer(playerid, gSelectionItemsTag[playerid][selecteditem], -1, -1);
  272. }
  273.  
  274. //------------------------------------------------
  275.  
  276. public OnPlayerConnect(playerid)
  277. {
  278. // Init all of the textdraw related globals
  279. gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  280. gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  281. gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  282. gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  283. gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  284.  
  285.  
  286. for(new x=0; x < SELECTION_ITEMS; x++) {
  287. gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  288. }
  289.  
  290. gItemAt[playerid] = 0;
  291.  
  292. return 1; // Allow other scripts to keep processing OnPlayerConnect
  293. }
  294.  
  295. //-------------------------------------------
  296. // Even though only Player* textdraws are used in this script,
  297. // OnPlayerClickTextDraw is still required to handle ESC
  298.  
  299. public OnPlayerClickTextDraw(playerid, Text:clickedid)
  300. {
  301. if(GetPVarInt(playerid, "vspawner_active") == 0) return 0;
  302.  
  303. // Handle: They cancelled (with ESC)
  304. if(clickedid == Text:INVALID_TEXT_DRAW) {
  305. DestroySelectionMenu(playerid);
  306. SetPVarInt(playerid, "vspawner_active", 0);
  307. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  308. return 1;
  309. }
  310.  
  311. return 0;
  312. }
  313.  
  314. //------------------------------------------------
  315.  
  316. public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
  317. {
  318. if(GetPVarInt(playerid, "vspawner_active") == 0) return 0;
  319.  
  320. new curpage = GetPVarInt(playerid, "vspawner_page");
  321.  
  322. // Handle: next button
  323. if(playertextid == gNextButtonTextDrawId[playerid]) {
  324. if(curpage < (GetNumberOfPages() - 1)) {
  325. SetPVarInt(playerid, "vspawner_page", curpage + 1);
  326. ShowPlayerModelPreviews(playerid);
  327. UpdatePageTextDraw(playerid);
  328. PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  329. } else {
  330. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  331. }
  332. return 1;
  333. }
  334.  
  335. // Handle: previous button
  336. if(playertextid == gPrevButtonTextDrawId[playerid]) {
  337. if(curpage > 0) {
  338. SetPVarInt(playerid, "vspawner_page", curpage - 1);
  339. ShowPlayerModelPreviews(playerid);
  340. UpdatePageTextDraw(playerid);
  341. PlayerPlaySound(playerid, 1084, 0.0, 0.0, 0.0);
  342. } else {
  343. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  344. }
  345. return 1;
  346. }
  347.  
  348. // Search in the array of textdraws used for the items
  349. new x=0;
  350. while(x != SELECTION_ITEMS) {
  351. if(playertextid == gSelectionItems[playerid][x]) {
  352. HandlePlayerItemSelection(playerid, x);
  353. PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  354. DestroySelectionMenu(playerid);
  355. CancelSelectTextDraw(playerid);
  356. SetPVarInt(playerid, "vspawner_active", 0);
  357. return 1;
  358. }
  359. x++;
  360. }
  361.  
  362. return 0;
  363. }
  364.  
  365. //------------------------------------------------
  366.  
  367. public OnPlayerCommandText(playerid, cmdtext[])
  368. {
  369. new cmd[256+1];
  370. new idx;
  371.  
  372. cmd = strtok(cmdtext, idx);
  373.  
  374. if(strcmp("/v", cmd, true) == 0)
  375. {
  376. // If there was a previously created selection menu, destroy it
  377. DestroySelectionMenu(playerid);
  378.  
  379. RemovePlayerFromVehicle(playerid);
  380. RemovePlayerFromVehicle(playerid);
  381. RemovePlayerFromVehicle(playerid);
  382. RemovePlayerFromVehicle(playerid);
  383. DestroyVehicle(playerid);
  384.  
  385. SetPVarInt(playerid, "vspawner_active", 1);
  386.  
  387.  
  388. //SetPVarInt(playerid, "vspawner_page", 0); // will reset the page back to the first
  389.  
  390. CreateSelectionMenu(playerid);
  391. SelectTextDraw(playerid, 0xACCBF1FF);
  392. return 1;
  393. }
  394.  
  395. return 0;
  396. }
  397. //------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement