#pragma once
#include "Jass.h"
#include "Natives.h"
#include "myJASSConstants.h"

extern struct HumanPlayer;
extern struct sRect2;
extern struct DataUnit;

struct sDoom
{
	bool vBool;
	unsigned short vNBaseRight;
	unsigned short vNBaseBottom;
	unsigned short vNBaseLeft;
	unsigned short vNBaseTop;
};

struct sSides
{
	bool vLeft;
	bool vTop;
	bool vRight;
	bool vBottom;
};

struct sSmallCage
{
	unsigned short vNSmallCage;
	sSides vTree;
	unsigned short vQTree;
	sSmallCage * vHead;
	unsigned int vInt3;
	sSides vCliff;
	unsigned short vQCliff;
	unsigned short vIdSmallCage;
	unsigned short vInt;
	unsigned short vX;
	unsigned short vY;
	bool vBuild;
	unsigned short vNBase;
	bool vLightning;
	bool vRicochet;
	bool vRepair;
	unsigned short vQObstacles;
	unsigned short vXBuild;
	unsigned short vYBuild;
	unsigned short vRemoteness;
	int vvi,vvj;
	unsigned short vNSmallCageTemp;
	bool vTower;
	unsigned short vRemotenessTower;
	bool vBusy;
	sDoom vDoom;
	unsigned short vIdDataUnit;
	sDoom vDoom2;
	bool vHeal;
};

struct sSmallCageRepair
{
	unsigned int vInt3;
	bool vBind;
	bool vRepair;
};

struct sBase
{
	static const int MAP_REPAIR_POS = 6;

	unsigned short vNBase;
	bool vBind;
	bool vTypeBazeOneCell;
	sSmallCageRepair vRepairPos[MAP_REPAIR_POS];
	unsigned short vQRepairPos;
	unsigned short vBeginningOfBase;
	sSmallCage *vMainWall;
	sSmallCage *vBuild;
	bool vSlayerBaze;
	HumanPlayer *pHumanPlayer;
	unsigned short vQCells;
	unsigned short vNBaseNoDear;
	bool vFreeAllRepairPos;
	char vColor[10];
	unsigned short vQ;
	unsigned short vvi;
	unsigned short vQFirstTower;
	unsigned short vQHeal;
	bool vTwoGold;
	bool vTempleAndLumberHouse;
	unsigned short vQGoldMine;
	unsigned short vHouse;
	bool vHouseForBlockVampire;
	unsigned short vNChainForHouseForBlockVampire;
	bool vBuildHeal;
	bool vFullWorkers;
	bool vDetoWorkers;

	bool fInit()
	{
		int vvi;
		vNBase = 0;
		vBind = FALSE;
		vTypeBazeOneCell = FALSE;
		for (vvi = 0; vvi < MAP_REPAIR_POS; vvi++)
		{
			vRepairPos[vvi].vInt3 = vSizeMap;
			vRepairPos[vvi].vRepair = FALSE;
			vRepairPos[vvi].vBind = FALSE;
		}
		vBeginningOfBase = 0;
		vQRepairPos = 0;
		vSlayerBaze = FALSE;
		pHumanPlayer = NULL;
		vQCells = 0;
		vNBaseNoDear = DO_NOT_FIND_BASE;
		vFreeAllRepairPos = FALSE;
		vQFirstTower = 0;
		vQHeal = 0;
		vTwoGold = FALSE;
		vTempleAndLumberHouse = FALSE;
		vQGoldMine = 0;
		vHouse = 0;
		vHouseForBlockVampire = FALSE;
		vNChainForHouseForBlockVampire = 0;
		vBuildHeal = FALSE;
		vFullWorkers = FALSE;
		vDetoWorkers = FALSE;
		return TRUE;
	}

	unsigned short fQPosRepairFree()
	{
		vQ = 0;
		for (vvi = 0; vvi < MAP_REPAIR_POS; vvi++)
		{
			if (vRepairPos[vvi].vRepair == TRUE)
			{
				vQ++;
			}
		}
		return vQRepairPos - vQ;
	}

	unsigned short fQPosRepair()
	{
		vQ = 0;
		for (vvi = 0; vvi < MAP_REPAIR_POS; vvi++)
		{
			if (vRepairPos[vvi].vRepair == TRUE)
			{
				vQ++;
			}
		}
		return vQ;
	}

	unsigned short fQPosRepairBindFree()
	{
		vQ = 0;
		for (vvi = 0; vvi < MAP_REPAIR_POS; vvi++)
		{
			if (vRepairPos[vvi].vBind == TRUE)
			{
				vQ++;
			}
		}
		return vQRepairPos - vQ;
	}

