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->squaredaccuracy_ = 500; |
---|
61 | this->action_ = Action::NONE; |
---|
62 | this->stopLookingAtTarget(); |
---|
63 | this->attackRange_ = 800; |
---|
64 | RegisterObject( CommonController ); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | CommonController::~CommonController() |
---|
69 | { |
---|
70 | //orxout(internal_error) << "I died, my Rank is " << rank_ << endl; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) |
---|
75 | { |
---|
76 | SUPER( CommonController, XMLPort, xmlelement, mode ); |
---|
77 | XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode ); |
---|
78 | //XMLPortParam( CommonController, "action", setActionXML, getActionXML, xmlelement, mode ); |
---|
79 | //XMLPortParam ( CommonController, "protect", setProtectXML, getProtectXML, xmlelement, mode ); |
---|
80 | //XMLPortParam ( CommonController, "enemy", setEnemyXML, getEnemyXML, xmlelement, mode ); |
---|
81 | XMLPortObject(CommonController, WorldEntity, "actionpoints", addActionpoint, getActionpoint, xmlelement, mode); |
---|
82 | |
---|
83 | } |
---|
84 | void CommonController::setProtectXML( std::string val ) |
---|
85 | { |
---|
86 | this->protectName_ = val; |
---|
87 | |
---|
88 | } |
---|
89 | void CommonController::tick(float dt) |
---|
90 | { |
---|
91 | if (this->bHasTargetPosition_) |
---|
92 | { |
---|
93 | this->moveToTargetPosition(dt); |
---|
94 | } |
---|
95 | else if (this->bLookAtTarget_) |
---|
96 | { |
---|
97 | this->lookAtTarget(dt); |
---|
98 | } |
---|
99 | if (bShooting_) |
---|
100 | { |
---|
101 | this->doFire(); |
---|
102 | } |
---|
103 | SUPER(CommonController, tick, dt); |
---|
104 | } |
---|
105 | |
---|
106 | std::string CommonController::getProtectXML () |
---|
107 | { |
---|
108 | if (!this->getProtect()) |
---|
109 | return "noProtectWasSet"; |
---|
110 | return CommonController::getName (orxonox_cast<Pawn*>(this->getProtect())); |
---|
111 | } |
---|
112 | std::string CommonController::getName(Pawn* entity) |
---|
113 | { |
---|
114 | std::string name = entity->getName(); |
---|
115 | if (name == "") |
---|
116 | { |
---|
117 | const void * address = static_cast<const void*>(entity); |
---|
118 | std::stringstream ss; |
---|
119 | ss << address; |
---|
120 | name = ss.str(); |
---|
121 | } |
---|
122 | return name; |
---|
123 | |
---|
124 | } |
---|
125 | void CommonController::setProtect (ControllableEntity* protect) |
---|
126 | { |
---|
127 | this->protect_ = protect; |
---|
128 | } |
---|
129 | ControllableEntity* CommonController::getProtect () |
---|
130 | { |
---|
131 | return this->protect_; |
---|
132 | } |
---|
133 | void CommonController::setActionXML( std::string val) |
---|
134 | { |
---|
135 | const std::string valUpper = getUppercase( val ); |
---|
136 | Action::Value value; |
---|
137 | |
---|
138 | if ( valUpper == "FIGHT" ) |
---|
139 | value = Action::FIGHT; |
---|
140 | else if ( valUpper == "FLY" ) |
---|
141 | value = Action::FLY; |
---|
142 | else if ( valUpper == "PROTECT" ) |
---|
143 | value = Action::PROTECT; |
---|
144 | else if ( valUpper == "NONE" ) |
---|
145 | value = Action::NONE; |
---|
146 | else if ( valUpper == "FIGHTALL" ) |
---|
147 | value = Action::FIGHTALL; |
---|
148 | else if ( valUpper == "ATTACK" ) |
---|
149 | value = Action::ATTACK; |
---|
150 | else |
---|
151 | ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ val + "'." ); |
---|
152 | this->setAction( value ); |
---|
153 | } |
---|
154 | std::string CommonController::getActionXML() |
---|
155 | { |
---|
156 | switch ( this->action_ ) |
---|
157 | { |
---|
158 | case Action::FIGHT: |
---|
159 | { |
---|
160 | return "FIGHT"; |
---|
161 | break; |
---|
162 | } |
---|
163 | case Action::FLY: |
---|
164 | { |
---|
165 | return "FLY"; |
---|
166 | break; |
---|
167 | } |
---|
168 | case Action::PROTECT: |
---|
169 | { |
---|
170 | return "PROTECT"; |
---|
171 | break; |
---|
172 | } |
---|
173 | case Action::NONE: |
---|
174 | { |
---|
175 | return "NONE"; |
---|
176 | break; |
---|
177 | } |
---|
178 | case Action::FIGHTALL: |
---|
179 | { |
---|
180 | return "FIGHTALL"; |
---|
181 | break; |
---|
182 | } |
---|
183 | case Action::ATTACK: |
---|
184 | { |
---|
185 | return "ATTACK"; |
---|
186 | break; |
---|
187 | } |
---|
188 | default: |
---|
189 | return "NONE"; |
---|
190 | break; |
---|
191 | } |
---|
192 | } |
---|
193 | void CommonController::setFormationModeXML( std::string val ) |
---|
194 | { |
---|
195 | const std::string valUpper = getUppercase( val ); |
---|
196 | FormationMode::Value value; |
---|
197 | |
---|
198 | if ( valUpper == "WALL" ) |
---|
199 | value = FormationMode::WALL; |
---|
200 | else if ( valUpper == "FINGER4" ) |
---|
201 | value = FormationMode::FINGER4; |
---|
202 | else if ( valUpper == "DIAMOND" ) |
---|
203 | value = FormationMode::DIAMOND; |
---|
204 | else |
---|
205 | ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." ); |
---|
206 | this->setFormationMode( value ); |
---|
207 | |
---|
208 | } |
---|
209 | std::string CommonController::getFormationModeXML() |
---|
210 | { |
---|
211 | switch ( this->formationMode_ ) |
---|
212 | { |
---|
213 | case FormationMode::WALL: |
---|
214 | { |
---|
215 | return "WALL"; |
---|
216 | break; |
---|
217 | } |
---|
218 | case FormationMode::FINGER4: |
---|
219 | { |
---|
220 | return "FINGER4"; |
---|
221 | break; |
---|
222 | } |
---|
223 | case FormationMode::DIAMOND: |
---|
224 | { |
---|
225 | return "DIAMOND"; |
---|
226 | break; |
---|
227 | } |
---|
228 | default: |
---|
229 | return "DIAMOND"; |
---|
230 | break; |
---|
231 | |
---|
232 | } |
---|
233 | } |
---|
234 | Action::Value CommonController::getAction () |
---|
235 | { |
---|
236 | return this->action_; |
---|
237 | } |
---|
238 | void CommonController::setAction (Action::Value action) |
---|
239 | { |
---|
240 | this->action_ = action; |
---|
241 | } |
---|
242 | |
---|
243 | void CommonController::setAction (Action::Value action, ControllableEntity* target) |
---|
244 | { |
---|
245 | this->action_ = action; |
---|
246 | if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK) |
---|
247 | { |
---|
248 | if (target) |
---|
249 | this->setTarget (target); |
---|
250 | } |
---|
251 | else if (action == Action::PROTECT) |
---|
252 | { |
---|
253 | if (target) |
---|
254 | this->setProtect (target); |
---|
255 | } |
---|
256 | } |
---|
257 | void CommonController::setAction (Action::Value action, const Vector3& target) |
---|
258 | { |
---|
259 | this->action_ = action; |
---|
260 | if (action == Action::FLY) |
---|
261 | { |
---|
262 | this->setTargetPosition (target); |
---|
263 | } |
---|
264 | |
---|
265 | } |
---|
266 | void CommonController::setAction (Action::Value action, const Vector3& target, const Quaternion& orient ) |
---|
267 | { |
---|
268 | this->action_ = action; |
---|
269 | if (action == Action::FLY) |
---|
270 | { |
---|
271 | this->setTargetPosition (target); |
---|
272 | this->setTargetOrientation (orient); |
---|
273 | } |
---|
274 | |
---|
275 | } |
---|
276 | void CommonController::addActionpoint(WorldEntity* actionpoint) |
---|
277 | { |
---|
278 | std::string actionName; |
---|
279 | Vector3 position; |
---|
280 | std::string targetName; |
---|
281 | |
---|
282 | if (static_cast<Actionpoint*> (actionpoint)) |
---|
283 | { |
---|
284 | Actionpoint* ap = static_cast<Actionpoint*> (actionpoint); |
---|
285 | |
---|
286 | actionName = ap->getActionXML(); |
---|
287 | targetName = ap->getName(); |
---|
288 | position = ap->getWorldPosition(); |
---|
289 | |
---|
290 | Action::Value value; |
---|
291 | |
---|
292 | if ( actionName == "FIGHT" ) |
---|
293 | { |
---|
294 | value = Action::FIGHT; |
---|
295 | |
---|
296 | } |
---|
297 | else if ( actionName == "FLY" ) |
---|
298 | { |
---|
299 | value = Action::FLY; |
---|
300 | |
---|
301 | } |
---|
302 | else if ( actionName == "PROTECT" ) |
---|
303 | { |
---|
304 | value = Action::PROTECT; |
---|
305 | |
---|
306 | } |
---|
307 | else if ( actionName == "NONE" ) |
---|
308 | { |
---|
309 | value = Action::NONE; |
---|
310 | |
---|
311 | } |
---|
312 | else if ( actionName == "FIGHTALL" ) |
---|
313 | { |
---|
314 | value = Action::FIGHTALL; |
---|
315 | |
---|
316 | } |
---|
317 | else if ( actionName == "ATTACK" ) |
---|
318 | { |
---|
319 | value = Action::ATTACK; |
---|
320 | |
---|
321 | |
---|
322 | } |
---|
323 | else |
---|
324 | ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ actionName + "'." ); |
---|
325 | //this->setAction( value ); |
---|
326 | Point p = { value, targetName, position }; |
---|
327 | parsedActionpoints_.push_back(p); |
---|
328 | orxout(internal_error) << "Pushed " << p.action << endl; |
---|
329 | } |
---|
330 | else |
---|
331 | { |
---|
332 | Point p = { Action::FLY, "", actionpoint->getWorldPosition() }; |
---|
333 | parsedActionpoints_.push_back(p); |
---|
334 | } |
---|
335 | this->actionpoints_.push_back(actionpoint); |
---|
336 | |
---|
337 | |
---|
338 | } |
---|
339 | |
---|
340 | WorldEntity* CommonController::getActionpoint(unsigned int index) const |
---|
341 | { |
---|
342 | if (index < this->actionpoints_.size()) |
---|
343 | return this->actionpoints_[index]; |
---|
344 | else |
---|
345 | return 0; |
---|
346 | } |
---|
347 | void CommonController::setClosestTarget() |
---|
348 | { |
---|
349 | this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); |
---|
350 | } |
---|
351 | Pawn* CommonController::closestTarget() |
---|
352 | { |
---|
353 | if (!this->getControllableEntity()) |
---|
354 | return 0; |
---|
355 | |
---|
356 | Pawn* closestTarget = 0; |
---|
357 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
358 | Gametype* gt = this->getGametype(); |
---|
359 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
360 | { |
---|
361 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
362 | continue; |
---|
363 | |
---|
364 | float distance = CommonController::distance (*itP, this->getControllableEntity()); |
---|
365 | if (distance < minDistance) |
---|
366 | { |
---|
367 | closestTarget = *itP; |
---|
368 | minDistance = distance; |
---|
369 | } |
---|
370 | } |
---|
371 | if (closestTarget) |
---|
372 | { |
---|
373 | return closestTarget; |
---|
374 | } |
---|
375 | return 0; |
---|
376 | } |
---|
377 | //POST: this starts doing what was asked by the last element of parsedActionpoints_, |
---|
378 | //if last element was failed to be parsed, next element will be executed. |
---|
379 | void CommonController::executeActionpoint() |
---|
380 | { |
---|
381 | if (!this->parsedActionpoints_.empty()) |
---|
382 | { |
---|
383 | this->action_ = this->parsedActionpoints_.back().action; |
---|
384 | |
---|
385 | switch ( this->action_ ) |
---|
386 | { |
---|
387 | case Action::FIGHT: |
---|
388 | { |
---|
389 | break; |
---|
390 | } |
---|
391 | case Action::FLY: |
---|
392 | { |
---|
393 | this->setTargetPosition( this->parsedActionpoints_.back().position ); |
---|
394 | if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) |
---|
395 | { |
---|
396 | this->nextActionpoint(); |
---|
397 | this->executeActionpoint(); |
---|
398 | } |
---|
399 | break; |
---|
400 | } |
---|
401 | case Action::PROTECT: |
---|
402 | { |
---|
403 | std::string protectName = this->parsedActionpoints_.back().name; |
---|
404 | |
---|
405 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
406 | { |
---|
407 | if (CommonController::getName(*itP) == protectName) |
---|
408 | { |
---|
409 | this->setProtect (static_cast<ControllableEntity*>(*itP)); |
---|
410 | } |
---|
411 | } |
---|
412 | if (!this->getProtect()) |
---|
413 | { |
---|
414 | this->nextActionpoint(); |
---|
415 | this->executeActionpoint(); |
---|
416 | } |
---|
417 | break; |
---|
418 | } |
---|
419 | case Action::NONE: |
---|
420 | { |
---|
421 | break; |
---|
422 | } |
---|
423 | case Action::FIGHTALL: |
---|
424 | { |
---|
425 | break; |
---|
426 | } |
---|
427 | case Action::ATTACK: |
---|
428 | { |
---|
429 | std::string targetName = this->parsedActionpoints_.back().name; |
---|
430 | |
---|
431 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
432 | { |
---|
433 | if (CommonController::getName(*itP) == targetName) |
---|
434 | { |
---|
435 | orxout(internal_error) << "Attacking" << endl; |
---|
436 | this->setTarget (static_cast<ControllableEntity*>(*itP)); |
---|
437 | } |
---|
438 | } |
---|
439 | if (!this->getTarget()) |
---|
440 | { |
---|
441 | this->nextActionpoint(); |
---|
442 | this->executeActionpoint(); |
---|
443 | } |
---|
444 | break; |
---|
445 | } |
---|
446 | default: |
---|
447 | break; |
---|
448 | } |
---|
449 | |
---|
450 | |
---|
451 | } |
---|
452 | else |
---|
453 | { |
---|
454 | this->setTarget(0); |
---|
455 | this->setTargetPosition(this->getControllableEntity()->getWorldPosition()); |
---|
456 | this->action_ = Action::NONE; |
---|
457 | } |
---|
458 | orxout(internal_error) << "Executing action " << this->getActionXML() << endl; |
---|
459 | } |
---|
460 | void CommonController::nextActionpoint() |
---|
461 | { |
---|
462 | if (!this->parsedActionpoints_.empty()) |
---|
463 | { |
---|
464 | this->parsedActionpoints_.pop_back(); |
---|
465 | } |
---|
466 | this->setAction(Action::NONE); |
---|
467 | } |
---|
468 | |
---|
469 | void CommonController::maneuver() |
---|
470 | { |
---|
471 | maneuverCounter_++; |
---|
472 | |
---|
473 | if (maneuverCounter_ > 5) |
---|
474 | maneuverCounter_ = 0; |
---|
475 | if ( this->target_ && this->getControllableEntity()) |
---|
476 | { |
---|
477 | Vector3 thisPosition = this->getControllableEntity()->getWorldPosition(); |
---|
478 | //Quaternion thisOrientation = this->getControllableEntity()->getOrientation(); |
---|
479 | |
---|
480 | this->setPositionOfTarget( getPredictedPosition( |
---|
481 | thisPosition, |
---|
482 | hardcoded_projectile_speed, |
---|
483 | this->target_->getWorldPosition() , |
---|
484 | this->target_->getVelocity() |
---|
485 | ) ); |
---|
486 | this->setOrientationOfTarget( this->target_->getOrientation() ); |
---|
487 | |
---|
488 | |
---|
489 | Vector3 diffVector = this->positionOfTarget_ - thisPosition; |
---|
490 | float diffLength = diffVector.length(); |
---|
491 | Vector3 diffUnit = diffVector/diffLength; |
---|
492 | |
---|
493 | |
---|
494 | |
---|
495 | //bool bThisIsLookingAtTarget = this->isLooking ( getControllableEntity(), this->target_, math::pi/4 ); |
---|
496 | bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/10.0f ); |
---|
497 | |
---|
498 | |
---|
499 | |
---|
500 | //too far? well, come closer then |
---|
501 | if ( diffLength > 3000 ) |
---|
502 | { |
---|
503 | if (diffLength < 6000) |
---|
504 | { |
---|
505 | |
---|
506 | } |
---|
507 | else |
---|
508 | { |
---|
509 | } |
---|
510 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
511 | } |
---|
512 | //too close? How do u expect to dodge anything? Just attack! |
---|
513 | else if ( diffLength < 500 ) |
---|
514 | { |
---|
515 | //at this point, just look and shoot |
---|
516 | if ( diffLength < 250 ) |
---|
517 | { |
---|
518 | this->stopMoving(); |
---|
519 | this->startLookingAtTarget(); |
---|
520 | } |
---|
521 | else |
---|
522 | { |
---|
523 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
524 | } |
---|
525 | } |
---|
526 | //Good distance? Check if target looks at us. It doesn't? Go hunt! |
---|
527 | else if ( !bTargetIsLookingAtThis ) |
---|
528 | { |
---|
529 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
530 | /* if (maneuverCounter_ == 0) |
---|
531 | { |
---|
532 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
533 | return; |
---|
534 | } |
---|
535 | else |
---|
536 | { |
---|
537 | dodge( thisPosition, diffUnit ); |
---|
538 | }*/ |
---|
539 | } |
---|
540 | //That's unfortunate, he is looking and probably shooting... try to dodge what we can... |
---|
541 | else |
---|
542 | { |
---|
543 | |
---|
544 | if (maneuverCounter_ == 0) |
---|
545 | { |
---|
546 | this->setTargetPosition( this->positionOfTarget_ ); |
---|
547 | return; |
---|
548 | } |
---|
549 | dodge( thisPosition, diffUnit ); |
---|
550 | |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | //orxout ( internal_error ) << "ManeuverType = " << this->maneuverType_ << endl; |
---|
555 | } |
---|
556 | ControllableEntity* CommonController::getTarget() |
---|
557 | { |
---|
558 | return this->target_; |
---|
559 | } |
---|
560 | void CommonController::dodge(Vector3& thisPosition, Vector3& diffUnit) |
---|
561 | { |
---|
562 | float factorX = 0, factorY = 0, factorZ = 0; |
---|
563 | float rand = randomInRange (0, 1); |
---|
564 | if (rand <= 0.5) |
---|
565 | { |
---|
566 | factorX = 1; |
---|
567 | } |
---|
568 | else |
---|
569 | { |
---|
570 | factorX = -1; |
---|
571 | } |
---|
572 | rand = randomInRange (0, 1); |
---|
573 | if (rand <= 0.5) |
---|
574 | { |
---|
575 | factorY = 1; |
---|
576 | } |
---|
577 | else |
---|
578 | { |
---|
579 | factorY = -1; |
---|
580 | } |
---|
581 | rand = randomInRange (0, 1); |
---|
582 | if (rand <= 0.5) |
---|
583 | { |
---|
584 | factorZ = 1; |
---|
585 | } |
---|
586 | else |
---|
587 | { |
---|
588 | factorZ = -1; |
---|
589 | } |
---|
590 | Vector3 target = ( diffUnit )* 8000.0f; |
---|
591 | Vector3* randVector = new Vector3( |
---|
592 | factorX * randomInRange( 10000, 40000 ), |
---|
593 | factorY * randomInRange( 10000, 40000 ), |
---|
594 | factorZ * randomInRange( 10000, 40000 ) |
---|
595 | ); |
---|
596 | Vector3 projection = randVector->dotProduct( diffUnit )* diffUnit; |
---|
597 | *randVector -= projection; |
---|
598 | target += *randVector; |
---|
599 | this->setTargetPosition( thisPosition + target ); |
---|
600 | } |
---|
601 | void CommonController::stopMoving() |
---|
602 | { |
---|
603 | this->bHasTargetPosition_ = false; |
---|
604 | } |
---|
605 | void CommonController::startLookingAtTarget() |
---|
606 | { |
---|
607 | this->bLookAtTarget_ = true; |
---|
608 | } |
---|
609 | void CommonController::stopLookingAtTarget() |
---|
610 | { |
---|
611 | this->bLookAtTarget_ = false; |
---|
612 | } |
---|
613 | void CommonController::lookAtTarget(float dt) |
---|
614 | { |
---|
615 | |
---|
616 | |
---|
617 | ControllableEntity* entity = this->getControllableEntity(); |
---|
618 | if ( !entity ) |
---|
619 | return; |
---|
620 | Vector2 coord = get2DViewCoordinates |
---|
621 | ( entity->getPosition() , |
---|
622 | entity->getOrientation() * WorldEntity::FRONT, |
---|
623 | entity->getOrientation() * WorldEntity::UP, |
---|
624 | positionOfTarget_ ); |
---|
625 | |
---|
626 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
627 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
628 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
629 | |
---|
630 | |
---|
631 | |
---|
632 | //Yaw and Pitch are enough to start facing the target |
---|
633 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
634 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
635 | |
---|
636 | |
---|
637 | } |
---|
638 | |
---|
639 | bool CommonController::setWingman ( CommonController* wingman ) |
---|
640 | { |
---|
641 | return false; |
---|
642 | } |
---|
643 | |
---|
644 | bool CommonController::hasWingman() |
---|
645 | { |
---|
646 | return true; |
---|
647 | } |
---|
648 | void CommonController::setTarget( ControllableEntity* target ) |
---|
649 | { |
---|
650 | this->target_ = target; |
---|
651 | //orxout ( internal_error ) << " TARGET SET " << endl; |
---|
652 | |
---|
653 | if ( this->target_ ) |
---|
654 | { |
---|
655 | this->setPositionOfTarget( target_->getWorldPosition() ); |
---|
656 | |
---|
657 | } |
---|
658 | } |
---|
659 | bool CommonController::hasTarget() |
---|
660 | { |
---|
661 | if ( this->target_ ) |
---|
662 | return true; |
---|
663 | return false; |
---|
664 | } |
---|
665 | void CommonController::setPositionOfTarget( const Vector3& target ) |
---|
666 | { |
---|
667 | this->positionOfTarget_ = target; |
---|
668 | this->bHasPositionOfTarget_ = true; |
---|
669 | } |
---|
670 | void CommonController::setOrientationOfTarget( const Quaternion& orient ) |
---|
671 | { |
---|
672 | this->orientationOfTarget_=orient; |
---|
673 | this->bHasOrientationOfTarget_=true; |
---|
674 | } |
---|
675 | |
---|
676 | void CommonController::setTargetPosition( const Vector3& target ) |
---|
677 | { |
---|
678 | this->targetPosition_ = target; |
---|
679 | this->bHasTargetPosition_ = true; |
---|
680 | } |
---|
681 | |
---|
682 | void CommonController::setTargetOrientation( const Quaternion& orient ) |
---|
683 | { |
---|
684 | this->targetOrientation_=orient; |
---|
685 | this->bHasTargetOrientation_=true; |
---|
686 | } |
---|
687 | |
---|
688 | void CommonController::setTargetOrientation( ControllableEntity* target ) |
---|
689 | { |
---|
690 | if ( target ) |
---|
691 | setTargetOrientation( target->getOrientation() ); |
---|
692 | } |
---|
693 | |
---|
694 | |
---|
695 | |
---|
696 | //copy the Roll orientation of given Quaternion. |
---|
697 | void CommonController::copyOrientation( const Quaternion& orient, float dt ) |
---|
698 | { |
---|
699 | //roll angle difference in radian |
---|
700 | float diff=orient.getRoll( false ).valueRadians() -( this->getControllableEntity() ->getOrientation() .getRoll( false ).valueRadians() ); |
---|
701 | while( diff>math::twoPi )diff-=math::twoPi; |
---|
702 | while( diff<-math::twoPi )diff+=math::twoPi; |
---|
703 | this->getControllableEntity() ->rotateRoll( diff*ROTATEFACTOR * dt ); |
---|
704 | } |
---|
705 | void CommonController::copyTargetOrientation( float dt ) |
---|
706 | { |
---|
707 | if ( bHasTargetOrientation_ ) |
---|
708 | { |
---|
709 | copyOrientation( targetOrientation_, dt ); |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | |
---|
714 | |
---|
715 | |
---|
716 | void CommonController::moveToTargetPosition( float dt ) |
---|
717 | { |
---|
718 | this->moveToPosition( this->targetPosition_, dt ); |
---|
719 | } |
---|
720 | void CommonController::moveToPosition( const Vector3& target, float dt ) |
---|
721 | { |
---|
722 | |
---|
723 | |
---|
724 | //100 is ( so far )the smallest tolerance ( empirically found )that can be reached, |
---|
725 | //with smaller distance spaceships can't reach position and go circles around it instead |
---|
726 | int tolerance = 65; |
---|
727 | |
---|
728 | ControllableEntity* entity = this->getControllableEntity(); |
---|
729 | Vector2 coord = get2DViewCoordinates |
---|
730 | ( entity->getPosition() , |
---|
731 | entity->getOrientation() * WorldEntity::FRONT, |
---|
732 | entity->getOrientation() * WorldEntity::UP, |
---|
733 | target ); |
---|
734 | |
---|
735 | float distance = ( target - this->getControllableEntity() ->getPosition() ).length(); |
---|
736 | |
---|
737 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
738 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
739 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
740 | |
---|
741 | |
---|
742 | if ( distance > tolerance ) |
---|
743 | { |
---|
744 | //Yaw and Pitch are enough to start facing the target |
---|
745 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
746 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
747 | |
---|
748 | //300 works, maybe less is better |
---|
749 | if ( distance < 400 ) |
---|
750 | { |
---|
751 | //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory |
---|
752 | //It's important that roll is not changed in the process of changing yaw and pitch |
---|
753 | //Wingmen won't face same direction as Leaders, but when Leaders start moving |
---|
754 | //Yaw and Pitch will adapt. |
---|
755 | if ( bHasTargetOrientation_ ) |
---|
756 | { |
---|
757 | copyTargetOrientation( dt ); |
---|
758 | } |
---|
759 | } |
---|
760 | |
---|
761 | this->getControllableEntity() ->moveFrontBack( SPEED * dt ); |
---|
762 | } |
---|
763 | else |
---|
764 | { |
---|
765 | bHasTargetPosition_ = false; |
---|
766 | bHasTargetOrientation_ = false; |
---|
767 | } |
---|
768 | } |
---|
769 | float CommonController::randomInRange( float a, float b ) |
---|
770 | { |
---|
771 | float random = rnd( 1.0f ); |
---|
772 | float diff = b - a; |
---|
773 | float r = random * diff; |
---|
774 | return a + r; |
---|
775 | } |
---|
776 | |
---|
777 | |
---|
778 | //to be called in action |
---|
779 | //PRE: relativeTargetPosition is desired position relative to the spaceship, |
---|
780 | //angleRoll is the angle in degrees of Roll that should be applied by the end of the movement |
---|
781 | //POST: target orientation and position are set, so that it can be used by MoveAndRoll() |
---|
782 | void CommonController::moveToPoint( const Vector3& relativeTargetPosition, float angleRoll ) |
---|
783 | { |
---|
784 | ControllableEntity* entity = this->getControllableEntity(); |
---|
785 | if ( !entity ) |
---|
786 | return; |
---|
787 | Quaternion orient = entity->getWorldOrientation(); |
---|
788 | Quaternion rotation = Quaternion( Degree( angleRoll ), Vector3::UNIT_Z ); |
---|
789 | |
---|
790 | Vector3 target = orient * relativeTargetPosition + entity->getWorldPosition(); |
---|
791 | setTargetPosition( target ); |
---|
792 | orient = orient * rotation; |
---|
793 | this->setTargetOrientation( orient ); |
---|
794 | |
---|
795 | } |
---|
796 | //to be called in tick |
---|
797 | //PRE: MoveToPoint was called |
---|
798 | //POST: spaceship changes its yaw and pitch to point towards targetPosition_, |
---|
799 | //moves towards targetPosition_ by amount depending on dt and its speed, |
---|
800 | //rolls by amount depending on the difference between angleRoll_ and angleRolled_, dt, and |
---|
801 | //angular speed |
---|
802 | //if position reached with a certain tolerance, and angleRolled_ = angleRoll_, returns false, |
---|
803 | //otherwise returns true |
---|
804 | //dt is normally around 0.02f, which makes it 1/0.02 = 50 frames/sec |
---|
805 | bool CommonController::moveAndRoll( float dt ) |
---|
806 | { |
---|
807 | float factor = 1; |
---|
808 | if ( !this->getControllableEntity() ) |
---|
809 | return false; |
---|
810 | if ( this->rank_ == Rank::DIVISIONLEADER ) |
---|
811 | factor = 0.8; |
---|
812 | if ( this->rank_ == Rank::SECTIONLEADER ) |
---|
813 | factor = 0.9; |
---|
814 | int tolerance = 60; |
---|
815 | |
---|
816 | ControllableEntity* entity = this->getControllableEntity(); |
---|
817 | if ( !entity ) |
---|
818 | return true; |
---|
819 | Vector2 coord = get2DViewCoordinates |
---|
820 | ( entity->getPosition() , |
---|
821 | entity->getOrientation() * WorldEntity::FRONT, |
---|
822 | entity->getOrientation() * WorldEntity::UP, |
---|
823 | targetPosition_ ); |
---|
824 | |
---|
825 | float distance = ( targetPosition_ - this->getControllableEntity() ->getPosition() ).length(); |
---|
826 | |
---|
827 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
828 | float rotateX = clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
829 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
830 | |
---|
831 | |
---|
832 | if ( distance > tolerance ) |
---|
833 | { |
---|
834 | //Yaw and Pitch are enough to start facing the target |
---|
835 | this->getControllableEntity() ->rotateYaw( -2.0f * ROTATEFACTOR * rotateX * dt ); |
---|
836 | this->getControllableEntity() ->rotatePitch( 2.0f * ROTATEFACTOR * rotateY * dt ); |
---|
837 | |
---|
838 | //Roll |
---|
839 | if ( bHasTargetOrientation_ ) |
---|
840 | { |
---|
841 | copyTargetOrientation( dt ); |
---|
842 | } |
---|
843 | |
---|
844 | //Move |
---|
845 | this->getControllableEntity() ->moveFrontBack( 1.2f * SPEED * factor * dt ); |
---|
846 | //if still moving, return false |
---|
847 | return false; |
---|
848 | } |
---|
849 | else |
---|
850 | { |
---|
851 | |
---|
852 | //if finished, return true; |
---|
853 | return true; |
---|
854 | } |
---|
855 | } |
---|
856 | |
---|
857 | float CommonController::squaredDistanceToTarget() const |
---|
858 | { |
---|
859 | if ( !this->getControllableEntity() ) |
---|
860 | return 0; |
---|
861 | if ( !this->target_ || !this->getControllableEntity() ) |
---|
862 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) ); |
---|
863 | else |
---|
864 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->positionOfTarget_ ) ); |
---|
865 | } |
---|
866 | |
---|
867 | bool CommonController::isLookingAtTarget( float angle )const |
---|
868 | { |
---|
869 | if ( !this->getControllableEntity() || !this->target_ ) |
---|
870 | return false; |
---|
871 | |
---|
872 | return ( getAngle( this->getControllableEntity() ->getPosition() , |
---|
873 | this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT, this->positionOfTarget_ ) < angle ); |
---|
874 | } |
---|
875 | bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) |
---|
876 | { |
---|
877 | if ( !entityThatLooks || !entityBeingLookedAt ) |
---|
878 | return false; |
---|
879 | return ( getAngle( entityThatLooks ->getPosition() , |
---|
880 | entityThatLooks->getOrientation() * WorldEntity::FRONT, |
---|
881 | entityBeingLookedAt->getWorldPosition() ) < angle ); |
---|
882 | } |
---|
883 | |
---|
884 | bool CommonController::canFire() |
---|
885 | { |
---|
886 | |
---|
887 | //no target? why fire? |
---|
888 | if ( !this->target_ ) |
---|
889 | return false; |
---|
890 | |
---|
891 | Vector3 newPositionOfTarget = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , |
---|
892 | hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() ); |
---|
893 | if ( newPositionOfTarget != Vector3::ZERO ) |
---|
894 | { |
---|
895 | this->setPositionOfTarget( newPositionOfTarget ); |
---|
896 | } |
---|
897 | |
---|
898 | float squaredDistance = squaredDistanceToTarget(); |
---|
899 | |
---|
900 | if ( squaredDistance < 9000000.0f && this->isLookingAtTarget( math::pi / 20.0f)) { |
---|
901 | return true; |
---|
902 | } |
---|
903 | else |
---|
904 | { |
---|
905 | return false; |
---|
906 | } |
---|
907 | |
---|
908 | } |
---|
909 | float CommonController::distance (ControllableEntity* entity1, ControllableEntity* entity2) |
---|
910 | { |
---|
911 | if (!entity1 || !entity2) |
---|
912 | return std::numeric_limits<float>::infinity(); |
---|
913 | return ( entity1->getPosition() - entity2->getPosition() ).length(); |
---|
914 | } |
---|
915 | bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
916 | { |
---|
917 | /*if (!entity1 || !entity2) |
---|
918 | return false; |
---|
919 | return entity1->getTeam() == entity2->getTeam();*/ |
---|
920 | if (entity1 == entity2) |
---|
921 | return true; |
---|
922 | |
---|
923 | int team1 = entity1->getTeam(); |
---|
924 | int team2 = entity2->getTeam(); |
---|
925 | |
---|
926 | Controller* controller = 0; |
---|
927 | if (entity1->getController()) |
---|
928 | controller = entity1->getController(); |
---|
929 | else |
---|
930 | controller = entity1->getXMLController(); |
---|
931 | if (controller) |
---|
932 | { |
---|
933 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
934 | if (ac) |
---|
935 | team1 = ac->getTeam(); |
---|
936 | } |
---|
937 | |
---|
938 | if (entity2->getController()) |
---|
939 | controller = entity2->getController(); |
---|
940 | else |
---|
941 | controller = entity2->getXMLController(); |
---|
942 | if (controller) |
---|
943 | { |
---|
944 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
945 | if (ac) |
---|
946 | team2 = ac->getTeam(); |
---|
947 | } |
---|
948 | |
---|
949 | TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype); |
---|
950 | if (tdm) |
---|
951 | { |
---|
952 | if (entity1->getPlayer()) |
---|
953 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
954 | |
---|
955 | if (entity2->getPlayer()) |
---|
956 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
957 | } |
---|
958 | |
---|
959 | TeamBaseMatchBase* base = 0; |
---|
960 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
961 | if (base) |
---|
962 | { |
---|
963 | switch (base->getState()) |
---|
964 | { |
---|
965 | case BaseState::ControlTeam1: |
---|
966 | team1 = 0; |
---|
967 | break; |
---|
968 | case BaseState::ControlTeam2: |
---|
969 | team1 = 1; |
---|
970 | break; |
---|
971 | case BaseState::Uncontrolled: |
---|
972 | default: |
---|
973 | team1 = -1; |
---|
974 | } |
---|
975 | } |
---|
976 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
977 | if (base) |
---|
978 | { |
---|
979 | switch (base->getState()) |
---|
980 | { |
---|
981 | case BaseState::ControlTeam1: |
---|
982 | team2 = 0; |
---|
983 | break; |
---|
984 | case BaseState::ControlTeam2: |
---|
985 | team2 = 1; |
---|
986 | break; |
---|
987 | case BaseState::Uncontrolled: |
---|
988 | default: |
---|
989 | team2 = -1; |
---|
990 | } |
---|
991 | } |
---|
992 | |
---|
993 | DroneController* droneController = 0; |
---|
994 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
995 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
996 | return true; |
---|
997 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
998 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
999 | return true; |
---|
1000 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
1001 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
1002 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
1003 | return true; |
---|
1004 | |
---|
1005 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
1006 | if (dynamic) |
---|
1007 | { |
---|
1008 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
1009 | |
---|
1010 | if (entity1->getPlayer()) |
---|
1011 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
1012 | |
---|
1013 | if (entity2->getPlayer()) |
---|
1014 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
1015 | |
---|
1016 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
---|
1017 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
1018 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
1019 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
1020 | else return true; |
---|
1021 | } |
---|
1022 | |
---|
1023 | return (team1 == team2 && team1 != -1); |
---|
1024 | } |
---|
1025 | void CommonController::doFire() |
---|
1026 | { |
---|
1027 | if ( !this->target_ || !this->getControllableEntity() ) |
---|
1028 | { |
---|
1029 | return; |
---|
1030 | } |
---|
1031 | |
---|
1032 | Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() ); |
---|
1033 | |
---|
1034 | if ( pawn ) |
---|
1035 | //pawn->setAimPosition( this->getControllableEntity() ->getWorldPosition() + 4000*( this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT )); |
---|
1036 | pawn->setAimPosition( this->positionOfTarget_ ); |
---|
1037 | |
---|
1038 | this->getControllableEntity() ->fire( 0 ); |
---|
1039 | } |
---|
1040 | |
---|
1041 | |
---|
1042 | } |
---|