Move cached static galaxy object from Game to GalaxyGenerator.

master
Lars W. (lwho) 2014-09-23 22:06:10 +02:00
parent 386a98e48d
commit b495d72ea0
7 changed files with 41 additions and 62 deletions

View File

@ -31,31 +31,8 @@ static const int s_saveVersion = 78;
static const char s_saveStart[] = "PIONEER";
static const char s_saveEnd[] = "END";
RefCountedPtr<Galaxy> Game::s_galaxy;
//static
void Game::InitGalaxy()
{
s_galaxy = GalaxyGenerator::Create();
s_galaxy->Init();
}
//static
void Game::UninitGalaxy()
{
s_galaxy.Reset();
}
//static
void Game::DumpGalaxy(FILE* file, Sint32 centerX, Sint32 centerY, Sint32 centerZ, Sint32 radius)
{
if (!s_galaxy)
InitGalaxy();
s_galaxy->Dump(file, centerX, centerY, centerZ, radius);
}
Game::Game(const SystemPath &path, double time) :
m_galaxy(s_galaxy),
m_galaxy(GalaxyGenerator::Create()),
m_time(time),
m_state(STATE_NORMAL),
m_wantHyperspace(false),
@ -63,13 +40,6 @@ Game::Game(const SystemPath &path, double time) :
m_requestedTimeAccel(TIMEACCEL_1X),
m_forceTimeAccel(false)
{
if (m_galaxy)
m_galaxy->FlushCaches();
if (!m_galaxy || !m_galaxy->GetGenerator()->IsDefault()) {
m_galaxy = s_galaxy = GalaxyGenerator::Create();
m_galaxy->Init();
}
// Now that we have a Galaxy, check the starting location
if (!path.IsBodyPath())
throw InvalidGameStartLocation("SystemPath is not a body path");
@ -140,7 +110,6 @@ Game::~Game()
}
Game::Game(Serializer::Reader &rd) :
m_galaxy(s_galaxy),
m_timeAccel(TIMEACCEL_PAUSED),
m_requestedTimeAccel(TIMEACCEL_PAUSED),
m_forceTimeAccel(false)
@ -157,10 +126,6 @@ Game::Game(Serializer::Reader &rd) :
throw SavedGameWrongVersionException();
}
// XXX This must be done after loading sectors once we can change them in game
if (m_galaxy)
m_galaxy->FlushCaches();
Serializer::Reader section;
// Preparing the Lua stuff
@ -171,13 +136,10 @@ Game::Game(Serializer::Reader &rd) :
section = rd.RdSection("GalaxyGen");
std::string genName = section.String();
GalaxyGenerator::Version genVersion = section.Int32();
if (!m_galaxy || genName != m_galaxy->GetGeneratorName() || genVersion != m_galaxy->GetGeneratorVersion()) {
m_galaxy = s_galaxy = GalaxyGenerator::Create(genName, genVersion);
if (!m_galaxy) {
Output("can't load savefile, unsupported galaxy generator %s, version %d\n", genName.c_str(), genVersion);
throw SavedGameWrongVersionException();
}
m_galaxy->Init();
m_galaxy = GalaxyGenerator::Create(genName, genVersion);
if (!m_galaxy) {
Output("can't load savefile, unsupported galaxy generator %s, version %d\n", genName.c_str(), genVersion);
throw SavedGameWrongVersionException();
}
// game state

View File

@ -41,10 +41,6 @@ class ObjectViewerView;
class Game {
public:
static void InitGalaxy();
static void UninitGalaxy();
static void DumpGalaxy(FILE* file, Sint32 centerX, Sint32 centerY, Sint32 centerZ, Sint32 radius);
// LoadGame and SaveGame throw exceptions on failure
static Game *LoadGame(const std::string &filename);
// XXX game arg should be const, and this should probably be a member function
@ -188,7 +184,6 @@ private:
TimeAccel m_requestedTimeAccel;
bool m_forceTimeAccel;
static RefCountedPtr<Galaxy> s_galaxy;
static const float s_timeAccelRates[];
static const float s_timeInvAccelRates[];
};

View File

@ -465,9 +465,10 @@ void Pi::Init(const std::map<std::string,std::string> &options, bool no_gui)
draw_progress(gauge, label, 0.1f);
if (config->HasEntry("GalaxyGenerator"))
GalaxyGenerator::SetDefaultGenerator(config->String("GalaxyGenerator"),
GalaxyGenerator::Init(config->String("GalaxyGenerator"),
config->Int("GalaxyGeneratorVersion", GalaxyGenerator::LAST_VERSION));
Game::InitGalaxy();
else
GalaxyGenerator::Init();
draw_progress(gauge, label, 0.2f);
@ -662,7 +663,7 @@ void Pi::Quit()
delete Pi::modelCache;
delete Pi::renderer;
delete Pi::config;
Game::UninitGalaxy();
GalaxyGenerator::Uninit();
delete Pi::planner;
SDL_Quit();
FileSystem::Uninit();

View File

@ -17,6 +17,7 @@ class Galaxy : public RefCounted {
private:
friend class GalaxyGenerator;
Galaxy(RefCountedPtr<GalaxyGenerator> galaxyGenerator);
void Init();
public:
// lightyears
@ -26,8 +27,6 @@ public:
~Galaxy();
void Init();
/* 0 - 255 */
Uint8 GetSectorDensity(const int sx, const int sy, const int sz) const;
FactionsDatabase* GetFactions() { return &m_factions; } // XXX const correctness

View File

@ -11,6 +11,22 @@ static const GalaxyGenerator::Version LAST_VERSION_LEGACY = 0;
std::string GalaxyGenerator::s_defaultGenerator = "legacy";
GalaxyGenerator::Version GalaxyGenerator::s_defaultVersion = LAST_VERSION_LEGACY;
RefCountedPtr<Galaxy> GalaxyGenerator::s_galaxy;
//static
void GalaxyGenerator::Init(const std::string& name, Version version)
{
s_defaultGenerator = name;
s_defaultVersion = (version == LAST_VERSION) ? GetLastVersion(name) : version;
GalaxyGenerator::Create(); // This will set s_galaxy
}
//static
void GalaxyGenerator::Uninit()
{
s_galaxy->FlushCaches();
s_galaxy.Reset();
}
//static
GalaxyGenerator::Version GalaxyGenerator::GetLastVersion(const std::string& name)
@ -21,21 +37,21 @@ GalaxyGenerator::Version GalaxyGenerator::GetLastVersion(const std::string& name
return LAST_VERSION;
}
//static
void GalaxyGenerator::SetDefaultGenerator(const std::string& name, Version version) {
s_defaultGenerator = name;
s_defaultVersion = (version == LAST_VERSION) ? GetLastVersion(name) : version;
}
// static
RefCountedPtr<Galaxy> GalaxyGenerator::Create(const std::string& name, Version version)
{
if (version == LAST_VERSION)
version = GetLastVersion(name);
if (s_galaxy && name == s_galaxy->GetGeneratorName() && version == s_galaxy->GetGeneratorVersion()) {
s_galaxy->FlushCaches();
return s_galaxy;
}
if (name == "legacy") {
Output("Creating new galaxy with generator '%s' version %d\n", name.c_str(), version);
if (version == 0) {
return RefCountedPtr<Galaxy>(new Galaxy(RefCountedPtr<GalaxyGenerator>(
s_galaxy = RefCountedPtr<Galaxy>(new Galaxy(RefCountedPtr<GalaxyGenerator>(
(new GalaxyGenerator(name, version))
->AddSectorStage(new SectorCustomSystemsGenerator(CustomSystem::CUSTOM_ONLY_RADIUS))
->AddSectorStage(new SectorRandomSystemsGenerator)
@ -43,6 +59,8 @@ RefCountedPtr<Galaxy> GalaxyGenerator::Create(const std::string& name, Version v
->AddStarSystemStage(new StarSystemCustomGenerator)
->AddStarSystemStage(new StarSystemRandomGenerator)
->AddStarSystemStage(new PopulateStarSystemGenerator))));
s_galaxy->Init();
return s_galaxy;
}
}
Output("Galaxy generation failed: Unknown generator '%s' version %d\n", name.c_str(), version);

View File

@ -19,6 +19,9 @@ public:
typedef int Version;
static const Version LAST_VERSION = -1;
static void Init(const std::string& name = std::string("legacy"), Version version = LAST_VERSION);
static void Uninit();
static RefCountedPtr<Galaxy> Create(const std::string& name, Version version = LAST_VERSION);
static RefCountedPtr<Galaxy> Create() {
return Create(s_defaultGenerator, s_defaultVersion);
@ -27,7 +30,6 @@ public:
static std::string GetDefaultGeneratorName() { return s_defaultGenerator; }
static Version GetDefaultGeneratorVersion() { return s_defaultVersion; }
static Version GetLastVersion(const std::string& name);
static void SetDefaultGenerator(const std::string& name, Version version = LAST_VERSION);
virtual ~GalaxyGenerator();
@ -67,6 +69,7 @@ private:
std::list<SectorGeneratorStage*> m_sectorStage;
std::list<StarSystemGeneratorStage*> m_starSystemStage;
static RefCountedPtr<Galaxy> s_galaxy;
static std::string s_defaultGenerator;
static Version s_defaultVersion;
};

View File

@ -134,7 +134,8 @@ start:
Output("pioneer: could not open \"%s\" for writing: %s\n", filename.c_str(), strerror(errno));
break;
}
Game::DumpGalaxy(file, sx, sy, sz, radius);
RefCountedPtr<Galaxy> galaxy = GalaxyGenerator::Create();
galaxy->Dump(file, sx, sy, sz, radius);
if (filename != "-" && fclose(file) != 0) {
Output("pioneer: writing to \"%s\" failed: %s\n", filename.c_str(), strerror(errno));
}