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 | //bug or feature? Press 4 control keys from {Q,W,E,A,S,D,C} at the same time or 3 keys from {Q,E,A,D}, spaceship goes in free fly mode |
---|
29 | #include "controllers/CommonController.h" |
---|
30 | #include "core/XMLPort.h" |
---|
31 | |
---|
32 | #include "weaponsystem/WeaponMode.h" |
---|
33 | #include "weaponsystem/WeaponPack.h" |
---|
34 | #include "weaponsystem/Weapon.h" |
---|
35 | #include "weaponsystem/WeaponSlot.h" |
---|
36 | #include "weaponsystem/WeaponSlot.h" |
---|
37 | #include "worldentities/pawns/SpaceShip.h" |
---|
38 | |
---|
39 | #include "Scene.h" |
---|
40 | |
---|
41 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
42 | #include "gametypes/TeamDeathmatch.h" |
---|
43 | #include "gametypes/Dynamicmatch.h" |
---|
44 | #include "gametypes/Mission.h" |
---|
45 | #include "gametypes/Gametype.h" |
---|
46 | #include "controllers/WaypointPatrolController.h" |
---|
47 | #include "controllers/NewHumanController.h" |
---|
48 | #include "controllers/DroneController.h" |
---|
49 | |
---|
50 | |
---|
51 | namespace orxonox |
---|
52 | { |
---|
53 | |
---|
54 | RegisterClass( CommonController ); |
---|
55 | const float SPEED = 0.9f/0.02f; |
---|
56 | const float ROTATEFACTOR = 1.0f/0.02f; |
---|
57 | |
---|
58 | CommonController::CommonController( Context* context ): Controller( context ) |
---|
59 | { |
---|
60 | this->action_ = Action::FLY; |
---|
61 | this->stopLookingAtTarget(); |
---|
62 | |
---|
63 | RegisterObject( CommonController ); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | CommonController::~CommonController() |
---|
68 | { |
---|
69 | //orxout(internal_error) << "I died, my Rank is " << rank_ << endl; |
---|
70 | } |
---|
71 | |
---|
72 | void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) |
---|
73 | { |
---|
74 | SUPER( CommonController, XMLPort, xmlelement, mode ); |
---|
75 | XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode ); |
---|
76 | XMLPortParam( CommonController, "action", setActionXML, getActionXML, xmlelement, mode ); |
---|
77 | XMLPortParam ( CommonController, "protect", setProtectXML, getProtectXML, xmlelement, mode ); |
---|
78 | //XMLPortParam ( CommonController, "enemy", setEnemyXML, getEnemyXML, xmlelement, mode ); |
---|
79 | } |
---|
80 | void CommonController::setProtectXML( std::string val ) |
---|
81 | { |
---|
82 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
83 | { |
---|
84 | if ((*itP)->getName() == val) |
---|
85 | { |
---|
86 | this->setProtect (static_cast<ControllableEntity*>(*itP)); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | std::string CommonController::getProtectXML () |
---|
91 | { |
---|
92 | if (!this->getProtect()) |
---|
93 | return "noProtectWasSet"; |
---|
94 | return this->getProtect()->getName(); |
---|
95 | } |
---|
96 | |
---|
97 | void CommonController::setProtect (ControllableEntity* protect) |
---|
98 | { |
---|
99 | this->protect_ = protect; |
---|
100 | } |
---|
101 | ControllableEntity* CommonController::getProtect () |
---|
102 | { |
---|
103 | return this->protect_; |
---|
104 | } |
---|
105 | void CommonController::setActionXML( std::string val) |
---|
106 | { |
---|
107 | const std::string valUpper = getUppercase( val ); |
---|
108 | Action::Value value; |
---|
109 | |
---|
110 | if ( valUpper == "FIGHT" ) |
---|
111 | value = Action::FIGHT; |
---|
112 | else if ( valUpper == "FLY" ) |
---|
113 | value = Action::FLY; |
---|
114 | else if ( valUpper == "PROTECT" ) |
---|
115 | value = Action::PROTECT; |
---|
116 | else |
---|
117 | ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ val + "'." ); |
---|
118 | this->setAction( value ); |
---|
119 | } |
---|
120 | std::string CommonController::getActionXML() |
---|
121 | { |
---|
122 | switch ( this->action_ ) |
---|
123 | { |
---|
124 | case Action::FIGHT: |
---|
125 | { |
---|
126 | return "FIGHT"; |
---|
127 | break; |
---|
128 | } |
---|
129 | case Action::FLY: |
---|
130 | { |
---|
131 | return "FLY"; |
---|
132 | break; |
---|
133 | } |
---|
134 | case Action::PROTECT: |
---|
135 | { |
---|
136 | return "PROTECT"; |
---|
137 | break; |
---|
138 | } |
---|
139 | default: |
---|
140 | return "FIGHT"; |
---|
141 | break; |
---|
142 | } |
---|
143 | } |
---|
144 | void CommonController::setFormationModeXML( std::string val ) |
---|
145 | { |
---|
146 | const std::string valUpper = getUppercase( val ); |
---|
147 | FormationMode::Value value; |
---|
148 | |
---|
149 | if ( valUpper == "WALL" ) |
---|
150 | value = FormationMode::WALL; |
---|
151 | else if ( valUpper == "FINGER4" ) |
---|
152 | value = FormationMode::FINGER4; |
---|
153 | else if ( valUpper == "DIAMOND" ) |
---|
154 | value = FormationMode::DIAMOND; |
---|
155 | else |
---|
156 | ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." ); |
---|
157 | this->setFormationMode( value ); |
---|
158 | |
---|
159 | } |
---|
160 | std::string CommonController::getFormationModeXML() |
---|
161 | { |
---|
162 | switch ( this->formationMode_ ) |
---|
163 | { |
---|
164 | case FormationMode::WALL: |
---|
165 | { |
---|
166 | return "WALL"; |
---|
167 | break; |
---|
168 | } |
---|
169 | case FormationMode::FINGER4: |
---|
170 | { |
---|
171 | return "FINGER4"; |
---|
172 | break; |
---|
173 | } |
---|
174 | case FormationMode::DIAMOND: |
---|
175 | { |
---|
176 | return "DIAMOND"; |
---|
177 | break; |
---|
178 | } |
---|
179 | default: |
---|
180 | return "DIAMOND"; |
---|
181 | break; |
---|
182 | |
---|
183 | } |
---|
184 | } |
---|
185 | Action::Value CommonController::getAction () |
---|
186 | { |
---|
187 | return this->action_; |
---|
188 | } |
---|
189 | void CommonController::setAction (Action::Value action) |
---|
190 | { |
---|
191 | this->action_ = action; |
---|
192 | } |
---|
193 | |
---|
194 | void CommonController::setAction (Action::Value action, ControllableEntity* target) |
---|
195 | { |
---|
196 | this->action_ = action; |
---|
197 | if (action == Action::FIGHT) |
---|
198 | { |
---|
199 | if (target) |
---|
200 | this->setTarget (target); |
---|
201 | } |
---|
202 | else if (action == Action::PROTECT) |
---|
203 | { |
---|
204 | } |
---|
205 | } |
---|
206 | void CommonController::setAction (Action::Value action, const Vector3& target) |
---|
207 | { |
---|
208 | this->action_ = action; |
---|
209 | if (action == Action::FLY) |
---|
210 | { |
---|
211 | this->setTargetPosition (target); |
---|
212 | } |
---|
213 | else if (action == Action::PROTECT) |
---|
214 | { |
---|
215 | |
---|
216 | } |
---|
217 | } |
---|
218 | void CommonController::setAction (Action::Value action, const Vector3& target, const Quaternion& orient ) |
---|
219 | { |
---|
220 | this->action_ = action; |
---|
221 | if (action == Action::FLY) |
---|
222 | { |
---|
223 | this->setTargetPosition (target); |
---|
224 | this->setTargetOrientation (orient); |
---|
225 | } |
---|
226 | else if (action == Action::PROTECT) |
---|
227 | { |
---|
228 | |
---|
229 | } |
---|
230 | } |
---|
231 | void CommonController::setClosestTarget() |
---|
232 | { |
---|
233 | if (!this->getControllableEntity()) |
---|
234 | return; |
---|
235 | |
---|
236 | Pawn* closestTarget = 0; |
---|
237 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
238 | Gametype* gt = this->getGametype(); |
---|
239 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
240 | { |
---|
241 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
242 | continue; |
---|
243 | |
---|
244 | float distance = CommonController::distance (*itP, this->getControllableEntity()); |
---|
245 | if (distance < minDistance) |
---|
246 | { |
---|
247 | closestTarget = *itP; |
---|
248 | minDistance = distance; |
---|
249 | } |
---|
250 | } |
---|
251 | if (closestTarget) |
---|
252 | { |
---|
253 | (this)->setTarget(static_cast<ControllableEntity*>(closestTarget)); |
---|
254 | } |
---|
255 | } |
---|
256 | void CommonController::maneuver() |
---|
257 | { |
---|
258 | maneuverCounter_++; |
---|
259 | |
---|
260 | if (maneuverCounter_ > 5) |
---|
261 | maneuverCounter_ = 0; |
---|
262 | if ( this->target_ && this->getControllableEntity()) |
---|
263 | { |
---|
264 | Vector3 thisPosition = this->getControllableEntity()->getWorldPosition(); |
---|
265 | //Quaternion thisOrientation = this->getControllableEntity()->getOrientation(); |
---|
266 | |
---|
267 | this->setPositionOfTarget( getPredictedPosition( |
---|
268 | thisPosition, |
---|
269 | hardcoded_projectile_speed, |
---|
270 | this->target_->getWorldPosition() , |
---|
271 | this->target_->getVelocity() |
---|
272 | ) ); |
---|
273 | this->setOrientationOfTarget( this->target_->getOrientation() ); |
---|
274 | |
---|
275 | |
---|
276 | Vector3 diffVector = this->positionOfTarget_ - thisPosition; |
---|
277 | float diffLength = diffVector.length(); |
---|
278 | Vector3 diffUnit = diffVector/diffLength; |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | //bool bThisIsLookingAtTarget = this->isLooking ( getControllableEntity(), this->target_, math::pi/4 ); |
---|
283 | bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/10.0f ); |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | //too far? well, come closer then |
---|
288 | if ( diffLength > 3000 ) |
---|
289 | { |
---|
290 | if (diffLength < 6000) |
---|
291 | { |
---|
292 | |
---|
293 | } |
---|
294 | else |
---|
295 | { |
---|
296 | } |
---|
297 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
298 | } |
---|
299 | //too close? How do u expect to dodge anything? Just attack! |
---|
300 | else if ( diffLength < 500 ) |
---|
301 | { |
---|
302 | //at this point, just look and shoot |
---|
303 | if ( diffLength < 250 ) |
---|
304 | { |
---|
305 | this->stopMoving(); |
---|
306 | this->startLookingAtTarget(); |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
311 | } |
---|
312 | } |
---|
313 | //Good distance? Check if target looks at us. It doesn't? Go hunt! |
---|
314 | else if ( !bTargetIsLookingAtThis ) |
---|
315 | { |
---|
316 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
317 | /* if (maneuverCounter_ == 0) |
---|
318 | { |
---|
319 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
320 | return; |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | dodge( thisPosition, diffUnit ); |
---|
325 | }*/ |
---|
326 | } |
---|
327 | //That's unfortunate, he is looking and probably shooting... try to dodge what we can... |
---|
328 | else |
---|
329 | { |
---|
330 | |
---|
331 | if (maneuverCounter_ == 0) |
---|
332 | { |
---|
333 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
334 | return; |
---|
335 | } |
---|
336 | dodge( thisPosition, diffUnit ); |
---|
337 | |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | //orxout ( internal_error ) << "ManeuverType = " << this->maneuverType_ << endl; |
---|
342 | } |
---|
343 | ControllableEntity* CommonController::getTarget() |
---|
344 | { |
---|
345 | return this->target_; |
---|
346 | } |
---|
347 | void CommonController::dodge(Vector3& thisPosition, Vector3& diffUnit) |
---|
348 | { |
---|
349 | float factorX = 0, factorY = 0, factorZ = 0; |
---|
350 | float rand = randomInRange (0, 1); |
---|
351 | if (rand <= 0.5) |
---|
352 | { |
---|
353 | factorX = 1; |
---|
354 | } |
---|
355 | else |
---|
356 | { |
---|
357 | factorX = -1; |
---|
358 | } |
---|
359 | rand = randomInRange (0, 1); |
---|
360 | if (rand <= 0.5) |
---|
361 | { |
---|
362 | factorY = 1; |
---|
363 | } |
---|
364 | else |
---|
365 | { |
---|
366 | factorY = -1; |
---|
367 | } |
---|
368 | rand = randomInRange (0, 1); |
---|
369 | if (rand <= 0.5) |
---|
370 | { |
---|
371 | factorZ = 1; |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | factorZ = -1; |
---|
376 | } |
---|
377 | Vector3 target = ( diffUnit )* 8000.0f; |
---|
378 | Vector3* randVector = new Vector3( |
---|
379 | factorX * randomInRange( 10000, 40000 ), |
---|
380 | factorY * randomInRange( 10000, 40000 ), |
---|
381 | factorZ * randomInRange( 10000, 40000 ) |
---|
382 | ); |
---|
383 | Vector3 projection = randVector->dotProduct( diffUnit )* diffUnit; |
---|
384 | *randVector -= projection; |
---|
385 | target += *randVector; |
---|
386 | this->setTargetPosition( thisPosition + target ); |
---|
387 | } |
---|
388 | void CommonController::stopMoving() |
---|
389 | { |
---|
390 | this->bHasTargetPosition_ = false; |
---|
391 | } |
---|
392 | void CommonController::startLookingAtTarget() |
---|
393 | { |
---|
394 | this->bLookAtTarget_ = true; |
---|
395 | } |
---|
396 | void CommonController::stopLookingAtTarget() |
---|
397 | { |
---|
398 | this->bLookAtTarget_ = false; |
---|
399 | } |
---|
400 | void CommonController::lookAtTarget(float dt) |
---|
401 | { |
---|
402 | |
---|
403 | |
---|
404 | ControllableEntity* entity = this->getControllableEntity(); |
---|
405 | if ( !entity ) |
---|
406 | return; |
---|
407 | Vector2 coord = get2DViewCoordinates |
---|
408 | ( entity->getPosition() , |
---|
409 | entity->getOrientation() * WorldEntity::FRONT, |
---|
410 | entity->getOrientation() * WorldEntity::UP, |
---|
411 | positionOfTarget_ ); |
---|
412 | |
---|
413 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
414 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
415 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | //Yaw and Pitch are enough to start facing the target |
---|
420 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
421 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
422 | |
---|
423 | |
---|
424 | } |
---|
425 | |
---|
426 | bool CommonController::setWingman ( CommonController* wingman ) |
---|
427 | { |
---|
428 | return false; |
---|
429 | } |
---|
430 | |
---|
431 | bool CommonController::hasWingman() |
---|
432 | { |
---|
433 | return true; |
---|
434 | } |
---|
435 | void CommonController::setTarget( ControllableEntity* target ) |
---|
436 | { |
---|
437 | this->target_ = target; |
---|
438 | //orxout ( internal_error ) << " TARGET SET " << endl; |
---|
439 | |
---|
440 | if ( this->target_ ) |
---|
441 | { |
---|
442 | this->setPositionOfTarget( target_->getWorldPosition() ); |
---|
443 | |
---|
444 | } |
---|
445 | } |
---|
446 | bool CommonController::hasTarget() |
---|
447 | { |
---|
448 | if ( this->target_ ) |
---|
449 | return true; |
---|
450 | return false; |
---|
451 | } |
---|
452 | void CommonController::setPositionOfTarget( const Vector3& target ) |
---|
453 | { |
---|
454 | this->positionOfTarget_ = target; |
---|
455 | this->bHasPositionOfTarget_ = true; |
---|
456 | } |
---|
457 | void CommonController::setOrientationOfTarget( const Quaternion& orient ) |
---|
458 | { |
---|
459 | this->orientationOfTarget_=orient; |
---|
460 | this->bHasOrientationOfTarget_=true; |
---|
461 | } |
---|
462 | |
---|
463 | void CommonController::setTargetPosition( const Vector3& target ) |
---|
464 | { |
---|
465 | this->targetPosition_ = target; |
---|
466 | this->bHasTargetPosition_ = true; |
---|
467 | } |
---|
468 | |
---|
469 | void CommonController::setTargetOrientation( const Quaternion& orient ) |
---|
470 | { |
---|
471 | this->targetOrientation_=orient; |
---|
472 | this->bHasTargetOrientation_=true; |
---|
473 | } |
---|
474 | |
---|
475 | void CommonController::setTargetOrientation( ControllableEntity* target ) |
---|
476 | { |
---|
477 | if ( target ) |
---|
478 | setTargetOrientation( target->getOrientation() ); |
---|
479 | } |
---|
480 | |
---|
481 | |
---|
482 | |
---|
483 | //copy the Roll orientation of given Quaternion. |
---|
484 | void CommonController::copyOrientation( const Quaternion& orient, float dt ) |
---|
485 | { |
---|
486 | //roll angle difference in radian |
---|
487 | float diff=orient.getRoll( false ).valueRadians() -( this->getControllableEntity() ->getOrientation() .getRoll( false ).valueRadians() ); |
---|
488 | while( diff>math::twoPi )diff-=math::twoPi; |
---|
489 | while( diff<-math::twoPi )diff+=math::twoPi; |
---|
490 | this->getControllableEntity() ->rotateRoll( diff*ROTATEFACTOR * dt ); |
---|
491 | } |
---|
492 | void CommonController::copyTargetOrientation( float dt ) |
---|
493 | { |
---|
494 | if ( bHasTargetOrientation_ ) |
---|
495 | { |
---|
496 | copyOrientation( targetOrientation_, dt ); |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | |
---|
501 | |
---|
502 | |
---|
503 | void CommonController::moveToTargetPosition( float dt ) |
---|
504 | { |
---|
505 | this->moveToPosition( this->targetPosition_, dt ); |
---|
506 | } |
---|
507 | void CommonController::moveToPosition( const Vector3& target, float dt ) |
---|
508 | { |
---|
509 | |
---|
510 | |
---|
511 | //100 is ( so far )the smallest tolerance ( empirically found )that can be reached, |
---|
512 | //with smaller distance spaceships can't reach position and go circles around it instead |
---|
513 | int tolerance = 65; |
---|
514 | |
---|
515 | ControllableEntity* entity = this->getControllableEntity(); |
---|
516 | Vector2 coord = get2DViewCoordinates |
---|
517 | ( entity->getPosition() , |
---|
518 | entity->getOrientation() * WorldEntity::FRONT, |
---|
519 | entity->getOrientation() * WorldEntity::UP, |
---|
520 | target ); |
---|
521 | |
---|
522 | float distance = ( target - this->getControllableEntity() ->getPosition() ).length(); |
---|
523 | |
---|
524 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
525 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
526 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
527 | |
---|
528 | |
---|
529 | if ( distance > tolerance ) |
---|
530 | { |
---|
531 | //Yaw and Pitch are enough to start facing the target |
---|
532 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
533 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
534 | |
---|
535 | //300 works, maybe less is better |
---|
536 | if ( distance < 400 ) |
---|
537 | { |
---|
538 | //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory |
---|
539 | //It's important that roll is not changed in the process of changing yaw and pitch |
---|
540 | //Wingmen won't face same direction as Leaders, but when Leaders start moving |
---|
541 | //Yaw and Pitch will adapt. |
---|
542 | if ( bHasTargetOrientation_ ) |
---|
543 | { |
---|
544 | copyTargetOrientation( dt ); |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | this->getControllableEntity() ->moveFrontBack( SPEED * dt ); |
---|
549 | } |
---|
550 | else |
---|
551 | { |
---|
552 | bHasTargetPosition_ = false; |
---|
553 | bHasTargetOrientation_ = false; |
---|
554 | } |
---|
555 | } |
---|
556 | float CommonController::randomInRange( float a, float b ) |
---|
557 | { |
---|
558 | float random = rnd( 1.0f ); |
---|
559 | float diff = b - a; |
---|
560 | float r = random * diff; |
---|
561 | return a + r; |
---|
562 | } |
---|
563 | |
---|
564 | |
---|
565 | //to be called in action |
---|
566 | //PRE: relativeTargetPosition is desired position relative to the spaceship, |
---|
567 | //angleRoll is the angle in degrees of Roll that should be applied by the end of the movement |
---|
568 | //POST: target orientation and position are set, so that it can be used by MoveAndRoll() |
---|
569 | void CommonController::moveToPoint( const Vector3& relativeTargetPosition, float angleRoll ) |
---|
570 | { |
---|
571 | ControllableEntity* entity = this->getControllableEntity(); |
---|
572 | if ( !entity ) |
---|
573 | return; |
---|
574 | Quaternion orient = entity->getWorldOrientation(); |
---|
575 | Quaternion rotation = Quaternion( Degree( angleRoll ), Vector3::UNIT_Z ); |
---|
576 | |
---|
577 | Vector3 target = orient * relativeTargetPosition + entity->getWorldPosition(); |
---|
578 | setTargetPosition( target ); |
---|
579 | orient = orient * rotation; |
---|
580 | this->setTargetOrientation( orient ); |
---|
581 | |
---|
582 | } |
---|
583 | //to be called in tick |
---|
584 | //PRE: MoveToPoint was called |
---|
585 | //POST: spaceship changes its yaw and pitch to point towards targetPosition_, |
---|
586 | //moves towards targetPosition_ by amount depending on dt and its speed, |
---|
587 | //rolls by amount depending on the difference between angleRoll_ and angleRolled_, dt, and |
---|
588 | //angular speed |
---|
589 | //if position reached with a certain tolerance, and angleRolled_ = angleRoll_, returns false, |
---|
590 | //otherwise returns true |
---|
591 | //dt is normally around 0.02f, which makes it 1/0.02 = 50 frames/sec |
---|
592 | bool CommonController::moveAndRoll( float dt ) |
---|
593 | { |
---|
594 | float factor = 1; |
---|
595 | if ( !this->getControllableEntity() ) |
---|
596 | return false; |
---|
597 | if ( this->rank_ == Rank::DIVISIONLEADER ) |
---|
598 | factor = 0.8; |
---|
599 | if ( this->rank_ == Rank::SECTIONLEADER ) |
---|
600 | factor = 0.9; |
---|
601 | int tolerance = 60; |
---|
602 | |
---|
603 | ControllableEntity* entity = this->getControllableEntity(); |
---|
604 | if ( !entity ) |
---|
605 | return true; |
---|
606 | Vector2 coord = get2DViewCoordinates |
---|
607 | ( entity->getPosition() , |
---|
608 | entity->getOrientation() * WorldEntity::FRONT, |
---|
609 | entity->getOrientation() * WorldEntity::UP, |
---|
610 | targetPosition_ ); |
---|
611 | |
---|
612 | float distance = ( targetPosition_ - this->getControllableEntity() ->getPosition() ).length(); |
---|
613 | |
---|
614 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
615 | float rotateX = clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
616 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
617 | |
---|
618 | |
---|
619 | if ( distance > tolerance ) |
---|
620 | { |
---|
621 | //Yaw and Pitch are enough to start facing the target |
---|
622 | this->getControllableEntity() ->rotateYaw( -2.0f * ROTATEFACTOR * rotateX * dt ); |
---|
623 | this->getControllableEntity() ->rotatePitch( 2.0f * ROTATEFACTOR * rotateY * dt ); |
---|
624 | |
---|
625 | //Roll |
---|
626 | if ( bHasTargetOrientation_ ) |
---|
627 | { |
---|
628 | copyTargetOrientation( dt ); |
---|
629 | } |
---|
630 | |
---|
631 | //Move |
---|
632 | this->getControllableEntity() ->moveFrontBack( 1.2f * SPEED * factor * dt ); |
---|
633 | //if still moving, return false |
---|
634 | return false; |
---|
635 | } |
---|
636 | else |
---|
637 | { |
---|
638 | |
---|
639 | //if finished, return true; |
---|
640 | return true; |
---|
641 | } |
---|
642 | } |
---|
643 | |
---|
644 | float CommonController::squaredDistanceToTarget() const |
---|
645 | { |
---|
646 | if ( !this->getControllableEntity() ) |
---|
647 | return 0; |
---|
648 | if ( !this->target_ || !this->getControllableEntity() ) |
---|
649 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) ); |
---|
650 | else |
---|
651 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->positionOfTarget_ ) ); |
---|
652 | } |
---|
653 | |
---|
654 | bool CommonController::isLookingAtTarget( float angle )const |
---|
655 | { |
---|
656 | if ( !this->getControllableEntity() || !this->target_ ) |
---|
657 | return false; |
---|
658 | |
---|
659 | return ( getAngle( this->getControllableEntity() ->getPosition() , |
---|
660 | this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT, this->positionOfTarget_ ) < angle ); |
---|
661 | } |
---|
662 | bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) |
---|
663 | { |
---|
664 | if ( !entityThatLooks || !entityBeingLookedAt ) |
---|
665 | return false; |
---|
666 | return ( getAngle( entityThatLooks ->getPosition() , |
---|
667 | entityThatLooks->getOrientation() * WorldEntity::FRONT, |
---|
668 | entityBeingLookedAt->getWorldPosition() ) < angle ); |
---|
669 | } |
---|
670 | |
---|
671 | bool CommonController::canFire() |
---|
672 | { |
---|
673 | |
---|
674 | //no target? why fire? |
---|
675 | if ( !this->target_ ) |
---|
676 | return false; |
---|
677 | |
---|
678 | Vector3 newPositionOfTarget = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , |
---|
679 | hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() ); |
---|
680 | if ( newPositionOfTarget != Vector3::ZERO ) |
---|
681 | { |
---|
682 | this->setPositionOfTarget( newPositionOfTarget ); |
---|
683 | } |
---|
684 | |
---|
685 | float squaredDistance = squaredDistanceToTarget(); |
---|
686 | |
---|
687 | if ( squaredDistance < 9000000.0f && this->isLookingAtTarget( math::pi / 20.0f)) { |
---|
688 | return true; |
---|
689 | } |
---|
690 | else |
---|
691 | { |
---|
692 | return false; |
---|
693 | } |
---|
694 | |
---|
695 | } |
---|
696 | float CommonController::distance (ControllableEntity* entity1, ControllableEntity* entity2) |
---|
697 | { |
---|
698 | if (!entity1 || !entity2) |
---|
699 | return std::numeric_limits<float>::infinity(); |
---|
700 | return ( entity1->getPosition() - entity2->getPosition() ).length(); |
---|
701 | } |
---|
702 | bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
703 | { |
---|
704 | /*if (!entity1 || !entity2) |
---|
705 | return false; |
---|
706 | return entity1->getTeam() == entity2->getTeam();*/ |
---|
707 | if (entity1 == entity2) |
---|
708 | return true; |
---|
709 | |
---|
710 | int team1 = entity1->getTeam(); |
---|
711 | int team2 = entity2->getTeam(); |
---|
712 | |
---|
713 | Controller* controller = 0; |
---|
714 | if (entity1->getController()) |
---|
715 | controller = entity1->getController(); |
---|
716 | else |
---|
717 | controller = entity1->getXMLController(); |
---|
718 | if (controller) |
---|
719 | { |
---|
720 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
721 | if (ac) |
---|
722 | team1 = ac->getTeam(); |
---|
723 | } |
---|
724 | |
---|
725 | if (entity2->getController()) |
---|
726 | controller = entity2->getController(); |
---|
727 | else |
---|
728 | controller = entity2->getXMLController(); |
---|
729 | if (controller) |
---|
730 | { |
---|
731 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
732 | if (ac) |
---|
733 | team2 = ac->getTeam(); |
---|
734 | } |
---|
735 | |
---|
736 | TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype); |
---|
737 | if (tdm) |
---|
738 | { |
---|
739 | if (entity1->getPlayer()) |
---|
740 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
741 | |
---|
742 | if (entity2->getPlayer()) |
---|
743 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
744 | } |
---|
745 | |
---|
746 | TeamBaseMatchBase* base = 0; |
---|
747 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
748 | if (base) |
---|
749 | { |
---|
750 | switch (base->getState()) |
---|
751 | { |
---|
752 | case BaseState::ControlTeam1: |
---|
753 | team1 = 0; |
---|
754 | break; |
---|
755 | case BaseState::ControlTeam2: |
---|
756 | team1 = 1; |
---|
757 | break; |
---|
758 | case BaseState::Uncontrolled: |
---|
759 | default: |
---|
760 | team1 = -1; |
---|
761 | } |
---|
762 | } |
---|
763 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
764 | if (base) |
---|
765 | { |
---|
766 | switch (base->getState()) |
---|
767 | { |
---|
768 | case BaseState::ControlTeam1: |
---|
769 | team2 = 0; |
---|
770 | break; |
---|
771 | case BaseState::ControlTeam2: |
---|
772 | team2 = 1; |
---|
773 | break; |
---|
774 | case BaseState::Uncontrolled: |
---|
775 | default: |
---|
776 | team2 = -1; |
---|
777 | } |
---|
778 | } |
---|
779 | |
---|
780 | DroneController* droneController = 0; |
---|
781 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
782 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
783 | return true; |
---|
784 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
785 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
786 | return true; |
---|
787 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
788 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
789 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
790 | return true; |
---|
791 | |
---|
792 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
793 | if (dynamic) |
---|
794 | { |
---|
795 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
796 | |
---|
797 | if (entity1->getPlayer()) |
---|
798 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
799 | |
---|
800 | if (entity2->getPlayer()) |
---|
801 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
802 | |
---|
803 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
---|
804 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
805 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
806 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
807 | else return true; |
---|
808 | } |
---|
809 | |
---|
810 | return (team1 == team2 && team1 != -1); |
---|
811 | } |
---|
812 | void CommonController::doFire() |
---|
813 | { |
---|
814 | if ( !this->target_ || !this->getControllableEntity() ) |
---|
815 | { |
---|
816 | return; |
---|
817 | } |
---|
818 | |
---|
819 | Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() ); |
---|
820 | |
---|
821 | if ( pawn ) |
---|
822 | //pawn->setAimPosition( this->getControllableEntity() ->getWorldPosition() + 4000*( this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT )); |
---|
823 | pawn->setAimPosition( this->positionOfTarget_ ); |
---|
824 | |
---|
825 | this->getControllableEntity() ->fire( 0 ); |
---|
826 | } |
---|
827 | |
---|
828 | |
---|
829 | } |
---|