#include "stdafx.h"
#include "Jass.h"
#include "WorkMap.h"
#include "Global_variables_extern.h"
#include "Basics_functions.h"
#include "war3map.h"

extern sGlobal *pGlobal;

integer fInitMapFromAnalizeMap()
{
	if (vHT_To_Analize_Work_Map)
	{
		FlushParentHashtable(vHT_To_Analize_Work_Map);
	}
	if (vHT_To_Analize_Work_Map == NULL)
	{
		vHT_To_Analize_Work_Map = InitHashtable();
		IncrementHandleReferences(vHT_To_Analize_Work_Map);
	}
	if (vWorldRect)
	{
		RemoveRect(vWorldRect);
		vWorldRect = NULL;
	}
	if (vWorldRect == NULL)
	{
		vWorldRect = GetWorldBounds();
	}
	vW = (unsigned short)((GetRectMaxX(vWorldRect) - GetRectMinX(vWorldRect)) / 128);
	vH = (unsigned short)((GetRectMaxY(vWorldRect) - GetRectMinY(vWorldRect)) / 128);
	vH2 = 4;
	vW2 = 4;
	vH3 = vH*vH2;
	vW3 = vW*vW2;
	vSizeMap = vH3 * vW3;
	if (pMapGlobal)
	{
		delete pMapGlobal;
		pMapGlobal = NULL;
	}
	if (pMapGlobal == NULL)
	{
		pMapGlobal = new char[vSizeMap];
	}
	if (fSetPathNoWater() == FALSE) goto m2;
	if (fSetPathLvlCliff() == FALSE) goto m2;
	if (fSetDestructableObjectsInWorkMap() == FALSE) goto m2;
	if (fSetUnitsInWorkMap() == FALSE) goto m2;
	if (vHT_To_Analize_Work_Map)
	{
		FlushParentHashtable(vHT_To_Analize_Work_Map);
	}
	if (vWorldRect)
	{
		RemoveRect(vWorldRect);
		vWorldRect = NULL;
	}
	//fWriteFileTGA();
	return (int)pMapGlobal;
m2:;
	if (pMapGlobal)
	{
		delete pMapGlobal;
		pMapGlobal = NULL;
	}
	if (vWorldRect)
	{
		RemoveRect(vWorldRect);
		vWorldRect = NULL;
	}
	return 0;
}

