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/CoreIncludes.h" |
---|
36 | #include "core/XMLPort.h" |
---|
37 | #include "core/command/ConsoleCommand.h" |
---|
38 | #include "worldentities/ControllableEntity.h" |
---|
39 | #include "worldentities/pawns/Pawn.h" |
---|
40 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
41 | #include "worldentities/pawns/SpaceShip.h" |
---|
42 | #include "gametypes/TeamDeathmatch.h" |
---|
43 | #include "gametypes/Dynamicmatch.h" |
---|
44 | #include "gametypes/Mission.h" |
---|
45 | #include "controllers/WaypointPatrolController.h" |
---|
46 | #include "controllers/NewHumanController.h" |
---|
47 | #include "controllers/DroneController.h" |
---|
48 | #include "weaponsystem/WeaponMode.h" |
---|
49 | #include "weaponsystem/WeaponPack.h" |
---|
50 | #include "weaponsystem/Weapon.h" |
---|
51 | #include "weaponsystem/WeaponSlot.h" |
---|
52 | #include "weaponsystem/WeaponSlot.h" |
---|
53 | |
---|
54 | namespace orxonox |
---|
55 | { |
---|
56 | SetConsoleCommand("ArtificialController", "formationflight", &ArtificialController::formationflight); |
---|
57 | SetConsoleCommand("ArtificialController", "masteraction", &ArtificialController::masteraction); |
---|
58 | SetConsoleCommand("ArtificialController", "followme", &ArtificialController::followme); |
---|
59 | SetConsoleCommand("ArtificialController", "passivebehaviour", &ArtificialController::passivebehaviour); |
---|
60 | SetConsoleCommand("ArtificialController", "formationsize", &ArtificialController::formationsize); |
---|
61 | SetConsoleCommand("ArtificialController", "setbotlevel", &ArtificialController::setAllBotLevel); |
---|
62 | |
---|
63 | static const unsigned int STANDARD_MAX_FORMATION_SIZE = 7; |
---|
64 | static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000; |
---|
65 | static const int FORMATION_LENGTH = 130; |
---|
66 | static const int FORMATION_WIDTH = 110; |
---|
67 | static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy |
---|
68 | static const float SPEED_MASTER = 0.6f; |
---|
69 | static const float ROTATEFACTOR_MASTER = 0.2f; |
---|
70 | static const float SPEED_FREE = 0.8f; |
---|
71 | static const float ROTATEFACTOR_FREE = 0.8f; |
---|
72 | |
---|
73 | |
---|
74 | ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) |
---|
75 | { |
---|
76 | RegisterObject(ArtificialController); |
---|
77 | |
---|
78 | this->target_ = 0; |
---|
79 | this->formationFlight_ = false; |
---|
80 | this->passive_ = false; |
---|
81 | this->maxFormationSize_ = STANDARD_MAX_FORMATION_SIZE; |
---|
82 | this->myMaster_ = 0; |
---|
83 | this->freedomCount_ = 0; |
---|
84 | this->team_ = -1; |
---|
85 | this->state_ = FREE; |
---|
86 | this->specificMasterAction_ = NONE; |
---|
87 | this->specificMasterActionHoldCount_ = 0; |
---|
88 | this->bShooting_ = false; |
---|
89 | this->bHasTargetPosition_ = false; |
---|
90 | this->speedCounter_ = 0.2f; |
---|
91 | this->targetPosition_ = Vector3::ZERO; |
---|
92 | |
---|
93 | this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); |
---|
94 | this->bSetupWorked = false; |
---|
95 | this->botlevel_ = 0.5f; |
---|
96 | this->mode_ = DEFAULT;////Vector-implementation: mode_.push_back(DEFAULT); |
---|
97 | this->timeout_ = 0; |
---|
98 | this->currentWaypoint_ = 0; |
---|
99 | this->setAccuracy(5); |
---|
100 | this->defaultWaypoint_ = NULL; |
---|
101 | } |
---|
102 | |
---|
103 | ArtificialController::~ArtificialController() |
---|
104 | { |
---|
105 | if (this->isInitialized()) |
---|
106 | {//Vector-implementation: mode_.erase(mode_.begin(),mode_.end()); |
---|
107 | this->waypoints_.clear(); |
---|
108 | this->removeFromFormation(); |
---|
109 | this->weaponModes_.clear(); |
---|
110 | for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it; ++it) |
---|
111 | { |
---|
112 | if (*it != this) |
---|
113 | { |
---|
114 | if (it->myMaster_ == this) |
---|
115 | { |
---|
116 | orxout(internal_error) << this << " is still master in " << (*it) << endl; |
---|
117 | it->myMaster_ = 0; |
---|
118 | } |
---|
119 | |
---|
120 | while (true) |
---|
121 | { |
---|
122 | std::vector<ArtificialController*>::iterator it2 = std::find(it->slaves_.begin(), it->slaves_.end(), this); |
---|
123 | if (it2 != it->slaves_.end()) |
---|
124 | { |
---|
125 | orxout(internal_error) << this << " is still slave in " << (*it) << endl; |
---|
126 | it->slaves_.erase(it2); |
---|
127 | } |
---|
128 | else |
---|
129 | break; |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
137 | { |
---|
138 | SUPER(ArtificialController, XMLPort, xmlelement, mode); |
---|
139 | |
---|
140 | XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1); |
---|
141 | XMLPortParam(ArtificialController, "formationFlight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(false); |
---|
142 | XMLPortParam(ArtificialController, "formationSize", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE); |
---|
143 | XMLPortParam(ArtificialController, "passive", setPassive, getPassive, xmlelement, mode).defaultValues(false); |
---|
144 | } |
---|
145 | |
---|
146 | // Documentation only here to get a faster overview for creating a useful documentation... |
---|
147 | |
---|
148 | /** |
---|
149 | @brief Activates / deactivates formationflight behaviour |
---|
150 | @param form activate formflight if form is true |
---|
151 | */ |
---|
152 | void ArtificialController::formationflight(const bool form) |
---|
153 | { |
---|
154 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
155 | { |
---|
156 | Controller* controller = 0; |
---|
157 | |
---|
158 | if (it->getController()) |
---|
159 | controller = it->getController(); |
---|
160 | else if (it->getXMLController()) |
---|
161 | controller = it->getXMLController(); |
---|
162 | |
---|
163 | if (!controller) |
---|
164 | continue; |
---|
165 | |
---|
166 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
167 | |
---|
168 | if (aiController) |
---|
169 | { |
---|
170 | aiController->formationFlight_ = form; |
---|
171 | if (!form) |
---|
172 | { |
---|
173 | aiController->removeFromFormation(); |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | @brief Get all masters to do a "specific master action" |
---|
181 | @param action which action to perform (integer, so it can be called with a console command (tmp solution)) |
---|
182 | */ |
---|
183 | void ArtificialController::masteraction(const int action) |
---|
184 | { |
---|
185 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
186 | { |
---|
187 | Controller* controller = 0; |
---|
188 | |
---|
189 | if (it->getController()) |
---|
190 | controller = it->getController(); |
---|
191 | else if (it->getXMLController()) |
---|
192 | controller = it->getXMLController(); |
---|
193 | |
---|
194 | if (!controller) |
---|
195 | continue; |
---|
196 | |
---|
197 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
198 | |
---|
199 | if(aiController && aiController->state_ == MASTER) |
---|
200 | { |
---|
201 | if (action == 1) |
---|
202 | aiController->spinInit(); |
---|
203 | if (action == 2) |
---|
204 | aiController->turn180Init(); |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | @brief A human player gets followed by its nearest master. Initiated by console command, so far intended for demonstration puproses (possible future pickup). |
---|
211 | */ |
---|
212 | void ArtificialController::followme() |
---|
213 | { |
---|
214 | |
---|
215 | Pawn *humanPawn = NULL; |
---|
216 | NewHumanController *currentHumanController = NULL; |
---|
217 | std::vector<ArtificialController*> allMasters; |
---|
218 | |
---|
219 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
220 | { |
---|
221 | Controller* controller = 0; |
---|
222 | |
---|
223 | if (it->getController()) |
---|
224 | controller = it->getController(); |
---|
225 | else if (it->getXMLController()) |
---|
226 | controller = it->getXMLController(); |
---|
227 | |
---|
228 | if (!controller) |
---|
229 | continue; |
---|
230 | |
---|
231 | currentHumanController = orxonox_cast<NewHumanController*>(controller); |
---|
232 | |
---|
233 | if(currentHumanController) humanPawn = *it; |
---|
234 | |
---|
235 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
236 | |
---|
237 | if(aiController && aiController->state_ == MASTER) |
---|
238 | allMasters.push_back(aiController); |
---|
239 | |
---|
240 | } |
---|
241 | |
---|
242 | if((humanPawn != NULL) && (allMasters.size() != 0)) |
---|
243 | { |
---|
244 | float posHuman = humanPawn->getPosition().length(); |
---|
245 | float distance = 0.0f; |
---|
246 | float minDistance = FLT_MAX; |
---|
247 | int index = 0; |
---|
248 | int i = 0; |
---|
249 | |
---|
250 | for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++) |
---|
251 | { |
---|
252 | if (!ArtificialController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue; |
---|
253 | distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); |
---|
254 | if(distance < minDistance) index = i; |
---|
255 | } |
---|
256 | allMasters[index]->followInit(humanPawn); |
---|
257 | } |
---|
258 | |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | @brief Sets shooting behaviour of pawns. |
---|
263 | @param passive if true, bots won't shoot. |
---|
264 | */ |
---|
265 | void ArtificialController::passivebehaviour(const bool passive) |
---|
266 | { |
---|
267 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
268 | { |
---|
269 | Controller* controller = 0; |
---|
270 | |
---|
271 | if (it->getController()) |
---|
272 | controller = it->getController(); |
---|
273 | else if (it->getXMLController()) |
---|
274 | controller = it->getXMLController(); |
---|
275 | |
---|
276 | if (!controller) |
---|
277 | continue; |
---|
278 | |
---|
279 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
280 | |
---|
281 | if(aiController) |
---|
282 | { |
---|
283 | aiController->passive_ = passive; |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | /** |
---|
290 | @brief Sets maximal formation size |
---|
291 | @param size maximal formation size. |
---|
292 | */ |
---|
293 | void ArtificialController::formationsize(const int size) |
---|
294 | { |
---|
295 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
296 | { |
---|
297 | Controller* controller = 0; |
---|
298 | |
---|
299 | if (it->getController()) |
---|
300 | controller = it->getController(); |
---|
301 | else if (it->getXMLController()) |
---|
302 | controller = it->getXMLController(); |
---|
303 | |
---|
304 | if (!controller) |
---|
305 | continue; |
---|
306 | |
---|
307 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
308 | |
---|
309 | if(aiController) |
---|
310 | { |
---|
311 | aiController->maxFormationSize_ = size; |
---|
312 | } |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | @brief Gets called when ControllableEntity is being changed. Resets the bot when it dies. |
---|
318 | */ |
---|
319 | void ArtificialController::changedControllableEntity() |
---|
320 | { |
---|
321 | if (!this->getControllableEntity()) |
---|
322 | this->removeFromFormation(); |
---|
323 | this->bSetupWorked = false; // reset weapon information |
---|
324 | this->setupWeapons(); |
---|
325 | } |
---|
326 | |
---|
327 | void ArtificialController::removeFromFormation() |
---|
328 | { |
---|
329 | if (this->state_ == SLAVE || this->myMaster_) // slaves can also be temporary free, so check if myMaster_ is set |
---|
330 | this->unregisterSlave(); |
---|
331 | else if (this->state_ == MASTER) |
---|
332 | this->setNewMasterWithinFormation(); |
---|
333 | } |
---|
334 | |
---|
335 | void ArtificialController::moveToPosition(const Vector3& target) |
---|
336 | { |
---|
337 | if (!this->getControllableEntity()) |
---|
338 | return; |
---|
339 | |
---|
340 | // Slave uses special movement if its master is in FOLLOW mode |
---|
341 | if(this->state_ == SLAVE && this->myMaster_ && this->myMaster_->specificMasterAction_ == FOLLOW) |
---|
342 | { |
---|
343 | // this->followForSlaves(target); |
---|
344 | // return; |
---|
345 | } |
---|
346 | |
---|
347 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
348 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
349 | |
---|
350 | |
---|
351 | if(this->state_ == FREE) |
---|
352 | { |
---|
353 | if (this->target_ || distance > 10) |
---|
354 | { |
---|
355 | // Multiply with ROTATEFACTOR_FREE to make them a bit slower |
---|
356 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x); |
---|
357 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y); |
---|
358 | } |
---|
359 | |
---|
360 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
361 | { |
---|
362 | this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance |
---|
363 | } else this->getControllableEntity()->moveFrontBack(SPEED_FREE); |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | |
---|
368 | if(this->state_ == MASTER) |
---|
369 | { |
---|
370 | if (this->target_ || distance > 10) |
---|
371 | { |
---|
372 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x); |
---|
373 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y); |
---|
374 | } |
---|
375 | |
---|
376 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
377 | { |
---|
378 | this->getControllableEntity()->moveFrontBack(-0.05f); |
---|
379 | } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
380 | } |
---|
381 | |
---|
382 | |
---|
383 | |
---|
384 | if(this->state_ == SLAVE) |
---|
385 | { |
---|
386 | |
---|
387 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x); |
---|
388 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y); |
---|
389 | |
---|
390 | if (distance < 300) |
---|
391 | { |
---|
392 | if (distance < 40) |
---|
393 | { |
---|
394 | this->getControllableEntity()->moveFrontBack(0.8f*SPEED_MASTER); |
---|
395 | } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER); |
---|
396 | |
---|
397 | } else { |
---|
398 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f); |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | if (distance < 10) |
---|
403 | { |
---|
404 | this->positionReached(); |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | void ArtificialController::absoluteMoveToPosition(const Vector3& target) |
---|
409 | { |
---|
410 | float minDistance = 40.0f; |
---|
411 | if (!this->getControllableEntity()) |
---|
412 | return; |
---|
413 | |
---|
414 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
415 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
416 | |
---|
417 | if (this->target_ || distance > minDistance) |
---|
418 | { |
---|
419 | // Multiply with ROTATEFACTOR_FREE to make them a bit slower |
---|
420 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x); |
---|
421 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y); |
---|
422 | this->getControllableEntity()->moveFrontBack(SPEED_FREE); |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | if (distance < minDistance) |
---|
427 | { |
---|
428 | this->positionReached(); |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | |
---|
433 | void ArtificialController::moveToTargetPosition() |
---|
434 | { |
---|
435 | this->moveToPosition(this->targetPosition_); |
---|
436 | } |
---|
437 | |
---|
438 | /** |
---|
439 | @brief Unregisters a slave from its master. Initiated by a slave. |
---|
440 | */ |
---|
441 | void ArtificialController::unregisterSlave() |
---|
442 | { |
---|
443 | if (this->myMaster_) |
---|
444 | { |
---|
445 | std::vector<ArtificialController*>::iterator it = std::find(this->myMaster_->slaves_.begin(), this->myMaster_->slaves_.end(), this); |
---|
446 | if (it != this->myMaster_->slaves_.end()) |
---|
447 | this->myMaster_->slaves_.erase(it); |
---|
448 | } |
---|
449 | |
---|
450 | this->myMaster_ = 0; |
---|
451 | this->state_ = FREE; |
---|
452 | } |
---|
453 | |
---|
454 | void ArtificialController::searchNewMaster() |
---|
455 | { |
---|
456 | |
---|
457 | if (!this->getControllableEntity()) |
---|
458 | return; |
---|
459 | |
---|
460 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
461 | this->forgetTarget(); |
---|
462 | int teamSize = 0; |
---|
463 | //go through all pawns |
---|
464 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
465 | { |
---|
466 | //same team? |
---|
467 | if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
468 | continue; |
---|
469 | |
---|
470 | //has it an ArtificialController? |
---|
471 | Controller* controller = 0; |
---|
472 | |
---|
473 | if (it->getController()) |
---|
474 | controller = it->getController(); |
---|
475 | else if (it->getXMLController()) |
---|
476 | controller = it->getXMLController(); |
---|
477 | |
---|
478 | if (!controller) |
---|
479 | continue; |
---|
480 | |
---|
481 | //is pawn oneself? |
---|
482 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
483 | continue; |
---|
484 | |
---|
485 | teamSize++; |
---|
486 | |
---|
487 | ArtificialController *newMaster = orxonox_cast<ArtificialController*>(controller); |
---|
488 | |
---|
489 | //is it a master? |
---|
490 | if (!newMaster || newMaster->state_ != MASTER) |
---|
491 | continue; |
---|
492 | |
---|
493 | float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
494 | |
---|
495 | // is pawn in range? |
---|
496 | if (distance < RADIUS_TO_SEARCH_FOR_MASTERS) |
---|
497 | { |
---|
498 | if(newMaster->slaves_.size() > this->maxFormationSize_) continue; |
---|
499 | |
---|
500 | for(std::vector<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++) |
---|
501 | { |
---|
502 | (*itSlave)->myMaster_ = newMaster; |
---|
503 | newMaster->slaves_.push_back(*itSlave); |
---|
504 | } |
---|
505 | this->slaves_.clear(); |
---|
506 | this->state_ = SLAVE; |
---|
507 | |
---|
508 | this->myMaster_ = newMaster; |
---|
509 | newMaster->slaves_.push_back(this); |
---|
510 | |
---|
511 | break; |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | if (this->state_ != SLAVE && teamSize != 0) |
---|
516 | { |
---|
517 | this->state_ = MASTER; |
---|
518 | this->myMaster_ = 0; |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | /** |
---|
523 | @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master. |
---|
524 | */ |
---|
525 | void ArtificialController::commandSlaves() |
---|
526 | { |
---|
527 | if(this->state_ != MASTER) return; |
---|
528 | |
---|
529 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
530 | Vector3 dest = this->getControllableEntity()->getPosition(); |
---|
531 | |
---|
532 | // 1 slave: follow |
---|
533 | if (this->slaves_.size() == 1) |
---|
534 | { |
---|
535 | dest += 4*orient*WorldEntity::BACK; |
---|
536 | this->slaves_.front()->setTargetPosition(dest); |
---|
537 | } |
---|
538 | else |
---|
539 | { |
---|
540 | dest += 1.0f*orient*WorldEntity::BACK; |
---|
541 | Vector3 pos = Vector3::ZERO; |
---|
542 | int i = 1; |
---|
543 | |
---|
544 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
545 | { |
---|
546 | pos = Vector3::ZERO; |
---|
547 | if (i <= 1) pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::LEFT); |
---|
548 | if (i == 2) pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::RIGHT); |
---|
549 | if (i == 3) pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::UP); |
---|
550 | if (i >= 4) |
---|
551 | { |
---|
552 | pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::DOWN); |
---|
553 | i = 1; |
---|
554 | dest += (float)FORMATION_LENGTH*(orient*WorldEntity::BACK); |
---|
555 | (*it)->setTargetPosition(pos); |
---|
556 | continue; |
---|
557 | } |
---|
558 | i++; |
---|
559 | (*it)->setTargetPosition(pos); |
---|
560 | } |
---|
561 | } |
---|
562 | } |
---|
563 | |
---|
564 | /** |
---|
565 | @brief Sets a new master within the formation. Called by a master. |
---|
566 | */ |
---|
567 | void ArtificialController::setNewMasterWithinFormation() |
---|
568 | { |
---|
569 | if(this->state_ != MASTER) return; |
---|
570 | |
---|
571 | if (!this->slaves_.empty()) |
---|
572 | { |
---|
573 | ArtificialController *newMaster = this->slaves_.back(); |
---|
574 | this->slaves_.pop_back(); |
---|
575 | |
---|
576 | newMaster->state_ = MASTER; |
---|
577 | newMaster->slaves_ = this->slaves_; |
---|
578 | newMaster->myMaster_ = 0; |
---|
579 | |
---|
580 | for(std::vector<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++) |
---|
581 | { |
---|
582 | (*it)->myMaster_ = newMaster; |
---|
583 | } |
---|
584 | } |
---|
585 | |
---|
586 | this->slaves_.clear(); |
---|
587 | this->specificMasterAction_ = NONE; |
---|
588 | this->state_ = FREE; |
---|
589 | } |
---|
590 | |
---|
591 | /** |
---|
592 | @brief Frees all slaves form a master. Initiated by a master. |
---|
593 | */ |
---|
594 | void ArtificialController::freeSlaves() |
---|
595 | { |
---|
596 | if(this->state_ != MASTER) return; |
---|
597 | |
---|
598 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
599 | { |
---|
600 | (*it)->state_ = FREE; |
---|
601 | (*it)->myMaster_ = 0; |
---|
602 | } |
---|
603 | this->slaves_.clear(); |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | @brief Master sets its slaves free for @ref FREEDOM_COUNT seconds. |
---|
608 | */ |
---|
609 | void ArtificialController::forceFreeSlaves() |
---|
610 | { |
---|
611 | if(this->state_ != MASTER) return; |
---|
612 | |
---|
613 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
614 | { |
---|
615 | (*it)->state_ = FREE; |
---|
616 | (*it)->forceFreedom(); |
---|
617 | (*it)->targetPosition_ = this->targetPosition_; |
---|
618 | (*it)->bShooting_ = true; |
---|
619 | // (*it)->getControllableEntity()->fire(0);// fire once for fun |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | void ArtificialController::loseMasterState() |
---|
624 | { |
---|
625 | this->freeSlaves(); |
---|
626 | this->state_ = FREE; |
---|
627 | } |
---|
628 | |
---|
629 | |
---|
630 | void ArtificialController::forceFreedom() |
---|
631 | { |
---|
632 | this->freedomCount_ = FREEDOM_COUNT; |
---|
633 | } |
---|
634 | |
---|
635 | /** |
---|
636 | @brief Checks wether caller has been forced free, decrements time to stay forced free. |
---|
637 | @return true if forced free. |
---|
638 | */ |
---|
639 | bool ArtificialController::forcedFree() |
---|
640 | { |
---|
641 | if(this->freedomCount_ > 0) |
---|
642 | { |
---|
643 | this->freedomCount_--; |
---|
644 | return true; |
---|
645 | } else return false; |
---|
646 | } |
---|
647 | |
---|
648 | /** |
---|
649 | @brief Used to continue a "specific master action" for a certain time and resuming normal behaviour after. |
---|
650 | */ |
---|
651 | void ArtificialController::specificMasterActionHold() |
---|
652 | { |
---|
653 | if(this->state_ != MASTER) return; |
---|
654 | |
---|
655 | if (specificMasterActionHoldCount_ == 0) |
---|
656 | { |
---|
657 | this->specificMasterAction_ = NONE; |
---|
658 | this->searchNewTarget(); |
---|
659 | } |
---|
660 | else specificMasterActionHoldCount_--; |
---|
661 | } |
---|
662 | |
---|
663 | /** |
---|
664 | @brief Master initializes a 180 degree turn. Leads to a "specific master action". |
---|
665 | */ |
---|
666 | void ArtificialController::turn180Init() |
---|
667 | { |
---|
668 | if(this->state_ != MASTER) return; |
---|
669 | |
---|
670 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
671 | |
---|
672 | this->setTargetPosition(this->getControllableEntity()->getPosition() + 1000.0f*orient*WorldEntity::BACK); |
---|
673 | |
---|
674 | this->specificMasterActionHoldCount_ = 4; |
---|
675 | |
---|
676 | this->specificMasterAction_ = TURN180; |
---|
677 | } |
---|
678 | |
---|
679 | /** |
---|
680 | @brief Execute the 180 degree turn. Called within tick. |
---|
681 | */ |
---|
682 | void ArtificialController::turn180() |
---|
683 | { |
---|
684 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); |
---|
685 | |
---|
686 | this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); |
---|
687 | this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); |
---|
688 | |
---|
689 | this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
690 | } |
---|
691 | |
---|
692 | /** |
---|
693 | @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action". |
---|
694 | */ |
---|
695 | void ArtificialController::spinInit() |
---|
696 | { |
---|
697 | if(this->state_ != MASTER) return; |
---|
698 | this->specificMasterAction_ = SPIN; |
---|
699 | this->specificMasterActionHoldCount_ = 10; |
---|
700 | } |
---|
701 | |
---|
702 | /** |
---|
703 | @brief Execute the spin. Called within tick. |
---|
704 | */ |
---|
705 | void ArtificialController::spin() |
---|
706 | { |
---|
707 | this->moveToTargetPosition(); |
---|
708 | this->getControllableEntity()->rotateRoll(0.8f); |
---|
709 | } |
---|
710 | |
---|
711 | /** |
---|
712 | @brief Master begins to follow a pawn. Is a "specific master action". |
---|
713 | @param pawn pawn to follow. |
---|
714 | @param always follows pawn forever if true (false if omitted). |
---|
715 | @param secondsToFollow seconds to follow the pawn if always is false. Will follow pawn 100 seconds if omitted (set in header). |
---|
716 | */ |
---|
717 | void ArtificialController::followInit(Pawn* pawn, const bool always, const int secondsToFollow) |
---|
718 | { |
---|
719 | if (pawn == NULL || this->state_ != MASTER) |
---|
720 | return; |
---|
721 | this->specificMasterAction_ = FOLLOW; |
---|
722 | |
---|
723 | this->setTarget(pawn); |
---|
724 | if (!always) |
---|
725 | this->specificMasterActionHoldCount_ = secondsToFollow; |
---|
726 | else |
---|
727 | this->specificMasterActionHoldCount_ = INT_MAX; //for now... |
---|
728 | |
---|
729 | } |
---|
730 | |
---|
731 | |
---|
732 | /** |
---|
733 | @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action". |
---|
734 | */ |
---|
735 | void ArtificialController::followRandomHumanInit() |
---|
736 | { |
---|
737 | |
---|
738 | Pawn *humanPawn = NULL; |
---|
739 | NewHumanController *currentHumanController = NULL; |
---|
740 | |
---|
741 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
742 | { |
---|
743 | if (!it->getController()) |
---|
744 | continue; |
---|
745 | |
---|
746 | currentHumanController = orxonox_cast<NewHumanController*>(it->getController()); |
---|
747 | if(currentHumanController) |
---|
748 | { |
---|
749 | if (!ArtificialController::sameTeam(this->getControllableEntity(), *it, this->getGametype())) continue; |
---|
750 | humanPawn = *it; |
---|
751 | break; |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | if((humanPawn != NULL)) |
---|
756 | this->followInit(humanPawn); |
---|
757 | } |
---|
758 | |
---|
759 | /** |
---|
760 | @brief Master follows target with adjusted speed. Called within tick. |
---|
761 | */ |
---|
762 | void ArtificialController::follow() |
---|
763 | { |
---|
764 | if (this->target_) |
---|
765 | this->moveToPosition(this->target_->getPosition()); |
---|
766 | else |
---|
767 | this->specificMasterActionHoldCount_ = 0; |
---|
768 | /* |
---|
769 | if (!this->getControllableEntity()) |
---|
770 | return; |
---|
771 | |
---|
772 | float distance = (this->target_->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
773 | |
---|
774 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->target_->getPosition()); |
---|
775 | |
---|
776 | |
---|
777 | this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); |
---|
778 | this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); |
---|
779 | |
---|
780 | float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->target_->getVelocity().squaredLength(); |
---|
781 | |
---|
782 | orxout() << "~follow distance: " << distance << "SpeedCounter: " << this->speedCounter_ << "~speedDiv: " << speedDiv << endl; |
---|
783 | if (distance < 800) |
---|
784 | { |
---|
785 | if (distance < 200) |
---|
786 | { |
---|
787 | this->speedCounter_ -= 0.5f; |
---|
788 | if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f; |
---|
789 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
790 | } else { |
---|
791 | if(speedDiv < 0) |
---|
792 | this->speedCounter_ += 0.01f; |
---|
793 | else |
---|
794 | this->speedCounter_ -= 0.05f; |
---|
795 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
796 | } |
---|
797 | |
---|
798 | } else { |
---|
799 | this->speedCounter_ += 0.05f; |
---|
800 | this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f); |
---|
801 | } |
---|
802 | // if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0; |
---|
803 | |
---|
804 | */ |
---|
805 | } |
---|
806 | |
---|
807 | |
---|
808 | /** |
---|
809 | @brief Slave moving behaviour when master is following a pawn, gets redirected from moveToPosition(const Vector3& target)). Called within tick. |
---|
810 | */ |
---|
811 | void ArtificialController::followForSlaves(const Vector3& target) |
---|
812 | { |
---|
813 | |
---|
814 | /* |
---|
815 | if (!this->getControllableEntity() && !this->myMaster_ && this->myMaster_->state_ != FOLLOW && !this->myMaster_->target_) |
---|
816 | return; |
---|
817 | |
---|
818 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
819 | |
---|
820 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
821 | |
---|
822 | |
---|
823 | this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); |
---|
824 | this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); |
---|
825 | |
---|
826 | |
---|
827 | float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->myMaster_->target_->getVelocity().squaredLength(); |
---|
828 | |
---|
829 | |
---|
830 | if (distance < 800) |
---|
831 | { |
---|
832 | if (distance < 200) |
---|
833 | { |
---|
834 | this->speedCounter_ -= 5.0f; |
---|
835 | if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f; |
---|
836 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
837 | } else { |
---|
838 | if(speedDiv < 0) |
---|
839 | this->speedCounter_ += 0.01f; |
---|
840 | else |
---|
841 | this->speedCounter_ -= 0.05f; |
---|
842 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
843 | } |
---|
844 | |
---|
845 | } else { |
---|
846 | this->speedCounter_ += 0.05f; |
---|
847 | this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f); |
---|
848 | } |
---|
849 | // if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0; |
---|
850 | */ |
---|
851 | } |
---|
852 | |
---|
853 | |
---|
854 | void ArtificialController::setTargetPosition(const Vector3& target) |
---|
855 | { |
---|
856 | this->targetPosition_ = target; |
---|
857 | this->bHasTargetPosition_ = true; |
---|
858 | } |
---|
859 | |
---|
860 | void ArtificialController::searchRandomTargetPosition() |
---|
861 | { |
---|
862 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
863 | this->bHasTargetPosition_ = true; |
---|
864 | } |
---|
865 | |
---|
866 | void ArtificialController::setTarget(Pawn* target) |
---|
867 | { |
---|
868 | this->target_ = target; |
---|
869 | |
---|
870 | if (target) |
---|
871 | this->targetPosition_ = target->getPosition(); |
---|
872 | } |
---|
873 | |
---|
874 | void ArtificialController::searchNewTarget() |
---|
875 | { |
---|
876 | if (!this->getControllableEntity()) |
---|
877 | return; |
---|
878 | |
---|
879 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
880 | this->forgetTarget(); |
---|
881 | |
---|
882 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
883 | { |
---|
884 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
885 | continue; |
---|
886 | |
---|
887 | /* So AI won't choose invisible Spaceships as target */ |
---|
888 | if (!it->getRadarVisibility()) |
---|
889 | continue; |
---|
890 | |
---|
891 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
892 | { |
---|
893 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
894 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
895 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
896 | if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / math::twoPi) |
---|
897 | < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / math::twoPi) + rnd(-250, 250)) |
---|
898 | { |
---|
899 | this->target_ = (*it); |
---|
900 | this->targetPosition_ = it->getPosition(); |
---|
901 | } |
---|
902 | } |
---|
903 | } |
---|
904 | } |
---|
905 | |
---|
906 | void ArtificialController::forgetTarget() |
---|
907 | { |
---|
908 | this->target_ = 0; |
---|
909 | this->bShooting_ = false; |
---|
910 | } |
---|
911 | |
---|
912 | void ArtificialController::aimAtTarget() |
---|
913 | { |
---|
914 | if (!this->target_ || !this->getControllableEntity()) |
---|
915 | return; |
---|
916 | |
---|
917 | static const float hardcoded_projectile_speed = 1250; |
---|
918 | |
---|
919 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); |
---|
920 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
921 | |
---|
922 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
923 | if (pawn) |
---|
924 | pawn->setAimPosition(this->targetPosition_); |
---|
925 | } |
---|
926 | |
---|
927 | bool ArtificialController::isCloseAtTarget(float distance) const |
---|
928 | { |
---|
929 | if (!this->getControllableEntity()) |
---|
930 | return false; |
---|
931 | |
---|
932 | if (!this->target_) |
---|
933 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
---|
934 | else |
---|
935 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
---|
936 | } |
---|
937 | |
---|
938 | bool ArtificialController::isLookingAtTarget(float angle) const |
---|
939 | { |
---|
940 | if (!this->getControllableEntity()) |
---|
941 | return false; |
---|
942 | |
---|
943 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
944 | } |
---|
945 | |
---|
946 | void ArtificialController::abandonTarget(Pawn* target) |
---|
947 | { |
---|
948 | if (target == this->target_) |
---|
949 | this->targetDied(); |
---|
950 | } |
---|
951 | |
---|
952 | void ArtificialController::targetDied() |
---|
953 | { |
---|
954 | this->forgetTarget(); |
---|
955 | this->searchRandomTargetPosition(); |
---|
956 | } |
---|
957 | |
---|
958 | bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
959 | { |
---|
960 | if(!entity1 || !entity2) |
---|
961 | return true; |
---|
962 | if (entity1 == entity2) |
---|
963 | return true; |
---|
964 | |
---|
965 | int team1 = -1; |
---|
966 | int team2 = -1; |
---|
967 | |
---|
968 | Controller* controller = 0; |
---|
969 | if (entity1->getController()) |
---|
970 | controller = entity1->getController(); |
---|
971 | else |
---|
972 | controller = entity1->getXMLController(); |
---|
973 | if (controller) |
---|
974 | { |
---|
975 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
976 | if (ac) |
---|
977 | team1 = ac->getTeam(); |
---|
978 | } |
---|
979 | |
---|
980 | if (entity2->getController()) |
---|
981 | controller = entity2->getController(); |
---|
982 | else |
---|
983 | controller = entity2->getXMLController(); |
---|
984 | if (controller) |
---|
985 | { |
---|
986 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
987 | if (ac) |
---|
988 | team2 = ac->getTeam(); |
---|
989 | } |
---|
990 | |
---|
991 | TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); |
---|
992 | if (tdm) |
---|
993 | { |
---|
994 | if (entity1->getPlayer()) |
---|
995 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
996 | |
---|
997 | if (entity2->getPlayer()) |
---|
998 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
999 | } |
---|
1000 | |
---|
1001 | Mission* miss = orxonox_cast<Mission*>(gametype); |
---|
1002 | if (miss) |
---|
1003 | { |
---|
1004 | if (entity1->getPlayer()) |
---|
1005 | team1 = miss->getTeam(entity1->getPlayer()); |
---|
1006 | |
---|
1007 | if (entity2->getPlayer()) |
---|
1008 | team2 = miss->getTeam(entity2->getPlayer()); |
---|
1009 | } |
---|
1010 | |
---|
1011 | TeamBaseMatchBase* base = 0; |
---|
1012 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
1013 | if (base) |
---|
1014 | { |
---|
1015 | switch (base->getState()) |
---|
1016 | { |
---|
1017 | case BaseState::ControlTeam1: |
---|
1018 | team1 = 0; |
---|
1019 | break; |
---|
1020 | case BaseState::ControlTeam2: |
---|
1021 | team1 = 1; |
---|
1022 | break; |
---|
1023 | case BaseState::Uncontrolled: |
---|
1024 | default: |
---|
1025 | team1 = -1; |
---|
1026 | } |
---|
1027 | } |
---|
1028 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
1029 | if (base) |
---|
1030 | { |
---|
1031 | switch (base->getState()) |
---|
1032 | { |
---|
1033 | case BaseState::ControlTeam1: |
---|
1034 | team2 = 0; |
---|
1035 | break; |
---|
1036 | case BaseState::ControlTeam2: |
---|
1037 | team2 = 1; |
---|
1038 | break; |
---|
1039 | case BaseState::Uncontrolled: |
---|
1040 | default: |
---|
1041 | team2 = -1; |
---|
1042 | } |
---|
1043 | } |
---|
1044 | |
---|
1045 | DroneController* droneController = 0; |
---|
1046 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
1047 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
1048 | return true; |
---|
1049 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
1050 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
1051 | return true; |
---|
1052 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
1053 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
1054 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
1055 | return true; |
---|
1056 | |
---|
1057 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
1058 | if (dynamic) |
---|
1059 | { |
---|
1060 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
1061 | |
---|
1062 | if (entity1->getPlayer()) |
---|
1063 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
1064 | |
---|
1065 | if (entity2->getPlayer()) |
---|
1066 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
1067 | |
---|
1068 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
---|
1069 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
1070 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
1071 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
1072 | else return true; |
---|
1073 | } |
---|
1074 | |
---|
1075 | return (team1 == team2 && team1 != -1); |
---|
1076 | } |
---|
1077 | |
---|
1078 | /** |
---|
1079 | @brief DoFire is called when a bot should shoot and decides which weapon is used and whether the bot shoots at all. |
---|
1080 | */ |
---|
1081 | void ArtificialController::doFire() |
---|
1082 | { |
---|
1083 | if(!this->bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ... |
---|
1084 | { |
---|
1085 | this->setupWeapons(); |
---|
1086 | } |
---|
1087 | else if(this->getControllableEntity() && weaponModes_.size()&&this->bShooting_ && this->isCloseAtTarget((1 + 2*botlevel_)*1000) && this->isLookingAtTarget(math::pi / 20.0f)) |
---|
1088 | { |
---|
1089 | int firemode; |
---|
1090 | float random = rnd(1);// |
---|
1091 | if (this->isCloseAtTarget(130) && (firemode = getFiremode("LightningGun")) > -1 ) |
---|
1092 | {//LENSFLARE: short range weapon |
---|
1093 | this->getControllableEntity()->fire(firemode); //ai uses lens flare if they're close enough to the target |
---|
1094 | } |
---|
1095 | else if( this->isCloseAtTarget(400) && (random < this->botlevel_) && (firemode = getFiremode("RocketFire")) > -1 ) |
---|
1096 | {//ROCKET: mid range weapon |
---|
1097 | this->mode_ = ROCKET; //Vector-implementation: mode_.push_back(ROCKET); |
---|
1098 | this->getControllableEntity()->fire(firemode); //launch rocket |
---|
1099 | if(this->getControllableEntity() && this->target_) //after fire(3) is called, getControllableEntity() refers to the rocket! |
---|
1100 | { |
---|
1101 | float speed = this->getControllableEntity()->getVelocity().length() - target_->getVelocity().length(); |
---|
1102 | if(!speed) speed = 0.1f; |
---|
1103 | float distance = target_->getPosition().length() - this->getControllableEntity()->getPosition().length(); |
---|
1104 | this->timeout_= distance/speed*sgn(speed*distance) + 1.8f; //predicted time of target hit (+ tolerance) |
---|
1105 | } |
---|
1106 | else |
---|
1107 | this->timeout_ = 4.0f; //TODO: find better default value |
---|
1108 | } |
---|
1109 | else if ((firemode = getFiremode("HsW01")) > -1 ) //LASER: default weapon |
---|
1110 | this->getControllableEntity()->fire(firemode); |
---|
1111 | } |
---|
1112 | } |
---|
1113 | |
---|
1114 | /** |
---|
1115 | @brief Information gathering: Which weapons are ready to use? |
---|
1116 | */ |
---|
1117 | void ArtificialController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions) |
---|
1118 | { |
---|
1119 | this->bSetupWorked = false; |
---|
1120 | if(this->getControllableEntity()) |
---|
1121 | { |
---|
1122 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
1123 | if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip |
---|
1124 | { |
---|
1125 | this->weaponModes_.clear(); // reset previous weapon information |
---|
1126 | WeaponSlot* wSlot = 0; |
---|
1127 | for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++) |
---|
1128 | { |
---|
1129 | WeaponMode* wMode = 0; |
---|
1130 | for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++) |
---|
1131 | { |
---|
1132 | std::string wName = wMode->getIdentifier()->getName(); |
---|
1133 | if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new" |
---|
1134 | weaponModes_[wName] = wMode->getMode(); |
---|
1135 | } |
---|
1136 | } |
---|
1137 | if(weaponModes_.size())//at least one weapon detected |
---|
1138 | this->bSetupWorked = true; |
---|
1139 | }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user); |
---|
1140 | } |
---|
1141 | } |
---|
1142 | |
---|
1143 | |
---|
1144 | void ArtificialController::setBotLevel(float level) |
---|
1145 | { |
---|
1146 | if (level < 0.0f) |
---|
1147 | this->botlevel_ = 0.0f; |
---|
1148 | else if (level > 1.0f) |
---|
1149 | this->botlevel_ = 1.0f; |
---|
1150 | else |
---|
1151 | this->botlevel_ = level; |
---|
1152 | } |
---|
1153 | |
---|
1154 | void ArtificialController::setAllBotLevel(float level) |
---|
1155 | { |
---|
1156 | for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it != ObjectList<ArtificialController>::end(); ++it) |
---|
1157 | it->setBotLevel(level); |
---|
1158 | } |
---|
1159 | |
---|
1160 | void ArtificialController::setPreviousMode() |
---|
1161 | { |
---|
1162 | this->mode_ = DEFAULT; //Vector-implementation: mode_.pop_back(); |
---|
1163 | } |
---|
1164 | |
---|
1165 | /** |
---|
1166 | @brief Manages boost. Switches between boost usage and boost safe mode. |
---|
1167 | */ |
---|
1168 | void ArtificialController::boostControl() |
---|
1169 | { |
---|
1170 | SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity()); |
---|
1171 | if(ship == NULL) return; |
---|
1172 | if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost |
---|
1173 | this->getControllableEntity()->boost(true); |
---|
1174 | else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost |
---|
1175 | this->getControllableEntity()->boost(false); |
---|
1176 | } |
---|
1177 | |
---|
1178 | int ArtificialController::getFiremode(std::string name) |
---|
1179 | { |
---|
1180 | for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it) |
---|
1181 | { |
---|
1182 | if (it->first == name) |
---|
1183 | return it->second; |
---|
1184 | } |
---|
1185 | return -1; |
---|
1186 | } |
---|
1187 | |
---|
1188 | void ArtificialController::addWaypoint(WorldEntity* waypoint) |
---|
1189 | { |
---|
1190 | this->waypoints_.push_back(waypoint); |
---|
1191 | } |
---|
1192 | |
---|
1193 | WorldEntity* ArtificialController::getWaypoint(unsigned int index) const |
---|
1194 | { |
---|
1195 | if (index < this->waypoints_.size()) |
---|
1196 | return this->waypoints_[index]; |
---|
1197 | else |
---|
1198 | return 0; |
---|
1199 | } |
---|
1200 | |
---|
1201 | /** |
---|
1202 | @brief Adds first waypoint of type name to the waypoint stack, which is within the searchDistance |
---|
1203 | @param name object-name of a point of interest (e.g. "PickupSpawner", "ForceField") |
---|
1204 | */ |
---|
1205 | void ArtificialController::updatePointsOfInterest(std::string name, float searchDistance) |
---|
1206 | { |
---|
1207 | WorldEntity* waypoint = NULL; |
---|
1208 | for (ObjectList<WorldEntity>::iterator it = ObjectList<WorldEntity>::begin(); it != ObjectList<WorldEntity>::end(); ++it) |
---|
1209 | { |
---|
1210 | if((*it)->getIdentifier() == ClassByString(name)) |
---|
1211 | { |
---|
1212 | ControllableEntity* controllable = this->getControllableEntity(); |
---|
1213 | if(!controllable) continue; |
---|
1214 | float actualDistance = ( (*it)->getPosition() - controllable->getPosition() ).length(); |
---|
1215 | if(actualDistance > searchDistance || actualDistance < 5.0f) continue; |
---|
1216 | // TODO: PickupSpawner: adjust waypoint accuracy to PickupSpawner's triggerdistance |
---|
1217 | // TODO: ForceField: analyze is angle between forcefield boost and own flying direction is acceptable |
---|
1218 | else |
---|
1219 | { |
---|
1220 | waypoint = *it; |
---|
1221 | break; |
---|
1222 | } |
---|
1223 | } |
---|
1224 | } |
---|
1225 | if(waypoint) |
---|
1226 | this->waypoints_.push_back(waypoint); |
---|
1227 | } |
---|
1228 | |
---|
1229 | /** |
---|
1230 | @brief Adds point of interest depending on context. Further Possibilites: "ForceField", "PortalEndPoint", "MovableEntity", "Dock" |
---|
1231 | */ |
---|
1232 | void ArtificialController::manageWaypoints() |
---|
1233 | { |
---|
1234 | if(!defaultWaypoint_) |
---|
1235 | this->updatePointsOfInterest("PickupSpawner", 200.0f); // long search radius if there is no default goal |
---|
1236 | else |
---|
1237 | this->updatePointsOfInterest("PickupSpawner", 20.0f); // take pickup en passant if there is a default waypoint |
---|
1238 | } |
---|
1239 | |
---|
1240 | } |
---|