Moved saved scene loading to CPlayerProfile

master
krzys-h 2015-08-04 09:55:39 +02:00
parent 72210d523f
commit fc8d8cb9d4
6 changed files with 185 additions and 143 deletions

View File

@ -424,6 +424,44 @@ void CPlayerProfile::SaveApperance()
// SAVE / LOAD SCENE
bool CPlayerProfile::HasAnySavedScene()
{
auto saveDirs = CResourceManager::ListDirectories(GetSaveDir());
for (auto dir : saveDirs)
{
if (CResourceManager::Exists(GetSaveFile(dir + "/data.sav")))
{
return true;
}
}
return false;
}
std::vector<SavedScene> CPlayerProfile::GetSavedSceneList()
{
auto saveDirs = CResourceManager::ListDirectories(GetSaveDir());
std::map<int, SavedScene> sortedSaveDirs;
for (auto dir : saveDirs)
{
std::string savegameFile = GetSaveFile(dir+"/data.sav");
if (CResourceManager::Exists(savegameFile))
{
CLevelParser levelParser(savegameFile);
levelParser.Load();
int time = levelParser.Get("Created")->GetParam("date")->AsInt();
sortedSaveDirs[time] = SavedScene(GetSaveFile(dir), levelParser.Get("Title")->GetParam("text")->AsString());
}
}
std::vector<SavedScene> result;
for (auto dir : sortedSaveDirs)
{
result.push_back(dir.second);
}
return result;
}
void CPlayerProfile::SaveScene(std::string dir, std::string info)
{
if (!CResourceManager::DirectoryExists(dir))
@ -436,3 +474,62 @@ void CPlayerProfile::SaveScene(std::string dir, std::string info)
CRobotMain::GetInstancePointer()->IOWriteScene(savegameFileName.c_str(), fileCBot.c_str(), const_cast<char*>(info.c_str()));
CRobotMain::GetInstancePointer()->MakeSaveScreenshot(dir + "/screen.png");
}
void CPlayerProfile::LoadScene(std::string dir)
{
std::string savegameFileName = dir + "/data.sav";
std::string fileCbot = CResourceManager::GetSaveLocation() + "/" + dir + "/cbot.run";
CLevelParser levelParser(savegameFileName);
levelParser.Load();
LevelCategory cat;
int chap;
int rank;
CLevelParserLine* line = levelParser.Get("Mission");
cat = GetLevelCategoryFromDir(line->GetParam("base")->AsString());
rank = line->GetParam("rank")->AsInt();
if (cat == LevelCategory::CustomLevels)
{
chap = 0;
std::string dir = line->GetParam("dir")->AsString();
CRobotMain::GetInstancePointer()->UpdateCustomLevelList();
auto customLevelList = CRobotMain::GetInstancePointer()->GetCustomLevelList();
for (unsigned int i = 0; i < customLevelList.size(); i++)
{
if (customLevelList[i] == dir)
{
chap = i+1;
break;
}
}
if (chap == 0) return; //TODO: Exception
}
else
{
if(line->GetParam("chap")->IsDefined())
{
chap = line->GetParam("chap")->AsInt();
}
else
{
// Backwards combatibility
chap = rank/100;
rank = rank%100;
}
}
CRobotMain::GetInstancePointer()->SetLevel(cat, chap, rank);
CRobotMain::GetInstancePointer()->IOReadScene(savegameFileName.c_str(), fileCbot.c_str());
}
bool CPlayerProfile::DeleteScene(std::string dir)
{
if (CResourceManager::DirectoryExists(dir))
{
return CResourceManager::RemoveDirectory(dir);
}
return false;
}

View File

