#include "RoleDataEx.h"
//-------------------------------------------------------------------------------------------------
//給物品到戰利品(Party分響)
bool    RoleDataEx::Sc_GiveItemToParty( int OrgID , int Count )
{
	return false;
}
//給物品到個人 TempBuffer(任物旗標)
bool    RoleDataEx::Sc_GiveItemToBuf( int OrgID , int Count )
{
	return GiveItem( OrgID , Count );
}
//刪除物品
bool    RoleDataEx::Sc_DelItemFromBody( int OrgID , int Count )
{
	return DelBodyItem( OrgID , Count );
}


//給名聲( +- )
void    RoleDataEx::Sc_GiveFame( int Value )
{

}
//給Exp ( +- )
void    RoleDataEx::Sc_GiveExp( int Value )
{
	Addexp( Value );
}
//給錢  ( +- )
bool    RoleDataEx::Sc_GiveMoney( int Value )
{
	return AddBodyMoney( Value );
}

//-------------------------------------------------------------------------------------------------

//檢查給物品到戰利品(Party分響)
bool    RoleDataEx::Sc_CheckGiveItemToParty( int OrgID , int Count )
{
	return false;
}
//檢查給物品到個人 TempBuffer(任物旗標)
bool    RoleDataEx::Sc_CheckGiveItemToBuf( int OrgID , int Count )
{
	return true;
}
//檢查刪除物品
bool    RoleDataEx::Sc_CheckItemFromBody( int OrgID , int Count )
{
    GameObjDbStructEx*	OrgDB;
    OrgDB = St_GetObjDB( OrgID );

	if( OrgDB == NULL )	return false;

	if( OrgDB->IsKeyItem() )
	{
		int ID = OrgDB->GUID -  Def_ObjectClass_KeyItem;
		return BaseData.KeyItem.GetFlag( ID );
	}
	return CalBodyItemCount( OrgID ) >= Count;
}