	unsigned short fQPosRepairBind()
	{
		vQ = 0;
		for (vvi = 0; vvi < MAP_REPAIR_POS; vvi++)
		{
			if (vRepairPos[vvi].vBind == TRUE)
			{
				vQ++;
			}
		}
		return vQ;
	}
	/*
	bool fBindBase(unsigned short vNBase);
	bool fFreeBase(unsigned short vNBase);

	bool MapBases::fFreeBase(unsigned short vNBase)
{
	if (vNBase >= JASS_NumBase_MIN &&
		vNBase <= JASS_NumBase_MAX)
	{
		if (vBindBase[vNBase-JASS_NumBase_MIN] == TRUE)
		{
			vBindBase[vNBase-JASS_NumBase_MIN] = FALSE;
			return TRUE;
		}
	}
	return FALSE;
}

bool MapBases::fBindBase(unsigned short vNBase)
{
	if (vNBase >= JASS_NumBase_MIN &&
		vNBase <= JASS_NumBase_MAX)
	{
		if (vBindBase[vNBase-JASS_NumBase_MIN] == FALSE)
		{
			vBindBase[vNBase-JASS_NumBase_MIN] = TRUE;
			return TRUE;
		}
	}
	return FALSE;
}*/

};

class MapBases
{
private:;
	bool fDefinitionOfADangerousCell(sSmallCage * vSmallCage, unsigned short vR);
	void fWriteFileTGA();
	bool fDistanceFromTheCenterOfTheMap();
	int vvz;
	int vSetOff;
	int vNSmallCage3;
	unsigned short vNSmallCage;
	unsigned short vIdDataUnit;
	sSmallCage *vForest;
	int vvi,vvj;
	unsigned short vRemoteness;
	unsigned short vRemoteness2;
	unsigned short vXBuild;
	unsigned short vYBuild;
	bool vFlagLightning;
	unsigned short vN;
	DataUnit *pDataUnit;
	bool vFlagBuild;
	unsigned short vIdSmallCage;
	unsigned short vQ;
	sBase *vBase;
	unsigned int vInt3;
public:
	static const int MAP_W = vW3/2;
	static const int MAP_H = vH3/2;
	static const int MAP_MAX_SMALL_CAGE = MAP_W * MAP_H;
	static const int MAP_MAX_BASE = JASS_NumBase_MAX;
	static const int MAP_MAX_REPAIR = 6;
	// Переменные
	sSmallCage vMap[MAP_MAX_SMALL_CAGE];
	//bool vBindBase[MAP_MAX_BASE];
	//bool vBindSlayerPlace;
	//bool vBindDancePool;
	//sSmallCageRepair vRepairPos[MAP_MAX_BASE][6];
	//unsigned short vBeginningOfBase[MAP_MAX_BASE];

	// Функции
	bool fInit ();

	bool fFreeBuild(sSmallCage *vSmallCage);
	sSmallCage * fFillBuild(unsigned int vInt3, unsigned short vXBuild, unsigned short vYBuild, unsigned short vIdSmallCage);
	unsigned int fFindHouse(sBase *vBase);
	bool fDetermineTheBeginningOfTheBase(sBase *vBase);
	bool fCheckFillBuild(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild, unsigned short vIdSmallCage, bool vBoolDoom);
	bool fDetermineTheBeginningOfTheBaseOneCell(sBase *vBase);
	HRECT fFindRectForHouse(real vX, real vY, unsigned short vIdDataUnit);
	unsigned fFindPosBuild(sBase *vBase, integer vId, unsigned short vR1, unsigned short vR2, bool vFlag, bool vFlagTower, bool vBoolDoom);
	bool fZoneTower(sBase *vBase);
	unsigned int fFindPosRepair(sBase *vBase);
	bool fFreePosRepair(sBase *vBase, unsigned int vInt3);
	bool fFreeZoneTower(unsigned int vInt3);
	unsigned int fFindPosSafeZone(sBase *vBase);
	void fFreeBusy(sBase *vBase);
	void fFillBusy(sBase *vBase);
	bool fCheckFillBuildTower(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild);
	unsigned short fQCellsToBase(sBase *vBase);
	unsigned int fQPosRepair(sBase *vBase);
	bool fFreeAllPosRepair(sBase *vBase);
	unsigned int fFindPosBuild(sBase *vBase, integer vId);
	bool fDoom(unsigned short vNSmallCage2, int vvj, int vInt2);
	unsigned short fQSmallCageForTower(sBase *vBase);
	bool fFillBuildHeal(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild);
	bool fFillDoom2(sSmallCage *vSmallCage, unsigned short vXBuild, unsigned short vYBuild, int vvj2);
	bool fPosRepairTrue(sBase *vBase, unsigned int vInt3);
	bool fFreePosRepairBind(sBase *vBase, unsigned int vInt3);
	bool fCheckPosRepair(sBase *vBase, unsigned int vInt3);
};