#include "stdafx.h"
#include "Global_variables_extern.h"
#include "Basics_functions.h"
#include "MapBases.h"

MapBases::MapBases()
{
	vMap = NULL;
	/*if (fInit() == FALSE)
	{
		this->~MapBases();
	}*/
}

MapBases::~MapBases()
{
	if (vMap)
	{
		delete vMap;
		vMap = NULL;
	}
}

bool MapBases::fInit ()
{
	if (vMapGlobal == NULL) return FALSE;
	char *pMap = (char*)vMapGlobal;
	int vvi, vvj;
	int vInt;
	int vQTree;
	int vQCliff;
	int vInt2;
	MAP_W = vW3/2;
	MAP_H = vH3/2;
	MAP_MAX_SMALL_CAGE = MAP_W * MAP_H;
	// Переменные
	if (vMap == NULL)
	{
		vMap = new sSmallCage[MAP_MAX_SMALL_CAGE];
	}
	// Бинды баз и обнуление массива позиций ремонта вала
	//vBindSlayerPlace = FALSE;
	//vBindDancePool = FALSE;
	//for (vvi = 0; vvi < MAP_MAX_BASE; vvi++)
	//{
		//vBindBase[vvi] = FALSE;
		//for (vvj = 0; vvj < MAP_MAX_REPAIR; vvj++)
		//{
			//vRepairPos[vvi][vvj].vInt3 = vSizeMap;
			//vRepairPos[vvi][vvj].vRepair = FALSE;
		//}
	//}
	// Присвоение начальных значений массиву маленьких клеток
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vRedCell = FALSE;
		vMap[vvi].vHeal = FALSE;
		vMap[vvi].vIdDataUnit = 0;
		vMap[vvi].vDoom.vNBaseRight = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom.vNBaseBottom = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom.vNBaseLeft = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom.vNBaseTop = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom.vBool = FALSE;
		vMap[vvi].vDoom2.vNBaseRight = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom2.vNBaseBottom = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom2.vNBaseLeft = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom2.vNBaseTop = DO_NOT_FIND_BASE;
		vMap[vvi].vDoom2.vBool = FALSE;
		vMap[vvi].vBusy = FALSE;
		vMap[vvi].vRemotenessTower = 0;
		vMap[vvi].vRemotenessGrive = 0;
		vMap[vvi].vRemotenessBase = 0;
		vMap[vvi].vTower = FALSE;
		vMap[vvi].vGrive = FALSE;
		vMap[vvi].vRepair = FALSE;
		vMap[vvi].vXBuild = 0;
		vMap[vvi].vYBuild = 0;
		vMap[vvi].vNSmallCage = vvi;
		vMap[vvi].vQObstacles = 0;
		vMap[vvi].vLightning = TRUE;
		vMap[vvi].vRicochet = TRUE;
		vMap[vvi].vNBase = 0;
		vMap[vvi].vTree.vBottom = FALSE;
		vMap[vvi].vTree.vLeft = FALSE;
		vMap[vvi].vTree.vTop = FALSE;
		vMap[vvi].vTree.vRight = FALSE;
		vMap[vvi].vQTree = 0;
		vMap[vvi].vCliff.vBottom = FALSE;
		vMap[vvi].vCliff.vLeft = FALSE;
		vMap[vvi].vCliff.vTop = FALSE;
		vMap[vvi].vCliff.vRight = FALSE;
		vMap[vvi].vQCliff = 0;
		vMap[vvi].vHead = NULL;
		vMap[vvi].vIdSmallCage = NO_UNIT;
		vMap[vvi].vX = vvi%MAP_W;
		vMap[vvi].vY = vvi/MAP_H;
		vMap[vvi].vInt3 = fConvertXYToI((vvi*2)%vW3,((vvi*2)/vH3)*2);
		vMap[vvi].vInt = (unsigned short)pMap[vMap[vvi].vInt3];
		if (vMap[vvi].vInt == JASS_UNKNOWN) return FALSE;
		vMap[vvi].vBuild = TRUE;
		vMap[vvi].vWalk = TRUE;
		switch (vMap[vvi].vInt)
		{
		case JASS_SLAYERPLACE:
		case JASS_DANCEPOOL:
		case JASS_PATH_NOWALK:
		case JASS_PATH_BLIGHT:
		case JASS_DOOD_DEFAULT_TREE:
		case JASS_WATER_LEVEL_1:
		case JASS_TELEPORT_1_1:
		case JASS_TELEPORT_1_2:
		case JASS_TELEPORT_2_1:
		case JASS_TELEPORT_2_2:
			vMap[vvi].vBuild = FALSE;
			break;
		}
		switch (vMap[vvi].vInt)
		{
		case JASS_PATH_NOWALK:
		case JASS_DOOD_DEFAULT_TREE:
			vMap[vvi].vWalk = FALSE;
			break;
		}
		if (vMap[vvi].vInt >= JASS_NumBase_MIN &&
			vMap[vvi].vInt <= JASS_NumBase_MAX)
		{
			vMap[vvi].vNBase = vMap[vvi].vInt;
		}
		
	}
	// Деревья и клиф-левелы и думы
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == FALSE) continue;
		vQTree = 0;
		vQCliff = 0;
		for (vvj = 0; vvj < 4; vvj++)
		{
			vInt = JASS_UNKNOWN;
			vInt2 = JASS_UNKNOWN;
			switch (vvj)
			{
			case 0:
				if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
				{
					vInt = vMap[vvi+MAP_W].vInt;// Низ
				}
				if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W*3) && vMap[vvi+MAP_W*3].vY < MAP_H)
				{
					vInt2 = vMap[vvi+MAP_W*3].vInt;// Вниз через 3 маленьких клеток
				}
				break;
			case 1:
				if (vvi>=1 && vMap[vvi-1].vX >= 0)
				{
					vInt = vMap[vvi-1].vInt;// Лево
				}
				if (vvi>=1*3 && vMap[vvi-1*3].vX >= 0)
				{
					vInt2 = vMap[vvi-1*3].vInt;// Лево
				}
				break;
			case 2:
				if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
				{
					vInt = vMap[vvi-MAP_W].vInt;// Верх
				}
				if (vvi>=MAP_W*3 && vMap[vvi-MAP_W*3].vY >= 0)
				{
					vInt2 = vMap[vvi-MAP_W*3].vInt;// Верх
				}
				break;
			case 3:
				if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
				{
					vInt = vMap[vvi+1].vInt;// Право
				}
				if (vvi<=(MAP_MAX_SMALL_CAGE-1-1*3) && vMap[vvi+1*3].vX < MAP_W)
				{
					vInt2 = vMap[vvi+1*3].vInt;// Право
				}
				break;
			}
			switch (vInt)
			{
			case JASS_DOOD_DEFAULT_TREE:
				vQTree++;
				switch(vvj)
				{
				case 0:
					vMap[vvi].vTree.vBottom = TRUE;
					break;
				case 1:
					vMap[vvi].vTree.vLeft = TRUE;
					break;
				case 2:
					vMap[vvi].vTree.vTop = TRUE;
					break;
				case 3:
					vMap[vvi].vTree.vRight = TRUE;
					break;
				}
				break;
			case JASS_PATH_NOWALK:
				vQCliff++;
				switch(vvj)
				{
				case 0:
					vMap[vvi].vCliff.vBottom = TRUE;
					break;
				case 1:
					vMap[vvi].vCliff.vLeft = TRUE;
					break;
				case 2:
					vMap[vvi].vCliff.vTop = TRUE;
					break;
				case 3:
					vMap[vvi].vCliff.vRight = TRUE;
					break;
				}
				break;
			case JASS_UNKNOWN:
				break;
			}
			if (vInt == JASS_DOOD_DEFAULT_TREE ||
				vInt == JASS_PATH_NOWALK)
			{
				if ((vInt2 >= JASS_NumBase_MIN &&
					vInt2 <= JASS_NumBase_MAX) ||
					vInt2 == JASS_SLAYERPLACE ||
					vInt2 == JASS_DANCEPOOL ||
					vInt2 == JASS_DANCEPOOLTELEPORT)
				{
					fDoom(vvi,vvj,vInt2);
				}
				if (vInt2 == JASS_PATH_BLIGHT ||
					vInt2 == JASS_WATER_LEVEL_1 ||
					vInt2 == JASS_PATH_NOWATER)
				{
					fDoom(vvi,vvj,0);
				}
			}
		}
		vMap[vvi].vQTree = vQTree;
		vMap[vvi].vQCliff = vQCliff;
		vMap[vvi].vQObstacles = vQTree + vQCliff;
	}
	// Определения опасных клеток от молнии и рикошета
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vNBase != 0)
		{
			vMap[vvi].vLightning = FALSE;
			for (vvj = 1; vvj <= 10; vvj++)
			{
				if (fDefinitionOfADangerousCell(&vMap[vvi],vvj) == TRUE)
				{
					vMap[vvi].vLightning = TRUE;
					vMap[vvi].vRicochet = TRUE;
					break;
				}
			}
		}
	}
	// Удалённость от центра карты
	if (fDistanceFromTheCenterOfTheMap() == FALSE) return FALSE;
	// Высоты
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vCliffLvl = GetTerrainCliffLevel(fConvertIToRX(vMap[vvi].vInt3),fConvertIToRY(vMap[vvi].vInt3));
	}
	// Красные клетки
	group g=CreateGroup();
	unit u;
	unsigned short vMinX, vMinY, vMaxX, vMaxY;
	GroupEnumUnitsOfPlayer(g,Player(PLAYER_NEUTRAL_PASSIVE),0);
	do
	{
		u=FirstOfGroup(g);
		//pDataUnit = fGetDataUnit(u);
		if (u != NULL &&
			GetUnitTypeId(u) == ID_UNIT_PASSIVE_BLOOD)
		{
			vMinX = fConverterX(GetUnitX(u)-64);
			vMinY = fConverterY(GetUnitY(u)+64);
			vMaxX = fConverterX(GetUnitX(u)+64);
			vMaxY = fConverterY(GetUnitY(u)-64);
			for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
			{
				if (vMap[vvi].vX >= vMinX &&
					vMap[vvi].vX <= vMaxX &&
					vMap[vvi].vY >= vMinY &&
					vMap[vvi].vY <= vMaxY)
				{
					vMap[vvi].vRedCell = TRUE;
				}
			}
		}
		GroupRemoveUnit(g,u);
	}
	while (u != 0);
	DestroyGroup(g);
	// Тупики и базы
	//if (fDeadEnds() == FALSE) return FALSE;
	//fWriteFileTGA();
	return TRUE;
}

bool MapBases::fDoom(unsigned short vNSmallCage2, int vvj, int vInt2)
{
	if (vInt2 < 0 || vNSmallCage2 < 0 || vvj < 0) return FALSE;
	vNSmallCage3 = vNSmallCage2;
	vSetOff = 0;
	switch(vvj)
	{
	case 0:
		for (vvz = 0; vvz < 2; vvz++)
		{
			vSetOff = vvz * (-MAP_W);// Верх
			if (vNSmallCage3+vSetOff>=0 && vMap[vNSmallCage3+vSetOff].vY >= 0)
			{
				vMap[vNSmallCage3+vSetOff].vDoom.vBool = TRUE;
				vMap[vNSmallCage3+vSetOff].vDoom.vNBaseBottom = vInt2;
			}
		}
		break;
	case 1:
		for (vvz = 0; vvz < 2; vvz++)
		{
			vSetOff = vvz * (1);// Право
			if (vNSmallCage3+vSetOff<=(MAP_MAX_SMALL_CAGE-1) && vMap[vNSmallCage3+vSetOff].vX < MAP_W)
			{
				vMap[vNSmallCage3+vSetOff].vDoom.vBool = TRUE;
				vMap[vNSmallCage3+vSetOff].vDoom.vNBaseLeft = vInt2;
			}
		}
		break;
	case 2:
		for (vvz = 0; vvz < 2; vvz++)
		{
			vSetOff = vvz * (MAP_W);// Вниз
			if (vNSmallCage3+vSetOff<=(MAP_MAX_SMALL_CAGE-1) && vMap[vNSmallCage3+vSetOff].vY < MAP_H)
			{
				vMap[vNSmallCage3+vSetOff].vDoom.vBool = TRUE;
				vMap[vNSmallCage3+vSetOff].vDoom.vNBaseTop = vInt2;
			}
		}
		break;
	case 3:
		for (vvz = 0; vvz < 2; vvz++)
		{
			vSetOff = vvz * (-1);// Влево
			if (vNSmallCage3+vSetOff>=0 && vMap[vNSmallCage3+vSetOff].vX >= 0)
			{
				vMap[vNSmallCage3+vSetOff].vDoom.vBool = TRUE;
				vMap[vNSmallCage3+vSetOff].vDoom.vNBaseRight = vInt2;
			}
		}
		break;
	default:
		return FALSE;
	}
	return TRUE;
}

HRECT MapBases::fFindRectForHouse(real vX, real vY, unsigned short vIdDataUnit)
{
	if (vIdDataUnit < 0 || vIdDataUnit >= MAX_COUNT_UNITS) return NULL;
	if (vDataUnits[vIdDataUnit].pDataUnit == NULL) return NULL;
	if (vDataUnits[vIdDataUnit].pDataUnit->vHead == NULL) return NULL;
	int vvi;
	sSmallCage *sSmallCage;
	sSmallCage = vDataUnits[vIdDataUnit].pDataUnit->vHead;
	if (vMap[sSmallCage->vNSmallCage - 1].vLightning == FALSE &&
		vMap[sSmallCage->vNSmallCage - 1].vTree.vLeft == TRUE)
	{
		return Rect(vX-128*2,vY+128*2,vX,vY-128*2);
	}
	if (vMap[sSmallCage->vNSmallCage + 2].vLightning == FALSE &&
		vMap[sSmallCage->vNSmallCage + 2].vTree.vRight == TRUE)
	{
		return Rect(vX,vY+128*2,vX+128*2,vY-128*2);
	}
	if (vMap[sSmallCage->vNSmallCage - MAP_W].vLightning == FALSE &&
		vMap[sSmallCage->vNSmallCage - MAP_W].vTree.vTop == TRUE)
	{
		return Rect(vX-128*2,vY+128*2,vX+128*2,vY);
	}
	if (vMap[sSmallCage->vNSmallCage + MAP_W*2].vLightning == FALSE &&
		vMap[sSmallCage->vNSmallCage + MAP_W*2].vTree.vLeft == TRUE)
	{
		return Rect(vX-128*2,vY,vX+128*2,vY-128*2);
	}
	return NULL;
}

// Очистить зону базы разведки местности
bool MapBases::fCleanZoneBaseExplorationOfTheArea(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vNBase == vBase->vNBase)
		{
			vMap[vvi].vExplorationOfTheArea = FALSE;
		}
	}
	return TRUE;
}

// Залить радиус обзора юнита
bool MapBases::fFillRadiusReviewUnit(DataUnit *pDataUnit)
{
	if (pDataUnit == NULL) return FALSE;
	fGetReviewRadius(pDataUnit->vId,&vRadiusReview);
	if (vRadiusReview <= 0) return FALSE;
	vRadiusMap = (unsigned short)(vRadiusReview/128);
	if (!(vRadiusMap >= 0 &&
			vRadiusMap < MAP_MAX_SMALL_CAGE))
	{
		return FALSE;
	}
	vInt3 = fConverterI(pDataUnit->LastCoordinates.X,pDataUnit->LastCoordinates.Y);
	if (pDataUnit->Fly == FALSE)
	{
		unsigned short vX1,vY1,vX2,vY2;
		vX1 = fConvertIToX(vInt3)/2;
		vY1 = fConvertIToY(vInt3)/2;
		vX2 = vX1+1;
		vY2 = vY1+1;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vWalk == TRUE &&
				vMap[vvi].vInt3 >= vInt3 &&
				vMap[vvi].vX >= vX1 &&
				vMap[vvi].vX <= vX2 &&
				vMap[vvi].vY >= vY1 &&
				vMap[vvi].vY <= vY2)
			{
				vNSmallCage = vvi;
				break;
			}
		}
		if (!(vNSmallCage >= 0 &&
			vNSmallCage < MAP_MAX_SMALL_CAGE))
		{
			return FALSE;
		}
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			vMap[vvi].vTemp = FALSE;
			vMap[vvi].vRemotenessTemp = 0;
		}
		vFlag = FALSE;
		vMap[vNSmallCage].vTemp = TRUE;
		vMap[vNSmallCage].vRemotenessTemp = 1;
		vRemoteness = 0;
		while (vFlag == FALSE)
		{
			vRemoteness++;
			if (vRemoteness >= vRadiusMap) break;
			vRemotenessNext = vRemoteness + 1;
			vFlag = TRUE;
			for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
			{
				if (vMap[vvi].vTemp == TRUE &&
					vMap[vvi].vRemotenessTemp == vRemoteness)
				{
					vCliffLvl = vMap[vvi].vCliffLvl;
					for (vvj = 0; vvj < 4; vvj++)
					{
						switch (vvj)
						{
						case 0:
							if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								if (vCliffLvl >= vMap[vvi+MAP_W].vCliffLvl &&
									vMap[vvi+MAP_W].vTemp == FALSE &&
									vMap[vvi+MAP_W].vRemotenessTemp == 0)
								{
									vMap[vvi+MAP_W].vTemp = TRUE;// Низ
									vMap[vvi+MAP_W].vRemotenessTemp = vRemotenessNext;
								}
							}
							break;
						case 1:
							if (vvi>=1 && vMap[vvi-1].vX >= 0)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								if (vCliffLvl >= vMap[vvi-1].vCliffLvl &&
									vMap[vvi-1].vTemp == FALSE &&
									vMap[vvi-1].vRemotenessTemp == 0)
								{
									vMap[vvi-1].vTemp = TRUE;// Лево
									vMap[vvi-1].vRemotenessTemp = vRemotenessNext;
								}
							}
							break;
						case 2:
							if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								if (vCliffLvl >= vMap[vvi-MAP_W].vCliffLvl &&
									vMap[vvi-MAP_W].vTemp == FALSE &&
									vMap[vvi-MAP_W].vRemotenessTemp == 0)
								{
									vMap[vvi-MAP_W].vTemp = TRUE;// Верх
									vMap[vvi-MAP_W].vRemotenessTemp = vRemotenessNext;
								}
							}
							break;
						case 3:
							if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								if (vCliffLvl >= vMap[vvi+1].vCliffLvl &&
									vMap[vvi+1].vTemp == FALSE &&
									vMap[vvi+1].vRemotenessTemp == 0)
								{
									vMap[vvi+1].vTemp = TRUE;// Право
									vMap[vvi+1].vRemotenessTemp = vRemotenessNext;
								}
							}
							break;
						}
					}
				}
			}
		}
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vTemp == TRUE)
			{
				vMap[vvi].vExplorationOfTheArea = TRUE;
			}
		}
	}
	return TRUE;
}

