#include "RoleDataEx.h"
//------------------------------------------------------------------------------------
//搜尋身體空欄位
int		RoleDataEx::SearchBodyEmptyPos( )
{
    int i;
    for( i = 0 ; i < BaseData.Body.Count ; i++ )
    {
        if( BaseData.Body.Item[i].IsEmpty() )
            return i;
    }

    return -1;
}

int		RoleDataEx::SearchBankEmptyPos( )
{
    int i;
    for( i = 0 ; i < BaseData.Bank.Count ; i++ )
    {
        if( BaseData.Bank.Item[i].IsEmpty() )
            return i;
    }

    return -1;
}

//-----------------------------------------------------------------------------------------------
//		關於怪物仇恨度處理
//-----------------------------------------------------------------------------------------------
//收尋某物件ID所對應此物件仇恨表的位置
//如果之前沒在裡面 則找一個適合的位置
NPCHateStruct*		RoleDataEx::GetHatePos( int	ID , int DBID )
{
    NPCHateStruct*	NPCHate; 
    int		i;
    int		MinHint = 1000000;
    int		MinID	= 0;
    int		TempID;
    int		TempHint;
    //	int		ID = Att->TempData.ItemID;

    for( i = 0 ; i < TempData.NPCHate.Hate.Size() ; i++)
    {
        TempID = TempData.NPCHate.Hate[i].ItemID;
        if( TempID == -1 || TempID == ID )
            break;

        TempHint = TempData.NPCHate.Hate[i].TotalHatePoint() + TempData.NPCHate.Hate[i].HitPoint;

        if( TempHint < MinHint )
        {
            MinHint = TempHint;
            MinID = i;
        }
    }


    if( i == _MAX_HATELIST_SIZE_ )
    {
        NPCHate = &TempData.NPCHate.Hate[MinID];
        //檢查此人的資料是否有問題
        if(	NPCHate->DBID != DBID )
        { 
//            RoleDataEx*	TargetVal = (RoleDataEx*)St_GetRoleData( ID);
//            if( TargetVal == NULL )

            NPCHate->DBID = DBID;
            NPCHate->HatePoint = 0;
			NPCHate->TempHatePoint = 0;
            NPCHate->HitPoint = 0;
            NPCHate->ItemID = ID;

        }

        return NPCHate;
    }
    else if( i == TempData.NPCHate.Hate.Size() )
    {
        NPCHateStruct TempSt;
        TempSt.Init();
        TempData.NPCHate.Hate.Push_Back( TempSt );
    }
        
    NPCHate = &TempData.NPCHate.Hate[i];
    NPCHate->DBID = DBID;
    NPCHate->ItemID = ID;	

    return NPCHate;
}

//找到地一個可堆疊的地方
int  RoleDataEx::SearchBodyFirstFit( const ItemFieldStruct *Item )
{
    if( Item->Count == 0 )
        return -1;

    GameObjDbStructEx*	OrgDB;
    OrgDB = St_GetObjDB( Item->OrgObjID );
    if( OrgDB == NULL )
        return -1;

	for ( int i = 0; i < BaseData.Body.Count ; i++ )
	{
		if( BaseData.Body.Item[i].OrgObjID != OrgDB->GUID )
			continue;

		if ( BaseData.Body.Item[i].Pos >= EM_TRADE_ITEM1 && BaseData.Body.Item[i].Pos <= EM_TRADE_ITEM25 )
			continue;

		if( BaseData.Body.Item[i].Count  < OrgDB->MaxHeap )
			return i;
	}	
    
    for( int i = 0 ; i < BaseData.Body.Count ; i++ )
    {
        if( BaseData.Body.Item[i].IsEmpty() )
            return i;
    }

    return -1;
}