Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/AIController.cc @ 10671

Last change on this file since 10671 was 10670, checked in by gania, 9 years ago

hold position in formation with waypoints

  • Property svn:eol-style set to native
File size: 12.0 KB
RevLine 
[2362]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:
[7163]25 *      Dominik Solenicki
[2362]26 *
27 */
28
29#include "AIController.h"
30
[3196]31#include "util/Math.h"
[2362]32#include "core/CoreIncludes.h"
[7284]33#include "core/command/Executor.h"
[5735]34#include "worldentities/ControllableEntity.h"
[7163]35#include "worldentities/pawns/Pawn.h"
[2362]36
37namespace orxonox
38{
[8729]39    const float AIController::ACTION_INTERVAL = 1.0f;
[2362]40
[9667]41    RegisterClass(AIController);
[2362]42
[9667]43    AIController::AIController(Context* context) : ArtificialController(context)
[2362]44    {
45        RegisterObject(AIController);
[5929]46        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this)));
[2362]47    }
48
49    AIController::~AIController()
50    {
51    }
52
53    void AIController::action()
54    {
55        float random;
56        float maxrand = 100.0f / ACTION_INTERVAL;
[2493]57
[7163]58        if (this->state_ == FREE)
59        {
[9016]60           
[7163]61            if (this->formationFlight_)
62            {
[10652]63                //When this is a master and was destroyed, destructor might complain that there are slaves of this, although this was removed from formation
64                //race conditions?
65                //destructor takes care of slaves anyway, so no need to worry about internal_error
[9016]66
[10652]67
[9016]68                //changed order -> searchNewMaster MUSTN'T be called in SLAVE-state (bugfix for internal-error messages at quit)
69                random = rnd(maxrand);
70                if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree()))
71                       this->searchNewMaster();
72
[7163]73                // return to Master after being forced free
[10652]74                if (this->freedomCount_ == ACTION_INTERVAL)
[7163]75                {
[8891]76                    this->state_ = SLAVE;
[10652]77                    this->freedomCount_ = 0; // ACTION_INTERVAL is 1 sec, freedomCount is a remaining time of temp. freedom
[7163]78                }
[10652]79            } else{
80                //form a formation
81                if (!this->forcedFree())
82                    this->searchNewMaster();
[7163]83            }
[9016]84            this->defaultBehaviour(maxrand);
85
86        }
87
[10670]88
[9016]89        if (this->state_ == SLAVE && this->formationMode_ == ATTACK) 
90        {
[7163]91            // search enemy
[10652]92            if ((!this->target_))
[7163]93                this->searchNewTarget();
[2362]94
[10652]95           
[7163]96            // shoot
[10652]97            if ((this->target_ && !this->bShooting_))
[7163]98                this->bShooting_ = true;
99
100            // stop shooting
[10652]101            if (this->bShooting_ && !this->target_)
[7163]102                this->bShooting_ = false;
103
104        }
105
106        if (this->state_ == MASTER)
107        {
[10654]108           
109            //-------------------------------------------------------
110            //collect data for AI behaviour
[10670]111            Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
112            Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
113            Vector3 meanOfAllies = *meanOfAlliesPtr;
114            Vector3 meanOfEnemies = *meanOfEnemiesPtr;
[10652]115
[10670]116
[10654]117            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
118            {
119
120                Gametype* gt=this->getGametype();
121                if (!gt)
122                {
123                    gt=it->getGametype();
124                }
125                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
126                {
[10670]127                    enemies_.push_back(*it);
[10654]128                } 
129                else {
[10670]130                    allies_.push_back(*it);
[10654]131                } 
132            }
[10670]133            if (enemies_.size() != 0 && allies_.size() != 0){
134                for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
135                    meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
136
137                meanOfEnemies /= enemies_.size();
138
139                for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
140                    meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
141
142                meanOfAllies /= allies_.size();
143
144                //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
145                orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
146                orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
147                orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
[10654]148            }
149            //-------------------------------------------------------
150           
[10670]151            //Decide which formationMode to choose
[10652]152            this->setFormationMode(ATTACK);
[10670]153            this->setDesiredPositionOfSlaves();
[7163]154
[10670]155            //this->commandSlaves();
156
[7163]157            if  (this->specificMasterAction_ != NONE)
158                    this->specificMasterActionHold();
159
160            else {
161
162                 // make 180 degree turn - a specific Master Action
[10654]163                /*
[7163]164                random = rnd(1000.0f);
165                if (random < 5)
166                   this->turn180Init();
167
168                // spin around - a specific Master Action
169                random = rnd(1000.0f);
170                if (random < 5)
171                   this->spinInit();
172
[10654]173                */
[9016]174                /*// follow a randomly chosen human - a specific Master Action
[7163]175                random = rnd(1000.0f);
176                if (random < 1)
177                   this->followRandomHumanInit();
[9016]178*/
[10652]179               /*
[7163]180                 // lose master status (only if less than 4 slaves in formation)
181                random = rnd(maxrand);
182                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
183                   this->loseMasterState();
[10652]184                */
185               
[7163]186                // look out for outher masters if formation is small
187                random = rnd(maxrand);
188                if(this->slaves_.size() < 3 && random < 20)
189                    this->searchNewMaster();
190
[9016]191                this->defaultBehaviour(maxrand);
[7163]192
193            }
194        }
[10670]195        allies_.clear();
196        enemies_.clear();
[2362]197    }
198
199    void AIController::tick(float dt)
200    {
[10651]201
[2362]202        if (!this->isActive())
203            return;
[8891]204        float random;
205        float maxrand = 100.0f / ACTION_INTERVAL;
206        ControllableEntity* controllable = this->getControllableEntity();
[10670]207        if (this->state_ == SLAVE)
208        {
209            Vector3 desiredAbsolutePosition = this->myMaster_->getControllableEntity()->getWorldPosition() + this->myMaster_->getControllableEntity()->getWorldOrientation()*desiredRelativePosition_;
210           
211            orxonox::WeakPtr<MovableEntity> waypoint = new MovableEntity(this->center_->getContext());
212            waypoint->setPosition(desiredAbsolutePosition);
213           
214            this->addWaypoint(waypoint);
215        }
[9016]216        //DOES: Either move to the waypoint or search for a Point of interest
[8891]217        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
[7163]218        {
[8891]219            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
[7163]220            {
[8891]221                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
222                if(wPoint)
[7163]223                {
[8891]224                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
225                    if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
226                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
[7168]227                }
[8891]228                else
229                    this->waypoints_.pop_back(); // remove invalid waypoints
[2362]230
[7163]231            }
[8891]232            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
233            {
234                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
235                random = rnd(maxrand);
236            }
237        }
[9016]238
239        if (this->mode_ == DEFAULT)
240        {
[8891]241            if (this->state_ == MASTER)
242            {
243                if (this->specificMasterAction_ ==  NONE)
244                {
245                    if (this->target_)
246                    {
247                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
248                            this->forgetTarget();
249                        else
250                        {
251                            this->aimAtTarget();
[10652]252                            this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
[8891]253                        }
254                    }
[2362]255
[8891]256                    if (this->bHasTargetPosition_)
257                        this->moveToTargetPosition();
258                    this->doFire();
259                }
260
261                if (this->specificMasterAction_  == TURN180)
[7163]262                    this->turn180();
263
[8891]264                if (this->specificMasterAction_ == SPIN)
[7163]265                    this->spin();
[8891]266                if (this->specificMasterAction_ == FOLLOW)
[7163]267                    this->follow();
[8891]268            }
[7163]269
[9016]270            if (this->state_ == SLAVE && this->formationMode_ != ATTACK)
[8891]271            {
272                if (this->bHasTargetPosition_)
273                    this->moveToTargetPosition();
274            }
[7163]275
[9016]276            if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) )
[8891]277            {
278                if (this->target_)
279                {
280                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
281                        this->forgetTarget();
[9016]282                    else this->aimAtTarget();
[8891]283                }
[7163]284
[8891]285                if (this->bHasTargetPosition_)
286                    this->moveToTargetPosition();
287
[9016]288                    this->doFire();
[8891]289            }
[9016]290        }
[8891]291        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
292        {   //Vector-implementation: mode_.back() == ROCKET;
293            if(controllable)
[9016]294            {//Check wether the bot is controlling the rocket and if the timeout is over.
295                if(controllable->getIdentifier() == ClassByString("Rocket"))
296
[8891]297                {
298                    this->follow();
299                    this->timeout_ -= dt;
300                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
301                    {
302                       controllable->fire(0);//kill the rocket
303                       this->setPreviousMode();//get out of rocket mode
304                    }
305                }
306                else
307                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
[7163]308            }
[8891]309            else
310                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
311        }//END_OF ROCKET MODE
[7163]312
[10652]313
[2362]314        SUPER(AIController, tick, dt);
315    }
[9016]316//**********************************************NEW
317    void AIController::defaultBehaviour(float maxrand)
[10652]318    { 
319        if (!this->target_)
320            this->searchNewTarget();
321        if (!(this->passive_) && (this->target_ && !this->bShooting_))
322            this->bShooting_ = true;
[10651]323           
[9016]324    }
325
[2362]326}
Note: See TracBrowser for help on using the repository browser.