// _map.cpp: главный файл проекта.

#include "stdafx.h"

#include <stdio.h>
#include <direct.h>

#include "module1.h"

using namespace System;
using namespace System::Runtime::InteropServices;

void fGenerate (int _kW, int _kH);
void fGenerateN (char *pDirect, char *pFileName, int _W, int _H, int _kW, int _kH, int _ClickN);
void fToSaveArchiveOfMap(char *pFullPath, int _W, int _H, int _Num);
void fToLoadArchivesOfMaps(char *pDirect);
int fCheckInteger2 (System::String ^_Str);
int fReadDataOfBase (char *_pStr, Base1 &_Base);
int fLoadFile1 (char *pFullPath, int &_Wm, int &_Hm, int &_kW, int &_kH);

int Generate;

int main(array<System::String ^> ^args)
{
	int vW;
	int vH;
	int vkW;
	int vkH;
	int vWm;
	int vHm;
	int vRects;
	char FullPath[1000];
	char Direct[1000];
	char FileName[1000];
	char FileName2[1000];
	char StartupPath[1000];
	int vN;
	String ^vCmdLine;
	String ^vStr;
	int vIndex;
	int vPreviousIndex;
	int vStrLen;
	int vFlag;
	int vCount;
	int vFlag2;
	int vFlagN;
	int vLen;

	getcwd (StartupPath, sizeof(StartupPath));
	
	// формат ввода
	// _map VampirismFire5.10b2.w3x _map_config.txt -n 10
	vCmdLine = Environment::CommandLine;
	//vCmdLine = "_map VampirismFire5.10b2.w3x _map_config.txt -n 10";

	vN = 1;

	vStrLen = vCmdLine->Length;
	
	vFlagN = 0;
	vFlag2 = 0;
	vCount = 0;
	vFlag = 0;
	vIndex = 0;
	vPreviousIndex = 0;
	for (int vvi=0;vvi<vStrLen;vvi++)
	{
		if (vCmdLine[vvi] != ' ')
			vFlag = 0;
		if ((vCmdLine[vvi] == ' ' || vvi == vStrLen-1) && vFlag == 0)
		{
			vFlag = 1;
			vIndex = vvi;
			if (vvi == vStrLen-1)
				vIndex += 1;
			vStr = vCmdLine->Substring(vPreviousIndex,vIndex-vPreviousIndex);
			if (vCount == 0 && vStr != "_map")
			{
				vFlag2 = 1;
				break;
			}
			if ((vCount == 1 || vCount == 2) && vStr->Length > 0)
			{
				if (vStr[0] == '-')
				{
					vFlag2 = 1;
					break;
				}
			}
			if (vCount == 1)
			{
				sprintf(FileName,"%s",(char*)(void*)Marshal::StringToHGlobalAnsi(vStr));
			}
			if (vCount == 2)
			{
				sprintf(FileName2,"%s",(char*)(void*)Marshal::StringToHGlobalAnsi(vStr));
			}
			if (vCount > 2)
			{
				if (vFlagN == 0)
				{
					if (vStr == "-n")
						vFlagN = 1;
				}
				else
				{
					if (vFlagN == 1)
					{
						if (fCheckInteger2(vStr))
							vN = Convert::ToInt32(vStr);
						if (vN < 1)
						{
							vFlag2 = 1;
							break;
						}
					}
					vFlagN = 0;
				}
			}
			vPreviousIndex = vIndex+1;
			vCount++;
		}
	}
	if (vFlag2 == 1)
		return 0;

	sprintf(Direct,"%s\\%s",StartupPath,"Maps");

	fToLoadArchivesOfMaps(Direct);
	
	vWm = 0;
	vHm = 0;
	vkW = 0;
	vkH = 0;

	sprintf(FullPath,"%s\\%s",StartupPath,FileName2);

	fLoadFile1(FullPath,vWm,vHm,vkW,vkH);
	vW = vWm*vkW;
	vH = vHm*vkW;

	fMapInitialization(vWm,vHm);	// инициализируем карту
	vRects = 1;
	fWorldMapInitialization(vW,vH,vRects,4);
	fOnInitialUpdate();

	//_splitpath(FullPath,NULL,Direct,FileName,NULL);
	
	// редактируем название архива карты
	vLen = strlen(FileName);

	if (vLen > 4)
	{
		if (memcmp(&FileName[vLen-4],".w3x",4) == 0 ||
			memcmp(&FileName[vLen-4],".w3m",4) == 0)
			FileName[vLen-4] = 0;
	}

	if (vW > 0 && vH > 0 && vkW >= 4 && vkH >= 4)
	{
		Console::WriteLine(L"Подождите идет генерация карт.");
		fGenerateN(StartupPath,FileName,vW,vH,vkW,vkH,vN);
	}

	Console::WriteLine(L"Здравствуй, мир!");

	vStr = Console::ReadLine();

    return 0;
}

