#include "stdafx.h"
#include "Basics_functions.h"
#include "Global_variables_extern.h"
#include "myJASSConstants.h"
#include "Authorization.h"
#include "Internet_time.h"
#include "DB_Cost.h"
#include "resource.h"
#include "Vampirism_fire.h"

HPLAYER fGetHost2()
{
	HPLAYER p;
    HGAMECACHE g = InitGameCache("GetHost");
    StoreInteger ( g, "Map", "Host", GetPlayerId(GetLocalPlayer ())+1);
    TriggerSyncStart ();
    SyncStoredInteger ( g, "Map", "Host" );
    TriggerSyncReady ();
    p = Player( GetStoredInteger ( g, "Map", "Host" )-1);
    FlushGameCache( g );
    g = NULL;
    return p;
}

integer fSetHost(HPLAYER vPlayer)
{
	if (vSetHostGo == 1) return 0;
	if (vPlayer == NULL) return 0;
	if (vHostPlayer != 0) return 0;
	vSetHostGo = 1;
	vSetHost = 0;
	vHostPlayer = vPlayer;
	vHost = GetPlayerColor(vHostPlayer) + 1;
	vSetHost = 1;
	vSetHostGo = 0;
	StoreInteger(vAuthorizationCache,"Map","vSetHost",vSetHost);
	SyncStoredInteger(vAuthorizationCache,"Map","vSetHost");
	StoreInteger(vAuthorizationCache,"Map","vHostPlayer",(integer)vHostPlayer);
	SyncStoredInteger(vAuthorizationCache,"Map","vHostPlayer");
	StoreInteger(vAuthorizationCache,"Map","vHost",(integer)vHost);
	SyncStoredInteger(vAuthorizationCache,"Map","vHost");
	return vSetHost;
}

bool fFreeHost()
{
	vSetHost = 0;
	vHostPlayer = 0;
	vHost = -1;
	StoreInteger(vAuthorizationCache,"Map","vSetHost",vSetHost);
	SyncStoredInteger(vAuthorizationCache,"Map","vSetHost");
	StoreInteger(vAuthorizationCache,"Map","vHostPlayer",(integer)vHostPlayer);
	SyncStoredInteger(vAuthorizationCache,"Map","vHostPlayer");
	StoreInteger(vAuthorizationCache,"Map","vHost",(integer)vHost);
	SyncStoredInteger(vAuthorizationCache,"Map","vHost");
	return TRUE;
}

integer fCheckSetHost()
{
	return vSetHost;
}

integer fCheckStartCommands()
{
	if (vStartCommandsGo == 1)
	{
		for(;;)
		{
			if (vStartCommands == 1) break;
		}
	}
	return vStartCommands;
}

unsigned short fGetIdDataUnit(integer vId, HPLAYERCOLOR vNPlayer)
{
	if (vLastUnit == NO_UNIT) return NO_UNIT;
	unsigned short vvi;
	DataUnit *pDataUnit;
	for (vvi = 0; vvi <= vLastUnit; vvi++)
	{
		pDataUnit = fGetDataUnit(vvi);
		if (pDataUnit)
		{
			if (pDataUnit->vId == vId && pDataUnit->vNPlayer == vNPlayer)
			{
				return pDataUnit->vIdDataUnit;
			}
		}
	}
	return NO_UNIT;
}

unsigned int fGetIntUnit(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	unsigned short vX = fConverterX(GetUnitX(pDataUnit->WhichUnit));
    unsigned short vY = fConverterY(GetUnitY(pDataUnit->WhichUnit));
	//sprintf(vTest,"fConvertXYToI(vX,vY)" );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	return fConvertXYToI(vX,vY);
}

integer fClearMapUnits(unsigned short vStorona)
{
	if (vStorona < 0 || vStorona >= 2) return 0;
	int vvi;
	for (vvi = 0; vvi < vSizeMap; vvi++)
	{
		vMapUnits[vStorona][vvi] = NO_UNIT;
	}
	return 1;
}

integer fUnitsMagazine()
{
	player vPlayer = Player(PLAYER_NEUTRAL_PASSIVE);
	vRecepts = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_NIZHNIY_MAGAZIN,vPlayer),THREAD_MAIN);
	vTopShop = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_VERHNIY_MAGAZIN,vPlayer),THREAD_MAIN);
	vGate = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_GATE,vPlayer),THREAD_MAIN);
	vSven = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_SVEN,vPlayer),THREAD_MAIN);
	vBansha = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_BANSHA,vPlayer),THREAD_MAIN);
	vManaWell = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_MANA_KOLODEC,vPlayer),THREAD_MAIN);
	vWellOfLife = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_KOLODEC_ZHIZNI,vPlayer),THREAD_MAIN);
	vBaseOfUnits = fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_BAZA_UNITOV,vPlayer),THREAD_MAIN);
	fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_PIT_1,vPlayer),THREAD_MAIN);
	fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_PIT_2,vPlayer),THREAD_MAIN);
	fCreateDataUnit(fGetUnit(ID_UNIT_PASSIVE_CREDITS,vPlayer),THREAD_MAIN);
	return 1;
}

integer fUnitsMainHuman()
{
	int vvi;
	for (vvi = 0; vvi < MAX_HUMAN_PLAYERS; vvi++)
	{
		fCreateDataUnit(fGetUnit(ID_UNIT_MAIN_HUMAN1,Player(vvi)),THREAD_MAIN);
	}
	return 1;
}

integer fCheñkGoTo(unsigned short vIdDataUnit)
{
	DataGoTo *current = pFirstDataGoTo;
	DataGoTo *next=0;
	while (current>0)
	{
		if (current->vIdDataUnit == vIdDataUnit)
		{
			if (current->IdetPoPuti == FALSE && current->vCelNeDostignuta == FALSE)
			{
				return 0;
			}
			else
			{
				return 1;
			}
		}
		next = current->next;
	}
	return -1;
}

void fInitPlayer()
{
	fFreePlayer();
	pHumanPlayerGlobal = new struct HumanPlayer[MAX_HUMAN_PLAYERS];
	pVampirePlayerGlobal = new struct VampirePlayer[MAX_VAMPIRE_PLAYERS];
	pPlayerUser = new struct sPlayerUser[MAX_PLAYERS];
	pDataHumans = new struct sDataHumans;
	pDataVampires = new struct sDataVampires;
	//pHumanPlayerGlobal = (HumanPlayer*) malloc(sizeof(HumanPlayer)*MAX_HUMAN_PLAYERS);
	//memset(pHumanPlayerGlobal,0,sizeof(pHumanPlayerGlobal)*MAX_HUMAN_PLAYERS);
	//pVampirePlayerGlobal = (VampirePlayer*) malloc(sizeof(VampirePlayer)*MAX_VAMPIRE_PLAYERS);
	//memset(pVampirePlayerGlobal,0,sizeof(pVampirePlayerGlobal)*MAX_VAMPIRE_PLAYERS);
	//pPlayerUser = (sPlayerUser*) malloc(sizeof(sPlayerUser)*MAX_PLAYERS);
	//memset(pPlayerUser,0,sizeof(pPlayerUser)*MAX_PLAYERS);
	//pDataHumans = (sDataHumans*) malloc(sizeof(sDataHumans));
	//memset(pDataHumans,0,sizeof(sDataHumans));
	//pDataVampires = (sDataVampires*) malloc(sizeof(sDataVampires));
	//memset(pDataVampires,0,sizeof(sDataVampires));
}

// Îñâîáîäèòü âñå ñòýêè äåéñòâèé
void fFreeAllStacksActions(sStackActions *vFirst)
{
	if (vFirst == NULL) return;
	sStackActions *vCurrent = vFirst;
	sStackActions *vNext = NULL;
	while (vCurrent)
	{
		vNext = vCurrent->vNext;
		vCurrent->vNext = NULL;
		//free(vCurrent);
		delete vCurrent;
		vCurrent = vNext;
	}
}

// Óäàëèòü âñå öåïî÷êè
void fFreeAllChains(sStackChains *vFirst)
{
	/*if (vFirst == NULL) return;
	sStackChains *vCurrent = vFirst;
	sStackChains *vNext = NULL;
	while (vCurrent)
	{
		vNext = vCurrent->vNext;
		vCurrent->vNext = NULL;
		//free(vCurrent->vName);
		delete vCurrent->vName;
		vCurrent->vName = NULL;
		//free(vCurrent);
		delete vCurrent;
		vCurrent = vNext;
	}*/
}

void fFreePlayer()
{
	int vvi;
	for (vvi = 0; vvi < vCountHumans; vvi++)
	{
	if (&pHumanPlayerGlobal[vvi] != NULL)
	{
		if (pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer != NULL)
		{
			if (vEndGame2 == 0 &&
				pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer != NULL)
			{
				if (pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer > 0)
				{
					DestroyTimer(pHumanPlayerGlobal[vvi].vParamPlayer.vTimerPlayer);
				}
			}
		}
		//pHumanPlayerGlobal[vvi].vParamPlayer.fFree();
		//pHumanPlayerGlobal[vvi].vActionStackForUnits.fFreeStackForUnitsAll();
		//fFreeAllStacksActions(pHumanPlayerGlobal[vvi].vCurrentAction);
		//pHumanPlayerGlobal[vvi].vCurrentAction = NULL;
		//pHumanPlayerGlobal[vvi].vLastAction = NULL;
		//fFreeAllChains(pHumanPlayerGlobal[vvi].vCurrentChain);
		//pHumanPlayerGlobal[vvi].vCurrentChain = NULL;
		//pHumanPlayerGlobal[vvi].vLastChain = NULL;
		/*for (int vvi = 0; vvi < MAX_HUMAN_PLAYERS; vvi++)
		{
			if (&pHumanPlayer[vvi] != NULL)
			{
				free(&pHumanPlayer[vvi]);
			}
		}*/
	}
	}
	if (vCountHumans > 0)
	{
		//free(pHumanPlayerGlobal);
		delete[] pHumanPlayerGlobal;
		pHumanPlayerGlobal = NULL;
	}
	for (vvi = 0; vvi < vCountVampires; vvi++)
	{
	if (&pVampirePlayerGlobal[vvi] != NULL)
	{
		if (pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer != NULL)
		{
			if (vEndGame2 == 0 &&
				pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer != NULL)
			{
				if (pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer > 0)
				{
					DestroyTimer(pVampirePlayerGlobal[vvi].vParamPlayer.vTimerPlayer);
				}
			}
		}
		//pVampirePlayerGlobal[vvi].vParamPlayer.fFree();
		//pVampirePlayerGlobal[vvi].vActionStackForUnits.fFreeStackForUnitsAll();
		/*for (int vvi = 0; vvi < MAX_VAMPIRE_PLAYERS; vvi++)
		{
			if (&pVampirePlayer[vvi] != NULL)
			{
				free(&pVampirePlayer[vvi]);
			}
		}*/
	}
	}
	if (vCountVampires > 0)
	{
		//free(pVampirePlayerGlobal);
		delete[] pVampirePlayerGlobal;
		pVampirePlayerGlobal = NULL;
	}
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (&pPlayerUser[vvi] != NULL)
		{
			if (pPlayerUser[vvi].hTriggerPlayerLeave != NULL)
			{
				fFreeTrigger(pPlayerUser[vvi].hTriggerPlayerLeave);
			}
		}
	}
	if (pPlayerUser != NULL)
	{
		//free(pPlayerUser);
		delete[] pPlayerUser;
		pPlayerUser = NULL;
	}
	if (pDataHumans != NULL)
	{
		//free(pDataHumans);
		delete pDataHumans;
		pDataHumans = NULL;
	}
	if (pDataVampires != NULL)
	{
		//free(pDataVampires);
		delete pDataVampires;
		pDataVampires = NULL;
	}
	vCountHumans = 0;
	vCountVampires = 0;
	vCountPlayersUser = 0;
}

integer fStartCommands()
{
	sprintf(vBuf,"fStartCommands() N_1\0");
	fLog(999,vBuf);
	if (vStartCommandsGo == 1) return 0;
	if (vStartCommands == 1) return 0;
	try
	{
	sprintf(vBuf,"fStartCommands() N_2\0");
	fLog(999,vBuf);
	vMaxBase = fMaxBase();
	if (vMaxBase == 0)
	{
		return 0;
	}
	//if (fGetHost2() != GetLocalPlayer()) return 0;
	vStartCommandsGo = 1;
	vStartCommands = 0;

	vSetHostGo = 0;
	vHostPlayer = 0;

	vAuthorization = 0;
	vCountAttempts = 0;
	vGameBegin = 0;
	vWin = 0;

	vStartGameSleep = FALSE;

	vSync = FALSE;
	vSync2 = FALSE;
	vSync4 = FALSE;

	pFirstDataGoTo = 0;
	pLastDataGoTo = 0;

	//pCurrentCreateDataUnit = 0;
	//pLastCreateDataUnit = 0;
	//pCurrentDeleteDataUnit = 0;
	//pLastDeleteDataUnit = 0;
	//vNLastDataUnit2 = NO_UNIT;

	vActionStackForCreateUnit.fInit();
	vActionStackForDelUnit.fInit();
	vActionStackForCreateedUnit.fInit();
	vNLastStackForUnits = NO_UNIT;
	vNLastStackChains = NO_UNIT;

	for (unsigned short vvi = 0; vvi < MAX_COUNT_UNITS; vvi++)
	{
		//vDataUnit2[vvi].vNext = 0;
		//vDataUnit2[vvi].vUnit = 0;
		//vDataUnit2[vvi].vNextForDel = 0;
		//vDataUnit2[vvi].vIdDataUnit = vvi;
		//vDataUnit2[vvi].vId = 0;
		//vDataUnit2[vvi].vNPlayer = 0;
		//vDataUnit2[vvi].vPlayer = 0;

		vStackForUnits[vvi].next = 0;
		vStackForUnits[vvi].vIdDataUnit2.pDataUnit = NULL;
		vStackForUnits[vvi].vIdDataUnit2.vId = NO_UNIT;
		vStackForUnits[vvi].vIdDataUnit2.vIdForComments = COMMENT_NO_UNIT;
		vStackForUnits[vvi].vIdForComments = COMMENT_NO_UNIT;
		vStackForUnits[vvi].vN = vvi;

		vStackChains[vvi].vId = 0;
		vStackChains[vvi].vAction = 0;
		vStackChains[vvi].vN = 0;
		vStackChains[vvi].vComment = 0;
		vStackChains[vvi].vCommentUnit = 0;
		vStackChains[vvi].vComplite = 0;
		vStackChains[vvi].vMandatoryTrain = FALSE;
		vStackChains[vvi].vName = NULL;
		vStackChains[vvi].vNext = NULL;
		vStackChains[vvi].vNResearch = 0;
		vStackChains[vvi].vQTrain = 0;
		vStackChains[vvi].vStage = 0;
	}
	sprintf(vBuf,"fStartCommands() N_3\0");
	fLog(999,vBuf);
	fInitPlayer();
	fInitIdUnits();
	for (int vvj = 0; vvj < 2; vvj++)
	{
		fClearMapUnits(vvj);
	}
	sprintf(vBuf,"fStartCommands() N_4\0");
	fLog(999,vBuf);
	if (fUnitsMagazine() == 0) return 0;
	sprintf(vBuf,"fStartCommands() N_4_1\0");
	fLog(999,vBuf);
	fAddDataUnit(ID_UNIT_PASSIVE_BLOOD,Player(PLAYER_NEUTRAL_PASSIVE));
	
	sprintf(vBuf,"fStartCommands() N_4_2\0");
	fLog(999,vBuf);
	//hTriggerEndGame = CreateTrigger();
	//IncrementHandleReferences(hTriggerEndGame);
	//TriggerAddAction(hTriggerEndGame, &fStopCommands);
	//TriggerRegisterGameEvent(hTriggerEndGame, EVENT_GAME_VICTORY);
	//TriggerRegisterGameEvent(hTriggerEndGame, EVENT_GAME_END_LEVEL);
	//TriggerRegisterGameEvent(hTriggerEndGame, EVENT_GAME_LEAVE_REGION);

	hTriggerChat = CreateTrigger();
	IncrementHandleReferences(hTriggerChat);
	TriggerAddAction(hTriggerChat, &fChat);
	sprintf(vBuf,"fStartCommands() N_4_3\0");
	fLog(999,vBuf);
	for (int vvi = 0; vvi < MAX_PLAYERS; vvi++)
	{
		hTriggerWinPlayer[vvi] = CreateTrigger();
		IncrementHandleReferences(hTriggerWinPlayer[vvi]);
		TriggerAddAction(hTriggerWinPlayer[vvi], &fWinPlayer);
		TriggerRegisterPlayerEvent(hTriggerWinPlayer[vvi],vHostPlayer,EVENT_PLAYER_VICTORY);
	}
	sprintf(vBuf,"fStartCommands() N_5\0");
	fLog(999,vBuf);
	hRegionWorldBounds = CreateRegion();
	vWorldRect = GetWorldBounds();
	RegionAddRect(hRegionWorldBounds, vWorldRect);
	
	hTriggerUnitEntersWorldBoundsRegion = CreateTrigger();
	IncrementHandleReferences(hTriggerUnitEntersWorldBoundsRegion);
	TriggerAddAction(hTriggerUnitEntersWorldBoundsRegion, &fActionUnitEntersWorldBoundsRegion);
	TriggerRegisterEnterRegion(hTriggerUnitEntersWorldBoundsRegion,hRegionWorldBounds,NULL);

	//hTriggerUnitLeaveWorldBoundsRegion = CreateTrigger();
	//IncrementHandleReferences(hTriggerUnitLeaveWorldBoundsRegion);
	//TriggerAddAction(hTriggerUnitLeaveWorldBoundsRegion, &fActionUnitLeaveWorldBoundsRegion);
	//TriggerRegisterLeaveRegion(hTriggerUnitLeaveWorldBoundsRegion,hRegionWorldBounds,NULL);
	sprintf(vBuf,"fStartCommands() N_6\0");
	fLog(999,vBuf);
	vSyncCache = InitGameCache("Sync");
	vSyncCache2 = InitGameCache("Sync2");
	vSyncCache3 = InitGameCache("Sync3");
	vSyncCache4 = InitGameCache("Sync4");
	vAuthorizationCache = InitGameCache("Authorization");
	vNumLog = 0;
	//fFogOfWar
	//vTimerForFogOfWar = CreateTimer();
	//TimerStart(vTimerForFogOfWar,1,true,&fFogOfWar);

	//unit vManaWell = fGetUnit(ID_UNIT_PASSIVE_MANA_KOLODEC,Player(PLAYER_NEUTRAL_PASSIVE));
	unit vUnit_ManaWell;
	real vX,vY;
	//vUnit_ManaWell = vDataUnits[vManaWell].pDataUnit->WhichUnit;
	//vX = GetUnitX(vUnit_ManaWell);
	//vY = GetUnitY(vUnit_ManaWell);
	vX = 0;
	vY = 512;
	vManaWellRect = Rect(vX - 600,vY + 600,vX + 600,vY - 600);
	vX = -1500;
	vY = -1500;
	vBaseZeroRect = Rect(vX - 1000,vY + 1000,vX + 1000,vY - 1000);
	vBaseVampires = Rect(-3000,3000,3000,-3000);
	sprintf(vBuf,"fStartCommands() N_7\0");
	fLog(999,vBuf);
	real vRealX, vRealY;
	//real vRealCell = (float)DOOD_MAP_SUBDIV/2;
	//real vRealForCenterCell = (float)DOOD_MAP_SUBDIV/4;
	for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
		vRealX = fGetMinIX2RX(vvi%vW3);
		vRealY = fGetMinIY2RY(vvi/vW3);
		//hRectMap[vvi] = Rect(vRealX,vRealY,vRealX+vRealCell,vRealY-vRealCell);
		hLocationMap[vvi] = Location(vRealX,vRealY);
		//hLocationCenterCellMap[vvi] = Location(vRealX+vRealForCenterCell,vRealY-vRealForCenterCell);
	}
	//vTimerGame = CreateTimer();
	//TimerStart(vTimerGame,2*60*60,false,NULL);
	vHT = InitHashtable();
	IncrementHandleReferences(vHT);
	//vHT_2 = InitHashtable();
	//FlushChildHashtable(vHT_2,CONST_DATA_UNIT_BY_UNIT);
	//fSendAllPlayer("Enter the command in the chat ""-host"" to be the host.");
	vStartCommands = 1;
	vStartCommandsGo = 0;
	sprintf(vBuf,"fStartCommands() N_8\0");
	fLog(999,vBuf);
	//CreateUnit(Player(10),ID_UNIT_GRIVE,0,0,0);
	//CreateUnit(Player(0),ID_UNIT_PASSIVE_BLOOD,0,0,0);
	/*CreateUnit(Player(0),1966092359,0,0,0);
	CreateUnit(Player(0),1751414125,200,0,0);
	CreateUnit(Player(0),1750364771,400,0,0);
	//CreateUnit(Player(0),1848651828,300,0,0);
	//CreateUnit(Player(0),1848651829,400,0,0);
	CreateUnit(Player(0),1747988784,600,0,0);
	CreateUnit(Player(0),1429221432,0,-400,0);
	CreateUnit(Player(0),1429221431,200,-400,0);
	CreateUnit(Player(0),1747989063,0,100,0);
	CreateUnit(Player(0),1848651847,200,100,0);
	CreateUnit(Player(0),1848651856,400,100,0);
	CreateUnit(Player(0),1747989066,600,100,0);
	CreateUnit(Player(0),1747989064,800,100,0);
	CreateUnit(Player(0),1848651847,0,200,0);
	CreateUnit(Player(0),1848651851,200,200,0);
	CreateUnit(Player(0),1848651855,400,200,0);
	CreateUnit(Player(0),1848651854,600,200,0);
	CreateUnit(Player(0),1867934067,800,200,0);
	CreateUnit(Player(0),1966092341,0,300,0);
	CreateUnit(Player(0),1848651827,200,300,0);
	CreateUnit(Player(0),1966092359,400,300,0);
	CreateUnit(Player(0),1848651829,0,-300,0);
	CreateUnit(Player(0),1966092341,200,-300,0);
	CreateUnit(Player(0),1867934067,400,-300,0);
	CreateUnit(Player(0),1848651854,600,-300,0);
	CreateUnit(Player(0),1966092359,700,-300,0);*/
	//CreateItem(1227895107,0,0);
	//CreateItem(1227895106,200,0);
	/*for (int vvi = 0; vvi < 12; vvi++)
	{
		sprintf(vTest,"GetItemTypeId = %d",GetItemTypeId(UnitItemInSlot(vDataUnits[vTopShop].pDataUnit->WhichUnit,vvi)));
		fSendAllPlayer(vTest);
	}*/
	/*int v1 = OrderId("inferno");
	v1 = OrderId("flare");
	unit u;
	SetPlayerState(Player(0),PLAYER_STATE_RESOURCE_GOLD,500000);
	u = CreateUnit(Player(0),ID_UNIT_INFERNAL,0,0,0);
	for (int vvi = 0; vvi <= 0xffffffff; vvi++)
	{
		if (vvi != ID_ORDER_STOP &&
			vvi != ID_ORDER_HOLDPOSITION &&
			vvi != ID_ORDER_PATROL &&
			vvi != ID_ORDER_ATTACK &&
			vvi != ID_ORDER_SMART &&
			vvi != ID_ORDER_MOVE &&
			vvi != 851973 &&
			vvi != 851984 &&
			vvi != 851985 &&
			vvi != 851991 &&
			vvi != 851987 &&
			vvi != 851975 &&
			vvi != 852019 &&
			vvi != 852116 &&
			vvi != 852131)
		if (IssueImmediateOrderById(u,vvi) == TRUE)
		{
			break;
		}
	}
	v1 = v1;*/
	}
	catch(...)
	{
		return 0;
	}
	sprintf(vBuf,"fStartCommands() N_9\0");
	fLog(999,vBuf);
	return 1;
}

void fActionUnitEntersWorldBoundsRegion()
{
	sprintf(vBuf,"fActionUnitEntersWorldBoundsRegion() N_1 NameUnit = %s\0",GetUnitName(GetTriggerUnit()));
	fLog(999,vBuf);
	fCreateDataUnit(GetTriggerUnit(),THREAD_MAIN);
}

/*void fActionUnitLeaveWorldBoundsRegion()
{
	HUNIT vUnit;
	integer vId;
	vUnit = GetTriggerUnit();
	vId = GetUnitTypeId(vUnit);
	if (!fIsSlayer(vId))
	{
		DataUnit *pDataUnit;
		pDataUnit = fGetDataUnit(vUnit);
		fDelDataUnit(pDataUnit->vIdDataUnit);
	}
}*/

void fWinPlayer()
{
	sprintf(vBuf,"fWinPlayer() N_1\0");
	fLog(999,vBuf);
	if (vAuthorization == 0 || vWin != 0) return;
	HPLAYER vWinPlayer;
	HPLAYERCOLOR vNPlayer;
	vWinPlayer = GetWinningPlayer();
	vNPlayer = GetPlayerColor(vWinPlayer);
	if (vNPlayer >= 0 && vNPlayer <= 9)
	{
		fWin(HUMANS);
		return;
	}
	if (vNPlayer >= 10 && vNPlayer <= 11)
	{
		fWin(VAMPIRES);
		return;
	}
}

void fWin(int vStorona)
{
	sprintf(vBuf,"fWin(int vStorona) N_1\0");
	fLog(999,vBuf);
	if (vStorona != HUMANS && vStorona != VAMPIRES) return;
	if (vAuthorization == 0 || vWin != 0) return;
	vWin = 1;
	if (fStopCommands() == 0) return;
	return;// Íå çàáóäò ýòî ïîòîì óáðàòü!
	int vvi;
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (pPlayerUser[vvi].vDead == FALSE)
		{
			goto m1;
		}
	}
	return;
m1:
	//char vSQL[1000];
	char vGame_time[20];
	char vÑountry[100];
	int vLen;
	char vInternet_date[8 + 1 + 8 + 1];
	float vTime;
	vInternet_date[8 + 1 + 8] = '\0';
	vTime = TimerGetElapsed(vTimerGame);
	if (vTime == 0) return;
	sprintf(vGame_time,"%d:%d:%d\0",(int)(vTime/(60*60)),(int)(vTime/(60)),(int)(vTime)%60);
	/*if (fGetInternetTime(vInternet_date) == FALSE)
	{
		vInternet_date[0] = '\0';
	}*/
	//vSQL[0] = '\0';
	for (vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		/*sprintf(vNickName,"%s\0",GetPlayerName(Player(vvi)));
		vLen = strlen(vNickName);
		if (vLen == 12)
		{
			if (memcmp(vNickName,"Ð˜Ð³Ñ€Ð¾Ðº ",vLen-2) == 0)
			{
				continue;
			}
		}*/
		//fRemoveDeadToNickName(vNickName);
		vÑountry[0] = '\0';
		vÑountry[1] = '\0';
		//fGetCountry(vÑountry,pPlayerUser[vvi].vName);
		vLen = strlen(vÑountry);
		if (vLen > 0)
		{
			vÑountry[0] = '\0';
			vÑountry[vLen-1] = '\0';
			fUTF8(vÑountry+1,vÑountry+1);
		}
		//fAddWin(pPlayerUser[vvi].vName,vInternet_date,vGame_time,vÑountry+1,NULL);
		//sprintf(vSQL,"%sINSERT INTO `win1` (`name`, `date`, `game_time`) VALUES ('%s', '%s', '%s');",vSQL,GetPlayerName(Player(vvi)),vInternet_date,vGame_time);
	}
	/*if (fAddSQL(vSQL) == TRUE)
	{
		vWin = 1;
	}*/
}

