#include "stdafx.h" #include "Logger.h" #include "NpcArray.h" void NpcArray::Init() { npcArray_updateHWND = NULL; npcArray_updateMSG = 0; npcArray_count = 0; InitializeCriticalSection( &cs_npcs_array ); int i; for( i=0; iisUnused() ) log_error( LOG_ERROR, "NpcArray_UpdateNpcInfo(): idx[%d] is unused!\n", idx ); // save old move vectors (since by default unused NpcInfo is not moving) int xDst = npcs_array[idx]->xDst; int yDst = npcs_array[idx]->yDst; int zDst = npcs_array[idx]->zDst; // memcpy( npcs_array[idx], pNpcInfo, sizeof(class L2Npc) ); // restore old move vectors (since by default unused NpcInfo is not moving) npcs_array[idx]->xDst = xDst; npcs_array[idx]->yDst = yDst; npcs_array[idx]->zDst = zDst; // Unlock(); PostUpdateMessage(); //log_error( LOG_OK, "NpcArray: Updated[%d]: [%S] [%S] (%d,%d,%d) tpml %u, oid %u\n", // idx, // name, title, x,y,z, templateID, objectID ); } /*void NpcArray::DelNPC( unsigned int objectID ) { Lock(); int idx = FindNPCByObjectID( objectID ); if( idx == -1 ) { Unlock(); return; } if( npcs_array[idx] ) { npcs_array[idx]->setUnused(); } //log_error( LOG_OK, "NpcArray: deleted " ); Unlock(); }*/ void NpcArray::DelNPCByArrayIdx( int idx ) { if( idx<0 || (idx>=NPCA_MAX_NPCS) ) return; Lock(); if( npcs_array[idx] ) { npcs_array[idx]->setUnused(); npcArray_count--; //log_error( LOG_OK, "NpcArray: deleted npc %u from index OK\n", objectID, idx ); } Unlock(); PostUpdateMessage(); } void NpcArray::DeleteAll() { Lock(); int i; for( i=0; isetUnused(); } npcArray_count = 0; Unlock(); PostUpdateMessage(); } void NpcArray::Lock() { EnterCriticalSection( &cs_npcs_array ); } void NpcArray::Unlock() { LeaveCriticalSection( &cs_npcs_array ); } int NpcArray::FindNPCByObjectID( unsigned int objectID ) { int ret = -1; int i; Lock(); for( i=0; iobjectID == objectID ) { ret = i; break; } } Unlock(); return ret; } int NpcArray::FindFreeIndex() { int i; Lock(); for( i=0; isetUnused(); // and set as unused Unlock(); return i; } if( npcs_array[i]->isUnused() ) { Unlock(); return i; } } Unlock(); log_error( LOG_ERROR, "NpcArray_FindFreeIndex(): canot find free index, limit is %d!\n", NPCA_MAX_NPCS ); return -1; } void NpcArray::SetUpdateCommand( HWND hWnd, UINT uMsg ) { npcArray_updateHWND = hWnd; npcArray_updateMSG = uMsg; } void NpcArray::PostUpdateMessage() { if( !npcArray_updateHWND ) return; if( npcArray_updateMSG < WM_USER ) return; PostMessage( npcArray_updateHWND, npcArray_updateMSG, 0, 0 ); } void NpcArray::DisplayToConsole() { int i = 0; unsigned int nDisplayed = 0; log_error( LOG_USERAI, "=== NPCs: %d ===\n", npcArray_count ); if( npcArray_count > 0 ) { Lock(); for( i=0; iisUnused() ) log_error( LOG_USERAI, "NPC: %2d: \n", i ); else { log_error( LOG_USERAI, "NPC: %2d: %u [%S] [%S]\n", i, npcs_array[i]->templateID, npcs_array[i]->charName, npcs_array[i]->charTitle ); nDisplayed++; } if( nDisplayed >= npcArray_count ) break; } Unlock(); } log_error( LOG_USERAI, "=== NPCs End ===\n" ); }