jlalt

gate

Jan 17th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.16 KB | None | 0 0
  1. #include a_samp
  2. #include streamer
  3. #include zcmd
  4.  
  5. #define red                     0xFF0000AA
  6. #define gateo                   980
  7. #define MAX_GATES               500
  8. #define _DB_                  "gate.db"
  9.  
  10. enum ginfo
  11. {
  12.     GID,
  13.     Opened,
  14.     Created,
  15.     GMove,
  16.     Float:GX,
  17.     Float:GY,
  18.     Float:GZ,
  19.     Float:GA1,
  20.     GPassword[32],
  21.     Placer[40],
  22.     PlaceDate[40],
  23. };
  24. new GateInfo[MAX_GATES][ginfo],cgate = 0,DB:Database;
  25.  
  26. public OnFilterScriptInit() {
  27.    Database = db_open(_DB_);
  28.    db_query(Database,
  29.    "CREATE TABLE IF NOT EXISTS `gates` (`ID` NUMBERIC,`Placer` TEXT, `PlaceDate` TEXT, `X` TEXT, `Y` TEXT, `Z` TEXT, `A` TEXT, `Password` TEXT )");
  30.    new Query[250],DBResult:Result,gateid = 0;
  31.    for(new i = 0; i < MAX_GATES; i++) {
  32.      format(Query, sizeof(Query),"SELECT * FROM gates WHERE ID = %d",i);
  33.      Result = db_query(Database, Query);
  34.      if(db_num_rows(Result)) {
  35.        format(Query, sizeof(Query),"UPDATE `gates` SET `ID` = '%d' WHERE `ID` = '%d'",gateid,i);
  36.        db_query(Database, Query);
  37.        gateid ++;
  38.      }
  39.    }
  40.    for(new i = 0; i < MAX_GATES; i++) {
  41.      format(Query, sizeof(Query),"SELECT * FROM gates WHERE ID = %d",i);
  42.      Result = db_query(Database, Query);
  43.      if(db_num_rows(Result)) {
  44.        new Save[4][100],ID1[10];
  45.        db_get_field_assoc(Result, "ID", ID1, 10);
  46.        GateInfo[i][GID] = strval(ID1);
  47.        db_get_field_assoc(Result, "X", Save[0], 100);
  48.        db_get_field_assoc(Result, "Y", Save[1], 100);
  49.        db_get_field_assoc(Result, "Z", Save[2], 100);
  50.        db_get_field_assoc(Result, "A", Save[3], 100);
  51.        db_get_field_assoc(Result, "Password",GateInfo[GateInfo[i][GID]][GPassword], 32);
  52.        db_get_field_assoc(Result, "Placer", GateInfo[GateInfo[i][GID]][Placer], 40);
  53.        db_get_field_assoc(Result, "PlaceDate", GateInfo[GateInfo[i][GID]][PlaceDate], 40);
  54.        GateInfo[GateInfo[i][GID]][Opened] = 0;
  55.        GateInfo[GateInfo[i][GID]][Created] = 1;
  56.        GateInfo[GateInfo[i][GID]][GX] = floatstr(Save[0]);
  57.        GateInfo[GateInfo[i][GID]][GY] = floatstr(Save[1]);
  58.        GateInfo[GateInfo[i][GID]][GZ] = floatstr(Save[2]);
  59.        GateInfo[GateInfo[i][GID]][GA1] = floatstr(Save[3]);
  60.        GateInfo[GateInfo[i][GID]][GMove] = CreateDynamicObject(gateo, GateInfo[GateInfo[i][GID]][GX], GateInfo[GateInfo[i][GID]][GY], GateInfo[GateInfo[i][GID]][GZ], 0, 0, GateInfo[GateInfo[i][GID]][GA1]);
  61.        cgate = strval(ID1)+1;
  62.      }
  63.    }
  64.    db_free_result(Result);
  65.    printf("%d Gates successfully loaded and created!",gateid);
  66. }
  67.  
  68. CMD:gopen(playerid,params[]) {
  69.     new i = NearGate(playerid);
  70.     if(i == -255) return SendClientMessage(playerid, red, "ERROR: You should be near gate to open it");
  71.     if(GateInfo[GateInfo[i][GID]][Opened] == 1) return SendClientMessage(playerid, red, "ERROR: This Gate is Already Opened");
  72.     new gPassword[32];
  73.     if(sscanf(params,"s",gPassword)) return SendClientMessage(playerid, red, "Usage: /gopen [password]");
  74.     if(strcmp(gPassword,GateInfo[GateInfo[i][GID]][GPassword],false) == 0) {
  75.         MoveDynamicObject(GateInfo[GateInfo[i][GID]][GMove], GateInfo[GateInfo[i][GID]][GX], GateInfo[GateInfo[i][GID]][GY], GateInfo[GateInfo[i][GID]][GZ]-5.4, 3);
  76.         SendClientMessage(playerid, -1, "You opened the gate");
  77.         GateInfo[GateInfo[i][GID]][Opened] = 1;
  78.     } else return SendClientMessage(playerid, red, "ERROR: Invalid Password!");
  79.     return 1;
  80. }
  81. CMD:gclose(playerid,params[]) {
  82.     new i = NearGate(playerid);
  83.     if(i == -255) return SendClientMessage(playerid, red, "ERROR: You should be near gate to open it");
  84.     if(GateInfo[GateInfo[i][GID]][Opened] == 0) return SendClientMessage(playerid, red, "ERROR: This Gate is Already Closed");
  85.     new gPassword[32];
  86.     if(sscanf(params,"s",gPassword)) return SendClientMessage(playerid, red, "Usage: /gclose [password]");
  87.     if(strcmp(gPassword,GateInfo[GateInfo[i][GID]][GPassword],false) == 0) {
  88.         MoveDynamicObject(GateInfo[GateInfo[i][GID]][GMove], GateInfo[GateInfo[i][GID]][GX], GateInfo[GateInfo[i][GID]][GY], GateInfo[GateInfo[i][GID]][GZ], 3);
  89.         SendClientMessage(playerid, -1, "You closed the gate");
  90.         GateInfo[GateInfo[i][GID]][Opened] = 0;
  91.     } else return SendClientMessage(playerid, red, "ERROR: Invalid Password!");
  92.     return 1;
  93. }
  94. CMD:gateinfo(playerid,params[]) {
  95.     if(IsPlayerAdmin(playerid)) {
  96.       new i = NearGate(playerid);
  97.       if(i == -255) return SendClientMessage(playerid, red, "ERROR: You should be near gate to get info");
  98.       new string[128];
  99.       format(string,sizeof string,"Gate Info ( Placed by: %s | Password: %s | Place Date: %s )",GateInfo[GateInfo[i][GID]][Placer],GateInfo[GateInfo[i][GID]][GPassword],
  100.       GateInfo[GateInfo[i][GID]][PlaceDate]);
  101.       SendClientMessage(playerid, -1, string);
  102.     } else return SendClientMessage(playerid, red, "You need to be level 1 and above to use this command");
  103.     return 1;
  104. }
  105. CMD:removegate(playerid,params[]) {
  106.     if(IsPlayerAdmin(playerid)) {
  107.         new i = NearGate(playerid);
  108.         if(i == -255) return SendClientMessage(playerid, red, "ERROR: You should be near gate to get info");
  109.         GateInfo[GateInfo[i][GID]][Created] = 0;
  110.         new Query[128],string[128];
  111.         format(Query, sizeof(Query),"DELETE FROM `gates` WHERE `ID` = '%d'", GateInfo[i][GID]);
  112.         db_query(Database, Query);
  113.         DestroyDynamicObject(GateInfo[GateInfo[i][GID]][GMove]);
  114.         format(string,sizeof string,"You have removed this gate [ ID: %d ]",GateInfo[i][GID]);
  115.         return SendClientMessage(playerid, -1, string);
  116.     } else return SendClientMessage(playerid, red, "ERROR: You need to be rcon to use this command");
  117. }
  118. CMD:changegpass(playerid,params[]) {
  119.     if(IsPlayerAdmin(playerid)) {
  120.       new i = NearGate(playerid);
  121.       if(i == -255) return SendClientMessage(playerid, red, "ERROR: You should be near gate to change password");
  122.       new gpassword[32];
  123.       if(sscanf(params,"s",gpassword)) return SendClientMessage(playerid, red, "Usage: /changegpass [new password]");
  124.       if(strlen(gpassword) > 32 || strlen(gpassword) < 4) return SendClientMessage(playerid, red, "ERROR: Gate Password should be between 4-32 words");
  125.       new string[128],Query[128];
  126.       GateInfo[GateInfo[i][GID]][GPassword] = gpassword;
  127.       format(Query, sizeof(Query),"UPDATE `gates` SET `password` = '%s' WHERE `ID` = '%d'", GateInfo[GateInfo[i][GID]][GPassword],GateInfo[i][GID]);
  128.       db_query(Database, Query);
  129.       format(string,sizeof string,"You have changed this gate password, New Password is: %s",GateInfo[GateInfo[i][GID]][GPassword]);
  130.       SendClientMessage(playerid, -1, string);
  131.     } else return SendClientMessage(playerid, red, "You need to be level 7 to use this command");
  132.     return 1;
  133. }
  134. CMD:placegate(playerid,params[]) {
  135.     if(IsPlayerAdmin(playerid)) {
  136.        new gpassword[32];
  137.        if(sscanf(params,"s",gpassword)) return SendClientMessage(playerid, red, "Usage: /placegate [password]");
  138.        if(strlen(gpassword) > 32 || strlen(gpassword) < 4) return SendClientMessage(playerid, red, "ERROR: Gate Password should be between 4-32 words");
  139.        new Float:X,Float:Y,Float:Z,Float:A;
  140.        GetPlayerPos(playerid, X,Y,Z);
  141.        GetPlayerFacingAngle(playerid,A);
  142.        new Query[400],Year,Month,Day,string[128];
  143.        getdate(Year,Month,Day);
  144.        format(string,sizeof string,"%d-%d-%d",Day,Month,Year);
  145.        format(Query, sizeof(Query),"INSERT INTO `gates`(`ID`,`X`,`Y`,`Z`,`A`,`Password`,`Placer`,`PlaceDate`) VALUES ('%d','%f','%f','%f','%f','%s','%s','%s')",
  146.        cgate,X,Y,Z+1.5,A,gpassword,pName(playerid),string);
  147.        db_query(Database, Query);
  148.        GateInfo[cgate][GX] = X;
  149.        GateInfo[cgate][GY] = Y;
  150.        GateInfo[cgate][GZ] = Z+1.5;
  151.        GateInfo[cgate][GA1] = A;
  152.        GateInfo[cgate][GPassword] = gpassword;
  153.        GateInfo[cgate][GID] = cgate;
  154.        GateInfo[cgate][Opened] = 0;
  155.        GateInfo[cgate][Created] = 1;
  156.        GateInfo[cgate][GMove] = CreateDynamicObject(gateo, X, Y, Z+1.5, 0, 0, A, -1, -1, -1,STREAMER_OBJECT_SD,STREAMER_OBJECT_DD);
  157.        format(GateInfo[cgate][Placer],MAX_PLAYER_NAME+1,"%s",pName(playerid));
  158.        format(GateInfo[cgate][PlaceDate],32,"%s",string);
  159.        SendClientMessage(playerid, -1, "You succesfully created a movable gate. Use /gopen or /gclose." );
  160.        cgate ++;
  161.        return 1;
  162.     } else return SendClientMessage(playerid, red, "ERROR: You need to be rcon to use this command");
  163. }
  164.  
  165. NearGate(playerid) {
  166.     for(new i = 0; i < MAX_GATES; i++) {
  167.         if(GateInfo[i][Created] == 1) {
  168.             if(IsPlayerInRangeOfPoint(playerid, 10.0, GateInfo[GateInfo[i][GID]][GX], GateInfo[GateInfo[i][GID]][GY], GateInfo[GateInfo[i][GID]][GZ])) {
  169.             return GateInfo[i][GID];
  170.             }
  171.  
  172.         }
  173.     }
  174.     return -255;
  175. }
  176.  
  177. sscanf(string[], format[], {Float,_}:...)
  178. {
  179.     #if defined isnull
  180.         if (isnull(string))
  181.     #else
  182.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  183.     #endif
  184.         {
  185.             return format[0];
  186.         }
  187.     #pragma tabsize 4
  188.     new
  189.         formatPos = 0,
  190.         stringPos = 0,
  191.         paramPos = 2,
  192.         paramCount = numargs(),
  193.         delim = ' ';
  194.     while (string[stringPos] && string[stringPos] <= ' ')
  195.     {
  196.         stringPos++;
  197.     }
  198.     while (paramPos < paramCount && string[stringPos])
  199.     {
  200.         switch (format[formatPos++])
  201.         {
  202.             case '\0':
  203.             {
  204.                 return 0;
  205.             }
  206.             case 'i', 'd':
  207.             {
  208.                 new
  209.                     neg = 1,
  210.                     num = 0,
  211.                     ch = string[stringPos];
  212.                 if (ch == '-')
  213.                 {
  214.                     neg = -1;
  215.                     ch = string[++stringPos];
  216.                 }
  217.                 do
  218.                 {
  219.                     stringPos++;
  220.                     if ('0' <= ch <= '9')
  221.                     {
  222.                         num = (num * 10) + (ch - '0');
  223.                     }
  224.                     else
  225.                     {
  226.                         return -1;
  227.                     }
  228.                 }
  229.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  230.                 setarg(paramPos, 0, num * neg);
  231.             }
  232.             case 'h', 'x':
  233.             {
  234.                 new
  235.                     num = 0,
  236.                     ch = string[stringPos];
  237.                 do
  238.                 {
  239.                     stringPos++;
  240.                     switch (ch)
  241.                     {
  242.                         case 'x', 'X':
  243.                         {
  244.                             num = 0;
  245.                             continue;
  246.                         }
  247.                         case '0' .. '9':
  248.                         {
  249.                             num = (num << 4) | (ch - '0');
  250.                         }
  251.                         case 'a' .. 'f':
  252.                         {
  253.                             num = (num << 4) | (ch - ('a' - 10));
  254.                         }
  255.                         case 'A' .. 'F':
  256.                         {
  257.                             num = (num << 4) | (ch - ('A' - 10));
  258.                         }
  259.                         default:
  260.                         {
  261.                             return -1;
  262.                         }
  263.                     }
  264.                 }
  265.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  266.                 setarg(paramPos, 0, num);
  267.             }
  268.             case 'c':
  269.             {
  270.                 setarg(paramPos, 0, string[stringPos++]);
  271.             }
  272.             case 'f':
  273.             {
  274.  
  275.                 new changestr[16], changepos = 0, strpos = stringPos;
  276.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  277.                 {
  278.                     changestr[changepos++] = string[strpos++];
  279.                     }
  280.                 changestr[changepos] = '\0';
  281.                 setarg(paramPos,0,_:floatstr(changestr));
  282.             }
  283.             case 'p':
  284.             {
  285.                 delim = format[formatPos++];
  286.                 continue;
  287.             }
  288.             case '\'':
  289.             {
  290.                 new
  291.                     end = formatPos - 1,
  292.                     ch;
  293.                 while ((ch = format[++end]) && ch != '\'') {}
  294.                 if (!ch)
  295.                 {
  296.                     return -1;
  297.                 }
  298.                 format[end] = '\0';
  299.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  300.                 {
  301.                     if (format[end + 1])
  302.                     {
  303.                         return -1;
  304.                     }
  305.                     return 0;
  306.                 }
  307.                 format[end] = '\'';
  308.                 stringPos = ch + (end - formatPos);
  309.                 formatPos = end + 1;
  310.             }
  311.             case 'u':
  312.             {
  313.                 new
  314.                     end = stringPos - 1,
  315.                     id = 0,
  316.                     bool:num = true,
  317.                     ch;
  318.                 while ((ch = string[++end]) && ch != delim)
  319.                 {
  320.                     if (num)
  321.                     {
  322.                         if ('0' <= ch <= '9')
  323.                         {
  324.                             id = (id * 10) + (ch - '0');
  325.                         }
  326.                         else
  327.                         {
  328.                             num = false;
  329.                         }
  330.                     }
  331.                 }
  332.                 if (num && IsPlayerConnected(id))
  333.                 {
  334.                     setarg(paramPos, 0, id);
  335.                 }
  336.                 else
  337.                 {
  338.                     #if !defined foreach
  339.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  340.                         #define __SSCANF_FOREACH__
  341.                     #endif
  342.                     string[end] = '\0';
  343.                     num = false;
  344.                     new
  345.                         ssname[MAX_PLAYER_NAME];
  346.                     id = end - stringPos;
  347.                     foreach (Player, playerid)
  348.                     {
  349.                         GetPlayerName(playerid, ssname, sizeof (ssname));
  350.                         if (!strcmp(ssname, string[stringPos], true, id))
  351.                         {
  352.                             setarg(paramPos, 0, playerid);
  353.                             num = true;
  354.                             break;
  355.                         }
  356.                     }
  357.                     if (!num)
  358.                     {
  359.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  360.                     }
  361.                     string[end] = ch;
  362.                     #if defined __SSCANF_FOREACH__
  363.                         #undef foreach
  364.                         #undef __SSCANF_FOREACH__
  365.                     #endif
  366.                 }
  367.                 stringPos = end;
  368.             }
  369.             case 's', 'z':
  370.             {
  371.                 new
  372.                     i = 0,
  373.                     ch;
  374.                 if (format[formatPos])
  375.                 {
  376.                     while ((ch = string[stringPos++]) && ch != delim)
  377.                     {
  378.                         setarg(paramPos, i++, ch);
  379.                     }
  380.                     if (!i)
  381.                     {
  382.                         return -1;
  383.                     }
  384.                 }
  385.                 else
  386.                 {
  387.                     while ((ch = string[stringPos++]))
  388.                     {
  389.                         setarg(paramPos, i++, ch);
  390.                     }
  391.                 }
  392.                 stringPos--;
  393.                 setarg(paramPos, i, '\0');
  394.             }
  395.             default:
  396.             {
  397.                 continue;
  398.             }
  399.         }
  400.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  401.         {
  402.             stringPos++;
  403.         }
  404.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  405.         {
  406.             stringPos++;
  407.         }
  408.         paramPos++;
  409.     }
  410.     do
  411.     {
  412.         if ((delim = format[formatPos++]) > ' ')
  413.         {
  414.             if (delim == '\'')
  415.             {
  416.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  417.             }
  418.             else if (delim != 'z')
  419.             {
  420.                 return delim;
  421.             }
  422.         }
  423.     }
  424.     while (delim > ' ');
  425.     return 0;
  426. }
  427.  
  428. pName(playerid) {
  429.   new pname[MAX_PLAYER_NAME+1];
  430.   GetPlayerName(playerid, pname, sizeof(pname));
  431.   return pname;
  432. }
Advertisement
Add Comment
Please, Sign In to add comment