5050// ------------------------------------------------------------------------
5151TunnelTracker::TunnelTracker ()
5252{
53+ #if RETAIL_COMPATIBLE_CRC
5354 m_tunnelCount = 0 ;
55+ #endif
5456 m_containListSize = 0 ;
5557 m_heroUnitsContained = 0 ;
5658 m_curNemesisID = INVALID_ID ;
@@ -224,21 +226,32 @@ Bool TunnelTracker::isInContainer( Object *obj )
224226// ------------------------------------------------------------------------
225227void TunnelTracker::onTunnelCreated ( const Object *newTunnel )
226228{
229+ DEBUG_ASSERTCRASH (std::find (m_tunnelIDs.begin (), m_tunnelIDs.end (), newTunnel->getID ()) == m_tunnelIDs.end (),
230+ (" TunnelTracker::onTunnelCreated - New tunnel was already added; this shouldn't happen" ));
231+
232+ #if RETAIL_COMPATIBLE_CRC
227233 m_tunnelCount++;
234+ #endif
228235 m_tunnelIDs.push_back ( newTunnel->getID () );
229236 m_needsFullHealTimeUpdate = true ;
230237}
231238
232239// ------------------------------------------------------------------------
233240void TunnelTracker::onTunnelDestroyed ( const Object *deadTunnel )
234241{
242+ #if RETAIL_COMPATIBLE_CRC
243+
235244 DEBUG_ASSERTCRASH (static_cast <Int>(m_tunnelCount) > 0 && m_tunnelCount == m_tunnelIDs.size (),
236245 (" TunnelTracker::onTunnelDestroyed - Tunnel count is unexpected" ));
237- DEBUG_ASSERTCRASH (std::find (m_tunnelIDs.begin (), m_tunnelIDs.end (), deadTunnel->getID ()) != m_tunnelIDs.end (),
246+
247+ const size_t oldSize = m_tunnelIDs.size ();
248+ m_tunnelIDs.remove (deadTunnel->getID ());
249+
250+ static_cast <void >(oldSize);
251+ DEBUG_ASSERTCRASH (oldSize != m_tunnelIDs.size (),
238252 (" TunnelTracker::onTunnelDestroyed - Attempting to remove object '%s' that has never been tracked as a tunnel" , deadTunnel->getName ().str ()));
239253
240254 m_tunnelCount--;
241- m_tunnelIDs.remove ( deadTunnel->getID () );
242255 m_needsFullHealTimeUpdate = true ;
243256
244257 if ( m_tunnelCount == 0 )
@@ -265,6 +278,48 @@ void TunnelTracker::onTunnelDestroyed( const Object *deadTunnel )
265278 obj->onContainedBy ( tunnel );
266279 }
267280 }
281+
282+ #else
283+
284+ const std::list<ObjectID>::iterator it = std::find (m_tunnelIDs.begin (), m_tunnelIDs.end (), deadTunnel->getID ());
285+ if (it == m_tunnelIDs.end ())
286+ {
287+ DEBUG_CRASH ((" TunnelTracker::onTunnelDestroyed - Attempting to remove object '%s' that has never been tracked as a tunnel" ,
288+ deadTunnel->getName ().str ()));
289+ }
290+ else
291+ {
292+ DEBUG_ASSERTCRASH (std::find (std::next (it), m_tunnelIDs.end (), deadTunnel->getID ()) == m_tunnelIDs.end (),
293+ (" TunnelTracker::onTunnelDestroyed - Tunnel should have been tracked only once" ));
294+
295+ m_tunnelIDs.erase (it);
296+ }
297+
298+ m_needsFullHealTimeUpdate = true ;
299+
300+ if ( m_tunnelIDs.empty () )
301+ {
302+ // Kill everyone in our contain list. Cave in!
303+ iterateContained ( destroyObject, nullptr , FALSE );
304+ m_containList.clear ();
305+ m_containListSize = 0 ;
306+ }
307+ else
308+ {
309+ Object *validTunnel = TheGameLogic->findObjectByID ( m_tunnelIDs.front () );
310+
311+ // Otherwise, make sure nobody inside remembers the dead tunnel as the one they entered
312+ // (scripts need to use so there must be something valid here)
313+ for (ContainedItemsList::iterator it = m_containList.begin (); it != m_containList.end (); )
314+ {
315+ Object* obj = *it;
316+ ++it;
317+ if ( obj->getContainedBy () == deadTunnel )
318+ obj->onContainedBy ( validTunnel );
319+ }
320+ }
321+
322+ #endif // RETAIL_COMPATIBLE_CRC
268323}
269324
270325// ------------------------------------------------------------------------
@@ -379,13 +434,19 @@ void TunnelTracker::crc( Xfer *xfer )
379434// ------------------------------------------------------------------------------------------------
380435/* * Xfer method
381436 * Version Info:
382- * 1: Initial version */
437+ * 1: Initial version
438+ * 2: TheSuperHackers @tweak Removed m_tunnelCount (should always be equal to m_tunnelIDs.size())
439+ */
383440// ------------------------------------------------------------------------------------------------
384441void TunnelTracker::xfer ( Xfer *xfer )
385442{
386443
387444 // version
445+ #if RETAIL_COMPATIBLE_XFER_SAVE
388446 XferVersion currentVersion = 1 ;
447+ #else
448+ XferVersion currentVersion = 2 ;
449+ #endif
389450 XferVersion version = currentVersion;
390451 xfer->xferVersion ( &version, currentVersion );
391452
@@ -424,8 +485,22 @@ void TunnelTracker::xfer( Xfer *xfer )
424485 m_needsFullHealTimeUpdate = true ;
425486 }
426487
427- // tunnel count
428- xfer->xferUnsignedInt ( &m_tunnelCount );
488+ #if RETAIL_COMPATIBLE_CRC
489+ if (version <= 1 )
490+ {
491+ xfer->xferUnsignedInt (&m_tunnelCount);
492+ }
493+ else
494+ {
495+ m_tunnelCount = m_tunnelIDs.size ();
496+ }
497+ #else
498+ if (version <= 1 )
499+ {
500+ UnsignedInt tunnelCount = m_tunnelIDs.size ();
501+ xfer->xferUnsignedInt (&tunnelCount);
502+ }
503+ #endif
429504
430505}
431506
0 commit comments