Advertisement
Guest User

sscanf2

a guest
Apr 14th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.00 KB | None | 0 0
  1. /*  
  2.  *  Version: MPL 1.1
  3.  *  
  4.  *  The contents of this file are subject to the Mozilla Public License Version
  5.  *  1.1 (the "License"); you may not use this file except in compliance with
  6.  *  the License. You may obtain a copy of the License at
  7.  *  [url]http://www.mozilla.org/MPL/[/url]
  8.  *  
  9.  *  Software distributed under the License is distributed on an "AS IS" basis,
  10.  *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  *  for the specific language governing rights and limitations under the
  12.  *  License.
  13.  *  
  14.  *  The Original Code is the sscanf 2.0 SA:MP plugin.
  15.  *  
  16.  *  The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  17.  *  Portions created by the Initial Developer are Copyright (C) 2010
  18.  *  the Initial Developer. All Rights Reserved.
  19.  *  
  20.  *  Contributor(s):
  21.  *  
  22.  *  Special Thanks to:
  23.  *  
  24.  *      SA:MP Team past, present and future
  25.  */
  26.  
  27. #if defined _inc_a_npc
  28.     #pragma library sscanf
  29. #elseif !defined _inc_a_samp
  30.     #error Please include <a_npc> or <a_samp> first.
  31. #endif
  32.  
  33. #define SSCANF:%0(%1) sscanf_%0(%1);public sscanf_%0(%1)
  34.  
  35. #if defined sscanf
  36.     #error sscanf (possibly the PAWN version) already defined.
  37. #endif
  38.  
  39. native sscanf(const data[], const format[], {Float,_}:...);
  40. native unformat(const data[], const format[], {Float,_}:...) = sscanf;
  41. native SSCANF_Init(players, invalid, len);
  42. native SSCANF_Join(playerid, const name[], npc);
  43. native SSCANF_Leave(playerid);
  44.  
  45. native SSCANF_Option(const name[], value);
  46.  
  47. stock const
  48.     SSCANF_QUIET[] = "SSCANF_QUIET",
  49.     OLD_DEFAULT_NAME[] = "OLD_DEFAULT_NAME",
  50.     MATCH_NAME_PARTIAL[] = "MATCH_NAME_PARTIAL",
  51.     CELLMIN_ON_MATCHES[] = "CELLMIN_ON_MATCHES",
  52.     OLD_DEFAULT_KUSTOM[] = "OLD_DEFAULT_KUSTOM",
  53.     OLD_DEFAULT_CUSTOM[] = "OLD_DEFAULT_CUSTOM";
  54.  
  55. static stock
  56.     bool:SSCANF_gInit = false,
  57.     SSCANF_g_sPlayers[MAX_PLAYERS char];
  58.  
  59. #if defined _inc_a_npc
  60.     forward SSCANF_PlayerCheck();
  61.    
  62.     /*
  63.       OnNPCModeInit
  64.  
  65.       Called when the script starts if it is a NPC mode, sets up the system,
  66.       then calls the "real" OnNPCModeInit (using the new ALS 2 hook method).
  67.     */
  68.    
  69.     public OnNPCModeInit()
  70.     {
  71.         SSCANF_Init(MAX_PLAYERS, INVALID_PLAYER_ID, MAX_PLAYER_NAME);
  72.         #if !defined SSCANF_NO_PLAYERS
  73.             // Initialise the system.
  74.             SSCANF_PlayerCheck();
  75.             SetTimer("SSCANF_PlayerCheck", 1, 1);
  76.         #endif
  77.         #if defined SSCANF_OnNPCModeInit
  78.             SSCANF_OnNPCModeInit();
  79.         #endif
  80.         return 1;
  81.     }
  82.    
  83.     #if defined _ALS_OnNPCModeInit
  84.         #undef OnNPCModeInit
  85.     #else
  86.         #define _ALS_OnNPCModeInit
  87.     #endif
  88.     #define OnNPCModeInit SSCANF_OnNPCModeInit
  89.     #if defined SSCANF_OnNPCModeInit
  90.         forward SSCANF_OnNPCModeInit();
  91.     #endif
  92.    
  93.     /*
  94.       SSCANF_PlayerCheck
  95.  
  96.       NPC modes have no "OnPlayerConnect callback, so we need to simulate one.
  97.     */
  98.    
  99.     #if !defined SSCANF_NO_PLAYERS
  100.         public SSCANF_PlayerCheck()
  101.         {
  102.             for (new i = 0; i != MAX_PLAYERS; ++i)
  103.             {
  104.                 if (IsPlayerConnected(i))
  105.                 {
  106.                     if (!SSCANF_g_sPlayers{i})
  107.                     {
  108.                         new
  109.                             name[MAX_PLAYER_NAME];
  110.                         GetPlayerName(i, name, sizeof (name));
  111.                         // We have no way to know if they are an NPC or not!
  112.                         SSCANF_Join(i, name, 0);
  113.                         SSCANF_g_sPlayers{i} = 1;
  114.                     }
  115.                 }
  116.                 else
  117.                 {
  118.                     if (SSCANF_g_sPlayers{i})
  119.                     {
  120.                         SSCANF_Leave(i);
  121.                         SSCANF_g_sPlayers{i} = 0;
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.     #endif
  127. #else
  128.     /*
  129.       OnFilterScriptInit
  130.  
  131.       Called when the script starts if it is a filterscript, sets up the system,
  132.       then calls the "real" OnFilterScriptInit (using the new ALS 2 hook
  133.       method).
  134.     */
  135.    
  136.     public OnFilterScriptInit()
  137.     {
  138.         SSCANF_Init(GetMaxPlayers(), INVALID_PLAYER_ID, MAX_PLAYER_NAME);
  139.         SSCANF_gInit = true;
  140.         #if defined SSCANF_OnFilterScriptInit
  141.             SSCANF_OnFilterScriptInit();
  142.         #endif
  143.         return 1;
  144.     }
  145.    
  146.     #if defined _ALS_OnFilterScriptInit
  147.         #undef OnFilterScriptInit
  148.     #else
  149.         #define _ALS_OnFilterScriptInit
  150.     #endif
  151.     #define OnFilterScriptInit SSCANF_OnFilterScriptInit
  152.     #if defined SSCANF_OnFilterScriptInit
  153.         forward SSCANF_OnFilterScriptInit();
  154.     #endif
  155.    
  156.     /*
  157.       OnGameModeInit
  158.  
  159.       Called when the script starts if it is a gamemode.  This callback is also
  160.       called in filterscripts so we don't want to reinitialise the system in
  161.       that case.
  162.     */
  163.    
  164.     public OnGameModeInit()
  165.     {
  166.         if (!SSCANF_gInit)
  167.         {
  168.             SSCANF_Init(GetMaxPlayers(), INVALID_PLAYER_ID, MAX_PLAYER_NAME);
  169.             SSCANF_gInit = true;
  170.         }
  171.         #if defined SSCANF_OnGameModeInit
  172.             SSCANF_OnGameModeInit();
  173.         #endif
  174.         return 1;
  175.     }
  176.    
  177.     #if defined _ALS_OnGameModeInit
  178.         #undef OnGameModeInit
  179.     #else
  180.         #define _ALS_OnGameModeInit
  181.     #endif
  182.     #define OnGameModeInit SSCANF_OnGameModeInit
  183.     #if defined SSCANF_OnGameModeInit
  184.         forward SSCANF_OnGameModeInit();
  185.     #endif
  186.    
  187.     /*
  188.       OnPlayerConnect
  189.  
  190.       Called when a player connects.  Actually increments an internal count so
  191.       that if a script ends and "OnPlayerDisconnect" is called then "sscanf"
  192.       still knows that the player is really connected.  Also stores their name
  193.       internally.
  194.     */
  195.    
  196.     public OnPlayerConnect(playerid)
  197.     {
  198.         new
  199.             name[MAX_PLAYER_NAME];
  200.         GetPlayerName(playerid, name, sizeof (name));
  201.         SSCANF_Join(playerid, name, IsPlayerNPC(playerid));
  202.         #if defined SSCANF_OnPlayerConnect
  203.             SSCANF_OnPlayerConnect(playerid);
  204.         #endif
  205.         return 1;
  206.     }
  207.    
  208.     #if defined _ALS_OnPlayerConnect
  209.         #undef OnPlayerConnect
  210.     #else
  211.         #define _ALS_OnPlayerConnect
  212.     #endif
  213.     #define OnPlayerConnect SSCANF_OnPlayerConnect
  214.     #if defined SSCANF_OnPlayerConnect
  215.         forward SSCANF_OnPlayerConnect(playerid);
  216.     #endif
  217.    
  218.     /*
  219.       OnPlayerDisconnect
  220.  
  221.       Called when a player disconnects, or when a script is ended.
  222.     */
  223.    
  224.     public OnPlayerDisconnect(playerid, reason)
  225.     {
  226.         #if defined SSCANF_OnPlayerDisconnect
  227.             SSCANF_OnPlayerDisconnect(playerid, reason);
  228.         #endif
  229.         SSCANF_Leave(playerid);
  230.         return 1;
  231.     }
  232.    
  233.     #if defined _ALS_OnPlayerDisconnect
  234.         #undef OnPlayerDisconnect
  235.     #else
  236.         #define _ALS_OnPlayerDisconnect
  237.     #endif
  238.     #define OnPlayerDisconnect SSCANF_OnPlayerDisconnect
  239.     #if defined SSCANF_OnPlayerDisconnect
  240.         forward SSCANF_OnPlayerDisconnect(playerid, reason);
  241.     #endif
  242. #endif
  243.  
  244. #define SSCANF_Init
  245. #define SSCANF_Join
  246. #define SSCANF_Leave
  247.  
  248. #define extract%0->%1; EXTRN%1;unformat(_:EXTRZ:EXTRV:EXTRX:%0,##,%1,,);
  249. #define unformat(_:EXTRZ:EXTRV:EXTRX:%0,##,%1);%2else if (unformat(_:EXTRV:EXTRX:%0,##,%1))
  250.  
  251. #define EXTRV:EXTRX:%0<%3>##,%9new%1,%2) EXTRY:%0##P<%3>,|||%1|||%2)
  252. #define EXTRX:%0##,%9new%1,%2) EXTRY:%0##,|||%1|||%2)
  253. #define EXTRY: EXTR8:EXTR9:EXTR0:EXTR1:EXTR2:EXTR3:EXTR4:
  254.  
  255. #define EXTR8:EXTR9:EXTR0:EXTR1:EXTR2:EXTR3:EXTR4:%0##%1,%2|||%6:%3=%9|||%4) %6_EXTRO:%0##%1,%2|||%3=%9|||%4)
  256. #define EXTR9:EXTR0:EXTR1:EXTR2:EXTR3:EXTR4:%0##%1,%2|||%3=%9|||%4) __EXTRO:%0##%1,%2|||%3=%9|||%4)
  257. #define EXTR0:EXTR1:EXTR2:EXTR3:EXTR4:%0##%1,%2|||%6:%3[%7]|||%4) %6_EXTRW:%0##%1,%2|||%3[%7]|||%4)
  258. #define EXTR1:EXTR2:EXTR3:EXTR4:%0##%1,%2|||%3[%7]|||%4) __EXTRW:%0##%1,%2|||%3|||%4)
  259. #define EXTR2:EXTR3:EXTR4:%0##%1,%2|||%6:%3|||%4) %6_EXTRN:%0##%1,%2|||%3|||%4)
  260. #define EXTR3:EXTR4:%0##%1,,%2||||||%4) %0##%1,%2)
  261. #define EXTR4:%0##%1,%2|||%3|||%4) __EXTRN:%0##%1,%2|||%3|||%4)
  262.  
  263. // Optional specifiers.
  264. #define __EXTRO:%0##%1,%2|||%3=%9|||%4,%5) EXTRY:%0##%1I"("#%9")"#,%2,%3|||%4|||%5)
  265. #define Float_EXTRO:%0##%1,%2|||%3=%9|||%4,%5) EXTRY:%0##%1F"("#%9")"#,%2,%3|||%4|||%5)
  266. #define player_EXTRO:%0##%1,%2|||%3=%9|||%4,%5) EXTRY:%0##%1U"("#%9")"#,%2,%3|||%4|||%5)
  267. #define string_EXTRO:%0##%1,%2|||%3[%7]=%9|||%4,%5) EXTRY:%0##%1S"("#%9")"#[%7],%2,%3|||%4|||%5)
  268.  
  269. // Normal specifiers (the double underscore is to work for "_:".
  270. #define __EXTRN:%0##%1,%2|||%3|||%4,%5) EXTRY:%0##%1i,%2,%3|||%4|||%5)
  271. #define Float_EXTRN:%0##%1,%2|||%3|||%4,%5) EXTRY:%0##%1f,%2,%3|||%4|||%5)
  272. #define player_EXTRN:%0##%1,%2|||%3|||%4,%5) EXTRY:%0##%1u,%2,%3|||%4|||%5)
  273. //#define string_EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1s[%7],%2,%3|||%4|||%5)
  274.  
  275. // Array versions of normal specifiers.
  276. #define __EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1a<i>[%7],%2,%3|||%4|||%5)
  277. #define Float_EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1a<f>[%7],%2,%3|||%4|||%5)
  278. #define player_EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1a<u>[%7],%2,%3|||%4|||%5)
  279. #define string_EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1s[%7],%2,%3|||%4|||%5)
  280.  
  281. // Get rid of excess leading space which causes warnings.
  282. #define EXTRN%0new%1; new%1;
  283.  
  284. #if !defined string
  285.     #define string:
  286. #endif
  287.  
  288. #define player:
  289.  
  290. #define hex:
  291. #define hex_EXTRO:%0##%1,%2|||%3=%9|||%4,%5) EXTRY:%0##%1H"("#%9")"#,%2,%3|||%4|||%5)
  292. #define hex_EXTRN:%0##%1,%2|||%3|||%4,%5) EXTRY:%0##%1h,%2,%3|||%4|||%5)
  293. #define hex_EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1a<h>[%7],%2,%3|||%4|||%5)
  294.  
  295. #define bin:
  296. #define bin_EXTRO:%0##%1,%2|||%3=%9|||%4,%5) EXTRY:%0##%1B"("#%9")"#,%2,%3|||%4|||%5)
  297. #define bin_EXTRN:%0##%1,%2|||%3|||%4,%5) EXTRY:%0##%1b,%2,%3|||%4|||%5)
  298. #define bin_EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1a<b>[%7],%2,%3|||%4|||%5)
  299.  
  300. #define kustom:%0<%1> %0
  301. #define kustom_EXTRO:%0##%1,%2|||%3<%8>=%9|||%4,%5) EXTRY:%0##%1K<%8>"("#%9")"#,%2,%3|||%4|||%5)
  302. #define kustom_EXTRN:%0##%1,%2|||%3<%8>|||%4,%5) EXTRY:%0##%1k<%8>,%2,%3|||%4|||%5)
  303. //#define bin_EXTRW:%0##%1,%2|||%3[%7]|||%4,%5) EXTRY:%0##%1a<b>[%7],%2,%3|||%4|||%5)
  304.  
  305. SSCANF:weapon(string[])
  306. {
  307.     // This function is VERY basic, needs VASTLY improving to detect variations.
  308.     if ('0' <= string[0] <= '9')
  309.     {
  310.         new
  311.             ret = strval(string);
  312.         if (0 <= ret <= 18 || 22 <= ret <= 46)
  313.         {
  314.             return ret;
  315.         }
  316.     }
  317.     else if (!strcmp(string, "Unarmed")) return 0;
  318.     else if (!strcmp(string, "Brass Knuckles")) return 1;
  319.     else if (!strcmp(string, "Golf Club")) return 2;
  320.     else if (!strcmp(string, "Night Stick")) return 3;
  321.     else if (!strcmp(string, "Knife")) return 4;
  322.     else if (!strcmp(string, "Baseball Bat")) return 5;
  323.     else if (!strcmp(string, "Shovel")) return 6;
  324.     else if (!strcmp(string, "Pool cue")) return 7;
  325.     else if (!strcmp(string, "Katana")) return 8;
  326.     else if (!strcmp(string, "Chainsaw")) return 9;
  327.     else if (!strcmp(string, "Purple Dildo")) return 10;
  328.     else if (!strcmp(string, "White Dildo")) return 11;
  329.     else if (!strcmp(string, "Long White Dildo")) return 12;
  330.     else if (!strcmp(string, "White Dildo 2")) return 13;
  331.     else if (!strcmp(string, "Flowers")) return 14;
  332.     else if (!strcmp(string, "Cane")) return 15;
  333.     else if (!strcmp(string, "Grenades")) return 16;
  334.     else if (!strcmp(string, "Tear Gas")) return 17;
  335.     else if (!strcmp(string, "Molotovs")) return 18;
  336.     else if (!strcmp(string, "Pistol")) return 22;
  337.     else if (!strcmp(string, "Silenced Pistol")) return 23;
  338.     else if (!strcmp(string, "Desert Eagle")) return 24;
  339.     else if (!strcmp(string, "Shotgun")) return 25;
  340.     else if (!strcmp(string, "Sawn Off Shotgun")) return 26;
  341.     else if (!strcmp(string, "Combat Shotgun")) return 27;
  342.     else if (!strcmp(string, "Micro Uzi")) return 28;
  343.     else if (!strcmp(string, "Mac 10")) return 28;
  344.     else if (!strcmp(string, "MP5")) return 29;
  345.     else if (!strcmp(string, "AK47")) return 30;
  346.     else if (!strcmp(string, "M4")) return 31;
  347.     else if (!strcmp(string, "Tec9")) return 32;
  348.     else if (!strcmp(string, "Rifle")) return 33;
  349.     else if (!strcmp(string, "Sniper Rifle")) return 34;
  350.     else if (!strcmp(string, "RPG")) return 35;
  351.     else if (!strcmp(string, "Missile Launcher")) return 36;
  352.     else if (!strcmp(string, "Flame Thrower")) return 37;
  353.     else if (!strcmp(string, "Minigun")) return 38;
  354.     else if (!strcmp(string, "Sachel Charges")) return 39;
  355.     else if (!strcmp(string, "Detonator")) return 40;
  356.     else if (!strcmp(string, "Spray Paint")) return 41;
  357.     else if (!strcmp(string, "Fire Extinguisher")) return 42;
  358.     else if (!strcmp(string, "Camera")) return 43;
  359.     else if (!strcmp(string, "Nightvision Goggles")) return 44;
  360.     else if (!strcmp(string, "Thermal Goggles")) return 45;
  361.     else if (!strcmp(string, "Parachute")) return 46;
  362.     return -1;
  363. }
  364.  
  365. SSCANF:vehicle(string[])
  366. {
  367.     // This function is VERY basic, needs VASTLY improving to detect variations.
  368.     if ('0' <= string[0] <= '9')
  369.     {
  370.         new
  371.             ret = strval(string);
  372.         if (400 <= ret <= 611)
  373.         {
  374.             return ret;
  375.         }
  376.     }
  377.     else if (!strcmp(string, "Landstalker")) return 400;
  378.     else if (!strcmp(string, "Bravura")) return 40
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement