Advertisement
Guest User

Brian

a guest
Aug 25th, 2010
1,401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.01 KB | None | 0 0
  1. /*******************************************************************************
  2.                     ____       _
  3.                    / __ )_____(_)___ _____
  4.                   / __  / ___/ / __ `/ __ \
  5.                  / /_/ / /  / / /_/ / / / /
  6.                 /_____/_/  /_/\__,_/_/ /_/
  7.                
  8.     Ingame Holding Object editor v1.1 (for SetPlayerHoldingObject)
  9.         (25th August, 2010), by Brian (Resident Kangaroo)
  10.                    
  11.     Sharing is caring, but don't rip (not that I can see this really being
  12.         ripped, it's not going to find much use in a gamemode).
  13.        
  14.         Updated with sscanf stock (rather than the plugin), textdraws, and
  15.                 a command (/bone) to switch bones while editing.
  16.  
  17. *******************************************************************************/
  18.  
  19.  
  20. #define FILTERSCRIPT
  21. #include <a_samp>
  22. #include <zcmd>
  23.  
  24. /******************************************************
  25. *** objectOffsets information (axes), for reference ***
  26. *******************************************************
  27.                 >> [0] = OffsetX
  28.                 >> [1] = OffsetY
  29.                 >> [2] = OffsetZ
  30.                 >> [3] = RotationX
  31.                 >> [4] = RotationY
  32.                 >> [5] = RotationZ
  33. ******************************************************/
  34. new Float:objectOffsets[MAX_PLAYERS][6], editingObject[MAX_PLAYERS], editingBone[MAX_PLAYERS],
  35. editingAxis[MAX_PLAYERS], editingStatus[MAX_PLAYERS], movedObject[MAX_PLAYERS], Text:PosText[MAX_PLAYERS];
  36. new atext[16] = "X offset";
  37.  
  38. stock SaveObj(comment[], playerid)
  39. {
  40.     new File: objfile;
  41.     if(!fexist("savedobjs.txt"))
  42.     {
  43.         objfile = fopen("savedobjs.txt", io_write);
  44.         if (objfile) { fclose(objfile); }
  45.     }
  46.     new string[256];
  47.     format(string,sizeof(string),"SetPlayerHoldingObject(playerid, %d, %d, %f, %f, %f, %f, %f, %f); // %s\n",editingObject[playerid],
  48.     editingBone[playerid],objectOffsets[playerid][0],objectOffsets[playerid][1],objectOffsets[playerid][2],objectOffsets[playerid][3],
  49.     objectOffsets[playerid][4],objectOffsets[playerid][5],comment);
  50.     objfile = fopen("savedobjs.txt", io_append);
  51.     fwrite(objfile, string);
  52.     fclose(objfile);
  53. }
  54.  
  55. public OnPlayerConnect(playerid)
  56. {
  57.     PosText[playerid] = TextDrawCreate(240.0,580.0," ");
  58.     for(new i = 0; i < 6; i++) { objectOffsets[playerid][i] = 0.0; }
  59.     editingObject[playerid] = -1;
  60.     editingStatus[playerid] = 0;
  61.     editingBone[playerid] = -1;
  62.     editingAxis[playerid] = -1;
  63.     return 1;
  64. }
  65. cmd(bone,playerid,params[])
  66. {
  67.     if(editingStatus[playerid] != 1) return SendClientMessage(playerid,0xFFFFFFFF,"You're not editing an object.");
  68.     new bone, string[64];
  69.     if(sscanf(params,"d",bone)) return SendClientMessage(playerid,0xFFFFFFFF,"SYNTAX: /bone (bone ID 0-18).");
  70.     else if(bone < 1 || bone > 18) return SendClientMessage(playerid,0xFFFFFFFF,"Invalid bone specified.");
  71.     else editingBone[playerid] = bone;
  72.     format(string,sizeof(string),"You are now editing object %d on bone %d.",editingObject[playerid], editingBone[playerid]);
  73.     SendClientMessage(playerid,0xFFFFFFFF,string);
  74.     SetPlayerHoldingObject(playerid, editingObject[playerid], editingBone[playerid], objectOffsets[playerid][0], objectOffsets[playerid][1],
  75.     objectOffsets[playerid][2], objectOffsets[playerid][3], objectOffsets[playerid][4], objectOffsets[playerid][5]);
  76.     return 1;
  77. }
  78. cmd(preview,playerid,params[])
  79. {
  80.     #pragma unused params
  81.     switch(editingStatus[playerid])
  82.     {
  83.         case 1:
  84.         {
  85.             SendClientMessage(playerid,0xFFFFFFFF,"You are now previewing the object; use /preview again to resume editing.");
  86.             TogglePlayerControllable(playerid, 1);
  87.             editingStatus[playerid] = 2;
  88.         }
  89.         case 2:
  90.         {
  91.             SendClientMessage(playerid,0xFFFFFFFF,"Preview finished.");
  92.             TogglePlayerControllable(playerid, 0);
  93.             editingStatus[playerid] = 1;
  94.         }
  95.         default: SendClientMessage(playerid,0xFFFFFFFF,"You're not editing/previewing an object.");
  96.     }
  97.     return 1;
  98. }
  99. cmd(editorhelp,playerid,params[])
  100. {
  101.     #pragma unused params
  102.     SendClientMessage(playerid,0xFFFFFFFF,"Available commands: /editor, /saveobj, /preview, /bone.");
  103.     return 1;
  104. }
  105. cmd(saveobj,playerid,params[])
  106. {
  107.     new string[128];
  108.     if(!editingStatus[playerid]) return SendClientMessage(playerid,0xFFFFFFFF,"You're not editing/previewing an object.");
  109.     if(sscanf(params,"s",string)) return SendClientMessage(playerid,0xFFFFFFFF,"SYNTAX: /saveobj (comment).");
  110.     SaveObj(string, playerid);
  111.     SendClientMessage(playerid,0xFFFFFFFF,"Object saved to 'scriptfiles/savedobjs.txt'.");
  112.     return 1;
  113. }
  114. cmd(editor,playerid,params[])
  115. {
  116.     new string[128], object, bone;
  117.     if(editingStatus[playerid])
  118.     {
  119.         TextDrawHideForPlayer(playerid, PosText[playerid]);
  120.         SendClientMessage(playerid,0xFFFFFFFF,"You have stopped editing. Last object settings:");
  121.         format(string,sizeof(string),"Object %d | Bone %d | Offset (XYZ) %f, %f, %f | Rotation (XYZ) %f, %f, %f",editingObject[playerid],
  122.         editingBone[playerid],objectOffsets[playerid][0],objectOffsets[playerid][1],objectOffsets[playerid][2],objectOffsets[playerid][3],
  123.         objectOffsets[playerid][4],objectOffsets[playerid][5]);
  124.         SendClientMessage(playerid,0xFFFFFFFF,string);
  125.         StopPlayerHoldingObject(playerid);
  126.         editingObject[playerid] = -1;
  127.         editingStatus[playerid] = 0;
  128.         editingBone[playerid] = -1;
  129.         editingAxis[playerid] = -1;
  130.         for(new i = 0; i < 6; i++) { objectOffsets[playerid][i] = 0.0; }
  131.         TogglePlayerControllable(playerid,1);
  132.         return 1;
  133.     }
  134.     if(sscanf(params,"dd",object,bone))
  135.     {
  136.         SendClientMessage(playerid,0xFFFFFFFF,"SYNTAX: /editor (object ID) (bone 0-18).");
  137.         return 1;
  138.     }
  139.     if(bone < 1 || bone > 18) return SendClientMessage(playerid,0xFFFFFFFF,"Invalid bone specified.");
  140.     TextDrawShowForPlayer(playerid, PosText[playerid]);
  141.     editingObject[playerid] = object; editingBone[playerid] = bone; editingStatus[playerid] = 1;
  142.     SetPlayerHoldingObject(playerid, editingObject[playerid], editingBone[playerid], objectOffsets[playerid][0], objectOffsets[playerid][1],
  143.     objectOffsets[playerid][2], objectOffsets[playerid][3], objectOffsets[playerid][4], objectOffsets[playerid][5]);
  144.     format(string,sizeof(string),"Editing object %d, bone %d - use up/down to adjust the value, and left/right to switch the axis.",object,bone);
  145.     TogglePlayerControllable(playerid,0);
  146.     SendClientMessage(playerid,0xFFFFFFFF,string);
  147.     return 1;
  148. }
  149. public OnPlayerUpdate(playerid)
  150. {
  151.     if(editingStatus[playerid] == 1)
  152.     {
  153.         new string[128];
  154.         if(movedObject[playerid] > 0)
  155.         {
  156.             SetPlayerHoldingObject(playerid, editingObject[playerid], editingBone[playerid], objectOffsets[playerid][0], objectOffsets[playerid][1],
  157.             objectOffsets[playerid][2], objectOffsets[playerid][3], objectOffsets[playerid][4], objectOffsets[playerid][5]);
  158.             movedObject[playerid]--;
  159.         }
  160.         new keys, updown, leftright;
  161.         GetPlayerKeys(playerid,keys,updown,leftright);
  162.         if(updown > 0)
  163.         {
  164.             if(editingAxis[playerid] < 3) objectOffsets[playerid][editingAxis[playerid]] -= 0.01;
  165.             else objectOffsets[playerid][editingAxis[playerid]] -= 1.0;
  166.             movedObject[playerid] = 500;
  167.         }
  168.         else if(updown < 0)
  169.         {
  170.             if(editingAxis[playerid] < 3) objectOffsets[playerid][editingAxis[playerid]] += 0.01;
  171.             else objectOffsets[playerid][editingAxis[playerid]] += 1.0;
  172.             movedObject[playerid] = 500;
  173.         }
  174.         if(leftright > 0)
  175.         {
  176.             if(editingAxis[playerid] != 0) editingAxis[playerid]--;
  177.             else editingAxis[playerid] = 5;
  178.             switch(editingAxis[playerid])
  179.             {
  180.                 case 0: atext = "X offset";
  181.                 case 1: atext = "Y offset";
  182.                 case 2: atext = "Z offset";
  183.                 case 3: atext = "X rotation";
  184.                 case 4: atext = "Y rotation";
  185.                 case 5: atext = "Z rotation";
  186.             }
  187.         }
  188.         else if(leftright < 0)
  189.         {
  190.             if(editingAxis[playerid] != 5) editingAxis[playerid]++;
  191.             else editingAxis[playerid] = 0;
  192.             switch(editingAxis[playerid])
  193.             {
  194.                 case 0: atext = "X offset";
  195.                 case 1: atext = "Y offset";
  196.                 case 2: atext = "Z offset";
  197.                 case 3: atext = "X rotation";
  198.                 case 4: atext = "Y rotation";
  199.                 case 5: atext = "Z rotation";
  200.             }
  201.         }
  202.         if(editingAxis[playerid] < 3) { format(string,sizeof(string),"~r~%s~n~~w~%f, %f, %f~n~~b~Object %d, bone %d",atext,objectOffsets[playerid][0],objectOffsets[playerid][1],objectOffsets[playerid][2], editingObject[playerid], editingBone[playerid]); }
  203.         else { format(string,sizeof(string),"~r~%s~n~~w~%f, %f, %f~n~~b~Object %d, bone %d",atext,objectOffsets[playerid][3], objectOffsets[playerid][4],objectOffsets[playerid][5], editingObject[playerid], editingBone[playerid]); }
  204.         TextDrawSetString(PosText[playerid], string);
  205.     }
  206.     return 1;
  207. }
  208. public OnPlayerDisconnect (playerid)
  209. {
  210.     TextDrawDestroy(PosText[playerid]);
  211.     return 1;
  212. }
  213.  
  214. stock sscanf(string[], format[], {Float,_}:...)
  215. {
  216.     #if defined isnull
  217.         if (isnull(string))
  218.     #else
  219.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  220.     #endif
  221.         {
  222.             return format[0];
  223.         }
  224.     #pragma tabsize 4
  225.     new
  226.         formatPos = 0,
  227.         stringPos = 0,
  228.         paramPos = 2,
  229.         paramCount = numargs(),
  230.         delim = ' ';
  231.     while (string[stringPos] && string[stringPos] <= ' ')
  232.     {
  233.         stringPos++;
  234.     }
  235.     while (paramPos < paramCount && string[stringPos])
  236.     {
  237.         switch (format[formatPos++])
  238.         {
  239.             case '\0':
  240.             {
  241.                 return 0;
  242.             }
  243.             case 'i', 'd':
  244.             {
  245.                 new
  246.                     neg = 1,
  247.                     num = 0,
  248.                     ch = string[stringPos];
  249.                 if (ch == '-')
  250.                 {
  251.                     neg = -1;
  252.                     ch = string[++stringPos];
  253.                 }
  254.                 do
  255.                 {
  256.                     stringPos++;
  257.                     if ('0' <= ch <= '9')
  258.                     {
  259.                         num = (num * 10) + (ch - '0');
  260.                     }
  261.                     else
  262.                     {
  263.                         return -1;
  264.                     }
  265.                 }
  266.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  267.                 setarg(paramPos, 0, num * neg);
  268.             }
  269.             case 'h', 'x':
  270.             {
  271.                 new
  272.                     num = 0,
  273.                     ch = string[stringPos];
  274.                 do
  275.                 {
  276.                     stringPos++;
  277.                     switch (ch)
  278.                     {
  279.                         case 'x', 'X':
  280.                         {
  281.                             num = 0;
  282.                             continue;
  283.                         }
  284.                         case '0' .. '9':
  285.                         {
  286.                             num = (num << 4) | (ch - '0');
  287.                         }
  288.                         case 'a' .. 'f':
  289.                         {
  290.                             num = (num << 4) | (ch - ('a' - 10));
  291.                         }
  292.                         case 'A' .. 'F':
  293.                         {
  294.                             num = (num << 4) | (ch - ('A' - 10));
  295.                         }
  296.                         default:
  297.                         {
  298.                             return -1;
  299.                         }
  300.                     }
  301.                 }
  302.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  303.                 setarg(paramPos, 0, num);
  304.             }
  305.             case 'c':
  306.             {
  307.                 setarg(paramPos, 0, string[stringPos++]);
  308.             }
  309.             case 'f':
  310.             {
  311.  
  312.                 new changestr[16], changepos = 0, strpos = stringPos;
  313.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  314.                 {
  315.                     changestr[changepos++] = string[strpos++];
  316.                     }
  317.                 changestr[changepos] = '\0';
  318.                 setarg(paramPos,0,_:floatstr(changestr));
  319.             }
  320.             case 'p':
  321.             {
  322.                 delim = format[formatPos++];
  323.                 continue;
  324.             }
  325.             case '\'':
  326.             {
  327.                 new
  328.                     end = formatPos - 1,
  329.                     ch;
  330.                 while ((ch = format[++end]) && ch != '\'') {}
  331.                 if (!ch)
  332.                 {
  333.                     return -1;
  334.                 }
  335.                 format[end] = '\0';
  336.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  337.                 {
  338.                     if (format[end + 1])
  339.                     {
  340.                         return -1;
  341.                     }
  342.                     return 0;
  343.                 }
  344.                 format[end] = '\'';
  345.                 stringPos = ch + (end - formatPos);
  346.                 formatPos = end + 1;
  347.             }
  348.             case 'u':
  349.             {
  350.                 new
  351.                     end = stringPos - 1,
  352.                     id = 0,
  353.                     bool:num = true,
  354.                     ch;
  355.                 while ((ch = string[++end]) && ch != delim)
  356.                 {
  357.                     if (num)
  358.                     {
  359.                         if ('0' <= ch <= '9')
  360.                         {
  361.                             id = (id * 10) + (ch - '0');
  362.                         }
  363.                         else
  364.                         {
  365.                             num = false;
  366.                         }
  367.                     }
  368.                 }
  369.                 if (num && IsPlayerConnected(id))
  370.                 {
  371.                     setarg(paramPos, 0, id);
  372.                 }
  373.                 else
  374.                 {
  375.                     #if !defined foreach
  376.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  377.                         #define __SSCANF_FOREACH__
  378.                     #endif
  379.                     string[end] = '\0';
  380.                     num = false;
  381.                     new
  382.                         name[MAX_PLAYER_NAME];
  383.                     id = end - stringPos;
  384.                     foreach (Player, playerid)
  385.                     {
  386.                         GetPlayerName(playerid, name, sizeof (name));
  387.                         if (!strcmp(name, string[stringPos], true, id))
  388.                         {
  389.                             setarg(paramPos, 0, playerid);
  390.                             num = true;
  391.                             break;
  392.                         }
  393.                     }
  394.                     if (!num)
  395.                     {
  396.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  397.                     }
  398.                     string[end] = ch;
  399.                     #if defined __SSCANF_FOREACH__
  400.                         #undef foreach
  401.                         #undef __SSCANF_FOREACH__
  402.                     #endif
  403.                 }
  404.                 stringPos = end;
  405.             }
  406.             case 's', 'z':
  407.             {
  408.                 new
  409.                     i = 0,
  410.                     ch;
  411.                 if (format[formatPos])
  412.                 {
  413.                     while ((ch = string[stringPos++]) && ch != delim)
  414.                     {
  415.                         setarg(paramPos, i++, ch);
  416.                     }
  417.                     if (!i)
  418.                     {
  419.                         return -1;
  420.                     }
  421.                 }
  422.                 else
  423.                 {
  424.                     while ((ch = string[stringPos++]))
  425.                     {
  426.                         setarg(paramPos, i++, ch);
  427.                     }
  428.                 }
  429.                 stringPos--;
  430.                 setarg(paramPos, i, '\0');
  431.             }
  432.             default:
  433.             {
  434.                 continue;
  435.             }
  436.         }
  437.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  438.         {
  439.             stringPos++;
  440.         }
  441.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  442.         {
  443.             stringPos++;
  444.         }
  445.         paramPos++;
  446.     }
  447.     do
  448.     {
  449.         if ((delim = format[formatPos++]) > ' ')
  450.         {
  451.             if (delim == '\'')
  452.             {
  453.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  454.             }
  455.             else if (delim != 'z')
  456.             {
  457.                 return delim;
  458.             }
  459.         }
  460.     }
  461.     while (delim > ' ');
  462.     return 0;
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement