Advertisement
Guest User

MobManager.cpp

a guest
Dec 12th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.27 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Mob Manager Functions
  4. //
  5. // This will handle all things related to the MobList map
  6. // and a thread for the mobs as well
  7. // NPCs are included in this section as well.
  8. //////////////////////////////////////////////////////////////////////
  9.  
  10. #include "Stdafx.h"
  11. #include "GameServer.h"
  12. #include <map>
  13.  
  14. CMobManager* g_pMobManager = NULL;
  15.  
  16. CMobManager::CMobManager()
  17. {
  18.     m_bRun = true;
  19. }
  20.  
  21. CMobManager::~CMobManager()
  22. {
  23.     Release();
  24. }
  25.  
  26. void CMobManager::Init()
  27. {
  28.     CreateMobThread();
  29.     CreateNpcList();
  30.     CreateMonsterList();
  31.    
  32. }
  33.  
  34. void CMobManager::Release()
  35. {
  36.  
  37. }
  38.  
  39.  
  40. void CMobManager::Run()
  41. {
  42.     CGameServer * app = (CGameServer*)NtlSfxGetApp();
  43.     while (m_bRun)
  44.     {
  45.  
  46.         Sleep(100);
  47.     }
  48. }
  49.  
  50. void CMobManager::CreateMobThread()
  51. {
  52.  
  53.     pThread = CNtlThreadFactory::CreateThread(this, "CMMThread");
  54.     pThread->Start();
  55.  
  56. }
  57.  
  58. RwUInt32        CMobManager::CreateUniqueId(void)
  59. {
  60.     if (m_uiMobId++)
  61.     {
  62.         if (m_uiMobId == 0xffffffff)
  63.             m_uiMobId = 0;
  64.     }
  65.  
  66.     return m_uiMobId;
  67. }
  68.  
  69.  
  70. void CMobManager::CreateNpcList()
  71. {
  72.     CGameServer * app = (CGameServer*)NtlSfxGetApp();
  73.     CSpawnTable* pNPCSpawnTbl = app->g_pTableContainer->GetNpcSpawnTable(1);
  74.     for (CTable::TABLEIT itNPCSpawn = pNPCSpawnTbl->Begin(); itNPCSpawn != pNPCSpawnTbl->End(); ++itNPCSpawn)
  75.     {
  76.         sSPAWN_TBLDAT* pNPCSpwnTblData = (sSPAWN_TBLDAT*)itNPCSpawn->second;
  77.         sNPC_TBLDAT* pNPCTblData = (sNPC_TBLDAT*)app->g_pTableContainer->GetNpcTable()->FindData(pNPCSpwnTblData->mob_Tblidx);
  78.  
  79.         if (pNPCTblData)
  80.         {
  81.             CMonster::MonsterData * cr = new CMonster::MonsterData;
  82.  
  83.             cr->Level = pNPCTblData->byLevel;
  84.             cr->CurEP = pNPCTblData->wBasic_EP;
  85.             cr->CurLP = pNPCTblData->wBasic_LP;
  86.             cr->FightMode = false;
  87.             cr->IsDead = false;
  88.             cr->isSpawned = false;
  89.             cr->MonsterID = pNPCTblData->tblidx;
  90.             cr->MonsterSpawnID = pNPCSpwnTblData->tblidx;
  91.             cr->Spawn_Loc = pNPCSpwnTblData->vSpawn_Loc;
  92.             cr->Spawn_Dir = pNPCSpwnTblData->vSpawn_Dir;
  93.             cr->MaxEP = pNPCTblData->wBasic_EP;
  94.             cr->MaxLP = pNPCTblData->wBasic_LP;
  95.             cr->curPos = cr->Spawn_Loc;
  96.             cr->UniqueID = pNPCSpwnTblData->tblidx;
  97.             m_map_Npc.insert(std::make_pair(cr->UniqueID, cr));
  98.         }
  99.     }
  100. }
  101.  
  102. void CMobManager::CreateMonsterList()
  103. {
  104.     CGameServer * app = (CGameServer*)NtlSfxGetApp();
  105.     CWorldTable* pWorldTable = app->g_pTableContainer->GetWorldTable();
  106.     CSpawnTable* pMOBSpawnTbl = app->g_pTableContainer->GetMobSpawnTable(1);
  107.  
  108.     for (CTable::TABLEIT itMOBSpawn = pMOBSpawnTbl->Begin(); itMOBSpawn != pMOBSpawnTbl->End(); ++itMOBSpawn)
  109.     {
  110.         sSPAWN_TBLDAT* pMOBSpwnTblData = (sSPAWN_TBLDAT*)itMOBSpawn->second;
  111.         sMOB_TBLDAT* pMOBTblData = (sMOB_TBLDAT*)app->g_pTableContainer->GetMobTable()->FindData(pMOBSpwnTblData->mob_Tblidx);
  112.  
  113.         if (pMOBTblData)
  114.         {
  115.             CMonster::MonsterData * cr = new CMonster::MonsterData;
  116.  
  117.             cr->Level = pMOBTblData->byLevel;
  118.             cr->CurEP = pMOBTblData->wBasic_EP;
  119.             cr->CurLP = pMOBTblData->wBasic_LP;
  120.             cr->FightMode = false;
  121.             cr->IsDead = false;
  122.             cr->isSpawned = false;
  123.             cr->MonsterID = pMOBTblData->tblidx;
  124.             cr->MonsterSpawnID = pMOBSpwnTblData->tblidx;
  125.             cr->Spawn_Loc = pMOBSpwnTblData->vSpawn_Loc;
  126.             cr->Spawn_Dir = pMOBSpwnTblData->vSpawn_Dir;
  127.             cr->MaxEP = pMOBTblData->wBasic_EP;
  128.             cr->MaxLP = pMOBTblData->wBasic_LP;
  129.             cr->Spawn_Quantity = pMOBSpwnTblData->bySpawn_Quantity;
  130.             cr->Move_DelayTime = pMOBSpwnTblData->byMove_DelayTime;
  131.             cr->Run_Speed = pMOBTblData->fRun_Speed;
  132.             cr->Run_Speed_origin = pMOBTblData->fRun_Speed_Origin;
  133.             cr->Walk_Speed = pMOBTblData->fWalk_Speed;
  134.             cr->Walk_Speed_origin = pMOBTblData->fWalk_Speed_Origin;
  135.             cr->Spawn_Cool_Time = pMOBSpwnTblData->wSpawn_Cool_Time * 1000;
  136.             cr->target = 0;
  137.             cr->curPos = cr->Spawn_Loc;
  138.             cr->Basic_aggro_point = pMOBTblData->byScan_Range;
  139.             cr->Attack_range = pMOBTblData->fAttack_Range;
  140.             cr->MaxchainAttackCount = pMOBTblData->byAttack_Animation_Quantity;
  141.             cr->chainAttackCount = 0;
  142.             cr->UniqueID = CreateUniqueId();
  143.             m_map_Monster.insert(std::make_pair(cr->UniqueID, cr));
  144.         }
  145.     }
  146. }
  147.  
  148. bool CMobManager::RunSpawnCheck(CNtlPacket * pPacket, sVECTOR3 curPos, CClientSession * pSession)
  149. {
  150.     CGameServer * app = (CGameServer*)NtlSfxGetApp();
  151.  
  152. #pragma region monstercheck
  153.     CMonster::MonsterData* creaturelist;
  154.  
  155.     for (IterType it = m_map_Monster.begin(); it != m_map_Monster.end(); it++)
  156.     {
  157.         creaturelist = it->second;
  158.         if (CreatureRangeCheck(curPos, creaturelist->Spawn_Loc) == true)
  159.         {
  160.             if (creaturelist->IsDead == false && creaturelist->isSpawned == false)
  161.             {
  162.                 CNtlPacket packet(sizeof(sGU_OBJECT_CREATE));
  163.                 sGU_OBJECT_CREATE * res = (sGU_OBJECT_CREATE *)packet.GetPacketData();
  164.                 creaturelist->isSpawned = true;
  165.                 res->wOpCode = GU_OBJECT_CREATE;
  166.                 res->sObjectInfo.objType = OBJTYPE_MOB;
  167.                 res->handle = creaturelist->UniqueID;
  168.                 res->sObjectInfo.mobState.sCharStateBase.vCurLoc.x = creaturelist->Spawn_Loc.x;
  169.                 res->sObjectInfo.mobState.sCharStateBase.vCurLoc.y = creaturelist->Spawn_Loc.y;
  170.                 res->sObjectInfo.mobState.sCharStateBase.vCurLoc.z = creaturelist->Spawn_Loc.z;
  171.                 res->sObjectInfo.mobState.sCharStateBase.vCurDir.x = creaturelist->Spawn_Dir.x;
  172.                 res->sObjectInfo.mobState.sCharStateBase.vCurDir.y = creaturelist->Spawn_Dir.y;
  173.                 res->sObjectInfo.mobState.sCharStateBase.vCurDir.z = creaturelist->Spawn_Dir.z;
  174.                 res->sObjectInfo.mobState.sCharStateBase.byStateID = CHARSTATE_SPAWNING;
  175.                 res->sObjectInfo.mobState.sCharStateBase.bFightMode = creaturelist->FightMode;
  176.                 res->sObjectInfo.mobBrief.tblidx = creaturelist->MonsterID;
  177.                 res->sObjectInfo.mobBrief.wCurEP = creaturelist->CurEP;
  178.                 res->sObjectInfo.mobBrief.wMaxEP = creaturelist->MaxEP;
  179.                 res->sObjectInfo.mobBrief.wCurLP = creaturelist->CurLP;
  180.                 res->sObjectInfo.mobBrief.wMaxLP = creaturelist->MaxLP;
  181.                 res->sObjectInfo.mobBrief.fLastRunningSpeed = creaturelist->Run_Speed;
  182.                 res->sObjectInfo.mobBrief.fLastWalkingSpeed = creaturelist->Walk_Speed;
  183.                 creaturelist->spawnedForHandle[???] = pSession->GetavatarHandle();
  184.                 packet.SetPacketLen(sizeof(sGU_OBJECT_CREATE));
  185.                 g_pApp->Send(pSession->GetHandle(), &packet);
  186.             }
  187.         }
  188.    
  189.         else if ((creaturelist->isSpawned == true && CreatureRangeCheck(curPos, creaturelist->Spawn_Loc) == false) || creaturelist->IsDead == true)
  190.         {
  191.            
  192.             CNtlPacket packet(sizeof(sGU_OBJECT_DESTROY));
  193.             sGU_OBJECT_DESTROY * res = (sGU_OBJECT_DESTROY*)packet.GetPacketData();
  194.             res->wOpCode = GU_OBJECT_DESTROY;
  195.             res->handle = creaturelist->UniqueID;
  196.             creaturelist->isSpawned = false;
  197.             //creaturelist->isAggro = false;
  198.             creaturelist->target = 0;
  199.             //m_map_Monster.erase(creaturelist->UniqueID);
  200.  
  201.             packet.SetPacketLen(sizeof(sGU_OBJECT_DESTROY));
  202.             g_pApp->Send(pSession->GetHandle(), &packet);
  203.         }
  204.     }
  205.  
  206.  
  207. #pragma endregion Monstercheckend
  208. #pragma region npccheck
  209.  
  210.     CSpawnTable* pNPCSpawnTbl = app->g_pTableContainer->GetNpcSpawnTable(1);
  211.     for (IterType it = m_map_Npc.begin(); it != m_map_Npc.end(); it++)
  212.     {
  213.         creaturelist = it->second;
  214.         sNPC_TBLDAT* pNPCTblData = (sNPC_TBLDAT*)app->g_pTableContainer->GetNpcTable()->FindData(creaturelist->MonsterID);
  215.  
  216.         if (pNPCTblData && app->mob->CreatureRangeCheck(curPos, creaturelist->Spawn_Loc) == true && creaturelist->isSpawned == false){
  217.                 CNtlPacket packet(sizeof(sGU_OBJECT_CREATE));
  218.                 sGU_OBJECT_CREATE * sPacket = (sGU_OBJECT_CREATE *)packet.GetPacketData();
  219.  
  220.                 sPacket->wOpCode = GU_OBJECT_CREATE;
  221.                 sPacket->sObjectInfo.objType = OBJTYPE_NPC;
  222.                 sPacket->handle = CreateUniqueId();
  223.                 sPacket->sObjectInfo.npcState.sCharStateBase.vCurLoc.x = creaturelist->Spawn_Loc.x;
  224.                 sPacket->sObjectInfo.npcState.sCharStateBase.vCurLoc.y = creaturelist->Spawn_Loc.y;
  225.                 sPacket->sObjectInfo.npcState.sCharStateBase.vCurLoc.z = creaturelist->Spawn_Loc.z;
  226.                 sPacket->sObjectInfo.npcState.sCharStateBase.vCurDir.x = creaturelist->Spawn_Dir.x;
  227.                 sPacket->sObjectInfo.npcState.sCharStateBase.vCurDir.y = creaturelist->Spawn_Dir.y;
  228.                 sPacket->sObjectInfo.npcState.sCharStateBase.vCurDir.z = creaturelist->Spawn_Dir.z;
  229.                 sPacket->sObjectInfo.npcState.sCharStateBase.byStateID = CHARSTATE_SPAWNING;
  230.                 sPacket->sObjectInfo.npcBrief.wCurEP = pNPCTblData->wBasic_EP;
  231.                 sPacket->sObjectInfo.npcBrief.wCurLP = pNPCTblData->wBasic_LP;
  232.                 sPacket->sObjectInfo.npcBrief.wMaxEP = pNPCTblData->wBasic_EP;
  233.                 sPacket->sObjectInfo.npcBrief.wMaxLP = pNPCTblData->wBasic_LP;
  234.                 sPacket->sObjectInfo.npcBrief.tblidx = creaturelist->MonsterID;
  235.                 creaturelist->isSpawned = true;
  236.                 //pSession->InsertIntoMyNpcList(pNPCSpwnTblData->mob_Tblidx, pNPCSpwnTblData->vSpawn_Loc);
  237.  
  238.                 packet.SetPacketLen(sizeof(sGU_OBJECT_CREATE));
  239.                 g_pApp->Send(pSession->GetHandle(), &packet);
  240.  
  241.             }
  242.         }
  243.     return true;
  244.  
  245. #pragma endregion Npccheckend
  246. }
  247.  
  248. float CMobManager::Distance(const sVECTOR3 mycurPos, const CNtlVector othercurPos)
  249. {
  250.     float first = (mycurPos.x - othercurPos.x) * (mycurPos.x - othercurPos.x);
  251.     float second = (mycurPos.y - othercurPos.y) * (mycurPos.y - othercurPos.y);
  252.     float third = (mycurPos.z - othercurPos.z) * (mycurPos.z - othercurPos.z);
  253.     float distance = sqrt(first + second + third);
  254.     return distance;
  255. }
  256.  
  257. bool CMobManager::CreatureRangeCheck(sVECTOR3 mycurPos, CNtlVector othercurPos)
  258. {
  259.     float distance = Distance(mycurPos, othercurPos);
  260.     if (distance < DISTANCE_TO_SPAWN)
  261.     {
  262.         return true;
  263.     }
  264.     return false;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement