Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

doFire() is definitely the error's source.

  • Property svn:eol-style set to native
File size: 10.1 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    static const float 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        }
117
118        if (this->state_ == SLAVE)
119        {
120
121        }
122
123        if (this->state_ == MASTER)
124        {
125
126
127            this->commandSlaves();
128
129            if  (this->specificMasterAction_ != NONE)
130                    this->specificMasterActionHold();
131
132            else {
133
134                 // make 180 degree turn - a specific Master Action
135                random = rnd(1000.0f);
136                if (random < 5)
137                   this->turn180Init();
138
139                // spin around - a specific Master Action
140                random = rnd(1000.0f);
141                if (random < 5)
142                   this->spinInit();
143
144                // follow a randomly chosen human - a specific Master Action
145                random = rnd(1000.0f);
146                if (random < 1)
147                   this->followRandomHumanInit();
148
149                 // lose master status (only if less than 4 slaves in formation)
150                random = rnd(maxrand);
151                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
152                   this->loseMasterState();
153
154                // look out for outher masters if formation is small
155                random = rnd(maxrand);
156                if(this->slaves_.size() < 3 && random < 20)
157                    this->searchNewMaster();
158
159                // search enemy
160                random = rnd(maxrand);
161                if (random < (botlevel_)*25 && (!this->target_))
162                    this->searchNewTarget();
163
164                // forget enemy
165                random = rnd(maxrand);
166                if (random < (1-botlevel_)*6 && (this->target_))
167                    this->forgetTarget();
168
169                // next enemy
170                random = rnd(maxrand);
171                if (random < 10 && (this->target_))
172                    this->searchNewTarget();
173
174                // fly somewhere
175                random = rnd(maxrand);
176                if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
177                    this->searchRandomTargetPosition();
178
179
180                // fly somewhere else
181                random = rnd(maxrand);
182                if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
183                    this->searchRandomTargetPosition();
184
185                // shoot
186                random = rnd(maxrand);
187                if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_))
188                {
189                    this->bShooting_ = true;
190                    this->forceFreeSlaves();
191                }
192
193                // stop shooting
194                random = rnd(maxrand);
195                if (random < ( (1- botlevel_)*25 ) && (this->bShooting_))
196                    this->bShooting_ = false;
197
198            }
199        }
200
201    }
202
203    void AIController::tick(float dt)
204    {
205        if (!this->isActive())
206            return;
207
208        float random;
209        float maxrand = 100.0f / ACTION_INTERVAL;
210        if(this->mode_ == DEFAULT)
211            {
212            if (this->state_ == MASTER)
213            {
214                if (this->specificMasterAction_ ==  NONE)
215                {
216                    if (this->target_)
217                    {
218                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
219                            this->forgetTarget();
220                        else
221                        {
222                            this->aimAtTarget();
223                            random = rnd(maxrand);
224                            if(this->botlevel_*100 > random)
225                                this->follow();//If a bot is shooting a player, it shouldn't let him go away easily.
226                        }
227                    }
228
229                    if (this->bHasTargetPosition_)
230                        this->moveToTargetPosition();
231
232                    if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(math::pi / 20.0f))
233                        this->getControllableEntity()->fire(0);
234                }
235
236                if (this->specificMasterAction_  == TURN180)
237                    this->turn180();
238
239                if (this->specificMasterAction_ == SPIN)
240                    this->spin();
241                if (this->specificMasterAction_ == FOLLOW)
242                    this->follow();
243            }
244
245            if (this->state_ == SLAVE)
246            {
247                if (this->bHasTargetPosition_)
248                    this->moveToTargetPosition();
249            }
250
251            if (this->state_ == FREE)
252            {
253                if (this->target_)
254                {
255                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
256                        this->forgetTarget();
257                    else
258                    {
259                        this->aimAtTarget();
260                        random = rnd(maxrand);
261                        if(this->botlevel_*100 > random)
262                            this->follow();//If a bot is shooting a player, it shouldn't let him go away easily.
263                     }
264                }
265
266                if (this->bHasTargetPosition_)
267                    this->moveToTargetPosition();
268
269                if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(math::pi / 20.0f))
270                    this->getControllableEntity()->fire(0);
271            }
272        }//END_OF DEFAULT MODE
273        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
274        {   //Vector-implementation: mode_.back() == ROCKET;
275            ControllableEntity *controllable = this->getControllableEntity(); 
276            if(controllable)
277            {
278                if(controllable->getRocket())//Check wether the bot is controlling the rocket and if the timeout is over.
279                {
280                    this->follow(); //TODO: CHECK: does follow make the bot crash into the target_ ?
281                    this->timeout_ -= dt;
282                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
283                    {
284                       controllable->fire(0);//kill the rocket
285                       this->setPreviousMode();//get out of rocket mode
286                    }
287                }
288                else
289                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
290            }
291            else
292                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
293        }//END_OF ROCKET MODE
294
295        SUPER(AIController, tick, dt);
296    }
297
298}
Note: See TracBrowser for help on using the repository browser.