#include "stdafx.h"
#include "Global_variables_extern.h"
#include "Basics_functions.h"
#include "Vampirism_fire.h"
#include "DB_Cost.h"
#include "HumanTactics.h"
#include "VampireTactics.h"

integer fBeginGameProcess()
{
	hTriggerChat = 0;
	vStartCommandsGo = 0;
	vStartCommands = 0;
	if (fStartCommands() == 0) return 0;
	try
	{
	//sprintf(vTest,"vNPlayerAI = %d",vNPlayerAI );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	//sprintf(vTest,"vHost = %d",vHost );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	HPLAYER vPlayerAI;
	char vNickName[200];
	int vLen;
	bool vFlagAI;
	integer vNPlayerAI;
	int vvi;
	for (vvi = 0; vvi < MAX_PLAYERS; vvi++)
	{
	vNPlayerAI = vvi;
	vFlagAI = FALSE;
	vPlayerAI = Player(vNPlayerAI);
	sprintf(vNickName,"%s\0",GetPlayerName(vPlayerAI));
	vLen = strlen(vNickName);
	if (vLen == 12 || vLen == 13)
	{
		if (memcmp(vNickName,"РРіСЂРѕРє ",10) == 0)
		{
			continue;
		}
	}
	if (vLen == 7)
	{
		if (memcmp(vNickName,"Human ",vLen-1) == 0)
		{
			vFlagAI = TRUE;
			goto m1;
		}
	}
	if (vLen == 8)
	{
		if (memcmp(vNickName,"Human ",vLen-2) == 0)
		{
			vFlagAI = TRUE;
			goto m1;
		}
	}
	if (vLen == 10)
	{
		if (memcmp(vNickName,"Vampire ",vLen-2) == 0)
		{
			vFlagAI = TRUE;
			goto m1;
		}
	}
	if (vFlagAI == FALSE)
	{
		if (vNPlayerAI >= 0 && vNPlayerAI <= 11)
		{
			pPlayerUser[vCountPlayersUser].vNPlayer = vNPlayerAI;
			pPlayerUser[vCountPlayersUser].vPlayer = vPlayerAI;
			sprintf(pPlayerUser[vCountPlayersUser].vName,"%s\0",vNickName);
			sprintf(pPlayerUser[vCountPlayersUser].vKeyPlayer,"%d\0",vNPlayerAI);
			pPlayerUser[vCountPlayersUser].vDead = FALSE;
			pPlayerUser[vCountPlayersUser].vLeave = FALSE;
			//pPlayerUser[vCountPlayersUser].hTriggerPlayerLeave = CreateTrigger();
			//IncrementHandleReferences(pPlayerUser[vCountPlayersUser].hTriggerPlayerLeave);
			//TriggerAddAction(pPlayerUser[vCountPlayersUser].hTriggerPlayerLeave, &fActionPlayerLeave);
			//TriggerRegisterPlayerEvent(pPlayerUser[vCountPlayersUser].hTriggerPlayerLeave,vPlayerAI,EVENT_PLAYER_LEAVE);
			if (hTriggerChat)TriggerRegisterPlayerChatEvent(hTriggerChat,vPlayerAI,"-host",true);
			if (hTriggerChat)TriggerRegisterPlayerChatEvent(hTriggerChat,vPlayerAI,"-kill",false);
			vCountPlayersUser++;
		}
		if (vvi < MAX_HUMAN_PLAYERS)
		{
			fCreateDataUnit(fGetUnit(ID_UNIT_MAIN_HUMAN1,vPlayerAI),THREAD_MAIN);
		}
		continue;
	}
m1:
	if (vNPlayerAI >= 0 && vNPlayerAI <= 9)
	{
		pHumanPlayerGlobal[vCountHumans].vParamPlayer.vAI = vNPlayerAI;
		pHumanPlayerGlobal[vCountHumans].vParamPlayer.vPlayer = vPlayerAI;
		vCountHumans++;
	}
	if (vNPlayerAI >= 10 && vNPlayerAI <= 11)
	{
		pVampirePlayerGlobal[vCountVampires].vParamPlayer.vAI = vNPlayerAI;
		pVampirePlayerGlobal[vCountVampires].vParamPlayer.vPlayer = vPlayerAI;
		vCountVampires++;
	}
	}
	if (vCountVampires == 1)
	{
		fSendAllPlayer("The vampire alone will not play.");
		return 0;
	}
	if (vGameBegin == 0 && ((vCountHumans > 0 && vCountVampires == 2) || (vCountPlayersUser > 0 && (vCountHumans > 0 || vCountVampires > 0))))
	{
		if (vMapBases.fInit() == FALSE) return 0;
		vAuthorization = 1;
		vGameBegin = 1;
		vTimerActionsGame = CreateTimer();
		//sprintf(vTest,"vCountHumans = %d",vCountHumans );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		//sprintf(vTest,"vCountVampires = %d",vCountVampires );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		//vSync = FALSE;
		//vCountForLoopGame = 0;
		/*char vKeyAIPlayer[3];
		for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			for (int vvj = 0; vvj < MAX_PLAYERS; vvj++)
			{
				sprintf(vKeyAIPlayer,"%d\0",vvj);
				StoreInteger(vSyncCache,vKeyAIPlayer,pPlayerUser[vvi].vKeyPlayer,0);
			}
		}
		vSync = FALSE;
		vCountForLoopGame = 0;
		for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			StoreInteger(vSyncCache,"LoadMap",pPlayerUser[vvi].vKeyPlayer,0);
		}*/
		TimerStart(vTimerActionsGame,0.1,true,&fGame);
		vTimerDataUnit2 = CreateTimer();
		TimerStart(vTimerDataUnit2,1,true,&fDataUnit2);
	}
	}
	catch(...)
	{
		return 0;
	}
	return 1;
}

void fActionPlayerLeave()
{
	sPlayerUser * pPlayerUser2 = fGetPlayerUser(GetTriggeringTrigger());
	if (pPlayerUser2 == NULL) return;
	pPlayerUser2->vLeave = TRUE;
	fDelDataUnit((unsigned short)pPlayerUser2->vNPlayer);
	if (GetStoredInteger(vAuthorizationCache,"Map","vHostPlayer") == GetLocalPlayer())
	{
		fFreeHost();
		fSendAllPlayer("The host is out. You can enter the ""-host"" command again.");
	}
}

sPlayerUser * fGetPlayerUser(HTRIGGER hTrigger)
{
	if (hTrigger == NULL) return NULL;
	int vvi;
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (pPlayerUser[vvi].hTriggerPlayerLeave == hTrigger)
		{
			return &pPlayerUser[vvi];
		}
	}
	return NULL;
}