bool fRemoveDeadToNickName(char *vNickName)
{
	if (vNickName == NULL) return FALSE;
	int vvi;
	int vLen;
	int vIndex1;
	int vIndex2;
	vIndex1 = -1;
	vIndex2 = -1;
	try
	{
		vLen = strlen(vNickName);
		if (vLen > 0)
		{
			for (vvi = 0; vvi < vLen; vvi++)
			{
				if (vNickName[vvi] == '(')
				{
					vIndex1 = vvi;
				}
				if (vNickName[vvi] == ')')
				{
					vIndex2 = vvi;
				}
			}
			if (vIndex2 == vLen - 1)
			{
				vNickName[vIndex1 - 1] = '\0';
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

bool fCheckLeftToNickName(char *vNickName)
{
	if (vNickName == NULL) return FALSE;
	int vvi;
	int vvj;
	int vvu;
	int vLen;
	int vIndex1;
	int vIndex2;
	int vCount;
	vIndex1 = -1;
	vIndex2 = -1;
	try
	{
		vLen = strlen(vNickName);
		if (vLen > 0)
		{
			for (vvi = 0; vvi < vLen; vvi++)
			{
				if (vNickName[vvi] == '(')
				{
					vIndex1 = vvi;
				}
				if (vNickName[vvi] == ')')
				{
					vIndex2 = vvi;
				}
			}
			if (vIndex2 == vLen - 1)
			{
				vCount = 0;
				vvu = 0;
				for (vvj = vIndex1 + 1; vvj <= vIndex2 - 1; vvj++)
				{
					if (vNickName[vvj] == 'L' && vvu == 0) vCount++;
					if (vNickName[vvj] == 'e' && vvu == 1) vCount++;
					if (vNickName[vvj] == 'f' && vvu == 2) vCount++;
					if (vNickName[vvj] == 't' && vvu == 3) vCount++;
					vvu++;
				}
				if (vCount == 4)
				{
					return TRUE;
				}
				vCount = 0;
				vvu = 0;
				for (vvj = vIndex1 + 1; vvj <= vIndex2 - 1; vvj++)
				{
					if (vNickName[vvj] == 'D' && vvu == 0) vCount++;
					if (vNickName[vvj] == 'e' && vvu == 1) vCount++;
					if (vNickName[vvj] == 'a' && vvu == 2) vCount++;
					if (vNickName[vvj] == 'd' && vvu == 3) vCount++;
					if (vNickName[vvj] == ' ' && vvu == 4) vCount++;
					if (vNickName[vvj] == '&' && vvu == 5) vCount++;
					if (vNickName[vvj] == ' ' && vvu == 6) vCount++;
					if (vNickName[vvj] == 'L' && vvu == 7) vCount++;
					if (vNickName[vvj] == 'e' && vvu == 8) vCount++;
					if (vNickName[vvj] == 'f' && vvu == 9) vCount++;
					if (vNickName[vvj] == 't' && vvu == 10) vCount++;
					vvu++;
				}
				if (vCount == 11)
				{
					return TRUE;
				}
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	}
	catch(...)
	{
		return FALSE;
	}
	return FALSE;
}

bool fUTF8(char *vUTF8, char *vANSI)
{
	if (vUTF8 == NULL) return FALSE;
	int vvi;
	char vTemp[100];
	int vLen;
	int vLen2;
	try
	{
		vLen = strlen(vUTF8);
		if (vLen > 0)
		{
			vLen2 = 0;
			for (vvi = 0; vvi < vLen; vvi++)
			{
				if (vUTF8[vvi] == '\\')
				{
					vTemp[vLen2] = vUTF8[vvi];
					vLen2++;
					vTemp[vLen2] = vUTF8[vvi];
					vLen2++;
				}
				else
				{
					vTemp[vLen2] = vUTF8[vvi];
					vLen2++;
				}
			}
			vTemp[vLen2] = '\0';
			if (vANSI != NULL)
			{
				vANSI[0] = '\0';
				sprintf(vANSI,vTemp);
			}
		}
		else
		{
			return FALSE;
		}
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

bool fUTF8ToANSI_UNICODE(char *vUTF8, char *vANSI, wchar_t *vUNICODE)
{
	if (vUTF8 == NULL) return FALSE;
	int vvi;
	wchar_t vChar[100];
	char vChar2[5];
	wchar_t c;
	int size_needed;
	int vLen;
	int vLen2;
	try
	{
		vLen = strlen(vUTF8);
		if (vLen > 0)
		{
			vLen2 = 0;
			for (vvi = 0; vvi < vLen; vvi++)
			{
				if (vUTF8[vvi] == '\\')
				{
					vChar[vLen2] = vUTF8[vvi];
					if (vUTF8[vvi + 1] != 'u'){
						vChar[vLen2] = vUTF8[vvi + 1];
						vvi += 1;
					}
					else if((vvi + 5) <= vLen - 1)
					{
						try
						{
							memcpy(vChar2,&vUTF8[vvi+2],4);
							vChar2[4] = '\0';
							vChar[vLen2] = strtol( vChar2, NULL, 16 );
							vLen2++;
						}
						catch (...)
						{
							return FALSE;
						}
						vvi += 5;
					}
				}
				else
				{
					vChar[vLen2] = vUTF8[vvi];
				}
			}
			vChar[vLen2] = '\0';
			if (vANSI != NULL)
			{
				vANSI[0] = '\0';
				size_needed = wcslen(vChar);
				WideCharToMultiByte(CP_ACP, 0, vChar, -1, vANSI, size_needed, NULL, NULL);
				vANSI[size_needed] = '\0';
			}
			if (vUNICODE != NULL)
			{
				vUNICODE[0] = '\0';
				wsprintf(vUNICODE,vChar);
			}
		}
		else
		{
			return FALSE;
		}
	}
	catch(...)
	{
		return FALSE;
	}
	return TRUE;
}

void fSendAllPlayer(String vMessage)
{
	int vvi;
	for (vvi = 0; vvi < MAX_PLAYERS; vvi++)
	{
		DisplayTextToPlayer( Player(vvi), 0.0, 0.0, vMessage );
	}
}

void fChat()
{
	integer vStrLen;
	String vStr;
	vStrLen = 0;
	vStr = GetEventPlayerChatString();
	if (vStr == NULL) return;
	vStrLen = strlen(vStr);
	/*if (vAuthorization == 0 && vGameBegin == 1 && vStrLen == 19 + 5)
	{
		if (memcmp("-key",vStr,4) != 0) return;
		if (vCountAttempts >= 3) return;
		char vMessage[20];
		sprintf(vMessage,"%s\0",vStr+5);
		if (fChekAuthorization(vMessage) == TRUE)
		{
			vAuthorization = 1;
			StoreInteger(vAuthorizationCache,"Map","vAuthorization",vAuthorization);
			SyncStoredInteger(vAuthorizationCache,"Map","vAuthorization");
			fSendAllPlayer("The key is activated.");
		}
		else
		{
			vCountAttempts++;
			fSendAllPlayer("The key didn't fit.");
		}
	}*/
	if (vStrLen == 5)
	{
		if (memcmp("-stop",vStr,5) == 0)
		{
			fStopCommands();
			HPLAYER vPlayer = GetLocalPlayer();
			DisplayTimedTextToPlayer( vPlayer, 0.0, 0.0, 5, "Game stoped!" );
			for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
			{
				if (pPlayerUser[vvi].vPlayer == vPlayer)
				{
					pPlayerUser[vvi].vLeave = TRUE;
					break;
				}
			}
			fStopCommands();
			return;
		}
	}
	if (GetStoredInteger(vAuthorizationCache,"Map","vHostPlayer") == 0 && vStrLen == 5)
	{
		if (memcmp("-host",vStr,5) == 0)
		{
			/*fSetHost(GetLocalPlayer());
			if (vHostPlayer > 0)
			{
				//hTriggerEndGame3 = CreateTrigger();
				//IncrementHandleReferences(hTriggerEndGame3);
				//TriggerAddAction(hTriggerEndGame3, &fStopCommands);
				//TriggerRegisterPlayerEvent(hTriggerEndGame3,vHostPlayer,EVENT_PLAYER_LEAVE);

				//TriggerRegisterPlayerChatEvent(hTriggerChat,vHostPlayer,"-key",false);
				TriggerRegisterPlayerChatEvent(hTriggerChat,vHostPlayer,"-stop",true);
				char vMessage2[200];
				sprintf(vMessage2,"Host - ""%s""",GetPlayerName(vHostPlayer));
				fSendAllPlayer(vMessage2);
				//DisplayTimedTextToPlayer( vHostPlayer, 0.0, 0.0, 5, "You are the host. Now you can enter the activation key." );
				//DisplayTimedTextToPlayer( vHostPlayer, 0.0, 0.0, 5, "You have 3 attempts to enter the key within 3 minutes." );
			}*/
			return;
		}
	}
	if (vStrLen >= 7)
	{
		if (memcmp("-kill",vStr,5) == 0)
		{
			/*char vMessage[20];
			int vInt2;
			sprintf(vMessage,"%s\0",vStr+6);
			vInt2 = atoi(vMessage);
			KillUnit(vDataUnits[vInt2].pDataUnit->WhichUnit);*/
			return;
		}
	}
	//KillUnit(vDataUnits[10].pDataUnit->WhichUnit);
	// Ðåñû
	//SetPlayerState(Player(0),PLAYER_STATE_RESOURCE_GOLD,500000);
	//SetPlayerState(Player(0),PLAYER_STATE_RESOURCE_LUMBER,500000);
	//sprintf(vTest,"ID_SKILL_MANABURN = %s",AbilityId2String(ID_SKILL_MANABURN));
	//DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 5, vTest );
	//sprintf(vTest,"1097364080 = %s",AbilityId2String(1097364080));
	//DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 5, vTest );
	//sprintf(vTest,"1097687650 = %s",AbilityId2String(1097687650));
	//DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 5, vTest );
}

integer fFreeTrigger(HTRIGGER hTrigger)
{
	if (vEndGame2 == 1) return 0;
	if (!hTrigger || hTrigger == 0xcdcdcdcd) return 0;
	DecrementHandleReferences(hTrigger);
	TriggerClearActions(hTrigger);
	DestroyTrigger(hTrigger);
	return 1;
}

integer fStopCommands()
{
	sprintf(vBuf,"fStopCommands() N_1");
	fLog(999,vBuf);
	if (vStartCommands == 0) return 0;
	int vvi;
	fFreeTrigger(hTriggerUnitEntersWorldBoundsRegion);
	//fFreeTrigger(hTriggerUnitLeaveWorldBoundsRegion);
	fFreeTrigger(hTriggerFillMapUnits);
	fFreeTrigger(hTriggerEndGame);
	if (hTriggerEndGame2 != 0) fFreeTrigger(hTriggerEndGame2);
	fFreeTrigger(hTriggerEndGame3);
	fFreeTrigger(hTriggerChat);
	fFreeTrigger(hTriggerChatAI);
	
	sprintf(vBuf,"fStopCommands() N_2");
	fLog(999,vBuf);
	if (vEndGame2 == 0)
	{
		RemoveRegion(hRegionWorldBounds);

		for (vvi = 0; vvi < vSizeMap; vvi++)
		{
			//RemoveRect(hRectMap[vvi]);
			RemoveLocation(hLocationMap[vvi]);
			//RemoveLocation(hLocationCenterCellMap[vvi]);
		}
		for (vvi = 0; vvi < MAX_PLAYERS; vvi++)
		{
			fFreeTrigger(hTriggerWinPlayer[vvi]);
		}

		RemoveRect(vWorldRect);
		RemoveRect(vManaWellRect);
		RemoveRect(vBaseZeroRect);
		RemoveRect(vBaseVampires);

		if (vTimerForFogOfWar > 0)
		{
			DestroyTimer(vTimerForFogOfWar);
			vTimerForFogOfWar = NULL;
		}
		if (vTimerGame > 0)
		{
			DestroyTimer(vTimerGame);
			vTimerGame = NULL;
		}
		if (vTimerActionsGame > 0)
		{
			DestroyTimer(vTimerActionsGame);
			vTimerActionsGame = NULL;
		}
		if (vTimerPreActionsGame > 0)
		{
			DestroyTimer(vTimerPreActionsGame);
			vTimerPreActionsGame = NULL;
		}
		if (vTimerDataUnit2 > 0)
		{
			DestroyTimer(vTimerDataUnit2);
			vTimerDataUnit2 = NULL;
		}
		if (vTimerMain > 0)
		{
			DestroyTimer(vTimerMain);
			vTimerMain = NULL;
		}
		if (vTextTagMap > 0)
		{
			DestroyTextTag(vTextTagMap);
			vTextTagMap = NULL;
		}
		FlushParentHashtable(vHT);
		//FlushParentHashtable(vHT_2);
		FlushGameCache(vSyncCache);
		FlushGameCache(vSyncCache2);
		FlushGameCache(vSyncCache3);
		FlushGameCache(vSyncCache4);
		FlushGameCache(vAuthorizationCache);
	}
	
	sprintf(vBuf,"fStopCommands() N_3");
	fLog(999,vBuf);
	vMaxBase = 0;
	vHostPlayer = 0;
	vAuthorization = 0;
	vCountAttempts = 0;
	vStartCommands = 0;
	vStartCommandsGo = 0;
	
	sprintf(vBuf,"fStopCommands() N_4");
	fLog(999,vBuf);
	//Sleep(2000);
	fFreeDataUnit();
	fFreeDataGoTo();
	fFreePlayer();
	
	sprintf(vBuf,"fStopCommands() N_5");
	fLog(999,vBuf);
	if (pMapGlobal != NULL)
	{
		//free(pMapGlobal);
		delete[] pMapGlobal;
		pMapGlobal = NULL;
	}
	
	sprintf(vBuf,"fStopCommands() N_6");
	fLog(999,vBuf);
	return 1;
}

void fFreeDataUnit()
{
	if (vLastUnit == NO_UNIT) return;
	unsigned short vvi;
	for (vvi = 0; vvi <= vLastUnit; vvi++)
	{
		if (vLastUnit == NO_UNIT) break;
		fDelDataUnit2(vvi);
	}
	if (vLastUnit != NO_UNIT)
	{
		vLastUnit = vLastUnit;
	}
	vLastUnit = NO_UNIT;
}

void fActionFillMapUnits(DataGoTo *pDataGoTo)
{
	if (vLastUnit == NO_UNIT) return;
	real vTimeGame = TimerGetElapsed(vTimerGame);
	if (vTimeGame - vTimeActionFillMapUnits <= 0.2)
	{
		return;
	}
	vTimeActionFillMapUnits = vTimeGame;
	fClearMapUnits(pDataGoTo->vStorona);
	unsigned short vvi;
	DataUnit *pDataUnit;
	for (vvi = 0; vvi <= vLastUnit; vvi++)
	{
		pDataUnit = fGetDataUnit(vvi);
		if (pDataUnit)
		{
			fFillUnitToMap(pDataUnit);
		}
	}
}

bool fInitMapGlobal()
{
	//vMapGlobal = fInitMap(fGetPath());
	vMapGlobal = fInitMapFromResources();
	if (vMapGlobal != NULL)
	{
		return TRUE;
	}
	return FALSE;
}

void fInitIdUnits()
{
	for (unsigned short vvi = 0; vvi < MAX_COUNT_UNITS; vvi++)
	{
		vDataUnits[vvi].vId = vvi;
		fSetUnit(vvi,NULL);
	}
}

integer fSetUnit(unsigned short vIdDataUnit, DataUnit *pDataUnit)
{
	try
	{
		vDataUnits[vIdDataUnit].pDataUnit = pDataUnit;
	}
	catch (...)
	{
		return 0;
	}
	return 1;
}

DataUnit * fGetLastConstruct()
{
	if (vLastUnit == NO_UNIT) return 0;
	try
	{
		for (unsigned short vvi = 0; vvi <= vLastUnit; vvi++)
		{
			if (vDataUnits[vvi].pDataUnit)
			{
				if (vDataUnits[vvi].pDataUnit->Zdanie == TRUE &&
					vDataUnits[vvi].pDataUnit->Postroeno == FALSE &&
					GetUnitTypeId(vDataUnits[vvi].pDataUnit->WhichUnit) == 0)
				{
					return vDataUnits[vvi].pDataUnit;
				}
			}
		}
	}
	catch (...)
	{
		return 0;
	}
	return 0;
}

DataUnit * fGetDataUnit(unsigned short vIdDataUnit)
{
	if (vIdDataUnit == NO_UNIT) return 0;
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS) return 0;
	try
	{
		if (vDataUnits[vIdDataUnit].pDataUnit &&
			vDataUnits[vIdDataUnit].pDataUnit->Dead == TRUE) return 0;
		return vDataUnits[vIdDataUnit].pDataUnit;
	}
	catch (...)
	{
		return 0;
	}
	return 0;
}

DataUnit * fGetDataUnit(HUNIT whichUnit, unsigned short &vOutIdDataUnit)
{
	if (vLastUnit == NO_UNIT) return 0;
	if (whichUnit == NULL) return 0;
	try
	{
		for (unsigned short vvi = 0; vvi <= vLastUnit; vvi++)
		{
			if (vDataUnits[vvi].pDataUnit)
			{
				if (vDataUnits[vvi].pDataUnit->WhichUnit == whichUnit)
				{
					vOutIdDataUnit = vvi;
					return vDataUnits[vvi].pDataUnit;
				}
			}
		}
	}
	catch (...)
	{
		return 0;
	}
	return 0;
}

DataUnit * fGetDataUnit(HUNIT whichUnit)
{
	if (whichUnit == NULL) return NULL;
	DataUnit *pDataUnit;
	unsigned short vOutIdDataUnit;
	//pDataUnit = (DataUnit *)LoadInteger(vHT_2,whichUnit,CONST_DATA_UNIT_BY_UNIT);
	pDataUnit = fGetDataUnit(whichUnit,vOutIdDataUnit);
	if (pDataUnit && (pDataUnit->Dead == TRUE || (unsigned int)pDataUnit->vName == 0xfeeefeee))
	{
		return NULL;
	}
	return pDataUnit;
}

/*DataUnit * fGetDataUnit(HTIMER vTimer)
{
	if (vTimer == NULL || vLastUnit == NO_UNIT) return 0;
	try
	{
		for (unsigned short vvi = 0; vvi <= vLastUnit; vvi++)
		{
			if (vDataUnits[vvi].pDataUnit)
			{
				if (vDataUnits[vvi].pDataUnit->vTimerMove == vTimer)
				{
					return vDataUnits[vvi].pDataUnit;
				}
			}
		}
	}
	catch (...)
	{
		return 0;
	}
	return 0;
}*/

/*integer fSetSpell(sDataSpell *pSpell, integer vId, integer lvl, bool Available)
{
	if (vId <= 0) return 0;
	pSpell->vId = vId;
	pSpell->Available = Available;
	pSpell->lvl = lvl;
	switch (vId)
	{
		case 1:
			//sprintf(pSpell->Spell,"%s","");
			pSpell->CoolDown = lvl * 1;
			pSpell->Damage = lvl * 1;
			pSpell->vRadius = 10;
			break;
	}
	return 1;
}*/

integer fCreateGoToDataUnit(unsigned short vIdDataUnit, unsigned int vMaxRect)
{
	if (vMaxRect <= 0) return 0;
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (!pDataUnit) return 0;
	pDataUnit->vGoTo = TRUE;
	pDataUnit->pPut = new struct DataGoTo;
	pDataUnit->pPut->pFillMap = new unsigned short[vSizeMap];
	//pDataUnit->pPut = (DataGoTo*) malloc(sizeof(DataGoTo));
	//pDataUnit->pPut->pFillMap = (unsigned short*) malloc(sizeof(unsigned short)*vSizeMap);
	//pDataUnit->pPut->vMassRect = new sRect*[vMaxRect];
	//pDataUnit->pPut->vMassRect2 = new unsigned int*[vMaxRect];
	pDataUnit->pPut->vMaxRect = vMaxRect;
	/*for (int vvi = 0; vvi < vMaxRect; vvi++)
	{
		//pDataUnit->pPut->vMassRect[vvi] = new sRect[1]; 
		//pDataUnit->pPut->vMassRect2[vvi] = new unsigned int[1];
	}*/
	pDataUnit->pPut->vMassRect = new struct sRect[vMaxRect];
	pDataUnit->pPut->vMassRect2 = new unsigned int[vMaxRect];
	//pDataUnit->pPut->vMassRect = (sRect*) malloc(sizeof(sRect)*vMaxRect);
	//pDataUnit->pPut->vMassRect2 = (unsigned int*) malloc(sizeof(unsigned int)*vMaxRect);
	pDataUnit->pPut->vActionStr = (char*) malloc(sizeof(char)*10);
	pDataUnit->pPut->vResult = 0;
	//pDataUnit->pPut->hTrigger = 0;
	//pDataUnit->pPut->hTimer = 0;
	pDataUnit->pPut->hThreadGoTo = NULL;
	return 1;
}

unsigned short fGetFreeIdDataUnit()
{
	DataUnit *pDataUnit;
	for (unsigned short vvi = MAX_OLD_UNITS + 1; vvi < MAX_COUNT_UNITS; vvi++)
	{
		pDataUnit = fGetDataUnit(vvi);
		if (pDataUnit == NULL)
		{
			return vvi;
		}
	}
	return NO_UNIT;
}

integer fCheckCreateIdDataUnit(unsigned short IdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(IdDataUnit);
	try
	{
		if (pDataUnit)
		{
			if (pDataUnit->WhichUnit)
				return 1;
		}
	}
	catch(...)
	{
		return -1;
	}
	return 0;
}

bool fIsResearch(integer vId)
{
	if (vId == ID_UNIT_BUILD_KUZNYA)
	{
		return TRUE;
	}
	return FALSE;
}

bool fIsBuildFirst(integer vId)
{
	if (vId == ID_UNIT_BUILD_GRADE_TENT ||
		vId == ID_UNIT_BUILD_GOLD_MINE_WHITE)//fRadius_Unit(vId)*2 > 128 &&
		//vId != ID_UNIT_BUILD_SLAYER_TAVERN)
	{
		return TRUE;
	}
	return FALSE;
}

bool fIsInfernal(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_INFERNAL:
	case ID_UNIT_FIRE_INFERNAL:
	case ID_UNIT_ACIDIC_INFERNAL:
	case ID_UNIT_FROST_INFERNAL:
		return TRUE;
	}
	return FALSE;
}

bool fIsHouse(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_BUILD_DOM:
		return TRUE;
	}
	return FALSE;
}

bool fIsWall(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_BUILD_WALL_BROWN:
	case ID_UNIT_BUILD_WALL_YELLOW:
	case ID_UNIT_BUILD_WALL_VIOLET:
	case ID_UNIT_BUILD_WALL_BLUE:
	case ID_UNIT_BUILD_WALL_GREEN:
	case ID_UNIT_BUILD_WALL_EMIRALD:
	case ID_UNIT_BUILD_WALL_DARKGREEN:
	case ID_UNIT_BUILD_WALL_DARKRED:
	case ID_UNIT_BUILD_WALL_RUBY:
	case ID_UNIT_BUILD_WALL_DIAMIND:
		return TRUE;
	}
	return FALSE;
}

bool fIsSlayer(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_SLAYER1:
	case ID_UNIT_SLAYER2:
		return TRUE;
	}
	return FALSE;
}

bool fIsVampire(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_VAMPIRE1:
	case ID_UNIT_VAMPIRE2:
	case ID_UNIT_VAMPIRE3:
		return TRUE;
	}
	return FALSE;
}

bool fIsFirstTower(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_BUILD_TOWER_WHITE:
	case ID_UNIT_BUILD_TOWER_GOLD:
	case ID_UNIT_BUILD_TOWER_KONUS:
	case ID_UNIT_BUILD_TOWER_MANA_LVL1:
	case ID_UNIT_BUILD_TOWER_HEAL_LVL1:
		return TRUE;
	}
	return FALSE;
}

bool fIsBigHPTower(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_BUILD_TOWER_MANA_LVL1:
	case ID_UNIT_BUILD_TOWER_HEAL_LVL1:
		return TRUE;
	}
	return FALSE;
}

bool fIsHealTower(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_BUILD_TOWER_HEAL_LVL1:
	case ID_UNIT_BUILD_TOWER_HEAL_LVL2:
		return TRUE;
	}
	return FALSE;
}

bool fIsMainHuman(integer vId)
{
	if (vId <= 0) return FALSE;
	switch(vId)
	{
	case ID_UNIT_MAIN_HUMAN1:
	case ID_UNIT_MAIN_HUMAN2:
		return TRUE;
	}
	return FALSE;
}

unsigned short fCreateDataUnit2(sDataUnit2 *pDataUnit2)
{
	if (pDataUnit2 == NULL) return NO_UNIT;
	if (pDataUnit2->vUnit <= 0) return NO_UNIT;
	if (GetUnitTypeId(pDataUnit2->vUnit) == 0)
	{
		RemoveUnit(pDataUnit2->vUnit);
		return NO_UNIT;
	}
	unsigned short vIdDataUnitOut;
	DataUnit *pDataUnit;
	unsigned short vIdDataUnit = NEW_UNIT;
	//integer vId;
	//pDataUnit = fGetDataUnit(vUnit);
	//if (pDataUnit != NULL) return -1;
	/*vId = GetUnitTypeId(pDataUnit2->vUnit);
	if (vId == 0)
	{
		fAddAllDataUnit();
		return NO_UNIT;
	}
	if (fIsSlayer(vId))
	{
		if (fGetDataUnit((unsigned short)(GetPlayerColor(GetOwningPlayer(vUnit)) + 12)) != NULL)
		{
			return NO_UNIT;
		}
	}*/
	if (vIdDataUnit == NEW_UNIT)
	{
		pDataUnit = NULL;
		pDataUnit = (DataUnit*) malloc(sizeof(DataUnit));
		//pDataUnit = new struct DataUnit;
		//memset(pDataUnit,0,sizeof(DataUnit));
		pDataUnit->WhichUnit = pDataUnit2->vUnit;
		pDataUnit->vId = pDataUnit2->vId;
		pDataUnit->vPlayer = pDataUnit2->vPlayer;//GetOwningPlayer(vUnit);
		pDataUnit->vNPlayer = pDataUnit2->vNPlayer;//GetPlayerColor(pDataUnit->vPlayer);
		pDataUnit->pPut = NULL;
		pDataUnit->vGoTo = FALSE;
		//vIdDataUnitOut = pDataUnit2->vIdDataUnit;
		//fSetUnit(vIdDataUnitOut,pDataUnit);
		//if (fIsGoTo(pDataUnit->vId) && fIsPlayerAI(pDataUnit->vNPlayer)) fCreateGoToDataUnit(vIdDataUnitOut,70000);
		if (!fIsVampire(pDataUnit->vId) &&
			!fIsMainHuman(pDataUnit->vId) &&
			!fIsSlayer(pDataUnit->vId) || pDataUnit->vNPlayer == NPLAYER_PASSIVE)
		{
			vIdDataUnitOut = fGetFreeIdDataUnit();
			fSetUnit(vIdDataUnitOut,pDataUnit);
		}
		else
		{
			if (fIsVampire(pDataUnit->vId) ||
				fIsMainHuman(pDataUnit->vId))
			{
				vIdDataUnitOut = pDataUnit->vNPlayer;
			}
			/*if (fIsMainHuman(pDataUnit->vId))
			{
				fSetSpell(&pDataUnit->vSpells[0],2,1,TRUE);
				fSetSpell(&pDataUnit->vSpells[1],1,1,TRUE);
			}*/
			if (fIsSlayer(pDataUnit->vId))
			{
				//fSetSpell(&pDataUnit->vSpells[0],2,1,TRUE);
				vIdDataUnitOut = pDataUnit->vNPlayer + 12;
			}
			fSetUnit(vIdDataUnitOut,pDataUnit);
			//if (!fIsSlayer(pDataUnit->vId) && fIsPlayerAI(pDataUnit->vNPlayer)) fCreateGoToDataUnit(vIdDataUnitOut,70000);
		}
	}
	if (fIsGoTo(pDataUnit->vId) && fIsPlayerAI(pDataUnit->vNPlayer)) fCreateGoToDataUnit(vIdDataUnitOut,70000);
	if (!pDataUnit) return NO_UNIT;
	pDataUnit->vIdDataUnit = vIdDataUnitOut;

	//sprintf(vTest,"vIdDataUnitOut = %d",vIdDataUnitOut);
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	//sprintf(vTest,"GetUnitName = %s",GetUnitName(pDataUnit->WhichUnit));
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	//sprintf(vTest,"vId = %d",vId);
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	/*if (pDataUnit->vId == ID_UNIT_SLAYER1)
	{
		pDataUnit->vId = pDataUnit->vId;
	}*/
	pDataUnit->vKeyLocalPlayer = (char*) malloc(sizeof(char)*3);
	pDataUnit->vKeyUnit = (char*) malloc(sizeof(char)*10);
	pDataUnit->vName = (char*) malloc(sizeof(char)*40);
	if (fSetParamUnit(pDataUnit) == 0)
	{
		free(pDataUnit->vKeyLocalPlayer);
		pDataUnit->vKeyLocalPlayer = NULL;
		free(pDataUnit->vKeyUnit);
		pDataUnit->vKeyUnit = NULL;
		free(pDataUnit->vName);
		pDataUnit->vName = NULL;
		free(pDataUnit);
		//delete pDataUnit;
		fSetUnit(vIdDataUnitOut,NULL);
		return NO_UNIT;
	}
	/*if (pDataUnit->vId == ID_UNIT_BUILD_WALL_BROWN)
	{
		pDataUnit->vId = pDataUnit->vId;
	}*/
	//SaveInteger(vHT_2,pDataUnit->WhichUnit,CONST_DATA_UNIT_BY_UNIT,(integer)pDataUnit);
	fFillUnitToMap(pDataUnit);

	pDataUnit->hTriggerUnitDeath = CreateTrigger();
	IncrementHandleReferences(pDataUnit->hTriggerUnitDeath);
	TriggerAddAction(pDataUnit->hTriggerUnitDeath, &fActionUnitDeath);
	TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitDeath,pDataUnit->WhichUnit,EVENT_UNIT_DEATH);

	pDataUnit->hTriggerUnitConstructFinish = CreateTrigger();
	IncrementHandleReferences(pDataUnit->hTriggerUnitConstructFinish);
	TriggerAddAction(pDataUnit->hTriggerUnitConstructFinish, &fUnitConstructFinish);

	SaveInteger(vHT,pDataUnit->hTriggerUnitConstructFinish,CONST_HUNIT_CONSTRUCT_START,(integer)pDataUnit);

	pDataUnit->hTriggerUnitResearchFinish = CreateTrigger();
	IncrementHandleReferences(pDataUnit->hTriggerUnitResearchFinish);
	TriggerAddAction(pDataUnit->hTriggerUnitResearchFinish, &fUnitResearchFinish);

	pDataUnit->hTriggerUnitResearchStart = CreateTrigger();
	IncrementHandleReferences(pDataUnit->hTriggerUnitResearchStart);
	TriggerAddAction(pDataUnit->hTriggerUnitResearchStart, &fUnitResearchStart);

	pDataUnit->hTriggerUnitAllEvents = CreateTrigger();
	IncrementHandleReferences(pDataUnit->hTriggerUnitAllEvents);
	TriggerAddAction(pDataUnit->hTriggerUnitAllEvents, &fActionUnitAllEvents);
	if (pDataUnit->vId != ID_UNIT_WORKER)
	{
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_ISSUED_POINT_ORDER);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_ISSUED_TARGET_ORDER);
	}
	//TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_DAMAGED);
	TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_ATTACKED);
	//TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_DECAY);
	if (fIsPlayerAI(pDataUnit->vNPlayer))
	{
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitResearchStart,pDataUnit->WhichUnit,EVENT_UNIT_RESEARCH_START);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitResearchFinish,pDataUnit->WhichUnit,EVENT_UNIT_RESEARCH_FINISH);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitConstructFinish,pDataUnit->WhichUnit,EVENT_UNIT_CONSTRUCT_FINISH);
		//TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_CONSTRUCT_FINISH);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_CONSTRUCT_CANCEL);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_UPGRADE_FINISH);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_UPGRADE_START);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_UPGRADE_CANCEL);
		//TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_RESEARCH_START);
		//TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_RESEARCH_FINISH);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_RESEARCH_CANCEL);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_SPELL_CAST);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_SPELL_CHANNEL);
		
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_HERO_LEVEL);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_HERO_SKILL);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_SPELL_FINISH);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_TRAIN_START);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_TRAIN_CANCEL);
		TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_TRAIN_FINISH);
	}
	TriggerRegisterUnitEvent(pDataUnit->hTriggerUnitAllEvents,pDataUnit->WhichUnit,EVENT_UNIT_PICKUP_ITEM);
	if (vIdDataUnitOut >= MAX_HUMAN_PLAYERS)
	{
		fAddToActionStackForUnits(pDataUnit);
	}
	if (vLastUnit <= vIdDataUnitOut || vLastUnit == NO_UNIT) vLastUnit = vIdDataUnitOut;
	return vIdDataUnitOut;
}

bool fIsGoTo(integer vId)
{
	if (fIsVampire(vId) ||
		fIsMainHuman(vId) ||
		vId == ID_UNIT_URSA ||
		vId == ID_UNIT_TRACKER)
	{
		return TRUE;
	}
	return FALSE;
}

bool fIsPlayerAI(HPLAYERCOLOR vNPlayer)
{
	for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
	{
		if (pPlayerUser[vvi].vNPlayer == vNPlayer)
		{
			return FALSE;
		}
	}
	/*int vvi;
	if (vCountHumans > 0)
	{
		if (vNPlayer >= 0 && vNPlayer <= 9)
		{
			for (vvi = 0; vvi < vCountHumans; vvi++)
			{
				if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == vNPlayer)
				{
					return TRUE;
				}
			}
		}
	}
	if (vCountVampires > 0)
	{
		if (vNPlayer >= 10 && vNPlayer <= 11)
		{
			for (vvi = 0; vvi < vCountVampires; vvi++)
			{
				if (pVampirePlayerGlobal[vvi].vParamPlayer.vAI == vNPlayer)
				{
					return TRUE;
				}
			}
		}
	}*/
	return TRUE;
}