//給錢  ( +- )
bool    RoleDataEx::Sc_CheckGiveMoney( int Value )
{
	return CheckAddBodyMoney( Value );
}
//-------------------------------------------------------------------------------------------------
int	RoleDataEx::Quest_Add ( int iQuestID )
{
	int iQuestIndex = -1;
	for( int index = 0; index < _MAX_QUEST_COUNT_; index++ )
	{
		if( BaseData.Quest.QuestLog[ index ].iQuestID == 0 )
		{
			iQuestIndex = index;
			BaseData.Quest.QuestLog[ index ].iQuestID	= iQuestID;
			BaseData.Quest.QuestLog[ index ].iMode		= 0;

			//Packet.emResult = EM_QUESTACCEPT_RESULT_OK;			
			break;
		}
	}

	return iQuestIndex;
}
//-------------------------------------------------------------------------------------------------
bool RoleDataEx::Quest_Failed						( int iQuestID )
{
	for( int index = 0; index < _MAX_QUEST_COUNT_; index++ )
	{
		if( BaseData.Quest.QuestLog[ index ].iQuestID == iQuestID )
		{
			//iQuestIndex = index;
			//BaseData.Quest.QuestLog[ index ].iQuestID	= iQuestID;
			BaseData.Quest.QuestLog[ index ].iMode	= -1;

			//Packet.emResult = EM_QUESTACCEPT_RESULT_OK;			
			return true;
		}
	}

	return false;
}
//-------------------------------------------------------------------------------------------------
void RoleDataEx::Quest_Complete ( int iQuestField, int iQuestID )
{
	if( BaseData.Quest.QuestLog[ iQuestField ].iQuestID == iQuestID )
	{
		Quest_Delete( iQuestField, iQuestID );
		BaseData.Quest.QuestHistory.SetFlagOn( iQuestID - Def_ObjectClass_QuestDetail );				
	}
}
//-------------------------------------------------------------------------------------------------
bool RoleDataEx::Quest_Delete ( int iQuestField, int iQuestID )
{
	
	if( BaseData.Quest.QuestLog[ iQuestField ].iQuestID== iQuestID )
	{
		int iQuest[ _MAX_QUEST_COUNT_ ];		// Client 身上有的 Quest 清單
		ZeroMemory( iQuest, sizeof( iQuest ) );

		//int *pQuestIndex = &( iQuest[ 0 ] );

		// 重新整理清單		
			BaseData.Quest.QuestLog[ iQuestField ].iQuestID = 0;
			BaseData.Quest.QuestLog[ iQuestField ].iMode	= 0;

			ZeroMemory( BaseData.Quest.iQuestKillCount[ iQuestField ], sizeof( BaseData.Quest.iQuestKillCount[ iQuestField ] ) );

			/*
			for( int index = 0; index < _MAX_QUEST_COUNT_; index++ )
			{
				if( BaseData.Quest.iQuest[ index ] != 0 )
				{
					*pQuestIndex = BaseData.Quest.iQuest[ index ];
					pQuestIndex++;
				}
			}
			*/

			//memcpy( BaseData.Quest.iQuest, iQuest, sizeof( iQuest ) );
/*
		// 將已打倒目標及給予物品欄位刪除			
			QuestTempDataStruct	*pQuestTempData = &( TempData.QuestTempData );

			int	iTempIndex		= -1;
			int iMaxTempIndex	= pQuestTempData->QuestKillCount;

			for( int index = 0; index < iMaxTempIndex; index++ )
			{
				if( pQuestTempData->QuestKillIndexTable[ index ] == iQuestField )
				{
					iTempIndex = index;
					break;
				}
			}

			if( iTempIndex != -1 )
			{
				QuestKillStruct Data[ _MAX_QUEST_COUNT_ ];
				ZeroMemory( Data, sizeof( Data ) );
				int	iCount = 0;

				ZeroMemory( &( pQuestTempData->QuestKill[ iTempIndex ] ) , sizeof( QuestKillStruct ) );

				for( int index = 0; index < _MAX_QUEST_COUNT_; index++ )
				{
					if( pQuestTempData->QuestKill[ index ].iKillObj[0] != 0 )
					{
						Data[ iCount ] = pQuestTempData->QuestKill[ index ];
						iCount++;
					}
				}

				memcpy( pQuestTempData->QuestKill, Data, sizeof( Data ) );
				pQuestTempData->QuestKillCount = iCount;
			}

		
		// 將點選物品的欄位刪除

			iTempIndex		= -1;
			iMaxTempIndex	= pQuestTempData->QuestCollectCount;

			for( int index = 0; index < iMaxTempIndex; index++ )
			{
				if( pQuestTempData->QuestCollectIndexTable[ index ] == iQuestField )
				{
					iTempIndex = index;
					break;

					for( int index = index; index < _MAX_QUEST_COUNT_; index++ )
					{
						pQuestTempData->QuestCollectIndexTable[ index ] = pQuestTempData->QuestCollectIndexTable[ (index+1) ];
					}
					pQuestTempData->QuestCollectIndexTable[ (_MAX_QUEST_COUNT_-1) ] = 0;
				}
			}

			if( iTempIndex != -1 )
			{

				for( int index = iTempIndex; index < _MAX_QUEST_COUNT_; index++ )
				{
					pQuestTempData->QuestCollect[ index ] = pQuestTempData->QuestCollect[ (index+1) ];
				}
				ZeroMemory( ( pQuestTempData->QuestCollect + ( _MAX_QUEST_COUNT_ - 1 ) ), sizeof( QuestClickStruct ) );

				//DELETE_ITEM_FROM_ARRAY( ARRAY, ID, MAX ) {	for( int index = ID; index < MAX, index++ )	{	ARRAY[ ( index ) ] = ARRAY[ ( index+1 ) ];	} ZeroMemory( &( ARRAY[ ( MAX-1 ) ] ), sizeof( ( ARRAY[ ( MAX-1 ) ] ) ) ); }

				pQuestTempData->QuestCollectCount--;
			}
*/
		return true;
	}

	return false;
}
//-------------------------------------------------------------------------------------------------
bool RoleDataEx::CheckQuestHistory_IsComplete		( int iQuestID )
{
	return BaseData.Quest.QuestHistory.GetFlag( iQuestID - Def_ObjectClass_QuestDetail);
}
//-------------------------------------------------------------------------------------------------
int	RoleDataEx::CheckQuest_IsAvailable			( GameObjectQuestDetail *pQuest, bool bIgnoreLV )
{
	// 檢查所有條件是否成立, 條件不成立的話就傳送失敗回去

		// 檢查職業 ( 尚未完成 )
			if( pQuest->iCheck_Class_Type != 0 )
			{

			}

		// 檢查性別
			if( pQuest->iCheck_Sex != 0 )
			{
				if( pQuest->iCheck_Sex == 1 ) // 檢查 Sex 為男
				{
					if( BaseData.Sex != EM_Sex_Boy )
						return EM_QUESTCHECK_RESULT_NOTOK;						
				}
				else
				{
					if( BaseData.Sex != EM_Sex_Girl )
						return EM_QUESTCHECK_RESULT_NOTOK;						
				}
			}
		
		// 檢查等級
			//if( bIgnoreLV == false && pQuest->iCheck_BaseLV != 0 )
			if( pQuest->iCheck_BaseLV != 0 ) 
			{
				bool	bOverRanger	= false;
				int		iOffSet		= 0;

				if( TempData.Attr.Level >= pQuest->iCheck_BaseLV )
				{
					iOffSet = TempData.Attr.Level - pQuest->iCheck_BaseLV;
					bOverRanger = true;
				}
				else
				{
					iOffSet = pQuest->iCheck_BaseLV - TempData.Attr.Level;
				}

				if( iOffSet > pQuest->iCheck_RangeLV )
				{
					if( bIgnoreLV == false )
						return EM_QUESTCHECK_RESULT_NOTOK;
					else
					{
						if( bOverRanger != true )
							return EM_QUESTCHECK_RESULT_NOTOK;
					}
				}
			}

		// 檢查稱號
			if( pQuest->iCheck_Title != 0 )
			{

			}

		// 檢查時間
			if( pQuest->iCheck_Time != 0 )
			{

			}

		// 檢查名聲值
			if( pQuest->iCheck_Rep != 0 )
			{
				

			}									

		// 檢查必要 Obj 
			if( pQuest->iCheck_ObjID[0] != 0 )
			{				
				for( int index = 0; index < _MAX_SIZE_QUEST_ITEM; index++ )
				{
					int iObjID = pQuest->iCheck_ObjID[ index ];
					if( iObjID != 0 )
					{						
						// 檢查是道具還是完成任務
						if( ( iObjID >= Def_ObjectClass_Item && iObjID < Def_ObjectClass_Item + Def_ObjectClass_Item_Total ) 
							|| ( iObjID >= Def_ObjectClass_KeyItem && iObjID < Def_ObjectClass_KeyItem + Def_ObjectClass_KeyItem_Total ) )
								
						{
							// 道具
							if( Sc_CheckItemFromBody( iObjID, pQuest->iCheck_ObjVal[ index ] ) == false )
							{
								return EM_QUESTCHECK_RESULT_NOTOK;
							}
						}
						else
							if( iObjID >= Def_ObjectClass_QuestDetail && iObjID < Def_ObjectClass_QuestDetail + Def_ObjectClass_QuestDetail_Total )
							{
								// 任務
								if( CheckQuestHistory_IsComplete( iObjID ) == false )
								{
									return EM_QUESTCHECK_RESULT_NOTOK;
								}
							}

					}
					else
						break;
				}
			}

	return EM_QUESTCHECK_RESULT_OK;

}
//-------------------------------------------------------------------------------------------------
int	RoleDataEx::CheckQuest_IsComplete ( int iQuestIndex, int iQuestID, GameObjectQuestDetail *pQuest )
{
	// 檢查所有條件是否成立, 條件不成立的話就傳送失敗回去

		// 檢查需求物品
			for( int index = 0; index < _MAX_SIZE_QUEST_ITEM; index++ )
			{
				if( pQuest->iRequest_ItemID[ index ] != 0 )
				{
					if( Sc_CheckItemFromBody( pQuest->iRequest_ItemID[ index ], pQuest->iRequest_ItemVal[ index ] ) == false )
					{
						return EM_QUESTCHECK_RESULT_NOTOK;
					}
				}
				else
					break;
			}

		// 檢查打倒目標
			for( int index = 0; index < _MAX_QUEST_KILLKIND_COUNT_; index++ )
			{
				if( pQuest->iRequest_KillID[ index ] != 0 )
				{
					if( BaseData.Quest.iQuestKillCount[ iQuestIndex ][ index ] != pQuest->iRequest_KillVal[ index ] )
					{
						return EM_QUESTCHECK_RESULT_NOTOK;
					}
				}
				else
					break;
			}

		// 檢查金錢
			if( pQuest->iRequest_Money != 0 )
			{
				if( pQuest->iRequest_Money >= BodyMoney() )
				{
					return EM_QUESTCHECK_RESULT_NOTOK;
				}
			}

	return EM_QUESTCHECK_RESULT_OK;
}
//-------------------------------------------------------------------------------------------------
int RoleDataEx::CheckQuestList_IsGotQuest ( int iQuestID )
{
	for( int index = 0; index < _MAX_QUEST_COUNT_; index++ )
	{
		if( BaseData.Quest.QuestLog[ index ].iQuestID == iQuestID )
		{
			return index;
		}
	}

	return -1;
}
//-------------------------------------------------------------------------------------------------
/*
int RoleDataEx::CheckQuest_RequestKill_AddCount ( int iDeadObjID )
{
	//RoleDataEx*				pRole		= g_pGameMain->RoleData();
	QuestTempDataStruct*	pQuestTempData	= &( TempData.QuestTempData );

	for( int index = 0; index < pQuestTempData->QuestKillCount; index++ )
	{
		//if( pQuestTempData->iKillObj[ index ] != 0 )
		if( pQuestTempData->QuestKill[ index ].iKillObj[ 0 ] != 0 )
		{
			for( int iKillIndex = 0; iKillIndex < _MAX_QUEST_KILLKIND_COUNT_; iKillIndex++ )
			{
				int	&KillObj	=  pQuestTempData->QuestKill[ index ].iKillObj[ iKillIndex ];

				if( KillObj != 0 )
				{
					if( KillObj == iDeadObjID )
					{
						int		iQuestField = pQuestTempData->QuestKillIndexTable[ index ];

						int		&KillVal	= pQuestTempData->QuestKill[ index].iKillVal[ iKillIndex ];
						BYTE	&KillCount	= BaseData.Quest.iQuestKillCount[ iQuestField ][ iKillIndex ];

						if( KillVal > KillCount )							
						{
							KillCount++;

							return index;	// 回傳 Temp `資料中的 index
						}
					}
				}
				else
					break;
			}
		}
		else
			break;
	}

	return -1;
}
*/
//-------------------------------------------------------------------------------------------------
/*
int RoleDataEx::CheckQuest_RequestKill_Complete ( int iTempIndex )
{
	
	QuestTempDataStruct*	pQuestTempData	= &( TempData.QuestTempData );
	int						iQuestField		= pQuestTempData->iIndexTable[ iTempIndex ];
	
	if( pQuestTempData->iKillObj[ iTempIndex ] != 0 )
	{
		for( int iKillIndex = 0; iKillIndex < _MAX_QUEST_KILLKIND_COUNT_; iKillIndex++ )
		{				
			if( pQuestTempData->iKillObj[ iTempIndex ][ iKillIndex ] != 0 )
			{
				

				if( pQuestTempData->iKillVal[ iTempIndex ][ iKillIndex ] != BaseData.Quest.iQuestKillCount[ iQuestField ][ iKillIndex ] )
				{
					return -1;
				}
			}
			else
				break;
		}
		
	}
	else
	{
		return -1;
	}

	return iQuestField;
	

	return -1;

}
*/

