Merge branch 'konstin-m_random_gp'

This commit is contained in:
hiker 2014-07-16 08:19:24 +10:00
commit ec1b0ab9fc
8 changed files with 151 additions and 90 deletions

View File

@ -37,6 +37,9 @@
// ----------------------------------------------------------------------------
/** Loads a grand prix definition from a file.
* \param filename Name of the file to load.
*/
GrandPrixData::GrandPrixData(const std::string& filename)
{
m_filename = filename;
@ -44,18 +47,34 @@ GrandPrixData::GrandPrixData(const std::string& filename)
StringUtils::removeExtension(filename));
m_editable = (filename.find(file_manager->getGPDir(), 0) == 0);
reload();
}
} // GrandPrixData
// ----------------------------------------------------------------------------
GrandPrixData::GrandPrixData(const unsigned int number_of_tracks,
const std::string& track_group,
const RandomGPInfoDialog::REVERSED use_reverse)
/** Creates a random grand prix from the specified parameters.
* \param number_of_tracks How many tracks to select.
* \param track_group From which track group to select the tracks.
* \param use_reverse How the reverse setting is to be determined.
* \param new_tracks If true, new tracks are selected, otherwise existing
* tracks will not be changed (used to e.g. increase the number of
* tracks in an already existing random grand prix).
*
*/
void GrandPrixData::createRandomGP(const unsigned int number_of_tracks,
const std::string &track_group,
const GPReverseType use_reverse,
bool new_tracks)
{
m_filename = "Random GP - Not loaded from a file!";
m_id = "random";
m_name = "Random Grand Prix";
m_editable = false;
if(new_tracks)
{
m_tracks.clear();
m_laps.clear();
m_reversed.clear();
}
m_tracks.reserve(number_of_tracks);
m_laps.reserve(number_of_tracks);
m_reversed.reserve(number_of_tracks);
@ -117,48 +136,64 @@ void GrandPrixData::changeTrackNumber(const unsigned int number_of_tracks,
}
// ----------------------------------------------------------------------------
void GrandPrixData::changeReverse(const RandomGPInfoDialog::REVERSED use_reverse)
/** Updates the GP data with newly decided reverse requirements.
* \param use_reverse How reverse setting for each track is to be determined.
*/
void GrandPrixData::changeReverse(const GrandPrixData::GPReverseType use_reverse)
{
for (unsigned int i = 0; i < m_tracks.size(); i++)
{
if (use_reverse == RandomGPInfoDialog::NO_REVERSE)
if (use_reverse == GP_NO_REVERSE)
m_reversed[i] = false;
else if (use_reverse == RandomGPInfoDialog::MIXED)
else if (use_reverse == GP_RANDOM_REVERSE)
if (track_manager->getTrack(m_tracks[i])->reverseAvailable())
m_reversed[i] = (rand() % 2 != 0);
else
m_reversed[i] = false;
else // all reversed
m_reversed[i] = track_manager->getTrack(m_tracks[i])->reverseAvailable();
}
}
} // for i < m_tracks.size()
} // changeReverse
// ----------------------------------------------------------------------------
/** Sets the id of this grand prix.
* \param id The new id.
*/
void GrandPrixData::setId(const std::string& id)
{
m_id = id;
}
} // setId
// ----------------------------------------------------------------------------
/** Sets the name of the grand prix.
* \param name New name.
*/
void GrandPrixData::setName(const irr::core::stringw& name)
{
m_name = name;
}
} // setName
// ----------------------------------------------------------------------------
/** Sets the filename of this grand prix.
* \param filename New filename.
*/
void GrandPrixData::setFilename(const std::string& filename)
{
m_filename = filename;
}
} // setFilename
// ----------------------------------------------------------------------------
/** Sets if this grand prix can be edited.
* \param editable New value.
*/
void GrandPrixData::setEditable(const bool editable)
{
m_editable = editable;
}
} // setEditable
// ----------------------------------------------------------------------------
/** Reloads grand prix from file.
*/
void GrandPrixData::reload()
{
m_tracks.clear();
@ -270,6 +305,8 @@ void GrandPrixData::reload()
} // reload()
// ----------------------------------------------------------------------------
/** Saves the grand prix data to a file.
*/
bool GrandPrixData::writeToFile()
{
try
@ -303,9 +340,12 @@ bool GrandPrixData::writeToFile()
m_filename.c_str(), e.what());
return false;
}
}
} // writeToFile
// ----------------------------------------------------------------------------
/** Checks if the grand prix data are consistent.
* \param log_error: If errors should be sent to the logger.
*/
bool GrandPrixData::checkConsistency(bool log_error) const
{
for (unsigned int i = 0; i < m_tracks.size(); i++)
@ -323,8 +363,7 @@ bool GrandPrixData::checkConsistency(bool log_error) const
}
}
return true;
}
} // checkConsistency
// ----------------------------------------------------------------------------
/** Returns true if the track is available. This is used to test if Fort Magma
@ -332,76 +371,99 @@ bool GrandPrixData::checkConsistency(bool log_error) const
* story mode, but will be available once all challenges are done and nolok
* is unlocked). It also prevents people from using the grand prix editor as
* a way to play tracks that still haven't been unlocked
* \param id Name of the track to test.
* \param include_locked If set to true, all tracks (including locked tracks)
* are considered to be available.
*/
bool GrandPrixData::isTrackAvailable(const std::string &id,
bool includeLocked ) const
bool include_locked ) const
{
if (includeLocked)
if (include_locked)
return true;
else if (id == "fortmagma")
return !PlayerManager::getCurrentPlayer()->isLocked("fortmagma");
else
return (!m_editable ||
!PlayerManager::get()->getCurrentPlayer()->isLocked(id));
}
} // isTrackAvailable
// ----------------------------------------------------------------------------
std::vector<std::string> GrandPrixData::getTrackNames(bool includeLocked) const
/** Returns the list of tracks that is available (i.e. unlocked) of this
* grand prix.
* \param include_locked If data for locked tracks should be included or not.
* \return A copy of the list of available tracks in this grand prix.
*/
std::vector<std::string> GrandPrixData::getTrackNames(bool include_locked) const
{
std::vector<std::string> names;
for (unsigned int i = 0; i < m_tracks.size(); i++)
{
if(isTrackAvailable(m_tracks[i], includeLocked))
if(isTrackAvailable(m_tracks[i], include_locked))
names.push_back(m_tracks[i]);
}
return names;
}
} // getTrackNames
// ----------------------------------------------------------------------------
std::vector<int> GrandPrixData::getLaps(bool includeLocked) const
/** Returns the laps for each available track of the grand prix.
* \param include_locked If data for locked tracks should be included or not.
* \return a std::vector containing the laps for each grand prix.
*/
std::vector<int> GrandPrixData::getLaps(bool include_locked) const
{
std::vector<int> laps;
for (unsigned int i = 0; i< m_tracks.size(); i++)
if(isTrackAvailable(m_tracks[i], includeLocked))
if(isTrackAvailable(m_tracks[i], include_locked))
laps.push_back(m_laps[i]);
return laps;
}
} // getLaps
// ----------------------------------------------------------------------------
std::vector<bool> GrandPrixData::getReverse(bool includeLocked) const
/** Returns the reverse setting for each available grand prix.
* \param include_locked If data for locked tracks should be included or not.
* \return A copy of alist with the reverse status for each track.
*/
std::vector<bool> GrandPrixData::getReverse(bool include_locked) const
{
std::vector<bool> reverse;
for (unsigned int i = 0; i< m_tracks.size(); i++)
if(isTrackAvailable(m_tracks[i], includeLocked))
if(isTrackAvailable(m_tracks[i], include_locked))
reverse.push_back(m_reversed[i]);
return reverse;
}
} // getReverse
// ----------------------------------------------------------------------------
/** Returns true if this grand prix can be edited.
*/
bool GrandPrixData::isEditable() const
{
return m_editable;
}
} // isEditable
// ----------------------------------------------------------------------------
/** Returns the number of tracks in this grand prix.
* \param include_locked If data for locked tracks should be included or not.
*/
unsigned int GrandPrixData::getNumberOfTracks(bool includeLocked) const
{
if (includeLocked)
return m_tracks.size();
else
return getTrackNames(false).size();
}
} // getNumberOfTracks
// ----------------------------------------------------------------------------
/** Returns the (translated) name of the track with the specified index.
*/
irr::core::stringw GrandPrixData::getTrackName(const unsigned int track) const
{
assert(track < getNumberOfTracks(true));
Track* t = track_manager->getTrack(m_tracks[track]);
assert(t != NULL);
return t->getName();
}
} // getTrackName
// ----------------------------------------------------------------------------
const std::string& GrandPrixData::getTrackId(const unsigned int track) const

