]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/game/ai/AI_Vagary.cpp
hello world
[icculus/iodoom3.git] / neo / game / ai / AI_Vagary.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 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.
23
24 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.
25
26 ===========================================================================
27 */
28 /***********************************************************************
29
30 game/ai/AI_Vagary.cpp
31
32 Vagary specific AI code
33
34 ***********************************************************************/
35
36 #include "../../idlib/precompiled.h"
37 #pragma hdrstop
38
39 #include "../Game_local.h"
40
41 class idAI_Vagary : public idAI {
42 public:
43         CLASS_PROTOTYPE( idAI_Vagary );
44
45 private:
46         void    Event_ChooseObjectToThrow( const idVec3 &mins, const idVec3 &maxs, float speed, float minDist, float offset );
47         void    Event_ThrowObjectAtEnemy( idEntity *ent, float speed );
48 };
49
50 const idEventDef AI_Vagary_ChooseObjectToThrow( "vagary_ChooseObjectToThrow", "vvfff", 'e' );
51 const idEventDef AI_Vagary_ThrowObjectAtEnemy( "vagary_ThrowObjectAtEnemy", "ef" );
52
53 CLASS_DECLARATION( idAI, idAI_Vagary )
54         EVENT( AI_Vagary_ChooseObjectToThrow,   idAI_Vagary::Event_ChooseObjectToThrow )
55         EVENT( AI_Vagary_ThrowObjectAtEnemy,    idAI_Vagary::Event_ThrowObjectAtEnemy )
56 END_CLASS
57
58 /*
59 ================
60 idAI_Vagary::Event_ChooseObjectToThrow
61 ================
62 */
63 void idAI_Vagary::Event_ChooseObjectToThrow( const idVec3 &mins, const idVec3 &maxs, float speed, float minDist, float offset ) {
64         idEntity *      ent;
65         idEntity *      entityList[ MAX_GENTITIES ];
66         int                     numListedEntities;
67         int                     i, index;
68         float           dist;
69         idVec3          vel;
70         idVec3          offsetVec( 0, 0, offset );
71         idEntity        *enemyEnt = enemy.GetEntity();
72
73         if ( !enemyEnt ) {
74                 idThread::ReturnEntity( NULL );
75         }
76
77         idVec3 enemyEyePos = lastVisibleEnemyPos + lastVisibleEnemyEyeOffset;
78         const idBounds &myBounds = physicsObj.GetAbsBounds();
79         idBounds checkBounds( mins, maxs );
80         checkBounds.TranslateSelf( physicsObj.GetOrigin() );
81         numListedEntities = gameLocal.clip.EntitiesTouchingBounds( checkBounds, -1, entityList, MAX_GENTITIES );
82
83         index = gameLocal.random.RandomInt( numListedEntities );
84         for ( i = 0; i < numListedEntities; i++, index++ ) {
85                 if ( index >= numListedEntities ) {
86                         index = 0;
87                 }
88                 ent = entityList[ index ];
89                 if ( !ent->IsType( idMoveable::Type ) ) {
90                         continue;
91                 }
92
93                 if ( ent->fl.hidden ) {
94                         // don't throw hidden objects
95                         continue;
96                 }
97
98                 idPhysics *entPhys = ent->GetPhysics();
99                 const idVec3 &entOrg = entPhys->GetOrigin();
100                 dist = ( entOrg - enemyEyePos ).LengthFast();
101                 if ( dist < minDist ) {
102                         continue;
103                 }
104
105                 idBounds expandedBounds = myBounds.Expand( entPhys->GetBounds().GetRadius() );
106                 if ( expandedBounds.LineIntersection( entOrg, enemyEyePos ) ) {
107                         // ignore objects that are behind us
108                         continue;
109                 }
110
111                 if ( PredictTrajectory( entPhys->GetOrigin() + offsetVec, enemyEyePos, speed, entPhys->GetGravity(), 
112                         entPhys->GetClipModel(), entPhys->GetClipMask(), MAX_WORLD_SIZE, NULL, enemyEnt, ai_debugTrajectory.GetBool() ? 4000 : 0, vel ) ) {
113                         idThread::ReturnEntity( ent );
114             return;
115                 }
116         }
117
118         idThread::ReturnEntity( NULL );
119 }
120
121 /*
122 ================
123 idAI_Vagary::Event_ThrowObjectAtEnemy
124 ================
125 */
126 void idAI_Vagary::Event_ThrowObjectAtEnemy( idEntity *ent, float speed ) {
127         idVec3          vel;
128         idEntity        *enemyEnt;
129         idPhysics       *entPhys;
130
131         entPhys = ent->GetPhysics();
132         enemyEnt = enemy.GetEntity();
133         if ( !enemyEnt ) {
134                 vel = ( viewAxis[ 0 ] * physicsObj.GetGravityAxis() ) * speed;
135         } else {
136                 PredictTrajectory( entPhys->GetOrigin(), lastVisibleEnemyPos + lastVisibleEnemyEyeOffset, speed, entPhys->GetGravity(), 
137                         entPhys->GetClipModel(), entPhys->GetClipMask(), MAX_WORLD_SIZE, NULL, enemyEnt, ai_debugTrajectory.GetBool() ? 4000 : 0, vel );
138                 vel *= speed;
139         }
140
141         entPhys->SetLinearVelocity( vel );
142
143         if ( ent->IsType( idMoveable::Type ) ) {
144                 idMoveable *ment = static_cast<idMoveable*>( ent );
145                 ment->EnableDamage( true, 2.5f );
146         }
147 }