Advertisement
Guest User

SAMP - Textdraw vehicle spawner

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