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 // 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) void CPlayerProfile::SaveScene(std::string dir, std::string info)
{ {
if (!CResourceManager::DirectoryExists(dir)) 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()->IOWriteScene(savegameFileName.c_str(), fileCBot.c_str(), const_cast<char*>(info.c_str()));
CRobotMain::GetInstancePointer()->MakeSaveScreenshot(dir + "/screen.png"); 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(); void DefPerso();
}; };
struct SavedScene
{
std::string path;
std::string name;
SavedScene(std::string _path = "", std::string _name = "")
{
path = _path;
name = _name;
}
};
class CPlayerProfile class CPlayerProfile
{ {
public: public:
@ -103,9 +115,16 @@ public:
//! Saves PlayerApperance structure //! Saves PlayerApperance structure
void SaveApperance(); 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 //! Saves current scene status
void SaveScene(std::string dir, std::string info); 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: protected:
//! Loads LevelInfo data for given category //! 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 ChangeColor(); // changes the colors of texture
if (read[0] != 0) // loading file ? if (read[0] != 0) // loading file ?
sel = IOReadScene(read, stack); sel = IOReadSceneObjects(read, stack);
continue; continue;
} }
@ -5216,8 +5216,15 @@ CObject* CRobotMain::IOReadObject(CLevelParserLine *line, const char* filename,
return obj; 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 //! 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); CLevelParser levelParser(filename);
levelParser.Load(); levelParser.Load();
@ -5787,6 +5794,11 @@ float CRobotMain::GetPersoAngle()
return m_dialog->GetPersoAngle(); return m_dialog->GetPersoAngle();
} }
void CRobotMain::SetLevel(LevelCategory cat, int chap, int rank)
{
m_dialog->SetLevel(cat, chap, rank);
}
LevelCategory CRobotMain::GetLevelCategory() LevelCategory CRobotMain::GetLevelCategory()
{ {
return m_dialog->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); m_displayText->DisplayError(err, goal, height, dist, time);
} }
void CRobotMain::UpdateCustomLevelList()
{
m_dialog->UpdateCustomLevelList();
}
std::string CRobotMain::GetCustomLevelName(int id) std::string CRobotMain::GetCustomLevelName(int id)
{ {
return m_dialog->GetCustomLevelName(id); return m_dialog->GetCustomLevelName(id);
} }
const std::vector<std::string>& CRobotMain::GetCustomLevelList()
{
return m_dialog->GetCustomLevelList();
}
void CRobotMain::StartMissionTimer() void CRobotMain::StartMissionTimer()
{ {
if (m_missionTimerEnabled && !m_missionTimerStarted) if (m_missionTimerEnabled && !m_missionTimerStarted)

View File

@ -257,6 +257,7 @@ public:
bool GetGamerOnlyHead(); bool GetGamerOnlyHead();
float GetPersoAngle(); float GetPersoAngle();
void SetLevel(LevelCategory cat, int chap, int rank);
LevelCategory GetLevelCategory(); LevelCategory GetLevelCategory();
int GetLevelChap(); int GetLevelChap();
int GetLevelRank(); int GetLevelRank();
@ -300,7 +301,8 @@ public:
bool IsBusy(); bool IsBusy();
bool IOWriteScene(const char *filename, const char *filecbot, char *info); bool IOWriteScene(const char *filename, const char *filecbot, char *info);
void IOWriteSceneFinished(); 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); void IOWriteObject(CLevelParserLine *line, CObject* obj);
CObject* IOReadObject(CLevelParserLine *line, const char* filename, int objRank); 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, 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 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); std::string GetCustomLevelName(int id);
const std::vector<std::string>& GetCustomLevelList();
void StartMissionTimer(); void StartMissionTimer();

View File

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

View File

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