/* $Id: bonuspack.cpp,v 1.10.4.1 2006/03/08 15:05:03 uwe Exp $ */ #include "bonuspack.hpp" #include "avatar.hpp" /**********************************************************/ BonusPack::BonusPack( World *const worldPointer ) : m_bag( NULL ), m_worldPointer( worldPointer ) { m_bag = NEW Bonus* [Bonus::getNumTypes()]; for( int i = 0; i < Bonus::getNumTypes(); i++ ) m_bag[i] = NULL; m_bag -= FIRST_BONUS; } /**********************************************************/ BonusPack::~BonusPack() { m_bag += FIRST_BONUS; for( int i = 0; i < Bonus::getNumTypes(); i++ ) delete m_bag[i]; delete [] m_bag; } /**********************************************************/ void BonusPack::add( Bonus* newBonus ) { DBG( 3 ) { checkIDRange( newBonus->getID(), "add" ); ASSERT( newBonus->mustBePutIntoPack(), "BonusPack::add: bonus \"%s\" must not be carried in " "the bonus pack\n", newBonus->getIDString() ); } if( m_bag[newBonus->getID()] == NULL ) { m_bag[newBonus->getID()] = newBonus; } else { m_bag[newBonus->getID()]->add( newBonus ); delete newBonus; } } /**********************************************************/ void BonusPack::remove( const int bonusID ) { DBG(2) checkIDRange( bonusID, "remove" ); delete m_bag[bonusID]; m_bag[bonusID] = NULL; } /**********************************************************/ void BonusPack::update( Avatar *const avatar ) { for( int id = FIRST_BONUS; id <= LAST_BONUS; id++ ) { if( m_bag[id] != NULL ) { if( ! m_bag[id]->updateInBonusPack(avatar) ) { remove( id ); } } } } /**********************************************************/ void BonusPack::resetForRespawn() { for( int id = FIRST_BONUS; id <= LAST_BONUS; id++ ) { if( m_bag[id] && !m_bag[id]->persistentInPack() ) { remove( id ); } } } /**********************************************************/ Uint32 BonusPack::getSerializeBufferSize() const { Uint32 size = (Uint32)Bonus::getNumTypes() * Serialize::sizeOf(); ADD_TAG_SIZE( size ); for( int id = FIRST_BONUS; id <= LAST_BONUS; id++ ) { if( m_bag[id] ) { size += m_bag[id]->getSerializeBufferSize(); // adds the tag size to "size", if tags are used ADD_TAG_SIZE( size ); } } return size; } /**********************************************************/ void BonusPack::serialize( Uint8*& bufferPointer ) const { // debugging macro START_SERIALIZED_SIZE_CHECK( bufferPointer ); SERIALIZE_TAG( bufferPointer ); for( int id = FIRST_BONUS; id <= LAST_BONUS; ++id ) { // set flag to mark, whether the bonus is present in the bonus pack const Uint8 presenceFlag = m_bag[id] == 0x0 ? 0 : 1; Serialize::serialize( presenceFlag, bufferPointer ); // serialize bonus, if present if( m_bag[id] ) { m_bag[id]->serialize( bufferPointer ); SERIALIZE_TAG( bufferPointer ); } } // debugging macro END_SERIALIZED_SIZE_CHECK( bufferPointer, BonusPack ); } /**********************************************************/ void BonusPack::deserialize( Uint8*& bufferPointer ) { DESERIALIZE_TAG( bufferPointer ); for( int id = FIRST_BONUS; id <= LAST_BONUS; ++id ) { // deserialize flag that indicates the presence of the bonus Uint8 presenceFlag = 0; Serialize::deserialize( bufferPointer, presenceFlag ); // deserialize bonus, if present if( presenceFlag ) { // let the world create the bonus object, ... m_bag[id] = dynamic_cast( m_worldPointer->newObject( id )); // ... but accroach it immediately from the world's control. m_worldPointer->unhookObject( m_bag[id] ); m_bag[id]->deserialize( bufferPointer ); DESERIALIZE_TAG( bufferPointer ); } else { m_bag[id] = 0x0; } } } /**********************************************************/ void BonusPack::checkIDRange( const int ID, const char* const fn ) const { ASSERT( FIRST_BONUS <= ID && ID <= LAST_BONUS, "BonusPack::ceckIDRange: called from BonusPack::" "%s: %d is out of range of valid bonus IDs [%d,%d]", fn, ID, ID, FIRST_BONUS, LAST_BONUS ); } /**********************************************************/