
#include "FightHero.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  char texta[TEXTAMAX][20];
extern char t_skillName[SKILLMAX][SKILLNAMEMAXLEN];
extern int t_tile[MAPSUM][MAPMAX][MAPMAX];
extern int t_citymapnum[CITYMAX];
extern int t_tilestyle[TILEMAX];

int useableskillnum2=0;
using namespace std;


void FightHero::Init(int choice,int citynumber)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	uchoice=choice;
	active=1;
	skill=0;
	at=0;
	df=0;
	skillnum=0;
	statue=0;
	isemporer = 0;
	this->citynumber=citynumber;

	for(int i=0;i<20;i++)
		skillno[i]=-1;
	GetTile();
	//InitSpecialHeroList();
	InitSkillSumOfHeroType();
	InitPostion();
}



void FightHero::Show(int xx,int yy,int picno)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	ShowFightHero(style,belong,xx,yy,picno);
	SetSkillList();
}

void FightHero::ShowDirct(int xx,int yy)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	ShowFightHeroDirct(style,belong,xx,yy);
	SetSkillList();
}

int FightHero::KeyDown(WPARAM KeyCode)
{
	wlogfile(1,__FUNCTION__,"开始",KeyCode);
	return 0;
}

void FightHero::ShowFightHero(int g,int h,int posx,int posy,int picno)//posx posy确认显示的位置（10*5）
{
	wlogfile(1,__FUNCTION__,"开始",0);
	if(CheckIfRiver()==1)//获取脚下地形，如果为河流
	{
		g=6;
	}
	int de=picno;
	if(active==0)
	{
		de=0;
	}
	ShowCGHero(0,"Res\\Fight\\fight_bing.sbp","Res\\Fight\\fight_bingmask.sbp",g*2+de,h,posx*80,posy*80);
}

void FightHero::ShowFightHeroDirct(int g,int h,int posx,int posy)//posx posy确认显示的位置（10*5）
{
	wlogfile(1,__FUNCTION__,"开始",0);
	if(CheckIfRiver()==1)//获取脚下地形，如果为河流
	{
		g=6;
	}
	ShowCGHeroDirct(0,"Res\\Fight\\fight_bing.sbp","Res\\Fight\\fight_bingmask.sbp",g*2,h,posx*80,posy*80);
}

void FightHero::ShowCGHero(int delay,char *name,char *name1,int g,int h,int ux,int uy)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	uy+=50;
	LoadPic(name1,hbmp);	//封装完成
	HBITMAP oldhbmp=(HBITMAP)SelectObject(hdcMem, hbmp);
	BitBlt(hdcTmp, ux, uy, 80, 80, hdcMem, g*80, h*80, SRCPAINT);
	SelectObject(hdcMem, oldhbmp);
	DeleteObject(hbmp);
	LoadPic(name,hbmp);
	GetObject(hbmp, sizeof(BITMAP), &bmp);
	oldhbmp=(HBITMAP)SelectObject(hdcMem, hbmp);
	BitBlt(hdcTmp, ux, uy, 80, 80, hdcMem, g*80, h*80, SRCAND);
	InvalidateRect(hwnd,NULL,false);
	SelectObject(hdcMem, oldhbmp);
	DeleteObject(hbmp);

}

void FightHero::ShowCGHeroDirct(int delay,char *name,char *name1,int g,int h,int ux,int uy)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	uy+=50;
	LoadPic(name1,hbmp);	//封装完成
	HBITMAP oldhbmp=(HBITMAP)SelectObject(hdcMem, hbmp);
	BitBlt(hdc, ux, uy, 80, 80, hdcMem, g*80, h*80, SRCPAINT);
	SelectObject(hdcMem, oldhbmp);
	DeleteObject(hbmp);
	LoadPic(name,hbmp);
	GetObject(hbmp, sizeof(BITMAP), &bmp);
	oldhbmp=(HBITMAP)SelectObject(hdcMem, hbmp);
	BitBlt(hdc, ux, uy, 80, 80, hdcMem, g*80, h*80, SRCAND);
	SelectObject(hdcMem, oldhbmp);
	//BitBlt(hdc, 0, 0, 800, 600, hdcTmp,0, 0, SRCPAINT);
	//InvalidateRect(hwnd,NULL,false);
	DeleteObject(hbmp);


}

