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