Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/orxonox/controllers/ArtificialController.cc @ 6918

Last change on this file since 6918 was 6918, checked in by gasserlu, 14 years ago

drone follows in realistic matter

  • Property svn:eol-style set to native
File size: 17.6 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 *      ...
26 *
27 */
28
29#include "ArtificialController.h"
30
31#include "core/CoreIncludes.h"
32#include "core/XMLPort.h"
33#include "worldentities/ControllableEntity.h"
34#include "worldentities/pawns/Pawn.h"
35#include "worldentities/pawns/TeamBaseMatchBase.h"
36#include "gametypes/TeamDeathmatch.h"
37#include "controllers/WaypointPatrolController.h"
38#include "controllers/DroneController.h"
39#include "util/Math.h"
40
41namespace orxonox
42{
43
44    static const unsigned int MAX_FORMATION_SIZE = 6;
45    static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
46    static const float SPEED_MASTER = 0.6f;
47    static const float ROTATEFACTOR_MASTER = 0.2f;
48    static const float SPEED_FREE = 0.8f;
49    static const float ROTATEFACTOR_FREE = 0.8f;
50
51    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
52    {
53        RegisterObject(ArtificialController);
54
55        this->target_ = 0;
56        this->myMaster_ = 0;
57        this->freedomCount_ = 0;
58        this->team_ = -1;
59        this->state_ = FREE;
60        this->bShooting_ = false;
61        this->bHasTargetPosition_ = false;
62        this->targetPosition_ = Vector3::ZERO;
63
64        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
65    }
66
67    ArtificialController::~ArtificialController()
68    {
69    }
70
71    void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
72    {
73        SUPER(ArtificialController, XMLPort, xmlelement, mode);
74
75        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
76    }
77
78// gets called when Bot dies
79    void ArtificialController::changedControllableEntity()
80    {
81        if(!getControllableEntity()) 
82        {
83        if (this->state_ == SLAVE) unregisterSlave();
84         if (this->state_ == MASTER) setNewMasterWithinFormation();
85        this->slaves_.clear();
86        this->state_ = FREE;
87
88        }
89    }
90
91    void ArtificialController::moveToPosition(const Vector3& target)
92    {
93        if (!this->getControllableEntity())
94            return;
95
96        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
97        float distance = (target - this->getControllableEntity()->getPosition()).length();
98
99
100        if(this->state_ == FREE)
101        {
102            if (this->target_ || distance > 10)
103            {
104                // Multiply with 0.8 to make them a bit slower
105                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x);
106                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y);
107            }
108
109            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
110            {
111              this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
112            } else this->getControllableEntity()->moveFrontBack(SPEED_FREE);
113        }
114
115
116
117        if(this->state_ == MASTER)
118        {
119            if (this->target_ || distance > 10)
120            {
121                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
122                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
123
124            }
125
126            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
127            {
128                this->getControllableEntity()->moveFrontBack(-0.05f);
129            } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
130        }
131
132
133
134        if(this->state_ == SLAVE)
135        {
136//             if (this->target_ || distance > 10)
137//             {
138                float rotateFactor;
139                if(this->state_ == SLAVE) rotateFactor = 1.0f;
140
141
142                this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
143                this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
144
145
146
147//             }
148
149            if (this->target_ && distance < 500 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
150            {
151                if (this->target_ && distance < 60)
152                {
153                    this->getControllableEntity()->setVelocity(0.8f*this->target_->getVelocity());
154                } else this->getControllableEntity()->moveFrontBack(-1.0f*exp(distance/100.0));
155
156            } else {
157                this->getControllableEntity()->moveFrontBack(1.0f + distance/500.0f);
158            }
159        }
160    }
161
162    void ArtificialController::moveToTargetPosition()
163    {
164        this->moveToPosition(this->targetPosition_);
165    }
166
167    int ArtificialController::getState()
168    {
169        return this->state_;
170    }
171
172    void ArtificialController::unregisterSlave() {
173        if(myMaster_)
174        {
175            std::vector<ArtificialController*>::iterator it = std::find(myMaster_->slaves_.begin(), myMaster_->slaves_.end(), this);
176            if( it != myMaster_->slaves_.end() )
177                myMaster_->slaves_.erase(it);
178//COUT(0) << "~unregister slave" << std::endl;
179        }
180    }
181
182    void ArtificialController::searchNewMaster()
183    {
184
185        if (!this->getControllableEntity())
186            return;
187
188        this->targetPosition_ = this->getControllableEntity()->getPosition();
189        this->forgetTarget();
190
191        //go through all pawns
192        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
193        {
194
195            //same team?
196            if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
197                continue;
198
199            //has it an ArtificialController and is it a master?
200            if (!it->getController())
201                continue;
202
203            ArtificialController *newMaster = static_cast<ArtificialController*>(it->getController());
204
205            if (!newMaster || newMaster->getState() != MASTER)
206                continue;
207
208            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
209
210            //is pawn oneself? && is pawn in range?
211            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() && distance < 5000) 
212            {
213                if(newMaster->slaves_.size() > MAX_FORMATION_SIZE) continue;
214
215                for(std::vector<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
216                {
217                    (*itSlave)->myMaster_ = newMaster;
218                    newMaster->slaves_.push_back(*itSlave);
219                }
220                this->slaves_.clear();
221                this->state_ = SLAVE;
222
223                this->myMaster_ = newMaster;
224                newMaster->slaves_.push_back(this);
225
226                break;
227            }
228        }//for
229
230        //hasn't encountered any masters in range? -> become a master
231        if (state_!=SLAVE) state_ = MASTER;//master encounters master? ->done
232    }
233
234    void ArtificialController::commandSlaves() {
235
236        Quaternion orient = this->getControllableEntity()->getOrientation();
237        Vector3 dest = this->getControllableEntity()->getPosition();
238
239        // 1 slave: follow
240        if (this->slaves_.size() == 1)
241        {
242            dest += 4*orient*WorldEntity::BACK;
243            this->slaves_.front()->setTargetPosition(dest);
244        }
245
246        // 2 slaves: triangle
247         if (this->slaves_.size() == 2) 
248        {
249            dest += 10*orient*WorldEntity::BACK;
250            this->slaves_[0]->setTargetPosition(dest + 10*orient*WorldEntity::LEFT);
251            this->slaves_[1]->setTargetPosition(dest + 10*orient*WorldEntity::RIGHT);
252        }
253
254        if (this->slaves_.size() > MAX_FORMATION_SIZE)
255        {
256            for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
257            {
258                (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
259            }
260        }
261    }
262
263    // binds slaves to new Master within formation
264    void ArtificialController::setNewMasterWithinFormation()
265    {
266
267        if (this->slaves_.empty())
268            return;
269
270        ArtificialController *newMaster = this->slaves_.back();
271        this->slaves_.pop_back();
272
273        if(!newMaster) return;
274        newMaster->state_ = MASTER;
275        newMaster->slaves_ = this->slaves_;
276
277        this->slaves_.clear();
278        this->state_ = SLAVE;
279        this->myMaster_ = newMaster;
280
281        for(std::vector<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
282        {
283            (*it)->myMaster_ = newMaster;
284        }
285
286    }
287
288    void ArtificialController::freeSlaves()
289    {
290        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
291        {
292            (*it)->state_ = FREE;
293        }
294        this->slaves_.clear();
295    }
296
297    void ArtificialController::forceFreeSlaves()
298    {
299        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
300        {
301            (*it)->state_ = FREE;
302            (*it)->forceFreedom();
303            (*it)->targetPosition_ = this->targetPosition_;
304            (*it)->bShooting_ = true;
305            (*it)->getControllableEntity()->fire(0);// fire once for fun
306        }
307    }
308
309    void ArtificialController::loseMasterState()
310    {
311        this->freeSlaves();
312        this->state_ = FREE;
313    }
314
315    void ArtificialController::forceFreedom()
316    {
317        this->freedomCount_ = FREEDOM_COUNT;
318    }
319
320    bool ArtificialController::forcedFree()
321    {
322        if(this->freedomCount_ > 0) 
323        {
324            this->freedomCount_--;
325            return true;
326        } else return false;
327    }
328
329    void ArtificialController::setTargetPosition(const Vector3& target)
330    {
331        this->targetPosition_ = target;
332        this->bHasTargetPosition_ = true;
333    }
334
335    void ArtificialController::searchRandomTargetPosition()
336    {
337        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
338        this->bHasTargetPosition_ = true;
339    }
340
341    void ArtificialController::setTarget(Pawn* target)
342    {
343        this->target_ = target;
344
345        if (target)
346            this->targetPosition_ = target->getPosition();
347    }
348
349    void ArtificialController::searchNewTarget()
350    {
351COUT(0) << "search new target - start" << std::endl;
352        if (!this->getControllableEntity())
353            return;
354
355        this->targetPosition_ = this->getControllableEntity()->getPosition();
356        this->forgetTarget();
357
358        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
359        {
360            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
361                continue;
362
363            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
364            {
365                float speed = this->getControllableEntity()->getVelocity().length();
366                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
367                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
368                if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / (2 * Ogre::Math::PI))
369                        < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)) + rnd(-250, 250))
370                {
371                    this->target_ = (*it);
372                    this->targetPosition_ = it->getPosition();
373                }
374            }
375        }
376COUT(0) << "search new target - end: " << this->target_ << std::endl;
377    }
378
379    void ArtificialController::forgetTarget()
380    {
381        this->target_ = 0;
382        this->bShooting_ = false;
383    }
384
385    void ArtificialController::aimAtTarget()
386    {
387        if (!this->target_ || !this->getControllableEntity())
388            return;
389
390        static const float hardcoded_projectile_speed = 1250;
391
392        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
393        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
394
395        Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity());
396        if (pawn)
397            pawn->setAimPosition(this->targetPosition_);
398    }
399
400    bool ArtificialController::isCloseAtTarget(float distance) const
401    {
402        if (!this->getControllableEntity())
403            return false;
404
405        if (!this->target_)
406            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
407        else
408            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
409    }
410
411    bool ArtificialController::isLookingAtTarget(float angle) const
412    {
413        if (!this->getControllableEntity())
414            return false;
415
416        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
417    }
418
419    void ArtificialController::abandonTarget(Pawn* target)
420    {
421        if (target == this->target_)
422            this->targetDied();
423    }
424
425    void ArtificialController::targetDied()
426    {
427        this->forgetTarget();
428        this->searchRandomTargetPosition();
429    }
430
431    bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
432    {
433        if (entity1 == entity2)
434            return true;
435
436        int team1 = -1;
437        int team2 = -1;
438
439        Controller* controller = 0;
440        if (entity1->getController())
441            controller = entity1->getController();
442        else
443            controller = entity1->getXMLController();
444        if (controller)
445        {
446            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
447            if (ac)
448                team1 = ac->getTeam();
449        }
450
451        if (entity1->getController())
452            controller = entity1->getController();
453        else
454            controller = entity1->getXMLController();
455        if (controller)
456        {
457            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
458            if (ac)
459                team2 = ac->getTeam();
460        }
461
462        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
463        if (tdm)
464        {
465            if (entity1->getPlayer())
466                team1 = tdm->getTeam(entity1->getPlayer());
467
468            if (entity2->getPlayer())
469                team2 = tdm->getTeam(entity2->getPlayer());
470        }
471
472        TeamBaseMatchBase* base = 0;
473        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
474        if (base)
475        {
476            switch (base->getState())
477            {
478                case BaseState::ControlTeam1:
479                    team1 = 0;
480                    break;
481                case BaseState::ControlTeam2:
482                    team1 = 1;
483                    break;
484                case BaseState::Uncontrolled:
485                default:
486                    team1 = -1;
487            }
488        }
489        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
490        if (base)
491        {
492            switch (base->getState())
493            {
494                case BaseState::ControlTeam1:
495                    team2 = 0;
496                    break;
497                case BaseState::ControlTeam2:
498                    team2 = 1;
499                    break;
500                case BaseState::Uncontrolled:
501                default:
502                    team2 = -1;
503            }
504        }
505
506        DroneController* droneController = 0;
507        droneController = orxonox_cast<DroneController*>(entity1->getController());
508        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
509            return true;
510        droneController = orxonox_cast<DroneController*>(entity2->getController());
511        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1)
512            return true;
513           
514        return (team1 == team2 && team1 != -1);
515    }
516}
Note: See TracBrowser for help on using the repository browser.