void fGenerate (int _kW, int _kH)
{
	//Generator of map
	Generate++;
	fCreateMap();
	fCreateWorldMap(_kW,_kH);
}

void fGenerateN (char *pDirect, char *pFileName, int _W, int _H, int _kW, int _kH, int _ClickN)
{
	char FullPath[1000];
	char FileName[1000];
	String ^vStr;

	if (!pDirect || !pFileName)
		return;
	
	if (_ClickN > 0)
	{
		Generate = 0;
		for (int vvi=0;vvi<_ClickN;vvi++)
		{
			fGenerate(_kW,_kH);
			sprintf(FileName,"%s%s%d%s%s",pFileName,"[",vvi,"]",".w3x");
			sprintf(FullPath,"%s\\%s",pDirect,FileName);
			fToSaveArchiveOfMap(FullPath,_W,_H,0);
			fCharToSysString(FileName,vStr);
			Console::WriteLine(vStr);
		}
	}
}
// Сохранить архив карты
void fToSaveArchiveOfMap(char *pFullPath, int _W, int _H, int _Num)
{
	RECT AllocationArea;

	if (!pFullPath)
		return;

	AllocationArea.left = 0;
	AllocationArea.top = 0;
	AllocationArea.right = _W;
	AllocationArea.bottom = _H;
	fSaveMapEditor(_Num,AllocationArea);

	fSaveFileArchive(pFullPath);
}
// Загрузить архивы карт
void fToLoadArchivesOfMaps(char *pDirect)
{
	char FullPath[1000];

	if (!pDirect)
		return;

	sprintf(FullPath,"%s%s",pDirect,"\\SlayerPlace.w3x");
	fLoadFileArchive(FullPath,1);
	sprintf(FullPath,"%s%s",pDirect,"\\DancePool.w3x");
	fLoadFileArchive(FullPath,2);
	sprintf(FullPath,"%s%s",pDirect,"\\BaseVampire.w3x");
	fLoadFileArchive(FullPath,3);
	sprintf(FullPath,"%s%s",pDirect,"\\VampirismFire5.10b2.w3x");
	fLoadFileArchive(FullPath,0);
}

// Читаем данные базы
int fReadDataOfBase (char *_pStr, Base1 &_Base)
{
	 //
	 char szBuffer[100];
	 Base1 vBase;
	 int vCount;
	 int vMaxCount;
	 char Sep;
	 int vLen;
	 char *chr;
	 char *pStr;

	 vMaxCount = 18;
	 Sep = '.';

	 pStr = _pStr;
	 chr=new char[]=_pStr;
	 vCount = 0;
	 for(int vvi=0;chr[vvi]!='\0';vvi++)
	 {
		 if (chr[vvi] == 0x20)
		 {
			 vLen = &chr[vvi]-pStr;
			 if(vLen > 0)
			 {
				 memcpy(szBuffer,pStr,vLen);
				 szBuffer[vLen] = NULL;
				 for (int vvj=0;vvj<vLen;vvj++)
				 {
					 if(szBuffer[vvj] == ',')
						 szBuffer[vvj] = '.';
				 }
				 pStr = szBuffer;
				 switch (vCount)
				 {
					case 0:
						vBase.Id = -1;
						if(fCheckInteger(pStr) == 1)
							vBase.Id = atoi(pStr);
						break;
					case 1:
						if(strcmp(pStr,"True") == 0) vBase.Open = true;
						if(strcmp(pStr,"False") == 0) vBase.Open = false;
						break;
					case 2:
						vBase.Type = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.Type = atoi(pStr);
						break;
					case 3:
						vBase.Location.left = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.Location.left = atoi(pStr);
						break;
					case 4:
						vBase.Location.top = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.Location.top = atoi(pStr);
						break;
					case 5:
						vBase.Location.right = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.Location.right = atoi(pStr);
						break;
					case 6:
						vBase.Location.bottom = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.Location.bottom = atoi(pStr);
						break;
					case 7:
						vBase.Size = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.Size = atoi(pStr);
						break;
					case 8:
						vBase.Allowance = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.Allowance = atoi(pStr);
						break;
					case 9:
						vBase.PosDistr = 0;
						if(fCheckInteger(pStr) == 1)
							vBase.PosDistr = atoi(pStr);
						break;
					case 10:
						vBase._c2 = 0;
						if(fCheckInteger(pStr) == 1)
							vBase._c2 = atoi(pStr);
						break;
					case 11:
						vBase._v2 = 0;
						if(fCheckInteger(pStr) == 1)
							vBase._v2 = atoi(pStr);
						break;
					case 12:
						vBase.Tree = 0;
						if(fCheckDouble(pStr,Sep) == 1)
							vBase.Tree = atof(pStr);
						break;
					case 13:
						vBase.FelledTrees = 0;
						if(fCheckDouble(pStr,Sep) == 1)
							vBase.FelledTrees = atof(pStr);
						break;
					case 14:
						vBase.LvlСhange = 0;
						if(fCheckDouble(pStr,Sep) == 1)
							vBase.LvlСhange = atof(pStr);
						break;
					case 15:
						vBase.IncLvl = 0;
						if(fCheckDouble(pStr,Sep) == 1)
							vBase.IncLvl = atof(pStr);
						break;
					case 16:
						vBase.WidePass = 0;
						if(fCheckDouble(pStr,Sep) == 1)
							vBase.WidePass = atof(pStr);
						break;
					case 17:
						vBase.Blood = 0;
						if(fCheckDouble(pStr,Sep) == 1)
							vBase.Blood = atof(pStr);
						break;
				 }
				 vCount++;
			 }
			 pStr = &chr[vvi]+1;
		 }
	 }

	 if (vCount != vMaxCount)
		 return 0;

	 _Base = vBase;

	 return 1;
 }

 // Загрузить конфигурационный файл
