Files
itgmania212121/stepmania/src/Inventory.cpp
T

213 lines
5.3 KiB
C++
Raw Normal View History

2003-02-25 00:33:42 +00:00
#include "global.h"
/*
-----------------------------------------------------------------------------
Class: Inventory
Desc: See header.
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
#include "Inventory.h"
#include "ThemeManager.h"
#include "RageUtil.h"
#include "GameState.h"
2003-04-07 21:24:14 +00:00
#include "RageTimer.h"
#include "PrefsManager.h"
2003-04-15 05:49:46 +00:00
#include "song.h"
2003-05-13 13:35:32 +00:00
#include "ScreenManager.h"
#include "ScreenGameplay.h"
2003-04-07 21:24:14 +00:00
2003-02-25 00:33:42 +00:00
2003-04-07 22:07:44 +00:00
#define NUM_ITEM_TYPES THEME->GetMetricF("Inventory","NumItemTypes")
#define ITEM_DURATION_SECONDS THEME->GetMetricF("Inventory","ItemDurationSeconds")
#define ITEM_COMBO( i ) THEME->GetMetricI("Inventory",ssprintf("Item%dCombo",i+1))
#define ITEM_EFFECT( i ) THEME->GetMetric ("Inventory",ssprintf("Item%dEffect",i+1))
#define ITEM_LEVEL( i ) THEME->GetMetricI("Inventory",ssprintf("Item%dLevel",i+1))
2003-04-22 04:54:04 +00:00
CachedThemeMetricF ITEM_USE_RATE_SECONDS("Inventory","ItemUseRateSeconds");
2003-02-25 00:33:42 +00:00
const PlayerNumber OPPOSITE_PLAYER[NUM_PLAYERS] = { PLAYER_2, PLAYER_1 };
2003-02-25 00:33:42 +00:00
2003-04-22 04:54:04 +00:00
#define ITEM_USE_PROBABILITY (1.f/ITEM_USE_RATE_SECONDS)
2003-04-07 21:24:14 +00:00
2003-04-07 05:14:27 +00:00
struct Item
{
2003-04-07 22:07:44 +00:00
AttackLevel level;
2003-04-07 05:14:27 +00:00
int iCombo;
CString sModifier;
};
vector<Item> g_Items;
void ReloadItems()
{
g_Items.clear();
for( int i=0; i<NUM_ITEM_TYPES; i++ )
{
Item item;
2003-04-07 22:07:44 +00:00
item.level = (AttackLevel)(ITEM_LEVEL(i)-1);
2003-04-07 05:14:27 +00:00
item.iCombo = ITEM_COMBO(i);
item.sModifier = ITEM_EFFECT(i);
2003-04-07 21:24:14 +00:00
g_Items.push_back( item );
2003-04-07 05:14:27 +00:00
}
}
2003-02-25 00:33:42 +00:00
Inventory::Inventory()
{
switch( GAMESTATE->m_PlayMode )
{
2003-08-19 04:27:50 +00:00
case PLAY_MODE_BATTLE:
break;
default:
ASSERT(0);
}
2003-02-25 00:33:42 +00:00
}
2003-05-13 13:35:32 +00:00
Inventory::~Inventory()
{
for( unsigned i=0; i<m_vpSoundUseItem.size(); i++ )
delete m_vpSoundUseItem[i];
m_vpSoundUseItem.clear();
}
void Inventory::Load( PlayerNumber pn )
2003-02-25 00:33:42 +00:00
{
ITEM_USE_RATE_SECONDS.Refresh();
ReloadItems();
m_PlayerNumber = pn;
m_iLastSeenCombo = 0;
2003-02-25 00:33:42 +00:00
// don't load battle sounds if they're not going to be used
switch( GAMESTATE->m_PlayMode )
{
2003-08-19 04:27:50 +00:00
case PLAY_MODE_BATTLE:
2003-05-13 13:35:32 +00:00
m_soundAcquireItem.Load( THEME->GetPathToS("Inventory aquire item") );
for( unsigned i=0; i<g_Items.size(); i++ )
{
2003-05-13 13:35:32 +00:00
RageSound* pSound = new RageSound;
CString sPath = THEME->GetPathToS( ssprintf("Inventory use item %u",i+1) );
pSound->Load( sPath );
m_vpSoundUseItem.push_back( pSound );
}
2003-05-13 13:35:32 +00:00
m_soundItemEnding.Load( THEME->GetPathToS("Inventory item ending") );
break;
}
}
void Inventory::Update( float fDelta )
2003-02-25 00:33:42 +00:00
{
if( GAMESTATE->m_bActiveAttackEndedThisUpdate[m_PlayerNumber] )
m_soundItemEnding.Play();
2003-02-25 00:33:42 +00:00
PlayerNumber pn = m_PlayerNumber;
2003-02-25 00:33:42 +00:00
// check to see if they deserve a new item
if( GAMESTATE->m_CurStageStats.iCurCombo[pn] != m_iLastSeenCombo )
2003-02-25 00:33:42 +00:00
{
int iOldCombo = m_iLastSeenCombo;
m_iLastSeenCombo = GAMESTATE->m_CurStageStats.iCurCombo[pn];
int iNewCombo = m_iLastSeenCombo;
2003-04-07 05:14:27 +00:00
#define CROSSED(i) (iOldCombo<i)&&(iNewCombo>=i)
#define BROKE_ABOVE(i) (iNewCombo<iOldCombo)&&(iOldCombo>=i)
2003-04-07 05:14:27 +00:00
for( unsigned i=0; i<g_Items.size(); i++ )
2003-02-25 00:33:42 +00:00
{
bool bEarnedThisItem = false;
if( PREFSMAN->m_bBreakComboToGetItem )
bEarnedThisItem = BROKE_ABOVE(g_Items[i].iCombo);
else
bEarnedThisItem = CROSSED(g_Items[i].iCombo);
if( bEarnedThisItem )
2003-04-07 05:14:27 +00:00
{
AwardItem( i );
break;
}
2003-02-25 00:33:42 +00:00
}
}
2003-04-07 21:24:14 +00:00
// use items if this player is CPU-controlled
if( GAMESTATE->m_PlayerController[m_PlayerNumber] != PC_HUMAN &&
2003-04-14 22:12:54 +00:00
GAMESTATE->m_fSongBeat < GAMESTATE->m_pCurSong->m_fLastBeat )
2003-04-07 21:24:14 +00:00
{
2003-04-22 04:54:04 +00:00
// every 1 seconds, try to use an item
2003-04-12 06:16:12 +00:00
int iLastSecond = (int)(RageTimer::GetTimeSinceStart() - fDelta);
2003-04-07 21:24:14 +00:00
int iThisSecond = (int)RageTimer::GetTimeSinceStart();
if( iLastSecond != iThisSecond )
{
2003-04-22 04:54:04 +00:00
for( int s=0; s<NUM_INVENTORY_SLOTS; s++ )
if( !GAMESTATE->m_Inventory[m_PlayerNumber][s].IsBlank() )
if( randomf(0,1) < ITEM_USE_PROBABILITY )
UseItem( s );
2003-04-07 21:24:14 +00:00
}
}
2003-02-25 00:33:42 +00:00
}
2003-04-07 05:14:27 +00:00
void Inventory::AwardItem( int iItemIndex )
2003-02-25 00:33:42 +00:00
{
// CPU player is vanity only. It should have no effect on gameplay
// and should not aquire/launch attacks.
if( GAMESTATE->IsCpuPlayer(m_PlayerNumber) )
return;
// search for the first open slot
int iOpenSlot = -1;
2003-02-25 02:51:04 +00:00
2003-10-25 22:00:58 +00:00
Attack* asInventory = GAMESTATE->m_Inventory[m_PlayerNumber]; //[NUM_INVENTORY_SLOTS]
2003-02-25 02:51:04 +00:00
2003-04-14 22:12:54 +00:00
if( asInventory[NUM_INVENTORY_SLOTS/2].IsBlank() )
iOpenSlot = NUM_INVENTORY_SLOTS/2;
else
2003-02-25 00:33:42 +00:00
{
for( int s=0; s<NUM_INVENTORY_SLOTS; s++ )
2003-04-14 22:12:54 +00:00
if( asInventory[s].IsBlank() )
{
iOpenSlot = s;
break;
}
2003-02-25 00:33:42 +00:00
}
if( iOpenSlot != -1 )
{
2003-10-25 22:00:58 +00:00
Attack a;
2003-04-14 22:12:54 +00:00
a.sModifier = g_Items[iItemIndex].sModifier;
a.fSecsRemaining = ITEM_DURATION_SECONDS;
a.level = g_Items[iItemIndex].level;
asInventory[iOpenSlot] = a;
m_soundAcquireItem.Play();
}
// else not enough room to insert item
}
2003-04-07 05:14:27 +00:00
void Inventory::UseItem( int iSlot )
{
2003-10-25 22:00:58 +00:00
Attack* asInventory = GAMESTATE->m_Inventory[m_PlayerNumber]; //[NUM_INVENTORY_SLOTS]
2003-02-25 00:33:42 +00:00
2003-04-14 22:12:54 +00:00
if( asInventory[iSlot].IsBlank() )
return;
2003-02-25 02:51:04 +00:00
PlayerNumber pnToAttack = OPPOSITE_PLAYER[m_PlayerNumber];
2003-10-25 22:00:58 +00:00
Attack a = asInventory[iSlot];
2003-04-07 22:07:44 +00:00
// remove the item
asInventory[iSlot].MakeBlank();
m_vpSoundUseItem[a.level]->Play();
GAMESTATE->LaunchAttack( pnToAttack, a );
2003-05-13 13:35:32 +00:00
float fPercentHealthToDrain = (a.level+1) / 10.f;
ASSERT( fPercentHealthToDrain > 0 );
GAMESTATE->m_fOpponentHealthPercent -= fPercentHealthToDrain;
CLAMP( GAMESTATE->m_fOpponentHealthPercent, 0.f, 1.f );
// play announcer sound
2003-05-13 13:35:32 +00:00
SCREENMAN->SendMessageToTopScreen( (ScreenMessage)(SM_BattleDamageLevel1+a.level) );
}