integer fBeginGameProcessAI(integer vNPlayerAI)
{
	/*if (fStartCommands() == 0)
	{
		if (fCheckStartCommands() == 0)
		{
			return 0;
		}
	}
	int vRandBase;
	int vCountBase = 0;
	int vvi,vvj;
	StackForUnits *pStackForUnits;
	if (vNPlayerAI >= 0 && vNPlayerAI <= 9)
	{
		pHumanPlayer[vCountHumans].vParamPlayer.vAI = vNPlayerAI;
		pHumanPlayer[vCountHumans].vParamPlayer.vPlayer = Player(vNPlayerAI);
			if (pHumanPlayer[vCountHumans].vParamPlayer.vTimerPlayer > 0)
			{
				DestroyTimer(pHumanPlayer[vCountHumans].vParamPlayer.vTimerPlayer);
			}
			pHumanPlayer[vCountHumans].vIdBase = -1;
			pHumanPlayer[vCountHumans].vIdBase = pHumanPlayer[vCountHumans].vParamPlayer.vAI;
			pHumanPlayer[vCountHumans].fInit();
			pHumanPlayer[vCountHumans].vParamPlayer.fInit(200,50);
			pHumanPlayer[vCountHumans].vActionStackForUnits.fInitStackForUnits(&pHumanPlayer[vCountHumans].vParamPlayer);
			fCreateDataUnit(fGetUnit(ID_UNIT_MAIN_HUMAN1,pHumanPlayer[vCountHumans].vParamPlayer.vPlayer));
			pHumanPlayer[vCountHumans].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vIdForComments = COMMENT_MAIN_HUMAN;
			pHumanPlayer[vCountHumans].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId = (unsigned short)pHumanPlayer[vCountHumans].vParamPlayer.vAI;
			pHumanPlayer[vCountHumans].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit = vDataUnits[pHumanPlayer[vCountHumans].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId].pDataUnit; 
			pStackForUnits = (StackForUnits*) malloc(sizeof(StackForUnits));
			pStackForUnits->pIdDataUnit.vId = pHumanPlayer[vCountHumans].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId;
			pStackForUnits->pIdDataUnit.pDataUnit = pHumanPlayer[vCountHumans].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit;
			pStackForUnits->vIdForComments = COMMENT_MAIN_HUMAN;
			pHumanPlayer[vCountHumans].vActionStackForUnits.fAddStackForUnits(pStackForUnits);
			pHumanPlayer[vCountHumans].vParamPlayer.vTimerPlayer = CreateTimer();
			TimerStart(pHumanPlayer[vCountHumans].vParamPlayer.vTimerPlayer,0.1,true,&fGameHuman);
			SaveInteger(vHT,pHumanPlayer[vCountHumans].vParamPlayer.vTimerPlayer,CONST_AI_PLAYER,(integer)&pHumanPlayer[vCountHumans]);
		vCountHumans++;
	}
	if (vNPlayerAI >= 10 && vNPlayerAI <= 11)
	{
		pVampirePlayer[vCountVampires].vParamPlayer.vAI = vNPlayerAI;
		pVampirePlayer[vCountVampires].vParamPlayer.vPlayer = Player(vNPlayerAI);
			if (pVampirePlayer[vCountVampires].vParamPlayer.vTimerPlayer > 0)
			{
				DestroyTimer(pVampirePlayer[vCountVampires].vParamPlayer.vTimerPlayer);
			}
			pVampirePlayer[vCountVampires].fInit();
			pVampirePlayer[vCountVampires].vParamPlayer.fInit(50,50);
			pVampirePlayer[vCountVampires].vActionStackForUnits.fInitStackForUnits(&pVampirePlayer[vCountVampires].vParamPlayer);
			pVampirePlayer[vCountVampires].vParamPlayer.vTimerPlayer = CreateTimer();
			TimerStart(pVampirePlayer[vCountVampires].vParamPlayer.vTimerPlayer,0.1,true,&fGameVampire);
			SaveInteger(vHT,pVampirePlayer[vCountVampires].vParamPlayer.vTimerPlayer,CONST_AI_PLAYER,(integer)&pVampirePlayer[vCountVampires]);
		vCountVampires++;
	}
	//if (vGameBegin == 0 && (vCountHumans > 0 && vCountVampires > 0))
	//{
		vAuthorization = 1;
		//vGameBegin = 1;
		//vTimerActionsGame = CreateTimer();
		//sprintf(vTest,"vCountHumans = %d",vCountHumans );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		//sprintf(vTest,"vCountVampires = %d",vCountVampires );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		//TimerStart(vTimerActionsGame,0.1,false,&fGame);			
		/*HPLAYER vPlayerAI;
		char vNickName[200];
		int vLen;
		//integer vNPlayerAI;
		
		for (vvi = 0; vvi < MAX_HUMAN_PLAYERS; vvi++)
		{
			sprintf(vNickName,"%s\0",GetPlayerName(Player(vvi)));
			vLen = strlen(vNickName);
			if (vLen == 12)
			{
				if (memcmp(vNickName,"РРіСЂРѕРє ",vLen-2) == 0)
				{
					continue;
				}
				fCreateDataUnit(fGetUnit(ID_UNIT_MAIN_HUMAN1,vPlayerAI));
			}
		}*/
	//}
	return 1;
}

