#define UTILHELP_H	aaa
//----------------------------------------------------------------------------
//  @brief
//        this file include procedures for working with cheats
//        

//  @ingroup
    
//  2006Oct05 created. Zmanovsky Peter. перенесено из AS2 (AS2, мы о тебе будем помнить вечно!)
//
//  (C) 2006    Sigma-Team
//-----------------------------------------------------------------------------

#ifndef COMMON_BONUS_H
	#include "maps\common_bonus.lgc"
#endif

#ifndef CONST_VEHI_H
	#include "maps\const_vehi.lgc"
#endif

#ifndef COMMON_STATISTICS_H
    #include "maps\common_statistics.lgc"
#endif

#ifndef UTIL_CRYSTALS_H
    #include "maps\util_crystals.lgc"
#endif

#ifndef UTIL_BUTTONS_H
    #include "maps\util_buttons.lgc"
#endif

//------------------------------------------------------------------------------------------
#define CHEAT_KEYWORD_MAXLEN	15
static int aKeyPressed[ CHEAT_KEYWORD_MAXLEN ];
#define CHEAT_STACK_MAXLEN		10
static int aCheatStack[ CHEAT_STACK_MAXLEN ];
static int iCheatStackSize = 0;
static int gbZmZmZmDebug = false;


//------------------------------------------------------------------------------------------
//remember last CHEAT_KEYWORD_MAXLEN pressed keys
RememberKey( int iKey )
{
	//store capital letters
	if( iKey >= 'a' &&  iKey <= 'z' )
		iKey = iKey - ('a'-'A');

	//remember key
	int i;
	for( i = 0; i < CHEAT_KEYWORD_MAXLEN - 1; i++ )
		aKeyPressed[ i ] = aKeyPressed[ i + 1 ];
	aKeyPressed[ CHEAT_KEYWORD_MAXLEN - 1 ] = iKey;
}


//------------------------------------------------------------------------------------------
CheatPopInt()
{
	if( iCheatStackSize > 0 )
	{
		iCheatStackSize--;
		return aCheatStack[ iCheatStackSize ];
	}
	return -1;
}


//------------------------------------------------------------------------------------------
CheatPushInt( int iVal )
{
	if( iCheatStackSize == CHEAT_STACK_MAXLEN )
	{
		int i; i = 0;
		for( i = 0; i < CHEAT_STACK_MAXLEN - 1; i++ )
			aCheatStack[ i ] = aCheatStack[ i + 1 ];
	}
	else
		iCheatStackSize++;
	//
	aCheatStack[ iCheatStackSize - 1 ] = iVal;
}


//------------------------------------------------------------------------------------------
//	:)))
CheatHappen( string sKeyWord, int iShift = 0 )
{
	sKeyWord = ToUpper( sKeyWord );
	int len; len = strlen( sKeyWord );
	//
	int i;
	int shift; shift = 0;
	i = 0;
	while( i < len )
	{
		int ind; ind = CHEAT_KEYWORD_MAXLEN - len + iShift + i - shift;	//for _d
		bool processed; processed = false;
		int c; c = charat( sKeyWord, i );
		//
		if( ( c == '_' ) && ( i + 1 < len ) )
		{
			int c2; c2 = charat( sKeyWord, i + 1 );
			if( c2 == 'D' )
			{
				if( aKeyPressed[ ind ] >= '0' && aKeyPressed[ ind ] <= '9' )
					CheatPushInt( aKeyPressed[ ind ] - '0' );
				else
					return false;
				processed = true;
				i++;
				shift++;
			}
		}

		if( ( !processed ) && ( c != aKeyPressed[ ind ] ) )
			return false;
		//
		i++;
	}

	//erase keys
	for( i = 0; i < CHEAT_KEYWORD_MAXLEN; i++ )
		aKeyPressed[ i ] = 0;
	Log( "Cheat happens: " + sKeyWord );
	//
	//SendCommand(EXT_CheatHappen);
	//
	return true;
}