int fLoadFile1 (char *pFullPath, int &_Wm, int &_Hm, int &_kW, int &_kH)
{
	 //
	 int vFlag;
	 int vFlag2;
	 BYTE *pMassByte;
	 int vSizeArray;
	 int _N;
	 int vWm;
	 int vHm;
	 int vkW;
	 int vkH;

	 char *pStr;
	 int vLen;
	 char *pch1;
	 char *pch2;

	 int vNum;
	 int vNum2;

	 Base1 vBase;
	 int *pListId;

	 vFlag = 0;
	 pMassByte = NULL;
	 
	 if (!fOpenFile(pFullPath,pMassByte,vSizeArray))
		 return 0;

	 fResetCarriageReturn(pMassByte,vSizeArray);

	 _N = 0;

	 fGetListIdOfBases(pListId,_N);
	 for (int vvi=0;vvi<_N;vvi++)
	 {
		 fDelBase(pListId[vvi]);
	 }

	 vNum = 0;
	 vNum2 = 0;
	 vFlag = 0;
	 vFlag2 = 0;

	 pch1 = (char*)pMassByte;
	 pch2 = (char*)pMassByte + vSizeArray;
	 do
	 {
		 pStr = pch1;
		 vLen = strlen(pStr);
		 pch1 += vLen;
		 if (pch1 >= pch2) break;
		 vNum++;
		 //
		 if (strcmp(pStr,"[SETTING_MAP]") == 0 && vFlag2 == 0)
		 {
			 vFlag2 = 1;
			 vNum2 = vNum;
		 }
		 if (strcmp(pStr,"[DATA]") == 0 && vFlag2 == 1)
		 {
			 vFlag2 = 2;
			 vNum2 = vNum;
		 }
		 if (vFlag2 == 1 && vNum > vNum2 && vLen != 0)
		 {
			 vFlag = 1;
			 if (memcmp(pStr,"Wm = ",5) == 0 &&
				 vLen > 5)
			 {
				 pStr += 5;
				 if(fCheckInteger(pStr) == 1)
				 {
					 vWm = atoi(pStr);
				 }
				 else
				 {
					 vFlag = 2;
					 break;
				 }
			 }
			 if (memcmp(pStr,"Hm = ",5) == 0 &&
				 vLen > 5)
			 {
				 pStr += 5;
				 if(fCheckInteger(pStr) == 1)
				 {
					 vHm = atoi(pStr);
				 }
				 else
				 {
					 vFlag = 2;
					 break;
				 }
			 }
			 if (memcmp(pStr,"kW = ",5) == 0 &&
				 vLen > 5)
			 {
				 pStr += 5;
				 if(fCheckInteger(pStr) == 1)
				 {
					 vkW = atoi(pStr);
				 }
				 else
				 {
					 vFlag = 2;
					 break;
				 }
			 }
			 if (memcmp(pStr,"kH = ",5) == 0 &&
				 vLen > 5)
			 {
				 pStr += 5;
				 if(fCheckInteger(pStr) == 1)
				 {
					 vkH = atoi(pStr);
				 }
				 else
				 {
					 vFlag = 2;
					 break;
				 }
			 }
		 }
		 //
		 if (vFlag2 == 2 && vNum > vNum2 && *pStr != ';' && vLen != 0)
		 {
			 if(fReadDataOfBase(pStr,vBase) == 1)
				 fReplaceBase(vBase);
		 }
		 //
		 while(*pch1 == NULL && pch1 < pch2)
			 pch1++;
		 //
	}
	while(pch1 < pch2);

	if (vFlag2 != 2 || vFlag == 0)
		return 0;

	//if (vWm > 0 && vHm > 0 && vkW >= 4 && vkH >= 4)
	//{
	_Wm = vWm;
	_Hm = vHm;
	_kW = vkW;
	_kH = vkH;
	//}
	return 1;
}