1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 00:19:56 +00:00
openmw-tes3mp/apps/openmw/mwstate/statemanagerimp.cpp

215 lines
5.8 KiB
C++
Raw Normal View History

2013-11-16 09:31:46 +00:00
#include "statemanagerimp.hpp"
#include <components/esm/esmwriter.hpp>
2013-11-21 11:24:24 +00:00
#include <components/esm/esmreader.hpp>
#include <components/settings/settings.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/journal.hpp"
#include "../mwbase/dialoguemanager.hpp"
#include "../mwbase/windowmanager.hpp"
2013-12-07 12:17:28 +00:00
#include "../mwbase/mechanicsmanager.hpp"
#include "../mwworld/player.hpp"
#include "../mwworld/class.hpp"
#include "../mwmechanics/npcstats.hpp"
2013-11-28 10:22:34 +00:00
void MWState::StateManager::cleanup()
{
if (mState!=State_NoGame)
{
MWBase::Environment::get().getDialogueManager()->clear();
MWBase::Environment::get().getJournal()->clear();
MWBase::Environment::get().getWorld()->clear();
2013-11-28 10:22:34 +00:00
mState = State_NoGame;
mCharacterManager.clearCurrentCharacter();
mTimePlayed = 0;
}
}
MWState::StateManager::StateManager (const boost::filesystem::path& saves, const std::string& game)
: mQuitRequest (false), mState (State_NoGame), mCharacterManager (saves, game), mTimePlayed (0)
2013-11-16 09:31:46 +00:00
{
}
void MWState::StateManager::requestQuit()
{
mQuitRequest = true;
}
bool MWState::StateManager::hasQuitRequest() const
{
return mQuitRequest;
}
MWState::StateManager::State MWState::StateManager::getState() const
{
return mState;
}
void MWState::StateManager::newGame (bool bypass)
{
2013-11-28 10:22:34 +00:00
cleanup();
if (!bypass)
{
MWBase::Environment::get().getWorld()->startNewGame();
MWBase::Environment::get().getWindowManager()->setNewGame (true);
}
else
MWBase::Environment::get().getWorld()->setGlobalInt ("chargenstate", -1);
mState = State_Running;
}
2013-11-18 14:38:08 +00:00
void MWState::StateManager::endGame()
{
mState = State_Ended;
MWBase::Environment::get().getWorld()->useDeathCamera();
2013-11-18 14:38:08 +00:00
}
void MWState::StateManager::saveGame (const std::string& description, const Slot *slot)
{
ESM::SavedGame profile;
MWBase::World& world = *MWBase::Environment::get().getWorld();
MWWorld::Ptr player = world.getPlayer().getPlayer();
profile.mContentFiles = world.getContentFiles();
profile.mPlayerName = player.getClass().getName (player);
profile.mPlayerLevel = player.getClass().getNpcStats (player).getLevel();
profile.mPlayerClass = player.get<ESM::NPC>()->mBase->mClass;
profile.mPlayerCell = world.getCellName();
profile.mInGameTime.mGameHour = world.getTimeStamp().getHour();
profile.mInGameTime.mDay = world.getDay();
profile.mInGameTime.mMonth = world.getMonth();
2013-11-28 08:27:10 +00:00
profile.mInGameTime.mYear = world.getYear();
profile.mTimePlayed = mTimePlayed;
profile.mDescription = description;
if (!slot)
slot = mCharacterManager.getCurrentCharacter()->createSlot (profile);
else
slot = mCharacterManager.getCurrentCharacter()->updateSlot (slot, profile);
std::ofstream stream (slot->mPath.string().c_str());
ESM::ESMWriter writer;
writer.setFormat (ESM::Header::CurrentFormat);
2013-12-03 13:28:46 +00:00
writer.setRecordCount (
2013-12-07 12:17:28 +00:00
1 // saved game header
+MWBase::Environment::get().getJournal()->countSavedGameRecords()
+MWBase::Environment::get().getWorld()->countSavedGameRecords()
);
2013-12-03 13:28:46 +00:00
writer.save (stream);
2013-12-03 13:28:46 +00:00
writer.startRecord (ESM::REC_SAVE);
slot->mProfile.save (writer);
2013-12-03 13:28:46 +00:00
writer.endRecord (ESM::REC_SAVE);
2013-11-28 10:22:34 +00:00
2013-12-03 13:28:46 +00:00
MWBase::Environment::get().getJournal()->write (writer);
2013-12-07 12:17:28 +00:00
MWBase::Environment::get().getWorld()->write (writer);
2013-11-28 10:22:34 +00:00
writer.close();
Settings::Manager::setString ("character", "Saves",
slot->mPath.parent_path().filename().string());
}
2013-11-21 11:24:24 +00:00
void MWState::StateManager::loadGame (const Character *character, const Slot *slot)
{
2013-11-28 10:22:34 +00:00
cleanup();
2013-11-21 11:24:24 +00:00
mTimePlayed = slot->mProfile.mTimePlayed;
2013-11-21 11:24:24 +00:00
ESM::ESMReader reader;
reader.open (slot->mPath.string());
2013-12-03 13:28:46 +00:00
while (reader.hasMoreRecs())
{
ESM::NAME n = reader.getRecName();
reader.getRecHeader();
switch (n.val)
{
case ESM::REC_SAVE:
// don't need to read that here
reader.skipRecord();
break;
2013-11-21 11:24:24 +00:00
2013-12-03 13:28:46 +00:00
case ESM::REC_JOUR:
case ESM::REC_QUES:
MWBase::Environment::get().getJournal()->readRecord (reader, n.val);
break;
2013-12-07 12:17:28 +00:00
case ESM::REC_ALCH:
case ESM::REC_ARMO:
case ESM::REC_BOOK:
case ESM::REC_CLAS:
case ESM::REC_CLOT:
case ESM::REC_ENCH:
case ESM::REC_NPC_:
case ESM::REC_SPEL:
case ESM::REC_WEAP:
MWBase::Environment::get().getWorld()->readRecord (reader, n.val);
break;
2013-12-03 13:28:46 +00:00
default:
// ignore invalid records
/// \todo log error
reader.skipRecord();
}
}
2013-11-21 11:24:24 +00:00
mCharacterManager.setCurrentCharacter(character);
2013-11-21 11:24:24 +00:00
mState = State_Running;
Settings::Manager::setString ("character", "Saves",
slot->mPath.parent_path().filename().string());
MWBase::Environment::get().getWorld()->setupPlayer();
MWBase::Environment::get().getWorld()->renderPlayer();
MWBase::Environment::get().getWindowManager()->updatePlayer();
2013-12-07 12:17:28 +00:00
MWBase::Environment::get().getMechanicsManager()->playerLoaded();
// for testing purpose only
ESM::Position pos;
pos.pos[0] = pos.pos[1] = pos.pos[2] = 0;
pos.rot[0] = pos.rot[1] = pos.rot[2] = 0;
MWBase::Environment::get().getWorld()->changeToExteriorCell (pos);
2013-11-21 11:24:24 +00:00
}
2013-11-24 15:58:41 +00:00
MWState::Character *MWState::StateManager::getCurrentCharacter (bool create)
{
2013-11-24 15:58:41 +00:00
return mCharacterManager.getCurrentCharacter (create);
}
MWState::StateManager::CharacterIterator MWState::StateManager::characterBegin()
{
return mCharacterManager.begin();
}
MWState::StateManager::CharacterIterator MWState::StateManager::characterEnd()
{
return mCharacterManager.end();
}
void MWState::StateManager::update (float duration)
{
mTimePlayed += duration;
}