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
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                //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
66
67
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
73                // return to Master after being forced free
74                if (this->freedomCount_ == ACTION_INTERVAL)
75                {
76                    this->state_ = SLAVE;
77                    this->freedomCount_ = 0; // ACTION_INTERVAL is 1 sec, freedomCount is a remaining time of temp. freedom
78                }
79            } else{
80                //form a formation
81                if (!this->forcedFree())
82                    this->searchNewMaster();
83            }
84            this->defaultBehaviour(maxrand);
85
86        }
87
88        if (this->state_ == SLAVE && this->formationMode_ == ATTACK) 
89        {
90            // search enemy
91            if ((!this->target_))
92                this->searchNewTarget();
93
94           
95            // shoot
96            if ((this->target_ && !this->bShooting_))
97                this->bShooting_ = true;
98
99            // stop shooting
100            if (this->bShooting_ && !this->target_)
101                this->bShooting_ = false;
102
103        }
104
105        if (this->state_ == MASTER)
106        {
107
108            this->setFormationMode(ATTACK);
109           
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
127                /*// follow a randomly chosen human - a specific Master Action
128                random = rnd(1000.0f);
129                if (random < 1)
130                   this->followRandomHumanInit();
131*/
132               /*
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();
137                */
138               
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
144                this->defaultBehaviour(maxrand);
145
146            }
147        }
148
149    }
150
151    void AIController::tick(float dt)
152    {
153
154        if (!this->isActive())
155            return;
156        float random;
157        float maxrand = 100.0f / ACTION_INTERVAL;
158        ControllableEntity* controllable = this->getControllableEntity();
159        //DOES: Either move to the waypoint or search for a Point of interest
160        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
161        {
162            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
163            {
164                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
165                if(wPoint)
166                {
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
170                }
171                else
172                    this->waypoints_.pop_back(); // remove invalid waypoints
173
174            }
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        }
181
182        if (this->mode_ == DEFAULT)
183        {
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();
195                            this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
196                        }
197                    }
198
199                    if (this->bHasTargetPosition_)
200                        this->moveToTargetPosition();
201                    this->doFire();
202                }
203
204                if (this->specificMasterAction_  == TURN180)
205                    this->turn180();
206
207                if (this->specificMasterAction_ == SPIN)
208                    this->spin();
209                if (this->specificMasterAction_ == FOLLOW)
210                    this->follow();
211            }
212
213            if (this->state_ == SLAVE && this->formationMode_ != ATTACK)
214            {
215                if (this->bHasTargetPosition_)
216                    this->moveToTargetPosition();
217            }
218
219            if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) )
220            {
221                if (this->target_)
222                {
223                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
224                        this->forgetTarget();
225                    else this->aimAtTarget();
226                }
227
228                if (this->bHasTargetPosition_)
229                    this->moveToTargetPosition();
230
231                    this->doFire();
232            }
233        }
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)
237            {//Check wether the bot is controlling the rocket and if the timeout is over.
238                if(controllable->getIdentifier() == ClassByString("Rocket"))
239
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
251            }
252            else
253                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
254        }//END_OF ROCKET MODE
255
256
257        SUPER(AIController, tick, dt);
258    }
259//**********************************************NEW
260    void AIController::defaultBehaviour(float maxrand)
261    { 
262        if (!this->target_)
263            this->searchNewTarget();
264        if (!(this->passive_) && (this->target_ && !this->bShooting_))
265            this->bShooting_ = true;
266           
267    }
268
269}
Note: See TracBrowser for help on using the repository browser.