void fAddToActionStackForUnits(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return;
	if (vCountHumans <= 0 && vCountVampires <= 0) return;
	HPLAYERCOLOR vNPlayer = pDataUnit->vNPlayer;
	int vvi;
	//StackForUnits *pStackForUnits;
	if (vCountHumans > 0)
	{
		if (vNPlayer >= 0 && vNPlayer <= 9)
		{
			for (vvi = 0; vvi < vCountHumans; vvi++)
			{
				if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == vNPlayer)
				{
					//pStackForUnits = (StackForUnits*) malloc(sizeof(StackForUnits));
					//pStackForUnits->pIdDataUnit = (IdDataUnit*) malloc(sizeof(IdDataUnit));
					//pStackForUnits = new struct StackForUnits;
					//pStackForUnits->pIdDataUnit = new struct IdDataUnit;
					//pStackForUnits->pIdDataUnit->vId = pDataUnit->vIdDataUnit;
					//pStackForUnits->pIdDataUnit->pDataUnit = pDataUnit;
					//pStackForUnits->vIdForComments = COMMENT_NEW_UNIT;
					//pHumanPlayerGlobal[vvi].vActionStackForUnits.fAddStackForUnits(pStackForUnits);
					pHumanPlayerGlobal[vvi].vActionStackForUnits.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (fIsMainHuman(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsMainHuman.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (fIsSlayer(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsSlayer.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (pDataUnit->vId == ID_UNIT_WORKER) pHumanPlayerGlobal[vvi].vActionStackForUnitsWorkers.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (pDataUnit->vId == ID_UNIT_URSA) pHumanPlayerGlobal[vvi].vActionStackForUnitsFurbolgs.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (fIsResearch(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsResearch.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (fIsFirstTower(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsTower.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (fIsGoTo(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsGoTo.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (pDataUnit->vId == ID_UNIT_TRACKER) pHumanPlayerGlobal[vvi].vActionStackForUnitsTracker.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					if (fIsHealTower(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsHeal.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					return;
				}
			}
		}
	}
	if (vCountVampires > 0)
	{
		if (vNPlayer >= 10 && vNPlayer <= 11)
		{
			for (vvi = 0; vvi < vCountVampires; vvi++)
			{
				if (pVampirePlayerGlobal[vvi].vParamPlayer.vAI == vNPlayer)
				{
					//pStackForUnits = (StackForUnits*) malloc(sizeof(StackForUnits));
					//pStackForUnits->pIdDataUnit = (IdDataUnit*) malloc(sizeof(IdDataUnit));
					//pStackForUnits = new struct StackForUnits;
					//pStackForUnits->pIdDataUnit = new struct IdDataUnit;
					//pStackForUnits->pIdDataUnit->vId = pDataUnit->vIdDataUnit;
					//pStackForUnits->pIdDataUnit->pDataUnit = pDataUnit;
					//pStackForUnits->vIdForComments = COMMENT_NEW_UNIT;
					//pVampirePlayerGlobal[vvi].vActionStackForUnits.fAddStackForUnits(pStackForUnits);
					pVampirePlayerGlobal[vvi].vActionStackForUnits.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT);
					return;
				}
			}
		}
	}
}

void fDelToActionStackForUnits(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return;
	if (vCountHumans <= 0 && vCountVampires <= 0) return;
	HPLAYERCOLOR vNPlayer = pDataUnit->vNPlayer;
	int vvi;
	if (vCountHumans > 0)
	{
		if (vNPlayer >= 0 && vNPlayer <= 9)
		{
			for (vvi = 0; vvi < vCountHumans; vvi++)
			{
				if (pHumanPlayerGlobal[vvi].vParamPlayer.vAI == vNPlayer)
				{
					pHumanPlayerGlobal[vvi].vActionStackForUnits.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (fIsMainHuman(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsMainHuman.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (fIsSlayer(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsSlayer.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (pDataUnit->vId == ID_UNIT_WORKER) pHumanPlayerGlobal[vvi].vActionStackForUnitsWorkers.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (pDataUnit->vId == ID_UNIT_URSA) pHumanPlayerGlobal[vvi].vActionStackForUnitsFurbolgs.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (fIsResearch(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsResearch.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (fIsFirstTower(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsTower.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (fIsGoTo(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsGoTo.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (pDataUnit->vId == ID_UNIT_TRACKER) pHumanPlayerGlobal[vvi].vActionStackForUnitsTracker.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					if (fIsHealTower(pDataUnit->vId)) pHumanPlayerGlobal[vvi].vActionStackForUnitsHeal.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					return;
				}
			}
		}
	}
	if (vCountVampires > 0)
	{
		if (vNPlayer >= 10 && vNPlayer <= 11)
		{
			for (vvi = 0; vvi < vCountVampires; vvi++)
			{
				if (pVampirePlayerGlobal[vvi].vParamPlayer.vAI == vNPlayer)
				{
					pVampirePlayerGlobal[vvi].vActionStackForUnits.fFreeStackForUnits(pDataUnit->vIdDataUnit);
					return;
				}
			}
		}
	}
}

void fFogOfWar()
{
	real vTimeGame = TimerGetElapsed(vTimerGame);
	if (vTimeGame - vTimeActionFillMapUnits <= 0.2)
	{
		return;
	}
	vTimeActionFillMapUnits = vTimeGame;
	if (vLastUnit == NO_UNIT) return;
	for (int vvj = 0; vvj < 2; vvj++)
	{
		fClearMapUnits(vvj);
	}
	try
	{
		for (unsigned short vvi = 0; vvi <= vLastUnit; vvi++)
		{
			if (vDataUnits[vvi].pDataUnit)
			{
				fFillUnitToMap(vDataUnits[vvi].pDataUnit);
			}
		}
	}
	catch (...)
	{
		return;
	}
	return;
}

integer fFillUnitToMap(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return 0;
	if (pDataUnit->vStorona < 0 || pDataUnit->vStorona > 2) return 0;
	if (pDataUnit->Steals == TRUE) return 0;
	if (pDataUnit->vDiff <= 0) return 0;
	pDataUnit->vRect1.vX1 = fConverterX(pDataUnit->LastCoordinates.X-pDataUnit->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
	pDataUnit->vRect1.vY1 = fConverterY(pDataUnit->LastCoordinates.Y+pDataUnit->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4);
	pDataUnit->vRect1.vX2 = pDataUnit->vRect1.vX1 + pDataUnit->vDiff - 1;
	pDataUnit->vRect1.vY2 = pDataUnit->vRect1.vY1 + pDataUnit->vDiff - 1;
	if (pDataUnit->vStorona == PASSIVE)
	{
		fFillRectToFillMap2(vMapUnits[VAMPIRES],&pDataUnit->vRect1,pDataUnit->vIdDataUnit);
		fFillRectToFillMap2(vMapUnits[HUMANS],&pDataUnit->vRect1,pDataUnit->vIdDataUnit);
		goto m1;
	}
	if (pDataUnit->Dead == FALSE)
	{
		fFillRectToFillMap2(vMapUnits[pDataUnit->vStorona],&pDataUnit->vRect1,pDataUnit->vIdDataUnit);
	}
	pDataUnit->VidenProtivniku = FALSE;
	if(pDataUnit->vStorona == HUMANS && IsUnitVisible(pDataUnit->WhichUnit,Player(11-1)) ||//&& !IsUnitInvisible(pDataUnit->WhichUnit,Player(1-1)) ||
	   pDataUnit->vStorona == VAMPIRES && IsUnitVisible(pDataUnit->WhichUnit,Player(1-1)))// && !IsUnitInvisible(pDataUnit->WhichUnit,Player(11-1)))
		pDataUnit->VidenProtivniku = TRUE;
	if (pDataUnit->VidenProtivniku == TRUE)
	{
		if (1)
		{
			pDataUnit->LastCoordinatesForEnemy.X = pDataUnit->LastCoordinates.X;
			pDataUnit->LastCoordinatesForEnemy.Y = pDataUnit->LastCoordinates.Y;
		}
		//if (pDataUnit->Zdanie == TRUE)
			//pDataUnit->ZdanieNaKarte = TRUE;
		if (pDataUnit->vStorona == HUMANS)
		{
			if (pDataUnit->Dead == FALSE) 
			{
				fFillRectToFillMap2(vMapUnits[VAMPIRES],&pDataUnit->vRect1,pDataUnit->vIdDataUnit);
			}
			if (pDataUnit->EnemyAdd == FALSE)
			{
				if (pDataVampires->vActionStackForUnitsEnemy.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT) == TRUE)
					pDataUnit->EnemyAdd = TRUE;
			}
		}
		if (pDataUnit->vStorona == VAMPIRES)
		{
			if (pDataUnit->Dead == FALSE) 
			{
				fFillRectToFillMap2(vMapUnits[HUMANS],&pDataUnit->vRect1,pDataUnit->vIdDataUnit);
			}
			if (pDataUnit->EnemyAdd == FALSE)
			{
				if (pDataHumans->vActionStackForUnitsEnemy.fAddStackForUnits(pDataUnit->vIdDataUnit,pDataUnit,COMMENT_NEW_UNIT) == TRUE)
					pDataUnit->EnemyAdd = TRUE;
			}
		}
	}
m1:
	return 1;
}

void fActionUnitResurrected()
{
	HUNIT whichUnit;
	whichUnit = GetTriggerUnit();
	if (whichUnit == NULL) return;
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(whichUnit,vOutIdDataUnit);
	pDataUnit = fGetDataUnit(whichUnit);
	if (pDataUnit)
	{
		if (fIsSlayer(pDataUnit->vId))
		{
			pDataUnit->Dead = FALSE;
		}
		fFreeTrigger(pDataUnit->hTriggerUnitResurrected);
	}
}

void fActionUnitDeath()
{
	sprintf(vBuf,"fActionUnitDeath() N_1\0");
	fLog(999,vBuf);
	HUNIT whichUnit;
	whichUnit = GetTriggerUnit();
	if (whichUnit == NULL) return;
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(whichUnit,vOutIdDataUnit);
	pDataUnit = fGetDataUnit(whichUnit);
	if (pDataUnit)
	{
		pDataUnit->Dead = TRUE;
		//fFillUnitToMap(pDataUnit);
		if (!fIsSlayer(pDataUnit->vId) || vWin == 1)
		{
			fDelDataUnit(pDataUnit->vIdDataUnit);
		}
		else
		{
			pDataUnit->hTriggerUnitResurrected = CreateTrigger();
			IncrementHandleReferences(pDataUnit->hTriggerUnitResurrected);
			TriggerAddAction(pDataUnit->hTriggerUnitResurrected, &fActionUnitResurrected);
			TriggerRegisterUnitStateEvent(pDataUnit->hTriggerUnitResurrected,
										  pDataUnit->WhichUnit, UNIT_STATE_LIFE,
										  GREATER_THAN_OR_EQUAL,
										  GetUnitState(pDataUnit->WhichUnit, UNIT_STATE_MAX_LIFE));
		}
	}
}

void fDelToActionStackForUnitsForEnemy(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return;
	if (pDataUnit->vStorona == HUMANS)
	{
		pDataVampires->vActionStackForUnitsEnemy.fFreeStackForUnits(pDataUnit->vIdDataUnit);
		pDataHumans->vActionStackForUnitsEnemy2.fFreeStackForUnits(pDataUnit->vIdDataUnit);
	}
	if (pDataUnit->vStorona == VAMPIRES)
	{
		pDataHumans->vActionStackForUnitsEnemy.fFreeStackForUnits(pDataUnit->vIdDataUnit);
	}
}

unsigned short fDelDataUnit2(unsigned short vIdDataUnit)
{
	sprintf(vBuf,"fDelDataUnit2(unsigned short vIdDataUnit) N_1\0");
	fLog(999,vBuf);
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS) return 0;
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	sprintf(vBuf,"fDelDataUnit2(unsigned short vIdDataUnit)  N_2\0");
	fLog(999,vBuf);
	if (!pDataUnit) return 0;
	sprintf(vBuf,"fDelDataUnit2(unsigned short vIdDataUnit)  N_3\0");
	fLog(999,vBuf);
	return fDelDataUnit2(pDataUnit);
}

bool fPreDelDataUnit2(DataUnit *pDataUnit)
{
	sprintf(vBuf,"fPreDelDataUnit2(DataUnit *pDataUnit)\0");
	fLog(999,vBuf);
	if (pDataUnit == NULL ||
		(pDataUnit &&
		(fNullUnit(pDataUnit->WhichUnit) == TRUE))) return FALSE;
	unsigned short vIdDataUnit;
	vIdDataUnit = pDataUnit->vIdDataUnit;
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS) return FALSE;
	fDelToActionStackForUnits(pDataUnit);
	fDelToActionStackForUnitsForEnemy(pDataUnit);
	if (pDataUnit->vHelpBase && pDataUnit->vPosRepair > 0)
	{
		//vMapBases.fFreePosRepair(pDataUnit->vHelpBase,pDataUnit->vPosRepair);
		vMapBases.fFreePosRepairBind(pDataUnit->vHelpBase,pDataUnit->vPosRepair);
		pDataUnit->vPosRepair = 0;
	}
	return TRUE;
}

unsigned short fDelDataUnit2(DataUnit *pDataUnit)
{
	sprintf(vBuf,"fDelDataUnit2(DataUnit *pDataUnit) N_1 pDataUnit = %hu\0",pDataUnit);
	fLog(996,vBuf);
	if (pDataUnit)
	{
		sprintf(vBuf,"fDelDataUnit2(DataUnit *pDataUnit) N_2 pDataUnit->WhichUnit = %hu\0",pDataUnit->WhichUnit);
		fLog(996,vBuf);
	}
	if (pDataUnit == NULL ||
		(pDataUnit &&
		(fNullUnit(pDataUnit->WhichUnit) == TRUE))) return 0;
	sprintf(vBuf,"pDataUnit->vName = %s\0",pDataUnit->vName);
	fLog(996,vBuf);
	unsigned short vIdDataUnit;
	vIdDataUnit = pDataUnit->vIdDataUnit;
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS) return 0;
	/*if (strlen(pDataUnit->vName) > 38)
	{
		pDataUnit->vName = pDataUnit->vName;
	}*/
	//fLog(9,pDataUnit->vName);
	/*if (fIsMainHuman(pDataUnit->vId))
	{
		pDataUnit->vId = pDataUnit->vId;
	}
	if (pDataUnit->vId == ID_UNIT_TINI)
	{
		pDataUnit->vId = pDataUnit->vId;
	}*/
	if (pDataUnit->vGoTo == TRUE)
	{
		//FlushStoredMission(vSyncCache2,pDataUnit->vKeyUnit);
		if (pDataUnit->pPut->hThreadGoTo)
		{
			TerminateThread(pDataUnit->pPut->hThreadGoTo,0);
		}
		//FlushGameCache(pDataUnit->vSyncCache);
		//if (pDataUnit->pPut->pFillMap && (unsigned int)pDataUnit->pPut->pFillMap != 0xdddddddd)
		//{
			//free(pDataUnit->pPut->pFillMap);
			delete[] pDataUnit->pPut->pFillMap;
			pDataUnit->pPut->pFillMap = NULL;
		//}
		/*try
		{
			for (int count = 0; count < pDataUnit->pPut->vMaxRect; count++)
			{
				delete []pDataUnit->pPut->vMassRect[count];
				delete []pDataUnit->pPut->vMassRect2[count];
			}
		}
		catch(...)
		{
		}*/
		//free(pDataUnit->pPut->vMassRect);
		//free(pDataUnit->pPut->vMassRect2);
		free(pDataUnit->pPut->vActionStr);
		pDataUnit->pPut->vActionStr = NULL;
		delete[] pDataUnit->pPut->vMassRect;
		delete[] pDataUnit->pPut->vMassRect2;
		pDataUnit->pPut->vMassRect2 = NULL;
		pDataUnit->pPut->vMassRect = NULL;
		if (vEndGame2 == 0)
		{
			/*if (pDataUnit->pPut->hTrigger > 0)
			{
				SaveInteger(vHT,pDataUnit->pPut->hTrigger,CONST_ACTION_GO_TO,0);
			}*/
			//if (pDataUnit->pPut->hTimer > 0)
			//{
			//	SaveInteger(vHT,pDataUnit->pPut->hTimer,CONST_ACTION_GO_TO,0);
			//	DestroyTimer(pDataUnit->pPut->hTimer);
			//}
		}
		//fFreeTrigger(pDataUnit->pPut->hTrigger);
		//pDataUnit->pPut->hTrigger = NULL;
		//pDataUnit->pPut->hTimer = NULL;
		fDelDataGoTo(pDataUnit->pPut,0);
		pDataUnit->pPut->next = NULL;
		delete pDataUnit->pPut;
		//free(pDataUnit->pPut);
	}
	pDataUnit->pPut = NULL;
	fFreeTrigger(pDataUnit->hTriggerUnitDeath);
	fFreeTrigger(pDataUnit->hTriggerUnitAllEvents);
	fFreeTrigger(pDataUnit->hTriggerUnitConstructFinish);
	fFreeTrigger(pDataUnit->hTriggerUnitResearchFinish);
	fFreeTrigger(pDataUnit->hTriggerUnitResearchStart);
	fDelToActionStackForUnits(pDataUnit);
	fDelToActionStackForUnitsForEnemy(pDataUnit);
	if (vEndGame2 == 0)
	{
		if (pDataUnit->vTimerMove > 0)
		{
			DestroyTimer(pDataUnit->vTimerMove);
			SaveInteger(vHT,pDataUnit->vTimerMove,CONST_TIMER_FOR_MOVE_UNIT,0);
		}
		if (pDataUnit->vTimerAttacked > 0)
		{
			DestroyTimer(pDataUnit->vTimerAttacked);
			SaveInteger(vHT,pDataUnit->vTimerAttacked,CONST_TIMER_FOR_ATTACKED_UNIT,0);
			SaveInteger(vHT,pDataUnit->vTimerAttacked,CONST_TIMER_FOR_ATTACKED_UNIT_2,0);
		}
		//SaveInteger(vHT_2,pDataUnit->WhichUnit,CONST_DATA_UNIT_BY_UNIT,0);
		//fLog(10,"10");
		RemoveUnit(pDataUnit->WhichUnit);
		pDataUnit->WhichUnit = NULL;
		//fLog(11,"11");
	}
	// Î÷èùàåì ñòðîåíèå ñ êàðòû áàç
	vMapBases.fFreeBuild(pDataUnit->vHead);
	if (pDataUnit->vHead)
	{
		pDataUnit->vHead = NULL;
	}
	if (pDataUnit->vHelpBase && pDataUnit->vPosRepair > 0)
	{
		//vMapBases.fFreePosRepair(pDataUnit->vHelpBase,pDataUnit->vPosRepair);
		vMapBases.fFreePosRepairBind(pDataUnit->vHelpBase,pDataUnit->vPosRepair);
		pDataUnit->vPosRepair = 0;
	}
	pDataUnit->vHelpBase = NULL;
	//fLog(13,"13");
	free(pDataUnit->vName);
	pDataUnit->vName = NULL;
	free(pDataUnit->vKeyUnit);
	pDataUnit->vKeyUnit = NULL;
	free(pDataUnit->vKeyLocalPlayer);
	pDataUnit->vKeyLocalPlayer = NULL;
	free(pDataUnit);
	//delete pDataUnit;
	pDataUnit = NULL;
	fSetUnit(vIdDataUnit,NULL);
	if (vIdDataUnit == vLastUnit)
	{
		vLastUnit = fGetLastUnit(vLastUnit);
	}
	if (vIdDataUnit < MAX_PLAYERS)
	{
		/*for (int vvi = 0; vvi <= vLastUnit; vvi++)
		{
			pDataUnit = fGetDataUnit((unsigned short)vvi);
			if (pDataUnit != NULL)
			if (pDataUnit->vNPlayer == vIdDataUnit &&
				((pDataUnit->Zdanie == FALSE &&
				pDataUnit->vNPlayer >= 0 &&
				pDataUnit->vNPlayer <= 9) ||
				pDataUnit->vNPlayer >= 10 &&
				pDataUnit->vNPlayer <= 11))
			{
				//fDelDataUnit((unsigned short)vvi);
			}
		}*/
		for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			if (pPlayerUser[vvi].vLeave == FALSE &&
				pPlayerUser[vvi].vDead == FALSE &&
				pPlayerUser[vvi].vNPlayer == vIdDataUnit)
			{
				pPlayerUser[vvi].vDead = TRUE;
				break;
			}
		}
		if (vStartCommands == 1 && TimerGetElapsed(vTimerGame) > 60 * 1)
		{
			fCheckWin();
		}
	}
	//fLog(14,"14");
	return 1;
}

void fCheckWin()
{
	int vvi;
	int vCountHumansTemp;
	int vCountVampiresTemp;
	vCountHumansTemp = 0;
	vCountVampiresTemp = 0;
	for (vvi = 0; vvi <= 9; vvi++)
	{
		if (vDataUnits[vvi].pDataUnit != NULL)
		{
			vCountHumansTemp++;
		}
	}
	for (vvi = 10; vvi <= 11; vvi++)
	{
		if (vDataUnits[vvi].pDataUnit != NULL)
		{
			vCountVampiresTemp++;
		}
	}
	if (vCountHumansTemp == 0 && vCountVampiresTemp > 0)
	{
		fWin(VAMPIRES);
		return;
	}
	if (vCountHumansTemp > 0 && vCountVampiresTemp == 0)
	{
		fWin(HUMANS);
		return;
	}
}

unsigned short fGetLastUnit(unsigned short vLastUnit)
{
	unsigned short vLastUnitNew;
	vLastUnitNew = vLastUnit;
	while (vLastUnitNew >= 0)
	{
		if (fGetDataUnit(vLastUnitNew) == NULL)
		{
			if (vLastUnitNew > 0)
			{
				vLastUnitNew--;
			}
			else
			{
				return NO_UNIT;
			}
		}
		else
		{
			return vLastUnitNew;
		}
	}
	return NO_UNIT;
}

integer fCheckGoDataUnit(unsigned short vIdDataUnit)
{
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS)
	{
		return -1;
	}
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (!pDataUnit)
	{
		return -1;
	}
	if (pDataUnit->pPut)
	{
		/*if (fIsRectToRect(&pDataUnit->pPut->vRect4,&pDataUnit->pPut->vRect5) == TRUE)
		{
			return 0;
		}*/
		if (pDataUnit->pPut->IdetPoPuti == TRUE)
		{
			return 1;
		}
		if (pDataUnit->pPut->vCelNeDostignuta == TRUE)
		{
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "666" );
			return -1;
		}
	}
	/*if (pDataUnit->pTempPut)
	{
		if (pDataUnit->pTempPut->IdetPoPuti == TRUE)
		{
			return 1;
		}
	}*/
	return 0;
}

unsigned int fGetDiffUnit (unit vUnit)
{
    return (unsigned int)fRadiusToDiff(fRadius_Unit(GetUnitTypeId(vUnit)));
}

unsigned short fRadiusToDiff (real r)
{
    return (unsigned short)(r/DOOD_MAP_SUBDIV*4);
}

unsigned short fGetCellInFillMap (unsigned short *pFillMap, unsigned short x, unsigned short y)
{
	if (x < 0 || y < 0 || x >= vW3 || y >= vH3)
	{
		return JASS_UNKNOWN;
	}
	return pFillMap[y*vW3+x];
}

integer fSetCellInFillMap (unsigned short *pFillMap, unsigned short x, unsigned short y, unsigned short vInt)
{
	if (x < 0 || y < 0 || x >= vW3 || y >= vH3)
	{
		return 0;
	}
	pFillMap[y*vW3+x] = vInt;
	return 1;
}

integer fFillRectToFillMap (unsigned short* pFillMap, sRect2* vRect, unsigned short vIntInFill)
{
	for (vRect->vvj2 = vRect->vY1; vRect->vvj2 <= vRect->vY2; vRect->vvj2++)
	{
        for (vRect->vvi2 = vRect->vX1; vRect->vvi2 <= vRect->vX2; vRect->vvi2++)
		{
			vRect->vInt = fGetCellInFillMap(pFillMap,vRect->vvi2,vRect->vvj2);
			if (vRect->vInt == JASS_M2) return 2;
            if (vRect->vInt < JASS_M1)
			{
                if (fSetCellInFillMap(pFillMap,vRect->vvi2,vRect->vvj2,vIntInFill) == 0)
				{
					return 0;
				}
			}
		}
	}
    return 1;
}

integer fCheckRectToI (sRect2* vRect, unsigned int vIntIn)
{
    vRect->vX = fConvertIToX(vIntIn);
    vRect->vY = fConvertIToY(vIntIn);
	if (vRect->vX1 == vRect->vX && vRect->vY1 == vRect->vY) return 1;
	/*
    for (vRect->vvj2 = vRect->vY1; vRect->vvj2 <= vRect->vY2; vRect->vvj2++)
	{
        for (vRect->vvi2 = vRect->vX1; vRect->vvi2 <= vRect->vX2; vRect->vvi2++)
		{
            if (vRect->vvi2 == vRect->vX && vRect->vvj2 == vRect->vY)
			{
                return 1;
			}
		}
	}*/
    return 0;
}

integer fFillMToI(DataGoTo *pDataGoTo, unsigned int vIntIn)
{
	/*if (pDataGoTo->vDiff == 1)
	{
		pDataGoTo->vDiff = pDataGoTo->vDiff;
	}*/
	if (pDataGoTo->pFillMap == NULL) return 0;
	pDataGoTo->vRect1.vX1 = fConverterX(pDataGoTo->vX_Unit-pDataGoTo->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
    pDataGoTo->vRect1.vY1 = fConverterY(pDataGoTo->vY_Unit+pDataGoTo->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4);
    pDataGoTo->vRect1.vX2 = pDataGoTo->vRect1.vX1 + pDataGoTo->vDiff - 1;
    pDataGoTo->vRect1.vY2 = pDataGoTo->vRect1.vY1 + pDataGoTo->vDiff - 1;
	if (fFillRectToFillMap2(pDataGoTo->pFillMap,&pDataGoTo->vRect1,JASS_M1) == 0)
	{
        return 0;
	}
	//sprintf(vTest,"123");
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	DataUnit *pDataUnit = fGetDataUnit(pDataGoTo,vIntIn);
	//sprintf(vTest,"pDataUnit = %d",(int)pDataUnit);
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	if (pDataUnit && pDataUnit->WhichUnit != pDataGoTo->vUnit)
	{
		//sprintf(vTest,"pDataUnit->vIdDataUnit = %d",(int)pDataUnit->vIdDataUnit);
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
		pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
		pDataUnit->vRect1.vX1 = fConverterX(pDataUnit->LastCoordinates.X-pDataUnit->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
		pDataUnit->vRect1.vY1 = fConverterY(pDataUnit->LastCoordinates.Y+pDataUnit->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4);
		pDataUnit->vRect1.vX2 = pDataUnit->vRect1.vX1 + pDataUnit->vDiff - 1;
		pDataUnit->vRect1.vY2 = pDataUnit->vRect1.vY1 + pDataUnit->vDiff - 1;
		/*sprintf(vTest,"pDataUnit->vX1 = %hu",pDataUnit->vX1);
		DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		sprintf(vTest,"pDataUnit->vY1 = %hu",pDataUnit->vY1);
		DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		sprintf(vTest,"pDataUnit->vX2 = %hu",pDataUnit->vX2);
		DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		sprintf(vTest,"pDataUnit->vY2 = %hu",pDataUnit->vY2);
		DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );*/
		if (fFillRectToFillMap2(pDataGoTo->pFillMap,&pDataUnit->vRect1,JASS_M2) == 0)
		{
			return 0;
		}
	}

	pDataGoTo->vRect3.vX1 = fConvertIToX(vIntIn);
    pDataGoTo->vRect3.vY1 = fConvertIToY(vIntIn);
    pDataGoTo->vRect3.vX2 = pDataGoTo->vRect3.vX1 + pDataGoTo->vDiff - 1;
    pDataGoTo->vRect3.vY2 = pDataGoTo->vRect3.vY1 + pDataGoTo->vDiff - 1;
	if (fCheckRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect3) == 0)
	{
		return 0;
	}

	pDataGoTo->i3 = 0;
	/*pDataGoTo->vMaxRect = 0;
	if (pDataGoTo->vPriority == TRUE)
	{
		pDataGoTo->vMaxRect = vSizeMap;
	}
	else
	{
		pDataGoTo->vMaxRect = 50000;
	}*/
	if (pDataGoTo->vMaxRect == 0)
	{
		return 0;
	}
	//pDataGoTo->vMassRect = new sRect*[pDataGoTo->vMaxRect];
	//pDataGoTo->vMassRect2 = new unsigned int*[pDataGoTo->vMaxRect];
	//pDataGoTo->vMassRect[0] = new sRect[1]; 
	pDataGoTo->vMassRect[0].vX1 = pDataGoTo->vRect1.vX1;
	pDataGoTo->vMassRect[0].vY1 = pDataGoTo->vRect1.vY1;
	pDataGoTo->vMassRect[0].vX2 = pDataGoTo->vRect1.vX2;
	pDataGoTo->vMassRect[0].vY2 = pDataGoTo->vRect1.vY2;
	//pDataGoTo->vMassRect2[0] = new unsigned int[1]; 
	pDataGoTo->vMassRect2[0] = 0;

    pDataGoTo->vN_Rect = 0;
    pDataGoTo->vN_Rect_2 = 0;
    pDataGoTo->vN_Rect_3 = 1;
	for (pDataGoTo->vvi = 1; pDataGoTo->vvi < vSizeMap; pDataGoTo->vvi++)
	{
		if (pDataGoTo->vMaxR < pDataGoTo->vvi)
		{
			pDataGoTo->vResult = 0;
			goto m1;
		}
		pDataGoTo->vN_Rect = pDataGoTo->vN_Rect_2;//0//1
        pDataGoTo->vN_Rect_2 = pDataGoTo->vN_Rect_3;//1//5
		for (pDataGoTo->vvj = pDataGoTo->vN_Rect; pDataGoTo->vvj < pDataGoTo->vN_Rect_2; pDataGoTo->vvj++)
		{
			/*if ((unsigned int)&(pDataGoTo->vMassRect[pDataGoTo->vvj]) == 0xcdcdcdcd)
			{
				goto m1;
			}*/
			/*if (pDataGoTo->vMassRect == NULL ||
				(unsigned int)pDataGoTo->vMassRect == 0xcdcdcdcd ||
				(unsigned int)pDataGoTo->vMassRect == 0xdddddddd ||
				pDataGoTo->vvj >= pDataGoTo->vMaxRect)
			{
				goto m1;
			}
			if ((unsigned int)pDataGoTo->vMassRect[pDataGoTo->vvj] == 0xdddddddd ||
				(unsigned int)pDataGoTo->vMassRect[pDataGoTo->vvj] == 0xcdcdcdcd)
			{
				delete pDataGoTo->vMassRect;
				delete pDataGoTo->vMassRect2;
				return 0;
			}*/
			//try
			//{
			if (pDataGoTo->vvj >= pDataGoTo->vMaxRect)
			{
				return 0;
			}
			pDataGoTo->vRect1.vX1 = pDataGoTo->vMassRect[pDataGoTo->vvj].vX1;
			pDataGoTo->vRect1.vY1 = pDataGoTo->vMassRect[pDataGoTo->vvj].vY1;
			pDataGoTo->vRect1.vX2 = pDataGoTo->vMassRect[pDataGoTo->vvj].vX2;
			pDataGoTo->vRect1.vY2 = pDataGoTo->vMassRect[pDataGoTo->vvj].vY2;
			//}
			//catch(...)
			//{
			//	goto m1;
			//}
			if (fCheckRectToI(&pDataGoTo->vRect1,vIntIn) == 1 &&
				fCheckRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect1) == 1)
			{
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "333+" );
				//sprintf(vTest,"pDataGoTo->vvi = %u",pDataGoTo->vvi);
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				for (pDataGoTo->vvz = 0; pDataGoTo->vvz < vSizeMap; pDataGoTo->vvz++)
				{
					pDataGoTo->pFillMap[pDataGoTo->vvz] = 0;
				}
                pDataGoTo->i2 = pDataGoTo->vvj;
				//sprintf(vTest,"pDataGoTo->vvj = %u",pDataGoTo->vvj);
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
                //pDataGoTo->i2 = pDataGoTo->i3 + 1;
				//sprintf(vTest,"pDataGoTo->i3 = %u",pDataGoTo->i3);
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//pDataGoTo->vR3 = pDataGoTo->vvi;
				pDataGoTo->vIntFill = JASS_M1;
				//vFlagTest = 0;
				//int vCount=0;
                for (pDataGoTo->vvz = pDataGoTo->vN_Rect_3 - 1; pDataGoTo->vvz >= 0; pDataGoTo->vvz--)
				{
					if (pDataGoTo->i2 == pDataGoTo->vvz &&
						pDataGoTo->vvz != -1)
					{
						//if (pDataGoTo->i2 != TERRA_UNIT)
						//{
							
						//}
						
						pDataGoTo->vRect1.vX1 = pDataGoTo->vMassRect[pDataGoTo->vvz].vX1;
						pDataGoTo->vRect1.vY1 = pDataGoTo->vMassRect[pDataGoTo->vvz].vY1;
						if (pDataGoTo->vIntFill != JASS_M1)
						{
							pDataGoTo->vRect1.vX2 = pDataGoTo->vMassRect[pDataGoTo->vvz].vX2;
							pDataGoTo->vRect1.vY2 = pDataGoTo->vMassRect[pDataGoTo->vvz].vY2;
						}
						else
						{
							pDataGoTo->vRect1.vX2 = pDataGoTo->vRect1.vX1;
							pDataGoTo->vRect1.vY2 = pDataGoTo->vRect1.vY1;
						}
						//pDataGoTo->vPerevod = JASS_M1+pDataGoTo->vvi-pDataGoTo->vR3;
						/*if (vFlagTest < 5)
						{
							sprintf(vTest,"pDataGoTo->vX1 = %hu",pDataGoTo->vX1);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"pDataGoTo->vY1 = %hu",pDataGoTo->vY1);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"pDataGoTo->i2 = %u",pDataGoTo->i2);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"pDataGoTo->vMassRect2[pDataGoTo->vvz][0] = %u",pDataGoTo->vMassRect2[pDataGoTo->vvz][0]);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							vFlagTest++;
						}*/
						
						if (fFillRectToFillMap2(pDataGoTo->pFillMap,&pDataGoTo->vRect1,pDataGoTo->vIntFill) == 0)//(&pDataGoTo->vPerevod)[0]) == 0)
						{
							//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "111" );
							pDataGoTo->vResult = 0;
							goto m1;
						}
						/*if (pDataGoTo->vIntFill-JASS_M1 > 320)
						{
						vTextTag2[pDataGoTo->vIntFill-JASS_M1] = CreateTextTag();
						SetTextTagText(vTextTag2[pDataGoTo->vIntFill-JASS_M1], "P", 0.023);
						SetTextTagPos(vTextTag2[pDataGoTo->vIntFill-JASS_M1], fGetMinIX2RX(pDataGoTo->vX1), fGetMinIY2RY(pDataGoTo->vY1), 0);
						SetTextTagColor(vTextTag2[pDataGoTo->vIntFill-JASS_M1], 255, 255, 255, 255);
						vTextTag2[pDataGoTo->vIntFill-JASS_M1] = CreateTextTag();
						SetTextTagText(vTextTag2[pDataGoTo->vIntFill-JASS_M1], "H", 0.023);
						SetTextTagPos(vTextTag2[pDataGoTo->vIntFill-JASS_M1], fGetMaxIX2RX(pDataGoTo->vX2), fGetMaxIY2RY(pDataGoTo->vY2), 0);
						SetTextTagColor(vTextTag2[pDataGoTo->vIntFill-JASS_M1], 255, 255, 255, 255);
						}*/
						/*if (vFlagTest < 5)
						{
							sprintf(vTest,"pDataGoTo->vIntFill-JASS_M1 = %u",pDataGoTo->vIntFill-JASS_M1);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"fGetMinIX2RX(pDataGoTo->vX1) = %f",fGetMinIX2RX(pDataGoTo->vX1));
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"fGetMinIY2RY(pDataGoTo->vY1) = %f",fGetMinIY2RY(pDataGoTo->vY1));
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							vFlagTest++;
						}
						if (vFlagTest >= 5)
						{
							pDataGoTo->vResult = 0;
							goto m1;
						}*/

						pDataGoTo->vIntFill++;
						pDataGoTo->i2 = pDataGoTo->vMassRect2[pDataGoTo->vvz];
						//pDataGoTo->vR3--;
					}
				}
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "555+" );
				pDataGoTo->vResult = 1;
				goto m1;
			}
			for (pDataGoTo->vvh = 0; pDataGoTo->vvh < 8; pDataGoTo->vvh++)
			{
				switch(pDataGoTo->vvh)
				{
					case 0:
						pDataGoTo->vRect3_2.vX1 = -1; pDataGoTo->vRect3_2.vY1 = 0; pDataGoTo->vRect3_2.vX2 = -1; pDataGoTo->vRect3_2.vY2 = 0;//L
						break;
					case 1:
						pDataGoTo->vRect3_2.vX1 = 0; pDataGoTo->vRect3_2.vY1 = -1; pDataGoTo->vRect3_2.vX2 = 0; pDataGoTo->vRect3_2.vY2 = -1;//T
						break;
					case 2:
						pDataGoTo->vRect3_2.vX1 = 1; pDataGoTo->vRect3_2.vY1 = 0; pDataGoTo->vRect3_2.vX2 = 1; pDataGoTo->vRect3_2.vY2 = 0;//R
						break;
					case 3:
						pDataGoTo->vRect3_2.vX1 = 0; pDataGoTo->vRect3_2.vY1 = 1; pDataGoTo->vRect3_2.vX2 = 0; pDataGoTo->vRect3_2.vY2 = 1;//B
						break;
					case 4:
						pDataGoTo->vRect3_2.vX1 = -1; pDataGoTo->vRect3_2.vY1 = -1; pDataGoTo->vRect3_2.vX2 = -1; pDataGoTo->vRect3_2.vY2 = -1;//TL
						break;
					case 5:
						pDataGoTo->vRect3_2.vX1 = 1; pDataGoTo->vRect3_2.vY1 = -1; pDataGoTo->vRect3_2.vX2 = 1; pDataGoTo->vRect3_2.vY2 = -1;//TR
						break;
					case 6:
						pDataGoTo->vRect3_2.vX1 = -1; pDataGoTo->vRect3_2.vY1 = 1; pDataGoTo->vRect3_2.vX2 = -1; pDataGoTo->vRect3_2.vY2 = 1;//BL
						break;
					case 7:
						pDataGoTo->vRect3_2.vX1 = 1; pDataGoTo->vRect3_2.vY1 = 1; pDataGoTo->vRect3_2.vX2 = 1; pDataGoTo->vRect3_2.vY2 = 1;//BR
						break;
					default:
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "222" );
						pDataGoTo->vResult = 0;
						goto m1;
				}
				pDataGoTo->vRect3_3.vX1 = (short)pDataGoTo->vRect1.vX1 + pDataGoTo->vRect3_2.vX1;
				pDataGoTo->vRect3_3.vY1 = (short)pDataGoTo->vRect1.vY1 + pDataGoTo->vRect3_2.vY1;
				pDataGoTo->vRect3_3.vX2 = (short)pDataGoTo->vRect1.vX2 + pDataGoTo->vRect3_2.vX2;
				pDataGoTo->vRect3_3.vY2 = (short)pDataGoTo->vRect1.vY2 + pDataGoTo->vRect3_2.vY2;

				if (pDataGoTo->vRect3_3.vX1 < 0 ||
					pDataGoTo->vRect3_3.vY1 < 0 ||
					pDataGoTo->vRect3_3.vX2 < 0 ||
					pDataGoTo->vRect3_3.vY2 < 0)
				{
					goto m2;
				}
				pDataGoTo->vRect2.vX1 = (unsigned short)pDataGoTo->vRect3_3.vX1;
				pDataGoTo->vRect2.vY1 = (unsigned short)pDataGoTo->vRect3_3.vY1;
				pDataGoTo->vRect2.vX2 = (unsigned short)pDataGoTo->vRect3_3.vX2;
				pDataGoTo->vRect2.vY2 = (unsigned short)pDataGoTo->vRect3_3.vY2;
				//sprintf(vTest,"pDataGoTo->vX1_2 = %hu",pDataGoTo->vX1_2);
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//sprintf(vTest,"pDataGoTo->vY1_2 = %hu",pDataGoTo->vY1_2);
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				if ((pDataGoTo->Fly == TRUE ||
					(pDataGoTo->Fly == FALSE && fCheckRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect2) == 1)) &&
					fCheckRectToIFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect2,JASS_M1+pDataGoTo->vvi) == 1)
				{
					pDataGoTo->vResultFill = fFillRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect2,JASS_M1+pDataGoTo->vvi);
					if (pDataGoTo->vResultFill == 1 || pDataGoTo->vResultFill == 2)
					{
						/*if (pDataGoTo->vMassRect == NULL ||
							(unsigned int)pDataGoTo->vMassRect == 0xcdcdcdcd ||
							(unsigned int)pDataGoTo->vMassRect == 0xdddddddd ||
							pDataGoTo->vMassRect2 == NULL ||
							(unsigned int)pDataGoTo->vMassRect2 == 0xcdcdcdcd ||
							(unsigned int)pDataGoTo->vMassRect2 == 0xdddddddd ||
							//(unsigned int)pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3] == 0xdddddddd ||
							//(unsigned int)pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3] == 0xcdcdcdcd ||
							pDataGoTo->vN_Rect_3 >= pDataGoTo->vMaxRect)
						{
							goto m1;
						}*/
						//sprintf(vTest,"pDataGoTo->Fly = %d",pDataGoTo->Fly);
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						//try
						//{
						//pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3] = new sRect[1];
						if (pDataGoTo->vN_Rect_3 >= pDataGoTo->vMaxRect)
						{
							return 0;
						}
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vX1 = pDataGoTo->vRect2.vX1;
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vY1 = pDataGoTo->vRect2.vY1;
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vX2 = pDataGoTo->vRect2.vX2;
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vY2 = pDataGoTo->vRect2.vY2;
						//pDataGoTo->vMassRect2[pDataGoTo->vN_Rect_3] = new unsigned int[1];
						pDataGoTo->vMassRect2[pDataGoTo->vN_Rect_3] = pDataGoTo->vvj;
						//}
						//catch(...)
						//{
						//	goto m1;
						//}
						if (pDataGoTo->vResultFill == 2 && pDataGoTo->i3 == 0)
						{
							pDataGoTo->i3 = pDataGoTo->vvj;
						}
						if (pDataGoTo->vResultFill == 1 && pDataGoTo->i3 == 0)
						{
							pDataGoTo->i2 = pDataGoTo->vvj + 1;
						}
						pDataGoTo->vN_Rect_3++;
						if (pDataGoTo->vN_Rect_3 >= pDataGoTo->vMaxRect)
						{
							goto m1;
						}
					}
				}
m2:;
			}
		}
		/*if (0)//pDataGoTo->vvi >= LIMIT_MOVE)
		{
			break;
		}*/
	}
m1:
	/*try
	{
	for (int count = 0; count < pDataGoTo->vN_Rect_3; count++)
	{
        delete []pDataGoTo->vMassRect[count];
		delete []pDataGoTo->vMassRect2[count];
	}
	}
	catch(...)
	{
	}
	delete pDataGoTo->vMassRect;
	delete pDataGoTo->vMassRect2;*/
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "444+" );
	return 1;
}

integer fFillRectToFillMap2 (unsigned short* pFillMap, sRect2* vRect, unsigned short vIntInFill)
{
    for (vRect->vvj2 = vRect->vY1; vRect->vvj2 <= vRect->vY2; vRect->vvj2++)
	{
		for (vRect->vvi2 = vRect->vX1; vRect->vvi2 <= vRect->vX2; vRect->vvi2++)
		{
            if (fSetCellInFillMap(pFillMap,vRect->vvi2,vRect->vvj2,vIntInFill) == 0)
			{
                return 0;
			}
		}
	}
    return 1;
}

integer fCheckRectToIFillMap (unsigned short* pFillMap, sRect2* vRect, unsigned short vIntInFill)
{
    vRect->vCount2=0;
	for (vRect->vvj2 = vRect->vY1; vRect->vvj2 <= vRect->vY2; vRect->vvj2++)
	{
		for (vRect->vvi2 = vRect->vX1; vRect->vvi2 <= vRect->vX2; vRect->vvi2++)
		{
            vRect->vInt = fGetCellInFillMap(pFillMap,vRect->vvi2,vRect->vvj2);
            if (vRect->vInt >= JASS_M1 && vRect->vInt <= vIntInFill)
			{
                vRect->vCount2++;
			}
		}
	}
    if (vRect->vCount2 != (vRect->vX2 - vRect->vX1 + 1) * (vRect->vY2 - vRect->vY1 + 1))
	{
        return 1;
	}
    return 0;
}

integer fCheckPath (unsigned short vIntIn)
{
    if (vIntIn == JASS_DOOD_DEFAULT_TREE || vIntIn == JASS_PATH_NOWALK || vIntIn == JASS_UNIT)
	{
        return 1;
	}
    return 0;
}

integer fCheckRectToFillMap (unsigned short* pFillMap, sRect2* vRect)
{
    for (vRect->vvj2 = vRect->vY1; vRect->vvj2 <= vRect->vY2; vRect->vvj2++)
	{
		for (vRect->vvi2 = vRect->vX1; vRect->vvi2 <= vRect->vX2; vRect->vvi2++)
		{
			vRect->vInt = fGetCellInFillMap(pFillMap,vRect->vvi2,vRect->vvj2);
			if (vRect->vInt == JASS_M2 || vRect->vInt == JASS_M1)
			{
				continue;
			}
            if (fCheckPath(vRect->vInt) == 1)
			{
				return 0;
			}
		}
	}
	return 1;
}

void fAddDataGoTo(DataGoTo *pDataGoTo)
{
	pDataGoTo->next = 0;
	if (pLastDataGoTo != 0)
	{
		if (pLastDataGoTo->next == 0)
		{
			pLastDataGoTo->next = pDataGoTo;
			pLastDataGoTo = pDataGoTo;
			pLastDataGoTo->next = 0;
		}
		else
		{
			return;
		}
	}
	else
	{
		pLastDataGoTo = pDataGoTo;
		pFirstDataGoTo = pDataGoTo;
	}
}

void fDelDataGoTo(DataGoTo *pDataGoTo, integer vFlag)
{
	DataGoTo *current = pFirstDataGoTo;
	DataGoTo *next=0;
	DataGoTo *forerunner=0;
	while (current && (unsigned int)current != 0xdddddddd)
	{
		if (current == pDataGoTo)
		{
			if (current == pFirstDataGoTo)
			{
				pFirstDataGoTo = NULL;
			}
			next = current->next;
			if (vFlag == 1)
			{
				pDataGoTo->next = NULL;
				//free(pDataGoTo);
				delete[] pDataGoTo->pFillMap;
				pDataGoTo->pFillMap = NULL;
				delete[] pDataGoTo->vMassRect;
				pDataGoTo->vMassRect = NULL;
				delete[] pDataGoTo->vMassRect2;
				pDataGoTo->vMassRect2 = NULL;
				delete pDataGoTo;
			}
			if (next != 0)// Åñëè ñëåäóþùèé íå íîëü
			{
				if (forerunner == 0)// Åñëè ïîñëåäíèé íîëü
				{
					//pLastDataGoTo = 0;
					//pFirstDataGoTo = 0;
					pFirstDataGoTo = next;
					break;
				}
				else
				{
					forerunner->next = next;
					break;
				}
			}
			else// Åñëè ñëåäóþùèé íîëü
			{
				if (forerunner != 0)// Åñëè ïîñëåäíèé íå íîëü
				{
					pLastDataGoTo = forerunner;
					forerunner->next = NULL;
					break;
				}
				else
				{
					pLastDataGoTo = 0;
					break;
				}
			}
		}
		else
		{
			if (current && (unsigned int)current != 0xdddddddd)
			{
				forerunner = current;
				current = current->next;
			}
		}
	}
}

void fFreeDataGoTo()
{
	DataGoTo *current = pFirstDataGoTo;
	DataGoTo *next=0;
	while (current && (unsigned int)current != 0xdddddddd)
	{
		next = current->next;
		//if ((unsigned int)next  != 0xdddddddd)
		if (1)
		{
			if (current->pFillMap && (unsigned int)current->pFillMap != 0xdddddddd)
			{
				//free(current->pFillMap);
				delete[] current->pFillMap;
				current->pFillMap = NULL;
			}
			//free(current->vMassRect);
			//free(current->vMassRect2);
			free(current->vActionStr);
			current->vActionStr = NULL;
			delete[] current->vMassRect;
			current->vMassRect = NULL;
			delete[] current->vMassRect2;
			current->vMassRect2 = NULL;
			//SaveInteger(vHT,current->hTrigger,CONST_ACTION_GO_TO,0);
			//fFreeTrigger(current->hTrigger);
			//current->hTrigger = NULL;
			//SaveInteger(vHT,current->hTimer,CONST_ACTION_GO_TO,0);
			//if (current->hTimer > 0)
			//{
			//	DestroyTimer(current->hTimer);
			//}
			//current->hTimer = NULL;
			current->next = NULL;
			delete current;
			//free(current);
		}
		current = next;
	}
	pFirstDataGoTo = 0;
	pLastDataGoTo = 0;
	if (pDataVampires->pPutGlobal && (unsigned int)pDataVampires->pPutGlobal != 0xdddddddd && (unsigned int)pDataVampires->pPutGlobal != 0xcdcdcdcd)
	{
		//if (pDataVampires->pPutGlobal->pFillMap && (unsigned int)pDataVampires->pPutGlobal->pFillMap != 0xdddddddd)
		//{
			delete[] pDataVampires->pPutGlobal->pFillMap;
			//free(pDataVampires->pPutGlobal->pFillMap);
			pDataVampires->pPutGlobal->pFillMap = NULL;
		//}
		/*try
		{
			for (int count = 0; count < pDataVampires->pPutGlobal->vMaxRect; count++)
			{
				delete []pDataVampires->pPutGlobal->vMassRect[count];
				delete []pDataVampires->pPutGlobal->vMassRect2[count];
			}
		}
		catch(...)
		{
		}*/
		//free(pDataVampires->pPutGlobal->vMassRect);
		//free(pDataVampires->pPutGlobal->vMassRect2);
		free(pDataVampires->pPutGlobal->vActionStr);
		pDataVampires->pPutGlobal->vActionStr = NULL;
		delete[] pDataVampires->pPutGlobal->vMassRect;
		pDataVampires->pPutGlobal->vMassRect = NULL;
		delete[] pDataVampires->pPutGlobal->vMassRect2;
		pDataVampires->pPutGlobal->vMassRect2 = NULL;
		//SaveInteger(vHT,pDataVampires->pPutGlobal->hTrigger,CONST_ACTION_GO_TO,0);
		//fFreeTrigger(pDataVampires->pPutGlobal->hTrigger);
		//pDataVampires->pPutGlobal->hTrigger = NULL;
		//SaveInteger(vHT,pDataVampires->pPutGlobal->hTimer,CONST_ACTION_GO_TO,0);
		//if (pDataVampires->pPutGlobal->hTimer > 0)
		//{
		//	DestroyTimer(pDataVampires->pPutGlobal->hTimer);
		//}
		//pDataVampires->pPutGlobal->hTimer = NULL;
		pDataVampires->pPutGlobal->next = NULL;
		//free(pDataVampires->pPutGlobal);
		delete pDataVampires->pPutGlobal;
		pDataVampires->pPutGlobal = NULL;
	}
}

//DataGoTo *fGetDataGoToBase(HTRIGGER hTrigger)
//DataGoTo *fGetDataGoToBase(HTIMER hTimer)
//{
//	DataGoTo *current = pFirstDataGoTo;
//	while (current && (unsigned int)current != 0xdddddddd)
//	{
		/*if ((unsigned int)current->hTrigger != 0xdddddddd && current->hTrigger == hTrigger)
		{
			return current;
		}*/
//		if ((unsigned int)current->hTimer != 0xdddddddd && current->hTimer == hTimer)
//		{
//			return current;
//		}
//		if (current == current->next) break;
//		current = current->next;
//	}
//	return NULL;
//}

//DataGoTo *fGetDataGoToBaseTimer(HTIMER vTimer)
//{
//	DataGoTo *current = pFirstDataGoTo;
//	while (current > 0 && (unsigned int)current != 0xdddddddd)
//	{
//		if (current->vTimer == vTimer)
//		{
//			return current;
//		}
//		if (current == current->next) break;
//		current = current->next;
//	}
//	return NULL;
//}

DataUnit * fGetDataUnit(DataGoTo *pDataGoTo, unsigned int vInt3)
{
	//sprintf(vTest,"vMapUnits[pDataGoTo->vStorona][vInt3] = %d",(integer)vMapUnits[pDataGoTo->vStorona][vInt3]);
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	
	return fGetDataUnit(vMapUnits[pDataGoTo->vStorona][vInt3]);
}

DWORD WINAPI ThreadGoTo(void *p)
{
	if (!p) return 0;
	DataGoTo *pDataGoTo;
	pDataGoTo = (DataGoTo*)p;
	pDataGoTo->vStartThread = TRUE;
	if (fFillMap(vMapGlobal, pDataGoTo->pFillMap) == 0)
	{
		goto m1;
	}
	if (pDataGoTo->vPriority == FALSE && fFillMapUnits(pDataGoTo) == 0)
	{
		goto m1;
	}
	if (pDataGoTo->vAction == ACTION_GO)
	{
		if (fFillMToI(pDataGoTo,pDataGoTo->vInt3) == 0)
		{
			goto m1;
		}
	}
	if (pDataGoTo->vAction == ACTION_BLINK)
	{
		if (fFillBlinkToI(pDataGoTo,pDataGoTo->vInt3) == 0)
		{
			goto m1;
		}
	}
	pDataGoTo->vStartThread = FALSE;
	pDataGoTo->hThreadGoTo = NULL;
	return 0;
m1:
	/*DataUnit *pDataUnit = fGetDataUnit(pDataGoTo->vIdDataUnit);
	if (pDataUnit)
	{
		fFinishActionUnit(pDataUnit->vIdDataUnit);
	}*/
	pDataGoTo->vStartThread = FALSE;
	pDataGoTo->hThreadGoTo = NULL;
    return 0;
}

integer fFillBlinkToI(DataGoTo *pDataGoTo, unsigned int vIntIn)
{
	pDataGoTo->vRect1.vX1 = fConverterX(pDataGoTo->vX_Unit-pDataGoTo->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
    pDataGoTo->vRect1.vY1 = fConverterY(pDataGoTo->vY_Unit+pDataGoTo->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4);
    pDataGoTo->vRect1.vX2 = pDataGoTo->vRect1.vX1 + pDataGoTo->vDiff - 1;
    pDataGoTo->vRect1.vY2 = pDataGoTo->vRect1.vY1 + pDataGoTo->vDiff - 1;
	if (fFillRectToFillMap2(pDataGoTo->pFillMap,&pDataGoTo->vRect1,JASS_M1) == 0)
	{
        return 0;
	}

	DataUnit *pDataUnit = fGetDataUnit(pDataGoTo,vIntIn);
	
	if (pDataUnit && pDataUnit->WhichUnit != pDataGoTo->vUnit)
	{
		pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
		pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
		pDataUnit->vRect1.vX1 = fConverterX(pDataUnit->LastCoordinates.X-pDataUnit->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
		pDataUnit->vRect1.vY1 = fConverterY(pDataUnit->LastCoordinates.Y+pDataUnit->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4);
		pDataUnit->vRect1.vX2 = pDataUnit->vRect1.vX1 + pDataUnit->vDiff - 1;
		pDataUnit->vRect1.vY2 = pDataUnit->vRect1.vY1 + pDataUnit->vDiff - 1;
		if (fFillRectToFillMap2(pDataGoTo->pFillMap,&pDataUnit->vRect1,JASS_M2) == 0)
		{
			return 0;
		}
	}

	pDataGoTo->vRect3.vX1 = fConvertIToX(vIntIn);
    pDataGoTo->vRect3.vY1 = fConvertIToY(vIntIn);
    pDataGoTo->vRect3.vX2 = pDataGoTo->vRect3.vX1 + pDataGoTo->vDiff - 1;
    pDataGoTo->vRect3.vY2 = pDataGoTo->vRect3.vY1 + pDataGoTo->vDiff - 1;
	if (fG(pDataGoTo->vRect1.vX1,
			pDataGoTo->vRect1.vY1,
			pDataGoTo->vRect3.vX1,
			pDataGoTo->vRect3.vY1) > 128)//pDataGoTo->vIdDataUnit > 9)
	{
		if (fFillRectToFillMap2(pDataGoTo->pFillMap,&pDataGoTo->vRect3,JASS_M2) == 0)
		{
			return 0;
		}
	}
	if (fCheckRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect3) == 0)
	{
		return 0;
	}
	if (pDataGoTo->vRect1.vX1 == pDataGoTo->vRect3.vX1 &&
		pDataGoTo->vRect1.vY1 == pDataGoTo->vRect3.vY1 &&
		pDataGoTo->vRect1.vX2 == pDataGoTo->vRect3.vX2 &&
		pDataGoTo->vRect1.vY2 == pDataGoTo->vRect3.vY2)
	{
		fFinishActionUnit(pDataGoTo->vIdDataUnit);
		return 1;
	}
	
	pDataGoTo->vBC = ((short)fConvertIToY(vIntIn) - (short)fConverterY(pDataGoTo->vY_Unit+pDataGoTo->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4)) * -1;
	pDataGoTo->vAC = (short)fConvertIToX(vIntIn) - (short)fConverterX(pDataGoTo->vX_Unit-pDataGoTo->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
	//sprintf(vTest,"pDataGoTo->vBC = %hu",pDataGoTo->vBC );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	//sprintf(vTest,"pDataGoTo->vAC = %hu",pDataGoTo->vAC );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	if (pDataGoTo->vBC == 0 && pDataGoTo->vAC == 0)
	{
		return 0;
	}
	pDataGoTo->vNapravlenie = -1;
	if (pDataGoTo->vBC >= 0 && pDataGoTo->vAC >= 0)
	{
		pDataGoTo->vNapravlenie = TOP_RIGHT;
	}
	if (pDataGoTo->vBC >= 0 && pDataGoTo->vAC < 0)
	{
		pDataGoTo->vNapravlenie = TOP_LEFT;
	}
	if (pDataGoTo->vBC < 0 && pDataGoTo->vAC < 0)
	{
		pDataGoTo->vNapravlenie = BOTTON_LEFT;
	}
	if (pDataGoTo->vBC < 0 && pDataGoTo->vAC >= 0)
	{
		pDataGoTo->vNapravlenie = BOTTON_RIGHT;
	}
	if (pDataGoTo->vNapravlenie == -1)
	{
		return 0;
	}
	//sprintf(vTest,"pDataGoTo->vNapravlenie = %d",pDataGoTo->vNapravlenie );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	pDataGoTo->i3 = 0;
	pDataGoTo->i2 = 0;

	//pDataGoTo->vMaxRect = 30;

	//pDataGoTo->vMassRect = new sRect*[pDataGoTo->vMaxRect];
	//pDataGoTo->vMassRect2 = new unsigned int*[pDataGoTo->vMaxRect];
	//pDataGoTo->vMassRect = new sRect*[vSizeMap];
	//pDataGoTo->vMassRect2 = new unsigned int*[vSizeMap];
    //pDataGoTo->vMassRect[0] = new sRect[1]; 
	pDataGoTo->vMassRect[0].vX1 = pDataGoTo->vRect1.vX1;
	pDataGoTo->vMassRect[0].vY1 = pDataGoTo->vRect1.vY1;
	pDataGoTo->vMassRect[0].vX2 = pDataGoTo->vRect1.vX2;
	pDataGoTo->vMassRect[0].vY2 = pDataGoTo->vRect1.vY2;
	//pDataGoTo->vMassRect2[0] = new unsigned int[1]; 
	pDataGoTo->vMassRect2[0] = 0;
	
	pDataGoTo->vIntFill = 0;
    pDataGoTo->vN_Rect = 0;
    pDataGoTo->vN_Rect_2 = 0;
    pDataGoTo->vN_Rect_3 = 1;
	pDataGoTo->vvi = pDataGoTo->vR2;
	pDataGoTo->vResult = 0;
	pDataGoTo->vFlag1 = FALSE;
	pDataGoTo->vvz = 0;
	do
	{
		//sprintf(vTest,"pDataGoTo->vvi = %u",pDataGoTo->vvi );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		pDataGoTo->vN_Rect = pDataGoTo->vN_Rect_2;//0//1
        pDataGoTo->vN_Rect_2 = pDataGoTo->vN_Rect_3-1;//1//5
		//sprintf(vTest,"pDataGoTo->vN_Rect = %u",pDataGoTo->vN_Rect );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		//sprintf(vTest,"pDataGoTo->vN_Rect_2 = %u",pDataGoTo->vN_Rect_2 );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		for (pDataGoTo->vvj = pDataGoTo->vN_Rect; pDataGoTo->vvj <= pDataGoTo->vN_Rect_2; pDataGoTo->vvj++)
		{
			/*if (pDataGoTo->vMassRect == NULL ||
				(unsigned int)pDataGoTo->vMassRect == 0xcdcdcdcd ||
				(unsigned int)pDataGoTo->vMassRect == 0xdddddddd ||
				(unsigned int)pDataGoTo->vMassRect[pDataGoTo->vvj] == 0xcdcdcdcd ||
				pDataGoTo->vvj >= pDataGoTo->vMaxRect)
			{
				goto m1;
			}*/
			//try
			//{
			pDataGoTo->vRect1.vX1 = pDataGoTo->vMassRect[pDataGoTo->vvj].vX1;
			pDataGoTo->vRect1.vY1 = pDataGoTo->vMassRect[pDataGoTo->vvj].vY1;
			pDataGoTo->vRect1.vX2 = pDataGoTo->vMassRect[pDataGoTo->vvj].vX2;
			pDataGoTo->vRect1.vY2 = pDataGoTo->vMassRect[pDataGoTo->vvj].vY2;
			//}
			//catch(...)
			//{
			//	goto m1;
			//}
			//sprintf(vTest,"pDataGoTo->vvj = %u",pDataGoTo->vvj );
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			if (fCheckRectToI(&pDataGoTo->vRect1,vIntIn) == 1 &&
				fCheckRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect1) == 1)
			{
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "111" );
				//pDataGoTo->vResult = 0;
				//goto m1;
				for (pDataGoTo->vvz = 0; pDataGoTo->vvz < vSizeMap; pDataGoTo->vvz++)
				{
					pDataGoTo->pFillMap[pDataGoTo->vvz] = 0;
				}
				pDataGoTo->i2 = pDataGoTo->vvj;
				pDataGoTo->vIntFill = JASS_M1;
				pDataGoTo->vCountBlink = 0;
                for (pDataGoTo->vvz = pDataGoTo->vN_Rect_3 - 1; pDataGoTo->vvz >= 0; pDataGoTo->vvz--)
				{
					if (pDataGoTo->i2 == pDataGoTo->vvz)
					{
						pDataGoTo->vRect1.vX1 = pDataGoTo->vMassRect[pDataGoTo->vvz].vX1;
						pDataGoTo->vRect1.vY1 = pDataGoTo->vMassRect[pDataGoTo->vvz].vY1;
						pDataGoTo->vRect1.vX2 = pDataGoTo->vMassRect[pDataGoTo->vvz].vX2;
						pDataGoTo->vRect1.vY2 = pDataGoTo->vMassRect[pDataGoTo->vvz].vY2;
						/*if (vFlagTest < 9)
						{
							sprintf(vTest,"pDataGoTo->vX1 = %hu",pDataGoTo->vX1);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"pDataGoTo->vY1 = %hu",pDataGoTo->vY1);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"pDataGoTo->i2 = %u",pDataGoTo->i2);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"pDataGoTo->vMassRect2[pDataGoTo->vvz][0] = %u",pDataGoTo->vMassRect2[pDataGoTo->vvz][0]);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							sprintf(vTest,"pDataGoTo->vIntFill = %u",pDataGoTo->vIntFill);
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
							vFlagTest++;
						}*/
						/*else
						{
							DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "111" );
							pDataGoTo->vResult = 0;
							goto m1;
						}*/
						if (fFillRectToFillMap2(pDataGoTo->pFillMap,&pDataGoTo->vRect1,pDataGoTo->vIntFill) == 0)//(&pDataGoTo->vPerevod)[0]) == 0)
						{
							pDataGoTo->vResult = 0;
							goto m1;
						}
						pDataGoTo->vIntFill++;
						pDataGoTo->i2 = pDataGoTo->vMassRect2[pDataGoTo->vvz];
						pDataGoTo->vCountBlink++;
					}
				}
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "888" );
							//pDataGoTo->vResult = 0;
							//goto m1;
				pDataGoTo->vResult = 1;
				goto m1;
			}
			/*pDataGoTo->vCount++;
			if (pDataGoTo->vCount > 10)
			{
				pDataGoTo->vResult = 0;
				goto m1;
			}*/
			/*if (pDataGoTo->vvi == 0)
			{
				DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "555" );
				pDataGoTo->vResult = 0;
				goto m1;
			}*/
			if (1)
			{
				//sprintf(vTest,"fConvertIToX(vIntIn) = %hu",fConvertIToX(vIntIn) );
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//sprintf(vTest,"fConvertIToY(vIntIn) = %hu",fConvertIToY(vIntIn));
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				// Ñòðîðîíû ïðÿìîóãîëüíîãî òðåóãîëüíèêà
				pDataGoTo->vBC = ((short)fConvertIToY(vIntIn) - (short)(pDataGoTo->vRect1.vY1 + pDataGoTo->vDiff / 2)) * -1;
				pDataGoTo->vAC = (short)fConvertIToX(vIntIn) - (short)(pDataGoTo->vRect1.vX1 + pDataGoTo->vDiff / 2);
				//sprintf(vTest,"pDataGoTo->vBC = %d",(int)pDataGoTo->vBC );
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//sprintf(vTest,"pDataGoTo->vAC = %d",(int)pDataGoTo->vAC );
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//pDataGoTo->vResult = 0;
				//goto m1;
				// Íàïðâàëåíèå ¹2
				if (pDataGoTo->vBC >= 0 && pDataGoTo->vAC >= 0)
				{
					pDataGoTo->vNapravlenie2 = TOP_RIGHT;
					goto m2;
				}
				if (pDataGoTo->vBC > 0 && pDataGoTo->vAC < 0)//=
				{
					pDataGoTo->vNapravlenie2 = TOP_LEFT;
					goto m2;
				}
				if (pDataGoTo->vBC <= 0 && pDataGoTo->vAC < 0)// íåò =
				{
					pDataGoTo->vNapravlenie2 = BOTTON_LEFT;
					goto m2;
				}
				if (pDataGoTo->vBC < 0 && pDataGoTo->vAC >= 0)
				{
					pDataGoTo->vNapravlenie2 = BOTTON_RIGHT;
					goto m2;
				}
m2:
				// Åñëè íàïðâëåíèå ¹1 ñîâïàäàåò ñ íàïðàâëåíèåì ¹2
				if (pDataGoTo->vNapravlenie2 == pDataGoTo->vNapravlenie ||
					pDataGoTo->vvz > 0)
				{
					if (pDataGoTo->vAC == 0)
					{
						pDataGoTo->vRad = vPI / 2;
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "222" );
						//pDataGoTo->vResult = 0;
						//goto m1;
					}
					else
					{
						if (pDataGoTo->vvz > 0)
						{
							switch (pDataGoTo->vNapravlenie2)
							{
								case TOP_RIGHT:
									pDataGoTo->vRad = atan((abs((double)pDataGoTo->vBC)/
														(abs((double)pDataGoTo->vAC+pDataGoTo->vR2-((double)pDataGoTo->vvz)))));
									break;
								case TOP_LEFT:
									pDataGoTo->vRad = atan((abs((double)pDataGoTo->vBC)/
														(abs((double)pDataGoTo->vAC-pDataGoTo->vR2-((double)pDataGoTo->vvz)))));
									break;
								case BOTTON_LEFT:
									pDataGoTo->vRad = atan((abs((double)pDataGoTo->vBC-pDataGoTo->vR2-((double)pDataGoTo->vvz))/
														(abs((double)pDataGoTo->vAC))));
									break;
								case BOTTON_RIGHT:
									pDataGoTo->vRad = atan((abs((double)pDataGoTo->vBC+pDataGoTo->vR2-((double)pDataGoTo->vvz))/
														(abs((double)pDataGoTo->vAC))));
									break;
								default:
									pDataGoTo->vResult = 0;
									goto m1;
							}
						}
						else
						{
							pDataGoTo->vRad = atan((abs((double)pDataGoTo->vBC)/
											   (abs((double)pDataGoTo->vAC))));
						}
					}
					//sprintf(vTest,"pDataGoTo->vRad = %lf",pDataGoTo->vRad );
					//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
					//sprintf(vTest,"pDataGoTo->vvi = %u",pDataGoTo->vvi );
					//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
					if (pDataGoTo->vFlag1 == FALSE)
					{
						pDataGoTo->vRect3.vX1 = (unsigned short)(round(cos(pDataGoTo->vRad) * (double)pDataGoTo->vvi));
						pDataGoTo->vRect3.vY1 = (unsigned short)(round(sin(pDataGoTo->vRad) * (double)pDataGoTo->vvi));
					}
					else
					{
						pDataGoTo->vRect3.vX1 = (unsigned short)(round(cos(pDataGoTo->vRad) * (double)pDataGoTo->vvi));
						pDataGoTo->vRect3.vY1 = (unsigned short)(round(cos(pDataGoTo->vRad) * (double)pDataGoTo->vvi));
					}
					//sprintf(vTest,"pDataGoTo->vX1_3 = %hu",pDataGoTo->vX1_3 );
					//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
					//sprintf(vTest,"pDataGoTo->vY1_3 = %hu",pDataGoTo->vY1_3 );
					//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
					pDataGoTo->vRect3.vX2 = pDataGoTo->vRect3.vX1;
					pDataGoTo->vRect3.vY2 = pDataGoTo->vRect3.vY1;
					switch (pDataGoTo->vNapravlenie)
					{
						case TOP_RIGHT:
							pDataGoTo->vRect2.vX1 = (unsigned short)((short)pDataGoTo->vRect1.vX1 + (short)(pDataGoTo->vRect3.vX1) * 1);
							pDataGoTo->vRect2.vY1 = (unsigned short)((short)pDataGoTo->vRect1.vY1 + (short)(pDataGoTo->vRect3.vY1) * -1);
							pDataGoTo->vRect2.vX2 = (unsigned short)((short)pDataGoTo->vRect1.vX2 + (short)(pDataGoTo->vRect3.vX2) * 1);
							pDataGoTo->vRect2.vY2 = (unsigned short)((short)pDataGoTo->vRect1.vY2 + (short)(pDataGoTo->vRect3.vY2) * -1);
							break;
						case TOP_LEFT:
							pDataGoTo->vRect2.vX1 = (unsigned short)((short)pDataGoTo->vRect1.vX1 + (short)(pDataGoTo->vRect3.vX1) * -1);
							pDataGoTo->vRect2.vY1 = (unsigned short)((short)pDataGoTo->vRect1.vY1 + (short)(pDataGoTo->vRect3.vY1) * -1);
							pDataGoTo->vRect2.vX2 = (unsigned short)((short)pDataGoTo->vRect1.vX2 + (short)(pDataGoTo->vRect3.vX2) * -1);
							pDataGoTo->vRect2.vY2 = (unsigned short)((short)pDataGoTo->vRect1.vY2 + (short)(pDataGoTo->vRect3.vY2) * -1);
							break;
						case BOTTON_LEFT:
							pDataGoTo->vRect2.vX1 = (unsigned short)((short)pDataGoTo->vRect1.vX1 + (short)(pDataGoTo->vRect3.vX1) * -1);
							pDataGoTo->vRect2.vY1 = (unsigned short)((short)pDataGoTo->vRect1.vY1 + (short)(pDataGoTo->vRect3.vY1) * 1);
							pDataGoTo->vRect2.vX2 = (unsigned short)((short)pDataGoTo->vRect1.vX2 + (short)(pDataGoTo->vRect3.vX2) * -1);
							pDataGoTo->vRect2.vY2 = (unsigned short)((short)pDataGoTo->vRect1.vY2 + (short)(pDataGoTo->vRect3.vY2) * 1);
							break;
						case BOTTON_RIGHT:
							pDataGoTo->vRect2.vX1 = (unsigned short)((short)pDataGoTo->vRect1.vX1 + (short)(pDataGoTo->vRect3.vX1) * 1);
							pDataGoTo->vRect2.vY1 = (unsigned short)((short)pDataGoTo->vRect1.vY1 + (short)(pDataGoTo->vRect3.vY1) * 1);
							pDataGoTo->vRect2.vX2 = (unsigned short)((short)pDataGoTo->vRect1.vX2 + (short)(pDataGoTo->vRect3.vX2) * 1);
							pDataGoTo->vRect2.vY2 = (unsigned short)((short)pDataGoTo->vRect1.vY2 + (short)(pDataGoTo->vRect3.vY2) * 1);
							break;
						default:
							pDataGoTo->vResult = 0;
							goto m1;
					}
					if (abs(pDataGoTo->vBC) <= (pDataGoTo->vR2/2) &&
						abs(pDataGoTo->vAC) <= (pDataGoTo->vR2/2))
					{
						pDataGoTo->vRect2.vX1 = pDataGoTo->vRect1.vX1;
						pDataGoTo->vRect2.vY1 = pDataGoTo->vRect1.vY1;
						pDataGoTo->vRect2.vX2 = pDataGoTo->vRect1.vX2;
						pDataGoTo->vRect2.vY2 = pDataGoTo->vRect1.vY2;
					}
					if (pDataGoTo->vRect2.vX1 >= vW3 || pDataGoTo->vRect2.vY1 >= vH3 || pDataGoTo->vRect2.vX2 >= vW3 || pDataGoTo->vRect2.vY2 >= vH3)
					{
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "999" );
						break;
					}
					//pDataGoTo->vX1_2 += 1;
					//pDataGoTo->vY1_2 += 1;
					//pDataGoTo->vX2_2 += 1;
					//pDataGoTo->vY2_2 += 1;
				}
				else
				{
					pDataGoTo->vRect2.vX1 = pDataGoTo->vRect1.vX1;
					pDataGoTo->vRect2.vY1 = pDataGoTo->vRect1.vY1;
					pDataGoTo->vRect2.vX2 = pDataGoTo->vRect1.vX2;
					pDataGoTo->vRect2.vY2 = pDataGoTo->vRect1.vY2;
					/*pDataGoTo->vRect2.vX1 = fConvertIToX(vIntIn) - (pDataGoTo->vDiff / 2);
					pDataGoTo->vRect2.vY1 = fConvertIToY(vIntIn) - (pDataGoTo->vDiff / 2);
					pDataGoTo->vRect2.vX2 = fConvertIToX(vIntIn) + (pDataGoTo->vDiff / 2) - 1;
					pDataGoTo->vRect2.vY2 = fConvertIToY(vIntIn) + (pDataGoTo->vDiff / 2) - 1;*/
				}
				//sprintf(vTest,"pDataGoTo->vX1_2 = %hu",pDataGoTo->vX1_2 );
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//sprintf(vTest,"pDataGoTo->vY1_2 = %hu",pDataGoTo->vY1_2 );
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//sprintf(vTest,"pDataGoTo->vX2_2 = %hu",pDataGoTo->vX2_2 );
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//sprintf(vTest,"pDataGoTo->vY2_2 = %hu",pDataGoTo->vY2_2 );
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				if ((pDataGoTo->Fly == TRUE ||
					(pDataGoTo->Fly == FALSE && fCheckRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect2) == 1)))
				{
					pDataGoTo->vResultFill = fFillRectToFillMap(pDataGoTo->pFillMap,&pDataGoTo->vRect2,JASS_M1+pDataGoTo->vIntFill);
					if (pDataGoTo->vResultFill == 1 || pDataGoTo->vResultFill == 2)
					{
						//sprintf(vTest,"pDataGoTo->vX1_2 = %hu",pDataGoTo->vX1_2 );
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						//sprintf(vTest,"pDataGoTo->vY1_2 = %hu",pDataGoTo->vY1_2 );
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						/*if (pDataGoTo->vMassRect == NULL ||
							(unsigned int)pDataGoTo->vMassRect == 0xcdcdcdcd ||
							(unsigned int)pDataGoTo->vMassRect == 0xdddddddd ||
							pDataGoTo->vN_Rect_3 >= pDataGoTo->vMaxRect)
						{
							goto m1;
						}*/
						//try
						//{
						//pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3] = new sRect[1]; 
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vX1 = pDataGoTo->vRect2.vX1;
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vY1 = pDataGoTo->vRect2.vY1;
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vX2 = pDataGoTo->vRect2.vX2;
						pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3].vY2 = pDataGoTo->vRect2.vY2;
						//pDataGoTo->vMassRect2[pDataGoTo->vN_Rect_3] = new unsigned int[1];
						pDataGoTo->vMassRect2[pDataGoTo->vN_Rect_3] = pDataGoTo->vvj;
						//}
						//catch(...)
						//{
						//	goto m1;
						//}
						/*if (pDataGoTo->vResultFill == 2 && pDataGoTo->i3 == 0)
						{
							pDataGoTo->i3 = pDataGoTo->vvj;
							goto m2;
						}
						if (pDataGoTo->vResultFill == 1)
						{
							pDataGoTo->i2 = pDataGoTo->vvj;
						}*/
						pDataGoTo->vN_Rect_3++;
						if (pDataGoTo->vN_Rect_3 >= pDataGoTo->vMaxRect)
						{
							//goto m1;
							return 0;
						}
						//pDataGoTo->vBC = ((short)fConvertIToY(vIntIn) - (short)(pDataGoTo->vY1_2 + pDataGoTo->vDiff / 2)) * -1;
						//pDataGoTo->vAC = (short)fConvertIToX(vIntIn) - (short)(pDataGoTo->vX1_2 + pDataGoTo->vDiff / 2);
						//sprintf(vTest,"pDataGoTo->vBC = %d",(int)pDataGoTo->vBC );
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						//sprintf(vTest,"pDataGoTo->vAC = %d",(int)pDataGoTo->vAC );
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						pDataGoTo->vvi = (unsigned int)sqrt((double)(pDataGoTo->vBC*pDataGoTo->vBC+pDataGoTo->vAC*pDataGoTo->vAC));
						//sprintf(vTest,"pDataGoTo->vvi = %u",pDataGoTo->vvi );
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						if (pDataGoTo->vvi > 48)
						{
							pDataGoTo->vvi = pDataGoTo->vR2;
						}
						else
						{
							pDataGoTo->vvz = 0;
							pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1].vX1 = (unsigned short)((short)fConvertIToX(vIntIn));
							pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1].vY1 = (unsigned short)((short)fConvertIToY(vIntIn));
							//pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1][0].vX2 = (unsigned short)((short)fConvertIToX(vIntIn)+1);
							//pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1][0].vY2 = (unsigned short)((short)fConvertIToY(vIntIn)+1);
							pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1].vX2 = pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1].vX1 + pDataGoTo->vDiff - 1;
							pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1].vY2 = pDataGoTo->vMassRect[pDataGoTo->vN_Rect_3-1].vY1 + pDataGoTo->vDiff - 1;
						}
						pDataGoTo->vvi++;
						pDataGoTo->vIntFill++;
						pDataGoTo->vFlag1 = FALSE;
						//sprintf(vTest,"pDataGoTo->vIntFill = %u",pDataGoTo->vIntFill );
						//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						if (pDataGoTo->vIntFill > 400)
						{
							//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "444" );
							pDataGoTo->vResult = 0;
							goto m1;
						}
						break;
					}
				}
				//else
				//{
					/*DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "333" );
					sprintf(vTest,"pDataGoTo->vNapravlenie2 = %d",pDataGoTo->vNapravlenie2 );
					DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
					sprintf(vTest,"pDataGoTo->vNapravlenie = %d",pDataGoTo->vNapravlenie );
					DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );*/
					//pDataGoTo->vResult = 0;
					//goto m1;
				//}
			}
		}
		if (pDataGoTo->vvi == 1 && 
			pDataGoTo->vFlag1 == FALSE)
		{
			pDataGoTo->vvi = 48 + 1;
			pDataGoTo->vvz++;
			if (pDataGoTo->vvz >= 48)
			{
				pDataGoTo->vFlag1 = TRUE;
			}
		}
		pDataGoTo->vvi--;
	}
	while (pDataGoTo->vvi >= 1);