// Найти начало базы и позицию вала
bool MapBases::fDetermineTheBeginningOfTheBase(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	int vvi, vvj;
	unsigned short vRemoteness;
	unsigned short vRemoteness2;
	bool vFlag;
	bool vFlagWhile;
	bool vFlagOneCell;
	bool vFlagMainWall;
	vBase->vMainWall.vQRepairPos = 0;
	vFlagMainWall = FALSE;
	vFlagOneCell = FALSE;
	if (vBase->vMainWall.fGetWall() != NULL)
	{
		vRemoteness = vBase->vMainWall.fGetWall()->vRemoteness;
		vFlagOneCell = TRUE;
		vFlagMainWall = TRUE;
	}
	else
	{
		vRemoteness = 0;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vNBase == vBase->vNBase)
			{
				if ((vMap[vvi].vRemoteness > vBase->vMinRemoteness) &&
					(vRemoteness == 0 ||
					vMap[vvi].vRemoteness < vRemoteness))
				{
					vRemoteness = vMap[vvi].vRemoteness;
				}
			}
		}
		if (vRemoteness == 0)
		{
			return FALSE;
		}
	}
	vFlagWhile = TRUE;
	while (vFlagWhile == TRUE)
	{
	vFlag = FALSE;
	vFlagWhile = FALSE;
	for (vvj = vRemoteness - 5; vvj < vRemoteness + 5; vvj++)
	{
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (((vFlagMainWall == FALSE && vFlagOneCell == FALSE && vMap[vvi].vRemoteness == vvj) ||
			(vFlagMainWall == TRUE && vRemoteness2 == vMap[vvi].vRemoteness && (vMap[vvi].vRemoteness >= (vRemoteness + 5) && vMap[vvi].vRemoteness <= (vRemoteness + 50))) ||
			(
			(vFlagOneCell == TRUE && vFlagMainWall == TRUE) && (vMap[vvi].vRemoteness > vRemoteness) && ((vMap[vvi].vRemoteness - vRemoteness) <= 25)
			)
			)  &&
			vMap[vvi].vX >= 2 &&
			vMap[vvi].vX <= MAP_W - 4 &&
			vMap[vvi].vY >= 2 &&
			vMap[vvi].vY <= MAP_H - 4 &&
			(vMap[vvi-2].vNBase == vBase->vNBase ||
			vMap[vvi+3].vNBase == vBase->vNBase ||
			vMap[vvi-MAP_W*2].vNBase == vBase->vNBase ||
			vMap[vvi+MAP_W*3].vNBase == vBase->vNBase) &&
			vMap[vvi].vQObstacles == 0 &&
			vMap[vvi].vBuild == TRUE &&
			vMap[vvi+1].vQObstacles == 0 &&
			vMap[vvi+1].vBuild == TRUE &&
			vMap[vvi+1+MAP_W].vQObstacles == 0 &&
			vMap[vvi+1+MAP_W].vBuild == TRUE &&
			vMap[vvi+MAP_W].vQObstacles == 0 &&
			vMap[vvi+MAP_W].vBuild == TRUE &&
			((vMap[vvi-1].vQObstacles == 1 &&//1
			vMap[vvi-1].vBuild == TRUE &&
			vMap[vvi-1+MAP_W].vQObstacles == 1 &&//2
			vMap[vvi-1+MAP_W].vBuild == TRUE &&
			vMap[vvi+2].vQObstacles == 1 &&//3
			vMap[vvi+2].vBuild == TRUE &&
			vMap[vvi+2+MAP_W].vQObstacles == 1 &&//4
			vMap[vvi+2+MAP_W].vBuild == TRUE &&
			vMap[vvi-1+MAP_W*2].vQObstacles == 1 &&//5
			vMap[vvi-1+MAP_W*2].vBuild == TRUE &&
			vMap[vvi+2+MAP_W*2].vQObstacles == 1 &&//6
			vMap[vvi+2+MAP_W*2].vBuild == TRUE &&
			vMap[vvi-1-MAP_W*1].vQObstacles == 1 &&//7
			vMap[vvi-1-MAP_W*1].vBuild == TRUE &&
			vMap[vvi+2-MAP_W*1].vQObstacles == 1 &&//8
			vMap[vvi+2-MAP_W*1].vBuild == TRUE) ||
			(vMap[vvi-MAP_W*1].vQObstacles == 1 &&//1
			vMap[vvi-MAP_W*1].vBuild == TRUE &&
			vMap[vvi-MAP_W*1+1].vQObstacles == 1 &&//2
			vMap[vvi-MAP_W*1+1].vBuild == TRUE &&
			vMap[vvi+MAP_W*2].vQObstacles == 1 &&//3
			vMap[vvi+MAP_W*2].vBuild == TRUE &&
			vMap[vvi+MAP_W*2+1].vQObstacles == 1 &&//4
			vMap[vvi+MAP_W*2+1].vBuild == TRUE &&
			vMap[vvi-MAP_W*1+2].vQObstacles == 1 &&//5
			vMap[vvi-MAP_W*1+2].vBuild == TRUE &&
			vMap[vvi+MAP_W*2+2].vQObstacles == 1 &&//6
			vMap[vvi+MAP_W*2+2].vBuild == TRUE &&
			vMap[vvi-MAP_W*1-1].vQObstacles == 1 &&//7
			vMap[vvi-MAP_W*1-1].vBuild == TRUE &&
			vMap[vvi+MAP_W*2-1].vQObstacles == 1 &&//8
			vMap[vvi+MAP_W*2-1].vBuild == TRUE)))
		{
			vBase->vMainWall.fSetWall(&vMap[vvi]);
			vMap[vvi].vNBase = vBase->vNBase;
			vMap[vvi+1].vNBase = vBase->vNBase;
			vMap[vvi+MAP_W*2].vNBase = vBase->vNBase;
			vMap[vvi+MAP_W*2+1].vNBase = vBase->vNBase;
			if (fClearRemotenessBase(vBase) && fRemotenessBase(vBase) < 40)
			{
				fClearRemotenessBase(vBase);
				vBase->vMainWall.fSetWall(NULL);
				goto m2;
			}
			if (fZoneTowerTemp2(vBase) == TRUE)
			{
				if (vBase->vQTowerInZoneMainWall < 20)
				{
					vBase->vMainWall.fSetWall(NULL);
					goto m2;
				}
			}
			vRemoteness = vMap[vvi].vRemoteness;
			vRemoteness2 = vRemoteness;
			vBase->vBeginningOfBase = vRemoteness;
			if (vFlag == FALSE) vFlag = TRUE;
			if (vFlagMainWall == FALSE)
			{
				vFlagWhile = TRUE;
			}
			if (vFlagMainWall == FALSE)
			{
				vFlagMainWall = TRUE;
				if (vFlagOneCell == FALSE)
				{
					vBase->vMainWall2.fSetWall(&vMap[vvi]);
				}
			}
			if (vFlagOneCell == TRUE)
			{
				vFlagWhile = FALSE;
			}
			goto m1;
			break;
m2:;
		}
	}
	if (vFlagMainWall == TRUE) break;
	}
	if (vFlagMainWall == TRUE &&
		vRemoteness2 < vRemoteness + 50)
	{
		vRemoteness2++;
		vFlagWhile = TRUE;
	}
m1:;
	}
	if (vBase->vMainWall.fGetWall() == vBase->vMainWall2.fGetWall())
	{
		vBase->vMainWall2.fSetWall(NULL);
	}
	else if (vBase->vMainWall.fGetWall() == NULL &&
			 vBase->vMainWall2.fGetWall())
	{
		vBase->vMainWall.fSetWall(vBase->vMainWall2.fGetWall());
		vBase->vMainWall2.fSetWall(NULL);
	}
	return vFlag;
}

// Найти начало базы и позицию вала с одноклеточным проходом на базу
bool MapBases::fDetermineTheBeginningOfTheBaseOneCell(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	int vvi, vvj;
	unsigned short vRemoteness;
	bool vFlag;
	vBase->vMainWall.vQRepairPos = 0;
	vRemoteness = 0;
	vFlag = FALSE;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vNBase == vBase->vNBase)
		{
			if ((vMap[vvi].vRemoteness > vBase->vMinRemoteness) &&
				(vRemoteness == 0 ||
				vMap[vvi].vRemoteness < vRemoteness))
			{
				vRemoteness = vMap[vvi].vRemoteness;
			}
		}
	}
	if (vRemoteness == 0)
	{
		return FALSE;
	}
	for (vvj = vRemoteness - 5; vvj < vRemoteness + 5; vvj++)
	{
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if ((vMap[vvi].vRemoteness == vvj) &&
			vMap[vvi].vX >= 2 &&
			vMap[vvi].vX <= MAP_W - 4 &&
			vMap[vvi].vY >= 2 &&
			vMap[vvi].vY <= MAP_H - 4 &&
			(vMap[vvi-2].vNBase == vBase->vNBase ||
			vMap[vvi+3].vNBase == vBase->vNBase ||
			vMap[vvi-MAP_W*2].vNBase == vBase->vNBase ||
			vMap[vvi+MAP_W*3].vNBase == vBase->vNBase) &&
			((vMap[vvi].vQObstacles == 0 &&
			vMap[vvi].vBuild == TRUE) ||
			(vMap[vvi+1].vQObstacles == 0 &&
			vMap[vvi+1].vBuild == TRUE) ||
			(vMap[vvi+MAP_W*1+1].vQObstacles == 0 &&
			vMap[vvi+MAP_W*1+1].vBuild == TRUE) ||
			(vMap[vvi+MAP_W*1].vQObstacles == 0 &&
			vMap[vvi+MAP_W*1].vBuild == TRUE)) &&
			(((vMap[vvi+1].vTree.vTop == TRUE ||
			vMap[vvi+1].vCliff.vTop == TRUE) &&
			vMap[vvi+1].vQObstacles == 1 &&
			vMap[vvi+1].vBuild == TRUE &&
			vMap[vvi+1+MAP_W].vQObstacles == 1 &&
			vMap[vvi+1+MAP_W].vBuild == TRUE) ||
			((vMap[vvi+MAP_W*1].vTree.vLeft == TRUE ||
			vMap[vvi+MAP_W*1].vCliff.vLeft == TRUE) &&
			vMap[vvi+MAP_W*1].vQObstacles == 1 &&
			vMap[vvi+MAP_W*1].vBuild == TRUE &&
			vMap[vvi+MAP_W*1+1].vQObstacles == 1 &&
			vMap[vvi+MAP_W*1+1].vBuild == TRUE) ||
			((vMap[vvi].vTree.vTop == TRUE ||
			vMap[vvi].vCliff.vTop == TRUE) &&
			vMap[vvi].vQObstacles == 1 &&
			vMap[vvi].vBuild == TRUE &&
			vMap[vvi+MAP_W*1].vQObstacles == 1 &&
			vMap[vvi+MAP_W*1].vBuild == TRUE) ||
			((vMap[vvi].vTree.vLeft == TRUE ||
			vMap[vvi].vCliff.vLeft == TRUE) &&
			vMap[vvi].vQObstacles == 1 &&
			vMap[vvi].vBuild == TRUE &&
			vMap[vvi+1].vQObstacles == 1 &&
			vMap[vvi+1].vBuild == TRUE)))
		{
			vBase->vMainWall.fSetWall(&vMap[vvi]);
			vMap[vvi].vNBase = vBase->vNBase;
			vMap[vvi+1].vNBase = vBase->vNBase;
			vMap[vvi+MAP_W*2].vNBase = vBase->vNBase;
			vMap[vvi+MAP_W*2+1].vNBase = vBase->vNBase;
			if (fFindPosBuild(vBase,ID_UNIT_BUILD_WALL_BROWN,0,0,FALSE,TRUE,TRUE) != vSizeMap)
			{
				if (vBase->vMainWall.vQRepairPos < 6)
				{
					vBase->vMainWall.fSetWall(NULL);
					goto m2;
				}
			}
			else
			{
				vBase->vMainWall.fSetWall(NULL);
				goto m2;
			}
			if (fClearRemotenessBase(vBase) && fRemotenessBase(vBase) < 40)
			{
				fClearRemotenessBase(vBase);
				vBase->vMainWall.fSetWall(NULL);
				goto m2;
			}
			if (fZoneTowerTemp2(vBase) == TRUE)
			{
				if (vBase->vQTowerInZoneMainWall < 20)
				{
					vBase->vMainWall.fSetWall(NULL);
					goto m2;
				}
			}
			vRemoteness = vMap[vvi].vRemoteness;
			vBase->vBeginningOfBase = vRemoteness;
			if (vFlag == FALSE) vFlag = TRUE;
			goto m1;
m2:;
		}
	}
	}
m1:;
	return vFlag;
}

// Забиндить позицию помощи игрока
bool MapBases::fBindNPlayerHelp(sBase *vBase, integer vNPlayer)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	if (vNPlayer >= 0 && vNPlayer < MAX_HUMAN_PLAYERS)
	if (vBase->vNPlayerHelp[vNPlayer] == FALSE)
	{
		vBase->vNPlayerHelp[vNPlayer] = TRUE;
		return TRUE;
	}
	return FALSE;
}

// Освободить позицию помощи игрока
bool MapBases::fFreeBindNPlayerHelp(sBase *vBase, integer vNPlayer)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	if (vNPlayer >= 0 && vNPlayer < MAX_HUMAN_PLAYERS)
	if (vBase->vNPlayerHelp[vNPlayer] == TRUE)
	{
		vBase->vNPlayerHelp[vNPlayer] = FALSE;
		return TRUE;
	}
	return FALSE;
}

// Найти позицию защиты вала на базе слаером свою
unsigned int MapBases::fFindMyPosSlayerDefense(sBase *vBase, HumanPlayer *pHumanPlayer)
{
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	if (vBase->fGetWall() == NULL) return vSizeMap;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQSlayerDefensePos; vvi++)
	{
		if (vBase->fGetWall()->vMassSlayerDefense[vvi].vBind == TRUE &&
			fCheckPos(vBase->fGetWall()->vMassSlayerDefense[vvi].vInt3,2,2) == TRUE &&
			vBase->fGetWall()->vMassSlayerDefense[vvi].pHumanPlayer == pHumanPlayer)
		{
			return vBase->fGetWall()->vMassSlayerDefense[vvi].vInt3;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQSlayerDefensePosBehind; vvi++)
	{
		if (vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vBind == TRUE &&
			fCheckPos(vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vInt3,2,2) == TRUE &&
			vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].pHumanPlayer == pHumanPlayer)
		{
			return vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vInt3;
		}
	}
	return vSizeMap;
}

// Найти позицию защиты вала на базе слаером и забиндить её
unsigned int MapBases::fFindPosSlayerDefense(sBase *vBase, HumanPlayer *pHumanPlayer)
{
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	if (vBase->fGetWall() == NULL) return vSizeMap;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQSlayerDefensePos; vvi++)
	{
		if (vBase->fGetWall()->vMassSlayerDefense[vvi].vBind == FALSE &&
			fCheckPos(vBase->fGetWall()->vMassSlayerDefense[vvi].vInt3,2,2) == TRUE)
		{
			vBase->fGetWall()->vMassSlayerDefense[vvi].vBind = TRUE;
			vBase->fGetWall()->vMassSlayerDefense[vvi].pHumanPlayer = pHumanPlayer;
			return vBase->fGetWall()->vMassSlayerDefense[vvi].vInt3;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQSlayerDefensePosBehind; vvi++)
	{
		if (vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vBind == FALSE &&
			fCheckPos(vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vInt3,2,2) == TRUE)
		{
			vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vBind = TRUE;
			vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].pHumanPlayer = pHumanPlayer;
			return vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vInt3;
		}
	}
	return vSizeMap;
}

// Освободить позицию защиты вала
bool MapBases::fFreePosSlayerDefense(sBase *vBase, HumanPlayer *pHumanPlayer)
{
	if (pHumanPlayer == NULL) return FALSE;
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	if (vBase->fGetWall() == NULL) return vSizeMap;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQSlayerDefensePos; vvi++)
	{
		if (vBase->fGetWall()->vMassSlayerDefense[vvi].vBind == TRUE &&
			vBase->fGetWall()->vMassSlayerDefense[vvi].pHumanPlayer == pHumanPlayer)
		{
			vBase->fGetWall()->vMassSlayerDefense[vvi].vBind = FALSE;
			return TRUE;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQSlayerDefensePosBehind; vvi++)
	{
		if (vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vBind == TRUE &&
			vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].pHumanPlayer == pHumanPlayer)
		{
			vBase->fGetWall()->vMassSlayerDefenseBehind[vvi].vBind = FALSE;
			return TRUE;
		}
	}
	return FALSE;
}

// Найти позицию ремонта вала и забиндить её
unsigned int MapBases::fFindPosRepair(sBase *vBase, HumanPlayer *pHumanPlayer)
{
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	if (vBase->fGetWall() == NULL) return vSizeMap;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPos; vvi++)
	{
		if (vBase->fGetWall()->vRepairPos[vvi].vBind == FALSE &&
			fCheckPos(vBase->fGetWall()->vRepairPos[vvi].vInt3,1,1) == TRUE)
		{
			vBase->fGetWall()->vRepairPos[vvi].vBind = TRUE;
			vBase->fGetWall()->vRepairPos[vvi].pHumanPlayer = pHumanPlayer;
			return vBase->fGetWall()->vRepairPos[vvi].vInt3;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPosBehind; vvi++)
	{
		if (vBase->fGetWall()->vRepairPosBehind[vvi].vBind == FALSE &&
			fCheckPos(vBase->fGetWall()->vRepairPosBehind[vvi].vInt3,1,1) == TRUE)
		{
			vBase->fGetWall()->vRepairPosBehind[vvi].vBind = TRUE;
			vBase->fGetWall()->vRepairPosBehind[vvi].pHumanPlayer = pHumanPlayer;
			return vBase->fGetWall()->vRepairPosBehind[vvi].vInt3;
		}
	}
	return vSizeMap;
}

// Проверить все позиции ремонта вала
bool MapBases::fCheckAllPosRepair(sBase *vBase, HumanPlayer *pHumanPlayer)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	if (vBase->fGetWall() == NULL) return FALSE;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPos; vvi++)
	{
		if (vBase->fGetWall()->vRepairPos[vvi].vBind == TRUE &&
			fCheckPos(vBase->fGetWall()->vRepairPos[vvi].vInt3,1,1) == TRUE)
		{
			for (vvz = 0; vvz < vBase->fGetWall()->vQRepairPos; vvz++)
			{
				if (vvi == vvz) continue;
				if (vBase->fGetWall()->vRepairPos[vvi].pHumanPlayer == vBase->fGetWall()->vRepairPos[vvz].pHumanPlayer &&
					vBase->fGetWall()->vRepairPos[vvz].vBind == TRUE)
				{
					vBase->fGetWall()->vRepairPos[vvi].vBind = FALSE;
					vBase->fGetWall()->vRepairPos[vvi].pHumanPlayer = NULL;
				}
			}
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPosBehind; vvi++)
	{
		if (vBase->fGetWall()->vRepairPosBehind[vvi].vBind == TRUE &&
			fCheckPos(vBase->fGetWall()->vRepairPosBehind[vvi].vInt3,1,1) == TRUE)
		{
			for (vvz = 0; vvz < vBase->fGetWall()->vQRepairPosBehind; vvz++)
			{
				if (vvi == vvz) continue;
				if (vBase->fGetWall()->vRepairPosBehind[vvi].pHumanPlayer == vBase->fGetWall()->vRepairPosBehind[vvz].pHumanPlayer &&
					vBase->fGetWall()->vRepairPosBehind[vvz].vBind == TRUE)
				{
					vBase->fGetWall()->vRepairPosBehind[vvz].vBind = FALSE;
					vBase->fGetWall()->vRepairPosBehind[vvz].pHumanPlayer = NULL;
				}
			}
		}
	}
	return TRUE;
}

bool MapBases::fPosBindTrue(sBase *vBase, unsigned int vInt3)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	if (vBase->fGetWall() == NULL) return FALSE;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPos; vvi++)
	{
		if (vBase->fGetWall()->vRepairPos[vvi].vBind == FALSE &&
			vBase->fGetWall()->vRepairPos[vvi].vInt3 == vInt3)
		{
			vBase->fGetWall()->vRepairPos[vvi].vBind = TRUE;
			return TRUE;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPosBehind; vvi++)
	{
		if (vBase->fGetWall()->vRepairPosBehind[vvi].vBind == FALSE &&
			vBase->fGetWall()->vRepairPosBehind[vvi].vInt3 == vInt3)
		{
			vBase->fGetWall()->vRepairPosBehind[vvi].vBind = TRUE;
			return TRUE;
		}
	}
	return FALSE;
}

bool MapBases::fCheckPos(unsigned int vInt3, unsigned short vXBuild, unsigned short vYBuild)
{
	for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
	{
		if ((vMap[vvj].vBuild == TRUE ||
			vMap[vvj].vInt == JASS_PATH_BLIGHT) &&
			vMap[vvj].vInt3 == vInt3)
		{
			if (fCheckFillUnit(&vMap[vvj],vXBuild,vYBuild,NO_UNIT) == TRUE)
			{
				return TRUE;
			}
		}
	}
	return FALSE;
}

// Проверить позицию бинда ремонта вала
bool MapBases::fCheckPosBind(sBase *vBase, unsigned int vInt3)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	if (vBase->fGetWall() == NULL) return FALSE;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPos; vvi++)
	{
		if (vBase->fGetWall()->vRepairPos[vvi].vBind == TRUE &&
			vBase->fGetWall()->vRepairPos[vvi].vInt3 == vInt3)
		{
			return TRUE;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPosBehind; vvi++)
	{
		if (vBase->fGetWall()->vRepairPosBehind[vvi].vBind == TRUE &&
			vBase->fGetWall()->vRepairPosBehind[vvi].vInt3 == vInt3)
		{
			return TRUE;
		}
	}
	return FALSE;
}

bool MapBases::fFreePosRepair(sBase *vBase, HumanPlayer *pHumanPlayer)
{
	if (pHumanPlayer == NULL) return FALSE;
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	if (vBase->fGetWall() == NULL) return vSizeMap;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPos; vvi++)
	{
		if (vBase->fGetWall()->vRepairPos[vvi].vBind == TRUE &&
			vBase->fGetWall()->vRepairPos[vvi].pHumanPlayer == pHumanPlayer)
		{
			vBase->fGetWall()->vRepairPos[vvi].vBind = FALSE;
			vBase->fGetWall()->vRepairPos[vvi].pHumanPlayer = NULL;
			return TRUE;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPos; vvi++)
	{
		if (vBase->fGetWall()->vRepairPosBehind[vvi].vBind == TRUE &&
			vBase->fGetWall()->vRepairPosBehind[vvi].pHumanPlayer == pHumanPlayer)
		{
			vBase->fGetWall()->vRepairPosBehind[vvi].vBind = FALSE;
			vBase->fGetWall()->vRepairPosBehind[vvi].pHumanPlayer = NULL;
			return TRUE;
		}
	}
	return FALSE;
}

// Освободить забинденую позицию ремонта
bool MapBases::fFreePosRepairBind(sBase *vBase, unsigned int vInt3)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	if (vBase->fGetWall() == NULL) return vSizeMap;
	if (vBase->fGetWall()->vBehind == FALSE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPos; vvi++)
	{
		if (vBase->fGetWall()->vRepairPos[vvi].vBind == TRUE &&
			vBase->fGetWall()->vRepairPos[vvi].vInt3 == vInt3)
		{
			vBase->fGetWall()->vRepairPos[vvi].vBind = FALSE;
			vBase->fGetWall()->vRepairPos[vvi].pHumanPlayer = NULL;
			return TRUE;
		}
	}
	if (vBase->fGetWall()->vBehind == TRUE)
	for (vvi = 0; vvi < vBase->fGetWall()->vQRepairPosBehind; vvi++)
	{
		if (vBase->fGetWall()->vRepairPosBehind[vvi].vBind == TRUE &&
			vBase->fGetWall()->vRepairPosBehind[vvi].vInt3 == vInt3)
		{
			vBase->fGetWall()->vRepairPosBehind[vvi].vBind = FALSE;
			vBase->fGetWall()->vRepairPosBehind[vvi].pHumanPlayer = NULL;
			return TRUE;
		}
	}
	return FALSE;
}