void fGame()
{
	//fSendAllPlayer("Please wait...");
	//sprintf(vTest,"Please wait...");
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	/*for (int vvj = 10; vvj > 0; vvj--)
	{
		sprintf(vTest,"...%d",vvj);
		fSendAllPlayer(vTest);
		Sleep(1000);
	}*/
	/*if (vCountPlayersUser > 1)
	{
		Sleep(10000);
	}*/
	//if (TimerGetElapsed(vTimerGame) < 0.3) return;
	if (vSync == FALSE)
	{
		sprintf(vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		//if (GetStoredInteger(vSyncCache,vKeyLocalPlayer,vKeyLocalPlayer) != 1)
		//{
			StoreInteger(vSyncCache3,vKeyLocalPlayer,vKeyLocalPlayer,1);
			SyncStoredInteger(vSyncCache3,vKeyLocalPlayer,vKeyLocalPlayer);
			//StoreInteger(vSyncCache,vKeyLocalPlayer,vKeyLocalPlayer,0);
		//}
		sprintf(vKeyLocalPlayer,"\0");

		for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			if (pPlayerUser[vvi].vLeave == FALSE)
			{
				if (GetStoredInteger(vSyncCache3,pPlayerUser[vvi].vKeyPlayer,pPlayerUser[vvi].vKeyPlayer) != 1)
				{
					return;
				}
				//else
				//{
					//if (pPlayerUser[vvi].vNPlayer == GetPlayerColor(GetLocalPlayer()))
					//{
					//	SyncStoredInteger(vSyncCache,pPlayerUser[vvi].vKeyPlayer,pPlayerUser[vvi].vKeyPlayer);
						//StoreInteger(vSyncCache,pPlayerUser[vvi].vKeyPlayer,pPlayerUser[vvi].vKeyPlayer,0);
					//}
				//}
			}
		}
		vSync = TRUE;
		//sprintf(vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		//FlushStoredMission(vSyncCache,vKeyLocalPlayer);
		//sprintf(vKeyLocalPlayer,"\0");
	}
	if (vSync == TRUE && vSync2 == FALSE)
	{
		sprintf(vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		StoreInteger(vSyncCache3,"LoadMap",vKeyLocalPlayer,1);
		SyncStoredInteger(vSyncCache3,"LoadMap",vKeyLocalPlayer);
		StoreInteger(vSyncCache3,"LoadMap",vKeyLocalPlayer,0);
		sprintf(vKeyLocalPlayer,"\0");
		vSync2 = TRUE;
	}
	if (vSync2 == TRUE)
	{
		for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			if (pPlayerUser[vvi].vLeave == FALSE)
			{
				if (GetStoredInteger(vSyncCache3,"LoadMap",pPlayerUser[vvi].vKeyPlayer) != 1)
				{
					return;
				}
			}
		}
	}
	vSync = FALSE;
	vStartGameSleep = TRUE;
	//real vTimeGetElapsed = TimerGetElapsed(vTimerGame);
	//sprintf(vTest,"vTimeGetElapsed = %f",vTimeGetElapsed);
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	//if (vTimeGetElapsed < 0.5) return;
	//sprintf(vTest,"fGame()");
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	/*if (vCountForLoopGame > 0.1 * 100  * 40)
	{
		goto m11;
	}
	vCountForLoopGame++;
	//if (TimerGetElapsed(vTimerGame) < 3) return;
	if (vSync == FALSE)
	{
		sprintf(vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		StoreInteger(vSyncCache,"LoadMap",vKeyLocalPlayer,1);
		SyncStoredInteger(vSyncCache,"LoadMap",vKeyLocalPlayer);
		StoreInteger(vSyncCache,"LoadMap",vKeyLocalPlayer,0);
		sprintf(vKeyLocalPlayer,"\0");
		vSync = TRUE;
	}
	if (vSync == TRUE)
	{
		//sprintf(vBuf,"%d\0",vCountPlayersUser);
		//fLog(31,vBuf);
		for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			if (pPlayerUser[vvi].vLeave == TRUE)
			{
				fLog(32,"+++");
			}
			if (pPlayerUser[vvi].vLeave == FALSE)
			{
				if (GetStoredInteger(vSyncCache,"LoadMap",pPlayerUser[vvi].vKeyPlayer) == 0)
				{
					return;
				}
			}
		}
	}
	fLog(0,"0");
	fLog(30,"---");*/
	//sprintf(vTest,"fGame()");
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	int vRandBase;
	int vCountBase;
	int vvi,vvj;
	bool vFlag;
	StackForUnits *pStackForUnits;
	//integer vAsid = 0x41736964;
	//UnitAddAbility(vDataUnits[vTopShop].pDataUnit->WhichUnit,vAsid);
	//integer vAneu = 0x416e6575;
	//UnitAddAbility(vDataUnits[vTopShop].pDataUnit->WhichUnit,vAneu);
	//HGROUP g = CreateGroup();
	//GroupEnumUnitsInRange(g,0,0,5000,Filter(&fFilterMainHuman));
	//DestroyGroup(g);
	//AddItemToStock(vDataUnits[vTopShop].pDataUnit->WhichUnit,ID_SHOP_ANEU,1,1);
	if (vCountHumans > 0)
	{
		for (vvi = 0; vvi < vCountHumans; vvi++)
		{
			//sprintf(vTest,"fGame_1()");
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			//sprintf(vTest,"pHumanPlayer[vvi].vParamPlayer.vTimerPlayer = %d",pHumanPlayer[vvi].vParamPlayer.vTimerPlayer);
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			if (pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer > 0)
			{
				DestroyTimer(pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer);
			}
			//sprintf(vTest,"fGame_2()");
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			//pHumanPlayerGlobal[vvi].vIdBase = -1;
			//pHumanPlayerGlobal[vvi].vIdBase = pHumanPlayerGlobal[vvi].vParamPlayer.vAI;
			pHumanPlayerGlobal[vvi].fInit();
			pHumanPlayerGlobal[vvi].vBase.fInit();
			// Выбераем базу
			vCountBase = 0;
			vRandBase = 0;
			while(vCountBase < vMapBases.MAP_MAX_BASE)
			{
				if (vvi < 6) vRandBase = GetRandomInt(0,vMaxBase);//rand() % (vMaxBase + 1);
				else vRandBase++;
				if(vRandBase != 0)
				{
					vFlag = FALSE;
					for (vvj = 0; vvj < vCountHumans; vvj++)
					{
						if (pHumanPlayerGlobal[vvj].vBase.vNBase == vRandBase)
						{
							vFlag = TRUE;
							break;
						}
					}
					if (vFlag == FALSE)
					{
						pHumanPlayerGlobal[vvi].vBase.vNBase = vRandBase;
						pHumanPlayerGlobal[vvi].vBase.vMainWall = NULL;
						pHumanPlayerGlobal[vvi].vBase.vQCells = vMapBases.fQCellsToBase(&pHumanPlayerGlobal[vvi].vBase);
						if (pHumanPlayerGlobal[vvi].vBase.vQCells < 400)
						{
							pHumanPlayerGlobal[vvi].vBase.vNBase = DO_NOT_FIND_BASE;
						}
						else
						{
							vMapBases.fDetermineTheBeginningOfTheBaseOneCell(&pHumanPlayerGlobal[vvi].vBase);
							vMapBases.fDetermineTheBeginningOfTheBase(&pHumanPlayerGlobal[vvi].vBase);
							if (pHumanPlayerGlobal[vvi].vBase.vMainWall != NULL &&
								((pHumanPlayerGlobal[vvi].vBase.vMainWall->vRemoteness > 90 && vvi%2 == 0) || vvi%2 == 1))
							{
								if (vMapBases.fZoneTower(&pHumanPlayerGlobal[vvi].vBase) == FALSE)
								{
									pHumanPlayerGlobal[vvi].vBase.vNBase = DO_NOT_FIND_BASE;
								}
								else
								{
									if (vMapBases.fQSmallCageForTower(&pHumanPlayerGlobal[vvi].vBase) < 80)
									{
										pHumanPlayerGlobal[vvi].vBase.vNBase = DO_NOT_FIND_BASE;
									}
									else
									{
										pHumanPlayerGlobal[vvi].vBase.pHumanPlayer = &pHumanPlayerGlobal[vvi];
										pHumanPlayerGlobal[vvi].vFlagDearBase = TRUE;
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 0) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","красный");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 1) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","синий");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 2) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","салатовый");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 3) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","фиолетовый");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 4) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","жёлтый");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 5) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","оранжевый");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 6) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","зеленый");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 7) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","розовый");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 8) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","серый");
										if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == 9) sprintf(pHumanPlayerGlobal[vvi].vBase.vColor,"%s\0","голубой");
										break;
									}
								}
							}
							else
							{
								pHumanPlayerGlobal[vvi].vBase.vNBase = DO_NOT_FIND_BASE;
							}
						}
					}
				}
				vCountBase++;
			}
			if (vvi%2 == 1) pHumanPlayerGlobal[vvi].vBase.vSlayerBaze = TRUE;
			// Базы может также не оказаться вообще, тогда игрок ливает в начале игры, это происходит на одноклеточных проходах баз. Они не допустимы на базе.
			/*if (pHumanPlayerGlobal[vvi].vBase.vNBase == 1)//DO_NOT_FIND_BASE)
			{
				pHumanPlayerGlobal[vvi].vBase.vMainWall = &vMapBases.vMap[13*256+2];
				pHumanPlayerGlobal[vvi].vBase.vBeginningOfBase = pHumanPlayerGlobal[vvi].vBase.vMainWall->vRemoteness;
			}*/
			pHumanPlayerGlobal[vvi].vParamPlayer.fInit(200,50);
			pHumanPlayerGlobal[vvi].vActionStackForUnits.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsGoTo.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsResearch.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsWorkers.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsSlayer.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsMainHuman.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsTower.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsFurbolgs.fInitStackForUnits(&pHumanPlayerGlobal[vvi].vParamPlayer);
			//
			fCreateDataUnit(fGetUnit(ID_UNIT_MAIN_HUMAN1,pHumanPlayerGlobal[vvi].vParamPlayer.vPlayer),THREAD_MAIN);
			// MainHuman
			pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vIdForComments = COMMENT_MAIN_HUMAN;
			pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId = (unsigned short)pHumanPlayerGlobal[vvi].vParamPlayer.vAI;
			pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit = vDataUnits[pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId].pDataUnit; 
			//pStackForUnits = (StackForUnits*) malloc(sizeof(StackForUnits));
			//pStackForUnits->pIdDataUnit = (IdDataUnit*) malloc(sizeof(IdDataUnit));
			//pStackForUnits = new struct StackForUnits;
			//pStackForUnits->pIdDataUnit = new struct IdDataUnit;
			//pStackForUnits->pIdDataUnit->vId = pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId;
			//pStackForUnits->pIdDataUnit->pDataUnit = pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit;
			//pStackForUnits->vIdForComments = COMMENT_MAIN_HUMAN;
			//pHumanPlayerGlobal[vvi].vActionStackForUnits.fAddStackForUnits(pStackForUnits);
			pHumanPlayerGlobal[vvi].vActionStackForUnits.fAddStackForUnits(pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId,
																		   pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit,
																		   COMMENT_MAIN_HUMAN);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsMainHuman.fAddStackForUnits(pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId,
																		   pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit,
																		   COMMENT_MAIN_HUMAN);
			pHumanPlayerGlobal[vvi].vActionStackForUnitsGoTo.fAddStackForUnits(pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId,
																		   pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit,
																		   COMMENT_MAIN_HUMAN);
			pHumanPlayerGlobal[vvi].vMainHuman.vUnitParam.pDataUnits = &vDataUnits[pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vId];
			fInitSpellsUnitsByHuman(&pHumanPlayerGlobal[vvi]);
			// Slayer
			/*pHumanPlayer[vvi].vParamPlayer.vUnit[COMMENT_SLAYER].vIdForComments = COMMENT_SLAYER;
			pHumanPlayer[vvi].vParamPlayer.vUnit[COMMENT_SLAYER].vId = (unsigned short)pHumanPlayer[vvi].vParamPlayer.vAI + 2 + 9 + 1;
			pHumanPlayer[vvi].vParamPlayer.vUnit[COMMENT_SLAYER].pDataUnit = vDataUnits[pHumanPlayer[vvi].vParamPlayer.vUnit[COMMENT_SLAYER].vId].pDataUnit; 
			pStackForUnits = (StackForUnits*) malloc(sizeof(StackForUnits));
			pStackForUnits->pIdDataUnit.vId = pHumanPlayer[vvi].vParamPlayer.vUnit[COMMENT_SLAYER].vId;
			pStackForUnits->pIdDataUnit.pDataUnit = fGetDataUnit(pHumanPlayer[vvi].vParamPlayer.vUnit[COMMENT_SLAYER].vId);
			pStackForUnits->vIdForComments = COMMENT_SLAYER;
			pHumanPlayer[vvi].vActionStackForUnits.fAddStackForUnits(pStackForUnits); 
			*/// TimerPlayer
			pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer = CreateTimer();
			TimerStart(pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer,0.1,true,&fGameHuman);
			SaveInteger(vHT,pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer,CONST_AI_PLAYER,(integer)&pHumanPlayerGlobal[vvi]);
		}
	}
	if (vCountVampires > 0)
	{
		for (vvi = 0; vvi < vCountVampires; vvi++)
		{
			if (pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer > 0)
			{
				DestroyTimer(pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer);
			}
			//
			pVampirePlayerGlobal[vvi].fInit();
			pVampirePlayerGlobal[vvi].vParamPlayer.fInit(50,50);
			pVampirePlayerGlobal[vvi].vActionStackForUnits.fInitStackForUnits(&pVampirePlayerGlobal[vvi].vParamPlayer);
			// MainVampire
			/*pVampirePlayer[vvi].vParamPlayer.vUnit[COMMENT_MAIN_VAMPIRE].vIdForComments = COMMENT_MAIN_VAMPIRE;
			pVampirePlayer[vvi].vParamPlayer.vUnit[COMMENT_MAIN_VAMPIRE].vId = (unsigned short)pVampirePlayer[vvi].vParamPlayer.vAI;
			pVampirePlayer[vvi].vParamPlayer.vUnit[COMMENT_MAIN_VAMPIRE].pDataUnit = vDataUnits[pVampirePlayer[vvi].vParamPlayer.vUnit[COMMENT_MAIN_VAMPIRE].vId].pDataUnit; 
			pStackForUnits = (StackForUnits*) malloc(sizeof(StackForUnits));
			pStackForUnits->pIdDataUnit.vId = pVampirePlayer[vvi].vParamPlayer.vUnit[COMMENT_MAIN_VAMPIRE].vId;
			pStackForUnits->pIdDataUnit.pDataUnit = pVampirePlayer[vvi].vParamPlayer.vUnit[COMMENT_MAIN_VAMPIRE].pDataUnit;
			pStackForUnits->vIdForComments = COMMENT_MAIN_VAMPIRE;
			pVampirePlayer[vvi].vActionStackForUnits.fAddStackForUnits(pStackForUnits);*/
			pVampirePlayerGlobal[vvi].vFarmBase = DO_NOT_FIND_BASE;
			pVampirePlayerGlobal[vvi].vVampire.fInit();
			// Timer
			pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer = CreateTimer();
			TimerStart(pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer,0.1,true,&fGameVampire);
			SaveInteger(vHT,pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer,CONST_AI_PLAYER,(integer)&pVampirePlayerGlobal[vvi]);
		}
	}
	//
	if (vCountVampires == 2)
	{
		pVampirePlayerGlobal[0].pPartner = &pVampirePlayerGlobal[1];
		pVampirePlayerGlobal[1].pPartner = &pVampirePlayerGlobal[0];
	}
	if (vCountVampires == 1)
	{
		pVampirePlayerGlobal[0].pPartner = NULL;
	}
	if (vCountVampires >= 1)
	{
		pDataVampires->fInit();
		pDataVampires->vActionStackForUnitsEnemy.fInitStackForUnits(&pVampirePlayerGlobal[0].vParamPlayer);
		//
		//pDataVampires->pPutGlobal = (DataGoTo*)malloc(sizeof(DataGoTo));
		//pDataVampires->pPutGlobal->pFillMap = (unsigned short*) malloc(sizeof(unsigned short)*vSizeMap);
		pDataVampires->pPutGlobal = new struct DataGoTo;
		pDataVampires->pPutGlobal->pFillMap = new unsigned short[vSizeMap];
		//pDataVampires->pPutGlobal->vMassRect = new sRect*[vSizeMap];
		//pDataVampires->pPutGlobal->vMassRect2 = new unsigned int*[vSizeMap];
		pDataVampires->pPutGlobal->vMaxRect = vSizeMap;
		/*for (vvi = 0; vvi < vSizeMap; vvi++)
		{
			pDataVampires->pPutGlobal->vMassRect[vvi] = new sRect[1]; 
			pDataVampires->pPutGlobal->vMassRect2[vvi] = new unsigned int[1];
		}*/
		pDataVampires->pPutGlobal->vMassRect = new struct sRect[vSizeMap];
		pDataVampires->pPutGlobal->vMassRect2 = new unsigned int[vSizeMap];
		//pDataVampires->pPutGlobal->vMassRect = (sRect*)malloc(sizeof(sRect)*vSizeMap);
		//pDataVampires->pPutGlobal->vMassRect2 = (unsigned int*) malloc(sizeof(unsigned int)*vSizeMap);
		pDataVampires->pPutGlobal->vActionStr = (char*) malloc(sizeof(char)*10);
		//
		pDataVampires->pPutGlobal->vStartThread = FALSE;
		pDataVampires->pPutGlobal->vResult = 0;
		pDataVampires->pPutGlobal->hThreadGoTo = NULL;
	}
	//
	if (vCountHumans >= 1)
	{
		pDataHumans->fInit();
		pDataHumans->vActionStackForUnitsEnemy.fInitStackForUnits(&pHumanPlayerGlobal[0].vParamPlayer);
	}
	/*if (vHostPlayer > 0 && vGameBegin == 1 && (vCountHumans > 0 || vCountVampires > 0))
	{
		hTriggerEndGame2 = CreateTrigger();
		IncrementHandleReferences(hTriggerEndGame2);
		TriggerAddAction(hTriggerEndGame2, &fEndGame);
		TriggerRegisterPlayerEvent(hTriggerEndGame2,vHostPlayer,EVENT_PLAYER_LEAVE);
	}*/
	/*vSync4_Priority = FALSE;
	fDataUnit2();
	vSync4_Priority = TRUE;*/
