#define  UTIL_NEWSURVIVE_H aaa
//----------------------------------------------------------------------------
//  @brief
//		new survive

//  @ingroup
    
//  2007Nov15 created. Zmanovsky Peter.
//  (C) 2007    Sigma-Team
//-----------------------------------------------------------------------------

#include "maps\survive_birth_new.lgc"
#include "maps\monsters.lgc"

#define MAX_TURRELS			4

static int giTurrels[MAX_TURRELS];
int giExplMark = 0;
int HelpText2 = 0;

//------------------------------------------------------------------------------------------
InitNewSurvive()
{
	int i;
	for( i = 0; i < sizeof(MonstersVid)/4; i++ )
		SetVidData(MonstersVid[i],VID_DETECTRANGE,2000);
	//
	int spr = FindNearestSprite(GETSPRITE_VID_FLAG + 350, 0, 0, 20000);
	if(spr)
	{
		SetFlagman(0, spr);
		SetVidData(350, VID_MAXHP, 1000);
		Action(spr, ACT_SET_HP, 1000);
		//
		giExplMark = CreateSprite(2215, GetX(spr), GetY(spr), GetZ(spr) + 70);
		Action(giExplMark, ACT_SET_INVISIBLE, 1);
	}
	//test
	//SetVidData(2130, VID_SPEED, GetVidData(2130, VID_SPEED) / 2);
	//SetVidData(2130, VID_GAMMA+1,0xDAEE80);
	//
	SetVidData(352, VID_CHILD+ANI_FIGHT, 137);
	SetVidData(352, VID_NO_CHILD+ANI_FIGHT, 2);
	//
	SetVidData(137, VID_CHILD+ANI_FIGHT, 196);
	//
	//SetVidData(156, VID_DAMAGE, 345);
	//
	//уменьшаем стреляющим в 2 раза
	SetVidData(1780, VID_BATTLERANGE, GetVidData(1780, VID_BATTLERANGE) / 2);
	SetVidData(2070, VID_BATTLERANGE, GetVidData(2070, VID_BATTLERANGE) / 2);
	SetVidData(2071, VID_BATTLERANGE, GetVidData(2071, VID_BATTLERANGE) / 2);
}


//------------------------------------------------------------------------------------------
CreateMonst4NewSurv(int iVid)
{
	int x = -1, y = -1;
	int untiBug = 20;
	do
	{
		x=Random(3);
		if     ( x==0 ) { x=-50;                y=Random(MapSizeY()); }
		else if( x==1 ) { x=MapSizeX()+50;      y=Random(MapSizeY()); }
		else if( x==2 ) { x=Random(MapSizeX()); y=-50;                }
		else            { x=Random(MapSizeX()); y=MapSizeY()+50;      }
		//
		untiBug--;
		//
	} while( CanPlace(iVid,x,y,0) && untiBug >= 0);
	//
	//Log3("vid = " + itoa(iVid) + " coor = " + itoa(x) + ", " + itoa(y));
	if(x != -1 || y != -1)
	{
		int unit = CreateSprite(iVid,x,y,0);
		Action( unit, ACT_ATTACK,Flagman(0),0);
	}
}