// Рандомная позиция на базе
unsigned int MapBases::fRandPosBase(sBase *vBase1)
{
	if (fNull2((unsigned int)vBase1)) return vSizeMap;
	for (vvi = GetRandomInt(0,MAP_MAX_SMALL_CAGE-1); vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vNBase == vBase1->vNBase &&
			vMap[vvi].vRemotenessBase > 0)
		{
			return vMap[vvi].vInt3;
		}
	}
	return vSizeMap;
}

// Следующая позиция для разведки базы
unsigned int MapBases::fNextPosBaseForExplorationOfTheArea(unsigned int vInt3, sBase *vBase1)
{
	if (fNull2((unsigned int)vBase1)) return vSizeMap;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vBusy == FALSE &&
			vMap[vvi].vNBase == vBase1->vNBase &&
			vMap[vvi].vRemotenessBase > 0 &&
			vMap[vvi].vExplorationOfTheArea == FALSE &&
			vMap[vvi].vInt3 >= vInt3)
		{
			return vMap[vvi].vInt3;
		}
	}
	return vSizeMap;
}

// Найти позицию безопасной зоны на базе
unsigned int MapBases::fFindPosSafeZone(sBase *vBase1)
{
	if (fNull2((unsigned int)vBase1)) return vSizeMap;
	vBase = NULL;
	if (vBase1 &&
		vBase1->vNBase == DO_NOT_FIND_BASE)
	{
		for (vvi = 0; vvi < vCountHumans; vvi++)
		{
			if (pHumanPlayerGlobal[vvi].vEndGame == FALSE &&
				pHumanPlayerGlobal[vvi].vBase.vNBase != DO_NOT_FIND_BASE)
			{
				vBase = &pHumanPlayerGlobal[vvi].vBase;
				break;
			}
		}
	}
	else
	{
		vBase = vBase1;
	}
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	//
	fFreeBusy(vBase);
	fFillBusy(vBase);
	if (vBase->vQHeal > 0)
	{
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vBuild == TRUE &&
				vMap[vvi].vNBase == vBase->vNBase &&
				vMap[vvi].vTower == TRUE &&
				vMap[vvi].vGrive == FALSE &&
				vBase->vMainWall.fGetWall() &&
				vMap[vvi].vRemoteness > vBase->vMainWall.fGetWall()->vRemoteness &&
				((((vBase->vMainWall.fGetWall()->vRemoteness - vMap[vvi].vRemoteness) <= 10) &&
				vBase->vSlayerBaze == FALSE) ||
				((vBase->vMainWall.fGetWall()->vRemoteness - vMap[vvi].vRemoteness) <= 10) &&
				vBase->vSlayerBaze == TRUE) &&
				fCheckFillBuild(&vMap[vvi],2,2,NO_UNIT,FALSE) == TRUE)
			{
				return vMap[vvi].vInt3;
			}
		}
	}
	//
	for (vvj = 0; vvj < 2; vvj++)
	{
		if (vvj == 0)
		{
			vFlagLightning = FALSE;
		}
		if (vvj == 1)
		{
			vFlagLightning = TRUE;
		}
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vBuild == TRUE &&
				vMap[vvi].vNBase == vBase->vNBase &&
				vMap[vvi].vTower == FALSE &&
				vMap[vvi].vGrive == FALSE &&
				vMap[vvi].vLightning == vFlagLightning &&
				vBase->vMainWall.fGetWall() &&
				vMap[vvi].vRemoteness > vBase->vMainWall.fGetWall()->vRemoteness &&
				((((vBase->vMainWall.fGetWall()->vRemoteness - vMap[vvi].vRemoteness) <= 25) &&
				vBase->vSlayerBaze == FALSE) ||
				((vBase->vMainWall.fGetWall()->vRemoteness - vMap[vvi].vRemoteness) <= 40) &&
				vBase->vSlayerBaze == TRUE) &&
				fCheckFillBuild(&vMap[vvi],2,2,NO_UNIT,FALSE) == TRUE)
			{
				return vMap[vvi].vInt3;
			}
		}
	}
	return vSizeMap;
}

// Подсчитать кол-во клеток на базе
unsigned short MapBases::fQCellsToBase(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return 0;
	int vvi;
	unsigned short vQ;
	vQ = 0;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vNBase == vBase->vNBase)
		{
			vQ++;
		}
	}
	return vQ;
}

// Найти позицию здания
unsigned int MapBases::fFindPosBuild(sBase *vBase, integer vId)
{
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	vInt3 = vSizeMap;
	vBase->vHouse = COMMENT_NO;
	switch(vId)
	{
	case ID_UNIT_BUILD_DOM:
		if (vBase->pHumanPlayer && vBase->pHumanPlayer->vCurrentChain)
		switch (vBase->pHumanPlayer->vCurrentChain->vComment)
		{
		case COMMENT_FIRST_DOM:
		case COMMENT_SECOND_DOM:
		case COMMENT_THIRD_DOM:
		case COMMENT_FOURTH_DOM:
		case COMMENT_FIFTH_DOM:
		case COMMENT_SIXTH_DOM:
		case COMMENT_HOUSE_FOR_HARVESTING:
			vBase->vHouse = COMMENT_HOUSE_FOR_HARVESTING;
			vInt3 = fFindHouse(vBase);
			if (vInt3 == vSizeMap)
			{
				vInt3 = fFindPosBuild(vBase,vId,10,120,FALSE,FALSE,FALSE);
			}
			break;
		case COMMENT_HOUSE_FOR_BLOCK_VAMPIRE:
		case COMMENT_HOUSE_FOR_BLOCK_VAMPIRE_2:
		case COMMENT_HOUSE_FOR_BLOCK_VAMPIRE_3:
		case COMMENT_HOUSE_FOR_BLOCK_VAMPIRE_4:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,0,120,FALSE,FALSE,TRUE);
			break;
		case COMMENT_HOUSE_FOR_BLOCK_DOOM:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,0,120,FALSE,FALSE,TRUE);
			break;
		case COMMENT_BUSY_UNIT:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,-80,0,FALSE,FALSE,TRUE);
			break;
		}
		break;
	case ID_UNIT_BUILD_TENT:
		vInt3 = fFindPosBuild(vBase,vId,20,120,FALSE,FALSE,FALSE);
		break;
	case ID_UNIT_BUILD_SLAYER_TAVERN:
	case ID_UNIT_BUILD_KUZNYA:
	case ID_UNIT_BUILD_ZAMOK:
	case ID_UNIT_BUILD_TOVERN:
		vInt3 = fFindPosBuild(vBase,vId,10,120,FALSE,FALSE,FALSE);
		break;
	case ID_UNIT_BUILD_HRAM:
		vInt3 = fFindPosBuild(vBase,vId,10,120-GetRandomInt(0,80),FALSE,FALSE,FALSE);
		break;
	case ID_UNIT_BUILD_WALL_BROWN:
	case ID_UNIT_BUILD_WALL_VIOLET:
	case ID_UNIT_BUILD_WALL_RUBY:
	case ID_UNIT_BUILD_WALL_DIAMOND:
		if (vBase->pHumanPlayer && vBase->pHumanPlayer->vCurrentChain)
		switch (vBase->pHumanPlayer->vCurrentChain->vComment)
		{
		case COMMENT_MAIN_WALL:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,0,0,FALSE,TRUE,TRUE);
			break;
		case COMMENT_MAIN_WALL_2:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,-50,0,FALSE,FALSE,TRUE);
			break;
		case COMMENT_WALLS_DOOM:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,0,120,FALSE,TRUE,TRUE);
			break;
		}
		break;
	case ID_UNIT_BUILD_TOWER_WHITE:
	case ID_UNIT_BUILD_TOWER_KONUS:
	case ID_UNIT_BUILD_TOWER_EARTH_LVL1:
		if (vBase->pHumanPlayer && vBase->pHumanPlayer->vCurrentChain)
		switch (vBase->pHumanPlayer->vCurrentChain->vComment)
		{
		case COMMENT_TOWER_HOUSE:
		case COMMENT_TOWER_FOR_BLOCK_SHADE_1:
		case COMMENT_TOWER_FOR_BLOCK_SHADE_2:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,0,120,FALSE,FALSE,TRUE);
			break;
		case COMMENT_ANTI_INVIZ_WALL:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,0,15,FALSE,TRUE,FALSE);
			break;
		default:
			vInt3 = fFindPosBuild(vBase,vId,0,120,FALSE,TRUE,TRUE);
			break;
		}
		break;
	case ID_UNIT_BUILD_TOWER_HEAL_LVL1:
	case ID_UNIT_BUILD_TOWER_HEAL_LVL2:
		vInt3 = fFindPosBuild(vBase,vId,2,7,FALSE,TRUE,FALSE);
		break;
	case ID_UNIT_BUILD_GOLD_MINE_WHITE:
	case ID_UNIT_BUILD_GOLD_MINE_GREEN:
	case ID_UNIT_BUILD_GOLD_MINE_RED:
	case ID_UNIT_BUILD_GOLD_MINE_GOLD:
		vInt3 = fFindPosBuild(vBase,vId,15,120,TRUE,FALSE,FALSE);
		break;
	case ID_UNIT_BUILD_BOX_LVL1:
	case ID_UNIT_BUILD_BOX_LVL2:
		vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
		vInt3 = fFindPosBuild(vBase,vId,0,120,FALSE,TRUE,TRUE);
		break;
	case ID_UNIT_BUILD_TOWER_MANA_LVL1:
	case ID_UNIT_BUILD_TOWER_MANA_LVL2:
	case ID_UNIT_BUILD_TOWER_DIAMOND_LVL1:
	case ID_UNIT_BUILD_TOWER_FROST:
	case ID_UNIT_BUILD_TOWER_COMET_LVL1:
	case ID_UNIT_BUILD_TOWER_COMET_LVL2:
	case ID_UNIT_BUILD_TOWER_YAD_LVL1:
	case ID_UNIT_BUILD_TOWER_SPIRE_LVL1:
	case ID_UNIT_BUILD_TOWER_SPIRE_LVL2:
	case ID_UNIT_BUILD_TOWER_BLOOD_LVL2:
	case ID_UNIT_BUILD_TOWER_ORACLE_LVL1:
	case ID_UNIT_BUILD_TOWER_ORACLE_LVL2:
		if (vBase->pHumanPlayer && vBase->pHumanPlayer->vCurrentChain)
		switch (vBase->pHumanPlayer->vCurrentChain->vComment)
		{
		case COMMENT_COMET_FIRST:
			vBase->vHouse = vBase->pHumanPlayer->vCurrentChain->vComment;
			vInt3 = fFindPosBuild(vBase,vId,0,30,TRUE,TRUE,FALSE);
			break;
		default:
			vInt3 = fFindPosBuild(vBase,vId,0,25,FALSE,TRUE,FALSE);
		}
		break;
	default:
		vInt3 = fFindPosBuild(vBase,vId,0,120,FALSE,FALSE,TRUE);
	}
	return vInt3;
}