m11:
	if (vTimerActionsGame > 0)
	{
		DestroyTimer(vTimerActionsGame);
	}
}

void fKillUnitsOfPlayer(player vPlayer)
{
	group g=CreateGroup();
	unit u;
	GroupEnumUnitsOfPlayer(g,vPlayer,0);
	do
	{
		u=FirstOfGroup(g);
		GroupRemoveUnit(g,u);
		if (u > 0)
		{
			KillUnit(u);
		}
	}
	while (u != 0);
}

void fKillUnitsBots()
{
	int vvi;
	if (vCountHumans > 0)
	{
		for (vvi = 0; vvi < vCountHumans; vvi++)
		{
			fKillUnitsOfPlayer(pHumanPlayerGlobal[vvi].vParamPlayer.vPlayer);
		}
	}
	if (vCountVampires > 0)
	{
		for (vvi = 0; vvi < vCountVampires; vvi++)
		{
			fKillUnitsOfPlayer(pVampirePlayerGlobal[vvi].vParamPlayer.vPlayer);
		}
	}
}

void fEndGame()
{
	fKillUnitsBots();
	fStopCommands();
}

void fEndGame2(Pointer *p)
{
	vEndGame2 = 1;
	fStopCommands();
	/*if (hJassApi)
	{
		FreeLibrary(hJassApi);
	}*/
	if (hJassApi_2)
	{
		FreeLibrary(hJassApi_2);
	}
	vEndGame2 = 0;
}

VampirePlayer * fGetVampirePlayer(HTIMER vTimer)
{
	if (vTimer == NULL) return 0;
	try
	{
		for (int vvi = 0; vvi < vCountVampires; vvi++)
		{
			if (&pVampirePlayerGlobal[vvi])
			{
				if (&pVampirePlayerGlobal[vvi].vParamPlayer)
				{
					if (pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer == vTimer)
					{
						return &pVampirePlayerGlobal[vvi];
					}
				}
			}
		}
	}
	catch (...)
	{
		return 0;
	}
	return 0;
}

HumanPlayer * fGetHumanePlayer(HTIMER vTimer)
{
	if (vTimer == NULL) return 0;
	try
	{
		for (int vvi = 0; vvi < vCountHumans; vvi++)
		{
			if (&pHumanPlayerGlobal[vvi])
			{
				if (&pHumanPlayerGlobal[vvi].vParamPlayer)
				{
					if (pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer == vTimer)
					{
						return &pHumanPlayerGlobal[vvi];
					}
				}
			}
		}
	}
	catch (...)
	{
		return 0;
	}
	return 0;
}

unsigned short fGoToAction(unsigned short vIdDataUnit, integer vInt, unsigned short vAction)
{
	//if (fSync(fGetDataUnit(vIdDataUnit)) == FALSE) return 0;
	//if (fCheckSync(fGetDataUnit(vIdDataUnit)) == FALSE) return 0;
	if (fGetActionUnit(vIdDataUnit) == ACTION_WAITING && fGetActionUnitFinish(vIdDataUnit) == 1)
	{
		fFreeActionUnit(vIdDataUnit);
		if (fUnitGoToInt(vIdDataUnit,vInt,vAction,LIMIT_GOTO_MAINHUMAN) == 1)
		{
			return 1;
		}
		else
		{
			fFinishActionUnit(vIdDataUnit);
		}
	}
	return 0;
}

bool fIsEnoughResourses(HPLAYER vPlayer, ParamPlayer *pParamPlayer, integer vNResearch, integer *vId)
{
	if (pParamPlayer == NULL) return FALSE;
	if (vNResearch < 0 || vNResearch >= pParamPlayer->MAX_RESEARCH) return FALSE;
	if (vPlayer == NULL) return FALSE;
	integer vGoldPlayer;
	integer vWoodPlayer;
	integer vGoldUnit;
	integer vWoodUnit;
	*vId = 0;
	if (fGetResoursesResearch(pParamPlayer,vNResearch,vId,&vGoldUnit,&vWoodUnit) == FALSE) return FALSE;
	if (*vId <= 0) return FALSE;
	vGoldPlayer = GetPlayerState(vPlayer,PLAYER_STATE_RESOURCE_GOLD);
	vWoodPlayer = GetPlayerState(vPlayer,PLAYER_STATE_RESOURCE_LUMBER);
	if(!(vGoldPlayer >= vGoldUnit && vWoodPlayer >= vWoodUnit))
	{
		return FALSE;
	}
	return TRUE;
}

bool fIsEnoughResourses(HPLAYER vPlayer, integer vId)
{
	if (vPlayer == NULL || vId == NULL) return FALSE;
	integer vGoldPlayer;
	integer vWoodPlayer;
	integer vFoodPlayer;
	integer vFoodPlayerCap;
	integer vGoldUnit;
	integer vWoodUnit;
	integer vFoodUnit;
	if (fGetResoursesUnit(vId,&vGoldUnit,&vWoodUnit) == FALSE) return FALSE;
	vGoldPlayer = GetPlayerState(vPlayer,PLAYER_STATE_RESOURCE_GOLD);
	vWoodPlayer = GetPlayerState(vPlayer,PLAYER_STATE_RESOURCE_LUMBER);
	if(!(vGoldPlayer >= vGoldUnit && vWoodPlayer >= vWoodUnit))
	{
		return FALSE;
	}
	vFoodPlayerCap = GetPlayerState(vPlayer,PLAYER_STATE_RESOURCE_FOOD_CAP);
	vFoodPlayer = GetPlayerState(vPlayer,PLAYER_STATE_RESOURCE_FOOD_USED);
	vFoodUnit = GetFoodUsed(vId);
	if(!(vFoodPlayerCap - vFoodPlayer >= vFoodUnit))
	{
		return FALSE;
	}
	return TRUE;
}

