move Course sorts to CourseUtil, add CourseID

This commit is contained in:
Chris Danford
2004-04-18 08:36:04 +00:00
parent e285500f27
commit 68e2315224
10 changed files with 290 additions and 191 deletions
-169
View File
@@ -868,61 +868,6 @@ bool Course::GetTotalSeconds( float& fSecondsOut ) const
return true;
}
//
// Sorting stuff
//
static bool CompareCoursePointersByName(const Course* pCourse1, const Course* pCourse2)
{
// HACK: strcmp and other string comparators appear to eat whitespace.
// For example, the string "Players Best 13-16" is sorted between
// "Players Best 1-4" and "Players Best 5-8". Replace the string " "
// with " 0" for comparison only.
// XXX: That doesn't happen to me, and it shouldn't (strcmp is strictly
// a byte sort, though CompareNoCase doesn't use strcmp). Are you sure
// you didn't have only one space before? -glenn
CString sName1 = pCourse1->m_sName;
CString sName2 = pCourse2->m_sName;
sName1.Replace( " " , " 0" );
sName2.Replace( " " , " 0" );
return sName1.CompareNoCase( sName2 ) == -1;
}
static bool CompareCoursePointersByAutogen(const Course* pCourse1, const Course* pCourse2)
{
int b1 = pCourse1->m_bIsAutogen;
int b2 = pCourse2->m_bIsAutogen;
if( b1 < b2 )
return true;
else if( b1 > b2 )
return false;
else
return CompareCoursePointersByName(pCourse1,pCourse2);
}
static bool CompareCoursePointersByDifficulty(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->GetEstimatedNumStages();
int iNum2 = pCourse2->GetEstimatedNumStages();
if( iNum1 < iNum2 )
return true;
else if( iNum1 > iNum2 )
return false;
else // iNum1 == iNum2
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
}
static bool CompareCoursePointersByTotalDifficulty(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->SortOrder_TotalDifficulty;
int iNum2 = pCourse2->SortOrder_TotalDifficulty;
if( iNum1 == iNum2 )
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
return iNum1 < iNum2;
}
bool Course::CourseHasBestOrWorst() const
{
for(unsigned i = 0; i < m_entries.size(); i++)
@@ -938,69 +883,6 @@ bool Course::CourseHasBestOrWorst() const
return false;
}
static bool MovePlayersBestToEnd( const Course* pCourse1, const Course* pCourse2 )
{
bool C1HasBest = pCourse1->CourseHasBestOrWorst();
bool C2HasBest = pCourse2->CourseHasBestOrWorst();
if( !C1HasBest && !C2HasBest )
return false;
if( C1HasBest && !C2HasBest )
return false;
if( !C1HasBest && C2HasBest )
return true;
return pCourse1->m_sName < pCourse2->m_sName;
}
static bool CompareRandom( const Course* pCourse1, const Course* pCourse2 )
{
return ( pCourse1->IsFixed() && !pCourse2->IsFixed() );
}
static bool CompareCoursePointersByRanking(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->SortOrder_Ranking;
int iNum2 = pCourse2->SortOrder_Ranking;
if( iNum1 == iNum2 )
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
return iNum1 < iNum2;
}
void SortCoursePointerArrayByDifficulty( vector<Course*> &apCourses )
{
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByDifficulty );
}
void SortCoursePointerArrayByRanking( vector<Course*> &apCourses )
{
for(unsigned i=0; i<apCourses.size(); i++)
apCourses[i]->UpdateCourseStats();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByRanking );
}
void SortCoursePointerArrayByTotalDifficulty( vector<Course*> &apCourses )
{
for(unsigned i=0; i<apCourses.size(); i++)
apCourses[i]->UpdateCourseStats();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByTotalDifficulty );
}
static bool CompareCoursePointersByType(const Course* pCourse1, const Course* pCourse2)
{
return pCourse1->GetPlayMode() < pCourse2->GetPlayMode();
}
void MoveRandomToEnd( vector<Course*> &apCourses )
{
stable_sort( apCourses.begin(), apCourses.end(), CompareRandom );
}
void SortCoursePointerArrayByType( vector<Course*> &apCourses )
{
stable_sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByType );
}
bool Course::HasBanner() const
{
return m_sBannerPath != "" && IsAFile(m_sBannerPath);
@@ -1085,54 +967,3 @@ RadarValues Course::GetRadarValues( StepsType st, CourseDifficulty cd ) const
return rv;
}
//
// Sorting stuff
//
static map<const Course*, float> course_sort_val;
bool CompareCoursePointersBySortValueAscending(const Course *pSong1, const Course *pSong2)
{
return course_sort_val[pSong1] < course_sort_val[pSong2];
}
bool CompareCoursePointersBySortValueDescending(const Course *pSong1, const Course *pSong2)
{
return course_sort_val[pSong1] > course_sort_val[pSong2];
}
bool CompareCoursePointersByTitle( const Course *pCourse1, const Course *pCourse2 )
{
return pCourse1->m_sName < pCourse2->m_sName;
}
void SortCoursePointerArrayByAvgDifficulty( vector<Course*> &apCourses )
{
RageTimer foo;
course_sort_val.clear();
for(unsigned i = 0; i < apCourses.size(); ++i)
course_sort_val[apCourses[i]] = apCourses[i]->GetMeter();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByTitle );
stable_sort( apCourses.begin(), apCourses.end(), CompareCoursePointersBySortValueAscending );
stable_sort( apCourses.begin(), apCourses.end(), MovePlayersBestToEnd );
}
void SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, ProfileSlot slot, bool bDescending )
{
Profile* pProfile = PROFILEMAN->GetProfile(slot);
if( pProfile == NULL )
return; // nothing to do since we don't have data
SortCoursePointerArrayByNumPlays( arrayCoursePointers, pProfile, bDescending );
}
void SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, const Profile* pProfile, bool bDescending )
{
ASSERT( pProfile );
for(unsigned i = 0; i < arrayCoursePointers.size(); ++i)
course_sort_val[arrayCoursePointers[i]] = (float) pProfile->GetCourseNumTimesPlayed(arrayCoursePointers[i]);
stable_sort( arrayCoursePointers.begin(), arrayCoursePointers.end(), bDescending ? CompareCoursePointersBySortValueDescending : CompareCoursePointersBySortValueAscending );
course_sort_val.clear();
}
-11
View File
@@ -163,15 +163,4 @@ private:
mutable InfoCache m_InfoCache;
};
void SortCoursePointerArrayByDifficulty( vector<Course*> &apCourses );
void SortCoursePointerArrayByType( vector<Course*> &apCourses );
void SortCoursePointerArrayByAvgDifficulty( vector<Course*> &apCourses );
void SortCoursePointerArrayByTotalDifficulty( vector<Course*> &apCourses );
void SortCoursePointerArrayByRanking( vector<Course*> &apCourses );
void SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, ProfileSlot slot, bool bDescending );
void SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, const Profile* pProfile, bool bDescending );
void MoveRandomToEnd( vector<Course*> &apCourses );
#endif
+207
View File
@@ -0,0 +1,207 @@
#include "global.h"
/*
-----------------------------------------------------------------------------
Class: CourseUtil
Desc: See header.
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
#include "CourseUtil.h"
#include "Course.h"
#include "RageTimer.h"
#include "ProfileManager.h"
#include "SongManager.h"
//
// Sorting stuff
//
static bool CompareCoursePointersByName(const Course* pCourse1, const Course* pCourse2)
{
// HACK: strcmp and other string comparators appear to eat whitespace.
// For example, the string "Players Best 13-16" is sorted between
// "Players Best 1-4" and "Players Best 5-8". Replace the string " "
// with " 0" for comparison only.
// XXX: That doesn't happen to me, and it shouldn't (strcmp is strictly
// a byte sort, though CompareNoCase doesn't use strcmp). Are you sure
// you didn't have only one space before? -glenn
CString sName1 = pCourse1->m_sName;
CString sName2 = pCourse2->m_sName;
sName1.Replace( " " , " 0" );
sName2.Replace( " " , " 0" );
return sName1.CompareNoCase( sName2 ) == -1;
}
static bool CompareCoursePointersByAutogen(const Course* pCourse1, const Course* pCourse2)
{
int b1 = pCourse1->m_bIsAutogen;
int b2 = pCourse2->m_bIsAutogen;
if( b1 < b2 )
return true;
else if( b1 > b2 )
return false;
else
return CompareCoursePointersByName(pCourse1,pCourse2);
}
static bool CompareCoursePointersByDifficulty(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->GetEstimatedNumStages();
int iNum2 = pCourse2->GetEstimatedNumStages();
if( iNum1 < iNum2 )
return true;
else if( iNum1 > iNum2 )
return false;
else // iNum1 == iNum2
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
}
static bool CompareCoursePointersByTotalDifficulty(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->SortOrder_TotalDifficulty;
int iNum2 = pCourse2->SortOrder_TotalDifficulty;
if( iNum1 == iNum2 )
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
return iNum1 < iNum2;
}
static bool MovePlayersBestToEnd( const Course* pCourse1, const Course* pCourse2 )
{
bool C1HasBest = pCourse1->CourseHasBestOrWorst();
bool C2HasBest = pCourse2->CourseHasBestOrWorst();
if( !C1HasBest && !C2HasBest )
return false;
if( C1HasBest && !C2HasBest )
return false;
if( !C1HasBest && C2HasBest )
return true;
return pCourse1->m_sName < pCourse2->m_sName;
}
static bool CompareRandom( const Course* pCourse1, const Course* pCourse2 )
{
return ( pCourse1->IsFixed() && !pCourse2->IsFixed() );
}
static bool CompareCoursePointersByRanking(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->SortOrder_Ranking;
int iNum2 = pCourse2->SortOrder_Ranking;
if( iNum1 == iNum2 )
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
return iNum1 < iNum2;
}
void CourseUtil::SortCoursePointerArrayByDifficulty( vector<Course*> &apCourses )
{
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByDifficulty );
}
void CourseUtil::SortCoursePointerArrayByRanking( vector<Course*> &apCourses )
{
for(unsigned i=0; i<apCourses.size(); i++)
apCourses[i]->UpdateCourseStats();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByRanking );
}
void CourseUtil::SortCoursePointerArrayByTotalDifficulty( vector<Course*> &apCourses )
{
for(unsigned i=0; i<apCourses.size(); i++)
apCourses[i]->UpdateCourseStats();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByTotalDifficulty );
}
static bool CompareCoursePointersByType(const Course* pCourse1, const Course* pCourse2)
{
return pCourse1->GetPlayMode() < pCourse2->GetPlayMode();
}
void CourseUtil::SortCoursePointerArrayByType( vector<Course*> &apCourses )
{
stable_sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByType );
}
void CourseUtil::MoveRandomToEnd( vector<Course*> &apCourses )
{
stable_sort( apCourses.begin(), apCourses.end(), CompareRandom );
}
static map<const Course*, float> course_sort_val;
bool CompareCoursePointersBySortValueAscending(const Course *pSong1, const Course *pSong2)
{
return course_sort_val[pSong1] < course_sort_val[pSong2];
}
bool CompareCoursePointersBySortValueDescending(const Course *pSong1, const Course *pSong2)
{
return course_sort_val[pSong1] > course_sort_val[pSong2];
}
bool CompareCoursePointersByTitle( const Course *pCourse1, const Course *pCourse2 )
{
return pCourse1->m_sName < pCourse2->m_sName;
}
void CourseUtil::SortCoursePointerArrayByAvgDifficulty( vector<Course*> &apCourses )
{
RageTimer foo;
course_sort_val.clear();
for(unsigned i = 0; i < apCourses.size(); ++i)
course_sort_val[apCourses[i]] = apCourses[i]->GetMeter();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByTitle );
stable_sort( apCourses.begin(), apCourses.end(), CompareCoursePointersBySortValueAscending );
stable_sort( apCourses.begin(), apCourses.end(), MovePlayersBestToEnd );
}
void CourseUtil::SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, ProfileSlot slot, bool bDescending )
{
Profile* pProfile = PROFILEMAN->GetProfile(slot);
if( pProfile == NULL )
return; // nothing to do since we don't have data
SortCoursePointerArrayByNumPlays( arrayCoursePointers, pProfile, bDescending );
}
void CourseUtil::SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, const Profile* pProfile, bool bDescending )
{
ASSERT( pProfile );
for(unsigned i = 0; i < arrayCoursePointers.size(); ++i)
course_sort_val[arrayCoursePointers[i]] = (float) pProfile->GetCourseNumTimesPlayed(arrayCoursePointers[i]);
stable_sort( arrayCoursePointers.begin(), arrayCoursePointers.end(), bDescending ? CompareCoursePointersBySortValueDescending : CompareCoursePointersBySortValueAscending );
course_sort_val.clear();
}
void CourseID::FromCourse( const Course *p )
{
if( p )
{
sPath = p->m_sPath;
sName = p->m_sName;
}
else
{
sPath = "";
sName = "";
}
}
Course *CourseID::ToCourse() const
{
Course* pCourse = NULL;
pCourse = SONGMAN->GetCourseFromPath( sPath );
if( pCourse )
return pCourse;
pCourse = SONGMAN->GetCourseFromName( sName );
return pCourse;
}
+43
View File
@@ -0,0 +1,43 @@
#ifndef COURSEUTIL_H
#define COURSEUTIL_H
/*
-----------------------------------------------------------------------------
Class: CourseUtil
Desc: A queue of songs and notes.
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
#include "GameConstantsAndTypes.h"
class Course;
class Profile;
namespace CourseUtil
{
void SortCoursePointerArrayByDifficulty( vector<Course*> &apCourses );
void SortCoursePointerArrayByType( vector<Course*> &apCourses );
void SortCoursePointerArrayByAvgDifficulty( vector<Course*> &apCourses );
void SortCoursePointerArrayByTotalDifficulty( vector<Course*> &apCourses );
void SortCoursePointerArrayByRanking( vector<Course*> &apCourses );
void SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, ProfileSlot slot, bool bDescending );
void SortCoursePointerArrayByNumPlays( vector<Course*> &arrayCoursePointers, const Profile* pProfile, bool bDescending );
void MoveRandomToEnd( vector<Course*> &apCourses );
};
class CourseID
{
CString sPath;
CString sName;
public:
CourseID() { FromCourse(NULL); }
void FromCourse( const Course *p );
Course *ToCourse() const;
};
#endif
+2 -1
View File
@@ -48,7 +48,8 @@ ScreenTitleMenu.cpp ScreenTitleMenu.h ScreenUnlock.cpp ScreenUnlock.h
DataStructures = \
Attack.cpp Attack.h BannerCache.cpp BannerCache.h Character.cpp Character.h CharacterHead.cpp CharacterHead.h \
CodeDetector.cpp CodeDetector.h EnumHelper.cpp EnumHelper.h Course.cpp Course.h Font.cpp Font.h FontCharAliases.cpp FontCharAliases.h \
CodeDetector.cpp CodeDetector.h EnumHelper.cpp EnumHelper.h Course.cpp Course.h CourseUtil.cpp CourseUtil.h \
Font.cpp Font.h FontCharAliases.cpp FontCharAliases.h \
FontCharmaps.cpp FontCharmaps.h Game.h GameConstantsAndTypes.cpp GameConstantsAndTypes.h GameDef.cpp GameDef.h \
GameInput.cpp GameInput.h Grade.cpp Grade.h HighScore.cpp HighScore.h Inventory.cpp Inventory.h LuaFunctions.h \
LuaHelpers.cpp LuaHelpers.h LyricsLoader.cpp LyricsLoader.h MenuInput.h ModeChoice.cpp ModeChoice.h \
+7 -6
View File
@@ -33,6 +33,7 @@
#include "ModeChoice.h"
#include "ActorUtil.h"
#include "SongUtil.h"
#include "CourseUtil.h"
#define FADE_SECONDS THEME->GetMetricF("MusicWheel","FadeSeconds")
@@ -626,25 +627,25 @@ void MusicWheel::BuildWheelItemDatas( vector<WheelItemData> &arrayWheelItemDatas
}
if (PREFSMAN->m_iCourseSortOrder == PrefsManager::COURSE_SORT_SONGS)
SortCoursePointerArrayByDifficulty( apCourses );
CourseUtil::SortCoursePointerArrayByDifficulty( apCourses );
else
{
if (PREFSMAN->m_iCourseSortOrder == PrefsManager::COURSE_SORT_METER)
SortCoursePointerArrayByAvgDifficulty( apCourses );
CourseUtil::SortCoursePointerArrayByAvgDifficulty( apCourses );
if (PREFSMAN->m_iCourseSortOrder == PrefsManager::COURSE_SORT_METER_SUM)
SortCoursePointerArrayByTotalDifficulty( apCourses );
CourseUtil::SortCoursePointerArrayByTotalDifficulty( apCourses );
if (PREFSMAN->m_iCourseSortOrder == PrefsManager::COURSE_SORT_RANK)
SortCoursePointerArrayByRanking( apCourses );
CourseUtil::SortCoursePointerArrayByRanking( apCourses );
// since we can't agree, make it an option
if (PREFSMAN->m_bMoveRandomToEnd)
MoveRandomToEnd( apCourses );
CourseUtil::MoveRandomToEnd( apCourses );
}
if( so == SORT_ALL_COURSES )
SortCoursePointerArrayByType( apCourses );
CourseUtil::SortCoursePointerArrayByType( apCourses );
arrayWheelItemDatas.clear(); // clear out the previous wheel items
+2 -1
View File
@@ -27,6 +27,7 @@
#include "RageUtil.h"
#include "SongUtil.h"
#include "StepsUtil.h"
#include "CourseUtil.h"
const CString STATS_HTML = "Stats.html";
@@ -464,7 +465,7 @@ void PrintPopularity( RageFile &f, const Profile *pProfile, CString sTitle, vect
{
unsigned uNumToShow = min( vpCourses.size(), (unsigned)100 );
SortCoursePointerArrayByNumPlays( vpCourses, pProfile, true );
CourseUtil::SortCoursePointerArrayByNumPlays( vpCourses, pProfile, true );
PRINT_OPEN(f, "Most Popular Courses" );
{
BEGIN_TABLE(2);
+2 -1
View File
@@ -35,6 +35,7 @@
#include "NotesLoaderSM.h"
#include "SongUtil.h"
#include "StepsUtil.h"
#include "CourseUtil.h"
SongManager* SONGMAN = NULL; // global and accessable from anywhere in our program
@@ -909,7 +910,7 @@ void SongManager::UpdateBest()
SongUtil::SortSongPointerArrayByNumPlays( m_pBestSongs[i], (ProfileSlot) i, true );
m_pBestCourses[i] = m_pCourses;
SortCoursePointerArrayByNumPlays( m_pBestCourses[i], (ProfileSlot) i, true );
CourseUtil::SortCoursePointerArrayByNumPlays( m_pBestCourses[i], (ProfileSlot) i, true );
}
}
+21 -2
View File
@@ -65,7 +65,7 @@ IntDir=.\../Debug6
TargetDir=\stepmania\stepmania\Program
TargetName=StepMania-debug
SOURCE="$(InputPath)"
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi
# End Special Build Tool
@@ -142,7 +142,7 @@ IntDir=.\../Release6
TargetDir=\stepmania\stepmania\Program
TargetName=StepMania
SOURCE="$(InputPath)"
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PreLink_Cmds=disasm\verinc cl /Zl /nologo /c verstub.cpp /Fo$(IntDir)\
PostBuild_Cmds=disasm\mapconv $(IntDir)\$(TargetName).map $(TargetDir)\StepMania.vdi
# End Special Build Tool
@@ -1207,6 +1207,25 @@ SOURCE=.\Course.h
# End Source File
# Begin Source File
SOURCE=.\CourseUtil.cpp
!IF "$(CFG)" == "StepMania - Win32 Debug"
!ELSEIF "$(CFG)" == "StepMania - Xbox Debug"
!ELSEIF "$(CFG)" == "StepMania - Win32 Release"
!ELSEIF "$(CFG)" == "StepMania - Xbox Release"
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\CourseUtil.h
# End Source File
# Begin Source File
SOURCE=.\EnumHelper.cpp
!IF "$(CFG)" == "StepMania - Win32 Debug"
+6
View File
@@ -572,6 +572,12 @@ cl /Zl /nologo /c verstub.cpp /Fo&quot;$(IntDir)&quot;\
<File
RelativePath="Course.h">
</File>
<File
RelativePath="CourseUtil.cpp">
</File>
<File
RelativePath="CourseUtil.h">
</File>
<File
RelativePath="EnumHelper.cpp">
</File>