// Найти позицию здания
unsigned int MapBases::fFindPosBuild(sBase *vBase, integer vId, int vR1, int vR2, bool vFlag, bool vFlagTower, bool vBoolDoom)
{
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	vBase->vBuild = NULL;
	if (fGetXYBuild(vId,&vXBuild,&vYBuild) == FALSE) return vSizeMap;
	// Позиции юнитов
	fFreeBusy(vBase);
	//fFillBusy(vBase);
	// Строим стенку(мейн вал)
	if (vR1 >= -50 &&
		vR2 <= 0)
	{
		vBase->pWall = NULL;
		if (vBase->vHouse == COMMENT_MAIN_WALL)
		{
			vBase->pWall = &vBase->vMainWall;
		}
		else if (vBase->vHouse == COMMENT_MAIN_WALL_2)
		{
			vBase->pWall = &vBase->vMainWall2;
		}
		if (vBase->pWall == NULL)
		{
			return vSizeMap;
		}
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vBase->pWall->fGetWall() &&
				vMap[vvi].vNSmallCage == vBase->pWall->fGetWall()->vNSmallCage)
			{
				vRemoteness3 = (int)vMap[vvi].vRemoteness - (int)vBase->vBeginningOfBase;
				if (vRemoteness3 >= vR1 &&
					vRemoteness3 <= vR2)
				{
					if (fCheckFillBuild(&vMap[vvi],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
					{
						vBase->vBuild = &vMap[vvi];
						vBase->pWall->vQRepairPos = 0;
						vBase->pWall->vQSlayerDefensePos = 0;
						vBase->pWall->vQRepairPosBehind = 0;
						vBase->pWall->vQSlayerDefensePosBehind = 0;

						vN = 4;
						// Не одноклеточная база
						if ((vMap[vBase->vBuild->vNSmallCage - MAP_W].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							vMap[vBase->vBuild->vNSmallCage - 1].vQObstacles == 1)
						{
							vN = 0;// верх
							vN2 = 3;// низ
							goto m5;
						}
						if ((vMap[vBase->vBuild->vNSmallCage - 1].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							vMap[vBase->vBuild->vNSmallCage - MAP_W].vQObstacles == 1)
						{
							vN = 1;// лево
							vN2 = 2;// право
							goto m5;
						}
						if ((vMap[vBase->vBuild->vNSmallCage + 2].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							vMap[vBase->vBuild->vNSmallCage - MAP_W].vQObstacles == 1)
						{
							vN = 2;// право
							vN2 = 1;// лево
							goto m5;
						}
						if ((vMap[vBase->vBuild->vNSmallCage + MAP_W*2].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							vMap[vBase->vBuild->vNSmallCage - 1].vQObstacles == 1)
						{
							vN = 3;// низ
							vN2 = 0;// верх
							goto m5;
						}
						// Одноклеточная база
						if ((vMap[vBase->vBuild->vNSmallCage - MAP_W].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							(vMap[vBase->vBuild->vNSmallCage].vQObstacles == 0 ||
							vMap[vBase->vBuild->vNSmallCage + 1].vQObstacles == 0))
						{
							vN = 0;// верх
							vN2 = 3;// низ
							goto m5;
						}
						if ((vMap[vBase->vBuild->vNSmallCage - 1].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							(vMap[vBase->vBuild->vNSmallCage].vQObstacles == 0 ||
							vMap[vBase->vBuild->vNSmallCage + MAP_W].vQObstacles == 0))
						{
							vN = 1;// лево
							vN2 = 2;// право
							goto m5;
						}
						if ((vMap[vBase->vBuild->vNSmallCage + 2].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							(vMap[vBase->vBuild->vNSmallCage + 1].vQObstacles == 0 ||
							vMap[vBase->vBuild->vNSmallCage + MAP_W + 1].vQObstacles == 0))
						{
							vN = 2;// право
							vN2 = 1;// лево
							goto m5;
						}
						if ((vMap[vBase->vBuild->vNSmallCage + MAP_W*2].vRemoteness > vBase->pWall->fGetWall()->vRemoteness) &&
							(vMap[vBase->vBuild->vNSmallCage + MAP_W].vQObstacles == 0 ||
							vMap[vBase->vBuild->vNSmallCage + MAP_W + 1].vQObstacles == 0))
						{
							vN = 3;// низ
							vN2 = 0;// верх
							goto m5;
						}
m5:
						if (vN < 0 || vN > 3) return vSizeMap;
						if (vN2 < 0 || vN2 > 3) return vSizeMap;
						for (vvz = 0; vvz < 2; vvz++)
						{
							if (vvz == 1)
							{
								vN = vN2;
							}
							for (vvj = 0; vvj < 12; vvj++)
							{
								switch (vN)
								{
								case 0:
									if (vvj > 5) continue;
									break;
								case 1:
									if (vvj > 2 && vvj < 9) continue;
									break;
								case 2:
									if (vvj < 3 || vvj > 8) continue;
									break;
								case 3:
									if (vvj < 6) continue;
									break;
								}
								switch (vvj)
								{
								case 0:
									vNSmallCage = vBase->vBuild->vNSmallCage - 1;//лево
									break;
								case 1:
									vNSmallCage = vBase->vBuild->vNSmallCage - 1 - MAP_W;//лево-верх
									break;
								case 2:
									vNSmallCage = vBase->vBuild->vNSmallCage - MAP_W;//верх
									break;
								case 3:
									vNSmallCage = vBase->vBuild->vNSmallCage + 1 - MAP_W;//верх
									break;
								case 4:
									vNSmallCage = vBase->vBuild->vNSmallCage + 2 - MAP_W;//верх-право
									break;
								case 5:
									vNSmallCage = vBase->vBuild->vNSmallCage + 2;//право
									break;
								case 6:
									vNSmallCage = vBase->vBuild->vNSmallCage + 2 + MAP_W;//право
									break;
								case 7:
									vNSmallCage = vBase->vBuild->vNSmallCage + 2 + MAP_W*2;//право-низ
									break;
								case 8:
									vNSmallCage = vBase->vBuild->vNSmallCage + 1 + MAP_W*2;//низ
									break;
								case 9:
									vNSmallCage = vBase->vBuild->vNSmallCage + MAP_W*2;//низ
									break;
								case 10:
									vNSmallCage = vBase->vBuild->vNSmallCage - 1 + MAP_W *2;//лево-низ
									break;
								case 11:
									vNSmallCage = vBase->vBuild->vNSmallCage - 1 + MAP_W;//лево
									break;
								default:
									return vSizeMap;
								}
								if (vMap[vNSmallCage].vBuild == TRUE)
								{
									if (vvz == 0)
									if (vBase->pWall->vQRepairPos < vBase->pWall->MAP_REPAIR_POS)
									{
										vMap[vNSmallCage].vRepair = TRUE;
										vMap[vNSmallCage].vTower = FALSE;
										vBase->pWall->vRepairPos[vBase->pWall->vQRepairPos].vInt3 = vMap[vNSmallCage].vInt3;
										vBase->pWall->vQRepairPos++;
									}
									else
									{
										return vSizeMap;
									}
									if (vvz == 1)
									if (vBase->pWall->vQRepairPosBehind < vBase->pWall->MAP_REPAIR_POS)
									{
										vMap[vNSmallCage].vRepair = TRUE;
										vMap[vNSmallCage].vTower = FALSE;
										vBase->pWall->vRepairPosBehind[vBase->pWall->vQRepairPosBehind].vInt3 = vMap[vNSmallCage].vInt3;
										vBase->pWall->vQRepairPosBehind++;
									}
									else
									{
										return vSizeMap;
									}
								}
							}
							for (vvj = 0; vvj < 6 * 4; vvj++)
							{
								switch (vN)
								{
								case 0:
									if (vvj < 6 * 0 || vvj > 6 * 1) continue;
									break;
								case 1:
									if (vvj < 6 * 1 || vvj > 6 * 2) continue;
									break;
								case 2:
									if (vvj < 6 * 2 || vvj > 6 * 3) continue;
									break;
								case 3:
									if (vvj < 6 * 3 || vvj > 6 * 4) continue;
									break;
								}
								switch (vvj)
								{
								case 0:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-1) + (MAP_W*2);//низ 1
									break;
								case 1:
									vNSmallCage = vBase->vBuild->vNSmallCage + (1) + (MAP_W*2);//низ 2
									break;
								case 2:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-1) + (MAP_W*4);//низ 3
									break;
								case 3:
									vNSmallCage = vBase->vBuild->vNSmallCage + (1) + (MAP_W*4);//низ 4
									break;
								case 4:
									vNSmallCage = vBase->vBuild->vNSmallCage + (0) + (MAP_W*2);//низ 5
									break;
								case 5:
									vNSmallCage = vBase->vBuild->vNSmallCage + (0) + (MAP_W*4);//низ 6
									break;
								case 6:
									vNSmallCage = vBase->vBuild->vNSmallCage + (2) + (-MAP_W*1);//право 1
									break;
								case 7:
									vNSmallCage = vBase->vBuild->vNSmallCage + (2) + (MAP_W*1);//право 2
									break;
								case 8:
									vNSmallCage = vBase->vBuild->vNSmallCage + (4) + (-MAP_W*1);//право 3
									break;
								case 9:
									vNSmallCage = vBase->vBuild->vNSmallCage + (4) + (MAP_W*1);//право 4
									break;
								case 10:
									vNSmallCage = vBase->vBuild->vNSmallCage + (2) + (MAP_W*0);//право 5
									break;
								case 11:
									vNSmallCage = vBase->vBuild->vNSmallCage + (4) + (MAP_W*0);//право 6
									break;
								case 12:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-2) + (MAP_W*1);//лево 1
									break;
								case 13:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-2) + (-MAP_W*1);//лево 2
									break;
								case 14:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-4) + (MAP_W*1);//лево 3
									break;
								case 15:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-4) + (-MAP_W*1);//лево 4
									break;
								case 16:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-2) + (MAP_W*0);//лево 5
									break;
								case 17:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-4) + (MAP_W*0);//лево 6
									break;
								case 18:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-1) + (-MAP_W*2);//верх 1
									break;
								case 19:
									vNSmallCage = vBase->vBuild->vNSmallCage + (1) + (-MAP_W*2);//верх 2
									break;
								case 20:
									vNSmallCage = vBase->vBuild->vNSmallCage + (-1) + (-MAP_W*4);//верх 3
									break;
								case 21:
									vNSmallCage = vBase->vBuild->vNSmallCage + (1) + (-MAP_W*4);//верх 4
									break;
								case 22:
									vNSmallCage = vBase->vBuild->vNSmallCage + (0) + (-MAP_W*2);//верх 5
									break;
								case 23:
									vNSmallCage = vBase->vBuild->vNSmallCage + (0) + (-MAP_W*4);//верх 6
									break;
								default:
									return vSizeMap;
								}
								if (vMap[vNSmallCage].vBuild == TRUE)
								{
									if (vvz == 0)
									if (vBase->pWall->vQSlayerDefensePos < vBase->pWall->MAP_SLAYER_DEFENSE_POS)
									{
										vBase->pWall->vMassSlayerDefense[vBase->pWall->vQSlayerDefensePos].vInt3 = vMap[vNSmallCage].vInt3;
										vBase->pWall->vQSlayerDefensePos++;
									}
									if (vvz == 1)
									if (vBase->pWall->vQSlayerDefensePosBehind < vBase->pWall->MAP_SLAYER_DEFENSE_POS)
									{
										vBase->pWall->vMassSlayerDefenseBehind[vBase->pWall->vQSlayerDefensePosBehind].vInt3 = vMap[vNSmallCage].vInt3;
										vBase->pWall->vQSlayerDefensePosBehind++;
									}
								}
							}
						}
						for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
						{
							for (vvj = 0; vvj < vBase->pWall->vQRepairPos; vvj++)
							{
								if(vMap[vvi].vInt3 == vBase->pWall->vRepairPos[vvj].vInt3)
								{
									vMap[vvi].vRemotenessBase = 1;
								}
							}
						}
						return vBase->vBuild->vInt3;
					}
				}
			}
		}
	}
	// Строим защиту от вампа
	if (vBase->vHouse == COMMENT_HOUSE_FOR_BLOCK_VAMPIRE)
	{
		if (vBase->vMainWall.fGetWall() == NULL)
		{
			return vSizeMap;
		}
		for (vvj = 0; vvj < 4; vvj++)
		{
			vFlagBuild = FALSE;
			switch (vvj)
			{
			case 0:
				//вверх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*3));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 1:
				//право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (3);
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 2:
				//низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*3));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 3:
				//лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-3);
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			}
			if (vFlagBuild == TRUE)
			{
				vBase->vBuild = &vMap[vNSmallCage];
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	// Строим защиту от вампа 2
	if (vBase->vHouse == COMMENT_HOUSE_FOR_BLOCK_VAMPIRE_2)
	{
		if (vBase->vMainWall.fGetWall() == NULL)
		{
			return vSizeMap;
		}
		for (vvj = 0; vvj < 4; vvj++)
		{
			vFlagBuild = FALSE;
			switch (vvj)
			{
			case 0:
				//вверх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*5));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 1:
				//право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (5);
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 2:
				//низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*5));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 3:
				//лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-5);
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			}
			if (vFlagBuild == TRUE)
			{
				vBase->vBuild = &vMap[vNSmallCage];
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	// Строим защиту от вампа 3
	if (vBase->vHouse == COMMENT_HOUSE_FOR_BLOCK_VAMPIRE_3)
	{
		if (vBase->vMainWall.fGetWall() == NULL)
		{
			return vSizeMap;
		}
		for (vvj = 0; vvj < 4; vvj++)
		{
			vFlagBuild = FALSE;
			switch (vvj)
			{
			case 0:
				//вверх-право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*3)) + 2;
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 1:
				//право-низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (3) + ((MAP_W*2));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 2:
				//низ-лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*3)) - 2;
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 3:
				//лево-верх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-3) + (-(MAP_W*2));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			}
			if (vFlagBuild == TRUE)
			{
				vBase->vBuild = &vMap[vNSmallCage];
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	// Строим защиту от вампа 4
	if (vBase->vHouse == COMMENT_HOUSE_FOR_BLOCK_VAMPIRE_4)
	{
		if (vBase->vMainWall.fGetWall() == NULL)
		{
			return vSizeMap;
		}
		for (vvj = 0; vvj < 4; vvj++)
		{
			vFlagBuild = FALSE;
			switch (vvj)
			{
			case 0:
				//вверх-лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*3)) - 2;
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 1:
				//право-верх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (3) + (-(MAP_W*2));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 2:
				//низ-право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*3)) + 2;
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 3:
				//лево-низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-3) + ((MAP_W*2));
				if (vBase->vBeginningOfBase < vMap[vNSmallCage].vRemoteness) break;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE)
				{
					vFlagBuild = TRUE;
				}
				break;
			}
			if (vFlagBuild == TRUE)
			{
				vBase->vBuild = &vMap[vNSmallCage];
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	// Строим первые две пушки
	if ((vBase->vQFirstTower < 2 && vId == ID_UNIT_BUILD_TOWER_WHITE) ||
		vId == ID_UNIT_BUILD_TOWER_EARTH_LVL1)
	{
		if (vBase->vMainWall.fGetWall() == NULL)
		{
			return vSizeMap;
		}
		for (vvj = 0; vvj < 8; vvj++)
		{
			vFlagBuild = FALSE;
			switch (vvj)
			{
			case 0:
				//вверх-лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*3) - 1);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 1:
				//вверх-право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*3) + 1);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 2:
				//право-верх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*1) + 3);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 3:
				//право-низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*1) + 3);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 4:
				//низ-право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*3) + 1);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 5:
				//низ-лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*3) - 1);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 6:
				//лево-низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*1) - 3);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 7:
				//лево-верх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*1) - 3);
				if (vBase->vBeginningOfBase > vMap[vNSmallCage].vRemoteness ||
					vBase->vNBase != vMap[vNSmallCage].vNBase) break;
				vRemoteness = vMap[vNSmallCage].vRemoteness - vBase->vBeginningOfBase;
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,TRUE) == TRUE &&
					vRemoteness > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			}
			if (vFlagBuild == TRUE)
			{
				vBase->vBuild = &vMap[vNSmallCage];
				fFillDoom2(&vMap[vNSmallCage],2,2,vvj);
				//goto m10;
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	// Строим первые две хилки
	if (vBase->vQHeal == 2 && vId == ID_UNIT_BUILD_TOWER_HEAL_LVL1)
	{
		if (vBase->vMainWall.fGetWall() == NULL)
		{
			return vSizeMap;
		}
//m10:
		for (vvj = 0; vvj < 8; vvj++)
		{
			vFlagBuild = FALSE;
			switch (vvj)
			{
			case 0:
				//вверх-лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*5) - 1);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 1:
				//вверх-право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*5) + 1);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 2:
				//право-верх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*1) + 5);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 3:
				//право-низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*1) + 5);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 4:
				//низ-право
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*5) + 1);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 5:
				//низ-лево
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*5) - 1);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 6:
				//лево-низ
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + ((MAP_W*1) - 5);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			case 7:
				//лево-верх
				vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage + (-(MAP_W*1) - 5);
				if (fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
					vMap[vNSmallCage].vRemotenessBase > 0)
				{
					vFlagBuild = TRUE;
				}
				break;
			}
			if (vFlagBuild == TRUE)
			{
				if (vBase->vBuild)
				{
					if (vFlagTower == TRUE &&
						fCheckFillBuildTower(&vMap[vNSmallCage],vXBuild,vYBuild) != TRUE)
					{
						goto m22;
					}
					fFillBuildHeal(&vMap[vNSmallCage],2,2);
					//fWriteFileTGA();
					
					return vBase->vBuild->vInt3;
				}
				vBase->vBuild = &vMap[vNSmallCage];
				return vBase->vBuild->vInt3;
			}
m22:;
		}
		return vSizeMap;
	}
	// Защита от дума и шамана не на красных точках
	if (vBase->vHouse == COMMENT_WALLS_DOOM)
	{
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vBuild == TRUE &&
				vMap[vvi].vDoom.vBool == TRUE &&
				vMap[vvi].vNBase == vBase->vNBase &&
				((vMap[vvi].vDoom.vNBaseBottom != DO_NOT_FIND_BASE && vMap[vvi].vDoom.vNBaseBottom != vBase->vNBase) ||
				(vMap[vvi].vDoom.vNBaseLeft != DO_NOT_FIND_BASE && vMap[vvi].vDoom.vNBaseLeft != vBase->vNBase) ||
				(vMap[vvi].vDoom.vNBaseRight != DO_NOT_FIND_BASE && vMap[vvi].vDoom.vNBaseRight != vBase->vNBase) ||
				(vMap[vvi].vDoom.vNBaseTop != DO_NOT_FIND_BASE && vMap[vvi].vDoom.vNBaseTop != vBase->vNBase)) &&
				vMap[vvi].vNBase == vBase->vNBase &&
				vMap[vvi].vBusy == FALSE &&
				fCheckFillBuild(&vMap[vvi],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE)
			{
				vBase->vBuild = &vMap[vvi];
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	// Защита от дума и шамана на красных точках
	if (vBase->vHouse == COMMENT_BOXS_DOOM)
	{
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vDoom.vBool == TRUE &&
				vMap[vvi].vNBase == vBase->vNBase &&
				vMap[vvi].vRedCell == TRUE &&
				vMap[vvi].vBusy == FALSE)
			{
				vBase->vBuild = &vMap[vvi];
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	fFillBusy(vBase);
	// Строим пушку около гривы
	if (vBase->vHouse == COMMENT_TOWER_HOUSE)
	{
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vGrive == TRUE &&
				vMap[vvi].vRemotenessGrive < 12 &&
				vMap[vvi].vNBase == vBase->vNBase &&
				vMap[vvi].vBusy == FALSE)
			{
				vBase->vBuild = &vMap[vvi];
				return vBase->vBuild->vInt3;
			}
		}
		return vSizeMap;
	}
	// Строим рядом с однотипными зданиями
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vHeal == FALSE &&
			vMap[vvi].vDoom.vBool == FALSE &&
			vMap[vvi].vNBase == vBase->vNBase &&
			vMap[vvi].vBusy == FALSE &&
			fCheckFillBuild(&vMap[vvi],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
			((vFlagTower == TRUE &&
			fCheckFillBuildTower(&vMap[vvi],vXBuild,vYBuild) == TRUE) ||
			(vFlagTower == FALSE &&
			vMap[vvi].vTower == FALSE)))
		{
			for (vvj = 0; vvj < 4; vvj++)
			{
				vFlagBuild = FALSE;
				switch(vvj)
				{
				case 0:
					vSetOff = -(MAP_W*vYBuild);// Верх
					if (vvi+vSetOff>=0 && vMap[vvi+vSetOff].vY >= 0)
					{
						vFlagBuild = TRUE;
					}
					break;
				case 1:
					vSetOff = (vXBuild);// Право
					if (vvi+vSetOff<=(MAP_MAX_SMALL_CAGE-1) && vMap[vvi+vSetOff].vX < MAP_W)
					{
						vFlagBuild = TRUE;
					}
					break;
				case 2:
					vSetOff = (vYBuild*MAP_W);// Вниз
					if (vvi+vSetOff<=(MAP_MAX_SMALL_CAGE-1) && vMap[vvi+vSetOff].vY < MAP_H)
					{
						vFlagBuild = TRUE;
					}
					break;
				case 3:
					vSetOff = -(vXBuild);// Влево
					if (vvi+vSetOff>=0 && vMap[vvi+vSetOff].vX >= 0)
					{
						vFlagBuild = TRUE;
					}
					break;
				}
				if (vFlagBuild == TRUE)
				{
					if (vMap[vvi+vSetOff].vBusy == TRUE)
					{
						pDataUnit = fGetDataUnit(vMap[vvi+vSetOff].vIdDataUnit);
						if (pDataUnit &&
							pDataUnit->vId == vId)
						{
							vBase->vBuild = &vMap[vvi];
							return vBase->vBuild->vInt3;
						}
					}
				}
			}
			
		}
	}
	//2 леса по бокам или 2 клиф-левела или 1 лес и 1 клиф-левел
	vForest = NULL;
	vRemoteness2 = 0;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vHeal == FALSE &&
			vMap[vvi].vDoom.vBool == FALSE &&
			vMap[vvi].vNBase == vBase->vNBase &&
			vMap[vvi].vLightning == FALSE &&
			(vMap[vvi].vQTree == 2 ||
			vMap[vvi].vQCliff == 2 ||
			(vMap[vvi].vQTree == 1 &&
			vMap[vvi].vQCliff == 1)) &&
			vMap[vvi].vRemoteness >= vBase->vBeginningOfBase)
		{
			vRemoteness = vMap[vvi].vRemoteness - vBase->vBeginningOfBase;
			if (vRemoteness >= vR1 &&
				vRemoteness <= vR2 &&
				(vRemoteness2 == 0 ||
				vRemoteness2 < vRemoteness))
			{
				vRemoteness2 = vRemoteness;
				vForest = &vMap[vvi];
			}
		}
	}
	if (vForest != NULL)
	{
		//верх-право
		vNSmallCage = vForest->vNSmallCage - (vXBuild - 1);
		if ((vForest->vTree.vTop == TRUE || vForest->vCliff.vTop == TRUE) &&
			(vForest->vTree.vRight == TRUE || vForest->vCliff.vRight == TRUE) &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
			((vFlagTower == TRUE &&
			fCheckFillBuildTower(&vMap[vNSmallCage],vXBuild,vYBuild) == TRUE) ||
			(vFlagTower == FALSE &&
			vMap[vNSmallCage].vTower == FALSE)))
		{
			goto m1;
		}
		//низ-право
		vNSmallCage = vForest->vNSmallCage - (vXBuild - 1) - MAP_W*(vYBuild - 1);
		if ((vForest->vTree.vBottom == TRUE || vForest->vCliff.vBottom == TRUE) &&
			(vForest->vTree.vRight == TRUE || vForest->vCliff.vRight == TRUE) &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
			((vFlagTower == TRUE &&
			fCheckFillBuildTower(&vMap[vNSmallCage],vXBuild,vYBuild) == TRUE) ||
			(vFlagTower == FALSE &&
			vMap[vNSmallCage].vTower == FALSE)))
		{
			goto m1;
		}
		//низ-лево
		vNSmallCage = vForest->vNSmallCage - MAP_W*(vYBuild - 1);
		if ((vForest->vTree.vBottom == TRUE || vForest->vCliff.vBottom == TRUE) &&
			(vForest->vTree.vLeft == TRUE || vForest->vCliff.vLeft == TRUE) &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
			((vFlagTower == TRUE &&
			fCheckFillBuildTower(&vMap[vNSmallCage],vXBuild,vYBuild) == TRUE) ||
			(vFlagTower == FALSE &&
			vMap[vNSmallCage].vTower == FALSE)))
		{
			goto m1;
		}
		//верх-лево
		vNSmallCage = vForest->vNSmallCage;
		if ((vForest->vTree.vTop == TRUE || vForest->vCliff.vTop == TRUE) &&
			(vForest->vTree.vLeft == TRUE || vForest->vCliff.vLeft == TRUE) &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
			((vFlagTower == TRUE &&
			fCheckFillBuildTower(&vMap[vNSmallCage],vXBuild,vYBuild) == TRUE) ||
			(vFlagTower == FALSE &&
			vMap[vNSmallCage].vTower == FALSE)))
		{
			goto m1;
		}
	}
	for (vvj = 0; vvj < 2; vvj++)
	{
		if (vFlag == TRUE)
		{
			if (vvj == 0)
			{
				vFlagLightning = FALSE;
			}
			if (vvj == 1)
			{
				vFlagLightning = TRUE;
			}
		}
		else
		{
			if (vvj == 0)
			{
				vFlagLightning = TRUE;
			}
			if (vvj == 1)
			{
				vFlagLightning = FALSE;
			}
		}
		vRemoteness2 = 0;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vBuild == TRUE &&
				vMap[vvi].vHeal == FALSE &&
				vMap[vvi].vNBase == vBase->vNBase &&
				vMap[vvi].vLightning == vFlagLightning &&
				vMap[vvi].vRemoteness >= vBase->vBeginningOfBase)
			{
				vRemoteness = vMap[vvi].vRemoteness - vBase->vBeginningOfBase;
				if (vRemoteness >= vR1 &&
					vRemoteness <= vR2 &&
					(vRemoteness2 == 0 ||
					vRemoteness2 < vRemoteness))
				{
					if (fCheckFillBuild(&vMap[vvi],vXBuild,vYBuild,NO_UNIT,vBoolDoom) == TRUE &&
						((vFlagTower == TRUE &&
						fCheckFillBuildTower(&vMap[vvi],vXBuild,vYBuild) == TRUE) ||
						(vFlagTower == FALSE &&
						vMap[vvi].vTower == FALSE)))
					{
						goto m2;
					}
					goto m3;
m2:
					vBase->vBuild = &vMap[vvi];
					vRemoteness2 = vRemoteness;
m3:;
				}
			}
		}
		if (vBase->vBuild != NULL)
		{
			return vBase->vBuild->vInt3;
		}
	}
	return vSizeMap;
m1:
	vBase->vBuild = &vMap[vNSmallCage];
	return vBase->vBuild->vInt3;
}

// Найти позицию дома
unsigned int MapBases::fFindHouse(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return vSizeMap;
	if (vBase->vNBase <= 0 || vBase->vNBase >= MAP_MAX_BASE) return vSizeMap;
	sSmallCage *vForest;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemoteness2;
	unsigned short vNSmallCage;
	unsigned short vXBuild;
	unsigned short vYBuild;
	bool vFlagLightning;
	if (fGetXYBuild(ID_UNIT_BUILD_DOM,&vXBuild,&vYBuild) == FALSE) return vSizeMap;
	// Позиции юнитов
	fFreeBusy(vBase);
	fFillBusy(vBase);
	vBase->vBuild = NULL;
	vForest = NULL;
	vRemoteness2 = 0;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vNBase == vBase->vNBase &&
			vMap[vvi].vLightning == FALSE &&
			vMap[vvi].vQTree == 2 &&
			vMap[vvi].vRemoteness >= vBase->vBeginningOfBase)
		{
			vRemoteness = vMap[vvi].vRemoteness - vBase->vBeginningOfBase;
			if (vRemoteness <= 120 &&
				(vRemoteness2 == 0 ||
				vRemoteness2 < vRemoteness))
			{
				vRemoteness2 = vRemoteness;
				vForest = &vMap[vvi];
			}
		}
	}
	if (vForest != NULL)
	{
		vNSmallCage = vForest->vNSmallCage - 2 + MAP_W;
		if (vForest->vTree.vTop == TRUE &&
			vForest->vTree.vRight == TRUE &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
			vMap[vNSmallCage].vTower == FALSE)
		{
			goto m1;
		}
		vNSmallCage = vForest->vNSmallCage - 2 - MAP_W*2;
		if (vForest->vTree.vBottom == TRUE &&
			vForest->vTree.vRight == TRUE &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
			vMap[vNSmallCage].vTower == FALSE)
		{
			goto m1;
		}
		vNSmallCage = vForest->vNSmallCage + 1 - MAP_W*2;
		if (vForest->vTree.vBottom == TRUE &&
			vForest->vTree.vLeft == TRUE &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
			vMap[vNSmallCage].vTower == FALSE)
		{
			goto m1;
		}
		vNSmallCage = vForest->vNSmallCage + 1 + MAP_W;
		if (vForest->vTree.vTop == TRUE &&
			vForest->vTree.vLeft == TRUE &&
			fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
			vMap[vNSmallCage].vTower == FALSE)
		{
			goto m1;
		}
	}
	for (vvj = 0; vvj < 2; vvj++)
	{
		if (vvj == 0)
		{
			vFlagLightning = FALSE;
		}
		if (vvj == 1)
		{
			vFlagLightning = TRUE;
		}
		vRemoteness2 = 0;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vBuild == TRUE &&
				vMap[vvi].vNBase == vBase->vNBase &&
				vMap[vvi].vLightning == vFlagLightning &&
				vMap[vvi].vQTree == 1 &&
				vMap[vvi].vRemoteness >= vBase->vBeginningOfBase)
			{
				vRemoteness = vMap[vvi].vRemoteness - vBase->vBeginningOfBase;
				if (vRemoteness <= 120 &&
					(vRemoteness2 == 0 ||
					vRemoteness2 < vRemoteness))
				{
					vForest = &vMap[vvi];
					if (vForest != NULL)
					{
						vNSmallCage = vForest->vNSmallCage + MAP_W;
						if (vForest->vTree.vTop == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage-2],2,2,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage+2],2,2,NO_UNIT,FALSE) == TRUE &&
							vMap[vNSmallCage].vTower == FALSE)
						{
							goto m2;
						}
						vNSmallCage = vForest->vNSmallCage - 2;
						if (vForest->vTree.vRight == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage - MAP_W*2],2,2,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage + MAP_W*2],2,2,NO_UNIT,FALSE) == TRUE &&
							vMap[vNSmallCage].vTower == FALSE)
						{
							goto m2;
						}
						vNSmallCage = vForest->vNSmallCage - MAP_W*2;
						if (vForest->vTree.vBottom == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage-2],2,2,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage+2],2,2,NO_UNIT,FALSE) == TRUE &&
							vMap[vNSmallCage].vTower == FALSE)
						{
							goto m2;
						}
						vNSmallCage = vForest->vNSmallCage + 1;
						if (vForest->vTree.vLeft == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage],vXBuild,vYBuild,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage - MAP_W*2],2,2,NO_UNIT,FALSE) == TRUE &&
							fCheckFillBuild(&vMap[vNSmallCage + MAP_W*2],2,2,NO_UNIT,FALSE) == TRUE &&
							vMap[vNSmallCage].vTower == FALSE)
						{
							goto m2;
						}
					}
					goto m3;
