AAS_debug.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "AAS_local.h"
  23. #include "../Game_local.h" // for cvars and debug drawing
  24. /*
  25. ============
  26. idAASLocal::DrawCone
  27. ============
  28. */
  29. void idAASLocal::DrawCone( const idVec3 &origin, const idVec3 &dir, float radius, const idVec4 &color ) const {
  30. int i;
  31. idMat3 axis;
  32. idVec3 center, top, p, lastp;
  33. axis[2] = dir;
  34. axis[2].NormalVectors( axis[0], axis[1] );
  35. axis[1] = -axis[1];
  36. center = origin + dir;
  37. top = center + dir * (3.0f * radius);
  38. lastp = center + radius * axis[1];
  39. for ( i = 20; i <= 360; i += 20 ) {
  40. p = center + sin( DEG2RAD(i) ) * radius * axis[0] + cos( DEG2RAD(i) ) * radius * axis[1];
  41. gameRenderWorld->DebugLine( color, lastp, p, 0 );
  42. gameRenderWorld->DebugLine( color, p, top, 0 );
  43. lastp = p;
  44. }
  45. }
  46. /*
  47. ============
  48. idAASLocal::DrawReachability
  49. ============
  50. */
  51. void idAASLocal::DrawReachability( const idReachability *reach ) const {
  52. gameRenderWorld->DebugArrow( colorCyan, reach->start, reach->end, 2 );
  53. if ( gameLocal.GetLocalPlayer() ) {
  54. gameRenderWorld->DrawText( va( "%d", reach->edgeNum ), ( reach->start + reach->end ) * 0.5f, 0.1f, colorWhite, gameLocal.GetLocalPlayer()->viewAxis );
  55. }
  56. switch( reach->travelType ) {
  57. case TFL_WALK: {
  58. const idReachability_Walk *walk = static_cast<const idReachability_Walk *>(reach);
  59. break;
  60. }
  61. default: {
  62. break;
  63. }
  64. }
  65. }
  66. /*
  67. ============
  68. idAASLocal::DrawEdge
  69. ============
  70. */
  71. void idAASLocal::DrawEdge( int edgeNum, bool arrow ) const {
  72. const aasEdge_t *edge;
  73. idVec4 *color;
  74. if ( !file ) {
  75. return;
  76. }
  77. edge = &file->GetEdge( edgeNum );
  78. color = &colorRed;
  79. if ( arrow ) {
  80. gameRenderWorld->DebugArrow( *color, file->GetVertex( edge->vertexNum[0] ), file->GetVertex( edge->vertexNum[1] ), 1 );
  81. } else {
  82. gameRenderWorld->DebugLine( *color, file->GetVertex( edge->vertexNum[0] ), file->GetVertex( edge->vertexNum[1] ) );
  83. }
  84. if ( gameLocal.GetLocalPlayer() ) {
  85. gameRenderWorld->DrawText( va( "%d", edgeNum ), ( file->GetVertex( edge->vertexNum[0] ) + file->GetVertex( edge->vertexNum[1] ) ) * 0.5f + idVec3(0,0,4), 0.1f, colorRed, gameLocal.GetLocalPlayer()->viewAxis );
  86. }
  87. }
  88. /*
  89. ============
  90. idAASLocal::DrawFace
  91. ============
  92. */
  93. void idAASLocal::DrawFace( int faceNum, bool side ) const {
  94. int i, j, numEdges, firstEdge;
  95. const aasFace_t *face;
  96. idVec3 mid, end;
  97. if ( !file ) {
  98. return;
  99. }
  100. face = &file->GetFace( faceNum );
  101. numEdges = face->numEdges;
  102. firstEdge = face->firstEdge;
  103. mid = vec3_origin;
  104. for ( i = 0; i < numEdges; i++ ) {
  105. DrawEdge( abs( file->GetEdgeIndex( firstEdge + i ) ), ( face->flags & FACE_FLOOR ) != 0 );
  106. j = file->GetEdgeIndex( firstEdge + i );
  107. mid += file->GetVertex( file->GetEdge( abs( j ) ).vertexNum[ j < 0 ] );
  108. }
  109. mid /= numEdges;
  110. if ( side ) {
  111. end = mid - 5.0f * file->GetPlane( file->GetFace( faceNum ).planeNum ).Normal();
  112. } else {
  113. end = mid + 5.0f * file->GetPlane( file->GetFace( faceNum ).planeNum ).Normal();
  114. }
  115. gameRenderWorld->DebugArrow( colorGreen, mid, end, 1 );
  116. }
  117. /*
  118. ============
  119. idAASLocal::DrawArea
  120. ============
  121. */
  122. void idAASLocal::DrawArea( int areaNum ) const {
  123. int i, numFaces, firstFace;
  124. const aasArea_t *area;
  125. idReachability *reach;
  126. if ( !file ) {
  127. return;
  128. }
  129. area = &file->GetArea( areaNum );
  130. numFaces = area->numFaces;
  131. firstFace = area->firstFace;
  132. for ( i = 0; i < numFaces; i++ ) {
  133. DrawFace( abs( file->GetFaceIndex( firstFace + i ) ), file->GetFaceIndex( firstFace + i ) < 0 );
  134. }
  135. for ( reach = area->reach; reach; reach = reach->next ) {
  136. DrawReachability( reach );
  137. }
  138. }
  139. /*
  140. ============
  141. idAASLocal::DefaultSearchBounds
  142. ============
  143. */
  144. const idBounds &idAASLocal::DefaultSearchBounds( void ) const {
  145. return file->GetSettings().boundingBoxes[0];
  146. }
  147. /*
  148. ============
  149. idAASLocal::ShowArea
  150. ============
  151. */
  152. void idAASLocal::ShowArea( const idVec3 &origin ) const {
  153. static int lastAreaNum;
  154. int areaNum;
  155. const aasArea_t *area;
  156. idVec3 org;
  157. areaNum = PointReachableAreaNum( origin, DefaultSearchBounds(), (AREA_REACHABLE_WALK|AREA_REACHABLE_FLY) );
  158. org = origin;
  159. PushPointIntoAreaNum( areaNum, org );
  160. if ( aas_goalArea.GetInteger() ) {
  161. int travelTime;
  162. idReachability *reach;
  163. RouteToGoalArea( areaNum, org, aas_goalArea.GetInteger(), TFL_WALK|TFL_AIR, travelTime, &reach );
  164. gameLocal.Printf( "\rtt = %4d", travelTime );
  165. if ( reach ) {
  166. gameLocal.Printf( " to area %4d", reach->toAreaNum );
  167. DrawArea( reach->toAreaNum );
  168. }
  169. }
  170. if ( areaNum != lastAreaNum ) {
  171. area = &file->GetArea( areaNum );
  172. gameLocal.Printf( "area %d: ", areaNum );
  173. if ( area->flags & AREA_LEDGE ) {
  174. gameLocal.Printf( "AREA_LEDGE " );
  175. }
  176. if ( area->flags & AREA_REACHABLE_WALK ) {
  177. gameLocal.Printf( "AREA_REACHABLE_WALK " );
  178. }
  179. if ( area->flags & AREA_REACHABLE_FLY ) {
  180. gameLocal.Printf( "AREA_REACHABLE_FLY " );
  181. }
  182. if ( area->contents & AREACONTENTS_CLUSTERPORTAL ) {
  183. gameLocal.Printf( "AREACONTENTS_CLUSTERPORTAL " );
  184. }
  185. if ( area->contents & AREACONTENTS_OBSTACLE ) {
  186. gameLocal.Printf( "AREACONTENTS_OBSTACLE " );
  187. }
  188. gameLocal.Printf( "\n" );
  189. lastAreaNum = areaNum;
  190. }
  191. if ( org != origin ) {
  192. idBounds bnds = file->GetSettings().boundingBoxes[ 0 ];
  193. bnds[ 1 ].z = bnds[ 0 ].z;
  194. gameRenderWorld->DebugBounds( colorYellow, bnds, org );
  195. }
  196. DrawArea( areaNum );
  197. }
  198. /*
  199. ============
  200. idAASLocal::ShowWalkPath
  201. ============
  202. */
  203. void idAASLocal::ShowWalkPath( const idVec3 &origin, int goalAreaNum, const idVec3 &goalOrigin ) const {
  204. int i, areaNum, curAreaNum, travelTime;
  205. idReachability *reach;
  206. idVec3 org, areaCenter;
  207. aasPath_t path;
  208. if ( !file ) {
  209. return;
  210. }
  211. org = origin;
  212. areaNum = PointReachableAreaNum( org, DefaultSearchBounds(), AREA_REACHABLE_WALK );
  213. PushPointIntoAreaNum( areaNum, org );
  214. curAreaNum = areaNum;
  215. for ( i = 0; i < 100; i++ ) {
  216. if ( !RouteToGoalArea( curAreaNum, org, goalAreaNum, TFL_WALK|TFL_AIR, travelTime, &reach ) ) {
  217. break;
  218. }
  219. if ( !reach ) {
  220. break;
  221. }
  222. gameRenderWorld->DebugArrow( colorGreen, org, reach->start, 2 );
  223. DrawReachability( reach );
  224. if ( reach->toAreaNum == goalAreaNum ) {
  225. break;
  226. }
  227. curAreaNum = reach->toAreaNum;
  228. org = reach->end;
  229. }
  230. if ( WalkPathToGoal( path, areaNum, origin, goalAreaNum, goalOrigin, TFL_WALK|TFL_AIR ) ) {
  231. gameRenderWorld->DebugArrow( colorBlue, origin, path.moveGoal, 2 );
  232. }
  233. }
  234. /*
  235. ============
  236. idAASLocal::ShowFlyPath
  237. ============
  238. */
  239. void idAASLocal::ShowFlyPath( const idVec3 &origin, int goalAreaNum, const idVec3 &goalOrigin ) const {
  240. int i, areaNum, curAreaNum, travelTime;
  241. idReachability *reach;
  242. idVec3 org, areaCenter;
  243. aasPath_t path;
  244. if ( !file ) {
  245. return;
  246. }
  247. org = origin;
  248. areaNum = PointReachableAreaNum( org, DefaultSearchBounds(), AREA_REACHABLE_FLY );
  249. PushPointIntoAreaNum( areaNum, org );
  250. curAreaNum = areaNum;
  251. for ( i = 0; i < 100; i++ ) {
  252. if ( !RouteToGoalArea( curAreaNum, org, goalAreaNum, TFL_WALK|TFL_FLY|TFL_AIR, travelTime, &reach ) ) {
  253. break;
  254. }
  255. if ( !reach ) {
  256. break;
  257. }
  258. gameRenderWorld->DebugArrow( colorPurple, org, reach->start, 2 );
  259. DrawReachability( reach );
  260. if ( reach->toAreaNum == goalAreaNum ) {
  261. break;
  262. }
  263. curAreaNum = reach->toAreaNum;
  264. org = reach->end;
  265. }
  266. if ( FlyPathToGoal( path, areaNum, origin, goalAreaNum, goalOrigin, TFL_WALK|TFL_FLY|TFL_AIR ) ) {
  267. gameRenderWorld->DebugArrow( colorBlue, origin, path.moveGoal, 2 );
  268. }
  269. }
  270. /*
  271. ============
  272. idAASLocal::ShowWallEdges
  273. ============
  274. */
  275. void idAASLocal::ShowWallEdges( const idVec3 &origin ) const {
  276. int i, areaNum, numEdges, edges[1024];
  277. idVec3 start, end;
  278. idPlayer *player;
  279. player = gameLocal.GetLocalPlayer();
  280. if ( !player ) {
  281. return;
  282. }
  283. areaNum = PointReachableAreaNum( origin, DefaultSearchBounds(), (AREA_REACHABLE_WALK|AREA_REACHABLE_FLY) );
  284. numEdges = GetWallEdges( areaNum, idBounds( origin ).Expand( 256.0f ), TFL_WALK, edges, 1024 );
  285. for ( i = 0; i < numEdges; i++ ) {
  286. GetEdge( edges[i], start, end );
  287. gameRenderWorld->DebugLine( colorRed, start, end );
  288. gameRenderWorld->DrawText( va( "%d", edges[i] ), ( start + end ) * 0.5f, 0.1f, colorWhite, player->viewAxis );
  289. }
  290. }
  291. /*
  292. ============
  293. idAASLocal::ShowHideArea
  294. ============
  295. */
  296. void idAASLocal::ShowHideArea( const idVec3 &origin, int targetAreaNum ) const {
  297. int areaNum, numObstacles;
  298. idVec3 target;
  299. aasGoal_t goal;
  300. aasObstacle_t obstacles[10];
  301. areaNum = PointReachableAreaNum( origin, DefaultSearchBounds(), (AREA_REACHABLE_WALK|AREA_REACHABLE_FLY) );
  302. target = AreaCenter( targetAreaNum );
  303. // consider the target an obstacle
  304. obstacles[0].absBounds = idBounds( target ).Expand( 16 );
  305. numObstacles = 1;
  306. DrawCone( target, idVec3(0,0,1), 16.0f, colorYellow );
  307. idAASFindCover findCover( target );
  308. if ( FindNearestGoal( goal, areaNum, origin, target, TFL_WALK|TFL_AIR, obstacles, numObstacles, findCover ) ) {
  309. DrawArea( goal.areaNum );
  310. ShowWalkPath( origin, goal.areaNum, goal.origin );
  311. DrawCone( goal.origin, idVec3(0,0,1), 16.0f, colorWhite );
  312. }
  313. }
  314. /*
  315. ============
  316. idAASLocal::PullPlayer
  317. ============
  318. */
  319. bool idAASLocal::PullPlayer( const idVec3 &origin, int toAreaNum ) const {
  320. int areaNum;
  321. idVec3 areaCenter, dir, vel;
  322. idAngles delta;
  323. aasPath_t path;
  324. idPlayer *player;
  325. player = gameLocal.GetLocalPlayer();
  326. if ( !player ) {
  327. return true;
  328. }
  329. idPhysics *physics = player->GetPhysics();
  330. if ( !physics ) {
  331. return true;
  332. }
  333. if ( !toAreaNum ) {
  334. return false;
  335. }
  336. areaNum = PointReachableAreaNum( origin, DefaultSearchBounds(), (AREA_REACHABLE_WALK|AREA_REACHABLE_FLY) );
  337. areaCenter = AreaCenter( toAreaNum );
  338. if ( player->GetPhysics()->GetAbsBounds().Expand( 8 ).ContainsPoint( areaCenter ) ) {
  339. return false;
  340. }
  341. if ( WalkPathToGoal( path, areaNum, origin, toAreaNum, areaCenter, TFL_WALK|TFL_AIR ) ) {
  342. dir = path.moveGoal - origin;
  343. dir[2] *= 0.5f;
  344. dir.Normalize();
  345. delta = dir.ToAngles() - player->cmdAngles - player->GetDeltaViewAngles();
  346. delta.Normalize180();
  347. player->SetDeltaViewAngles( player->GetDeltaViewAngles() + delta * 0.1f );
  348. dir[2] = 0.0f;
  349. dir.Normalize();
  350. dir *= 100.0f;
  351. vel = physics->GetLinearVelocity();
  352. dir[2] = vel[2];
  353. physics->SetLinearVelocity( dir );
  354. return true;
  355. }
  356. else {
  357. return false;
  358. }
  359. }
  360. /*
  361. ============
  362. idAASLocal::RandomPullPlayer
  363. ============
  364. */
  365. void idAASLocal::RandomPullPlayer( const idVec3 &origin ) const {
  366. int rnd, i, n;
  367. if ( !PullPlayer( origin, aas_pullPlayer.GetInteger() ) ) {
  368. rnd = gameLocal.random.RandomFloat() * file->GetNumAreas();
  369. for ( i = 0; i < file->GetNumAreas(); i++ ) {
  370. n = (rnd + i) % file->GetNumAreas();
  371. if ( file->GetArea( n ).flags & (AREA_REACHABLE_WALK|AREA_REACHABLE_FLY) ) {
  372. aas_pullPlayer.SetInteger( n );
  373. }
  374. }
  375. } else {
  376. ShowWalkPath( origin, aas_pullPlayer.GetInteger(), AreaCenter( aas_pullPlayer.GetInteger() ) );
  377. }
  378. }
  379. /*
  380. ============
  381. idAASLocal::ShowPushIntoArea
  382. ============
  383. */
  384. void idAASLocal::ShowPushIntoArea( const idVec3 &origin ) const {
  385. int areaNum;
  386. idVec3 target;
  387. target = origin;
  388. areaNum = PointReachableAreaNum( target, DefaultSearchBounds(), (AREA_REACHABLE_WALK|AREA_REACHABLE_FLY) );
  389. PushPointIntoAreaNum( areaNum, target );
  390. gameRenderWorld->DebugArrow( colorGreen, origin, target, 1 );
  391. }
  392. /*
  393. ============
  394. idAASLocal::Test
  395. ============
  396. */
  397. void idAASLocal::Test( const idVec3 &origin ) {
  398. if ( !file ) {
  399. return;
  400. }
  401. if ( aas_randomPullPlayer.GetBool() ) {
  402. RandomPullPlayer( origin );
  403. }
  404. if ( ( aas_pullPlayer.GetInteger() > 0 ) && ( aas_pullPlayer.GetInteger() < file->GetNumAreas() ) ) {
  405. ShowWalkPath( origin, aas_pullPlayer.GetInteger(), AreaCenter( aas_pullPlayer.GetInteger() ) );
  406. PullPlayer( origin, aas_pullPlayer.GetInteger() );
  407. }
  408. if ( ( aas_showPath.GetInteger() > 0 ) && ( aas_showPath.GetInteger() < file->GetNumAreas() ) ) {
  409. ShowWalkPath( origin, aas_showPath.GetInteger(), AreaCenter( aas_showPath.GetInteger() ) );
  410. }
  411. if ( ( aas_showFlyPath.GetInteger() > 0 ) && ( aas_showFlyPath.GetInteger() < file->GetNumAreas() ) ) {
  412. ShowFlyPath( origin, aas_showFlyPath.GetInteger(), AreaCenter( aas_showFlyPath.GetInteger() ) );
  413. }
  414. if ( ( aas_showHideArea.GetInteger() > 0 ) && ( aas_showHideArea.GetInteger() < file->GetNumAreas() ) ) {
  415. ShowHideArea( origin, aas_showHideArea.GetInteger() );
  416. }
  417. if ( aas_showAreas.GetBool() ) {
  418. ShowArea( origin );
  419. }
  420. if ( aas_showWallEdges.GetBool() ) {
  421. ShowWallEdges( origin );
  422. }
  423. if ( aas_showPushIntoArea.GetBool() ) {
  424. ShowPushIntoArea( origin );
  425. }
  426. }