Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/orxonox/controllers/ArtificialController.cc @ 8736

Last change on this file since 8736 was 8736, checked in by jo, 13 years ago

Bots use boost now.

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