m2:
					vBase->vBuild = &vMap[vNSmallCage];
					vRemoteness2 = vRemoteness;
m3:;
				}
			}
		}
		if (vBase->vBuild != NULL)
		{
			return vBase->vBuild->vInt3;
		}
	}
	return vSizeMap;
m1:
	vBase->vBuild = &vMap[vNSmallCage];
	return vBase->vBuild->vInt3;
}

void MapBases::fFreeBusy(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return;
	if (vBase->vNBase <= 0) return;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vNBase == vBase->vNBase)
		{
			vMap[vvi].vBusy = FALSE;
			vMap[vvi].vIdDataUnit = 0;
		}
	}
}

void MapBases::fFillBusy(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return;
	if (vBase->vNBase <= 0) return;
	fClearMapUnits(HUMANS);
	for (vvi = 0; vvi <= vLastUnit; vvi++)
	{
		pDataUnit = fGetDataUnit((unsigned short)vvi);
		if (pDataUnit &&
			GetUnitTypeId(pDataUnit->WhichUnit) == 0)
		{
			//fDelDataUnit2(pDataUnit->vIdDataUnit);
			continue;
		}
		if (pDataUnit &&
			pDataUnit->Dead == FALSE)
		{
			fFillUnitToMap(pDataUnit);
		}
	}
	
	for (vvi = 0; vvi < vSizeMap; vvi++)
	{
		vNSmallCage = (int)((vvi/vW3)/2)*MAP_W + (vvi%vW3)/2;
		if (vNSmallCage >= 0 &&
			vNSmallCage < MAP_MAX_SMALL_CAGE &&
			vMap[vNSmallCage].vBuild == TRUE &&
			vMap[vNSmallCage].vNBase == vBase->vNBase)
		{
			vIdDataUnit = vMapUnits[HUMANS][vvi];
			if (vIdDataUnit != NO_UNIT &&
				vBase->pHumanPlayer &&
				vBase->pHumanPlayer->vCurrentChain &&
				vBase->pHumanPlayer->vParamPlayer.vUnit[vBase->pHumanPlayer->vCurrentChain->vCommentUnit].vIdForComments == vBase->pHumanPlayer->vCurrentChain->vCommentUnit &&
				vIdDataUnit != vBase->pHumanPlayer->vParamPlayer.vUnit[vBase->pHumanPlayer->vCurrentChain->vCommentUnit].pDataUnit->vIdDataUnit)
			{
				vMap[vNSmallCage].vBusy = TRUE;
				vMap[vNSmallCage].vIdDataUnit = vIdDataUnit;
			}
		}
		
	}
}

bool MapBases::fFillBuildHeal(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild)
{
	if (vSmallCage == NULL) return FALSE;
	if (vXBuild <= 0) return FALSE;
	if (vYBuild <= 0) return FALSE;
	if (vSmallCage->vX < 0 || vSmallCage->vX >= MAP_W) return FALSE;
	if (vSmallCage->vY < 0 || vSmallCage->vY >= MAP_H) return FALSE;
	if ((vSmallCage->vX + vXBuild) < 0 || (vSmallCage->vX + vXBuild) >= MAP_W) return FALSE;
	if ((vSmallCage->vY + vYBuild) < 0 || (vSmallCage->vY + vYBuild) >= MAP_H) return FALSE;
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj < vSmallCage->vY + vYBuild; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi < vSmallCage->vX + vXBuild; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			vMap[vSmallCage->vNSmallCageTemp].vHeal = TRUE;
		}
	}
	return TRUE;
}

bool MapBases::fFillDoom2(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild, int vvj2)
{
	if (vvj2 < 0) return FALSE;
	if (vSmallCage == NULL) return FALSE;
	if (vXBuild <= 0) return FALSE;
	if (vYBuild <= 0) return FALSE;
	if (vSmallCage->vX < 0 || vSmallCage->vX >= MAP_W) return FALSE;
	if (vSmallCage->vY < 0 || vSmallCage->vY >= MAP_H) return FALSE;
	if ((vSmallCage->vX + vXBuild) < 0 || (vSmallCage->vX + vXBuild) >= MAP_W) return FALSE;
	if ((vSmallCage->vY + vYBuild) < 0 || (vSmallCage->vY + vYBuild) >= MAP_H) return FALSE;
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj < vSmallCage->vY + vYBuild; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi < vSmallCage->vX + vXBuild; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			vMap[vSmallCage->vNSmallCageTemp].vDoom2.vBool = TRUE;
			switch (vvj2)
			{
			case 0://верх-лево
			case 1://верх-право
				if (vSmallCage->vNSmallCageTemp+MAP_W*5 < 0 || vSmallCage->vNSmallCageTemp+MAP_W*5 >= MAP_MAX_SMALL_CAGE) break;
				vMap[vSmallCage->vNSmallCageTemp+MAP_W*5].vDoom2.vBool = TRUE;
				vMap[vSmallCage->vNSmallCageTemp+MAP_W*5].vDoom2.vNBaseTop = vSmallCage->vNBase;
				break;
			case 2://право-верх
			case 3://право-низ
				if (vSmallCage->vNSmallCageTemp+1*5 < 0 || vSmallCage->vNSmallCageTemp+1*5 >= MAP_MAX_SMALL_CAGE) break;
				vMap[vSmallCage->vNSmallCageTemp+1*5].vDoom2.vBool = TRUE;
				vMap[vSmallCage->vNSmallCageTemp+1*5].vDoom2.vNBaseRight = vSmallCage->vNBase;
				break;
			case 4://низ-право
			case 5://низ-лево
				if (vSmallCage->vNSmallCageTemp-MAP_W*5 < 0 || vSmallCage->vNSmallCageTemp-MAP_W*5 >= MAP_MAX_SMALL_CAGE) break;
				vMap[vSmallCage->vNSmallCageTemp-MAP_W*5].vDoom2.vBool = TRUE;
				vMap[vSmallCage->vNSmallCageTemp-MAP_W*5].vDoom2.vNBaseBottom = vSmallCage->vNBase;
				break;
			case 6://лево-низ
			case 7://лево-верх
				if (vSmallCage->vNSmallCageTemp-1*5 < 0 || vSmallCage->vNSmallCageTemp-1*5 >= MAP_MAX_SMALL_CAGE) break;
				vMap[vSmallCage->vNSmallCageTemp-1*5].vDoom2.vBool = TRUE;
				vMap[vSmallCage->vNSmallCageTemp-1*5].vDoom2.vNBaseLeft = vSmallCage->vNBase;
				break;
			}
		}
	}
	return TRUE;
}

bool MapBases::fCheckFillBuild(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild, unsigned short vIdSmallCage, bool vBoolDoom)
{
	if (vSmallCage == NULL) return FALSE;
	if (vXBuild <= 0) return FALSE;
	if (vYBuild <= 0) return FALSE;
	if (vIdSmallCage < 0 || vIdSmallCage > MAX_COUNT_UNITS) return FALSE;
	if (vSmallCage->vX < 0 || vSmallCage->vX >= MAP_W) return FALSE;
	if (vSmallCage->vY < 0 || vSmallCage->vY >= MAP_H) return FALSE;
	if ((vSmallCage->vX + vXBuild) < 0 || (vSmallCage->vX + vXBuild) >= MAP_W) return FALSE;
	if ((vSmallCage->vY + vYBuild) < 0 || (vSmallCage->vY + vYBuild) >= MAP_H) return FALSE;
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj < vSmallCage->vY + vYBuild; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi < vSmallCage->vX + vXBuild; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage != vIdSmallCage &&
				vDataUnits[vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage].pDataUnit &&
				vDataUnits[vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage].pDataUnit->Dead == TRUE)
			{
				fDelDataUnit2(vDataUnits[vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage].pDataUnit);
			}
			if (!vBoolDoom &&
				(vMap[vSmallCage->vNSmallCageTemp].vDoom2.vBool != vBoolDoom ||
				(vMap[vSmallCage->vNSmallCageTemp].vDoom.vBool != vBoolDoom &&
				vMap[vSmallCage->vNSmallCageTemp].vRemotenessBase > 0 &&
				!(vMap[vSmallCage->vNSmallCageTemp].vDoom.vNBaseBottom == vMap[vSmallCage->vNSmallCageTemp].vNBase ||
				vMap[vSmallCage->vNSmallCageTemp].vDoom.vNBaseLeft == vMap[vSmallCage->vNSmallCageTemp].vNBase ||
				vMap[vSmallCage->vNSmallCageTemp].vDoom.vNBaseRight == vMap[vSmallCage->vNSmallCageTemp].vNBase ||
				vMap[vSmallCage->vNSmallCageTemp].vDoom.vNBaseTop == vMap[vSmallCage->vNSmallCageTemp].vNBase))) ||
				vMap[vSmallCage->vNSmallCageTemp].vRepair != FALSE ||
				vMap[vSmallCage->vNSmallCageTemp].vBuild != TRUE ||
				vMap[vSmallCage->vNSmallCageTemp].vBusy != FALSE ||
				vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage != vIdSmallCage)
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

bool MapBases::fCheckFillUnit(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild, unsigned short vIdSmallCage)
{
	if (vSmallCage == NULL) return FALSE;
	if (vXBuild <= 0) return FALSE;
	if (vYBuild <= 0) return FALSE;
	if (vIdSmallCage < 0 || vIdSmallCage > MAX_COUNT_UNITS) return FALSE;
	if (vSmallCage->vX < 0 || vSmallCage->vX >= MAP_W) return FALSE;
	if (vSmallCage->vY < 0 || vSmallCage->vY >= MAP_H) return FALSE;
	if ((vSmallCage->vX + vXBuild) < 0 || (vSmallCage->vX + vXBuild) >= MAP_W) return FALSE;
	if ((vSmallCage->vY + vYBuild) < 0 || (vSmallCage->vY + vYBuild) >= MAP_H) return FALSE;
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj < vSmallCage->vY + vYBuild; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi < vSmallCage->vX + vXBuild; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (!(vMap[vSmallCage->vNSmallCageTemp].vBuild == TRUE ||
				vMap[vSmallCage->vNSmallCageTemp].vInt == JASS_PATH_BLIGHT) &&
				vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage != vIdSmallCage)
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

bool MapBases::fCheckFillBuildTower(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild)
{
	if (vSmallCage == NULL) return FALSE;
	if (vXBuild <= 0) return FALSE;
	if (vYBuild <= 0) return FALSE;
	if (vSmallCage->vX < 0 || vSmallCage->vX >= MAP_W) return FALSE;
	if (vSmallCage->vY < 0 || vSmallCage->vY >= MAP_H) return FALSE;
	if ((vSmallCage->vX + vXBuild) < 0 || (vSmallCage->vX + vXBuild) >= MAP_W) return FALSE;
	if ((vSmallCage->vY + vYBuild) < 0 || (vSmallCage->vY + vYBuild) >= MAP_H) return FALSE;
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj < vSmallCage->vY + vYBuild; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi < vSmallCage->vX + vXBuild; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (vMap[vSmallCage->vNSmallCageTemp].vTower != TRUE ||
				vMap[vSmallCage->vNSmallCageTemp].vHeal != FALSE)
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

sSmallCage * MapBases::fFillBuild(unsigned int vInt3, unsigned short vXBuild, unsigned short vYBuild, unsigned short vIdSmallCage)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return NULL;
	if (vXBuild <= 0) return NULL;
	if (vYBuild <= 0) return NULL;
	if (vIdSmallCage < 0 || vIdSmallCage >= MAX_COUNT_UNITS) return NULL;
	sSmallCage *vSmallCage;
	int vvi;
	vSmallCage = NULL;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vInt3 == vInt3)
		{
			vSmallCage = &vMap[vvi];
			break;
		}
	}
	if (vSmallCage == NULL) return NULL;
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj < vSmallCage->vY + vYBuild; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi < vSmallCage->vX + vXBuild; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			vMap[vSmallCage->vNSmallCageTemp].vHead = vSmallCage;
			vMap[vSmallCage->vNSmallCageTemp].vXBuild = vXBuild;
			vMap[vSmallCage->vNSmallCageTemp].vYBuild = vYBuild;
			vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage = vIdSmallCage;
		}
	}
	return vSmallCage;
}

bool MapBases::fFreeBuild(sSmallCage *vSmallCage)
{
	if (fNull2((unsigned int)vSmallCage)) return FALSE;
	if (vSmallCage->vHead != vSmallCage) return FALSE;
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj < vSmallCage->vY + vSmallCage->vYBuild; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi < vSmallCage->vX + vSmallCage->vXBuild; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (vSmallCage->vHead != &vMap[vSmallCage->vNSmallCageTemp])
			{
				vMap[vSmallCage->vNSmallCageTemp].vHead = NULL;
				vMap[vSmallCage->vNSmallCageTemp].vXBuild = 0;
				vMap[vSmallCage->vNSmallCageTemp].vYBuild = 0;
			}
			vMap[vSmallCage->vNSmallCageTemp].vIdSmallCage = NO_UNIT;
		}
	}
	vSmallCage->vHead->vXBuild = 0;
	vSmallCage->vHead->vYBuild = 0;
	vSmallCage->vHead->vHead = NULL;
	return TRUE;
}

int MapBases::fTypeWoodBuild(sSmallCage *vSmallCage)
{
	if (fNull2((unsigned int)vSmallCage)) return FALSE;
	if (vSmallCage->vHead != vSmallCage) return FALSE;
	vTop = FALSE;
	vLeft = FALSE;
	vBottom = FALSE;
	vRight = FALSE;
	//top
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj > vSmallCage->vY - 2; vSmallCage->vvj--)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi == vSmallCage->vX; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (vSmallCage->vNSmallCageTemp >= 0 &&
				vSmallCage->vNSmallCageTemp < MAP_MAX_SMALL_CAGE &&
				vMap[vSmallCage->vNSmallCageTemp].vQTree > 0)
			{
				vTop = TRUE;
				goto m1;
			}
		}
	}
m1:;
	//left
	for (vSmallCage->vvj = vSmallCage->vY; vSmallCage->vvj == vSmallCage->vY; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX; vSmallCage->vvi > vSmallCage->vX - 2; vSmallCage->vvi--)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (vSmallCage->vNSmallCageTemp >= 0 &&
				vSmallCage->vNSmallCageTemp < MAP_MAX_SMALL_CAGE &&
				vMap[vSmallCage->vNSmallCageTemp].vQTree > 0)
			{
				vLeft = TRUE;
				goto m2;
			}
		}
	}
m2:;
	//bottom
	for (vSmallCage->vvj = vSmallCage->vY + vSmallCage->vYBuild - 1; vSmallCage->vvj < vSmallCage->vY + vSmallCage->vYBuild - 1 + 2; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX + vSmallCage->vXBuild - 1; vSmallCage->vvi == vSmallCage->vX + vSmallCage->vXBuild - 1; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (vSmallCage->vNSmallCageTemp >= 0 &&
				vSmallCage->vNSmallCageTemp < MAP_MAX_SMALL_CAGE &&
				vMap[vSmallCage->vNSmallCageTemp].vQTree > 0)
			{
				vBottom = TRUE;
				goto m3;
			}
		}
	}
m3:;
	//right
	for (vSmallCage->vvj = vSmallCage->vY + vSmallCage->vYBuild - 1; vSmallCage->vvj == vSmallCage->vY + vSmallCage->vYBuild - 1; vSmallCage->vvj++)
	{
		for (vSmallCage->vvi = vSmallCage->vX + vSmallCage->vXBuild - 1; vSmallCage->vvi < vSmallCage->vX + vSmallCage->vXBuild - 1 + 2; vSmallCage->vvi++)
		{
			vSmallCage->vNSmallCageTemp = vSmallCage->vvj*MAP_W+vSmallCage->vvi;
			if (vSmallCage->vNSmallCageTemp >= 0 &&
				vSmallCage->vNSmallCageTemp < MAP_MAX_SMALL_CAGE &&
				vMap[vSmallCage->vNSmallCageTemp].vQTree > 0)
			{
				vRight = TRUE;
				goto m4;
			}
		}
	}
m4:;
	if (vTop == TRUE && vLeft == FALSE && vBottom == FALSE && vRight == FALSE) return CONST_TYPE_WOOD_PLACE_WORK_TOP;
	if (vTop == FALSE && vLeft == TRUE && vBottom == FALSE && vRight == FALSE) return CONST_TYPE_WOOD_PLACE_WORK_LEFT;
	if (vTop == FALSE && vLeft == FALSE && vBottom == TRUE && vRight == FALSE) return CONST_TYPE_WOOD_PLACE_WORK_BOTTOM;
	if (vTop == FALSE && vLeft == FALSE && vBottom == FALSE && vRight == TRUE) return CONST_TYPE_WOOD_PLACE_WORK_RIGHT;
	if (vTop == TRUE && vLeft == TRUE && vBottom == FALSE && vRight == FALSE) return CONST_TYPE_WOOD_PLACE_WORK_TOP_LEFT;
	if (vTop == TRUE && vLeft == FALSE && vBottom == FALSE && vRight == TRUE) return CONST_TYPE_WOOD_PLACE_WORK_TOP_RIGHT;
	if (vTop == TRUE && vLeft == FALSE && vBottom == TRUE && vRight == FALSE) return CONST_TYPE_WOOD_PLACE_WORK_TOP_BOTTOM;
	if (vTop == FALSE && vLeft == TRUE && vBottom == FALSE && vRight == TRUE) return CONST_TYPE_WOOD_PLACE_WORK_LEFT_RIGHT;
	if (vTop == FALSE && vLeft == TRUE && vBottom == TRUE && vRight == FALSE) return CONST_TYPE_WOOD_PLACE_WORK_LEFT_BOTTOM;
	if (vTop == FALSE && vLeft == FALSE && vBottom == TRUE && vRight == TRUE) return CONST_TYPE_WOOD_PLACE_WORK_BOTTOM_RIGHT;
	if (vTop == TRUE && vLeft == FALSE && vBottom == TRUE && vRight == TRUE) return CONST_TYPE_WOOD_PLACE_WORK_TOP_RIGHT_BOTTOM;
	if (vTop == FALSE && vLeft == TRUE && vBottom == TRUE && vRight == TRUE) return CONST_TYPE_WOOD_PLACE_WORK_RIGHT_BOTTOM_LEFT;
	if (vTop == TRUE && vLeft == TRUE && vBottom == TRUE && vRight == FALSE) return CONST_TYPE_WOOD_PLACE_WORK_BOTTOM_LEFT_TOP;
	if (vTop == TRUE && vLeft == TRUE && vBottom == FALSE && vRight == TRUE) return CONST_TYPE_WOOD_PLACE_WORK_LEFT_TOP_RIGHT;
	return -1;
}

// Удалённость на базе от вала.
unsigned short MapBases::fRemotenessBase(real vX, real vY)
{
	unsigned short vIntX, vIntY;
	vIntX = fConverterX(vX); 
	vIntY = fConverterY(vY);
	// 256x256			//512x512
	vNSmallCage3 = (vIntX-vIntX%2)/2 + ((vIntY-vIntY%2)/2)*MAP_W;
	if (vNSmallCage3 < 0 || vNSmallCage3 >= MAP_MAX_SMALL_CAGE) return 0;
	return vMap[vNSmallCage3].vRemotenessBase;
}

unsigned short MapBases::fMaxRemotenessBase(real vX, real vY)
{
	unsigned short vIntX, vIntY;
	unsigned short vNBase;
	vIntX = fConverterX(vX); 
	vIntY = fConverterY(vY);
	vNSmallCage3 = (vIntX-vIntX%2)/2 + ((vIntY-vIntY%2)/2)*MAP_W;
	if (vNSmallCage3 < 0 || vNSmallCage3 >= MAP_MAX_SMALL_CAGE) return 0;
	vNBase = vMap[vNSmallCage3].vNBase;
	vRemoteness = vMap[vNSmallCage3].vRemoteness;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if ((vMap[vvi].vBuild == TRUE ||
			vMap[vvi].vInt == JASS_PATH_BLIGHT) &&
			vMap[vvi].vRemoteness > 0 &&
			vMap[vvi].vRemoteness > vRemoteness)
		{
			vRemoteness = vMap[vvi].vRemoteness;
			vNSmallCage3 = vvi;
		}
	}
	return vMap[vNSmallCage3].vInt3;
}

// Удалённость от центра карты.
unsigned short MapBases::fRemoteness(real vX, real vY)
{
	unsigned short vIntX, vIntY;
	vIntX = fConverterX(vX); 
	vIntY = fConverterY(vY);
	// 256x256			//512x512
	vNSmallCage3 = (vIntX-vIntX%2)/2 + ((vIntY-vIntY%2)/2)*MAP_W;
	if (vNSmallCage3 < 0 || vNSmallCage3 >= MAP_MAX_SMALL_CAGE) return 0;
	return vMap[vNSmallCage3].vRemoteness;
}

