Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/formation/src/orxonox/controllers/Masterable.cc @ 8948

Last change on this file since 8948 was 8948, checked in by willis, 12 years ago

debugging

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