Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10681 was 10678, checked in by gania, 9 years ago

Didn't delete old structure, but intend to replace it with new: FleetController is a Base type, it should manage divisions, each consisting of 2 sections, each section is a pair of a Leader and a Wingman. Divisions can have different formation types: Echelon, Wall, Finger Four, Diamond, Vee. Leader decides what target to shoot first, wingmen cover Leader's rear. When 2 sections merge into a division, one of the Leaders becomes a Wingman. FleetController asks Leaders to attack targets, fly to positions or protect targets.

  • Property svn:eol-style set to native
File size: 12.3 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();
[10673]207        if (this->state_ == SLAVE && controllable && this->mode_ == DEFAULT)
[10670]208        {
209           
[10673]210            if (this->myMaster_ && this->myMaster_->getControllableEntity() && desiredRelativePosition_){
211                Vector3 desiredAbsolutePosition = this->myMaster_->getControllableEntity()->getWorldPosition() + this->myMaster_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_);
212                this->moveToPosition (desiredAbsolutePosition);
213                //WorldEntity* waypoint = new WorldEntity(this->getContext());
214                //waypoint->setPosition(desiredAbsolutePosition);
[10670]215           
[10673]216                //this->addWaypoint(waypoint);
217           
218            }
219         
[10670]220        }
[9016]221        //DOES: Either move to the waypoint or search for a Point of interest
[8891]222        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
[7163]223        {
[8891]224            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
[7163]225            {
[8891]226                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
227                if(wPoint)
[7163]228                {
[8891]229                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
230                    if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
231                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
[7168]232                }
[8891]233                else
234                    this->waypoints_.pop_back(); // remove invalid waypoints
[2362]235
[7163]236            }
[8891]237            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
238            {
239                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
240                random = rnd(maxrand);
241            }
242        }
[9016]243
244        if (this->mode_ == DEFAULT)
245        {
[8891]246            if (this->state_ == MASTER)
247            {
248                if (this->specificMasterAction_ ==  NONE)
249                {
250                    if (this->target_)
251                    {
252                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
253                            this->forgetTarget();
254                        else
255                        {
256                            this->aimAtTarget();
[10652]257                            this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
[8891]258                        }
259                    }
[2362]260
[8891]261                    if (this->bHasTargetPosition_)
262                        this->moveToTargetPosition();
263                    this->doFire();
264                }
265
266                if (this->specificMasterAction_  == TURN180)
[7163]267                    this->turn180();
268
[8891]269                if (this->specificMasterAction_ == SPIN)
[7163]270                    this->spin();
[8891]271                if (this->specificMasterAction_ == FOLLOW)
[7163]272                    this->follow();
[8891]273            }
[7163]274
[9016]275            if (this->state_ == SLAVE && this->formationMode_ != ATTACK)
[8891]276            {
277                if (this->bHasTargetPosition_)
278                    this->moveToTargetPosition();
279            }
[7163]280
[9016]281            if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) )
[8891]282            {
283                if (this->target_)
284                {
285                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
286                        this->forgetTarget();
[9016]287                    else this->aimAtTarget();
[8891]288                }
[7163]289
[8891]290                if (this->bHasTargetPosition_)
291                    this->moveToTargetPosition();
292
[9016]293                    this->doFire();
[8891]294            }
[9016]295        }
[8891]296        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
297        {   //Vector-implementation: mode_.back() == ROCKET;
298            if(controllable)
[9016]299            {//Check wether the bot is controlling the rocket and if the timeout is over.
300                if(controllable->getIdentifier() == ClassByString("Rocket"))
301
[8891]302                {
303                    this->follow();
304                    this->timeout_ -= dt;
305                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
306                    {
307                       controllable->fire(0);//kill the rocket
308                       this->setPreviousMode();//get out of rocket mode
309                    }
310                }
311                else
312                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
[7163]313            }
[8891]314            else
315                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
316        }//END_OF ROCKET MODE
[7163]317
[10652]318
[2362]319        SUPER(AIController, tick, dt);
320    }
[9016]321//**********************************************NEW
322    void AIController::defaultBehaviour(float maxrand)
[10652]323    { 
324        if (!this->target_)
325            this->searchNewTarget();
326        if (!(this->passive_) && (this->target_ && !this->bShooting_))
327            this->bShooting_ = true;
[10651]328           
[9016]329    }
330
[10678]331}
Note: See TracBrowser for help on using the repository browser.