m1:
				//sprintf(vTest,"return 0;");
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
				//return 0;
	/*for (int count = 0; count < pDataGoTo->vN_Rect_3; count++)
	{
        delete []pDataGoTo->vMassRect[count];
		delete []pDataGoTo->vMassRect2[count];
	}
	delete pDataGoTo->vMassRect;
	delete pDataGoTo->vMassRect2;*/
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "444+" );
	return 1;
}

double round(double d)
{
  return floor(d + 0.5);
}

integer vTest1(void *p)
{
	if (!p) return 0;
	DataGoTo *pDataGoTo;
	pDataGoTo = (DataGoTo*)p;
	if (fFillMap(vMapGlobal, pDataGoTo->pFillMap) == 0)
	{
		goto m1;
	}
	if (fFillMapUnits(pDataGoTo) == 0)
	{
		goto m1;
	}
	if (pDataGoTo->vAction == ACTION_GO)
	{
		if (fFillMToI(pDataGoTo,pDataGoTo->vInt3) == 0)
		{
			goto m1;
		}
	}
	if (pDataGoTo->vAction == ACTION_BLINK)
	{
		if (fFillBlinkToI(pDataGoTo,pDataGoTo->vInt3) == 0)
		{
			goto m1;
		}
	}
	return 0;
m1:
	DataUnit *pDataUnit = fGetDataUnit(pDataGoTo->vIdDataUnit);
	if (pDataUnit)
	{
		fFinishActionUnit(pDataUnit->vIdDataUnit);
	}
    return 0;
}

