

#include "Save.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "PublicFun.h"

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 int version_now;
extern int color;
int version_save;

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 char t_heroName[HEROMAX][HERONAMEMAXLEN]; 
using namespace std;


void Save::Init(int owner,int year)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	this->owner[0]=owner;
	this->year[0]=year;
	count=0;
	
}
void Save::Show()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	HFONT	h;
	HFONT oldh;
	if(color==1)
	{		
		h = CreateFont(45,0,0,0,FW_BLACK,0,FALSE,FALSE, 
			GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
			DEFAULT_QUALITY,FIXED_PITCH|FF_MODERN,"隶书"); 
		oldh=(HFONT)SelectObject(hdcTmp,h);
		SetTextColor(hdcTmp,RGB(255, 255, 255));

	}
	ShowCG(0,"Res\\save.sbp",0,0);
	ShowText(ownername,155,220);
	char str[10];
	sprintf_s(str,"%d",year[0]);
	if(owner[0]!=0)ShowText(str,500,220);
	ShowText(ownername1,155,290);
	sprintf_s(str,"%d",year[1]);
	if(owner[1]!=0)ShowText(str,500,290);
	ShowText(ownername2,155,360);
	sprintf_s(str,"%d",year[2]);
	if(owner[2]!=0)ShowText(str,500,360);
	BitBlt(hdcTmp,155,count*70+210,480,70,hdcTmp,155,count*70+210,DSTINVERT);
	if(color==1)
	{	 
		SelectObject(hdcTmp,oldh);
		SetTextColor(hdcTmp,RGB(0, 0, 0));
		DeleteObject(h);
		
	}

}

int Save::KeyDown(WPARAM KeyCode)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	if(KeyCode==KeyDown0)
	{

		count++;
		if(count>2)
		{
			count=0;
		}
		Show();
		return 3;
	}
	else if(KeyCode==KeyUp0)
	{
		count--;
		if(count<0)
		{
			count=2;
		}
		Show();
		return 3;
	}
	else if(KeyCode==KeyReturn)
	{
		return count;
	}
	else if(KeyCode==KeyEsc)
	{
		return -1;
	}
	else
	{
		return 3;
	}
	return 3;
}

int Save::Read(int retval)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	int i,j;
	char name[NORMALSTRLEN];
	if(retval==-1)
	{
		int mychoice = choice;//20170621修正武将名便城市的BUG
		//读取君主名 年份
		//ChangeRes("Res\\Save\\save0.ssv");
		ifstream infile("Res\\Save\\save0.ssv",ios::in);
		infile>>owner[0];
		infile>>year[0];
		infile>>choice;
		infile.close();
		InitSSS(choice);
		//ChangeRes("Res\\Save\\save0.ssv");
		if(owner[0]>0)sprintf_s(ownername,"%s",t_heroName[owner[0]-1]);

		//ChangeRes("Res\\Save\\save1.ssv");
		ifstream infile1("Res\\Save\\save1.ssv",ios::in);
		infile1>>owner[1];
		infile1>>year[1];
		infile1>>choice;
		infile1.close();
		InitSSS(choice);
		//ChangeRes("Res\\Save\\save1.ssv");
		if(owner[1]>0)sprintf_s(ownername1,"%s",t_heroName[owner[1]-1]);

		//ChangeRes("Res\\Save\\save2.ssv");
		ifstream infile2("Res\\Save\\save2.ssv",ios::in);
		infile2>>owner[2];
		infile2>>year[2];
		infile2>>choice;
		infile2.close();
		InitSSS(choice);
		//ChangeRes("Res\\Save\\save2.ssv");
		
		if(owner[2]>0)sprintf_s(ownername2,"%s",t_heroName[owner[2]-1]);
		choice = mychoice;
		InitSSS(choice);

		if(owner[0]==0)
		{
			sprintf_s(ownername,"%s",GetStrText("ATRR_STR70"));
			
		}
		if(owner[1]==0)
		{
			sprintf_s(ownername1,"%s",GetStrText("ATRR_STR70"));
		}
		if(owner[2]==0)
		{
			sprintf_s(ownername2,"%s",GetStrText("ATRR_STR70"));
		}
		return 0;
		
	}
	else if(retval==0)
	{			
		sprintf_s(name,"%s","Res\\Save\\save0.ssv");
	}
	else if(retval==1)
	{			
		sprintf_s(name,"%s","Res\\Save\\save1.ssv");
	}
	else if(retval==2)
	{			
		sprintf_s(name,"%s","Res\\Save\\save2.ssv");
	}

	//ChangeRes(name);
	ifstream infile(name,ios::in);

	infile>>owner1;
	if(owner1==0)
	{
		infile.close();
		return -1;
	}
	infile>>year1;
	infile>>choice;
	InitSSS(choice);

	for(i=0;i<HEROMAX;i++)
	{
		for(j=0;j<18;j++)
		{
			infile>>herosave[i][j];
		}
	}
	for(i=0;i<CITYMAX;i++)
	{
			for(j=0;j<19;j++)
		{
			infile>>citysave[i][j];
		}
	}
	for(i=0;i<GOODMAX;i++)
	{
		for(j=0;j<10;j++)
		{
			infile>>goodsave[i][j];
		}
	}
	for(i=0;i<TASKLISTMAX;i++)
	{
		for(j=0;j<10;j++)
		{
			infile>>tasklistsave[i][j];
		}
	}
	for(i=0;i<12;i++)
	{
		infile>>herolistsave[i];
	}
	for(i=0;i<4;i++)
	{
		infile>>citylistsave[i];
	}
	for(i=0;i<9;i++)
	{
		infile>>goodlistsave[i];
	}
	for(i=0;i<9;i++)
	{
		infile>>gamemapsave[i];
	}
	infile>>month;
	infile>>version_save;
	if(version_save>version_now)
	{
		infile.close();
		return -2;
	}
	infile>>tasknum;

	//ChangeRes(name);
	return 0;
		
}