bool fSetPathLvlCliff()
{
	integer vLevelCliff;
	integer vTerrainType;
	Boolean vIsTerrainPathable;
	Boolean vIsPointBlighted;
	Boolean vIsWater;
	real vX, vY;
	int vvi;
	real vX1_DancePool;
	real vX2_DancePool;
	real vY1_DancePool;
	real vY2_DancePool;
	real vX1_SlayerPlace;
	real vX2_SlayerPlace;
	real vY1_SlayerPlace;
	real vY2_SlayerPlace;
	real vX1_Teleport_1_1;
	real vX2_Teleport_1_1;
	real vY1_Teleport_1_1;
	real vY2_Teleport_1_1;
	real vX1_Teleport_1_2;
	real vX2_Teleport_1_2;
	real vY1_Teleport_1_2;
	real vY2_Teleport_1_2;
	real vX1_Teleport_2_1;
	real vX2_Teleport_2_1;
	real vY1_Teleport_2_1;
	real vY2_Teleport_2_1;
	real vX1_Teleport_2_2;
	real vX2_Teleport_2_2;
	real vY1_Teleport_2_2;
	real vY2_Teleport_2_2;
	real vX1_TeleportDancePool_1_1;
	real vX2_TeleportDancePool_1_1;
	real vY1_TeleportDancePool_1_1;
	real vY2_TeleportDancePool_1_1;
	real vX1_TeleportDancePool_1_2;
	real vX2_TeleportDancePool_1_2;
	real vY1_TeleportDancePool_1_2;
	real vY2_TeleportDancePool_1_2;
	real vX1_TeleportDancePool_2_1;
	real vX2_TeleportDancePool_2_1;
	real vY1_TeleportDancePool_2_1;
	real vY2_TeleportDancePool_2_1;
	real vX1_TeleportDancePool_2_2;
	real vX2_TeleportDancePool_2_2;
	real vY1_TeleportDancePool_2_2;
	real vY2_TeleportDancePool_2_2;
	if (pGlobal)
	{
		vX1_DancePool = GetRectMinX(pGlobal->oo);
		vX2_DancePool = GetRectMaxX(pGlobal->oo);
		vY1_DancePool = GetRectMinY(pGlobal->oo);
		vY2_DancePool = GetRectMaxY(pGlobal->oo);
		vX1_SlayerPlace = GetRectMinX(pGlobal->ro);
		vX2_SlayerPlace = GetRectMaxX(pGlobal->ro);
		vY1_SlayerPlace = GetRectMinY(pGlobal->ro);
		vY2_SlayerPlace = GetRectMaxY(pGlobal->ro);
		vX1_Teleport_1_1 = GetRectMinX(pGlobal->Eo);
		vX2_Teleport_1_1 = GetRectMaxX(pGlobal->Eo);
		vY1_Teleport_1_1 = GetRectMinY(pGlobal->Eo);
		vY2_Teleport_1_1 = GetRectMaxY(pGlobal->Eo);
		vX1_Teleport_1_2 = GetRectMinX(pGlobal->Vo);
		vX2_Teleport_1_2 = GetRectMaxX(pGlobal->Vo);
		vY1_Teleport_1_2 = GetRectMinY(pGlobal->Vo);
		vY2_Teleport_1_2 = GetRectMaxY(pGlobal->Vo);
		vX1_Teleport_2_1 = GetRectMinX(pGlobal->Xo);
		vX2_Teleport_2_1 = GetRectMaxX(pGlobal->Xo);
		vY1_Teleport_2_1 = GetRectMinY(pGlobal->Xo);
		vY2_Teleport_2_1 = GetRectMaxY(pGlobal->Xo);
		vX1_Teleport_2_2 = GetRectMinX(pGlobal->Oo);
		vX2_Teleport_2_2 = GetRectMaxX(pGlobal->Oo);
		vY1_Teleport_2_2 = GetRectMinY(pGlobal->Oo);
		vY2_Teleport_2_2 = GetRectMaxY(pGlobal->Oo);
		vX1_TeleportDancePool_1_1 = GetRectMinX(pGlobal->bo);
		vX2_TeleportDancePool_1_1 = GetRectMaxX(pGlobal->bo);
		vY1_TeleportDancePool_1_1 = GetRectMinY(pGlobal->bo);
		vY2_TeleportDancePool_1_1 = GetRectMaxY(pGlobal->bo);
		vX1_TeleportDancePool_1_2 = GetRectMinX(pGlobal->Bo);
		vX2_TeleportDancePool_1_2 = GetRectMaxX(pGlobal->Bo);
		vY1_TeleportDancePool_1_2 = GetRectMinY(pGlobal->Bo);
		vY2_TeleportDancePool_1_2 = GetRectMaxY(pGlobal->Bo);
		vX1_TeleportDancePool_2_1 = GetRectMinX(pGlobal->co);
		vX2_TeleportDancePool_2_1 = GetRectMaxX(pGlobal->co);
		vY1_TeleportDancePool_2_1 = GetRectMinY(pGlobal->co);
		vY2_TeleportDancePool_2_1 = GetRectMaxY(pGlobal->co);
		vX1_TeleportDancePool_2_2 = GetRectMinX(pGlobal->Co);
		vX2_TeleportDancePool_2_2 = GetRectMaxX(pGlobal->Co);
		vY1_TeleportDancePool_2_2 = GetRectMinY(pGlobal->Co);
		vY2_TeleportDancePool_2_2 = GetRectMaxY(pGlobal->Co);
	}
	for (vvi = 0; vvi < vSizeMap; vvi++)
	{
		vX = fConvertIToRX(vvi)+(real)(DOOD_MAP_SUBDIV/4);
		vY = fConvertIToRY(vvi)-(real)(DOOD_MAP_SUBDIV/4);
		vLevelCliff = GetTerrainCliffLevel(vX,vY);
		vTerrainType = GetTerrainType(vX,vY);
		vIsTerrainPathable = IsTerrainPathable(vX,vY,PATHING_TYPE_WALKABILITY);
		vIsPointBlighted = IsPointBlighted(vX,vY);
		vIsWater = !IsTerrainPathable(vX,vY,PATHING_TYPE_FLOATABILITY);
		if (vIsWater == TRUE)
		{
			pMapGlobal[vvi] = (char)JASS_WATER_LEVEL_1;
		}
		if (vX >= vX1_DancePool &&
			vX <= vX2_DancePool &&
			vY >= vY1_DancePool &&
			vY <= vY2_DancePool)
		{
			pMapGlobal[vvi] = (char)JASS_DANCEPOOL;
		}
		if (vX >= vX1_SlayerPlace &&
			vX <= vX2_SlayerPlace &&
			vY >= vY1_SlayerPlace &&
			vY <= vY2_SlayerPlace)
		{
			pMapGlobal[vvi] = (char)JASS_SLAYERPLACE;
		}
		if (vIsTerrainPathable == TRUE)
		{
			pMapGlobal[vvi] = (char)JASS_PATH_NOWALK;
		}
		if (vIsPointBlighted == TRUE)
		{
			pMapGlobal[vvi] = (char)JASS_PATH_BLIGHT;
		}
		else if (vTerrainType == ID_TERRAIN_TYPE_BLIGHT2)
		{
			pMapGlobal[vvi] = (char)JASS_PATH_BLIGHT;
		}
		if (vX >= vX1_Teleport_1_1 &&
			vX <= vX2_Teleport_1_1 &&
			vY >= vY1_Teleport_1_1 &&
			vY <= vY2_Teleport_1_1)
		{
			pMapGlobal[vvi] = (char)JASS_TELEPORT_1_1;
		}
		if (vX >= vX1_Teleport_1_2 &&
			vX <= vX2_Teleport_1_2 &&
			vY >= vY1_Teleport_1_2 &&
			vY <= vY2_Teleport_1_2)
		{
			pMapGlobal[vvi] = (char)JASS_TELEPORT_1_2;
		}
		if (vX >= vX1_Teleport_2_1 &&
			vX <= vX2_Teleport_2_1 &&
			vY >= vY1_Teleport_2_1 &&
			vY <= vY2_Teleport_2_1)
		{
			pMapGlobal[vvi] = (char)JASS_TELEPORT_2_1;
		}
		if (vX >= vX1_Teleport_2_2 &&
			vX <= vX2_Teleport_2_2 &&
			vY >= vY1_Teleport_2_2 &&
			vY <= vY2_Teleport_2_2)
		{
			pMapGlobal[vvi] = (char)JASS_TELEPORT_2_2;
		}
		if (vX >= vX1_TeleportDancePool_1_1 &&
			vX <= vX2_TeleportDancePool_1_1 &&
			vY >= vY1_TeleportDancePool_1_1 &&
			vY <= vY2_TeleportDancePool_1_1)
		{
			pMapGlobal[vvi] = (char)JASS_DANCEPOOLTELEPORT;
		}
		if (vX >= vX1_TeleportDancePool_1_2 &&
			vX <= vX2_TeleportDancePool_1_2 &&
			vY >= vY1_TeleportDancePool_1_2 &&
			vY <= vY2_TeleportDancePool_1_2)
		{
			pMapGlobal[vvi] = (char)JASS_DANCEPOOLTELEPORT;
		}
		if (vX >= vX1_TeleportDancePool_2_1 &&
			vX <= vX2_TeleportDancePool_2_1 &&
			vY >= vY1_TeleportDancePool_2_1 &&
			vY <= vY2_TeleportDancePool_2_1)
		{
			pMapGlobal[vvi] = (char)JASS_DANCEPOOLTELEPORT;
		}
		if (vX >= vX1_TeleportDancePool_2_2 &&
			vX <= vX2_TeleportDancePool_2_2 &&
			vY >= vY1_TeleportDancePool_2_2 &&
			vY <= vY2_TeleportDancePool_2_2)
		{
			pMapGlobal[vvi] = (char)JASS_DANCEPOOLTELEPORT;
		}
	}
	return TRUE;
}