integer fFillMapUnits(DataGoTo *pDataGoTo)
{
	if (pDataGoTo == NULL) return 0;
	unsigned short vInt;
	DataUnit *pDataUnit;
	for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
		vInt = vMapUnits[pDataGoTo->vStorona][vvi];
		if (vInt != NO_UNIT)
		{
			pDataUnit = fGetDataUnit(vInt);
			if (pDataUnit)
			{
				if (pDataUnit->Fly == FALSE && pDataGoTo->vIdDataUnit != pDataUnit->vIdDataUnit && pDataUnit->vId != ID_UNIT_WORKER)//Âîðêåðîâ íå çàëèâàåì, è âîçäóøíûõ è ñåáÿ òîæå
				{
					pDataGoTo->pFillMap[vvi] = JASS_UNIT;
				}
			}
		}
	}
	return 1;
}

integer fUnitGoToInt(unsigned short vIdDataUnit, integer vInt, unsigned short vAction, unsigned int vMaxR)
{
	//sprintf(vTest,"vInt = %d",(int)vInt );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	if (vMapGlobal == 0) return 0;
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS || vMaxR < 0 || vMaxR > vSizeMap) return 0;
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (!pDataUnit) return 0;
	if (!pDataUnit->WhichUnit) return 0;
	if (pDataUnit->vGoTo == FALSE) return 0;
	DataGoTo *pDataGoTo;
	pDataUnit->pPut->IdetPoPuti = FALSE;
	pDataUnit->vAction = vAction;
	pDataUnit->vIntForGoTo = vInt;
	pDataGoTo = pDataUnit->pPut;
	pDataGoTo->vMaxR = vMaxR;
	pDataGoTo->vIdDataUnit = vIdDataUnit;
	pDataGoTo->vId = pDataUnit->vId;
    pDataGoTo->vX_Unit = GetUnitX(pDataUnit->WhichUnit);
    pDataGoTo->vY_Unit = GetUnitY(pDataUnit->WhichUnit);
	pDataGoTo->vInt3 = (unsigned int)vInt;
	pDataGoTo->vResult = 0;
	pDataGoTo->vStorona = pDataUnit->vStorona;
	pDataGoTo->Fly = pDataUnit->Fly;
	pDataGoTo->vAction = vAction;
	pDataGoTo->vUnit = pDataUnit->WhichUnit;
	pDataGoTo->vCurrentIntFill = JASS_M1 + 16;
	switch (pDataGoTo->vAction)
	{
		case ACTION_GO:
			sprintf(pDataGoTo->vActionStr,"move");
			pDataGoTo->vR2 = 16;//fGetDiffUnit(pDataGoTo->vUnit)*8;
			pDataGoTo->vTimeForSpeed = 3;
			break;
		case ACTION_BLINK:
			sprintf(pDataGoTo->vActionStr,"blink");
			pDataGoTo->vR2 = 48;
			pDataGoTo->vTimeForSpeed = 3;
			break;
		default:
			return 0;
	}

	if (pDataGoTo->vBoolTimer == TRUE)
	{
		//if (vEndGame2 == 0)
		//{
		//	if (pDataGoTo->vTimer > 0)
		//	{
		//		DestroyTimer(pDataGoTo->vTimer);
		//	}
		//}
		pDataGoTo->vBoolTimer = FALSE;
	}
	pDataUnit->pPut->vCelNeDostignuta = FALSE;
	pDataGoTo->vBoolTimer = FALSE;
	pDataGoTo->vR = pDataGoTo->vR2;
	pDataGoTo->vFlagDT = FALSE;
	pDataGoTo->vCount = 0;
	pDataGoTo->vFlag = 0;
	pDataGoTo->vRadius_Unit = fRadius_Unit(pDataGoTo->vId);
    pDataGoTo->vDiff = fRadiusToDiff(pDataGoTo->vRadius_Unit);
	if (pDataGoTo->vAction == ACTION_GO || pDataGoTo->vAction == ACTION_BLINK)
	{
		pDataGoTo->vRect5.vX1 = fConverterX(GetUnitX(pDataGoTo->vUnit)-pDataGoTo->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
		pDataGoTo->vRect5.vY1 = fConverterY(GetUnitY(pDataGoTo->vUnit)+pDataGoTo->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4);
		pDataGoTo->vRect5.vX2 = pDataGoTo->vRect5.vX1 + pDataGoTo->vDiff - 1;
		pDataGoTo->vRect5.vY2 = pDataGoTo->vRect5.vY1 + pDataGoTo->vDiff - 1;
	}
	if (pDataGoTo->vAction == ACTION_BLINK)
	{
		pDataGoTo->vX4 = fConverterX(GetUnitX(pDataGoTo->vUnit));
		pDataGoTo->vY4 = fConverterY(GetUnitY(pDataGoTo->vUnit));
		/*pDataGoTo->vRect5.vX1 = pDataGoTo->vX4;
		pDataGoTo->vRect5.vY1 = pDataGoTo->vY4;
		pDataGoTo->vRect5.vX2 = pDataGoTo->vRect5.vX1 + pDataGoTo->vDiff - 1;
		pDataGoTo->vRect5.vY2 = pDataGoTo->vRect5.vY1 + pDataGoTo->vDiff - 1;*/
	}
	pDataGoTo->IdetPoPuti = TRUE;
	pDataGoTo->vPriority = FALSE;
	if (pDataGoTo->vPriority == FALSE)
	{
		fActionFillMapUnits(pDataGoTo);
	}
	if (pDataGoTo->hThreadGoTo)
	{
		pDataGoTo->hThreadGoTo = pDataGoTo->hThreadGoTo;
		//TerminateThread(pDataGoTo->hThreadGoTo,0);
	}
	pDataGoTo->hThreadGoTo = NULL;
	pDataGoTo->hThreadGoTo = CreateThread( NULL, 0, ThreadGoTo, (void*)pDataGoTo, 0 , NULL);
	//pDataGoTo->hThreadGoTo = CreateThread( NULL, 0, ThreadGoTo, (void*)pDataGoTo, 0 , NULL);
	//WaitForSingleObject(pDataGoTo->hThreadGoTo, INFINITE);
	//vTest1((void*)pDataGoTo);
	//pDataGoTo->vThreadGoTo = TRUE;
	//fAddDataGoTo(pDataGoTo);
	//ExecuteFunc("fThreadGoTo3_JASS");
	return 1;
}

bool fGlobalGoToInt(integer vInt, DataGoTo *pDataGoTo, DataUnit *pDataUnit)
{
	if (pDataGoTo == NULL) return FALSE;
	if (pDataUnit == NULL) return FALSE;
	if (vMapGlobal == 0) return FALSE;
	pDataGoTo->IdetPoPuti = FALSE;
	pDataGoTo->vId = ID_UNIT_DUMMY;
    pDataGoTo->vX_Unit = pDataUnit->LastCoordinates.X;
    pDataGoTo->vY_Unit = pDataUnit->LastCoordinates.Y;
	pDataGoTo->vInt3 = (unsigned int)vInt;
	pDataGoTo->vResult = 0;
	pDataGoTo->vStorona = pDataUnit->vStorona;
	pDataGoTo->Fly = FALSE;
	pDataGoTo->vAction = ACTION_GO;
	pDataGoTo->vRadius_Unit = 16;
    pDataGoTo->vDiff = 1;
	pDataGoTo->vMaxR = vSizeMap;
	sprintf(pDataGoTo->vActionStr,"move");
	pDataGoTo->vPriority = TRUE;
	if (pDataGoTo->hThreadGoTo)
	{
		TerminateThread(pDataGoTo->hThreadGoTo,0);
	}
	pDataGoTo->hThreadGoTo = NULL;
	pDataGoTo->hThreadGoTo = CreateThread( NULL, 0, ThreadGoTo, (void*)pDataGoTo, 0 , NULL);
	return 1;
}

/*void fThreadGoTo3()
{
	DataGoTo *pCurrentDataGoTo;
	pCurrentDataGoTo = pFirstDataGoTo;
	while(pCurrentDataGoTo)
	{
		if (pCurrentDataGoTo->vThreadGoTo == TRUE)
		{
			pCurrentDataGoTo->vThreadGoTo = FALSE;
			fThreadGoTo2(pCurrentDataGoTo);
		}
		pCurrentDataGoTo = pCurrentDataGoTo->next;
	}
}*/

/*void fThreadGoTo2(DataGoTo *pDataGoTo)
{
	if (!pDataGoTo) return;
	if (fFillMap(vMapGlobal, pDataGoTo->pFillMap) == 0)
	{
		goto m1;
	}
	if (fFillMapUnits(pDataGoTo) == 0)
	{
		goto m1;
	}
	if (pDataGoTo->vAction == ACTION_GO)
	{
		if (fFillMToI(pDataGoTo,pDataGoTo->vInt3) == 0)
		{
			goto m1;
		}
	}
	if (pDataGoTo->vAction == ACTION_BLINK)
	{
		if (fFillBlinkToI(pDataGoTo,pDataGoTo->vInt3) == 0)
		{
			goto m1;
		}
	}
	return;
m1:
	DataUnit *pDataUnit = fGetDataUnit(pDataGoTo->vIdDataUnit);
	if (pDataUnit)
	{
		fFinishActionUnit(pDataUnit->vIdDataUnit);
	}
}*/

// Çàïóñòèòü GoTo
integer fGoTo(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit->vGoTo == FALSE) return 0;
	DataGoTo *pDataGoTo;
	// Åñëè ïóòü íîëü èëè 0xcccccccc, òî íîëü
	if (!pDataUnit->pPut || (int)pDataUnit->pPut == 0xcccccccc)
	{
		return 0;
	}
	// Åñëè èä¸ò ïî ïóòè ôàéëñå, òî 1
	if (pDataUnit->pPut->IdetPoPuti == FALSE)
	{
		return 1;
	}
	// Åñëè èä¸ò ïî ïóòè òðó, òî ïóòü
	if (pDataUnit->pPut->IdetPoPuti == TRUE)
	{
		pDataGoTo = pDataUnit->pPut;
	}
	else
	{
		// Åñëè èä¸ò ïî ïóòè ôàëñå, è ìû ïðèøëè òî 2
		if (pDataUnit->pPut->vCurrentIntFill == JASS_M1)
		{
			return 2;
		}
		else
		{
			// Åñëè èä¸ò ïî ïóòè ôàëñå è ìû íå ïðèøëè òî 0
			return 0;
		}
	}
	// Åñëè ðåçóëò íîëü, òî íîëü
	if (pDataGoTo->vResult == 0)
	{
		return 0;
	}
	/*if (pDataGoTo->hTrigger > 0)
	{
		fFreeTrigger(pDataGoTo->hTrigger);
	}
	pDataGoTo->hTrigger = CreateTrigger();*/
	/*if (pDataGoTo->hTimer > 0)
	{
		DestroyTimer(pDataGoTo->hTimer);
	}*/
	//pDataGoTo->hTimer = CreateTimer();
	pDataGoTo->vTimeForBlink = 0;//TimerGetElapsed(vTimerGame);
	fAddDataGoTo(pDataGoTo);
	//IncrementHandleReferences(pDataGoTo->hTrigger);
	//TriggerAddAction(pDataGoTo->hTrigger, &fActionGoTo);
	if (pDataGoTo->vAction == ACTION_GO)
	{
		//TimerStart(pDataGoTo->hTimer,0.1,true,&fActionGoTo);
		//TriggerRegisterTimerEvent(pDataGoTo->hTrigger,0.1,TRUE);
	}
	if (pDataGoTo->vAction == ACTION_BLINK)
	{
		//TimerStart(pDataGoTo->hTimer,0.1,true,&fActionGoTo);
		//TriggerRegisterTimerEvent(pDataGoTo->hTrigger,0.1,TRUE);
	}
	pDataGoTo->vTimeGoTo = pDataUnit->vTimeGoTo;
	//SaveInteger(vHT,pDataGoTo->hTrigger,CONST_ACTION_GO_TO,(integer)pDataGoTo);
	//SaveInteger(vHT,pDataGoTo->hTimer,CONST_ACTION_GO_TO,(integer)pDataGoTo);
	return 1;
}

// Ïðîâåðèòü GoTo
integer fCheckGoTo(unsigned short vIdDataUnit)
{
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS) return -1;
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (!pDataUnit) return -1;
	if (!pDataUnit->pPut) return -1;
	if (pDataUnit->pPut)
	{
		if (pDataUnit->pPut->vResult == 1)
		{
			return 1;
		}
	}
	return 0;
}

integer fGetCurrentIntFill(unsigned short *pFillMap, sRect2* vRect)
{
	vRect->vFill_1 = 0;
	vRect->vFill_2 = 0;
	vRect->vFlag = 0;
	//vRect->vCount2 = 0;
	for (vRect->vvj2 = vRect->vY1; vRect->vvj2 <= vRect->vY2; vRect->vvj2++)
	{
		for (vRect->vvi2 = vRect->vX1; vRect->vvi2 <= vRect->vX2; vRect->vvi2++)
		{
			vRect->vInt = fGetCellInFillMap(pFillMap,vRect->vvi2,vRect->vvj2);
			if (vRect->vInt == JASS_UNKNOWN) return 0;
			if (vRect->vInt == JASS_M1) return JASS_M1;
			//if (vRect->vInt == JASS_M1 && vRect->vFlag == 1) return 0;
			/*if (vRect->vInt == JASS_M1)
			{
				vRect->vCount2++;
			}*/
			if (vRect->vInt != 0)
			{
				if (vRect->vFill_1 == 0)
				{
					vRect->vFill_1 = vRect->vInt;
					vRect->vFill_2 = vRect->vInt;
				}
				else
				{
					vRect->vFill_2 = vRect->vInt;
					if (vRect->vFill_2 < vRect->vFill_1)
					{
						vRect->vFill_1 = vRect->vFill_2;
					}
				}
			}
			else
			{
				return 0;
				//vRect->vFlag = 1;
			}
		}
	}
	/*if (vRect->vCount2 != 0)
	{
		if (vRect->vCount2 != (vRect->vX2 - vRect->vX1 + 1) * (vRect->vY2 - vRect->vY1 + 1))
		{
			return 0;
		}
	}*/
	return vRect->vFill_1;
}

