Advertisement
JeffryUGP

Andreaas - Vehicle Spawner Edit

Jun 22nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 14.03 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         207
  10. #define SELECTION_ITEMS     21
  11. #define ITEMS_PER_LINE      7
  12.  
  13. #define HEADER_TEXT "Vehicles"
  14. #define NEXT_TEXT   "Next"
  15. #define PREV_TEXT   "Prev"
  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. new adminCar[MAX_VEHICLES];
  35.  
  36. new gItemList[TOTAL_ITEMS] = {
  37. 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,
  38. 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,
  39. 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,
  40. 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,
  41. 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,
  42. 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,
  43. 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
  44. };
  45.  
  46. //------------------------------------------------
  47.  
  48. GetNumberOfPages()
  49. {
  50.     if((gTotalItems >= SELECTION_ITEMS) && (gTotalItems % SELECTION_ITEMS) == 0)
  51.     {
  52.         return (gTotalItems / SELECTION_ITEMS);
  53.     }
  54.     else return (gTotalItems / SELECTION_ITEMS) + 1;
  55. }
  56.  
  57. //------------------------------------------------
  58.  
  59. PlayerText:CreateCurrentPageTextDraw(playerid, Float:Xpos, Float:Ypos)
  60. {
  61.     new PlayerText:txtInit;
  62.     txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, "0/0");
  63.     PlayerTextDrawUseBox(playerid, txtInit, 0);
  64.     PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  65.     PlayerTextDrawFont(playerid, txtInit, 1);
  66.     PlayerTextDrawSetShadow(playerid, txtInit, 0);
  67.     PlayerTextDrawSetOutline(playerid, txtInit, 1);
  68.     PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  69.     PlayerTextDrawShow(playerid, txtInit);
  70.     return txtInit;
  71. }
  72.  
  73. //------------------------------------------------
  74. // Creates a button textdraw and returns the textdraw ID.
  75.  
  76. PlayerText:CreatePlayerDialogButton(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height, button_text[])
  77. {
  78.     new PlayerText:txtInit;
  79.     txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, button_text);
  80.     PlayerTextDrawUseBox(playerid, txtInit, 1);
  81.     PlayerTextDrawBoxColor(playerid, txtInit, 0x000000FF);
  82.     PlayerTextDrawBackgroundColor(playerid, txtInit, 0x000000FF);
  83.     PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  84.     PlayerTextDrawFont(playerid, txtInit, 1);
  85.     PlayerTextDrawSetShadow(playerid, txtInit, 0); // no shadow
  86.     PlayerTextDrawSetOutline(playerid, txtInit, 0);
  87.     PlayerTextDrawColor(playerid, txtInit, 0x4A5A6BFF);
  88.     PlayerTextDrawSetSelectable(playerid, txtInit, 1);
  89.     PlayerTextDrawAlignment(playerid, txtInit, 2);
  90.     PlayerTextDrawTextSize(playerid, txtInit, Height, Width); // The width and height are reversed for centering.. something the game does <g>
  91.     PlayerTextDrawShow(playerid, txtInit);
  92.     return txtInit;
  93. }
  94.  
  95. //------------------------------------------------
  96.  
  97. PlayerText:CreatePlayerHeaderTextDraw(playerid, Float:Xpos, Float:Ypos, header_text[])
  98. {
  99.     new PlayerText:txtInit;
  100.     txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, header_text);
  101.     PlayerTextDrawUseBox(playerid, txtInit, 0);
  102.     PlayerTextDrawLetterSize(playerid, txtInit, 1.25, 3.0);
  103.     PlayerTextDrawFont(playerid, txtInit, 0);
  104.     PlayerTextDrawSetShadow(playerid, txtInit, 0);
  105.     PlayerTextDrawSetOutline(playerid, txtInit, 1);
  106.     PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  107.     PlayerTextDrawShow(playerid, txtInit);
  108.     return txtInit;
  109. }
  110.  
  111. //------------------------------------------------
  112.  
  113. PlayerText:CreatePlayerBackgroundTextDraw(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height)
  114. {
  115.     new PlayerText:txtBackground = CreatePlayerTextDraw(playerid, Xpos, Ypos,
  116.     "                                            ~n~"); // enough space for everyone
  117.     PlayerTextDrawUseBox(playerid, txtBackground, 1);
  118.     PlayerTextDrawBoxColor(playerid, txtBackground, 0x00000099);
  119.     PlayerTextDrawLetterSize(playerid, txtBackground, 5.0, 5.0);
  120.     PlayerTextDrawFont(playerid, txtBackground, 0);
  121.     PlayerTextDrawSetShadow(playerid, txtBackground, 0);
  122.     PlayerTextDrawSetOutline(playerid, txtBackground, 0);
  123.     PlayerTextDrawColor(playerid, txtBackground,0x000000FF);
  124.     PlayerTextDrawTextSize(playerid, txtBackground, Width, Height);
  125.     PlayerTextDrawBackgroundColor(playerid, txtBackground, 0x00000099);
  126.     PlayerTextDrawShow(playerid, txtBackground);
  127.     return txtBackground;
  128. }
  129.  
  130. //------------------------------------------------
  131. // Creates a model preview sprite
  132.  
  133. PlayerText:CreateModelPreviewTextDraw(playerid, modelindex, Float:Xpos, Float:Ypos, Float:width, Float:height)
  134. {
  135.     new PlayerText:txtPlayerSprite = CreatePlayerTextDraw(playerid, Xpos, Ypos, ""); // it has to be set with SetText later
  136.     PlayerTextDrawFont(playerid, txtPlayerSprite, TEXT_DRAW_FONT_MODEL_PREVIEW);
  137.     PlayerTextDrawColor(playerid, txtPlayerSprite, 0xFFFFFFFF);
  138.     PlayerTextDrawBackgroundColor(playerid, txtPlayerSprite, 0x000000EE);
  139.     PlayerTextDrawTextSize(playerid, txtPlayerSprite, width, height); // Text size is the Width:Height
  140.     PlayerTextDrawSetPreviewModel(playerid, txtPlayerSprite, modelindex);
  141.     PlayerTextDrawSetPreviewRot(playerid,txtPlayerSprite, -16.0, 0.0, -55.0);
  142.     PlayerTextDrawSetSelectable(playerid, txtPlayerSprite, 1);
  143.     PlayerTextDrawShow(playerid,txtPlayerSprite);
  144.     return txtPlayerSprite;
  145. }
  146.  
  147. //------------------------------------------------
  148.  
  149. DestroyPlayerModelPreviews(playerid)
  150. {
  151.     new x=0;
  152.     while(x != SELECTION_ITEMS) {
  153.         if(gSelectionItems[playerid][x] != PlayerText:INVALID_TEXT_DRAW) {
  154.             PlayerTextDrawDestroy(playerid, gSelectionItems[playerid][x]);
  155.             gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  156.         }
  157.         x++;
  158.     }
  159. }
  160.  
  161. //------------------------------------------------
  162.  
  163. ShowPlayerModelPreviews(playerid)
  164. {
  165.     new x=0;
  166.     new Float:BaseX = DIALOG_BASE_X;
  167.     new Float:BaseY = DIALOG_BASE_Y - (SPRITE_DIM_Y * 0.33); // down a bit
  168.     new linetracker = 0;
  169.  
  170.     new itemat = GetPVarInt(playerid, "vspawner_page") * SELECTION_ITEMS;
  171.  
  172.     // Destroy any previous ones created
  173.     DestroyPlayerModelPreviews(playerid);
  174.  
  175.     while(x != SELECTION_ITEMS && itemat < gTotalItems) {
  176.         if(linetracker == 0) {
  177.             BaseX = DIALOG_BASE_X + 25.0; // in a bit from the box
  178.             BaseY += SPRITE_DIM_Y + 1.0; // move on the Y for the next line
  179.         }
  180.         gSelectionItems[playerid][x] = CreateModelPreviewTextDraw(playerid, gItemList[itemat], BaseX, BaseY, SPRITE_DIM_X, SPRITE_DIM_Y);
  181.         gSelectionItemsTag[playerid][x] = gItemList[itemat];
  182.         BaseX += SPRITE_DIM_X + 1.0; // move on the X for the next sprite
  183.         linetracker++;
  184.         if(linetracker == ITEMS_PER_LINE) linetracker = 0;
  185.         itemat++;
  186.         x++;
  187.     }
  188. }
  189.  
  190. //------------------------------------------------
  191.  
  192. UpdatePageTextDraw(playerid)
  193. {
  194.     new PageText[64+1];
  195.     format(PageText, 64, "%d/%d", GetPVarInt(playerid,"vspawner_page") + 1, GetNumberOfPages());
  196.     PlayerTextDrawSetString(playerid, gCurrentPageTextDrawId[playerid], PageText);
  197. }
  198.  
  199. //------------------------------------------------
  200.  
  201. CreateSelectionMenu(playerid)
  202. {
  203.     gBackgroundTextDrawId[playerid] = CreatePlayerBackgroundTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y + 20.0, DIALOG_WIDTH, DIALOG_HEIGHT);
  204.     gHeaderTextDrawId[playerid] = CreatePlayerHeaderTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y, HEADER_TEXT);
  205.     gCurrentPageTextDrawId[playerid] = CreateCurrentPageTextDraw(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y + 15.0);
  206.     gNextButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, NEXT_TEXT);
  207.     gPrevButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 90.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, PREV_TEXT);
  208.  
  209.     ShowPlayerModelPreviews(playerid);
  210.     UpdatePageTextDraw(playerid);
  211. }
  212.  
  213. //------------------------------------------------
  214.  
  215. DestroySelectionMenu(playerid)
  216. {
  217.     DestroyPlayerModelPreviews(playerid);
  218.  
  219.     PlayerTextDrawDestroy(playerid, gHeaderTextDrawId[playerid]);
  220.     PlayerTextDrawDestroy(playerid, gBackgroundTextDrawId[playerid]);
  221.     PlayerTextDrawDestroy(playerid, gCurrentPageTextDrawId[playerid]);
  222.     PlayerTextDrawDestroy(playerid, gNextButtonTextDrawId[playerid]);
  223.     PlayerTextDrawDestroy(playerid, gPrevButtonTextDrawId[playerid]);
  224.  
  225.     gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  226.     gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  227.     gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  228.     gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  229.     gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  230. }
  231.  
  232. //------------------------------------------------
  233.  
  234. SpawnVehicle_InfrontOfPlayer(playerid, vehiclemodel, color1, color2)
  235. {
  236.     new Float:x,Float:y,Float:z;
  237.     new Float:facing;
  238.     new Float:distance;
  239.  
  240.     GetPlayerPos(playerid, x, y, z);
  241.     GetPlayerFacingAngle(playerid, facing);
  242.  
  243.     new Float:size_x,Float:size_y,Float:size_z;
  244.     GetVehicleModelInfo(vehiclemodel, VEHICLE_MODEL_INFO_SIZE, size_x, size_y, size_z);
  245.  
  246.     distance = size_x + 0.5;
  247.  
  248.     x += (distance * floatsin(-facing, degrees));
  249.     y += (distance * floatcos(-facing, degrees));
  250.  
  251.     facing += 90.0;
  252.     if(facing > 360.0) facing -= 360.0;
  253.  
  254.     return CreateVehicle(vehiclemodel, x, y, z + (size_z * 0.25), facing, color1, color2, -1);
  255. }
  256.  
  257. //------------------------------------------------
  258.  
  259. HandlePlayerItemSelection(playerid, selecteditem)
  260. {
  261.     // In this case we're spawning a vehicle for them
  262.     new vehicleid = SpawnVehicle_InfrontOfPlayer(playerid, gSelectionItemsTag[playerid][selecteditem], -1, -1);
  263.     adminCar[vehicleid] = 1;
  264. }
  265.  
  266. //------------------------------------------------
  267.  
  268. public OnPlayerConnect(playerid)
  269. {
  270.     // Init all of the textdraw related globals
  271.     gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  272.     gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  273.     gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  274.     gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  275.     gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  276.  
  277.     for(new x=0; x < SELECTION_ITEMS; x++) {
  278.         gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  279.     }
  280.  
  281.     gItemAt[playerid] = 0;
  282.  
  283.     return 1; // Allow other scripts to keep processing OnPlayerConnect
  284. }
  285.  
  286. //-------------------------------------------
  287. // Even though only Player* textdraws are used in this script,
  288. // OnPlayerClickTextDraw is still required to handle ESC
  289.  
  290. public OnPlayerClickTextDraw(playerid, Text:clickedid)
  291. {
  292.     if(GetPVarInt(playerid, "vspawner_active") == 0) return 0;
  293.  
  294.     // Handle: They cancelled (with ESC)
  295.     if(clickedid == Text:INVALID_TEXT_DRAW) {
  296.         DestroySelectionMenu(playerid);
  297.         SetPVarInt(playerid, "vspawner_active", 0);
  298.         PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  299.         return 1;
  300.     }
  301.  
  302.     return 0;
  303. }
  304.  
  305. //------------------------------------------------
  306.  
  307. public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
  308. {
  309.     if(GetPVarInt(playerid, "vspawner_active") == 0) return 0;
  310.  
  311.     new curpage = GetPVarInt(playerid, "vspawner_page");
  312.  
  313.     // Handle: next button
  314.     if(playertextid == gNextButtonTextDrawId[playerid]) {
  315.         if(curpage < (GetNumberOfPages() - 1)) {
  316.             SetPVarInt(playerid, "vspawner_page", curpage + 1);
  317.             ShowPlayerModelPreviews(playerid);
  318.             UpdatePageTextDraw(playerid);
  319.             PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  320.         } else {
  321.             PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  322.         }
  323.         return 1;
  324.     }
  325.  
  326.     // Handle: previous button
  327.     if(playertextid == gPrevButtonTextDrawId[playerid]) {
  328.         if(curpage > 0) {
  329.             SetPVarInt(playerid, "vspawner_page", curpage - 1);
  330.             ShowPlayerModelPreviews(playerid);
  331.             UpdatePageTextDraw(playerid);
  332.             PlayerPlaySound(playerid, 1084, 0.0, 0.0, 0.0);
  333.         } else {
  334.             PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  335.         }
  336.         return 1;
  337.     }
  338.  
  339.     // Search in the array of textdraws used for the items
  340.     new x=0;
  341.     while(x != SELECTION_ITEMS) {
  342.         if(playertextid == gSelectionItems[playerid][x]) {
  343.             HandlePlayerItemSelection(playerid, x);
  344.             PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  345.             DestroySelectionMenu(playerid);
  346.             CancelSelectTextDraw(playerid);
  347.             SetPVarInt(playerid, "vspawner_active", 0);
  348.             return 1;
  349.         }
  350.         x++;
  351.     }
  352.  
  353.     return 0;
  354. }
  355.  
  356. //------------------------------------------------
  357.  
  358. public OnPlayerCommandText(playerid, cmdtext[])
  359. {
  360.     new cmd[256+1];
  361.     new idx;
  362.  
  363.     if(!IsPlayerAdmin(playerid)) return 0;
  364.  
  365.     cmd = strtok(cmdtext, idx);
  366.  
  367.     if(strcmp("/vspawner", cmd, true) == 0)
  368.     {
  369.         // If there was a previously created selection menu, destroy it
  370.         DestroySelectionMenu(playerid);
  371.  
  372.         SetPVarInt(playerid, "vspawner_active", 1);
  373.         //SetPVarInt(playerid, "vspawner_page", 0); // will reset the page back to the first
  374.  
  375.         CreateSelectionMenu(playerid);
  376.         SelectTextDraw(playerid, 0xACCBF1FF);
  377.         return 1;
  378.     }
  379.  
  380.     return 0;
  381. }
  382. //------------------------------------------------
  383.  
  384. public OnVehicleSpawn(vehicleid)
  385. {
  386.     if(adminCar[vehicleid]) DestroyVehicle(vehicleid);
  387.     adminCar[vehicleid] = 0;
  388.     return 1;
  389. }
  390. //------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement