Files
itgmania212121/stepmania/src/Steps.cpp
T

353 lines
8.6 KiB
C++
Raw Normal View History

/*
* This stores a single note pattern for a song.
*
* We can have too much data to keep everything decompressed as NoteData, so most
* songs are kept in memory compressed as SMData until requested. NoteData is normally
* not requested casually during gameplay; we can move through screens, the music
* wheel, etc. without touching any NoteData.
*
* To save more memory, if data is cached on disk, read it from disk on demand. Not
* all Steps will have an associated file for this purpose. (Profile edits don't do
* this yet.)
*
* Data can be on disk (always compressed), compressed in memory, and uncompressed in
* memory.
*/
2003-02-16 04:01:45 +00:00
#include "global.h"
2003-08-03 00:57:20 +00:00
#include "Steps.h"
#include "StepsUtil.h"
2003-02-16 04:28:17 +00:00
#include "song.h"
2003-08-03 00:57:20 +00:00
#include "Steps.h"
2002-05-20 08:59:37 +00:00
#include "IniFile.h"
#include "RageUtil.h"
#include "RageLog.h"
2002-07-03 03:13:13 +00:00
#include "NoteData.h"
2002-05-20 08:59:37 +00:00
#include "GameInput.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
#include "MsdFile.h"
2002-08-23 00:22:27 +00:00
#include "GameManager.h"
2003-08-07 06:36:34 +00:00
#include "NoteDataUtil.h"
#include "ProfileManager.h"
#include "PrefsManager.h"
#include "NotesLoaderSM.h"
2002-05-20 08:59:37 +00:00
const int MAX_DESCRIPTION_LENGTH = 20;
2003-08-03 00:57:20 +00:00
Steps::Steps()
2002-05-20 08:59:37 +00:00
{
2004-02-07 05:57:19 +00:00
m_StepsType = STEPS_TYPE_INVALID;
2004-02-08 01:05:53 +00:00
m_LoadedFromProfile = PROFILE_SLOT_INVALID;
m_uHash = 0;
m_Difficulty = DIFFICULTY_INVALID;
2002-05-20 08:59:37 +00:00
m_iMeter = 0;
2002-06-30 23:19:33 +00:00
2002-12-14 21:43:00 +00:00
notes = NULL;
2004-10-23 23:41:49 +00:00
notes_comp = "";
2003-01-02 22:10:51 +00:00
parent = NULL;
2002-05-20 08:59:37 +00:00
}
2003-08-03 00:57:20 +00:00
Steps::~Steps()
2002-05-20 08:59:37 +00:00
{
2004-10-23 23:41:49 +00:00
SAFE_DELETE( notes );
2002-05-20 08:59:37 +00:00
}
2004-10-23 17:43:49 +00:00
void Steps::SetNoteData( const NoteData& noteDataNew )
2002-07-03 21:27:26 +00:00
{
2004-10-23 17:43:49 +00:00
ASSERT( noteDataNew.GetNumTracks() == GameManager::StepsTypeToNumTracks(m_StepsType) );
2002-12-14 21:43:00 +00:00
2003-01-02 22:10:51 +00:00
DeAutogen();
2004-10-23 23:41:49 +00:00
SAFE_DELETE( notes );
2004-10-23 17:43:49 +00:00
notes = new NoteData( noteDataNew );
2004-10-23 23:41:49 +00:00
NoteDataUtil::GetSMNoteDataString( *notes, notes_comp );
m_uHash = GetHashForString( notes_comp );
2002-07-03 21:27:26 +00:00
}
2002-05-20 08:59:37 +00:00
2004-10-23 17:43:49 +00:00
void Steps::GetNoteData( NoteData& noteDataOut ) const
2002-07-03 03:13:13 +00:00
{
2003-01-19 21:14:27 +00:00
ASSERT(this);
2003-07-15 19:40:41 +00:00
2002-12-14 21:43:00 +00:00
Decompress();
2003-07-15 19:40:41 +00:00
if( notes != NULL )
2004-10-23 17:43:49 +00:00
noteDataOut = *notes;
2003-07-15 19:40:41 +00:00
else
{
2004-10-23 17:43:49 +00:00
noteDataOut.ClearAll();
noteDataOut.SetNumTracks( GameManager::StepsTypeToNumTracks(m_StepsType) );
2003-07-15 19:40:41 +00:00
}
2002-07-03 03:13:13 +00:00
}
2002-05-20 08:59:37 +00:00
2004-10-23 23:41:49 +00:00
void Steps::SetSMNoteData( const CString &notes_comp_ )
2002-12-14 21:33:29 +00:00
{
2004-10-23 23:41:49 +00:00
SAFE_DELETE( notes );
2002-12-14 21:43:00 +00:00
2004-10-23 23:41:49 +00:00
notes_comp = notes_comp_;
m_uHash = GetHashForString( notes_comp );
2002-12-14 21:33:29 +00:00
}
2004-10-30 07:42:03 +00:00
/* XXX: this function should pull data from cache, like Decompress() */
2004-10-23 23:41:49 +00:00
void Steps::GetSMNoteData( CString &notes_comp_out ) const
2002-12-14 21:33:29 +00:00
{
2004-10-30 07:40:31 +00:00
if( notes_comp.empty() )
2002-12-14 21:43:00 +00:00
{
2004-10-23 23:41:49 +00:00
if( !notes )
2003-11-17 03:38:24 +00:00
{
/* no data is no data */
2004-10-23 23:41:49 +00:00
notes_comp_out = "";
2003-11-17 03:38:24 +00:00
return;
}
2004-10-23 23:41:49 +00:00
NoteDataUtil::GetSMNoteDataString( *notes, notes_comp );
2002-12-14 21:43:00 +00:00
}
2004-10-23 23:41:49 +00:00
notes_comp_out = notes_comp;
2002-12-14 21:33:29 +00:00
}
2003-12-18 21:24:30 +00:00
float Steps::PredictMeter() const
2003-12-18 20:35:46 +00:00
{
2003-12-19 03:18:52 +00:00
float pMeter = 0.775f;
const float RadarCoeffs[NUM_RADAR_CATEGORIES] =
2003-12-19 04:11:11 +00:00
{
10.1f, 5.27f,-0.905f, -1.10f, 2.86f,
0,0,0,0,0
};
2003-12-18 21:23:52 +00:00
for( int r = 0; r < NUM_RADAR_CATEGORIES; ++r )
2003-12-19 03:18:52 +00:00
pMeter += this->GetRadarValues()[r] * RadarCoeffs[r];
const float DifficultyCoeffs[NUM_DIFFICULTIES] =
2003-12-19 04:11:11 +00:00
{
2004-02-08 01:05:53 +00:00
-0.877f, -0.877f, 0, 0.722f, 0.722f, 0
2003-12-19 04:11:11 +00:00
};
2003-12-19 03:35:23 +00:00
pMeter += DifficultyCoeffs[this->GetDifficulty()];
2003-12-19 03:18:52 +00:00
2003-12-18 21:23:52 +00:00
// Init non-radar values
const float SV = this->GetRadarValues()[RADAR_STREAM] * this->GetRadarValues()[RADAR_VOLTAGE];
const float ChaosSquare = this->GetRadarValues()[RADAR_CHAOS] * this->GetRadarValues()[RADAR_CHAOS];
2003-12-19 03:35:23 +00:00
pMeter += -6.35f * SV;
pMeter += -2.58f * ChaosSquare;
2003-12-22 18:26:17 +00:00
if (pMeter < 1) pMeter = 1;
2003-12-18 20:35:46 +00:00
return pMeter;
}
2003-08-03 00:57:20 +00:00
void Steps::TidyUpData()
2002-10-06 16:56:58 +00:00
{
if( GetDifficulty() == DIFFICULTY_INVALID )
2003-12-03 00:28:17 +00:00
SetDifficulty( StringToDifficulty(GetDescription()) );
if( GetDifficulty() == DIFFICULTY_INVALID )
{
2003-12-03 00:28:17 +00:00
if( GetMeter() == 1 ) SetDifficulty( DIFFICULTY_BEGINNER );
else if( GetMeter() <= 3 ) SetDifficulty( DIFFICULTY_EASY );
else if( GetMeter() <= 6 ) SetDifficulty( DIFFICULTY_MEDIUM );
else SetDifficulty( DIFFICULTY_HARD );
2002-10-06 16:56:58 +00:00
}
2003-12-03 00:28:17 +00:00
if( GetMeter() < 1) // meter is invalid
2003-12-19 07:28:32 +00:00
SetMeter( int(PredictMeter()) );
2004-08-29 11:04:13 +00:00
if( int(m_sDescription.size()) > MAX_DESCRIPTION_LENGTH )
m_sDescription = m_sDescription.Left( MAX_DESCRIPTION_LENGTH );
2002-05-20 08:59:37 +00:00
}
2003-08-03 00:57:20 +00:00
void Steps::Decompress() const
2002-12-14 21:43:00 +00:00
{
2003-03-13 01:26:50 +00:00
if(notes)
return; // already decompressed
if(parent)
2003-01-02 22:10:51 +00:00
{
2003-03-13 01:26:50 +00:00
// get autogen notes
2004-10-23 17:43:49 +00:00
NoteData notedata;
parent->GetNoteData( notedata );
2003-01-02 22:10:51 +00:00
notes = new NoteData;
2004-07-12 02:19:24 +00:00
int iNewTracks = GameManager::StepsTypeToNumTracks(m_StepsType);
2004-05-20 19:05:37 +00:00
if( this->m_StepsType == STEPS_TYPE_LIGHTS_CABINET )
{
2004-10-23 17:43:49 +00:00
NoteDataUtil::LoadTransformedLights( notedata, *notes, iNewTracks );
2004-05-20 19:05:37 +00:00
} else {
2004-10-23 17:43:49 +00:00
NoteDataUtil::LoadTransformedSlidingWindow( notedata, *notes, iNewTracks );
2004-05-20 19:05:37 +00:00
NoteDataUtil::FixImpossibleRows( *notes, m_StepsType );
}
return;
}
2004-10-30 07:40:31 +00:00
if( !m_sFilename.empty() && notes_comp.empty() )
{
/* We have data on disk and not in memory. Load it. */
Song s;
SMLoader ld;
if( !ld.LoadFromSMFile( m_sFilename, s, true ) )
{
LOG->Warn( "Couldn't load \"%s\"", m_sFilename.c_str() );
return;
}
/* Find the steps. */
StepsID ID;
ID.FromSteps( this );
2004-08-12 04:49:15 +00:00
Steps *pSteps = ID.ToSteps( &s, true, false ); // don't use cache
if( pSteps == NULL )
{
LOG->Warn( "Couldn't find %s in \"%s\"", ID.ToString().c_str(), m_sFilename.c_str() );
return;
}
2004-10-23 23:41:49 +00:00
pSteps->GetSMNoteData( notes_comp );
2003-01-02 22:10:51 +00:00
}
2004-10-30 07:40:31 +00:00
if( notes_comp.empty() )
2003-03-13 01:26:50 +00:00
{
2003-08-07 06:16:17 +00:00
/* there is no data, do nothing */
2003-03-13 01:26:50 +00:00
}
else
{
// load from compressed
notes = new NoteData;
2004-07-12 02:19:24 +00:00
notes->SetNumTracks( GameManager::StepsTypeToNumTracks(m_StepsType) );
2003-01-02 22:10:51 +00:00
2004-10-23 23:41:49 +00:00
NoteDataUtil::LoadFromSMNoteDataString( *notes, notes_comp );
2003-03-13 01:26:50 +00:00
}
2002-12-14 21:43:00 +00:00
}
2003-08-03 00:57:20 +00:00
void Steps::Compress() const
2002-12-14 21:43:00 +00:00
{
if( !m_sFilename.empty() )
{
/* We have a file on disk; clear all data in memory. */
2004-10-23 23:41:49 +00:00
SAFE_DELETE( notes );
2004-10-30 07:18:28 +00:00
/* Be careful; 'x = ""', notes_comp.clear() and notes_comp.reserve(0)
* don't always free the alocated memory. */
notes_comp = CString("");
}
2004-10-23 23:41:49 +00:00
if( notes_comp.empty() )
2002-12-14 21:43:00 +00:00
{
if(!notes) return; /* no data is no data */
2004-10-23 23:41:49 +00:00
NoteDataUtil::GetSMNoteDataString( *notes, notes_comp );
2002-12-14 21:43:00 +00:00
}
2004-10-23 23:41:49 +00:00
SAFE_DELETE( notes );
2002-12-14 21:43:00 +00:00
}
2003-01-02 22:10:51 +00:00
/* Copy our parent's data. This is done when we're being changed from autogen
* to normal. (needed?) */
2003-08-03 00:57:20 +00:00
void Steps::DeAutogen()
2003-01-02 22:10:51 +00:00
{
if(!parent)
return; /* OK */
2003-03-13 01:26:50 +00:00
Decompress(); // fills in notes with sliding window transform
2003-01-02 22:10:51 +00:00
m_sDescription = Real()->m_sDescription;
m_Difficulty = Real()->m_Difficulty;
2004-02-07 05:57:19 +00:00
m_iMeter = Real()->m_iMeter;
2004-03-12 05:24:32 +00:00
m_RadarValues = Real()->m_RadarValues;
2003-01-02 22:10:51 +00:00
parent = NULL;
2003-03-13 01:26:50 +00:00
Compress();
2003-01-02 22:10:51 +00:00
}
2003-12-29 22:50:54 +00:00
void Steps::AutogenFrom( const Steps *parent_, StepsType ntTo )
2003-01-02 22:10:51 +00:00
{
parent = parent_;
2003-08-07 06:36:34 +00:00
m_StepsType = ntTo;
2003-01-02 22:10:51 +00:00
}
2003-08-07 06:16:17 +00:00
void Steps::CopyFrom( Steps* pSource, StepsType ntTo ) // pSource does not have to be of the same StepsType!
2003-01-30 07:18:33 +00:00
{
2003-08-07 06:36:34 +00:00
m_StepsType = ntTo;
2003-01-30 07:18:33 +00:00
NoteData noteData;
2004-10-23 17:43:49 +00:00
pSource->GetNoteData( noteData );
2004-07-12 02:19:24 +00:00
noteData.SetNumTracks( GameManager::StepsTypeToNumTracks(ntTo) );
2004-10-23 17:43:49 +00:00
this->SetNoteData( noteData );
2003-01-30 07:18:33 +00:00
this->SetDescription( "Copied from "+pSource->GetDescription() );
this->SetDifficulty( pSource->GetDifficulty() );
this->SetMeter( pSource->GetMeter() );
2004-07-11 07:21:33 +00:00
this->SetRadarValues( pSource->GetRadarValues() );
2003-01-30 07:18:33 +00:00
}
2003-08-07 06:16:17 +00:00
void Steps::CreateBlank( StepsType ntTo )
2003-01-30 07:18:33 +00:00
{
2003-08-07 06:36:34 +00:00
m_StepsType = ntTo;
2003-01-30 07:18:33 +00:00
NoteData noteData;
2004-07-12 02:19:24 +00:00
noteData.SetNumTracks( GameManager::StepsTypeToNumTracks(ntTo) );
2004-10-23 17:43:49 +00:00
this->SetNoteData( noteData );
2003-01-30 07:18:33 +00:00
}
2003-08-03 00:57:20 +00:00
const Steps *Steps::Real() const
2003-01-02 22:10:51 +00:00
{
2004-02-13 23:50:21 +00:00
ASSERT( this );
2003-01-02 22:10:51 +00:00
if(parent) return parent;
return this;
}
2003-08-03 00:57:20 +00:00
bool Steps::IsAutogen() const
2003-01-02 22:10:51 +00:00
{
return parent != NULL;
}
void Steps::SetFile( CString fn )
{
m_sFilename = fn;
}
2003-08-03 00:57:20 +00:00
void Steps::SetDescription(CString desc)
2003-01-02 22:10:51 +00:00
{
DeAutogen();
m_sDescription = desc;
}
2003-08-03 00:57:20 +00:00
void Steps::SetDifficulty(Difficulty d)
2003-01-02 22:10:51 +00:00
{
DeAutogen();
m_Difficulty = d;
}
2003-08-03 00:57:20 +00:00
void Steps::SetMeter(int meter)
2003-01-02 22:10:51 +00:00
{
DeAutogen();
m_iMeter = meter;
}
2004-07-11 07:21:33 +00:00
void Steps::SetRadarValues( const RadarValues& v )
2003-01-02 22:10:51 +00:00
{
DeAutogen();
2004-07-11 07:21:33 +00:00
m_RadarValues = v;
2003-01-02 22:10:51 +00:00
}
2004-05-31 21:35:31 +00:00
/*
* (c) 2001-2004 Chris Danford, Glenn Maynard, David Wilson
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/