void FightHero::SetPosition(int g)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	//int centerx=2;
	//int centery=16;
	//int cityx=15;
	//int cityy=15;
	
	/*int warfactx[8][10]={
		0,1,0,0,-1,-1,1,-2,1,2,
		0,1,0,0,-1,2,2,-1,-1,-2,
		0,1,0,0,-1,1,2,-1,-1,-2,
		0,1,0,0,-1,-2,2,-1,1,0,
		0,1,0,0,-1,-2,-2,1,1,2,
		0,1,0,0,-1,-1,-2,1,1,2,
		0,1,0,0,-1,-2,2,-1,1,0,
		0,1,0,0,-1,-1,2,-1,-1,-2};

	int warfacty[8][10]={
		0,0,-1,1,0,-1,-2,1,1,2,
		0,0,-1,1,0,-2,2,-1,1,0,
		0,0,-1,1,0,-1,1,-2,1,2,
		0,0,-1,1,0,-2,-2,1,1,2,
		0,0,-1,1,0,-2,2,-1,1,0,
		0,0,-1,1,0,1,-1,2,-1,-2,
		0,0,-1,1,0,2,2,-1,-1,-2,
		0,0,-1,1,0,-1,-1,2,-1,-2};*/

	/*int warfactx1[9][10]={0,1,0,0,-1,-1,2,-1,-1,-2,
		0,1,0,0,-1,1,2,-1,-1,-2,
		0,1,0,0,-1,-2,-2,1,1,2,
		0,1,0,0,-1,-1,-2,1,1,2,
		0,1,0,0,-1,-2,2,-1,1,0,
		0,1,0,0,-1,2,2,-1,-1,-2,
		0,1,0,0,-1,1,2,-1,-1,-2,
		0,1,0,0,-1,-2,2,-1,1,0,
		0,1,0,0,-1,-1,1,-2,1,2};

	int warfacty1[9][10]={0,1,0,0,-1,-1,2,-1,-1,-2,
		0,0,-1,1,0,1,-1,2,-1,-2,
		0,0,-1,1,0,-2,2,-1,1,0,
		0,0,-1,1,0,1,-1,2,-1,-2,
		0,0,-1,1,0,2,2,-1,-1,-2,
		0,0,-1,1,0,-2,2,-1,1,0,
		0,0,-1,1,0,-1,1,-2,1,2,
		0,0,-1,1,0,-2,-2,1,1,2,
		0,0,-1,1,0,-1,-2,1,1,2};*/



	if(warstyle==0)//进攻战
	{
		if(g<FIGHTHEROMAX)
		{
			int way1=way-1;
			OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,way1);
			OverflowCheck(__FILE__,__LINE__,FIGHTHEROMAX,g);
			if(way1<0)way1=0;
				if(way1>7)way1=7;
			positionx=centerx+t_warfactx[way1][g];
			positiony=centery+t_warfacty[way1][g];
			belong=0;
		}
		else
		{
			int way2=way-1;
			OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,way2);
			OverflowCheck(__FILE__,__LINE__,FIGHTHEROMAX,g-10);
			if(way2<0)way2=0;
				if(way2>7)way2=7;
			positionx=cityx+t_warfactx[way2][g-10];
			positiony=cityy+t_warfacty[way2][g-10];
			belong=1;
		}

	}
	else
	{
		if(g<FIGHTHEROMAX)
		{
			int way1=way-1;
			OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,way1);
			OverflowCheck(__FILE__,__LINE__,FIGHTHEROMAX,g);
			if(way1<0)way1=0;
				if(way1>7)way1=7;
			positionx=cityx+t_warfactx[way1][g];
			positiony=cityy+t_warfacty[way1][g];
			belong=0;
		}
		else
		{
			int way2=way-1;
			OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,way2);
			OverflowCheck(__FILE__,__LINE__,FIGHTHEROMAX,g-10);
			if(way2<0)way2=0;
				if(way2>7)way2=7;
			positionx=centerx+t_warfactx[way2][g-10];
			positiony=centery+t_warfacty[way2][g-10];
			belong=1;
		}

	}
}

int FightHero::CheckIfRiver()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	

	//ChangeRes(name);
	if(tile[positionx][positiony]>=16&&tile[positionx][positiony]!=41)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int FightHero::GetTile()
{
	
	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];
		}
	}
	
	return 0;
}

void FightHero::SetSkillList()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	skillnum = 0;//201707
	if(specicalhero > 0)	//1庞统 2诸葛亮 3华佗 4徐庶
	{
		OverflowCheck(__FILE__,__LINE__,10,specicalhero+1);
		skillno[0]=t_specialhero[specicalhero+1][1];
		skillnum ++;
	/*case 1:
		skillno[0]=t;
		skillnum=1;
		break;
	case 2:
		skillno[0]=23;
		skillnum=1;
		break;
	case 3:
		skillno[0]=24;
		skillnum=1;
		break;
	case 4:
		skillno[0]=18;
		skillnum=1;
		break;
	*/

	}

	if(isemporer == 1)
	{
		OverflowCheck(__FILE__,__LINE__,20,skillnum);
		skillno[skillnum]=24;//君主 谍报
		skillnum ++;
	}

	OverflowCheck(__FILE__,__LINE__,6,style);
	for(int i = 0 ; i < t_skillsum[style] ; i++)
	{
		skillno[skillnum]=t_skilltype[style][i];
		skillnum ++;
	}
	/*if(style==1)
	{
		skillno[skillnum+0]=0;//火攻0
		skillno[skillnum+1]=1;//滚木
		skillno[skillnum+2]=2;//落石
		skillno[skillnum+3]=3;//奋战
		skillnum+=4;
	}
	else if(style==2)
	{
		skillno[skillnum+0]=4;//飞矢
		skillno[skillnum+1]=5;//箭雨5
		skillno[skillnum+2]=6;//火箭
		skillnum+=3;
	}
	else if(style==0)
	{
		skillno[skillnum+0]=7;//践踏
		skillno[skillnum+1]=8;//冲锋
		skillno[skillnum+2]=9;//突击
		skillnum+=3;
	}
	else if(style==3)
	{
		skillno[skillnum+0]=10;//水淹10
		skillno[skillnum+1]=11;//撞击
		skillno[skillnum+2]=6;//火箭
		skillnum+=3;
	}
	else if(style==5)
	{
		skillno[skillnum+0]=12;//咒封
		skillno[skillnum+1]=13;//定身
		skillno[skillnum+2]=14;//留言
		skillno[skillnum+3]=15;//援兵15
		skillno[skillnum+4]=16;//烈火
		skillno[skillnum+5]=17;//海啸
		skillno[skillnum+6]=18;//奇门
		skillno[skillnum+7]=19;//援军
		skillno[skillnum+8]=20;//围攻20
		skillnum+=9;
	}
	else if(style==4)
	{
		skillno[skillnum+0]=7;//践踏
		skillno[skillnum+1]=8;//冲锋
		skillno[skillnum+2]=9;//突击
		skillno[skillnum+3]=21;//突袭
		skillnum+=4;
	}*/
	//WriteSkilltoFile();

}

void FightHero::WriteSkilltoFile()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	
	ofstream outfile("Res\\tmp.stp",ios::out);
	//char *c=new char[20];
	for(int i=0;i<skillnum;i++)
	{

		for(int j=0;j<4;j++)
		{
			if(skillno[i]<0||skillno[i]>255)break;
			OverflowCheck(__FILE__,__LINE__,SKILLMAX,skillno[i]);
			outfile<<t_skillName[skillno[i]][j];
		}
		outfile<<"\n";
	}
	outfile.close();
}

