Advertisement
RanAway

[ Pawn - includes ] smartstocks include

Feb 12th, 2025
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 17.03 KB | None | 0 0
  1. #if defined _smartstocks_included
  2.     #endinput
  3. #endif
  4.  
  5. #define _smartstocks_included
  6.  
  7. #include < amxmisc >
  8. #include < fun >
  9. #include < reapi >
  10. #include < cstrike >
  11. #include < fakemeta >
  12. #include < hamsandwich >
  13.  
  14. /**
  15.  * Checks if a player has the specified access flag and is connected to the server.
  16.  *
  17.  * @param %1       The player's ID.
  18.  * @param %2       The access flag to check (e.g., ADMIN_BAN).
  19.  *
  20.  * @return         True if the player is connected and has the specified access flag, false otherwise.
  21.  */
  22. #define CHECK_ACCESS(%1,%2) ( is_user_connected( %1 ) && get_user_flags( %1 ) & %2 )
  23.  
  24. /**
  25.  * Enumeration of time constants in seconds.
  26.  *
  27.  * This enum defines various time-related constants used for calculations
  28.  * involving time durations in seconds.
  29.  */
  30. enum
  31. {
  32.     YEAR = 31536000,        // Number of seconds in a year
  33.     LEAPYEAR = 31622400,    // Number of seconds in a leap year
  34.     LONGMONTH = 2678400,    // Number of seconds in a long month (31 days)
  35.     NORMALMONTH = 2592000// Number of seconds in a normal month (30 days)
  36.     SHORTMONTH = 2419200,   // Number of seconds in a short month (28/29 days)
  37.     WEEK = 604800,          // Number of seconds in a week
  38.     DAY = 86400,            // Number of seconds in a day
  39.     HOUR = 3600,            // Number of seconds in an hour
  40.     MINUTE = 60,            // Number of seconds in a minute
  41.     SECOND = 1              // Number of seconds in a second
  42. }
  43.  
  44. /**
  45.  * Converts a given time in seconds into a structured array containing years, months, days, hours, minutes, and seconds.
  46.  *
  47.  * @param time       The total time in seconds to convert.
  48.  * @return           An array containing the breakdown of time: [seconds, minutes, hours, days, months, years].
  49.  *
  50.  * @note             This function assumes each month has 30 days, ignoring variations in month lengths (such as 31 or 29 days).
  51.  */
  52. stock CountTime( time )
  53. {
  54.     new time_values[ 6 ]
  55.  
  56.     time_values[ 0 ] = time % 60, time /= 60    // seconds
  57.     time_values[ 1 ] = time % 60, time /= 60    // minutes
  58.     time_values[ 2 ] = time % 24, time /= 24    // hours
  59.     time_values[ 3 ] = time % 30, time /= 30    // days
  60.     time_values[ 4 ] = time % 12, time /= 12    // months
  61.     time_values[ 5 ] = time                     // years
  62.  
  63.     return time_values
  64. }
  65.  
  66. /**
  67.  * Formats the given time according to the specified format.
  68.  *
  69.  * @param timemethod     Format string (e.g., "%H:%M:%S")
  70.  * @param timetoconvert  Time value to format (default is 0 for the current time)
  71.  *
  72.  * Example:
  73.  *      For more formatting options, refer to https://cplusplus.com/reference/ctime/strftime/
  74.  *      Some common format specifiers:
  75.  *      - %H : Hours (00-23)
  76.  *      - %M : Minutes (00-59)
  77.  *      - %S : Seconds (00-59)
  78.  *      - %d : Day of the month (01-31)
  79.  *      - %m : Month as a decimal number (01-12)
  80.  *      - %y : Year, last two digits (00-99)
  81.  *      - %Y : Year (e.g., 2023)
  82.  *
  83.  * @return      Formatted time string
  84.  */
  85. stock Getformattime( const timemethod[], timetoconvert = 0 )
  86. {
  87.     new fmtime[ 512 ]
  88.     format_time( fmtime, charsmax( fmtime ), timemethod, timetoconvert )
  89.     return fmtime
  90. }
  91.  
  92. /**
  93.  * Flags for GetHigherAccess
  94.  */
  95. enum ( <<= 1 )
  96. {
  97.     INCLUDE_BOTS,        // 1  (1 << 0)
  98.     CHECK_IMMUNITY,      // 2  (1 << 1)
  99.     BLOCK_EQUAL_FLAGS,   // 4  (1 << 2)
  100.     SELF_COMPARISON      // 8  (1 << 3)
  101. }
  102.  
  103. /**
  104.  * Compares access levels between two players and determines which has higher access based on provided flags.
  105.  *
  106.  * @param id         The ID of the first player.
  107.  * @param compareid  The ID of the second player to compare against.
  108.  * @param flags      Optional flags to control the comparison behavior:
  109.  *                   - INCLUDE_BOTS: Include bots and HLTV clients in the comparison.
  110.  *                   - CHECK_IMMUNITY: Gives priority to immunity status for the compareid player.
  111.  *                   - BLOCK_EQUAL_FLAGS: Treat equal access levels as a failure in comparison.
  112.  *                   - SELF_COMPARISON: Allows self-comparison, treating `id == compareid` as a higher access.
  113.  *
  114.  * @return           True if the first player has higher or equal access based on flags; False otherwise.
  115.  */
  116. stock GetHigherAccess( id, compareid, flags = SELF_COMPARISON )
  117. {
  118.     if( !is_user_admin( compareid ) || ( ~flags & INCLUDE_BOTS && ( is_user_bot( compareid ) || is_user_hltv( compareid ) ) ) )
  119.         return true
  120.  
  121.     if( flags & SELF_COMPARISON && id == compareid )
  122.         return true
  123.  
  124.     new idflags = get_user_flags( id )
  125.     new compareflags = get_user_flags( compareid )
  126.  
  127.     if( flags & CHECK_IMMUNITY && compareflags & ADMIN_IMMUNITY )
  128.         return false
  129.  
  130.     if( idflags > compareflags )
  131.         return true
  132.     else if( idflags < compareflags )
  133.         return false
  134.  
  135.     return flags & BLOCK_EQUAL_FLAGS ? false : true
  136. }
  137.  
  138. /**
  139.  * Gets the authentication ID (SteamID) of a player.
  140.  *
  141.  * @param id         Player index
  142.  *
  143.  * @return           Authentication ID (SteamID) of the player
  144.  */
  145. stock GetAuth( id )
  146. {
  147.     new AuthID[ 32 ]
  148.     get_user_authid( id, AuthID, charsmax( AuthID ) )
  149.     return AuthID
  150. }
  151.  
  152. /**
  153.  * Gets the IP address of a player.
  154.  *
  155.  * @param id         Player index
  156.  * @param port       Whether to include the port (1 by default)
  157.  *
  158.  * @return           IP address string of the player
  159.  */
  160. stock GetIP( id, port = 1 )
  161. {
  162.     new IP[ 32 ]
  163.     get_user_ip( id, IP, charsmax( IP ), port )
  164.     return IP
  165. }
  166.  
  167. /**
  168.  * Adds an item to the menu, similar to menu_makecallback,
  169.  * but allows the button to be manipulated.
  170.  *
  171.  * @param menu          The menu to which the item is added.
  172.  * @param string        The text of the item.
  173.  * @param number        The item number for formatting, used to display the item's index (default: 0).
  174.  * @param callback      Whether to treat the item as a callback (default: true).
  175.  * @param info          Item info string for internal information.
  176.  * @param defaultColor  The default color formatting (default: \r for red).
  177.  *
  178.  * @return              The result of the menu addition.
  179.  */
  180. stock MenuCallback( menu, string[], number[] = "#.", callback = true, const info[] = "", const defaultColor[] = "\r" )
  181. {
  182.     new text[ 512 ]
  183.     if( callback ) formatex( text, charsmax( text ), "%s%s \d%s", defaultColor, number, string )
  184.     return callback ? menu_addtext2( menu, text ) : menu_additem( menu, string, info )
  185. }
  186.  
  187. /**
  188.  * Retrieves the numeric key associated with a menu item.
  189.  *
  190.  * This function fetches information about a specific item in a menu and
  191.  * extracts a numeric key from it, which is useful for further processing
  192.  * or referencing.
  193.  *
  194.  * @param menu       The menu handle.
  195.  * @param key        The index of the item within the menu.
  196.  *
  197.  * @return           The numeric key extracted from the item's data.
  198.  */
  199. stock GetMenuKey( menu, key )
  200. {
  201.     new data[ 6 ]
  202.     menu_item_getinfo( menu, key, _, data, charsmax( data ) )
  203.     return str_to_num( data )
  204. }
  205.  
  206. /**
  207.  * Converts a number to a string representation.
  208.  *
  209.  * This function takes an integer number and converts it to a string format
  210.  * that can be used for display or logging purposes.
  211.  *
  212.  * @param num        The integer number to be converted to a string.
  213.  *
  214.  * @return           A string representation of the given number.
  215.  */
  216. stock GetNumStr( num )
  217. {
  218.     new string[ 32 ]
  219.     num_to_str( num, string, charsmax( string ) )
  220.     return string
  221. }
  222.  
  223. /**
  224.  * Sets all elements of a 2D array to a specified value.
  225.  *
  226.  * @param array         The 2D array to be modified.
  227.  * @param dim1          The first dimension size (rows) of the array.
  228.  * @param dim2          The second dimension size (columns) of the array.
  229.  * @param value         The value to set for each element in the array.
  230.  *
  231.  * @noreturn
  232.  */
  233. stock arrayset2D( any:array[][], dim1, dim2, any:value )
  234. {
  235.     for( new i = 0; i < dim1; i++ )
  236.         for( new j = 0; j < dim2; j++ )
  237.             array[ i ][ j ] = value
  238. }
  239.  
  240. /**
  241.  * Spawns a player with optional custom health and armor values.
  242.  *
  243.  * @param id         The ID of the player to spawn.
  244.  * @param health     Optional. Sets the player's health. If 0, health is not modified after spawn.
  245.  * @param armor      Optional. Sets the player's armor. Defaults to 0 if not specified or set to 0.
  246.  *
  247.  * @noreturn
  248.  */
  249. stock dospawn( id, health = 0, armor = 0 )
  250. {
  251.     dllfunc( DLLFunc_Spawn, id )
  252.  
  253.     if( health ) set_user_health( id, health )
  254.     if( armor ) set_user_armor( id, armor )
  255. }
  256.  
  257. /**
  258.  * Retrieves the model name of a player.
  259.  *
  260.  * This function fetches the model name associated with a player, based on their ID.
  261.  *
  262.  * @param id         The player's ID whose model name is to be retrieved.
  263.  *
  264.  * @return           A string containing the player's model name.
  265.  */
  266. stock GetModelName( id )
  267. {
  268.     new model[ 32 ]
  269.     cs_get_user_model( id, model, charsmax( model ) )
  270.     return model
  271. }
  272.  
  273. /**
  274.  * Retrieves the team name of a player.
  275.  *
  276.  * This function fetches the team name associated with a player based on their ID.
  277.  *
  278.  * @param id         The player's ID whose team name is to be retrieved.
  279.  *
  280.  * @return           A string containing the player's team name ("counter-terrorist", "terrorist", or "spectator").
  281.  */
  282. stock GetTeamName( id )
  283. {
  284.     new teamname[ 32 ]
  285.     teamname = get_user_team( id ) == 2 ? "counter-terrorist" : get_user_team( id ) == 1 ? "terrorist" : "spectator"
  286.     return teamname
  287. }
  288.  
  289. /**
  290.  * Categorys data.
  291.  */
  292. #define MAX_WEAPONSCOUNT    27
  293. #define MAX_PRIMARY         18
  294. #define MAX_SECONDARY       6
  295. #define MAX_NADES           3
  296.  
  297. /**
  298.  * Array that lists weapon names organized by category.
  299.  */
  300. new weaponslist[][] =
  301. {               //----------------- Assault rifles ----------------//
  302.     "m4a1",         "ak47",     "awp",      "m3",       "xm1014",       "tmp",
  303.     "mac10",        "mp5navy""p90",      "ump45",    "famas",        "galil",
  304.     "sg552",        "aug",      "scout",    "sg550",    "g3sg1",        "m249",
  305.                 //-------------------- Pistols --------------------//
  306.     "glock18",      "usp",      "p228",     "deagle",   "fiveseven",    "elite",
  307.                 //-------------------- Grenades --------------------//
  308.     "hegrenade",                "smokegrenade",                         "flashbang"
  309. }
  310.  
  311. /**
  312.  * Gives a player a specified weapon, optionally with custom ammo in the clip and magazine.
  313.  *
  314.  * @param id            The player ID to whom the weapon will be given.
  315.  * @param weaponname    The name of the weapon to be given.
  316.  * @param clip          The number of rounds in the clip. Default is -1 (uses the weapon's max clip capacity).
  317.  * @param mag           The number of rounds in the magazine. Default is -1 (uses the weapon's max ammo capacity).
  318.  *
  319.  * @noreturn
  320.  */
  321. stock GiveWeapon( id, weaponname[], clip = -1, mag = -1 )
  322. {
  323.     if( clip <= -1 ) clip = rg_get_weapon_info( rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), WI_GUN_CLIP_SIZE )
  324.     if( mag <= -1 ) mag = rg_get_weapon_info( rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), WI_MAX_ROUNDS )
  325.  
  326.     for( new i = ( MAX_PRIMARY + MAX_SECONDARY ); i < MAX_WEAPONSCOUNT; i++ ) if( equali( weaponname, weaponslist[ i ] ) )
  327.     {
  328.         clip = -2
  329.         break
  330.     }
  331.  
  332.     rg_give_item( id, GetWeaponName( weaponname ) )
  333.     if( clip != -2 ) rg_set_user_ammo( id, rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), clip )
  334.     rg_set_user_bpammo( id, rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), mag )
  335. }
  336.  
  337. /**
  338.  * Constructs the full weapon name in the format "weapon_<name>".
  339.  *
  340.  * @param weapon     The short name of the weapon (e.g., "ak47", "m4a1").
  341.  *
  342.  * @return           The formatted full weapon name as a string (e.g., "weapon_ak47").
  343.  */
  344. stock GetWeaponName( const weapon[] )
  345. {
  346.     new weaponname[ 32 ]
  347.     formatex( weaponname, charsmax( weaponname ), "weapon_%s", weapon )
  348.     return weaponname
  349. }
  350.  
  351. /**
  352.  * Retrieves the list of weapons for a specified player.
  353.  *
  354.  * @param id         The player's ID for whom to retrieve the weapons.
  355.  *
  356.  * @return           The number of weapons owned by the specified player.
  357.  */
  358. stock GetWeapons( id )
  359. {
  360.     new weapons[ 32 ], num
  361.     get_user_weapons( id, weapons, num )
  362.     return num
  363. }
  364.  
  365. /**
  366.  * Gets the current map name.
  367.  *
  368.  * @deprecated       use MapName if your server support it
  369.  *
  370.  * @return           Current map name
  371.  */
  372.  //#pragma deprecated Use MapName instead. 
  373. stock CurrentMap()
  374. {
  375.     new currentmap[ 35 ]
  376.     get_mapname( currentmap, charsmax( currentmap ) )
  377.     return currentmap
  378. }
  379.  
  380. /**
  381.  * Displays a HUD message to a player.
  382.  *
  383.  * This function can display either a standard HUD message or a dynamic HUD message
  384.  * depending on the value of the 'dhud' parameter.
  385.  *
  386.  * @param id            The player ID to whom the message will be displayed.
  387.  * @param dhud          If true, displays a dynamic HUD message; otherwise, a standard HUD message.
  388.  * @param msg           The message to be displayed.
  389.  * @param r             Red color component (0-255) for the message text.
  390.  * @param g             Green color component (0-255) for the message text.
  391.  * @param b             Blue color component (0-255) for the message text.
  392.  * @param x             X-coordinate for the message position (default is -1.0, center).
  393.  * @param y             Y-coordinate for the message position (default is 0.35).
  394.  * @param effects       Effects to apply to the message (e.g., fade, blink).
  395.  * @param fxtime        Duration for effects to take place (default is 6.0 seconds).
  396.  * @param holdtime      Duration to hold the message on the screen (default is 12.0 seconds).
  397.  * @param fadeintime    Duration for the message to fade in (default is 0.1 seconds).
  398.  * @param fadeouttime   Duration for the message to fade out (default is 0.2 seconds).
  399.  *
  400.  * @noreturn
  401.  */
  402. stock GetHudMsg( id, bool:dhud = false, const msg[], r = 255, g = 255, b = 255, Float:x = -1.0, Float:y = 0.35, effects = 0, Float:fxtime = 6.0, Float:holdtime = 12.0, Float:fadeintime = 0.1, Float:fadeouttime = 0.2 )
  403. {
  404.     if( dhud )
  405.     {
  406.         set_dhudmessage( r, g, b, x, y, effects, fxtime, holdtime, fadeintime, fadeouttime )
  407.         show_dhudmessage( id, msg )
  408.     }
  409.     else
  410.     {
  411.         set_hudmessage( r, g, b, x, y, effects, fxtime, holdtime, fadeintime, fadeouttime )
  412.         show_hudmessage( id, msg )
  413.     }
  414. }
  415.  
  416. /**
  417.  * Retrieves the value of a specified console variable (cvar).
  418.  *
  419.  * @param cvar       The name of the console variable to retrieve.
  420.  *
  421.  * Example:
  422.  *      - GetCvar( "hostname" )        // Retrieves the server's hostname as a string
  423.  *
  424.  * @return      The value of the specified console variable as a string.
  425.  */
  426. stock GetCvar( const cvar[] )
  427. {
  428.     new cvarname[ 64 ]
  429.     get_cvar_string( cvar, cvarname, charsmax( cvarname ) )
  430.     return cvarname
  431. }
  432.  
  433. /**
  434.  * Retrieves the path to the local information for a specified directory name.
  435.  *
  436.  * @param dirname    The name of the directory to retrieve the path for.
  437.  *
  438.  * Example:
  439.  *      - GetDirPath( "amxx_logs" )        // Retrieves the directory path
  440.  *
  441.  * Supported Directory Locations:
  442.  * - amxx_logs       : Path to logs generated by AMXX (addons/amxmodx/logs)
  443.  * - amxx_configsdir : Path to configuration files (addons/amxmodx/configs)
  444.  * - amxx_datadir    : Path to data files used by AMXX (addons/amxmodx/data)
  445.  * - amxx_modules    : Path to the modules configuration file (addons/amxmodx/configs/modules.ini)
  446.  * - amxx_plugins    : Path to the plugins configuration file (addons/amxmodx/configs/plugins.ini)
  447.  * - amxx_pluginsdir : Path to the directory where plugins are stored (addons/amxmodx/plugins)
  448.  * - amxx_modulesdir : Path to the directory where modules are stored (addons/amxmodx/modules)
  449.  * - amxx_vault      : Path to the vault data file (addons/amxmodx/data/vault.ini)
  450.  *
  451.  * @return         A string representing the path to the configuration directory.
  452.  */
  453. stock GetDirPath( const dirname[] )
  454. {
  455.     new localinfo[ 24 ]
  456.     get_localinfo( dirname, localinfo, charsmax( localinfo ) )
  457.     return localinfo
  458. }
  459.  
  460. /**
  461.  * Logs a message to both the server console and a file, with timestamp support.
  462.  *
  463.  * @param filepath    The relative path of the log file under the "amxx_logs" directory.
  464.  * @param TimeZone    The timezone offset in seconds (default is 0, for UTC).
  465.  * @param string      The format string for the log message.
  466.  * @param any...         Additional arguments to format the log message.
  467.  *
  468.  * @noreturn
  469.  */
  470. stock logfile( const filepath[], TimeZone = 0, const string[], any:... )
  471. {
  472.     new logpath[ 128 ], logmsg[ 191 ]
  473.  
  474.     formatex( logpath, charsmax( logpath ), "%s/%s.log", GetDirPath( "amxx_logs" ), filepath )
  475.     vformat( logmsg, charsmax( logmsg ), fmt( "%s | %s", Getformattime( "%H:%M:%S - %d/%m/%y", get_systime( TimeZone ) ), string ), 4 )
  476.  
  477.     server_print( "%s", logmsg )
  478.  
  479.     new File = fopen( logpath, "a+" )
  480.     fprintf( File, "%s^n", logmsg )
  481.     fclose( File )
  482. }
  483.  
  484. /**
  485.  * Searches for the first empty line in a file and returns its line number.
  486.  *
  487.  * @param filepath      The path to the file to search.
  488.  * @return              The line number of the first empty line if found, or -1 if no empty line exists or the file cannot be opened.
  489.  */
  490. stock findemptyline( const filepath[] )
  491. {
  492.     new Line[ 512 ], foundlines = -1, linecounter = -1, File = fopen( filepath, "r+" )
  493.  
  494.     if( File )
  495.     {
  496.         while( !feof( File ) )
  497.         {
  498.             linecounter++
  499.             fgets( File, Line, charsmax( Line ) )
  500.             trim( Line )
  501.  
  502.             if( !Line[ 0 ] )
  503.             {
  504.                 foundlines = linecounter
  505.                 break
  506.             }
  507.         }
  508.         fclose( File )
  509.     }
  510.  
  511.     if( foundlines == -1 ) write_file( filepath, "", foundlines )
  512.  
  513.     return foundlines
  514. }
  515.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement