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 | * ... |
---|
24 | * Co-authors: |
---|
25 | * Dominik Solenicki |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include <climits> |
---|
30 | #include "controllers/FormationController.h" |
---|
31 | |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | |
---|
34 | #include "core/XMLPort.h" |
---|
35 | #include "core/command/ConsoleCommand.h" |
---|
36 | |
---|
37 | #include "worldentities/ControllableEntity.h" |
---|
38 | #include "worldentities/pawns/Pawn.h" |
---|
39 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
40 | #include "gametypes/TeamDeathmatch.h" |
---|
41 | #include "gametypes/Dynamicmatch.h" |
---|
42 | #include "gametypes/Mission.h" |
---|
43 | #include "gametypes/Gametype.h" |
---|
44 | #include "controllers/WaypointPatrolController.h" |
---|
45 | #include "controllers/NewHumanController.h" |
---|
46 | #include "controllers/DroneController.h" |
---|
47 | |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | |
---|
52 | SetConsoleCommand("FormationController", "formationflight", &FormationController::formationflight); |
---|
53 | SetConsoleCommand("FormationController", "masteraction", &FormationController::masteraction); |
---|
54 | SetConsoleCommand("FormationController", "followme", &FormationController::followme); |
---|
55 | SetConsoleCommand("FormationController", "passivebehaviour", &FormationController::passivebehaviour); |
---|
56 | SetConsoleCommand("FormationController", "formationsize", &FormationController::formationsize); |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | static const unsigned int STANDARD_MAX_FORMATION_SIZE = 9; |
---|
62 | static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000; |
---|
63 | static const float FORMATION_LENGTH = 110; |
---|
64 | static const float FORMATION_WIDTH = 110; |
---|
65 | static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy |
---|
66 | static const float SPEED_MASTER = 0.6f; |
---|
67 | static const float ROTATEFACTOR_MASTER = 0.2f; |
---|
68 | static const float SPEED_FREE = 0.8f; |
---|
69 | static const float ROTATEFACTOR_FREE = 0.8f; |
---|
70 | |
---|
71 | FormationController::FormationController(BaseObject* creator) : Controller(creator) |
---|
72 | { |
---|
73 | RegisterObject(FormationController); |
---|
74 | |
---|
75 | this->target_ = 0; |
---|
76 | this->formationFlight_ = false; |
---|
77 | this->passive_ = false; |
---|
78 | this->maxFormationSize_ = STANDARD_MAX_FORMATION_SIZE; |
---|
79 | this->myMaster_ = 0; |
---|
80 | this->freedomCount_ = 0; |
---|
81 | |
---|
82 | this->state_ = FREE; |
---|
83 | this->formationMode_ = NORMAL; |
---|
84 | this->specificMasterAction_ = NONE; |
---|
85 | this->specificMasterActionHoldCount_ = 0; |
---|
86 | this->bShooting_ = false; |
---|
87 | this->bHasTargetPosition_ = false; |
---|
88 | this->bHasTargetOrientation_=false; |
---|
89 | this->speedCounter_ = 0.2f; |
---|
90 | this->targetPosition_ = Vector3::ZERO; |
---|
91 | this->team_=-1; |
---|
92 | this->target_.setCallback(createFunctor(&FormationController::targetDied, this)); |
---|
93 | } |
---|
94 | |
---|
95 | FormationController::~FormationController() |
---|
96 | { |
---|
97 | if (this->isInitialized()) |
---|
98 | { |
---|
99 | this->removeFromFormation(); |
---|
100 | |
---|
101 | for (ObjectList<FormationController>::iterator it = ObjectList<FormationController>::begin(); it; ++it) |
---|
102 | { |
---|
103 | if (*it != this) |
---|
104 | { |
---|
105 | if (it->myMaster_ == this) |
---|
106 | { |
---|
107 | orxout(internal_error) << this << " is still master in " << (*it) << endl; |
---|
108 | it->myMaster_ = 0; |
---|
109 | } |
---|
110 | |
---|
111 | while (true) |
---|
112 | { |
---|
113 | std::vector<FormationController*>::iterator it2 = std::find(it->slaves_.begin(), it->slaves_.end(), this); |
---|
114 | if (it2 != it->slaves_.end()) |
---|
115 | { |
---|
116 | orxout(internal_error) << this << " is still slave in " << (*it) << endl; |
---|
117 | it->slaves_.erase(it2); |
---|
118 | } |
---|
119 | else |
---|
120 | break; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | void FormationController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
128 | { |
---|
129 | SUPER(FormationController, XMLPort, xmlelement, mode); |
---|
130 | |
---|
131 | XMLPortParam(FormationController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1); |
---|
132 | XMLPortParam(FormationController, "formationFlight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(false); |
---|
133 | XMLPortParam(FormationController, "formationSize", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE); |
---|
134 | XMLPortParam(FormationController, "passive", setPassive, getPassive, xmlelement, mode).defaultValues(false); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | /** |
---|
140 | @brief Activates / deactivates formationflight behaviour |
---|
141 | @param form activate formflight if form is true |
---|
142 | */ |
---|
143 | void FormationController::formationflight(const bool form) |
---|
144 | { |
---|
145 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
146 | { |
---|
147 | Controller* controller = 0; |
---|
148 | |
---|
149 | if (it->getController()) |
---|
150 | controller = it->getController(); |
---|
151 | else if (it->getXMLController()) |
---|
152 | controller = it->getXMLController(); |
---|
153 | |
---|
154 | if (!controller) |
---|
155 | continue; |
---|
156 | |
---|
157 | FormationController *aiController = orxonox_cast<FormationController*>(controller); |
---|
158 | |
---|
159 | if (aiController) |
---|
160 | { |
---|
161 | aiController->formationFlight_ = form; |
---|
162 | if (!form) |
---|
163 | { |
---|
164 | aiController->removeFromFormation(); |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | @brief Get all masters to do a "specific master action" |
---|
172 | @param action which action to perform (integer, so it can be called with a console command (tmp solution)) |
---|
173 | */ |
---|
174 | void FormationController::masteraction(const int action) |
---|
175 | { |
---|
176 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
177 | { |
---|
178 | Controller* controller = 0; |
---|
179 | |
---|
180 | if (it->getController()) |
---|
181 | controller = it->getController(); |
---|
182 | else if (it->getXMLController()) |
---|
183 | controller = it->getXMLController(); |
---|
184 | |
---|
185 | if (!controller) |
---|
186 | continue; |
---|
187 | |
---|
188 | FormationController *aiController = orxonox_cast<FormationController*>(controller); |
---|
189 | |
---|
190 | if(aiController && aiController->state_ == MASTER) |
---|
191 | { |
---|
192 | if (action == 1) |
---|
193 | aiController->spinInit(); |
---|
194 | if (action == 2) |
---|
195 | aiController->turn180Init(); |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | @brief Sets shooting behaviour of pawns. |
---|
202 | @param passive if true, bots won't shoot. |
---|
203 | */ |
---|
204 | void FormationController::passivebehaviour(const bool passive) |
---|
205 | { |
---|
206 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
207 | { |
---|
208 | Controller* controller = 0; |
---|
209 | |
---|
210 | if (it->getController()) |
---|
211 | controller = it->getController(); |
---|
212 | else if (it->getXMLController()) |
---|
213 | controller = it->getXMLController(); |
---|
214 | |
---|
215 | if (!controller) |
---|
216 | continue; |
---|
217 | |
---|
218 | FormationController *aiController = orxonox_cast<FormationController*>(controller); |
---|
219 | |
---|
220 | if(aiController) |
---|
221 | { |
---|
222 | aiController->passive_ = passive; |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | @brief Sets maximal formation size |
---|
229 | @param size maximal formation size. |
---|
230 | */ |
---|
231 | void FormationController::formationsize(const int size) |
---|
232 | { |
---|
233 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
234 | { |
---|
235 | Controller* controller = 0; |
---|
236 | |
---|
237 | if (it->getController()) |
---|
238 | controller = it->getController(); |
---|
239 | else if (it->getXMLController()) |
---|
240 | controller = it->getXMLController(); |
---|
241 | |
---|
242 | if (!controller) |
---|
243 | continue; |
---|
244 | |
---|
245 | FormationController *aiController = orxonox_cast<FormationController*>(controller); |
---|
246 | |
---|
247 | if(aiController) |
---|
248 | { |
---|
249 | aiController->maxFormationSize_ = size; |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | //used, when slaves are in DEFEND mode. |
---|
255 | void FormationController::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage) |
---|
256 | { |
---|
257 | if (!this->formationFlight_ || this->state_ != MASTER || this->formationMode_ != DEFEND) |
---|
258 | return; |
---|
259 | this->masterAttacked(originator); |
---|
260 | } |
---|
261 | |
---|
262 | void FormationController::removeFromFormation() |
---|
263 | { |
---|
264 | if (this->state_ == SLAVE || this->myMaster_) // slaves can also be temporary free, so check if myMaster_ is set |
---|
265 | this->unregisterSlave(); |
---|
266 | else if (this->state_ == MASTER) |
---|
267 | this->setNewMasterWithinFormation(); |
---|
268 | } |
---|
269 | |
---|
270 | void FormationController::moveToPosition(const Vector3& target) |
---|
271 | { |
---|
272 | if (!this->getControllableEntity()) |
---|
273 | return; |
---|
274 | |
---|
275 | // Slave uses special movement if its master is in FOLLOW mode |
---|
276 | if(this->state_ == SLAVE && this->myMaster_ && this->myMaster_->specificMasterAction_ == FOLLOW) |
---|
277 | { |
---|
278 | // this->followForSlaves(target); |
---|
279 | // return; |
---|
280 | } |
---|
281 | |
---|
282 | Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
283 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
284 | float rotateX = clamp(coord.x * 10, -1.0f, 1.0f); |
---|
285 | float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); |
---|
286 | |
---|
287 | if(this->state_ == FREE) |
---|
288 | { |
---|
289 | if (this->target_ || distance > 10) |
---|
290 | { |
---|
291 | // Multiply with ROTATEFACTOR_FREE to make them a bit slower |
---|
292 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * rotateX); |
---|
293 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * rotateY); |
---|
294 | } |
---|
295 | |
---|
296 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
297 | { |
---|
298 | this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance |
---|
299 | } else this->getControllableEntity()->moveFrontBack(SPEED_FREE); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | if(this->state_ == MASTER) |
---|
305 | { |
---|
306 | if (this->target_ || distance > 10) |
---|
307 | { |
---|
308 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * rotateX); |
---|
309 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * rotateY); |
---|
310 | } |
---|
311 | |
---|
312 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
313 | { |
---|
314 | this->getControllableEntity()->moveFrontBack(-0.05f); |
---|
315 | } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | |
---|
320 | if(this->state_ == SLAVE) |
---|
321 | { |
---|
322 | |
---|
323 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * rotateX); |
---|
324 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * rotateY); |
---|
325 | |
---|
326 | if (distance < 300) |
---|
327 | { |
---|
328 | if (bHasTargetOrientation_) |
---|
329 | { |
---|
330 | copyTargetOrientation(); |
---|
331 | } |
---|
332 | if (distance < 100) |
---|
333 | { //linear speed reduction |
---|
334 | this->getControllableEntity()->moveFrontBack(distance/100.0f*0.4f*SPEED_MASTER); |
---|
335 | } |
---|
336 | else |
---|
337 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER); |
---|
338 | } |
---|
339 | else |
---|
340 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f); |
---|
341 | } |
---|
342 | |
---|
343 | if (distance < 10) |
---|
344 | { |
---|
345 | this->positionReached(); |
---|
346 | bHasTargetOrientation_=false; |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | |
---|
352 | void FormationController::moveToTargetPosition() |
---|
353 | { |
---|
354 | this->moveToPosition(this->targetPosition_); |
---|
355 | } |
---|
356 | |
---|
357 | //copy the Roll orientation of given Quaternion. |
---|
358 | void FormationController::copyOrientation(const Quaternion& orient) |
---|
359 | { |
---|
360 | //roll angle difference in radian |
---|
361 | float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians()); |
---|
362 | while(diff>math::twoPi) diff-=math::twoPi; |
---|
363 | while(diff<-math::twoPi) diff+=math::twoPi; |
---|
364 | this->getControllableEntity()->rotateRoll(diff*ROTATEFACTOR_MASTER); |
---|
365 | } |
---|
366 | |
---|
367 | void FormationController::copyTargetOrientation() |
---|
368 | { |
---|
369 | if (bHasTargetOrientation_) |
---|
370 | { |
---|
371 | copyOrientation(targetOrientation_); |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | /** |
---|
377 | @brief Unregisters a slave from its master. Initiated by a slave. |
---|
378 | */ |
---|
379 | void FormationController::unregisterSlave() |
---|
380 | { |
---|
381 | if (this->myMaster_) |
---|
382 | { |
---|
383 | std::vector<FormationController*>::iterator it = std::find(this->myMaster_->slaves_.begin(), this->myMaster_->slaves_.end(), this); |
---|
384 | if (it != this->myMaster_->slaves_.end()) |
---|
385 | this->myMaster_->slaves_.erase(it); |
---|
386 | } |
---|
387 | |
---|
388 | this->myMaster_ = 0; |
---|
389 | this->state_ = FREE; |
---|
390 | } |
---|
391 | |
---|
392 | void FormationController::searchNewMaster() |
---|
393 | { |
---|
394 | if (this->state_==SLAVE) |
---|
395 | return; |
---|
396 | if (!this->getControllableEntity()) |
---|
397 | return; |
---|
398 | |
---|
399 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
400 | this->forgetTarget(); |
---|
401 | int teamSize = 0; |
---|
402 | //go through all pawns |
---|
403 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
404 | { |
---|
405 | |
---|
406 | //same team? |
---|
407 | Gametype* gt=this->getGametype(); |
---|
408 | if (!gt) |
---|
409 | { |
---|
410 | gt=it->getGametype(); |
---|
411 | } |
---|
412 | if (!FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it),gt)) |
---|
413 | continue; |
---|
414 | |
---|
415 | //has it an FormationController? |
---|
416 | Controller* controller = 0; |
---|
417 | |
---|
418 | if (it->getController()) |
---|
419 | controller = it->getController(); |
---|
420 | else if (it->getXMLController()) |
---|
421 | controller = it->getXMLController(); |
---|
422 | |
---|
423 | if (!controller) |
---|
424 | continue; |
---|
425 | |
---|
426 | //is pawn oneself? |
---|
427 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
428 | continue; |
---|
429 | |
---|
430 | teamSize++; |
---|
431 | |
---|
432 | FormationController *newMaster = orxonox_cast<FormationController*>(controller); |
---|
433 | |
---|
434 | //is it a master? |
---|
435 | if (!newMaster || newMaster->state_ != MASTER) |
---|
436 | continue; |
---|
437 | |
---|
438 | float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
439 | |
---|
440 | // is pawn in range? |
---|
441 | if (distance < RADIUS_TO_SEARCH_FOR_MASTERS) |
---|
442 | { |
---|
443 | if(newMaster->slaves_.size() > this->maxFormationSize_) continue; |
---|
444 | |
---|
445 | for(std::vector<FormationController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++) |
---|
446 | { |
---|
447 | (*itSlave)->myMaster_ = newMaster; |
---|
448 | newMaster->slaves_.push_back(*itSlave); |
---|
449 | } |
---|
450 | this->slaves_.clear(); |
---|
451 | this->state_ = SLAVE; |
---|
452 | |
---|
453 | this->myMaster_ = newMaster; |
---|
454 | newMaster->slaves_.push_back(this); |
---|
455 | |
---|
456 | break; |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | if (this->state_ != SLAVE && teamSize != 0) |
---|
461 | { |
---|
462 | this->state_ = MASTER; |
---|
463 | this->myMaster_ = 0; |
---|
464 | } |
---|
465 | } |
---|
466 | |
---|
467 | /** |
---|
468 | @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master. |
---|
469 | */ |
---|
470 | void FormationController::commandSlaves() |
---|
471 | { |
---|
472 | if(this->state_ != MASTER) return; |
---|
473 | |
---|
474 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
475 | Vector3 dest = this->getControllableEntity()->getPosition(); |
---|
476 | |
---|
477 | // 1 slave: follow |
---|
478 | if (this->slaves_.size() == 1) |
---|
479 | { |
---|
480 | dest += 4*orient*WorldEntity::BACK; |
---|
481 | this->slaves_.front()->setTargetPosition(dest); |
---|
482 | } |
---|
483 | else |
---|
484 | // formation: |
---|
485 | { |
---|
486 | dest += 1.0f*orient*WorldEntity::BACK; |
---|
487 | Vector3 pos = Vector3::ZERO; |
---|
488 | bool left=true; |
---|
489 | int i = 1; |
---|
490 | |
---|
491 | for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
492 | { |
---|
493 | pos = Vector3::ZERO; |
---|
494 | if (left) |
---|
495 | { |
---|
496 | pos+=dest+i*FORMATION_WIDTH*(orient*WorldEntity::LEFT); |
---|
497 | } else{ |
---|
498 | pos+=dest+i*FORMATION_WIDTH*(orient*WorldEntity::RIGHT); |
---|
499 | i++; |
---|
500 | dest+=FORMATION_LENGTH*(orient*WorldEntity::BACK); |
---|
501 | } |
---|
502 | (*it)->setTargetOrientation(orient); |
---|
503 | (*it)->setTargetPosition(pos); |
---|
504 | left=!left; |
---|
505 | } |
---|
506 | } |
---|
507 | } |
---|
508 | |
---|
509 | /** |
---|
510 | @brief Sets a new master within the formation. Called by a master. |
---|
511 | */ |
---|
512 | void FormationController::setNewMasterWithinFormation() |
---|
513 | { |
---|
514 | if(this->state_ != MASTER) return; |
---|
515 | |
---|
516 | if (!this->slaves_.empty()) |
---|
517 | { |
---|
518 | FormationController *newMaster = this->slaves_.back(); |
---|
519 | this->slaves_.pop_back(); |
---|
520 | |
---|
521 | newMaster->state_ = MASTER; |
---|
522 | newMaster->slaves_ = this->slaves_; |
---|
523 | newMaster->myMaster_ = 0; |
---|
524 | |
---|
525 | for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++) |
---|
526 | { |
---|
527 | (*it)->myMaster_ = newMaster; |
---|
528 | } |
---|
529 | } |
---|
530 | |
---|
531 | this->slaves_.clear(); |
---|
532 | this->specificMasterAction_ = NONE; |
---|
533 | this->state_ = FREE; |
---|
534 | } |
---|
535 | |
---|
536 | |
---|
537 | // Sets newMaster as the new master within the formation. Called by the master. |
---|
538 | void FormationController::setNewMasterWithinFormation(FormationController* newMaster) |
---|
539 | { |
---|
540 | if(this->state_ != MASTER || newMaster->myMaster_ != this) return; |
---|
541 | |
---|
542 | if (!this->slaves_.empty()) |
---|
543 | { |
---|
544 | std::vector<FormationController*>::iterator it2 = std::find(this->slaves_.begin(), this->slaves_.end(), newMaster); |
---|
545 | if (it2 != this->slaves_.end()) |
---|
546 | { |
---|
547 | this->slaves_.erase(it2); |
---|
548 | } |
---|
549 | |
---|
550 | newMaster->state_ = MASTER; |
---|
551 | newMaster->slaves_ = this->slaves_; |
---|
552 | newMaster->myMaster_ = 0; |
---|
553 | |
---|
554 | for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++) |
---|
555 | { |
---|
556 | (*it)->myMaster_ = newMaster; |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | this->slaves_.clear(); |
---|
561 | this->specificMasterAction_ = NONE; |
---|
562 | this->state_ = FREE; |
---|
563 | } |
---|
564 | |
---|
565 | |
---|
566 | |
---|
567 | /** |
---|
568 | @brief Frees all slaves from a master. Initiated by a master. |
---|
569 | */ |
---|
570 | void FormationController::freeSlaves() |
---|
571 | { |
---|
572 | if(this->state_ != MASTER) return; |
---|
573 | |
---|
574 | for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
575 | { |
---|
576 | (*it)->state_ = FREE; |
---|
577 | (*it)->myMaster_ = 0; |
---|
578 | } |
---|
579 | this->slaves_.clear(); |
---|
580 | } |
---|
581 | |
---|
582 | /** |
---|
583 | @brief Master sets its slaves free for @ref FREEDOM_COUNT seconds. |
---|
584 | */ |
---|
585 | void FormationController::forceFreeSlaves() |
---|
586 | { |
---|
587 | if(this->state_ != MASTER) return; |
---|
588 | |
---|
589 | for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
590 | { |
---|
591 | (*it)->state_ = FREE; |
---|
592 | (*it)->forceFreedom(); |
---|
593 | (*it)->targetPosition_ = this->targetPosition_; |
---|
594 | (*it)->bShooting_ = true; |
---|
595 | // (*it)->getControllableEntity()->fire(0);// fire once for fun |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | void FormationController::loseMasterState() |
---|
600 | { |
---|
601 | this->freeSlaves(); |
---|
602 | this->state_ = FREE; |
---|
603 | } |
---|
604 | |
---|
605 | |
---|
606 | void FormationController::forceFreedom() |
---|
607 | { |
---|
608 | this->freedomCount_ = FREEDOM_COUNT; |
---|
609 | } |
---|
610 | |
---|
611 | /** |
---|
612 | @brief Checks wether caller has been forced free, decrements time to stay forced free. |
---|
613 | @return true if forced free. |
---|
614 | */ |
---|
615 | bool FormationController::forcedFree() |
---|
616 | { |
---|
617 | if(this->freedomCount_ > 0) |
---|
618 | { |
---|
619 | this->freedomCount_--; |
---|
620 | return true; |
---|
621 | } else return false; |
---|
622 | } |
---|
623 | |
---|
624 | |
---|
625 | /** |
---|
626 | @brief Call to take the lead of formation (if free, become slave of nearest formation, then, if Slave, become Master) |
---|
627 | */ |
---|
628 | void FormationController::takeLeadOfFormation() |
---|
629 | { |
---|
630 | if (!this->getControllableEntity() || this->state_==MASTER) |
---|
631 | return; |
---|
632 | |
---|
633 | //search new Master, then take lead |
---|
634 | if (this->state_==FREE && this->myMaster_==0) |
---|
635 | { |
---|
636 | searchNewMaster(); |
---|
637 | } |
---|
638 | |
---|
639 | if (this->state_==SLAVE) //become master of this formation |
---|
640 | { |
---|
641 | this->slaves_=this->myMaster_->slaves_; |
---|
642 | this->myMaster_->slaves_.clear(); |
---|
643 | this->myMaster_->state_=SLAVE; |
---|
644 | this->myMaster_->myMaster_=this; |
---|
645 | |
---|
646 | //delete myself in slavelist |
---|
647 | std::vector<FormationController*>::iterator it2 = std::find(this->slaves_.begin(), this->slaves_.end(), this); |
---|
648 | if (it2 != this->slaves_.end()) |
---|
649 | { |
---|
650 | this->slaves_.erase(it2); |
---|
651 | } |
---|
652 | //add previous master |
---|
653 | this->slaves_.push_back(this->myMaster_); |
---|
654 | //set this as new master |
---|
655 | for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
656 | { |
---|
657 | (*it)->myMaster_=this; |
---|
658 | } |
---|
659 | this->myMaster_=0; |
---|
660 | this->state_=MASTER; |
---|
661 | } |
---|
662 | /*/debug |
---|
663 | if (this->state_==SLAVE) |
---|
664 | {orxout(debug_output) << this << " is slave "<< endl;} |
---|
665 | else if (this->state_==MASTER) |
---|
666 | {orxout(debug_output) << this << " is now a master of "<<this->slaves_.size()<<" slaves."<< endl;} |
---|
667 | if (this->state_==FREE) |
---|
668 | {orxout(debug_output) << this << " is free "<< endl;}*/ |
---|
669 | } |
---|
670 | /** |
---|
671 | @brief if called, half of the formation will attack the originator |
---|
672 | */ |
---|
673 | void FormationController::masterAttacked(Pawn* originator) |
---|
674 | { |
---|
675 | if (this->state_!=MASTER) return; |
---|
676 | unsigned int i=0; |
---|
677 | for(std::vector<FormationController*>::reverse_iterator it = slaves_.rbegin(); it != slaves_.rend(); it++) |
---|
678 | { |
---|
679 | if ((*it)->state_!=FREE) |
---|
680 | { |
---|
681 | (*it)->state_=FREE; |
---|
682 | (*it)->forceFreedom(); |
---|
683 | (*it)->target_=originator; |
---|
684 | } |
---|
685 | i++; |
---|
686 | if (i>=slaves_.size()/2) break; //half the formation should attack. |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | |
---|
691 | /** |
---|
692 | @brief Sets the new mode. If master, set it for all slaves. |
---|
693 | */ |
---|
694 | void FormationController::setFormationMode(FormationMode val) |
---|
695 | { |
---|
696 | this->formationMode_ = val; |
---|
697 | if (this->state_ == MASTER) |
---|
698 | { |
---|
699 | for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
700 | { |
---|
701 | (*it)->formationMode_ = val; |
---|
702 | if (val == ATTACK) |
---|
703 | (*it)->forgetTarget(); |
---|
704 | } |
---|
705 | } |
---|
706 | } |
---|
707 | |
---|
708 | /** |
---|
709 | @brief Used to continue a "specific master action" for a certain time and resuming normal behaviour after. |
---|
710 | */ |
---|
711 | void FormationController::specificMasterActionHold() |
---|
712 | { |
---|
713 | if(this->state_ != MASTER) return; |
---|
714 | |
---|
715 | if (specificMasterActionHoldCount_ == 0) |
---|
716 | { |
---|
717 | this->specificMasterAction_ = NONE; |
---|
718 | this->searchNewTarget(); |
---|
719 | } |
---|
720 | else |
---|
721 | specificMasterActionHoldCount_--; |
---|
722 | } |
---|
723 | |
---|
724 | /** |
---|
725 | @brief Master initializes a 180 degree turn. Leads to a "specific master action". |
---|
726 | */ |
---|
727 | void FormationController::turn180Init() |
---|
728 | { |
---|
729 | if(this->state_ != MASTER) return; |
---|
730 | |
---|
731 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
732 | |
---|
733 | this->setTargetPosition(this->getControllableEntity()->getPosition() + 1000.0f*orient*WorldEntity::BACK); |
---|
734 | |
---|
735 | this->specificMasterActionHoldCount_ = 4; |
---|
736 | |
---|
737 | this->specificMasterAction_ = TURN180; |
---|
738 | } |
---|
739 | |
---|
740 | /** |
---|
741 | @brief Execute the 180 degree turn. Called within tick. |
---|
742 | */ |
---|
743 | void FormationController::turn180() |
---|
744 | { |
---|
745 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); |
---|
746 | |
---|
747 | this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); |
---|
748 | this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); |
---|
749 | |
---|
750 | this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
751 | } |
---|
752 | |
---|
753 | /** |
---|
754 | @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action". |
---|
755 | */ |
---|
756 | void FormationController::spinInit() |
---|
757 | { |
---|
758 | if(this->state_ != MASTER) return; |
---|
759 | this->specificMasterAction_ = SPIN; |
---|
760 | this->specificMasterActionHoldCount_ = 10; |
---|
761 | } |
---|
762 | |
---|
763 | /** |
---|
764 | @brief Execute the spin. Called within tick. |
---|
765 | */ |
---|
766 | void FormationController::spin() |
---|
767 | { |
---|
768 | this->moveToTargetPosition(); |
---|
769 | this->getControllableEntity()->rotateRoll(0.8f); |
---|
770 | } |
---|
771 | |
---|
772 | /** |
---|
773 | @brief A human player gets followed by its nearest master. Initiated by console command, so far intended for demonstration puproses (possible future pickup). |
---|
774 | */ |
---|
775 | void FormationController::followme() |
---|
776 | { |
---|
777 | |
---|
778 | Pawn *humanPawn = NULL; |
---|
779 | NewHumanController *currentHumanController = NULL; |
---|
780 | std::vector<FormationController*> allMasters; |
---|
781 | |
---|
782 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
783 | { |
---|
784 | Controller* controller = 0; |
---|
785 | |
---|
786 | if (it->getController()) |
---|
787 | controller = it->getController(); |
---|
788 | else if (it->getXMLController()) |
---|
789 | controller = it->getXMLController(); |
---|
790 | |
---|
791 | if (!controller) |
---|
792 | continue; |
---|
793 | |
---|
794 | currentHumanController = orxonox_cast<NewHumanController*>(controller); |
---|
795 | |
---|
796 | if(currentHumanController) humanPawn = *it; |
---|
797 | |
---|
798 | FormationController *aiController = orxonox_cast<FormationController*>(controller); |
---|
799 | |
---|
800 | if(aiController && aiController->state_ == MASTER) |
---|
801 | allMasters.push_back(aiController); |
---|
802 | |
---|
803 | } |
---|
804 | |
---|
805 | if((humanPawn != NULL) && (allMasters.size() != 0)) |
---|
806 | { |
---|
807 | float posHuman = humanPawn->getPosition().length(); |
---|
808 | float distance = 0.0f; |
---|
809 | float minDistance = FLT_MAX; |
---|
810 | int index = 0; |
---|
811 | int i = 0; |
---|
812 | |
---|
813 | for(std::vector<FormationController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++) |
---|
814 | { |
---|
815 | if (!FormationController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue; |
---|
816 | distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); |
---|
817 | if(distance < minDistance) index = i; |
---|
818 | } |
---|
819 | allMasters[index]->followInit(humanPawn); |
---|
820 | } |
---|
821 | } |
---|
822 | |
---|
823 | /** |
---|
824 | @brief Master begins to follow a pawn. Is a "specific master action". |
---|
825 | @param pawn pawn to follow. |
---|
826 | @param always follows pawn forever if true (false if omitted). |
---|
827 | @param secondsToFollow seconds to follow the pawn if always is false. Will follow pawn 100 seconds if omitted (set in header). |
---|
828 | */ |
---|
829 | void FormationController::followInit(Pawn* pawn, const bool always, const int secondsToFollow) |
---|
830 | { |
---|
831 | if (pawn == NULL || this->state_ != MASTER) |
---|
832 | return; |
---|
833 | this->specificMasterAction_ = FOLLOW; |
---|
834 | |
---|
835 | this->setTarget(pawn); |
---|
836 | if (!always) |
---|
837 | this->specificMasterActionHoldCount_ = secondsToFollow; |
---|
838 | else |
---|
839 | this->specificMasterActionHoldCount_ = INT_MAX; //for now... |
---|
840 | |
---|
841 | } |
---|
842 | |
---|
843 | /** |
---|
844 | @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action". |
---|
845 | */ |
---|
846 | void FormationController::followRandomHumanInit() |
---|
847 | { |
---|
848 | |
---|
849 | Pawn *humanPawn = NULL; |
---|
850 | NewHumanController *currentHumanController = NULL; |
---|
851 | |
---|
852 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
853 | { |
---|
854 | if (!it->getController()) |
---|
855 | continue; |
---|
856 | |
---|
857 | currentHumanController = orxonox_cast<NewHumanController*>(it->getController()); |
---|
858 | if(currentHumanController) |
---|
859 | { |
---|
860 | if (!FormationController::sameTeam(this->getControllableEntity(), *it, this->getGametype())) continue; |
---|
861 | humanPawn = *it; |
---|
862 | break; |
---|
863 | } |
---|
864 | } |
---|
865 | |
---|
866 | if((humanPawn != NULL)) |
---|
867 | this->followInit(humanPawn); |
---|
868 | } |
---|
869 | |
---|
870 | |
---|
871 | /** |
---|
872 | @brief Master follows target with adjusted speed. Called within tick. |
---|
873 | */ |
---|
874 | void FormationController::follow() |
---|
875 | { |
---|
876 | if (this->target_) |
---|
877 | this->moveToPosition(this->target_->getPosition()); |
---|
878 | else |
---|
879 | this->specificMasterActionHoldCount_ = 0; |
---|
880 | } |
---|
881 | |
---|
882 | |
---|
883 | void FormationController::setTargetPosition(const Vector3& target) |
---|
884 | { |
---|
885 | this->targetPosition_ = target; |
---|
886 | this->bHasTargetPosition_ = true; |
---|
887 | } |
---|
888 | |
---|
889 | void FormationController::searchRandomTargetPosition() |
---|
890 | { |
---|
891 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
892 | this->bHasTargetPosition_ = true; |
---|
893 | } |
---|
894 | |
---|
895 | void FormationController::setTargetOrientation(const Quaternion& orient) |
---|
896 | { |
---|
897 | this->targetOrientation_=orient; |
---|
898 | this->bHasTargetOrientation_=true; |
---|
899 | } |
---|
900 | |
---|
901 | void FormationController::setTargetOrientation(Pawn* target) |
---|
902 | { |
---|
903 | if (target) |
---|
904 | setTargetOrientation(target->getOrientation()); |
---|
905 | } |
---|
906 | |
---|
907 | void FormationController::setTarget(Pawn* target) |
---|
908 | { |
---|
909 | this->target_ = target; |
---|
910 | |
---|
911 | if (target) |
---|
912 | this->targetPosition_ = target->getPosition(); |
---|
913 | } |
---|
914 | |
---|
915 | void FormationController::searchNewTarget() |
---|
916 | { |
---|
917 | if (!this->getControllableEntity()) |
---|
918 | return; |
---|
919 | |
---|
920 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
921 | this->forgetTarget(); |
---|
922 | |
---|
923 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
924 | { |
---|
925 | if (FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
926 | continue; |
---|
927 | |
---|
928 | /* So AI won't choose invisible Spaceships as target */ |
---|
929 | if (!it->getRadarVisibility()) |
---|
930 | continue; |
---|
931 | |
---|
932 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
933 | { |
---|
934 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
935 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
936 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
937 | if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / math::twoPi) |
---|
938 | < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / math::twoPi) + rnd(-250, 250)) |
---|
939 | { |
---|
940 | this->target_ = (*it); |
---|
941 | this->targetPosition_ = it->getPosition(); |
---|
942 | } |
---|
943 | } |
---|
944 | } |
---|
945 | } |
---|
946 | |
---|
947 | void FormationController::forgetTarget() |
---|
948 | { |
---|
949 | this->target_ = 0; |
---|
950 | this->bShooting_ = false; |
---|
951 | } |
---|
952 | |
---|
953 | void FormationController::targetDied() |
---|
954 | { |
---|
955 | this->forgetTarget(); |
---|
956 | this->searchRandomTargetPosition(); |
---|
957 | } |
---|
958 | |
---|
959 | bool FormationController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
960 | { |
---|
961 | if (entity1 == entity2) |
---|
962 | return true; |
---|
963 | |
---|
964 | int team1 = -1; |
---|
965 | int team2 = -1; |
---|
966 | |
---|
967 | Controller* controller = 0; |
---|
968 | if (entity1->getController()) |
---|
969 | controller = entity1->getController(); |
---|
970 | else |
---|
971 | controller = entity1->getXMLController(); |
---|
972 | if (controller) |
---|
973 | { |
---|
974 | FormationController* ac = orxonox_cast<FormationController*>(controller); |
---|
975 | if (ac) |
---|
976 | team1 = ac->getTeam(); |
---|
977 | } |
---|
978 | |
---|
979 | if (entity2->getController()) |
---|
980 | controller = entity2->getController(); |
---|
981 | else |
---|
982 | controller = entity2->getXMLController(); |
---|
983 | if (controller) |
---|
984 | { |
---|
985 | FormationController* ac = orxonox_cast<FormationController*>(controller); |
---|
986 | if (ac) |
---|
987 | team2 = ac->getTeam(); |
---|
988 | } |
---|
989 | |
---|
990 | TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); |
---|
991 | if (tdm) |
---|
992 | { |
---|
993 | if (entity1->getPlayer()) |
---|
994 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
995 | |
---|
996 | if (entity2->getPlayer()) |
---|
997 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
998 | } |
---|
999 | |
---|
1000 | Mission* miss = orxonox_cast<Mission*>(gametype); |
---|
1001 | if (miss) |
---|
1002 | { |
---|
1003 | if (entity1->getPlayer()) |
---|
1004 | team1 = miss->getTeam(entity1->getPlayer()); |
---|
1005 | |
---|
1006 | if (entity2->getPlayer()) |
---|
1007 | team2 = miss->getTeam(entity2->getPlayer()); |
---|
1008 | } |
---|
1009 | |
---|
1010 | TeamBaseMatchBase* base = 0; |
---|
1011 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
1012 | if (base) |
---|
1013 | { |
---|
1014 | switch (base->getState()) |
---|
1015 | { |
---|
1016 | case BaseState::ControlTeam1: |
---|
1017 | team1 = 0; |
---|
1018 | break; |
---|
1019 | case BaseState::ControlTeam2: |
---|
1020 | team1 = 1; |
---|
1021 | break; |
---|
1022 | case BaseState::Uncontrolled: |
---|
1023 | default: |
---|
1024 | team1 = -1; |
---|
1025 | } |
---|
1026 | } |
---|
1027 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
1028 | if (base) |
---|
1029 | { |
---|
1030 | switch (base->getState()) |
---|
1031 | { |
---|
1032 | case BaseState::ControlTeam1: |
---|
1033 | team2 = 0; |
---|
1034 | break; |
---|
1035 | case BaseState::ControlTeam2: |
---|
1036 | team2 = 1; |
---|
1037 | break; |
---|
1038 | case BaseState::Uncontrolled: |
---|
1039 | default: |
---|
1040 | team2 = -1; |
---|
1041 | } |
---|
1042 | } |
---|
1043 | |
---|
1044 | DroneController* droneController = 0; |
---|
1045 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
1046 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
1047 | return true; |
---|
1048 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
1049 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
1050 | return true; |
---|
1051 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
1052 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
1053 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
1054 | return true; |
---|
1055 | |
---|
1056 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
1057 | if (dynamic) |
---|
1058 | { |
---|
1059 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
1060 | |
---|
1061 | if (entity1->getPlayer()) |
---|
1062 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
1063 | |
---|
1064 | if (entity2->getPlayer()) |
---|
1065 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
1066 | |
---|
1067 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
---|
1068 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
1069 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
1070 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
1071 | else return true; |
---|
1072 | } |
---|
1073 | |
---|
1074 | return (team1 == team2 && team1 != -1); |
---|
1075 | } |
---|
1076 | |
---|
1077 | void FormationController::absoluteMoveToPosition(const Vector3& target) |
---|
1078 | { |
---|
1079 | float minDistance = 40.0f; |
---|
1080 | if (!this->getControllableEntity()) |
---|
1081 | return; |
---|
1082 | |
---|
1083 | Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
1084 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
1085 | |
---|
1086 | if (this->target_ || distance > minDistance) |
---|
1087 | { |
---|
1088 | // Multiply with ROTATEFACTOR_FREE to make them a bit slower |
---|
1089 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * clamp(coord.x * 10, -1.0f, 1.0f)); |
---|
1090 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * clamp(coord.y * 10, -1.0f, 1.0f)); |
---|
1091 | this->getControllableEntity()->moveFrontBack(SPEED_FREE); |
---|
1092 | } |
---|
1093 | |
---|
1094 | |
---|
1095 | if (distance < minDistance) |
---|
1096 | { |
---|
1097 | this->positionReached(); |
---|
1098 | } |
---|
1099 | } |
---|
1100 | |
---|
1101 | } |
---|