void FightHero::GetMenuSkill()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	//menuskill=new Menu();
	int useableskillnum=0;
	int tmpskillnum=0;
	if(specicalhero>0)	//1庞统 2诸葛亮 3华佗 4徐庶 
	{
	
		tmpskillnum=1;
		
	}
	if(isemporer == 1)
	{
		tmpskillnum++;
	}

	char name[NORMALSTRLEN];
	sprintf_s(name,"%s","Res\\Fight\\fightheroskillsum.sdt");
	ifstream infile(name,ios::in);
	int i_t = 0;
	int t_skillsum[HEROTYPEMAX][DEGREEMAX];
	for(int g=0;g<HEROTYPEMAX;g++)
	{
		for(int h=0;h<DEGREEMAX;h++)
		{			
			infile>>t_skillsum[g][h];
		}
	}
	
	
	infile.close();
	OverflowCheck(__FILE__,__LINE__,DEGREEMAX,degree-1);
	if(degree < 1 || degree > 20)degree = 1;//20170721
	useableskillnum = t_skillsum[style][degree-1];
	/*switch(style)
	{
	case CAVALRY:
		if(degree<7)
		{
			useableskillnum=1;
		}
		else if(degree<11)
		{
			useableskillnum=2;
		}
		else
		{
			useableskillnum=3;
		}
		break;
	case INFANTRY:
		if(degree<6)
		{
			useableskillnum=1;
		}
		else if(degree<11)
		{
			useableskillnum=2;
		}
		else if(degree<16)
		{
			useableskillnum=3;
		}
		else
		{
			useableskillnum=4;
		}
		break;
	case ARCHER:
		if(degree<6)
		{
			useableskillnum=1;
		}
		else if(degree<13)
		{
			useableskillnum=2;
		}
		else
		{
			useableskillnum=3;
		}
		break;
	case SAILOR:
		if(degree<7)
		{
			useableskillnum=1;
		}
		else if(degree<13)
		{
			useableskillnum=2;
		}
		else
		{
			useableskillnum=3;
		}
		break;
	case GREATMPARM:
		if(degree<2)
		{
			useableskillnum=1;
		}
		else if(degree<6)
		{
			useableskillnum=2;
		}
		else if(degree<7)
		{
			useableskillnum=3;
		}
		else if(degree<11)
		{
			useableskillnum=4;
		}
		else if(degree<12)
		{
			useableskillnum=5;
		}
		else if(degree<14)
		{
			useableskillnum=6;
		}
		else if(degree<17)
		{
			useableskillnum=7;
		}
		else if(degree<19)
		{
			useableskillnum=8;
		}
		else
		{
			useableskillnum=9;
		}
		break;
	case GREATHPARM:
		if(degree<7)
		{
			useableskillnum=1;
		}
		else if(degree<11)
		{
			useableskillnum=2;
		}
		else if(degree<16)
		{
			useableskillnum=3;
		}
		else
		{
			useableskillnum=4;
		}
		break;
	}*/
	useableskillnum+=tmpskillnum;
	useableskillnum2=useableskillnum;
	//menuskill.InitMenu(2,useableskillnum>1?2:1,420,220,useableskillnum-1,"Res\\tmp.stp",1);
	menuskill.InitMenu(2,useableskillnum>1?2:1,420,220,useableskillnum-1,"Res\\tmp.stp",3);//20150920修正技能菜单问题
	//DeleteFile("Res\\tmp.stp");
}

void FightHero::ShowMenuSkill()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	menuskill.Show(0);
}

