Files
itgmania212121/stepmania/src/Course.cpp
T

989 lines
27 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-06-14 22:25:22 +00:00
/*
-----------------------------------------------------------------------------
Class: Course
Desc: See header.
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
#include "Course.h"
2002-07-23 01:41:40 +00:00
#include "PrefsManager.h"
2003-02-16 04:28:17 +00:00
#include "song.h"
2002-07-23 01:41:40 +00:00
#include "GameManager.h"
#include "SongManager.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
#include "RageLog.h"
#include "MsdFile.h"
2002-07-28 20:28:37 +00:00
#include "PlayerOptions.h"
#include "SongOptions.h"
#include "RageUtil.h"
2003-02-10 23:17:57 +00:00
#include "TitleSubstitution.h"
2003-08-03 00:13:55 +00:00
#include "Steps.h"
#include "GameState.h"
2003-06-15 02:07:31 +00:00
#include "BannerCache.h"
2003-07-22 07:56:46 +00:00
#include "RageFile.h"
#include "arch/arch.h"
#include "ThemeManager.h"
2002-06-14 22:25:22 +00:00
2003-07-17 22:44:17 +00:00
/* Amount to increase meter ranges to make them difficult: */
const int DIFFICULT_METER_CHANGE = 2;
/* Maximum lower value of ranges when difficult: */
const int MAX_BOTTOM_RANGE = 10;
2003-07-27 19:14:05 +00:00
/* -1 is the default parameter of a few Course calls; leaving it out indicates
* to use GAMESTATE->m_bDifficultCourses. */
static bool IsDifficult( int Difficult )
{
if( Difficult == -1 )
return GAMESTATE->m_bDifficultCourses;
else
return !!Difficult;
}
Course::Course()
{
m_bIsAutogen = false;
m_bRepeat = false;
m_bRandomize = false;
2003-03-07 05:24:52 +00:00
m_bDifficult = false;
2003-01-26 09:46:30 +00:00
m_iLives = -1;
2003-07-21 22:33:01 +00:00
m_iMeter = -1;
2003-01-22 05:29:27 +00:00
2003-06-30 08:06:47 +00:00
SetDefaultScore();
}
void Course::SetDefaultScore()
{
2003-01-22 05:29:27 +00:00
//
// Init high scores
//
2003-08-07 06:38:18 +00:00
for( unsigned i=0; i<NUM_STEPS_TYPES; i++ )
2003-02-17 20:35:20 +00:00
for( int j=0; j<NUM_RANKING_LINES; j++ )
2003-01-26 07:33:03 +00:00
{
2003-06-30 18:53:17 +00:00
m_RankingScores[i][j].iScore = IsOni()? 573:573000;
m_RankingScores[i][j].fSurviveTime = 57.3f;
m_RankingScores[i][j].sName = DEFAULT_RANKING_NAME;
2003-01-26 07:33:03 +00:00
}
2003-01-22 05:29:27 +00:00
for( unsigned m=0; m<NUM_MEMORY_CARDS; m++ )
2003-08-07 06:38:18 +00:00
for( unsigned i=0; i<NUM_STEPS_TYPES; i++ )
2003-01-26 07:33:03 +00:00
{
m_MemCardScores[m][i].iNumTimesPlayed = 0;
2003-06-30 08:06:47 +00:00
m_MemCardScores[m][i].iScore = 0;
m_MemCardScores[m][i].fSurviveTime = 0;
2003-01-26 07:33:03 +00:00
}
}
2002-07-02 17:34:20 +00:00
2003-07-20 01:54:17 +00:00
PlayMode Course::GetPlayMode() const
{
2003-07-27 19:14:05 +00:00
if( m_bRepeat )
2003-07-20 01:54:17 +00:00
return PLAY_MODE_ENDLESS;
2003-07-27 19:14:05 +00:00
return m_iLives > 0? PLAY_MODE_ONI:PLAY_MODE_NONSTOP;
2003-07-20 01:54:17 +00:00
}
2003-07-21 22:33:01 +00:00
const int DifficultMeterRamp = 3;
float Course::GetMeter( int Difficult ) const
2003-07-21 22:33:01 +00:00
{
if( m_iMeter != -1 )
return float(m_iMeter + IsDifficult(Difficult)? DifficultMeterRamp:0);
2003-07-21 22:33:01 +00:00
/*LOG->Trace( "Course file '%s' contains a song '%s%s%s' that is not present",
m_sPath.c_str(), sGroup.c_str(), sGroup.size()? SLASH:"", sSong.c_str());*/
2003-07-21 22:33:01 +00:00
vector<Info> ci;
2003-08-07 06:36:34 +00:00
GetCourseInfo( GAMESTATE->GetCurrentStyleDef()->m_StepsType, ci, Difficult );
2003-07-21 22:33:01 +00:00
if( ci.size() == 0 )
return 0;
2003-07-27 19:14:05 +00:00
/* Take the average meter. */
2003-07-21 22:33:01 +00:00
float fTotalMeter = 0;
for( unsigned c = 0; c < ci.size(); ++c )
{
if( ci[c].Mystery )
2003-07-21 22:33:01 +00:00
{
switch( GetDifficulty(ci[c]) )
{
case DIFFICULTY_INVALID:
{
int iMeterLow, iMeterHigh;
2003-07-27 19:14:05 +00:00
GetMeterRange(ci[c], iMeterLow, iMeterHigh );
fTotalMeter += (iMeterLow + iMeterHigh) / 2.0f;
2003-07-21 22:33:01 +00:00
break;
}
case DIFFICULTY_BEGINNER: fTotalMeter += 1; break;
case DIFFICULTY_EASY: fTotalMeter += 2; break;
case DIFFICULTY_MEDIUM: fTotalMeter += 5; break;
case DIFFICULTY_HARD: fTotalMeter += 7; break;
case DIFFICULTY_CHALLENGE: fTotalMeter += 9; break;
}
}
else
2003-07-21 23:16:06 +00:00
fTotalMeter += ci[c].pNotes->GetMeter();
2003-07-21 22:33:01 +00:00
}
return fTotalMeter / ci.size();
2003-07-21 22:33:01 +00:00
}
2003-02-05 19:16:00 +00:00
void Course::LoadFromCRSFile( CString sPath )
2002-07-02 17:34:20 +00:00
{
LOG->Trace( "Course::LoadFromCRSFile( '%s' )", sPath.c_str() );
m_sPath = sPath; // save path
2002-07-27 19:29:51 +00:00
MsdFile msd;
if( !msd.ReadFile(sPath) )
2003-04-25 00:01:35 +00:00
RageException::Throw( "Error opening CRS file '%s'.", sPath.c_str() );
2002-07-02 17:34:20 +00:00
2002-07-27 19:29:51 +00:00
CString sDir, sFName, sExt;
splitrelpath( sPath, sDir, sFName, sExt );
sFName = sDir + sFName;
2002-07-02 17:34:20 +00:00
2002-07-27 19:29:51 +00:00
CStringArray arrayPossibleBanners;
GetDirListing( sFName + ".png", arrayPossibleBanners, false, true );
GetDirListing( sFName + ".jpg", arrayPossibleBanners, false, true );
GetDirListing( sFName + ".bmp", arrayPossibleBanners, false, true );
GetDirListing( sFName + ".gif", arrayPossibleBanners, false, true );
2002-10-31 02:11:52 +00:00
if( !arrayPossibleBanners.empty() )
2003-06-15 02:07:31 +00:00
{
2002-07-27 19:29:51 +00:00
m_sBannerPath = arrayPossibleBanners[0];
2002-07-02 17:34:20 +00:00
2003-06-15 02:07:31 +00:00
/* Cache and load the course banner. */
BANNERCACHE->CacheBanner( m_sBannerPath );
}
2003-01-14 22:10:04 +00:00
for( unsigned i=0; i<msd.GetNumValues(); i++ )
2002-07-02 17:34:20 +00:00
{
2003-01-14 22:44:30 +00:00
CString sValueName = msd.GetParam(i, 0);
const MsdFile::value_t &sParams = msd.GetValue(i);
2002-07-02 17:34:20 +00:00
2002-07-27 19:29:51 +00:00
// handle the data
if( 0 == stricmp(sValueName, "COURSE") )
{
2002-07-27 19:29:51 +00:00
m_sName = sParams[1];
m_sTranslitName = m_sName;
}
2002-07-27 19:29:51 +00:00
else if( 0 == stricmp(sValueName, "REPEAT") )
2002-07-02 17:34:20 +00:00
{
2003-01-14 22:44:30 +00:00
CString str = sParams[1];
str.MakeLower();
if( str.Find("yes") != -1 )
2002-07-27 19:29:51 +00:00
m_bRepeat = true;
2002-07-02 17:34:20 +00:00
}
2002-07-27 19:29:51 +00:00
else if( 0 == stricmp(sValueName, "LIVES") )
m_iLives = atoi( sParams[1] );
2002-07-02 17:34:20 +00:00
2003-07-21 22:33:01 +00:00
else if( 0 == stricmp(sValueName, "METER") )
m_iMeter = atoi( sParams[1] );
2002-07-27 19:29:51 +00:00
else if( 0 == stricmp(sValueName, "SONG") )
2002-07-02 17:34:20 +00:00
{
2003-08-10 23:48:10 +00:00
CourseEntry new_entry;
// infer entry::Type from the first param
2003-04-14 07:11:04 +00:00
if( sParams[1].Left(strlen("BEST")) == "BEST" )
{
2003-08-10 23:48:10 +00:00
new_entry.type = COURSE_ENTRY_BEST;
2003-04-14 07:11:04 +00:00
new_entry.players_index = atoi( sParams[1].Right(sParams[1].size()-strlen("BEST")) ) - 1;
CLAMP( new_entry.players_index, 0, 500 );
}
2003-04-14 07:11:04 +00:00
else if( sParams[1].Left(strlen("WORST")) == "WORST" )
{
2003-08-10 23:48:10 +00:00
new_entry.type = COURSE_ENTRY_WORST;
2003-04-14 07:11:04 +00:00
new_entry.players_index = atoi( sParams[1].Right(sParams[1].size()-strlen("WORST")) ) - 1;
CLAMP( new_entry.players_index, 0, 500 );
}
else if( sParams[1] == "*" )
{
new_entry.mystery = true;
2003-08-10 23:48:10 +00:00
new_entry.type = COURSE_ENTRY_RANDOM;
}
else if( sParams[1].Right(1) == "*" )
{
new_entry.mystery = true;
2003-08-10 23:48:10 +00:00
new_entry.type = COURSE_ENTRY_RANDOM_WITHIN_GROUP;
CString sSong = sParams[1];
sSong.Replace( "\\", "/" );
CStringArray bits;
split( sSong, "/", bits );
if( bits.size() == 2 )
new_entry.group_name = bits[0];
else
LOG->Warn( "Course file '%s' contains a random_within_group entry '%s' that is invalid. "
"Song should be in the format '<group>/*'.",
2003-04-25 00:01:35 +00:00
m_sPath.c_str(), sSong.c_str());
if( !SONGMAN->DoesGroupExist(new_entry.group_name) )
{
/* XXX: We need a place to put "user warnings". This is too loud for info.txt--
* it obscures important warnings--and regular users never look there, anyway. */
//LOG->Warn( "Course file '%s' random_within_group entry '%s' specifies a group that doesn't exist. "
// "This entry will be ignored.",
// m_sPath.c_str(), sSong.c_str());
continue; // skip this #SONG
}
}
else
{
2003-08-10 23:48:10 +00:00
new_entry.type = COURSE_ENTRY_FIXED;
CString sSong = sParams[1];
new_entry.pSong = SONGMAN->FindSong( sSong );
if( new_entry.pSong == NULL )
{
/* XXX: We need a place to put "user warnings". This is too loud for info.txt--
* it obscures important warnings--and regular users never look there, anyway. */
LOG->Trace( "Course file '%s' contains a fixed song entry '%s' that does not exist. "
"This entry will be ignored.",
m_sPath.c_str(), sSong.c_str());
continue; // skip this #SONG
}
}
new_entry.difficulty = StringToDifficulty( sParams[2] );
if( new_entry.difficulty == DIFFICULTY_INVALID )
{
int retval = sscanf( sParams[2], "%d..%d", &new_entry.low_meter, &new_entry.high_meter );
if( retval == 1 )
new_entry.high_meter = new_entry.low_meter;
else if( retval != 2 )
{
LOG->Warn("Course file '%s' contains an invalid difficulty setting: \"%s\", 3..6 used instead",
m_sPath.c_str(), sParams[2].c_str());
new_entry.low_meter = 3;
new_entry.high_meter = 6;
}
2002-10-12 19:30:15 +00:00
}
{
/* If "showcourse" or "noshowcourse" is in the list, force new_entry.mystery
* on or off. */
CStringArray mods;
split( sParams[3], ",", mods, true );
for( int j = (int) mods.size()-1; j >= 0 ; --j )
{
if( !mods[j].CompareNoCase("showcourse") )
new_entry.mystery = false;
else if( !mods[j].CompareNoCase("noshowcourse") )
new_entry.mystery = true;
else
continue;
mods.erase(mods.begin() + j);
}
new_entry.modifiers = join( ",", mods );
}
m_entries.push_back( new_entry );
2002-07-02 17:34:20 +00:00
}
else
2003-04-25 00:01:35 +00:00
LOG->Trace( "Unexpected value named '%s'", sValueName.c_str() );
2002-07-02 17:34:20 +00:00
}
static TitleSubst tsub("courses");
2003-02-10 23:17:57 +00:00
CString ignore;
tsub.Subst(m_sName, ignore, ignore,
ignore, ignore, ignore);
2003-06-30 08:06:47 +00:00
SetDefaultScore();
2002-07-02 17:34:20 +00:00
}
2002-07-23 01:41:40 +00:00
2003-04-11 00:12:22 +00:00
void Course::Save()
{
ASSERT( !m_bIsAutogen );
2003-07-22 07:56:46 +00:00
FILE* fp = Ragefopen( m_sPath, "w" );
2003-04-11 00:12:22 +00:00
if( fp == NULL )
{
2003-04-25 00:01:35 +00:00
LOG->Warn( "Could not write course file '%s'.", m_sPath.c_str() );
2003-04-11 00:12:22 +00:00
return;
}
2003-04-25 00:01:35 +00:00
fprintf( fp, "#COURSE:%s;\n", m_sName.c_str() );
2003-04-11 00:12:22 +00:00
fprintf( fp, "#REPEAT:%s;\n", m_bRepeat ? "YES" : "NO" );
fprintf( fp, "#LIVES:%i;\n", m_iLives );
2003-07-21 22:33:01 +00:00
fprintf( fp, "#METER:%i;\n", m_iMeter );
2003-04-11 00:12:22 +00:00
for( unsigned i=0; i<m_entries.size(); i++ )
{
2003-08-10 23:48:10 +00:00
const CourseEntry& entry = m_entries[i];
2003-04-11 00:12:22 +00:00
switch( entry.type )
{
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_FIXED:
2003-04-25 00:01:35 +00:00
fprintf( fp, "#SONG:%s", entry.pSong->GetSongDir().c_str() );
2003-04-11 00:12:22 +00:00
break;
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_RANDOM:
2003-04-11 00:12:22 +00:00
fprintf( fp, "#SONG:*" );
break;
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_RANDOM_WITHIN_GROUP:
fprintf( fp, "#SONG:%s/*", entry.group_name.c_str() );
2003-04-11 00:12:22 +00:00
break;
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_BEST:
2003-04-14 07:11:04 +00:00
fprintf( fp, "#SONG:BEST%d", entry.players_index+1 );
2003-04-11 00:12:22 +00:00
break;
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_WORST:
2003-04-14 07:11:04 +00:00
fprintf( fp, "#SONG:WORST%d", entry.players_index+1 );
2003-04-11 00:12:22 +00:00
break;
default:
ASSERT(0);
}
fprintf( fp, ":" );
2003-04-11 00:12:22 +00:00
if( entry.difficulty != DIFFICULTY_INVALID )
fprintf( fp, "%s", DifficultyToString(entry.difficulty).c_str() );
2003-04-11 00:12:22 +00:00
fprintf( fp, ":" );
2003-04-11 00:12:22 +00:00
if( entry.low_meter != -1 && entry.high_meter != -1 )
fprintf( fp, "%d..%d", entry.low_meter, entry.high_meter );
fprintf( fp, ":%s", entry.modifiers.c_str() );
2003-08-10 23:48:10 +00:00
bool default_mystery = !(entry.type == COURSE_ENTRY_RANDOM || entry.type == COURSE_ENTRY_RANDOM_WITHIN_GROUP);
if( default_mystery != entry.mystery )
{
if( entry.modifiers != "" )
fprintf( fp, "," );
fprintf( fp, entry.mystery? "noshowcourse":"showcourse" );
}
2003-04-11 00:12:22 +00:00
fprintf( fp, ";\n" );
}
fclose( fp );
}
void Course::AutogenEndlessFromGroup( CString sGroupName, vector<Song*> &apSongsInGroup )
2002-07-23 01:41:40 +00:00
{
m_bIsAutogen = true;
2002-07-29 03:06:55 +00:00
m_bRepeat = true;
m_bRandomize = true;
m_iLives = -1;
2003-07-21 22:33:01 +00:00
m_iMeter = -1;
m_sName = SONGMAN->ShortenGroupName( sGroupName );
m_sBannerPath = SONGMAN->GetGroupBannerPath( sGroupName );
2002-07-23 01:41:40 +00:00
// We want multiple songs, so we can try to prevent repeats during
// gameplay. (We might still get a repeat at the repeat boundary,
// but that'd be rare.) -glenn
2003-08-10 23:48:10 +00:00
CourseEntry e;
e.type = COURSE_ENTRY_RANDOM_WITHIN_GROUP;
e.group_name = sGroupName;
e.difficulty = DIFFICULTY_MEDIUM;
2003-08-06 08:05:41 +00:00
e.mystery = true;
vector<Song*> vSongs;
SONGMAN->GetSongs( vSongs, e.group_name );
for( unsigned i = 0; i < vSongs.size(); ++i)
m_entries.push_back( e );
2003-06-30 08:06:47 +00:00
SetDefaultScore();
2002-07-23 01:41:40 +00:00
}
void Course::AutogenNonstopFromGroup( CString sGroupName, vector<Song*> &apSongsInGroup )
{
AutogenEndlessFromGroup( sGroupName, apSongsInGroup );
m_bRepeat = false;
m_sName += " Random";
// resize to 4
while( m_entries.size() < 4 )
m_entries.push_back( m_entries[0] );
while( m_entries.size() > 4 )
m_entries.pop_back();
2003-06-30 08:06:47 +00:00
SetDefaultScore();
}
2003-07-17 22:44:17 +00:00
/*
* Difficult courses do the following:
*
* For entries with a meter range, bump it up by DIFFICULT_METER_CHANGE;
* eg. 3..6 -> 5..8, with a minimum no higher than MAX_BOTTOM_RANGE.
*
* For entries with a difficulty class, use notes one class harder, if they
* exist. This way, if a static song entry points to a difficulty, we'll always
* play that song, even if we're on difficult and harder notes don't exist. (The
* exception is a static song entry with a meter range, but that's not very useful.)
*/
2003-08-07 06:16:17 +00:00
bool Course::HasDifficult( StepsType nt ) const
{
2003-07-21 21:54:07 +00:00
/* Check to see if any songs would change if difficult. */
2003-07-17 22:44:17 +00:00
2003-07-21 21:54:07 +00:00
vector<Info> Normal, Hard;
2003-07-27 19:14:05 +00:00
GetCourseInfo( nt, Normal, false );
GetCourseInfo( nt, Hard, true );
2003-07-17 22:44:17 +00:00
2003-07-21 21:54:07 +00:00
if( Normal.size() != Hard.size() )
2003-07-17 22:44:17 +00:00
return true; /* it changed */
2003-07-21 21:54:07 +00:00
for( unsigned i=0; i<Normal.size(); i++ )
{
if( Normal[i].CourseIndex != Hard[i].CourseIndex )
return true; /* it changed */
if( Normal[i].Mystery )
2003-07-21 21:54:07 +00:00
{
2003-08-10 23:48:10 +00:00
const CourseEntry &e = m_entries[ Normal[i].CourseIndex ];
2003-07-21 21:54:07 +00:00
/* Difficulties under CHALLENGE change by getting harder. */
if( e.difficulty < DIFFICULTY_CHALLENGE )
return true;
/* Meters under MAX_BOTTOM_RANGE..MAX_BOTTOM_RANGE change by getting harder. */
if( e.difficulty != DIFFICULTY_INVALID &&
e.low_meter < MAX_BOTTOM_RANGE &&
e.high_meter < MAX_BOTTOM_RANGE )
return true;
continue;
}
2003-07-21 23:16:06 +00:00
if( Normal[i].pSong != Hard[i].pSong || Normal[i].pNotes != Hard[i].pNotes )
2003-07-17 22:44:17 +00:00
return true;
2003-07-21 21:54:07 +00:00
}
2003-07-17 22:44:17 +00:00
return false;
}
2002-07-23 01:41:40 +00:00
2003-08-07 06:16:17 +00:00
bool Course::IsPlayableIn( StepsType nt ) const
{
2003-07-21 21:54:07 +00:00
vector<Info> ci;
GetCourseInfo( nt, ci );
return ci.size() > 0;
}
2003-08-07 06:16:17 +00:00
static vector<Song*> GetFilteredBestSongs( StepsType nt )
2003-08-04 21:34:45 +00:00
{
vector<Song*> vSongsByMostPlayed = SONGMAN->GetBestSongs();
// filter out songs that don't have both medium and hard steps and long ver sons
for( int j=vSongsByMostPlayed.size()-1; j>=0; j-- )
{
Song* pSong = vSongsByMostPlayed[j];
if( pSong->m_fMusicLengthSeconds > PREFSMAN->m_fLongVerSongSeconds ||
pSong->m_fMusicLengthSeconds > PREFSMAN->m_fMarathonVerSongSeconds ||
!pSong->GetStepsByDifficulty(nt, DIFFICULTY_MEDIUM, PREFSMAN->m_bAutogenMissingTypes) ||
!pSong->GetStepsByDifficulty(nt, DIFFICULTY_HARD, PREFSMAN->m_bAutogenMissingTypes) )
2003-08-04 21:34:45 +00:00
vSongsByMostPlayed.erase( vSongsByMostPlayed.begin()+j );
}
return vSongsByMostPlayed;
}
2003-08-07 06:16:17 +00:00
void Course::GetCourseInfo( StepsType nt, vector<Course::Info> &ci, int Difficult ) const
2002-07-23 01:41:40 +00:00
{
2003-08-10 23:48:10 +00:00
vector<CourseEntry> entries = m_entries;
/* Different seed for each course, but the same for the whole round: */
RandomGen rnd( GAMESTATE->m_iRoundSeed + GetHashForString(m_sName) );
if( m_bRandomize )
{
/* Always randomize the same way per round. Otherwise, the displayed course
* will change every time it's viewed, and the displayed order will have no
* bearing on what you'll actually play. */
random_shuffle( entries.begin(), entries.end(), rnd );
}
2003-08-04 21:34:45 +00:00
/* This can take some time, so don't fill it out unless we need it. */
bool bMostPlayedSet = false;
vector<Song*> vSongsByMostPlayed;
vector<Song*> AllSongsShuffled = SONGMAN->GetAllSongs();
random_shuffle( AllSongsShuffled.begin(), AllSongsShuffled.end(), rnd );
int CurSong = 0; /* Current offset into AllSongsShuffled */
2003-07-21 21:54:07 +00:00
ci.clear();
for( unsigned i=0; i<entries.size(); i++ )
{
2003-08-10 23:48:10 +00:00
const CourseEntry &e = entries[i];
Song* pSong = NULL; // fill this in
2003-08-03 00:13:55 +00:00
Steps* pNotes = NULL; // fill this in
2003-07-17 22:44:17 +00:00
/* This applies difficult mode for meter ranges. (If it's a difficulty
* class, we'll do it below.) */
int low_meter, high_meter;
2003-07-27 19:14:05 +00:00
GetMeterRange( i, low_meter, high_meter, Difficult );
2002-07-23 01:41:40 +00:00
switch( e.type )
{
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_FIXED:
pSong = e.pSong;
if( pSong )
{
if( e.difficulty == DIFFICULTY_INVALID )
pNotes = pSong->GetStepsByMeter( nt, low_meter, high_meter, PREFSMAN->m_bAutogenMissingTypes );
else
pNotes = pSong->GetStepsByDifficulty( nt, e.difficulty, PREFSMAN->m_bAutogenMissingTypes );
}
break;
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_RANDOM:
case COURSE_ENTRY_RANDOM_WITHIN_GROUP:
{
// find a song with the notes we want
for( unsigned j=0; j<AllSongsShuffled.size(); j++ )
{
/* See if the first song matches what we want. */
2003-08-01 12:12:02 +00:00
ASSERT( unsigned(CurSong) < AllSongsShuffled.size() );
pSong = AllSongsShuffled[CurSong];
ASSERT( pSong );
CurSong = (CurSong+1) % AllSongsShuffled.size();
2003-08-10 23:48:10 +00:00
if(e.type == COURSE_ENTRY_RANDOM_WITHIN_GROUP &&
pSong->m_sGroupName.CompareNoCase(e.group_name))
continue; /* wrong group */
if( e.difficulty == DIFFICULTY_INVALID )
pNotes = pSong->GetStepsByMeter( nt, low_meter, high_meter, PREFSMAN->m_bAutogenMissingTypes );
else
pNotes = pSong->GetStepsByDifficulty( nt, e.difficulty, PREFSMAN->m_bAutogenMissingTypes );
if( pNotes ) // found a match
break; // stop searching
pSong = NULL;
pNotes = NULL;
}
}
break;
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_BEST:
case COURSE_ENTRY_WORST:
{
2003-08-04 21:34:45 +00:00
if( !bMostPlayedSet )
{
bMostPlayedSet = true;
vSongsByMostPlayed = GetFilteredBestSongs( nt );
}
if( e.players_index >= (int)vSongsByMostPlayed.size() )
break;
switch( e.type )
{
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_BEST:
pSong = vSongsByMostPlayed[e.players_index];
break;
2003-08-10 23:48:10 +00:00
case COURSE_ENTRY_WORST:
pSong = vSongsByMostPlayed[vSongsByMostPlayed.size()-1-e.players_index];
break;
default:
ASSERT(0);
}
if( e.difficulty == DIFFICULTY_INVALID )
pNotes = pSong->GetStepsByMeter( nt, low_meter, high_meter, PREFSMAN->m_bAutogenMissingTypes );
else
pNotes = pSong->GetStepsByDifficulty( nt, e.difficulty, PREFSMAN->m_bAutogenMissingTypes );
if( pNotes == NULL )
pNotes = pSong->GetClosestNotes( nt, DIFFICULTY_MEDIUM );
}
break;
default:
ASSERT(0);
}
if( !pSong || !pNotes )
continue; // this song entry isn't playable. Skip.
2003-07-17 22:44:17 +00:00
/* If e.difficulty == DIFFICULTY_INVALID, then we already increased difficulty
* based on meter. */
2003-07-27 19:14:05 +00:00
if( IsDifficult(Difficult) && e.difficulty != DIFFICULTY_INVALID )
{
/* See if we can find a NoteData that's one notch more difficult than
* the one we found above. */
Difficulty dc = pNotes->GetDifficulty();
if(dc < DIFFICULTY_CHALLENGE)
{
dc = Difficulty(dc + 1);
Steps* pNewNotes = pSong->GetStepsByDifficulty( nt, dc, PREFSMAN->m_bAutogenMissingTypes );
if(pNewNotes)
pNotes = pNewNotes;
}
}
2003-07-21 21:54:07 +00:00
Info cinfo;
2003-07-21 23:16:06 +00:00
cinfo.pSong = pSong;
cinfo.pNotes = pNotes;
2003-07-21 21:54:07 +00:00
cinfo.Modifiers = e.modifiers;
2003-08-10 23:48:10 +00:00
cinfo.Random = ( e.type == COURSE_ENTRY_RANDOM || e.type == COURSE_ENTRY_RANDOM_WITHIN_GROUP );
cinfo.Mystery = e.mystery;
2003-07-21 21:59:10 +00:00
cinfo.CourseIndex = i;
2003-07-27 19:14:05 +00:00
cinfo.Difficult = IsDifficult(Difficult);
2003-07-21 21:54:07 +00:00
ci.push_back( cinfo );
}
2002-07-27 19:29:51 +00:00
}
RageColor Course::GetColor() const
2002-07-27 19:29:51 +00:00
{
2003-07-27 14:54:56 +00:00
switch (PREFSMAN->m_iCourseSortOrder)
{
2003-07-27 19:14:05 +00:00
case PrefsManager::COURSE_SORT_SONGS:
2003-07-27 14:54:56 +00:00
if( m_entries.size() >= 7 )
return RageColor(1,0,0,1); // red
else if( m_entries.size() >= 4 )
return RageColor(1,1,0,1); // yellow
else
return RageColor(0,1,0,1); // green
// never should get here
break;
2003-07-27 19:14:05 +00:00
case PrefsManager::COURSE_SORT_METER:
2003-08-12 00:28:57 +00:00
if (GetMeter() > 100)
2003-07-27 14:54:56 +00:00
return RageColor(0,0,1,1); // blue
2003-08-12 00:28:57 +00:00
if (GetMeter() > 8.5)
2003-07-27 14:54:56 +00:00
return RageColor(1,0,0,1); // red
2003-08-12 00:28:57 +00:00
if (GetMeter() >= 7)
2003-07-27 14:54:56 +00:00
return RageColor(1,0.5f,0,1); // orange
2003-08-12 00:28:57 +00:00
if (GetMeter() >= 5)
2003-07-27 14:54:56 +00:00
return RageColor(1,1,0,1); // yellow
return RageColor(0,1,0,1); // green
2003-07-27 19:14:05 +00:00
case PrefsManager::COURSE_SORT_METER_SUM:
2003-07-27 14:54:56 +00:00
if (SortOrder_TotalDifficulty > 100000)
return RageColor(0,0,1,1); // blue
if (SortOrder_TotalDifficulty >= 40)
return RageColor(1,0,0,1); // red
if (SortOrder_TotalDifficulty >= 30)
return RageColor(1,0.5f,0,1); // orange
if (SortOrder_TotalDifficulty >= 20)
return RageColor(1,1,0,1); // yellow
return RageColor(0,1,0,1); // green
2003-07-27 19:14:05 +00:00
case PrefsManager::COURSE_SORT_RANK:
2003-07-27 14:54:56 +00:00
if (SortOrder_Ranking == 3)
return RageColor(0,0,1,1); // blue
if (SortOrder_Ranking == 2)
return RageColor(1,0.5f,0,1); // orange
if (SortOrder_Ranking == 1)
return RageColor(0,1,0,1); // green
return RageColor(1,1,0,1); // yellow, never should get here
2003-07-27 19:14:05 +00:00
default:
ASSERT(0);
return RageColor(1,1,1,1); // white, never should reach here
2003-07-27 14:54:56 +00:00
}
2002-07-27 19:29:51 +00:00
}
Difficulty Course::GetDifficulty( const Info &stage ) const
{
Difficulty dc = m_entries[stage.CourseIndex].difficulty;
2003-04-20 20:38:17 +00:00
2003-07-27 19:14:05 +00:00
if( stage.Difficult && dc < DIFFICULTY_CHALLENGE )
dc = Difficulty(dc + 1);
return dc;
}
2003-07-27 19:14:05 +00:00
void Course::GetMeterRange( int stage, int& iMeterLowOut, int& iMeterHighOut, int Difficult ) const
{
iMeterLowOut = m_entries[stage].low_meter;
iMeterHighOut = m_entries[stage].high_meter;
2003-07-17 22:44:17 +00:00
2003-07-27 19:14:05 +00:00
if( m_entries[stage].difficulty == DIFFICULTY_INVALID && IsDifficult(Difficult) )
2003-07-17 22:44:17 +00:00
{
iMeterHighOut += DIFFICULT_METER_CHANGE;
iMeterLowOut += DIFFICULT_METER_CHANGE;
iMeterLowOut = min( iMeterLowOut, MAX_BOTTOM_RANGE );
}
2002-07-28 20:28:37 +00:00
}
void Course::GetMeterRange( const Info &stage, int& iMeterLowOut, int& iMeterHighOut ) const
{
2003-07-27 19:14:05 +00:00
GetMeterRange( stage.CourseIndex, iMeterLowOut, iMeterHighOut, stage.Difficult );
}
bool Course::GetTotalSeconds( float& fSecondsOut ) const
2002-12-17 05:23:45 +00:00
{
2003-07-21 21:54:07 +00:00
vector<Info> ci;
2003-08-07 06:16:17 +00:00
GetCourseInfo( STEPS_TYPE_DANCE_SINGLE, ci );
fSecondsOut = 0;
2003-07-21 21:54:07 +00:00
for( unsigned i=0; i<ci.size(); i++ )
{
if( ci[i].Mystery )
2003-07-21 21:54:07 +00:00
return false;
2003-07-21 23:16:06 +00:00
fSecondsOut += ci[i].pSong->m_fMusicLengthSeconds;
2003-07-21 21:54:07 +00:00
}
return true;
2002-12-17 05:23:45 +00:00
}
struct RankingToInsert
{
PlayerNumber pn;
2003-06-30 08:06:47 +00:00
int iScore;
2003-01-27 02:00:38 +00:00
float fSurviveTime;
2003-06-30 08:06:47 +00:00
static bool CompareDescending( const RankingToInsert &hs1, const RankingToInsert &hs2 )
{
2003-06-30 08:06:47 +00:00
return hs1.iScore > hs2.iScore;
}
static void SortDescending( vector<RankingToInsert>& vHSout )
{
sort( vHSout.begin(), vHSout.end(), CompareDescending );
}
};
2003-08-07 06:16:17 +00:00
void Course::AddScores( StepsType nt, bool bPlayerEnabled[NUM_PLAYERS], int iScore[NUM_PLAYERS], float fSurviveTime[NUM_PLAYERS], int iRankingIndexOut[NUM_PLAYERS], bool bNewRecordOut[NUM_PLAYERS] )
{
vector<RankingToInsert> vHS;
for( int p=0; p<NUM_PLAYERS; p++ )
{
2003-01-27 02:00:38 +00:00
iRankingIndexOut[p] = -1;
2003-02-16 07:54:41 +00:00
bNewRecordOut[p] = false;
if( !bPlayerEnabled[p] )
continue; // skip
// Update memory card
m_MemCardScores[p][nt].iNumTimesPlayed++;
m_MemCardScores[MEMORY_CARD_MACHINE][nt].iNumTimesPlayed++;
2003-06-30 08:06:47 +00:00
if( iScore[p] > m_MemCardScores[p][nt].iScore )
{
2003-06-30 08:06:47 +00:00
m_MemCardScores[p][nt].iScore = iScore[p];
m_MemCardScores[p][nt].fSurviveTime = fSurviveTime[p];
bNewRecordOut[p] = true;
}
2003-06-30 08:06:47 +00:00
if( iScore[p] > m_MemCardScores[MEMORY_CARD_MACHINE][nt].iScore )
{
2003-06-30 08:06:47 +00:00
m_MemCardScores[MEMORY_CARD_MACHINE][nt].iScore = iScore[p];
m_MemCardScores[MEMORY_CARD_MACHINE][nt].fSurviveTime = fSurviveTime[p];
}
// Update Ranking
RankingToInsert hs;
2003-06-30 08:06:47 +00:00
hs.iScore = iScore[p];
hs.fSurviveTime = fSurviveTime[p];
hs.pn = (PlayerNumber)p;
vHS.push_back( hs );
}
// Sort descending before inserting.
// This guarantees that a high score will not switch poitions on us when we later insert scores for the other player
RankingToInsert::SortDescending( vHS );
for( unsigned i=0; i<vHS.size(); i++ )
{
RankingToInsert& newHS = vHS[i];
RankingScore* rankingScores = m_RankingScores[nt];
2003-01-27 02:00:38 +00:00
for( int i=0; i<NUM_RANKING_LINES; i++ )
{
2003-06-30 08:06:47 +00:00
if( newHS.iScore > rankingScores[i].iScore )
{
// We found the insert point. Shift down.
for( int j=NUM_RANKING_LINES-1; j>i; j-- )
rankingScores[j] = rankingScores[j-1];
// insert
rankingScores[i].fSurviveTime = newHS.fSurviveTime;
2003-06-30 08:06:47 +00:00
rankingScores[i].iScore = newHS.iScore;
rankingScores[i].sName = DEFAULT_RANKING_NAME;
2003-01-27 02:00:38 +00:00
iRankingIndexOut[newHS.pn] = i;
break;
}
}
}
}
2002-07-27 19:29:51 +00:00
//
// 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);
}
2003-02-16 08:11:41 +00:00
static bool CompareCoursePointersByDifficulty(const Course* pCourse1, const Course* pCourse2)
2002-07-27 19:29:51 +00:00
{
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 );
2002-07-27 19:29:51 +00:00
}
static bool CompareCoursePointersByAvgDifficulty(const Course* pCourse1, const Course* pCourse2)
{
float fNum1 = pCourse1->GetMeter( false );
float fNum2 = pCourse2->GetMeter( false );
2003-08-09 22:08:17 +00:00
if( fNum1 < fNum2 )
return true;
2003-08-09 22:08:17 +00:00
else if( fNum1 > fNum2 )
return false;
2003-08-09 22:08:17 +00:00
else // fNum1 == fNum2
return ( pCourse1->m_sName < pCourse2->m_sName );
}
static bool CompareCoursePointersByTotalDifficulty(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->SortOrder_TotalDifficulty;
int iNum2 = pCourse2->SortOrder_TotalDifficulty;
if( iNum1 < iNum2 )
return true;
else if( iNum1 > iNum2 )
return false;
else // iNum1 == iNum2
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
}
bool Course::CourseHasBestOrWorst() const
{
for(unsigned i = 0; i < m_entries.size(); i++)
{
switch( m_entries[i].type )
{
case COURSE_ENTRY_BEST:
case COURSE_ENTRY_WORST:
return true;
}
}
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 CompareCoursePointersByRanking(const Course* pCourse1, const Course* pCourse2)
{
int iNum1 = pCourse1->SortOrder_Ranking;
int iNum2 = pCourse2->SortOrder_Ranking;
if( iNum1 < iNum2 )
return true;
else if( iNum1 > iNum2 )
return false;
else // iNum1 == iNum2
return CompareCoursePointersByAutogen( pCourse1, pCourse2 );
}
2003-01-03 05:56:28 +00:00
void SortCoursePointerArrayByDifficulty( vector<Course*> &apCourses )
2002-07-27 19:29:51 +00:00
{
2002-10-24 08:40:27 +00:00
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByDifficulty );
2002-07-27 19:29:51 +00:00
}
void SortCoursePointerArrayByRanking( vector<Course*> &apCourses )
{
2003-08-04 21:37:07 +00:00
for(unsigned i=0; i<apCourses.size(); i++)
apCourses[i]->UpdateCourseStats();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByRanking );
}
void SortCoursePointerArrayByAvgDifficulty( vector<Course*> &apCourses )
{
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByAvgDifficulty );
stable_sort( apCourses.begin(), apCourses.end(), MovePlayersBestToEnd );
}
void SortCoursePointerArrayByTotalDifficulty( vector<Course*> &apCourses )
{
2003-08-04 21:37:07 +00:00
for(unsigned i=0; i<apCourses.size(); i++)
apCourses[i]->UpdateCourseStats();
sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByTotalDifficulty );
}
2003-07-20 08:50:19 +00:00
static bool CompareCoursePointersByType(const Course* pCourse1, const Course* pCourse2)
{
return pCourse1->GetPlayMode() < pCourse2->GetPlayMode();
}
void SortCoursePointerArrayByType( vector<Course*> &apCourses )
{
stable_sort( apCourses.begin(), apCourses.end(), CompareCoursePointersByType );
}
bool Course::HasBanner() const
{
return m_sBannerPath != "" && IsAFile(m_sBannerPath);
}
void Course::UpdateCourseStats()
{
SortOrder_TotalDifficulty = 0;
unsigned i;
2003-08-04 20:45:34 +00:00
// courses with random/players best-worst songs should go at the end
for(i = 0; i < m_entries.size(); i++)
{
2003-08-10 23:48:10 +00:00
if ( m_entries[i].type == COURSE_ENTRY_FIXED )
2003-08-04 20:45:34 +00:00
continue;
if ( SortOrder_Ranking == 2 )
SortOrder_Ranking = 3;
SortOrder_TotalDifficulty = 999999; // large number
return;
}
vector<Info> ci;
2003-08-07 06:36:34 +00:00
GetCourseInfo( GAMESTATE->GetCurrentStyleDef()->m_StepsType, ci );
2003-08-04 20:45:34 +00:00
for( i = 0; i < ci.size(); i++ )
SortOrder_TotalDifficulty += ci[i].pNotes->GetMeter();
// OPTIMIZATION: Ranking info isn't dependant on style, so
// call it sparingly. Its handled on startup and when
// themes change..
2003-08-09 22:08:17 +00:00
LOG->Trace("%s: Total feet: %d, Average Difficulty: %f",
this->m_sName.c_str(),
2003-08-12 00:28:57 +00:00
SortOrder_TotalDifficulty );
2003-08-07 10:29:40 +00:00
}