
#include "FightMap.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "PublicFun.h"
#include <fstream>

extern  PAINTSTRUCT ps;
extern 	HDC hdc;
extern 	HBITMAP hbmp; 
extern 	HBITMAP hbmp2; 
extern 	BITMAP bmp;
extern 	HDC hdcMem;
extern 	HDC hdcTmp;
extern 	RECT rect;
extern 	HWND hwnd;

extern int KeyReturn;
extern int KeyEsc;
extern int KeyHome;
extern int KeySpace;
extern int KeyLeft0;
extern int KeyUp0;
extern int KeyRight0;
extern int KeyDown0;

extern int t_tile[MAPSUM][MAPMAX][MAPMAX];
extern int t_citymapnum[CITYMAX];
extern int t_tilestyle[TILEMAX];

using namespace std;

void FightMap::Init(int choice,int citynumber)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	//uchoice=choice;
	this->citynumber=citynumber;
	startx=0;
	starty=0;	
	arrowx=4;
	arrowy=2;

	InitMapStyle();
	//ChangeRes("Res\\Fight\\fightmap1.sdt");
	/*char name[NORMALSTRLEN];
	int citymapnum[]={6,0,1,6,3,4,2,5,6,3,1,0,2,3,4,0,2,1,4,6,1,1,3,5,6,6,3,3,4,2,6,6,1,2,3,4,5,1};
	int retch=citymapnum[citynumber];
	if (retch==0)sprintf_s(name,"%s","Res\\Fight\\fightmap00.sdt");
	if (retch==1)sprintf_s(name,"%s","Res\\Fight\\fightmap01.sdt");
	if (retch==2)sprintf_s(name,"%s","Res\\Fight\\fightmap02.sdt");
	if (retch==3)sprintf_s(name,"%s","Res\\Fight\\fightmap03.sdt");
	if (retch==4)sprintf_s(name,"%s","Res\\Fight\\fightmap04.sdt");
	if (retch==5)sprintf_s(name,"%s","Res\\Fight\\fightmap05.sdt");
	if (retch==6)sprintf_s(name,"%s","Res\\Fight\\fightmap06.sdt");
	ifstream infile(name,ios::in);

	for(int g=0;g<MAPMAX;g++)
	{
	for(int h=0;h<MAPMAX;h++)
	{			
	infile>>tile[g][h];
	if(tile[g][h]==1)
	{
	tilestyle[g][h]=0;//平地
	}
	if(tile[g][h]==2)
	{
	tilestyle[g][h]=1;//草地
	}
	if(tile[g][h]==3)
	{
	tilestyle[g][h]=5;//城池
	}
	if(tile[g][h]==4)
	{
	tilestyle[g][h]=4;//村庄
	}
	if(tile[g][h]==5)
	{
	tilestyle[g][h]=3;//森林
	}
	if(tile[g][h]>=6&&tile[g][h]<=15)
	{
	tilestyle[g][h]=2;//山脉
	}
	if(tile[g][h]>=16&&tile[g][h]<=45&&tile[g][h]!=41)
	{
	tilestyle[g][h]=7;//河流
	}
	if(tile[g][h]==41)
	{
	tilestyle[g][h]=6;//营寨
	}
	}

	}
	infile.close();*/
	//ChangeRes(name);

}



void FightMap::Show(int g)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	LoadFromBMP();
	ShowArrow();
}

void FightMap::ShowStart(int g,int warstyle)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	if(g==-2)//游戏开始时的敌方位置显示
	{
		GetStartPosition(-1,warstyle);//获取startx,starty，地方
		LoadFromBMPStart();
	}
	else if(g==-1)//游戏开始时的我方位置显示
	{
		GetStartPosition(0,warstyle);//获取startx,starty,我方
		LoadFromBMPStart();
	}
	else
	{
		LoadFromBMPStart();
	}
}

void FightMap::ShowMove(int x,int y)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	LoadPic("Res\\Fight\\tiles.sbp",hbmp);	//封装完成		
	GetObject(hbmp, sizeof(BITMAP), &bmp);
	HBITMAP oldhbmp=(HBITMAP)SelectObject(hdcMem, hbmp);	
	for(int g=0;g<5;g++)
	{
		for(int h=0;h<10;h++)
		{
			BitBlt(hdcTmp, h*80, 50+g*80, 80, 80, hdcMem, 0, tile[g+x][h+y]*80, SRCCOPY);
		}

	}
	BitBlt(hdc, 0, 0, 800, 600, hdcTmp, 0,0, SRCCOPY);
	SelectObject(hdcMem, oldhbmp);
	DeleteObject(hbmp);
}

