Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7094 was 7090, checked in by dafrick, 14 years ago

Merged ppspickups4 branch.

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