void fActionGoTo(DataUnit* pDataUnit, real vTimerGetElapsed)
{
	if (pDataUnit == NULL) return;
	//DataGoTo *pDataGoTo = fGetDataGoToBase(GetTriggeringTrigger());
	//HTRIGGER hTrigger;
	//hTrigger = GetTriggeringTrigger();
	/*if (hTrigger == NULL)
	{
		return;
	}*/
	//HTIMER hTimer = GetExpiredTimer();
	//if (hTimer == NULL) return;
	//real vTimerGetElapsed = TimerGetElapsed(vTimerGame);
	//DataGoTo *pDataGoTo = (DataGoTo *)LoadInteger(vHT,hTrigger,CONST_ACTION_GO_TO);
	//DataGoTo *pDataGoTo = (DataGoTo *)LoadInteger(vHT,hTimer,CONST_ACTION_GO_TO);
	DataGoTo *pDataGoTo = pDataUnit->pPut;
	if (pDataGoTo == 0 || (unsigned int)pDataGoTo == 0xdddddddd) 
	{
		//fFreeTrigger(GetTriggeringTrigger());
		//if (hTimer > 0)
		//{
		//	DestroyTimer(hTimer);
		//}
		return;
	}
	pDataGoTo->vTimeElapsedGoTo = vTimerGetElapsed - pDataGoTo->vTimeGoTo;
	if (pDataGoTo->vTimeElapsedGoTo < 0)
	{
		pDataGoTo->IdetPoPuti = FALSE;
		pDataGoTo->vCelNeDostignuta = TRUE;
		return;
	}
	//DataUnit *pDataUnit = fGetDataUnit(pDataGoTo->vUnit);
	//if (pDataUnit == NULL)
	//{
		//fFreeTrigger(GetTriggeringTrigger());
		//if (hTimer > 0)
		//{
		//	DestroyTimer(hTimer);
		//}
		//return;
	//}
	if (UnitHasBuffsEx(pDataUnit->WhichUnit,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE,FALSE) == TRUE)
	{
		return;
	}
	if (pDataGoTo->vAction == ACTION_BLINK)
	{
		//fLog(0,"0");
		//fLog(41000,"41000");
		//sprintf(vBuf,"Color - %hu, Blink - %f, T - %f, Dif - %f\0",pDataUnit->vIdDataUnit,pDataGoTo->vTimeForBlink,vTimerGetElapsed,vTimerGetElapsed - pDataGoTo->vTimeForBlink);
		//fLog(41011,vBuf);
		if (pDataGoTo->vTimeForBlink != 0 &&
			vTimerGetElapsed - pDataGoTo->vTimeForBlink < 0.6)
		{
			pDataUnit->Permission = FALSE;
			pDataUnit->PermissionMove = FALSE;
			return;
		}
	}
	if (pDataGoTo->vResult == 0)
	{
		goto m1;
	}
	if (pDataGoTo->IdetPoPuti == FALSE)
	{
		pDataGoTo->vFlagDT = 1;
		goto m1;
	}
	if (1)
	{
		if (pDataGoTo->vAction == ACTION_GO || pDataGoTo->vAction == ACTION_BLINK)
		{
			pDataGoTo->vRect4.vX1 = fConverterX(GetUnitX(pDataGoTo->vUnit)-pDataGoTo->vRadius_Unit+(float)DOOD_MAP_SUBDIV/4);
			pDataGoTo->vRect4.vY1 = fConverterY(GetUnitY(pDataGoTo->vUnit)+pDataGoTo->vRadius_Unit-(float)DOOD_MAP_SUBDIV/4);
			pDataGoTo->vRect4.vX2 = pDataGoTo->vRect4.vX1 + pDataGoTo->vDiff - 1;
			pDataGoTo->vRect4.vY2 = pDataGoTo->vRect4.vY1 + pDataGoTo->vDiff - 1;
		}
		if (pDataGoTo->vAction == ACTION_BLINK)
		{
			pDataGoTo->vX3 = fConverterX(GetUnitX(pDataGoTo->vUnit));
			pDataGoTo->vY3 = fConverterY(GetUnitY(pDataGoTo->vUnit));
		}
		if (fIsPointToRect(GetUnitX(pDataGoTo->vUnit),GetUnitY(pDataGoTo->vUnit),&pDataGoTo->vRect5) && pDataGoTo->vAction == ACTION_BLINK ||
			fIsRectToRect(&pDataGoTo->vRect4,&pDataGoTo->vRect5) == TRUE && pDataGoTo->vAction == ACTION_GO)
		{
m2:
			if (pDataGoTo->vAction == ACTION_GO)
			{
				pDataGoTo->vCurrentIntFill = fGetCurrentIntFill(pDataGoTo->pFillMap,&pDataGoTo->vRect4);
				if (pDataGoTo->vCurrentIntFill - JASS_M1 <= pDataGoTo->vR)
				{
					pDataGoTo->vR = pDataGoTo->vCurrentIntFill - JASS_M1;
				}
				if (pDataGoTo->vCurrentIntFill > 0 &&
					(fIsMainHuman(pDataUnit->vId) == TRUE &&
					pDataGoTo->vCurrentIntFill <= JASS_M1 + pDataGoTo->vR2 ||
					fIsMainHuman(pDataUnit->vId) == FALSE))
				{
					pDataGoTo->vNextIntFill = pDataGoTo->vCurrentIntFill - pDataGoTo->vR;
					if (fGetINext(pDataGoTo->pFillMap,&pDataGoTo->vRect4,&pDataGoTo->vRect5,pDataGoTo->vNextIntFill,pDataGoTo->vR,pDataGoTo->vDiff) == FALSE)
					{
						pDataGoTo->vInt2 = JASS_UNKNOWN;
					}
					else
					{
						pDataGoTo->vInt2 = 0;
					}
				}
				else
				{
					pDataGoTo->vCelNeDostignuta = TRUE;
					pDataGoTo->vFlagDT = TRUE;
				}
			}
			if (pDataGoTo->vAction == ACTION_BLINK)
			{
				pDataGoTo->vCurrentIntFill = fGetCellInFillMap(pDataGoTo->pFillMap,pDataGoTo->vX3,pDataGoTo->vY3);
				if (pDataGoTo->vCurrentIntFill > JASS_M1)
				{
					pDataGoTo->vNextIntFill = pDataGoTo->vCurrentIntFill - 1;
					pDataGoTo->vInt2 = 0;
					pDataGoTo->vInt2 = fGetINext(pDataGoTo->pFillMap,pDataGoTo->vNextIntFill,pDataGoTo->vDiff);
				}
			}
			
			if (pDataGoTo->vCurrentIntFill == JASS_M1)
			{
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "777" );
				pDataGoTo->vFlagDT = TRUE;
			}
			if (pDataGoTo->vInt2 != JASS_UNKNOWN)
			{
				if (pDataGoTo->vAction == ACTION_BLINK)
				{
					pDataGoTo->vX4 = fConvertIToX(pDataGoTo->vInt2);
					pDataGoTo->vY4 = fConvertIToY(pDataGoTo->vInt2);
					pDataGoTo->vRect5.vX1 = pDataGoTo->vX4;
					pDataGoTo->vRect5.vY1 = pDataGoTo->vY4;
					pDataGoTo->vRect5.vX2 = pDataGoTo->vRect5.vX1 + pDataGoTo->vDiff - 1;
					pDataGoTo->vRect5.vY2 = pDataGoTo->vRect5.vY1 + pDataGoTo->vDiff - 1;
				}
				if (fIsRectToRect(&pDataGoTo->vRect4,&pDataGoTo->vRect5) == TRUE)
				{
					pDataGoTo->vFlagDT = TRUE;
					goto m1;
				}
				pDataGoTo->vG = fG(pDataUnit->LastCoordinates.X,
								   pDataUnit->LastCoordinates.Y,
								   fConvertIToRX(pDataGoTo->vInt2),
								   fConvertIToRY(pDataGoTo->vInt2));
				if (//pDataGoTo->vNextIntFill == JASS_M1 &&
					pDataUnit->Move == FALSE &&
					pDataGoTo->vAction == ACTION_BLINK &&
					pDataGoTo->vG < 130 &&
					pDataGoTo->vNextIntFill == JASS_M1)
				{
					fFinishActionUnit(pDataUnit->vIdDataUnit);
					pDataUnit->vSyncClean = TRUE;
					if (pDataGoTo->vG > 100)
					{
						pDataUnit->vActionN = ACTION_N_NEW;
						pDataUnit->vIntForGoTo = fJumpBack(pDataUnit);
						pDataUnit->vAction = ACTION_GO;
						pDataGoTo->vTimeForBlink = vTimerGetElapsed;
						IssuePointOrderLoc(pDataGoTo->vUnit,pDataGoTo->vActionStr,hLocationMap[fJumpBack(pDataUnit)]);
					}
					return;
				}
				if (pDataGoTo->vBoolTimer == TRUE)
				{
					//if (vEndGame2 == 0)
					//{
					//	if (pDataGoTo->vTimer > 0)
					//	{
					//		DestroyTimer(pDataGoTo->vTimer);
					//	}
					//}
					pDataGoTo->vBoolTimer = FALSE;
				}
				if (pDataGoTo->vBoolTimer == FALSE)
				{
					//pDataGoTo->vTimer = CreateTimer();
					//SaveInteger(vHT,pDataGoTo->vTimer,CONST_EXPIRED_TIMER_GO_TO,(integer)pDataGoTo);
					if (pDataGoTo->vAction == ACTION_GO)
					{
						//TimerStart(pDataGoTo->vTimer,pDataGoTo->vR * pDataGoTo->vTimeForSpeed,FALSE,fExpiredTimerGoTo);
					}
					if (pDataGoTo->vAction == ACTION_BLINK)
					{
						//TimerStart(pDataGoTo->vTimer,pDataGoTo->vR * pDataGoTo->vTimeForSpeed,FALSE,fExpiredTimerGoTo);
					}
					pDataGoTo->vTime = vTimerGetElapsed + pDataGoTo->vR * pDataGoTo->vTimeForSpeed;
					pDataGoTo->vBoolTimer = TRUE;
					pDataGoTo->vCount++;
				}
				pDataUnit->Permission = TRUE;
				pDataUnit->PermissionMove = TRUE;
                if (pDataGoTo->vAction == ACTION_GO)
				{
					/*if (vFlagTest < 100)
					{
						vTextTag2[vFlagTest] = CreateTextTag();
						SetTextTagText(vTextTag2[vFlagTest], "P", 0.013);
						SetTextTagPos(vTextTag2[vFlagTest], GetLocationX(hLocationCenterCellMap[pDataGoTo->vInt2]), GetLocationY(hLocationCenterCellMap[pDataGoTo->vInt2]), 0);
						SetTextTagColor(vTextTag2[vFlagTest], 255, 255, 255, 255);
						vFlagTest++;
					}*/
					//fIssuePointOrderToXY(pDataGoTo->vUnit,pDataGoTo->vActionStr,pDataGoTo->vX4,pDataGoTo->vY4);
					//IssuePointOrder(pDataGoTo->vUnit,pDataGoTo->vActionStr,GetLocationX(hLocationMap[pDataGoTo->vInt2])+16,GetLocationY(hLocationMap[pDataGoTo->vInt2])-16);
					//IssuePointOrderLoc(pDataGoTo->vUnit,pDataGoTo->vActionStr,hLocationCenterCellMap[pDataGoTo->vInt2]);
					fIssuePointOrderToRect(pDataGoTo->vUnit,pDataGoTo->vActionStr,&pDataGoTo->vRect5,pDataGoTo->vRadius_Unit);
				}
				if (pDataGoTo->vAction == ACTION_BLINK)
				{
					//fLog(0,"0");
					//fLog(400,"400");
					pDataGoTo->vTimeForBlink = vTimerGetElapsed;
					IssuePointOrderLoc(pDataGoTo->vUnit,pDataGoTo->vActionStr,hLocationMap[pDataGoTo->vInt2]);
				}
				if (pDataGoTo->vCurrentIntFill-JASS_M1 >= pDataGoTo->vR2)
				{
					pDataGoTo->vR = pDataGoTo->vR2;
				}
				else
				{
					if (pDataGoTo->vAction == ACTION_GO)
					{
						pDataGoTo->vR = pDataGoTo->vCurrentIntFill-(JASS_M1+1);
					}
					if (pDataGoTo->vAction == ACTION_BLINK)
					{
						pDataGoTo->vR = pDataGoTo->vCurrentIntFill-JASS_M1;
					}
				}
				pDataGoTo->vFlag = 0;
				pDataGoTo->vCount = 0;
			}
            else
			{
				if (pDataGoTo->vFlagDT == TRUE)
				{
					goto m1;
				}
				pDataGoTo->vCount++;
				if (pDataGoTo->vR > 0) pDataGoTo->vR--;
				if (pDataGoTo->vR <= 0)
				{
					if (pDataGoTo->vFlag == 0)
					{
						pDataGoTo->vR = 1;
						pDataGoTo->vFlag = 1;
					}
					else
					{
						pDataGoTo->vFlagDT = 1;
						goto m1;
					}
				}
				goto m2;
			}
		}
		else
		{
			//pDataGoTo->vCurrentIntFill = fGetCellInFillMap(pDataGoTo->pFillMap,pDataGoTo->vX3,pDataGoTo->vY3);
			//if (pDataGoTo->vCurrentIntFill == JASS_M1)
			//{
				/*if (pDataGoTo->vRect4.vX1 == pDataGoTo->vRect5.vX1 &&
					pDataGoTo->vRect4.vY1 == pDataGoTo->vRect5.vY1 &&
					pDataGoTo->vRect4.vX2 == pDataGoTo->vRect5.vX2 &&
					pDataGoTo->vRect4.vY2 == pDataGoTo->vRect5.vY2)
				{*/
					//fActionUnitFinished(pDataGoTo->vIdDataUnit);
					//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "777" );
					//pDataGoTo->vFlagDT = TRUE;
					//goto m1;
				//}
			//}
			if (pDataGoTo->vNextIntFill == JASS_M1 &&
				pDataGoTo->vAction == ACTION_BLINK &&
				/*fG(pDataUnit->LastCoordinates.X,
				   pDataUnit->LastCoordinates.Y,
				   fConvertIToRX(pDataGoTo->vInt2),
				   fConvertIToRY(pDataGoTo->vInt2)) < 51 &&
				   1*/
				abs((double)pDataGoTo->vRect4.vX1-(double)pDataGoTo->vRect5.vX1) <= 1 &&
				abs((double)pDataGoTo->vRect4.vY1-(double)pDataGoTo->vRect4.vY1) <= 1)
			{
				//fFinishActionUnit(pDataUnit->vIdDataUnit);
				pDataUnit->vSyncClean = TRUE;
				pDataUnit->vActionN = ACTION_N_NEW;
				//pDataUnit->vIntForGoTo = fJumpBack(pDataUnit);
				pDataUnit->vAction = ACTION_GO;
			}
			pDataGoTo->vCount++;
			if ((pDataGoTo->vCount > pDataGoTo->vR2 * 0.1 && pDataGoTo->vAction == ACTION_BLINK) ||
				(pDataGoTo->vCount > pDataGoTo->vR2 * 10 && pDataGoTo->vAction == ACTION_GO) ||
				(pDataGoTo->vNextIntFill == JASS_M1 && pDataGoTo->vAction == ACTION_BLINK &&
				pDataGoTo->vCount > 10))//10)
			{
				pDataGoTo->vFlagDT = TRUE;
			}
		}
	}
	else
	{
		pDataGoTo->vFlagDT = TRUE;
	}
m1:
	if (pDataGoTo->vFlagDT == TRUE)
	{
		if (pDataGoTo->vBoolTimer == TRUE)
		{
			//if (vEndGame2 == 0)
			//{
			//	if (pDataGoTo->vTimer > 0)
			//	{
			//		DestroyTimer(pDataGoTo->vTimer);
			//	}
			//}
			pDataGoTo->vBoolTimer = FALSE;
		}
		//SaveInteger(vHT,pDataGoTo->hTrigger,CONST_ACTION_GO_TO,0);
		//fFreeTrigger(pDataGoTo->hTrigger);
		//pDataGoTo->hTrigger = NULL;
		//SaveInteger(vHT,pDataGoTo->hTimer,CONST_ACTION_GO_TO,0);
		//if (pDataGoTo->hTimer > 0)
		//{
		//	DestroyTimer(pDataGoTo->hTimer);
		//}
		//pDataGoTo->hTimer = NULL;
		fDelDataGoTo(pDataGoTo,0);
		pDataGoTo->IdetPoPuti = FALSE;
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, "We 222" );
	}
}

integer fUnitAttack(unsigned short vIdDataUnit, unsigned short vIdDataUnit2)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	DataUnit *pDataUnit2;
	pDataUnit2 = fGetDataUnit(vIdDataUnit2);
	if (pDataUnit &&
		GetUnitTypeId(pDataUnit->WhichUnit) != 0 &&
		pDataUnit2 &&
		GetUnitTypeId(pDataUnit2->WhichUnit) != 0)
	{
		if (IssueTargetOrderById(pDataUnit->WhichUnit,
								 ID_ORDER_ATTACK,
								 pDataUnit2->WhichUnit) == FALSE)
		{
			return 0;
		};
		pDataUnit->vAction = ACTION_ATTACK;
		return 1;
	}
	return 0;
}

integer fBuild(unsigned short vIdDataUnit, integer vInt, integer vIdBuild, real *vX, real *vY, real *vRadius)
{
	if (vIdBuild <= 0) return 0;
	DataUnit *pDataUnit;
	unsigned short vX2, vY2;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		//pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
		//pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
		*vRadius = fRadius_Unit(vIdBuild);
		vX2 = fConvertIToX(vInt);
		vY2 = fConvertIToY(vInt);
		*vX = fGetMinIX2RX(vX2) + *vRadius;
		*vY = fGetMinIY2RY(vY2) - *vRadius;
		IssueImmediateOrderById(pDataUnit->WhichUnit,0);
		if (IssueBuildOrderById(pDataUnit->WhichUnit,vIdBuild,*vX,*vY) == FALSE)
		{
			return 0;
		};
		pDataUnit->vAction = ACTION_BUILD;
		pDataUnit->vIdBuild = vIdBuild;
		return 1;
	}
	return 0;
}

bool fIsBuild(unsigned short vIdDataUnit, integer vInt, integer vIdBuild)
{
	if (vIdBuild <= 0) return 0;
	DataUnit *pDataUnit;
	real vX,vY;
	real vRadius;
	unsigned short vX2, vY2;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		vRadius = fRadius_Unit(vIdBuild);
		vX2 = fConvertIToX(vInt);
		vY2 = fConvertIToY(vInt);
		vX = fGetMinIX2RX(vX2) + vRadius;
		vY = fGetMinIY2RY(vY2) - vRadius;
		if (IssueBuildOrderById(pDataUnit->WhichUnit,vIdBuild,vX,vY) == FALSE)
		{
			return FALSE;
		}
		else
		{
			IssueImmediateOrderById(pDataUnit->WhichUnit,0);
			return TRUE;
		}
		
	}
	return FALSE;
}

void fExpiredTimerGoTo(DataGoTo* pDataGoTo, real vTimerGetElapsed)
{
	//HTIMER vTimer1 = GetExpiredTimer();
	//DataGoTo *pDataGoTo = fGetDataGoToBaseTimer(vTimer1);
	//if (vTimer1 == NULL) return;
	//DataGoTo *pDataGoTo = (DataGoTo *)LoadInteger(vHT,vTimer1,CONST_EXPIRED_TIMER_GO_TO);
	//sprintf(vTest,"pDataGoTo 2 = %d",(int)pDataGoTo );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	if (pDataGoTo == 0) 
	{
		//if (vEndGame2 == 0)
		//{
		//	if (vTimer1 > 0)
		//	{
		//		DestroyTimer(vTimer1);
		//	}
		//}
		return;
	}
	//sprintf(vTest,"pDataGoTo->vBoolTimer 2 = %d",pDataGoTo->vBoolTimer );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	if (pDataGoTo->vBoolTimer == TRUE)
	{
		if (vTimerGetElapsed - pDataGoTo->vTime < 0) return;
		//if (vEndGame2 == 0)
		//{
		//	if (pDataGoTo->vTimer > 0)
		//	{
		//		DestroyTimer(pDataGoTo->vTimer);
		//	}
		//}
		pDataGoTo->vBoolTimer = FALSE;
		pDataGoTo->IdetPoPuti = FALSE;
		pDataGoTo->vCelNeDostignuta = TRUE;
		//fFreeActionUnit(pDataGoTo->vIdDataUnit);
		//sprintf(vTest,"pDataGoTo->vCelNeDostignuta = %d",pDataGoTo->vCelNeDostignuta );
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	}
}

unsigned int fGetINext (unsigned short *pFillMap, unsigned short vNextIntFill, unsigned short vDiff)
{
    unsigned short vInt;
	int vCount;
	int vvz;
	vCount = 0;
    for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
        vInt = pFillMap[vvi];
        if (vInt == vNextIntFill)
		{
			return vvi + (vDiff / 2) * (vW3 + 1);
			/*sprintf(vTest,"vvi = %d",vvi );
			DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );*/
			/*for (int vvj = 0; vvj < MAX_RADIUS_UNIT; vvj++)
			{
				vvz = vvi + (vW3 + 1) * (vvj + 1);
				if (vvz < vSizeMap)
				{
					vInt = pFillMap[vvz];
					if (vInt != vNextIntFill)
					{
						sprintf(vTest,"vvj = %d",vvj );
						DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						sprintf(vTest,"(unsigned int)(vvi + (vW3 + 1) * ((vvj+1)/2))");
						DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
						return (unsigned int)(vvi + (vW3 + 1) * ((vvj+1)/2));
					}
				}
			}*/
			
		} 
	}
    return JASS_UNKNOWN;
}

bool fGetINext (unsigned short *pFillMap, sRect2 *vRect1, sRect2 *vRect2, unsigned short vNextIntFill, unsigned int vR, unsigned short vDiff)
{

	unsigned short vInt;
	int vvi, vvh;
    int vX;
    int vY;

	int vQ;
	double vStepRad;

	sRect2 vRect;
	
	vStepRad = vPI / (vR);// + 1);
	vQ = (4 * vPI) / vStepRad;

	for (vvi = 0; vvi < vQ - 1; vvi++)
	{
		vRect.vX1 = vRect1->vX1 + (unsigned short)(cos(vvi*vStepRad) * (vR));
		vRect.vY1 = vRect1->vY1 + (unsigned short)(sin(vvi*vStepRad + vPI/2) * (vR));
		vRect.vX2 = vRect.vX1 + vDiff - 1;
		vRect.vY2 = vRect.vY1 + vDiff - 1;
		
		if (vRect.vX2 - vRect.vX1 < 512 &&
			vRect.vY2 - vRect.vY1 < 512 &&
			vRect.vX2 != 0 &&
			vRect.vY2 != 0)
		{
			vInt = fGetCurrentIntFill(pFillMap,&vRect);
			if (vInt == vNextIntFill)
			{
				goto m2;
			}
		}
	}

	for (vvh = 0; vvh < 8; vvh++)
	{
		switch(vvh)
		{
			case 7:
				vX = -1; vY = 0;//L
				break;
			case 6:
				vX = 0; vY = -1;//T
				break;
			case 5:
				vX = 1; vY = 0;//R
				break;
			case 4:
				vX = 0; vY = 1;//B
				break;
			case 3:
				vX = -1; vY = -1;//TL
				break;
			case 2:
				vX = 1; vY = -1;//TR
				break;
			case 1:
				vX = -1; vY = 1;//BL
				break;
			case 0:
				vX = 1; vY = 1;//BR
				break;
			default:
				return 0;
		}
		if (vvh >= 0 && vvh <= 7)
		{
			vRect.vX1 = vRect1->vX1 + vX * (vR);
			vRect.vY1 = vRect1->vY1 + vY * (vR);
			vRect.vX2 = vRect.vX1 + vDiff - 1;
			vRect.vY2 = vRect.vY1 + vDiff - 1;
		
			vInt = fGetCurrentIntFill(pFillMap,&vRect);
			if (vInt == vNextIntFill)
			{
				goto m2;
			}
		}
	}
    return FALSE;
m2:
	vRect2->vX1 = vRect.vX1;
	vRect2->vY1 = vRect.vY1;
	vRect2->vX2 = vRect.vX2;
	vRect2->vY2 = vRect.vY2;
	return TRUE;
}

integer fActionUnitFinished(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		pDataUnit->vActionFinish = 1;
		return 1;
	}
	return 0;
}

integer fGetActionUnitFinish(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		return pDataUnit->vActionFinish;
	}
	return -1;
}

integer fGetActionUnit(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		return pDataUnit->vAction;
	}
	return -1;
}

integer fFreeActionUnit(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		/*if (pDataUnit->vNPlayer >= 10 &&
			pDataUnit->vNPlayer <= 11)
		{
			pDataUnit->vNPlayer = pDataUnit->vNPlayer;
		}*/
		pDataUnit->vAction = ACTION_WAITING;
		pDataUnit->vActionN = 0;
		pDataUnit->vCountActions = 0;
		pDataUnit->vActionFinish = 0;
		pDataUnit->vSyncClean = TRUE;
		if (pDataUnit->vPosRepair > 0 &&
			//pDataUnit->vHelpBase &&
			pDataUnit->vHelping == TRUE &&
			pDataUnit->vStageHelp != 4 &&
			pDataUnit->vStageHelp != 6)
		{
			fFreePosRepair(pDataUnit);
		}
		//pDataUnit->vBlockNewBlink = FALSE;
		return 1;
	}
	return 0;
}

unsigned short fQPosRepairMainHuman(sBase *vBase)
{
	if (vBase == NULL) return 0;
	vBase->vQ = 0;
	for (vBase->vvi = 0; vBase->vvi < vBase->MAP_REPAIR_POS; vBase->vvi++)
	{
		if (vBase->vRepairPos[vBase->vvi].vBind == TRUE &&
			vBase->vRepairPos[vBase->vvi].pHumanPlayer &&
			vBase->vRepairPos[vBase->vvi].pHumanPlayer->vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit &&
			(vBase->vRepairPos[vBase->vvi].pHumanPlayer->vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit->vStageHelp == 6 ||
			vBase->vRepairPos[vBase->vvi].pHumanPlayer->vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].pDataUnit->vStageHelp == 11))
		{
			vBase->vQ++;
		}
	}
	return vBase->vQ;
}

bool fFreePosRepair(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return FALSE;
	int vvi;
	for (vvi = 0; vvi < vCountHumans; vvi++)
	{
		if (pHumanPlayerGlobal[vvi].vEndGame == FALSE &&
			pHumanPlayerGlobal[vvi].vParamPlayer.vUnit[COMMENT_MAIN_HUMAN].vIdForComments == COMMENT_MAIN_HUMAN)
		{
			vMapBases.fFreePosRepairBind(&pHumanPlayerGlobal[vvi].vBase,pDataUnit->vPosRepair);
		}
	}
	pDataUnit->vPosRepair = 0;
	return TRUE;
}

integer fFinishActionUnit(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		/*if (pDataUnit->vNPlayer >= 10 &&
			pDataUnit->vNPlayer <= 11)
		{
			pDataUnit->vNPlayer = pDataUnit->vNPlayer;
		}*/
		pDataUnit->vAction = ACTION_WAITING;
		pDataUnit->vActionN = 0;
		pDataUnit->vCountActions = 0;
		pDataUnit->vActionFinish = 1;
		pDataUnit->PermissionMove = FALSE;
		pDataUnit->Permission = FALSE;
		pDataUnit->vSyncClean = TRUE;
		//pDataUnit->vBlockNewBlink = FALSE;
		return 1;
	}
	return 0;
}

/*integer fGetObstacle(unsigned short vIdDataUnit)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		return pDataUnit->vObstacle;
	}
	return -1;
}

integer fSetObstacle(unsigned short vIdDataUnit, unsigned short vObstacle)
{
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		pDataUnit->vObstacle = vObstacle;
		return 1;
	}
	return 0;
}*/

void fTimerForMoveUnit()
{
	if (vEndGame2 == 1)
	{
		return;
	}
	HTIMER vTimer = GetExpiredTimer();
	if (vWin == 1)
	{
		if (vTimer > 0)
		{
			DestroyTimer(vTimer);
		}
		return;
	}
	DataUnit *pDataUnit;
	//pDataUnit = fGetDataUnit(t);
	if (vTimer == NULL) return;
	pDataUnit = (DataUnit *)LoadInteger(vHT,vTimer,CONST_TIMER_FOR_MOVE_UNIT);
	/*if (pDataUnit == NULL)
	{
		if (vTimer > 0)
		{
			//DestroyTimer(vTimer);
		}
	}*/
	if (pDataUnit)
	{
		pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
		pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
		
		pDataUnit->vIdOrder = GetUnitCurrentOrder(pDataUnit->WhichUnit);
		if (pDataUnit->vIdOrder != ID_ORDER_MOVE &&
			pDataUnit->vIdOrder != ID_ORDER_SMART &&
			pDataUnit->vIdOrder != ID_ORDER_BLINK &&
			pDataUnit->vIdOrder != ID_ORDER_HARVEST &&
			pDataUnit->vIdOrder != ID_ORDER_RESUMEHARVESTING)
		{
			if (pDataUnit->Move == TRUE)
			//if (1)
			{
				if (vEndGame2 == 0)
				{
					if (pDataUnit->vTimerMove > 0)
					{
						DestroyTimer(pDataUnit->vTimerMove);
						SaveInteger(vHT,pDataUnit->vTimerMove,CONST_TIMER_FOR_MOVE_UNIT,0);
					}
				}
				pDataUnit->vTimerMove = NULL;
			}
			pDataUnit->Move = FALSE;
			pDataUnit->Harvest = FALSE;
		}
	}
}

void fActionUnitMove()
{
	HUNIT vUnit = GetTriggerUnit();
	if (vUnit == 0) return;
	/*if (fIsPlayerAI(GetPlayerColor(GetOwningPlayer(vUnit))))
	{
		Integer vIdOrder;
		vIdOrder = GetUnitCurrentOrder(vUnit);
		if (vIdOrder == OrderId("harvest") || vIdOrder == OrderId("resumeharvesting"))
		{
			return;
		}
	}*/
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit)
	{
		//Integer vIdOrder;
		pDataUnit->vIdOrder = GetUnitCurrentOrder(pDataUnit->WhichUnit);
		if (pDataUnit->vIdOrder == ID_ORDER_HARVEST ||
			pDataUnit->vIdOrder == ID_ORDER_RESUMEHARVESTING)
		{
			return;
		}
		//sprintf(vTest,"Order = %s",OrderId2String(vIdOrder));
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		pDataUnit->LastCoordinates.X = GetUnitX(vUnit);
		pDataUnit->LastCoordinates.Y = GetUnitY(vUnit);
		if (pDataUnit->Move == TRUE)
		//if (1)
		{
			if (vEndGame2 == 0)
			{
				if (pDataUnit->vTimerMove > 0)
				{
					DestroyTimer(pDataUnit->vTimerMove);
					SaveInteger(vHT,pDataUnit->vTimerMove,CONST_TIMER_FOR_MOVE_UNIT,0);
				}
			}
			pDataUnit->vTimerMove = NULL;
		}
		pDataUnit->Move = TRUE;
		pDataUnit->vTimerMove = CreateTimer();
		SaveInteger(vHT,pDataUnit->vTimerMove,CONST_TIMER_FOR_MOVE_UNIT,(integer)pDataUnit);
		TimerStart(pDataUnit->vTimerMove, 0.5, TRUE, &fTimerForMoveUnit);
	}
}

void fTimerForAttackedUnit()
{
	if (vEndGame2 == 1)
	{
		return;
	}
	HTIMER vTimer = GetExpiredTimer();
	if (vWin == 1)
	{
		if (vTimer > 0)
		{
			DestroyTimer(vTimer);
		}
		return;
	}
	DataUnit *pDataUnit;
	DataUnit *pDataUnit2;
	Integer vIdOrder;
	//pDataUnit = fGetDataUnit(t);
	if (vTimer == NULL) return;
	pDataUnit = (DataUnit *)LoadInteger(vHT,vTimer,CONST_TIMER_FOR_ATTACKED_UNIT);
	pDataUnit2 = (DataUnit *)LoadInteger(vHT,vTimer,CONST_TIMER_FOR_ATTACKED_UNIT_2);
	/*if (pDataUnit2 == NULL &&
		pDataUnit == NULL)
	{
		if (vTimer > 0)
		{
			vTimer = vTimer;
			//DestroyTimer(vTimer);
		}
	}*/
	if (pDataUnit2 && (unsigned int)pDataUnit2->vName != 0xfeeefeee)
	{
		pDataUnit2->Atakovan = FALSE;
	}
	if (pDataUnit && (unsigned int)pDataUnit->vName != 0xfeeefeee)
	{
		if (pDataUnit->Zdanie == FALSE)
		{
			pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
			pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
		}
		vIdOrder = GetUnitCurrentOrder(pDataUnit->WhichUnit);
		//sprintf(vTest,"Order = %s",OrderId2String(vIdOrder));
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		if (vIdOrder != ID_ORDER_ATTACK)// && vIdOrder != OrderId("smart"))
		{
			if (pDataUnit->Atakuet == TRUE)
			//if (1)
			{
				if (vEndGame2 == 0)
				{
					if (pDataUnit->vTimerAttacked > 0)
					{
						DestroyTimer(pDataUnit->vTimerAttacked);
						SaveInteger(vHT,pDataUnit->vTimerAttacked,CONST_TIMER_FOR_ATTACKED_UNIT,0);
					}
				}
				pDataUnit->vTimerAttacked = NULL;
			}
			pDataUnit->Atakuet = FALSE;
			pDataUnit->TargetUnit = NULL;
			pDataUnit->Harvest = FALSE;
		}
	}
}

void fActionUnitAttacked()
{
	HUNIT vUnit = GetAttacker();
	HUNIT vUnit2 = GetTriggerUnit();
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	if (vUnit == NULL || vUnit2 == NULL) return;
	//if (u == NULL) return;
	DataUnit *pDataUnit;
	DataUnit *pDataUnit2;
	pDataUnit = fGetDataUnit(vUnit);
	pDataUnit2 = fGetDataUnit(vUnit2);
	if (pDataUnit && pDataUnit2)
	{
		/*if (pDataUnit->vStorona == HUMANS &&
			pDataUnit2->vStorona == HUMANS &&
			pDataUnit->vNPlayer == pDataUnit2->vNPlayer &&
			pDataUnit2->vStorona == HUMANS &&
			fIsMainHuman(pDataUnit->vId))
		{
			//fDelDataUnit(pDataUnit2->vIdDataUnit);
		}*/
		//pDataUnit->TargetUnit = GetOrderTargetUnit();
		if (pDataUnit->Atakuet == TRUE)
		//if (1)
		{
			if (vEndGame2 == 0)
			{
				if (pDataUnit->vTimerAttacked > 0)
				{
					DestroyTimer(pDataUnit->vTimerAttacked);
					SaveInteger(vHT,pDataUnit->vTimerAttacked,CONST_TIMER_FOR_ATTACKED_UNIT,0);
					SaveInteger(vHT,pDataUnit->vTimerAttacked,CONST_TIMER_FOR_ATTACKED_UNIT_2,0);
				}
			}
			pDataUnit->vTimerAttacked = NULL;
		}
		pDataUnit->Atakuet = FALSE;
		/*if (!fIsVampire(pDataUnit->vId) &&
			pDataUnit->vId != ID_UNIT_TINI &&
			pDataUnit->vId != ID_UNIT_GRIVE)
		{
			return;
		}
		if (!fIsMainHuman(pDataUnit2->vId) &&
			pDataUnit->vId != ID_UNIT_WORKER)
		{
			return;
		}*/
		if ((GetUnitState(pDataUnit2->WhichUnit, UNIT_STATE_MAX_LIFE) - GetUnitState(pDataUnit2->WhichUnit, UNIT_STATE_LIFE)) <= 1) return;
		pDataUnit2->Atakovan = TRUE;
		if (pDataUnit->TargetUnit != pDataUnit2->WhichUnit)
		{
			pDataUnit->TargetUnit = pDataUnit2->WhichUnit;
		}
		if (pDataUnit->Zdanie == FALSE)
		{
			pDataUnit->LastCoordinates.X = GetUnitX(vUnit);
			pDataUnit->LastCoordinates.Y = GetUnitY(vUnit);
		}
		pDataUnit->Atakuet = TRUE;
		pDataUnit->vTimerAttacked = CreateTimer();
		SaveInteger(vHT,pDataUnit->vTimerAttacked,CONST_TIMER_FOR_ATTACKED_UNIT,(integer)pDataUnit);
		SaveInteger(vHT,pDataUnit->vTimerAttacked,CONST_TIMER_FOR_ATTACKED_UNIT_2,(integer)pDataUnit2);
		TimerStart(pDataUnit->vTimerAttacked, 1, FALSE, &fTimerForAttackedUnit);
	}
}