void FightHero::GetStartPosition()
{
	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};

	OverflowCheck(__FILE__,__LINE__,CITYMAX,citynumber);
	int retch=t_citymapnum[citynumber];
	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 g=0;g<t_sum;g++)
	{
		OverflowCheck(__FILE__,__LINE__,MAPSUM,g);
		infile>>t_cityx[g];
		infile>>t_cityy[g];
	}
	infile>>t_sum;
	for(int g=0;g<t_sum;g++)
	{
		OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,g);
		infile>>t_centery[g];
		infile>>t_centerx[g];
	}
	infile.close();

	cityx = t_cityx[retch];
	cityy = t_cityy[retch];
	cityy+=4;
	cityx+=2;
	/*switch(retch)
	{
	case 0:
		cityx=15;
		cityy=15;
		break;
	case 1:
		cityx=17;
		cityy=16;
		break;
	case 2:
		cityx=14;
		cityy=15;
		break;
	case 3:
		cityx=17;
		cityy=14;
		break;
	case 4:
		cityx=15;
		cityy=15;
		break;
	case 5:
		cityx=17;
		cityy=16;
		break;
	case 6:
		cityx=15;
		cityy=13;
		break;
	}*/

	way=GetMyPosition();//citynumber,citynumfrom
	/*switch(way)
	{
	case 1://DIRECT_ES:
		centery = 22;
		centerx = 26;
		break;
	case 2://DIRECT_N:
		centery = 12;
		centerx = 0;
		break;
	case 3://DIRECT_EN:
		centery = 22;
		centerx = 2;
		break;
	case 4://DIRECT_E:
		centery = 22;
		centerx = 13;
		break;
	case 5://DIRECT_S:
		centery = 14;
		centerx = 26;
		break;
	case 6://DIRECT_WS:
		centery = 2;
		centerx = 26;
		break;
	case 7://DIRECT_W:
		centery = 2;
		centerx = 13;
		break;
	case 8://DIRECT_WN:
		centery = 0;
		centerx = 0;
		break;
	}*/
	OverflowCheck(__FILE__,__LINE__,DIRCTIONMAX,way-1);
	centerx = t_centerx[way-1];
	centery = t_centery[way-1];
	centery+=4;
	centerx+=2;



}

int FightHero::GetMyPosition()//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++)
	{
		OverflowCheck(__FILE__,__LINE__,CITYMAX,citynumber);
		if(fact[citynumber][u]==citynumfrom)
			return u+1;

	}
	return 0;
}

int FightHero::InitSpecialHeroList()
{
	char name[NORMALSTRLEN];
	sprintf_s(name,"%s%d%s","Res\\specialhero",uchoice+1,".sdt");
	ifstream infile(name,ios::in);
	
	for(int g=0;g<SPECIALHEROMAX;g++)
	{
		for(int h=0;h<2;h++)
		{			
			infile>>t_specialhero[g][h];
			if(infile.eof()!=0) break;
		}
		if(infile.eof()!=0) break;
	}
	
	infile.close();

	for(int g=0;g<SPECIALHEROMAX;g++)
	{
		if(t_specialhero[g][1]==0)break;
		if(num == t_specialhero[g][0])
		{
			specicalhero = g+1;
			return 0;
		}
	}
	
	return 0;
}

int FightHero::InitSkillSumOfHeroType()
{
	char name[NORMALSTRLEN];
	sprintf_s(name,"%s","Res\\Fight\\fightheroskillid.sdt");
	ifstream infile(name,ios::in);
	
	for(int g=0;g<HEROTYPEMAX;g++)
	{
		infile>>t_skillsum[g];
		for(int h=0;h<t_skillsum[g];h++)
		{			
			infile>>t_skilltype[g][h];
			if(infile.eof()!=0) break;
		}
		if(infile.eof()!=0) break;
	}
	
	infile.close();
	return 0;
}

int FightHero::InitPostion()
{
	
	GetStartPosition();
	ifstream infile("Res\\Fight\\fightheropos.sdt",ios::in);;
	for(int i=0;i<DIRCTIONMAX;i++)
	{
		for(int j=0;j<FIGHTHEROMAX;j++)
		{
			infile>>t_warfactx[i][j];

		}

	}
	for(int i=0;i<DIRCTIONMAX;i++)
	{
		for(int j=0;j<FIGHTHEROMAX;j++)
		{
			infile>>t_warfacty[i][j];

		}

	}
	infile.close();
	return 0;
}