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