stk-code_catmod/src/config/saved_grand_prix.hpp
2014-02-06 14:43:47 +11:00

165 lines
5.9 KiB
C++

//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2010-2013 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_SAVED_GRAND_PRIX_HPP
#define HEADER_SAVED_GRAND_PRIX_HPP
#include <string>
#include "config/user_config.hpp"
#include "race/race_manager.hpp"
#include "utils/ptr_vector.hpp"
class RaceManager;
// ============================================================================
/**
* \brief Class for managing saved Grand-Prix's
* A list of all possible resumable GP's is stored in the user config.
* \ingroup config
*/
class SavedGrandPrix
{
private:
class SavedGPKart
{
friend class SavedGrandPrix;
GroupUserConfigParam m_group;
StringUserConfigParam m_ident;
IntUserConfigParam m_score, m_local_player_id, m_global_player_id;
FloatUserConfigParam m_overall_time;
public:
SavedGPKart(GroupUserConfigParam * group, const XMLNode* node);
SavedGPKart(GroupUserConfigParam * group,
const std::string &ident,
int score,
int local_player_id,
int global_player_id,
float overall_time);
}; // SavedGPKart
protected:
/**
* For saving to config file.
* WARNING : m_savedgp_group has to be declared before the other userconfigparams!
*/
GroupUserConfigParam m_savedgp_group;
unsigned int m_player_id;
/** Identifier of this GP. */
StringUserConfigParam m_gp_id;
/** Difficulty at which this GP was run. */
IntUserConfigParam m_difficulty;
/** Number of player karts used in this GP. */
IntUserConfigParam m_player_karts;
/** Index of the next to run track. */
IntUserConfigParam m_next_track;
PtrVector<SavedGPKart> m_karts;
public:
/**
* Constructor to create a new entry.
*/
SavedGrandPrix(unsigned int player_id,
const std::string &gp_id,
RaceManager::Difficulty difficulty,
int player_karts,
int last_track,
const std::vector<RaceManager::KartStatus> &kart_list);
/**
* Constructor to deserialize a entry that was saved to a XML file
*/
SavedGrandPrix(const XMLNode* node);
void setKarts(const std::vector<RaceManager::KartStatus> &kart_list);
void clearKarts();
void loadKarts(std::vector<RaceManager::KartStatus> & kart_list);
// ------------------------------------------------------------------------
/** Returns the player id for this saved GP. */
unsigned int getPlayerID() const { return m_player_id; }
// ------------------------------------------------------------------------
/** Returns the grand prix id. */
std::string getGPID() const { return m_gp_id; }
// ------------------------------------------------------------------------
/** Returns the difficulty of this GP. */
int getDifficulty() const { return m_difficulty; }
// ------------------------------------------------------------------------
/** Returns the total number of karts of this GP. */
int getTotalKarts() const { return m_karts.size(); }
// ------------------------------------------------------------------------
/** Returns the number of player karts in this GP. */
int getPlayerKarts() const { return m_player_karts; }
// ------------------------------------------------------------------------
/** Returns the index of the last track finished when this GP was saved. */
int getNextTrack() const { return m_next_track; }
// ------------------------------------------------------------------------
/** Sets the index of the last track finished. */
void setNextTrack(int next_track) { m_next_track = next_track; }
// ------------------------------------------------------------------------
/** Removed this SavedGrandPrix from the list of all SavedGrandPrix, and
* deletes it. */
void remove()
{
UserConfigParams::m_saved_grand_prix_list.remove(this);
delete this;
} // remove
// ------------------------------------------------------------------------
/** Finds the right SavedGrandPrix given the specified data, or
* NULL if no matching GP was found.
*/
static SavedGrandPrix* getSavedGP(unsigned int player,
const std::string &gpid,
int difficulty, int total_karts,
int player_karts)
{
for (unsigned int n=0; n<UserConfigParams::m_saved_grand_prix_list.size(); n++)
{
SavedGrandPrix* gp = &UserConfigParams::m_saved_grand_prix_list[n];
if ((gp->getGPID() == gpid) &&
(gp->getPlayerID() == player) &&
(gp->getDifficulty() == difficulty) &&
(gp->getTotalKarts() == total_karts) &&
(gp->getPlayerKarts() == player_karts)){
return gp;
} // if
} // for n
return NULL;
} // getSavedGP
// ------------------------------------------------------------------------
}; // class SavedGrandPrix
#endif
/*EOF*/