View File

@ -24,7 +24,6 @@
#include <string>
#include <vector>
#include "states_screens/dialogs/random_gp_dialog.hpp"
#include "utils/translation.hpp"
using irr::core::stringw;
@ -69,21 +68,35 @@ private:
*/
bool isTrackAvailable(const std::string &id, bool includeLocked) const;
public:
/** Used to define the reverse setting when creating a random GP:
* No reverse, all reverse (if available on the track), random
* selection. */
enum GPReverseType
{
GP_NO_REVERSE = 0,
GP_ALL_REVERSE = 1,
GP_RANDOM_REVERSE = 2
}; // GPReverseType
public:
#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
# pragma warning(disable:4290)
#endif
/** Load the GrandPrixData from the given filename */
GrandPrixData(const std::string& filename);
/** Needed for simple creation of an instance of GrandPrixData */
GrandPrixData() {};
/** Creates a new random GP */
GrandPrixData(const unsigned int number_of_tracks,
const std::string& track_group,
const RandomGPInfoDialog::REVERSED use_reverse);
void changeTrackNumber(const unsigned int number_of_tracks,
const std::string& track_group);
void changeReverse(const RandomGPInfoDialog::REVERSED use_reverse);
void changeReverse(const GPReverseType use_reverse);
void createRandomGP(const unsigned int number_of_tracks,
const std::string& track_group,
const GPReverseType use_reverse,
bool new_tracks=false);
// Methods for the GP editor
void setId(const std::string& id);

