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

CorpseCleaner.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /**
  18. * @file CorpseCleaner.cpp
  19. * Implements functionality for corpse addition and removal to and from the landscape.
  20. *
  21. * The CorseCleaner object contains an array of all corpses presently on the landscape.
  22. * This class is referenced whenever a corpse is created, removed, or check to be removed.
  23. */
  24. #include "CorpseCleaner.h"
  25. DWORD CorpseCleaner::Corpses[MAX_CORPSES][4];
  26. time_t CorpseCleaner::NextCleanTime;
  27. time_t CorpseCleaner::longtime;
  28. /***************
  29. * constructors
  30. **************/
  31. /**
  32. * Handles the creation of of the CorpseCleaner corpse array.
  33. *
  34. * Called whenever the CorpseCleaner object should be initialized.
  35. */
  36. CorpseCleaner::CorpseCleaner()
  37. {
  38. for(DWORD i = 0; i < MAX_CORPSES; i++)
  39. {
  40. Corpses[i][0] = 0; // Objects GUID
  41. Corpses[i][1] = 0; // Decay Timer
  42. Corpses[i][2] = 0; // Is this a ReSpawnable object
  43. // 0 = No, #>0 is delay time for respawn after corpse is removed
  44. Corpses[i][3] = 0; // Unused
  45. }
  46. time(&NextCleanTime + DELAY);
  47. }
  48. CorpseCleaner::~CorpseCleaner()
  49. {
  50. }
  51. /**********
  52. * methods
  53. *********/
  54. /**
  55. * Handles the addition of a corpse to the CorpseCleaner.
  56. *
  57. * This function should be called whenever a corpse is created.
  58. * @param dwGUID - The GUID of the corpse to add
  59. * @param dwDelay - The default length of time for a corpse to remain on the landscape
  60. * @param dwReSpawn - The additional delay for the the particular corpse
  61. */
  62. bool CorpseCleaner::AddCorpse(DWORD dwGUID, DWORD dwDelay, DWORD dwReSpawn)
  63. {
  64. bool fAdded = false;
  65. DWORD dwCleanupDelay;
  66. if(dwDelay == 0)
  67. {
  68. dwCleanupDelay = CORPSE_DELAY;
  69. }
  70. else
  71. {
  72. dwCleanupDelay = dwDelay;
  73. }
  74. for(int i = 0; i < MAX_CORPSES; i++)
  75. {
  76. if(Corpses[i][0] == 0)
  77. {
  78. time(&longtime);
  79. Corpses[i][0] = dwGUID;
  80. Corpses[i][1] = longtime + dwCleanupDelay;
  81. if(dwReSpawn == 0)
  82. {
  83. Corpses[i][2] = 0;
  84. }
  85. else
  86. {
  87. Corpses[i][2] = longtime + dwReSpawn + dwCleanupDelay;
  88. }
  89. i = MAX_CORPSES + 1;
  90. fAdded = true;
  91. }
  92. }
  93. return fAdded;
  94. }
  95. /**
  96. * Handles the removal of a corpse from the CorpseCleaner.
  97. *
  98. * This function should be called whenever a corpse is removed.
  99. * @param dwGUID - The GUID of the corpse to remove
  100. */
  101. bool CorpseCleaner::RemoveCorpse(DWORD dwGUID)
  102. {
  103. bool fRemoved = false;
  104. for(int i = 0; i < MAX_CORPSES; i++)
  105. {
  106. if(Corpses[i][0] == dwGUID)
  107. {
  108. Corpses[i][0] = 0;
  109. Corpses[i][1] = 0;
  110. Corpses[i][2] = 0;
  111. i = MAX_CORPSES + 1;
  112. fRemoved = true;
  113. }
  114. }
  115. return fRemoved;
  116. }
  117. /**
  118. * Handles the removal of a corpse from the landscape.
  119. *
  120. * This function should be called periodically to check for corpse removal.
  121. * If a corpse is to be removed, the corpse is removed from the given landblock list and from the CorpseCleaner list
  122. */
  123. void CorpseCleaner::Cleanup()
  124. {
  125. for(int i = 0; i < MAX_CORPSES; i++)
  126. {
  127. time(&longtime);
  128. if( ( Corpses[i][0] != 0 ) && ( longtime > Corpses[i][1] ) && ( Corpses[i][1] != 0 ) )
  129. {
  130. cObject* pcObject = cWorldManager::FindObject( Corpses[i][0] );
  131. if(pcObject)
  132. {
  133. // Remove the Corpse from the landscape
  134. cMessage cmRemModel;
  135. cmRemModel << 0xF747L << Corpses[i][0] << DWORD( 1 );
  136. cWorldManager::SendToAllWithin( 5, pcObject->m_Location, cmRemModel, 3 );
  137. SimpleAI::RemoveMonster(Corpses[i][0]);
  138. if( Corpses[i][2] == 0 )
  139. {
  140. RemoveCorpse( Corpses[i][0] );
  141. // Destory the Object completely
  142. cWorldManager::RemoveObject( pcObject );
  143. }
  144. else
  145. {
  146. // this is a Respawning Object So Do Not Destroy it
  147. // Set Corpse Timer to zero
  148. Corpses[i][1] = 0;
  149. // And Remove from the Landblock
  150. cWorldManager::MoveRemObject( pcObject );
  151. pcObject->m_Location.m_dwLandBlock = 0xE9E9002F;
  152. cWorldManager::MoveAddObject( pcObject );
  153. }
  154. }
  155. else
  156. {
  157. //Nothing to Do Here - Object is not valid
  158. }
  159. }
  160. else
  161. {
  162. }
  163. if( ( Corpses[i][0] != 0 ) && ( longtime > Corpses[i][2] ) && ( Corpses[i][1] == 0 ))
  164. {
  165. // ReSpawn Activation code
  166. cObject* pcObjectb = cWorldManager::FindObject( Corpses[i][0] );
  167. if(pcObjectb)
  168. {
  169. // ReSpawn the Monster
  170. pcObjectb->ReSpawn( pcObjectb );
  171. // Finally Clear the entry from the Corpse Cleaner array
  172. RemoveCorpse( Corpses[i][0] );
  173. }
  174. else
  175. {
  176. //Nothing to Do Here - Object is not valid
  177. RemoveCorpse( Corpses[i][0] );
  178. }
  179. }
  180. else
  181. {
  182. // Do absolutely nothing
  183. }
  184. }
  185. // Set time of Next Cleanup Run
  186. time(&NextCleanTime + DELAY);
  187. }