//------------------------------------------------------------------------------------------
NewSurviveTact()
{
	int i, j;
	//
	int shift = 0;
	int t = (GetTime()-StartGameTime) / 1000;
	while(shift < NS_PRD_CNT && t>=SurvPeriodsLen[shift])
	{
		t -= SurvPeriodsLen[shift];
		shift++;
	}
    if(shift >= NS_PRD_CNT)
		shift = NS_PRD_CNT-1;
//
//static int phase_text = 0;
//string text = itoa(shift);
//iff(1)
//    phase_text = CreateSprite(5, 50, 240, 200);
//Action(phase_text, ACT_SET_TEXT, &text);
//
	shift *= MONST_VID_CNT;
	//
	int fX = GetX(Flagman(0));
	int fY = GetY(Flagman(0));
	//
	//рожаем монстров, которых должно быть постоянное кол-во на карте
	for(i = 0; i < MONST_VID_CNT; i++)
	{
		if(MonsterBirthNew[shift+i] < 0)
		{
			int curCnt = 0;
			//
			if(MonstersVid[i] >= 1760 && MonstersVid[i] <= 1762 || MonstersVid[i] >= 1770 && MonstersVid[i] <= 1772)
			{
				curCnt =  GetVidData(1760, VID_COUNT);
				curCnt += GetVidData(1761, VID_COUNT);
				curCnt += GetVidData(1762, VID_COUNT);
				curCnt += GetVidData(1770, VID_COUNT);
				curCnt += GetVidData(1771, VID_COUNT);
				curCnt += GetVidData(1772, VID_COUNT);
			}
			else
				curCnt = GetVidData(MonstersVid[i], VID_COUNT);
			//
			int d = -MonsterBirthNew[shift+i] - curCnt;
			for(j = 0; j < d; j++)
				CreateMonst4NewSurv(MonstersVid[i]);
		}
	}
	//
	int unit;
	//
	static int siPrevTime = 0;
	if(!siPrevTime || GetTime()-siPrevTime >= 1000)
	{
		siPrevTime = GetTime();
		//
		for(unit=FirstUnit(U_UNIT); unit; unit=NextUnit())
		{
			if(Action(unit, ACT_GET_ARMY)==1)
			{
				int spr = 0;
				//
				for(i = 0; i < MAX_TURRELS; i++)
				{
					if(giTurrels[i] && SizeTo(unit, GetX(giTurrels[i]), GetY(giTurrels[i])) <= 400)
					{
						spr = giTurrels[i];
						Action(giTurrels[i], ACT_ATTACK, unit);
						break;
					}
				}
				//
				if(!spr)
    				spr = FindNearestSprite(GETSPRITE_UNIT + GETSPRITE_ARMY0, GetX(unit), GetY(unit), 600);
//					spr = FindNearestSprite(GETSPRITE_VID_FLAG + 2210, GetX(unit), GetY(unit), 80);
				if(spr)
					Action(unit, ACT_ATTACK, spr, 0);
				else
					Action(unit, ACT_ATTACK,Flagman(0),0);
			}
		}
	}
	//
	//взрыв при клике правой кнопкой мыши
	giExplChargePrcnt = 0;
	if(USE_EXPLOSION)
	{
		static int siPrevTime2 = 0;
		if(siPrevTime2 == 0 && PlayerScores)
			siPrevTime2 = GetTime();
		if(GetTime()-siPrevTime2 >= EXPLOSION_CHARGE && GetInputState() & INPUT_RCLICK && Flagman(0))
		{
			siPrevTime2 = GetTime();
			//
			CreateSprite(157, fX, fY+0, 0);
			EarthQuake(300);
            Effect(EFF_NUKE,0xffffff);
			//
			for(unit=FirstUnit(U_UNIT); unit; unit=NextUnit())
			{
				if(Action(unit, ACT_GET_ARMY)==1 && SizeTo(unit, fX, fY) <= EXPLOSION_DMG_RADIUS)
					Action(unit, ACT_DAMAGE, 1000);
			}
		}
		//
		if(siPrevTime2)
		{
			giExplChargePrcnt = 100 * (GetTime()-siPrevTime2) / EXPLOSION_CHARGE;
			if(giExplChargePrcnt > 100)
			{
				giExplChargePrcnt = 100;
				Action(giExplMark, ACT_SET_INVISIBLE, 0);
				//
				iff(1)
				{
					if(HelpText)
					{
						Destroy(HelpText);
						HelpText = 0;
					}
					HelpText2 = CreateText(3, ScreenX()/2, ScreenY()-HELP_TEXT_BTM_SCRENN, 0,
															GetString("menu", "SurvExplReady"), BEH_CENTER);
				}
			}
			else
			{
				Action(giExplMark, ACT_SET_INVISIBLE, 1);
				if(HelpText2)
				{
					Destroy(HelpText2);
					HelpText2 = 0;
				}
			}
		}
	}
	//
	//
	/*int TURREL_COST = 1500;
	if(GetKey()=='1' && !giTurrels[0] && giExpa >= TURREL_COST)
	{
		giTurrels[0] = CreateSprite(2301, fX - 200, fY + 200, 0);
		giExpa -= TURREL_COST;
	}
	//
	if(GetKey()=='2' && !giTurrels[1] && giExpa >= TURREL_COST)
	{
		giTurrels[1] = CreateSprite(2301, fX - 200, fY - 200, 0);
		giExpa -= TURREL_COST;
	}
	//
	if(GetKey()=='3' && !giTurrels[2] && giExpa >= TURREL_COST)
	{
		giTurrels[2] = CreateSprite(2301, fX + 200, fY - 200, 0);
		giExpa -= TURREL_COST;
	}
	//
	if(GetKey()=='4' && !giTurrels[3] && giExpa >= TURREL_COST)
	{
		giTurrels[3] = CreateSprite(2301, fX + 200, fY + 200, 0);
		giExpa -= TURREL_COST;
	}*/
}


//------------------------------------------------------------------------------------------
/*F2301_15(int unit)
{
	int i;
	for(i = 0; i < MAX_TURRELS; i++)
	{
		if(giTurrels[i] == unit)
		{
			giTurrels[i] = 0;
			break;
		}
	}
}
F2301_16(int unit) {F2301_15(unit);}*/


//------------------------------------------------------------------------------------------
F2210_DAMAGE(int unit,int beating_unit, int damage)
{
	if(Action(beating_unit, ACT_GET_ARMY) != 1)
		return 1;
	return 0;
}

