Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined _smartstocks_included
- #endinput
- #endif
- #define _smartstocks_included
- #include < amxmisc >
- #include < fun >
- #include < reapi >
- #include < cstrike >
- #include < fakemeta >
- #include < hamsandwich >
- /**
- * Checks if a player has the specified access flag and is connected to the server.
- *
- * @param %1 The player's ID.
- * @param %2 The access flag to check (e.g., ADMIN_BAN).
- *
- * @return True if the player is connected and has the specified access flag, false otherwise.
- */
- #define CHECK_ACCESS(%1,%2) ( is_user_connected( %1 ) && get_user_flags( %1 ) & %2 )
- /**
- * Enumeration of time constants in seconds.
- *
- * This enum defines various time-related constants used for calculations
- * involving time durations in seconds.
- */
- enum
- {
- YEAR = 31536000, // Number of seconds in a year
- LEAPYEAR = 31622400, // Number of seconds in a leap year
- LONGMONTH = 2678400, // Number of seconds in a long month (31 days)
- NORMALMONTH = 2592000, // Number of seconds in a normal month (30 days)
- SHORTMONTH = 2419200, // Number of seconds in a short month (28/29 days)
- WEEK = 604800, // Number of seconds in a week
- DAY = 86400, // Number of seconds in a day
- HOUR = 3600, // Number of seconds in an hour
- MINUTE = 60, // Number of seconds in a minute
- SECOND = 1 // Number of seconds in a second
- }
- /**
- * Converts a given time in seconds into a structured array containing years, months, days, hours, minutes, and seconds.
- *
- * @param time The total time in seconds to convert.
- * @return An array containing the breakdown of time: [seconds, minutes, hours, days, months, years].
- *
- * @note This function assumes each month has 30 days, ignoring variations in month lengths (such as 31 or 29 days).
- */
- stock CountTime( time )
- {
- new time_values[ 6 ]
- time_values[ 0 ] = time % 60, time /= 60 // seconds
- time_values[ 1 ] = time % 60, time /= 60 // minutes
- time_values[ 2 ] = time % 24, time /= 24 // hours
- time_values[ 3 ] = time % 30, time /= 30 // days
- time_values[ 4 ] = time % 12, time /= 12 // months
- time_values[ 5 ] = time // years
- return time_values
- }
- /**
- * Formats the given time according to the specified format.
- *
- * @param timemethod Format string (e.g., "%H:%M:%S")
- * @param timetoconvert Time value to format (default is 0 for the current time)
- *
- * Example:
- * For more formatting options, refer to https://cplusplus.com/reference/ctime/strftime/
- * Some common format specifiers:
- * - %H : Hours (00-23)
- * - %M : Minutes (00-59)
- * - %S : Seconds (00-59)
- * - %d : Day of the month (01-31)
- * - %m : Month as a decimal number (01-12)
- * - %y : Year, last two digits (00-99)
- * - %Y : Year (e.g., 2023)
- *
- * @return Formatted time string
- */
- stock Getformattime( const timemethod[], timetoconvert = 0 )
- {
- new fmtime[ 512 ]
- format_time( fmtime, charsmax( fmtime ), timemethod, timetoconvert )
- return fmtime
- }
- /**
- * Flags for GetHigherAccess
- */
- enum ( <<= 1 )
- {
- INCLUDE_BOTS, // 1 (1 << 0)
- CHECK_IMMUNITY, // 2 (1 << 1)
- BLOCK_EQUAL_FLAGS, // 4 (1 << 2)
- SELF_COMPARISON // 8 (1 << 3)
- }
- /**
- * Compares access levels between two players and determines which has higher access based on provided flags.
- *
- * @param id The ID of the first player.
- * @param compareid The ID of the second player to compare against.
- * @param flags Optional flags to control the comparison behavior:
- * - INCLUDE_BOTS: Include bots and HLTV clients in the comparison.
- * - CHECK_IMMUNITY: Gives priority to immunity status for the compareid player.
- * - BLOCK_EQUAL_FLAGS: Treat equal access levels as a failure in comparison.
- * - SELF_COMPARISON: Allows self-comparison, treating `id == compareid` as a higher access.
- *
- * @return True if the first player has higher or equal access based on flags; False otherwise.
- */
- stock GetHigherAccess( id, compareid, flags = SELF_COMPARISON )
- {
- if( !is_user_admin( compareid ) || ( ~flags & INCLUDE_BOTS && ( is_user_bot( compareid ) || is_user_hltv( compareid ) ) ) )
- return true
- if( flags & SELF_COMPARISON && id == compareid )
- return true
- new idflags = get_user_flags( id )
- new compareflags = get_user_flags( compareid )
- if( flags & CHECK_IMMUNITY && compareflags & ADMIN_IMMUNITY )
- return false
- if( idflags > compareflags )
- return true
- else if( idflags < compareflags )
- return false
- return flags & BLOCK_EQUAL_FLAGS ? false : true
- }
- /**
- * Gets the authentication ID (SteamID) of a player.
- *
- * @param id Player index
- *
- * @return Authentication ID (SteamID) of the player
- */
- stock GetAuth( id )
- {
- new AuthID[ 32 ]
- get_user_authid( id, AuthID, charsmax( AuthID ) )
- return AuthID
- }
- /**
- * Gets the IP address of a player.
- *
- * @param id Player index
- * @param port Whether to include the port (1 by default)
- *
- * @return IP address string of the player
- */
- stock GetIP( id, port = 1 )
- {
- new IP[ 32 ]
- get_user_ip( id, IP, charsmax( IP ), port )
- return IP
- }
- /**
- * Adds an item to the menu, similar to menu_makecallback,
- * but allows the button to be manipulated.
- *
- * @param menu The menu to which the item is added.
- * @param string The text of the item.
- * @param number The item number for formatting, used to display the item's index (default: 0).
- * @param callback Whether to treat the item as a callback (default: true).
- * @param info Item info string for internal information.
- * @param defaultColor The default color formatting (default: \r for red).
- *
- * @return The result of the menu addition.
- */
- stock MenuCallback( menu, string[], number[] = "#.", callback = true, const info[] = "", const defaultColor[] = "\r" )
- {
- new text[ 512 ]
- if( callback ) formatex( text, charsmax( text ), "%s%s \d%s", defaultColor, number, string )
- return callback ? menu_addtext2( menu, text ) : menu_additem( menu, string, info )
- }
- /**
- * Retrieves the numeric key associated with a menu item.
- *
- * This function fetches information about a specific item in a menu and
- * extracts a numeric key from it, which is useful for further processing
- * or referencing.
- *
- * @param menu The menu handle.
- * @param key The index of the item within the menu.
- *
- * @return The numeric key extracted from the item's data.
- */
- stock GetMenuKey( menu, key )
- {
- new data[ 6 ]
- menu_item_getinfo( menu, key, _, data, charsmax( data ) )
- return str_to_num( data )
- }
- /**
- * Converts a number to a string representation.
- *
- * This function takes an integer number and converts it to a string format
- * that can be used for display or logging purposes.
- *
- * @param num The integer number to be converted to a string.
- *
- * @return A string representation of the given number.
- */
- stock GetNumStr( num )
- {
- new string[ 32 ]
- num_to_str( num, string, charsmax( string ) )
- return string
- }
- /**
- * Sets all elements of a 2D array to a specified value.
- *
- * @param array The 2D array to be modified.
- * @param dim1 The first dimension size (rows) of the array.
- * @param dim2 The second dimension size (columns) of the array.
- * @param value The value to set for each element in the array.
- *
- * @noreturn
- */
- stock arrayset2D( any:array[][], dim1, dim2, any:value )
- {
- for( new i = 0; i < dim1; i++ )
- for( new j = 0; j < dim2; j++ )
- array[ i ][ j ] = value
- }
- /**
- * Spawns a player with optional custom health and armor values.
- *
- * @param id The ID of the player to spawn.
- * @param health Optional. Sets the player's health. If 0, health is not modified after spawn.
- * @param armor Optional. Sets the player's armor. Defaults to 0 if not specified or set to 0.
- *
- * @noreturn
- */
- stock dospawn( id, health = 0, armor = 0 )
- {
- dllfunc( DLLFunc_Spawn, id )
- if( health ) set_user_health( id, health )
- if( armor ) set_user_armor( id, armor )
- }
- /**
- * Retrieves the model name of a player.
- *
- * This function fetches the model name associated with a player, based on their ID.
- *
- * @param id The player's ID whose model name is to be retrieved.
- *
- * @return A string containing the player's model name.
- */
- stock GetModelName( id )
- {
- new model[ 32 ]
- cs_get_user_model( id, model, charsmax( model ) )
- return model
- }
- /**
- * Retrieves the team name of a player.
- *
- * This function fetches the team name associated with a player based on their ID.
- *
- * @param id The player's ID whose team name is to be retrieved.
- *
- * @return A string containing the player's team name ("counter-terrorist", "terrorist", or "spectator").
- */
- stock GetTeamName( id )
- {
- new teamname[ 32 ]
- teamname = get_user_team( id ) == 2 ? "counter-terrorist" : get_user_team( id ) == 1 ? "terrorist" : "spectator"
- return teamname
- }
- /**
- * Categorys data.
- */
- #define MAX_WEAPONSCOUNT 27
- #define MAX_PRIMARY 18
- #define MAX_SECONDARY 6
- #define MAX_NADES 3
- /**
- * Array that lists weapon names organized by category.
- */
- new weaponslist[][] =
- { //----------------- Assault rifles ----------------//
- "m4a1", "ak47", "awp", "m3", "xm1014", "tmp",
- "mac10", "mp5navy", "p90", "ump45", "famas", "galil",
- "sg552", "aug", "scout", "sg550", "g3sg1", "m249",
- //-------------------- Pistols --------------------//
- "glock18", "usp", "p228", "deagle", "fiveseven", "elite",
- //-------------------- Grenades --------------------//
- "hegrenade", "smokegrenade", "flashbang"
- }
- /**
- * Gives a player a specified weapon, optionally with custom ammo in the clip and magazine.
- *
- * @param id The player ID to whom the weapon will be given.
- * @param weaponname The name of the weapon to be given.
- * @param clip The number of rounds in the clip. Default is -1 (uses the weapon's max clip capacity).
- * @param mag The number of rounds in the magazine. Default is -1 (uses the weapon's max ammo capacity).
- *
- * @noreturn
- */
- stock GiveWeapon( id, weaponname[], clip = -1, mag = -1 )
- {
- if( clip <= -1 ) clip = rg_get_weapon_info( rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), WI_GUN_CLIP_SIZE )
- if( mag <= -1 ) mag = rg_get_weapon_info( rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), WI_MAX_ROUNDS )
- for( new i = ( MAX_PRIMARY + MAX_SECONDARY ); i < MAX_WEAPONSCOUNT; i++ ) if( equali( weaponname, weaponslist[ i ] ) )
- {
- clip = -2
- break
- }
- rg_give_item( id, GetWeaponName( weaponname ) )
- if( clip != -2 ) rg_set_user_ammo( id, rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), clip )
- rg_set_user_bpammo( id, rg_get_weapon_info( GetWeaponName( weaponname ), WI_ID ), mag )
- }
- /**
- * Constructs the full weapon name in the format "weapon_<name>".
- *
- * @param weapon The short name of the weapon (e.g., "ak47", "m4a1").
- *
- * @return The formatted full weapon name as a string (e.g., "weapon_ak47").
- */
- stock GetWeaponName( const weapon[] )
- {
- new weaponname[ 32 ]
- formatex( weaponname, charsmax( weaponname ), "weapon_%s", weapon )
- return weaponname
- }
- /**
- * Retrieves the list of weapons for a specified player.
- *
- * @param id The player's ID for whom to retrieve the weapons.
- *
- * @return The number of weapons owned by the specified player.
- */
- stock GetWeapons( id )
- {
- new weapons[ 32 ], num
- get_user_weapons( id, weapons, num )
- return num
- }
- /**
- * Gets the current map name.
- *
- * @deprecated use MapName if your server support it
- *
- * @return Current map name
- */
- //#pragma deprecated Use MapName instead.
- stock CurrentMap()
- {
- new currentmap[ 35 ]
- get_mapname( currentmap, charsmax( currentmap ) )
- return currentmap
- }
- /**
- * Displays a HUD message to a player.
- *
- * This function can display either a standard HUD message or a dynamic HUD message
- * depending on the value of the 'dhud' parameter.
- *
- * @param id The player ID to whom the message will be displayed.
- * @param dhud If true, displays a dynamic HUD message; otherwise, a standard HUD message.
- * @param msg The message to be displayed.
- * @param r Red color component (0-255) for the message text.
- * @param g Green color component (0-255) for the message text.
- * @param b Blue color component (0-255) for the message text.
- * @param x X-coordinate for the message position (default is -1.0, center).
- * @param y Y-coordinate for the message position (default is 0.35).
- * @param effects Effects to apply to the message (e.g., fade, blink).
- * @param fxtime Duration for effects to take place (default is 6.0 seconds).
- * @param holdtime Duration to hold the message on the screen (default is 12.0 seconds).
- * @param fadeintime Duration for the message to fade in (default is 0.1 seconds).
- * @param fadeouttime Duration for the message to fade out (default is 0.2 seconds).
- *
- * @noreturn
- */
- 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 )
- {
- if( dhud )
- {
- set_dhudmessage( r, g, b, x, y, effects, fxtime, holdtime, fadeintime, fadeouttime )
- show_dhudmessage( id, msg )
- }
- else
- {
- set_hudmessage( r, g, b, x, y, effects, fxtime, holdtime, fadeintime, fadeouttime )
- show_hudmessage( id, msg )
- }
- }
- /**
- * Retrieves the value of a specified console variable (cvar).
- *
- * @param cvar The name of the console variable to retrieve.
- *
- * Example:
- * - GetCvar( "hostname" ) // Retrieves the server's hostname as a string
- *
- * @return The value of the specified console variable as a string.
- */
- stock GetCvar( const cvar[] )
- {
- new cvarname[ 64 ]
- get_cvar_string( cvar, cvarname, charsmax( cvarname ) )
- return cvarname
- }
- /**
- * Retrieves the path to the local information for a specified directory name.
- *
- * @param dirname The name of the directory to retrieve the path for.
- *
- * Example:
- * - GetDirPath( "amxx_logs" ) // Retrieves the directory path
- *
- * Supported Directory Locations:
- * - amxx_logs : Path to logs generated by AMXX (addons/amxmodx/logs)
- * - amxx_configsdir : Path to configuration files (addons/amxmodx/configs)
- * - amxx_datadir : Path to data files used by AMXX (addons/amxmodx/data)
- * - amxx_modules : Path to the modules configuration file (addons/amxmodx/configs/modules.ini)
- * - amxx_plugins : Path to the plugins configuration file (addons/amxmodx/configs/plugins.ini)
- * - amxx_pluginsdir : Path to the directory where plugins are stored (addons/amxmodx/plugins)
- * - amxx_modulesdir : Path to the directory where modules are stored (addons/amxmodx/modules)
- * - amxx_vault : Path to the vault data file (addons/amxmodx/data/vault.ini)
- *
- * @return A string representing the path to the configuration directory.
- */
- stock GetDirPath( const dirname[] )
- {
- new localinfo[ 24 ]
- get_localinfo( dirname, localinfo, charsmax( localinfo ) )
- return localinfo
- }
- /**
- * Logs a message to both the server console and a file, with timestamp support.
- *
- * @param filepath The relative path of the log file under the "amxx_logs" directory.
- * @param TimeZone The timezone offset in seconds (default is 0, for UTC).
- * @param string The format string for the log message.
- * @param any... Additional arguments to format the log message.
- *
- * @noreturn
- */
- stock logfile( const filepath[], TimeZone = 0, const string[], any:... )
- {
- new logpath[ 128 ], logmsg[ 191 ]
- formatex( logpath, charsmax( logpath ), "%s/%s.log", GetDirPath( "amxx_logs" ), filepath )
- vformat( logmsg, charsmax( logmsg ), fmt( "%s | %s", Getformattime( "%H:%M:%S - %d/%m/%y", get_systime( TimeZone ) ), string ), 4 )
- server_print( "%s", logmsg )
- new File = fopen( logpath, "a+" )
- fprintf( File, "%s^n", logmsg )
- fclose( File )
- }
- /**
- * Searches for the first empty line in a file and returns its line number.
- *
- * @param filepath The path to the file to search.
- * @return The line number of the first empty line if found, or -1 if no empty line exists or the file cannot be opened.
- */
- stock findemptyline( const filepath[] )
- {
- new Line[ 512 ], foundlines = -1, linecounter = -1, File = fopen( filepath, "r+" )
- if( File )
- {
- while( !feof( File ) )
- {
- linecounter++
- fgets( File, Line, charsmax( Line ) )
- trim( Line )
- if( !Line[ 0 ] )
- {
- foundlines = linecounter
- break
- }
- }
- fclose( File )
- }
- if( foundlines == -1 ) write_file( filepath, "", foundlines )
- return foundlines
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement