Clone of UAS2 @ https://github.com/drudgedance/uas2

WorldManager.h 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * This file is part of UAS2.
  3. *
  4. * UAS2 is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * UAS2 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with UASv1; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /* Special thanks to David Simpson for his work on the cell.dat and portal.dat extraction code */
  18. /**
  19. * @file WorldManager.h
  20. */
  21. #ifndef __WORLDMANAGER_H
  22. #define __WORLDMANAGER_H
  23. #pragma warning(disable:4786) //warning: identifier was truncated to '255' characters in the browser information
  24. #include <winsock2.h>
  25. #include <algorithm>
  26. #include <stack>
  27. #include "Avatar.h"
  28. #include "Client.h"
  29. #include "Object.h"
  30. #define CELLSECSIZE 64
  31. #define NUMFILELOC 0x03F
  32. #define ROOTDIRPTRLOC 0x148
  33. class cLandBlock
  34. {
  35. friend class cWorldManager;
  36. public:
  37. cLandBlock( WORD wLandBlock, BOOL fAddToHash = TRUE )
  38. : m_wLandBlock( wLandBlock ),
  39. m_pcPrev ( NULL ),
  40. m_pcNext ( NULL )
  41. {
  42. if ( fAddToHash )
  43. Hash_Add( this );
  44. Load_LandBlockZ( this );
  45. }
  46. ~cLandBlock( )
  47. {
  48. const WORD wSum = HIBYTE( m_wLandBlock ) + LOBYTE( m_wLandBlock );
  49. if ( m_pcPrev ) m_pcPrev->m_pcNext = m_pcNext;
  50. else m_lpcHashTable[wSum] = m_pcNext;
  51. if ( m_pcNext ) m_pcNext->m_pcPrev = m_pcPrev;
  52. }
  53. static inline void Hash_Load( )
  54. {
  55. ZeroMemory( m_lpcHashTable, sizeof( m_lpcHashTable ) );
  56. }
  57. static inline void Hash_Unload( )
  58. {
  59. cLandBlock *pcLB, *pcPrevLB;
  60. for ( int i = 0; i < 510; ++i )
  61. {
  62. pcLB = m_lpcHashTable[i];
  63. while ( pcLB )
  64. {
  65. pcPrevLB = pcLB;
  66. pcLB = pcLB->m_pcNext;
  67. while ( !pcPrevLB->m_lstObjects.empty( ) )
  68. {
  69. SAFEDELETE( pcPrevLB->m_lstObjects.front( ) )
  70. pcPrevLB->m_lstObjects.pop_front( );
  71. }
  72. SAFEDELETE( pcPrevLB )
  73. }
  74. }
  75. }
  76. static inline cLandBlock *Hash_New( WORD wLandBlock )
  77. {
  78. cLandBlock *pcLB = Hash_Find( wLandBlock );
  79. if ( pcLB )
  80. return pcLB;
  81. return new cLandBlock( wLandBlock );
  82. }
  83. static inline void Hash_Remove( cLandBlock *pcLB )
  84. {
  85. SAFEDELETE( pcLB )
  86. }
  87. static inline cLandBlock *Hash_Find( WORD wLandBlock )
  88. {
  89. const WORD wSum = HIBYTE( wLandBlock ) + LOBYTE( wLandBlock );
  90. cLandBlock *pcLB = m_lpcHashTable[wSum];
  91. while ( pcLB )
  92. { //crash occurs here
  93. if ( pcLB->m_wLandBlock == wLandBlock ) return pcLB;
  94. else pcLB = pcLB->m_pcNext;
  95. }
  96. //UpdateConsole(" Landblock hash checked...%d\n",&wLandBlock);
  97. return NULL;
  98. }
  99. WORD m_wLandBlock;
  100. float m_flLandBlockZ[9][9];
  101. private:
  102. static cLandBlock *m_lpcHashTable[510];
  103. static inline void Hash_Add( cLandBlock *pcLB )
  104. {
  105. const WORD wSum = HIBYTE( pcLB->m_wLandBlock ) + LOBYTE( pcLB->m_wLandBlock );
  106. if ( !m_lpcHashTable[wSum] )
  107. m_lpcHashTable[wSum] = pcLB;
  108. else
  109. {
  110. pcLB->m_pcNext = m_lpcHashTable[wSum];
  111. m_lpcHashTable[wSum]->m_pcPrev = pcLB;
  112. m_lpcHashTable[wSum] = pcLB;
  113. }
  114. }
  115. /**
  116. * Loads the cell.dat information for the specified file.
  117. *
  118. * Calls FetchFilePos to find a landblock's data's position.
  119. * Calls FetchFile to fetch a landblock's data from its found position.
  120. *
  121. * @param *pcLB - A pointer to the landblock the data of which is to be loaded from the cell.dat.
  122. */
  123. static inline void Load_LandBlockZ( cLandBlock *pcLB )
  124. {
  125. FILE *inFile;
  126. int read;
  127. UINT rootDirPtr, id, filePos, len;
  128. UCHAR *buf, *zData;
  129. inFile = fopen("cell.dat", "rb");
  130. if (inFile == NULL)
  131. {
  132. UpdateConsole(" Cell.dat: Open failed.\r\n");
  133. return;
  134. }
  135. read = fseek(inFile, ROOTDIRPTRLOC, SEEK_SET);
  136. if (read != 0)
  137. {
  138. UpdateConsole(" Cell.dat: Read error.\r\n");
  139. fclose(inFile);
  140. return;
  141. }
  142. read = fread(&rootDirPtr, sizeof(UINT), 1, inFile);
  143. if (read != 1)
  144. {
  145. UpdateConsole(" Cell.dat: End of file reached.\r\n");
  146. fclose(inFile);
  147. return;
  148. }
  149. id = pcLB->m_wLandBlock * 0x10000 + 0xFFFF;
  150. if (!cLandBlock::FetchFilePos(inFile, rootDirPtr, id, &filePos, &len))
  151. {
  152. UpdateConsole(" Cell.dat: File not found.\r\n");
  153. fclose(inFile);
  154. return;
  155. }
  156. buf = (UCHAR *)malloc(len);
  157. if (!cLandBlock::FetchFile(inFile, filePos, len, buf))
  158. {
  159. free(buf);
  160. fclose(inFile);
  161. return;
  162. }
  163. zData = &buf[170];
  164. char Command[100];
  165. for (int x = 0; x < 9; x++)
  166. {
  167. for (int y = 0; y < 9; y++)
  168. {
  169. pcLB->m_flLandBlockZ[x][y] = zData[x * 9 + y];
  170. sprintf(Command,"zData: %f\r\n", pcLB->m_flLandBlockZ[x][y]);
  171. //UpdateConsole(Command);
  172. }
  173. }
  174. free(buf);
  175. fclose(inFile);
  176. }
  177. /**
  178. * Finds the location of a landblock's data in the cell.dat.
  179. *
  180. * @param &inFile - The address of the file/directory to search (the cell.dat).
  181. * @param disPos - The beginning position of the file pointer (in BYTEs).
  182. * @param id - The landblock to be loaded from the cell.dat.
  183. * @param *filePos - A pointer to the variable that should receive the position of the landblock data.
  184. * @param *len - A pointer to the variable that should receive the length of the landblock data.
  185. */
  186. static inline int FetchFilePos(FILE *inFile, UINT dirPos, UINT id, UINT *filePos, UINT *len)
  187. {
  188. UINT dir[4 * CELLSECSIZE];
  189. UINT i;
  190. UINT numFiles;
  191. int read;
  192. while (1)
  193. {
  194. if (dirPos == 0)
  195. {
  196. UpdateConsole(" Cell.dat: NULL directory entry found.\r\n");
  197. return 0;
  198. }
  199. read = fseek(inFile, dirPos, SEEK_SET);
  200. if (read != 0)
  201. {
  202. UpdateConsole(" Cell.dat: Sector is beyond end of file.\r\n");
  203. return 0;
  204. }
  205. read = fread(dir, sizeof(UINT), CELLSECSIZE, inFile);
  206. if (read != CELLSECSIZE)
  207. {
  208. UpdateConsole(" Cell.dat: Sector doesn't contain enough words.\r\n");
  209. return 0;
  210. }
  211. dirPos = dir[0];
  212. if (dirPos != 0)
  213. {
  214. read = fseek(inFile, dirPos, SEEK_SET);
  215. if (read != 0)
  216. {
  217. UpdateConsole(" Cell.dat: Seek is beyond end of file.\r\n");
  218. return 0;
  219. }
  220. read = fread(&dirPos, sizeof(UINT), 1, inFile);
  221. if (read != 1)
  222. {
  223. UpdateConsole(" Cell.dat: Sector does not exist.\r\n");
  224. return 0;
  225. }
  226. read = fread(&dir[CELLSECSIZE], sizeof(UINT), CELLSECSIZE - 1, inFile);
  227. if (read != CELLSECSIZE - 1)
  228. {
  229. UpdateConsole(" Cell.dat: Sector doesn't contain enough words.\r\n");
  230. return 0;
  231. }
  232. }
  233. if (dirPos != 0)
  234. {
  235. read = fseek(inFile, dirPos, SEEK_SET);
  236. if (read != 0)
  237. {
  238. UpdateConsole(" Cell.dat: Seek is beyond end of file.\r\n");
  239. return 0;
  240. }
  241. read = fread(&dirPos, sizeof(UINT), 1, inFile);
  242. if (read != 1)
  243. {
  244. UpdateConsole(" Cell.dat: Sector does not exist.\r\n");
  245. return 0;
  246. }
  247. read = fread(&dir[CELLSECSIZE * 2 - 1], sizeof(UINT), CELLSECSIZE - 1, inFile);
  248. if (read != CELLSECSIZE - 1)
  249. {
  250. UpdateConsole(" Cell.dat: Sector doesn't contain enough words.\r\n");
  251. return 0;
  252. }
  253. }
  254. if (dirPos != 0)
  255. {
  256. read = fseek(inFile, dirPos, SEEK_SET);
  257. if (read != 0)
  258. {
  259. UpdateConsole(" Cell.dat: Seek is beyond end of file\r\n");
  260. return 0;
  261. }
  262. read = fread(&dirPos, sizeof(UINT), 1, inFile);
  263. if (read != 1)
  264. {
  265. UpdateConsole(" Cell.dat: Sector does not exist.\r\n");
  266. return 0;
  267. }
  268. read = fread(&dir[CELLSECSIZE * 3 - 2], sizeof(UINT), CELLSECSIZE - 1, inFile);
  269. if (read != CELLSECSIZE - 1)
  270. {
  271. UpdateConsole(" Cell.dat: Sector doesn't contain enough words\r\n");
  272. return 0;
  273. }
  274. }
  275. numFiles = dir[NUMFILELOC];
  276. if (numFiles >= NUMFILELOC)
  277. {
  278. UpdateConsole(" Cell.dat: Number of files exceeds directory entries.\r\n");
  279. return 0;
  280. }
  281. i = 0;
  282. while ((i < numFiles) && (id > dir[i * 3 + NUMFILELOC + 1]))
  283. {
  284. i++;
  285. }
  286. if (i < numFiles)
  287. {
  288. if (id == dir[i*3 + NUMFILELOC + 1])
  289. {
  290. *filePos = dir[i * 3 + NUMFILELOC + 2];
  291. *len = dir[i * 3 + NUMFILELOC + 3];
  292. return 1;
  293. }
  294. }
  295. if (dir[1] == 0)
  296. {
  297. filePos = 0;
  298. len = 0;
  299. return 0;
  300. }
  301. dirPos = dir[i + 1];
  302. }
  303. return 0;
  304. }
  305. /**
  306. * Fetches landblock information from the cell.dat.
  307. *
  308. * @param *inFile - A pointer to the file/directory to search (the cell.dat).
  309. * @param filePos - The variable that should receive the position of the landblock data.
  310. * @param len - The variable that should receive the length of the landblock data.
  311. * *@param &buf - A pointer to the buffer that should receive the landblock data.
  312. */
  313. static inline int FetchFile(FILE *inFile, UINT filePos, UINT len, UCHAR *buf)
  314. {
  315. int read, doChain;
  316. UINT sec[CELLSECSIZE];
  317. if (filePos == 0)
  318. {
  319. UpdateConsole(" Cell.dat: Null file pointer found.\r\n");
  320. return 0;
  321. }
  322. doChain = 1;
  323. while (doChain)
  324. {
  325. read = fseek(inFile, filePos, SEEK_SET);
  326. if (read != 0)
  327. {
  328. UpdateConsole(" Cell.dat: Seek failed.\r\n");
  329. return 0;
  330. }
  331. read = fread(sec, sizeof(UINT), CELLSECSIZE, inFile);
  332. if (read != CELLSECSIZE)
  333. {
  334. UpdateConsole(" Cell.dat: Sector doesn't contain enough words.\r\n");
  335. return 0;
  336. }
  337. filePos = sec[0] * 0x7FFFFFFF;
  338. if (len > (CELLSECSIZE - 1) * sizeof(UINT))
  339. {
  340. memcpy(buf, &sec[1], (CELLSECSIZE - 1) * sizeof(UINT));
  341. buf += (CELLSECSIZE - 1) * sizeof(UINT);
  342. len -= (CELLSECSIZE - 1) * sizeof(UINT);
  343. }
  344. else
  345. {
  346. memcpy(buf, &sec[1], len);
  347. len = 0;
  348. }
  349. if (filePos == 0)
  350. doChain = 0;
  351. }
  352. return 1;
  353. }
  354. std::list< cClient * > m_lstClients;
  355. std::list< cClient * > m_lstFocusedClients;
  356. std::list< cObject * > m_lstObjects;
  357. cLandBlock *m_pcPrev;
  358. cLandBlock *m_pcNext;
  359. };
  360. class cWorldManager
  361. {
  362. friend class cMasterServer;
  363. public:
  364. cWorldManager ( ) {}
  365. ~cWorldManager ( ) {}
  366. typedef std::vector< ConfirmPanel >::iterator iterConfirmPanel_lst;
  367. static void Load( );
  368. static void Unload( );
  369. static BOOL RemoveClient ( cClient *pcClient, BOOL fRemoveAvatar = TRUE, BOOL fDeleteAvatar = TRUE );
  370. static BOOL AddClient ( cClient *pcClient, BOOL fSpawnAvatar = TRUE );
  371. static BOOL MoveAvatar ( cClient *pcClient, cLocation& NewLoc, WORD wAnim = 0, float flPlaySpeed = 1.0f );
  372. static BOOL TeleportAvatar ( cClient *pcClient, cLocation& NewLoc );
  373. static BOOL RemoveObject ( cObject *pcObject, BOOL fRemoveObject = TRUE, BOOL fDeleteObject = TRUE );
  374. static BOOL AddObject ( cObject *pcObject, BOOL fSpawnObject = TRUE );
  375. static BOOL MoveAddObject ( cObject *pcObject ); // Object changes landblock location
  376. static BOOL MoveRemObject ( cObject *pcObject ); // Object changes landblock location
  377. static void RemoveAllObjects( );
  378. static inline void SendToAllInLandBlock( cLocation& Loc, cMessage& cmPacket, WORD wGroup )
  379. {
  380. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( Loc.m_dwLandBlock ) );
  381. if ( pcLB )
  382. SendToAllInLandBlock( pcLB, cmPacket, wGroup );
  383. }
  384. static inline void SendToAllInFocus( cLocation& Loc, cMessage& cmPacket, WORD wGroup )
  385. {
  386. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( Loc.m_dwLandBlock ) );
  387. if ( pcLB )
  388. SendToAllInFocus( pcLB, cmPacket, wGroup );
  389. }
  390. static inline void SendToAllWithin( BYTE bDistance, cLocation& Loc, cMessage& cmPacket, WORD wGroup )
  391. {
  392. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( Loc.m_dwLandBlock ) );
  393. if ( pcLB )
  394. SendToAllWithin( bDistance, pcLB, cmPacket, wGroup );
  395. }
  396. static inline void SendToOthersInLandBlock( cLocation& Loc, cClient *pcClient, cMessage& cmPacket, WORD wGroup )
  397. {
  398. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( Loc.m_dwLandBlock ) );
  399. if ( pcLB )
  400. SendToOthersInLandBlock( pcLB, pcClient, cmPacket, wGroup );
  401. }
  402. static inline void SendToOthersInFocus( cLocation& Loc, cClient *pcClient, cMessage& cmPacket, WORD wGroup )
  403. {
  404. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( Loc.m_dwLandBlock ) );
  405. if ( pcLB )
  406. SendToOthersInFocus( pcLB, pcClient, cmPacket, wGroup );
  407. }
  408. static inline void SendToOthersWithin( BYTE bDistance, cLocation& Loc, cClient *pcClient, cMessage& cmPacket, WORD wGroup )
  409. {
  410. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( Loc.m_dwLandBlock ) );
  411. if ( pcLB )
  412. SendToOthersWithin( bDistance, pcLB, pcClient, cmPacket, wGroup );
  413. }
  414. static inline cObject *GetCollisionPortal( cLocation OldLoc, cLocation NewLoc )
  415. {
  416. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( OldLoc.m_dwLandBlock ) );
  417. for ( iterObject_lst itObject = pcLB->m_lstObjects.begin( ); itObject != pcLB->m_lstObjects.end( ); ++itObject )
  418. {
  419. //UpdateConsole(" Item Checked: %s Type: %d\r\n", (*itObject)->Name(), (*itObject)->GetType());
  420. if ( (*itObject)->GetType() == 1000 )
  421. {
  422. float flDist = cPhysics::Get3DLineDistance( OldLoc, NewLoc, (*itObject)->m_Location );
  423. float flOldDist = cPhysics::Get3DRange( OldLoc, (*itObject)->m_Location );
  424. /*
  425. char szPortalInfo[100];
  426. sprintf( szPortalInfo, "Portal Found: %s, Range: %f, OldRange: %f\r\n", (*itObject)->Name(), flDist, flOldDist );
  427. UpdateConsole((char *)szPortalInfo);
  428. */
  429. if ( flDist < .05 && flOldDist >= .05)
  430. return *itObject;
  431. }
  432. }
  433. return NULL;
  434. }
  435. static inline cObject *GetCollisionMonster( DWORD SpellGUID, cLocation SpellLoc )
  436. {
  437. cLandBlock *pcLB = cLandBlock::Hash_Find( HIWORD( SpellLoc.m_dwLandBlock ) );
  438. for ( iterObject_lst itObject = pcLB->m_lstObjects.begin( ); itObject != pcLB->m_lstObjects.end( ); ++itObject )
  439. {
  440. //UpdateConsole("Item Checked: %s Type: %d\r\n", (*itObject)->Name(), (*itObject)->GetType());
  441. if ( SpellGUID != (*itObject)->GetGUID() ) //(*itObject)->GetType() == 0 )
  442. {
  443. float flDist = cPhysics::GetRange( SpellLoc, (*itObject)->m_Location );
  444. char szPortalInfo[100];
  445. sprintf( szPortalInfo, "Collision Target: %s, Range: %f\r\n", (*itObject)->Name(), flDist );
  446. //UpdateConsole((char *)szPortalInfo);
  447. if ( flDist < .05 && (*itObject)->m_dwObjectFlags1 & 0x00000010 && (*itObject)->m_fDeadOrAlive == true)
  448. return *itObject;
  449. }
  450. }
  451. return NULL;
  452. }
  453. static inline cObject *FindObject( DWORD dwGUID )
  454. {
  455. cLandBlock *pcLB;
  456. for ( int i = 0; i < 510; ++i )
  457. {
  458. pcLB = cLandBlock::m_lpcHashTable[i];
  459. while ( pcLB )
  460. {
  461. for ( iterObject_lst itObject = pcLB->m_lstObjects.begin( ); itObject != pcLB->m_lstObjects.end( ); ++itObject )
  462. {
  463. if ( dwGUID == (*itObject)->GetGUID( ) )
  464. return *itObject;
  465. }
  466. pcLB = pcLB->m_pcNext;
  467. }
  468. }
  469. return NULL;
  470. }
  471. //Test to find corpses
  472. static inline cCorpse *FindCorpse( DWORD dwGUID )
  473. {
  474. cLandBlock *pcLB;
  475. for ( int i = 0; i < 510; ++i )
  476. {
  477. pcLB = cLandBlock::m_lpcHashTable[i];
  478. while ( pcLB )
  479. {
  480. for ( iterObject_lst itObject = pcLB->m_lstObjects.begin( ); itObject != pcLB->m_lstObjects.end( ); ++itObject )
  481. {
  482. if ( dwGUID == (*itObject)->GetGUID( ) )
  483. return reinterpret_cast<cCorpse *>(*itObject);
  484. }
  485. pcLB = pcLB->m_pcNext;
  486. }
  487. }
  488. return NULL;
  489. }
  490. //Find avatars
  491. //Fix: Search within landblock focus.
  492. static inline cAvatar *FindAvatar( DWORD dwGUID )
  493. {
  494. cLandBlock *pcLB;
  495. for ( int i = 0; i < 510; ++i )
  496. {
  497. pcLB = cLandBlock::m_lpcHashTable[i];
  498. while ( pcLB )
  499. {
  500. for ( iterClient_lst itClient = pcLB->m_lstClients.begin( ); itClient != pcLB->m_lstClients.end( ); ++itClient )
  501. {
  502. // char szCharInfo[100];
  503. // sprintf( szCharInfo, "Client Avatar %d\n",(*itClient)->m_pcAvatar->m_dwGUID );
  504. // UpdateConsole ((char *)szCharInfo);
  505. if ( dwGUID == (*itClient)->m_pcAvatar->m_dwGUID )
  506. return reinterpret_cast<cAvatar *>((*itClient)->m_pcAvatar);
  507. }
  508. pcLB = pcLB->m_pcNext;
  509. }
  510. }
  511. return NULL;
  512. }
  513. //k109: Added this to try and implement item ID in npc inventory.
  514. static inline cNPC *FindNPC( DWORD dwGUID )
  515. {
  516. cLandBlock *pcLB;
  517. for ( int i = 0; i < 510; ++i )
  518. {
  519. pcLB = cLandBlock::m_lpcHashTable[i];
  520. while ( pcLB )
  521. {
  522. for ( iterObject_lst itObject = pcLB->m_lstObjects.begin( ); itObject != pcLB->m_lstObjects.end( ); ++itObject )
  523. {
  524. if ( dwGUID == (*itObject)->GetGUID( ) )
  525. return reinterpret_cast<cNPC *>(*itObject);
  526. }
  527. pcLB = pcLB->m_pcNext;
  528. }
  529. }
  530. return NULL;
  531. }
  532. //TODO: Create list to hold recycled ObjectGUIDs (GUIDs of objects that have ceased existing).
  533. //Preference would be given to using rec ycled GUIDs from this list over creating new ones.
  534. static inline UINT NewGUID_Avatar ( ) { return ++m_dwGUIDCycle_Avatar; }
  535. static inline UINT NewGUID_Object ( ) { return ++m_dwGUIDCycle_Object; }
  536. static inline void AddUnusedAvatarGUID ( DWORD dwGUID ) { m_lstUnusedAvatarGUIDs.push_back(dwGUID); }
  537. static inline void AddUnusedObjectGUID ( DWORD dwGUID ) { m_lstUnusedObjectGUIDs.push_back(dwGUID); }
  538. static inline DWORD GetUnusedAvatarGUID ( ) { return m_lstUnusedAvatarGUIDs.back(); }
  539. static inline DWORD GetUnusedObjectGUID ( ) { return m_lstUnusedObjectGUIDs.back(); }
  540. static char g_szAccessFile[MAX_PATH+20];
  541. static inline DWORD CurrentConfirmSeq ( ) { return m_dwConfirmSequence; }
  542. static inline DWORD NewConfirmSeq ( ) { return ++m_dwConfirmSequence; }
  543. static void AddPendingConfirm ( DWORD sequence, DWORD type, std::string szText, DWORD senderGUID, DWORD receiptGUID = NULL );
  544. static void FindPendingConfirm ( DWORD dwSeq, DWORD dwReply );
  545. private:
  546. static inline void RemoveAvatar( cClient *pcClient, cLandBlock *pcLB, WORD wAnim = 0, float flPlaySpeed = 1.0f )
  547. {
  548. if ( wAnim )
  549. {
  550. pcClient->m_pcAvatar->LogoutCharacter();
  551. cMessage cmAnim = pcClient->m_pcAvatar->Animation( wAnim, flPlaySpeed );
  552. SendToAllInFocus( pcLB, cmAnim, 3 );
  553. }
  554. cMessage cmRemModel;
  555. cmRemModel << 0xF747L << pcClient->m_pcAvatar->GetGUID( ) << DWORD( pcClient->m_pcAvatar->m_wNumLogins );
  556. SendToOthersInFocus( pcLB, pcClient, cmRemModel, 3 );
  557. }
  558. static inline void RemoveObject( cObject *pcObject, cLandBlock *pcLB )
  559. {
  560. cMessage cmRemModel;
  561. cmRemModel << 0xF747L << pcObject->GetGUID( ) << DWORD( pcObject->m_wNumLogins );
  562. SendToAllInFocus( pcLB, cmRemModel, 3 );
  563. }
  564. /*
  565. static inline void RemoveObjectContainer( cClient *pcClient, cObject *pcObject, DWORD dwContainer )
  566. {
  567. cMessage cmRemoveItem;
  568. cmRemoveItem << 0x0024L << pcObject->GetGUID( );
  569. pcClient->AddPacket(WORLD_SERVER,cmRemoveItem,4);
  570. }
  571. */
  572. static inline void SpawnAvatar( cClient *pcClient, cLandBlock *pcLB )
  573. {
  574. cMessage cmLogin;
  575. *((DWORD *)&MSG_00000002[4]) = pcClient->m_pcAvatar->GetGUID( );
  576. cmLogin.CannedData( MSG_00000002, sizeof( MSG_00000002 ) );
  577. // Receive the create messages for all other object in focus range.
  578. ReceiveAllObjectsInFocus( pcClient, pcLB );
  579. // Send this avatar's create and materialize messages to others.
  580. /*
  581. BOOL fContinue;
  582. do
  583. {
  584. cMessage cmChildren = pcClient->m_pcAvatar->CreateChildren( fContinue );
  585. if( fContinue )
  586. SendToAllInFocus( pcLB, cmChildren, 3 );
  587. } while ( fContinue );
  588. */
  589. SendToOthersInFocus( pcLB, pcClient, pcClient->m_pcAvatar->CreatePacket( ), 3 );
  590. SendToAllInFocus( pcLB, cmLogin, 3 );
  591. }
  592. static inline void SpawnObject( cObject *pcObject, cLandBlock *pcLB, cClient *pcClient = NULL )
  593. {
  594. if( pcClient )
  595. SendToOthersInFocus( pcLB, pcClient, pcObject->CreatePacket( ), 3 );
  596. else
  597. SendToAllInFocus( pcLB, pcObject->CreatePacket( ), 3 );
  598. }
  599. static inline void SendToAllInLandBlock( cLandBlock *pcLB, cMessage& cmPacket, WORD wGroup )
  600. {
  601. for ( iterClient_lst itClient = pcLB->m_lstClients.begin( ); itClient != pcLB->m_lstClients.end( ); ++itClient )
  602. (*itClient)->AddPacket( WORLD_SERVER, cmPacket, wGroup );
  603. }
  604. static inline void SendToAllInFocus( cLandBlock *pcLB, cMessage& cmPacket, WORD wGroup )
  605. {
  606. SendToAllInLandBlock( pcLB, cmPacket, wGroup );
  607. for ( iterClient_lst itClient = pcLB->m_lstFocusedClients.begin( ); itClient != pcLB->m_lstFocusedClients.end( ); ++itClient )
  608. (*itClient)->AddPacket( WORLD_SERVER, cmPacket, wGroup );
  609. }
  610. static inline void SendToAllWithin( BYTE bDistance, cLandBlock *pcLB, cMessage& cmPacket, WORD wGroup )
  611. {
  612. const WORD wDiameter = (bDistance * 2 + 1) * (bDistance * 2 + 1);
  613. WORD *pwLBs = new WORD[wDiameter];
  614. LandBlocksWithin( bDistance, pcLB->m_wLandBlock, pwLBs );
  615. SendToAllInLandBlock( pcLB, cmPacket, wGroup );
  616. for ( int i = 0; i < wDiameter - 1; ++i )
  617. {
  618. pcLB = cLandBlock::Hash_Find( pwLBs[i] );
  619. if ( pcLB )
  620. SendToAllInLandBlock( pcLB, cmPacket, wGroup );
  621. }
  622. SAFEDELETE_ARRAY( pwLBs )
  623. }
  624. static inline void SendToOthersInLandBlock( cLandBlock *pcLB, cClient *pcClient, cMessage& cmPacket, WORD wGroup )
  625. {
  626. for ( iterClient_lst itClient = pcLB->m_lstClients.begin( ); itClient != pcLB->m_lstClients.end( ); ++itClient )
  627. {
  628. if ( pcClient != *itClient )
  629. (*itClient)->AddPacket( WORLD_SERVER, cmPacket, wGroup );
  630. }
  631. }
  632. static inline void SendToOthersInFocus( cLandBlock *pcLB, cClient *pcClient, cMessage& cmPacket, WORD wGroup )
  633. {
  634. SendToOthersInLandBlock( pcLB, pcClient, cmPacket, wGroup );
  635. for ( iterClient_lst itClient = pcLB->m_lstFocusedClients.begin( ); itClient != pcLB->m_lstFocusedClients.end( ); ++itClient )
  636. {
  637. if ( pcClient != *itClient )
  638. {
  639. try
  640. {
  641. (*itClient)->AddPacket( WORLD_SERVER, cmPacket, wGroup );
  642. }
  643. catch ( char * str )
  644. {
  645. UpdateConsole(" Exception occured: %s", str);
  646. }
  647. }
  648. }
  649. }
  650. static inline void SendToOthersWithin( BYTE bDistance, cLandBlock *pcLB, cClient *pcClient, cMessage& cmPacket, WORD wGroup )
  651. {
  652. const WORD wDiameter = (bDistance * 2 + 1) * (bDistance * 2 + 1);
  653. WORD *pwLBs = new WORD[wDiameter];
  654. LandBlocksWithin( bDistance, pcLB->m_wLandBlock, pwLBs );
  655. SendToOthersInLandBlock( pcLB, pcClient, cmPacket, wGroup );
  656. for ( int i = 0; i < wDiameter - 1; ++i )
  657. {
  658. pcLB = cLandBlock::Hash_Find( pwLBs[i] );
  659. if ( pcLB )
  660. SendToOthersInLandBlock( pcLB, pcClient, cmPacket, wGroup );
  661. }
  662. SAFEDELETE_ARRAY( pwLBs )
  663. }
  664. static inline void AddClient( cLandBlock *pcLB, cClient *pcClient )
  665. {
  666. iterClient_lst itClient = std::find( pcLB->m_lstClients.begin( ), pcLB->m_lstClients.end( ), pcClient );
  667. if ( itClient == pcLB->m_lstClients.end( ) )
  668. pcLB->m_lstClients.push_back( pcClient );
  669. }
  670. static inline void AddFocus( cLandBlock *pcLB, cClient *pcClient )
  671. {
  672. iterClient_lst itClient = std::find( pcLB->m_lstFocusedClients.begin( ), pcLB->m_lstFocusedClients.end( ), pcClient );
  673. if ( itClient == pcLB->m_lstFocusedClients.end( ) )
  674. pcLB->m_lstFocusedClients.push_back( pcClient );
  675. }
  676. static inline void AddObject( cLandBlock *pcLB, cObject *pcObject )
  677. {
  678. iterObject_lst itObject = std::find( pcLB->m_lstObjects.begin( ), pcLB->m_lstObjects.end( ), pcObject );
  679. if ( itObject == pcLB->m_lstObjects.end( ) )
  680. pcLB->m_lstObjects.push_back( pcObject );
  681. }
  682. static inline BOOL RemClient( cLandBlock *pcLB, cClient *pcClient )
  683. {
  684. iterClient_lst itClient = std::find( pcLB->m_lstClients.begin( ), pcLB->m_lstClients.end( ), pcClient );
  685. if ( itClient == pcLB->m_lstClients.end( ) )
  686. return FALSE;
  687. pcLB->m_lstClients.erase( itClient );
  688. if ( pcLB->m_lstClients.empty( ) && pcLB->m_lstFocusedClients.empty( ) && pcLB->m_lstObjects.empty( ) )
  689. cLandBlock::Hash_Remove( pcLB );
  690. return TRUE;
  691. }
  692. static inline BOOL RemFocus( cLandBlock *pcLB, cClient *pcClient)
  693. {
  694. iterClient_lst itClient = std::find( pcLB->m_lstFocusedClients.begin( ), pcLB->m_lstFocusedClients.end( ), pcClient );
  695. if ( itClient == pcLB->m_lstFocusedClients.end( ) )
  696. return FALSE;
  697. pcLB->m_lstFocusedClients.erase( itClient );
  698. if ( pcLB->m_lstClients.empty( ) && pcLB->m_lstFocusedClients.empty( ) && pcLB->m_lstObjects.empty( ) )
  699. cLandBlock::Hash_Remove( pcLB );
  700. return TRUE;
  701. }
  702. static inline BOOL RemObject( cLandBlock *pcLB, cObject *pcObject )
  703. {
  704. iterObject_lst itObject = std::find( pcLB->m_lstObjects.begin( ), pcLB->m_lstObjects.end( ), pcObject );
  705. if ( itObject == pcLB->m_lstObjects.end( ) )
  706. return FALSE;
  707. pcLB->m_lstObjects.erase( itObject );
  708. return TRUE;
  709. }
  710. static void ReceiveAllObjectsInFocus ( cClient *pcClient, cLandBlock *pcLB );
  711. static void ReceiveAllObjectsInLandBlock( cClient *pcClient, cLandBlock *pcLB );
  712. static void LandBlocksWithin( BYTE bDistance, WORD wCenterLB, WORD *pwStore );
  713. static DWORD m_dwGUIDCycle_Avatar;
  714. static DWORD m_dwGUIDCycle_Object;
  715. static std::list< DWORD > m_lstUnusedAvatarGUIDs;
  716. static std::list< DWORD > m_lstUnusedObjectGUIDs;
  717. static DWORD m_dwConfirmSequence;
  718. static std::vector< ConfirmPanel > m_lstConfirmsPending;
  719. };
  720. #endif // #ifndef __WORLDMANAGER_H