// MapCreatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MapCreator.h"
#include "MapCreatorDlg.h"

#include "GeoPanel.h"
#include "TexturePanel.h"
#include "WaterPanel.h"
#include "CulturalPanel.h"


#include <Stuff\Stuff.hpp>
#include <GameOS\ToolOS.hpp>
#include <ImageLib\Image.h>

#ifndef USEEXTERNALLY
#include "MapCreatorDlg.h"
#endif 

using namespace Stuff;
//#define USEEXTERNALLY

#ifdef USEEXTERNALLY
#else
#define BUILDFILENAME "Content\\MechWarrior4.Build"
#endif


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapCreatorDlg dialog

CMapCreatorDlg::CMapCreatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMapCreatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMapCreatorDlg)
	m_MapName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_MapName="MyMap";
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMapCreatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMapCreatorDlg)
	DDX_Control(pDX, IDOK, m_MapNameCon);
	DDX_Control(pDX, IDC_DLGTAB, m_Tab);
	DDX_Text(pDX, IDC_MAPNAME, m_MapName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMapCreatorDlg, CDialog)
//{{AFX_MSG_MAP(CMapCreatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATE, OnCreate)
ON_NOTIFY(TCN_SELCHANGE, IDC_DLGTAB, OnSelchangeDlgtab)
ON_EN_CHANGE(IDC_MAPNAME, OnChangeMapname)
ON_EN_KILLFOCUS(IDC_MAPNAME, OnKillfocusMapname)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapCreatorDlg message handlers

BOOL CMapCreatorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
									/*	
									HKEY pathkey;
									if(ERROR_SUCCESS !=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Microsoft Games\\MechWarrior Vengeance",0,KEY_QUERY_VALUE,&pathkey))
									{
									MessageBox("Mech Warrior 4 is not istalled correctly on this computer",MB_OK);
									PostQuitMessage(0);
									}
									
									  DWORD ktype;
									  DWORD size=	MAX_PATH;
									  char dbuf[MAX_PATH];
									  if(ERROR_SUCCESS !=RegQueryValueEx(pathkey,"EXE Path",NULL,&ktype,(BYTE *)dbuf,&size) || size<=0 || ktype!=REG_SZ) 
									  {
									  MessageBox("Mech Warrior 4 is not istalled correctly on this computer",MB_OK);
									  PostQuitMessage(0);
									  }
	*/	
	GeoPan=new CGeoPanel(&m_Tab);
	TextPan=new CTexturePanel(&m_Tab);
	WatPan=new CWaterPanel(&m_Tab);

	m_Tab.InsertItem(TCIF_TEXT|TCIF_PARAM,0,"Geometry",0,(DWORD)GeoPan);
	m_Tab.InsertItem(TCIF_TEXT|TCIF_PARAM,1,"Texture",0,(DWORD)TextPan);
	m_Tab.InsertItem(TCIF_TEXT|TCIF_PARAM,2,"Water",0,(DWORD)WatPan);
#ifndef USEEXTERNALLY
	CultPan=new CCulturalPanel(&m_Tab);
	m_Tab.InsertItem(TCIF_TEXT|TCIF_PARAM,3,"Culturals",0,(DWORD)CultPan);
#endif
	//	m_gamePath=dbuf;
	
	GeoPan->ShowWindow(SW_SHOW);
	m_Tab.SetCurSel(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMapCreatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMapCreatorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMapCreatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMapCreatorDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CMapCreatorDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CMapCreatorDlg::CreateINI()
{
	CString ini_filename,map_path;
	map_path="Content\\Maps\\"+m_MapName;
	ini_filename=map_path+"\\tctd.ini";
	NotationFile config_file(ini_filename);
	Page *page=config_file.SetPage(m_MapName);
	Check_Object(page);
	
	GeoPan->WriteEntries(page);
	TextPan->WriteEntries(page);
	WatPan->WriteEntries(page,GeoPan->GetLowHFVal());
#ifndef USEEXTERNALLY	
//	gos_CreateDirectory("Content\\Maps\\"+m_MapName+"\\Culturals");
	CultPan->WriteEntries(page,"Content\\Maps\\"+m_MapName+"\\Culturals");
#endif	
	
	
	config_file.Save();
	
	
}

void CMapCreatorDlg::CreateDataFile()
{
	
	CString data_filename,map_path;
	map_path="Content\\Maps\\"+m_MapName;
	data_filename=map_path+"\\"+m_MapName+".data";
	NotationFile data_file(data_filename);
	Page *page=data_file.SetPage("gamedata");
	CString text_base="Maps\\"+m_MapName+"\\";
	
	page->SetEntry("grid",m_MapName+".erf");
	page->SetEntry("cull","nevercull");
	page->SetEntry("class","mechwarrior4::mwmap");
	
	CString str;
	int pos;
	
	
	if(TextPan->m_IGData)
	{
		str=TextPan->m_TTEdit;
		str=str.Left(str.ReverseFind('.'));
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		page->SetEntry("texturedata",str+".fgd");
		page->SetEntry("tcffilename",str+".index.tcf");
	}
	
	if(WatPan->m_UseWater)
	{
		page->SetEntry("waterlevel",WatPan->m_WaterLevel);
		
		str=WatPan->m_WText;
		str=str.Left(str.ReverseFind('.'));
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		page->SetEntry("watertexture","maps\\"+m_MapName+"\\"+str);
		
		str=WatPan->m_UWText;
		str=str.Left(str.ReverseFind('.'));
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		page->SetEntry("underwatertexture","maps\\"+m_MapName+"\\"+str);
		
		
		str=WatPan->m_DTText;
		str=str.Left(str.ReverseFind('.'));
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		page->SetEntry("waterdetailtexture","maps\\"+m_MapName+"\\"+str);
		
	}
	
	//	page->SetEntry(tcffilename=alpine01.index.tcf
	page->SetEntry("HudMap","maps\\"+m_MapName+"\\"+m_MapName);
	
	Check_Object(page);
	data_file.Save();	
}

void CMapCreatorDlg::CreateHintFile()
{
	//if(TextPan->m_IGData) return;
	CString hint_filename,map_path,text_base;
	map_path="Content\\Textures\\Maps\\"+m_MapName;
	hint_filename=map_path+"\\"+"textures.hint";
	text_base="Maps\\"+m_MapName+"\\";
	NotationFile hint_file(hint_filename);
	
	hint_file.DeleteAllPages();
	CString str;
	int pos;
	
	
	CString ImgName, BaseName,DirName;
	BaseName=m_MapName;
	
	
	int ZoneCountX = GeoPan->ZoneCountX;
	int ZoneCountY = GeoPan->ZoneCountY;
	
	DirName="Maps\\"+m_MapName;
	
	int zrow, zcol;
	for(zrow=0;zrow<ZoneCountY;zrow++)
	{
		for(zcol=0;zcol<ZoneCountX;zcol++)
		{
			CString subDirName;
			subDirName.Format("%s\\%c%c", DirName, 'A'+ZoneCountY-zrow-1, 'A'+ZoneCountX-zcol-1);
			{
				int i, j;
				
				for(j=0;j<2;j++)
				{
					for(i=0;i<2;i++)
					{
						
						ImgName.Format("%s\\%c%c\\%s_%c%c_1_%02X%02X",
							DirName,
							'A'+ZoneCountY-zrow-1,
							'A'+ZoneCountX-zcol-1,
							BaseName,
							'A'+ZoneCountY-zrow-1,
							'A'+ZoneCountX-zcol-1,
							i,
							j
							);
						hint_file.AddPage(ImgName);
					}
				}
			}
			
			ImgName.Format("%s\\%c%c\\%s_%c%c_0_0000",
				DirName,
				'A'+ZoneCountY-zrow-1,
				'A'+ZoneCountX-zcol-1,
				BaseName,
				'A'+ZoneCountY-zrow-1,
				'A'+ZoneCountX-zcol-1
				);
			hint_file.AddPage(ImgName);
		}
	}
	
	
	ImgName.Format("%s\\%s",
		DirName,
		BaseName
		);
	hint_file.AddPage(ImgName);
	
	/*
	str=TextPan->m_TTEdit;
	str=str.Left(str.ReverseFind('.'));
	pos=str.ReverseFind('\\');
	if(pos>0) str=str.Mid(pos+1);
	hint_file.AddPage(text_base+str);
	*/
	str=TextPan->m_DetailTexture;
	str=str.Left(str.ReverseFind('.'));
	pos=str.ReverseFind('\\');
	if(pos>0) str=str.Mid(pos+1);
	hint_file.AddPage(text_base+str);
	
	
	if(WatPan->m_UseWater)
	{
		str=WatPan->m_WText;
		str=str.Left(str.ReverseFind('.'));
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		hint_file.AddPage(text_base+str);
		
		str=WatPan->m_UWText;
		str=str.Left(str.ReverseFind('.'));
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		hint_file.AddPage(text_base+str);
		
		str=WatPan->m_DTText;
		str=str.Left(str.ReverseFind('.'));
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		hint_file.AddPage(text_base+str);
		
	}
	
	
	hint_file.Save();	
	
}

void CMapCreatorDlg::CreateInstance()
{
	
	
	CString data_filename,map_path;
	map_path="Content\\Maps\\"+m_MapName;
	data_filename=map_path+"\\"+m_MapName+".instance";
	NotationFile data_file(data_filename);
	Page *page=data_file.SetPage(m_MapName);
	
	page->SetEntry("Model",m_MapName+".data");
	page->SetEntry("ExecutionState","AlwaysExecuteState");
	
	Check_Object(page);
	data_file.Save();	
	
}

void CMapCreatorDlg::SplitTextures()
{
	//	CString ImgName,DirName="Content\\Textures\\Maps\\"+m_MapName;
	
	
	if(TextPan->m_IGData) return;
	
	CString ImgName, BaseName,DirName;
	BaseName=m_MapName;
	
	
	int ZoneCountX = GeoPan->ZoneCountX;
	int ZoneCountY = GeoPan->ZoneCountY;
	
	DirName="Content\\Textures\\Maps\\"+m_MapName;
	
	if(!gos_DoesFileExist(DirName))
		gos_CreateDirectory(DirName);
	
	Image imgseg,totimg,savimg;
	
	imgseg.CreateBlank(256,256, ITYPE_RGB, 32);
	imgseg.MaskTo(RGBMask(0xff0000,0x00ff00,0x0000ff));
	
	totimg.Load((char *)(LPCSTR)(TextPan->m_TTEdit));
	totimg.MaskTo(RGBMask(0xff0000,0x00ff00,0x0000ff));
	
	
	int zrow, zcol;
	
	for(zrow=0;zrow<ZoneCountY;zrow++)
	{
		for(zcol=0;zcol<ZoneCountX;zcol++)
		{
			CString subDirName;
			
			subDirName.Format("%s\\%c%c", DirName, 'A'+ZoneCountY-zrow-1, 'A'+ZoneCountX-zcol-1);
			
			if(!gos_DoesFileExist(subDirName))
			{
				gos_CreateDirectory(subDirName);
			}
			
			
			
			{
				int iw = totimg.GetWidth()/(2*ZoneCountX);
				int ih = totimg.GetHeight()/(2*ZoneCountY);
				int i, j;
				
				for(j=0;j<2;j++)
				{
					for(i=0;i<2;i++)
					{
						imgseg.Blt(
							totimg, 
							CRect(
							0,
							0,
							imgseg.GetWidth(),
							imgseg.GetHeight()
							),
							CRect(
							zcol*512+i*iw,
							zrow*512+j*ih,
							zcol*512+(i+1)*iw,
							zrow*512+(j+1)*ih
							)
							);		
						
						ImgName.Format("%s\\%c%c\\%s_%c%c_1_%02X%02X.tga",
							DirName,
							'A'+ZoneCountY-zrow-1,
							'A'+ZoneCountX-zcol-1,
							BaseName,
							'A'+ZoneCountY-zrow-1,
							'A'+ZoneCountX-zcol-1,
							i,
							j
							);
						
						savimg=imgseg;
						savimg.MakeRGB24();
						savimg.SaveTga((char *)(LPCSTR)ImgName);
					}
				}
			}
			
			imgseg.Blt(
				totimg, 
				CRect(
				0,
				0,
				imgseg.GetWidth(),
				imgseg.GetHeight()
				),
				CRect(
				zcol*512,
				zrow*512,
				zcol*512 + (totimg.GetWidth()/ZoneCountX),
				zrow*512 + (totimg.GetHeight()/ZoneCountY)
				)
				);		
			
			ImgName.Format("%s\\%c%c\\%s_%c%c_0_0000.Tga",
				DirName,
				'A'+ZoneCountY-zrow-1,
				'A'+ZoneCountX-zcol-1,
				BaseName,
				'A'+ZoneCountY-zrow-1,
				'A'+ZoneCountX-zcol-1
				);
			
			savimg=imgseg;
			savimg.MakeRGB24();
			savimg.SaveTga((char *)(LPCSTR)ImgName);
		}
	}
	
	
	imgseg.Blt(
		totimg, 
		CRect(
		0,
		0,
		imgseg.GetWidth(),
		imgseg.GetHeight()
		),
		CRect(
		0,
		0,
		totimg.GetWidth(),
		totimg.GetHeight()
		)
		);		
	
	ImgName.Format("%s\\%s.Tga",
		DirName,
		BaseName
		);
	
	savimg=imgseg;
	savimg.MakeRGB24();
	savimg.SaveTga((char *)(LPCSTR)ImgName);
	
	
}

void CMapCreatorDlg::CopyFiles()
{
	CString destpath;
	destpath="Content\\Maps\\"+m_MapName;
	
	NotationFile bfile(destpath+"\\"+m_MapName+".build");
	
	bfile.DeleteAllPages();
	Page *page=bfile.AddPage("Resource\\Maps\\"+m_MapName+".mw4");
	page->SetEntry("instance","maps\\"+m_MapName+"\\"+m_MapName+".instance");
	page->SetEntry("TexturePool","textures\\maps\\"+m_MapName+"\\textures.hint");
	
	
	
	
	CString str,texture_dest;
	int pos;
	texture_dest="Content\\textures\\maps\\"+m_MapName+"\\";
	/*
	str=TextPan->m_TTEdit;
	pos=str.ReverseFind('\\');
	if(pos>0) str=str.Mid(pos+1);
	CopyFile(TextPan->m_TTEdit,texture_dest+str,FALSE);
	*/
	str=TextPan->m_DetailTexture;
	pos=str.ReverseFind('\\');
	if(pos>0) str=str.Mid(pos+1);
	CopyFile(TextPan->m_DetailTexture,texture_dest+str,FALSE);
	TextPan->m_ActualTexturePath="maps\\"+m_MapName+"\\"+str.Left(str.ReverseFind('.'));
	
	if(WatPan->m_UseWater)
	{
		str=WatPan->m_WText;
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		CopyFile(WatPan->m_WText,texture_dest+str,FALSE);
		WatPan->m_BasePath="maps\\"+m_MapName+"\\";
		
		str=WatPan->m_UWText;
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		CopyFile(WatPan->m_UWText,texture_dest+str,FALSE);
		
		str=WatPan->m_DTText;
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		CopyFile(WatPan->m_DTText,texture_dest+str,FALSE);
	}
	
	
	
	
	if(!gos_DoesFileExist(destpath))
	{
		//	gos_CrateDirectory(destpath);
	}
	
	bfile.Save();
}

void CMapCreatorDlg::UpdateBuildFile()
{
	CString map_buildpath;
	CString map_shortpath;
	CString map_longpath;
	CString mission_instancepath;
	CString mission_buildpath;
	map_shortpath.Format("Maps\\%s\\%s.build",m_MapName,m_MapName);	
	map_longpath="Content\\"+map_shortpath;
	
	
	
	////////////////////////////////////////////////////////////
	//	Enter the data in the build file
	
#ifdef USEEXTERNALLY	
	
#else	
	
	NotationFile build_file(BUILDFILENAME);
	Page *build_page = build_file.FindPage("");
	Check_Object(build_page);
	Note *texture_note = build_page->FindNote("textures.build");
	Check_Object(texture_note);
	
	NotationFile texture_file;
	texture_note->GetEntry(&texture_file);
	Page *texture_page = texture_file.FindPage("");
	Check_Object(texture_page);
	Note *core_note = texture_page->FindNote("core.build");
	Check_Object(core_note);
	
	NotationFile core_file;
	core_note->GetEntry(&core_file);
	Page *core_page = core_file.FindPage("");
	Check_Object(core_page);
	Note *prop_note = core_page->FindNote("props.build");
	Check_Object(prop_note);
	
	NotationFile prop_file;
	prop_note->GetEntry(&prop_file);
	Page *prop_page = prop_file.FindPage("");
	Check_Object(prop_page);
	Note *map_note = prop_page->FindNote(map_shortpath);
	
	NotationFile maps_file;
	if(!map_note)
	{
		map_note=prop_page->AddNote(map_shortpath);
		Check_Object(map_note);
	}
	else
	{
		Check_Object(map_note);
		map_note->GetEntry(&maps_file);
	}
	
	map_note->SetEntry(&maps_file);
	
#endif
	
	
#ifdef USEEXTERNALLY	
	//	prop_file.Save();
#else
	prop_note->SetEntry(&prop_file);
	core_note->SetEntry(&core_file);
	texture_note->SetEntry(&texture_file);
	build_file.Save();
	
#endif
}

void CMapCreatorDlg::OnCreate() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_MapName.Replace(' ','_');
	m_MapName.Replace('/','_');  
	m_MapName.Replace('\\','_');
	m_MapName.Replace('?','_'); 
	m_MapName.Replace('*','_'); 
	m_MapName.Replace('|','_'); 
	m_MapName.Replace('<','_'); 
	m_MapName.Replace('>','_'); 
	
	UpdateData(FALSE);
	
	if(m_MapName=="") 
	{
		MessageBox("Give the map a valid name","No Name",MB_OK|MB_ICONERROR);
		return;
	}
	
	
	DestPath="Content\\Maps\\"+m_MapName;
	if(gos_DoesFileExist(DestPath))
	{
		if(MessageBox("A map by this name exists, OverWrite?","OverWrite Warning",MB_YESNO|MB_ICONQUESTION)==IDNO)
			return;
	}
	
	
	if(!GeoPan->ValidateEntries()) {m_Tab.SetCurSel(0); UpdateTabWin();  return; }
	if(!TextPan->ValidateEntries(GeoPan->ZoneCountX,GeoPan->ZoneCountY)) {m_Tab.SetCurSel(1); UpdateTabWin(); return; }
	if(!WatPan->ValidateEntries()) {m_Tab.SetCurSel(2); UpdateTabWin();  return; }
	
	gos_CreateDirectory("Content");
	gos_CreateDirectory("Content\\Maps\\");
	gos_CreateDirectory("Content\\Textures\\");
	gos_CreateDirectory("Content\\Textures\\Maps\\");
	gos_CreateDirectory("Content\\Maps\\"+m_MapName);
	gos_CreateDirectory("Content\\Maps\\"+m_MapName+"\\Culturals");
	gos_CreateDirectory("Content\\Textures\\Maps\\"+m_MapName);
	
	
	
	SplitTextures();
	CopyFiles();
	CreateINI();
	CreateDataFile();
	CreateInstance();
	CreateHintFile();
	UpdateBuildFile();
	if(RunTCT())
	{
		DeleteFile("Resource\\Maps\\"+m_MapName+".mw4");
		DeleteFile("Resource\\Maps\\"+m_MapName+".dep");
		MessageBox("Map created successfully. Run Mission Editor to create a mission with this map","Operation Complete",MB_OK);
	}
	else
		MessageBox("There was an Error in Createing your map","Operation Error",MB_OK);
}


