1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or ( at your option )any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * Dominik Solenicki |
---|
26 | * |
---|
27 | */ |
---|
28 | #include "controllers/FightingController.h" |
---|
29 | #include "core/XMLPort.h" |
---|
30 | #include "util/Math.h" |
---|
31 | |
---|
32 | |
---|
33 | namespace orxonox |
---|
34 | { |
---|
35 | |
---|
36 | RegisterClass (FightingController); |
---|
37 | |
---|
38 | FightingController::FightingController( Context* context ): FlyingController( context ) |
---|
39 | { |
---|
40 | this->attackRange_ = 2000; |
---|
41 | this->stopLookingAtTarget(); |
---|
42 | |
---|
43 | RegisterObject( FightingController ); |
---|
44 | } |
---|
45 | FightingController::~FightingController() |
---|
46 | { |
---|
47 | |
---|
48 | } |
---|
49 | void FightingController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) |
---|
50 | { |
---|
51 | SUPER( FightingController, XMLPort, xmlelement, mode ); |
---|
52 | } |
---|
53 | void FightingController::lookAtTarget(float dt) |
---|
54 | { |
---|
55 | ControllableEntity* entity = this->getControllableEntity(); |
---|
56 | if ( !entity ) |
---|
57 | return; |
---|
58 | Vector2 coord = get2DViewCoordinates |
---|
59 | ( entity->getPosition() , |
---|
60 | entity->getOrientation() * WorldEntity::FRONT, |
---|
61 | entity->getOrientation() * WorldEntity::UP, |
---|
62 | positionOfTarget_ ); |
---|
63 | |
---|
64 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
65 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
66 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
67 | |
---|
68 | //Yaw and Pitch are enough to start facing the target |
---|
69 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
70 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
71 | } |
---|
72 | void FightingController::stopLookingAtTarget() |
---|
73 | { |
---|
74 | this->bLookAtTarget_ = false; |
---|
75 | } |
---|
76 | void FightingController::startLookingAtTarget() |
---|
77 | { |
---|
78 | this->bLookAtTarget_ = true; |
---|
79 | } |
---|
80 | bool FightingController::hasTarget() |
---|
81 | { |
---|
82 | if ( this->target_ ) |
---|
83 | return true; |
---|
84 | return false; |
---|
85 | } |
---|
86 | |
---|
87 | void FightingController::setTarget( ControllableEntity* target ) |
---|
88 | { |
---|
89 | this->target_ = target; |
---|
90 | if ( this->target_ ) |
---|
91 | { |
---|
92 | this->setPositionOfTarget( target_->getWorldPosition() ); |
---|
93 | } |
---|
94 | } |
---|
95 | void FightingController::setPositionOfTarget( const Vector3& target ) |
---|
96 | { |
---|
97 | this->positionOfTarget_ = target; |
---|
98 | this->bHasPositionOfTarget_ = true; |
---|
99 | } |
---|
100 | void FightingController::setOrientationOfTarget( const Quaternion& orient ) |
---|
101 | { |
---|
102 | this->orientationOfTarget_=orient; |
---|
103 | this->bHasOrientationOfTarget_=true; |
---|
104 | } |
---|
105 | |
---|
106 | void FightingController::maneuver() |
---|
107 | { |
---|
108 | maneuverCounter_++; |
---|
109 | if (maneuverCounter_ > 5) |
---|
110 | maneuverCounter_ = 0; |
---|
111 | |
---|
112 | if ( !this->target_ || !this->getControllableEntity()) |
---|
113 | return; |
---|
114 | |
---|
115 | Vector3 thisPosition = this->getControllableEntity()->getWorldPosition(); |
---|
116 | this->setPositionOfTarget( getPredictedPosition( |
---|
117 | thisPosition, |
---|
118 | hardcoded_projectile_speed, |
---|
119 | this->target_->getWorldPosition() , |
---|
120 | this->target_->getVelocity() |
---|
121 | ) ); |
---|
122 | this->setOrientationOfTarget( this->target_->getOrientation() ); |
---|
123 | |
---|
124 | Vector3 diffVector = this->positionOfTarget_ - thisPosition; |
---|
125 | float diffLength = diffVector.length(); |
---|
126 | Vector3 diffUnit = diffVector/diffLength; |
---|
127 | |
---|
128 | bool bTargetIsLookingAtThis = CommonController::isLooking ( this->target_, getControllableEntity(), math::pi/20.0f ); |
---|
129 | |
---|
130 | //too far? well, come closer then |
---|
131 | if (diffLength > this->attackRange_) |
---|
132 | { |
---|
133 | this->spread_ = 300; |
---|
134 | this->formationMode_ = FormationMode::WALL; |
---|
135 | this->bKeepFormation_ = true; |
---|
136 | |
---|
137 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
138 | } |
---|
139 | //too close? How do u expect to dodge anything? Just attack! |
---|
140 | else if ( diffLength < 500 ) |
---|
141 | { |
---|
142 | this->bKeepFormation_ = false; |
---|
143 | |
---|
144 | //at this point, just look and shoot |
---|
145 | if ( diffLength < 250 ) |
---|
146 | { |
---|
147 | this->stopMoving(); |
---|
148 | this->startLookingAtTarget(); |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
153 | } |
---|
154 | } |
---|
155 | //Good distance? Check if target looks at us. It doesn't? Go hunt! |
---|
156 | else if ( !bTargetIsLookingAtThis ) |
---|
157 | { |
---|
158 | this->bKeepFormation_ = false; |
---|
159 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
160 | } |
---|
161 | //That's unfortunate, he is looking and probably shooting... try to dodge what we can... |
---|
162 | else |
---|
163 | { |
---|
164 | this->bKeepFormation_ = false; |
---|
165 | if (maneuverCounter_ == 0) |
---|
166 | { |
---|
167 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
168 | return; |
---|
169 | } |
---|
170 | dodge( thisPosition, diffUnit ); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | void FightingController::dodge(Vector3& thisPosition, Vector3& diffUnit) |
---|
175 | { |
---|
176 | float factorX = 0, factorY = 0, factorZ = 0; |
---|
177 | float rand = CommonController::randomInRange (0, 1); |
---|
178 | |
---|
179 | if (rand <= 0.5) |
---|
180 | { factorX = 1; } |
---|
181 | else |
---|
182 | { factorX = -1; } |
---|
183 | rand = CommonController::randomInRange (0, 1); |
---|
184 | if (rand <= 0.5) |
---|
185 | { factorY = 1; } |
---|
186 | else |
---|
187 | { factorY = -1; } |
---|
188 | rand = CommonController::randomInRange (0, 1); |
---|
189 | if (rand <= 0.5) |
---|
190 | { factorZ = 1; } |
---|
191 | else |
---|
192 | { factorZ = -1; } |
---|
193 | |
---|
194 | Vector3 target = ( diffUnit )* 8000.0f; |
---|
195 | Vector3* randVector = new Vector3( |
---|
196 | factorX * CommonController::randomInRange( 10000, 40000 ), |
---|
197 | factorY * CommonController::randomInRange( 10000, 40000 ), |
---|
198 | factorZ * CommonController::randomInRange( 10000, 40000 ) |
---|
199 | ); |
---|
200 | Vector3 projection = randVector->dotProduct( diffUnit )* diffUnit; |
---|
201 | Vector3 randV = *randVector; |
---|
202 | delete randVector; |
---|
203 | randV -= projection; |
---|
204 | target += randV; |
---|
205 | this->setTargetPosition( thisPosition + target ); |
---|
206 | } |
---|
207 | bool FightingController::canFire() |
---|
208 | { |
---|
209 | //no target? why fire? |
---|
210 | if ( !this->target_ ) |
---|
211 | return false; |
---|
212 | |
---|
213 | Vector3 newPositionOfTarget = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , |
---|
214 | hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() ); |
---|
215 | if ( newPositionOfTarget != Vector3::ZERO ) |
---|
216 | { |
---|
217 | this->setPositionOfTarget( newPositionOfTarget ); |
---|
218 | } |
---|
219 | |
---|
220 | float squaredDistance = squaredDistanceToTarget(); |
---|
221 | |
---|
222 | if ( squaredDistance < this->attackRange_*this->attackRange_ && this->isLookingAtTarget( math::pi / 20.0f)) |
---|
223 | { |
---|
224 | return true; |
---|
225 | } |
---|
226 | else |
---|
227 | { |
---|
228 | return false; |
---|
229 | } |
---|
230 | } |
---|
231 | void FightingController::doFire() |
---|
232 | { |
---|
233 | if ( !this->target_ || !this->getControllableEntity() ) |
---|
234 | { |
---|
235 | return; |
---|
236 | } |
---|
237 | |
---|
238 | Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() ); |
---|
239 | |
---|
240 | if ( pawn ) |
---|
241 | pawn->setAimPosition( this->positionOfTarget_ ); |
---|
242 | this->getControllableEntity() ->fire( 0 ); |
---|
243 | } |
---|
244 | float FightingController::squaredDistanceToTarget() const |
---|
245 | { |
---|
246 | if ( !this->getControllableEntity() ) |
---|
247 | return 0; |
---|
248 | if ( !this->target_ || !this->getControllableEntity() ) |
---|
249 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) ); |
---|
250 | else |
---|
251 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->positionOfTarget_ ) ); |
---|
252 | } |
---|
253 | bool FightingController::isLookingAtTarget( float angle ) |
---|
254 | { |
---|
255 | if ( !this->getControllableEntity() || !this->target_ ) |
---|
256 | return false; |
---|
257 | return CommonController::isLooking(this->getControllableEntity(), this->getTarget(), angle); |
---|
258 | } |
---|
259 | void FightingController::setClosestTarget() |
---|
260 | { |
---|
261 | this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); |
---|
262 | } |
---|
263 | |
---|
264 | Pawn* FightingController::closestTarget() |
---|
265 | { |
---|
266 | if (!this->getControllableEntity()) |
---|
267 | return 0; |
---|
268 | |
---|
269 | Pawn* closestTarget = 0; |
---|
270 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
271 | Gametype* gt = this->getGametype(); |
---|
272 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
273 | { |
---|
274 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
275 | continue; |
---|
276 | |
---|
277 | float distance = CommonController::distance (*itP, this->getControllableEntity()); |
---|
278 | if (distance < minDistance) |
---|
279 | { |
---|
280 | closestTarget = *itP; |
---|
281 | minDistance = distance; |
---|
282 | } |
---|
283 | } |
---|
284 | if (closestTarget) |
---|
285 | { |
---|
286 | return closestTarget; |
---|
287 | } |
---|
288 | return 0; |
---|
289 | } |
---|
290 | } |
---|