Advertisement
Guest User

Matthias

a guest
May 26th, 2009
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.26 KB | None | 0 0
  1. /* ======================================================================================//
  2.  
  3.     DO NOT edit and re-publish this without my permission. I take credits for all of this.
  4.    
  5.     Thanks to:
  6.     - DracoBlue (for dcmd)
  7.     - The creator or sscanf
  8.     - The creator of strtok
  9.     - WeirdoSport for a real small bug fix :+
  10.    
  11.     Any suggestions / bugs? Report them at the topic at forum.sa-mp.com!
  12.     Have fun editing objects!
  13.    
  14. // =====================================================================================*/
  15.  
  16. #include <a_samp>
  17.  
  18. #define COLOR_BRIGHTRED 0xFF0000AA
  19. #define COLOR_GREEN 0x33AA33AA
  20. #define COLOR_YELLOW 0xFFFF00AA
  21. #define COLOR_WHITE 0xFFFFFFAA
  22. #define COLOR_BLUE 0x33CCFFAA
  23.  
  24. #define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  25.  
  26. new EditingObject[MAX_PLAYERS];
  27. new StickedToObject[MAX_PLAYERS];
  28.  
  29. new newobj;
  30. new Object;
  31. new Float:oX;
  32. new Float:oY;
  33. new Float:oZ;
  34. new Float:xR;
  35. new Float:yR;
  36. new Float:zR;
  37.  
  38. public OnFilterScriptInit()
  39. {
  40.     print("\n============================================================");
  41.     print(" [FS] Ingame Object Editor by Matthias aka GTA_Rules loaded.\r");
  42.     print("============================================================\n");
  43.     return 1;
  44. }
  45.  
  46. public OnFilterScriptExit()
  47. {
  48.     print("\n==============================================================");
  49.     print(" [FS] Ingame Object Editor by Matthias aka GTA_Rules unloaded.");
  50.     print("==============================================================\n");
  51.     return 1;
  52. }
  53.  
  54. public OnPlayerCommandText(playerid, cmdtext[])
  55. {
  56.     new cmd[256], tmp[256];
  57.     new idx;
  58.     cmd = strtok(cmdtext, idx);
  59.     tmp = strtok(cmdtext, idx);
  60.  
  61.     dcmd(ohelp,5, cmdtext);
  62.     dcmd(stick,5, cmdtext);
  63.     dcmd(abort,5, cmdtext);
  64.     dcmd(moveup,6,cmdtext);
  65.     dcmd(objects,7, cmdtext);
  66.     dcmd(unstick,7, cmdtext);
  67.     dcmd(movedown,8, cmdtext);
  68.     dcmd(moveleft,8, cmdtext);
  69.     dcmd(objecttp,8, cmdtext);
  70.     dcmd(moveright,9, cmdtext);
  71.     dcmd(addobject,9, cmdtext);
  72.     dcmd(saveobject,10,cmdtext);
  73.     dcmd(moveforwards,12, cmdtext);
  74.     dcmd(movebackwards,13, cmdtext);
  75.    
  76.     return 0;
  77. }
  78.     dcmd_addobject(playerid,params[])
  79.     {
  80.         if(EditingObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You're already editing an object! Use /saveobject to save it! Or use /abort to cancel this object!");
  81.  
  82.         new Float:X, Float:Y, Float:Z, str[256];
  83.         if(sscanf(params, "d", newobj)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /addobject <objectid>");
  84.         if(newobj > 10000 || newobj < 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: Invalid objectid!");
  85.  
  86.         GetPlayerPos(playerid, X, Y, Z);
  87.         EditingObject[playerid] = 1;
  88.         Object = CreateObject(newobj, X+5, Y+5, Z, 0, 0, 0);
  89.         format(str, sizeof str, "* Succesfully placed object: %d on the coordinates: %.2f, %.2f, %.2f!", newobj, X, Y, Z);
  90.         SendClientMessage(playerid, COLOR_BLUE, str);
  91.         oX = X+5;
  92.         oY = Y+5;
  93.         oZ = Z;
  94.         return 1;
  95.     }
  96.    
  97.  
  98.     dcmd_saveobject(playerid,params[])
  99.     {
  100.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  101.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  102.        
  103.         new string[256], Note[128];
  104.         if(sscanf(params, "s", Note)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /saveobject <note>");
  105.        
  106.         new File:pos = fopen("objects.txt", io_append);
  107.         format(string, sizeof string, "CreateObject(%d, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f); // %s\r\n", newobj, oX, oY, oZ, xR, yR, zR, Note);
  108.         fwrite(pos, string), fclose(pos);
  109.  
  110.         SendClientMessage(playerid, COLOR_GREEN, "Your object has been saved in objects.txt");
  111.         EditingObject[playerid] = 0;
  112.         return 1;
  113.     }
  114.    
  115.     dcmd_abort(playerid,params[])
  116.     {
  117.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  118.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  119.  
  120.         SendClientMessage(playerid, COLOR_BLUE, "* Aborted.");
  121.         EditingObject[playerid] = 0;
  122.        
  123.         DestroyObject(Object);
  124.         #pragma unused params
  125.         return 1;
  126.     }
  127.    
  128.     dcmd_objecttp(playerid,params[])
  129.     {
  130.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  131.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  132.  
  133.         SendClientMessage(playerid, COLOR_GREEN, "You have been teleported to your object!");
  134.         SetPlayerPos(playerid, oX +2, oY +2, oZ);
  135.         #pragma unused params
  136.         return 1;
  137.     }
  138.  
  139.     dcmd_moveup(playerid, params[])
  140.     {
  141.         new ZCoord;
  142.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  143.         if(sscanf(params, "d", ZCoord)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /moveup <Z coord>");
  144.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  145.        
  146.         oZ = oZ + ZCoord;
  147.         SetObjectPos(Object, oX, oY, oZ);
  148.         SendClientMessage(playerid, COLOR_GREEN, "Your object has been succesfully moved up.");
  149.         return 1;
  150.     }
  151.    
  152.     dcmd_movedown(playerid, params[])
  153.     {
  154.         new ZCoord;
  155.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  156.         if(sscanf(params, "d", ZCoord)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /movedown <Z coord>");
  157.         if(ZCoord > -1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: Your input value must be smaller than 0!");
  158.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  159.  
  160.         oZ = oZ + ZCoord;
  161.         SetObjectPos(Object, oX, oY, oZ);
  162.         SendClientMessage(playerid, COLOR_GREEN, "Your object has been succesfully moved downwards.");
  163.         return 1;
  164.     }
  165.    
  166.     dcmd_moveleft(playerid, params[])
  167.     {
  168.         new Coord;
  169.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  170.         if(sscanf(params, "d", Coord)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /moveleft <coord>");
  171.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  172.  
  173.         oX = oX + Coord;
  174.         SetObjectPos(Object, oX, oY, oZ);
  175.         SendClientMessage(playerid, COLOR_GREEN, "Your object has been succesfully moved to the left.");
  176.         return 1;
  177.     }
  178.    
  179.     dcmd_moveforwards(playerid, params[])
  180.     {
  181.         new Coord;
  182.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  183.         if(sscanf(params, "d", Coord)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /movelefty <coord>");
  184.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  185.  
  186.         oY = oY + Coord;
  187.         SetObjectPos(Object, oX, oY, oZ);
  188.         SendClientMessage(playerid, COLOR_GREEN, "Your object has been succesfully moved to the left.");
  189.         return 1;
  190.     }
  191.  
  192.     dcmd_moveright(playerid, params[])
  193.     {
  194.         new Coord;
  195.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  196.         if(sscanf(params, "d", Coord)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /moveright <coord>");
  197.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  198.  
  199.         oX = oX - Coord;
  200.         SetObjectPos(Object, oX, oY, oZ);
  201.         SendClientMessage(playerid, COLOR_GREEN, "Your object has been succesfully moved to the right.");
  202.         return 1;
  203.     }
  204.    
  205.     dcmd_movebackwards(playerid, params[])
  206.     {
  207.         new Coord;
  208.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  209.         if(sscanf(params, "d", Coord)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /moverighty <coord>");
  210.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You can't use commands when you are sticked to an object! Use /unstick first!");
  211.  
  212.         oY = oY - Coord;
  213.         SetObjectPos(Object, oX, oY, oZ);
  214.         SendClientMessage(playerid, COLOR_GREEN, "Your object has been succesfully moved to the right.");
  215.         return 1;
  216.     }
  217.    
  218.     dcmd_stick(playerid, params[])
  219.     {
  220.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  221.         if(StickedToObject[playerid] == 1) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You are already sticked to the object!");
  222.        
  223.         StickedToObject[playerid] = 1;
  224.         AttachObjectToPlayer( Object, playerid, 2.0, 2.0, 0.0, 0, 1.5, 2 );
  225.        
  226.         #pragma unused params
  227.         return 1;
  228.     }
  229.    
  230.     dcmd_unstick(playerid, params[])
  231.     {
  232.         new Float:Pxx, Float:Pyy, Float:Pzz;
  233.         if(EditingObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't editing any objects!");
  234.         if(StickedToObject[playerid] == 0) return SendClientMessage(playerid, COLOR_BRIGHTRED, "ERROR: You aren't sticked to an object!");
  235.  
  236.         GetPlayerPos(playerid, Pxx, Pyy, Pzz);
  237.         DestroyObject(Object);
  238.         oX = Pxx + 0.1;
  239.         oY = Pyy + 0.1;
  240.         oZ = Pzz + 0.01;
  241.         Object = CreateObject(newobj, oX, oY, oZ, xR, yR, zR);
  242.         StickedToObject[playerid] = 0;
  243.         SendClientMessage(playerid, COLOR_GREEN, "* You have been released from your object!");
  244.         SetObjectPos(Object, oX, oY, oZ);
  245.         SetObjectRot(Object, xR, yR, zR);
  246.         #pragma unused params
  247.         return 1;
  248.     }
  249.    
  250.     dcmd_ohelp(playerid, params[])
  251.     {
  252.         #pragma unused params
  253.         SendClientMessage(playerid, COLOR_YELLOW, "* Commands:");
  254.         SendClientMessage(playerid, COLOR_GREEN, "/moveup, /movedown, /moveleft, /moveright, /moveforwards, /movebackwards");
  255.         SendClientMessage(playerid, COLOR_GREEN, "/addobject, /saveobject, /abort, /stick, /unstick /objects /objecttp");
  256.         return 1;
  257.     }
  258.    
  259.     dcmd_objects(playerid, params[])
  260.     {
  261.         #pragma unused params
  262.         SendClientMessage(playerid, COLOR_YELLOW, "Here are a few usefull weapon ID's.");
  263.         SendClientMessage(playerid, COLOR_GREEN, "321-373: Weapons");
  264.         SendClientMessage(playerid, COLOR_GREEN, "966 - 998: Fences");
  265.         SendClientMessage(playerid, COLOR_GREEN, "3607 - 3609: Houses");
  266.         SendClientMessage(playerid, COLOR_GREEN, "1420 - 1663: Some kind of garbage");
  267.         return 1;
  268.     }
  269.  
  270.  
  271.    
  272.        
  273.  
  274. // ===================================================================================================================================//
  275.  
  276. strtok(const string[], &index)
  277. {
  278.         new length = strlen(string);
  279.         while ((index < length) && (string[index] <= ' '))
  280.         {
  281.                 index++;
  282.         }
  283.  
  284.         new offset = index;
  285.         new result[20];
  286.         while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  287.         {
  288.                 result[index - offset] = string[index];
  289.                 index++;
  290.         }
  291.         result[index - offset] = EOS;
  292.         return result;
  293. }
  294.  
  295.  
  296. stock sscanf(string[], format[], {Float,_}:...)
  297. {
  298.     #if defined isnull
  299.         if (isnull(string))
  300.     #else
  301.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  302.     #endif
  303.         {
  304.             return format[0];
  305.         }
  306.     #pragma tabsize 4
  307.     new
  308.         formatPos = 0,
  309.         stringPos = 0,
  310.         paramPos = 2,
  311.         paramCount = numargs(),
  312.         delim = ' ';
  313.     while (string[stringPos] && string[stringPos] <= ' ')
  314.     {
  315.         stringPos++;
  316.     }
  317.     while (paramPos < paramCount && string[stringPos])
  318.     {
  319.         switch (format[formatPos++])
  320.         {
  321.             case '\0':
  322.             {
  323.                 return 0;
  324.             }
  325.             case 'i', 'd':
  326.             {
  327.                 new
  328.                     neg = 1,
  329.                     num = 0,
  330.                     ch = string[stringPos];
  331.                 if (ch == '-')
  332.                 {
  333.                     neg = -1;
  334.                     ch = string[++stringPos];
  335.                 }
  336.                 do
  337.                 {
  338.                     stringPos++;
  339.                     if ('0' <= ch <= '9')
  340.                     {
  341.                         num = (num * 10) + (ch - '0');
  342.                     }
  343.                     else
  344.                     {
  345.                         return -1;
  346.                     }
  347.                 }
  348.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  349.                 setarg(paramPos, 0, num * neg);
  350.             }
  351.             case 'h', 'x':
  352.             {
  353.                 new
  354.                     num = 0,
  355.                     ch = string[stringPos];
  356.                 do
  357.                 {
  358.                     stringPos++;
  359.                     switch (ch)
  360.                     {
  361.                         case 'x', 'X':
  362.                         {
  363.                             num = 0;
  364.                             continue;
  365.                         }
  366.                         case '0' .. '9':
  367.                         {
  368.                             num = (num << 4) | (ch - '0');
  369.                         }
  370.                         case 'a' .. 'f':
  371.                         {
  372.                             num = (num << 4) | (ch - ('a' - 10));
  373.                         }
  374.                         case 'A' .. 'F':
  375.                         {
  376.                             num = (num << 4) | (ch - ('A' - 10));
  377.                         }
  378.                         default:
  379.                         {
  380.                             return -1;
  381.                         }
  382.                     }
  383.                 }
  384.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  385.                 setarg(paramPos, 0, num);
  386.             }
  387.             case 'c':
  388.             {
  389.                 setarg(paramPos, 0, string[stringPos++]);
  390.             }
  391.             case 'f':
  392.             {
  393.                 setarg(paramPos, 0, _:floatstr(string[stringPos]));
  394.             }
  395.             case 'p':
  396.             {
  397.                 delim = format[formatPos++];
  398.                 continue;
  399.             }
  400.             case '\'':
  401.             {
  402.                 new
  403.                     end = formatPos - 1,
  404.                     ch;
  405.                 while ((ch = format[++end]) && ch != '\'') {}
  406.                 if (!ch)
  407.                 {
  408.                     return -1;
  409.                 }
  410.                 format[end] = '\0';
  411.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  412.                 {
  413.                     if (format[end + 1])
  414.                     {
  415.                         return -1;
  416.                     }
  417.                     return 0;
  418.                 }
  419.                 format[end] = '\'';
  420.                 stringPos = ch + (end - formatPos);
  421.                 formatPos = end + 1;
  422.             }
  423.             case 'u':
  424.             {
  425.                 new
  426.                     end = stringPos - 1,
  427.                     id = 0,
  428.                     bool:num = true,
  429.                     ch;
  430.                 while ((ch = string[++end]) && ch != delim)
  431.                 {
  432.                     if (num)
  433.                     {
  434.                         if ('0' <= ch <= '9')
  435.                         {
  436.                             id = (id * 10) + (ch - '0');
  437.                         }
  438.                         else
  439.                         {
  440.                             num = false;
  441.                         }
  442.                     }
  443.                 }
  444.                 if (num && IsPlayerConnected(id))
  445.                 {
  446.                     setarg(paramPos, 0, id);
  447.                 }
  448.                 else
  449.                 {
  450.                     #if !defined foreach
  451.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  452.                         #define __SSCANF_FOREACH__
  453.                     #endif
  454.                     string[end] = '\0';
  455.                     num = false;
  456.                     new
  457.                         name[MAX_PLAYER_NAME];
  458.                     id = end - stringPos;
  459.                     foreach (Player, playerid)
  460.                     {
  461.                         GetPlayerName(playerid, name, sizeof (name));
  462.                         if (!strcmp(name, string[stringPos], true, id))
  463.                         {
  464.                             setarg(paramPos, 0, playerid);
  465.                             num = true;
  466.                             break;
  467.                         }
  468.                     }
  469.                     if (!num)
  470.                     {
  471.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  472.                     }
  473.                     string[end] = ch;
  474.                     #if defined __SSCANF_FOREACH__
  475.                         #undef foreach
  476.                         #undef __SSCANF_FOREACH__
  477.                     #endif
  478.                 }
  479.                 stringPos = end;
  480.             }
  481.             case 's', 'z':
  482.             {
  483.                 new
  484.                     i = 0,
  485.                     ch;
  486.                 if (format[formatPos])
  487.                 {
  488.                     while ((ch = string[stringPos++]) && ch != delim)
  489.                     {
  490.                         setarg(paramPos, i++, ch);
  491.                     }
  492.                     if (!i)
  493.                     {
  494.                         return -1;
  495.                     }
  496.                 }
  497.                 else
  498.                 {
  499.                     while ((ch = string[stringPos++]))
  500.                     {
  501.                         setarg(paramPos, i++, ch);
  502.                     }
  503.                 }
  504.                 stringPos--;
  505.                 setarg(paramPos, i, '\0');
  506.             }
  507.             default:
  508.             {
  509.                 continue;
  510.             }
  511.         }
  512.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  513.         {
  514.             stringPos++;
  515.         }
  516.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  517.         {
  518.             stringPos++;
  519.         }
  520.         paramPos++;
  521.     }
  522.     do
  523.     {
  524.         if ((delim = format[formatPos++]) > ' ')
  525.         {
  526.             if (delim == '\'')
  527.             {
  528.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  529.             }
  530.             else if (delim != 'z')
  531.             {
  532.                 return delim;
  533.             }
  534.         }
  535.     }
  536.     while (delim > ' ');
  537.     return 0;
  538. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement