Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/orxonox/controllers/FormationController.cc @ 9271

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

svn:eol-style native (no changes in code)

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