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