bool fSetPathNoWater()
{
	for (int vvi = 0; vvi < vSizeMap; vvi++)
	{
		pMapGlobal[vvi] = (char)JASS_PATH_NOWATER;
	}
	return TRUE;
}

bool fSetDestructableObjectsInWorkMap()
{
	integer vParentKey;
	HTIMER vTimer;
	vTimer = GetExpiredTimer();
	vParentKey = vTimer;
	if (vParentKey == NULL) return FALSE;
	SaveInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_MAP_GLOBAL,(integer)pMapGlobal);
	EnumDestructablesInRect(vWorldRect,NULL,&fActionAnalizeDestructableObjects);
	return TRUE;
}

void fActionAnalizeDestructableObjects()
{
	integer vParentKey;
	HTIMER vTimer;
	vTimer = GetExpiredTimer();
	vParentKey = vTimer;
	if (vParentKey == NULL)
	{
		return;
	}
	SaveDestructableHandle(vHT_To_Analize_Work_Map,vParentKey,CONST_HDESTRUCTABLE_ENUM_2,GetEnumDestructable());
	SaveInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_ID_DESTRUCTABLE,GetDestructableTypeId(LoadDestructableHandle(vHT_To_Analize_Work_Map,vParentKey,CONST_HDESTRUCTABLE_ENUM_2)));
	SaveReal(vHT_To_Analize_Work_Map,vParentKey,CONST_X_DESTRUCTABLE,GetDestructableX(LoadDestructableHandle(vHT_To_Analize_Work_Map,vParentKey,CONST_HDESTRUCTABLE_ENUM_2)));
	SaveReal(vHT_To_Analize_Work_Map,vParentKey,CONST_Y_DESTRUCTABLE,GetDestructableY(LoadDestructableHandle(vHT_To_Analize_Work_Map,vParentKey,CONST_HDESTRUCTABLE_ENUM_2)));
	switch (LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_ID_DESTRUCTABLE))
	{
	case ID_TREE:
		if (GetDestructableLife(LoadDestructableHandle(vHT_To_Analize_Work_Map,vParentKey,CONST_HDESTRUCTABLE_ENUM_2)) <= 0)
		{
			return;
		}
		SaveInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_CODE_DESTRUCTABLE,(integer)JASS_DOOD_DEFAULT_TREE);
		break;
	default:
		return;
	}
	real vRaduis;
	vRaduis = 0;
	vRaduis = fRadius_Destructable(LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_ID_DESTRUCTABLE));
	if (vRaduis <= 0)
	{
		return;
	}
	SaveInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_W_DESTRUCTABLE,(integer)(vRaduis*2/32));
	SaveInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_H_DESTRUCTABLE,(integer)(vRaduis*2/32));
	unsigned int vIntIn;
	vIntIn = fConverterI(LoadReal(vHT_To_Analize_Work_Map,vParentKey,CONST_X_DESTRUCTABLE)-vRaduis,LoadReal(vHT_To_Analize_Work_Map,vParentKey,CONST_Y_DESTRUCTABLE)+vRaduis);
	//unsigned short vX22,vY22;
	//real vX, vY;
	//unsigned short vW22, vH22;
	//vX = LoadReal(vHT_To_Analize_Work_Map,vParentKey,CONST_X_DESTRUCTABLE);
	//vY = LoadReal(vHT_To_Analize_Work_Map,vParentKey,CONST_Y_DESTRUCTABLE);
	//vX22 = fConvertIToX(vIntIn);
	//vY22 = fConvertIToY(vIntIn);
	//vW22 = (unsigned short)LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_W_DESTRUCTABLE);
	//vH22 = (unsigned short)LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_H_DESTRUCTABLE);
	unsigned short vvi, vvj;
	for (vvj = fConvertIToY(vIntIn); vvj < fConvertIToY(vIntIn) + (unsigned short)LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_H_DESTRUCTABLE); vvj++)
	{
		for (vvi = fConvertIToX(vIntIn); vvi < fConvertIToX(vIntIn) + (unsigned short)LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_W_DESTRUCTABLE); vvi++)
		{
			if (vvj*vW3 + vvi >= 0 && (unsigned int)(vvj*vW3 + vvi) < vSizeMap)
			{
				((char*)LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_MAP_GLOBAL))[vvj*vW3 + vvi] = (char)LoadInteger(vHT_To_Analize_Work_Map,vParentKey,CONST_CODE_DESTRUCTABLE);
			}
			/*else
			{
				vvj = vvj;
			}*/
		}
	}
}