bool CMapCreatorDlg::RunTCT()
{
	CString cmdopts;
	cmdopts="";
	if(GeoPan->m_FinalPass) cmdopts+=" /p";
	if(GeoPan->m_InterestData) cmdopts+=" /uid";
	switch(GeoPan->m_TriCap)
	{
	case 0: break;
	case 1: cmdopts+=" /s"; break;
	case 2: cmdopts+=" /y"; break;
	}
	
	cmdopts+=" /i";
	
	if(gos_DoesFileExist("Tctd.exe"))
	{
		int err;
		err=system("Tctd /f Content\\Maps\\"+m_MapName+"\\tctd.ini"+cmdopts);
		ASSERT(err!=-1);
		err=system("Tctd /f Content\\Maps\\"+m_MapName+"\\tctd.ini -bsp"+cmdopts);
		ASSERT(err!=-1);
	}
	else
	{
		MessageBox("Cannot Find Tctd.exe,Make sure this file exists and try again",MB_OK);
		return false;
	}
	
	return true;
}

void CMapCreatorDlg::OnSelchangeDlgtab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	UpdateTabWin();
	*pResult = 0;
}

void CMapCreatorDlg::UpdateTabWin()
{
	int i,sel;
	TCITEM item;
	ZeroMemory(&item,sizeof(item));
	sel=m_Tab.GetCurSel();
	
	for(i=0;i<m_Tab.GetItemCount();i++)
	{
		item.mask=TCIF_PARAM;
		BOOL res=m_Tab.GetItem(i,&item);
		ASSERT(res);
		CWnd *wnd=(CWnd *)item.lParam;
		ASSERT(wnd!=NULL);
		wnd->ShowWindow(sel==i?SW_SHOW:SW_HIDE);
		
	}
	
}

void CMapCreatorDlg::OnChangeMapname() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CMapCreatorDlg::OnKillfocusMapname() 
{
	UpdateData(TRUE);
	m_MapName.Replace(' ','_');
	UpdateData(FALSE);
	
}