View File

@ -34,7 +34,6 @@ const char* GrandPrixManager::SUFFIX = ".grandprix";
// ----------------------------------------------------------------------------
GrandPrixManager::GrandPrixManager()
{
m_random_gp = NULL; // better do it explicitly and avoid weird stuff
loadFiles();
} // GrandPrixManager
@ -42,10 +41,7 @@ GrandPrixManager::GrandPrixManager()
GrandPrixManager::~GrandPrixManager()
{
for(unsigned int i=0; i<m_gp_data.size(); i++)
{
delete m_gp_data[i];
}
delete m_random_gp;
} // ~GrandPrixManager
// ----------------------------------------------------------------------------
@ -97,7 +93,7 @@ void GrandPrixManager::load(const std::string& filename)
catch (std::runtime_error& e)
{
Log::error("GrandPrixManager",
"Ignoring grand prix %s (%s)\n", filename.c_str(), e.what());
"Ignoring Grand Prix %s (%s)\n", filename.c_str(), e.what());
}
} // load
@ -156,9 +152,6 @@ GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const
// ----------------------------------------------------------------------------
GrandPrixData* GrandPrixManager::editGrandPrix(const std::string& s) const
{
if (s == "random")
return m_random_gp;
for(unsigned int i=0; i<m_gp_data.size(); i++)
{
if(m_gp_data[i]->getId() == s)

View File

@ -22,7 +22,8 @@
#include <vector>
#include <string>
#include "race/grand_prix_data.hpp"
#include "irrlicht.h"
class GrandPrixData;
/**
* \ingroup race
@ -47,10 +48,6 @@ private:
bool existsName(const irr::core::stringw& name) const;
public:
/** saved here by a random GP dialog to avoid dangling pinters or
* memory leaks */
GrandPrixData* m_random_gp;
GrandPrixManager();
~GrandPrixManager();
void reload();

View File

@ -53,8 +53,8 @@ GPInfoDialog::GPInfoDialog(const std::string& gp_ident)
doInit();
m_curr_time = 0.0f;
m_gp = grand_prix_manager->getGrandPrix(gp_ident);
m_gp->checkConsistency();
m_gp = *grand_prix_manager->getGrandPrix(gp_ident);
m_gp.checkConsistency();
m_under_title = m_area.getHeight()/7;
m_over_body = m_area.getHeight()/7;
@ -72,7 +72,7 @@ GPInfoDialog::~GPInfoDialog()
{
GUIEngine::Screen* curr_screen = GUIEngine::getCurrentScreen();
if (curr_screen->getName() == "tracks.stkgui")
static_cast<TracksScreen*>(curr_screen)->setFocusOnGP(m_gp->getId());
static_cast<TracksScreen*>(curr_screen)->setFocusOnGP(m_gp.getId());
}
// ----------------------------------------------------------------------------
@ -81,7 +81,7 @@ void GPInfoDialog::addTitle()
{
core::rect< s32 > area_top(0, 0, m_area.getWidth(), m_under_title);
IGUIStaticText* title = GUIEngine::getGUIEnv()->addStaticText(
translations->fribidize(m_gp->getName()),
translations->fribidize(m_gp.getName()),
area_top, false, true, // border, word wrap
m_irrlicht_window);
title->setTabStop(false);
@ -92,7 +92,7 @@ void GPInfoDialog::addTitle()
void GPInfoDialog::addTracks()
{
const std::vector<std::string> tracks = m_gp->getTrackNames();
const std::vector<std::string> tracks = m_gp.getTrackNames();
const unsigned int track_amount = tracks.size();
int height_of_one_line = std::min((m_lower_bound - m_over_body)/(track_amount+1),
@ -185,7 +185,7 @@ void GPInfoDialog::addScreenshot()
m_screenshot_widget->m_h = m_area.getWidth()*3/8; // *(3/4)*(1/2)
}
Track* track = track_manager->getTrack(m_gp->getTrackNames()[0]);
Track* track = track_manager->getTrack(m_gp.getTrackNames()[0]);
m_screenshot_widget->m_properties[GUIEngine::PROP_ICON] = (track->getScreenshotFile().c_str());
m_screenshot_widget->setParent(m_irrlicht_window);
m_screenshot_widget->add();
@ -203,7 +203,7 @@ void GPInfoDialog::addButtons()
SavedGrandPrix* saved_gp = SavedGrandPrix::getSavedGP( StateManager::get()
->getActivePlayerProfile(0)
->getUniqueID(),
m_gp->getId(),
m_gp.getId(),
race_manager->getDifficulty(),
race_manager->getNumberOfKarts(),
race_manager->getNumLocalPlayers());
@ -250,7 +250,7 @@ void GPInfoDialog::addButtons()
void GPInfoDialog::onEnterPressedInternal()
{
// Save the GP id because dismiss() will destroy this instance
std::string gp_id = m_gp->getId();
std::string gp_id = m_gp.getId();
ModalDialog::dismiss();
// Disable accidentally unlocking of a challenge
PlayerManager::getCurrentPlayer()->setCurrentChallenge("");
@ -264,7 +264,7 @@ GUIEngine::EventPropagation GPInfoDialog::processEvent(const std::string& event_
if (event_source == "start" || event_source == "continue")
{
// Save GP identifier, since dismiss will delete this object.
std::string gp_id = m_gp->getId();
std::string gp_id = m_gp.getId();
// Also create a copy of the string: it is a reference to data
// in a widget in the dialog - so if we call dismiss, this reference
// becomes invalid!
@ -288,7 +288,7 @@ void GPInfoDialog::onUpdate(float dt)
m_curr_time += dt;
int frameAfter = (int)(m_curr_time / 1.5f);
const std::vector<std::string> tracks = m_gp->getTrackNames();
const std::vector<std::string> tracks = m_gp.getTrackNames();
if (frameAfter >= (int)tracks.size())
{
frameAfter = 0;

View File

@ -20,7 +20,7 @@
#define HEADER_GP_INFO_DIALOG_HPP
#include "guiengine/modaldialog.hpp"
// Don't include grand_prix_data.hpp here or the compilation will fail
#include "race/grand_prix_data.hpp"
class GrandPrixData;
@ -39,7 +39,9 @@ class GPInfoDialog : public GUIEngine::ModalDialog
protected: // Necessary for RandomGPInfoDialog
GUIEngine::IconButtonWidget* m_screenshot_widget;
float m_curr_time;
GrandPrixData* m_gp;
/** The grand prix data. */
GrandPrixData m_gp;
/** height of the separator over the body */
int m_over_body;

View File

@ -33,11 +33,12 @@ using irr::gui::IGUIStaticText;
typedef GUIEngine::SpinnerWidget Spinner;
RandomGPInfoDialog::RandomGPInfoDialog()
{
// Defaults - loading selection from last time frrom a file would be better
m_number_of_tracks = 2; // We can assume that there are at least 2 standard tracks
m_trackgroup = "standard";
m_use_reverse = NO_REVERSE;
m_use_reverse = GrandPrixData::GP_NO_REVERSE;
doInit();
m_curr_time = 0.0f;
@ -46,14 +47,7 @@ RandomGPInfoDialog::RandomGPInfoDialog()
m_over_body = m_area.getHeight()/7 + SPINNER_HEIGHT + 10; // 10px space
m_lower_bound = m_area.getHeight()*6/7;
// The GP manager is be used to make the GP live longer than this dialog
if (grand_prix_manager->m_random_gp)
{
delete grand_prix_manager->m_random_gp;
grand_prix_manager->m_random_gp = NULL;
}
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
grand_prix_manager->m_random_gp = m_gp;
m_gp.createRandomGP(m_number_of_tracks, m_trackgroup, m_use_reverse);
addTitle();
addSpinners();
@ -141,15 +135,17 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent(
{
if (eventSource == "start")
{
// Save GP data, since dismiss will delete this object.
GrandPrixData gp = m_gp;
ModalDialog::dismiss();
race_manager->startGP(grand_prix_manager->m_random_gp, false, false);
race_manager->startGP(&gp, false, false);
return GUIEngine::EVENT_BLOCK;
}
else if (eventSource == "Number of tracks")
{
// The old gp can be reused because there's only track deletion/adding
m_number_of_tracks = getWidget<Spinner>("Number of tracks")->getValue();
m_gp->changeTrackNumber(m_number_of_tracks, m_trackgroup);
m_gp.changeTrackNumber(m_number_of_tracks, m_trackgroup);
addTracks();
}
else if (eventSource == "Trackgroup")
@ -171,22 +167,22 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent(
if (s->getValue() > (signed)max)
s->setValue(max);
delete m_gp;
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
grand_prix_manager->m_random_gp = m_gp;
// Create a new (i.e. with new tracks) random gp, since the old
// tracks might not all belong to the newly selected group.
m_gp.createRandomGP(m_number_of_tracks, m_trackgroup, m_use_reverse,
/*new_tracks*/true);
addTracks();
}
else if (eventSource == "reverse")
{
Spinner* r = getWidget<Spinner>("reverse");
m_use_reverse = static_cast<REVERSED>(r->getValue());
m_gp->changeReverse(m_use_reverse);
m_use_reverse = static_cast<GrandPrixData::GPReverseType>(r->getValue());
m_gp.changeReverse(m_use_reverse);
}
else if (eventSource == "reload")
{
delete m_gp;
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
grand_prix_manager->m_random_gp = m_gp;
m_gp.createRandomGP(m_number_of_tracks, m_trackgroup, m_use_reverse,
/*new_tracks*/true);
addTracks();
}

View File

@ -24,17 +24,15 @@
class RandomGPInfoDialog : public GPInfoDialog
{
public:
enum REVERSED
{
NO_REVERSE = 0,
ALL_REVERSE = 1,
MIXED = 2
};
private:
/** How many tracks to pick. */
unsigned int m_number_of_tracks;
/** Name of the track group from which to pick tracks. */
std::string m_trackgroup;
REVERSED m_use_reverse;
/** How reverse settings should be determined. */
GrandPrixData::GPReverseType m_use_reverse;
public:
static const int SPINNER_HEIGHT = 40;