//CMap.cpp


#include <winsock2.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <fstream>
#include <cstdlib>

#define PI 3.1415926

#include "CMap.h"
#include "CImageManager.h"
#include "CTextReader.h"
#include "CBlockObject.h"
#include "CObjectManager.h"


		/************************/


void CMap::Clear()
{

}

void CMap::Shutdown()
{	

}

void CMap::Init(STRINGC MapName)
{

		

	//load info of map	
	STRINGC MapInfoFileName = MapName + STRINGC(".map");
	
	Width=CTextReader::getInt("Width",MapInfoFileName);
	Height=CTextReader::getInt("Height",MapInfoFileName);

	Assert(Width>0,"Unable to find MapWidth.")
	Assert(Height>0,"Unable to find MapHeight.")


	//load tiles
	STRINGC MapTileFileName = MapName + STRINGC(".tile");	
	int NbrTiles = CTextReader::getInt("TotalTiles",MapTileFileName);

	STRINGC TileFileNames[100]; // = new STRINGC[NbrTiles];
	for(int i=1; i < NbrTiles+1; i++)
	{
		TileFileNames[i] = CTextReader::getString(STRINGC("Tile") + STRINGC(i),MapTileFileName);				
	}



	std::ifstream from(MapTileFileName);
	//char* tmp = new char[Width*2+5];
	char* tmp = new char[150];
	STRINGC MapLine="";

	while(MapLine!="Map")
	{
		//from.getline(tmp,Width*2+5 ,'\n');
		from.getline(tmp,150 ,'\n');
		MapLine=tmp;
	}

	//from is now ready to start reading the map
	

	for(int y=0; y < Height; y++)
	{				
		from.getline(tmp,150 ,'\n');			
		MapLine=tmp;
		
		for(int x=0; x < Width; x++)
		{			
			//Assert(false, STRINGC(atoi(&tmp[x*2])),MapTileFileName)
			//Assert(false, STRINGC(TileFileNames[atoi(&tmp[x])]))			

//			LOG(STRINGC( STRINGC("Loading Tile: " ) +  STRINGC(x) + STRINGC(",") + STRINGC(y)).GetString());
			map[x][y].Init(TileFileNames[atoi(&tmp[x*3])],x,y);
			map[x][y].setAnchor(Anchor);			
		}

	}


	from.close();

	//delete TileFileNames;
	//delete tmp;

	TileWidth = map[0][0].image->getWidth();
	TileHeight = map[0][0].image->getHeight();

	setAnchor();





	delete[] tmp;

	loadObjects(MapName);

	typ=typMap;
	//delete TileFileNames;

}

void CMap::Init()
{		
	Assert(false, "What are you trying to initilize?");

}





		/***********************/

void CMap::Draw()
{
///	g_Console.AddMessage("Drawing Map");


	for(int x=0; x < Width; x++)
	{
		for(int y=0; y < Height; y++)
		{			
			
			map[x][y].Draw();

		}

	}

	
}


		/************************
			We probably wont scroll much 
			in our game. This function needs 
			to be optimized
		************************/


void CMap::Scroll(POINT p)
{

	for(int x=0; x < Width; x++)
	{
		for(int y=0; y < Height; y++)
		{	

			map[x][y].Scroll(p);			

		}

	}
		

}


void CMap::setAnchor()
{
	Anchor.x = this->getX();
	Anchor.y = this->getY();

	for(int x=0; x < Width; x++)
	{
		for(int y=0; y < Height; y++)
		{								
			map[x][y].setAnchor(Anchor);
		}

	}
}



void CMap::Collision(CGameObject* obj)
{
/*
	switch(obj->typ)
	{

	case typPlayer:

		//Figure out what tile his bounding box is touching.
		CPlayerObject* player = (CPlayerObject*)obj;
		int x = player->getX() - Anchor.x;
		int y = player->getX() - Anchor.y;


		
		break;

	}
*/
}

void CMap::loadObjects(STRINGC MapName)
{
	//load info of map	
	STRINGC MapObjectFileName = MapName + STRINGC(".obj");
		

	//load Objects	
	int NbrObjects = CTextReader::getInt("TotalObjects",MapObjectFileName);

	STRINGC ObjectFileNames[100]; // = new STRINGC[NbrTiles];
	for(int i=1; i < NbrObjects+1; i++)
	{
		ObjectFileNames[i] = CTextReader::getString(STRINGC("Object") + STRINGC(i),MapObjectFileName);				
	}



	std::ifstream from(MapObjectFileName);
	//char* tmp = new char[Width*2+5];
	char* tmp = new char[150];
	STRINGC MapLine="";

	while(MapLine!="Map")
	{
		//from.getline(tmp,Width*2+5 ,'\n');
		from.getline(tmp,150 ,'\n');
		MapLine=tmp;
	}

	//from is now ready to start reading the map	

	for(int y=0; y < Height; y++)
	{				
		from.getline(tmp,150 ,'\n');			
		MapLine=tmp;
		
		for(int x=0; x < Width; x++)
		{	
			//please dont ever ever ever code something like this. this is horrible.			
			int objIndex = atoi(&tmp[x*3]);
			if(objIndex > 0)
			{				
				CBlockObject* block = (CBlockObject*)g_ObjectManager.CreateObject(typBlock);
				block->Init(ObjectFileNames[objIndex],x*TileWidth + Anchor.x,y*TileHeight + Anchor.y);	

				if(objIndex>1)
					block->setBreakAble(true);
			}
		}
	}


	from.close();	
	delete[] tmp;


}



Direction CMap::getOppositeDirection(Direction d)
{
	if(d==N)
		return S;

	if(d==S)
		return N;

	if(d==W)
		return E;

	if(d==E)
		return W;



}
