Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/controllers/ArtificialController.cc @ 7097

Last change on this file since 7097 was 7097, checked in by landauf, 14 years ago

trying to activate formationflight also for xml enemies

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