Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/controllers/FormationController.cc @ 9256

Last change on this file since 9256 was 9256, checked in by landauf, 12 years ago

I think this implementation of the hit() function rather belongs to FormationController than to HumanController

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