int FightMap::KeyDown(WPARAM KeyCode)
{
	wlogfile(1,__FUNCTION__,"开始",KeyCode);
	if(KeyCode==KeyUp0)
	{
		if(arrowy>0)
		{
			arrowy--;
		}
		else 
		{
			startx--;
		}
		if(startx<0)
		{
			startx=0;

		}
		Show(0);
	}
	else if(KeyCode==KeyDown0)
	{
		if(arrowy<4)
		{
			arrowy++;
		}
		else 
		{
			startx++;
		}
		if(startx>26)
		{
			startx=26;

		}
		Show(0);
	}
	else if(KeyCode==KeyLeft0)
	{
		if(arrowx>0)
		{
			arrowx--;
		}
		else 
		{
			starty--;
		}
		if(starty<0)
		{
			starty=0;

		}
		Show(0);
	}
	else if (KeyCode ==KeyRight0)
	{

		if(arrowx<9)
		{
			arrowx++;
		}
		else 
		{
			starty++;
		}
		if(starty>22)
		{
			starty=22;

		}
		Show(0);
	}
	return 0;
}

void FightMap::LoadFromBMP()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	LoadPic("Res\\Fight\\tiles.sbp",hbmp);	//封装完成		
	GetObject(hbmp, sizeof(BITMAP), &bmp);
	HBITMAP oldhbmp=(HBITMAP)SelectObject(hdcMem, hbmp);	
	for(int g=0;g<5;g++)
	{
		for(int h=0;h<10;h++)
		{
			BitBlt(hdcTmp, h*80, 50+g*80, 80, 80, hdcMem, 0, tile[g+startx][h+starty]*80, SRCCOPY);
		}
	}
	InvalidateRect(hwnd,NULL,FALSE);
	SelectObject(hdcMem, oldhbmp);

	DeleteObject(hbmp);



}

void FightMap::LoadFromBMPStart()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	LoadPic("Res\\Fight\\tiles.sbp",hbmp);	//封装完成		
	GetObject(hbmp, sizeof(BITMAP), &bmp);
	HBITMAP oldhbmp=(HBITMAP)SelectObject(hdcMem, hbmp);	
	for(int g=0;g<5;g++)
	{
		for(int h=0;h<10;h++)
		{
			BitBlt(hdcTmp, h*80, 50+g*80, 80, 80, hdcMem, 0, tile[g+startx][h+starty]*80, SRCCOPY);
		}

	}
	BitBlt(hdc, 0, 0, 800, 600, hdcTmp, 0,0, SRCCOPY);
	SelectObject(hdcMem, oldhbmp);
	DeleteObject(hbmp);
	//Delay(2000);

}

void FightMap::ShowArrow()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	ShowCGAdd(0,"Res\\Fight\\arrow.sbp","Res\\Fight\\arrowmask.sbp",arrowx*80,arrowy*80);

}

void FightMap::GetStartPosition(int g,int warstyle)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	//int citymapnum[]={6,0,1,6,3,4,2,5,6,3,1,0,2,3,4,0,2,1,4,6,1,1,3,5,6,6,3,3,4,2,6,6,1,2,3,4,5,1};
	char name[NORMALSTRLEN];
	sprintf_s(name,"%s","Res\\Fight\\fightstartpos.sdt");
	ifstream infile(name,ios::in);
	int t_sum = 0;
	infile>>t_sum;
	for(int i=0;i<t_sum;i++)
	{
		OverflowCheck(__FILE__,__LINE__,MAPSUM,i);
		infile>>t_cityx[i];
		infile>>t_cityy[i];
	}
	infile>>t_sum;
	for(int i=0;i<t_sum;i++)
	{
		OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,i);
		infile>>t_centery[i];
		infile>>t_centerx[i];
	}
	infile.close();
	//if(warstyle==0)//进攻战 
	{
		if((g==-1&&warstyle==0)||(g==0&&warstyle==1))//敌方位置
		{

			int retch=t_citymapnum[citynumber];//城池位置 7种地形



			startx = t_cityx[retch];
			starty = t_cityy[retch];
			/*switch(retch)
			{
			case 0:
			startx=13;
			starty=11;
			break;
			case 1:
			startx=15;
			starty=12;
			break;
			case 2:
			startx=12;
			starty=11;
			break;
			case 3:
			startx=15;
			starty=10;
			break;
			case 4:
			startx=13;
			starty=11;
			break;
			case 5:
			startx=15;
			starty=12;
			break;
			case 6:
			startx=13;
			starty=9;
			break;
			}*/
		}
		else if((g==0&&warstyle==0)||(g==-1&&warstyle==1))
		{
			//starty = 12;
			//startx = 0;
			int tmpret=GetMyPosition(warstyle);//citynumber,citynumfrom//进攻点位置 8个方位
			OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,tmpret-1);
			startx = t_centerx[tmpret-1];
			starty = t_centery[tmpret-1];
			/*switch(tmpret)
			{
			case 1://DIRECT_ES:
				starty = 22;
				startx = 26;
				break;
			case 2://DIRECT_N:
				starty = 12;
				startx = 0;
				break;
			case 3://DIRECT_EN:
				starty = 22;
				startx = 2;
				break;
			case 4://DIRECT_E:
				starty = 22;
				startx = 13;
				break;
			case 5://DIRECT_S:
				starty = 14;
				startx = 26;
				break;
			case 6://DIRECT_WS:
				starty = 2;
				startx = 26;
				break;
			case 7://DIRECT_W:
				starty = 2;
				startx = 13;
				break;
			case 8://DIRECT_WN:
				starty = 0;
				startx = 0;
				break;
			}*/

		}
	}


}

