Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6833 was 6795, checked in by solex, 15 years ago

death of master results in forming a new formation. Correct response to master death still to solve.

  • Property svn:eol-style set to native
File size: 14.1 KB
RevLine 
[2362]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"
[6601]32#include "core/XMLPort.h"
[5735]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"
[3049]38
[2362]39namespace orxonox
40{
41    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
42    {
43        RegisterObject(ArtificialController);
44
45        this->target_ = 0;
[6695]46        this->myMaster_ = 0;
47        //this->slaveNumber_ = -1;
[6640]48        this->team_ = -1;//new
[6683]49        this->state_ = FREE;//new
[2362]50        this->bShooting_ = false;
51        this->bHasTargetPosition_ = false;
52        this->targetPosition_ = Vector3::ZERO;
[6795]53        //this->slaves_ = new std::list<ArtificialController*>;
[6417]54
[5929]55        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
[2362]56    }
57
58    ArtificialController::~ArtificialController()
59    {
60    }
61
[6601]62    void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
63    {
64        SUPER(ArtificialController, XMLPort, xmlelement, mode);
65
66        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
67    }
68
[3049]69    void ArtificialController::moveToPosition(const Vector3& target)
[2362]70    {
71        if (!this->getControllableEntity())
72            return;
73
[3049]74        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
75        float distance = (target - this->getControllableEntity()->getPosition()).length();
[2362]76
[2493]77        if (this->target_ || distance > 10)
[2362]78        {
79            // Multiply with 0.8 to make them a bit slower
[6502]80            this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x);
81            this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y);
[2362]82        }
83
[2493]84        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
[6502]85            this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance
[2362]86        else
[6502]87            this->getControllableEntity()->moveFrontBack(0.8f);
[2362]88    }
89
[3049]90    void ArtificialController::moveToTargetPosition()
91    {
92        this->moveToPosition(this->targetPosition_);
93    }
94
[6640]95    int ArtificialController::getState()
96    {
97        return this->state_;
98    }
99
[6695]100    void ArtificialController::unregisterSlave() {
101        if(myMaster_)
102        {
[6795]103            myMaster_->slaves_.remove(this);
[6695]104        }
105    }
106
[6640]107    void ArtificialController::searchNewMaster()
108    {
[6695]109
[6640]110        if (!this->getControllableEntity())
111            return;
112
113        this->targetPosition_ = this->getControllableEntity()->getPosition();
114        this->forgetTarget();
115
116        //go through all pawns
117        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
118        {
[6695]119
120            //same team?
[6640]121            if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
122                continue;
123
[6695]124            //has it an ArtificialController and is it a master?
125            if (!it->getController())
126                continue;
[6640]127
128            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
[6695]129            if (!controller || controller->getState() != MASTER)
[6640]130                continue;
131
132            //is pawn oneself? && is pawn in range?
[6695]133            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000
[6640]134            {
[6795]135                if(controller->slaves_.size() > 9) continue;
[6696]136
137                this->freeAllSlaves();
[6795]138                this->slaves_.clear();
[6683]139                this->state_ = SLAVE;
[6696]140
[6695]141                this->myMaster_ = controller;
[6795]142                controller->slaves_.push_back(this);
[6696]143
[6695]144                break;
[6640]145            }
146        }//for
147
148        //hasn't encountered any masters in range? -> become a master
[6695]149        if (state_!=SLAVE) state_ = MASTER; // keep in mind: what happens when two masters encounter eache other? -> has to be evaluated in the for loop within master mode in AIcontroller...
150
[6640]151    }
152
[6683]153    void ArtificialController::commandSlaves() {
154
[6795]155        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
[6695]156        {
157            (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
158        }
159/*
[6683]160        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
161        {
[6695]162
163            if (!it->getController())
164                continue;
165
[6683]166            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
[6695]167            if (!controller || controller->getState() != SLAVE)
[6683]168                continue;
[6695]169            //controller->setTargetPosition(this->getControllableEntity()->getPosition());
[6683]170            controller->targetPosition_ = this->getControllableEntity()->getPosition();
[6695]171            controller->bHasTargetPosition_ = true;
[6683]172        }
[6695]173*/
[6683]174    }
175
[6795]176    // binds slaves to new Master within formation
177    void ArtificialController::setNewMasterWithinFormation()
178    {
179COUT(0) << "~setNewMasterWithinFormation 1" << std::endl;
180        if (this->slaves_.empty())
181            return;
182COUT(0) << "~setNewMasterWithinFormation 1b" << std::endl;
183
184        ArtificialController *newMaster = this->slaves_.back();
185COUT(0) << "~setNewMasterWithinFormation 2" << std::endl;
186        this->slaves_.pop_back();
187COUT(0) << "~setNewMasterWithinFormation 3" << std::endl;
188        if(!newMaster) return;
189COUT(0) << "~setNewMasterWithinFormation 4" << std::endl;
190        newMaster->state_ = MASTER;
191        newMaster->slaves_ = this->slaves_;
192        //this->slaves_.clear();
193
194        this->state_ = SLAVE;
195        this->myMaster_ = newMaster;
196
197COUT(0) << "~setNewMasterWithinFormation 5" << std::endl;
198        for(std::list<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
199        {
200COUT(0) << "~setNewMasterWithinFormation 6" << std::endl;
201            (*it)->myMaster_ = newMaster;
202        }
203COUT(0) << "~setNewMasterWithinFormation 7" << std::endl;
204
205    }
206
[6683]207    void ArtificialController::freeAllSlaves()
208    {
[6695]209
[6696]210
[6795]211        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
[6696]212        {
213            (*it)->state_ = FREE;
214        }
215/*
[6683]216        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
217        {
218            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
[6695]219            if (controller && controller->getState() != SLAVE)
[6683]220                continue;
221
222            controller->state_ = FREE;
223        }
[6696]224*/
225    }
[6683]226
[6696]227    void ArtificialController::loseMasterState()
228    {
229        this->freeAllSlaves();
230        this->state_ = FREE;
[6683]231    }
232
[3049]233    void ArtificialController::setTargetPosition(const Vector3& target)
234    {
235        this->targetPosition_ = target;
236        this->bHasTargetPosition_ = true;
237    }
238
[2362]239    void ArtificialController::searchRandomTargetPosition()
240    {
[2493]241        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
[2362]242        this->bHasTargetPosition_ = true;
243    }
244
[3049]245    void ArtificialController::setTarget(Pawn* target)
246    {
247        this->target_ = target;
248
249        if (target)
250            this->targetPosition_ = target->getPosition();
251    }
252
[2362]253    void ArtificialController::searchNewTarget()
254    {
255        if (!this->getControllableEntity())
256            return;
257
258        this->targetPosition_ = this->getControllableEntity()->getPosition();
259        this->forgetTarget();
260
261        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
262        {
[3049]263            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
264                continue;
265
[2506]266            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
[2362]267            {
268                float speed = this->getControllableEntity()->getVelocity().length();
269                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
270                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
271                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))
272                        < 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))
273                {
274                    this->target_ = (*it);
275                    this->targetPosition_ = it->getPosition();
276                }
277            }
278        }
279    }
280
281    void ArtificialController::forgetTarget()
282    {
283        this->target_ = 0;
284        this->bShooting_ = false;
285    }
286
287    void ArtificialController::aimAtTarget()
288    {
289        if (!this->target_ || !this->getControllableEntity())
290            return;
291
[2493]292        static const float hardcoded_projectile_speed = 1250;
[2362]293
[2493]294        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
[2362]295        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
[6417]296
297        Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity());
298        if (pawn)
299            pawn->setAimPosition(this->targetPosition_);
[2362]300    }
301
302    bool ArtificialController::isCloseAtTarget(float distance) const
303    {
304        if (!this->getControllableEntity())
305            return false;
306
307        if (!this->target_)
308            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
309        else
310            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
311    }
312
313    bool ArtificialController::isLookingAtTarget(float angle) const
314    {
315        if (!this->getControllableEntity())
316            return false;
317
318        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
319    }
320
[5929]321    void ArtificialController::abandonTarget(Pawn* target)
[2362]322    {
[5929]323        if (target == this->target_)
324            this->targetDied();
[2362]325    }
[3049]326
[5929]327    void ArtificialController::targetDied()
328    {
329        this->forgetTarget();
330        this->searchRandomTargetPosition();
331    }
332
[3049]333    bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
334    {
335        if (entity1 == entity2)
336            return true;
337
338        int team1 = -1;
339        int team2 = -1;
340
[6601]341        Controller* controller = 0;
342        if (entity1->getController())
343            controller = entity1->getController();
344        else
345            controller = entity1->getXMLController();
346        if (controller)
[3049]347        {
[6601]348            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
349            if (ac)
350                team1 = ac->getTeam();
[3049]351        }
[6601]352
353        if (entity1->getController())
354            controller = entity1->getController();
355        else
356            controller = entity1->getXMLController();
357        if (controller)
[3049]358        {
[6601]359            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
360            if (ac)
361                team2 = ac->getTeam();
[3049]362        }
363
[3325]364        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
[3049]365        if (tdm)
366        {
367            if (entity1->getPlayer())
368                team1 = tdm->getTeam(entity1->getPlayer());
369
370            if (entity2->getPlayer())
371                team2 = tdm->getTeam(entity2->getPlayer());
372        }
373
[3086]374        TeamBaseMatchBase* base = 0;
[3325]375        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
[3086]376        if (base)
377        {
378            switch (base->getState())
379            {
[3280]380                case BaseState::ControlTeam1:
[3086]381                    team1 = 0;
382                    break;
[3280]383                case BaseState::ControlTeam2:
[3086]384                    team1 = 1;
385                    break;
[3280]386                case BaseState::Uncontrolled:
[3086]387                default:
388                    team1 = -1;
389            }
390        }
[3325]391        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
[3086]392        if (base)
393        {
394            switch (base->getState())
395            {
[3280]396                case BaseState::ControlTeam1:
[3086]397                    team2 = 0;
398                    break;
[3280]399                case BaseState::ControlTeam2:
[3086]400                    team2 = 1;
401                    break;
[3280]402                case BaseState::Uncontrolled:
[3086]403                default:
404                    team2 = -1;
405            }
406        }
407
[3049]408        return (team1 == team2 && team1 != -1);
409    }
[2362]410}
Note: See TracBrowser for help on using the repository browser.