bool MapBases::fDistanceFromTheCenterOfTheMap()
{
	bool vFlag;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemotenessNext;
	int vNSmallCage;
	bool vTeleport1;
	bool vTeleport2;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vRemoteness = 0;
	}
	vNSmallCage = (MAP_H/2-1)*MAP_W+(MAP_W/2-1);
	vFlag = FALSE;
	vMap[vNSmallCage].vRemoteness = 1;
	vRemoteness = 0;
	vTeleport1 = FALSE;
	vTeleport2 = FALSE;
	while (vFlag == FALSE)
	{
		vRemoteness++;
		vRemotenessNext = vRemoteness+1;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if ((vMap[vvi].vBuild == TRUE ||
				vMap[vvi].vInt == JASS_PATH_BLIGHT ||
				vMap[vvi].vInt == JASS_WATER_LEVEL_1 ||
				vMap[vvi].vInt == JASS_TELEPORT_1_1 ||
				vMap[vvi].vInt == JASS_TELEPORT_1_2 ||
				vMap[vvi].vInt == JASS_TELEPORT_2_1 ||
				vMap[vvi].vInt == JASS_TELEPORT_2_2) &&
				vMap[vvi].vRemoteness > 0 &&
				vMap[vvi].vRemoteness == vRemoteness)
			{
				if (vTeleport1 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_1_1)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_1_2)
						{
							vvi = vvj;
							vTeleport1 = TRUE;
							break;
						}
					}
				}
				if (vTeleport1 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_1_2)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_1_1)
						{
							vvi = vvj;
							vTeleport1 = TRUE;
							break;
						}
					}
				}
				if (vTeleport2 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_2_1)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_2_2)
						{
							vvi = vvj;
							vTeleport2 = TRUE;
							break;
						}
					}
				}
				if (vTeleport2 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_2_2)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_2_1)
						{
							vvi = vvj;
							vTeleport2 = TRUE;
							break;
						}
					}
				}
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi+MAP_W].vBuild == TRUE ||
								vMap[vvi+MAP_W].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi+MAP_W].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi+MAP_W].vRemoteness == 0)
							{
								vMap[vvi+MAP_W].vRemoteness = vRemotenessNext;// Низ
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi-1].vBuild == TRUE ||
								vMap[vvi-1].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi-1].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi-1].vRemoteness == 0)
							{
								vMap[vvi-1].vRemoteness = vRemotenessNext;// Лево
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi-MAP_W].vBuild == TRUE ||
								vMap[vvi-MAP_W].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi-MAP_W].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi-MAP_W].vRemoteness == 0)
							{
								vMap[vvi-MAP_W].vRemoteness = vRemotenessNext;// Верх
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi+1].vBuild == TRUE ||
								vMap[vvi+1].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi+1].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi+1].vRemoteness == 0)
							{
								vMap[vvi+1].vRemoteness = vRemotenessNext;// Право
							}
						}
						break;
					}
				}
			}
		}
	}
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vRemoteness == 0)
		{
			vvi = vvi;
		}
	}
	return TRUE;
}

bool MapBases::fDeadEnds()
{
	bool vFlag;
	bool vFlag2;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemotenessNext;
	int vNSmallCage;
	unsigned short vCountNBase;
	bool vTeleport1;
	bool vTeleport2;
	char *pMap = (char*)vMapGlobal;
	vCountNBase = 0;
	vNSmallCage = (MAP_H/2-1)*MAP_W+(MAP_W/2-1);
	vFlag = FALSE;
	vRemoteness = 0;
	vTeleport1 = FALSE;
	vTeleport2 = FALSE;
	while (vFlag == FALSE)
	{
		vRemoteness++;
		vRemotenessNext = vRemoteness+1;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if ((vMap[vvi].vBuild == TRUE ||
				vMap[vvi].vInt == JASS_PATH_BLIGHT ||
				vMap[vvi].vInt == JASS_WATER_LEVEL_1 ||
				vMap[vvi].vInt == JASS_TELEPORT_1_1 ||
				vMap[vvi].vInt == JASS_TELEPORT_1_2 ||
				vMap[vvi].vInt == JASS_TELEPORT_2_1 ||
				vMap[vvi].vInt == JASS_TELEPORT_2_2) &&
				vMap[vvi].vRemoteness > 0 &&
				vMap[vvi].vRemoteness == vRemoteness)
			{
				if (vTeleport1 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_1_1)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_1_2)
						{
							vvi = vvj;
							vTeleport1 = TRUE;
							break;
						}
					}
				}
				if (vTeleport1 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_1_2)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_1_1)
						{
							vvi = vvj;
							vTeleport1 = TRUE;
							break;
						}
					}
				}
				if (vTeleport2 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_2_1)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_2_2)
						{
							vvi = vvj;
							vTeleport2 = TRUE;
							break;
						}
					}
				}
				if (vTeleport2 == FALSE &&
					vMap[vvi].vInt == JASS_TELEPORT_2_2)
				{
					for (vvj = 0; vvj < MAP_MAX_SMALL_CAGE; vvj++)
					{
						if (vMap[vvj].vInt == JASS_TELEPORT_2_1)
						{
							vvi = vvj;
							vTeleport2 = TRUE;
							break;
						}
					}
				}
				vFlag2 = FALSE;
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi+MAP_W].vBuild == TRUE ||
								vMap[vvi+MAP_W].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi+MAP_W].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi+MAP_W].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi+MAP_W].vRemoteness == 0)
							{
								// Низ
								if (vFlag2 == FALSE) vFlag2 = TRUE;
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi-1].vBuild == TRUE ||
								vMap[vvi-1].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi-1].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi-1].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi-1].vRemoteness == 0)
							{
								// Лево
								if (vFlag2 == FALSE) vFlag2 = TRUE;
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi-MAP_W].vBuild == TRUE ||
								vMap[vvi-MAP_W].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi-MAP_W].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi-MAP_W].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi-MAP_W].vRemoteness == 0)
							{
								// Верх
								if (vFlag2 == FALSE) vFlag2 = TRUE;
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if ((vMap[vvi+1].vBuild == TRUE ||
								vMap[vvi+1].vInt == JASS_PATH_BLIGHT ||
								vMap[vvi+1].vInt == JASS_WATER_LEVEL_1 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_1_1 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_1_2 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_2_1 ||
								vMap[vvi+1].vInt == JASS_TELEPORT_2_2) &&
								vMap[vvi+1].vRemoteness == 0)
							{
								// Право
								if (vFlag2 == FALSE) vFlag2 = TRUE;
							}
						}
						break;
					}
				}
				if (vFlag2 == FALSE && vMap[vvi].vBuild == TRUE && vMap[vvi].vNBase == 0)
				{
					if (fConstructionBase(vvi,MAP_MIN_Q_CELL_FOR_BASE,MAP_MAX_Q_CELL_FOR_BASE,vCountNBase+1) == TRUE)
					{
						vCountNBase++;
					}
				}
			}
		}
	}
	vMaxBase = vCountNBase;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vNBase != 0)
		{
			if (vMap[vvi].vNBase >= JASS_NumBase_MIN &&
				vMap[vvi].vNBase <= JASS_NumBase_MAX)
			{
				vMap[vvi].vInt = vMap[vvi].vNBase;
				pMap[vMap[vvi].vInt3] = (char)vMap[vvi].vInt;
				pMap[vMap[vvi].vInt3+1] = (char)vMap[vvi].vInt;
				pMap[vMap[vvi].vInt3+vW3] = (char)vMap[vvi].vInt;
				pMap[vMap[vvi].vInt3+vW3+1] = (char)vMap[vvi].vInt;
			}
		}
	}
	if (vCountNBase > 0)
	{
		fWriteMap(fGetPath());
	}
	return TRUE;
}

