AI_Vagary.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /***********************************************************************
  21. game/ai/AI_Vagary.cpp
  22. Vagary specific AI code
  23. ***********************************************************************/
  24. #include "../../idlib/precompiled.h"
  25. #pragma hdrstop
  26. #include "../Game_local.h"
  27. class idAI_Vagary : public idAI {
  28. public:
  29. CLASS_PROTOTYPE( idAI_Vagary );
  30. private:
  31. void Event_ChooseObjectToThrow( const idVec3 &mins, const idVec3 &maxs, float speed, float minDist, float offset );
  32. void Event_ThrowObjectAtEnemy( idEntity *ent, float speed );
  33. };
  34. const idEventDef AI_Vagary_ChooseObjectToThrow( "vagary_ChooseObjectToThrow", "vvfff", 'e' );
  35. const idEventDef AI_Vagary_ThrowObjectAtEnemy( "vagary_ThrowObjectAtEnemy", "ef" );
  36. CLASS_DECLARATION( idAI, idAI_Vagary )
  37. EVENT( AI_Vagary_ChooseObjectToThrow, idAI_Vagary::Event_ChooseObjectToThrow )
  38. EVENT( AI_Vagary_ThrowObjectAtEnemy, idAI_Vagary::Event_ThrowObjectAtEnemy )
  39. END_CLASS
  40. /*
  41. ================
  42. idAI_Vagary::Event_ChooseObjectToThrow
  43. ================
  44. */
  45. void idAI_Vagary::Event_ChooseObjectToThrow( const idVec3 &mins, const idVec3 &maxs, float speed, float minDist, float offset ) {
  46. idEntity * ent;
  47. idEntity * entityList[ MAX_GENTITIES ];
  48. int numListedEntities;
  49. int i, index;
  50. float dist;
  51. idVec3 vel;
  52. idVec3 offsetVec( 0, 0, offset );
  53. idEntity *enemyEnt = enemy.GetEntity();
  54. if ( !enemyEnt ) {
  55. idThread::ReturnEntity( NULL );
  56. }
  57. idVec3 enemyEyePos = lastVisibleEnemyPos + lastVisibleEnemyEyeOffset;
  58. const idBounds &myBounds = physicsObj.GetAbsBounds();
  59. idBounds checkBounds( mins, maxs );
  60. checkBounds.TranslateSelf( physicsObj.GetOrigin() );
  61. numListedEntities = gameLocal.clip.EntitiesTouchingBounds( checkBounds, -1, entityList, MAX_GENTITIES );
  62. index = gameLocal.random.RandomInt( numListedEntities );
  63. for ( i = 0; i < numListedEntities; i++, index++ ) {
  64. if ( index >= numListedEntities ) {
  65. index = 0;
  66. }
  67. ent = entityList[ index ];
  68. if ( !ent->IsType( idMoveable::Type ) ) {
  69. continue;
  70. }
  71. if ( ent->fl.hidden ) {
  72. // don't throw hidden objects
  73. continue;
  74. }
  75. idPhysics *entPhys = ent->GetPhysics();
  76. const idVec3 &entOrg = entPhys->GetOrigin();
  77. dist = ( entOrg - enemyEyePos ).LengthFast();
  78. if ( dist < minDist ) {
  79. continue;
  80. }
  81. idBounds expandedBounds = myBounds.Expand( entPhys->GetBounds().GetRadius() );
  82. if ( expandedBounds.LineIntersection( entOrg, enemyEyePos ) ) {
  83. // ignore objects that are behind us
  84. continue;
  85. }
  86. if ( PredictTrajectory( entPhys->GetOrigin() + offsetVec, enemyEyePos, speed, entPhys->GetGravity(),
  87. entPhys->GetClipModel(), entPhys->GetClipMask(), MAX_WORLD_SIZE, NULL, enemyEnt, ai_debugTrajectory.GetBool() ? 4000 : 0, vel ) ) {
  88. idThread::ReturnEntity( ent );
  89. return;
  90. }
  91. }
  92. idThread::ReturnEntity( NULL );
  93. }
  94. /*
  95. ================
  96. idAI_Vagary::Event_ThrowObjectAtEnemy
  97. ================
  98. */
  99. void idAI_Vagary::Event_ThrowObjectAtEnemy( idEntity *ent, float speed ) {
  100. idVec3 vel;
  101. idEntity *enemyEnt;
  102. idPhysics *entPhys;
  103. entPhys = ent->GetPhysics();
  104. enemyEnt = enemy.GetEntity();
  105. if ( !enemyEnt ) {
  106. vel = ( viewAxis[ 0 ] * physicsObj.GetGravityAxis() ) * speed;
  107. } else {
  108. PredictTrajectory( entPhys->GetOrigin(), lastVisibleEnemyPos + lastVisibleEnemyEyeOffset, speed, entPhys->GetGravity(),
  109. entPhys->GetClipModel(), entPhys->GetClipMask(), MAX_WORLD_SIZE, NULL, enemyEnt, ai_debugTrajectory.GetBool() ? 4000 : 0, vel );
  110. vel *= speed;
  111. }
  112. entPhys->SetLinearVelocity( vel );
  113. if ( ent->IsType( idMoveable::Type ) ) {
  114. idMoveable *ment = static_cast<idMoveable*>( ent );
  115. ment->EnableDamage( true, 2.5f );
  116. }
  117. }