unsigned short fTrainAction(HPLAYER vPlayer, ActionStackForUnits * pActionStackForUnits, unsigned short vComment, integer vIdDataUnitTrain, unsigned short vCount)
{
	if (vIdDataUnitTrain <= 0) return 0;
	DataUnit *pDataUnit;
	unsigned short vFlag = 0;
	pDataUnit = pActionStackForUnits->fGetDataUnit(vComment);
	if (pDataUnit && (unsigned int)pDataUnit != 0xdddddddd)
	{
		if (pDataUnit->Postroeno == TRUE)
		{
			for (unsigned short vvi = 0; vvi < vCount; vvi++)
			{
				if (fIsEnoughResourses(vPlayer,vIdDataUnitTrain) == TRUE)
				{
					if (IssueImmediateOrderById(pDataUnit->WhichUnit,vIdDataUnitTrain) == TRUE)
					{
						vFlag = 1;
					}
				}
			}
			return vFlag;
		}
	}
	return vFlag;
}

unsigned short fTrainActionResearch(ParamPlayer *pParamPlayer, DataUnit *pDataUnit, integer vNResearch)
{
	integer vId; 
	unsigned short vFlag = 0;
	if (pDataUnit && (unsigned int)pDataUnit != 0xdddddddd)
	{
		if (pDataUnit->Postroeno == TRUE)
		{
			if (fIsEnoughResourses(pParamPlayer->vPlayer,pParamPlayer,vNResearch,&vId) == TRUE)
			{
				if(IssueImmediateOrderById(pDataUnit->WhichUnit,vId) == TRUE)
				{
					pDataUnit->vNResearch = vNResearch;
					vFlag = 1;
				}
			}
			return vFlag;
		}
	}
	return vFlag;
}

real fXCenter(integer vId, unsigned short vX2)
{
	if (vId <= 0) return FALSE;
	if (vX2 < 0 || vX2 >= vW3) return FALSE;
	real vX;
	real vRadius;
	vRadius = fRadius_Unit(vId);
	vX = fGetMinIX2RX(vX2) + vRadius;
	return vX;
}

real fYCenter(integer vId, unsigned short vY2)
{
	if (vId <= 0) return FALSE;
	if (vY2 < 0 || vY2 >= vH3) return FALSE;
	real vY;
	real vRadius;
	vRadius = fRadius_Unit(vId);
	vY = fGetMinIY2RY(vY2) - vRadius;
	return vY;
}

real fXCenter(integer vId, unsigned int vIntIn)
{
	if (vId <= 0) return FALSE;
	if (vIntIn < 0 || vIntIn >= vSizeMap) return FALSE;
	real vX;
	real vRadius;
	unsigned short vX2;
	vRadius = fRadius_Unit(vId);
	vX2 = fConvertIToX(vIntIn);
	vX = fGetMinIX2RX(vX2) + vRadius;
	return vX;
}

real fYCenter(integer vId, unsigned int vIntIn)
{
	if (vId <= 0) return FALSE;
	if (vIntIn < 0 || vIntIn >= vSizeMap) return FALSE;
	real vY;
	real vRadius;
	unsigned short vY2;
	vRadius = fRadius_Unit(vId);
	vY2 = fConvertIToY(vIntIn);
	vY = fGetMinIY2RY(vY2) - vRadius;
	return vY;
}

bool fUnitToRect(DataUnit *pDataUnit, HRECT vRect)
{
	if (pDataUnit == NULL || vRect == 0) return FALSE;
	real vX,vY;
	vX = GetUnitX(pDataUnit->WhichUnit);
	vY = GetUnitY(pDataUnit->WhichUnit);
	if (fIsXYToRect(vX,vY,vRect) == TRUE)
	{
		return TRUE;
	}
	return FALSE;
}

bool fIsXYToRect(real vX, real vY, HRECT vRect)
{
	real vMinX,vMinY,vMaxX,vMaxY;
	vMinX = GetRectMinX(vRect);
	vMinY = GetRectMinY(vRect);
	vMaxX = GetRectMaxX(vRect);
	vMaxY = GetRectMaxY(vRect);
	if (vMinX <= vX && vMaxX >= vX &&
		vMinY <= vY && vMaxY >= vY)
	{
		return TRUE;
	}
	return FALSE;
}

bool fTransferRes(HPLAYER vPlayerFrom, HPLAYER vPlayerToWhom, HPLAYERSTATE vRes, integer vCost)
{
	integer vResPlayerFrom;
	integer vResPlayerToWhom;
	vResPlayerFrom = GetPlayerState(vPlayerFrom,vRes);
	vResPlayerToWhom = GetPlayerState(vPlayerToWhom,vRes);
	if (vResPlayerFrom-vCost >= 0)
	{
		SetPlayerState(vPlayerFrom,vRes,vResPlayerFrom-vCost);
		SetPlayerState(vPlayerToWhom,vRes,vResPlayerToWhom+vCost);
		return TRUE;
	}
	return FALSE;
}

bool fBuyAUnit(unsigned short vShop, integer vIdUnit, sInventory *vInventory)
{
	if (vShop == NULL || vIdUnit == NULL) return FALSE;
	vInventory->vG = fG(vDataUnits[vShop].pDataUnit->LastCoordinates.X,vDataUnits[vShop].pDataUnit->LastCoordinates.Y,
		                vInventory->pDataUnit->LastCoordinates.X,vInventory->pDataUnit->LastCoordinates.Y);
	if (vInventory->vG > 128 * 5) return FALSE;
	vInventory->vGoldPlayer = GetPlayerState(vInventory->pDataUnit->vPlayer,PLAYER_STATE_RESOURCE_GOLD);
	vInventory->vWoodPlayer = GetPlayerState(vInventory->pDataUnit->vPlayer,PLAYER_STATE_RESOURCE_LUMBER);
	fGetResoursesUnit(vIdUnit,&vInventory->vGold,&vInventory->vWood);
	if (vInventory->vGoldPlayer - vInventory->vGold >= 0 &&
		vInventory->vWoodPlayer - vInventory->vWood >= 0)
	{
		if (IssueNeutralImmediateOrderById(vInventory->pDataUnit->vPlayer, vDataUnits[vShop].pDataUnit->WhichUnit, vIdUnit) == TRUE)
		{
			return TRUE;
		}
	}
	return FALSE;
}

bool fBuyItem(unsigned short vShop, integer vIdItem, sInventory *vInventory)
{
	if (vShop == NULL || vIdItem == NULL || vInventory == NULL) return FALSE;
	vInventory->vG = fG(vDataUnits[vShop].pDataUnit->LastCoordinates.X,vDataUnits[vShop].pDataUnit->LastCoordinates.Y,
		                vInventory->pDataUnit->LastCoordinates.X,vInventory->pDataUnit->LastCoordinates.Y);
	if (vInventory->vG > 128 * 5) return FALSE;
	if (vInventory->vQFreeSlots() <= 0) return FALSE;
	vInventory->vGoldPlayer = GetPlayerState(vInventory->pDataUnit->vPlayer,PLAYER_STATE_RESOURCE_GOLD);
	vInventory->vWoodPlayer = GetPlayerState(vInventory->pDataUnit->vPlayer,PLAYER_STATE_RESOURCE_LUMBER);
	fGetResoursesItem(vIdItem,&vInventory->vGold,&vInventory->vWood);
	if (vInventory->vGoldPlayer - vInventory->vGold >= 0 &&
		vInventory->vWoodPlayer - vInventory->vWood >= 0)
	{
		if (IssueNeutralImmediateOrderById(vInventory->pDataUnit->vPlayer, vDataUnits[vShop].pDataUnit->WhichUnit, vIdItem) == TRUE)
		{
			vInventory->fRefreshInventory();
			return TRUE;
		}
	}
	return FALSE;
}

