#include "StdAfx.h" #include "ItemScript.h" cItemNpcStoreLexer::cItemNpcStoreLexer( const char* buffer, unsigned int size ) : cLexer( buffer, size ) { BindKeyword( "npc_store", eTOKEN_NPCSTORE ); BindKeyword( "tab", eTOKEN_TAB ); } /// cItemNpcStore cItemNpcStore::cItemNpcStore() { } cItemNpcStore::~cItemNpcStore() { for(int i = 0; i < (int)mTabAry.GetSize(); i++) { sNpcItemData* p = (sNpcItemData*)(mTabAry[i]); p->mItem.Clear(); SAFE_DELETE( p ); } mTabAry.Clear(); } bool cItemNpcStore::Load( cParser& parser ) { if( parser.ExpectTokenString( "{" ) == false ) return false; cToken token; cLexer* lexer = parser.GetLexer(); if( !lexer ) { assert(0); return false; } while( lexer->GetNextToken( &token ) ) { if( token == "}" ) break; switch( token.mType ) { case eTOKEN_TAB: { unsigned long textIdx = 0; /// ÅØ½ºÆ® À妽º unsigned long rate = 0; /// ÆÇ¸Å±Ý ºñÀ² unsigned char force = 0; /// ¼¼·Â ±¸ºÐ - [ 0:Å׸£/ÄÚÀÎ,1:ºÒ,2:¹°,3:¹Ù¶÷,4:¶¥ ] textIdx = (unsigned long)parser.ParseInt(); rate = (unsigned long)parser.ParseInt(); force = (unsigned char)parser.ParseInt(); if( mTabAry.GetSize() > NPCSTORE_TAB_COUNT ) { assert( 0 && "tab index out of range" ); return false; } sNpcItemData* pItemData = new sNpcItemData; pItemData->mTextIdx = textIdx; pItemData->mRate = rate; pItemData->mForce = force; mTabAry.PushBack( pItemData ); if( LoadTab( pItemData, parser ) == false ) { assert( 0 ); return false; } } break; default: assert( 0 && "invalid token" ); return false; } } return true; } bool cItemNpcStore::LoadTab( sNpcItemData* pTab, cParser& parser ) { if( parser.ExpectTokenString( "{" ) == false ) return false; cToken token; cLexer* lexer = parser.GetLexer(); if( !lexer ) { assert(0); return false; } while( lexer->GetNextToken( &token ) ) { if( token == "}" ) break; switch( token.mType ) { case eTOKEN_INT: { unsigned long itemIndex = (unsigned long)token.ToInt(); pTab->mItem.PushBack( itemIndex ); } break; default: assert( 0 && "invalid token" ); return false; } } return true; } bool cItemNpcStore::IsItemIdx ( unsigned long itemIdx ) { for(int i = 0; i < (int)mTabAry.GetSize(); i++) { sNpcItemData* p = (sNpcItemData*)(mTabAry[i]); for( int i = 0; i < (int)p->mItem.GetSize(); i++ ) { if( p->mItem[i] == itemIdx ) return true; } } return false; } bool cItemNpcStore::IsItemIdx ( unsigned int tabIndex, unsigned long itemIdx ) { if( tabIndex > mTabAry.GetSize() ) return false; sNpcItemData* p = (sNpcItemData*)(mTabAry[tabIndex]); for( int i = 0; i < (int)p->mItem.GetSize(); i++ ) { if( p->mItem[i] == itemIdx ) return true; } return false; } unsigned long cItemNpcStore::GetItemIndex( unsigned int tabIndex, unsigned int cellIndex ) { sNpcItemData* pTab = (sNpcItemData*)(mTabAry[tabIndex]); if( pTab ) { if( pTab->mItem.GetSize() > cellIndex ) return pTab->mItem[cellIndex]; } return 0; } unsigned long cItemNpcStore::GetTextIndex( unsigned int tabIndex ) { sNpcItemData* pTab = (sNpcItemData*)(mTabAry[tabIndex]); if( pTab ) return pTab->mTextIdx; return 0; } unsigned int cItemNpcStore::GetItemCount( unsigned int tabIndex ) { sNpcItemData* pTab = (sNpcItemData*)(mTabAry[tabIndex]); if( pTab ) return pTab->mItem.GetSize(); return 0; } sNpcItemData* cItemNpcStore::GetNpcItemData( unsigned int tabIndex ) { if( tabIndex > mTabAry.GetSize() ) return false; sNpcItemData* p = (sNpcItemData*)(mTabAry[tabIndex]); return ( ( p ) ? p : NULL ); } unsigned long cItemNpcStore::GetItemSellRate( unsigned int tabIndex ) { if( tabIndex > mTabAry.GetSize() || tabIndex < 0 ) { assert( 0 && "Bad tabIndex" ); return ULONG_MAX; } sNpcItemData* p = (sNpcItemData*)(mTabAry[tabIndex]); if( p ) return p->mRate; else { assert( 0 && "Bad NpcItemData" ); return ULONG_MAX; } } unsigned char cItemNpcStore::GetItemSellType( unsigned int tabIndex ) { if( tabIndex > mTabAry.GetSize() || tabIndex < 0 ) { assert( 0 && "Bad tabIndex" ); return 1; } sNpcItemData* p = (sNpcItemData*)(mTabAry[tabIndex]); if( p ) return p->mForce; else { assert( 0 && "Bad NpcItemData" ); return 1; } } /// cItemScript cItemScript* cItemScript::mpItemScript = NULL; cItemScript::cItemScript(void) { if( mpItemScript ) { assert(NULL); } else { mpItemScript = this; } } cItemScript::~cItemScript(void) { Release(); } bool cItemScript::Init() { if( !LoadNpcStore( "./Script/Resource/Item_NpcStore.txt" ) ) { assert(NULL); return false; } return true; } void cItemScript::Release() { cNpcStoreMap::cIterator i = mNpcStoreMap.Begin(); cNpcStoreMap::cIterator iend = mNpcStoreMap.End(); for( ; i != iend; ++i ) { cItemNpcStore* p = (cItemNpcStore*)(i->mSecond); SAFE_DELETE( p ); } mNpcStoreMap.Clear(); } bool cItemScript::LoadNpcStore(const cString& pathName) { /// ÆÄÀÏ ¿­±â cFileLoader loader; if ( loader.Open( pathName, true ) == false ) { assert( 0 && "failed to load npc store" ); return false; } /// ·º¼­¿¡ ¹äÁÖ°í ÆÄ½Ì cToken token; cItemNpcStoreLexer lexer( loader.GetBufferPtr(), loader.GetSize() ); cParser parser( &lexer, pathName ); while ( lexer.IsEnd() == false ) { lexer.GetNextToken( &token ); switch( token.mType ) { case eTOKEN_ERROR: return false; case eTOKEN_NULL: continue; case eTOKEN_NPCSTORE: { unsigned int i = parser.ParseInt( ); /// Npc À妽º cItemNpcStore* p = new cItemNpcStore; /// Npc »óÁ¡ »ý¼º if ( p->Load( parser ) == false ) /// ·Îµù { delete p; return false; } if ( mNpcStoreMap.Insert( i, p ) == false ) /// Npc »óÁ¡ ¸Ê¿¡ Ãß°¡ { assert( 0 && "failed to insert npc store, maybe already exist" ); return false; } } break; default: assert( 0 && "invalid token" ); return false; } } return true; } cItemNpcStore* cItemScript::GetItemNpcStore(unsigned int npcIndex) const { cNpcStoreMap::cConstIterator i = mNpcStoreMap.Find( npcIndex ); return ( i != mNpcStoreMap.End( ) ) ? (cItemNpcStore*)i->mSecond : NULL; }