Added saving challenges to config file

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3640 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2009-06-23 01:44:49 +00:00
parent d2cd16910a
commit 09ce7366c8
6 changed files with 57 additions and 33 deletions

View File

@ -19,6 +19,9 @@
#include "challenges/challenge.hpp" #include "challenges/challenge.hpp"
#include <iostream>
#include "io/xml_node.hpp"
#include "karts/kart_properties_manager.hpp" #include "karts/kart_properties_manager.hpp"
#include "karts/kart_properties.hpp" #include "karts/kart_properties.hpp"
#include "race/grand_prix_manager.hpp" #include "race/grand_prix_manager.hpp"
@ -137,26 +140,34 @@ const std::string Challenge::getUnlockedMessage() const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Loads the state for a challenge object (esp. m_state), and calls the /** Loads the state for a challenge object (esp. m_state), and calls the
* virtual function loadState for additional information * virtual function loadAdditionalInfo for additional information
*/ */
void Challenge::load(const lisp::Lisp* config) void Challenge::load(const XMLNode* challengesNode)
{ {
const lisp::Lisp* subnode= config->getLisp(getId()); const XMLNode* node = challengesNode->getNode( getId() );
if(!subnode) return; if(node == NULL) return;
// See if the challenge is solved (it's activated later from the // See if the challenge is solved (it's activated later from the
// unlock_manager). // unlock_manager).
bool finished=false;
subnode->get("solved", finished); std::string solvedString;
node->get("solved", &solvedString);
bool finished = (solvedString == "true");
m_state = finished ? CH_SOLVED : CH_INACTIVE; m_state = finished ? CH_SOLVED : CH_INACTIVE;
if(!finished) loadState(subnode);
if(m_state == CH_SOLVED)
{
std::cout << "Solved challenge!! " << getId().c_str() << std::endl;
}
if(!finished) loadAdditionalInfo(node);
} // load } // load
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Challenge::save(lisp::Writer* writer) void Challenge::save(std::ofstream& writer)
{ {
writer->beginList(getId()); writer << " <" << getId() << " solved=\"" << (isSolved() ? "true" : "false") << "\"";
writer->write("solved", isSolved()); if(!isSolved()) saveAdditionalInfo(writer);
if(!isSolved()) saveState(writer); writer << " />\n";
writer->endList(getId());
} // save } // save

View File

@ -22,10 +22,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <fstream>
#include "lisp/lisp.hpp" class XMLNode;
#include "lisp/parser.hpp"
#include "lisp/writer.hpp"
enum REWARD_TYPE enum REWARD_TYPE
{UNLOCK_TRACK, {UNLOCK_TRACK,
@ -86,13 +85,13 @@ public:
void setActive() {m_state = CH_ACTIVE; } void setActive() {m_state = CH_ACTIVE; }
const std::vector<std::string>& const std::vector<std::string>&
getPrerequisites() const {return m_prerequisites; } getPrerequisites() const {return m_prerequisites; }
void load(const lisp::Lisp* config); void load(const XMLNode* config);
void save(lisp::Writer* writer); void save(std::ofstream& writer);
// These functions are meant for customisation, e.g. load/save // These functions are meant for customisation, e.g. load/save
// additional state information specific to the challenge // additional state information specific to the challenge
virtual void loadState(const lisp::Lisp* config) {}; virtual void loadAdditionalInfo(const XMLNode* config) {};
virtual void saveState(lisp::Writer* writer) {}; virtual void saveAdditionalInfo(std::ofstream& writer) {};
// These functions are called when a race/gp is finished. It allows // These functions are called when a race/gp is finished. It allows
// the challenge to unlock features (when returning true), otherwise // the challenge to unlock features (when returning true), otherwise

View File

@ -26,6 +26,7 @@
#include "challenges/challenge.hpp" #include "challenges/challenge.hpp"
#include "race/race_manager.hpp" #include "race/race_manager.hpp"
#include "lisp/lisp.hpp"
class ChallengeData : public Challenge class ChallengeData : public Challenge
{ {

View File

@ -167,24 +167,32 @@ Challenge* UnlockManager::getChallenge(const std::string& id)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** This is called from user_config when reading the config file /** This is called from user_config when reading the config file
*/ */
void UnlockManager::load(const lisp::Lisp* config) void UnlockManager::load(const XMLNode* configRoot)
{ {
const XMLNode* challengesNode = configRoot->getNode("challenges");
if(challengesNode == NULL) return;
for(AllChallengesType::iterator i =m_all_challenges.begin(); for(AllChallengesType::iterator i =m_all_challenges.begin();
i!=m_all_challenges.end(); i++) i!=m_all_challenges.end(); i++)
{ {
i->second->load(config); i->second->load(challengesNode);
} }
computeActive(); computeActive();
} // load } // load
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void UnlockManager::save(lisp::Writer* writer) void UnlockManager::save(std::ofstream& writer)
{ {
for(AllChallengesType::iterator i =m_all_challenges.begin(); writer << " <challenges>\n";
i!=m_all_challenges.end(); i++)
for(AllChallengesType::iterator i = m_all_challenges.begin();
i!= m_all_challenges.end(); i++)
{ {
i->second->save(writer); i->second->save(writer);
} // for i in m_all_challenges }
writer << " </challenges>\n\n";
} // save } // save
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -23,9 +23,9 @@
#include <map> #include <map>
#include "challenges/challenge.hpp" #include "challenges/challenge.hpp"
#include "lisp/lisp.hpp" #include <fstream>
#include "lisp/parser.hpp"
#include "lisp/writer.hpp" class XMLNode;
class UnlockManager class UnlockManager
{ {
@ -40,8 +40,8 @@ public:
UnlockManager (); UnlockManager ();
void addChallenge (Challenge *c); void addChallenge (Challenge *c);
void addChallenge (const std::string& filename); void addChallenge (const std::string& filename);
void load (const lisp::Lisp*); void load (const XMLNode*);
void save (lisp::Writer* writer); void save (std::ofstream& writer);
std::vector<const Challenge*> std::vector<const Challenge*>
getActiveChallenges(); getActiveChallenges();
const std::vector<const Challenge*> const std::vector<const Challenge*>

View File

@ -527,6 +527,10 @@ bool UserConfig::loadConfig(const std::string& filename)
std::cout << "----- player : " << name.c_str() << std::endl; std::cout << "----- player : " << name.c_str() << std::endl;
} }
// --- Read challenges
unlock_manager->load(root);
delete root; delete root;
return true; return true;
@ -536,8 +540,6 @@ bool UserConfig::loadConfig(const std::string& filename)
/** Write settings to config file. */ /** Write settings to config file. */
void UserConfig::saveConfig(const std::string& filepath) void UserConfig::saveConfig(const std::string& filepath)
{ {
// TODO : save challenges state
const int DIR_EXIST = CheckAndCreateDir(); const int DIR_EXIST = CheckAndCreateDir();
// Check if the config directory exists (again, since it was already checked // Check if the config directory exists (again, since it was already checked
// when reading the config file - this is done in case that the problem was // when reading the config file - this is done in case that the problem was
@ -568,6 +570,9 @@ void UserConfig::saveConfig(const std::string& filepath)
//std::cout << "saving parameter " << i << " to file\n"; //std::cout << "saving parameter " << i << " to file\n";
all_params[i].write(configfile); all_params[i].write(configfile);
} }
unlock_manager->save(configfile);
configfile << "</stkconfig>\n"; configfile << "</stkconfig>\n";
configfile.close(); configfile.close();