void fActionItemsGold()
{
	HTIMER t = GetExpiredTimer();
	if (LoadInteger(vHT,t,CONST_FLAG_FOR_GOLD_COIN) == 0) return;
	HITEM vItem = GetEnumItem();
	//String vName = GetItemName(vItem);
	integer vIdItem = GetItemTypeId(vItem);
	//int vLen;
	if (vIdItem == ID_ITEM_GOLD1 || vIdItem == ID_ITEM_GOLD3)
	{
		HPLAYER vPlayer = LoadPlayerHandle(vHT,t,CONST_PLAYER_FOR_GOLD_COIN);
		if (IsItemVisible(vItem) == FALSE) return;
		integer vIdUnitForGoldCoin = LoadInteger(vHT,t,CONST_ID_UNIT_FOR_GOLD_COIN);
		real vG;
		if (fIsVampire(vIdUnitForGoldCoin) == TRUE)
		{
			vG = fG(LoadReal(vHT,t,CONST_HUNIT_VAMPIRE_X),
				    LoadReal(vHT,t,CONST_HUNIT_VAMPIRE_Y),
					GetItemX(vItem),
					GetItemY(vItem));
			if (vG < 128*16)
			{
				SaveInteger(vHT,t,CONST_FLAG_FOR_GOLD_COIN,0);
				SaveItemHandle(vHT,t,CONST_GOLD_COIN,vItem);
				return;
			}
		}
		if (fIsSlayer(vIdUnitForGoldCoin) == TRUE)
		{
			SaveInteger(vHT,t,CONST_FLAG_FOR_GOLD_COIN,0);
			SaveItemHandle(vHT,t,CONST_GOLD_COIN,vItem);
		}
		
	}
	/*vLen = strlen(vName);
	if (memcmp(vName,"Small gold Coin",vLen) == 0 ||
		memcmp(vName,"Large gold Coin",vLen) == 0)
	{
		SaveItemHandle(vHT,GetExpiredTimer(),20,vItem);
	}*/
}

void fRephreshLastCoordinates(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return;
	pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
	pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
}

bool fSync(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return FALSE;
	if (pDataUnit->vSync == TRUE) return TRUE;
	//pDataUnit->vTimerGetElapsedGoTo=int(TimerGetElapsed(vTimerGame)*10.0)/10.0;
	//pDataUnit->vTimerGoTo=TimerGetElapsed(vTimerGame);
	//StoreReal(vSyncCache,pDataUnit->vKeyUnit,"vTimerGetElapsedGoTo",pDataUnit->vTimerGoTo);
	//SyncStoredReal(vSyncCache,pDataUnit->vKeyUnit,"vTimerGetElapsedGoTo");
	//StoreReal(vSyncCache,pDataUnit->vKeyUnit,"vTimerGetElapsedGoTo",0);
	try
	{
		/*int vvi;
		for (vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			StoreInteger(pDataUnit->vSyncCache,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer,0);
		}*/
		/*sprintf(pDataUnit->vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		StoreReal(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,TimerGetElapsed(vTimerGame));
		SyncStoredReal(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer);
		StoreReal(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,0);*/
		sprintf(pDataUnit->vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		StoreInteger(vSyncCache2,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,1);
		SyncStoredInteger(vSyncCache2,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer);
		StoreInteger(vSyncCache2,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,0);
		//StoreInteger(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,1);//Грязный
		//SyncStoredInteger(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer);//Говорю что грязный
		//StoreInteger(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,0);
		sprintf(pDataUnit->vKeyLocalPlayer,"\0");
		pDataUnit->vSync = TRUE;
		return TRUE;
	}
	catch(...)
	{
	}
	return FALSE;
}

bool fCheckSync(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return FALSE;
	if (pDataUnit->vSync == FALSE) return FALSE;
	/*pDataUnit->vTimerGoTo = GetStoredReal(vSyncCache,pDataUnit->vKeyUnit,"vTimerGetElapsedGoTo");
	integer vActionN = GetStoredInteger(vSyncCache,pDataUnit->vKeyUnit,"vActionN");
	real vTime = TimerGetElapsed(vTimerGame);
	if (pDataUnit->vTimerGoTo != 0 &&
		vTime - pDataUnit->vTimerGoTo >= 1.06 &&
		//(pDataUnit->pPut->vAction == ACTION_BLINK || pDataUnit->pPut->vAction == ACTION_GO ||
		//(pDataUnit->vAction == ACTION_GO && vTime - pDataUnit->vTimerGoTo >= pDataUnit->pPut->i2 * 0.16)) &&
		pDataUnit->vActionN == vActionN)
	{
		StoreReal(vSyncCache,pDataUnit->vKeyUnit,"vTimerGetElapsedGoTo",0);
		StoreInteger(vSyncCache,pDataUnit->vKeyUnit,"vActionN",0);
		pDataUnit->vSync = FALSE;
		return TRUE;
	}*/
	int vvi;
	real vTimeSync;
	if (pDataUnit->vTimeSync == 0)
	{
		vTimeSync = 0;
	}
	else
	{
		vTimeSync = TimerGetElapsed(vTimerGame) - pDataUnit->vTimeSync;
	}
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (pPlayerUser[vvi].vLeave == FALSE)//pPlayerUser[vvi].vDead == FALSE)
		{
			if (//GetStoredInteger(vSyncCache,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer) == 2 || //Если игрок чистый
				//GetStoredInteger(vSyncCache,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer) == 0 ||
				GetStoredInteger(vSyncCache2,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer) != 1)// ||
			//if (GetStoredReal(vSyncCache,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer) == 0)
			{
				if (vTimeSync != 0 &&
					vTimeSync > 15)
				{
					fLog(40000,"40000");
					if (vSync == TRUE)
					{
						fCheckLeavePlayers();
						if (vSync == FALSE)
						{
							pDataUnit->vSync = FALSE;
							pDataUnit->vSyncClean = FALSE;
							return FALSE;
						}
					}
					pDataUnit->vSync = FALSE;
					pDataUnit->vSyncClean = FALSE;
					return FALSE;
				}
				else if (vTimeSync != 0 &&
					     vTimeSync > 9)
				{
					fLog(20000,"20000");
					for (vvi = 0; vvi < vCountPlayersUser; vvi++)
					{
						if (pPlayerUser[vvi].vLeave == FALSE)
						{
							if (fCheckLeftToNickName(GetPlayerName(pPlayerUser[vvi].vPlayer)) == TRUE)
							{
								pPlayerUser[vvi].vLeave = TRUE;
								fDelDataUnit(pPlayerUser[vvi].vNPlayer);
							}
						}
					}
				}
				else if (vTimeSync != 0 &&
					     vTimeSync > 10)
				{
					fLog(30000,"30000");
					if (vSync == FALSE)
					{
						if (fCheckLeavePlayers() == FALSE)
						{
							pDataUnit->vSync = FALSE;
							pDataUnit->vSyncClean = FALSE;
							return FALSE;
						}
					}
					if (vSync == TRUE)
					{
						return FALSE;
					}
					/*FlushStoredInteger(vSyncCache2,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer);
					if (vvi >= vCountPlayersUser - 1)
					{
						pDataUnit->vSync = FALSE;
						pDataUnit->vSyncClean = FALSE;
						return FALSE;
					}*/
				}
				else
				{
					return FALSE;
				}
			}
		}
	}
	//FlushStoredMission(vSyncCache2,pDataUnit->vKeyUnit);
	/*float vTimerGetElapsedGoTo;
	float vTimerGetElapsedGoTo2 = 0;
	float vTimerGetElapsedGoTo3 = TimerGetElapsed(vTimerGame);
	bool vFlag = FALSE;
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (pPlayerUser[vvi].vLeave == FALSE)
		{
			vTimerGetElapsedGoTo = GetStoredReal(vSyncCache,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer);
			if (vTimerGetElapsedGoTo != vTimerGetElapsedGoTo2 && vTimerGetElapsedGoTo2 != 0)
			{
				vTimerGetElapsedGoTo2 = vTimerGetElapsedGoTo;
				//vFlag = true;
				break;
			}
			if (vTimerGetElapsedGoTo == 0)
			{
				vTimerGetElapsedGoTo2 = vTimerGetElapsedGoTo;
			}
			vTimerGetElapsedGoTo2 = vTimerGetElapsedGoTo;
		}
		else
		{
			vTimerGetElapsedGoTo2 = vTimerGetElapsedGoTo;
		}
	}
	if (vFlag == TRUE) return FALSE;*/
	/*for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		StoreInteger(pDataUnit->vSyncCache,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer,0);
		//StoreReal(vSyncCache,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer,0);
	}*/
	//sprintf(vBuf,"%f\0",vTimeSync);
	//fLog(10000,vBuf);
	//sprintf(vBuf,"Color - %hu\0",pDataUnit->vIdDataUnit);
	//fLog(10001,vBuf);
	pDataUnit->vSync = FALSE;
	pDataUnit->vSyncClean = FALSE;
	return TRUE;			
}

