Guest User

SA-MP Teleportsystem v2

a guest
Feb 23rd, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.92 KB | None | 0 0
  1. #include <a_samp>
  2. #include <zcmd>
  3.  
  4. #define FILE_NAME "teleports.txt"
  5.  
  6. #define COLOR_GREEN 0x33AA33AA
  7. #define COLOR_RED 0xAA3333AA
  8.  
  9. new Line[32][144];
  10. new str[1024];
  11.  
  12. enum tp_Info
  13. {
  14.     Float:tp_x,
  15.     Float:tp_y,
  16.     Float:tp_z,
  17.     tp_interior,
  18.     tp_world,
  19.     tp_name[32],
  20.     bool:tp_created
  21. }
  22.  
  23. new TP[32][tp_Info];
  24.  
  25. public OnFilterScriptInit()
  26. {
  27.     print("Teleport-Filterscript gestartet.");
  28.    
  29.     LoadTeleports();
  30.     return 1;
  31. }
  32.  
  33. /* Sonstige Funktionen */
  34.  
  35. stock SetFileLine(filename[],ln,text[])
  36. {
  37.     if(!fexist(filename)) return 0;
  38.    
  39.     new File:file = fopen(filename,io_read), lines;
  40.    
  41.     while(fread(file,Line[lines]))
  42.     {
  43.         lines ++;
  44.     }
  45.    
  46.     fclose(file);
  47.    
  48.     if(!text[0])
  49.     {
  50.         Line[ln][0] = '\0';
  51.     }
  52.     else
  53.     {
  54.         format(Line[ln],144,text);
  55.     }
  56.    
  57.     file = fopen(filename,io_write);
  58.    
  59.     for(new i=0;i<lines;i++)
  60.     {
  61.         if(Line[i][0] != '\0')
  62.         {
  63.             fwrite(file,Line[i]);
  64.         }
  65.         else
  66.         {
  67.             continue;
  68.         }
  69.     }
  70.    
  71.     fclose(file);
  72.     return 1;
  73. }
  74.  
  75. stock sscanf(string[], format[], {Float,_}:...)
  76. {
  77.     #if defined isnull
  78.         if (isnull(string))
  79.     #else
  80.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  81.     #endif
  82.         {
  83.             return format[0];
  84.         }
  85.     #pragma tabsize 4
  86.     new
  87.         formatPos = 0,
  88.         stringPos = 0,
  89.         paramPos = 2,
  90.         paramCount = numargs(),
  91.         delim = ' ';
  92.     while (string[stringPos] && string[stringPos] <= ' ')
  93.     {
  94.         stringPos++;
  95.     }
  96.     while (paramPos < paramCount && string[stringPos])
  97.     {
  98.         switch (format[formatPos++])
  99.         {
  100.             case '\0':
  101.             {
  102.                 return 0;
  103.             }
  104.             case 'i', 'd':
  105.             {
  106.                 new
  107.                     neg = 1,
  108.                     num = 0,
  109.                     ch = string[stringPos];
  110.                 if (ch == '-')
  111.                 {
  112.                     neg = -1;
  113.                     ch = string[++stringPos];
  114.                 }
  115.                 do
  116.                 {
  117.                     stringPos++;
  118.                     if ('0' <= ch <= '9')
  119.                     {
  120.                         num = (num * 10) + (ch - '0');
  121.                     }
  122.                     else
  123.                     {
  124.                         return -1;
  125.                     }
  126.                 }
  127.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  128.                 setarg(paramPos, 0, num * neg);
  129.             }
  130.             case 'h', 'x':
  131.             {
  132.                 new
  133.                     num = 0,
  134.                     ch = string[stringPos];
  135.                 do
  136.                 {
  137.                     stringPos++;
  138.                     switch (ch)
  139.                     {
  140.                         case 'x', 'X':
  141.                         {
  142.                             num = 0;
  143.                             continue;
  144.                         }
  145.                         case '0' .. '9':
  146.                         {
  147.                             num = (num << 4) | (ch - '0');
  148.                         }
  149.                         case 'a' .. 'f':
  150.                         {
  151.                             num = (num << 4) | (ch - ('a' - 10));
  152.                         }
  153.                         case 'A' .. 'F':
  154.                         {
  155.                             num = (num << 4) | (ch - ('A' - 10));
  156.                         }
  157.                         default:
  158.                         {
  159.                             return -1;
  160.                         }
  161.                     }
  162.                 }
  163.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  164.                 setarg(paramPos, 0, num);
  165.             }
  166.             case 'c':
  167.             {
  168.                 setarg(paramPos, 0, string[stringPos++]);
  169.             }
  170.             case 'f':
  171.             {
  172.  
  173.                 new changestr[16], changepos = 0, strpos = stringPos;
  174.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  175.                 {
  176.                     changestr[changepos++] = string[strpos++];
  177.                     }
  178.                 changestr[changepos] = '\0';
  179.                 setarg(paramPos,0,_:floatstr(changestr));
  180.             }
  181.             case 'p':
  182.             {
  183.                 delim = format[formatPos++];
  184.                 continue;
  185.             }
  186.             case '\'':
  187.             {
  188.                 new
  189.                     end = formatPos - 1,
  190.                     ch;
  191.                 while ((ch = format[++end]) && ch != '\'') {}
  192.                 if (!ch)
  193.                 {
  194.                     return -1;
  195.                 }
  196.                 format[end] = '\0';
  197.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  198.                 {
  199.                     if (format[end + 1])
  200.                     {
  201.                         return -1;
  202.                     }
  203.                     return 0;
  204.                 }
  205.                 format[end] = '\'';
  206.                 stringPos = ch + (end - formatPos);
  207.                 formatPos = end + 1;
  208.             }
  209.             case 'u':
  210.             {
  211.                 new
  212.                     end = stringPos - 1,
  213.                     id = 0,
  214.                     bool:num = true,
  215.                     ch;
  216.                 while ((ch = string[++end]) && ch != delim)
  217.                 {
  218.                     if (num)
  219.                     {
  220.                         if ('0' <= ch <= '9')
  221.                         {
  222.                             id = (id * 10) + (ch - '0');
  223.                         }
  224.                         else
  225.                         {
  226.                             num = false;
  227.                         }
  228.                     }
  229.                 }
  230.                 if (num && IsPlayerConnected(id))
  231.                 {
  232.                     setarg(paramPos, 0, id);
  233.                 }
  234.                 else
  235.                 {
  236.                     #if !defined foreach
  237.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  238.                         #define __SSCANF_FOREACH__
  239.                     #endif
  240.                     string[end] = '\0';
  241.                     num = false;
  242.                     new
  243.                         name[MAX_PLAYER_NAME];
  244.                     id = end - stringPos;
  245.                     foreach (Player, playerid)
  246.                     {
  247.                         GetPlayerName(playerid, name, sizeof (name));
  248.                         if (!strcmp(name, string[stringPos], true, id))
  249.                         {
  250.                             setarg(paramPos, 0, playerid);
  251.                             num = true;
  252.                             break;
  253.                         }
  254.                     }
  255.                     if (!num)
  256.                     {
  257.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  258.                     }
  259.                     string[end] = ch;
  260.                     #if defined __SSCANF_FOREACH__
  261.                         #undef foreach
  262.                         #undef __SSCANF_FOREACH__
  263.                     #endif
  264.                 }
  265.                 stringPos = end;
  266.             }
  267.             case 's', 'z':
  268.             {
  269.                 new
  270.                     i = 0,
  271.                     ch;
  272.                 if (format[formatPos])
  273.                 {
  274.                     while ((ch = string[stringPos++]) && ch != delim)
  275.                     {
  276.                         setarg(paramPos, i++, ch);
  277.                     }
  278.                     if (!i)
  279.                     {
  280.                         return -1;
  281.                     }
  282.                 }
  283.                 else
  284.                 {
  285.                     while ((ch = string[stringPos++]))
  286.                     {
  287.                         setarg(paramPos, i++, ch);
  288.                     }
  289.                 }
  290.                 stringPos--;
  291.                 setarg(paramPos, i, '\0');
  292.             }
  293.             default:
  294.             {
  295.                 continue;
  296.             }
  297.         }
  298.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  299.         {
  300.             stringPos++;
  301.         }
  302.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  303.         {
  304.             stringPos++;
  305.         }
  306.         paramPos++;
  307.     }
  308.     do
  309.     {
  310.         if ((delim = format[formatPos++]) > ' ')
  311.         {
  312.             if (delim == '\'')
  313.             {
  314.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  315.             }
  316.             else if (delim != 'z')
  317.             {
  318.                 return delim;
  319.             }
  320.         }
  321.     }
  322.     while (delim > ' ');
  323.     return 0;
  324. }
  325.  
  326. /* Teleportfunktionen */
  327.  
  328. stock CreateTeleport(Float:x,Float:y,Float:z,interior,world,name[])
  329. {
  330.     new File:file = fopen(FILE_NAME,io_append);
  331.     format(str,144,"%f|%f|%f|%d|%d|%s\r\n",x,y,z,interior,world,name);
  332.     fwrite(file,str);
  333.     fclose(file);
  334.     LoadTeleports();
  335.     return 1;
  336. }
  337.  
  338. stock LoadTeleports()
  339. {
  340.     if(!fexist(FILE_NAME)) return 0;
  341.    
  342.     new File:file = fopen(FILE_NAME,io_read), index;
  343.    
  344.     while(fread(file,str))
  345.     {
  346.         sscanf(str,"p|fffdds",TP[index][tp_x],TP[index][tp_y],TP[index][tp_z],TP[index][tp_interior],TP[index][tp_world],TP[index][tp_name]);
  347.         TP[index][tp_created] = true;
  348.         index ++;
  349.     }
  350.    
  351.     if(index < 32)
  352.     {
  353.         for(new i=index;i<32;i++)
  354.         {
  355.             TP[i][tp_created] = false;
  356.         }
  357.     }
  358.    
  359.     fclose(file);
  360.     printf("%d Teleport(s) geladen.",index);
  361.     return index;
  362. }
  363.  
  364. stock DeleteTeleport(id)
  365. {
  366.     if(TP[id][tp_created] == false) return 0;
  367.    
  368.     SetFileLine(FILE_NAME,id,"");
  369.     LoadTeleports();
  370.     return 1;
  371. }
  372.  
  373. /* Befehle */
  374.  
  375. COMMAND:teleports(playerid,params[])
  376. {
  377.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,COLOR_RED,"Du bist kein RCON-Admin!");
  378.  
  379.     ShowPlayerDialog(playerid,1336,DIALOG_STYLE_LIST,"Teleportverwaltung","{33AA33}Teleports anzeigen\n{33AA33}Teleports löschen\n{33AA33}Teleports erstellen","Okay!","Abbrechen");
  380.     return 1;
  381. }
  382.  
  383. /* Callbacks */
  384.  
  385. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  386. {
  387.     if(response)
  388.     {
  389.         switch(dialogid)
  390.         {
  391.             case 1336:
  392.             {
  393.                 switch(listitem)
  394.                 {
  395.                     case 0:
  396.                     {
  397.                         if(LoadTeleports() == 0) return SendClientMessage(playerid,COLOR_RED,"Bisher wurden keine Teleports erstellt!");
  398.  
  399.                         for(new i=0;i<32;i++)
  400.                         {
  401.                             if(TP[i][tp_created] == true)
  402.                             {
  403.                                 format(str,1024,"%s\n{33AA33}%s",str,TP[i][tp_name]);
  404.                             }
  405.                         }
  406.  
  407.                         ShowPlayerDialog(playerid,1337,DIALOG_STYLE_LIST,"Teleports",str,"Los!","Zurück");
  408.                     }
  409.                     case 1:
  410.                     {
  411.                         if(LoadTeleports() == 0) return SendClientMessage(playerid,COLOR_RED,"Bisher wurden keine Teleports erstellt!");
  412.  
  413.                         for(new i=0;i<32;i++)
  414.                         {
  415.                             if(TP[i][tp_created] == true)
  416.                             {
  417.                                 format(str,1024,"%s\n{33AA33}%s",str,TP[i][tp_name]);
  418.                             }
  419.                         }
  420.  
  421.                         ShowPlayerDialog(playerid,1338,DIALOG_STYLE_LIST,"Teleports löschen",str,"Löschen!","Zurück");
  422.                     }
  423.                     case 2:
  424.                     {
  425.                         ShowPlayerDialog(playerid,1339,DIALOG_STYLE_INPUT,"Teleports erstellen","Gib einen Namen für diesen Ort ein:","Erstellen!","Zurück");
  426.                     }
  427.                 }
  428.             }
  429.             case 1337:
  430.             {
  431.                 SetPlayerPos(playerid,TP[listitem][tp_x],TP[listitem][tp_y],TP[listitem][tp_z]);
  432.                 SetPlayerInterior(playerid,TP[listitem][tp_interior]);
  433.                 SetPlayerVirtualWorld(playerid,TP[listitem][tp_world]);
  434.                 format(str,144,"Du hast dich zum Teleport \"%s\" teleportiert.",TP[listitem][tp_name]);
  435.                 SendClientMessage(playerid,COLOR_GREEN,str);
  436.             }
  437.             case 1338:
  438.             {
  439.                 format(str,144,"Teleport \"%s\" gelöscht.",TP[listitem][tp_name]);
  440.                 SendClientMessage(playerid,COLOR_GREEN,str);
  441.                 DeleteTeleport(listitem);
  442.             }
  443.             case 1339:
  444.             {
  445.                 if(!inputtext[0]) return SendClientMessage(playerid,COLOR_RED,"Du musst einen Namen eingeben!");
  446.                 if(strlen(inputtext) > 32) return SendClientMessage(playerid,COLOR_RED,"Der Name darf nicht mehr als 32 Zeichen lang sein!");
  447.                
  448.                 new Float:x, Float:y, Float:z;
  449.                 GetPlayerPos(playerid,x,y,z);
  450.                 CreateTeleport(x,y,z,GetPlayerInterior(playerid),GetPlayerVirtualWorld(playerid),inputtext);
  451.                 format(str,144,"Teleport \"%s\" erstellt. Tippe /teleports um die Teleportverwaltung aufzurufen.",inputtext);
  452.                 SendClientMessage(playerid,COLOR_GREEN,str);
  453.             }
  454.         }
  455.     }
  456.     else if(!response)
  457.     {
  458.         if(dialogid == 1337 || dialogid == 1338 || dialogid == 1339)
  459.         {
  460.             ShowPlayerDialog(playerid,1336,DIALOG_STYLE_LIST,"Teleportverwaltung","{33AA33}Teleports anzeigen\n{33AA33}Teleports löschen\n{33AA33}Teleports erstellen","Okay!","Abbrechen");
  461.         }
  462.     }
  463.     return 1;
  464. }
Advertisement
Add Comment
Please, Sign In to add comment