//------------------------------------------------------------------------------------------
MakeDamageInRadius(int iRadius, int iDamage)
{
	int xx; xx = GetInputX();
	int yy; yy = GetInputY();
	int unit;
	for( unit = FirstInBox( xx-iRadius, yy-iRadius, xx+iRadius, yy+iRadius ); unit; unit = NextInBox() )
	{
		if( SizeTo(unit, xx, yy) <= iRadius )
			Action(unit, ACT_DAMAGE, iDamage);
	}
}


//------------------------------------------------------------------------------------------
KillMonstInRad(int flagman, int radius)
{
	int i;
	for( i = FirstUnit( U_UNIT ); i; i = NextUnit() )
	{
		if( Action( i, ACT_GET_ARMY ) == 1 )
		{
			int dist; dist = SizeTo(flagman, GetX(i), GetY(i));
			if( dist < radius)
				Action( i, ANI_DEATH );
		}
	}
}


//------------------------------------------------------------------------------------------
CheatsTact()
{
	//initialize array
	int i;
	iff(1)
	{
		for( i = 0; i < CHEAT_KEYWORD_MAXLEN; i++ )
			aKeyPressed[ i ] = 0;
	}
	//
	if( GetKey() )
		RememberKey( GetKey() );
	//
	/*if(!Z_DEBUG && !SendCommand(EXT_IsDebugMode))
	{
		//в сетке и survive нет читов
		if(SendCommand(EXT_GETSURVIVEMODE))
			return;
		#ifdef NET_GAME_RUN
			return;
		#endif
	}*/
	
	//
	//if( Flagman(0) && GetKey() == VK_F6 )
	//	KillMonstInRad(Flagman(0), 1000);
	//
	if( OneTimeInQuarterSecond() )
	{
		//наносим урон 1000 под курсором
		if( CheatHappen( "stddd" ) )
			MakeDamageInRadius(40, 1000);
		//remove gamma
		if( CheatHappen( "remgamma" ) )
			SetGamma( 0x000000 );
		//kill all monsters
		if( (CheatHappen("stkk") ) && Flagman(0) )
			KillMonstInRad(Flagman(0), 1000);
		//
		if( CheatHappen( "stk9" ) )
		{
			for( i = FirstUnit( U_UNIT ); i; i = NextUnit() )
			{
				if( Action( i, ACT_GET_ARMY ) == 1 )
					Action( i, ANI_DEATH );
			}
		}
		//instant win
		if( CheatHappen( "stnn" ) || CheatHappen( "stwnn" ))
			bCheatWin = true;
		if( CheatHappen( "stfff" ) )
			bCheatFly = true;
		//
		if( CheatHappen( "stmus_d_d", 2 ) )
		{
			//change music
			int x2; x2 = CheatPopInt();
			int x1; x1 = CheatPopInt();
			//Action1 = x1 * 10 + x2;
		}
		//
		if( CheatHappen( "cc" ) && Call_v(EXT_GetDebugMode))
			BigCrystalSetBorn(true);
		if( CheatHappen( "stgold" ) || CheatHappen( "stggg" ) )
		{
			gbGoldCoins = !gbGoldCoins;
			PlaySFX(51);
			SetValI("GoldCoins", gbGoldCoins);
		}
		//
		if( CheatHappen( "stmusstop" ) )
			StopMusic();
		//
		if( CheatHappen( "biv" ) ) BnsIncreaseVehi();
		if( CheatHappen( "bdbs" ) ) BnsDecrBallSpeed();
		if( CheatHappen( "bibs" ) ) BnsIncrBallSpeed();
		if( CheatHappen( "blw" ) ) BnsLevelWin();
		if( CheatHappen( "b502" ) ) ChangeAllBallsTo(502);	//бонус превращения шарика в разрушитель
		if( CheatHappen( "bdv" ) ) BnsDecreaseVehi();
		if( CheatHappen( "bal" ) ) BnsAddLife();
		if( CheatHappen( "btb" ) ) BnsTrippleBalls();
		if( CheatHappen( "b501" ) ) ChangeAllBallsTo(501);	//бонус превращения шарика в уменьшенный
		if( CheatHappen( "bvs" ) ) BnsVehSticky();
		if( CheatHappen( "bvg" ) ) BnsVehGun();
		//if( CheatHappen( "bbd" ) ) BnsBallsDeath();
		if( CheatHappen( "b504" ) ) ChangeAllBallsTo(504);	// бонус превращения шарика в взрывающий
		if( CheatHappen( "b500" ) ) ChangeAllBallsTo(500);	// бонус превращения шарика в нормальный
		if( CheatHappen( "b503" ) ) ChangeAllBallsTo(503);	//// бонус превращения шарика в увеличенный
		if( CheatHappen( "bvm" ) ) BnsVehMissile();
		//
		if( CheatHappen( "u_d", 1 ) )
		{
			int type; type = CheatPopInt();
			if(type == 0)
				Call_ii(EXT_SETVEHIRADIUS, HORIZ_RADIUS_0, VERT_RADIUS_0);
			if(type == 1)
				Call_ii(EXT_SETVEHIRADIUS, HORIZ_RADIUS_1, VERT_RADIUS_1);
			if(type == 2)
				Call_ii(EXT_SETVEHIRADIUS, HORIZ_RADIUS_2, VERT_RADIUS_2);
			if(type == 3)
				Call_ii(EXT_SETVEHIRADIUS, HORIZ_RADIUS_3, VERT_RADIUS_3);
			if(type == 4)
				Call_ii(EXT_SETVEHIRADIUS, HORIZ_RADIUS_4, VERT_RADIUS_4);
		}
		//
		iff(VEHI_DBG_ON)
			Call_v(EXT_ToggleVehiDbg);
		if( CheatHappen( "uu" ) )
			Call_v(EXT_ToggleVehiDbg);
		if( CheatHappen( "hh_d_d", 2 ) )
		{
			int x2 = CheatPopInt()-1;
			int x1 = CheatPopInt()-1;
			ShowHelpMenu(x1*100+x2);
		}
		if( CheatHappen( "rr" ) )
			ReloadHelpMenu();
		if( CheatHappen( "zmzmzm" ) )
			gbZmZmZmDebug = true;
		if( CheatHappen( "cursor" ) )
			SetCursor(CURSOR_NORMAL);
		if( CheatHappen( "hponscreen" ) )
			GetTotalHPOnScreen(true);
		if( CheatHappen( "stkill" ) )
			gbCheatAllObjDeath = true;
		//
		if(Z_DEBUG || gbZmZmZmDebug)
		{
			if( CheatHappen( "ss" ) )
			{
				MakeDamageInRadius(10, 30);
				giCheatCrystX = GetInputX();
				giCheatCrystY = GetInputY();
			}
			if( CheatHappen( "dd" ) )
			{
				MakeDamageInRadius(40, 1000);
				giCheatCrystX = GetInputX();
				giCheatCrystY = GetInputY();
			}
			if( CheatHappen( "ee" ) )
				MakeDamageInRadius(100, 1000);
			if( CheatHappen( "kkk" ) && bBossOnScreen)
			{
				CheatBossDeath();
			}
			if( CheatHappen( "dede" ))
			{
				DecLife();
			}
			if(CheatHappen("bb"))
			{
				//AddCrystalScore();
				//CreateFallingGold();
				//ShowHelpMenu(0);
				//TryMakeDeathByBoss();
				//ShowStatistics(STAT_TYPE_FLY);
				//ShowStatistics(STAT_TYPE_NORM);
				//GetTotalHPOnScreen(true);
				ShowStatistics(STAT_TYPE_END);
				//ShowStatistics(STAT_TYPE_TIP_END);
				//AddLife(AL_NONE);
				//CreateGoldSalut(3, GetInputX(), GetInputY());
				//DestoyObjectsOnScreen(true);
				//ShowHelpMenu(8);
				//BeginCylMove();
				//AddCurRecord();
				//SaveCurData();
				//CreateSprite(432, GetInputX(), GetInputY(), 50);
				//EarthQuake(2000);
				//if(PlayerVehi)
				//{
				//	Action(PlayerVehi, ACT_DAMAGE, 1000);
				//	bDeathByBoss = true;
				//}
			}
        }
	}
}