bool fCheckLeavePlayers()
{
	int vvi;
	if (vSync == TRUE) goto m1;
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (pPlayerUser[vvi].vLeave == FALSE)
		{
			StoreInteger(vSyncCache2,"CheckLeavePlayers",pPlayerUser[vvi].vKeyPlayer,0);
		}
		else
		{
			if (HaveStoredInteger(vSyncCache2,"CheckLeavePlayers",pPlayerUser[vvi].vKeyPlayer) == TRUE)
			{
				fLog(1000,"1000");
				FlushStoredInteger(vSyncCache2,"CheckLeavePlayers",pPlayerUser[vvi].vKeyPlayer);
				fLog(1001,"1001");
			}
		}
	}
	sprintf(vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
	StoreInteger(vSyncCache2,"CheckLeavePlayers",vKeyLocalPlayer,1);
	SyncStoredInteger(vSyncCache2,"CheckLeavePlayers",vKeyLocalPlayer);
	StoreInteger(vSyncCache2,"CheckLeavePlayers",vKeyLocalPlayer,0);
	sprintf(vKeyLocalPlayer,"\0");
	vSync = TRUE;
	return TRUE;
m1:
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (pPlayerUser[vvi].vLeave == FALSE)
		{
			if (GetStoredInteger(vSyncCache2,"CheckLeavePlayers",pPlayerUser[vvi].vKeyPlayer) == 0 &&
				fCheckLeftToNickName(GetPlayerName(pPlayerUser[vvi].vPlayer)) == TRUE)
			{
				pPlayerUser[vvi].vLeave = TRUE;
				fDelDataUnit(pPlayerUser[vvi].vNPlayer);
			}
			if (HaveStoredInteger(vSyncCache2,"CheckLeavePlayers",pPlayerUser[vvi].vKeyPlayer) == TRUE)
			{
				fLog(3000,"3000");
				FlushStoredInteger(vSyncCache2,"CheckLeavePlayers",pPlayerUser[vvi].vKeyPlayer);
				fLog(3001,"3001");
			}
		}
	}
	//FlushStoredMission(vSyncCache2,"CheckLeavePlayers");
	vSync = FALSE;
	return TRUE;
}

bool fFreeSync(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return FALSE;
	if (pDataUnit->vSyncClean == TRUE) return TRUE;
	try
	{
		int vvi;
		for (vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			if (pPlayerUser[vvi].vLeave == FALSE)
			{
				StoreInteger(vSyncCache2,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer,0);
			}
			else
			{
				if (HaveStoredInteger(vSyncCache2,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer) == TRUE)
				{
					fLog(2000,"2000");
					FlushStoredInteger(vSyncCache2,pDataUnit->vKeyUnit,pPlayerUser[vvi].vKeyPlayer);
					fLog(2001,"2001");
				}
			}
		}
		//sprintf(pDataUnit->vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		//StoreInteger(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,2);//Чищу
		//SyncStoredInteger(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer);//Говорю что почистился
		//StoreInteger(vSyncCache,pDataUnit->vKeyUnit,pDataUnit->vKeyLocalPlayer,0);
		//sprintf(pDataUnit->vKeyLocalPlayer,"\0");
		pDataUnit->vTimeSync = TimerGetElapsed(vTimerGame);
		pDataUnit->vSyncClean = TRUE;
		pDataUnit->vSync = FALSE;
		return TRUE;
	}
	catch(...)
	{
	}
	return FALSE;
}

bool fCompliteBuild(DataUnit *pDataUnit, float vTime)
{
	if (pDataUnit == NULL) return FALSE;
	if (pDataUnit->vTimeEndBuild < vTime) return FALSE;
	pDataUnit->Postroeno = TRUE;
	return TRUE;
}

bool fCompliteResearch(DataUnit *pDataUnit, float vTime)
{
	if (pDataUnit == NULL) return FALSE;
	if (pDataUnit->vTimeEndResearch < vTime) return FALSE;
	pDataUnit->Researched = TRUE;
	pDataUnit->Researching = FALSE;
	return TRUE;
}

bool fCompliteTrain(DataUnit *pDataUnit, float vTime)
{
	if (pDataUnit == NULL) return FALSE;
	if (pDataUnit->vTimeEndTrain < vTime) return FALSE;
	
	return TRUE;
}

bool fGetLastCoordinates(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return FALSE;
	Integer vId = GetUnitTypeId(pDataUnit->WhichUnit);
	if (vId == 0)
	{
		fLog(100,"100");
		return FALSE;
	}
	/*if (pDataUnit->vId == ID_UNIT_BUILD_DOM)
	{
		sprintf(vBuf,"%f\0",pDataUnit->LastCoordinates.X);
		fLog(22,vBuf);
		sprintf(vBuf,"%f\0",pDataUnit->LastCoordinates.Y);
		fLog(23,vBuf);
	}*/
	pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
	pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
	/*if (pDataUnit->vId == ID_UNIT_BUILD_DOM)
	{
		sprintf(vBuf,"%f\0",pDataUnit->LastCoordinates.X);
		fLog(24,vBuf);
		sprintf(vBuf,"%f\0",pDataUnit->LastCoordinates.Y);
		fLog(25,vBuf);
	}*/
}

unsigned short fNBaseUnit(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return 0;
	char *pMap = (char*)vMapGlobal;
	unsigned int vInt3 = fConvertXYToI(fConverterX(pDataUnit->LastCoordinates.X),fConverterY(pDataUnit->LastCoordinates.Y));
	return (unsigned short)pMap[vInt3];
}