Advertisement
Guest User

Buttons

a guest
Aug 22nd, 2011
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 25.28 KB | None | 0 0
  1. /*##############################################################################
  2.  
  3.  
  4.                     #########################################
  5.                     #                                       #
  6.                     #     BUTTONS - FILTERSCRIPT BY YOM     #
  7.                     #               v1.3 BETA               #
  8.                     #       Steal my work and die >:D       #
  9.                     #                                       #
  10.                     #########################################
  11.  
  12.  
  13. - Informations about this file:
  14. ===============================
  15.  
  16.     -   You must #include <yom_buttons> in the scripts you want to use buttons
  17.  
  18.  
  19.  
  20.  
  21. - Copyright:
  22. ============
  23.  
  24.     -   Yom's Scripts Factory © ®.
  25.     -   You can use this script and distribute it but,
  26.     -   You WILL NOT sell it or tell it's your own work.
  27.  
  28.  
  29.  
  30. - Versions changes:
  31. ===================
  32.  
  33.     -   1.0 :   Initial release.
  34.  
  35.     -   1.1 :   Small tweaks here and there.
  36.  
  37.     -   1.2 :   Removed: rX and rY parameters (was useless, for me anyway)
  38.                 Changed: rZ parameter is now named Angle
  39.                 Changed: CreateButton now returns the objectid, not the buttonid.
  40.                 Added: Sets the pos of the player, so he press exactly on the button.
  41.  
  42.     -   1.3 :   Changed again: CreateButton returns the buttonid (from 1 to MAX_BUTTONS).
  43.                 Changed: Player can press the button from any angle
  44.                 Added: more or less useful functions for use in your scripts
  45.                 Added: sound
  46.                 Added: button placer (if debug enabled, type /button)
  47.                 THIS IS A BETA VERSION, PLEASE DON'T BE A BASTARD, AND REPORT BUGS/PROBLEMS!
  48.  
  49.  
  50. ##############################################################################*/
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. /*################################ CONFIGURATION #############################*/
  62.  
  63. //max buttons that can be created.
  64. #define MAX_BUTTONS     50
  65.  
  66.  
  67. //this is the distance max for detecting buttons. If the player is at this
  68. //distance, or less, of a button, he will press this button.
  69. #define MAX_DISTANCE    1.3
  70.  
  71.  
  72. //this is the object modelid used for buttons.
  73. //i'm asking for nice objects, if you know some that can be used as buttons, tell me!
  74. #define OBJECT          2886
  75.  
  76.  
  77. //comment this line to disable the sound, or change the number for another sound
  78. //don't forget that you can do whatever you want in OnPlayerPressButton
  79. #define SOUND           1083
  80.  
  81.  
  82. //comment this line to disable debug (recommended once you finished using the
  83. //buttons editor, this will make the script more efficient)
  84. //#define DEBUG
  85.  
  86. /*############################################################################*/
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. /*----------------------------------------------------------------------------*/
  98. #include <a_samp>
  99.  
  100.  
  101. #define INVALID_BUTTON_ID   -1
  102.  
  103.  
  104. enum BUTTON_INFOS
  105. {
  106.     bool:Created,
  107.     bool:Moving,
  108.     bool:Usable[MAX_PLAYERS],
  109.     Float:Pos[4],
  110.     ObjectID
  111. }
  112.  
  113. new ButtonInfo[MAX_BUTTONS+1][BUTTON_INFOS];
  114.  
  115. #if defined DEBUG
  116.  
  117.     enum PLAYER_INFOS
  118.     {
  119.         Float:MSpeed,
  120.         SelectedButton,
  121.         TimerID
  122.     }
  123.  
  124.     new PlayerInfo[MAX_PLAYERS][PLAYER_INFOS];
  125.     new String[128];
  126.  
  127. #endif
  128. /*----------------------------------------------------------------------------*/
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. /*----------------------------------------------------------------------------*/
  140. Float:Distance3D(Float:PointA[], Float:PointB[], bool:sqrt = true)
  141. {
  142.     new Float:Dist[4];
  143.  
  144.     for (new i = 0; i < 3; i++)
  145.     {
  146.         Dist[i] = PointA[i] - PointB[i];
  147.         Dist[i] *= Dist[i];
  148.     }
  149.  
  150.     Dist[3] = Dist[0] + Dist[1] + Dist[2];
  151.  
  152.     return sqrt ? floatsqroot(Dist[3]) : Dist[3];
  153. }
  154. /*----------------------------------------------------------------------------*/
  155.  
  156.  
  157. /*----------------------------------------------------------------------------*/
  158. Float:Angle2D(Float:PointA[], Float:PointB[])
  159. {
  160.     new bool:A_LS_B[2], Float:Dist[2], Float:Angle;
  161.  
  162.     for (new i = 0; i < 2; i++)
  163.     {
  164.         A_LS_B[i] = PointA[i] < PointB[i];
  165.         Dist[i] = A_LS_B[i] ? PointB[i] - PointA[i] : PointA[i] - PointB[i];
  166.     }
  167.  
  168.     Angle = atan2(Dist[1],Dist[0]);
  169.     Angle = A_LS_B[0] ? 270.0 + Angle : 90.0 - Angle;
  170.     Angle = A_LS_B[1] ? Angle : 180.0 - Angle;
  171.  
  172.     return Angle;
  173. }
  174. /*----------------------------------------------------------------------------*/
  175.  
  176.  
  177. /*----------------------------------------------------------------------------*/
  178. GetClosestButton(Float:Point[], &Float:Distance = 0.0)
  179. {
  180.     new Closest = INVALID_BUTTON_ID, Float:Distance2 = 100000.0;
  181.  
  182.     for (new buttonid = 1, highest = FS_GetHighestButtonID(); buttonid <= highest; buttonid ++)
  183.     {
  184.         if (ButtonInfo[buttonid][Created])
  185.         {
  186.             Distance = Distance3D(Point, ButtonInfo[buttonid][Pos]);
  187.  
  188.             if (Distance < Distance2)
  189.             {
  190.                 Distance2 = Distance;
  191.                 Closest = buttonid;
  192.             }
  193.         }
  194.     }
  195.  
  196.     Distance = Distance2;
  197.  
  198.     return Closest;
  199. }
  200. /*----------------------------------------------------------------------------*/
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. /*----------------------------------------------------------------------------*/
  212. forward FS_CreateButton(Float:X, Float:Y, Float:Z, Float:Angle);
  213. public FS_CreateButton(Float:X, Float:Y, Float:Z, Float:Angle)
  214. {
  215.     new buttonid;
  216.  
  217.     for(buttonid = 1; buttonid <= MAX_BUTTONS; buttonid ++)
  218.         if (!ButtonInfo[buttonid][Created])
  219.             break;
  220.  
  221.     ButtonInfo[buttonid][ObjectID]  = CreateObject(OBJECT,X,Y,Z,0.0,0.0,Angle);
  222.     ButtonInfo[buttonid][Pos][0]    = X;
  223.     ButtonInfo[buttonid][Pos][1]    = Y;
  224.     ButtonInfo[buttonid][Pos][2]    = Z;
  225.     ButtonInfo[buttonid][Pos][3]    = Angle;
  226.     ButtonInfo[buttonid][Moving]    = false;
  227.     ButtonInfo[buttonid][Created]   = true;
  228.  
  229.     for (new playerid = 0; playerid < MAX_PLAYERS; playerid ++)
  230.         ButtonInfo[buttonid][Usable][playerid] = true;
  231.  
  232.     return buttonid;
  233. }
  234. /*----------------------------------------------------------------------------*/
  235.  
  236.  
  237. /*----------------------------------------------------------------------------*/
  238. forward FS_DestroyButton(buttonid);
  239. public FS_DestroyButton(buttonid)
  240. {
  241.     if (FS_IsValidButton(buttonid))
  242.     {
  243.         CallRemoteFunction("OnButtonDestroyed", "i", buttonid);
  244.         ButtonInfo[buttonid][Created] = false;
  245.         DestroyObject(ButtonInfo[buttonid][ObjectID]);
  246.     }
  247. }
  248. /*----------------------------------------------------------------------------*/
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259. /*----------------------------------------------------------------------------*/
  260. forward FS_SetButtonPos(buttonid, Float:X, Float:Y, Float:Z, Float:Angle);
  261. public FS_SetButtonPos(buttonid, Float:X, Float:Y, Float:Z, Float:Angle)
  262. {
  263.     if (FS_IsValidButton(buttonid))
  264.     {
  265.         new objectid = ButtonInfo[buttonid][ObjectID];
  266.         SetObjectPos(objectid, X, Y, Z);
  267.         SetObjectRot(objectid, 0.0, 0.0, Angle);
  268.         ButtonInfo[buttonid][Pos][0] = X;
  269.         ButtonInfo[buttonid][Pos][1] = Y;
  270.         ButtonInfo[buttonid][Pos][2] = Z;
  271.         ButtonInfo[buttonid][Pos][3] = Angle;
  272.     }
  273. }
  274. /*----------------------------------------------------------------------------*/
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. /*----------------------------------------------------------------------------*/
  286. forward FS_MoveButton(buttonid, Float:X, Float:Y, Float:Z, Float:Speed);
  287. public FS_MoveButton(buttonid, Float:X, Float:Y, Float:Z, Float:Speed)
  288. {
  289.     if (FS_IsValidButton(buttonid))
  290.     {
  291.         MoveObject(ButtonInfo[buttonid][ObjectID], X, Y, Z, Speed);
  292.         ButtonInfo[buttonid][Moving] = true;
  293.         ButtonInfo[buttonid][Pos][0] = 99999.9;
  294.         ButtonInfo[buttonid][Pos][1] = 99999.9;
  295.         ButtonInfo[buttonid][Pos][2] = 99999.9;
  296.     }
  297. }
  298. /*----------------------------------------------------------------------------*/
  299.  
  300.  
  301. /*----------------------------------------------------------------------------*/
  302. forward FS_StopButton(buttonid);
  303. public FS_StopButton(buttonid)
  304. {
  305.     if (FS_IsValidButton(buttonid))
  306.         StopObject(ButtonInfo[buttonid][ObjectID]);
  307. }
  308. /*----------------------------------------------------------------------------*/
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. /*----------------------------------------------------------------------------*/
  320. forward bool:FS_IsValidButton(buttonid);
  321. public bool:FS_IsValidButton(buttonid)
  322. {
  323.     return (buttonid <= MAX_BUTTONS && ButtonInfo[buttonid][Created]);
  324. }
  325. /*----------------------------------------------------------------------------*/
  326.  
  327.  
  328. /*----------------------------------------------------------------------------*/
  329. forward FS_GetHighestButtonID();
  330. public FS_GetHighestButtonID()
  331. {
  332.     for (new buttonid = MAX_BUTTONS; buttonid > 0; buttonid --)
  333.         if (ButtonInfo[buttonid][Created])
  334.             return buttonid;
  335.  
  336.     return INVALID_BUTTON_ID;
  337. }
  338. /*----------------------------------------------------------------------------*/
  339.  
  340.  
  341. /*----------------------------------------------------------------------------*/
  342. forward FS_GetButtonObjectID(buttonid);
  343. public FS_GetButtonObjectID(buttonid)
  344. {
  345.     return FS_IsValidButton(buttonid) ? ButtonInfo[buttonid][ObjectID] : INVALID_OBJECT_ID;
  346. }
  347. /*----------------------------------------------------------------------------*/
  348.  
  349.  
  350. /*----------------------------------------------------------------------------*/
  351. forward FS_GetObjectButtonID(objectid);
  352. public FS_GetObjectButtonID(objectid)
  353. {
  354.     for (new buttonid = 1, highest = FS_GetHighestButtonID(); buttonid <= highest; buttonid ++)
  355.         if (ButtonInfo[buttonid][Created] && ButtonInfo[buttonid][ObjectID] == objectid)
  356.             return buttonid;
  357.  
  358.     return INVALID_BUTTON_ID;
  359. }
  360. /*----------------------------------------------------------------------------*/
  361.  
  362.  
  363. /*----------------------------------------------------------------------------*/
  364. forward FS_PrintButtonsInfos();
  365. public FS_PrintButtonsInfos()
  366. {
  367.     print
  368.     (
  369.         "\n \
  370.         ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n \
  371.         ³                   Buttons Informations                  ³\n \
  372.         ÃÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄ´\n \
  373.         ³ButtonID³ObjectID³    X    ³    Y    ³    Z    ³    A    ³\n \
  374.         ÃÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄ´"
  375.     );
  376.  
  377.     for (new buttonid = 1; buttonid <= MAX_BUTTONS; buttonid ++)
  378.     {
  379.         if (ButtonInfo[buttonid][Created])
  380.         {
  381.             printf
  382.             (
  383.                 " ³%8d³%8d³%6.2f³%6.2f³%6.2f³%6.2f³",
  384.                 buttonid,
  385.                 ButtonInfo[buttonid][ObjectID],
  386.                 ButtonInfo[buttonid][Pos][0],
  387.                 ButtonInfo[buttonid][Pos][1],
  388.                 ButtonInfo[buttonid][Pos][2],
  389.                 ButtonInfo[buttonid][Pos][3]
  390.             );
  391.         }
  392.     }
  393.  
  394.     print(" ÀÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÙ\n");
  395. }
  396. /*----------------------------------------------------------------------------*/
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407. /*----------------------------------------------------------------------------*/
  408. forward Float:FS_GetDistanceToButton(buttonid, Float:X, Float:Y, Float:Z);
  409. public Float:FS_GetDistanceToButton(buttonid, Float:X, Float:Y, Float:Z)
  410. {
  411.     if (FS_IsValidButton(buttonid))
  412.     {
  413.         new Float:Point[3];
  414.  
  415.         Point[0] = X;
  416.         Point[1] = Y;
  417.         Point[2] = Z;
  418.  
  419.         return Distance3D(Point, ButtonInfo[buttonid][Pos]);
  420.     }
  421.  
  422.     return -1.0;
  423. }
  424. /*----------------------------------------------------------------------------*/
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435. /*----------------------------------------------------------------------------*/
  436. forward FS_TeleportPlayerToButton(playerid, buttonid);
  437. public FS_TeleportPlayerToButton(playerid, buttonid)
  438. {
  439.     if (FS_IsValidButton(buttonid) && !ButtonInfo[buttonid][Moving])
  440.     {
  441.         new Float:Angle = ButtonInfo[buttonid][Pos][3];
  442.  
  443.         SetPlayerPos
  444.         (
  445.             playerid,
  446.             ButtonInfo[buttonid][Pos][0] - (0.65 * floatsin(-Angle,degrees)),
  447.             ButtonInfo[buttonid][Pos][1] - (0.65 * floatcos(-Angle,degrees)),
  448.             ButtonInfo[buttonid][Pos][2] - 0.63
  449.         );
  450.  
  451.         SetPlayerFacingAngle(playerid, -Angle);
  452.         SetCameraBehindPlayer(playerid);
  453.     }
  454. }
  455. /*----------------------------------------------------------------------------*/
  456.  
  457.  
  458. /*----------------------------------------------------------------------------*/
  459. forward FS_ToggleButtonEnabledForPlayer(playerid, buttonid, bool:enabled);
  460. public FS_ToggleButtonEnabledForPlayer(playerid, buttonid, bool:enabled)
  461. {
  462.     if (FS_IsValidButton(buttonid))
  463.         ButtonInfo[buttonid][Usable][playerid] = enabled;
  464. }
  465. /*----------------------------------------------------------------------------*/
  466.  
  467.  
  468. /*----------------------------------------------------------------------------*/
  469. forward FS_ToggleButtonEnabled(buttonid, bool:enabled);
  470. public FS_ToggleButtonEnabled(buttonid, bool:enabled)
  471. {
  472.     if (FS_IsValidButton(buttonid))
  473.         for (new playerid = 0; playerid < MAX_PLAYERS; playerid ++)
  474.             ButtonInfo[buttonid][Usable][playerid] = enabled;
  475. }
  476. /*----------------------------------------------------------------------------*/
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487. /*----------------------------------------------------------------------------*/
  488. forward OnPlayerPressButton_Delay(playerid, buttonid);
  489. public OnPlayerPressButton_Delay(playerid, buttonid)
  490. {
  491.     #if defined SOUND
  492.         PlayerPlaySound(playerid, SOUND, 0.0, 0.0, 0.0);
  493.     #endif
  494.     CallRemoteFunction("OnPlayerPressButton", "ii", playerid, buttonid);
  495. }
  496. /*----------------------------------------------------------------------------*/
  497.  
  498.  
  499. /*----------------------------------------------------------------------------*/
  500. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  501. {
  502.     if (GetPlayerState(playerid) == PLAYER_STATE_ONFOOT)
  503.     {
  504.         if (newkeys & 16)
  505.         {
  506.             new Float:Distance, Float:Angle, Float:PlayerPos[3], buttonid;
  507.  
  508.             GetPlayerPos(playerid, PlayerPos[0], PlayerPos[1], PlayerPos[2]);
  509.  
  510.             buttonid = GetClosestButton(PlayerPos, Distance);
  511.  
  512.             if (buttonid != INVALID_BUTTON_ID && ButtonInfo[buttonid][Usable][playerid] && Distance <= MAX_DISTANCE)
  513.             {
  514.                 Angle = Angle2D(PlayerPos, ButtonInfo[buttonid][Pos]);
  515.  
  516.                 SetPlayerFacingAngle(playerid, Angle);
  517.  
  518.                 SetPlayerPos
  519.                 (
  520.                     playerid,
  521.                     ButtonInfo[buttonid][Pos][0] - (0.65 * floatsin(-Angle,degrees)),
  522.                     ButtonInfo[buttonid][Pos][1] - (0.65 * floatcos(-Angle,degrees)),
  523.                     ButtonInfo[buttonid][Pos][2] - 0.63
  524.                 );
  525.  
  526.                 ApplyAnimation(playerid, "HEIST9", "Use_SwipeCard", 10.0, 0, 0, 0, 0, 0);
  527.                 SetTimerEx("OnPlayerPressButton_Delay", 500, false, "ii", playerid, buttonid);
  528.             }
  529.         }
  530.  
  531.  
  532.         #if defined DEBUG
  533.  
  534.         else if (newkeys & KEY_HANDBRAKE)
  535.         {
  536.             if (PlayerInfo[playerid][SelectedButton] != INVALID_BUTTON_ID)
  537.             {
  538.                 switch (PlayerInfo[playerid][MSpeed])
  539.                 {
  540.                     case 1.0 : { PlayerInfo[playerid][MSpeed] =  2.5; String = "Slow"; }
  541.                     case 2.5 : { PlayerInfo[playerid][MSpeed] =  5.0; String = "Normal"; }
  542.                     case 5.0 : { PlayerInfo[playerid][MSpeed] = 15.0; String = "Fast"; }
  543.                     default  : { PlayerInfo[playerid][MSpeed] =  1.0; String = "Very Slow"; }
  544.                 }
  545.  
  546.                 format(String, sizeof(String), "~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~w~Movement speed~n~~r~%s~w~!", String);
  547.                 GameTextForPlayer(playerid, String, 1500, 3);
  548.             }
  549.         }
  550.  
  551.         else if (newkeys & KEY_WALK)
  552.         {
  553.             if (PlayerInfo[playerid][SelectedButton] != INVALID_BUTTON_ID)
  554.                 OnPlayerCommandText(playerid, "/button deselect");
  555.             else
  556.                 OnPlayerCommandText(playerid, "/button select");
  557.         }
  558.  
  559.         #endif
  560.  
  561.     }
  562. }
  563. /*----------------------------------------------------------------------------*/
  564.  
  565.  
  566. /*----------------------------------------------------------------------------*/
  567. public OnObjectMoved(objectid)
  568. {
  569.     new buttonid = FS_GetObjectButtonID(objectid);
  570.  
  571.     if (buttonid != INVALID_BUTTON_ID)
  572.     {
  573.         new Float:ObjectPos[3];
  574.         GetObjectPos(objectid, ObjectPos[0], ObjectPos[1], ObjectPos[2]);
  575.         ButtonInfo[buttonid][Pos][0] = ObjectPos[0];
  576.         ButtonInfo[buttonid][Pos][1] = ObjectPos[1];
  577.         ButtonInfo[buttonid][Pos][2] = ObjectPos[2];
  578.         ButtonInfo[buttonid][Moving] = false;
  579.         CallRemoteFunction("OnButtonMoved", "i", buttonid);
  580.     }
  581. }
  582. /*----------------------------------------------------------------------------*/
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593. /*----------------------------------------------------------------------------*/
  594. public OnPlayerConnect(playerid)
  595. {
  596.     #if defined DEBUG
  597.         if (PlayerInfo[playerid][SelectedButton] != INVALID_BUTTON_ID)
  598.         {
  599.             PlayerInfo[playerid][SelectedButton] = INVALID_BUTTON_ID;
  600.             PlayerInfo[playerid][MSpeed] = 5.0;
  601.             KillTimer(PlayerInfo[playerid][TimerID]);
  602.         }
  603.     #endif
  604.  
  605.     ApplyAnimation(playerid, "HEIST9", "Use_SwipeCard", 10.0, 0, 0, 0, 0, 0);
  606. }
  607. /*----------------------------------------------------------------------------*/
  608.  
  609.  
  610. /*----------------------------------------------------------------------------*/
  611. public OnGameModeInit()
  612. {
  613.     #if defined DEBUG
  614.         FS_PrintButtonsInfos();
  615.     #endif
  616.  
  617.     return true;
  618. }
  619. /*----------------------------------------------------------------------------*/
  620.  
  621.  
  622. /*----------------------------------------------------------------------------*/
  623. public OnGameModeExit()
  624. {
  625.     #if defined DEBUG
  626.         FS_PrintButtonsInfos();
  627.     #endif
  628.  
  629.     for (new buttonid = 1; buttonid <= MAX_BUTTONS; buttonid ++)
  630.         if (ButtonInfo[buttonid][Created])
  631.             FS_DestroyButton(buttonid);
  632.  
  633.     return true;
  634. }
  635. /*----------------------------------------------------------------------------*/
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646. /*----------------------------------------------------------------------------*/
  647. #if defined DEBUG
  648.  
  649.  
  650.  
  651.     argpos(const string[], idx = 0, sep = ' ')
  652.     {
  653.         for(new i = idx, j = strlen(string); i < j; i++)
  654.             if (string[i] == sep && string[i+1] != sep)
  655.                 return i+1;
  656.  
  657.         return -1;
  658.     }
  659.  
  660.  
  661.  
  662.     public OnPlayerCommandText(playerid, cmdtext[])
  663.     {
  664.         if (strlen(cmdtext) > 50)
  665.         {
  666.             SendClientMessage(playerid, 0xFF0000FF, "Invalid command length (exceeding 50 characters)");
  667.             return true;
  668.         }
  669.  
  670.  
  671.         if(!strcmp(cmdtext, "/button", .length = 7))
  672.         {
  673.  
  674.             if (!IsPlayerAdmin(playerid))
  675.                 return true;
  676.  
  677.  
  678.  
  679.             new arg1 = argpos(cmdtext);
  680.  
  681.  
  682.  
  683.             if (!cmdtext[arg1])
  684.             {
  685.                 SendClientMessage(playerid, 0xFF0000FF, "Button Editor's commands:");
  686.                 SendClientMessage(playerid, 0xFFFFFFFF, "\"/button create\" - Create a button at your position, then type \"/button select\" to move this button.");
  687.                 SendClientMessage(playerid, 0xFFFFFFFF, "\"/button select <opt:buttonid>\" - If you don't use the optional parameter, it will select the closest button.");
  688.                 SendClientMessage(playerid, 0xFFFFFFFF, "\"/button save <opt:comment>\" - Save the positions of the selected button, optionally with a short comment.");
  689.                 SendClientMessage(playerid, 0xFFFFFFFF, "\"/button deselect\" - Deselect the selected button, this stops the editing mode.");
  690.                 SendClientMessage(playerid, 0xFF0000FF, "Button Editor's keys:");
  691.                 SendClientMessage(playerid, 0xFFFFFFFF, "Use Directional key to move the selected button forward, backward, to the left and to the right.");
  692.                 SendClientMessage(playerid, 0xFFFFFFFF, "Use Look Behind + Left or Right to rotate the selected button.");
  693.                 SendClientMessage(playerid, 0xFFFFFFFF, "Use Secondary Fire to change the movement speed.");
  694.                 SendClientMessage(playerid, 0xFFFFFFFF, "Use Walk to toggle Deselect/Select closest button.");
  695.                 return true;
  696.             }
  697.  
  698.  
  699.  
  700.             else if (!strcmp(cmdtext[arg1], "create", .length = 6))
  701.             {
  702.                 new Float:PlayerPos[4], buttonid;
  703.                 GetPlayerPos(playerid, PlayerPos[0], PlayerPos[1], PlayerPos[2]);
  704.                 GetPlayerFacingAngle(playerid, PlayerPos[3]);
  705.  
  706.                 buttonid = FS_CreateButton(PlayerPos[0],PlayerPos[1],PlayerPos[2] + 0.63,PlayerPos[3]);
  707.                 format(String, sizeof(String), "Buttonid %d created! Select it with \"/button select\"", buttonid);
  708.                 SendClientMessage(playerid, 0x00FF00FF, String);
  709.                 return true;
  710.             }
  711.  
  712.  
  713.  
  714.             else if (!strcmp(cmdtext[arg1], "select", .length = 6))
  715.             {
  716.                 new arg2 = argpos(cmdtext, arg1),
  717.                     buttonid;
  718.  
  719.                 if (PlayerInfo[playerid][SelectedButton] != INVALID_BUTTON_ID)
  720.                     KillTimer(PlayerInfo[playerid][TimerID]);
  721.  
  722.                 if (!cmdtext[arg2])
  723.                 {
  724.                     new Float:PlayerPos[3];
  725.                     GetPlayerPos(playerid, PlayerPos[0], PlayerPos[1], PlayerPos[2]);
  726.                     buttonid = GetClosestButton(PlayerPos);
  727.  
  728.                     if (buttonid == INVALID_BUTTON_ID)
  729.                         SendClientMessage(playerid, 0xFF0000FF, "Can't find a button! You may need to create one!");
  730.  
  731.                     else
  732.                     {
  733.                         PlayerInfo[playerid][SelectedButton] = buttonid;
  734.                         PlayerInfo[playerid][TimerID] = SetTimerEx("ButtonEditor_Timer", 50, true, "ii", playerid, PlayerInfo[playerid][SelectedButton]);
  735.                         TogglePlayerControllable(playerid, false);
  736.                         format(String, sizeof(String), "Buttonid %d selected! Once you placed it where you want, save it with \"/button save <comment>\"", buttonid);
  737.                         SendClientMessage(playerid, 0x00FF00FF, String);
  738.                     }
  739.                 }
  740.                 else
  741.                 {
  742.                     buttonid = strval(cmdtext[arg2]);
  743.  
  744.                     if (FS_IsValidButton(buttonid))
  745.                     {
  746.                         PlayerInfo[playerid][SelectedButton] = buttonid;
  747.                         PlayerInfo[playerid][TimerID] = SetTimerEx("ButtonEditor_Timer", 50, true, "ii", playerid, PlayerInfo[playerid][SelectedButton]);
  748.                         TogglePlayerControllable(playerid, false);
  749.                         format(String, sizeof(String), "Buttonid %d selected! Once you placed it where you want, save it with \"/button save\"", buttonid);
  750.                         SendClientMessage(playerid, 0x00FF00FF, String);
  751.                     }
  752.  
  753.                     else
  754.                         SendClientMessage(playerid, 0xFF0000FF, "This buttonid is invalid!");
  755.  
  756.                 }
  757.                 return true;
  758.             }
  759.  
  760.  
  761.  
  762.             else if (!strcmp(cmdtext[arg1], "save", .length = 4))
  763.             {
  764.                 new arg2 = argpos(cmdtext, arg1),
  765.                     buttonid = PlayerInfo[playerid][SelectedButton];
  766.  
  767.                 if (buttonid != INVALID_BUTTON_ID)
  768.                 {
  769.                     new File:savedbuttons_file = fopen("savedbuttons.txt", io_append);
  770.  
  771.                     if (!cmdtext[arg2])
  772.                     {
  773.                         format
  774.                         (
  775.                             String,
  776.                             sizeof(String),
  777.                             "CreateButton(%.2f, %.2f, %.2f, %.1f);\r\n",
  778.                             ButtonInfo[buttonid][Pos][0],
  779.                             ButtonInfo[buttonid][Pos][1],
  780.                             ButtonInfo[buttonid][Pos][2],
  781.                             float(floatround(ButtonInfo[buttonid][Pos][3])%360)
  782.                         );
  783.                     }
  784.                     else
  785.                     {
  786.                         format
  787.                         (
  788.                             String,
  789.                             sizeof(String),
  790.                             "CreateButton(%.2f, %.2f, %.2f, %.1f); // %s\r\n",
  791.                             ButtonInfo[buttonid][Pos][0],
  792.                             ButtonInfo[buttonid][Pos][1],
  793.                             ButtonInfo[buttonid][Pos][2],
  794.                             float(floatround(ButtonInfo[buttonid][Pos][3])%360),
  795.                             cmdtext[arg2]
  796.                         );
  797.                     }
  798.  
  799.                     fwrite(savedbuttons_file,String);
  800.                     fclose(savedbuttons_file);
  801.  
  802.                     SendClientMessage(playerid, 0x00FF00FF, "Button's informations saved in \"/scriptfiles/savedbuttons.txt\"!");
  803.                 }
  804.                 else
  805.                     SendClientMessage(playerid, 0xFF0000FF, "Umm..Select a button first?");
  806.  
  807.                 return true;
  808.             }
  809.  
  810.  
  811.  
  812.             else if (!strcmp(cmdtext[arg1], "deselect", .length = 6))
  813.             {
  814.                 if (PlayerInfo[playerid][SelectedButton] != INVALID_BUTTON_ID)
  815.                 {
  816.                     format(String, sizeof(String), "Buttonid %d deselected!", PlayerInfo[playerid][SelectedButton]);
  817.                     SendClientMessage(playerid, 0x00FF00FF, String);
  818.                     PlayerInfo[playerid][SelectedButton] = INVALID_BUTTON_ID;
  819.                     TogglePlayerControllable(playerid, true);
  820.                     KillTimer(PlayerInfo[playerid][TimerID]);
  821.                 }
  822.                 else
  823.                     SendClientMessage(playerid, 0xFF0000FF, "Umm..Select a button first?");
  824.  
  825.                 return true;
  826.             }
  827.  
  828.  
  829.  
  830.             return false;
  831.         }
  832.  
  833.         return false;
  834.     }
  835.  
  836.  
  837.  
  838.  
  839.     forward ButtonEditor_Timer(playerid, buttonid);
  840.     public ButtonEditor_Timer(playerid, buttonid)
  841.     {
  842.         new PlayerKeys[3],
  843.             Float:Move_Sin = PlayerInfo[playerid][MSpeed]/100.0 * floatsin(-ButtonInfo[buttonid][Pos][3], degrees),
  844.             Float:Move_Cos = PlayerInfo[playerid][MSpeed]/100.0 * floatcos(-ButtonInfo[buttonid][Pos][3], degrees);
  845.  
  846.         GetPlayerKeys(playerid,PlayerKeys[0],PlayerKeys[1],PlayerKeys[2]);
  847.  
  848.         if (PlayerKeys[0] + PlayerKeys[1] + PlayerKeys[2] != 0 && PlayerKeys[0] != KEY_HANDBRAKE)
  849.         {
  850.             if (PlayerKeys[0] & 512)
  851.             {
  852.                 if (PlayerKeys[2] == KEY_LEFT)
  853.                     ButtonInfo[buttonid][Pos][3] += PlayerInfo[playerid][MSpeed]/5.0;
  854.  
  855.                 else if (PlayerKeys[2] == KEY_RIGHT)
  856.                     ButtonInfo[buttonid][Pos][3] -= PlayerInfo[playerid][MSpeed]/5.0;
  857.  
  858.                 format
  859.                 (
  860.                     String,
  861.                     sizeof(String),
  862.                     "~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~w~Rotating buttonid ~r~%d ~n~~w~Angle=~r~%d",
  863.                     buttonid,
  864.                     floatround(ButtonInfo[buttonid][Pos][3]) % 360
  865.                 );
  866.  
  867.                 GameTextForPlayer(playerid, String, 1500, 3);
  868.             }
  869.  
  870.             else
  871.             {
  872.                 if (PlayerKeys[1] == KEY_UP)
  873.                 {
  874.                     ButtonInfo[buttonid][Pos][0] += Move_Sin;
  875.                     ButtonInfo[buttonid][Pos][1] += Move_Cos;
  876.                 }
  877.                 else if (PlayerKeys[1] == KEY_DOWN)
  878.                 {
  879.                     ButtonInfo[buttonid][Pos][0] -= Move_Sin;
  880.                     ButtonInfo[buttonid][Pos][1] -= Move_Cos;
  881.                 }
  882.  
  883.                 if (PlayerKeys[2] == KEY_LEFT)
  884.                 {
  885.                     ButtonInfo[buttonid][Pos][0] -= Move_Cos;
  886.                     ButtonInfo[buttonid][Pos][1] += Move_Sin;
  887.                 }
  888.                 else if (PlayerKeys[2] == KEY_RIGHT)
  889.                 {
  890.                     ButtonInfo[buttonid][Pos][0] += Move_Cos;
  891.                     ButtonInfo[buttonid][Pos][1] -= Move_Sin;
  892.                 }
  893.  
  894.                 format
  895.                 (
  896.                     String,
  897.                     sizeof(String),
  898.                     "~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~w~Moving buttonid ~r~%d ~n~~w~X=~r~%.2f ~w~Y=~r~%.2f",
  899.                     buttonid,
  900.                     ButtonInfo[buttonid][Pos][0],
  901.                     ButtonInfo[buttonid][Pos][1]
  902.                 );
  903.  
  904.                 GameTextForPlayer(playerid, String, 1500, 3);
  905.             }
  906.  
  907.             FS_SetButtonPos
  908.             (
  909.                 buttonid,
  910.                 ButtonInfo[buttonid][Pos][0],
  911.                 ButtonInfo[buttonid][Pos][1],
  912.                 ButtonInfo[buttonid][Pos][2],
  913.                 ButtonInfo[buttonid][Pos][3]
  914.             );
  915.         }
  916.     }
  917.  
  918.  
  919.  
  920.  
  921. #endif
  922. /*----------------------------------------------------------------------------*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement