Game::Game(const SystemPath &path, const vector3d &pos, double time) : m_time(time), m_state(STATE_NORMAL), m_wantHyperspace(false), m_timeAccel(TIMEACCEL_1X), m_requestedTimeAccel(TIMEACCEL_1X), m_forceTimeAccel(false) { Pi::FlushCaches(); if (!Pi::GetGalaxy()->GetGenerator().IsDefault()) Pi::CreateGalaxy(); m_space.reset(new Space(this, path)); Body *b = m_space->FindBodyForPath(&path); assert(b); m_player.reset(new Player("kanara")); m_space->AddBody(m_player.get()); m_player->SetFrame(b->GetFrame()); m_player->SetPosition(pos); m_player->SetVelocity(vector3d(0,0,0)); Polit::Init(); CreateViews(); EmitPauseState(IsPaused()); }
Game::Game(const SystemPath &path, double time) : m_time(time), m_state(STATE_NORMAL), m_wantHyperspace(false), m_timeAccel(TIMEACCEL_1X), m_requestedTimeAccel(TIMEACCEL_1X), m_forceTimeAccel(false) { Pi::FlushCaches(); if (!Pi::GetGalaxy()->GetGenerator().IsDefault()) Pi::CreateGalaxy(); m_space.reset(new Space(this, path)); SpaceStation *station = static_cast<SpaceStation*>(m_space->FindBodyForPath(&path)); assert(station); m_player.reset(new Player("kanara")); m_space->AddBody(m_player.get()); m_player->SetFrame(station->GetFrame()); m_player->SetDockedWith(station, 0); Polit::Init(); CreateViews(); EmitPauseState(IsPaused()); }
Game::Game(const SystemPath &path, double time) : m_galaxy(GalaxyGenerator::Create()), m_time(time), m_state(STATE_NORMAL), m_wantHyperspace(false), m_timeAccel(TIMEACCEL_1X), m_requestedTimeAccel(TIMEACCEL_1X), m_forceTimeAccel(false) { // Now that we have a Galaxy, check the starting location if (!path.IsBodyPath()) throw InvalidGameStartLocation("SystemPath is not a body path"); RefCountedPtr<const Sector> s = m_galaxy->GetSector(path); if (size_t(path.systemIndex) >= s->m_systems.size()) { char buf[128]; std::sprintf(buf, "System %u in sector <%d,%d,%d> does not exist", unsigned(path.systemIndex), int(path.sectorX), int(path.sectorY), int(path.sectorZ)); throw InvalidGameStartLocation(std::string(buf)); } RefCountedPtr<StarSystem> sys = m_galaxy->GetStarSystem(path); if (path.bodyIndex >= sys->GetNumBodies()) { char buf[256]; std::sprintf(buf, "Body %d in system <%d,%d,%d : %d ('%s')> does not exist", unsigned(path.bodyIndex), int(path.sectorX), int(path.sectorY), int(path.sectorZ), unsigned(path.systemIndex), sys->GetName().c_str()); throw InvalidGameStartLocation(std::string(buf)); } m_space.reset(new Space(this, m_galaxy, path)); Body *b = m_space->FindBodyForPath(&path); assert(b); m_player.reset(new Player("kanara")); m_space->AddBody(m_player.get()); m_player->SetFrame(b->GetFrame()); if (b->GetType() == Object::SPACESTATION) { m_player->SetDockedWith(static_cast<SpaceStation*>(b), 0); } else { const SystemBody *sbody = b->GetSystemBody(); m_player->SetPosition(vector3d(0, 1.5*sbody->GetRadius(), 0)); m_player->SetVelocity(vector3d(0,0,0)); } Polit::Init(m_galaxy); CreateViews(); EmitPauseState(IsPaused()); }
Game::Game(Serializer::Reader &rd) : m_timeAccel(TIMEACCEL_PAUSED), m_requestedTimeAccel(TIMEACCEL_PAUSED), m_forceTimeAccel(false) { // signature check for (Uint32 i = 0; i < strlen(s_saveStart)+1; i++) if (rd.Byte() != s_saveStart[i]) throw SavedGameCorruptException(); // version check rd.SetStreamVersion(rd.Int32()); Output("savefile version: %d\n", rd.StreamVersion()); if (rd.StreamVersion() != s_saveVersion) { Output("can't load savefile, expected version: %d\n", s_saveVersion); throw SavedGameWrongVersionException(); } // XXX This must be done after loading sectors once we can change them in game Pi::FlushCaches(); Serializer::Reader section; // Preparing the Lua stuff LuaRef::InitLoad(); Pi::luaSerializer->InitTableRefs(); // galaxy generator section = rd.RdSection("GalaxyGen"); std::string genName = section.String(); GalaxyGenerator::Version genVersion = section.Int32(); if (genName != Pi::GetGalaxy()->GetGeneratorName() || genVersion != Pi::GetGalaxy()->GetGeneratorVersion()) { if (!Pi::CreateGalaxy(genName, genVersion)) { Output("can't load savefile, unsupported galaxy generator %s, version %d\n", genName.c_str(), genVersion); throw SavedGameWrongVersionException(); } } // game state section = rd.RdSection("Game"); m_time = section.Double(); m_state = State(section.Int32()); m_wantHyperspace = section.Bool(); m_hyperspaceProgress = section.Double(); m_hyperspaceDuration = section.Double(); m_hyperspaceEndTime = section.Double(); // space, all the bodies and things section = rd.RdSection("Space"); m_space.reset(new Space(this, section, m_time)); m_player.reset(static_cast<Player*>(m_space->GetBodyByIndex(section.Int32()))); assert(!m_player->IsDead()); // Pioneer does not support necromancy // space transition state section = rd.RdSection("HyperspaceClouds"); // hyperspace clouds being brought over from the previous system Uint32 nclouds = section.Int32(); for (Uint32 i = 0; i < nclouds; i++) m_hyperspaceClouds.push_back(static_cast<HyperspaceCloud*>(Body::Unserialize(section, 0))); // system political stuff section = rd.RdSection("Polit"); Polit::Unserialize(section); // views LoadViews(rd); // lua section = rd.RdSection("LuaModules"); Pi::luaSerializer->Unserialize(section); Pi::luaSerializer->UninitTableRefs(); LuaRef::UninitLoad(); // signature check for (Uint32 i = 0; i < strlen(s_saveEnd)+1; i++) if (rd.Byte() != s_saveEnd[i]) throw SavedGameCorruptException(); EmitPauseState(IsPaused()); }
Game::Game(const Json::Value &jsonObj) : m_timeAccel(TIMEACCEL_PAUSED), m_requestedTimeAccel(TIMEACCEL_PAUSED), m_forceTimeAccel(false) { // signature check if (!jsonObj.isMember("signature")) throw SavedGameCorruptException(); Json::Value signature = jsonObj["signature"]; if (signature.isString() && signature.asString().compare(s_saveStart) == 0) {} else throw SavedGameCorruptException(); // version check if (!jsonObj.isMember("version")) throw SavedGameCorruptException(); Json::Value version = jsonObj["version"]; if (!version.isInt()) throw SavedGameCorruptException(); Output("savefile version: %d\n", version.asInt()); if (version.asInt() == s_saveVersion) {} else { Output("can't load savefile, expected version: %d\n", s_saveVersion); throw SavedGameWrongVersionException(); } // Preparing the Lua stuff Pi::luaSerializer->InitTableRefs(); // galaxy generator m_galaxy = Galaxy::LoadFromJson(jsonObj); // game state if (!jsonObj.isMember("time")) throw SavedGameCorruptException(); if (!jsonObj.isMember("state")) throw SavedGameCorruptException(); m_time = StrToDouble(jsonObj["time"].asString()); m_state = State(jsonObj["state"].asInt()); if (!jsonObj.isMember("want_hyperspace")) throw SavedGameCorruptException(); if (!jsonObj.isMember("hyperspace_progress")) throw SavedGameCorruptException(); if (!jsonObj.isMember("hyperspace_duration")) throw SavedGameCorruptException(); if (!jsonObj.isMember("hyperspace_end_time")) throw SavedGameCorruptException(); m_wantHyperspace = jsonObj["want_hyperspace"].asBool(); m_hyperspaceProgress = StrToDouble(jsonObj["hyperspace_progress"].asString()); m_hyperspaceDuration = StrToDouble(jsonObj["hyperspace_duration"].asString()); m_hyperspaceEndTime = StrToDouble(jsonObj["hyperspace_end_time"].asString()); // space, all the bodies and things if (!jsonObj.isMember("player")) throw SavedGameCorruptException(); m_space.reset(new Space(this, m_galaxy, jsonObj, m_time)); m_player.reset(static_cast<Player*>(m_space->GetBodyByIndex(jsonObj["player"].asUInt()))); assert(!m_player->IsDead()); // Pioneer does not support necromancy // hyperspace clouds being brought over from the previous system if (!jsonObj.isMember("hyperspace_clouds")) throw SavedGameCorruptException(); Json::Value hyperspaceCloudArray = jsonObj["hyperspace_clouds"]; if (!hyperspaceCloudArray.isArray()) throw SavedGameCorruptException(); for (Uint32 i = 0; i < hyperspaceCloudArray.size(); i++) m_hyperspaceClouds.push_back(static_cast<HyperspaceCloud*>(Body::FromJson(hyperspaceCloudArray[i], 0))); // views LoadViewsFromJson(jsonObj); // lua Pi::luaSerializer->FromJson(jsonObj); Pi::luaSerializer->UninitTableRefs(); // signature check (don't really need this anymore) if (!jsonObj.isMember("trailing_signature")) throw SavedGameCorruptException(); Json::Value trailingSignature = jsonObj["trailing_signature"]; if (trailingSignature.isString() && trailingSignature.asString().compare(s_saveEnd) == 0) {} else throw SavedGameCorruptException(); EmitPauseState(IsPaused()); }