#define COMMON_LOADSAVE_H aaa

#ifndef UTIL_STATISTICS_H
    #include "maps\util_statistics.lgc"
#endif

//------------------------------------------------------------------------------------------
//используются только в магазине
static int PlayerAmmo[11];
static int PlayerWeapon[11];

//------------------------------------------------------------------------------------------
ClearDataForNewGame(int iGameType);
ReloadVidAndSetVidParameters();
LoadCurData();
GetCurUserName();


//------------------------------------------------------------------------------------------
GetLevNumForSaves(bool bShop, int iLevNum)
{
	int ret = (iLevNum-1)*2+1;
	if(bShop)
		ret--;
	return ret;
}


//------------------------------------------------------------------------------------------
BeginNewGame(int iGameType, string sPlayerName, int iDifficultyLevel)
{
	if(strlen(sPlayerName) == 0)
		sPlayerName = GetString("menu","DefaultName");
	Log2("GameType" + itoa(iGameType));
	//
	Call_s(EXT_SetCurUserOrAddNew, sPlayerName);
	Log2("bng EXT_SetCurUserOrAddNew - " + sPlayerName);
	//
	ClearDataForNewGame(iGameType);
	//
	giGame_Type = iGameType;
	SetValI2(true, "GameType",iGameType);
	SetValS2(true, "PlayerSex",gsPlayer_Sex);
	SetValI2(false, "DifficultyLevel",iDifficultyLevel);
	//
	if(giGame_Type == GAME_TYPE_SURVIVE)
		Call_i(EXT_SaveLevel, 777);
	else
	if(giGame_Type == GAME_TYPE_GUN_STAND)
		Call_i(EXT_SaveLevel, 999);
	else
	{
		int iLev = 1;
		if(DEMO_VERSION)
			iLev = 3;
		Call_i(EXT_SaveLevel, GetLevNumForSaves(true, iLev));
	}
	//
	ReloadVidAndSetVidParameters();
	//
	SetGlobValS("PlayerNameLast", sPlayerName);
	SetGlobValS("PlayerSexLast",gsPlayer_Sex);
	SetGlobValI("DifficultyLevelLast",iDifficultyLevel);
	//
	if(giGame_Type == GAME_TYPE_COMPAIGN || giGame_Type == GAME_TYPE_ADDON || giGame_Type == GAME_TYPE_ADDON2 )
	{
		Load("maps\\shop.map");
	}
	else
	if(giGame_Type == GAME_TYPE_SURVIVE)
	{
		SetValI2(false, "LevelNumber",-10);
		Load("maps\\survive_01.map");
	}
    else if(giGame_Type == GAME_TYPE_GUN_STAND)
	{
		SetValI2(false, "LevelNumber",-10);
		Load("maps\\survive_02.map");
	}
}


//------------------------------------------------------------------------------------------
//перегрузка всех VID т.к. в процессе предыдущих игр они могли поменяться
ClearDataForNewGame(int iGameType)
{
	Call_v(EXT_ClearLevel);
	//
	SetValI2(false, "LevelNumber",1);
	SetValI2(true, "PlayerMoney",LevelMoney[0]);
	SetValI2(true, "PlayerScores",0);
	SetValI2(true, "giExpa",0);
	SetValI2(true, "giExpLevels",0);
	//
	int i;
	for( i = 0; i < WPN_CNT; i++ )
	{
		SetValI2(true, "PlayerWeapon"+itoa(i),0);
		SetValI2(true, "PlayerAmmo"+itoa(i),0);
		SetValI2(true, "WpnUpgrade"+itoa(i),0);
	}
	SetValI2(true, "PlayerWeapon1",1);
	SetValI2(true, "PlayerAmmo1",999999);
	//
	for( i = 0; i< ITEM_CNT; i++ )
		SetValI2(true, "PlayerItem"+itoa(i),0);
	SetValI2(true, "PlayerItem"+itoa(7),2);//PlayerLife
	//
	for(i = 0; i < SKILLS_CNT; i++)
		SetValI2(true, "giSkills"+itoa(i),0);
	//
	ResetFullRankInRegistry();
	//
	if(DEMO_VERSION && iGameType == GAME_TYPE_COMPAIGN)
	{
		SetValI2(false, "LevelNumber",3);
		SetValI2(true, "PlayerMoney",1500);
		SetValI2(true, "giExpLevels",2);
		SetValI2(true, "PlayerWeapon"+itoa(2),1);
		SetValI2(true, "PlayerAmmo"+itoa(2),100);
		SetValI2(true, "PlayerItem"+itoa(8),1);
	}
}


//------------------------------------------------------------------------------------------
OnContinueGame()
{
	Log2("OnContinueGame()");
	int curUser = Call_v(EXT_GetCurUserG);
	if(curUser >= 0)
		Log2(Call_i(EXT_GetUsrNameG, curUser));
	//
	LoadCurData();
	ReloadVidAndSetVidParameters();
	//для статистики
//	InitStatIfNeeded();
//	for( i = 0; i < VSTAT_MCNT; i++ )
//		SetReg(GetDefaultRegPath(),VSTAT_NAMES[i],GetReg(load_path,VSTAT_NAMES[i],0));
}


//------------------------------------------------------------------------------------------
LoadCurData()
{
	int curUser = Call_v(EXT_GetCurUserG);
	if(curUser < 0 )
		Call_s(EXT_SetCurUserOrAddNew, GetGlobValS("PlayerNameLast",GetString("menu","DefaultName")));
	//
	LevelNumber = GetValI2(false, "LevelNumber",1);
#ifdef CUR_LEVEL_NUM
    LevelNumber = CUR_LEVEL_NUM;
#endif
	//
	int iAbsLevNum = GetLevNumForSaves(false, LevelNumber);

#ifdef SHOP_MAP
	iAbsLevNum = GetLevNumForSaves(true, LevelNumber);
#endif
#ifdef SURVIVE_MAP
	iAbsLevNum = 777;
#endif
#ifdef NEW_SURVIVE
	iAbsLevNum = 999;
#endif
	Call_i(EXT_LoadLevel, iAbsLevNum);

//#ifdef SHOP_MAP
//	LevelNumber = GetValI2(false, "LevelNumber",1);
//#endif

	giGame_Type			= GetValI2(true, "GameType",GAME_TYPE_COMPAIGN);
	gbState_Bar			= GetValI2(false, "StateBar",1);
	gsPlayer_Sex		= GetValS2(true, "PlayerSex","male");
	PlayerMoney			= GetValI2(true, "PlayerMoney",0);
	PlayerScores		= GetValI2(true, "PlayerScores",0);
	giExpa				= GetValI2(true, "giExpa",0);
	giExpLevels			= GetValI2(true, "giExpLevels",0);
	PlayerName			= GetCurUserName();
	//
	Log2("LoadCurData()" + PlayerName + ". LevelNumber = " + itoa(LevelNumber));
	//
	int i;
	for(i = 0; i < SKILLS_CNT; i++)
		giSkills[i] = GetValI2(true, "giSkills"+itoa(i),0);
	//
	//if(Z_DEBUG)
	//	giExpLevels = 10;
}


//------------------------------------------------------------------------------------------
#define SCD_SHOP     1
#define SCD_WIN      2
#define SCD_ONDEAD   3

//------------------------------------------------------------------------------------------
SaveCurData2(int iType)
{
  int    i;
  //
  if(iType == SCD_WIN)
  {
    Log3("giExpa for Level" + itoa(LevelNumber-1) + "= " + itoa(giExpa));
  }
  //
  if(iType != SCD_ONDEAD)
	  SetValI2(false, "LevelNumber",LevelNumber);
  SetValI2(true, "PlayerMoney",PlayerMoney);
  SetValI2(true, "PlayerScores",PlayerScores);
  SetValI2(true, "giExpa",giExpa);
  SetValI2(true, "giExpLevels",giExpLevels);
  //
  for(i = 0; i < SKILLS_CNT; i++)
    SetValI2(true, "giSkills"+itoa(i),giSkills[i]);
  //
  if(iType == SCD_SHOP)
  {
    for( i = 0; i < WPN_CNT; i++ )
    {
      SetValI2(true, "PlayerAmmo"+itoa(i), PlayerAmmo[i] );
      SetValI2(true, "PlayerWeapon"+itoa(i), PlayerWeapon[i]);
      SetValI2(true, "WpnUpgrade"+itoa(i), WpnUpgrade[i]);
    }
    for( i = 0; i < ITEM_CNT; i++ )
      SetValI2(true, "PlayerItem"+itoa(i),PlayerItem[i]);
  }
  else
  if( Flagman(0) )
  {
    for( i = 0; i < WPN_CNT; i++ )
    {
      SetValI2(true, "PlayerAmmo"+itoa(i), Action(Flagman(0),ACT_GET_AMMO,i) );
      SetValI2(true, "PlayerWeapon"+itoa(i),Action(Flagman(0),ACT_HAVE_ITEM,260+i));
      SetValI2(true, "WpnUpgrade"+itoa(i), WpnUpgrade[i]);
    }
    PlayerItem[730-720] = Action(Flagman(0),ACT_HAVE_ITEM,230);//аптечка спасательная
/*    for( i = 0; i < 3; i++ )//бронежилеты
      if( GetVidData(200+i,VID_COUNT) )
        PlayerItem[4+i] = 1;
      else
        PlayerItem[4+i] = 0;*/
    if( GetVidData(290,VID_COUNT) )//дрон
      PlayerItem[11] = 1;
    else
      PlayerItem[11] = 0;
    PlayerItem[4+0] = PlayerItem[4+1] = PlayerItem[4+2] = 0;//PlayerItem[730-720] = PlayerItem[11] = 0;
    for( i = 0; i < ITEM_CNT; i++ )
      SetValI2(true, "PlayerItem"+itoa(i),PlayerItem[i]);
  }
  //
    //zmNEW
//    InitStatIfNeeded();
  //  for( i = 0; i < VSTAT_MCNT; i++ )
    //    SetReg(save_path,VSTAT_NAMES[i],GetReg(GetDefaultRegPath(),VSTAT_NAMES[i],0));
	//
	if(iType != SCD_ONDEAD)
	{
		Call_i(EXT_SaveLevel, GetLevNumForSaves(iType != SCD_SHOP, LevelNumber));
		Call_v(EXT_FlushUserData);
	}
}


//------------------------------------------------------------------------------------------
GetCurUserName()
{
	string ret = "";
	int i = Call_v(EXT_GetCurUserG);
	if(i>=0)
		ret = Call_i(EXT_GetUsrNameG, i);
	//
	return ret;
}

