Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/controllers/AIController.cc @ 8891

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

Ai and tutorial improvements merged back to the trunk. AI features: all weapons are used, the ai-firestrength is configurable, bots are able to collect pickups . I've set the tutorial level as default level.

  • Property svn:eol-style set to native
File size: 11.9 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    CreateFactory(AIController);
42
43    AIController::AIController(BaseObject* creator) : ArtificialController(creator)
44    {
45        RegisterObject(AIController);
46
47        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this)));
48    }
49
50    AIController::~AIController()
51    {
52    }
53
54    void AIController::action()
55    {
56        float random;
57        float maxrand = 100.0f / ACTION_INTERVAL;
58
59        if (this->state_ == FREE)
60        {
61
62            if (this->formationFlight_)
63            {
64                // return to Master after being forced free
65                if (this->freedomCount_ == 1)
66                {
67                    this->state_ = SLAVE;
68                    this->freedomCount_ = 0;
69                }
70
71                random = rnd(maxrand);
72                if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree()))
73                    this->searchNewMaster();
74            }
75
76            // search enemy
77            random = rnd(maxrand);
78            if (random < (15 + botlevel_* 20) && (!this->target_))
79                this->searchNewTarget();
80
81            // forget enemy
82            random = rnd(maxrand);
83            if (random < ((1-botlevel_)*6) && (this->target_))
84                this->forgetTarget();
85
86            // next enemy
87            random = rnd(maxrand);
88            if (random < (botlevel_*20) && (this->target_))
89                this->searchNewTarget();
90
91            // fly somewhere
92            random = rnd(maxrand);
93            if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
94                this->searchRandomTargetPosition();
95
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);
108            if (!(this->passive_) && random < (75 + botlevel_*25) && (this->target_ && !this->bShooting_))
109                this->bShooting_ = true;
110
111            // stop shooting
112            random = rnd(maxrand);
113            if (random < ((1 - botlevel_)*25) && (this->bShooting_))
114                this->bShooting_ = false;
115
116            // boost
117            random = rnd(maxrand);
118            if (random < botlevel_*100 )
119                this->boostControl();
120
121            // update Checkpoints
122            /*random = rnd(maxrand);
123            if (this->defaultWaypoint_ && random > (maxrand-10))
124                this->manageWaypoints();
125            else //if(random > maxrand-10) //CHECK USABILITY!!*/
126            if (this->waypoints_.size() == 0 )
127                this->manageWaypoints();
128
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);
174                if (random < (botlevel_)*25 && (!this->target_))
175                    this->searchNewTarget();
176
177                // forget enemy
178                random = rnd(maxrand);
179                if (random < (1-botlevel_)*6 && (this->target_))
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);
200                if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_))
201                {
202                    this->bShooting_ = true;
203                    this->forceFreeSlaves();
204                }
205
206                // stop shooting
207                random = rnd(maxrand);
208                if (random < ( (1- botlevel_)*25 ) && (this->bShooting_))
209                    this->bShooting_ = false;
210
211                // boost
212                random = rnd(maxrand);
213                if (random < botlevel_*100 )
214                    this->boostControl();
215
216                // update Checkpoints
217                /*random = rnd(maxrand);
218                if (this->defaultWaypoint_ && random > (maxrand-10))
219                    this->manageWaypoints();
220                else //if(random > maxrand-10) //CHECK USABILITY!!*/
221                if (this->waypoints_.size() == 0 )
222                    this->manageWaypoints();
223            }
224        }
225
226    }
227
228    void AIController::tick(float dt)
229    {
230        if (!this->isActive())
231            return;
232
233        float random;
234        float maxrand = 100.0f / ACTION_INTERVAL;
235        ControllableEntity* controllable = this->getControllableEntity();
236
237        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
238        {
239            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
240            {
241                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
242                if(wPoint)
243                {
244                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
245                    if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
246                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
247                }
248                else
249                    this->waypoints_.pop_back(); // remove invalid waypoints
250
251            }
252            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
253            {
254                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
255                random = rnd(maxrand);
256            }
257        }
258        if(this->mode_ == DEFAULT)
259            {
260            if (this->state_ == MASTER)
261            {
262                if (this->specificMasterAction_ ==  NONE)
263                {
264                    if (this->target_)
265                    {
266                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
267                            this->forgetTarget();
268                        else
269                        {
270                            this->aimAtTarget();
271                            random = rnd(maxrand);
272                            if(this->botlevel_*100 > random && !this->isCloseAtTarget(20))
273                                this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
274                        }
275                    }
276
277                    if (this->bHasTargetPosition_)
278                        this->moveToTargetPosition();
279
280                    this->doFire();
281                }
282
283                if (this->specificMasterAction_  == TURN180)
284                    this->turn180();
285
286                if (this->specificMasterAction_ == SPIN)
287                    this->spin();
288                if (this->specificMasterAction_ == FOLLOW)
289                    this->follow();
290            }
291
292            if (this->state_ == SLAVE)
293            {
294                if (this->bHasTargetPosition_)
295                    this->moveToTargetPosition();
296            }
297
298            if (this->state_ == FREE)
299            {
300                if (this->target_)
301                {
302                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
303                        this->forgetTarget();
304                    else
305                    {
306                        this->aimAtTarget();
307                        random = rnd(maxrand);
308
309                        if(this->botlevel_*100 > random && !this->isCloseAtTarget(20))
310                            this->follow();//If a bot is shooting a player, it shouldn't let him go away easily.
311                     }
312                }
313
314                if (this->bHasTargetPosition_)
315                    this->moveToTargetPosition();
316
317                this->doFire();
318            }
319        }//END_OF DEFAULT MODE
320        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
321        {   //Vector-implementation: mode_.back() == ROCKET;
322            if(controllable)
323            {
324                if(controllable->getRocket())//Check wether the bot is controlling the rocket and if the timeout is over.
325                {
326                    this->follow();
327                    this->timeout_ -= dt;
328                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
329                    {
330                       controllable->fire(0);//kill the rocket
331                       this->setPreviousMode();//get out of rocket mode
332                    }
333                }
334                else
335                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
336            }
337            else
338                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
339        }//END_OF ROCKET MODE
340
341        SUPER(AIController, tick, dt);
342    }
343
344}
Note: See TracBrowser for help on using the repository browser.