Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/controllers/ArtificialController.cc @ 7106

Last change on this file since 7106 was 7106, checked in by landauf, 14 years ago

fixed a crash: if the player dies, the AI shouldn't follow him anymore

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