home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / ai / AAS.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  9KB  |  463 lines

  1. #include "../../idlib/precompiled.h"
  2. #pragma hdrstop
  3.  
  4. // RAVEN BEGIN
  5. #include "../Game_local.h"
  6. // RAVEN END
  7. #include "AAS_local.h"
  8.  
  9. /*
  10. ============
  11. idAAS::Alloc
  12. ============
  13. */
  14. idAAS *idAAS::Alloc( void ) {
  15. // RAVEN BEGIN
  16. // jnewquist: Tag scope and callees to track allocations using "new".
  17.     MEM_SCOPED_TAG(tag,MA_AAS);
  18. // RAVEN END
  19.     return new idAASLocal;
  20. }
  21.  
  22. /*
  23. ============
  24. idAAS::idAAS
  25. ============
  26. */
  27. idAAS::~idAAS( void ) {
  28. }
  29.  
  30. /*
  31. ============
  32. idAASLocal::idAASLocal
  33. ============
  34. */
  35. idAASLocal::idAASLocal( void ) {
  36.     file = NULL;
  37. }
  38.  
  39. /*
  40. ============
  41. idAASLocal::~idAASLocal
  42. ============
  43. */
  44. idAASLocal::~idAASLocal( void ) {
  45.     Shutdown();
  46. }
  47.  
  48. /*
  49. ============
  50. idAASLocal::Init
  51. ============
  52. */
  53. bool idAASLocal::Init( const idStr &mapName, unsigned int mapFileCRC ) {
  54.     if ( file && mapName.Icmp( file->GetName() ) == 0 && mapFileCRC == file->GetCRC() ) {
  55.         gameLocal.Printf( "Keeping %s\n", file->GetName() );
  56.         RemoveAllObstacles();
  57.     }
  58.     else {
  59.         Shutdown();
  60.  
  61.         file = AASFileManager->LoadAAS( mapName, mapFileCRC );
  62.         if ( !file ) {
  63.             common->DWarning( "Couldn't load AAS file: '%s'", mapName.c_str() );
  64.             return false;
  65.         }
  66. // RAVEN BEGIN
  67. // rhummer: Check if this is a dummy file, since it really has no valid data dump it.
  68.         else if ( file->IsDummyFile( mapFileCRC ) ) {
  69.             AASFileManager->FreeAAS( file );
  70.             file = NULL;
  71.             return false;
  72.         }
  73. // RAVEN END
  74.         SetupRouting();
  75.     }
  76.     return true;
  77. }
  78.  
  79. /*
  80. ============
  81. idAASLocal::Shutdown
  82. ============
  83. */
  84. void idAASLocal::Shutdown( void ) {
  85.     if ( file ) {
  86.         ShutdownRouting();
  87.         RemoveAllObstacles();
  88.         AASFileManager->FreeAAS( file );
  89.         file = NULL;
  90.     }
  91. }
  92.  
  93. /*
  94. ============
  95. idAASLocal::Stats
  96. ============
  97. */
  98. void idAASLocal::Stats( void ) const {
  99.     if ( !file ) {
  100.         return;
  101.     }
  102.     common->Printf( "[%s]\n", file->GetName() );
  103.     file->PrintInfo();
  104.     RoutingStats();
  105. }
  106.  
  107. // RAVEN BEGIN
  108. // jscott: added
  109. /*
  110. ============
  111. idAASLocal::StatsSummary
  112. ============
  113. */
  114. size_t idAASLocal::StatsSummary( void ) const {
  115.  
  116.     int        size;
  117.  
  118.     if( !file ) {
  119.  
  120.         return( 0 );
  121.     }
  122.  
  123.     size = ( numAreaTravelTimes * sizeof( unsigned short ) ) 
  124.             + ( areaCacheIndexSize * sizeof( idRoutingCache * ) ) 
  125.             + ( portalCacheIndexSize * sizeof( idRoutingCache * ) );
  126.  
  127.     return( file->GetMemorySize() + size );
  128. }
  129. // RAVEN END
  130.  
  131. /*
  132. ============
  133. idAASLocal::GetSettings
  134. ============
  135. */
  136. const idAASSettings *idAASLocal::GetSettings( void ) const {
  137.     if ( !file ) {
  138.         return NULL;
  139.     }
  140.     return &file->GetSettings();
  141. }
  142.  
  143. /*
  144. ============
  145. idAASLocal::PointAreaNum
  146. ============
  147. */
  148. int idAASLocal::PointAreaNum( const idVec3 &origin ) const {
  149.     if ( !file ) {
  150.         return 0;
  151.     }
  152.     return file->PointAreaNum( origin );
  153. }
  154.  
  155. /*
  156. ============
  157. idAASLocal::PointReachableAreaNum
  158. ============
  159. */
  160. int idAASLocal::PointReachableAreaNum( const idVec3 &origin, const idBounds &searchBounds, const int areaFlags ) const {
  161.     if ( !file ) {
  162.         return 0;
  163.     }
  164.  
  165.     return file->PointReachableAreaNum( origin, searchBounds, areaFlags, TFL_INVALID );
  166. }
  167.  
  168. /*
  169. ============
  170. idAASLocal::BoundsReachableAreaNum
  171. ============
  172. */
  173. int idAASLocal::BoundsReachableAreaNum( const idBounds &bounds, const int areaFlags ) const {
  174.     if ( !file ) {
  175.         return 0;
  176.     }
  177.     
  178.     return file->BoundsReachableAreaNum( bounds, areaFlags, TFL_INVALID );
  179. }
  180.  
  181. /*
  182. ============
  183. idAASLocal::PushPointIntoAreaNum
  184. ============
  185. */
  186. void idAASLocal::PushPointIntoAreaNum( int areaNum, idVec3 &origin ) const {
  187.     if ( !file ) {
  188.         return;
  189.     }
  190.     file->PushPointIntoAreaNum( areaNum, origin );
  191. }
  192.  
  193. /*
  194. ============
  195. idAASLocal::AreaCenter
  196. ============
  197. */
  198. idVec3 idAASLocal::AreaCenter( int areaNum ) const {
  199.     if ( !file ) {
  200.         return vec3_origin;
  201.     }
  202.     return file->GetArea( areaNum ).center;
  203. }
  204.  
  205. // RAVEN BEGIN
  206. // bdube: added
  207. /*
  208. ============
  209. idAASLocal::AreaRadius
  210. ============
  211. */
  212. float idAASLocal::AreaRadius( int areaNum ) const {
  213.     if ( !file ) {
  214.         return 0;
  215.     }
  216.     return file->GetArea( areaNum ).bounds.GetRadius();
  217. }
  218. // mcg: added
  219. /*
  220. ============
  221. idAASLocal::AreaBounds
  222. ============
  223. */
  224. idBounds & idAASLocal::AreaBounds( int areaNum ) const {
  225.     return file->GetArea( areaNum ).bounds;
  226. }
  227. /*
  228. ============
  229. idAASLocal::AreaCeiling
  230. ============
  231. */
  232. float idAASLocal::AreaCeiling( int areaNum ) const {
  233.     return file->GetArea( areaNum ).ceiling;
  234. }
  235. // RAVEN END
  236.  
  237. /*
  238. ============
  239. idAASLocal::AreaFlags
  240. ============
  241. */
  242. int idAASLocal::AreaFlags( int areaNum ) const {
  243.     if ( !file ) {
  244.         return 0;
  245.     }
  246.     return file->GetArea( areaNum ).flags;
  247. }
  248.  
  249. /*
  250. ============
  251. idAASLocal::AreaTravelFlags
  252. ============
  253. */
  254. int idAASLocal::AreaTravelFlags( int areaNum ) const {
  255.     if ( !file ) {
  256.         return 0;
  257.     }
  258.     return file->GetArea( areaNum ).travelFlags;
  259. }
  260.  
  261. /*
  262. ============
  263. idAASLocal::Trace
  264. ============
  265. */
  266. bool idAASLocal::Trace( aasTrace_t &trace, const idVec3 &start, const idVec3 &end ) const {
  267.     if ( !file ) {
  268.         trace.fraction = 0.0f;
  269.         trace.lastAreaNum = 0;
  270.         trace.numAreas = 0;
  271.         return true;
  272.     }
  273.     return file->Trace( trace, start, end );
  274. }
  275.  
  276. /*
  277. ============
  278. idAASLocal::GetPlane
  279. ============
  280. */
  281. const idPlane &idAASLocal::GetPlane( int planeNum ) const {
  282.     if ( !file ) {
  283.         static idPlane dummy;
  284.         return dummy;
  285.     }
  286.     return file->GetPlane( planeNum );
  287. }
  288.  
  289. /*
  290. ============
  291. idAASLocal::GetEdgeVertexNumbers
  292. ============
  293. */
  294. void idAASLocal::GetEdgeVertexNumbers( int edgeNum, int verts[2] ) const {
  295.     if ( !file ) {
  296.         verts[0] = verts[1] = 0;
  297.         return;
  298.     }
  299.     const int *v = file->GetEdge( abs(edgeNum) ).vertexNum;
  300.     verts[0] = v[INTSIGNBITSET(edgeNum)];
  301.     verts[1] = v[INTSIGNBITNOTSET(edgeNum)];
  302. }
  303.  
  304. /*
  305. ============
  306. idAASLocal::GetEdge
  307. ============
  308. */
  309. void idAASLocal::GetEdge( int edgeNum, idVec3 &start, idVec3 &end ) const {
  310.     if ( !file ) {
  311.         start.Zero();
  312.         end.Zero();
  313.         return;
  314.     }
  315.     const int *v = file->GetEdge( abs(edgeNum) ).vertexNum;
  316.     start = file->GetVertex( v[INTSIGNBITSET(edgeNum)] );
  317.     end = file->GetVertex( v[INTSIGNBITNOTSET(edgeNum)] );
  318. }
  319.  
  320.  
  321. /*
  322. ===============================================================================
  323.  
  324.     idAASCallback
  325.  
  326. ===============================================================================
  327. */
  328.  
  329. /*
  330. ============
  331. idAASCallback::~idAASCallback
  332. ============
  333. */
  334. idAASCallback::~idAASCallback ( void ) {
  335. }
  336.  
  337. /*
  338. ============
  339. idAASCallback::Test
  340. ============
  341. */
  342. idAASCallback::testResult_t idAASCallback::Test ( class idAAS *aas, int areaNum, const idVec3& origin, float minDistance, float maxDistance, const idVec3* point, aasGoal_t& goal ) {
  343.     // Get AAS file
  344.     idAASFile* file = ((idAAS&)*aas).GetFile ( );
  345.     if ( !file ) {
  346.         return TEST_BADAREA;
  347.     }
  348.     
  349.     // Get area for edges
  350.     aasArea_t& area = file->GetArea ( areaNum );
  351.  
  352.     if ( ai_debugTactical.GetInteger ( ) > 1 ) {
  353.         gameRenderWorld->DebugLine ( colorYellow, area.center, area.center + idVec3(0,0,80.0f), 10000 );
  354.     }
  355.     
  356.     // Make sure the area itself is valid
  357.     if ( !TestArea ( aas, areaNum, area ) ) {
  358.         return TEST_BADAREA;
  359.     }
  360.  
  361.     if ( ai_debugTactical.GetInteger ( ) > 1 && point ) {
  362.         gameRenderWorld->DebugLine ( colorMagenta, *point, *point + idVec3(0,0,64.0f), 10000 );
  363.     }
  364.     
  365.     // Test the original origin first
  366.     if ( point && TestPointDistance ( origin, *point, minDistance, maxDistance) && TestPoint ( aas, *point ) ) {
  367.         goal.areaNum = areaNum;
  368.         goal.origin  = *point;
  369.         return TEST_OK;
  370.     }
  371.  
  372.     if ( ai_debugTactical.GetInteger ( ) > 1 ) {
  373.         gameRenderWorld->DebugLine ( colorCyan, area.center, area.center + idVec3(0,0,64.0f), 10000 );
  374.     }
  375.     
  376.     // Test the center of the area
  377.     if ( TestPointDistance ( origin, area.center, minDistance, maxDistance) && TestPoint ( aas, area.center, area.ceiling ) ) {
  378.         goal.areaNum = areaNum;
  379.         goal.origin  = area.center;
  380.         return TEST_OK;
  381.     }
  382.     
  383.     // For each face test all available edges
  384.     int    f;
  385.     int    e;
  386.     for ( f = 0; f < area.numFaces; f ++ ) {
  387.         aasFace_t& face = file->GetFace ( abs ( file->GetFaceIndex (area.firstFace + f ) ) );
  388.         
  389.         // for each edge test a point between the center of the edge and the center
  390.         for ( e = 0; e < face.numEdges; e ++ ) {
  391.             idVec3 edgeCenter = file->EdgeCenter ( abs( file->GetEdgeIndex( face.firstEdge + e ) ) );     
  392.             idVec3 dir        = area.center - edgeCenter;
  393.             float  dist;
  394.             for ( dist = dir.Normalize() - 64.0f; dist > 0.0f; dist -= 64.0f ) {                
  395.                 idVec3 testPoint = edgeCenter + dir * dist;
  396.                 if ( ai_debugTactical.GetInteger ( ) > 1 ) {
  397.                     gameRenderWorld->DebugLine ( colorPurple, testPoint, testPoint + idVec3(0,0,64.0f), 10000 );
  398.                 }
  399.  
  400.                 if ( TestPointDistance ( origin, testPoint, minDistance, maxDistance) && TestPoint ( aas, testPoint, area.ceiling ) ) {
  401.                     goal.areaNum = areaNum;
  402.                     goal.origin  = testPoint;
  403.                     return TEST_OK;
  404.                 }
  405.             }
  406.         }
  407.     }
  408.     
  409.     return TEST_BADPOINT;
  410. }
  411.  
  412. /*
  413. ============
  414. idAASCallback::Init
  415. ============
  416. */
  417. bool idAASCallback::TestPointDistance ( const idVec3& origin, const idVec3& point, float minDistance, float maxDistance ) {
  418.     float dist = (origin - point).LengthFast ( );
  419.     if ( minDistance > 0.0f && dist < minDistance ) {
  420.         return false;
  421.     }
  422.     if ( maxDistance > 0.0f && dist > maxDistance ) {
  423.         return false;
  424.     }
  425.     return true;
  426. }
  427.  
  428. /*
  429. ============
  430. idAASCallback::Init
  431. ============
  432. */
  433. void idAASCallback::Init ( void ) {
  434. }
  435.  
  436. /*
  437. ============
  438. idAASCallback::Finish
  439. ============
  440. */
  441. void idAASCallback::Finish ( void ) {
  442. }
  443.  
  444. /*
  445. ============
  446. idAASCallback::TestArea
  447. ============
  448. */
  449. bool idAASCallback::TestArea ( class idAAS *aas, int areaNum, const aasArea_t& area ) {
  450.     return true;
  451. }
  452.  
  453. /*
  454. ============
  455. idAASCallback::TestPoint
  456. ============
  457. */
  458. bool idAASCallback::TestPoint ( class idAAS *aas, const idVec3& pos, const float zAllow ) {
  459.     return true;
  460. }
  461.  
  462. // RAVEN END
  463.