//==============================================================================
//	littlewhitey Las Venturas Deathmatch (global.pwn)
//      Credits:	adamcs, BrandonB, littlewhitey, Mike, Simon
//      Version:    1.0
//==============================================================================

#if defined __global__included
	#endinput
#endif

#define __global__included

//==============================================================================
// Macros

#define minrand(%1,%2)		random(%2 - %1) + %1
#define dcmd(%1,%2,%3)		if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1  // Created by DracoBlue
#define SendUsage(%1,%2)    SendClientMessage(%1,COLOR_WHITE,"> Использование: " %2)
#define SendError(%1,%2)    SendClientMessage(%1,COLOR_RED,"" %2)

//==============================================================================
// Functions

stock IsStringSame( const string1[ ], const string2[ ], len )
{
	// Created by Sintax.
	// Checks if string1 is the same as string 2.
	
	for(new i = 0; i < len; i++)
	{
	    if ( string1[ i ] != string2[ i ] )
			return 0;
		
		if ( string1[ i ] == '\0' || string1[i] == '\n' )
			return 1;
	}
	return 1;
}

stock GetXYInFrontOfPlayer( PlayerID, &Float:fPlayerX, &Float:fPlayerY, Float:fPlayerDistance )
{
	// Created by Y_Less with a simple mod by Simon (getting vehicle angle).
	// Gets the X and Y coordinates in front of a player.
	
	new
	    _:		iPlayerVehicleID= GetPlayerVehicleID( PlayerID ),
		Float:	fPlayerAngle	= 0.0;

	GetPlayerPos( PlayerID, fPlayerX, fPlayerY, fPlayerAngle );
	
	if ( iPlayerVehicleID )
		GetVehicleZAngle( iPlayerVehicleID, fPlayerAngle );
	else
	    GetPlayerFacingAngle( PlayerID, fPlayerAngle );


	fPlayerX += ( fPlayerDistance * floatsin( -fPlayerAngle, degrees ) );
	fPlayerY += ( fPlayerDistance * floatcos( -fPlayerAngle, degrees ) );
}

stock IsValidSkin(skinid)
{
	// Created by Simon.
	// Checks whether the skinid parsed is crashable or not.

	#define	MAX_BAD_SKINS   14

	new badSkins[MAX_BAD_SKINS] = {
		3, 4, 5, 6, 8, 42, 65, 74, 86,
		119, 149, 208, 273, 289
	};

	if  (skinid < 0 || skinid > 299) return false;
	for (new i = 0; i < MAX_BAD_SKINS; i++) {
	    if (skinid == badSkins[i]) return false;
	}

	#undef MAX_BAD_SKINS
	return true;
}

stock IsPlayerInRange( PlayerID, Float:X, Float:Y, Float:Z, Float:Range, Float:ZRange=4.0 )
{
	/* Checks if the player is in the range of the specified coordinates
	 * using the specified range.
	 */
	 
	new
		Float:pX,
		Float:pY,
		Float:pZ;

	GetPlayerPos( PlayerID, pX, pY, pZ );

	if ( floatsqroot( floatpower( floatabs( floatsub( X, pX ) ),2 ) + floatpower ( floatabs (floatsub( Y, pY ) ),2 ) ) < Range && ( pZ < Z + ZRange ) && ( pZ > Z - ZRange ) )
		return 1;

	else
		return 0;
}

stock strtok( const string[], &index )
{
	/* Seperates a string into chunks using spaces.
	 * Starts checking at index.
	 */
	new length = strlen( string );
	while ((index < length) && (string[index] <= ' '))
	{
		index++;
	}

	new offset = index;
	new result[20];
	while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
	{
		result[index - offset] = string[index];
		index++;
	}
	result[index - offset] = EOS;
	return result;
}

stock IsPlayerInArea( PlayerID, Float:MinX, Float:MaxX, Float:MinY, Float:MaxY, Float:MinZ=-99999.0, Float:MaxZ=99999.9 )
{
	// Checks if the player is inside the cube specified.

	new Float:pX, Float:pY, Float:pZ;

	GetPlayerPos(PlayerID, pX, pY, pZ);

	if (pX > MinX && pX < MaxX && pY > MinY && pY < MaxY && pZ > MinZ && pZ < MaxZ) return 1;
	else return 0;
}

stock IsPointInArea( Float:pX, Float:pY, Float:pZ, Float:MinX, Float:MaxX, Float:MinY, Float:MaxY, Float:MinZ=-99999.0, Float:MaxZ=99999.9 )
{
	// Checks if a point is inside the cube specified.

	if (pX > MinX && pX < MaxX && pY > MinY && pY < MaxY && pZ > MinZ && pZ < MaxZ) return 1;
	else return 0;
}

stock sscanf(string[], format[], {Float,_}:...)
{
	new
		formatPos,
		stringPos,
		paramPos = 2,
		paramCount = numargs();
	while (paramPos < paramCount && string[stringPos])
	{
		switch (format[formatPos])
		{
			case '\0': break;
			case 'i', 'd': setarg(paramPos, 0, strval(string[stringPos]));
			case 'c': setarg(paramPos, 0, string[stringPos]);
			case 'f': setarg(paramPos, 0, _:floatstr(string[stringPos]));
			case 's':
			{
				new
					end = format[formatPos + 1] == '\0' ? '\0' : ' ',
					i;
				while (string[stringPos] != end) setarg(paramPos, i++, string[stringPos++]);
				setarg(paramPos, i, '\0');
			}
			default: goto skip;
		}
		while (string[stringPos] && string[stringPos] != ' ') stringPos++;
		while (string[stringPos] == ' ') stringPos++;
		paramPos++;
		skip:
		formatPos++;
	}
	return format[formatPos] ? 0 : 1;
}

stock IsNumeric(const string[]) {
	new length=strlen(string);
	if (length==0) return false;
	for (new i = 0; i < length; i++) {
		if (
		(string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
		|| (string[i]=='-' && i!=0)                                             // A '-' but not at first.
		|| (string[i]=='+' && i!=0)                                             // A '+' but not at first.
		) return false;
	}
	if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
	return true;
}

stock strcpy( dest[ ], src[ ], startdest = 0, startsrc = 0 )
{
	// Created by Peter
	
	for ( new i = startsrc, j = strlen( src ); i < j; i++ )
	{
		dest[ startdest++ ] = src[ i ];
	}
	dest[ startdest ] = 0;
}

#define WEAPON_NOSLOT -1

stock GetWeaponSlot(weaponid)
{
	// Created by Betamaster.
	
	new slot;
	switch(weaponid){
		case 0, 1: slot = 0;            // No weapon
		case 2 .. 9: slot = 1;          // Melee
		case 22 .. 24: slot = 2;        // Handguns
		case 25 .. 27: slot = 3;        // Shotguns
		case 28, 29, 32: slot = 4;      // Sub-Machineguns
		case 30, 31: slot = 5;          // Machineguns
		case 33, 34: slot = 6;          // Rifles
		case 35 .. 38: slot = 7;        // Heavy Weapons
		case 16, 18, 39: slot = 8;      // Projectiles
		case 42, 43: slot = 9;          // Special 1
		case 14: slot = 10;             // Gifts
		case 44 .. 46: slot = 11;       // Special 2
		case 40: slot = 12;             // Detonators
		default: slot = WEAPON_NOSLOT;  // No slot
	}
	return slot;
}

stock power( x, y )
{
	assert y >= 0;
	new r = 1;
	for ( new i = 0; i < y; i++ )
		r *= x;
	return r;
}

