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