void Save::Write(int retval)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	int i,j;
	char name[NORMALSTRLEN];
	if(retval==0)
	{			
		sprintf_s(name,"%s","Res\\Save\\save0.ssv");
	}
	else if(retval==1)
	{			
		sprintf_s(name,"%s","Res\\Save\\save1.ssv");
	}
	else if(retval==2)
	{			
		sprintf_s(name,"%s","Res\\Save\\save2.ssv");
	}
	else if(retval==9)
	{			
		sprintf_s(name,"%s","Res\\Save\\save9.ssv");
	}
	//ChangeRes(name);
	ofstream outfile(name,ios::out);
	outfile<<owner1;
	outfile<<" ";
	outfile<<year1;
	outfile<<" ";
	outfile<<choice;
	outfile<<"\n";
	for(i=0;i<HEROMAX;i++)
	{
		for(j=0;j<18;j++)
		{
			outfile<<herosave[i][j];
			outfile<<" ";
		}
		outfile<<"\n";
	}
	outfile<<"\n";
	for(i=0;i<CITYMAX;i++)
	{
			for(j=0;j<19;j++)
		{
			outfile<<citysave[i][j];
			outfile<<" ";
		}
		outfile<<"\n";
	}
	outfile<<"\n";
	for(i=0;i<GOODMAX;i++)
	{
		for(j=0;j<10;j++)
		{
			outfile<<goodsave[i][j];
			outfile<<" ";
		}
		outfile<<"\n";
	}
	outfile<<"\n";
	for(i=0;i<TASKLISTMAX;i++)
	{
		for(j=0;j<10;j++)
		{
			outfile<<tasklistsave[i][j];
			outfile<<" ";
		}
		outfile<<"\n";
	}
	outfile<<"\n";
	for(i=0;i<12;i++)
	{
		outfile<<herolistsave[i];
		outfile<<" ";
	}
	outfile<<"\n";
	for(i=0;i<4;i++)
	{
		outfile<<citylistsave[i];
		outfile<<" ";
	}
	outfile<<"\n";
	for(i=0;i<9;i++)
	{
		outfile<<goodlistsave[i];
		outfile<<" ";
	}
	outfile<<"\n";
	for(i=0;i<9;i++)
	{
		outfile<<gamemapsave[i];
		outfile<<" ";
	}
	outfile<<"\n";
	outfile<<month;
	outfile<<"\n";
	outfile<<version_now;
	outfile<<"\n";
	outfile<<tasknum;
	outfile<<"\n";
	outfile.close();

	//ChangeRes(name);
}

