#include "StdAfx.h"
#include "actiondie.h"
#include "GameSrv.h"

#include "../Common/Protocol.h"

#include "GridManager.h"

#include "Monster.h"
#include "SkillManager.h"
#include "Player.h"
#include "ObjectManager.h"
#include "Drop.h"
#include "AIManager.h"
#include "QuestManager.h"

cActionDie::cActionDie(void) : cAction( eACT_DIE )
{
}



cActionDie::~cActionDie(void)
{
}



void cActionDie::ActionInit( cMonster* pMonster, unsigned long accumTime )
{
	cAction::ActionInit( pMonster, accumTime );
	/// 여기서 몬스터를 삭제하게되면 damage 메세지 발송하기전에 삭제되서 발송을 못한다.

	mDieEndTime = accumTime + 5000;
	mFirstRun = true;
}



eACTION_CHANGE cActionDie::Action( cMonster* pMonster, unsigned long /*deltaTime*/, unsigned long accumTime )
{
	if( mFirstRun == true )
	{
		mFirstRun = false;
		
		if( pMonster->IsOutsideDestroy() == true )
			Destroy( pMonster );
		else
			Die( pMonster );	
	}

	if( mDieEndTime < accumTime )
	{
		/// objectmanager 몬스터 삭제 목록에 넣음
		OBJECTMANAGER->InsertDeleteMonster( pMonster->GetObjectID() );

		/// 몬스터 죽음 프로세스 끝냄 - 리젠
		return eACTCH_DIE_END;
	}

	return eACTCH_CONTINUE;

}


void cActionDie::Die( cMonster* pMonster )
{
	if( pMonster->GetfirstAttack() == 0 )
	{
		NETWORK2->PostServerEvent("cActionDie::Action GetfirstAttack() == 0");
	}

	if( pMonster->IsCriDie() == true )
		pMonster->SendSpeech( eMONSTERTALK_DIE2 );
	else
		pMonster->SendSpeech( eMONSTERTALK_DIE1 );			

	unsigned long partyIdx = 0;
	cPlayer* player = pMonster->NearGridFindPlayer( pMonster->GetfirstAttack() );
	if( player )
	{
		partyIdx = player->GetPartyIndex();
		if( partyIdx > 0 )
		{
			/// 파티 전체 갱신
			PARTYMAN->SendToPartyHunt( pMonster, player, pMonster->GetRaceGender() );
		}
		else
		{
			/// 자기자신만 갱신
			player->UpdateDutyHunt( pMonster->GetRaceGender() );
		}
	}

	/// 드랍 소유권 계산
	unsigned long dropPlayerIdx = pMonster->GetfirstAttack();
	/// 선공 유저가 몬스터HP의 20%를 깎으면 선공유저 소유
	unsigned long damage = pMonster->GetTakeDamage( dropPlayerIdx );
	if( (float)damage < pMonster->GetMaxHP() * 0.2f )
	{
		/// 선공 유저가 20%를 깎지못하면 가장 데미지가 큰유저가 소유
		TakeDamageRoot* pTakeDamageRoot = pMonster->GetTakeDamageRoot();
		if( pTakeDamageRoot != NULL )
		{
			PerTakeDamage* per = (PerTakeDamage*)pTakeDamageRoot->pool;
			while( per )
			{
				if( damage < per->takeDamage )
				{
					damage = per->takeDamage;
					dropPlayerIdx = per->playerIdx;
				}

				per = (PerTakeDamage*)per->next;
			}
		}
		else
			NETWORK2->PostServerEvent("cActionDie::Die pTakeDamageMap == NULL");
       
	}

	/// 드롭 시스템 - 드롭
	DROP->MonsterDrop( pMonster->GetObjectID(), dropPlayerIdx, partyIdx );
	DROP->MonsterQuestDrop( pMonster->GetObjectID(), dropPlayerIdx, partyIdx );

	return Destroy( pMonster );
}


void cActionDie::Destroy( cMonster* pMonster )
{
	pMonster->Release();

	/// 그리드 시스템에 몬스터 삭제
	GRIDMANAGER->RemoveMonster( pMonster );

	if( pMonster->IsCheatTempMonster() == false )
	{
		/// 리젠 대기 몬스터 목록에 넣음
		AIMANAGER->InsertReadyMonster( pMonster->GetObjectID(), pMonster->GetRegenMonsterInfo()->mRegenIdx,
			pMonster->GetRegenMonsterInfo()->mGroupNumber );
	}
}