@ -43,6 +43,18 @@ struct PlayerApperance
void DefPerso();
};
struct SavedScene
{
std::string path;
std::string name;
SavedScene(std::string _path = "", std::string _name = "")
{
path = _path;
name = _name;
}
};
class CPlayerProfile
{
public:
@ -103,9 +115,16 @@ public:
//! Saves PlayerApperance structure
void SaveApperance();
//! Returns true if player has at least one saved scene
bool HasAnySavedScene();
//! Returns list of all saved scenes
std::vector<SavedScene> GetSavedSceneList();
//! Saves current scene status
void SaveScene(std::string dir, std::string info);
//TODO: Move saved scenes load and list here
//! Loads scene
void LoadScene(std::string dir);
//! Delete saved scene
bool DeleteScene(std::string dir);
protected:
//! Loads LevelInfo data for given category

View File

@ -3440,7 +3440,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
ChangeColor(); // changes the colors of texture
if (read[0] != 0) // loading file ?
sel = IOReadScene(read, stack);
sel = IOReadSceneObjects(read, stack);
continue;
}
@ -5216,8 +5216,15 @@ CObject* CRobotMain::IOReadObject(CLevelParserLine *line, const char* filename,
return obj;
}
void CRobotMain::IOReadScene(const char *filename, const char *filecbot)
{
m_dialog->SetSceneRead(filename);
m_dialog->SetStackRead(filecbot);
ChangePhase(PHASE_LOADING);
}
//! Resumes some part of the game
CObject* CRobotMain::IOReadScene(const char *filename, const char *filecbot)
CObject* CRobotMain::IOReadSceneObjects(const char *filename, const char *filecbot)
{
CLevelParser levelParser(filename);
levelParser.Load();
@ -5787,6 +5794,11 @@ float CRobotMain::GetPersoAngle()
return m_dialog->GetPersoAngle();
}
void CRobotMain::SetLevel(LevelCategory cat, int chap, int rank)
{
m_dialog->SetLevel(cat, chap, rank);
}
LevelCategory CRobotMain::GetLevelCategory()
{
return m_dialog->GetLevelCategory();
@ -6029,11 +6041,21 @@ void CRobotMain::DisplayError(Error err, Math::Vector goal, float height, float
m_displayText->DisplayError(err, goal, height, dist, time);
}
void CRobotMain::UpdateCustomLevelList()
{
m_dialog->UpdateCustomLevelList();
}
std::string CRobotMain::GetCustomLevelName(int id)
{
return m_dialog->GetCustomLevelName(id);
}
const std::vector<std::string>& CRobotMain::GetCustomLevelList()
{
return m_dialog->GetCustomLevelList();
}
void CRobotMain::StartMissionTimer()
{
if (m_missionTimerEnabled && !m_missionTimerStarted)

View File

@ -257,6 +257,7 @@ public:
bool GetGamerOnlyHead();
float GetPersoAngle();
void SetLevel(LevelCategory cat, int chap, int rank);
LevelCategory GetLevelCategory();
int GetLevelChap();
int GetLevelRank();
@ -300,7 +301,8 @@ public:
bool IsBusy();
bool IOWriteScene(const char *filename, const char *filecbot, char *info);
void IOWriteSceneFinished();
CObject* IOReadScene(const char *filename, const char *filecbot);
void IOReadScene(const char *filename, const char *filecbot);
CObject* IOReadSceneObjects(const char *filename, const char *filecbot);
void IOWriteObject(CLevelParserLine *line, CObject* obj);
CObject* IOReadObject(CLevelParserLine *line, const char* filename, int objRank);
@ -311,7 +313,9 @@ public:
void DisplayError(Error err, CObject* pObj, float time=10.0f);
void DisplayError(Error err, Math::Vector goal, float height=15.0f, float dist=60.0f, float time=10.0f);
void UpdateCustomLevelList();
std::string GetCustomLevelName(int id);
const std::vector<std::string>& GetCustomLevelList();
void StartMissionTimer();

View File

@ -859,7 +859,7 @@ void CMainDialog::ChangePhase(Phase phase)
ddim.x = dim.x*2.5f;
pb = pw->CreateButton(pos, ddim, -1, EVENT_INTERFACE_READ);
pb->SetState(STATE_SHADOW);
if ( !IsIOReadScene() ) // no file to read?
if ( !m_main->GetPlayerProfile()->HasAnySavedScene() ) // no file to read?
{
pb->ClearState(STATE_ENABLE);
}
@ -2791,10 +2791,7 @@ bool CMainDialog::EventProcess(const Event &event)
}
if ( event.type == EVENT_INTERFACE_IOREAD )
{
if ( IOReadScene() )
{
m_main->ChangePhase(PHASE_LOADING);
}
IOReadScene();
return false;
}
@ -2830,21 +2827,19 @@ bool CMainDialog::EventProcess(const Event &event)
}
if ( event.type == EVENT_INTERFACE_IOWRITE )
{
IOWriteScene();
m_interface->DeleteControl(EVENT_WINDOW5);
ChangePhase(PHASE_SIMUL);
StopSuspend();
IOWriteScene();
m_interface->DeleteControl(EVENT_WINDOW5);
return false;
}
if ( event.type == EVENT_INTERFACE_IOREAD )
{
if ( IOReadScene() )
{
m_interface->DeleteControl(EVENT_WINDOW5);
ChangePhase(PHASE_SIMUL);
StopSuspend();
m_main->ChangePhase(PHASE_LOADING);
}
ChangePhase(PHASE_SIMUL);
StopSuspend();
IOReadScene();
return false;
}
@ -3920,23 +3915,6 @@ void CMainDialog::ColorPerso()
else apperance.colorBand = color;
}
// Indicates if there is at least one backup.
bool CMainDialog::IsIOReadScene()
{
auto saveDirs = CResourceManager::ListDirectories(m_main->GetPlayerProfile()->GetSaveDir());
for (auto dir : saveDirs)
{
if (CResourceManager::Exists(m_main->GetPlayerProfile()->GetSaveFile(dir + "/data.sav")))
{
return true;
}
}
return false;
}
// Builds the file name by default.
void CMainDialog::IOReadName()
@ -3987,30 +3965,10 @@ void CMainDialog::IOReadList()
pl->Flush();
m_saveList.clear();
auto saveDirs = CResourceManager::ListDirectories(m_main->GetPlayerProfile()->GetSaveDir());
//std::sort(saveDirs.begin(), saveDirs.end());
std::map<int, std::string> sortedSaveDirs;
std::map<int, std::string> names;
for (auto dir : saveDirs)
for(const SavedScene& save : m_main->GetPlayerProfile()->GetSavedSceneList())
{
std::string savegameFile = m_main->GetPlayerProfile()->GetSaveFile(dir+"/data.sav");
if (CResourceManager::Exists(savegameFile))
{
CLevelParser levelParser(savegameFile);
levelParser.Load();
int time = levelParser.Get("Created")->GetParam("date")->AsInt();
sortedSaveDirs[time] = m_main->GetPlayerProfile()->GetSaveFile(dir);
names[time] = levelParser.Get("Title")->GetParam("text")->AsString();
}
}
for (auto dir : sortedSaveDirs)
{
pl->SetItemName(m_saveList.size(), names[dir.first].c_str());
m_saveList.push_back(dir.second);
pl->SetItemName(m_saveList.size(), save.name.c_str());
m_saveList.push_back(save.path);
}
// invalid index
@ -4024,13 +3982,9 @@ void CMainDialog::IOReadList()
pl->SetSelect(m_saveList.size());
pl->ShowSelect(false); // shows the selected columns
unsigned int i;
std::string screenName;
for ( i=0; i < m_saveList.size(); i++ )
for (unsigned int i = 0; i < m_saveList.size(); i++)
{
screenName = "textures/../" + m_saveList.at(i) + "/screen.png";
m_engine->DeleteTexture(screenName);
m_engine->DeleteTexture(m_saveList.at(i) + "/screen.png");
}
}
@ -4087,25 +4041,21 @@ void CMainDialog::IODeleteScene()
{
CWindow* pw;
CList* pl;
int sel;
pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
if ( pw == 0 ) return;
pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_IOLIST));
if ( pl == 0 ) return;
sel = pl->GetSelect();
if ( sel == -1 || m_saveList.size() <= static_cast<unsigned int>(sel))
int sel = pl->GetSelect();
if (sel < 0 || sel >= static_cast<int>(m_saveList.size())) return;
if (!m_main->GetPlayerProfile()->DeleteScene(m_saveList.at(sel)))
{
m_sound->Play(SOUND_TZOING);
return;
}
if (CResourceManager::DirectoryExists(m_saveList.at(sel)))
{
CResourceManager::RemoveDirectory(m_saveList.at(sel));
}
IOReadList();
}
@ -4126,26 +4076,22 @@ std::string clearName(char *name)
// Writes the scene.
bool CMainDialog::IOWriteScene()
void CMainDialog::IOWriteScene()
{
CWindow* pw;
CList* pl;
CEdit* pe;
char info[100];
int sel;
pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
if ( pw == nullptr ) return false;
if ( pw == nullptr ) return;
pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_IOLIST));
if ( pl == nullptr ) return false;
if ( pl == nullptr ) return;
pe = static_cast<CEdit*>(pw->SearchControl(EVENT_INTERFACE_IONAME));
if ( pe == nullptr ) return false;
if ( pe == nullptr ) return;
sel = pl->GetSelect();
if ( sel == -1 )
{
return false;
}
int sel = pl->GetSelect();
if ( sel == -1 ) return;
std::string dir;
pe->GetText(info, 100);
@ -4159,8 +4105,6 @@ bool CMainDialog::IOWriteScene()
}
m_main->GetPlayerProfile()->SaveScene(dir, info);
return true;
}
void CMainDialog::MakeSaveScreenshot(const std::string& name)
@ -4171,72 +4115,23 @@ void CMainDialog::MakeSaveScreenshot(const std::string& name)
// Reads the scene.
bool CMainDialog::IOReadScene()
void CMainDialog::IOReadScene()
{
CWindow* pw;
CList* pl;
int sel;
pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
if ( pw == nullptr ) return false;
if ( pw == nullptr ) return;
pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_IOLIST));
if ( pl == nullptr ) return false;
if ( pl == nullptr ) return;
sel = pl->GetSelect();
if ( sel == -1 || m_saveList.size() <= static_cast<unsigned int>(sel) )
{
return false;
}
int sel = pl->GetSelect();
if (sel < 0 || sel >= static_cast<int>(m_saveList.size())) return;
std::string fileName = m_saveList.at(sel) + "/" + "data.sav";
std::string fileCbot = CResourceManager::GetSaveLocation()+"/"+m_saveList.at(sel) + "/" + "cbot.run";
CLevelParser levelParser(fileName);
levelParser.Load();
CLevelParserLine* line = levelParser.Get("Mission");
m_category = GetLevelCategoryFromDir(line->GetParam("base")->AsString());
m_levelRank = line->GetParam("rank")->AsInt();
if (m_category == LevelCategory::CustomLevels)
{
m_levelChap = 0;
std::string dir = line->GetParam("dir")->AsString();
UpdateCustomLevelList();
for (unsigned int i = 0; i < m_customLevelList.size(); i++)
{
if (m_customLevelList[i] == dir)
{
m_levelChap = i+1;
break;
}
}
if (m_levelChap == 0)
{
return false;
}
}
else
{
if(line->GetParam("chap")->IsDefined())
{
m_levelChap = line->GetParam("chap")->AsInt();
}
else
{
// Backwards combatibility
int rank = line->GetParam("rank")->AsInt();
m_levelChap = rank/100;
m_levelRank = rank%100;
}
}
m_main->GetPlayerProfile()->LoadScene(m_saveList.at(sel));
m_chap[m_category] = m_levelChap-1;
m_sel[m_category] = m_levelRank-1;
m_sceneRead = fileName;
m_stackRead = fileCbot;
return true;
}
// Updates the lists according to the cheat code.
@ -5525,7 +5420,7 @@ void CMainDialog::StartAbort()
pos.y = 0.53f;
pb = pw->CreateButton(pos, dim, -1, EVENT_INTERFACE_READ);
pb->SetState(STATE_SHADOW);
if ( !IsIOReadScene() ) // no file to read?
if ( !m_main->GetPlayerProfile()->HasAnySavedScene() ) // no file to read?
{
pb->ClearState(STATE_ENABLE);
}
@ -6085,5 +5980,10 @@ std::string CMainDialog::GetCustomLevelName(int id)
return m_customLevelList[id-1];
}
const std::vector<std::string>& CMainDialog::GetCustomLevelList()
{
return m_customLevelList;
}
} // namespace Ui

View File

@ -107,6 +107,7 @@ public:
void UpdateCustomLevelList();
std::string GetCustomLevelName(int id);
const std::vector<std::string>& GetCustomLevelList();
void MakeSaveScreenshot(const std::string& name);
@ -125,13 +126,12 @@ protected:
void CameraPerso();
void FixPerso(int rank, int index);
void ColorPerso();
bool IsIOReadScene();
void IOReadName();
void IOReadList();
void IOUpdateList();
void IODeleteScene();
bool IOWriteScene();
bool IOReadScene();
void IOWriteScene();
void IOReadScene();
int GetChapPassed();
void UpdateSceneChap(int &chap);
void UpdateSceneList(int chap, int &sel);