Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/orxonox/controllers/AIController.cc @ 8791

Last change on this file since 8791 was 8791, checked in by jo, 13 years ago

Bug is triggered in AIController.cc @ line 243. Can't find the reason at the moment.

  • Property svn:eol-style set to native
File size: 11.7 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{
39    static const float ACTION_INTERVAL = 1.0f;
40
41    CreateFactory(AIController);
42
43    AIController::AIController(BaseObject* creator) : ArtificialController(creator)
44    {
45        RegisterObject(AIController);
46
[5929]47        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this)));
[2362]48    }
49
50    AIController::~AIController()
51    {
52    }
53
54    void AIController::action()
55    {
56        float random;
57        float maxrand = 100.0f / ACTION_INTERVAL;
[2493]58
[7163]59        if (this->state_ == FREE)
60        {
[2362]61
[7163]62            if (this->formationFlight_)
63            {
64                // return to Master after being forced free
65                if (this->freedomCount_ == 1)
66                {
[8732]67                    this->state_ = SLAVE;
68                    this->freedomCount_ = 0;
[7163]69                }
[2362]70
[7163]71                random = rnd(maxrand);
72                if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree()))
73                    this->searchNewMaster();
74            }
[2493]75
[7163]76            // search enemy
77            random = rnd(maxrand);
[8724]78            if (random < (15 + botlevel_* 20) && (!this->target_))
[7163]79                this->searchNewTarget();
[2362]80
[7163]81            // forget enemy
82            random = rnd(maxrand);
[8724]83            if (random < ((1-botlevel_)*6) && (this->target_))
[7163]84                this->forgetTarget();
[2362]85
[7163]86            // next enemy
87            random = rnd(maxrand);
[8724]88            if (random < (botlevel_*20) && (this->target_))
[7163]89                this->searchNewTarget();
[2493]90
[7163]91            // fly somewhere
92            random = rnd(maxrand);
93            if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
94                this->searchRandomTargetPosition();
[2362]95
[7163]96            // stop flying
97            random = rnd(maxrand);
98            if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
99                this->bHasTargetPosition_ = false;
100
101            // fly somewhere else
102            random = rnd(maxrand);
103            if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
104                this->searchRandomTargetPosition();
105
106            // shoot
107            random = rnd(maxrand);
[8724]108            if (!(this->passive_) && random < (75 + botlevel_*25) && (this->target_ && !this->bShooting_))
[7163]109                this->bShooting_ = true;
110
111            // stop shooting
112            random = rnd(maxrand);
[8724]113            if (random < ((1 - botlevel_)*25) && (this->bShooting_))
[7163]114                this->bShooting_ = false;
115
[8786]116            // boost
117            random = rnd(maxrand);
118            if (random < botlevel_*100 )
119                this->boostControl();
120
121            // update Checkpoints
[8791]122            /*random = rnd(maxrand);
[8786]123            if (this->defaultWaypoint_ && random > (maxrand-10))
124                this->manageWaypoints();
[8791]125            else //if(random > maxrand-10) //CHECK USABILITY!!*/
126            if (this->waypoints_.size() == 0 )
[8786]127                this->manageWaypoints();
128
[7163]129        }
130
131        if (this->state_ == SLAVE)
132        {
133
134        }
135
136        if (this->state_ == MASTER)
137        {
138
139
140            this->commandSlaves();
141
142            if  (this->specificMasterAction_ != NONE)
143                    this->specificMasterActionHold();
144
145            else {
146
147                 // make 180 degree turn - a specific Master Action
148                random = rnd(1000.0f);
149                if (random < 5)
150                   this->turn180Init();
151
152                // spin around - a specific Master Action
153                random = rnd(1000.0f);
154                if (random < 5)
155                   this->spinInit();
156
157                // follow a randomly chosen human - a specific Master Action
158                random = rnd(1000.0f);
159                if (random < 1)
160                   this->followRandomHumanInit();
161
162                 // lose master status (only if less than 4 slaves in formation)
163                random = rnd(maxrand);
164                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
165                   this->loseMasterState();
166
167                // look out for outher masters if formation is small
168                random = rnd(maxrand);
169                if(this->slaves_.size() < 3 && random < 20)
170                    this->searchNewMaster();
171
172                // search enemy
173                random = rnd(maxrand);
[8732]174                if (random < (botlevel_)*25 && (!this->target_))
[7163]175                    this->searchNewTarget();
176
177                // forget enemy
178                random = rnd(maxrand);
[8732]179                if (random < (1-botlevel_)*6 && (this->target_))
[7163]180                    this->forgetTarget();
181
182                // next enemy
183                random = rnd(maxrand);
184                if (random < 10 && (this->target_))
185                    this->searchNewTarget();
186
187                // fly somewhere
188                random = rnd(maxrand);
189                if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
190                    this->searchRandomTargetPosition();
191
192
193                // fly somewhere else
194                random = rnd(maxrand);
195                if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
196                    this->searchRandomTargetPosition();
197
198                // shoot
199                random = rnd(maxrand);
[8732]200                if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_))
[7163]201                {
[8732]202                    this->bShooting_ = true;
203                    this->forceFreeSlaves();
[7163]204                }
205
206                // stop shooting
207                random = rnd(maxrand);
[8732]208                if (random < ( (1- botlevel_)*25 ) && (this->bShooting_))
[7163]209                    this->bShooting_ = false;
210
[8786]211                // boost
[8758]212                random = rnd(maxrand);
213                if (random < botlevel_*100 )
[8786]214                    this->boostControl();
215
216                // update Checkpoints
[8791]217                /*random = rnd(maxrand);
[8786]218                if (this->defaultWaypoint_ && random > (maxrand-10))
219                    this->manageWaypoints();
[8791]220                else //if(random > maxrand-10) //CHECK USABILITY!!*/
221                if (this->waypoints_.size() == 0 )
[8786]222                    this->manageWaypoints();
[7163]223            }
224        }
225
[2362]226    }
227
228    void AIController::tick(float dt)
229    {
[8730]230        if (!this->isActive())
231            return;
[8758]232
[8724]233        float random;
234        float maxrand = 100.0f / ACTION_INTERVAL;
[8786]235        ControllableEntity* controllable = this->getControllableEntity();
236
237        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
[8769]238        {
[8786]239            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
240            {
[8791]241                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
242                if(wPoint)
243                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
244                if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
[8786]245                    this->waypoints_.pop_back(); // if goal is reached, remove it from the list
[8791]246
[8786]247            }
248            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
249            {
250                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
251                random = rnd(maxrand);
252            }
[8769]253        }
[8731]254        if(this->mode_ == DEFAULT)
255            {
256            if (this->state_ == MASTER)
257            {
258                if (this->specificMasterAction_ ==  NONE)
259                {
260                    if (this->target_)
261                    {
262                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
263                            this->forgetTarget();
[8732]264                        else
265                        {
266                            this->aimAtTarget();
267                            random = rnd(maxrand);
[8766]268                            if(this->botlevel_*100 > random && !this->isCloseAtTarget(20))
[8758]269                                this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
[8732]270                        }
[8731]271                    }
[8724]272
[8731]273                    if (this->bHasTargetPosition_)
274                        this->moveToTargetPosition();
275
[8733]276                    this->doFire();
[8731]277                }
278
279                if (this->specificMasterAction_  == TURN180)
280                    this->turn180();
281
282                if (this->specificMasterAction_ == SPIN)
283                    this->spin();
284                if (this->specificMasterAction_ == FOLLOW)
285                    this->follow();
286            }
287
288            if (this->state_ == SLAVE)
[7163]289            {
[8731]290                if (this->bHasTargetPosition_)
291                    this->moveToTargetPosition();
292            }
293
294            if (this->state_ == FREE)
295            {
[7163]296                if (this->target_)
297                {
[7168]298                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
299                        this->forgetTarget();
[8732]300                    else
301                    {
302                        this->aimAtTarget();
303                        random = rnd(maxrand);
[8766]304
305                        if(this->botlevel_*100 > random && !this->isCloseAtTarget(20))
[8732]306                            this->follow();//If a bot is shooting a player, it shouldn't let him go away easily.
307                     }
[7168]308                }
[2362]309
[7163]310                if (this->bHasTargetPosition_)
311                    this->moveToTargetPosition();
[2362]312
[8733]313                this->doFire();
[7163]314            }
[8731]315        }//END_OF DEFAULT MODE
316        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
317        {   //Vector-implementation: mode_.back() == ROCKET;
318            if(controllable)
[7163]319            {
[8731]320                if(controllable->getRocket())//Check wether the bot is controlling the rocket and if the timeout is over.
321                {
[8736]322                    this->follow();
[8731]323                    this->timeout_ -= dt;
324                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
325                    {
326                       controllable->fire(0);//kill the rocket
327                       this->setPreviousMode();//get out of rocket mode
328                    }
329                }
330                else
331                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
[7163]332            }
[8731]333            else
334                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
335        }//END_OF ROCKET MODE
[7163]336
[2362]337        SUPER(AIController, tick, dt);
338    }
339
340}
Note: See TracBrowser for help on using the repository browser.