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/CommonController.h" |
---|
29 | #include "core/XMLPort.h" |
---|
30 | |
---|
31 | #include "weaponsystem/WeaponMode.h" |
---|
32 | #include "weaponsystem/WeaponPack.h" |
---|
33 | #include "weaponsystem/Weapon.h" |
---|
34 | #include "weaponsystem/WeaponSlot.h" |
---|
35 | #include "weaponsystem/WeaponSlot.h" |
---|
36 | #include "worldentities/pawns/SpaceShip.h" |
---|
37 | |
---|
38 | #include "Scene.h" |
---|
39 | #include <OgreRay.h> |
---|
40 | #include <OgreSceneQuery.h> |
---|
41 | #include <OgreCamera.h> |
---|
42 | #include <OgreSceneManager.h> |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | |
---|
46 | RegisterClass(CommonController); |
---|
47 | float SPEED = 0.7f/0.02f; |
---|
48 | float ROTATEFACTOR = 0.3f/0.02f; |
---|
49 | |
---|
50 | CommonController::CommonController(Context* context) : Controller(context) |
---|
51 | { |
---|
52 | this->bSetupWorked = false; |
---|
53 | |
---|
54 | this->executingManeuver_ = false; |
---|
55 | this->executingMoveToPoint_ = false; |
---|
56 | |
---|
57 | this->maneuverType_ = ManeuverType::NONE; |
---|
58 | RegisterObject(CommonController); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | CommonController::~CommonController() |
---|
63 | { |
---|
64 | } |
---|
65 | |
---|
66 | void CommonController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
67 | { |
---|
68 | SUPER(CommonController, XMLPort, xmlelement, mode); |
---|
69 | XMLPortParam(CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode); |
---|
70 | |
---|
71 | } |
---|
72 | void CommonController::setFormationModeXML(std::string val) |
---|
73 | { |
---|
74 | const std::string valUpper = getUppercase(val); |
---|
75 | FormationMode::Value value; |
---|
76 | if (valUpper == "VEE") |
---|
77 | value = FormationMode::VEE; |
---|
78 | else if (valUpper == "WALL") |
---|
79 | value = FormationMode::WALL; |
---|
80 | else if (valUpper == "FINGER4") |
---|
81 | value = FormationMode::FINGER4; |
---|
82 | else if (valUpper == "DIAMOND") |
---|
83 | value = FormationMode::DIAMOND; |
---|
84 | else |
---|
85 | ThrowException(ParseError, std::string("Attempting to set an unknown FormationMode: '") + val + "'."); |
---|
86 | this->setFormationMode(value); |
---|
87 | |
---|
88 | } |
---|
89 | std::string CommonController::getFormationModeXML() |
---|
90 | { |
---|
91 | switch (this->formationMode_) |
---|
92 | { |
---|
93 | case FormationMode::VEE: |
---|
94 | { |
---|
95 | return "VEE"; |
---|
96 | break; |
---|
97 | } |
---|
98 | case FormationMode::WALL: |
---|
99 | { |
---|
100 | return "WALL"; |
---|
101 | break; |
---|
102 | } |
---|
103 | case FormationMode::FINGER4: |
---|
104 | { |
---|
105 | return "FINGER4"; |
---|
106 | break; |
---|
107 | } |
---|
108 | case FormationMode::DIAMOND: |
---|
109 | { |
---|
110 | return "DIAMOND"; |
---|
111 | break; |
---|
112 | } |
---|
113 | default: |
---|
114 | return "DIAMOND"; |
---|
115 | break; |
---|
116 | |
---|
117 | } |
---|
118 | } |
---|
119 | void CommonController::maneuver() |
---|
120 | { |
---|
121 | |
---|
122 | if (this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity()) |
---|
123 | { |
---|
124 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
125 | float diffLength = diffVector.length(); |
---|
126 | Vector3 diffUnit = diffVector/diffLength; |
---|
127 | |
---|
128 | Vector3 myForwardVector = this->getControllableEntity()->getOrientation() * WorldEntity::FRONT; |
---|
129 | float myDotProduct = diffVector.dotProduct(myForwardVector); |
---|
130 | |
---|
131 | Vector3 opponentForwardVector = this->target_->getOrientation() * WorldEntity::FRONT; |
---|
132 | float opponentDotProduct = diffVector.dotProduct(opponentForwardVector); |
---|
133 | |
---|
134 | float myAngle = math::arccos ( myForwardVector.dotProduct(diffVector)/(diffLength) ); |
---|
135 | float targetAngle = math::arccos ( opponentForwardVector.dotProduct(-diffVector)/(diffLength) ); |
---|
136 | |
---|
137 | bool bThisIsLookingAtTarget = (myAngle/(diffLength*diffLength) < math::pi/8000000.0f); |
---|
138 | bool bTargetIsLookingAtThis = (targetAngle/(diffLength*diffLength) < math::pi/8000000.0f); |
---|
139 | |
---|
140 | float angleDiff = targetAngle - myAngle; |
---|
141 | //I am looking when my angle < pi/6 |
---|
142 | //if his angle is bigger than mine |
---|
143 | if ( angleDiff > 0 ) |
---|
144 | { |
---|
145 | //if diff is insignificant |
---|
146 | if ( bThisIsLookingAtTarget && bTargetIsLookingAtThis ) |
---|
147 | { |
---|
148 | //if this can make target overshoot |
---|
149 | if ( diffLength < 200 ) |
---|
150 | { |
---|
151 | Vector3* target = new Vector3 ( 0, -200, -200 ); |
---|
152 | moveToPoint( |
---|
153 | *target, |
---|
154 | randomInRange(45, 180) |
---|
155 | ); |
---|
156 | } |
---|
157 | //do scissors |
---|
158 | else |
---|
159 | { |
---|
160 | Vector3 target = (diffUnit) * 150.0f |
---|
161 | Vector3* randVector = new Vector3( |
---|
162 | randomInRange(-300, 300), |
---|
163 | randomInRange(-300, 300), |
---|
164 | randomInRange(-300, 300) |
---|
165 | ); |
---|
166 | Vector3 projection = randVector.dotProduct(diffUnit) * diffUnit; |
---|
167 | *randVector -= projection; |
---|
168 | target += randVector; |
---|
169 | moveToPoint( |
---|
170 | *target, |
---|
171 | randomInRange(45, 180) |
---|
172 | ); |
---|
173 | } |
---|
174 | } |
---|
175 | //this has advantage |
---|
176 | else |
---|
177 | { |
---|
178 | |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | } |
---|
183 | if (this->getControllableEntity() && !this->target_) |
---|
184 | { |
---|
185 | this->maneuverType_ = ManeuverType::NONE; |
---|
186 | } |
---|
187 | orxout (internal_error) << "ManeuverType = " << this->maneuverType_ << endl; |
---|
188 | } |
---|
189 | void CommonController::chooseManeuverType() |
---|
190 | { |
---|
191 | |
---|
192 | if (this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity()) |
---|
193 | { |
---|
194 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
195 | Vector3 myForwardVector = this->getControllableEntity()->getOrientation() * WorldEntity::FRONT; |
---|
196 | float myDotProduct = diffVector.dotProduct(myForwardVector); |
---|
197 | |
---|
198 | Vector3 opponentForwardVector = this->target_->getOrientation() * WorldEntity::FRONT; |
---|
199 | float opponentDotProduct = diffVector.dotProduct(opponentForwardVector); |
---|
200 | |
---|
201 | |
---|
202 | switch ((myDotProduct > 0) - (myDotProduct < 0)) |
---|
203 | { |
---|
204 | case 1: |
---|
205 | { |
---|
206 | switch ((opponentDotProduct > 0) - (opponentDotProduct < 0)) |
---|
207 | { |
---|
208 | case 1: |
---|
209 | { |
---|
210 | this->maneuverType_ = ManeuverType::OFFENSIVE; |
---|
211 | break; |
---|
212 | } |
---|
213 | case 0: |
---|
214 | { |
---|
215 | this->maneuverType_ = ManeuverType::OFFENSIVE; |
---|
216 | break; |
---|
217 | } |
---|
218 | case -1: |
---|
219 | { |
---|
220 | this->maneuverType_ = ManeuverType::NEUTRAL; |
---|
221 | break; |
---|
222 | } |
---|
223 | } |
---|
224 | break; |
---|
225 | } |
---|
226 | case 0: |
---|
227 | { |
---|
228 | switch ((opponentDotProduct > 0) - (opponentDotProduct < 0)) |
---|
229 | { |
---|
230 | case 1: |
---|
231 | { |
---|
232 | this->maneuverType_ = ManeuverType::OFFENSIVE; |
---|
233 | break; |
---|
234 | } |
---|
235 | case 0: |
---|
236 | { |
---|
237 | this->maneuverType_ = ManeuverType::NEUTRAL; |
---|
238 | break; |
---|
239 | } |
---|
240 | case -1: |
---|
241 | { |
---|
242 | this->maneuverType_ = ManeuverType::DEFENCIVE; |
---|
243 | break; |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | break; |
---|
248 | } |
---|
249 | case -1: |
---|
250 | { |
---|
251 | switch ((opponentDotProduct > 0) - (opponentDotProduct < 0)) |
---|
252 | { |
---|
253 | case 1: |
---|
254 | { |
---|
255 | this->maneuverType_ = ManeuverType::NEUTRAL; |
---|
256 | break; |
---|
257 | } |
---|
258 | case 0: |
---|
259 | { |
---|
260 | this->maneuverType_ = ManeuverType::DEFENCIVE; |
---|
261 | break; |
---|
262 | } |
---|
263 | case -1: |
---|
264 | { |
---|
265 | this->maneuverType_ = ManeuverType::DEFENCIVE; |
---|
266 | break; |
---|
267 | } |
---|
268 | } |
---|
269 | break; |
---|
270 | } |
---|
271 | } |
---|
272 | } |
---|
273 | if (this->getControllableEntity() && !this->target_) |
---|
274 | { |
---|
275 | this->maneuverType_ = ManeuverType::NONE; |
---|
276 | } |
---|
277 | orxout (internal_error) << "ManeuverType = " << this->maneuverType_ << endl; |
---|
278 | } |
---|
279 | bool CommonController::setWingman (CommonController* wingman) |
---|
280 | { |
---|
281 | return false; |
---|
282 | } |
---|
283 | |
---|
284 | bool CommonController::hasWingman() |
---|
285 | { |
---|
286 | return true; |
---|
287 | } |
---|
288 | void CommonController::setTarget(ControllableEntity* target) |
---|
289 | { |
---|
290 | this->target_ = target; |
---|
291 | orxout (internal_error) << " TARGET SET " << endl; |
---|
292 | |
---|
293 | if (this->target_ ) |
---|
294 | { |
---|
295 | this->setPositionOfTarget(target_->getWorldPosition()); |
---|
296 | |
---|
297 | } |
---|
298 | } |
---|
299 | bool CommonController::hasTarget() |
---|
300 | { |
---|
301 | if (this->target_) |
---|
302 | return true; |
---|
303 | return false; |
---|
304 | } |
---|
305 | void CommonController::setPositionOfTarget(const Vector3& target) |
---|
306 | { |
---|
307 | this->positionOfTarget_ = target; |
---|
308 | this->bHasPositionOfTarget_ = true; |
---|
309 | } |
---|
310 | void CommonController::setOrientationOfTarget(const Quaternion& orient) |
---|
311 | { |
---|
312 | this->orientationOfTarget_=orient; |
---|
313 | this->bHasOrientationOfTarget_=true; |
---|
314 | } |
---|
315 | |
---|
316 | void CommonController::setTargetPosition(const Vector3& target) |
---|
317 | { |
---|
318 | this->targetPosition_ = target; |
---|
319 | this->bHasTargetPosition_ = true; |
---|
320 | } |
---|
321 | |
---|
322 | void CommonController::setTargetOrientation(const Quaternion& orient) |
---|
323 | { |
---|
324 | this->targetOrientation_=orient; |
---|
325 | this->bHasTargetOrientation_=true; |
---|
326 | } |
---|
327 | |
---|
328 | void CommonController::setTargetOrientation(ControllableEntity* target) |
---|
329 | { |
---|
330 | if (target) |
---|
331 | setTargetOrientation(target->getOrientation()); |
---|
332 | } |
---|
333 | |
---|
334 | /*void CommonController::spin() |
---|
335 | { |
---|
336 | this->moveToTargetPosition(); |
---|
337 | this->getControllableEntity()->rotateRoll(8.0f); |
---|
338 | } |
---|
339 | void CommonController::turn180() |
---|
340 | { |
---|
341 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); |
---|
342 | |
---|
343 | this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); |
---|
344 | this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); |
---|
345 | |
---|
346 | this->getControllableEntity()->moveFrontBack(SPEED); |
---|
347 | }*/ |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | //copy the Roll orientation of given Quaternion. |
---|
352 | void CommonController::copyOrientation(const Quaternion& orient, float dt) |
---|
353 | { |
---|
354 | //roll angle difference in radian |
---|
355 | float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians()); |
---|
356 | while(diff>math::twoPi) diff-=math::twoPi; |
---|
357 | while(diff<-math::twoPi) diff+=math::twoPi; |
---|
358 | this->getControllableEntity()->rotateRoll(diff*ROTATEFACTOR * dt); |
---|
359 | } |
---|
360 | void CommonController::copyTargetOrientation(float dt) |
---|
361 | { |
---|
362 | if (bHasTargetOrientation_) |
---|
363 | { |
---|
364 | copyOrientation(targetOrientation_, dt); |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | |
---|
371 | void CommonController::moveToTargetPosition(float dt) |
---|
372 | { |
---|
373 | this->moveToPosition(this->targetPosition_, dt); |
---|
374 | } |
---|
375 | void CommonController::moveToPosition(const Vector3& target, float dt) |
---|
376 | { |
---|
377 | float factor = 1; |
---|
378 | if (!this->getControllableEntity()) |
---|
379 | return; |
---|
380 | if (this->rank_ == Rank::DIVISIONLEADER) |
---|
381 | factor = 0.8; |
---|
382 | if (this->rank_ == Rank::SECTIONLEADER) |
---|
383 | factor = 0.9; |
---|
384 | |
---|
385 | //100 is (so far) the smallest tolerance (empirically found) that can be reached, |
---|
386 | //with smaller distance spaceships can't reach position and go circles around it instead |
---|
387 | int tolerance = 60; |
---|
388 | |
---|
389 | ControllableEntity* entity = this->getControllableEntity(); |
---|
390 | Vector2 coord = get2DViewCoordinates |
---|
391 | (entity->getPosition(), |
---|
392 | entity->getOrientation() * WorldEntity::FRONT, |
---|
393 | entity->getOrientation() * WorldEntity::UP, |
---|
394 | target); |
---|
395 | |
---|
396 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
397 | |
---|
398 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
399 | float rotateX = clamp(coord.x * 10, -1.0f, 1.0f); |
---|
400 | float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); |
---|
401 | |
---|
402 | |
---|
403 | if (distance > tolerance) |
---|
404 | { |
---|
405 | //Yaw and Pitch are enough to start facing the target |
---|
406 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX * dt); |
---|
407 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY * dt); |
---|
408 | |
---|
409 | //300 works, maybe less is better |
---|
410 | if (distance < 400) |
---|
411 | { |
---|
412 | //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory |
---|
413 | //It's important that roll is not changed in the process of changing yaw and pitch |
---|
414 | //Wingmen won't face same direction as Leaders, but when Leaders start moving |
---|
415 | //Yaw and Pitch will adapt. |
---|
416 | if (bHasTargetOrientation_) |
---|
417 | { |
---|
418 | copyTargetOrientation(dt); |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED*factor * dt); |
---|
423 | } |
---|
424 | else |
---|
425 | { |
---|
426 | bHasTargetPosition_ = false; |
---|
427 | bHasTargetOrientation_ = false; |
---|
428 | } |
---|
429 | } |
---|
430 | float CommonController::randomInRange(float a, float b) |
---|
431 | { |
---|
432 | float random = rnd(1.0f); |
---|
433 | float diff = b - a; |
---|
434 | float r = random * diff; |
---|
435 | return a + r; |
---|
436 | } |
---|
437 | void CommonController::attack() |
---|
438 | { |
---|
439 | if ( !this->getControllableEntity() ) |
---|
440 | return; |
---|
441 | if ( this->target_ ) |
---|
442 | { |
---|
443 | this->positionOfTarget_ = getPredictedPosition( |
---|
444 | this->getControllableEntity()->getWorldPosition(), |
---|
445 | hardcoded_projectile_speed, |
---|
446 | this->target_->getWorldPosition(), |
---|
447 | this->target_->getVelocity() |
---|
448 | ); |
---|
449 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
450 | float diffLength = diffVector.length(); |
---|
451 | if (diffLength < 100) |
---|
452 | { |
---|
453 | Vector3* targetPosition; |
---|
454 | targetPosition = new Vector3 ( |
---|
455 | //randomInRange(200, 300), |
---|
456 | 0, |
---|
457 | //randomInRange(-300, -200), |
---|
458 | 0, |
---|
459 | randomInRange(-300, -400) |
---|
460 | ); |
---|
461 | Quaternion rotationToTarget = (this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector); |
---|
462 | Vector3 target = rotationToTarget * (*targetPosition); |
---|
463 | moveToPoint( |
---|
464 | target, |
---|
465 | randomInRange(45, 180) |
---|
466 | ); |
---|
467 | executingMoveToPoint_ = true; |
---|
468 | return; |
---|
469 | } |
---|
470 | this->bShooting_ = true; |
---|
471 | this->positionOfTarget_ = getPredictedPosition( |
---|
472 | this->getControllableEntity()->getWorldPosition(), |
---|
473 | hardcoded_projectile_speed, |
---|
474 | this->target_->getWorldPosition(), |
---|
475 | this->target_->getVelocity() |
---|
476 | ); |
---|
477 | this->targetPosition_ = positionOfTarget_; |
---|
478 | |
---|
479 | } |
---|
480 | else |
---|
481 | { |
---|
482 | this->chooseManeuverType(); |
---|
483 | } |
---|
484 | } |
---|
485 | void CommonController::scissors() |
---|
486 | { |
---|
487 | if ( !this->getControllableEntity() ) |
---|
488 | return; |
---|
489 | if ( this->target_ ) |
---|
490 | { |
---|
491 | this->positionOfTarget_ = getPredictedPosition( |
---|
492 | this->getControllableEntity()->getWorldPosition(), |
---|
493 | hardcoded_projectile_speed, |
---|
494 | this->target_->getWorldPosition(), |
---|
495 | this->target_->getVelocity() |
---|
496 | ); |
---|
497 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
498 | float diffLength = diffVector.length(); |
---|
499 | Vector3 opponentForwardVector = this->target_->getOrientation() * WorldEntity::FRONT; |
---|
500 | float opponentDotProduct = diffVector.dotProduct(opponentForwardVector); |
---|
501 | |
---|
502 | int f = (int) rnd(100.0f); |
---|
503 | f = (f % 2 == 0 ? 1 : -1); |
---|
504 | |
---|
505 | if(!this->executingMoveToPoint_) |
---|
506 | { |
---|
507 | Vector3* targetPosition; |
---|
508 | if ( diffLength < 100 ) |
---|
509 | { |
---|
510 | targetPosition = new Vector3 ( |
---|
511 | //f * randomInRange(200, 300), |
---|
512 | 0, |
---|
513 | //f * randomInRange(-300, -200), |
---|
514 | 0, |
---|
515 | //randomInRange(-300, -400) |
---|
516 | 0 |
---|
517 | ); |
---|
518 | } |
---|
519 | else |
---|
520 | { |
---|
521 | if ( opponentDotProduct < 0 ) |
---|
522 | { |
---|
523 | targetPosition = new Vector3 ( |
---|
524 | //f * randomInRange(200, 300), |
---|
525 | 0, |
---|
526 | //f * randomInRange(-300, -200), |
---|
527 | 0, |
---|
528 | //randomInRange(-300, -400) |
---|
529 | -300 |
---|
530 | ); |
---|
531 | } |
---|
532 | else |
---|
533 | { |
---|
534 | targetPosition = new Vector3 ( |
---|
535 | //f * randomInRange(200, 300), |
---|
536 | 0, |
---|
537 | //f * randomInRange(-300, -200), |
---|
538 | 0, |
---|
539 | //randomInRange(-300, -400) |
---|
540 | 300 |
---|
541 | ); |
---|
542 | } |
---|
543 | } |
---|
544 | Quaternion rotationToTarget = (this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector); |
---|
545 | Vector3 target = rotationToTarget * (*targetPosition); |
---|
546 | moveToPoint( |
---|
547 | target, |
---|
548 | randomInRange(45, 180) |
---|
549 | ); |
---|
550 | executingMoveToPoint_ = true; |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | else |
---|
555 | { |
---|
556 | this->chooseManeuverType(); |
---|
557 | } |
---|
558 | } |
---|
559 | void CommonController::gunsD() |
---|
560 | { |
---|
561 | if ( !this->getControllableEntity() ) |
---|
562 | return; |
---|
563 | if ( this->target_ ) |
---|
564 | { |
---|
565 | this->positionOfTarget_ = getPredictedPosition( |
---|
566 | this->getControllableEntity()->getWorldPosition(), |
---|
567 | hardcoded_projectile_speed, |
---|
568 | this->target_->getWorldPosition(), |
---|
569 | this->target_->getVelocity() |
---|
570 | ); |
---|
571 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
572 | float diffLength = diffVector.length(); |
---|
573 | if(!this->executingMoveToPoint_) |
---|
574 | { |
---|
575 | Vector3* targetPosition; |
---|
576 | if ( diffLength < 200 ) |
---|
577 | { |
---|
578 | targetPosition = new Vector3 ( |
---|
579 | //f * randomInRange(200, 300), |
---|
580 | 0, |
---|
581 | //f * randomInRange(-300, -200), |
---|
582 | 0, |
---|
583 | //randomInRange(-300, -400) |
---|
584 | 0 |
---|
585 | ); |
---|
586 | } |
---|
587 | else if ( diffLength < 500 ) |
---|
588 | { |
---|
589 | targetPosition = new Vector3 ( |
---|
590 | //randomInRange(100, 200), |
---|
591 | 0, |
---|
592 | //randomInRange(-200, -100), |
---|
593 | 0, |
---|
594 | //randomInRange(-400, -600) |
---|
595 | 500 |
---|
596 | ); |
---|
597 | } |
---|
598 | else |
---|
599 | { |
---|
600 | targetPosition = new Vector3 ( |
---|
601 | //randomInRange(200, 300), |
---|
602 | 0, |
---|
603 | //randomInRange(-300, -200), |
---|
604 | 0, |
---|
605 | //randomInRange(-400, -600) |
---|
606 | 500 |
---|
607 | ); |
---|
608 | } |
---|
609 | Quaternion rotationToTarget = (this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector); |
---|
610 | Vector3 target = rotationToTarget * (*targetPosition); |
---|
611 | moveToPoint( |
---|
612 | target, |
---|
613 | randomInRange(45, 180) |
---|
614 | ); |
---|
615 | executingMoveToPoint_ = true; |
---|
616 | } |
---|
617 | } |
---|
618 | else |
---|
619 | { |
---|
620 | this->chooseManeuverType(); |
---|
621 | } |
---|
622 | } |
---|
623 | //to be called in action |
---|
624 | //PRE: relativeTargetPosition is desired position relative to the spaceship, |
---|
625 | //angleRoll is the angle in degrees of Roll that should be applied by the end of the movement |
---|
626 | //POST: target orientation and position are set, so that it can be used by MoveAndRoll() |
---|
627 | void CommonController::moveToPoint(const Vector3& relativeTargetPosition, float angleRoll) |
---|
628 | { |
---|
629 | ControllableEntity* entity = this->getControllableEntity(); |
---|
630 | if (!entity) |
---|
631 | return; |
---|
632 | Quaternion orient = entity->getWorldOrientation(); |
---|
633 | Quaternion rotation = Quaternion(Degree(angleRoll), Vector3::UNIT_Z); |
---|
634 | |
---|
635 | Vector3 target = orient * relativeTargetPosition + entity->getWorldPosition(); |
---|
636 | setTargetPosition(target); |
---|
637 | orient = orient * rotation; |
---|
638 | this->setTargetOrientation(orient); |
---|
639 | |
---|
640 | } |
---|
641 | //to be called in tick |
---|
642 | //PRE: MoveToPoint was called |
---|
643 | //POST: spaceship changes its yaw and pitch to point towards targetPosition_, |
---|
644 | //moves towards targetPosition_ by amount depending on dt and its speed, |
---|
645 | //rolls by amount depending on the difference between angleRoll_ and angleRolled_, dt, and |
---|
646 | //angular speed |
---|
647 | //if position reached with a certain tolerance, and angleRolled_ = angleRoll_, returns false, |
---|
648 | //otherwise returns true |
---|
649 | //dt is normally around 0.02f, which makes it 1/0.02 = 50 frames/sec |
---|
650 | bool CommonController::moveAndRoll(float dt) |
---|
651 | { |
---|
652 | float factor = 1; |
---|
653 | if (!this->getControllableEntity()) |
---|
654 | return false; |
---|
655 | if (this->rank_ == Rank::DIVISIONLEADER) |
---|
656 | factor = 0.8; |
---|
657 | if (this->rank_ == Rank::SECTIONLEADER) |
---|
658 | factor = 0.9; |
---|
659 | int tolerance = 60; |
---|
660 | |
---|
661 | ControllableEntity* entity = this->getControllableEntity(); |
---|
662 | if (!entity) |
---|
663 | return true; |
---|
664 | Vector2 coord = get2DViewCoordinates |
---|
665 | (entity->getPosition(), |
---|
666 | entity->getOrientation() * WorldEntity::FRONT, |
---|
667 | entity->getOrientation() * WorldEntity::UP, |
---|
668 | targetPosition_); |
---|
669 | |
---|
670 | float distance = (targetPosition_ - this->getControllableEntity()->getPosition()).length(); |
---|
671 | |
---|
672 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
673 | float rotateX = clamp(coord.x * 10, -1.0f, 1.0f); |
---|
674 | float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); |
---|
675 | |
---|
676 | |
---|
677 | if (distance > tolerance) |
---|
678 | { |
---|
679 | //Yaw and Pitch are enough to start facing the target |
---|
680 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX * dt); |
---|
681 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY * dt); |
---|
682 | |
---|
683 | //Roll |
---|
684 | if (bHasTargetOrientation_) |
---|
685 | { |
---|
686 | copyTargetOrientation(dt); |
---|
687 | } |
---|
688 | |
---|
689 | //Move |
---|
690 | this->getControllableEntity()->moveFrontBack(1.2f * SPEED * factor * dt); |
---|
691 | //if still moving, return false |
---|
692 | return false; |
---|
693 | } |
---|
694 | else |
---|
695 | { |
---|
696 | |
---|
697 | //if finished, return true; |
---|
698 | return true; |
---|
699 | } |
---|
700 | } |
---|
701 | |
---|
702 | float CommonController::squaredDistanceToTarget() const |
---|
703 | { |
---|
704 | if ( !this->getControllableEntity() ) |
---|
705 | return 0; |
---|
706 | if ( !this->target_ ) |
---|
707 | return ( this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) ); |
---|
708 | else |
---|
709 | return ( this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) ); |
---|
710 | } |
---|
711 | |
---|
712 | bool CommonController::isLookingAtTarget(float angle) const |
---|
713 | { |
---|
714 | if (!this->getControllableEntity()) |
---|
715 | return false; |
---|
716 | |
---|
717 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
718 | } |
---|
719 | |
---|
720 | bool CommonController::canFire() |
---|
721 | { |
---|
722 | float squaredDistance = squaredDistanceToTarget(); |
---|
723 | if ( this->bShooting_ && squaredDistance < 9000000 && squaredDistance > 10000 && this->isLookingAtTarget(math::pi /(0.0002f*squaredDistance)) ) |
---|
724 | { |
---|
725 | return true; |
---|
726 | } |
---|
727 | else |
---|
728 | { |
---|
729 | return false; |
---|
730 | } |
---|
731 | |
---|
732 | } |
---|
733 | void CommonController::doFire() |
---|
734 | { |
---|
735 | if (!this->target_ || !this->getControllableEntity()) |
---|
736 | return; |
---|
737 | static const float hardcoded_projectile_speed = 750; |
---|
738 | |
---|
739 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity()); |
---|
740 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
741 | |
---|
742 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
743 | |
---|
744 | if (pawn) |
---|
745 | //pawn->setAimPosition(this->getControllableEntity()->getWorldPosition() + 4000*(this->getControllableEntity()->getOrientation() * WorldEntity::FRONT)); |
---|
746 | pawn->setAimPosition(this->targetPosition_); |
---|
747 | |
---|
748 | this->getControllableEntity()->fire(0); |
---|
749 | } |
---|
750 | |
---|
751 | |
---|
752 | } |
---|