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 "sys/platform.h"
37 #include "script/Script_Thread.h"
38 
39 #include "gamesys/SysCvar.h"
40 #include "Moveable.h"
41 
42 #include "ai/AI.h"
43 
44 class idAI_Vagary : public idAI {
45 public:
46 	CLASS_PROTOTYPE( idAI_Vagary );
47 
48 private:
49 	void	Event_ChooseObjectToThrow( const idVec3 &mins, const idVec3 &maxs, float speed, float minDist, float offset );
50 	void	Event_ThrowObjectAtEnemy( idEntity *ent, float speed );
51 };
52 
53 const idEventDef AI_Vagary_ChooseObjectToThrow( "vagary_ChooseObjectToThrow", "vvfff", 'e' );
54 const idEventDef AI_Vagary_ThrowObjectAtEnemy( "vagary_ThrowObjectAtEnemy", "ef" );
55 
CLASS_DECLARATION(idAI,idAI_Vagary)56 CLASS_DECLARATION( idAI, idAI_Vagary )
57 	EVENT( AI_Vagary_ChooseObjectToThrow,	idAI_Vagary::Event_ChooseObjectToThrow )
58 	EVENT( AI_Vagary_ThrowObjectAtEnemy,	idAI_Vagary::Event_ThrowObjectAtEnemy )
59 END_CLASS
60 
61 /*
62 ================
63 idAI_Vagary::Event_ChooseObjectToThrow
64 ================
65 */
66 void idAI_Vagary::Event_ChooseObjectToThrow( const idVec3 &mins, const idVec3 &maxs, float speed, float minDist, float offset ) {
67 	idEntity *	ent;
68 	idEntity *	entityList[ MAX_GENTITIES ];
69 	int			numListedEntities;
70 	int			i, index;
71 	float		dist;
72 	idVec3		vel;
73 	idVec3		offsetVec( 0, 0, offset );
74 	idEntity	*enemyEnt = enemy.GetEntity();
75 
76 	if ( !enemyEnt ) {
77 		idThread::ReturnEntity( NULL );
78 	}
79 
80 	idVec3 enemyEyePos = lastVisibleEnemyPos + lastVisibleEnemyEyeOffset;
81 	const idBounds &myBounds = physicsObj.GetAbsBounds();
82 	idBounds checkBounds( mins, maxs );
83 	checkBounds.TranslateSelf( physicsObj.GetOrigin() );
84 	numListedEntities = gameLocal.clip.EntitiesTouchingBounds( checkBounds, -1, entityList, MAX_GENTITIES );
85 
86 	index = gameLocal.random.RandomInt( numListedEntities );
87 	for ( i = 0; i < numListedEntities; i++, index++ ) {
88 		if ( index >= numListedEntities ) {
89 			index = 0;
90 		}
91 		ent = entityList[ index ];
92 		if ( !ent->IsType( idMoveable::Type ) ) {
93 			continue;
94 		}
95 
96 		if ( ent->fl.hidden ) {
97 			// don't throw hidden objects
98 			continue;
99 		}
100 
101 		idPhysics *entPhys = ent->GetPhysics();
102 		const idVec3 &entOrg = entPhys->GetOrigin();
103 		dist = ( entOrg - enemyEyePos ).LengthFast();
104 		if ( dist < minDist ) {
105 			continue;
106 		}
107 
108 		idBounds expandedBounds = myBounds.Expand( entPhys->GetBounds().GetRadius() );
109 		if ( expandedBounds.LineIntersection( entOrg, enemyEyePos ) ) {
110 			// ignore objects that are behind us
111 			continue;
112 		}
113 
114 		if ( PredictTrajectory( entPhys->GetOrigin() + offsetVec, enemyEyePos, speed, entPhys->GetGravity(),
115 			entPhys->GetClipModel(), entPhys->GetClipMask(), MAX_WORLD_SIZE, NULL, enemyEnt, ai_debugTrajectory.GetBool() ? 4000 : 0, vel ) ) {
116 			idThread::ReturnEntity( ent );
117 			return;
118 		}
119 	}
120 
121 	idThread::ReturnEntity( NULL );
122 }
123 
124 /*
125 ================
126 idAI_Vagary::Event_ThrowObjectAtEnemy
127 ================
128 */
Event_ThrowObjectAtEnemy(idEntity * ent,float speed)129 void idAI_Vagary::Event_ThrowObjectAtEnemy( idEntity *ent, float speed ) {
130 	idVec3		vel;
131 	idEntity	*enemyEnt;
132 	idPhysics	*entPhys;
133 
134 	entPhys	= ent->GetPhysics();
135 	enemyEnt = enemy.GetEntity();
136 	if ( !enemyEnt ) {
137 		vel = ( viewAxis[ 0 ] * physicsObj.GetGravityAxis() ) * speed;
138 	} else {
139 		PredictTrajectory( entPhys->GetOrigin(), lastVisibleEnemyPos + lastVisibleEnemyEyeOffset, speed, entPhys->GetGravity(),
140 			entPhys->GetClipModel(), entPhys->GetClipMask(), MAX_WORLD_SIZE, NULL, enemyEnt, ai_debugTrajectory.GetBool() ? 4000 : 0, vel );
141 		vel *= speed;
142 	}
143 
144 	entPhys->SetLinearVelocity( vel );
145 
146 	if ( ent->IsType( idMoveable::Type ) ) {
147 		idMoveable *ment = static_cast<idMoveable*>( ent );
148 		ment->EnableDamage( true, 2.5f );
149 	}
150 }
151