Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/formation/src/orxonox/controllers/FormationController.cc @ 8985

Last change on this file since 8985 was 8978, checked in by willis, 13 years ago

renamed Masterable to FormationController, some minor changes

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