void fCast(HEVENTID vEventId)
{
	HUNIT vUnit;
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	integer vGetSpellAbilityId;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit)
	{
		vGetSpellAbilityId = GetSpellAbilityId();
		/*if (vIdOrder == ID_ORDER_FARSIGHT)
		{
			sprintf(vTest,"Order = %s",OrderId2String(vIdOrder));
			DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			sprintf(vTest,"vIdOrder = %d",vIdOrder);
			DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		}*/
		if (vGetSpellAbilityId == ID_SKILL_DETONATE)
		{
			fDelDataUnit(pDataUnit->vIdDataUnit);
			return;
		}
		else
		if (pDataUnit->Permission == FALSE && vGetSpellAbilityId != ID_SKILL_BLINK && vGetSpellAbilityId != ID_SKILL_MAIN_HUMAN_BLINK)
		{
			switch(pDataUnit->vId)
			{
			case ID_UNIT_MAIN_HUMAN1:
			case ID_UNIT_MAIN_HUMAN2:
			case ID_UNIT_URSA:
				IssueImmediateOrderById(pDataUnit->WhichUnit,ID_ORDER_STOP);
				//fFinishActionUnit(pDataUnit->vIdDataUnit);
				break;
			case ID_UNIT_VAMPIRE1:
			case ID_UNIT_VAMPIRE2:
			case ID_UNIT_VAMPIRE3:
				IssueImmediateOrderById(pDataUnit->WhichUnit,ID_ORDER_STOP);
				break;
			}
		}
		/*if (pDataUnit->Permission == TRUE && vEventId == EVENT_UNIT_SPELL_CAST &&
			pDataUnit->vHelping == TRUE &&
			vGetSpellAbilityId == ID_SKILL_REPAIR &&
			(fIsMainHuman(pDataUnit->vId) == TRUE ||
			pDataUnit->vId == ID_UNIT_URSA) &&
			fIsWall(GetUnitTypeId(pDataUnit->TargetUnit)) == FALSE
			)
		{
			IssueImmediateOrderById(pDataUnit->WhichUnit,ID_ORDER_STOP);
		}*/
		if (pDataUnit->Permission == TRUE && vEventId == EVENT_UNIT_SPELL_CAST &&
			vGetSpellAbilityId != ID_SKILL_BLINK &&
			vGetSpellAbilityId != ID_SKILL_MAIN_HUMAN_BLINK &&
			vGetSpellAbilityId != ID_SKILL_MANABURN &&
			vGetSpellAbilityId != ID_SKILL_REPAIR)//&& vIdOrder == ID_ORDER_MANABURN)
		{
			switch(pDataUnit->vId)
			{
			case ID_UNIT_MAIN_HUMAN1:
			case ID_UNIT_MAIN_HUMAN2:
				//IssueImmediateOrderById(pDataUnit->WhichUnit,ID_ORDER_STOP);
				fFinishActionUnit(pDataUnit->vIdDataUnit);
				break;
			}
		}
	}
}

bool fIsTwoVampires()
{
	if (vDataUnits[10 + 0].pDataUnit &&
		vDataUnits[10 + 1].pDataUnit)
	{
		return TRUE;
	}
	return FALSE;
}

void fItem()
{
	sprintf(vBuf,"fItem() N_1\0");
	fLog(999,vBuf);
	HITEM vItem;
	integer vIdItem;
	vItem = GetManipulatedItem();
	vIdItem = GetItemTypeId(vItem);
	sprintf(vBuf,"fItem() N_2\0");
	fLog(999,vBuf);
	if (vIdItem == ID_ITEM_GOLD1 || vIdItem == ID_ITEM_GOLD3)
	{
		RemoveItem(vItem);
		sprintf(vBuf,"fItem() N_3\0");
		fLog(999,vBuf);
	}
	else
	{
		sprintf(vBuf,"fItem() N_4\0");
		fLog(999,vBuf);
		HUNIT vUnit;
		vUnit = GetTriggerUnit();
		if (vUnit == NULL) return;
		DataUnit *pDataUnit;
		pDataUnit = fGetDataUnit(vUnit);
		if (pDataUnit)
		{
			pDataUnit->vPickupItem = TRUE;
		}
		sprintf(vBuf,"fItem() N_5\0");
		fLog(999,vBuf);
	}
}

void fSettingUnit(HEVENTID vEventId)
{
	HUNIT vUnit;
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	//if (u == NULL) return;
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit)
	{
		switch(vEventId)
		{
		//case EVENT_UNIT_CONSTRUCT_FINISH:
	    case EVENT_UNIT_UPGRADE_FINISH:
			//pDataUnit->vActionFinish = 1;
			if (pDataUnit->Postroeno == FALSE)
			{
				pDataUnit->fGetPostroeno();
			}
			if (pDataUnit->Postroeno == TRUE)
			{
				fFinishActionUnit(pDataUnit->vIdDataUnit);
				pDataUnit->Upgrade = FALSE;
				pDataUnit->Upgrade2 = FALSE;
				if (pDataUnit->vIdTrain <= 0)
				{
					pDataUnit->vIdTrain = pDataUnit->vIdTrain;
				}
				pDataUnit->vId = pDataUnit->vIdTrain;
				pDataUnit->vIdTrain = 0;
				sprintf(pDataUnit->vName,"%s\0",GetUnitName(pDataUnit->WhichUnit));
				//sprintf(vBuf,"Upgrade Complite: %s - %f\0",pDataUnit->vName,TimerGetElapsed(vTimerGame)-pDataUnit->vTimeStartTrain);
				//fLog(96,vBuf);
			}
			else
			{
				pDataUnit->Upgrade = pDataUnit->Upgrade;
			}
			/*real v1;
			real v2;
			v1 = GetUnitState(pDataUnit->WhichUnit, UNIT_STATE_MAX_LIFE);
			v2 = GetUnitState(pDataUnit->WhichUnit, UNIT_STATE_LIFE);*/
			/*if (pDataUnit->Postroeno == FALSE &&
				(GetUnitState(pDataUnit->WhichUnit, UNIT_STATE_MAX_LIFE) -
				GetUnitState(pDataUnit->WhichUnit, UNIT_STATE_LIFE) <= 1))
			{
				pDataUnit->Postroeno = TRUE;
				pDataUnit->Upgrade = TRUE;
			}*/
			break;
		case EVENT_UNIT_UPGRADE_START:
			if (pDataUnit->Postroeno == FALSE)
			{
				pDataUnit->fGetPostroeno();
			}
			if (pDataUnit->Postroeno == TRUE)
			{
				//pDataUnit->vActionFinish = 0;
				pDataUnit->Upgrade = TRUE;
				pDataUnit->Upgrade2 = TRUE;
				pDataUnit->vIdTrain = GetUnitTypeId(pDataUnit->WhichUnit);
				if (pDataUnit->vIdTrain <= 0)
				{
					pDataUnit->vIdTrain = pDataUnit->vIdTrain;
				}
				fGetTimeCompliteTrain(pDataUnit->vIdTrain,&pDataUnit->vTimeCompliteTrain);
				pDataUnit->vTimeStartTrain = TimerGetElapsed(vTimerGame);
				pDataUnit->vTimeEndTrain = pDataUnit->vTimeStartTrain + pDataUnit->vTimeCompliteTrain;
				//sprintf(vTest,"UNIT_UPGRADE vId = %d",pDataUnit->vId);
				//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			}
			else
			{
				pDataUnit->Upgrade = FALSE;
			}
			break;
		case EVENT_UNIT_CONSTRUCT_CANCEL:
			break;
		case EVENT_UNIT_UPGRADE_CANCEL:
			pDataUnit->Upgrade = FALSE;
			pDataUnit->Upgrade2 = FALSE;
			pDataUnit->vIdTrain = 0;
			break;
		/*case EVENT_UNIT_RESEARCH_START:
			pDataUnit->Researched = FALSE;
			pDataUnit->Researching = TRUE;
			pDataUnit->vIdResearch = GetResearched();
			break;*/
		/*case EVENT_UNIT_RESEARCH_FINISH:
			pDataUnit->Researched = TRUE;
			pDataUnit->Researching = FALSE;*/
			//sprintf(vTest,"RESEARCH vId = %d",pDataUnit->vIdResearch);
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			//break;
		case EVENT_UNIT_RESEARCH_CANCEL:
			pDataUnit->Researched = FALSE;
			pDataUnit->Researching = FALSE;
			break;
		case EVENT_UNIT_HERO_LEVEL:
			pDataUnit->Lvl = TRUE;
			break;
		case EVENT_UNIT_HERO_SKILL:
			//sprintf(vTest,"SKILL vId = %d",GetLearnedSkill());
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			break;
		case EVENT_UNIT_SPELL_FINISH:
			if (pDataUnit->vAI == TRUE)
			{
				pDataUnit->Permission = FALSE;
			}
			break;
		/*case EVENT_UNIT_TRAIN_START:
			if (fIsInfernal(pDataUnit->vId))
			{
				pDataUnit->Postroeno = FALSE;
			}
			break;*/
		//case EVENT_UNIT_TRAIN_FINISH:

			/*if (fIsInfernal(pDataUnit->vId))
			{
				pDataUnit->Postroeno = TRUE;
				pDataUnit->vId = GetUnitTypeId(pDataUnit->WhichUnit);
				sprintf(vTest,"UNIT_TRAIN_FINISH vId = %d",pDataUnit->vId);
				DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			}*/
			//break;
		default:
			//sprintf(vTest,"UNKNOWN");
			//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
			break;
		}
	}
}

void fStopMove()
{
	HUNIT vUnit;
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	HUNIT vUnit2;
	vUnit2 = GetOrderTarget();
	if (vUnit2 == NULL) return;
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit)
	{
		/*if (IsUnitEnemy(vUnit2,pDataUnit->vPlayer) != TRUE)//þíèò ñîþçíèê
		{
			DataUnit *pDataUnit2;
			pDataUnit2 = fGetDataUnit(vUnit2);
			if (pDataUnit2 && !(pDataUnit2->vId == ID_UNIT_BUILD_DOM &&
				fRemotenessBase(pDataUnit2) <= 0))//åñëè íå äîìèê íà íóëåâîé áàçå
			{
				return;
			}
		}*/
		Integer vIdOrder;
		Integer vId;
		vId = GetUnitTypeId(pDataUnit->WhichUnit);
		if (vId == 0)
		{
			return;
		}
		vIdOrder = GetUnitCurrentOrder(pDataUnit->WhichUnit);
		if (pDataUnit->PermissionMove == FALSE && vIdOrder != ID_ORDER_BLINK)// && vIdOrder != ID_ORDER_MANABURN)
		{
			switch(pDataUnit->vId)
			{
			case ID_UNIT_MAIN_HUMAN1:
			case ID_UNIT_MAIN_HUMAN2:
			case ID_UNIT_TINI:
			case ID_UNIT_URSA:
				IssueImmediateOrderById(pDataUnit->WhichUnit,ID_ORDER_STOP);
				IssuePointOrderById(pDataUnit->WhichUnit,ID_ORDER_MOVE,pDataUnit->LastCoordinates.X,pDataUnit->LastCoordinates.Y);
				break;
			}
			return;
		}
	}
}

void fUnitResearchStart()
{
	sprintf(vBuf,"fUnitResearchStart() N_1\0");
	fLog(999,vBuf);
	if (vEndGame2 == 1) return;
	if (vWin == 1) return;
	HUNIT vUnit;
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	//if (u == NULL) return;
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit)
	{
		pDataUnit->Researched = FALSE;
		pDataUnit->Researching = TRUE;
		pDataUnit->vIdResearch = GetResearched();
		fGetTimeCompliteResearch(pDataUnit->vIdResearch,&pDataUnit->vTimeCompliteResearch);
		pDataUnit->vTimeStartResearch = TimerGetElapsed(vTimerGame);
		pDataUnit->vTimeEndResearch = pDataUnit->vTimeStartResearch + pDataUnit->vTimeCompliteResearch;
	}
	else
	{
		//fLog(7,"7");
		fAddAllDataUnitOfPlayer(GetOwningPlayer(vUnit));
	}
}

void fUnitResearchFinish()
{
	sprintf(vBuf,"fUnitResearchFinish() N_1\0");
	fLog(999,vBuf);
	if (vEndGame2 == 1) return;
	if (vWin == 1) return;
	HUNIT vUnit;
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	//if (u == NULL) return;
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit)
	{
		pDataUnit->Researched = TRUE;
		pDataUnit->Researching = FALSE;
		//sprintf(vBuf,"Research Complite: %hu - %f\0",pDataUnit->vNResearch,TimerGetElapsed(vTimerGame)-pDataUnit->vTimeStartResearch);
		//fLog(97,vBuf);
	}
	else
	{
		//fLog(6,"6");
		fAddAllDataUnitOfPlayer(GetOwningPlayer(vUnit));
	}
}

void fUnitConstructFinish()
{
	sprintf(vBuf,"fUnitConstructFinish() N_1\0");
	fLog(999,vBuf);
	if (vEndGame2 == 1) return;
	if (vWin == 1) return;
	HUNIT vUnit;
	HTRIGGER hTrigger;
	hTrigger = GetTriggeringTrigger();
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	DataUnit *pDataUnit;
	DataUnit *pDataUnit2;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	//if (u == NULL) return;
	pDataUnit2 = (DataUnit*) LoadInteger(vHT,hTrigger,CONST_HUNIT_CONSTRUCT_START);
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit &&
		pDataUnit2 &&
		pDataUnit == pDataUnit2)
	{
		fFinishActionUnit(pDataUnit->vIdDataUnit);
		pDataUnit->fGetPostroeno();	//pDataUnit->Postroeno = TRUE;
		//fGetLastCoordinates(pDataUnit);
		//sprintf(vBuf,"Construct Complite: %s - %f\0",pDataUnit->vName,TimerGetElapsed(vTimerGame)-pDataUnit->vTimeStartBuild);
		//fLog(99,vBuf);
	}
	else
	{
		sprintf(vBuf,"fUnitConstructFinish() N_2\0");
		fLog(999,vBuf);
		pDataUnit = pDataUnit2;//fGetLastConstruct();
		if (pDataUnit)
		{
			fFinishActionUnit(pDataUnit->vIdDataUnit);
			pDataUnit->fGetPostroeno();	//pDataUnit->Postroeno = TRUE;
			sprintf(vBuf,"fUnitConstructFinish() N_3\0");
			fLog(999,vBuf);
			RemoveUnit(pDataUnit->WhichUnit);
			sprintf(vBuf,"fUnitConstructFinish() N_4\0");
			fLog(999,vBuf);
			pDataUnit->WhichUnit = vUnit;
			sprintf(pDataUnit->vKeyUnit,"%d\0",pDataUnit->WhichUnit);
			/*if (pDataUnit->vGoTo == TRUE)
			{
				FlushGameCache(pDataUnit->vSyncCache);
				pDataUnit->vSyncCache = InitGameCache(pDataUnit->vKeyUnit);
			}*/
		}
		sprintf(vBuf,"fUnitConstructFinish() N_5\0");
		fLog(999,vBuf);
	}
	sprintf(vBuf,"fUnitConstructFinish() N_6\0");
	fLog(999,vBuf);
}

void fTrainStart()
{
	if (vEndGame2 == 1) return;
	if (vWin == 1) return;
	HUNIT vUnit;
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	DataUnit *pDataUnit;
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit != NULL)
	{
		pDataUnit->vIdTrain = GetTrainedUnitType();
		fGetTimeCompliteTrain(pDataUnit->vIdTrain,&pDataUnit->vTimeCompliteTrain);
		pDataUnit->vTimeStartTrain = TimerGetElapsed(vTimerGame);
		pDataUnit->vTimeEndTrain = pDataUnit->vTimeStartTrain + pDataUnit->vTimeCompliteTrain;
		pDataUnit->Train = TRUE;
	}
}

void fTrainFinish()
{
	if (vEndGame2 == 1) return;
	if (vWin == 1) return;
	HUNIT vUnit;
	vUnit = GetTriggerUnit();
	if (vUnit == NULL) return;
	DataUnit *pDataUnit;
	//unsigned short vOutIdDataUnit;
	//pDataUnit = fGetDataUnit(u,vOutIdDataUnit);
	//if (u == NULL) return;
	pDataUnit = fGetDataUnit(vUnit);
	if (pDataUnit != NULL)
	{
		pDataUnit->Train = FALSE;
		//sprintf(vBuf,"Train Complite: %s - %f\0",pDataUnit->vName,TimerGetElapsed(vTimerGame)-pDataUnit->vTimeStartTrain);
		//fLog(98,vBuf);
	}
}

void fActionUnitAllEvents()
{
	sprintf(vBuf,"fActionUnitAllEvents() N_1\0");
	fLog(999,vBuf);
	try
	{
	if (vEndGame2 == 1) return;
	if (vWin == 1) return;
	HEVENTID vEventId = GetTriggerEventId();
	//integer vIdOrder;
	switch(vEventId)
	{
	case EVENT_UNIT_PICKUP_ITEM:
		sprintf(vBuf,"fActionUnitAllEvents() N_2\0");
		fLog(999,vBuf);
		fItem();
		sprintf(vBuf,"fActionUnitAllEvents() N_3\0");
		fLog(999,vBuf);
		break;
	case EVENT_UNIT_SPELL_CAST:
	case EVENT_UNIT_SPELL_CHANNEL:
		sprintf(vBuf,"fActionUnitAllEvents() N_4\0");
		fLog(999,vBuf);
		fCast(vEventId);
		sprintf(vBuf,"fActionUnitAllEvents() N_5\0");
		fLog(999,vBuf);
		break;
	case EVENT_UNIT_ISSUED_TARGET_ORDER:
		sprintf(vBuf,"fActionUnitAllEvents() N_6\0");
		fLog(999,vBuf);
		fStopMove();
		sprintf(vBuf,"fActionUnitAllEvents() N_7\0");
		fLog(999,vBuf);
		break;
	case EVENT_UNIT_ISSUED_POINT_ORDER:
		//vIdOrder = GetUnitCurrentOrder(GetTriggerUnit());
		//sprintf(vTest,"Order = %s",OrderId2String(vIdOrder));
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		sprintf(vBuf,"fActionUnitAllEvents() N_8\0");
		fLog(999,vBuf);
		fActionUnitMove();
		sprintf(vBuf,"fActionUnitAllEvents() N_9\0");
		fLog(999,vBuf);
		break;
	//case EVENT_UNIT_DAMAGED:
	case EVENT_UNIT_ATTACKED:
		//KillUnit(GetTriggerUnit());
		//sprintf(vTest,"EVENT_UNIT_DAMAGED");
		//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
		sprintf(vBuf,"fActionUnitAllEvents() N_10\0");
		fLog(999,vBuf);
		fActionUnitAttacked();
		sprintf(vBuf,"fActionUnitAllEvents() N_11\0");
		fLog(999,vBuf);
		break;
	case EVENT_UNIT_TRAIN_START:
		sprintf(vBuf,"fActionUnitAllEvents() N_12\0");
		fLog(999,vBuf);
		fTrainStart();
		sprintf(vBuf,"fActionUnitAllEvents() N_13\0");
		fLog(999,vBuf);
		break;
	case EVENT_UNIT_TRAIN_FINISH:
		sprintf(vBuf,"fActionUnitAllEvents() N_14\0");
		fLog(999,vBuf);
		fTrainFinish();
		sprintf(vBuf,"fActionUnitAllEvents() N_15\0");
		fLog(999,vBuf);
		break;
	case EVENT_UNIT_TRAIN_CANCEL:
		sprintf(vBuf,"fActionUnitAllEvents() N_16\0");
		fLog(999,vBuf);
		fTrainFinish();
		sprintf(vBuf,"fActionUnitAllEvents() N_17\0");
		fLog(999,vBuf);
		break;
	default:
		sprintf(vBuf,"fActionUnitAllEvents() N_18\0");
		fLog(999,vBuf);
		fSettingUnit(vEventId);
		sprintf(vBuf,"fActionUnitAllEvents() N_19\0");
		fLog(999,vBuf);
		break;
	}
	}
	catch (const char* error_message)
	{
		//MessageBoxA(NULL,error_message,"Error fActionUnitAllEvents!",MB_OK | MB_ICONWARNING);
		sprintf(vBuf,"fActionUnitAllEvents = %s\0",error_message);
		fLog(66,vBuf);
	}
	sprintf(vBuf,"fActionUnitAllEvents() N_20\0");
	fLog(999,vBuf);
}

integer fFillMap(integer vMap, unsigned short *pFillMap)
{
	char *pMap = (char*)vMap;
	for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
		pFillMap[vvi] = (unsigned short)pMap[vvi];
	}
	return 1;
}

integer fFindIToMap (integer vIntIn)
{
	//sprintf(vTest,"vMapGlobal = %d",vMapGlobal );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	char *pMap = (char*)vMapGlobal;
    integer vInt;
    for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
        vInt = (integer)pMap[vvi];
        if (vInt == vIntIn)
		{
			return vvi;
		} 
	}
    return -1;
}

bool fFindIToMapCenter (integer vIntIn, real *vX, real *vY)
{
	//sprintf(vTest,"vMapGlobal = %d",vMapGlobal );
	//DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, vTest );
	char *pMap = (char*)vMapGlobal;
    integer vInt;
	sRect vRect;
	bool vFlag;
	vRect.vX1 = vW3-1;
	vRect.vY1 = vH3-1;
	vRect.vX2 = 0;
	vRect.vY2 = 0;
	vFlag = 0;
    for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
        vInt = (integer)pMap[vvi];
        if (vInt == vIntIn)
		{
			if (vFlag == 0)
			{
				vFlag = 1;
			}
			if (vRect.vX1 > vvi%vW3)
			{
				vRect.vX1 = vvi%vW3;
			}
			if (vRect.vX2 < vvi%vW3)
			{
				vRect.vX2 = vvi%vW3;
			}
			if (vRect.vY1 > vvi/vH3)
			{
				vRect.vY1 = vvi/vH3;
			}
			if (vRect.vY2 < vvi/vH3)
			{
				vRect.vY2 = vvi/vH3;
			}
		} 
	}
	if (vFlag == 0) return FALSE;
	*vX = fConverterIX2RX(vRect.vX1 + (vRect.vX2 - vRect.vX1) / 2);
	*vY = fConverterIY2RY(vRect.vY1 + (vRect.vY2 - vRect.vY1) / 2);
    return TRUE;
}

unsigned short fMaxBase ()
{
	char *pMap = (char*)vMapGlobal;
    integer vInt;
	integer vInt2;
	vInt2 = 0;
    for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
        vInt = (integer)pMap[vvi];
        if (vInt >= JASS_NumBase_MIN && vInt <= JASS_NumBase_MAX)
		{
			if (vInt2 < vInt)
			{
				vInt2 = vInt;
			}
		} 
	}
    return vInt2;
}

unsigned short fFindBaseByDataUnit (DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return DO_NOT_FIND_BASE;
	char *pMap = (char*)vMapGlobal;
    integer vInt;
	integer vInt2;
	vInt2 = fConvertXYToI(fConverterX(pDataUnit->LastCoordinatesForEnemy.X),
						  fConverterY(pDataUnit->LastCoordinatesForEnemy.Y));
	if (vInt2 < 0 || vInt2 >= vSizeMap) return DO_NOT_FIND_BASE;
    vInt = (integer)pMap[vInt2];
	if (vInt == 0 ||
		vInt == JASS_SLAYERPLACE ||
		vInt == JASS_DANCEPOOL ||
		vInt == JASS_PATH_BLIGHT ||
		vInt == JASS_WATER_LEVEL_1 ||
		vInt == JASS_PATH_NOWATER)
		return 0;
    return vInt;
}

unsigned short fFindBaseByDataUnitNoEnemy (DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return DO_NOT_FIND_BASE;
	char *pMap = (char*)vMapGlobal;
    integer vInt;
	integer vInt2;
	if (pDataUnit->vId == ID_UNIT_WORKER)
	{
		pDataUnit->LastCoordinates.X = GetUnitX(pDataUnit->WhichUnit);
		pDataUnit->LastCoordinates.Y = GetUnitY(pDataUnit->WhichUnit);
	}
	vInt2 = fConvertXYToI(fConverterX(pDataUnit->LastCoordinates.X),
						  fConverterY(pDataUnit->LastCoordinates.Y));
	if (vInt2 < 0 || vInt2 >= vSizeMap) return DO_NOT_FIND_BASE;
    vInt = (integer)pMap[vInt2];
	if (vInt == 0 ||
		vInt == JASS_SLAYERPLACE ||
		vInt == JASS_DANCEPOOL ||
		vInt == JASS_PATH_BLIGHT ||
		vInt == JASS_WATER_LEVEL_1 ||
		vInt == JASS_PATH_NOWATER)
		return 0;
    return vInt;
}

integer fModulo (integer a, integer b)
{
	return a - b * (a/b);
}

unsigned int fConverterI(real vX, real vY)
{
	return fConvertXYToI(fConverterX(vX), fConverterY(vY));
}

real fConvertIToRX(unsigned int vIntIn)
{
	return fConverterIX2RX(fConvertIToX(vIntIn));
}

real fConvertIToRY(unsigned int vIntIn)
{
	return fConverterIY2RY(fConvertIToY(vIntIn));
}

unsigned short fConvertIToX (unsigned int vIntIn)
{
	if (vIntIn < 0 || vIntIn >= vSizeMap) return vSizeMap;
    return vIntIn%vW3;//fModulo(vIntIn,vW3);
}

unsigned short fConvertIToY (unsigned int vIntIn)
{
	if (vIntIn < 0 || vIntIn >= vSizeMap) return vSizeMap;
    return (unsigned short)(floor((float)vIntIn / (float)vW3));
}

unsigned int fConvertXYToI (unsigned short vX, unsigned short vY)
{
	if (vX < 0 || vX >= vW3) return vSizeMap;
	if (vY < 0 || vY >= vH3) return vSizeMap;
    return (unsigned int)(vX + vY * vW3);
}

unsigned short fConverterX (real X)
{
	return (unsigned short)(floor((X-(((float)vW * 128 / -2)))/((float)DOOD_MAP_SUBDIV/2)));//-1;
}

unsigned short fConverterY (real Y)
{
	return (unsigned short)(floor((((float)vH * 128) - Y+(((float)vH * 128 / -2)))/((float)DOOD_MAP_SUBDIV/2)));//+1;
}

real fConverterIX2RX (unsigned short vX)
{
	//return ((float)vW * 128 / -2)+(float)(vX)*((float)DOOD_MAP_SUBDIV/2)+(float)DOOD_MAP_SUBDIV/4+(float)DOOD_MAP_SUBDIV/2;//128/vW2;
	return ((float)vW * 128 / -2)+(float)(vX)*((float)DOOD_MAP_SUBDIV/2)+(float)DOOD_MAP_SUBDIV/4;
}

real fConverterIY2RY (unsigned short vY)
{
	//return ((float)vH * 128 / 2)-(float)(vY)*((float)DOOD_MAP_SUBDIV/2)-(float)DOOD_MAP_SUBDIV/4+(float)DOOD_MAP_SUBDIV/2;//128/vH2;
	return ((float)vH * 128 / 2)-(float)(vY)*((float)DOOD_MAP_SUBDIV/2)-(float)DOOD_MAP_SUBDIV/4;
}

real fGetMinIX2RX (unsigned short vX)
{
	return ((float)vW * 128 / -2)+(float)(vX)*((float)DOOD_MAP_SUBDIV/2);
}

real fGetMaxIX2RX (unsigned short vX)
{
	return ((float)vW * 128 / -2)+(float)(vX)*((float)DOOD_MAP_SUBDIV/2)+(float)DOOD_MAP_SUBDIV/2;
}

real fGetMinIY2RY (unsigned short vY)
{
	return ((float)vH * 128 / 2)-(float)(vY)*((float)DOOD_MAP_SUBDIV/2);
}

real fGetMaxIY2RY (unsigned short vY)
{
	return ((float)vH * 128 / 2)-(float)(vY)*((float)DOOD_MAP_SUBDIV/2)-(float)DOOD_MAP_SUBDIV/2;
}

void fIssuePointOrderToXY(unit vUnit, string vStr, unsigned short vX, unsigned short vY)
{
    IssuePointOrder(vUnit,vStr,fConverterIX2RX(vX),fConverterIY2RY(vY));
}

void fIssuePointOrderToRect(unit vUnit, string vStr, sRect2 *vRect, real vRadius_Unit)
{
	IssuePointOrder(vUnit,vStr,fGetMinIX2RX(vRect->vX1) + vRadius_Unit,fGetMinIY2RY(vRect->vY1)-vRadius_Unit);
}

bool fIsRectToRect(sRect2 *vRect1, sRect2 *vRect2)
{
	if(vRect1 == NULL || vRect2 == NULL) return FALSE;
	if (vRect1->vX1 == vRect2->vX1 &&
		vRect1->vY1 == vRect2->vY1 &&
		vRect1->vX2 == vRect2->vX2 &&
		vRect1->vY2 == vRect2->vY2)
	{
		return TRUE;
	}
	return FALSE;
}

bool fCenterToRect(Coordinates * vCoordinates, sRect2 *vRect)
{
	if(vRect == NULL) return FALSE;
	unsigned short vDiffX, vDiffY;
	vDiffX = vRect->vX2 - vRect->vX1;
	vDiffY = vRect->vY2 - vRect->vY1;
	vCoordinates->X = fConverterIX2RX(vRect->vX1 + vDiffX);
	vCoordinates->Y = fConverterIY2RY(vRect->vY1 + vDiffY);
	return TRUE;
}

bool fMiddleRect(sRect2 *vRect1, sRect2 *vRect2, sRect2 *vRectMiddle)
{
	if (vRect1 == NULL || vRect2 == NULL || vRectMiddle == NULL) return FALSE;
	if (vRect2->vX1 > vRect1->vX1)
	{
		vRectMiddle->vX1 = vRect1->vX1 + (vRect2->vX1 - vRect1->vX1) / 2;
	}
	else
	{
		vRectMiddle->vX1 = vRect1->vX1 - (vRect2->vX1 - vRect1->vX1) / 2;
	}
	if (vRect2->vY1 > vRect1->vY1)
	{
		vRectMiddle->vY1 = vRect1->vY1 + (vRect2->vY1 - vRect1->vY1) / 2;
	}
	else
	{
		vRectMiddle->vY1 = vRect1->vY1 - (vRect2->vY1 - vRect1->vY1) / 2;
	}
	return TRUE;
}

bool fIsPointToRect(real vX, real vY, sRect2 *vRect)
{
	real vMinX,vMinY,vMaxX,vMaxY;
	vMinX = fGetMinIX2RX(vRect->vX1);
	vMinY = fGetMinIY2RY(vRect->vY1);
	vMaxX = fGetMaxIX2RX(vRect->vX2);
	vMaxY = fGetMaxIY2RY(vRect->vY2);
	if (vMinX <= vX && vMaxX >= vX &&
		vMinY >= vY && vMaxY <= vY)
	{
		return TRUE;
	}
	return FALSE;
}

bool fIsVampireNearby(DataUnit *pDataUnit, HumanPlayer *pHumanPlayer)
{
	if (pDataUnit == NULL || pHumanPlayer == NULL) return FALSE;
	for (pHumanPlayer->vvi = 0; pHumanPlayer->vvi < MAX_VAMPIRE_PLAYERS; pHumanPlayer->vvi++)
	{
		if (vDataUnits[10 + pHumanPlayer->vvi].pDataUnit &&
			IsUnitVisible(vDataUnits[10 + pHumanPlayer->vvi].pDataUnit->WhichUnit,pHumanPlayer->vParamPlayer.vPlayer) == TRUE &&
			fG(pDataUnit->LastCoordinates.X,
			   pDataUnit->LastCoordinates.Y,
			   vDataUnits[10 + pHumanPlayer->vvi].pDataUnit->LastCoordinatesForEnemy.X,
			   vDataUnits[10 + pHumanPlayer->vvi].pDataUnit->LastCoordinatesForEnemy.Y) < 128 * 5)
		{
			return TRUE;
		}
	}
	return FALSE;
}

bool fIsVampireNearby(unsigned int vInt3, HumanPlayer *pHumanPlayer)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return FALSE;
	if (pHumanPlayer == NULL) return FALSE;
	for (pHumanPlayer->vvi = 0; pHumanPlayer->vvi < MAX_VAMPIRE_PLAYERS; pHumanPlayer->vvi++)
	{
		if (vDataUnits[10 + pHumanPlayer->vvi].pDataUnit &&
			IsUnitVisible(vDataUnits[10 + pHumanPlayer->vvi].pDataUnit->WhichUnit,pHumanPlayer->vParamPlayer.vPlayer) == TRUE &&
			fG(fConvertIToRX(vInt3),
			   fConvertIToRY(vInt3),
			   vDataUnits[10 + pHumanPlayer->vvi].pDataUnit->LastCoordinatesForEnemy.X,
			   vDataUnits[10 + pHumanPlayer->vvi].pDataUnit->LastCoordinatesForEnemy.Y) < 128 * 5)
		{
			return TRUE;
		}
	}
	return FALSE;
}

DataUnit * fDataUnitInt3(unsigned int vInt3, unsigned short vStorona)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return NULL;
	unsigned short vInt;
	vInt = vMapUnits[vStorona][vInt3];
	if (vInt != NO_UNIT)
	{
		return fGetDataUnit(vInt);
	}
	return NULL;
}

