Avoid some code duplication. Thanks to sachith500.

This commit is contained in:
Deve 2014-02-02 10:53:33 +01:00
parent 1aa4be85f3
commit 57d0975c50
3 changed files with 36 additions and 53 deletions

View File

@ -149,6 +149,29 @@ Track::~Track()
#endif
} // ~Track
//-----------------------------------------------------------------------------
/** Returns number of completed challenges */
unsigned int Track::getNumOfCompletedChallenges()
{
unsigned int unlocked_challenges = 0;
GameSlot* slot = unlock_manager->getCurrentSlot();
for (unsigned int i=0; i<m_challenges.size(); i++)
{
if (m_challenges[i].m_challenge_id == "tutorial")
{
unlocked_challenges++;
continue;
}
if (slot->getChallenge(m_challenges[i].m_challenge_id)
->isSolvedAtAnyDifficulty())
{
unlocked_challenges++;
}
}
return unlocked_challenges;
} // getNumOfCompletedChallenges
//-----------------------------------------------------------------------------
/** Removes all cached data structures. This is called before the resolution
* is changed.
@ -961,24 +984,9 @@ bool Track::loadMainTrack(const XMLNode &root)
}
else if (condition == "allchallenges")
{
unsigned int unlocked_challenges = 0;
GameSlot* slot = unlock_manager->getCurrentSlot();
for (unsigned int c=0; c<m_challenges.size(); c++)
{
if (m_challenges[c].m_challenge_id == "tutorial")
{
unlocked_challenges++;
continue;
}
if (slot->getChallenge(m_challenges[c].m_challenge_id)
->isSolvedAtAnyDifficulty())
{
unlocked_challenges++;
}
}
// allow ONE unsolved challenge : the last one
if (unlocked_challenges < m_challenges.size() - 1) continue;
if (getNumOfCompletedChallenges() < m_challenges.size() - 1)
continue;
}
else if (condition.size() > 0)
{
@ -993,24 +1001,9 @@ bool Track::loadMainTrack(const XMLNode &root)
}
else if (neg_condition == "allchallenges")
{
unsigned int unlocked_challenges = 0;
GameSlot* slot = unlock_manager->getCurrentSlot();
for (unsigned int c=0; c<m_challenges.size(); c++)
{
if (m_challenges[c].m_challenge_id == "tutorial")
{
unlocked_challenges++;
continue;
}
if (slot->getChallenge(m_challenges[c].m_challenge_id)
->isSolvedAtAnyDifficulty())
{
unlocked_challenges++;
}
}
// allow ONE unsolved challenge : the last one
if (unlocked_challenges >= m_challenges.size() - 1) continue;
if (getNumOfCompletedChallenges() >= m_challenges.size() - 1)
continue;
}
else if (neg_condition.size() > 0)
{

View File

@ -206,6 +206,7 @@ private:
/** True if this track (textures and track data) should be cached. Used
* for the overworld. */
bool m_cache_track;
#ifdef DEBUG
/** A list of textures that were cached before the track is loaded.
@ -527,6 +528,9 @@ public:
/** Returns the number of modes available for this track. */
unsigned int getNumberOfModes() const { return m_all_modes.size(); }
// ------------------------------------------------------------------------
/** Returns number of completed challenges. */
unsigned int getNumOfCompletedChallenges();
// ------------------------------------------------------------------------
/** Returns the name of the i-th. mode. */
const std::string &getModeName(unsigned int i) const
{ return m_all_modes[i].m_name; }

View File

@ -679,27 +679,13 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who)
else if (m_action == "big_door")
{
m_action_active = false;
unsigned int unlocked_challenges = 0;
GameSlot* slot = unlock_manager->getCurrentSlot();
std::vector<OverworldChallenge> m_challenges = World::getWorld()->getTrack()->getChallengeList();
for (unsigned int i=0; i<m_challenges.size(); i++)
{
if (m_challenges[i].m_challenge_id == "tutorial")
{
unlocked_challenges++;
continue;
}
if (slot->getChallenge(m_challenges[i].m_challenge_id)
->isSolvedAtAnyDifficulty())
{
unlocked_challenges++;
}
}
Track* m_track = World::getWorld()->getTrack();
unsigned int unlocked_challenges = m_track->getNumOfCompletedChallenges();
std::vector<OverworldChallenge> m_challenges = m_track->getChallengeList();
// allow ONE unsolved challenge : the last one and check for negation
if ((unlocked_challenges < m_challenges.size() - 1))
// allow ONE unsolved challenge : the last one
if (unlocked_challenges < m_challenges.size() - 1)
{
new TutorialMessageDialog(_("Complete all challenges to unlock the big door!"), true);
}