unify code types

This commit is contained in:
Glenn Maynard
2007-03-15 20:38:12 +00:00
parent c25aff2f4e
commit 223f4b57c2
4 changed files with 130 additions and 52 deletions
+114 -44
View File
@@ -10,6 +10,7 @@
#include "RageUtil.h"
#include "PlayerState.h"
#include "InputEventPlus.h"
#include "Foreach.h"
const char *CodeNames[] = {
"Easier1",
@@ -56,46 +57,103 @@ XToString( Code );
static CodeItem g_CodeItems[NUM_Code];
static const float g_fSimultaneousThreshold = 0.05f;
bool CodeItem::EnteredCode( GameController controller ) const
{
if( controller == GameController_Invalid )
return false;
if( buttons.size() == 0 )
if( m_aPresses.size() == 0 )
return false;
switch( m_Type )
{
case sequence:
return INPUTQUEUE->MatchesSequence( controller, &buttons[0], buttons.size(), fMaxSecondsBack );
case hold_and_press:
{
RageTimer OldestTimeAllowed = RageTimer() - 0.05f;
InputEventPlus iep;
if( !INPUTQUEUE->WasPressedRecently(controller, buttons[buttons.size()-1], OldestTimeAllowed, &iep) )
return false;
RageTimer OldestTimeAllowed;
OldestTimeAllowed += -fMaxSecondsBack;
// Check that all but the last were being held when the last button was pressed.
for( unsigned i=0; i<buttons.size()-1; i++ )
// iterate newest to oldest
int iSequenceIndex = m_aPresses.size()-1; // count down
const vector<InputEventPlus> &aQueue = INPUTQUEUE->GetQueue( controller );
int iQueueIndex = aQueue.size()-1;
while( iQueueIndex >= 0 )
{
{
const InputEventPlus &iep = aQueue[iQueueIndex];
if( iep.DeviceI.ts < OldestTimeAllowed )
return false;
}
/* Search backwards for all of Press.m_aButtonsToPress pressed within g_fTapThreshold seconds
* with m_aButtonsToHold pressed. */
const ButtonPress &Press = m_aPresses[iSequenceIndex];
bool bMatched = false;
int iMinSearchIndexUsed = iQueueIndex;
for( int b=0; b<(int) Press.m_aButtonsToPress.size(); ++b )
{
/* Search backwards for the buttons in this tap, within the tap threshold since iQueueIndex. */
RageTimer OldestTimeAllowedForTap( aQueue[iQueueIndex].DeviceI.ts );
OldestTimeAllowedForTap += -g_fSimultaneousThreshold;
const InputEventPlus *pIEP = NULL;
int iQueueSearchIndex = iQueueIndex;
for( ; iQueueSearchIndex>=0; --iQueueSearchIndex ) // iterate newest to oldest
{
GameInput gi( controller, buttons[i] );
if( !INPUTMAPPER->IsBeingPressed(gi, MultiPlayer_Invalid, &iep.InputList) )
return false;
const InputEventPlus &iep = aQueue[iQueueSearchIndex];
if( iep.DeviceI.ts < OldestTimeAllowedForTap ) // buttons are too old. Stop searching because we're not going to find a match
break;
if( iep.GameI.button == Press.m_aButtonsToPress[b] )
{
pIEP = &iep;
break;
}
}
if( pIEP == NULL )
break; // didn't find the button
// Check that m_aButtonsToHold were being held when the buttons were pressed.
bool bAllButtonsPressed = true;
for( unsigned i=0; i<Press.m_aButtonsToHold.size(); i++ )
{
GameInput gi( controller, Press.m_aButtonsToHold[i] );
if( !INPUTMAPPER->IsBeingPressed(gi, MultiPlayer_Invalid, &pIEP->InputList) )
bAllButtonsPressed = false;
}
if( !bAllButtonsPressed )
continue;
if( b == (int) Press.m_aButtonsToPress.size()-1 )
{
bMatched = true;
iMinSearchIndexUsed = min( iMinSearchIndexUsed, iQueueSearchIndex );
}
}
if( !bMatched )
{
/* The press wasn't matched. If m_bAllowIntermediatePresses is true,
* skip the last press, and look again. */
if( !Press.m_bAllowIntermediatePresses )
return false;
--iQueueIndex;
continue;
}
if( iSequenceIndex == 0 )
{
// we matched the whole pattern. Empty the queue so we don't match on it again.
INPUTQUEUE->ClearQueue( controller );
return true;
}
break;
case tap:
return INPUTQUEUE->AllWerePressedRecently( controller, &buttons[0], buttons.size(), fMaxSecondsBack );
default:
ASSERT(0);
return false;
/* The press was matched. */
iQueueIndex = iMinSearchIndexUsed - 1;
--iSequenceIndex;
}
return false;
}
bool CodeItem::Load( RString sButtonsNames )
{
buttons.clear();
m_aPresses.clear();
vector<RString> asButtonNames;
@@ -104,27 +162,28 @@ bool CodeItem::Load( RString sButtonsNames )
if( bHasAPlus )
{
m_Type = tap;
// press all buttons simultaneously
split( sButtonsNames, "+", asButtonNames, false );
}
else if( bHasADash )
{
m_Type = hold_and_press;
// hold the first iNumButtons-1 buttons, then press the last
split( sButtonsNames, "-", asButtonNames, false );
}
else
{
m_Type = sequence;
// press the buttons in sequence
split( sButtonsNames, ",", asButtonNames, false );
}
if( asButtonNames.size() < 1 )
{
if( sButtonsNames != "" )
LOG->Trace( "The code '%s' is less than 2 buttons, so it will be ignored.", sButtonsNames.c_str() );
LOG->Trace( "Ignoring empty code \"%s\".", sButtonsNames.c_str() );
return false;
}
vector<GameButton> buttons;
for( unsigned i=0; i<asButtonNames.size(); i++ ) // for each button in this code
{
const RString sButtonName = asButtonNames[i];
@@ -133,31 +192,42 @@ bool CodeItem::Load( RString sButtonsNames )
const GameButton gb = INPUTMAPPER->GetInputScheme()->ButtonNameToIndex( sButtonName );
if( gb == GameButton_Invalid )
{
LOG->Trace( "The code '%s' contains an unrecognized button '%s'.", sButtonsNames.c_str(), sButtonName.c_str() );
buttons.clear();
LOG->Trace( "The code \"%s\" contains an unrecognized button \"%s\".", sButtonsNames.c_str(), sButtonName.c_str() );
m_aPresses.clear();
return false;
}
buttons.push_back( gb );
}
switch( m_Type )
if( bHasAPlus )
{
case sequence:
if( buttons.size() == 1 )
fMaxSecondsBack = -1;
else
fMaxSecondsBack = (buttons.size()-1)*0.6f;
break;
case hold_and_press:
fMaxSecondsBack = -1.f; // not applicable
break;
case tap:
fMaxSecondsBack = 0.05f; // simultaneous
break;
default:
ASSERT(0);
m_aPresses.push_back( ButtonPress() );
FOREACH( GameButton, buttons, gb )
{
m_aPresses.back().m_aButtonsToPress.push_back( *gb );
}
}
else if( bHasADash )
{
m_aPresses.push_back( ButtonPress() );
m_aPresses.back().m_aButtonsToHold.insert( m_aPresses.back().m_aButtonsToHold.begin(), buttons.begin(), buttons.end()-1 );
m_aPresses.back().m_aButtonsToPress.push_back( buttons.back() );
}
else
{
FOREACH( GameButton, buttons, gb )
{
m_aPresses.push_back( ButtonPress() );
m_aPresses.back().m_aButtonsToPress.push_back( *gb );
m_aPresses.back().m_bAllowIntermediatePresses = false;
}
}
if( m_aPresses.size() == 1 )
fMaxSecondsBack = 0.55f;
else
fMaxSecondsBack = (m_aPresses.size()-1)*0.6f;
// if we make it here, we found all the buttons in the code
return true;