Advertisement
Guest User

Matthias

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