CaveSystem.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: CaveSystem.cpp /////////////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood July 2002
  25. // Desc: System responsible for keeping track of all cave systems on the map
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/GameState.h"
  29. #include "Common/TunnelTracker.h"
  30. #include "Common/Xfer.h"
  31. #include "GameLogic/CaveSystem.h"
  32. CaveSystem *TheCaveSystem = NULL;
  33. CaveSystem::CaveSystem()
  34. {
  35. }
  36. CaveSystem::~CaveSystem()
  37. {
  38. }
  39. void CaveSystem::init()
  40. {
  41. }
  42. void CaveSystem::reset()
  43. {
  44. for( std::vector<TunnelTracker*>::iterator iter = m_tunnelTrackerVector.begin(); iter != m_tunnelTrackerVector.end(); iter++ )
  45. {
  46. TunnelTracker *currentTracker = *iter;
  47. if( currentTracker )// could be NULL, since we don't slide back to fill deleted entries so offsets don't shift
  48. {
  49. currentTracker->deleteInstance();
  50. }
  51. }
  52. m_tunnelTrackerVector.clear();
  53. }
  54. void CaveSystem::update()
  55. {
  56. }
  57. Bool CaveSystem::canSwitchIndexToIndex( Int oldIndex, Int newIndex )
  58. {
  59. // When I grant permission, you need to do it. ie call Unregister and then re-register with the new number
  60. TunnelTracker *oldTracker = NULL;
  61. TunnelTracker *newTracker = NULL;
  62. if( m_tunnelTrackerVector.size() > oldIndex )
  63. {
  64. oldTracker = m_tunnelTrackerVector[oldIndex];
  65. if( oldTracker && oldTracker->getContainCount() > 0 )
  66. return FALSE;// You can't switch a connection if one of the two is non empty
  67. }
  68. if( m_tunnelTrackerVector.size() > newIndex )
  69. {
  70. newTracker = m_tunnelTrackerVector[newIndex];
  71. if( newTracker && newTracker->getContainCount() > 0 )
  72. return FALSE;// You can't switch a connection if one of the two is non empty
  73. }
  74. // Both are either empty or non-existent, so go ahead.
  75. // (Remember non-exist is only a valid case because you are going to do the switch now.)
  76. return TRUE;
  77. }
  78. void CaveSystem::registerNewCave( Int theIndex )
  79. {
  80. Bool needToCreate = FALSE;
  81. if( theIndex >= m_tunnelTrackerVector.size() )
  82. {
  83. // You are new and off the edge, so I will fill NULLs up to you and then make a newTracker at that spot
  84. while( theIndex >= m_tunnelTrackerVector.size() )
  85. m_tunnelTrackerVector.push_back( NULL );
  86. needToCreate = TRUE;
  87. }
  88. else
  89. {
  90. // else you either exist or have existed, so I will either let things be or re-create that slot
  91. if( m_tunnelTrackerVector[theIndex] == NULL )
  92. needToCreate = TRUE;
  93. }
  94. if( needToCreate )// if true, we new theIndex is the index of a NULL to be filled
  95. m_tunnelTrackerVector[theIndex] = newInstance(TunnelTracker);
  96. }
  97. void CaveSystem::unregisterCave( Int theIndex )
  98. {
  99. // Doesn't need to do a thing. ContainModule logic knows how to say goodbye, and a TunnelTracker
  100. // knows how to exist while having no entry points.
  101. theIndex;
  102. }
  103. TunnelTracker *CaveSystem::getTunnelTrackerForCaveIndex( Int theIndex )
  104. {
  105. TunnelTracker *theTracker = NULL;
  106. if( theIndex < m_tunnelTrackerVector.size() )
  107. {
  108. theTracker = m_tunnelTrackerVector[theIndex];
  109. }
  110. DEBUG_ASSERTCRASH( theTracker != NULL, ("No one should be interested in a sub-cave that doesn't exist.") );
  111. return theTracker;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. /** Xfer Method
  115. * Version Info
  116. * 1: Initial version */
  117. // ------------------------------------------------------------------------------------------------
  118. void CaveSystem::xfer( Xfer *xfer )
  119. {
  120. // version
  121. XferVersion currentVersion = 1;
  122. XferVersion version = currentVersion;
  123. xfer->xferVersion( &version, currentVersion );
  124. // tunnel tracker size and data
  125. UnsignedShort count = m_tunnelTrackerVector.size();
  126. xfer->xferUnsignedShort( &count );
  127. TunnelTracker *tracker;
  128. if( xfer->getXferMode() == XFER_SAVE )
  129. {
  130. std::vector< TunnelTracker* >::iterator it;
  131. for( it = m_tunnelTrackerVector.begin(); it != m_tunnelTrackerVector.end(); ++it )
  132. {
  133. // xfer data
  134. tracker = *it;
  135. xfer->xferSnapshot( tracker );
  136. } // end
  137. } // end if, save
  138. else
  139. {
  140. // the list must be empty now
  141. if( m_tunnelTrackerVector.empty() == FALSE )
  142. {
  143. DEBUG_CRASH(( "CaveSystem::xfer - m_tunnelTrackerVector should be empty but is not\n" ));
  144. throw SC_INVALID_DATA;
  145. } // end if
  146. // read each item
  147. for( UnsignedShort i = 0; i < count; ++i )
  148. {
  149. // allocate new tracker
  150. tracker = newInstance( TunnelTracker );
  151. // read data
  152. xfer->xferSnapshot( tracker );
  153. // put in vector
  154. m_tunnelTrackerVector.push_back( tracker );
  155. } // end for, i
  156. } // end else, laod
  157. } // end xfer