

#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 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;

using namespace std;


void Save::Init(int type)
{
	this->type = type;
	count = 0;

}
void Save::Show()
{
	//HFONT oldh;
	//HFONT	h;
	//h = CreateFont(16,0,0,0,FW_NORMAL,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));
	if(showConfirmDlg == 1)
	{
		//显示确认对话框
		HPEN hpen=CreatePen(PS_SOLID,1, RGB(0,0,0));
		HPEN oldhpen=(HPEN)SelectObject(hdcTmp,hpen);
		Rectangle(hdcTmp,30+3,18+3,130+11,73+7); 
		Rectangle(hdcTmp,30+4,18+4,130+10,73+6); 
		Rectangle(hdcTmp,30+5,18+5,130+9,73+5); 
		Rectangle(hdcTmp,30,18,130+8,73+4); 	
		SelectObject(hdcTmp,oldhpen);
		DeleteObject(hpen);	
		ShowText("覆盖原进度？",40,25,16);	

		Rectangle(hdcTmp,50-4,50-2,66+4,66+2); 	
		Rectangle(hdcTmp,90-4,50-2,106+4,66+2); 

		ShowText("是",50,50,16);
		ShowText("否",90,50,16);



		//反色选中项
		BitBlt(hdcTmp,50-2+40*attFlag,50,20,16,hdcTmp,50-2+40*attFlag,50,DSTINVERT);
	}
	else
	{
		if(type == 1)//写
		{
			ShowPic(0,PIC_OTHER,2,15,0,0);
		}
		else//读
		{
			ShowPic(0,PIC_OTHER,2,16,0,0);
		}
		//ShowText("22222222222",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,0,0,480,70,hdcTmp,0,0,DSTINVERT);
		BitBlt(hdcTmp,66,count*22+28,84,17,hdcTmp,66,count*22+28,DSTINVERT);
		ShowHeadAndSceneName();

		//SelectObject(hdcTmp,oldh);
		//SetTextColor(hdcTmp,RGB(0, 0, 0));
		//DeleteObject(h);
	}

}

int Save::KeyDown(WPARAM KeyCode)
{
	if(showConfirmDlg == 1)
	{
		if(KeyCode==KeyLeft0)
		{
			attFlag = 0;
			Show();
		}
		else if(KeyCode==KeyRight0)
		{
			attFlag = 1;
			Show();
		}
		else if(KeyCode==KeyReturn)
		{
			if(attFlag == 0)//确定
			{
				showConfirmDlg = 0;
				Show();
				Write(count);
				return 1;
			}
			else if(attFlag == 1)//取消
			{
				showConfirmDlg = 0;
				//Show();
				return -1;
			}
		}
	}
	else 
	{
		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)
		{
			if(type == 0)//读
			{
				if (!IsVoid(count))//20210911
				{
					int rtr = Read(count);
					if(rtr == -2)
					{
						//TimerMessage("存档版本>游戏版本！",50,280,12,1,1000);
						return 3;
					}
				}
				else
				{
					//TimerMessage("无法读取空存档！",50,280,12,1,1000);
					return 3;
				}
				
			}
			else if (type == 1)
			{
				//20160304 增加 覆盖验证
				if (!IsVoid(count))
				{
					showConfirmDlg = 1;
					attFlag = 0;
					Show();
					return 4;
				}
				else
				{
					Write(count);
				}
			}
			return 1;
		}
		else if(KeyCode==KeyEsc)
		{
			return -1;
		}
		else
		{
			return 3;
		}
	}
	return 3;
}

int Save::Read(int retval)
{
	wlogfile(1, __FUNCTION__ ,"saveid=",retval);
	//int i,j;
	char name[200];
	memset(name,0,sizeof(name));
	if(retval==-1)//读取所在地图和角色
	{
		char name[200];
		char name1[200];
		memset(name,0,sizeof(name));
		GetModuleFileName(NULL,name1,200); 
		(strrchr(name1, '\\'))[1] = 0;
		sprintf_s(name,"%s\\Res\\%s0.ssv",name1,gameName);
		_actorNum[0] = 0;//初始化
		ifstream infile(name,ios::in);
		infile>>_gameName[0];
		infile>>_sceneName[0];
		infile>>_actorNum[0];//队列中角色数目
		for(int i = 0; i< _actorNum[0];i++)
		{
			infile>>_actorNo[0][i];
		}
		infile.close();

		sprintf_s(name,"%s\\Res\\%s1.ssv",name1,gameName);
		_actorNum[1] = 0;//初始化
		ifstream infile1(name,ios::in);
		infile1>>_gameName[1];
		infile1>>_sceneName[1];
		infile1>>_actorNum[1];//队列中角色数目
		for(int i = 0; i< _actorNum[1];i++)
		{
			infile1>>_actorNo[1][i];
		}
		infile1.close();

		sprintf_s(name,"%s\\Res\\%s2.ssv",name1,gameName);
		_actorNum[2] = 0;//初始化
		ifstream infile2(name,ios::in);
		infile2>>_gameName[2];
		infile2>>_sceneName[2];
		infile2>>_actorNum[2];//队列中角色数目
		for(int i = 0; i< _actorNum[2];i++)
		{
			infile2>>_actorNo[2][i];
		}
		infile2.close();

		ShowHeadAndSceneName();

		return 0;

	}
	else if(retval==0)
	{			
		char name[200];
		char name1[200];
		memset(name,0,sizeof(name));
		GetModuleFileName(NULL,name1,200); 
		(strrchr(name1, '\\'))[1] = 0;
		sprintf_s(name,"%s\\Res\\%s0.ssv",name1,gameName);

		ifstream infile(name,ios::in);
		infile>>gameName;
		infile>>sceneName;
		infile>>actorNum;//队列中角色数目
		for(int i = 0; i< actorNum;i++)
		{
			infile>>actorNo[i];
		}
		infile>>mapType;
		infile>>mapNo;
		infile>>mapX;
		infile>>mapY;
		infile>>scriptType;
		infile>>scriptNo;
		infile>>controlId;

		if(controlId > 100)//v1.30版本控制
		{
			versionno = controlId;
			infile>>controlId;
			for(int j = 0 ;j<25;j++)
			{
				for(int k = 0 ;k < 28;k++)
				{
					infile>>actorInfo[j][k];
				}
			}
		}
		else
		{
			versionno = 160902100;
			for(int j = 0 ;j<4;j++)
			{
				for(int k = 0 ;k < 28;k++)
				{
					infile>>actorInfo[j][k];
				}
			}
		}

		
		infile>>money;

		infile>>goodListCount;
		for(int u = 0; u< goodListCount;u++)
		{
			infile>>goodList[u][0];
			infile>>goodList[u][1];
			infile>>goodList[u][2];
		}
		infile>>equipListCount;
		for(int u = 0; u< equipListCount;u++)
		{
			infile>>equipList[u][0];
			infile>>equipList[u][1];
			infile>>equipList[u][2];
		}
		for(int i = 0 ; i < 2400 ;i ++)
		{
			infile>>globalEvent[i];
		}

		for(int i = 0 ; i < 240 ;i ++)
		{
			infile>>globalValue[i];
		}

		infile>>saveEnable;
		infile>>fightEnable;

		for(int i = 0 ; i < 12 ;i ++)
		{
			infile>>fightSave[i];
		}

		for(int i = 0 ; i < 255 ;i ++)
		{
			for(int j = 0 ;j < 5;j++)
			{
				infile>>npcSaved[i][j];//showflag,x,y,picno
			}
		}

		for(int i = 0 ; i < 255 ;i ++)
		{
			for(int j = 0 ;j < 5;j++)
			{
				infile>>sceneObjSaved[i][j];//showflag,x,y,picno
			}
		}

		if(versionno > 170116130)
		{
			for(int i = 0 ; i < 25 ; i ++)
			{
				infile>>magicMax[i];
			}

			for(int i = 0 ; i < 25 ; i ++)
			{
				for(int j = 0 ; j < magicMax[i] ; j ++)
				{
					infile>>magicSaved[i][j][0];
					infile>>magicSaved[i][j][1];
				}
			}

			for(int i = 0 ; i < 25 ; i ++)
			{
				infile>>magicExLearned[i];
			}
		}
		else
		{
			for(int i = 0 ; i < 4 ; i ++)
			{
				infile>>magicMax[i];
			}

			for(int i = 0 ; i < 4 ; i ++)
			{
				for(int j = 0 ; j < magicMax[i] ; j ++)
				{
					infile>>magicSaved[i][j][0];
					infile>>magicSaved[i][j][1];
				}
			}

			for(int i = 0 ; i < 4 ; i ++)
			{
				infile>>magicExLearned[i];
			}
		}
		infile>>missflag;
		infile>>throwflag;
		infile>>posEnable;
		infile>>timeEventNo;
		infile>>timeEventSecond;
		infile>>timeEventNum;

		if(versionno > 210627182)//1.82以上版本另外存储npc的faceTo step信息
		{
			for(int i = 0 ; i < 255 ;i ++)
			{
				for(int j = 5 ;j < 8;j++)
				{
					infile>>npcSaved[i][j];//faceTo step
				}
			}
		}

		infile.close();

	}
	else if(retval==1)
	{			
		char name[200];
		char name1[200];
		memset(name,0,sizeof(name));
		GetModuleFileName(NULL,name1,200); 
		(strrchr(name1, '\\'))[1] = 0;
		sprintf_s(name,"%s\\Res\\%s1.ssv",name1,gameName);

		ifstream infile(name,ios::in);
		infile>>gameName;
		infile>>sceneName;
		infile>>actorNum;//队列中角色数目
		for(int i = 0; i< actorNum;i++)
		{
			infile>>actorNo[i];
		}
		infile>>mapType;
		infile>>mapNo;
		infile>>mapX;
		infile>>mapY;
		infile>>scriptType;
		infile>>scriptNo;
		infile>>controlId;

		if(controlId > 100)//v1.30版本控制
		{
			versionno = controlId;
			infile>>controlId;
			for(int j = 0 ;j<25;j++)
			{
				for(int k = 0 ;k < 28;k++)
				{
					infile>>actorInfo[j][k];
				}
			}
		}
		else
		{
			for(int j = 0 ;j<4;j++)
			{
				for(int k = 0 ;k < 28;k++)
				{
					infile>>actorInfo[j][k];
				}
			}
		}
		infile>>money;
		infile>>goodListCount;
		for(int u = 0; u< goodListCount;u++)
		{
			infile>>goodList[u][0];
			infile>>goodList[u][1];
			infile>>goodList[u][2];
		}
		infile>>equipListCount;
		for(int u = 0; u< equipListCount;u++)
		{
			infile>>equipList[u][0];
			infile>>equipList[u][1];
			infile>>equipList[u][2];
		}
		for(int i = 0 ; i < 2400 ;i ++)
		{
			infile>>globalEvent[i];
		}

		for(int i = 0 ; i < 240 ;i ++)
		{
			infile>>globalValue[i];
		}

		infile>>saveEnable;
		infile>>fightEnable;

		for(int i = 0 ; i < 12 ;i ++)
		{
			infile>>fightSave[i];
		}

		for(int i = 0 ; i < 255 ;i ++)
		{
			for(int j = 0 ;j < 5;j++)
			{
				infile>>npcSaved[i][j];//showflag,x,y,picid
			}
		}

		for(int i = 0 ; i < 255 ;i ++)
		{
			for(int j = 0 ;j < 5;j++)
			{
				infile>>sceneObjSaved[i][j];//showflag,x,y,picno
			}
		}



		if(versionno > 170116130)
		{
			for(int i = 0 ; i < 25 ; i ++)
			{
				infile>>magicMax[i];
			}

			for(int i = 0 ; i < 25 ; i ++)
			{
				for(int j = 0 ; j < magicMax[i] ; j ++)
				{
					infile>>magicSaved[i][j][0];
					infile>>magicSaved[i][j][1];
				}
			}

			for(int i = 0 ; i < 25 ; i ++)
			{
				infile>>magicExLearned[i];
			}
		}
		else
		{
			for(int i = 0 ; i < 4 ; i ++)
			{
				infile>>magicMax[i];
			}

			for(int i = 0 ; i < 4 ; i ++)
			{
				for(int j = 0 ; j < magicMax[i] ; j ++)
				{
					infile>>magicSaved[i][j][0];
					infile>>magicSaved[i][j][1];
				}
			}

			for(int i = 0 ; i < 4 ; i ++)
			{
				infile>>magicExLearned[i];
			}
		}
		infile>>missflag;
		infile>>throwflag;
		infile>>posEnable;//20160811
		infile>>timeEventNo;
		infile>>timeEventSecond;
		infile>>timeEventNum;

		if(versionno > 210627182)//1.82以上版本另外存储npc的faceTo step信息
		{
			for(int i = 0 ; i < 255 ;i ++)
			{
				for(int j = 5 ;j < 8;j++)
				{
					infile>>npcSaved[i][j];//faceTo step
				}
			}
		}

		infile.close();
	}
	else if(retval==2)
	{			
		char name[200];
		char name1[200];
		memset(name,0,sizeof(name));
		GetModuleFileName(NULL,name1,200); 
		(strrchr(name1, '\\'))[1] = 0;
		sprintf_s(name,"%s\\Res\\%s2.ssv",name1,gameName);

		ifstream infile(name,ios::in);
		infile>>gameName;
		infile>>sceneName;
		infile>>actorNum;//队列中角色数目
		for(int i = 0; i< actorNum;i++)
		{
			infile>>actorNo[i];
		}
		infile>>mapType;
		infile>>mapNo;
		infile>>mapX;
		infile>>mapY;
		infile>>scriptType;
		infile>>scriptNo;
		infile>>controlId;

		if(controlId > 100)//v1.30版本控制
		{
			versionno = controlId;
			infile>>controlId;
			for(int j = 0 ;j<25;j++)
			{
				for(int k = 0 ;k < 28;k++)
				{
					infile>>actorInfo[j][k];
				}
			}
		}
		else
		{
			for(int j = 0 ;j<4;j++)
			{
				for(int k = 0 ;k < 28;k++)
				{
					infile>>actorInfo[j][k];
				}
			}
		}
		infile>>money;
		infile>>goodListCount;
		for(int u = 0; u< goodListCount;u++)
		{
			infile>>goodList[u][0];
			infile>>goodList[u][1];
			infile>>goodList[u][2];
		}
		infile>>equipListCount;
		for(int u = 0; u< equipListCount;u++)
		{
			infile>>equipList[u][0];
			infile>>equipList[u][1];
			infile>>equipList[u][2];
		}
		for(int i = 0 ; i < 2400 ;i ++)
		{
			infile>>globalEvent[i];
		}

		for(int i = 0 ; i < 240 ;i ++)
		{
			infile>>globalValue[i];
		}

		infile>>saveEnable;
		infile>>fightEnable;

		for(int i = 0 ; i < 12 ;i ++)
		{
			infile>>fightSave[i];
		}

		for(int i = 0 ; i < 255 ;i ++)
		{
			for(int j = 0 ;j < 5;j++)
			{
				infile>>npcSaved[i][j];//showflag,x,y,picid
			}
		}

		for(int i = 0 ; i < 255 ;i ++)
		{
			for(int j = 0 ;j < 5;j++)
			{
				infile>>sceneObjSaved[i][j];//showflag,x,y,picno
			}
		}

		if(versionno > 170116130)
		{
			for(int i = 0 ; i < 25 ; i ++)
			{
				infile>>magicMax[i];
			}

			for(int i = 0 ; i < 25 ; i ++)
			{
				for(int j = 0 ; j < magicMax[i] ; j ++)
				{
					infile>>magicSaved[i][j][0];
					infile>>magicSaved[i][j][1];
				}
			}

			for(int i = 0 ; i < 25 ; i ++)
			{
				infile>>magicExLearned[i];
			}
		}
		else
		{
			for(int i = 0 ; i < 4 ; i ++)
			{
				infile>>magicMax[i];
			}

			for(int i = 0 ; i < 4 ; i ++)
			{
				for(int j = 0 ; j < magicMax[i] ; j ++)
				{
					infile>>magicSaved[i][j][0];
					infile>>magicSaved[i][j][1];
				}
			}

			for(int i = 0 ; i < 4 ; i ++)
			{
				infile>>magicExLearned[i];
			}
		}
		infile>>missflag;
		infile>>throwflag;
		infile>>posEnable;
		infile>>timeEventNo;
		infile>>timeEventSecond;
		infile>>timeEventNum;

		if(versionno > 210627182)//1.82以上版本另外存储npc的faceTo step信息
		{
			for(int i = 0 ; i < 255 ;i ++)
			{
				for(int j = 5 ;j < 8;j++)
				{
					infile>>npcSaved[i][j];//faceTo step
				}
			}
		}

		infile.close();
	}

	return 0;

}

void Save::ShowHeadAndSceneName()
{
	for(int i = 0;i<3;i++)
	{
		if(_actorNum[i] >0)
		{
			ShowText(_sceneName[i],67,i*22+29);
			for(int j = 0 ;j< _actorNum[i];j++)
			{
				ShowHeadPic(0,_actorNo[i][j],12+j*16,i*22+24,6);
			}
		}
		else
		{
			ShowText("空档案",67,i*22+29);
			//ShowHeadPic(0,1,12,i*22+28,6);

		}
	}



}

void Save::Write(int retval)
{
	wlogfile(1, __FUNCTION__ ,"saveid=",retval);
	//int i,j;
	char name[200];
	char name1[200];
	memset(name,0,sizeof(name));
	GetModuleFileName(NULL,name1,200); 
	(strrchr(name1, '\\'))[1] = 0;
	if(retval==0)
	{			
		//sprintf_s(name,"E:\\SkyRPGFactory\\SkySimulater\\Res\\%s0.ssv",gameName);

		sprintf_s(name,"%s\\Res\\%s0.ssv",name1,gameName);
	}
	else if(retval==1)
	{			
		sprintf_s(name,"%s\\Res\\%s1.ssv",name1,gameName);
	}
	else if(retval==2)
	{			
		sprintf_s(name,"%s\\Res\\%s2.ssv",name1,gameName);
	}
	ofstream outfile(name,ios::out);
	outfile<<gameName;
	outfile<<"\n";
	outfile<<sceneName;
	outfile<<"\n";
	outfile<<actorNum;//队列中角色数目
	outfile<<"\n";
	for(int i = 0; i< actorNum;i++)
	{
		outfile<<actorNo[i];
		outfile<< " ";
	}
	outfile<<"\n";
	outfile<<mapType;
	outfile<<" ";
	outfile<<mapNo;
	outfile<<" ";
	outfile<<mapX;
	outfile<<" ";
	outfile<<mapY;
	outfile<<"\n";
	outfile<<scriptType;
	outfile<<" ";
	outfile<<scriptNo;
	outfile<<"\n";
	outfile<<versionno;
	outfile<<"\n";
	outfile<<controlId;
	outfile<<"\n";

	for(int j = 0 ;j<25;j++)
	{
		for(int k = 0 ;k < 28;k++)
		{
			outfile<<actorInfo[j][k];
			outfile<<" ";
		}
		outfile<<"\n";
	}
	outfile<<money;
	outfile<<"\n";

	outfile<<goodListCount;
	outfile<<"\n";
	for(int u = 0; u< goodListCount;u++)
	{
		outfile<<goodList[u][0];
		outfile<<" ";
		outfile<<goodList[u][1];
		outfile<<" ";
		outfile<<goodList[u][2];
		outfile<<"\n";
	}
	outfile<<equipListCount;
	outfile<<"\n";
	for(int u = 0; u< equipListCount;u++)
	{
		outfile<<equipList[u][0];
		outfile<<" ";
		outfile<<equipList[u][1];
		outfile<<" ";
		outfile<<equipList[u][2];
		outfile<<"\n";
	}



	for(int i = 0 ; i < 2400 ;i ++)
	{
		outfile<<globalEvent[i];
		outfile<<" ";
	}
	outfile<<"\n";

	for(int i = 0 ; i < 240 ;i ++)
	{
		outfile<<globalValue[i];
		outfile<<" ";
	}
	outfile<<"\n";

	outfile<<saveEnable;
	outfile<<" ";
	outfile<<fightEnable;
	outfile<<" ";
	for(int i =0 ; i < 12 ; i ++)
	{
		outfile<<fightSave[i];//初始化战争信息
		outfile<<" ";
	}
	outfile<<"\n";
	for(int i =0 ; i < 255 ; i ++)
	{
		for(int j =0 ; j < 5 ; j ++)
		{

			outfile<<npcSaved[i][j];
			outfile<<" ";
		}
		outfile<<"\n";
	}

	for(int i =0 ; i < 255 ; i ++)
	{
		for(int j =0 ; j < 5 ; j ++)
		{

			outfile<<sceneObjSaved[i][j];
			outfile<<" ";
		}
		outfile<<"\n";
	}

	for(int i = 0 ; i < 25 ; i ++)
	{
		outfile<<magicMax[i];
		outfile<<" ";
	}
	outfile<<"\n";

	for(int i = 0 ; i < 25 ; i ++)
	{
		for(int j = 0 ; j < magicMax[i] ; j ++)
		{
			outfile<<magicSaved[i][j][0];
			outfile<<" ";
			outfile<<magicSaved[i][j][1];
			outfile<<"\n";
		}
		//outfile<<"\n";
	}
	//outfile<<"\n";

	for(int i = 0 ; i < 25 ; i ++)
	{
		outfile<<magicExLearned[i];
		outfile<<" ";
	}
	outfile<<"\n";
	outfile<<missflag;
	outfile<<"\n";
	outfile<<throwflag;
	outfile<<"\n";
	outfile<<posEnable;//20160811
	outfile<<"\n";
	outfile<<timeEventNo;
	outfile<<"\n";
	outfile<<timeEventSecond;
	outfile<<"\n";
	outfile<<timeEventNum;
	outfile<<"\n";

	for(int i =0 ; i < 255 ; i ++)
	{
		for(int j =5 ; j < 8 ; j ++)
		{

			outfile<<npcSaved[i][j];
			outfile<<" ";
		}
		outfile<<"\n";
	}

	outfile.close();


}

int Save::IsVoid(int num)
{
	char name[200];
	char name1[200];
	memset(name,0,sizeof(name));
	GetModuleFileName(NULL,name1,200); 
	(strrchr(name1, '\\'))[1] = 0;
	sprintf_s(name,"%s\\Res\\%s%d.ssv",name1,gameName,num);

	FILE * fp;

	fp=fopen(name,"r");

	if(fp==NULL)
	{
		//fclose(fp);//20160625
		return 1;
	}
	else
	{
		fclose(fp);
		return 0;
	}
}

