home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / physics / Clip.cpp next >
C/C++ Source or Header  |  2005-11-14  |  56KB  |  2,121 lines

  1.  
  2. #include "../../idlib/precompiled.h"
  3. #pragma hdrstop
  4.  
  5. #include "../Game_local.h"
  6.  
  7. #define    MAX_SECTOR_DEPTH                12
  8. #define MAX_SECTORS                        ((1<<(MAX_SECTOR_DEPTH+1))-1)
  9.  
  10. // RAVEN BEGIN
  11. // ddynerman: SD's clip sector code
  12. typedef struct clipSector_s {
  13.     int                        contents;
  14.     int                        dynamicContents;
  15.     struct clipLink_s *        clipLinks;
  16. } clipSector_t;
  17. // RAVEN END
  18.  
  19. typedef struct clipLink_s {
  20.     idClipModel *            clipModel;
  21.     struct clipSector_s *    sector;
  22.     struct clipLink_s *        prevInSector;
  23.     struct clipLink_s *        nextInSector;
  24.     struct clipLink_s *        nextLink;
  25. } clipLink_t;
  26.  
  27. typedef struct trmCache_s {
  28.     idTraceModel            trm;
  29.     int                        refCount;
  30.     float                    volume;
  31.     idVec3                    centerOfMass;
  32.     idMat3                    inertiaTensor;
  33.     const idMaterial *        material;
  34.     idCollisionModel *        collisionModel;
  35. } trmCache_t;
  36.  
  37. idVec3 vec3_boxEpsilon( CM_BOX_EPSILON, CM_BOX_EPSILON, CM_BOX_EPSILON );
  38.  
  39. // RAVEN BEGIN
  40. // jnewquist: Mark memory tags for idBlockAlloc
  41. idBlockAlloc<clipLink_t, 1024, MA_PHYSICS>    clipLinkAllocator;
  42. // RAVEN END
  43.  
  44. typedef enum {
  45.     CPT_NONE = -1,
  46.     CPT_TRANSLATION,
  47.     CPT_CONTACTS,
  48.     CPT_CONTENTS,
  49.  
  50.     CPT_MAX_TYPES
  51. } CLIP_PROFILE_TYPES;
  52.  
  53. #if 0
  54.  
  55. static idTimer                clipProfileTimer[ CPT_MAX_TYPES ];
  56. static CLIP_PROFILE_TYPES    currentProfileType = CPT_NONE;
  57. static int                    clipProfileDepth = 0;
  58.  
  59. static void BeginClipProfile( CLIP_PROFILE_TYPES type ) {
  60.     clipProfileDepth++;
  61.  
  62.     if ( clipProfileDepth == 1 ) {
  63.         clipProfileTimer[ type ].Start();
  64.         currentProfileType = type;
  65.     }
  66. }
  67.  
  68. static void EndClipProfile( CLIP_PROFILE_TYPES type ) {
  69.     clipProfileDepth--;
  70.  
  71.     if ( clipProfileDepth == 0 ) {
  72.         clipProfileTimer[ currentProfileType ].Stop();
  73.         currentProfileType = CPT_NONE;
  74.     }
  75. }
  76.  
  77. void ClearClipProfile( void ) {
  78.     for( int i = 0; i < CPT_MAX_TYPES; i++ ) {
  79.         clipProfileTimer[ i ].Clear();
  80.     }
  81. }
  82.  
  83. void DisplayClipProfile( void ) {
  84.     for( int i = 0; i < CPT_MAX_TYPES; i++ ) {
  85.         common->Printf( "%d:%d  ", i, ( int )clipProfileTimer[ i ].Milliseconds() );
  86.     }
  87.     common->Printf( "\n" );
  88. }
  89. #else
  90.  
  91. #define BeginClipProfile( type )
  92.  
  93. #define EndClipProfile( type )
  94.  
  95. #endif
  96.  
  97.  
  98. /*
  99. ===============================================================
  100.  
  101.     idClipModel trace model cache
  102.  
  103. ===============================================================
  104. */
  105.  
  106. static idList<trmCache_s*>        traceModelCache;
  107. static idHashIndex                traceModelHash;
  108.     
  109. /*
  110. ===============
  111. idClipModel::ClearTraceModelCache
  112. ===============
  113. */
  114. void idClipModel::ClearTraceModelCache( void ) {
  115.     int i;
  116.  
  117.     for ( i = 0; i < traceModelCache.Num(); i++ ) {
  118.         collisionModelManager->FreeModel( traceModelCache[i]->collisionModel );
  119.         traceModelCache[i]->collisionModel = NULL;
  120.     }
  121.     traceModelCache.DeleteContents( true );
  122.     traceModelHash.Free();
  123. }
  124.  
  125. /*
  126. ===============
  127. idClipModel::CacheCollisionModels
  128. ===============
  129. */
  130. void idClipModel::CacheCollisionModels( void ) {
  131.     int i;
  132.  
  133.     for ( i = 0; i < traceModelCache.Num(); i++ ) {
  134.         if ( traceModelCache[i]->collisionModel == NULL ) {
  135.             traceModelCache[i]->collisionModel = collisionModelManager->ModelFromTrm( gameLocal.GetMapName(), va( "traceModel%d", i ), traceModelCache[i]->trm, traceModelCache[i]->material );
  136.         }
  137.     }
  138. }
  139.  
  140. /*
  141. ===============
  142. idClipModel::TraceModelCacheSize
  143. ===============
  144. */
  145. int idClipModel::TraceModelCacheSize( void ) {
  146.     return traceModelCache.Num() * sizeof( idTraceModel );
  147. }
  148.  
  149. /*
  150. ===============
  151. idClipModel::AllocTraceModel
  152. ===============
  153. */
  154. int idClipModel::AllocTraceModel( const idTraceModel &trm, const idMaterial *material ) {
  155.     int i, hashKey, traceModelIndex;
  156.     trmCache_t *entry;
  157.  
  158.     hashKey = GetTraceModelHashKey( trm );
  159.     for ( i = traceModelHash.First( hashKey ); i >= 0; i = traceModelHash.Next( i ) ) {
  160.         if ( traceModelCache[i]->trm == trm ) {
  161.             traceModelCache[i]->refCount++;
  162.             return i;
  163.         }
  164.     }
  165.  
  166.     entry = new trmCache_t;
  167.     entry->trm = trm;
  168.     entry->trm.GetMassProperties( 1.0f, entry->volume, entry->centerOfMass, entry->inertiaTensor );
  169.     entry->refCount = 1;
  170.     entry->material = material;
  171.     traceModelIndex = traceModelCache.Append( entry );
  172.     traceModelHash.Add( hashKey, traceModelIndex );
  173.  
  174.     entry->collisionModel = collisionModelManager->ModelFromTrm( gameLocal.GetMapName(), va( "traceModel%d", traceModelIndex ), trm, material );
  175.  
  176.     return traceModelIndex;
  177. }
  178.  
  179. /*
  180. ===============
  181. idClipModel::FreeTraceModel
  182. ===============
  183. */
  184. void idClipModel::FreeTraceModel( int traceModelIndex ) {
  185.     if ( traceModelIndex < 0 || traceModelIndex >= traceModelCache.Num() || traceModelCache[traceModelIndex]->refCount <= 0 ) {
  186.         gameLocal.Warning( "idClipModel::FreeTraceModel: tried to free uncached trace model" );
  187.         return;
  188.     }
  189.     traceModelCache[traceModelIndex]->refCount--;
  190. }
  191.  
  192. /*
  193. ===============
  194. idClipModel::CopyTraceModel
  195. ===============
  196. */
  197. int idClipModel::CopyTraceModel( const int traceModelIndex ) {
  198.     if ( traceModelIndex < 0 || traceModelIndex >= traceModelCache.Num() || traceModelCache[traceModelIndex]->refCount <= 0 ) {
  199.         gameLocal.Warning( "idClipModel::CopyTraceModel: tried to copy an uncached trace model" );
  200.         return -1;
  201.     }
  202.     traceModelCache[traceModelIndex]->refCount++;
  203.     return traceModelIndex;
  204. }
  205.  
  206. /*
  207. ===============
  208. idClipModel::GetCachedTraceModel
  209. ===============
  210. */
  211. idTraceModel *idClipModel::GetCachedTraceModel( int traceModelIndex ) {
  212.     return &traceModelCache[traceModelIndex]->trm;
  213. }
  214.  
  215. /*
  216. ===============
  217. idClipModel::GetCachedTraceModel
  218. ===============
  219. */
  220. idCollisionModel *idClipModel::GetCachedCollisionModel( int traceModelIndex ) {
  221.     return traceModelCache[traceModelIndex]->collisionModel;
  222. }
  223.  
  224. /*
  225. ===============
  226. idClipModel::GetTraceModelHashKey
  227. ===============
  228. */
  229. int idClipModel::GetTraceModelHashKey( const idTraceModel &trm ) {
  230.     const idVec3 &v = trm.bounds[0];
  231.     return ( trm.type << 8 ) ^ ( trm.numVerts << 4 ) ^ ( trm.numEdges << 2 ) ^ ( trm.numPolys << 0 ) ^ idMath::FloatHash( v.ToFloatPtr(), v.GetDimension() );
  232. }
  233.  
  234. /*
  235. ===============
  236. idClipModel::SaveTraceModels
  237. ===============
  238. */
  239. void idClipModel::SaveTraceModels( idSaveGame *savefile ) {
  240.     int i;
  241.  
  242.     savefile->WriteInt( traceModelCache.Num() );
  243.     for ( i = 0; i < traceModelCache.Num(); i++ ) {
  244.         trmCache_t *entry = traceModelCache[i];
  245.  
  246.         savefile->Write( &entry->trm, sizeof( entry->trm ) );
  247.         savefile->WriteFloat( entry->volume );
  248.         savefile->WriteVec3( entry->centerOfMass );
  249.         savefile->WriteMat3( entry->inertiaTensor );
  250.         savefile->WriteMaterial( entry->material );
  251.     }
  252. }
  253.  
  254. /*
  255. ===============
  256. idClipModel::RestoreTraceModels
  257. ===============
  258. */
  259. void idClipModel::RestoreTraceModels( idRestoreGame *savefile ) {
  260.     int i, num;
  261.  
  262.     ClearTraceModelCache();
  263.  
  264.     savefile->ReadInt( num );
  265.     traceModelCache.SetNum( num );
  266.     for ( i = 0; i < num; i++ ) {
  267.         trmCache_t *entry = new trmCache_t;
  268.  
  269.         savefile->Read( &entry->trm, sizeof( entry->trm ) );
  270.         savefile->ReadFloat( entry->volume );
  271.         savefile->ReadVec3( entry->centerOfMass );
  272.         savefile->ReadMat3( entry->inertiaTensor );
  273.         savefile->ReadMaterial( entry->material );
  274.  
  275.         entry->refCount = 0;
  276.         entry->collisionModel = NULL;
  277.  
  278.         traceModelCache[i] = entry;
  279.         traceModelHash.Add( GetTraceModelHashKey( entry->trm ), i );
  280.     }
  281.  
  282.     CacheCollisionModels();
  283. }
  284.  
  285.  
  286. /*
  287. ===============================================================
  288.  
  289.     idClipModel
  290.  
  291. ===============================================================
  292. */
  293.  
  294. /*
  295. ================
  296. idClipModel::FreeModel
  297. ================
  298. */
  299. void idClipModel::FreeModel( void ) {
  300.  
  301.     if ( traceModelIndex != -1 ) {
  302.         FreeTraceModel( traceModelIndex );
  303.         traceModelIndex = -1;
  304.     }
  305.  
  306.     if ( collisionModel != NULL ) {
  307.         collisionModelManager->FreeModel( collisionModel );
  308.         collisionModel = NULL;
  309.     }
  310.  
  311.     renderModelHandle = -1;
  312. }
  313.  
  314. // RAVEN BEGIN
  315. // ddynerman: SD's clip sector code
  316. /*
  317. ================
  318. idClipModel::UpdateDynamicContents
  319. ================
  320. */
  321. void idClipModel::UpdateDynamicContents( void ) {
  322.     idClip::UpdateDynamicContents( this );
  323. }
  324. // RAVEN END
  325.  
  326. /*
  327. ================
  328. idClipModel::LoadModel
  329. ================
  330. */
  331. bool idClipModel::LoadModel( const char *name ) {
  332.     FreeModel();
  333.     collisionModel = collisionModelManager->LoadModel( gameLocal.GetMapName(), name );
  334.     if ( collisionModel != NULL ) {
  335.         collisionModel->GetBounds( bounds );
  336.         collisionModel->GetContents( contents );
  337.         return true;
  338.     } else {
  339.         bounds.Zero();
  340.         return false;
  341.     }
  342. }
  343.  
  344. /*
  345. ================
  346. idClipModel::LoadModel
  347. ================
  348. */
  349. void idClipModel::LoadModel( const idTraceModel &trm, const idMaterial *material ) {
  350.     FreeModel();
  351.     traceModelIndex = AllocTraceModel( trm, material );
  352.     bounds = trm.bounds;
  353. }
  354.  
  355. /*
  356. ================
  357. idClipModel::LoadModel
  358. ================
  359. */
  360. void idClipModel::LoadModel( const int renderModelHandle ) {
  361.     FreeModel();
  362.     this->renderModelHandle = renderModelHandle;
  363.     if ( renderModelHandle != -1 ) {
  364.         const renderEntity_t *renderEntity = gameRenderWorld->GetRenderEntity( renderModelHandle );
  365.         if ( renderEntity ) {
  366.             bounds = renderEntity->bounds;
  367.         }
  368.     }
  369. }
  370.  
  371. /*
  372. ================
  373. idClipModel::Init
  374. ================
  375. */
  376. void idClipModel::Init( void ) {
  377.     enabled = true;
  378.     entity = NULL;
  379.     id = 0;
  380.     owner = NULL;
  381.     origin.Zero();
  382.     axis.Identity();
  383.     bounds.Zero();
  384.     absBounds.Zero();
  385.     contents = CONTENTS_BODY;
  386.     collisionModel = NULL;
  387.     renderModelHandle = -1;
  388.     traceModelIndex = -1;
  389.     clipLinks = NULL;
  390.     touchCount = -1;
  391. // RAVEN BEGIN
  392. // ddynerman: SD's clip sector code
  393.     checked = false;
  394. // RAVEN END
  395. }
  396.  
  397. /*
  398. ================
  399. idClipModel::idClipModel
  400. ================
  401. */
  402. idClipModel::idClipModel( void ) {
  403.     Init();
  404. }
  405.  
  406. /*
  407. ================
  408. idClipModel::idClipModel
  409. ================
  410. */
  411. idClipModel::idClipModel( const char *name ) {
  412.     Init();
  413.     LoadModel( name );
  414. }
  415.  
  416. /*
  417. ================
  418. idClipModel::idClipModel
  419. ================
  420. */
  421. idClipModel::idClipModel( const idTraceModel &trm, const idMaterial *material ) {
  422.     Init();
  423.     LoadModel( trm, material );
  424. }
  425.  
  426. /*
  427. ================
  428. idClipModel::idClipModel
  429. ================
  430. */
  431. idClipModel::idClipModel( const int renderModelHandle ) {
  432.     Init();
  433.     contents = CONTENTS_RENDERMODEL;
  434.     LoadModel( renderModelHandle );
  435. }
  436.  
  437. /*
  438. ================
  439. idClipModel::idClipModel
  440. ================
  441. */
  442. idClipModel::idClipModel( const idClipModel *model ) {
  443.     enabled = model->enabled;
  444.     entity = model->entity;
  445.     id = model->id;
  446.     owner = model->owner;
  447.     origin = model->origin;
  448.     axis = model->axis;
  449.     bounds = model->bounds;
  450.     absBounds = model->absBounds;
  451.     contents = model->contents;
  452.     collisionModel = NULL;
  453.     if ( model->collisionModel != NULL ) {
  454.         collisionModel = collisionModelManager->LoadModel( gameLocal.GetMapName(), model->collisionModel->GetName() );
  455.     }
  456.     traceModelIndex = -1;
  457.     if ( model->traceModelIndex != -1 ) {
  458.         traceModelIndex = CopyTraceModel( model->traceModelIndex );
  459.     }
  460.     renderModelHandle = model->renderModelHandle;
  461.     clipLinks = NULL;
  462.     touchCount = -1;
  463. // RAVEN BEGIN
  464. // ddynerman: SD's clip sector code
  465.     checked = false;
  466. // RAVEN END
  467. }
  468.  
  469. /*
  470. ================
  471. idClipModel::~idClipModel
  472. ================
  473. */
  474. idClipModel::~idClipModel( void ) {
  475.     // make sure the clip model is no longer linked
  476.     Unlink();
  477.     FreeModel();
  478. }
  479.  
  480. /*
  481. ================
  482. idClipModel::Save
  483. ================
  484. */
  485. void idClipModel::Save( idSaveGame *savefile ) const {
  486.     savefile->WriteBool( enabled );
  487.     savefile->WriteObject( entity );
  488.     savefile->WriteInt( id );
  489.     savefile->WriteObject( owner );
  490.     savefile->WriteVec3( origin );
  491.     savefile->WriteMat3( axis );
  492.     savefile->WriteBounds( bounds );
  493.     savefile->WriteBounds( absBounds );
  494.     savefile->WriteInt( contents );
  495.     if ( collisionModel != NULL ) {
  496.         savefile->WriteString( collisionModel->GetName() );
  497.     } else {
  498.         savefile->WriteString( "" );
  499.     }
  500.     savefile->WriteInt( traceModelIndex );
  501.     savefile->WriteInt( renderModelHandle );
  502.     savefile->WriteBool( clipLinks != NULL );
  503.     savefile->WriteInt( touchCount );
  504.  
  505.     savefile->WriteBool ( checked );    // cnicholson: Added unsaved var
  506. }
  507.  
  508. /*
  509. ================
  510. idClipModel::Restore
  511. ================
  512. */
  513. void idClipModel::Restore( idRestoreGame *savefile ) {
  514.     idStr collisionModelName;
  515.     bool linked;
  516.  
  517.     savefile->ReadBool( enabled );
  518.     savefile->ReadObject( reinterpret_cast<idClass *&>( entity ) );
  519.     savefile->ReadInt( id );
  520.     savefile->ReadObject( reinterpret_cast<idClass *&>( owner ) );
  521.     savefile->ReadVec3( origin );
  522.     savefile->ReadMat3( axis );
  523.     savefile->ReadBounds( bounds );
  524.     savefile->ReadBounds( absBounds );
  525.     savefile->ReadInt( contents );
  526.     savefile->ReadString( collisionModelName );
  527.     if ( collisionModelName.Length() ) {
  528.         collisionModel = collisionModelManager->LoadModel( gameLocal.GetMapName(), collisionModelName );
  529.     } else {
  530.         collisionModel = NULL;
  531.     }
  532.     savefile->ReadInt( traceModelIndex );
  533.     if ( traceModelIndex >= 0 ) {
  534.         traceModelCache[traceModelIndex]->refCount++;
  535.     }
  536.     savefile->ReadInt( renderModelHandle );
  537.     savefile->ReadBool( linked );
  538.     savefile->ReadInt( touchCount );
  539.  
  540.     savefile->ReadBool ( checked );    // cnicholson: Added unrestored var
  541.  
  542.     // the render model will be set when the clip model is linked
  543.     renderModelHandle = -1;
  544.     clipLinks = NULL;
  545.     touchCount = -1;
  546.  
  547.     if ( linked ) {
  548. // RAVEN BEGIN
  549. // ddynerman: multiple clip worlds
  550.         Link( entity, id, origin, axis, renderModelHandle );
  551. // RAVEN END
  552.     }
  553. }
  554.  
  555. /*
  556. ================
  557. idClipModel::SetPosition
  558. ================
  559. */
  560. void idClipModel::SetPosition( const idVec3 &newOrigin, const idMat3 &newAxis ) {
  561.     if ( clipLinks ) {
  562.         Unlink();    // unlink from old position
  563.     }
  564.     origin = newOrigin;
  565.     axis = newAxis;
  566. }
  567.  
  568. /*
  569. ================
  570. idClipModel::GetCollisionModel
  571. ================
  572. */
  573. idCollisionModel * idClipModel::GetCollisionModel( void ) const {
  574.     assert( renderModelHandle == -1 );
  575.     if ( collisionModel != NULL ) {
  576.         return collisionModel;
  577.     } else if ( traceModelIndex != -1 ) {
  578.         return GetCachedCollisionModel( traceModelIndex );
  579.     } else {
  580.         // this happens in multiplayer on the combat models
  581.         if ( entity ) {
  582.             gameLocal.Warning( "idClipModel::GetCollisionModel: clip model %d on '%s' (%x) is not a collision or trace model", id, entity->name.c_str(), entity->entityNumber );
  583.         }
  584.         return 0;
  585.     }
  586. }
  587.  
  588. /*
  589. ================
  590. idClipModel::GetMassProperties
  591. ================
  592. */
  593. void idClipModel::GetMassProperties( const float density, float &mass, idVec3 ¢erOfMass, idMat3 &inertiaTensor ) const {
  594.     if ( traceModelIndex == -1 ) {
  595.         gameLocal.Error( "idClipModel::GetMassProperties: clip model %d on '%s' is not a trace model\n", id, entity->name.c_str() );
  596.     }
  597.  
  598.     trmCache_t *entry = traceModelCache[traceModelIndex];
  599.     mass = entry->volume * density;
  600.     centerOfMass = entry->centerOfMass;
  601.     inertiaTensor = density * entry->inertiaTensor;
  602. }
  603.  
  604. /*
  605. ===============
  606. idClipModel::Unlink
  607. ===============
  608. */
  609. void idClipModel::Unlink( void ) {
  610.     clipLink_t *link;
  611.  
  612.     for ( link = clipLinks; link; link = clipLinks ) {
  613.         clipLinks = link->nextLink;
  614.         if ( link->prevInSector ) {
  615.             link->prevInSector->nextInSector = link->nextInSector;
  616.         } else {
  617.             link->sector->clipLinks = link->nextInSector;
  618.         }
  619.         if ( link->nextInSector ) {
  620.             link->nextInSector->prevInSector = link->prevInSector;
  621.         }
  622. // RAVEN BEGIN
  623. // ddynerman: SD's clip sector code
  624.         idClip::UpdateDynamicContents( link->sector );
  625. // RAVEN END
  626.  
  627.         clipLinkAllocator.Free( link );
  628.     }
  629. }
  630.  
  631. /*
  632. ===============
  633. idClipModel::Link
  634. ===============
  635. */
  636. // RAVEN BEGIN
  637. // ddynerman: multiple clip worlds
  638. void idClipModel::Link( void ) {
  639.  
  640.     assert( idClipModel::entity );
  641.     if ( !idClipModel::entity ) {
  642.         return;
  643.     }
  644.  
  645.     idClip* clp = gameLocal.GetEntityClipWorld( idClipModel::entity );
  646.  
  647.     if ( clipLinks ) {
  648.         Unlink();    // unlink from old position
  649.     }
  650.  
  651.     if ( bounds.IsCleared() ) {
  652.         return;
  653.     }
  654.  
  655.     // set the abs box
  656.     if ( axis.IsRotated() ) {
  657.         // expand for rotation
  658.         absBounds.FromTransformedBounds( bounds, origin, axis );
  659.     } else {
  660.         // normal
  661.         absBounds[0] = bounds[0] + origin;
  662.         absBounds[1] = bounds[1] + origin;
  663.     }
  664.  
  665.     // because movement is clipped an epsilon away from an actual edge,
  666.     // we must fully check even when bounding boxes don't quite touch
  667.     absBounds[0] -= vec3_boxEpsilon;
  668.     absBounds[1] += vec3_boxEpsilon;
  669. // RAVEN BEGIN
  670. // ddynerman: SD's clip sector code
  671.     int coords[ 4 ];
  672.     clp->CoordsForBounds( coords, absBounds );
  673.  
  674.     int x, y;
  675.     for( x = coords[ 0 ]; x < coords[ 2 ]; x++ ) {
  676.         for( y = coords[ 1 ]; y < coords[ 3 ]; y++ ) {
  677.             clipSector_t* sector = &clp->clipSectors[ x + ( y << CLIPSECTOR_DEPTH ) ];
  678.  
  679.             sector->dynamicContents |= GetContents();
  680.  
  681.             clipLink_t* link = clipLinkAllocator.Alloc();
  682.             link->clipModel = this;
  683.             link->sector = sector;
  684.             link->nextInSector = sector->clipLinks;
  685.             link->prevInSector = NULL;
  686.             if ( sector->clipLinks ) {
  687.                 sector->clipLinks->prevInSector = link;
  688.             }
  689.             sector->clipLinks = link;
  690.             link->nextLink = clipLinks;
  691.             clipLinks = link;
  692.         }
  693.     }
  694. // RAVEN END
  695. }
  696.  
  697. /*
  698. ===============
  699. idClipModel::Link
  700. ===============
  701. */
  702. // RAVEN BEGIN
  703. // ddynerman: multiple clip worlds
  704. void idClipModel::Link( idEntity *ent, int newId, const idVec3 &newOrigin, const idMat3 &newAxis, int renderModelHandle ) {
  705.     this->entity = ent;
  706.     this->id = newId;
  707.     this->origin = newOrigin;
  708.     this->axis = newAxis;
  709.     if ( renderModelHandle != -1 ) {
  710.         this->renderModelHandle = renderModelHandle;
  711.         const renderEntity_t *renderEntity = gameRenderWorld->GetRenderEntity( renderModelHandle );
  712.         if ( renderEntity ) {
  713.             this->bounds = renderEntity->bounds;
  714.         }
  715.     }
  716.     this->Link();
  717. }
  718. // RAVEN END
  719.  
  720.  
  721. /*
  722. ===============================================================
  723.  
  724.     idClip
  725.  
  726. ===============================================================
  727. */
  728.  
  729. // RAVEN BEGIN
  730. // ddynerman: change to static
  731. idClipModel idClip::defaultClipModel;
  732.  
  733. idClipModel *idClip::DefaultClipModel( void ) {
  734.     // initialize a default clip model
  735.     if( defaultClipModel.traceModelIndex == -1 ) {
  736.         defaultClipModel.LoadModel( idTraceModel( idBounds( idVec3( 0, 0, 0 ) ).Expand( 8 ) ), NULL );
  737.     }
  738.  
  739.     return &defaultClipModel;
  740. }
  741.  
  742. void idClip::FreeDefaultClipModel( void ) {
  743.     if ( defaultClipModel.traceModelIndex != -1 ) {
  744.         idClipModel::FreeTraceModel( defaultClipModel.traceModelIndex );
  745.         defaultClipModel.traceModelIndex = -1;
  746.     }
  747. }
  748. // RAVEN END
  749.  
  750. /*
  751. ===============
  752. idClip::idClip
  753. ===============
  754. */
  755. idClip::idClip( void ) {
  756.     clipSectors = NULL;
  757.     world = NULL;
  758.     worldBounds.Zero();
  759.     numRotations = numTranslations = numMotions = numRenderModelTraces = numContents = numContacts = 0;
  760. }
  761.  
  762. /*
  763. ===============
  764. idClip::CreateClipSectors_r
  765.  
  766. Builds a uniformly subdivided tree for the given world size
  767. ===============
  768. */
  769. clipSector_t *idClip::CreateClipSectors_r( const int depth, const idBounds &bounds, idVec3 &maxSector ) {
  770. // RAVEN BEGIN
  771. // ddynerman: SD's clip sector code
  772.     if( clipSectors ) {
  773.         delete[] clipSectors;
  774.         clipSectors = NULL;
  775.     }
  776.     nodeOffsetVisual = bounds[ 0 ];
  777.  
  778.     int i;
  779.     for( i = 0; i < 3; i++ ) {        
  780. //jshepard: this crashes too often
  781. #ifdef _DEBUG
  782.         if( bounds[ 1 ][ i ] - bounds[ 0 ][ i ] )    {
  783.             nodeScale[ i ] = depth / ( bounds[ 1 ][ i ] - bounds[ 0 ][ i ] );
  784.         } else {
  785.             gameLocal.Error("zero size bounds while creating clipsectors");
  786.             nodeScale[ i ] = depth;
  787.         }
  788.         if( nodeScale[ i ] ) {    
  789.             nodeOffset[ i ] = nodeOffsetVisual[ i ] + ( 0.5f / nodeScale[ i ] );
  790.         } else {
  791.             gameLocal.Error("zero size nodeScale while creating clipsectors");
  792.             nodeOffset[ i] = nodeOffset[ i] + 0.5f;
  793.         }
  794. #else 
  795.         nodeScale[ i ] = depth / ( bounds[ 1 ][ i ] - bounds[ 0 ][ i ] );
  796.         nodeOffset[ i ] = nodeOffsetVisual[ i ] + ( 0.5f / nodeScale[ i ] );
  797.  
  798. #endif
  799.     }
  800.  
  801.     clipSectors = new clipSector_t[ Square( depth ) ];
  802.     memset( clipSectors, 0, Square( depth ) * sizeof( clipSector_t ) );
  803.     return clipSectors;
  804. // RAVEN END
  805. }
  806.  
  807. /*
  808. ===============
  809. idClip::Init
  810. ===============
  811. */
  812. void idClip::Init( void ) {
  813.     idVec3 size, maxSector = vec3_origin;
  814.  
  815.     // get world map bounds
  816.     world = collisionModelManager->LoadModel( gameLocal.GetMapName(), WORLD_MODEL_NAME );
  817.     world->GetBounds( worldBounds );
  818.  
  819.     // create world sectors
  820. // RAVEN BEGIN
  821. // mwhitlock: Dynamic memory consolidation
  822.     RV_PUSH_HEAP_MEM(this);
  823. // ddynerman: SD's clip sector code
  824.     CreateClipSectors_r( CLIPSECTOR_WIDTH, worldBounds, maxSector );
  825.     GetClipSectorsStaticContents();
  826. // mwhitlock: Dynamic memory consolidation
  827.     RV_POP_HEAP();
  828. // RAVEN END
  829.  
  830.     size = worldBounds[1] - worldBounds[0];
  831.     gameLocal.Printf( "map bounds are (%1.1f, %1.1f, %1.1f)\n", size[0], size[1], size[2] );
  832.     gameLocal.Printf( "max clip sector is (%1.1f, %1.1f, %1.1f)\n", maxSector[0], maxSector[1], maxSector[2] );
  833.  
  834.     // set counters to zero
  835.     numRotations = numTranslations = numMotions = numRenderModelTraces = numContents = numContacts = 0;
  836. }
  837.  
  838. /*
  839. ===============
  840. idClip::Shutdown
  841. ===============
  842. */
  843. void idClip::Shutdown( void ) {
  844.     delete[] clipSectors;
  845.     clipSectors = NULL;
  846.  
  847.     // free the trace model used for the temporaryClipModel
  848.     if ( temporaryClipModel.traceModelIndex != -1 ) {
  849.         idClipModel::FreeTraceModel( temporaryClipModel.traceModelIndex );
  850.         temporaryClipModel.traceModelIndex = -1;
  851.     }
  852.  
  853.     clipLinkAllocator.Shutdown();
  854. }
  855.  
  856. /*
  857. ====================
  858. idClip::ClipModelsTouchingBounds_r
  859. ====================
  860. */
  861. typedef struct listParms_s {
  862.     idBounds        bounds;
  863.     int                contentMask;
  864.     idClipModel    **    list;
  865.     int                count;
  866.     int                maxCount;
  867. } listParms_t;
  868.  
  869.  
  870. /*
  871. ================
  872. idClip::ClipModelsTouchingBounds
  873. ================
  874. */
  875. int idClip::ClipModelsTouchingBounds( const idBounds &bounds, int contentMask, idClipModel **clipModelList, int maxCount ) const {
  876.     listParms_t parms;
  877.  
  878. // RAVEN BEGIN
  879. // ddynerman: SD's clip sector code
  880.     int clipCount = 0;
  881.     static idClipModel* clipModels[ MAX_GENTITIES ];
  882.  
  883.     assert( maxCount <= MAX_GENTITIES );
  884.     if( maxCount > MAX_GENTITIES ) {
  885.         maxCount = MAX_GENTITIES;
  886.     }
  887. // RAVEN END
  888.  
  889.     if (    bounds[0][0] > bounds[1][0] ||
  890.             bounds[0][1] > bounds[1][1] ||
  891.             bounds[0][2] > bounds[1][2] ) {
  892.         // we should not go through the tree for degenerate or backwards bounds
  893.         assert( false );
  894.         return 0;
  895.     }
  896.  
  897.     parms.bounds[0] = bounds[0] - vec3_boxEpsilon;
  898.     parms.bounds[1] = bounds[1] + vec3_boxEpsilon;
  899.     parms.contentMask = contentMask;
  900.     parms.list = clipModelList;
  901.     parms.count = 0;
  902.     parms.maxCount = maxCount;
  903.  
  904. // RAVEN BEGIN
  905. // ddynerman: SD's clip sector code
  906.     int coords[ 4 ];
  907.     CoordsForBounds( coords, parms.bounds );
  908.  
  909.     int x, y;
  910.     for( x = coords[ 0 ]; x < coords[ 2 ]; x++ ) {
  911.         for( y = coords[ 1 ]; y < coords[ 3 ]; y++ ) {
  912.             clipSector_t* sector = &clipSectors[ x + ( y << CLIPSECTOR_DEPTH ) ];
  913.  
  914.             if( !( sector->dynamicContents & contentMask ) ) {
  915.                 continue;
  916.             }
  917.  
  918.             for ( clipLink_t* link = sector->clipLinks; link && clipCount < MAX_GENTITIES; link = link->nextInSector ) {
  919.                 idClipModel* model = link->clipModel;
  920.  
  921.                 if( model->checked || !model->enabled || !( model->GetContents() & contentMask ) ) {
  922.                     continue;
  923.                 }
  924.  
  925.                 model->checked = true;
  926.                 clipModels[ clipCount++ ] = model;
  927.             }
  928.         }
  929.     }
  930.  
  931.     for( x = 0; x < clipCount; x++ ) {
  932.         clipModels[ x ]->checked = false;
  933.     }
  934.  
  935.     for( x = 0; x < clipCount; x++ ) {
  936.         idClipModel* model = clipModels[ x ];
  937.         
  938.         // if the bounds really do overlap
  939.         if (    model->absBounds[0].x > parms.bounds[1].x ||
  940.                 model->absBounds[1].x < parms.bounds[0].x ||
  941.                 model->absBounds[0].y > parms.bounds[1].y ||
  942.                 model->absBounds[1].y < parms.bounds[0].y ||
  943.                 model->absBounds[0].z > parms.bounds[1].z ||
  944.                 model->absBounds[1].z < parms.bounds[0].z ) {
  945.             continue;
  946.         }
  947.  
  948.         if( parms.count >= parms.maxCount ) {
  949. //            gameLocal.Warning( "idClip::ClipModelsTouchingBounds Max Count Hit\n" );
  950.             break;
  951.         }
  952.  
  953.         parms.list[ parms.count++ ] = model;
  954.     }
  955. // RAVEN END
  956.  
  957.     return parms.count;
  958. }
  959.  
  960. /*
  961. ================
  962. idClip::EntitiesTouchingBounds
  963. ================
  964. */
  965. int idClip::EntitiesTouchingBounds( const idBounds &bounds, int contentMask, idEntity **entityList, int maxCount ) const {
  966.     idClipModel *clipModelList[MAX_GENTITIES];
  967.     int i, j, count, entCount;
  968.  
  969.     count = idClip::ClipModelsTouchingBounds( bounds, contentMask, clipModelList, MAX_GENTITIES );
  970.     entCount = 0;
  971.     for ( i = 0; i < count; i++ ) {
  972.         // entity could already be in the list because an entity can use multiple clip models
  973.         for ( j = 0; j < entCount; j++ ) {
  974.             if ( entityList[j] == clipModelList[i]->entity ) {
  975.                 break;
  976.             }
  977.         }
  978.         if ( j >= entCount ) {
  979.             if ( entCount >= maxCount ) {
  980.                 gameLocal.Warning( "idClip::EntitiesTouchingBounds: max count" );
  981.                 return entCount;
  982.             }
  983.             entityList[entCount] = clipModelList[i]->entity;
  984.             entCount++;
  985.         }
  986.     }
  987.  
  988.     return entCount;
  989. }
  990.  
  991. // RAVEN BEGIN
  992. // ddynerman: MP helper function
  993. /*
  994. ================
  995. idClip::PlayersTouchingBounds
  996. ================
  997. */
  998. int idClip::PlayersTouchingBounds( const idBounds &bounds, int contentMask, idPlayer **playerList, int maxCount ) const {
  999.     idClipModel *clipModelList[MAX_GENTITIES];
  1000.     int i, j, count, playerCount;
  1001.  
  1002.     count = idClip::ClipModelsTouchingBounds( bounds, contentMask, clipModelList, MAX_GENTITIES );
  1003.     playerCount = 0;
  1004.     for ( i = 0; i < count; i++ ) {
  1005.         // entity could already be in the list because an entity can use multiple clip models
  1006.         for ( j = 0; j < playerCount; j++ ) {
  1007.             if ( playerList[j] == clipModelList[i]->entity ) {
  1008.                 break;
  1009.             }
  1010.         }
  1011.         if ( j >= playerCount ) {
  1012.             if ( playerCount >= maxCount ) {
  1013.                 gameLocal.Warning( "idClip::EntitiesTouchingBounds: max count" );
  1014.                 return playerCount;
  1015.             }
  1016. // RAVEN BEGIN
  1017. // jnewquist: Use accessor for static class type 
  1018.             if ( clipModelList[i]->entity->IsType( idPlayer::GetClassType() ) ) {
  1019. // RAVEN END
  1020.                 playerList[playerCount] = static_cast<idPlayer*>(clipModelList[i]->entity);
  1021.                 playerCount++;
  1022.             }
  1023.         }
  1024.     }
  1025.  
  1026.     return playerCount;
  1027. }
  1028. // RAVEN END
  1029.  
  1030. // RAVEN BEGIN
  1031. // ddynerman: SD's clip sector code
  1032.  
  1033. /*
  1034. ====================
  1035. idClip::DrawAreaClipSectors
  1036. ====================
  1037. */
  1038. void idClip::DrawAreaClipSectors( float range ) const {
  1039.     idClipModel* clipModels[ MAX_GENTITIES ];
  1040.  
  1041.     idPlayer* player = gameLocal.GetLocalPlayer();
  1042.     if ( !player ) {
  1043.         return;
  1044.     }
  1045.  
  1046.     idBounds bounds;
  1047.     bounds[0] = player->GetPhysics()->GetOrigin() - idVec3( range, range, range );
  1048.     bounds[1] = player->GetPhysics()->GetOrigin() + idVec3( range, range, range );
  1049.  
  1050.     int count = ClipModelsTouchingBounds( bounds, MASK_ALL, clipModels, MAX_GENTITIES );
  1051.  
  1052.     int i;
  1053.     for ( i = 0; i < count; i++ ) {
  1054.         idEntity* owner = clipModels[ i ]->GetEntity();
  1055.  
  1056.         const idVec3& org = clipModels[ i ]->GetOrigin();
  1057.         const idBounds& bounds = clipModels[ i ]->GetBounds();
  1058.  
  1059.         gameRenderWorld->DebugBounds( colorCyan, bounds, org );
  1060.         gameRenderWorld->DrawText( owner->GetClassname(), org, 0.5f, colorCyan, player->viewAngles.ToMat3(), 1 );
  1061.     }
  1062. }
  1063.  
  1064. /*
  1065. ====================
  1066. idClip::DrawClipSectors
  1067. ====================
  1068. */
  1069. void idClip::DrawClipSectors( void ) const {
  1070.     idBounds bounds;
  1071.  
  1072.     idPlayer* player = gameLocal.GetLocalPlayer();
  1073.     if ( !player ) {
  1074.         return;
  1075.     }
  1076.  
  1077.     int i;
  1078.     idVec3 inverseNodeScale;
  1079.     for( i = 0; i < 3; i++ ) {
  1080.         inverseNodeScale[ i ] = 1 / nodeScale[ i ];
  1081.     }
  1082.  
  1083.     const char* filter = g_showClipSectorFilter.GetString();
  1084.     idTypeInfo* type = idClass::GetClass( filter );
  1085.  
  1086.     int x;
  1087.     for( x = 0; x < CLIPSECTOR_WIDTH; x++ ) {
  1088.         int y;
  1089.         for( y = 0; y < CLIPSECTOR_WIDTH; y++ ) {
  1090. //            idWinding w( 4 );
  1091.  
  1092.             bounds[ 0 ].x = ( inverseNodeScale.x * x ) + nodeOffsetVisual.x + 1;
  1093.             bounds[ 0 ].y = ( inverseNodeScale.y * y ) + nodeOffsetVisual.y + 1;
  1094.             bounds[ 0 ].z = player->GetPhysics()->GetBounds()[0].z;
  1095.  
  1096.             bounds[ 1 ].x = ( inverseNodeScale.x * ( x + 1 ) ) + nodeOffsetVisual.x - 1;
  1097.             bounds[ 1 ].y = ( inverseNodeScale.y * ( y + 1 ) ) + nodeOffsetVisual.y - 1;
  1098.             bounds[ 1 ].z = player->GetPhysics()->GetBounds()[1].z;
  1099.  
  1100.             idVec3 point;
  1101.             point.x = ( bounds[ 0 ].x + bounds[ 1 ].x ) * 0.5f;
  1102.             point.y = ( bounds[ 0 ].y + bounds[ 1 ].y ) * 0.5f;
  1103.             point.z = 0.f;
  1104.  
  1105. /*            point.x = bounds[ 0 ].x;
  1106.             point.y = bounds[ 0 ].y;
  1107.             w.AddPoint( point );
  1108.  
  1109.             point.x = bounds[ 1 ].x;
  1110.             point.y = bounds[ 0 ].y;
  1111.             w.AddPoint( point );
  1112.  
  1113.             point.x = bounds[ 1 ].x;
  1114.             point.y = bounds[ 1 ].y;
  1115.             w.AddPoint( point );
  1116.  
  1117.             point.x = bounds[ 0 ].x;
  1118.             point.y = bounds[ 1 ].y;
  1119.             w.AddPoint( point );*/
  1120.  
  1121.             clipSector_t* sector = &clipSectors[ x + ( y << CLIPSECTOR_DEPTH ) ];
  1122.  
  1123.             clipLink_t* link = sector->clipLinks;
  1124.             while ( link ) {
  1125.                 if ( type && !link->clipModel->GetEntity()->IsType( *type ) ) {
  1126.                     link = link->nextInSector;
  1127.                 } else {
  1128.                     break;
  1129.                 }
  1130.             }
  1131.  
  1132.             if( link ) {
  1133.                 
  1134.                 gameRenderWorld->DrawText( link->clipModel->GetEntity()->GetClassname(), point, 0.5f, colorCyan, player->viewAngles.ToMat3(), 1 );
  1135.                 gameRenderWorld->DebugBounds( colorMagenta, bounds );
  1136.                 gameRenderWorld->DebugBounds( colorYellow, link->clipModel->GetBounds(), link->clipModel->GetOrigin() );
  1137.  
  1138.             } else {
  1139.  
  1140. //                gameRenderWorld->DrawText( sector->clipLinks->clipModel->GetEntity()->GetClassname(), point, 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  1141.  
  1142.             }
  1143.         }
  1144.     }
  1145. }
  1146.  
  1147. /*
  1148. ====================
  1149. idClip::GetClipSectorsStaticContents
  1150. ====================
  1151. */
  1152. void idClip::GetClipSectorsStaticContents( void ) {
  1153.     idBounds bounds;
  1154.  
  1155.     bounds[ 0 ].x = 0;
  1156.     bounds[ 0 ].y = 0;
  1157.     bounds[ 0 ].z = worldBounds[ 0 ].z;
  1158.  
  1159.     bounds[ 1 ].x = 1 / nodeScale.x;
  1160.     bounds[ 1 ].y = 1 / nodeScale.y;
  1161.     bounds[ 1 ].z = worldBounds[ 1 ].z;
  1162.  
  1163.     idTraceModel* trm = new idTraceModel( bounds );
  1164.  
  1165.     idVec3 org;
  1166.     org.z = 0;
  1167.  
  1168.     int x;
  1169.     for( x = 0; x < CLIPSECTOR_WIDTH; x++ ) {
  1170.         int y;
  1171.         for( y = 0; y < CLIPSECTOR_WIDTH; y++ ) {
  1172.             org.x = ( x / nodeScale.x ) + nodeOffset.x;
  1173.             org.y = ( y / nodeScale.y ) + nodeOffset.y;
  1174.  
  1175.             int contents = collisionModelManager->Contents( org, trm, mat3_identity, -1, world, vec3_origin, mat3_default );
  1176.             clipSectors[ x + ( y << CLIPSECTOR_DEPTH ) ].contents = contents;
  1177.         }
  1178.     }
  1179.     // mwhitlock: Fix leak in SD's code.
  1180.     delete trm;
  1181. }
  1182.  
  1183. // RAVEN END
  1184.  
  1185. /*
  1186. ====================
  1187. idClip::GetTraceClipModels
  1188.  
  1189.   an ent will be excluded from testing if:
  1190.   cm->entity == passEntity ( don't clip against the pass entity )
  1191.   cm->entity == passOwner ( missiles don't clip with owner )
  1192.   cm->owner == passEntity ( don't interact with your own missiles )
  1193.   cm->owner == passOwner ( don't interact with other missiles from same owner )
  1194. ====================
  1195. */
  1196. // RAVEN BEGIN
  1197. // nmckenzie: had to add a second pass entity so we can safely ignore both a guy and his target in some cases
  1198. int idClip::GetTraceClipModels( const idBounds &bounds, int contentMask, const idEntity *passEntity, idClipModel **clipModelList, const idEntity *passEntity2 ) const {
  1199. // RAVEN END
  1200.     int i, num;
  1201.     idClipModel    *cm;
  1202.     idEntity *passOwner;
  1203.  
  1204.     num = ClipModelsTouchingBounds( bounds, contentMask, clipModelList, MAX_GENTITIES );
  1205.  
  1206.     if ( !passEntity ) {
  1207.         return num;
  1208.     }
  1209.  
  1210.     if ( passEntity->GetPhysics()->GetNumClipModels() > 0 ) {
  1211.         passOwner = passEntity->GetPhysics()->GetClipModel()->GetOwner();
  1212.     } else {
  1213.         passOwner = NULL;
  1214.     }
  1215.  
  1216.     for ( i = 0; i < num; i++ ) {
  1217.  
  1218.         cm = clipModelList[i];
  1219.  
  1220.         // check if we should ignore this entity
  1221.         if ( cm->entity == passEntity ) {
  1222.             clipModelList[i] = NULL;            // don't clip against the pass entity
  1223.         }
  1224. // RAVEN BEGIN
  1225. // nmckenzie: we have cases where both a guy and his target need to be ignored by a translation
  1226.         else if ( cm->entity == passEntity2 ){
  1227.             clipModelList[i] = NULL;
  1228. // RAVEN END
  1229.         } else if ( cm->entity == passOwner ) {
  1230.             clipModelList[i] = NULL;            // missiles don't clip with their owner
  1231.         } else if ( cm->owner ) {
  1232.             if ( cm->owner == passEntity ) {
  1233.                 clipModelList[i] = NULL;        // don't clip against own missiles
  1234.             } else if ( cm->owner == passOwner ) {
  1235.                 clipModelList[i] = NULL;        // don't clip against other missiles from same owner
  1236.             }
  1237.         }
  1238.     }
  1239.  
  1240.     return num;
  1241. }
  1242.  
  1243. /*
  1244. ============
  1245. idClip::TraceRenderModel
  1246. ============
  1247. */
  1248. void idClip::TraceRenderModel( trace_t &trace, const idVec3 &start, const idVec3 &end, const float radius, const idMat3 &axis, idClipModel *touch ) const {
  1249.     trace.fraction = 1.0f;
  1250.  
  1251.     // if the trace is passing through the bounds
  1252.     if ( touch->absBounds.Expand( radius ).LineIntersection( start, end ) ) {
  1253.         modelTrace_t modelTrace;
  1254.  
  1255.         // test with exact render model and modify trace_t structure accordingly
  1256.         if ( gameRenderWorld->ModelTrace( modelTrace, touch->renderModelHandle, start, end, radius ) ) {
  1257.             trace.fraction = modelTrace.fraction;
  1258.             trace.endAxis = axis;
  1259.             trace.endpos = modelTrace.point;
  1260.             trace.c.normal = modelTrace.normal;
  1261.             trace.c.dist = modelTrace.point * modelTrace.normal;
  1262.             trace.c.point = modelTrace.point;
  1263.             trace.c.type = CONTACT_TRMVERTEX;
  1264.             trace.c.modelFeature = 0;
  1265.             trace.c.trmFeature = 0;
  1266.             trace.c.contents = modelTrace.material->GetContentFlags();
  1267.             trace.c.material = modelTrace.material;
  1268.  
  1269. // RAVEN BEGIN
  1270. // jscott: for material types
  1271.             trace.c.materialType = modelTrace.materialType;
  1272. // RAVEN END
  1273.  
  1274.             // NOTE: trace.c.id will be the joint number
  1275.             touch->id = JOINT_HANDLE_TO_CLIPMODEL_ID( modelTrace.jointNumber );
  1276.         }
  1277.     }
  1278. }
  1279.  
  1280. /*
  1281. ============
  1282. idClip::TraceModelForClipModel
  1283. ============
  1284. */
  1285. const idTraceModel *idClip::TraceModelForClipModel( const idClipModel *mdl ) const {
  1286.     if ( !mdl ) {
  1287.         return NULL;
  1288.     } else {
  1289.         if ( !mdl->IsTraceModel() ) {
  1290.             if ( mdl->GetEntity() ) {
  1291.                 gameLocal.Error( "TraceModelForClipModel: clip model %d on '%s' is not a trace model\n", mdl->GetId(), mdl->GetEntity()->name.c_str() );
  1292.             } else {
  1293.                 gameLocal.Error( "TraceModelForClipModel: clip model %d is not a trace model\n", mdl->GetId() );
  1294.             }
  1295.         }
  1296.         return idClipModel::GetCachedTraceModel( mdl->traceModelIndex );
  1297.     }
  1298. }
  1299.  
  1300. /*
  1301. ============
  1302. idClip::TestHugeTranslation
  1303. ============
  1304. */
  1305. ID_INLINE bool TestHugeTranslation( trace_t &results, const idClipModel *mdl, const idVec3 &start, const idVec3 &end, const idMat3 &trmAxis ) {
  1306.     if ( mdl != NULL && ( end - start ).LengthSqr() > Square( CM_MAX_TRACE_DIST ) ) {
  1307.         assert( 0 );
  1308.  
  1309.         results.fraction = 0.0f;
  1310.         results.endpos = start;
  1311.         results.endAxis = trmAxis;
  1312.         memset( &results.c, 0, sizeof( results.c ) );
  1313.         results.c.point = start;
  1314.  
  1315.         if ( mdl->GetEntity() ) {
  1316.             gameLocal.Printf( "huge translation for clip model %d on entity %d '%s'\n", mdl->GetId(), mdl->GetEntity()->entityNumber, mdl->GetEntity()->GetName() );
  1317.         } else {
  1318.             gameLocal.Printf( "huge translation for clip model %d\n", mdl->GetId() );
  1319.         }
  1320.         return true;
  1321.     }
  1322.     return false;
  1323. }
  1324.  
  1325. /*
  1326. ============
  1327. idClip::TranslationEntities
  1328. ============
  1329. */
  1330. // RAVEN BEGIN
  1331. // nmckenzie: had to add a second pass entity so we can safely ignore both a guy and his target in some cases
  1332. void idClip::TranslationEntities( trace_t &results, const idVec3 &start, const idVec3 &end,
  1333.                         const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity, const idEntity *passEntity2 ) {
  1334. // RAVEN END
  1335.     int i, num;
  1336.     idClipModel *touch, *clipModelList[MAX_GENTITIES];
  1337.     idBounds traceBounds;
  1338.     float radius;
  1339.     trace_t trace;
  1340.     const idTraceModel *trm;
  1341.  
  1342.     if ( TestHugeTranslation( results, mdl, start, end, trmAxis ) ) {
  1343.         return;
  1344.     }
  1345.  
  1346.     trm = TraceModelForClipModel( mdl );
  1347.  
  1348.     results.fraction = 1.0f;
  1349.     results.endpos = end;
  1350.     results.endAxis = trmAxis;
  1351.  
  1352.     if ( !trm ) {
  1353.         traceBounds.FromPointTranslation( start, end - start );
  1354.         radius = 0.0f;
  1355.     } else {
  1356.         traceBounds.FromBoundsTranslation( trm->bounds, start, trmAxis, end - start );
  1357.         radius = trm->bounds.GetRadius();
  1358.     }
  1359.  
  1360. // RAVEN BEGIN
  1361. // nmckenzie: had to add a second pass entity so we can safely ignore both a guy and his target in some cases
  1362.     num = GetTraceClipModels( traceBounds, contentMask, passEntity, clipModelList, passEntity2 );
  1363. // RAVEN END
  1364.  
  1365.     for ( i = 0; i < num; i++ ) {
  1366.         touch = clipModelList[i];
  1367.  
  1368.         if ( !touch ) {
  1369.             continue;
  1370.         }
  1371.  
  1372.         if ( touch->renderModelHandle != -1 ) {
  1373.             idClip::numRenderModelTraces++;
  1374.             TraceRenderModel( trace, start, end, radius, trmAxis, touch );
  1375.         } else {
  1376.             idClip::numTranslations++;
  1377.             collisionModelManager->Translation( &trace, start, end, trm, trmAxis, contentMask,
  1378.                                     touch->GetCollisionModel(), touch->origin, touch->axis );
  1379.         }
  1380.  
  1381.         if ( trace.fraction < results.fraction ) {
  1382.             results = trace;
  1383.             results.c.entityNum = touch->entity->entityNumber;
  1384.             results.c.id = touch->id;
  1385.             if ( results.fraction == 0.0f ) {
  1386.                 break;
  1387.             }
  1388.         }
  1389.     }
  1390. }
  1391.  
  1392. /*
  1393. ============
  1394. idClip::Translation
  1395. ============
  1396. */
  1397. // RAVEN BEGIN
  1398. // nmckenzie: we have cases where both a guy and his target need to be ignored by a translation
  1399. bool idClip::Translation( trace_t &results, const idVec3 &start, const idVec3 &end,
  1400.                         const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity, const idEntity *passEntity2 ) {
  1401. // RAVEN END
  1402.     BeginClipProfile( CPT_TRANSLATION );
  1403.  
  1404.     int i, num;
  1405.     idClipModel *touch, *clipModelList[MAX_GENTITIES];
  1406.     idBounds traceBounds;
  1407.     float radius;
  1408.     trace_t trace;
  1409.     const idTraceModel *trm;
  1410.  
  1411. // RAVEN BEGIN
  1412. // rjohnson: added debug line drawing for traces
  1413.     if ( g_showCollisionTraces.GetInteger() >= 2 && !g_stopTime.GetBool() ) {
  1414.         gameRenderWorld->DebugLine( colorCyan, start, end, 1000 );
  1415.     }
  1416. // RAVEN END
  1417.  
  1418.     if ( TestHugeTranslation( results, mdl, start, end, trmAxis ) ) {
  1419.         EndClipProfile( CPT_TRANSLATION );
  1420.         return true;
  1421.     }
  1422.  
  1423.     trm = TraceModelForClipModel( mdl );
  1424.  
  1425.     if ( !passEntity || passEntity->entityNumber != ENTITYNUM_WORLD ) {
  1426.         // test world
  1427.         idClip::numTranslations++;
  1428.         collisionModelManager->Translation( &results, start, end, trm, trmAxis, contentMask, world, vec3_origin, mat3_default );
  1429.         results.c.entityNum = results.fraction != 1.0f ? ENTITYNUM_WORLD : ENTITYNUM_NONE;
  1430.         if ( results.fraction == 0.0f ) {
  1431.             EndClipProfile( CPT_TRANSLATION );
  1432.             return true;        // blocked immediately by the world
  1433.         }
  1434.     } else {
  1435.         memset( &results, 0, sizeof( results ) );
  1436.         results.fraction = 1.0f;
  1437.         results.endpos = end;
  1438.         results.endAxis = trmAxis;
  1439.     }
  1440.  
  1441.     if ( !trm ) {
  1442.         traceBounds.FromPointTranslation( start, results.endpos - start );
  1443.         radius = 0.0f;
  1444.     } else {
  1445.         traceBounds.FromBoundsTranslation( trm->bounds, start, trmAxis, results.endpos - start );
  1446.         radius = trm->bounds.GetRadius();
  1447.     }
  1448.  
  1449. // RAVEN BEGIN
  1450. // nmckenzie: we have cases where both a guy and his target need to be ignored by a translation
  1451.     num = GetTraceClipModels( traceBounds, contentMask, passEntity, clipModelList, passEntity2 );
  1452. // RAVEN END
  1453.  
  1454.     for ( i = 0; i < num; i++ ) {
  1455.         touch = clipModelList[i];
  1456.  
  1457.         if ( !touch ) {
  1458.             continue;
  1459.         }
  1460.  
  1461.         if ( touch->renderModelHandle != -1 ) {
  1462.             idClip::numRenderModelTraces++;
  1463.             TraceRenderModel( trace, start, end, radius, trmAxis, touch );
  1464.         } else {
  1465.             idClip::numTranslations++;
  1466.             collisionModelManager->Translation( &trace, start, end, trm, trmAxis, contentMask,
  1467.                                     touch->GetCollisionModel(), touch->origin, touch->axis );
  1468.         }
  1469.  
  1470.         if ( trace.fraction < results.fraction ) {
  1471.             results = trace;
  1472.             results.c.entityNum = touch->entity->entityNumber;
  1473.             results.c.id = touch->id;
  1474.  
  1475. // RAVEN BEGIN
  1476. // jscott: for material types
  1477.             results.c.materialType = trace.c.materialType;
  1478.  
  1479. // mekberg: copy contents
  1480.             if ( touch->IsTraceModel( ) ) {
  1481.                 results.c.contents = touch->GetContents( );
  1482.             }
  1483. // RAVEN END
  1484.  
  1485.             if ( results.fraction == 0.0f ) {
  1486.                 break;
  1487.             }
  1488.         }
  1489.     }
  1490.  
  1491.     EndClipProfile( CPT_TRANSLATION );
  1492.  
  1493.     return ( results.fraction < 1.0f );
  1494. }
  1495.  
  1496. /*
  1497. ============
  1498. idClip::Rotation
  1499. ============
  1500. */
  1501. bool idClip::Rotation( trace_t &results, const idVec3 &start, const idRotation &rotation,
  1502.                     const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity ) {
  1503.     int i, num;
  1504.     idClipModel *touch, *clipModelList[MAX_GENTITIES];
  1505.     idBounds traceBounds;
  1506.     trace_t trace;
  1507.     const idTraceModel *trm;
  1508.  
  1509.     trm = TraceModelForClipModel( mdl );
  1510.  
  1511.     if ( !passEntity || passEntity->entityNumber != ENTITYNUM_WORLD ) {
  1512.         // test world
  1513.         idClip::numRotations++;
  1514.         collisionModelManager->Rotation( &results, start, rotation, trm, trmAxis, contentMask, world, vec3_origin, mat3_default );
  1515.         results.c.entityNum = results.fraction != 1.0f ? ENTITYNUM_WORLD : ENTITYNUM_NONE;
  1516.         if ( results.fraction == 0.0f ) {
  1517.             return true;        // blocked immediately by the world
  1518.         }
  1519.     } else {
  1520.         memset( &results, 0, sizeof( results ) );
  1521.         results.fraction = 1.0f;
  1522.         results.endpos = start;
  1523.         results.endAxis = trmAxis * rotation.ToMat3();
  1524.     }
  1525.  
  1526.     if ( !trm ) {
  1527.         traceBounds.FromPointRotation( start, rotation );
  1528.     } else {
  1529.         traceBounds.FromBoundsRotation( trm->bounds, start, trmAxis, rotation );
  1530.     }
  1531.  
  1532.     num = GetTraceClipModels( traceBounds, contentMask, passEntity, clipModelList );
  1533.  
  1534.     for ( i = 0; i < num; i++ ) {
  1535.         touch = clipModelList[i];
  1536.  
  1537.         if ( !touch ) {
  1538.             continue;
  1539.         }
  1540.  
  1541.         // no rotational collision with render models
  1542.         if ( touch->renderModelHandle != -1 ) {
  1543.             continue;
  1544.         }
  1545.  
  1546.         idClip::numRotations++;
  1547.         collisionModelManager->Rotation( &trace, start, rotation, trm, trmAxis, contentMask,
  1548.                             touch->GetCollisionModel(), touch->origin, touch->axis );
  1549.  
  1550.         if ( trace.fraction < results.fraction ) {
  1551.             results = trace;
  1552.             results.c.entityNum = touch->entity->entityNumber;
  1553.             results.c.id = touch->id;
  1554.             if ( results.fraction == 0.0f ) {
  1555.                 break;
  1556.             }
  1557.         }
  1558.     }
  1559.  
  1560.     return ( results.fraction < 1.0f );
  1561. }
  1562.  
  1563. /*
  1564. ============
  1565. idClip::Motion
  1566. ============
  1567. */
  1568. bool idClip::Motion( trace_t &results, const idVec3 &start, const idVec3 &end, const idRotation &rotation,
  1569.                     const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity ) {
  1570.     int i, num;
  1571.     idClipModel *touch, *clipModelList[MAX_GENTITIES];
  1572.     idVec3 dir, endPosition;
  1573.     idBounds traceBounds;
  1574.     float radius;
  1575.     trace_t translationalTrace, rotationalTrace, trace;
  1576.     idRotation endRotation;
  1577.     const idTraceModel *trm;
  1578.  
  1579.     assert( rotation.GetOrigin() == start );
  1580.  
  1581.     if ( TestHugeTranslation( results, mdl, start, end, trmAxis ) ) {
  1582.         return true;
  1583.     }
  1584.  
  1585.     if ( mdl != NULL && rotation.GetAngle() != 0.0f && rotation.GetVec() != vec3_origin ) {
  1586.         // if no translation
  1587.         if ( start == end ) {
  1588.             // pure rotation
  1589.             return Rotation( results, start, rotation, mdl, trmAxis, contentMask, passEntity );
  1590.         }
  1591.     } else if ( start != end ) {
  1592.         // pure translation
  1593.         return Translation( results, start, end, mdl, trmAxis, contentMask, passEntity );
  1594.     } else {
  1595.         // no motion
  1596.         results.fraction = 1.0f;
  1597.         results.endpos = start;
  1598.         results.endAxis = trmAxis;
  1599.         return false;
  1600.     }
  1601.  
  1602.     trm = TraceModelForClipModel( mdl );
  1603.  
  1604.     radius = trm->bounds.GetRadius();
  1605.  
  1606.     if ( !passEntity || passEntity->entityNumber != ENTITYNUM_WORLD ) {
  1607.         // translational collision with world
  1608.         idClip::numTranslations++;
  1609.         collisionModelManager->Translation( &translationalTrace, start, end, trm, trmAxis, contentMask, world, vec3_origin, mat3_default );
  1610.         translationalTrace.c.entityNum = translationalTrace.fraction != 1.0f ? ENTITYNUM_WORLD : ENTITYNUM_NONE;
  1611.     } else {
  1612.         memset( &translationalTrace, 0, sizeof( translationalTrace ) );
  1613.         translationalTrace.fraction = 1.0f;
  1614.         translationalTrace.endpos = end;
  1615.         translationalTrace.endAxis = trmAxis;
  1616.     }
  1617.  
  1618.     if ( translationalTrace.fraction != 0.0f ) {
  1619.  
  1620.         traceBounds.FromBoundsRotation( trm->bounds, start, trmAxis, rotation );
  1621.         dir = translationalTrace.endpos - start;
  1622.         for ( i = 0; i < 3; i++ ) {
  1623.             if ( dir[i] < 0.0f ) {
  1624.                 traceBounds[0][i] += dir[i];
  1625.             }
  1626.             else {
  1627.                 traceBounds[1][i] += dir[i];
  1628.             }
  1629.         }
  1630.  
  1631.         num = GetTraceClipModels( traceBounds, contentMask, passEntity, clipModelList );
  1632.  
  1633.         for ( i = 0; i < num; i++ ) {
  1634.             touch = clipModelList[i];
  1635.  
  1636.             if ( !touch ) {
  1637.                 continue;
  1638.             }
  1639.  
  1640.             if ( touch->renderModelHandle != -1 ) {
  1641.                 idClip::numRenderModelTraces++;
  1642.                 TraceRenderModel( trace, start, end, radius, trmAxis, touch );
  1643.             } else {
  1644.                 idClip::numTranslations++;
  1645.                 collisionModelManager->Translation( &trace, start, end, trm, trmAxis, contentMask,
  1646.                                         touch->GetCollisionModel(), touch->origin, touch->axis );
  1647.             }
  1648.  
  1649.             if ( trace.fraction < translationalTrace.fraction ) {
  1650.                 translationalTrace = trace;
  1651.                 translationalTrace.c.entityNum = touch->entity->entityNumber;
  1652.                 translationalTrace.c.id = touch->id;
  1653.                 if ( translationalTrace.fraction == 0.0f ) {
  1654.                     break;
  1655.                 }
  1656.             }
  1657.         }
  1658.     } else {
  1659.         num = -1;
  1660.     }
  1661.  
  1662.     endPosition = translationalTrace.endpos;
  1663.     endRotation = rotation;
  1664.     endRotation.SetOrigin( endPosition );
  1665.  
  1666.     if ( !passEntity || passEntity->entityNumber != ENTITYNUM_WORLD ) {
  1667.         // rotational collision with world
  1668.         idClip::numRotations++;
  1669.         collisionModelManager->Rotation( &rotationalTrace, endPosition, endRotation, trm, trmAxis, contentMask, world, vec3_origin, mat3_default );
  1670.         rotationalTrace.c.entityNum = rotationalTrace.fraction != 1.0f ? ENTITYNUM_WORLD : ENTITYNUM_NONE;
  1671.     } else {
  1672.         memset( &rotationalTrace, 0, sizeof( rotationalTrace ) );
  1673.         rotationalTrace.fraction = 1.0f;
  1674.         rotationalTrace.endpos = endPosition;
  1675.         rotationalTrace.endAxis = trmAxis * rotation.ToMat3();
  1676.     }
  1677.  
  1678.     if ( rotationalTrace.fraction != 0.0f ) {
  1679.  
  1680.         if ( num == -1 ) {
  1681.             traceBounds.FromBoundsRotation( trm->bounds, endPosition, trmAxis, endRotation );
  1682.             num = GetTraceClipModels( traceBounds, contentMask, passEntity, clipModelList );
  1683.         }
  1684.  
  1685.         for ( i = 0; i < num; i++ ) {
  1686.             touch = clipModelList[i];
  1687.  
  1688.             if ( !touch ) {
  1689.                 continue;
  1690.             }
  1691.  
  1692.             // no rotational collision detection with render models
  1693.             if ( touch->renderModelHandle != -1 ) {
  1694.                 continue;
  1695.             }
  1696.  
  1697.             idClip::numRotations++;
  1698.             collisionModelManager->Rotation( &trace, endPosition, endRotation, trm, trmAxis, contentMask,
  1699.                                 touch->GetCollisionModel(), touch->origin, touch->axis );
  1700.  
  1701.             if ( trace.fraction < rotationalTrace.fraction ) {
  1702.                 rotationalTrace = trace;
  1703.                 rotationalTrace.c.entityNum = touch->entity->entityNumber;
  1704.                 rotationalTrace.c.id = touch->id;
  1705.                 if ( rotationalTrace.fraction == 0.0f ) {
  1706.                     break;
  1707.                 }
  1708.             }
  1709.         }
  1710.     }
  1711.  
  1712.     if ( rotationalTrace.fraction < 1.0f ) {
  1713.         results = rotationalTrace;
  1714.     } else {
  1715.         results = translationalTrace;
  1716.         results.endAxis = rotationalTrace.endAxis;
  1717.     }
  1718.  
  1719.     results.fraction = Max( translationalTrace.fraction, rotationalTrace.fraction );
  1720.  
  1721.     return ( translationalTrace.fraction < 1.0f || rotationalTrace.fraction < 1.0f );
  1722. }
  1723.  
  1724. /*
  1725. ============
  1726. idClip::Contacts
  1727. ============
  1728. */
  1729. int idClip::Contacts( contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth,
  1730.                      const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity ) {
  1731.     BeginClipProfile( CPT_CONTACTS );
  1732.  
  1733.     int i, j, num, n, numContacts;
  1734.     idClipModel *touch, *clipModelList[MAX_GENTITIES];
  1735.     idBounds traceBounds;
  1736.     const idTraceModel *trm;
  1737.  
  1738.     trm = TraceModelForClipModel( mdl );
  1739.  
  1740.     if ( !passEntity || passEntity->entityNumber != ENTITYNUM_WORLD ) {
  1741.         // test world
  1742.         idClip::numContacts++;
  1743.         numContacts = collisionModelManager->Contacts( contacts, maxContacts, start, dir, depth, trm, trmAxis, contentMask, world, vec3_origin, mat3_default );
  1744.     } else {
  1745.         numContacts = 0;
  1746.     }
  1747.  
  1748.     for ( i = 0; i < numContacts; i++ ) {
  1749.         contacts[i].entityNum = ENTITYNUM_WORLD;
  1750.         contacts[i].id = 0;
  1751.     }
  1752.  
  1753.     if ( numContacts >= maxContacts ) {
  1754.         EndClipProfile( CPT_CONTACTS );
  1755.         return numContacts;
  1756.     }
  1757.  
  1758.     if ( !trm ) {
  1759.         traceBounds = idBounds( start ).Expand( depth );
  1760.     } else {
  1761.         traceBounds.FromTransformedBounds( trm->bounds, start, trmAxis );
  1762.         traceBounds.ExpandSelf( depth );
  1763.     }
  1764.  
  1765.     num = GetTraceClipModels( traceBounds, contentMask, passEntity, clipModelList );
  1766.  
  1767.     for ( i = 0; i < num; i++ ) {
  1768.         touch = clipModelList[i];
  1769.  
  1770.         if ( !touch ) {
  1771.             continue;
  1772.         }
  1773.  
  1774.         // no contacts with render models
  1775.         if ( touch->renderModelHandle != -1 ) {
  1776.             continue;
  1777.         }
  1778.  
  1779.         idClip::numContacts++;
  1780.         n = collisionModelManager->Contacts( contacts + numContacts, maxContacts - numContacts,
  1781.                                 start, dir, depth, trm, trmAxis, contentMask,
  1782.                                     touch->GetCollisionModel(), touch->origin, touch->axis );
  1783.  
  1784.         for ( j = 0; j < n; j++ ) {
  1785.             contacts[numContacts].entityNum = touch->entity->entityNumber;
  1786.             contacts[numContacts].id = touch->id;
  1787.             numContacts++;
  1788.         }
  1789.  
  1790.         if ( numContacts >= maxContacts ) {
  1791.             break;
  1792.         }
  1793.     }
  1794.  
  1795.     EndClipProfile( CPT_CONTACTS );
  1796.  
  1797.     return numContacts;
  1798. }
  1799.  
  1800. /*
  1801. ============
  1802. idClip::Contents
  1803. ============
  1804. */
  1805. // RAVEN BEGIN
  1806. // AReis: Added ability to get the entity that was touched as well.
  1807. int idClip::Contents( const idVec3 &start, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity, idEntity **touchedEntity ) {
  1808. // RAVEN END
  1809.     BeginClipProfile( CPT_CONTENTS );
  1810.  
  1811.     int i, num, contents;
  1812.     idClipModel *touch, *clipModelList[MAX_GENTITIES];
  1813.     idBounds traceBounds;
  1814.     const idTraceModel *trm;
  1815.  
  1816.     trm = TraceModelForClipModel( mdl );
  1817.  
  1818.     if ( !passEntity || passEntity->entityNumber != ENTITYNUM_WORLD ) {
  1819.         // test world
  1820.         idClip::numContents++;
  1821.         contents = collisionModelManager->Contents( start, trm, trmAxis, contentMask, world, vec3_origin, mat3_default );
  1822.     } else {
  1823.         contents = 0;
  1824.     }
  1825.  
  1826.     if ( !trm ) {
  1827.         traceBounds[0] = start;
  1828.         traceBounds[1] = start;
  1829.     } else if ( trmAxis.IsRotated() ) {
  1830.         traceBounds.FromTransformedBounds( trm->bounds, start, trmAxis );
  1831.     } else {
  1832.         traceBounds[0] = trm->bounds[0] + start;
  1833.         traceBounds[1] = trm->bounds[1] + start;
  1834.     }
  1835.  
  1836.     num = GetTraceClipModels( traceBounds, -1, passEntity, clipModelList );
  1837.  
  1838.     for ( i = 0; i < num; i++ ) {
  1839.         touch = clipModelList[i];
  1840.  
  1841.         if ( !touch ) {
  1842.             continue;
  1843.         }
  1844.  
  1845.         // no contents test with render models
  1846.         if ( touch->renderModelHandle != -1 ) {
  1847.             continue;
  1848.         }
  1849.  
  1850.         // if the entity does not have any contents we are looking for
  1851.         if ( ( touch->contents & contentMask ) == 0 ) {
  1852.             continue;
  1853.         }
  1854.  
  1855.         // if the entity has no new contents flags
  1856.         if ( ( touch->contents & contents ) == touch->contents ) {
  1857.             continue;
  1858.         }
  1859.  
  1860.         idClip::numContents++;
  1861.         if ( collisionModelManager->Contents( start, trm, trmAxis, contentMask, touch->GetCollisionModel(), touch->origin, touch->axis ) ) {
  1862.             contents |= ( touch->contents & contentMask );
  1863.         }
  1864.  
  1865. // RAVEN BEGIN
  1866.         // Only sends back one entity for now. Ahh well, if this is a problem, have it send back a list...
  1867.         if ( touchedEntity )
  1868.         {
  1869.             *touchedEntity = touch->GetEntity();
  1870.         }
  1871. // RAVEN END
  1872.     }
  1873.  
  1874.     EndClipProfile( CPT_CONTENTS );
  1875.  
  1876.     return contents;
  1877. }
  1878.  
  1879. /*
  1880. ============
  1881. idClip::TranslationModel
  1882. ============
  1883. */
  1884. void idClip::TranslationModel( trace_t &results, const idVec3 &start, const idVec3 &end,
  1885.                     const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
  1886.                     idCollisionModel *model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) {
  1887.     const idTraceModel *trm = TraceModelForClipModel( mdl );
  1888.     idClip::numTranslations++;
  1889.     collisionModelManager->Translation( &results, start, end, trm, trmAxis, contentMask, model, modelOrigin, modelAxis );
  1890. }
  1891.  
  1892. /*
  1893. ============
  1894. idClip::RotationModel
  1895. ============
  1896. */
  1897. void idClip::RotationModel( trace_t &results, const idVec3 &start, const idRotation &rotation,
  1898.                     const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
  1899.                     idCollisionModel *model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) {
  1900.     const idTraceModel *trm = TraceModelForClipModel( mdl );
  1901.     idClip::numRotations++;
  1902.     collisionModelManager->Rotation( &results, start, rotation, trm, trmAxis, contentMask, model, modelOrigin, modelAxis );
  1903. }
  1904.  
  1905. /*
  1906. ============
  1907. idClip::ContactsModel
  1908. ============
  1909. */
  1910. int idClip::ContactsModel( contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth,
  1911.                     const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
  1912.                     idCollisionModel *model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) {
  1913.     const idTraceModel *trm = TraceModelForClipModel( mdl );
  1914.     idClip::numContacts++;
  1915.     return collisionModelManager->Contacts( contacts, maxContacts, start, dir, depth, trm, trmAxis, contentMask, model, modelOrigin, modelAxis );
  1916. }
  1917.  
  1918. /*
  1919. ============
  1920. idClip::ContentsModel
  1921. ============
  1922. */
  1923. int idClip::ContentsModel( const idVec3 &start,
  1924.                     const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
  1925.                     idCollisionModel *model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) {
  1926.     const idTraceModel *trm = TraceModelForClipModel( mdl );
  1927.     idClip::numContents++;
  1928.     return collisionModelManager->Contents( start, trm, trmAxis, contentMask, model, modelOrigin, modelAxis );
  1929. }
  1930.  
  1931. /*
  1932. ============
  1933. idClip::GetModelContactFeature
  1934. ============
  1935. */
  1936. bool idClip::GetModelContactFeature( const contactInfo_t &contact, const idClipModel *clipModel, idFixedWinding &winding ) const {
  1937.     int i;
  1938.     idCollisionModel *model;
  1939.     idVec3 start, end;
  1940.  
  1941.     model = NULL;
  1942.     winding.Clear();
  1943.  
  1944.     if ( clipModel == NULL ) {
  1945.         model = NULL;
  1946.     } else {
  1947.         if ( clipModel->renderModelHandle != -1 ) {
  1948.             winding += contact.point;
  1949.             return true;
  1950.         } else if ( clipModel->traceModelIndex != -1 ) {
  1951.             model = idClipModel::GetCachedCollisionModel( clipModel->traceModelIndex );
  1952.         } else {
  1953.             model = clipModel->collisionModel;
  1954.         }
  1955.     }
  1956.  
  1957.     // if contact with a collision model
  1958.     if ( model != NULL ) {
  1959.         switch( contact.type ) {
  1960.             case CONTACT_EDGE: {
  1961.                 // the model contact feature is a collision model edge
  1962.                 model->GetEdge( contact.modelFeature, start, end );
  1963.                 winding += start;
  1964.                 winding += end;
  1965.                 break;
  1966.             }
  1967.             case CONTACT_MODELVERTEX: {
  1968.                 // the model contact feature is a collision model vertex
  1969.                 model->GetVertex( contact.modelFeature, start );
  1970.                 winding += start;
  1971.                 break;
  1972.             }
  1973.             case CONTACT_TRMVERTEX: {
  1974.                 // the model contact feature is a collision model polygon
  1975.                 model->GetPolygon( contact.modelFeature, winding );
  1976.                 break;
  1977.             }
  1978.         }
  1979.     }
  1980.  
  1981.     // transform the winding to world space
  1982.     if ( clipModel ) {
  1983.         for ( i = 0; i < winding.GetNumPoints(); i++ ) {
  1984.             winding[i].ToVec3() *= clipModel->axis;
  1985.             winding[i].ToVec3() += clipModel->origin;
  1986.         }
  1987.     }
  1988.  
  1989.     return true;
  1990. }
  1991.  
  1992. /*
  1993. ============
  1994. idClip::PrintStatistics
  1995. ============
  1996. */
  1997. void idClip::PrintStatistics( void ) {
  1998. // RAVEN BEGIN
  1999. // rjohnson: added trace model cache size
  2000.     gameLocal.Printf( "t=%-3d, r=%-3d, m=%-3d, render=%-3d, contents=%-3d, contacts=%-3d, cache=%d\n",
  2001.                         numTranslations, numRotations, numMotions, numRenderModelTraces, numContents, numContacts, traceModelCache.Num() * sizeof( idTraceModel ) );
  2002. // RAVEN END
  2003. }
  2004.  
  2005. /*
  2006. ============
  2007. idClip::DrawClipModels
  2008. ============
  2009. */
  2010. void idClip::DrawClipModels( const idVec3 &eye, const float radius, const idEntity *passEntity, const idTypeInfo* type ) {
  2011.     int                i, num;
  2012.     idBounds        bounds;
  2013.     idClipModel        *clipModelList[MAX_GENTITIES];
  2014.     idClipModel        *clipModel;
  2015.  
  2016.     bounds = idBounds( eye ).Expand( radius );
  2017.  
  2018.     num = idClip::ClipModelsTouchingBounds( bounds, -1, clipModelList, MAX_GENTITIES );
  2019.  
  2020.     for ( i = 0; i < num; i++ ) {
  2021.         clipModel = clipModelList[i];
  2022.         if ( clipModel->GetEntity() == passEntity ) {
  2023.             continue;
  2024.         }
  2025.         if ( type && !clipModel->GetEntity()->IsType( *type ) ) {
  2026.             continue;
  2027.         }
  2028.         if ( clipModel->renderModelHandle != -1 ) {
  2029.             gameRenderWorld->DebugBounds( colorCyan, clipModel->GetAbsBounds() );
  2030.             continue;
  2031.         }
  2032.  
  2033.         idCollisionModel *model = clipModel->GetCollisionModel();
  2034.         if ( model != NULL ) {
  2035.             collisionModelManager->DrawModel( model, clipModel->GetOrigin(), clipModel->GetAxis(), eye, mat3_identity, radius );
  2036.         }
  2037.     }
  2038. }
  2039.  
  2040. /*
  2041. ============
  2042. idClip::DrawModelContactFeature
  2043. ============
  2044. */
  2045. bool idClip::DrawModelContactFeature( const contactInfo_t &contact, const idClipModel *clipModel, int lifetime ) const {
  2046.     int i;
  2047.     idMat3 axis;
  2048.     idFixedWinding winding;
  2049.  
  2050.     if ( !GetModelContactFeature( contact, clipModel, winding ) ) {
  2051.         return false;
  2052.     }
  2053.  
  2054.     axis = contact.normal.ToMat3();
  2055.  
  2056.     if ( winding.GetNumPoints() == 1 ) {
  2057.         gameRenderWorld->DebugLine( colorCyan, winding[0].ToVec3(), winding[0].ToVec3() + 2.0f * axis[0], lifetime );
  2058.         gameRenderWorld->DebugLine( colorWhite, winding[0].ToVec3() - 1.0f * axis[1], winding[0].ToVec3() + 1.0f * axis[1], lifetime );
  2059.         gameRenderWorld->DebugLine( colorWhite, winding[0].ToVec3() - 1.0f * axis[2], winding[0].ToVec3() + 1.0f * axis[2], lifetime );
  2060.     } else {
  2061.         for ( i = 0; i < winding.GetNumPoints(); i++ ) {
  2062.             gameRenderWorld->DebugLine( colorCyan, winding[i].ToVec3(), winding[(i+1)%winding.GetNumPoints()].ToVec3(), lifetime );
  2063.         }
  2064.     }
  2065.  
  2066.     axis[0] = -axis[0];
  2067.     axis[2] = -axis[2];
  2068.     gameRenderWorld->DrawText( contact.material->GetName(), winding.GetCenter() - 4.0f * axis[2], 0.1f, colorWhite, axis, 1, 5000 );
  2069.  
  2070.     return true;
  2071. }
  2072.  
  2073. // RAVEN BEGIN
  2074. // rjohnson: added debug hud support
  2075.  
  2076. void idClip::DebugHudStatistics( void )
  2077. {
  2078.     gameDebug.SetInt( "physics_translations", numTranslations );
  2079.     gameDebug.SetInt( "physics_rotations", numRotations );
  2080.     gameDebug.SetInt( "physics_motions", numMotions );
  2081.     gameDebug.SetInt( "physics_render_model_traces", numRenderModelTraces );
  2082.     gameDebug.SetInt( "physics_contents", numContents );
  2083.     gameDebug.SetInt( "physics_contacts", numContacts );
  2084. }
  2085.  
  2086. void idClip::ClearStatistics( void )
  2087. {
  2088.     numRotations = numTranslations = numMotions = numRenderModelTraces = numContents = numContacts = 0;
  2089. }
  2090.  
  2091. // RAVEN END
  2092.  
  2093. // RAVEN BEGIN
  2094. // ddynerman: SD's clip sector code
  2095. /*
  2096. ============
  2097. idClip::UpdateDynamicContents
  2098. ============
  2099. */
  2100. void idClip::UpdateDynamicContents( clipSector_t* sector ) {
  2101.     sector->dynamicContents = 0;
  2102.  
  2103.     clipLink_t* link;
  2104.     for( link = sector->clipLinks; link; link = link->nextInSector ) {
  2105.         sector->dynamicContents |= link->clipModel->GetContents();
  2106.     }
  2107. }
  2108.  
  2109. /*
  2110. ============
  2111. idClip::UpdateDynamicContents
  2112. ============
  2113. */
  2114. void idClip::UpdateDynamicContents( idClipModel* clipModel ) {
  2115.     clipLink_s* link = clipModel->clipLinks;
  2116.     while ( link ) {
  2117.         idClip::UpdateDynamicContents( link->sector );
  2118.         link = link->nextLink;
  2119.     }
  2120. }
  2121. // RAVEN END