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 "ArtificialController.h" |
---|
30 | |
---|
31 | #include <vector> |
---|
32 | #include <climits> |
---|
33 | |
---|
34 | #include "util/Math.h" |
---|
35 | #include "core/ConsoleCommand.h" |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/XMLPort.h" |
---|
38 | #include "worldentities/ControllableEntity.h" |
---|
39 | #include "worldentities/pawns/Pawn.h" |
---|
40 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
41 | #include "gametypes/TeamDeathmatch.h" |
---|
42 | #include "gametypes/Dynamicmatch.h" |
---|
43 | #include "controllers/WaypointPatrolController.h" |
---|
44 | #include "controllers/NewHumanController.h" |
---|
45 | #include "controllers/DroneController.h" |
---|
46 | |
---|
47 | namespace orxonox |
---|
48 | { |
---|
49 | SetConsoleCommand(ArtificialController, formationflight, true); |
---|
50 | SetConsoleCommand(ArtificialController, masteraction, true); |
---|
51 | SetConsoleCommand(ArtificialController, followme, true); |
---|
52 | SetConsoleCommand(ArtificialController, passivebehaviour, true); |
---|
53 | SetConsoleCommand(ArtificialController, formationsize, true); |
---|
54 | |
---|
55 | static const unsigned int STANDARD_MAX_FORMATION_SIZE = 7; |
---|
56 | static const int FORMATION_LENGTH = 130; |
---|
57 | static const int FORMATION_WIDTH = 110; |
---|
58 | static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy |
---|
59 | static const float SPEED_MASTER = 0.6f; |
---|
60 | static const float ROTATEFACTOR_MASTER = 0.2f; |
---|
61 | static const float SPEED_FREE = 0.8f; |
---|
62 | static const float ROTATEFACTOR_FREE = 0.8f; |
---|
63 | static const int SECONDS_TO_FOLLOW_HUMAN = 100; |
---|
64 | |
---|
65 | ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) |
---|
66 | { |
---|
67 | RegisterObject(ArtificialController); |
---|
68 | |
---|
69 | this->target_ = 0; |
---|
70 | this->formationFlight_ = true; |
---|
71 | this->passive_ = false; |
---|
72 | this->maxFormationSize_ = STANDARD_MAX_FORMATION_SIZE; |
---|
73 | this->myMaster_ = 0; |
---|
74 | this->freedomCount_ = 0; |
---|
75 | this->team_ = -1; |
---|
76 | this->state_ = FREE; |
---|
77 | this->specificMasterAction_ = NONE; |
---|
78 | this->specificMasterActionHoldCount_ = 0; |
---|
79 | this->bShooting_ = false; |
---|
80 | this->bHasTargetPosition_ = false; |
---|
81 | this->targetPosition_ = Vector3::ZERO; |
---|
82 | this->humanToFollow_ = NULL; |
---|
83 | |
---|
84 | this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); |
---|
85 | } |
---|
86 | |
---|
87 | ArtificialController::~ArtificialController() |
---|
88 | { |
---|
89 | } |
---|
90 | |
---|
91 | void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
92 | { |
---|
93 | SUPER(ArtificialController, XMLPort, xmlelement, mode); |
---|
94 | |
---|
95 | XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1); |
---|
96 | XMLPortParam(ArtificialController, "formationflight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true); |
---|
97 | XMLPortParam(ArtificialController, "formation_size", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE); |
---|
98 | } |
---|
99 | |
---|
100 | // Documentation only here to get a faster overview for creating a useful documentation... |
---|
101 | |
---|
102 | /** |
---|
103 | @brief Activates / deactivates formationflight behaviour |
---|
104 | @param form activate formflight if form is true |
---|
105 | */ |
---|
106 | void ArtificialController::formationflight(bool form) |
---|
107 | { |
---|
108 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
109 | { |
---|
110 | if (!it->getController()) |
---|
111 | continue; |
---|
112 | |
---|
113 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(it->getController()); |
---|
114 | |
---|
115 | if(aiController) |
---|
116 | { |
---|
117 | aiController->formationFlight_ = form; |
---|
118 | if(!form) |
---|
119 | { |
---|
120 | if(aiController->state_ == MASTER) aiController->freeSlaves(); |
---|
121 | aiController->state_ = FREE; |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | @brief Get all masters to do a "specific master action" |
---|
129 | @param action which action to perform (integer, so it can be called with a console command (tmp solution)) |
---|
130 | */ |
---|
131 | void ArtificialController::masteraction(int action) |
---|
132 | { |
---|
133 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
134 | { |
---|
135 | if (!it->getController()) |
---|
136 | continue; |
---|
137 | |
---|
138 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(it->getController()); |
---|
139 | |
---|
140 | if(aiController && aiController->state_ == MASTER) |
---|
141 | { |
---|
142 | if (action == 1) |
---|
143 | aiController->spinInit(); |
---|
144 | if (action == 2) |
---|
145 | aiController->turn180Init(); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | @brief A human player gets followed by its nearest master. Initiated by console command, intended for demonstration puproses. Does not work at the moment. |
---|
152 | */ |
---|
153 | void ArtificialController::followme() |
---|
154 | { |
---|
155 | |
---|
156 | Pawn *humanPawn = NULL; |
---|
157 | NewHumanController *currentHumanController = NULL; |
---|
158 | std::vector<ArtificialController*> allMasters; |
---|
159 | |
---|
160 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
161 | { |
---|
162 | if (!it->getController()) |
---|
163 | continue; |
---|
164 | |
---|
165 | currentHumanController = orxonox_cast<NewHumanController*>(it->getController()); |
---|
166 | |
---|
167 | if(currentHumanController) humanPawn = *it; |
---|
168 | |
---|
169 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(it->getController()); |
---|
170 | |
---|
171 | if(aiController || aiController->state_ == MASTER) |
---|
172 | allMasters.push_back(aiController); |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | if((humanPawn != NULL) && (allMasters.size() != 0)) |
---|
177 | { |
---|
178 | float posHuman = humanPawn->getPosition().length(); |
---|
179 | float distance = 0.0f; |
---|
180 | float minDistance = FLT_MAX; |
---|
181 | int index = 0; |
---|
182 | int i = 0; |
---|
183 | |
---|
184 | for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++) |
---|
185 | { |
---|
186 | distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); |
---|
187 | if(distance < minDistance) index = i; |
---|
188 | } |
---|
189 | allMasters[index]->humanToFollow_ = humanPawn; |
---|
190 | // allMasters[index]->followHuman(humanPawn, false); |
---|
191 | } |
---|
192 | |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | @brief Sets shooting behaviour of pawns. |
---|
197 | @param passive if true, bots won't shoot. |
---|
198 | */ |
---|
199 | void ArtificialController::passivebehaviour(bool passive) |
---|
200 | { |
---|
201 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
202 | { |
---|
203 | if (!it->getController()) |
---|
204 | continue; |
---|
205 | |
---|
206 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(it->getController()); |
---|
207 | |
---|
208 | if(aiController) |
---|
209 | { |
---|
210 | aiController->passive_ = passive; |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | @brief Sets maximal formation size |
---|
218 | @param size maximal formation size. |
---|
219 | */ |
---|
220 | void ArtificialController::formationsize(int size) |
---|
221 | { |
---|
222 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
223 | { |
---|
224 | if (!it->getController()) |
---|
225 | continue; |
---|
226 | |
---|
227 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(it->getController()); |
---|
228 | |
---|
229 | if(aiController) |
---|
230 | { |
---|
231 | aiController->maxFormationSize_ = size; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | @brief Gets called when ControllableEntity is being changed. Resets the bot when it dies. |
---|
238 | */ |
---|
239 | void ArtificialController::changedControllableEntity() |
---|
240 | { |
---|
241 | if(!getControllableEntity()) |
---|
242 | { |
---|
243 | if (this->state_ == SLAVE) unregisterSlave(); |
---|
244 | if (this->state_ == MASTER) setNewMasterWithinFormation(); |
---|
245 | this->slaves_.clear(); |
---|
246 | this->state_ = FREE; |
---|
247 | |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | void ArtificialController::moveToPosition(const Vector3& target) |
---|
253 | { |
---|
254 | if (!this->getControllableEntity()) |
---|
255 | return; |
---|
256 | |
---|
257 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
258 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
259 | |
---|
260 | |
---|
261 | if(this->state_ == FREE) |
---|
262 | { |
---|
263 | if (this->target_ || distance > 10) |
---|
264 | { |
---|
265 | // Multiply with 0.8 to make them a bit slower |
---|
266 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x); |
---|
267 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y); |
---|
268 | } |
---|
269 | |
---|
270 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
271 | { |
---|
272 | this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance |
---|
273 | } else this->getControllableEntity()->moveFrontBack(SPEED_FREE); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | if(this->state_ == MASTER) |
---|
279 | { |
---|
280 | if (this->target_ || distance > 10) |
---|
281 | { |
---|
282 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x); |
---|
283 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y); |
---|
284 | } |
---|
285 | |
---|
286 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
287 | { |
---|
288 | this->getControllableEntity()->moveFrontBack(-0.05f); |
---|
289 | } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | |
---|
294 | if(this->state_ == SLAVE) |
---|
295 | { |
---|
296 | |
---|
297 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x); |
---|
298 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y); |
---|
299 | |
---|
300 | if (distance < 300) |
---|
301 | { |
---|
302 | if (distance < 40) |
---|
303 | { |
---|
304 | this->getControllableEntity()->moveFrontBack(0.8f*SPEED_MASTER); |
---|
305 | } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER); |
---|
306 | |
---|
307 | } else { |
---|
308 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f); |
---|
309 | } |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | void ArtificialController::moveToTargetPosition() |
---|
314 | { |
---|
315 | this->moveToPosition(this->targetPosition_); |
---|
316 | } |
---|
317 | |
---|
318 | int ArtificialController::getState() |
---|
319 | { |
---|
320 | return this->state_; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | @brief Unregisters a slave from its master. Called by a slave. |
---|
325 | */ |
---|
326 | void ArtificialController::unregisterSlave() { |
---|
327 | if(myMaster_) |
---|
328 | { |
---|
329 | std::vector<ArtificialController*>::iterator it = std::find(myMaster_->slaves_.begin(), myMaster_->slaves_.end(), this); |
---|
330 | if( it != myMaster_->slaves_.end() ) |
---|
331 | myMaster_->slaves_.erase(it); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | void ArtificialController::searchNewMaster() |
---|
336 | { |
---|
337 | |
---|
338 | if (!this->getControllableEntity()) |
---|
339 | return; |
---|
340 | |
---|
341 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
342 | this->forgetTarget(); |
---|
343 | int teamSize = 0; |
---|
344 | //go through all pawns |
---|
345 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
346 | { |
---|
347 | |
---|
348 | //same team? |
---|
349 | if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
350 | continue; |
---|
351 | |
---|
352 | //has it an ArtificialController? |
---|
353 | if (!it->getController()) |
---|
354 | continue; |
---|
355 | |
---|
356 | //is pawn oneself? |
---|
357 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
358 | continue; |
---|
359 | |
---|
360 | teamSize++; |
---|
361 | |
---|
362 | ArtificialController *newMaster = orxonox_cast<ArtificialController*>(it->getController()); |
---|
363 | |
---|
364 | //is it a master? |
---|
365 | if (!newMaster || newMaster->getState() != MASTER) |
---|
366 | continue; |
---|
367 | |
---|
368 | float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
369 | |
---|
370 | // is pawn in range? |
---|
371 | if (distance < 5000) |
---|
372 | { |
---|
373 | if(newMaster->slaves_.size() > this->maxFormationSize_) continue; |
---|
374 | |
---|
375 | for(std::vector<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++) |
---|
376 | { |
---|
377 | (*itSlave)->myMaster_ = newMaster; |
---|
378 | newMaster->slaves_.push_back(*itSlave); |
---|
379 | } |
---|
380 | this->slaves_.clear(); |
---|
381 | this->state_ = SLAVE; |
---|
382 | |
---|
383 | this->myMaster_ = newMaster; |
---|
384 | newMaster->slaves_.push_back(this); |
---|
385 | |
---|
386 | break; |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | if (state_ != SLAVE && teamSize != 0) state_ = MASTER; |
---|
391 | |
---|
392 | } |
---|
393 | |
---|
394 | /** |
---|
395 | @brief Commands the slaves of a master into a formation. Called by a master. |
---|
396 | */ |
---|
397 | void ArtificialController::commandSlaves() |
---|
398 | { |
---|
399 | if(this->state_ != MASTER) return; |
---|
400 | |
---|
401 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
402 | Vector3 dest = this->getControllableEntity()->getPosition(); |
---|
403 | |
---|
404 | // 1 slave: follow |
---|
405 | if (this->slaves_.size() == 1) |
---|
406 | { |
---|
407 | dest += 4*orient*WorldEntity::BACK; |
---|
408 | this->slaves_.front()->setTargetPosition(dest); |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | dest += 1.0f*orient*WorldEntity::BACK; |
---|
413 | Vector3 pos = Vector3::ZERO; |
---|
414 | int i = 1; |
---|
415 | |
---|
416 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
417 | { |
---|
418 | pos = Vector3::ZERO; |
---|
419 | if (i <= 1) pos += dest + FORMATION_WIDTH*(orient*WorldEntity::LEFT); |
---|
420 | if (i == 2) pos += dest + FORMATION_WIDTH*(orient*WorldEntity::RIGHT); |
---|
421 | if (i == 3) pos += dest + FORMATION_WIDTH*(orient*WorldEntity::UP); |
---|
422 | if (i >= 4) |
---|
423 | { |
---|
424 | pos += dest + FORMATION_WIDTH*(orient*WorldEntity::DOWN); |
---|
425 | i = 1; |
---|
426 | dest += FORMATION_LENGTH*(orient*WorldEntity::BACK); |
---|
427 | (*it)->setTargetPosition(pos); |
---|
428 | continue; |
---|
429 | } |
---|
430 | i++; |
---|
431 | (*it)->setTargetPosition(pos); |
---|
432 | } |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | @brief Sets a new master within the formation. Called by a master. |
---|
438 | */ |
---|
439 | void ArtificialController::setNewMasterWithinFormation() |
---|
440 | { |
---|
441 | if(this->state_ != MASTER) return; |
---|
442 | |
---|
443 | if (this->slaves_.empty()) |
---|
444 | return; |
---|
445 | |
---|
446 | ArtificialController *newMaster = this->slaves_.back(); |
---|
447 | this->slaves_.pop_back(); |
---|
448 | |
---|
449 | if(!newMaster) return; |
---|
450 | newMaster->state_ = MASTER; |
---|
451 | newMaster->slaves_ = this->slaves_; |
---|
452 | |
---|
453 | this->slaves_.clear(); |
---|
454 | this->state_ = SLAVE; |
---|
455 | this->myMaster_ = newMaster; |
---|
456 | |
---|
457 | for(std::vector<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++) |
---|
458 | { |
---|
459 | (*it)->myMaster_ = newMaster; |
---|
460 | } |
---|
461 | |
---|
462 | } |
---|
463 | |
---|
464 | /** |
---|
465 | @brief Frees all slaves form a master. Called by a master. |
---|
466 | */ |
---|
467 | void ArtificialController::freeSlaves() |
---|
468 | { |
---|
469 | if(this->state_ != MASTER) return; |
---|
470 | |
---|
471 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
472 | { |
---|
473 | (*it)->state_ = FREE; |
---|
474 | } |
---|
475 | this->slaves_.clear(); |
---|
476 | } |
---|
477 | |
---|
478 | /** |
---|
479 | @brief Master sets its slaves free for @var FREEDOM_COUNT seconds. |
---|
480 | */ |
---|
481 | void ArtificialController::forceFreeSlaves() |
---|
482 | { |
---|
483 | if(this->state_ != MASTER) return; |
---|
484 | |
---|
485 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
486 | { |
---|
487 | (*it)->state_ = FREE; |
---|
488 | (*it)->forceFreedom(); |
---|
489 | (*it)->targetPosition_ = this->targetPosition_; |
---|
490 | (*it)->bShooting_ = true; |
---|
491 | // (*it)->getControllableEntity()->fire(0);// fire once for fun |
---|
492 | } |
---|
493 | } |
---|
494 | |
---|
495 | void ArtificialController::loseMasterState() |
---|
496 | { |
---|
497 | this->freeSlaves(); |
---|
498 | this->state_ = FREE; |
---|
499 | } |
---|
500 | |
---|
501 | |
---|
502 | void ArtificialController::forceFreedom() |
---|
503 | { |
---|
504 | this->freedomCount_ = FREEDOM_COUNT; |
---|
505 | } |
---|
506 | |
---|
507 | /** |
---|
508 | @brief Checks wether caller has been forced free, decrements time to stay forced free. |
---|
509 | @return true if forced free. |
---|
510 | */ |
---|
511 | bool ArtificialController::forcedFree() |
---|
512 | { |
---|
513 | if(this->freedomCount_ > 0) |
---|
514 | { |
---|
515 | this->freedomCount_--; |
---|
516 | return true; |
---|
517 | } else return false; |
---|
518 | } |
---|
519 | |
---|
520 | /** |
---|
521 | @brief Used to continue a "specific master action" for a certain time and resuming normal behaviour after. |
---|
522 | */ |
---|
523 | void ArtificialController::specificMasterActionHold() |
---|
524 | { |
---|
525 | if(this->state_ != MASTER) return; |
---|
526 | |
---|
527 | if (specificMasterActionHoldCount_ == 0) |
---|
528 | { |
---|
529 | this->specificMasterAction_ = NONE; |
---|
530 | this->searchNewTarget(); |
---|
531 | } |
---|
532 | else specificMasterActionHoldCount_--; |
---|
533 | } |
---|
534 | |
---|
535 | /** |
---|
536 | @brief Master initializes a 180 degree turn. Leads to a "specific master action". |
---|
537 | */ |
---|
538 | void ArtificialController::turn180Init() |
---|
539 | { |
---|
540 | COUT(0) << "~turnInit" << std::endl; |
---|
541 | if(this->state_ != MASTER) return; |
---|
542 | |
---|
543 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
544 | |
---|
545 | this->setTargetPosition(this->getControllableEntity()->getPosition() + 1000.0f*orient*WorldEntity::BACK); |
---|
546 | |
---|
547 | this->specificMasterActionHoldCount_ = 4; |
---|
548 | |
---|
549 | this->specificMasterAction_ = TURN180; |
---|
550 | } |
---|
551 | |
---|
552 | /** |
---|
553 | @brief Execute the 180 degree turn. Called within tick. |
---|
554 | */ |
---|
555 | void ArtificialController::turn180() |
---|
556 | { |
---|
557 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); |
---|
558 | |
---|
559 | this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); |
---|
560 | this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); |
---|
561 | |
---|
562 | this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
563 | } |
---|
564 | |
---|
565 | /** |
---|
566 | @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action". Not yet implemented. |
---|
567 | */ |
---|
568 | void ArtificialController::spinInit() |
---|
569 | { |
---|
570 | COUT(0) << "~spinInit" << std::endl; |
---|
571 | if(this->state_ != MASTER) return; |
---|
572 | this->specificMasterAction_ = SPIN; |
---|
573 | this->specificMasterActionHoldCount_ = 10; |
---|
574 | } |
---|
575 | |
---|
576 | /** |
---|
577 | @brief Execute the spin. Called within tick. |
---|
578 | */ |
---|
579 | void ArtificialController::spin() |
---|
580 | { |
---|
581 | this->moveToTargetPosition(); |
---|
582 | this->getControllableEntity()->rotateRoll(0.8f); |
---|
583 | } |
---|
584 | |
---|
585 | /** |
---|
586 | @brief Master begins to follow a human player. Is a "specific master action". |
---|
587 | @param humanController human to follow. |
---|
588 | @param alaways follows human forever if true, else it follows it for @var SECONDS_TO_FOLLOW_HUMAN seconds. |
---|
589 | */ |
---|
590 | void ArtificialController::followHumanInit(Pawn* human, bool always) |
---|
591 | { |
---|
592 | COUT(0) << "~followInit" << std::endl; |
---|
593 | if (human == NULL || this->state_ != MASTER) |
---|
594 | return; |
---|
595 | |
---|
596 | this->specificMasterAction_ = FOLLOWHUMAN; |
---|
597 | |
---|
598 | this->setTarget(human); |
---|
599 | if (!always) |
---|
600 | this->specificMasterActionHoldCount_ = SECONDS_TO_FOLLOW_HUMAN; |
---|
601 | else |
---|
602 | this->specificMasterActionHoldCount_ = INT_MAX; //for now... |
---|
603 | |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | @brief Follows target with adjusted speed. Called within tick. |
---|
608 | */ |
---|
609 | void ArtificialController::follow() |
---|
610 | { |
---|
611 | this->moveToTargetPosition(); //standard position apprach for now. |
---|
612 | } |
---|
613 | |
---|
614 | |
---|
615 | |
---|
616 | void ArtificialController::setTargetPosition(const Vector3& target) |
---|
617 | { |
---|
618 | this->targetPosition_ = target; |
---|
619 | this->bHasTargetPosition_ = true; |
---|
620 | } |
---|
621 | |
---|
622 | void ArtificialController::searchRandomTargetPosition() |
---|
623 | { |
---|
624 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
625 | this->bHasTargetPosition_ = true; |
---|
626 | } |
---|
627 | |
---|
628 | void ArtificialController::setTarget(Pawn* target) |
---|
629 | { |
---|
630 | this->target_ = target; |
---|
631 | |
---|
632 | if (target) |
---|
633 | this->targetPosition_ = target->getPosition(); |
---|
634 | } |
---|
635 | |
---|
636 | void ArtificialController::searchNewTarget() |
---|
637 | { |
---|
638 | if (!this->getControllableEntity()) |
---|
639 | return; |
---|
640 | |
---|
641 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
642 | this->forgetTarget(); |
---|
643 | |
---|
644 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
645 | { |
---|
646 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
647 | continue; |
---|
648 | |
---|
649 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
650 | { |
---|
651 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
652 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
653 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
654 | if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / (2 * Ogre::Math::PI)) |
---|
655 | < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)) + rnd(-250, 250)) |
---|
656 | { |
---|
657 | this->target_ = (*it); |
---|
658 | this->targetPosition_ = it->getPosition(); |
---|
659 | } |
---|
660 | } |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | void ArtificialController::forgetTarget() |
---|
665 | { |
---|
666 | this->target_ = 0; |
---|
667 | this->bShooting_ = false; |
---|
668 | } |
---|
669 | |
---|
670 | void ArtificialController::aimAtTarget() |
---|
671 | { |
---|
672 | if (!this->target_ || !this->getControllableEntity()) |
---|
673 | return; |
---|
674 | |
---|
675 | static const float hardcoded_projectile_speed = 1250; |
---|
676 | |
---|
677 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); |
---|
678 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
679 | |
---|
680 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
681 | if (pawn) |
---|
682 | pawn->setAimPosition(this->targetPosition_); |
---|
683 | } |
---|
684 | |
---|
685 | bool ArtificialController::isCloseAtTarget(float distance) const |
---|
686 | { |
---|
687 | if (!this->getControllableEntity()) |
---|
688 | return false; |
---|
689 | |
---|
690 | if (!this->target_) |
---|
691 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
---|
692 | else |
---|
693 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
---|
694 | } |
---|
695 | |
---|
696 | bool ArtificialController::isLookingAtTarget(float angle) const |
---|
697 | { |
---|
698 | if (!this->getControllableEntity()) |
---|
699 | return false; |
---|
700 | |
---|
701 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
702 | } |
---|
703 | |
---|
704 | void ArtificialController::abandonTarget(Pawn* target) |
---|
705 | { |
---|
706 | if (target == this->target_) |
---|
707 | this->targetDied(); |
---|
708 | } |
---|
709 | |
---|
710 | void ArtificialController::targetDied() |
---|
711 | { |
---|
712 | this->forgetTarget(); |
---|
713 | this->searchRandomTargetPosition(); |
---|
714 | } |
---|
715 | |
---|
716 | bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
717 | { |
---|
718 | if (entity1 == entity2) |
---|
719 | return true; |
---|
720 | |
---|
721 | int team1 = -1; |
---|
722 | int team2 = -1; |
---|
723 | |
---|
724 | Controller* controller = 0; |
---|
725 | if (entity1->getController()) |
---|
726 | controller = entity1->getController(); |
---|
727 | else |
---|
728 | controller = entity1->getXMLController(); |
---|
729 | if (controller) |
---|
730 | { |
---|
731 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
732 | if (ac) |
---|
733 | team1 = ac->getTeam(); |
---|
734 | } |
---|
735 | |
---|
736 | if (entity1->getController()) |
---|
737 | controller = entity1->getController(); |
---|
738 | else |
---|
739 | controller = entity1->getXMLController(); |
---|
740 | if (controller) |
---|
741 | { |
---|
742 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
743 | if (ac) |
---|
744 | team2 = ac->getTeam(); |
---|
745 | } |
---|
746 | |
---|
747 | TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); |
---|
748 | if (tdm) |
---|
749 | { |
---|
750 | if (entity1->getPlayer()) |
---|
751 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
752 | |
---|
753 | if (entity2->getPlayer()) |
---|
754 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
755 | } |
---|
756 | |
---|
757 | TeamBaseMatchBase* base = 0; |
---|
758 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
759 | if (base) |
---|
760 | { |
---|
761 | switch (base->getState()) |
---|
762 | { |
---|
763 | case BaseState::ControlTeam1: |
---|
764 | team1 = 0; |
---|
765 | break; |
---|
766 | case BaseState::ControlTeam2: |
---|
767 | team1 = 1; |
---|
768 | break; |
---|
769 | case BaseState::Uncontrolled: |
---|
770 | default: |
---|
771 | team1 = -1; |
---|
772 | } |
---|
773 | } |
---|
774 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
775 | if (base) |
---|
776 | { |
---|
777 | switch (base->getState()) |
---|
778 | { |
---|
779 | case BaseState::ControlTeam1: |
---|
780 | team2 = 0; |
---|
781 | break; |
---|
782 | case BaseState::ControlTeam2: |
---|
783 | team2 = 1; |
---|
784 | break; |
---|
785 | case BaseState::Uncontrolled: |
---|
786 | default: |
---|
787 | team2 = -1; |
---|
788 | } |
---|
789 | } |
---|
790 | |
---|
791 | DroneController* droneController = 0; |
---|
792 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
793 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
794 | return true; |
---|
795 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
796 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
797 | return true; |
---|
798 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
799 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
800 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
801 | return true; |
---|
802 | |
---|
803 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
804 | if (dynamic) |
---|
805 | { |
---|
806 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
807 | |
---|
808 | if (entity1->getPlayer()) |
---|
809 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
810 | |
---|
811 | if (entity2->getPlayer()) |
---|
812 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
813 | |
---|
814 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
---|
815 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
816 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
817 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
818 | else return true; |
---|
819 | } |
---|
820 | |
---|
821 | return (team1 == team2 && team1 != -1); |
---|
822 | } |
---|
823 | } |
---|