Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

deleted old behaviour of AIController (partly), forced ships to form a formation, TODO make tactics for different formation modes, choose one depending on what's better.

  • Property svn:eol-style set to native
File size: 9.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
88        if (this->state_ == SLAVE && this->formationMode_ == ATTACK) 
89        {
[7163]90            // search enemy
[10652]91            if ((!this->target_))
[7163]92                this->searchNewTarget();
[2362]93
[10652]94           
[7163]95            // shoot
[10652]96            if ((this->target_ && !this->bShooting_))
[7163]97                this->bShooting_ = true;
98
99            // stop shooting
[10652]100            if (this->bShooting_ && !this->target_)
[7163]101                this->bShooting_ = false;
102
103        }
104
105        if (this->state_ == MASTER)
106        {
[10652]107
108            this->setFormationMode(ATTACK);
109           
[7163]110            this->commandSlaves();
111
112            if  (this->specificMasterAction_ != NONE)
113                    this->specificMasterActionHold();
114
115            else {
116
117                 // make 180 degree turn - a specific Master Action
118                random = rnd(1000.0f);
119                if (random < 5)
120                   this->turn180Init();
121
122                // spin around - a specific Master Action
123                random = rnd(1000.0f);
124                if (random < 5)
125                   this->spinInit();
126
[9016]127                /*// follow a randomly chosen human - a specific Master Action
[7163]128                random = rnd(1000.0f);
129                if (random < 1)
130                   this->followRandomHumanInit();
[9016]131*/
[10652]132               /*
[7163]133                 // lose master status (only if less than 4 slaves in formation)
134                random = rnd(maxrand);
135                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
136                   this->loseMasterState();
[10652]137                */
138               
[7163]139                // look out for outher masters if formation is small
140                random = rnd(maxrand);
141                if(this->slaves_.size() < 3 && random < 20)
142                    this->searchNewMaster();
143
[9016]144                this->defaultBehaviour(maxrand);
[7163]145
146            }
147        }
148
[2362]149    }
150
151    void AIController::tick(float dt)
152    {
[10651]153
[2362]154        if (!this->isActive())
155            return;
[8891]156        float random;
157        float maxrand = 100.0f / ACTION_INTERVAL;
158        ControllableEntity* controllable = this->getControllableEntity();
[9016]159        //DOES: Either move to the waypoint or search for a Point of interest
[8891]160        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
[7163]161        {
[8891]162            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
[7163]163            {
[8891]164                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
165                if(wPoint)
[7163]166                {
[8891]167                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
168                    if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
169                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
[7168]170                }
[8891]171                else
172                    this->waypoints_.pop_back(); // remove invalid waypoints
[2362]173
[7163]174            }
[8891]175            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
176            {
177                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
178                random = rnd(maxrand);
179            }
180        }
[9016]181
182        if (this->mode_ == DEFAULT)
183        {
[8891]184            if (this->state_ == MASTER)
185            {
186                if (this->specificMasterAction_ ==  NONE)
187                {
188                    if (this->target_)
189                    {
190                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
191                            this->forgetTarget();
192                        else
193                        {
194                            this->aimAtTarget();
[10652]195                            this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
[8891]196                        }
197                    }
[2362]198
[8891]199                    if (this->bHasTargetPosition_)
200                        this->moveToTargetPosition();
201                    this->doFire();
202                }
203
204                if (this->specificMasterAction_  == TURN180)
[7163]205                    this->turn180();
206
[8891]207                if (this->specificMasterAction_ == SPIN)
[7163]208                    this->spin();
[8891]209                if (this->specificMasterAction_ == FOLLOW)
[7163]210                    this->follow();
[8891]211            }
[7163]212
[9016]213            if (this->state_ == SLAVE && this->formationMode_ != ATTACK)
[8891]214            {
215                if (this->bHasTargetPosition_)
216                    this->moveToTargetPosition();
217            }
[7163]218
[9016]219            if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) )
[8891]220            {
221                if (this->target_)
222                {
223                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
224                        this->forgetTarget();
[9016]225                    else this->aimAtTarget();
[8891]226                }
[7163]227
[8891]228                if (this->bHasTargetPosition_)
229                    this->moveToTargetPosition();
230
[9016]231                    this->doFire();
[8891]232            }
[9016]233        }
[8891]234        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
235        {   //Vector-implementation: mode_.back() == ROCKET;
236            if(controllable)
[9016]237            {//Check wether the bot is controlling the rocket and if the timeout is over.
238                if(controllable->getIdentifier() == ClassByString("Rocket"))
239
[8891]240                {
241                    this->follow();
242                    this->timeout_ -= dt;
243                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
244                    {
245                       controllable->fire(0);//kill the rocket
246                       this->setPreviousMode();//get out of rocket mode
247                    }
248                }
249                else
250                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
[7163]251            }
[8891]252            else
253                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
254        }//END_OF ROCKET MODE
[7163]255
[10652]256
[2362]257        SUPER(AIController, tick, dt);
258    }
[9016]259//**********************************************NEW
260    void AIController::defaultBehaviour(float maxrand)
[10652]261    { 
262        if (!this->target_)
263            this->searchNewTarget();
264        if (!(this->passive_) && (this->target_ && !this->bShooting_))
265            this->bShooting_ = true;
[10651]266           
[9016]267    }
268
[2362]269}
Note: See TracBrowser for help on using the repository browser.