Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
savegame.h File Reference
#include "gfx/vec.h"
#include <string>
#include <set>
#include <vector>
#include "SharedPool.h"

Go to the source code of this file.

Classes

struct  SavedUnits
 
class  SaveGame
 

Functions

void WriteSaveGame (class Cockpit *cp, bool auto_save)
 
const std::string & GetCurrentSaveGame ()
 
std::string SetCurrentSaveGame (std::string newname)
 
const std::string & GetSaveDir ()
 
void CopySavedShips (std::string filename, int player_num, const std::vector< std::string > &starships, bool load)
 

Function Documentation

void CopySavedShips ( std::string  filename,
int  player_num,
const std::vector< std::string > &  starships,
bool  load 
)

Definition at line 276 of file savegame.cpp.

References VSFileSystem::CreateDirectoryHome(), e, f, GetWritePlayerSaveGame(), VSFileSystem::Ok, VSFileSystem::VSFile::OpenCreateWrite(), VSFileSystem::VSFile::OpenReadOnly(), VSFileSystem::VSFile::ReadFull(), VSFileSystem::savedunitpath, VSFileSystem::UnitFile, VSFileSystem::UnitSaveFile, and VSFileSystem::VSFile::Write().

Referenced by bootstrap_main_loop(), NetClient::startGame(), Cockpit::Update(), and WriteSaveGame().

277 {
278  for (unsigned int i = 0; i < starships.size(); i += 2) {
279  if (i == 2) i = 1;
280  VSFile src, dst;
281  string srcnam = filename;
282  string dstnam = GetWritePlayerSaveGame( player_num );
283  string tmp;
284  if (load) {
285  tmp = srcnam;
286  srcnam = dstnam;
287  dstnam = tmp;
288  }
289  VSError e = src.OpenReadOnly( srcnam+"/"+starships[i]+".csv", UnitSaveFile );
290  if (e <= Ok) {
292  VSError f = dst.OpenCreateWrite( dstnam+"/"+starships[i]+".csv", UnitFile );
293  if (f <= Ok) {
294  string srcdata = src.ReadFull();
295  dst.Write( srcdata );
296  } else {
297  printf( "Error: Cannot Copy Unit %s from save file %s to %s\n",
298  starships[i].c_str(),
299  srcnam.c_str(),
300  dstnam.c_str() );
301  }
302  } else {
303  printf( "Error: Cannot Open Unit %s from save file %s.\n",
304  starships[i].c_str(),
305  srcnam.c_str() );
306  }
307  }
308 }
const std::string& GetCurrentSaveGame ( )

Definition at line 998 of file savegame.cpp.

References CurrentSaveGameName.

Referenced by UniverseUtil::getCurrentSaveGame().

999 {
1000  return CurrentSaveGameName;
1001 }
const std::string& GetSaveDir ( )

Definition at line 1010 of file savegame.cpp.

References VSFileSystem::homedir.

Referenced by UniverseUtil::getSaveDir().

1011 {
1012  static string rv = VSFileSystem::homedir+"/save/";
1013  return rv;
1014 }
std::string SetCurrentSaveGame ( std::string  newname)
void WriteSaveGame ( class Cockpit cp,
bool  auto_save 
)

Definition at line 310 of file savegame.cpp.

References _Universe, Universe::AccessCockpit(), Cockpit::activeStarSystem, CopySavedShips(), Cockpit::credits, StarSystem::getFileName(), Unit::GetHull(), Cockpit::GetSaveParent(), Cockpit::GetUnitModifications(), GetWritePlayerSaveGame(), Unit::LocalPosition(), Universe::numPlayers(), Cockpit::PackUnitInfo(), Cockpit::savegame, SaveGame::SetPlayerLocation(), SaveGame::SetSavedCredits(), SaveGame::SetStarSystem(), SaveGame::WriteSaveGame(), and Unit::WriteUnit().

Referenced by BaseComputer::actionConfirmedSaveGame(), bootstrap_main_loop(), buyShip(), and UniverseUtil::saveGame().

311 {
312  int player_num = 0;
313  for (unsigned int kk = 0; kk < _Universe->numPlayers(); ++kk)
314  if (_Universe->AccessCockpit( kk ) == cp)
315  player_num = kk;
316  Unit *un = cp->GetSaveParent();
317  if (!un)
318  return;
319  if (un->GetHull() > 0) {
320  vector< string > packedInfo;
321  cp->PackUnitInfo(packedInfo);
322 
323  cp->savegame->WriteSaveGame( cp->activeStarSystem->getFileName().c_str(),
324  un->LocalPosition(), cp->credits, packedInfo, auto_save ? -1 : player_num );
325  un->WriteUnit( cp->GetUnitModifications().c_str() );
326  if (GetWritePlayerSaveGame( player_num ).length() && !auto_save) {
327  cp->savegame->SetSavedCredits( _Universe->AccessCockpit()->credits );
328  cp->savegame->SetStarSystem( cp->activeStarSystem->getFileName() );
329  cp->savegame->SetPlayerLocation( un->LocalPosition() );
330  CopySavedShips( cp->GetUnitModifications(), player_num, packedInfo, false );
331  }
332  }
333 }