bool fSetUnitsInWorkMap()
{
	return TRUE;
}

void 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];
	unsigned int vInt3;

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

	for (vvj=vH3-1; vvj>=0; vvj-=2)
	{
		for (vvi=0; vvi<vW3; vvi+=2)
		{
			vInt3 = (unsigned int)(vvj*vW3+vvi);
			if (pMapGlobal[vInt3] == (char)JASS_PATH_NOWATER) {bMassColor[2] = 43; bMassColor[1] = 253; bMassColor[0] = 53;}//
			else {bMassColor[2] = 0; bMassColor[1] = 0; bMassColor[0] = 0;}//
			if (pMapGlobal[vInt3] == (char)JASS_DOOD_DEFAULT_TREE) {bMassColor[2] = 55; bMassColor[1] = 132; bMassColor[0] = 89;}//
			if (pMapGlobal[vInt3] == (char)JASS_PATH_BLIGHT) {bMassColor[2] = 91; bMassColor[1] = 95; bMassColor[0] = 82;}//
			if (pMapGlobal[vInt3] == (char)JASS_WATER_LEVEL_1) {bMassColor[2] = 44; bMassColor[1] = 38; bMassColor[0] = 251;}//
			if (pMapGlobal[vInt3] == (char)JASS_PATH_NOWALK) {bMassColor[2] = 105; bMassColor[1] = 3; bMassColor[0] = 11;}//red
			if (pMapGlobal[vInt3] == (char)JASS_DANCEPOOL) {bMassColor[2] = 6; bMassColor[1] = 213; bMassColor[0] = 223;}//
			if (pMapGlobal[vInt3] == (char)JASS_SLAYERPLACE) {bMassColor[2] = 225; bMassColor[1] = 165; bMassColor[0] = 4;}//
			if (pMapGlobal[vInt3] == (char)JASS_TELEPORT_1_1 ||
				pMapGlobal[vInt3] == (char)JASS_TELEPORT_1_2 ||
				pMapGlobal[vInt3] == (char)JASS_TELEPORT_2_1 ||
				pMapGlobal[vInt3] == (char)JASS_TELEPORT_2_2) {bMassColor[2] = 6; bMassColor[1] = 213; bMassColor[0] = 183;}//
			if (pMapGlobal[vInt3] == (char)JASS_DANCEPOOLTELEPORT) {bMassColor[2] = 255; bMassColor[1] = 0; bMassColor[0] = 0;}//red2
			bMassColor[3] = 0xFF;
			fwrite(&bMassColor,4,1,pFile);
		}
	}
	fclose(pFile);
}