Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012/src/orxonox/controllers/FormationController.cc @ 9229

Last change on this file since 9229 was 9229, checked in by decapitb, 12 years ago

merged towerdefense

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