Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Didn't delete old structure, but intend to replace it with new: FleetController is a Base type, it should manage divisions, each consisting of 2 sections, each section is a pair of a Leader and a Wingman. Divisions can have different formation types: Echelon, Wall, Finger Four, Diamond, Vee. Leader decides what target to shoot first, wingmen cover Leader's rear. When 2 sections merge into a division, one of the Leaders becomes a Wingman. FleetController asks Leaders to attack targets, fly to positions or protect targets.

  • Property svn:eol-style set to native
File size: 12.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
89        if (this->state_ == SLAVE && this->formationMode_ == ATTACK) 
90        {
91            // search enemy
92            if ((!this->target_))
93                this->searchNewTarget();
94
95           
96            // shoot
97            if ((this->target_ && !this->bShooting_))
98                this->bShooting_ = true;
99
100            // stop shooting
101            if (this->bShooting_ && !this->target_)
102                this->bShooting_ = false;
103
104        }
105
106        if (this->state_ == MASTER)
107        {
108           
109            //-------------------------------------------------------
110            //collect data for AI behaviour
111            Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
112            Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
113            Vector3 meanOfAllies = *meanOfAlliesPtr;
114            Vector3 meanOfEnemies = *meanOfEnemiesPtr;
115
116
117            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
118            {
119
120                Gametype* gt=this->getGametype();
121                if (!gt)
122                {
123                    gt=it->getGametype();
124                }
125                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
126                {
127                    enemies_.push_back(*it);
128                } 
129                else {
130                    allies_.push_back(*it);
131                } 
132            }
133            if (enemies_.size() != 0 && allies_.size() != 0){
134                for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
135                    meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
136
137                meanOfEnemies /= enemies_.size();
138
139                for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
140                    meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
141
142                meanOfAllies /= allies_.size();
143
144                //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
145                orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
146                orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
147                orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
148            }
149            //-------------------------------------------------------
150           
151            //Decide which formationMode to choose
152            this->setFormationMode(ATTACK);
153            this->setDesiredPositionOfSlaves();
154
155            //this->commandSlaves();
156
157            if  (this->specificMasterAction_ != NONE)
158                    this->specificMasterActionHold();
159
160            else {
161
162                 // make 180 degree turn - a specific Master Action
163                /*
164                random = rnd(1000.0f);
165                if (random < 5)
166                   this->turn180Init();
167
168                // spin around - a specific Master Action
169                random = rnd(1000.0f);
170                if (random < 5)
171                   this->spinInit();
172
173                */
174                /*// follow a randomly chosen human - a specific Master Action
175                random = rnd(1000.0f);
176                if (random < 1)
177                   this->followRandomHumanInit();
178*/
179               /*
180                 // lose master status (only if less than 4 slaves in formation)
181                random = rnd(maxrand);
182                if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
183                   this->loseMasterState();
184                */
185               
186                // look out for outher masters if formation is small
187                random = rnd(maxrand);
188                if(this->slaves_.size() < 3 && random < 20)
189                    this->searchNewMaster();
190
191                this->defaultBehaviour(maxrand);
192
193            }
194        }
195        allies_.clear();
196        enemies_.clear();
197    }
198
199    void AIController::tick(float dt)
200    {
201
202        if (!this->isActive())
203            return;
204        float random;
205        float maxrand = 100.0f / ACTION_INTERVAL;
206        ControllableEntity* controllable = this->getControllableEntity();
207        if (this->state_ == SLAVE && controllable && this->mode_ == DEFAULT)
208        {
209           
210            if (this->myMaster_ && this->myMaster_->getControllableEntity() && desiredRelativePosition_){
211                Vector3 desiredAbsolutePosition = this->myMaster_->getControllableEntity()->getWorldPosition() + this->myMaster_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_);
212                this->moveToPosition (desiredAbsolutePosition);
213                //WorldEntity* waypoint = new WorldEntity(this->getContext());
214                //waypoint->setPosition(desiredAbsolutePosition);
215           
216                //this->addWaypoint(waypoint);
217           
218            }
219         
220        }
221        //DOES: Either move to the waypoint or search for a Point of interest
222        if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
223        {
224            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
225            {
226                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
227                if(wPoint)
228                {
229                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
230                    if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
231                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
232                }
233                else
234                    this->waypoints_.pop_back(); // remove invalid waypoints
235
236            }
237            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
238            {
239                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
240                random = rnd(maxrand);
241            }
242        }
243
244        if (this->mode_ == DEFAULT)
245        {
246            if (this->state_ == MASTER)
247            {
248                if (this->specificMasterAction_ ==  NONE)
249                {
250                    if (this->target_)
251                    {
252                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
253                            this->forgetTarget();
254                        else
255                        {
256                            this->aimAtTarget();
257                            this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
258                        }
259                    }
260
261                    if (this->bHasTargetPosition_)
262                        this->moveToTargetPosition();
263                    this->doFire();
264                }
265
266                if (this->specificMasterAction_  == TURN180)
267                    this->turn180();
268
269                if (this->specificMasterAction_ == SPIN)
270                    this->spin();
271                if (this->specificMasterAction_ == FOLLOW)
272                    this->follow();
273            }
274
275            if (this->state_ == SLAVE && this->formationMode_ != ATTACK)
276            {
277                if (this->bHasTargetPosition_)
278                    this->moveToTargetPosition();
279            }
280
281            if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) )
282            {
283                if (this->target_)
284                {
285                    if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
286                        this->forgetTarget();
287                    else this->aimAtTarget();
288                }
289
290                if (this->bHasTargetPosition_)
291                    this->moveToTargetPosition();
292
293                    this->doFire();
294            }
295        }
296        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
297        {   //Vector-implementation: mode_.back() == ROCKET;
298            if(controllable)
299            {//Check wether the bot is controlling the rocket and if the timeout is over.
300                if(controllable->getIdentifier() == ClassByString("Rocket"))
301
302                {
303                    this->follow();
304                    this->timeout_ -= dt;
305                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
306                    {
307                       controllable->fire(0);//kill the rocket
308                       this->setPreviousMode();//get out of rocket mode
309                    }
310                }
311                else
312                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
313            }
314            else
315                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
316        }//END_OF ROCKET MODE
317
318
319        SUPER(AIController, tick, dt);
320    }
321//**********************************************NEW
322    void AIController::defaultBehaviour(float maxrand)
323    { 
324        if (!this->target_)
325            this->searchNewTarget();
326        if (!(this->passive_) && (this->target_ && !this->bShooting_))
327            this->bShooting_ = true;
328           
329    }
330
331}
Note: See TracBrowser for help on using the repository browser.