Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/waypoints/src/orxonox/controllers/FormationController.cc @ 9043

Last change on this file since 9043 was 9043, checked in by scmoritz, 12 years ago
File size: 36.0 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 <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 int FORMATION_LENGTH =  110;
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  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  void FormationController::removeFromFormation()
255    {
256        if (this->state_ == SLAVE || this->myMaster_) // slaves can also be temporary free, so check if myMaster_ is set
257            this->unregisterSlave();
258        else if (this->state_ == MASTER)
259            this->setNewMasterWithinFormation();
260    }
261
262    void FormationController::moveToPosition(const Vector3& target)
263    {
264        if (!this->getControllableEntity())
265            return;
266
267        // Slave uses special movement if its master is in FOLLOW mode
268        if(this->state_ == SLAVE && this->myMaster_ && this->myMaster_->specificMasterAction_ == FOLLOW)
269        {
270//             this->followForSlaves(target);
271//             return;
272        }
273
274        Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
275        float distance = (target - this->getControllableEntity()->getPosition()).length();
276                if(coord.x < 0.01 && coord.y < 0.01)
277                {
278                        // I AM HERE
279                        //this->getControllableEntity()->setOrientation() 
280                }
281
282        if(this->state_ == FREE)
283        {
284            if (this->target_ || distance > 10)
285            {
286                // Multiply with ROTATEFACTOR_FREE to make them a bit slower
287                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x);
288                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y);
289            }
290
291            if (this->target_ && distance <  200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
292            {
293              this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
294            } else this->getControllableEntity()->moveFrontBack(SPEED_FREE);
295        }
296
297
298
299        if(this->state_ == MASTER)
300        {
301            if (this->target_ || distance > 10)
302            {
303                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
304                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
305            }
306
307            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
308            {
309                this->getControllableEntity()->moveFrontBack(-0.05f);
310            } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
311        }
312
313
314
315        if(this->state_ == SLAVE)
316        {
317
318           this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
319           this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
320
321            if (distance < 300)
322            {
323                 if (bHasTargetOrientation_)
324                    {
325                        copyTargetOrientation();
326                    }
327                if (distance < 100)
328                {   //linear speed reduction
329                    this->getControllableEntity()->moveFrontBack(distance/100.0f*0.4f*SPEED_MASTER);
330                   
331                } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER);
332
333            } else {
334                this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f);
335            }
336        }
337
338        if (distance < 10)
339        {
340            this->positionReached();
341            bHasTargetOrientation_=false;
342        }
343    }
344
345
346
347  void FormationController::moveToTargetPosition()
348    {
349        this->moveToPosition(this->targetPosition_);
350    }
351
352  //copy the Roll orientation of given Quaternion.
353  void FormationController::copyOrientation(const Quaternion& orient)
354    {
355        //roll angle difference in radian
356        float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians());
357        while(diff>math::twoPi) diff-=math::twoPi;
358        while(diff<-math::twoPi) diff+=math::twoPi;
359        this->getControllableEntity()->rotateRoll(diff*ROTATEFACTOR_MASTER);
360    }
361
362    void FormationController::copyTargetOrientation()
363    {
364        if (bHasTargetOrientation_)
365        {
366            copyOrientation(targetOrientation_);
367        }
368    }
369
370
371   /**
372        @brief Unregisters a slave from its master. Initiated by a slave.
373    */
374    void FormationController::unregisterSlave()
375    {
376        if (this->myMaster_)
377        {
378            std::vector<FormationController*>::iterator it = std::find(this->myMaster_->slaves_.begin(), this->myMaster_->slaves_.end(), this);
379            if (it != this->myMaster_->slaves_.end())
380                this->myMaster_->slaves_.erase(it);
381        }
382
383        this->myMaster_ = 0;
384        this->state_ = FREE;
385    }
386
387    void FormationController::searchNewMaster()
388    {
389        if (this->state_==SLAVE) 
390           return;
391        if (!this->getControllableEntity())
392            return;
393
394        this->targetPosition_ = this->getControllableEntity()->getPosition();
395        this->forgetTarget();
396        int teamSize = 0;
397        //go through all pawns
398        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
399        {
400           
401            //same team?
402            Gametype* gt=this->getGametype();
403            if (!gt)
404            {
405                gt=it->getGametype();
406            }
407            if (!FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it),gt))
408                continue;
409
410            //has it an FormationController?
411            Controller* controller = 0;
412
413            if (it->getController())
414                controller = it->getController();
415            else if (it->getXMLController())
416                controller = it->getXMLController();
417
418            if (!controller)
419                continue;
420
421            //is pawn oneself?
422            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
423                continue;
424
425            teamSize++;
426
427            FormationController *newMaster = orxonox_cast<FormationController*>(controller);
428
429            //is it a master?
430            if (!newMaster || newMaster->state_ != MASTER)
431                continue;
432
433            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
434
435            // is pawn in range?
436            if (distance < RADIUS_TO_SEARCH_FOR_MASTERS)
437            {
438                if(newMaster->slaves_.size() > this->maxFormationSize_) continue;
439
440                for(std::vector<FormationController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
441                {
442                    (*itSlave)->myMaster_ = newMaster;
443                    newMaster->slaves_.push_back(*itSlave);
444                }
445                this->slaves_.clear();
446                this->state_ = SLAVE;
447
448                this->myMaster_ = newMaster;
449                newMaster->slaves_.push_back(this);
450
451                break;
452            }
453        }
454
455        if (this->state_ != SLAVE  && teamSize != 0)
456        {
457            this->state_ = MASTER;
458            this->myMaster_ = 0;
459        }
460    }
461 /**
462        @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master.
463    */
464
465void FormationController::commandSlaves()
466    {
467        if(this->state_ != MASTER) return;
468
469        Quaternion orient = this->getControllableEntity()->getOrientation();
470        Vector3 dest = this->getControllableEntity()->getPosition();
471
472        // 1 slave: follow
473        if (this->slaves_.size() == 1)
474        {
475            dest += 4*orient*WorldEntity::BACK;
476            this->slaves_.front()->setTargetPosition(dest);
477        }
478        else
479        // formation:
480        {
481            dest += 1.0f*orient*WorldEntity::BACK;
482            Vector3 pos = Vector3::ZERO;
483                 bool left=true;
484            int i = 1;
485           
486            for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
487            {
488                pos = Vector3::ZERO;
489                if (left)
490                {
491                    pos+=dest+i*FORMATION_WIDTH*(orient*WorldEntity::LEFT);
492                } else{
493                    pos+=dest+i*FORMATION_WIDTH*(orient*WorldEntity::RIGHT);
494                    i++;
495                    dest+=FORMATION_LENGTH*(orient*WorldEntity::BACK);
496                }               
497                (*it)->setTargetOrientation(orient);
498                (*it)->setTargetPosition(pos);
499                left=!left;
500            }
501        }
502    }
503
504    /**
505        @brief Sets a new master within the formation. Called by a master.
506    */
507    void FormationController::setNewMasterWithinFormation()
508    {
509        if(this->state_ != MASTER) return;
510
511        if (!this->slaves_.empty())
512        {
513            FormationController *newMaster = this->slaves_.back();
514            this->slaves_.pop_back();
515
516            newMaster->state_ = MASTER;
517            newMaster->slaves_ = this->slaves_;
518            newMaster->myMaster_ = 0;
519
520            for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
521            {
522                (*it)->myMaster_ = newMaster;
523            }
524        }
525
526        this->slaves_.clear();
527        this->specificMasterAction_ = NONE;
528        this->state_ = FREE;
529    }
530
531
532  /**
533        @brief Frees all slaves form a master. Initiated by a master.
534    */
535    void FormationController::freeSlaves()
536    {
537        if(this->state_ != MASTER) return;
538
539        for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
540        {
541            (*it)->state_ = FREE;
542            (*it)->myMaster_ = 0;
543        }
544        this->slaves_.clear();
545    }
546
547    /**
548        @brief Master sets its slaves free for @ref FREEDOM_COUNT seconds.
549    */
550    void FormationController::forceFreeSlaves()
551    {
552        if(this->state_ != MASTER) return;
553
554        for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
555        {
556            (*it)->state_ = FREE;
557            (*it)->forceFreedom();
558            (*it)->targetPosition_ = this->targetPosition_;
559            (*it)->bShooting_ = true;
560//             (*it)->getControllableEntity()->fire(0);// fire once for fun
561        }
562    }
563
564    void FormationController::loseMasterState()
565    {
566        this->freeSlaves();
567        this->state_ = FREE;
568    }
569
570
571    void FormationController::forceFreedom()
572    {
573        this->freedomCount_ = FREEDOM_COUNT;
574    }
575
576    /**
577        @brief Checks wether caller has been forced free, decrements time to stay forced free.
578        @return true if forced free.
579    */
580    bool FormationController::forcedFree()
581    {
582        if(this->freedomCount_ > 0)
583        {
584            this->freedomCount_--;
585            return true;
586        } else return false;
587    }
588
589
590    /**
591        @brief Call to take the lead of formation (if free, become slave of nearest formation, then, if Slave, become Master)
592    */
593    void FormationController::takeLeadOfFormation()
594    {
595        if (!this->getControllableEntity() || this->state_==MASTER)
596            return;
597
598        //search new Master, then take lead
599        if (this->state_==FREE && this->myMaster_==0)
600        {
601          searchNewMaster();
602        }
603
604        if (this->state_==SLAVE)  //become master of this formation
605        {   
606            this->slaves_=this->myMaster_->slaves_;
607            this->myMaster_->slaves_.clear();
608            this->myMaster_->state_=SLAVE;
609            this->myMaster_->myMaster_=this;
610           
611            //delete myself in slavelist
612            std::vector<FormationController*>::iterator it2 = std::find(this->slaves_.begin(), this->slaves_.end(), this);
613            if (it2 != this->slaves_.end())
614            {
615                 this->slaves_.erase(it2);
616            }
617            //add previous master
618            this->slaves_.push_back(this->myMaster_);
619            //set this as new master
620            for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
621            {
622                 (*it)->myMaster_=this;
623            }
624            this->myMaster_=0;
625            this->state_=MASTER;
626        }
627        /*/debug
628        if (this->state_==SLAVE)
629           {orxout(debug_output) << this << " is slave "<< endl;}
630        else if (this->state_==MASTER)
631           {orxout(debug_output) << this << " is now a master of "<<this->slaves_.size()<<" slaves."<< endl;}
632        if (this->state_==FREE)
633           {orxout(debug_output) << this << " is free "<< endl;}*/
634    }
635    /**
636      @brief if called, half of the formation will attack the originator
637    */
638    void FormationController::masterAttacked(Pawn* originator)
639    {
640       if (this->state_!=MASTER) return;
641       unsigned int i=0;
642       for(std::vector<FormationController*>::reverse_iterator it = slaves_.rbegin(); it != slaves_.rend(); it++)
643       {
644           if ((*it)->state_!=FREE)
645           {
646               (*it)->state_=FREE;
647               (*it)->forceFreedom();
648               (*it)->target_=originator;
649           }
650           i++;
651           if (i>=slaves_.size()/2) break; //half the formation should attack.
652       }
653    }     
654
655
656    /**
657      @brief Sets the new mode. If master, set it for all slaves.
658    */
659    void FormationController::setFormationMode(FormationMode val)
660    {
661        this->formationMode_ = val;
662        if (this->state_ == MASTER)
663        {
664            for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
665            {
666                 (*it)->formationMode_ = val;
667                 if (val == ATTACK)
668                     (*it)->forgetTarget();
669            }
670        }
671    }
672
673    /**
674        @brief Used to continue a "specific master action" for a certain time and resuming normal behaviour after.
675    */
676    void FormationController::specificMasterActionHold()
677    {
678        if(this->state_ != MASTER) return;
679
680        if (specificMasterActionHoldCount_ == 0)
681         {
682            this->specificMasterAction_ = NONE;
683            this->searchNewTarget();
684         }
685        else specificMasterActionHoldCount_--;
686    }
687
688    /**
689        @brief Master initializes a 180 degree turn. Leads to a "specific master action".
690    */
691    void FormationController::turn180Init()
692    {
693        if(this->state_ != MASTER) return;
694
695        Quaternion orient = this->getControllableEntity()->getOrientation();
696
697        this->setTargetPosition(this->getControllableEntity()->getPosition() + 1000.0f*orient*WorldEntity::BACK);
698
699        this->specificMasterActionHoldCount_ = 4;
700
701        this->specificMasterAction_ = TURN180;
702    }
703
704    /**
705        @brief Execute the 180 degree turn. Called within tick.
706    */
707    void FormationController::turn180()
708    {
709            Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_);
710
711            this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x);
712            this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y);
713
714            this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
715    }
716
717    /**
718        @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action".
719    */
720    void FormationController::spinInit()
721    {
722        if(this->state_ != MASTER) return;
723        this->specificMasterAction_ = SPIN;
724        this->specificMasterActionHoldCount_ = 10;
725    }
726
727    /**
728        @brief Execute the spin. Called within tick.
729    */
730    void FormationController::spin()
731    {
732            this->moveToTargetPosition();
733            this->getControllableEntity()->rotateRoll(0.8f);
734    }
735
736  /**
737        @brief A human player gets followed by its nearest master. Initiated by console command, so far intended for demonstration puproses (possible future pickup).
738    */
739    void FormationController::followme()
740    {
741
742        Pawn *humanPawn = NULL;
743        NewHumanController *currentHumanController = NULL;
744        std::vector<FormationController*> allMasters;
745
746        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
747        {
748            Controller* controller = 0;
749
750            if (it->getController())
751                controller = it->getController();
752            else if (it->getXMLController())
753                controller = it->getXMLController();
754
755            if (!controller)
756                continue;
757
758            currentHumanController = orxonox_cast<NewHumanController*>(controller);
759
760            if(currentHumanController) humanPawn = *it;
761
762            FormationController *aiController = orxonox_cast<FormationController*>(controller);
763
764            if(aiController && aiController->state_ == MASTER)
765                allMasters.push_back(aiController);
766
767        }
768
769        if((humanPawn != NULL) && (allMasters.size() != 0))
770        {
771                float posHuman = humanPawn->getPosition().length();
772                float distance = 0.0f;
773                float minDistance = FLT_MAX;
774                int index = 0;
775                int i = 0;
776
777                for(std::vector<FormationController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++)
778                    {
779                        if (!FormationController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue;
780                        distance = posHuman - (*it)->getControllableEntity()->getPosition().length();
781                        if(distance < minDistance) index = i;
782                    }
783                allMasters[index]->followInit(humanPawn);
784            }
785
786    }
787
788
789
790
791
792    /**
793        @brief Master begins to follow a pawn. Is a "specific master action".
794        @param pawn pawn to follow.
795        @param always follows pawn forever if true (false if omitted).
796        @param secondsToFollow seconds to follow the pawn if always is false. Will follow pawn 100 seconds if omitted (set in header).
797    */
798    void FormationController::followInit(Pawn* pawn, const bool always, const int secondsToFollow)
799    {
800        if (pawn == NULL || this->state_ != MASTER)
801            return;
802        this->specificMasterAction_  =  FOLLOW;
803
804        this->setTarget(pawn);
805        if (!always)
806            this->specificMasterActionHoldCount_ = secondsToFollow;
807        else
808            this->specificMasterActionHoldCount_ = INT_MAX; //for now...
809
810    }
811
812   /**
813        @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action".
814    */
815    void FormationController::followRandomHumanInit()
816    {
817
818        Pawn *humanPawn = NULL;
819        NewHumanController *currentHumanController = NULL;
820
821        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
822        {
823            if (!it->getController())
824                continue;
825
826            currentHumanController = orxonox_cast<NewHumanController*>(it->getController());
827            if(currentHumanController)
828            {
829                if (!FormationController::sameTeam(this->getControllableEntity(), *it, this->getGametype())) continue;
830                humanPawn = *it;
831                break;
832            }
833        }
834
835        if((humanPawn != NULL))
836                this->followInit(humanPawn);
837    }
838
839
840  /**
841        @brief Master follows target with adjusted speed. Called within tick.
842    */
843    void FormationController::follow()
844    {
845        if (this->target_)
846            this->moveToPosition(this->target_->getPosition());
847        else
848            this->specificMasterActionHoldCount_ = 0;
849    }
850
851
852  void FormationController::setTargetPosition(const Vector3& target)
853    {
854        this->targetPosition_ = target;
855        this->bHasTargetPosition_ = true;
856    }
857
858    void FormationController::searchRandomTargetPosition()
859    {
860        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
861        this->bHasTargetPosition_ = true;
862    }
863
864    void FormationController::setTargetOrientation(const Quaternion& orient)
865    {
866        this->targetOrientation_=orient;       
867        this->bHasTargetOrientation_=true;
868    }
869
870    void FormationController::setTargetOrientation(Pawn* target)
871    {
872        if (target)
873            setTargetOrientation(target->getOrientation());
874    }
875
876    void FormationController::setTarget(Pawn* target)
877    {
878        this->target_ = target;
879
880        if (target)
881            this->targetPosition_ = target->getPosition();
882    }
883
884    void FormationController::searchNewTarget()
885    {
886        if (!this->getControllableEntity())
887            return;
888
889        this->targetPosition_ = this->getControllableEntity()->getPosition();
890        this->forgetTarget();
891
892        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
893        {
894            if (FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
895                continue;
896
897            /* So AI won't choose invisible Spaceships as target */
898            if (!it->getRadarVisibility())
899                continue;
900
901            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
902            {
903                float speed = this->getControllableEntity()->getVelocity().length();
904                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
905                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
906                if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / math::twoPi)
907                        < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / math::twoPi) + rnd(-250, 250))
908                {
909                    this->target_ = (*it);
910                    this->targetPosition_ = it->getPosition();
911                }
912            }
913        }
914    }
915
916  void FormationController::forgetTarget()
917    {
918        this->target_ = 0;
919        this->bShooting_ = false;
920    }
921
922   void FormationController::targetDied()
923    {
924        this->forgetTarget();
925        this->searchRandomTargetPosition();
926    }
927
928  bool FormationController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
929    {
930        if (entity1 == entity2)
931            return true;
932
933        int team1 = -1;
934        int team2 = -1;
935
936        Controller* controller = 0;
937        if (entity1->getController())
938            controller = entity1->getController();
939        else
940            controller = entity1->getXMLController();
941        if (controller)
942        {
943            FormationController* ac = orxonox_cast<FormationController*>(controller);
944            if (ac)
945                team1 = ac->getTeam();
946        }
947
948        if (entity2->getController())
949            controller = entity2->getController();
950        else
951            controller = entity2->getXMLController();
952        if (controller)
953        {
954            FormationController* ac = orxonox_cast<FormationController*>(controller);
955            if (ac)
956                team2 = ac->getTeam();
957        }
958
959        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
960        if (tdm)
961        {
962            if (entity1->getPlayer())
963                team1 = tdm->getTeam(entity1->getPlayer());
964
965            if (entity2->getPlayer())
966                team2 = tdm->getTeam(entity2->getPlayer());
967        }
968
969        Mission* miss = orxonox_cast<Mission*>(gametype);
970        if (miss)
971        {
972            if (entity1->getPlayer())
973                team1 = miss->getTeam(entity1->getPlayer());
974
975            if (entity2->getPlayer())
976                team2 = miss->getTeam(entity2->getPlayer());
977        }
978
979        TeamBaseMatchBase* base = 0;
980        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
981        if (base)
982        {
983            switch (base->getState())
984            {
985                case BaseState::ControlTeam1:
986                    team1 = 0;
987                    break;
988                case BaseState::ControlTeam2:
989                    team1 = 1;
990                    break;
991                case BaseState::Uncontrolled:
992                default:
993                    team1 = -1;
994            }
995        }
996        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
997        if (base)
998        {
999            switch (base->getState())
1000            {
1001                case BaseState::ControlTeam1:
1002                    team2 = 0;
1003                    break;
1004                case BaseState::ControlTeam2:
1005                    team2 = 1;
1006                    break;
1007                case BaseState::Uncontrolled:
1008                default:
1009                    team2 = -1;
1010            }
1011        }
1012
1013        DroneController* droneController = 0;
1014        droneController = orxonox_cast<DroneController*>(entity1->getController());
1015        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
1016            return true;
1017        droneController = orxonox_cast<DroneController*>(entity2->getController());
1018        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1)
1019            return true;
1020        DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController());
1021        DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController());
1022        if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner())
1023            return true;
1024
1025        Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype);
1026        if (dynamic)
1027        {
1028            if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;}
1029
1030            if (entity1->getPlayer())
1031                team1 = dynamic->getParty(entity1->getPlayer());
1032
1033            if (entity2->getPlayer())
1034                team2 = dynamic->getParty(entity2->getPlayer());
1035
1036            if (team1 ==-1 ||team2 ==-1 ) {return false;}
1037            else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;}
1038            else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;}
1039            else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;}
1040            else return true;
1041        }
1042
1043        return (team1 == team2 && team1 != -1);
1044    }
1045
1046    void FormationController::absoluteMoveToPosition(const Vector3& target)
1047    {
1048        float minDistance = 40.0f;
1049        if (!this->getControllableEntity())
1050            return;
1051
1052        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
1053        float distance = (target - this->getControllableEntity()->getPosition()).length();
1054
1055            if (this->target_ || distance > minDistance)
1056            {
1057                // Multiply with ROTATEFACTOR_FREE to make them a bit slower
1058                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x);
1059                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y);
1060                this->getControllableEntity()->moveFrontBack(SPEED_FREE);
1061            }
1062
1063
1064        if (distance < minDistance)
1065        {
1066            this->positionReached();
1067        }
1068    }
1069
1070}
Note: See TracBrowser for help on using the repository browser.