Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed AIController → collecting data about spaceships with each action()

  • Property svn:eol-style set to native
File size: 11.2 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            //-------------------------------------------------------
109            //collect data for AI behaviour
110            Vector3 meanOfEnemies;
111            Vector3 meanOfAllies;
112
113            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
114            {
115
116                Gametype* gt=this->getGametype();
117                if (!gt)
118                {
119                    gt=it->getGametype();
120                }
121                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
122                {
123                    enemies.push_back(*it);
124                } 
125                else {
126                    allies.push_back(*it);
127                } 
128            }
129            if (enemies.size() != 0 && allies.size() != 0){
130            for (std::vector<AIController*>::iterator it = enemies.begin() ; it != enemies.end(); ++it)
131                meanOfEnemies += (*it)->getControllableEntity()->getPosition();
132            meanOfEnemies /= enemies.size();
133            for (std::vector<AIController*>::iterator it = allies.begin() ; it != allies.end(); ++it)
134                meanOfAllies += (*it)->getControllableEntity()->getPosition();
135            meanOfAllies /= allies.size();
136            //orxout(internal_error) << "There are " << enemiesCounter << " enemies, mean position is " << meanOfEnemies << endl;
137            orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
138            orxout(internal_error) << "mean of Allies is " << meanOfAllies << ", with a size " << allies.size() << endl;
139            orxout(internal_error) << "mean of Enemies is " << meanOfEnemies << ", with a size " << enemies.size() << endl;
140            }
141            //-------------------------------------------------------
142           
143
144            this->setFormationMode(ATTACK);
145           
146            this->commandSlaves();
147
148            if  (this->specificMasterAction_ != NONE)
149                    this->specificMasterActionHold();
150
151            else {
152
153                 // make 180 degree turn - a specific Master Action
154                /*
155                random = rnd(1000.0f);
156                if (random < 5)
157                   this->turn180Init();
158
159                // spin around - a specific Master Action
160                random = rnd(1000.0f);
161                if (random < 5)
162                   this->spinInit();
163
164                */
165                /*// follow a randomly chosen human - a specific Master Action
166                random = rnd(1000.0f);
167                if (random < 1)
168                   this->followRandomHumanInit();
169*/
170               /*
171                 // lose master status (only if less than 4 slaves in formation)
172                random = rnd(maxrand);
173                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
174                   this->loseMasterState();
175                */
176               
177                // look out for outher masters if formation is small
178                random = rnd(maxrand);
179                if(this->slaves_.size() < 3 && random < 20)
180                    this->searchNewMaster();
181
182                this->defaultBehaviour(maxrand);
183
184            }
185        }
186        allies.clear();
187        enemies.clear();
188    }
189
190    void AIController::tick(float dt)
191    {
192
193        if (!this->isActive())
194            return;
195        float random;
196        float maxrand = 100.0f / ACTION_INTERVAL;
197        ControllableEntity* controllable = this->getControllableEntity();
198        //DOES: Either move to the waypoint or search for a Point of interest
199        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
200        {
201            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
202            {
203                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
204                if(wPoint)
205                {
206                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
207                    if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
208                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
209                }
210                else
211                    this->waypoints_.pop_back(); // remove invalid waypoints
212
213            }
214            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
215            {
216                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
217                random = rnd(maxrand);
218            }
219        }
220
221        if (this->mode_ == DEFAULT)
222        {
223            if (this->state_ == MASTER)
224            {
225                if (this->specificMasterAction_ ==  NONE)
226                {
227                    if (this->target_)
228                    {
229                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
230                            this->forgetTarget();
231                        else
232                        {
233                            this->aimAtTarget();
234                            this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
235                        }
236                    }
237
238                    if (this->bHasTargetPosition_)
239                        this->moveToTargetPosition();
240                    this->doFire();
241                }
242
243                if (this->specificMasterAction_  == TURN180)
244                    this->turn180();
245
246                if (this->specificMasterAction_ == SPIN)
247                    this->spin();
248                if (this->specificMasterAction_ == FOLLOW)
249                    this->follow();
250            }
251
252            if (this->state_ == SLAVE && this->formationMode_ != ATTACK)
253            {
254                if (this->bHasTargetPosition_)
255                    this->moveToTargetPosition();
256            }
257
258            if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) )
259            {
260                if (this->target_)
261                {
262                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
263                        this->forgetTarget();
264                    else this->aimAtTarget();
265                }
266
267                if (this->bHasTargetPosition_)
268                    this->moveToTargetPosition();
269
270                    this->doFire();
271            }
272        }
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            if(controllable)
276            {//Check wether the bot is controlling the rocket and if the timeout is over.
277                if(controllable->getIdentifier() == ClassByString("Rocket"))
278
279                {
280                    this->follow();
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
296        SUPER(AIController, tick, dt);
297    }
298//**********************************************NEW
299    void AIController::defaultBehaviour(float maxrand)
300    { 
301        if (!this->target_)
302            this->searchNewTarget();
303        if (!(this->passive_) && (this->target_ && !this->bShooting_))
304            this->bShooting_ = true;
305           
306    }
307
308}
Note: See TracBrowser for help on using the repository browser.