bool MapBases::fConstructionBase(int vNSmallCage_1, int vMinCell, int vMaxCell, unsigned short vNBase)
{
	if (!(vNSmallCage_1 >= 0 &&
		vNSmallCage_1 < MAP_MAX_SMALL_CAGE))
	{
		return FALSE;
	}
	if (vNBase < 0 || vNBase >= MAP_MAX_BASE) return FALSE;
	if (vMinCell > vMaxCell) return FALSE;
	bool vFlag;
	bool vFlagMainWall;
	bool vFlagExitWhile;
	bool vFlagMainWall2;
	bool vFlagFillBase;
	int vCountOpenCells;
	int vvi,vvj,vvz,vvk,vvl;
	int vNSmallCage;
	int vCountCellBase;
	int vCountCellBase2;
	int vNSmallCageWallMain;
	int vNSmallCageWallMainPrevious;
	int vRemotenessMin;
	int vRemotenessMax;
	unsigned short vRemoteness;
	int vCountCellNoTempBase;
	int vQTowerInBase;
	bool vFlagMin;
	int vRemotenessMinPrevious;
	bool vFlagTemp2;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vNBaseTemp = 0;
		vMap[vvi].vNBaseTemp2 = 0;
	}
	vNSmallCage = vNSmallCage_1;
	vFlag = FALSE;
	vMap[vNSmallCage].vNBaseTemp = vNBase;
	vCountCellBase = 1;
	vNSmallCageWallMain = vNSmallCage;
	vRemoteness = vMap[vNSmallCage].vRemoteness;
	vFlagExitWhile = FALSE;
	vFlagMainWall2 = FALSE;
	vCountOpenCells = 0;
	vFlagFillBase = FALSE;
	vNSmallCageWallMainPrevious = -1;
	vCountCellBase2 = 0;
	vRemotenessMin = 0;
	vRemotenessMinPrevious = 0;
	while (vFlag == FALSE)
	{
		vFlagMainWall = FALSE;
		//if (vFlagMainWall2 == FALSE)
		for (vvj = vRemoteness; vvj <= vRemoteness; vvj++)
		{
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vRemoteness == 0) continue;
			if ((vMap[vvi].vRemoteness == vvj) &&
				vMap[vvi].vX >= 2 &&
				vMap[vvi].vX <= MAP_W - 4 &&
				vMap[vvi].vY >= 2 &&
				vMap[vvi].vY <= MAP_H - 4 &&
				(vMap[vvi-2].vNBaseTemp == vNBase ||
				vMap[vvi+3].vNBaseTemp == vNBase ||
				vMap[vvi-MAP_W*2].vNBaseTemp == vNBase ||
				vMap[vvi+MAP_W*3].vNBaseTemp == vNBase) &&
				((vMap[vvi].vQObstacles == 0 &&
				vMap[vvi].vBuild == TRUE) ||
				(vMap[vvi+1].vQObstacles == 0 &&
				vMap[vvi+1].vBuild == TRUE) ||
				(vMap[vvi+MAP_W*1+1].vQObstacles == 0 &&
				vMap[vvi+MAP_W*1+1].vBuild == TRUE) ||
				(vMap[vvi+MAP_W*1].vQObstacles == 0 &&
				vMap[vvi+MAP_W*1].vBuild == TRUE)) &&
				(((vMap[vvi+1].vTree.vTop == TRUE ||
				vMap[vvi+1].vCliff.vTop == TRUE) &&
				vMap[vvi+1].vQObstacles == 1 &&
				vMap[vvi+1].vBuild == TRUE &&
				vMap[vvi+1+MAP_W].vQObstacles == 1 &&
				vMap[vvi+1+MAP_W].vBuild == TRUE) ||
				((vMap[vvi+MAP_W*1].vTree.vLeft == TRUE ||
				vMap[vvi+MAP_W*1].vCliff.vLeft == TRUE) &&
				vMap[vvi+MAP_W*1].vQObstacles == 1 &&
				vMap[vvi+MAP_W*1].vBuild == TRUE &&
				vMap[vvi+MAP_W*1+1].vQObstacles == 1 &&
				vMap[vvi+MAP_W*1+1].vBuild == TRUE) ||
				((vMap[vvi].vTree.vTop == TRUE ||
				vMap[vvi].vCliff.vTop == TRUE) &&
				vMap[vvi].vQObstacles == 1 &&
				vMap[vvi].vBuild == TRUE &&
				vMap[vvi+MAP_W*1].vQObstacles == 1 &&
				vMap[vvi+MAP_W*1].vBuild == TRUE) ||
				((vMap[vvi].vTree.vLeft == TRUE ||
				vMap[vvi].vCliff.vLeft == TRUE) &&
				vMap[vvi].vQObstacles == 1 &&
				vMap[vvi].vBuild == TRUE &&
				vMap[vvi+1].vQObstacles == 1 &&
				vMap[vvi+1].vBuild == TRUE)))
			{
				vFlagMainWall = TRUE;
			}
			if ((vMap[vvi].vRemoteness == vvj) &&
				vMap[vvi].vX >= 2 &&
				vMap[vvi].vX <= MAP_W - 4 &&
				vMap[vvi].vY >= 2 &&
				vMap[vvi].vY <= MAP_H - 4 &&
				(vMap[vvi-2].vNBaseTemp == vNBase ||
				vMap[vvi+3].vNBaseTemp == vNBase ||
				vMap[vvi-MAP_W*2].vNBaseTemp == vNBase ||
				vMap[vvi+MAP_W*3].vNBaseTemp == vNBase) &&
				vMap[vvi].vQObstacles == 0 &&
				vMap[vvi].vBuild == TRUE &&
				vMap[vvi+1].vQObstacles == 0 &&
				vMap[vvi+1].vBuild == TRUE &&
				vMap[vvi+1+MAP_W].vQObstacles == 0 &&
				vMap[vvi+1+MAP_W].vBuild == TRUE &&
				vMap[vvi+MAP_W].vQObstacles == 0 &&
				vMap[vvi+MAP_W].vBuild == TRUE &&
				((vMap[vvi-1].vQObstacles == 1 &&//1
				vMap[vvi-1].vBuild == TRUE &&
				vMap[vvi-1+MAP_W].vQObstacles == 1 &&//2
				vMap[vvi-1+MAP_W].vBuild == TRUE &&
				vMap[vvi+2].vQObstacles == 1 &&//3
				vMap[vvi+2].vBuild == TRUE &&
				vMap[vvi+2+MAP_W].vQObstacles == 1 &&//4
				vMap[vvi+2+MAP_W].vBuild == TRUE &&
				vMap[vvi-1+MAP_W*2].vQObstacles == 1 &&//5
				vMap[vvi-1+MAP_W*2].vBuild == TRUE &&
				vMap[vvi+2+MAP_W*2].vQObstacles == 1 &&//6
				vMap[vvi+2+MAP_W*2].vBuild == TRUE &&
				vMap[vvi-1-MAP_W*1].vQObstacles == 1 &&//7
				vMap[vvi-1-MAP_W*1].vBuild == TRUE &&
				vMap[vvi+2-MAP_W*1].vQObstacles == 1 &&//8
				vMap[vvi+2-MAP_W*1].vBuild == TRUE) ||
				(vMap[vvi-MAP_W*1].vQObstacles == 1 &&//1
				vMap[vvi-MAP_W*1].vBuild == TRUE &&
				vMap[vvi-MAP_W*1+1].vQObstacles == 1 &&//2
				vMap[vvi-MAP_W*1+1].vBuild == TRUE &&
				vMap[vvi+MAP_W*2].vQObstacles == 1 &&//3
				vMap[vvi+MAP_W*2].vBuild == TRUE &&
				vMap[vvi+MAP_W*2+1].vQObstacles == 1 &&//4
				vMap[vvi+MAP_W*2+1].vBuild == TRUE &&
				vMap[vvi-MAP_W*1+2].vQObstacles == 1 &&//5
				vMap[vvi-MAP_W*1+2].vBuild == TRUE &&
				vMap[vvi+MAP_W*2+2].vQObstacles == 1 &&//6
				vMap[vvi+MAP_W*2+2].vBuild == TRUE &&
				vMap[vvi-MAP_W*1-1].vQObstacles == 1 &&//7
				vMap[vvi-MAP_W*1-1].vBuild == TRUE &&
				vMap[vvi+MAP_W*2-1].vQObstacles == 1 &&//8
				vMap[vvi+MAP_W*2-1].vBuild == TRUE)))
			{
				vFlagMainWall = TRUE;
			}
			if (vFlagMainWall == TRUE)
			{
				if (vCountCellBase > vMaxCell)
				{
					vFlagExitWhile = TRUE;
					break;
				}
				if (vNSmallCageWallMainPrevious != vvi)
				{
					/*if (vMap[vvi].vX >= 30 &&
						vMap[vvi].vX <= 50 &&
						vMap[vvi].vY >= 17 &&
						vMap[vvi].vY <= 50)
					{
						vFlagExitWhile = vFlagExitWhile;
					}*/
					vNSmallCageWallMainPrevious = vNSmallCageWallMain;
					vNSmallCageWallMain = vvi;
					vMap[vvi].vNBaseTemp = vNBase;
					vMap[vvi+1].vNBaseTemp = vNBase;
					vMap[vvi+MAP_W].vNBaseTemp = vNBase;
					vMap[vvi+MAP_W+1].vNBaseTemp = vNBase;
					vRemoteness = vMap[vvi].vRemoteness;
					vFlagMainWall2 = TRUE;
					vRemotenessMax = 0;
					vFlagMin = FALSE;
					for (vvk = vMap[vvi - MAP_W].vY; vvk <= vMap[vvi+2*MAP_W].vY; vvk++)
					{
						for (vvz = vMap[vvi - 1].vX; vvz <= vMap[vvi+2].vX; vvz++)
						{
							if (vMap[vvk*MAP_W+vvz].vBuild == TRUE &&
								vMap[vvk*MAP_W+vvz].vNBaseTemp == 0)
							{
								vMap[vvk*MAP_W+vvz].vNBaseTemp = vNBase;
							}
							if (vMap[vvk*MAP_W+vvz].vBuild == TRUE &&
								(vMap[vvk*MAP_W+vvz].vRemoteness < vRemotenessMin ||
								vRemotenessMin == 0))
							{
								if (vFlagMin == FALSE)
								{
									vRemotenessMinPrevious = vRemotenessMin;
									vFlagMin = TRUE;
								}
								vRemotenessMin = vMap[vvk*MAP_W+vvz].vRemoteness;
							}
							if (vMap[vvk*MAP_W+vvz].vBuild == TRUE &&
								(vMap[vvk*MAP_W+vvz].vRemoteness > vRemotenessMax ||
								vRemotenessMax == 0))
							{
								vRemotenessMax = vMap[vvk*MAP_W+vvz].vRemoteness;
							}
						}
					}
					/*if (vFlagMin == TRUE)
					{
						vCountCellNoTempBase = 0;
						for (vvk = vMap[vvi - MAP_W].vY; vvk <= vMap[vvi+2*MAP_W].vY; vvk++)
						{
							for (vvz = vMap[vvi - 1].vX; vvz <= vMap[vvi+2].vX; vvz++)
							{
								if (vMap[vvk*MAP_W+vvz].vBuild == TRUE &&
									vMap[vvk*MAP_W+vvz].vRemoteness == vRemotenessMin)
								{
									if (vvz == vMap[vvi - 1].vX || vvz == vMap[vvi+2].vX)
									for (vvl = vMap[vvi - MAP_W].vY; vvl <= vMap[vvi+2*MAP_W].vY; vvl++)
									{
										if (vMap[vvl*MAP_W+vvz].vBuild == TRUE)
										{
											if (vMap[vvl*MAP_W+vvz].vNBaseTemp == 0)
											{
												vCountCellNoTempBase++;
											}
										}
									}
									if (vvk == vMap[vvi - MAP_W].vY || vvk == vMap[vvi+2*MAP_W].vY)
									for (vvl = vMap[vvi - 1].vX; vvl <= vMap[vvi+2].vX; vvl++)
									{
										if (vMap[vvk*MAP_W+vvl].vBuild == TRUE)
										{
											if (vMap[vvk*MAP_W+vvl].vNBaseTemp == 0)
											{
												vCountCellNoTempBase++;
											}
										}
									}
									goto m55;
								}
							}
						}
m55:;
						if (vCountCellNoTempBase <= 0)
						{
							return FALSE;
						}
					}*/
					if (vFlagFillBase == FALSE &&
						vCountCellBase >= vMinCell && vCountCellBase <= vMaxCell)
					{
						vCountOpenCells = 0;
						for (vvk = 0; vvk < MAP_MAX_SMALL_CAGE; vvk++)
						{
							if (vMap[vvk].vBuild == TRUE &&
								vMap[vvk].vRemoteness >= vRemotenessMax &&
								vMap[vvk].vNBase == 0 &&
								vMap[vvk].vNBaseTemp == vNBase)
							{
								for (vvl = 0; vvl < 4; vvl++)
								{
									switch (vvl)
									{
									case 0:
										if (vvk<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvk+MAP_W].vY < MAP_H)
										{
											if (vMap[vvk+MAP_W].vBuild == TRUE &&
												vMap[vvk+MAP_W].vNBase == 0 &&
												vMap[vvk+MAP_W].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 1:
										if (vvk>=1 && vMap[vvk-1].vX >= 0)
										{
											if (vMap[vvk-1].vBuild == TRUE &&
												vMap[vvk-1].vNBase == 0 &&
												vMap[vvk-1].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 2:
										if (vvk>=MAP_W && vMap[vvk-MAP_W].vY >= 0)
										{
											if (vMap[vvk-MAP_W].vBuild == TRUE &&
												vMap[vvk-MAP_W].vNBase == 0 &&
												vMap[vvk-MAP_W].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 3:
										if (vvk<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvk+1].vX < MAP_W)
										{
											if (vMap[vvk+1].vBuild == TRUE &&
												vMap[vvk+1].vNBase == 0 &&
												vMap[vvk+1].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									}
								}
							}
						}
						if (vCountOpenCells == 0)
						{
							vFlagFillBase = TRUE;
						}
					}
					vFlagTemp2 = TRUE;
					if (vFlagTemp2 == TRUE && vFlagFillBase == TRUE && vFlagMin == TRUE)
					{
						vCountOpenCells = 0;
						for (vvk = 0; vvk < MAP_MAX_SMALL_CAGE; vvk++)
						{
							if (vMap[vvk].vBuild == TRUE &&
								vMap[vvk].vRemoteness == vRemotenessMax &&
								vMap[vvk].vNBase == 0 &&
								vMap[vvk].vNBaseTemp == vNBase)
							{
								for (vvl = 0; vvl < 4; vvl++)
								{
									switch (vvl)
									{
									case 0:
										if (vvk<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvk+MAP_W].vY < MAP_H)
										{
											if (vMap[vvk+MAP_W].vBuild == TRUE &&
												vMap[vvk+MAP_W].vNBase == 0 &&
												vMap[vvk+MAP_W].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 1:
										if (vvk>=1 && vMap[vvk-1].vX >= 0)
										{
											if (vMap[vvk-1].vBuild == TRUE &&
												vMap[vvk-1].vNBase == 0 &&
												vMap[vvk-1].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 2:
										if (vvk>=MAP_W && vMap[vvk-MAP_W].vY >= 0)
										{
											if (vMap[vvk-MAP_W].vBuild == TRUE &&
												vMap[vvk-MAP_W].vNBase == 0 &&
												vMap[vvk-MAP_W].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 3:
										if (vvk<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvk+1].vX < MAP_W)
										{
											if (vMap[vvk+1].vBuild == TRUE &&
												vMap[vvk+1].vNBase == 0 &&
												vMap[vvk+1].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									}
								}
							}
						}
						if (vCountOpenCells > 0)
						{
							vFlagTemp2 = FALSE;
						}
					}
					if (vFlagTemp2 == TRUE && vFlagFillBase == TRUE)
					{
						vCountOpenCells = 0;
						for (vvk = 0; vvk < MAP_MAX_SMALL_CAGE; vvk++)
						{
							if (vMap[vvk].vBuild == TRUE &&
								vMap[vvk].vNBase == 0 &&
								vMap[vvk].vNBaseTemp == vNBase)
							{
								for (vvl = 0; vvl < 4; vvl++)
								{
									switch (vvl)
									{
									case 0:
										if (vvk<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvk+MAP_W].vY < MAP_H)
										{
											if (vMap[vvk+MAP_W].vBuild == TRUE &&
												vMap[vvk+MAP_W].vNBase == 0 &&
												vMap[vvk+MAP_W].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 1:
										if (vvk>=1 && vMap[vvk-1].vX >= 0)
										{
											if (vMap[vvk-1].vBuild == TRUE &&
												vMap[vvk-1].vNBase == 0 &&
												vMap[vvk-1].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 2:
										if (vvk>=MAP_W && vMap[vvk-MAP_W].vY >= 0)
										{
											if (vMap[vvk-MAP_W].vBuild == TRUE &&
												vMap[vvk-MAP_W].vNBase == 0 &&
												vMap[vvk-MAP_W].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									case 3:
										if (vvk<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvk+1].vX < MAP_W)
										{
											if (vMap[vvk+1].vBuild == TRUE &&
												vMap[vvk+1].vNBase == 0 &&
												vMap[vvk+1].vNBaseTemp == 0)
											{
												vCountOpenCells++;
											}
										}
										break;
									}
								}
							}
						}
						if (vCountOpenCells >= 7)
						{
							vFlagTemp2 = FALSE;
						}
					}
					if (vFlagFillBase == TRUE &&
						vFlagTemp2 == TRUE)
					for (vvk = 0; vvk < MAP_MAX_SMALL_CAGE; vvk++)
					{
						if (vMap[vvk].vNBaseTemp2 == 0 &&
							vMap[vvk].vNBaseTemp != 0)
						{
							vMap[vvk].vNBaseTemp2 = vMap[vvk].vNBaseTemp;
							vCountCellBase2++;
						}
					}
					if (vFlagFillBase == TRUE &&
						vFlagMin == TRUE &&
						vCountCellBase2 >= vMinCell && vCountCellBase2 <= vMaxCell)
					{
						vQTowerInBase = fZoneTowerTemp(vNSmallCageWallMain,vNBase)/4;
						if (vQTowerInBase >= 33)
						{
							vFlagExitWhile = TRUE;
							break;
						}
					}
					break;
				}
				else
				{
					//return FALSE;
				}
				break;
			}
		}
		}
		if (vFlagExitWhile == TRUE)
		{
			break;
		}
m66:;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vBuild == TRUE &&
				(vNSmallCageWallMain == vNSmallCage_1 ||
				(vNSmallCageWallMain != vNSmallCage_1 &&
				(vFlagFillBase == FALSE &&
				(vFlagMainWall2 == FALSE ||
				(vFlagMainWall2 == TRUE &&
				vMap[vvi].vRemoteness > vMap[vNSmallCageWallMain].vRemoteness)) ||
				vFlagFillBase == TRUE))) &&
				vMap[vvi].vNBase == 0 &&
				vMap[vvi].vNBaseTemp == vNBase)
			{
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vMap[vvi+MAP_W].vBuild == TRUE &&
								vMap[vvi+MAP_W].vNBase == 0 &&
								vMap[vvi+MAP_W].vNBaseTemp == 0)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi+MAP_W].vNBaseTemp = vNBase;// Низ
								vCountCellBase++;
								if (vRemoteness > vMap[vvi+MAP_W].vRemoteness) vRemoteness = vMap[vvi+MAP_W].vRemoteness;
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vMap[vvi-1].vBuild == TRUE &&
								vMap[vvi-1].vNBase == 0 &&
								vMap[vvi-1].vNBaseTemp == 0)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi-1].vNBaseTemp = vNBase;// Лево
								vCountCellBase++;
								if (vRemoteness > vMap[vvi-1].vRemoteness) vRemoteness = vMap[vvi-1].vRemoteness;
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vMap[vvi-MAP_W].vBuild == TRUE &&
								vMap[vvi-MAP_W].vNBase == 0 &&
								vMap[vvi-MAP_W].vNBaseTemp == 0)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi-MAP_W].vNBaseTemp = vNBase;// Верх
								vCountCellBase++;
								if (vRemoteness > vMap[vvi-MAP_W].vRemoteness) vRemoteness = vMap[vvi-MAP_W].vRemoteness;
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vMap[vvi+1].vBuild == TRUE &&
								vMap[vvi+1].vNBase == 0 &&
								vMap[vvi+1].vNBaseTemp == 0)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi+1].vNBaseTemp = vNBase;// Право
								vCountCellBase++;
								if (vRemoteness > vMap[vvi+1].vRemoteness) vRemoteness = vMap[vvi+1].vRemoteness;
							}
						}
						break;
					}
				}
				if (vFlagMainWall2 == FALSE || vFlagFillBase == TRUE)
				{
					break;
				}
			}
		}
		if (vCountCellBase > vMaxCell)
		{
			break;
			//return FALSE;
		}
		/*if (vFlag == TRUE && vFlagMainWall2 == TRUE && vCountOpenCells >= 6)
		{
			vNSmallCageWallMain = vNSmallCageWallMainPrevious;
			break;
		}*/
		if (vFlag == FALSE && vFlagMainWall2 == TRUE && vFlagFillBase == FALSE)
		{
			goto m66;
		}
		if (vFlag == TRUE && vFlagMainWall2 == TRUE && vFlagFillBase == FALSE)
		{
			//vFlagMainWall2 = FALSE;
			vFlagFillBase = TRUE;
			vFlag = FALSE;
			vFlagTemp2 = TRUE;
			if (vFlagFillBase == TRUE)
			{
				vCountOpenCells = 0;
				for (vvk = 0; vvk < MAP_MAX_SMALL_CAGE; vvk++)
				{
					if (vMap[vvk].vBuild == TRUE &&
						//vMap[vvk].vRemoteness >= vMap[vNSmallCageWallMain].vRemoteness &&
						vMap[vvk].vNBase == 0 &&
						vMap[vvk].vNBaseTemp == vNBase)
					{
						for (vvl = 0; vvl < 4; vvl++)
						{
							switch (vvl)
							{
							case 0:
								if (vvk<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvk+MAP_W].vY < MAP_H)
								{
									if (vMap[vvk+MAP_W].vBuild == TRUE &&
										vMap[vvk+MAP_W].vNBase == 0 &&
										vMap[vvk+MAP_W].vNBaseTemp == 0)
									{
										vCountOpenCells++;
									}
								}
								break;
							case 1:
								if (vvk>=1 && vMap[vvk-1].vX >= 0)
								{
									if (vMap[vvk-1].vBuild == TRUE &&
										vMap[vvk-1].vNBase == 0 &&
										vMap[vvk-1].vNBaseTemp == 0)
									{
										vCountOpenCells++;
									}
								}
								break;
							case 2:
								if (vvk>=MAP_W && vMap[vvk-MAP_W].vY >= 0)
								{
									if (vMap[vvk-MAP_W].vBuild == TRUE &&
										vMap[vvk-MAP_W].vNBase == 0 &&
										vMap[vvk-MAP_W].vNBaseTemp == 0)
									{
										vCountOpenCells++;
									}
								}
								break;
							case 3:
								if (vvk<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvk+1].vX < MAP_W)
								{
									if (vMap[vvk+1].vBuild == TRUE &&
										vMap[vvk+1].vNBase == 0 &&
										vMap[vvk+1].vNBaseTemp == 0)
									{
										vCountOpenCells++;
									}
								}
								break;
							}
						}
					}
				}
				if (vCountOpenCells >= 9)
				{
					vFlagTemp2 = FALSE;
				}
			}
			if (vFlagFillBase == TRUE &&
				vFlagTemp2 == TRUE)
			for (vvk = 0; vvk < MAP_MAX_SMALL_CAGE; vvk++)
			{
				if (vMap[vvk].vNBaseTemp2 == 0 &&
					vMap[vvk].vNBaseTemp != 0)
				{
					vMap[vvk].vNBaseTemp2 = vMap[vvk].vNBaseTemp;
					vCountCellBase2++;
				}
			}
		}
	}
	if (vFlagFillBase == TRUE &&
		vCountCellBase2 >= vMinCell && vCountCellBase2 <= vMaxCell)
	{
		/*vRemotenessMin = 0;
		vRemotenessMax = 0;
		vvi = vNSmallCageWallMain;
		for (vvk = vMap[vvi - MAP_W].vY; vvk <= vMap[vvi+2*MAP_W].vY; vvk++)
		{
			for (vvz = vMap[vvi - 1].vX; vvz <= vMap[vvi+2].vX; vvz++)
			{
				if (vMap[vvk*MAP_W+vvz].vBuild == TRUE &&
					(vMap[vvk*MAP_W+vvz].vRemoteness < vRemotenessMin ||
					vRemotenessMin == 0))
				{
					vRemotenessMin = vMap[vvk*MAP_W+vvz].vRemoteness;
				}
				if (vMap[vvk*MAP_W+vvz].vBuild == TRUE &&
					(vMap[vvk*MAP_W+vvz].vRemoteness > vRemotenessMax ||
					vRemotenessMax == 0))
				{
					vRemotenessMax = vMap[vvk*MAP_W+vvz].vRemoteness;
				}
			}
		}
		vCountCellNoTempBase = 0;
		for (vvk = vMap[vvi - MAP_W].vY; vvk <= vMap[vvi+2*MAP_W].vY; vvk++)
		{
			for (vvz = vMap[vvi - 1].vX; vvz <= vMap[vvi+2].vX; vvz++)
			{
				if (vMap[vvk*MAP_W+vvz].vBuild == TRUE &&
					vMap[vvk*MAP_W+vvz].vRemoteness == vRemotenessMin)
				{
					if (vvz == vMap[vvi - 1].vX || vvz == vMap[vvi+2].vX)
					for (vvl = vMap[vvi - MAP_W].vY; vvl <= vMap[vvi+2*MAP_W].vY; vvl++)
					{
						if (vMap[vvl*MAP_W+vvz].vBuild == TRUE)
						{
							vMap[vvl*MAP_W+vvz].vNBaseTemp = 0;
						}
					}
					if (vvk == vMap[vvi - MAP_W].vY || vvk == vMap[vvi+2*MAP_W].vY)
					for (vvl = vMap[vvi - 1].vX; vvl <= vMap[vvi+2].vX; vvl++)
					{
						if (vMap[vvk*MAP_W+vvl].vBuild == TRUE)
						{
							vMap[vvk*MAP_W+vvl].vNBaseTemp = 0;
						}
					}
					goto m65;
				}
			}
		}
m65:;*/
		if (vFlagFillBase == TRUE)
		{
			vCountOpenCells = 0;
			for (vvk = 0; vvk < MAP_MAX_SMALL_CAGE; vvk++)
			{
				if (vMap[vvk].vBuild == TRUE &&
					vMap[vvk].vRemoteness >= vMap[vNSmallCageWallMain].vRemoteness &&
					vMap[vvk].vNBase == 0 &&
					vMap[vvk].vNBaseTemp2 == vNBase)
				{
					for (vvl = 0; vvl < 4; vvl++)
					{
						switch (vvl)
						{
						case 0:
							if (vvk<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvk+MAP_W].vY < MAP_H)
							{
								if (vMap[vvk+MAP_W].vBuild == TRUE &&
									vMap[vvk+MAP_W].vNBase == 0 &&
									vMap[vvk+MAP_W].vNBaseTemp2 == 0)
								{
									vCountOpenCells++;
								}
							}
							break;
						case 1:
							if (vvk>=1 && vMap[vvk-1].vX >= 0)
							{
								if (vMap[vvk-1].vBuild == TRUE &&
									vMap[vvk-1].vNBase == 0 &&
									vMap[vvk-1].vNBaseTemp2 == 0)
								{
									vCountOpenCells++;
								}
							}
							break;
						case 2:
							if (vvk>=MAP_W && vMap[vvk-MAP_W].vY >= 0)
							{
								if (vMap[vvk-MAP_W].vBuild == TRUE &&
									vMap[vvk-MAP_W].vNBase == 0 &&
									vMap[vvk-MAP_W].vNBaseTemp2 == 0)
								{
									vCountOpenCells++;
								}
							}
							break;
						case 3:
							if (vvk<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvk+1].vX < MAP_W)
							{
								if (vMap[vvk+1].vBuild == TRUE &&
									vMap[vvk+1].vNBase == 0 &&
									vMap[vvk+1].vNBaseTemp2 == 0)
								{
									vCountOpenCells++;
								}
							}
							break;
						}
					}
				}
			}
			if (vCountOpenCells >= 9)
			{
				return FALSE;
			}
		}
		vQTowerInBase = fZoneTowerTemp(vNSmallCageWallMain,vNBase)/4;
		if (vQTowerInBase < 20)
		{
			return FALSE;
		}
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vBuild == TRUE &&
				//vMap[vvi].vRemoteness >= vMap[vNSmallCageWallMain].vRemoteness &&
				vMap[vvi].vNBase == 0 &&
				vMap[vvi].vNBaseTemp2 == vNBase)
			{
				vMap[vvi].vNBase = vNBase;
			}
		}
		fWriteFileTGA();
		return TRUE;
	}
	return FALSE;
}

int MapBases::fZoneTowerTemp(int vMainWall, unsigned short vNBase)
{
	bool vFlag;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemotenessNext;
	int vNSmallCage;
	int vQCellTower;
	vNSmallCage = vMainWall;
	if (!(vNSmallCage >= 0 &&
		vNSmallCage < MAP_MAX_SMALL_CAGE))
	{
		return FALSE;
	}
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vTemp = FALSE;
		vMap[vvi].vRemotenessTemp = 0;
	}
	vFlag = FALSE;
	vMap[vNSmallCage].vTemp = TRUE;
	vMap[vNSmallCage].vRemotenessTemp = 1;
	vRemoteness = 0;
	vQCellTower = 1;
	while (vFlag == FALSE)
	{
		vRemoteness++;
		if (vRemoteness >= 14) break;
		vRemotenessNext = vRemoteness + 1;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vTemp == TRUE &&
				vMap[vvi].vRemotenessTemp == vRemoteness)
			{
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+MAP_W].vTemp == FALSE &&
								vMap[vvi+MAP_W].vRemotenessTemp == 0)
							{
								vMap[vvi+MAP_W].vTemp = TRUE;// Низ
								vMap[vvi+MAP_W].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi+MAP_W].vBuild == TRUE &&
									vMap[vvi+MAP_W].vNBaseTemp2 == vNBase)
								{
									vQCellTower++;
								}
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-1].vTemp == FALSE &&
								vMap[vvi-1].vRemotenessTemp == 0)
							{
								vMap[vvi-1].vTemp = TRUE;// Лево
								vMap[vvi-1].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi-1].vBuild == TRUE &&
									vMap[vvi-1].vNBaseTemp2 == vNBase)
								{
									vQCellTower++;
								}
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-MAP_W].vTemp == FALSE &&
								vMap[vvi-MAP_W].vRemotenessTemp == 0)
							{
								vMap[vvi-MAP_W].vTemp = TRUE;// Верх
								vMap[vvi-MAP_W].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi-MAP_W].vBuild == TRUE &&
									vMap[vvi-MAP_W].vNBaseTemp2 == vNBase)
								{
									vQCellTower++;
								}
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+1].vTemp == FALSE &&
								vMap[vvi+1].vRemotenessTemp == 0)
							{
								vMap[vvi+1].vTemp = TRUE;// Право
								vMap[vvi+1].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi+1].vBuild == TRUE &&
									vMap[vvi+1].vNBaseTemp2 == vNBase)
								{
									vQCellTower++;
								}
							}
						}
						break;
					}
				}
			}
		}
	}
	return vQCellTower;
}

unsigned short MapBases::fBaseN(unsigned int vInt3)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return 0;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vInt3 == vInt3)
		{
			return vMap[vvi].vNBase;
		}
	}
	return 0;
}

bool MapBases::fFreeZoneTower(unsigned int vInt3)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return FALSE;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vInt3 == vInt3)
		{
			vMap[vvi].vTower = FALSE;
			break;
		}
	}
	return TRUE;
}

unsigned short MapBases::fQSmallCageForTower(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return MAP_MAX_SMALL_CAGE;
	vQ = 0;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vBuild == TRUE &&
			vMap[vvi].vTower == TRUE &&
			vMap[vvi].vNBase == vBase->vNBase)
		{
			vQ++;
		}
	}
	return vQ;
}

bool MapBases::fZoneTower(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase < 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	if (vBase->vMainWall.fGetWall() == NULL) return FALSE;
	bool vFlag;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemotenessNext;
	int vNSmallCage;
	vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage;
	if (!(vNSmallCage >= 0 &&
		vNSmallCage < MAP_MAX_SMALL_CAGE))
	{
		return FALSE;
	}
	vFlag = FALSE;
	vMap[vNSmallCage].vTower = TRUE;
	vMap[vNSmallCage].vRemotenessTower = 1;
	vRemoteness = 0;
	while (vFlag == FALSE)
	{
		vRemoteness++;
		if (vRemoteness >= 14) break;
		vRemotenessNext = vRemoteness + 1;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vTower == TRUE &&
				vMap[vvi].vRemotenessTower == vRemoteness)
			{
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+MAP_W].vTower == FALSE &&
								vMap[vvi+MAP_W].vRemotenessTower == 0)
							{
								vMap[vvi+MAP_W].vTower = TRUE;// Низ
								vMap[vvi+MAP_W].vRemotenessTower = vRemotenessNext;
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-1].vTower == FALSE &&
								vMap[vvi-1].vRemotenessTower == 0)
							{
								vMap[vvi-1].vTower = TRUE;// Лево
								vMap[vvi-1].vRemotenessTower = vRemotenessNext;
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-MAP_W].vTower == FALSE &&
								vMap[vvi-MAP_W].vRemotenessTower == 0)
							{
								vMap[vvi-MAP_W].vTower = TRUE;// Верх
								vMap[vvi-MAP_W].vRemotenessTower = vRemotenessNext;
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+1].vTower == FALSE &&
								vMap[vvi+1].vRemotenessTower == 0)
							{
								vMap[vvi+1].vTower = TRUE;// Право
								vMap[vvi+1].vRemotenessTower = vRemotenessNext;
							}
						}
						break;
					}
				}
			}
		}
	}
	//fWriteFileTGA();
	return TRUE;
}