unsigned int fInt3DataUnit(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return vSizeMap;
	return fConvertXYToI(fConverterX(pDataUnit->LastCoordinates.X),fConverterY(pDataUnit->LastCoordinates.Y));
}

bool fIsBaseHuman(DataUnit *pDataUnit, HumanPlayer *pHumanPlayer)
{
	if (pDataUnit == NULL || pHumanPlayer == NULL) return FALSE;
	unsigned short vNBase;
	vNBase = fNBaseUnit(pDataUnit);
	if (vNBase == pHumanPlayer->vBase.vNBase) return TRUE;
	if (vNBase >= JASS_NumBase_MIN && vNBase <= JASS_NumBase_MAX)
	{
		for (pHumanPlayer->vvi = 0; pHumanPlayer->vvi < vCountHumans; pHumanPlayer->vvi++)
		{
			if (pHumanPlayerGlobal[pHumanPlayer->vvi].vEndGame == FALSE &&
				pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vNBase == vNBase &&
				pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vBeginningOfBase <= fRemoteness(pDataUnit))
			{
				return TRUE;
			}
		}
	}
	return FALSE;
}

bool fIsPosUnit(unsigned short vIdDataUnit, unsigned int vInt3)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return FALSE;
	DataUnit *pDataUnit;
	real vX,vY;
	real vRadius;
	unsigned short vX2, vY2;
	pDataUnit = fGetDataUnit(vIdDataUnit);
	if (pDataUnit)
	{
		vRadius = fRadius_Unit(pDataUnit->vId);
		vX2 = fConvertIToX(vInt3);
		vY2 = fConvertIToY(vInt3);
		vX = fGetMinIX2RX(vX2) + vRadius;
		vY = fGetMinIY2RY(vY2) - vRadius;
		if (pDataUnit->LastCoordinates.X == vX &&
			pDataUnit->LastCoordinates.Y == vY)
		{
			return TRUE;
		}
	}
	return FALSE;
}

bool fGriveZone(DataUnit *pDataUnit, HumanPlayer *pHumanPlayer)
{
	if (pDataUnit == NULL || pHumanPlayer == NULL) return FALSE;
	if (pDataHumans->vActionStackForUnitsEnemy.fGetQuantityUnits(ID_UNIT_GRIVE) == 0) return FALSE;
	StackForUnits * vCurrent;
	pHumanPlayer->vBaseAnAlly = fBase(pDataUnit,pHumanPlayer);
	if (pHumanPlayer->vBaseAnAlly == NULL) return vSizeMap;
	vMapBases.fFreeZoneGrive(pHumanPlayer->vBaseAnAlly);
	vCurrent = pDataHumans->vActionStackForUnitsEnemy.first;
	while (vCurrent)
	{
		if (vCurrent->vIdDataUnit2.pDataUnit)
		{
			if (vCurrent->vIdDataUnit2.pDataUnit->vId == ID_UNIT_GRIVE)
			{
				vMapBases.fZoneGrive(pHumanPlayer->vBaseAnAlly,
					                 fConvertXYToI(fConverterX(vCurrent->vIdDataUnit2.pDataUnit->LastCoordinatesForEnemy.X),
									               fConverterY(vCurrent->vIdDataUnit2.pDataUnit->LastCoordinatesForEnemy.Y)));
			}
		}
		vCurrent = vCurrent->next;
	}
	return TRUE;
}

sBase * fBase(DataUnit *pDataUnit, HumanPlayer *pHumanPlayer)
{
	if (pDataUnit == NULL || pHumanPlayer == NULL) return NULL;
	unsigned short vNBase;
	vNBase = fNBaseUnit(pDataUnit);
	if (vNBase < JASS_NumBase_MIN || vNBase > JASS_NumBase_MAX) return NULL;
	if (vNBase >= JASS_NumBase_MIN && vNBase <= JASS_NumBase_MAX)
	{
		for (pHumanPlayer->vvi = 0; pHumanPlayer->vvi < vCountHumans; pHumanPlayer->vvi++)
		{
			if (pHumanPlayerGlobal[pHumanPlayer->vvi].vEndGame == FALSE &&
				pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vNBase == vNBase)
			{
				return &pHumanPlayerGlobal[pHumanPlayer->vvi].vBase;
			}
		}
	}
	return NULL;
}

// Ïðîâåðèòü ïðÿìîóãîëüíèê íà ñâîáîäíîå ìåñòî
bool fCheckMapUnitsRect(sRect2 *vRect2, unsigned short vStorona)
{
	if (vRect2 == NULL) return FALSE;
	for (vRect2->vvj2 = vRect2->vY1; vRect2->vvj2 <= vRect2->vY2; vRect2->vvj2++)
	{
		for (vRect2->vvi2 = vRect2->vX1; vRect2->vvi2 <= vRect2->vX2; vRect2->vvi2++)
		{
			if (vMapUnits[vStorona][fConvertXYToI(vRect2->vvi2,vRect2->vvj2)] != NO_UNIT)
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

unsigned int fJumpBack (DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return vSizeMap;
	unsigned int vInt3;
	int vCount;
	vInt3 = vSizeMap;
	vCount = 0;
	while (vCount < 100)
	{
		vInt3 = fConvertXYToI(fConverterX(pDataUnit->LastCoordinates.X+GetRandomReal(-(128*10),(128*10))),fConverterY(pDataUnit->LastCoordinates.Y+GetRandomReal(-(128*10),(128*10))));
		if (vCount > 90)
		{
			break;
		}
		if (vMapBases.fBaseN(vInt3) != fNBaseUnit(pDataUnit) ||
			vMapBases.fRemotenessBase(fConvertIToRX(vInt3),fConvertIToRY(vInt3)) <= 0)
		{
			vInt3 = vSizeMap;
		}
		if (vInt3 != vSizeMap) break;
		vCount++;
	}
	return vInt3;
}

// Íàéòè áàçó äëÿ çàùèòû ñëàåðîì è çàáèíäèòü íà íåé ïîçèöèþ
sBase * fFindBaseSlayerDefense(HumanPlayer *pHumanPlayer)
{
	if (pHumanPlayer == NULL) return NULL;
	for (pHumanPlayer->vvi = 0; pHumanPlayer->vvi < vCountHumans; pHumanPlayer->vvi++)
	{
		if (pHumanPlayerGlobal[pHumanPlayer->vvi].vEndGame == FALSE &&
			pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vSlayerDefense == TRUE &&
			pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.fQPosSlayerDefenseFree() > 0)
		{
			if (vMapBases.fFindPosSlayerDefense(&pHumanPlayerGlobal[pHumanPlayer->vvi].vBase,pHumanPlayer) != vSizeMap)
			{
				return &pHumanPlayerGlobal[pHumanPlayer->vvi].vBase;
			}
		}
	}
	return NULL;
}

// Íàéòè ìåñòî øàìàíà
unsigned int fFindPosShaman(HumanPlayer *pHumanPlayer)
{
	if (pHumanPlayer == NULL) return vSizeMap;
	for (pHumanPlayer->vvi = 0; pHumanPlayer->vvi < vCountHumans; pHumanPlayer->vvi++)
	{
		if (pHumanPlayerGlobal[pHumanPlayer->vvi].vEndGame == FALSE &&
			pHumanPlayerGlobal[pHumanPlayer->vvi].vFlagEscapeFromTheBase == FALSE &&
			pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vShaman == TRUE &&
			pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vInt3_Shaman >= 0 &&
			pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vInt3_Shaman < vSizeMap)
		{
			return pHumanPlayerGlobal[pHumanPlayer->vvi].vBase.vInt3_Shaman;
		}
	}
	return vSizeMap;
}

bool fCheckPos(DataUnit* pDataUnit, unsigned int vInt3)
{
	if (pDataUnit == NULL) return 0;
	return vMapBases.fCheckPos(vInt3,pDataUnit->vDiff/2,pDataUnit->vDiff/2);
}

unsigned short fRemoteness(DataUnit* pDataUnit)
{
	if (pDataUnit == NULL) return 0;
	return vMapBases.fRemoteness(pDataUnit->LastCoordinates.X,pDataUnit->LastCoordinates.Y);
}

unsigned short fRemotenessBase(DataUnit* pDataUnit)
{
	if (pDataUnit == NULL) return 0;
	return vMapBases.fRemotenessBase(pDataUnit->LastCoordinates.X,pDataUnit->LastCoordinates.Y);
}

unsigned short fRemotenessBase(unsigned int vInt3)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return 0;
	return vMapBases.fRemotenessBase(fConvertIToRX(vInt3),fConvertIToRY(vInt3));
}

unsigned short fMaxRemotenessBase(DataUnit* pDataUnit)
{
	if (pDataUnit == NULL) return 0;
	return vMapBases.fMaxRemotenessBase(pDataUnit->LastCoordinates.X,pDataUnit->LastCoordinates.Y);
}

unit fGetUnit(int vId, player vPlayer)
{
	group g=CreateGroup();
	unit u;
	GroupEnumUnitsOfPlayer(g,vPlayer,0);
	do
	{
		u=FirstOfGroup(g);
		if (GetUnitTypeId(u)==vId)
		{
			DestroyGroup(g);
			return u;
		}
		GroupRemoveUnit(g,u);
	}
	while (u != 0);
	DestroyGroup(g);
	return 0;
}

void fAddDataUnit(int vId, player vPlayer)
{
	group g=CreateGroup();
	unit u;
	GroupEnumUnitsOfPlayer(g,vPlayer,0);
	do
	{
		u=FirstOfGroup(g);
		if (GetUnitTypeId(u)==vId)
		{
			fCreateDataUnit(u,THREAD_DO_NOT_MAIN);
		}
		GroupRemoveUnit(g,u);
	}
	while (u != 0);
	DestroyGroup(g);
}

void fAddAllDataUnit()
{
	group g=CreateGroup();
	unit u;
	GroupEnumUnitsInRect(g,vWorldRect,0);
	do
	{
		u=FirstOfGroup(g);
		if (fGetDataUnit(u) == NULL)
		{
			fCreateDataUnit(u,THREAD_DO_NOT_MAIN);
		}
		GroupRemoveUnit(g,u);
	}
	while (u != 0);
	DestroyGroup(g);
}

void fAddAllDataUnitOfPlayer(HPLAYER vPlayer)
{
	group g=CreateGroup();
	unit u;
	GroupEnumUnitsOfPlayer(g,vPlayer,0);
	do
	{
		u=FirstOfGroup(g);
		if (fGetDataUnit(u) == NULL)
		{
			fCreateDataUnit(u,THREAD_DO_NOT_MAIN);
		}
		GroupRemoveUnit(g,u);
	}
	while (u != 0);
	DestroyGroup(g);
}

real fG(real vX1, real vY1, real vX2, real vY2)
{
	return sqrtf(powf(abs(vX2-vX1),2)+powf(abs(vY2-vY1),2));
}

integer fInitMap(integer vPathToW3)
{
	char FullPath[1000];
	int vSize;
	FILE *pFile;
	char *pMap;
	sprintf(FullPath,"%s\\%s",(char*)vPathToW3,"Map.txt");
	pFile = fopen(FullPath,"rb");	// îòêðûòü ôàéë
	if (pFile == NULL)
		return FALSE;
	fseek(pFile, 0L, SEEK_SET);
	vSize = filelength(fileno(pFile));
	if (vSize <= 0 || vSize > vSizeMap)
	{
		fclose(pFile);	// çàêðûâàåì ôàéë
		return NULL;
	}
	//pMap = (char*) malloc(vSize+1);
	pMap = new char[vSize+1];
	fread(pMap, 1, vSize, pFile);	// ñ÷èòûâàåì ìàññèâ áàéòîâ
	pMap[vSizeMap] = '0';
	fclose(pFile);	// çàêðûâàåì ôàéë
	return (int)pMap;
}

integer fInitMapFromResources()
{
	HMODULE handle = GetModuleHandle(L"Vampirism_fire.dll");
	if (!handle)
	{
		return NULL;
	}
	HRSRC hRes;
	hRes = FindResource(handle, MAKEINTRESOURCE(IDR_TXT1), L"txt");
	if (hRes == NULL)
	{
		return NULL;
	}
    HGLOBAL SecTXT;
	SecTXT = LoadResource(handle, hRes); 
	DWORD size;
	size = SizeofResource(handle, hRes);
	if (size <= 0 || size > vSizeMap)
	{
		return NULL;
	}
	//pMapGlobal = (char *)malloc(size);
	pMapGlobal = new char[size];
	memcpy(pMapGlobal,(char *)LockResource(SecTXT),size);
	return (int)pMapGlobal;
}

integer fGetPath()
{
	char Direct[1000];
	wchar_t Path3[1000];
	char *pPathToW3;
	//pPathToW3 = (char*) malloc(1000);
	pPathToW3 = new char[1000];
	getcwd (Direct, sizeof(Direct));
	sprintf(pPathToW3,"%s\\%s",Direct,"Vampirism_fire");
	MultiByteToWideChar(CP_ACP, 0, pPathToW3, - 1, Path3, sizeof(pPathToW3));
	CreateDirectory(Path3,NULL);
	return (int)pPathToW3;
}

// Ôóíêöèÿ äëÿ ñîçäàíèÿ è óäàëåíèÿ þíèòîâ
void fDataUnit2()
{
	sprintf(vBuf,"fDataUnit2() N_1\0");
	fLog(999,vBuf);
	if (vStartGameSleep == FALSE)
	{
		return;
	}
	/*if (vSync4_Priority == FALSE)
	{
		goto m1;
	}*/
	/*if (vSync4 == FALSE)
	{
		sprintf(vKeyLocalPlayer,"%d\0",GetPlayerColor(GetLocalPlayer()));
		StoreInteger(vSyncCache4,"fDataUnit2",vKeyLocalPlayer,1);
		SyncStoredInteger(vSyncCache4,"fDataUnit2",vKeyLocalPlayer);
		StoreInteger(vSyncCache4,"fDataUnit2",vKeyLocalPlayer,0);
		sprintf(vKeyLocalPlayer,"\0");
		vSync4 = TRUE;
	}
	if (vSync4 == TRUE)
	{
		for (int vvi = 0; vvi < vCountPlayersUser; vvi++)
		{
			if (pPlayerUser[vvi].vLeave == FALSE)
			{
				if (GetStoredInteger(vSyncCache4,"fDataUnit2",pPlayerUser[vvi].vKeyPlayer) != 1)
				{
					return;
				}
			}
			else if (HaveStoredInteger(vSyncCache4,"fDataUnit2",pPlayerUser[vvi].vKeyPlayer) == TRUE)
			{
				FlushStoredInteger(vSyncCache4,"fDataUnit2",pPlayerUser[vvi].vKeyPlayer);
			}
		}
	}
	vSync4 = FALSE;*/
	/*char vNagruzka[1500];
	for (int vvi = 0; vvi < 1500; vvi++)
	{
		vNagruzka[vvi] = '1';
	}
	vNagruzka[1499] = 0;*/
m1:
	//fLog(0,"0");
	//fLog(300,"300");
	//sDataUnit2 *vForerunner;
	sprintf(vBuf,"fDataUnit2() N_2\0");
	fLog(999,vBuf);
	vActionStackForCreateUnit.vNext = NULL;
	//vForerunner = NULL;
	vActionStackForCreateUnit.vCurrent = vActionStackForCreateUnit.pCurrentDataUnit2;
	sprintf(vBuf,"fDataUnit2() N_3\0");
	fLog(999,vBuf);
	while(vActionStackForCreateUnit.vCurrent)
	{
		sprintf(vBuf,"fDataUnit2() N_4\0");
	fLog(999,vBuf);
		vActionStackForCreateUnit.vNext = vActionStackForCreateUnit.vCurrent->vNext;
		sprintf(vBuf,"fDataUnit2() N_5\0");
	fLog(999,vBuf);
		vActionStackForCreateUnit.vIdDataUnit = fCreateDataUnit2(vActionStackForCreateUnit.vCurrent);
		sprintf(vBuf,"fDataUnit2() N_6\0");
	fLog(999,vBuf);
		if (vActionStackForCreateUnit.vIdDataUnit == NO_UNIT)
		{
			sprintf(vBuf,"fDataUnit2() N_7\0");
	fLog(999,vBuf);
			vActionStackForCreateUnit.fFreeOneStackForUnit(vActionStackForCreateUnit.vCurrent->vN);
			sprintf(vBuf,"fDataUnit2() N_8\0");
	fLog(999,vBuf);
		}
		else
		{
			sprintf(vBuf,"fDataUnit2() N_9\0");
	fLog(999,vBuf);
			vActionStackForCreateUnit.vCurrent->vIdDataUnit = vActionStackForCreateUnit.vIdDataUnit;
			sprintf(vBuf,"fDataUnit2() N_10\0");
	fLog(999,vBuf);
			vActionStackForCreateedUnit.fAddDataUnit(vActionStackForCreateUnit.vCurrent);
			sprintf(vBuf,"fDataUnit2() N_11\0");
	fLog(999,vBuf);
		}
		vActionStackForCreateUnit.vCurrent = vActionStackForCreateUnit.vNext;
		sprintf(vBuf,"fDataUnit2() N_12\0");
	fLog(999,vBuf);
		if (vActionStackForCreateUnit.vCurrent && vActionStackForCreateUnit.vNext == vActionStackForCreateUnit.vCurrent->vNext) break;
		sprintf(vBuf,"fDataUnit2() N_13\0");
	fLog(999,vBuf);
	}
	sprintf(vBuf,"fDataUnit2() N_14\0");
	fLog(999,vBuf);
	vActionStackForCreateUnit.fNullVar();
	vActionStackForDelUnit.vNext = NULL;
	vActionStackForDelUnit.vCurrent = vActionStackForDelUnit.pCurrentDataUnit2;
	sprintf(vBuf,"fDataUnit2() N_15\0");
	fLog(999,vBuf);
	while(vActionStackForDelUnit.vCurrent)
	{
		sprintf(vBuf,"fDataUnit2() N_16\0");
	fLog(999,vBuf);
		vActionStackForDelUnit.vNext = vActionStackForDelUnit.vCurrent->vNext;
		sprintf(vBuf,"fDataUnit2() N_17 vActionStackForDelUnit.vCurrent = %hu\0",vActionStackForDelUnit.vCurrent);
	fLog(999,vBuf);
		if (fDelDataUnit2(vActionStackForDelUnit.vCurrent->pDataUnit) == 1)
		{
			sprintf(vBuf,"fDataUnit2() N_18\0");
	fLog(999,vBuf);
			vActionStackForDelUnit.fFreeOneStackForUnit(vActionStackForDelUnit.vCurrent->vN);
			sprintf(vBuf,"fDataUnit2() N_19\0");
	fLog(999,vBuf);
		}
		sprintf(vBuf,"fDataUnit2() N_20\0");
	fLog(999,vBuf);
		vActionStackForDelUnit.vCurrent = vActionStackForDelUnit.vNext;
		sprintf(vBuf,"fDataUnit2() N_21\0");
	fLog(999,vBuf);
		if (vActionStackForDelUnit.vCurrent && vActionStackForDelUnit.vNext == vActionStackForDelUnit.vCurrent->vNext) break;
		sprintf(vBuf,"fDataUnit2() N_22\0");
	fLog(999,vBuf);
	}
	sprintf(vBuf,"fDataUnit2() N_23\0");
	fLog(999,vBuf);
	vActionStackForDelUnit.fNullVar();
	sprintf(vBuf,"fDataUnit2() N_24\0");
	fLog(999,vBuf);
}

// Óäàëèòü èç ñòåêà þíèòà
unsigned short fDelDataUnit(unsigned short vIdDataUnit)
{
	vActionStackForDelUnit.pDataUnit = fGetDataUnit(vIdDataUnit);
	if (!vActionStackForDelUnit.pDataUnit) return NO_UNIT;
	vActionStackForDelUnit.pDataUnit->Dead = TRUE;
	fPreDelDataUnit2(vActionStackForDelUnit.pDataUnit);
	vActionStackForCreateedUnit.pDataUnit2 = vActionStackForCreateedUnit.fFindDataUnit2(vIdDataUnit);
	if (vActionStackForCreateedUnit.pDataUnit2 == NULL) return 0;
	vActionStackForCreateedUnit.pDataUnit2->pDataUnit = vActionStackForDelUnit.pDataUnit;
	vActionStackForDelUnit.fAddDataUnit(vActionStackForCreateedUnit.pDataUnit2);
	return vActionStackForCreateedUnit.fDelDataUnit(vActionStackForCreateedUnit.pDataUnit2->vN);
	//return fDelDataUnit2(vIdDataUnit);
}

// Äîáàâèòü â ñòåê ñîçäàíèÿ þíèòà
unsigned short fCreateDataUnit(unit vUnit, unsigned short vFlagThread)
{
	if (vFlagThread == THREAD_DO_NOT_MAIN)
	{
		return vActionStackForCreateUnit.fCreateDataUnit(vUnit);
	}
	if (vFlagThread == THREAD_MAIN)
	{
		if (vUnit <= 0) return NO_UNIT;
		pDataUnit2.vId = GetUnitTypeId(vUnit);
		if (pDataUnit2.vId == 0)
		{
			return NO_UNIT;
		}
		pDataUnit2.vUnit = vUnit;
		pDataUnit2.vPlayer = GetOwningPlayer(pDataUnit2.vUnit);
		pDataUnit2.vNPlayer = GetPlayerColor(pDataUnit2.vPlayer);
		if (fIsSlayer(pDataUnit2.vId))
		{
			if (fGetDataUnit((unsigned short)(pDataUnit2.vNPlayer + 12)) != NULL)
			{
				return NO_UNIT;
			}
		}
		pDataUnit2.vIdDataUnit = fCreateDataUnit2(&pDataUnit2);
		if (pDataUnit2.vIdDataUnit == NO_UNIT)
		{
			return NO_UNIT;
		}
		vActionStackForCreateedUnit.fAddDataUnit(&pDataUnit2);
		return pDataUnit2.vIdDataUnit;
	}
	return NO_UNIT;
}

/*// Óäàëèòü èç ñòåêà þíèòà
unsigned short fDelDataUnit(unsigned short vIdDataUnit)
{
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS) return 0; 
	if (pLastDeleteDataUnit)
	{
		pLastDeleteDataUnit->vNext = &vDataUnit2[vIdDataUnit];
		pLastDeleteDataUnit = &vDataUnit2[vIdDataUnit];
	}
	else
	{
		pLastDeleteDataUnit = &vDataUnit2[vIdDataUnit];
		pCurrentDeleteDataUnit = &vDataUnit2[vIdDataUnit];
	}
	if (vIdDataUnit == vNLastDataUnit2)
	{
		if (vIdDataUnit > 0)
		{
			vNLastDataUnit2--;
		}
		else if (vIdDataUnit == 0)
		{
			vNLastDataUnit2 = NO_UNIT;
		}
	}
	return 1;
}

// Äîáàâèòü â ñòåê ñîçäàíèÿ þíèòà
unsigned short fCreateDataUnit(unit vUnit)
{
	if (vUnit <= 0) return NO_UNIT;
	unsigned short vN;
	integer vId;
	HPLAYER vPlayer;
	HPLAYERCOLOR vNPlayer;
	vId = GetUnitTypeId(vUnit);
	if (vId == 0)
	{
		return NO_UNIT;
	}
	vPlayer = GetOwningPlayer(vUnit);
	vNPlayer = GetPlayerColor(vPlayer);
	vN = fFindFreeNInSteckDataUnit2(vId,vNPlayer);
	if (vN >= 0 && vN < MAX_COUNT_UNITS)
	{
		vDataUnit2[vN].vUnit = vUnit;
		vDataUnit2[vN].vId = vId;
		vDataUnit2[vN].vPlayer = vPlayer;
		vDataUnit2[vN].vNPlayer = vNPlayer;
		vDataUnit2[vN].vNext = NULL;
		if (pLastCreateDataUnit)
		{
			pLastCreateDataUnit->vNext = &vDataUnit2[vN];
			pLastCreateDataUnit = &vDataUnit2[vN];
		}
		else
		{
			pLastCreateDataUnit = &vDataUnit2[vN];
			pCurrentCreateDataUnit = &vDataUnit2[vN];
		}
		return vN;
	}
	return NO_UNIT;
}

// Ïîèñê ñâîáîäíîãî íîìåðà â ñòàêå ñîçäàíèÿ è óäàëåíèÿ äàííûõ äëÿ þíèòîâ
unsigned short fFindFreeNInSteckDataUnit2(integer vId, HPLAYERCOLOR vNPlayer)
{
	if (vId < 0 || vNPlayer < 0) return NO_UNIT;
	if (fIsSlayer(vId))
	{
		if (fGetDataUnit((unsigned short)(vNPlayer + 12)) != NULL)
		{
			return NO_UNIT;
		}
		return vNPlayer + 12;
	}
	if (fIsVampire(vId) ||
		fIsMainHuman(vId))
	{
		return vNPlayer;
	}
	if (vNLastDataUnit2 == NO_UNIT)
	{
		vNLastDataUnit2 = MAX_OLD_UNITS + 1;
		return vNLastDataUnit2;
	}
	if (vNLastDataUnit2 >= 0 && vNLastDataUnit2 < MAX_COUNT_UNITS)
	{
		if (vNLastDataUnit2 + 1 < MAX_COUNT_UNITS)
		{
			vNLastDataUnit2++;
			return vNLastDataUnit2;
		}
		for (unsigned short vvi = vNLastDataUnit2; vvi >= 0; vvi--)
		{
			if (vDataUnit2[vvi].vUnit == 0)
			{
				vNLastDataUnit2 = vvi;
			}
			else
			{
				if (vvi == vNLastDataUnit2)
				{
					break;
				}
				else
				{
					return vNLastDataUnit2;
				}
			}
			if (vvi == 0) break;
		}
		for (unsigned short vvi = 0; vvi < vNLastDataUnit2; vvi++)
		{
			if (vDataUnit2[vvi].vUnit == 0)
			{
				return vvi;
			}
		}
	}
	return LIMIT_UNIT;
}*/

bool fHarvestingNearestTree(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return FALSE;

	HTIMER vTimer;
	vTimer = CreateTimer();

	real vX,vY;
	vX = GetUnitX(pDataUnit->WhichUnit);
	vY = GetUnitY(pDataUnit->WhichUnit);

	integer vParentKey;

	vParentKey = vTimer;
	
	SaveReal(vHT,vParentKey,CONST_HUNIT_HOUSE_X,vX);
	SaveReal(vHT,vParentKey,CONST_HUNIT_HOUSE_Y,vY);
	SaveReal(vHT,vParentKey,CONST_HUNIT_HOUSE_G,0);
	SaveInteger(vHT,vParentKey,CONST_HUNIT_HOUSE,(Integer)pDataUnit);

	TimerStart(vTimer,0.01,false,&fDestAction);
	return TRUE;
}

void fDestAction()
{
	HDESTRUCTABLE vDest;
	HRECT vRect;
	integer vParentKey;
	real vX,vY;
	DataUnit *pDataUnit;
	HTIMER vTimer;
	vTimer = GetExpiredTimer();
	vParentKey = vTimer;
	if (vParentKey == NULL) return;
	pDataUnit = (DataUnit*)LoadInteger(vHT,vParentKey,CONST_HUNIT_HOUSE);
	if (pDataUnit == NULL)
	{
		DestroyTimer(vTimer);
		return;
	}
	vDest = NULL;
	vX = LoadReal(vHT,vParentKey,CONST_HUNIT_HOUSE_X);
	vY = LoadReal(vHT,vParentKey,CONST_HUNIT_HOUSE_Y);
	vRect = Rect(vX-128*4,vY+128*4,vX+128*4,vY-128*4);
	EnumDestructablesInRect(vRect,NULL,&fFlagAction);
	vDest = LoadDestructableHandle(vHT,vParentKey,CONST_HDESTRUCTABLE_TREE);
	RemoveRect(vRect);
	if (vDest == NULL)
	{
		DestroyTimer(vTimer);
		return;
	}
	if (pDataUnit && 
		IssueTargetOrderById(pDataUnit->WhichUnit,
							ID_ORDER_SMART,
							vDest) == TRUE);
	DestroyTimer(vTimer);
}

void fFlagAction()
{
	integer vParentKey;
	vParentKey = GetExpiredTimer();
	if (vParentKey == NULL) return;
	SaveDestructableHandle(vHT,vParentKey,CONST_HDESTRUCTABLE_ENUM,GetEnumDestructable());
	SaveInteger(vHT,vParentKey,CONST_ID_TREE,GetDestructableTypeId(LoadDestructableHandle(vHT,vParentKey,CONST_HDESTRUCTABLE_ENUM)));
	if (LoadInteger(vHT,vParentKey,CONST_ID_TREE) == ID_TREE)//vId == 1)
	{
		SaveReal(vHT,vParentKey,CONST_HDESTRUCTABLE_X,GetDestructableX(LoadDestructableHandle(vHT,vParentKey,CONST_HDESTRUCTABLE_ENUM)));
		SaveReal(vHT,vParentKey,CONST_HDESTRUCTABLE_Y,GetDestructableY(LoadDestructableHandle(vHT,vParentKey,CONST_HDESTRUCTABLE_ENUM)));
		SaveReal(vHT,vParentKey,CONST_HDESTRUCTABLE_G,sqrtf(powf(abs(LoadReal(vHT,vParentKey,CONST_HDESTRUCTABLE_X)-LoadReal(vHT,vParentKey,CONST_HUNIT_HOUSE_X)),2)+
			                            powf(abs(LoadReal(vHT,vParentKey,CONST_HDESTRUCTABLE_Y)-LoadReal(vHT,vParentKey,CONST_HUNIT_HOUSE_Y)),2)));
		if (LoadReal(vHT,vParentKey,CONST_HDESTRUCTABLE_G) < LoadReal(vHT,vParentKey,CONST_HUNIT_HOUSE_G) || LoadReal(vHT,vParentKey,CONST_HUNIT_HOUSE_G) == 0)
		{
			SaveReal(vHT,vParentKey,CONST_HUNIT_HOUSE_G,LoadReal(vHT,vParentKey,CONST_HDESTRUCTABLE_G));
			SaveDestructableHandle(vHT,vParentKey,CONST_HDESTRUCTABLE_TREE,LoadDestructableHandle(vHT,vParentKey,CONST_HDESTRUCTABLE_ENUM));
		}
	}
}

bool fNullUnit(unit WhichUnit)
{
	if (WhichUnit == 0xdddddddd ||
		WhichUnit == 0xcdcdcdcd ||
		WhichUnit == 0xcccccccc ||
		WhichUnit == 0xfeeefeee)
	{
		return TRUE;
	}
	return FALSE;
}

void fLog(int vFlag, char *vText)
{
	return;
	if (!(vFlag == 996)) return;
	char FullPath[300];
	char Path2[300];
	char szBuffer[300];
	int vLen;
	FILE * pFile;
	sprintf(Path2,"%s\0","C:\\Games\\VF_Project\\Logs");
	if (vNLocalPlayer >= 0 && vNLocalPlayer <= 11)
	{
		sprintf(FullPath,"%s\\TimerGetElapsed[%d].txt",Path2,vNLocalPlayer);//GetPlayerColor(GetLocalPlayer()));
	}
	else
	{
		sprintf(FullPath,"%s\\TimerGetElapsed[LogAll].txt",Path2);
	}
	pFile = fopen(FullPath,"ab");// îòêðûâàåì ôàéë
	if (pFile==NULL)
		return;

	//fseek(m_fiJASS, 0L, SEEK_SET);
	//memset(&szBuffer,'\0',250000);	
	ZeroMemory(&szBuffer,sizeof(szBuffer));
	//fread(&szBuffer,1,0x5c6,m_fiJASS);
	if (vFlag == 0)
	{
	if (vNumLog == 0)
	{
		sprintf(szBuffer,"-------------------\n");
		vLen = strlen(szBuffer);
		fwrite(&szBuffer, 1, vLen, pFile);
	}
	float vTimerGetElapsed = TimerGetElapsed(vTimerGame);
	sprintf(szBuffer,"%d - %f\n\0",vNumLog,vTimerGetElapsed);
	vLen = strlen(szBuffer);
	fwrite(&szBuffer, 1, vLen, pFile);

	vNumLog++;
	}
	if (vFlag > 0)
	{
		sprintf(szBuffer,"vFlag == %d\n",vFlag);
		vLen = strlen(szBuffer);
		fwrite(&szBuffer, 1, vLen, pFile);
		sprintf(szBuffer,"%s\n",vText);
		vLen = strlen(szBuffer);
		fwrite(&szBuffer, 1, vLen, pFile);
	}
	fclose(pFile);
}