//-------------------------------------------------------------------------------------------------
/*
void RoleDataEx::CheckQuest_RequestKill_SetTempData ( int iQuestField, GameObjectQuestDetail *pQuest )
{
	// 更新 TempData
	//--------------------------------------------------------------------
	if( pQuest->iRequest_KillID[ 0 ] != 0 )	// 檢查有無指定打倒的目標
	{
		QuestTempDataStruct	*pQuestTempData = &( TempData.QuestTempData );
		int					index			= pQuestTempData->QuestKillCount;

		pQuestTempData->QuestKillIndexTable[ index ] = iQuestField;

		for( int iKillKind = 0; iKillKind < _MAX_QUEST_KILLKIND_COUNT_; iKillKind++ )
		{
			if( pQuest->iRequest_KillID[ iKillKind ] != 0 )
			{
				pQuestTempData->QuestKill[ index ].iKillObj[ iKillKind ] = pQuest->iRequest_KillID[ iKillKind ];
				pQuestTempData->QuestKill[ index ].iKillVal[ iKillKind ] = pQuest->iRequest_KillVal[ iKillKind ];

				//pQuestTempData->iKillObj[ index ][ iKillKind ]		= pQuest->iRequest_KillID[ iKillKind ];
				//pQuestTempData->iKillVal[ index ][ iKillKind ]		= pQuest->iRequest_KillVal[ iKillKind ];
				//pQuestTempData->fDropPercent[ index ][ iKillKind ]	= pQuest->ip
			}
			else
				break;
		}

		pQuestTempData->QuestKillCount++;
	}
}

//-------------------------------------------------------------------------------------------------
void RoleDataEx::CheckQuest_KillToGetItem_SetTempData ( int iQuestField, GameObjectQuestDetail *pQuest )
{
	// 更新 TempData
	//--------------------------------------------------------------------
	if( pQuest->iProcess_KilltoGetItem_ObjID[ 0 ] != 0 )	// 檢查有無指定打倒的目標
	{
		QuestTempDataStruct	*pQuestTempData = &( TempData.QuestTempData );
		int					index			= pQuestTempData->QuestKillCount;

		pQuestTempData->QuestKillIndexTable[ index ] = iQuestField;

		for( int iKillKind = 0; iKillKind < _MAX_QUEST_KILLKIND_COUNT_; iKillKind++ )
		{
			// 檢
			if( pQuest->iProcess_KilltoGetItem_ObjID[ iKillKind ] != 0 )
			{
				pQuestTempData->QuestKill[ index].iKillObj[ iKillKind ]			= pQuest->iProcess_KilltoGetItem_ObjID[ iKillKind ];			
				pQuestTempData->QuestKill[ index].fDropPercent[ iKillKind ]		= pQuest->iProcess_KilltoGetItem_ProcPercent[ iKillKind ];
				pQuestTempData->QuestKill[ index].iDropItem[ iKillKind ]		= pQuest->iProcess_KilltoGetItem_GetItem[ iKillKind ];
				pQuestTempData->QuestKill[ index].iDropItemVal[ iKillKind ]		= pQuest->iProcess_KilltoGetItem_GetItemVal[ iKillKind ];																	  				
			}
			else
				break;
		}

		pQuestTempData->QuestKillCount++;
	}
}
//-------------------------------------------------------------------------------------------------
void RoleDataEx::CheckQuest_ClickToGetItem_SetTempData ( int iQuestField, GameObjectQuestDetail *pQuest )
{
	// 更新 TempData
	//--------------------------------------------------------------------
	if( pQuest->iProcess_ClickToGetItem_ObjID[ 0 ] != 0 )	// 檢查有無指定打倒的目標
	{
		QuestTempDataStruct	*pQuestTempData = &( TempData.QuestTempData );
		int					iField			= pQuestTempData->QuestCollectCount;

		pQuestTempData->QuestCollectIndexTable[ iField ] = iQuestField;

		for( int index = 0; index < _MAX_QUEST_KILLKIND_COUNT_; index++ )
		{
			if( pQuest->iProcess_ClickToGetItem_ObjID[ index ] != 0 )
			{
				pQuestTempData->QuestCollect[ iField ].iClickObj	[ index ]	= pQuest->iProcess_ClickToGetItem_ObjID			[ index ];			
				pQuestTempData->QuestCollect[ iField ].fProcPercent	[ index ]	= pQuest->iProcess_ClickToGetItem_ProcPercent	[ index ];
				pQuestTempData->QuestCollect[ iField ].iDropItem	[ index ]	= pQuest->iProcess_ClickToGetItem_GetItem		[ index ];
				pQuestTempData->QuestCollect[ iField ].iDropItemVal	[ index ]	= pQuest->iProcess_ClickToGetItem_GetItemVal	[ index ];																	  				
			}
			else
				break;
		}

		pQuestTempData->QuestCollectCount++;
	}
}
*/
//-------------------------------------------------------------------------------------------------
bool RoleDataEx::Sc_CheckItemFromBank ( int OrgID, int Count )
{
    return CalBankItemCount( OrgID ) >= Count;
	//return 0;
}
//-------------------------------------------------------------------------------------------------
bool RoleDataEx::Sc_CheckItemFromRole		( int OrgID, int Count )
{
    GameObjDbStructEx*	OrgDB;
    OrgDB = St_GetObjDB( OrgID );

	if( OrgDB == NULL )	return false;

	if( OrgDB->IsKeyItem() )
	{
		int ID = OrgDB->GUID -  Def_ObjectClass_KeyItem;
		return BaseData.KeyItem.GetFlag( ID );
	}
	else
	{
		// Item
		int iCount = Sc_GetItemNumFromRole( OrgID );
		return ( iCount >= Count );
	}
}
//-------------------------------------------------------------------------------------------------
int RoleDataEx::Sc_GetItemNumFromRole		( int OrgID )
{
	GameObjDbStructEx*	OrgDB;
    OrgDB = St_GetObjDB( OrgID );

	if( OrgDB == NULL )	return false;

	if( OrgDB->IsKeyItem() )
	{
		int ID = OrgDB->GUID -  Def_ObjectClass_KeyItem;
		if( BaseData.KeyItem.GetFlag( ID ) )
			return 1;
		else
			return 0;
	}
	else
	{
		// Item
		int iCount = 0;

		iCount	=	CalBankItemCount( OrgID );
		iCount	+=	CalBodyItemCount( OrgID );

		return iCount;
	}
}