Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Old AIController default behaviour is commented out

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