int FightMap::GetMyPosition(int warstyle)//citynumber,citynumfrom
{
	wlogfile(1,__FUNCTION__,"开始",0);

	char name[NORMALSTRLEN];
	sprintf_s(name,"%s","Res\\Fight\\fightcitypos.sdt");
	ifstream infile(name,ios::in);
	int i_t = 0;
	int fact[CITYMAX][DIRCTIONMAX];
	for(int g=0;g<CITYMAX;g++)
	{
		for(int h=0;h<DIRCTIONMAX;h++)
		{			
			infile>>fact[g][h];
			if(infile.eof()!=0) break;
		}
		if(infile.eof()!=0) break;
	}

	infile.close();

	/*int fact[38][8]={4,0,0,0,0,0,0,0,
	8,0,0,3,0,7,0,0,
	0,0,0,0,0,0,2,0,
	9,0,0,0,0,0,0,1,
	0,0,6,0,11,0,0,0,
	0,0,0,7,12,0,0,5,
	0,0,2,0,13,0,6,0,
	0,0,0,0,0,14,0,2,
	0,0,0,10,15,0,0,4,
	0,0,0,11,0,0,9,0,
	21,5,0,16,0,15,0,10,
	0,6,0,13,16,0,0,0,
	0,7,0,14,17,0,12,0,
	19,0,8,0,18,0,13,0,
	21,9,11,0,0,20,0,0,
	0,12,0,17,22,0,0,11,
	23,13,0,0,0,0,16,0,
	0,14,0,19,23,0,0,0,
	0,0,0,0,0,0,18,14,
	0,0,15,0,25,0,0,0,
	0,0,0,22,27,0,15,11,
	0,16,0,23,0,0,21,0,
	30,18,0,24,0,29,22,17,
	0,0,0,0,0,0,23,0,
	32,20,0,26,0,31,0,0,
	0,0,0,0,0,0,25,0,
	34,21,0,28,0,33,0,0,
	0,0,0,29,34,0,27,0,
	35,0,23,0,0,0,28,0,
	0,0,0,0,0,35,0,23,
	0,0,25,0,0,0,0,0,
	0,0,0,33,0,0,0,25,
	36,0,27,0,0,0,32,0,
	0,28,0,35,37,0,0,27,
	0,0,30,0,38,0,34,29,
	0,0,0,37,0,0,0,33,
	0,34,0,38,0,0,36,0,
	0,35,0,0,0,0,37,0};*/
	int ret=0;
	for(int u=0;u<DIRCTIONMAX;u++)
	{
		if(fact[citynumber][u]==citynumfrom)
			return u+1;

	}
	return 0;
}

int FightMap::InitMapStyle()
{
	
	int retch=0;
	if(citynumber>=CITYMAX)retch=0;//20161216
	else
	{
		retch = t_citymapnum[citynumber];
	}

	for(int g=0;g<MAPMAX;g++)
	{
		for(int h=0;h<MAPMAX;h++)
		{			
			tile[g][h] = t_tile[retch][g][h];
			OverflowCheck(__FILE__,__LINE__,TILEMAX,t_tile[retch][g][h]-1);
			tilestyle[g][h] = t_tilestyle[t_tile[retch][g][h]-1];
			
		}			
	}
	return 0;
}