bool MapBases::fZoneTowerTemp2(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase < 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	if (vBase->vMainWall.fGetWall() == NULL) return FALSE;
	bool vFlag;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemotenessNext;
	int vNSmallCage;
	int vQCellTower;
	vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage;
	if (!(vNSmallCage >= 0 &&
		vNSmallCage < MAP_MAX_SMALL_CAGE))
	{
		return FALSE;
	}
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vTemp = FALSE;
		vMap[vvi].vRemotenessTemp = 0;
	}
	vFlag = FALSE;
	vMap[vNSmallCage].vTemp = TRUE;
	vMap[vNSmallCage].vRemotenessTemp = 1;
	vRemoteness = 0;
	vQCellTower = 1;
	while (vFlag == FALSE)
	{
		vRemoteness++;
		if (vRemoteness >= 14) break;
		vRemotenessNext = vRemoteness + 1;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vTemp == TRUE &&
				vMap[vvi].vRemotenessTemp == vRemoteness)
			{
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+MAP_W].vTemp == FALSE &&
								vMap[vvi+MAP_W].vRemotenessTemp == 0)
							{
								vMap[vvi+MAP_W].vTemp = TRUE;// Низ
								vMap[vvi+MAP_W].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi+MAP_W].vBuild &&
									vMap[vvi+MAP_W].vNBase == vBase->vNBase &&
									vMap[vvi+MAP_W].vRemotenessBase != 0)
								{
									vQCellTower++;
								}
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-1].vTemp == FALSE &&
								vMap[vvi-1].vRemotenessTemp == 0)
							{
								vMap[vvi-1].vTemp = TRUE;// Лево
								vMap[vvi-1].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi-1].vBuild &&
									vMap[vvi-1].vNBase == vBase->vNBase &&
									vMap[vvi-1].vRemotenessBase != 0)
								{
									vQCellTower++;
								}
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-MAP_W].vTemp == FALSE &&
								vMap[vvi-MAP_W].vRemotenessTemp == 0)
							{
								vMap[vvi-MAP_W].vTemp = TRUE;// Верх
								vMap[vvi-MAP_W].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi-MAP_W].vBuild &&
									vMap[vvi-MAP_W].vNBase == vBase->vNBase &&
									vMap[vvi-MAP_W].vRemotenessBase != 0)
								{
									vQCellTower++;
								}
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+1].vTemp == FALSE &&
								vMap[vvi+1].vRemotenessTemp == 0)
							{
								vMap[vvi+1].vTemp = TRUE;// Право
								vMap[vvi+1].vRemotenessTemp = vRemotenessNext;
								if (vMap[vvi+1].vBuild &&
									vMap[vvi+1].vNBase == vBase->vNBase &&
									vMap[vvi+1].vRemotenessBase != 0)
								{
									vQCellTower++;
								}
							}
						}
						break;
					}
				}
			}
		}
	}
	vBase->vQTowerInZoneMainWall = 0;
	if (vQCellTower >= 8)
	{
		vBase->vQTowerInZoneMainWall = (unsigned short)(vQCellTower/4-1);
	}
	//fWriteFileTGA();
	return TRUE;
}

bool MapBases::fFreeZoneGrive()
{
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vGrive = FALSE;
		vMap[vvi].vRemotenessGrive = 0;
	}
	return TRUE;
}

bool MapBases::fZoneGrive(unsigned int vInt3)
{
	if (vInt3 < 0 || vInt3 >= vSizeMap) return FALSE;
	bool vFlag;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemotenessNext;
	int vNSmallCage;
	unsigned short vX1,vY1,vX2,vY2;
	vX1 = fConvertIToX(vInt3)/2;
	vY1 = fConvertIToY(vInt3)/2;
	vX2 = vX1 + 1;
	vY2 = vY1 + 1;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vWalk == TRUE &&
			vMap[vvi].vInt3 >= vInt3 &&
			vMap[vvi].vX >= vX1 &&
			vMap[vvi].vX <= vX2 &&
			vMap[vvi].vY >= vY1 &&
			vMap[vvi].vY <= vY2)
		{
			vNSmallCage = vvi;
			break;
		}
	}
	if (!(vNSmallCage >= 0 &&
		vNSmallCage < MAP_MAX_SMALL_CAGE))
	{
		return FALSE;
	}
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vTemp = FALSE;
		vMap[vvi].vRemotenessTemp = 0;
	}
	vMap[vNSmallCage].vTemp = TRUE;
	vMap[vNSmallCage].vRemotenessTemp = 1;
	vFlag = FALSE;
	vRemoteness = 0;
	while (vFlag == FALSE)
	{
		vRemoteness++;
		if (vRemoteness >= 20) break;
		vRemotenessNext = vRemoteness + 1;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vTemp == TRUE &&
				vMap[vvi].vRemotenessTemp == vRemoteness)
			{
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+MAP_W].vTemp == FALSE &&
								vMap[vvi+MAP_W].vRemotenessTemp == 0)
							{
								vMap[vvi+MAP_W].vTemp = TRUE;// Низ
								vMap[vvi+MAP_W].vRemotenessTemp = vRemotenessNext;
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-1].vTemp == FALSE &&
								vMap[vvi-1].vRemotenessTemp == 0)
							{
								vMap[vvi-1].vTemp = TRUE;// Лево
								vMap[vvi-1].vRemotenessTemp = vRemotenessNext;
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi-MAP_W].vTemp == FALSE &&
								vMap[vvi-MAP_W].vRemotenessTemp == 0)
							{
								vMap[vvi-MAP_W].vTemp = TRUE;// Верх
								vMap[vvi-MAP_W].vRemotenessTemp = vRemotenessNext;
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vFlag == TRUE) vFlag = FALSE;
							if (vMap[vvi+1].vTemp == FALSE &&
								vMap[vvi+1].vRemotenessTemp == 0)
							{
								vMap[vvi+1].vTemp = TRUE;// Право
								vMap[vvi+1].vRemotenessTemp = vRemotenessNext;
							}
						}
						break;
					}
				}
			}
		}
	}
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vTemp == TRUE)
		{
			vMap[vvi].vGrive = TRUE;
			vMap[vvi].vRemotenessGrive = vMap[vvi].vRemotenessTemp;
		}
	}
	return TRUE;
}

unsigned short MapBases::fRemotenessBase(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return 0;
	if (vBase->vNBase < 0 || vBase->vNBase >= MAP_MAX_BASE) return 0;
	if (vBase->vMainWall.fGetWall() == NULL) return 0;
	bool vFlag;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemotenessNext;
	int vNSmallCage;
	vNSmallCage = vBase->vMainWall.fGetWall()->vNSmallCage;
	if (!(vNSmallCage >= 0 &&
		vNSmallCage < MAP_MAX_SMALL_CAGE))
	{
		return FALSE;
	}
	vFlag = FALSE;
	vRemotenessNext = 1;
	vMap[vNSmallCage].vRemotenessBase = vRemotenessNext;
	vRemoteness = vMap[vNSmallCage].vRemoteness;
	while (vFlag == FALSE)
	{
		vRemotenessNext++;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if ((vMap[vvi].vNBase == vBase->vNBase) &&
				((vMap[vvi].vRemotenessBase == (vRemotenessNext - 1))))
			{
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vMap[vvi+MAP_W].vRemotenessBase != 0 &&
								vMap[vvi+MAP_W].vBuild == TRUE &&
								(vMap[vvi+MAP_W].vRemotenessBase) < (vMap[vvi].vRemotenessBase - 1))
							{
								return MAP_MAX_SMALL_CAGE;
							}
							if (vMap[vvi+MAP_W].vRemotenessBase == 0 &&
								vMap[vvi+MAP_W].vBuild == TRUE &&
								((((vMap[vvi+MAP_W].vRemoteness) > (vRemoteness - 0)) &&
								vRemotenessNext < 10 &&
								vMap[vvi].vRemotenessBase != 0) ||
								vRemotenessNext >= 10))
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi+MAP_W].vRemotenessBase = vRemotenessNext;// Низ
								vMap[vvi+MAP_W].vNBase = vBase->vNBase;
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vMap[vvi-1].vRemotenessBase != 0 &&
								vMap[vvi-1].vBuild == TRUE &&
								(vMap[vvi-1].vRemotenessBase) < (vMap[vvi].vRemotenessBase - 1))
							{
								return MAP_MAX_SMALL_CAGE;
							}
							if (vMap[vvi-1].vRemotenessBase == 0 &&
								vMap[vvi-1].vBuild == TRUE &&
								((((vMap[vvi-1].vRemoteness) > (vRemoteness - 0)) &&
								vRemotenessNext < 10 &&
								vMap[vvi].vRemotenessBase != 0) ||
								vRemotenessNext >= 10))
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi-1].vRemotenessBase = vRemotenessNext;// Лево
								vMap[vvi-1].vNBase = vBase->vNBase;
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vMap[vvi-MAP_W].vRemotenessBase != 0 &&
								vMap[vvi-MAP_W].vBuild == TRUE &&
								(vMap[vvi-MAP_W].vRemotenessBase) < (vMap[vvi].vRemotenessBase - 1))
							{
								return MAP_MAX_SMALL_CAGE;
							}
							if (vMap[vvi-MAP_W].vRemotenessBase == 0 &&
								vMap[vvi-MAP_W].vBuild == TRUE &&
								((((vMap[vvi-MAP_W].vRemoteness) > (vRemoteness - 0)) &&
								vRemotenessNext < 10 &&
								vMap[vvi].vRemotenessBase != 0) ||
								vRemotenessNext >= 10))
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi-MAP_W].vRemotenessBase = vRemotenessNext;// Верх
								vMap[vvi-MAP_W].vNBase = vBase->vNBase;
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vMap[vvi+1].vRemotenessBase != 0 &&
								vMap[vvi+1].vBuild == TRUE &&
								(vMap[vvi+1].vRemotenessBase) < (vMap[vvi].vRemotenessBase - 1))
							{
								return MAP_MAX_SMALL_CAGE;
							}
							if (vMap[vvi+1].vRemotenessBase == 0 &&
								vMap[vvi+1].vBuild == TRUE &&
								((((vMap[vvi+1].vRemoteness) > (vRemoteness - 0)) &&
								vRemotenessNext < 10 &&
								vMap[vvi].vRemotenessBase != 0) ||
								vRemotenessNext >= 10))
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi+1].vRemotenessBase = vRemotenessNext;// Право
								vMap[vvi+1].vNBase = vBase->vNBase;
							}
						}
						break;
					}
				}
			}
		}
	}
	return vRemotenessNext;
}

bool MapBases::fClearRemotenessBase(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase < 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	int vvi;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vNBase == vBase->vNBase)
		{
			vMap[vvi].vRemotenessBase = 0;
		}
	}
	return TRUE;
}

/*bool MapBases::fZoneTower(sBase *vBase)
{
	if (fNull2((unsigned int)vBase)) return FALSE;
	if (vBase->vNBase < 0 || vBase->vNBase >= MAP_MAX_BASE) return FALSE;
	int vvi,vvj;
	// Зона пушек
	int vNSmallCage;
	int vX;
	int vY;
	int vQ;
	double vStepRad;
	int vR;
	vMap[vBase->vMainWall->vNSmallCage].vTower = TRUE;
	for (vR = 1; vR <= 17; vR++)
	{
		vStepRad = (vPI/2) / (vR);
		vQ = (2 * vPI) / vStepRad;

		for (vvi = 0; vvi < vQ; vvi++)
		{
			vX = (int)(cos(vvi*vStepRad) * ((double)vR));
			vY = (int)(sin(vvi*vStepRad) * ((double)vR));
			if ((int)vBase->vMainWall->vX + vX < 0 ||
				(int)vBase->vMainWall->vX + vX >= MAP_W ||
				(int)vBase->vMainWall->vY + vY < 0 ||
				(int)vBase->vMainWall->vY + vY >= MAP_H)
			{
				continue;
			}
			vNSmallCage = (int)vBase->vMainWall->vNSmallCage + vY * MAP_W + vX;
			if (vNSmallCage >= 0 &&
				vNSmallCage < MAP_MAX_SMALL_CAGE &&
				vMap[vNSmallCage].vNBase == vBase->vNBase &&
				vMap[vNSmallCage].vBuild == TRUE)
			{
				vMap[vNSmallCage].vTower = TRUE;
			}
		}
	}
	//fWriteFileTGA();
	return TRUE;
}*/

void MapBases::fWriteFileTGA()
{
	FILE *pFile;
	char Direct[1000];
	wchar_t Path3[1000];
	char Path[1000];
	char FullPath[1000];
	int vvi, vvj;

	getcwd (Direct, sizeof(Direct));
	sprintf(Path,"%s\\%s",Direct,"MapFileTGA");
	MultiByteToWideChar(CP_ACP, 0, Path, - 1, Path3, sizeof(Path));
	CreateDirectory(Path3,NULL);

	sprintf(FullPath,"%s\\%s",Path,"MapTGA.tga");
	pFile = fopen(FullPath,"wb");// открываем файл
	if (pFile==NULL)
		return;
	BYTE ByteCapTGA[18] = {0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x20,0x08};
	COLORREF vColor;
	BYTE bMassColor[4];

	fwrite(&ByteCapTGA,18,1,pFile);

	for (vvj=MAP_H-1; vvj>=0; vvj--)
	{
		for (vvi=0; vvi<MAP_W; vvi++)
		{
			if (vMap[vvj*MAP_W+vvi].vBuild == TRUE) {bMassColor[2] = 43; bMassColor[1] = 253; bMassColor[0] = 53;}//
			else {bMassColor[2] = 0; bMassColor[1] = 0; bMassColor[0] = 0;}//
			if (vMap[vvj*MAP_W+vvi].vBuild == TRUE && vMap[vvj*MAP_W+vvi].vRemotenessBase > 0) {bMassColor[2] = 105; bMassColor[1] = 3; bMassColor[0] = 11;}//
			if (vMap[vvj*MAP_W+vvi].vBuild == TRUE && vMap[vvj*MAP_W+vvi].vNBase > 0 && vMap[vvj*MAP_W+vvi].vNBase <= JASS_NumBase_MAX) {bMassColor[2] = 105; bMassColor[1] = (vMap[vvj*MAP_W+vvi].vNBase*2 <= 255 ? vMap[vvj*MAP_W+vvi].vNBase*2 : 3); bMassColor[0] = 11;}//
			bMassColor[3] = 0xFF;
			fwrite(&bMassColor,4,1,pFile);
		}
	}
	fclose(pFile);
}

bool MapBases::fDefinitionOfADangerousCell(sSmallCage * vSmallCage, unsigned short vR)
{
	if (vSmallCage == NULL) return FALSE;
	int vNSmallCage;
	int vX;
	int vY;
	int vvi;
	int vQ;
	double vStepRad;

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

	for (vvi = 0; vvi < vQ; vvi++)
	{
		vX = cos(vvi*vStepRad) * (vR);
		vY = sin(vvi*vStepRad) * (vR);
		if ((int)vSmallCage->vX + vX < 0 ||
			(int)vSmallCage->vX + vX >= MAP_W ||
			(int)vSmallCage->vY + vY < 0 ||
			(int)vSmallCage->vY + vY >= MAP_H)
		{
			continue;
		}
		vNSmallCage = (int)vSmallCage->vNSmallCage + vY * MAP_W + vX;
		if (vNSmallCage >= 0 &&
			vNSmallCage < MAP_MAX_SMALL_CAGE &&
			vMap[vNSmallCage].vNBase != vSmallCage->vNBase &&
			vMap[vNSmallCage].vBuild == TRUE)
		{
			return TRUE;
		}
	}
	return FALSE;
}

unsigned short MapBases::fRemotenessInt3ToInt3(unsigned int vInt3_1, unsigned int vInt3_2)
{
	if (vInt3_1 < 0 || vInt3_1 >= vSizeMap) return MAP_MAX_SMALL_CAGE - 1;
	if (vInt3_2 < 0 || vInt3_2 >= vSizeMap) return MAP_MAX_SMALL_CAGE - 1;
	bool vFlag;
	int vvi,vvj;
	unsigned short vRemotenessNext;
	int vNSmallCage1;
	int vNSmallCage2;
	unsigned short vX1,vY1,vX2,vY2;
	vNSmallCage1 = -1;
	vNSmallCage2 = -1;
	vX1 = fConvertIToX(vInt3_1)/2;
	vY1 = fConvertIToY(vInt3_1)/2;
	vX2 = vX1+1;
	vY2 = vY1+1;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vWalk == TRUE &&
			vMap[vvi].vInt3 >= vInt3_1 &&
			vMap[vvi].vX >= vX1 &&
			vMap[vvi].vX <= vX2 &&
			vMap[vvi].vY >= vY1 &&
			vMap[vvi].vY <= vY2)
		{
			vNSmallCage1 = vvi;
			break;
		}
	}
	if (!(vNSmallCage1 >= 0 &&
		vNSmallCage1 < MAP_MAX_SMALL_CAGE))
	{
		return MAP_MAX_SMALL_CAGE - 1;
	}
	vX1 = fConvertIToX(vInt3_2)/2;
	vY1 = fConvertIToY(vInt3_2)/2;
	vX2 = vX1+1;
	vY2 = vY1+1;
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		if (vMap[vvi].vWalk == TRUE &&
			vMap[vvi].vInt3 >= vInt3_2 &&
			vMap[vvi].vX >= vX1 &&
			vMap[vvi].vX <= vX2 &&
			vMap[vvi].vY >= vY1 &&
			vMap[vvi].vY <= vY2)
		{
			vNSmallCage2 = vvi;
			break;
		}
	}
	if (!(vNSmallCage2 >= 0 &&
		vNSmallCage2 < MAP_MAX_SMALL_CAGE))
	{
		return MAP_MAX_SMALL_CAGE - 1;
	}
	for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
	{
		vMap[vvi].vRemotenessTemp = 0;
		vMap[vvi].vRemotenessTemp2 = 0;
		//vMap[vvi].vTemp = FALSE;
	}
	vFlag = FALSE;
	vRemotenessNext = 1;
	vMap[vNSmallCage1].vRemotenessTemp = vRemotenessNext;
	vMap[vNSmallCage2].vRemotenessTemp = vRemotenessNext;
	vMap[vNSmallCage1].vRemotenessTemp2 = 1;
	vMap[vNSmallCage2].vRemotenessTemp2 = 2;
	while (vFlag == FALSE)
	{
		vRemotenessNext++;
		if (vRemotenessNext > 35) break;
		vFlag = TRUE;
		for (vvi = 0; vvi < MAP_MAX_SMALL_CAGE; vvi++)
		{
			if (vMap[vvi].vWalk == TRUE &&
				(vMap[vvi].vRemotenessTemp == (vRemotenessNext - 1)))
			{
				for (vvj = 0; vvj < 4; vvj++)
				{
					switch (vvj)
					{
					case 0:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-MAP_W) && vMap[vvi+MAP_W].vY < MAP_H)
						{
							if (vMap[vvi+MAP_W].vWalk == TRUE &&
								vMap[vvi+MAP_W].vRemotenessTemp2 != 0 &&
								vMap[vvi].vRemotenessTemp2 != 0 &&
								vMap[vvi+MAP_W].vRemotenessTemp2 != vMap[vvi].vRemotenessTemp2)
							{
								return vRemotenessNext * 2;
							}
							if (vMap[vvi+MAP_W].vRemotenessTemp == 0 &&
								vMap[vvi+MAP_W].vWalk == TRUE)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi+MAP_W].vRemotenessTemp = vRemotenessNext;// Низ
								vMap[vvi+MAP_W].vRemotenessTemp2 = vMap[vvi].vRemotenessTemp2;
							}
						}
						break;
					case 1:
						if (vvi>=1 && vMap[vvi-1].vX >= 0)
						{
							if (vMap[vvi-1].vWalk == TRUE &&
								vMap[vvi-1].vRemotenessTemp2 != 0 &&
								vMap[vvi].vRemotenessTemp2 != 0 &&
								vMap[vvi-1].vRemotenessTemp2 != vMap[vvi].vRemotenessTemp2)
							{
								return vRemotenessNext * 2;
							}
							if (vMap[vvi-1].vRemotenessTemp == 0 &&
								vMap[vvi-1].vWalk == TRUE)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi-1].vRemotenessTemp = vRemotenessNext;// Лево
								vMap[vvi-1].vRemotenessTemp2 = vMap[vvi].vRemotenessTemp2;
							}
						}
						break;
					case 2:
						if (vvi>=MAP_W && vMap[vvi-MAP_W].vY >= 0)
						{
							if (vMap[vvi-MAP_W].vWalk == TRUE &&
								vMap[vvi-MAP_W].vRemotenessTemp2 != 0 &&
								vMap[vvi].vRemotenessTemp2 != 0 &&
								vMap[vvi-MAP_W].vRemotenessTemp2 != vMap[vvi].vRemotenessTemp2)
							{
								return vRemotenessNext * 2;
							}
							if (vMap[vvi-MAP_W].vRemotenessTemp == 0 &&
								vMap[vvi-MAP_W].vWalk == TRUE)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi-MAP_W].vRemotenessTemp = vRemotenessNext;// Верх
								vMap[vvi-MAP_W].vRemotenessTemp2 = vMap[vvi].vRemotenessTemp2;
							}
						}
						break;
					case 3:
						if (vvi<=(MAP_MAX_SMALL_CAGE-1-1) && vMap[vvi+1].vX < MAP_W)
						{
							if (vMap[vvi+1].vWalk == TRUE &&
								vMap[vvi+1].vRemotenessTemp2 != 0 &&
								vMap[vvi].vRemotenessTemp2 != 0 &&
								vMap[vvi+1].vRemotenessTemp2 != vMap[vvi].vRemotenessTemp2)
							{
								return vRemotenessNext * 2;
							}
							if (vMap[vvi+1].vRemotenessTemp == 0 &&
								vMap[vvi+1].vWalk == TRUE)
							{
								if (vFlag == TRUE) vFlag = FALSE;
								vMap[vvi+1].vRemotenessTemp = vRemotenessNext;// Право
								vMap[vvi+1].vRemotenessTemp2 = vMap[vvi].vRemotenessTemp2;
							}
						}
						break;
					}
				}
			}
		}
	}
	return MAP_MAX_SMALL_CAGE - 1;
}