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, 14 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
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
39namespace orxonox
40{
41    ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
42    {
43        RegisterObject(ArtificialController);
44
45        this->target_ = 0;
46        this->myMaster_ = 0;
47        //this->slaveNumber_ = -1;
48        this->team_ = -1;//new
49        this->state_ = FREE;//new
50        this->bShooting_ = false;
51        this->bHasTargetPosition_ = false;
52        this->targetPosition_ = Vector3::ZERO;
53        //this->slaves_ = new std::list<ArtificialController*>;
54
55        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
56    }
57
58    ArtificialController::~ArtificialController()
59    {
60    }
61
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
69    void ArtificialController::moveToPosition(const Vector3& target)
70    {
71        if (!this->getControllableEntity())
72            return;
73
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();
76
77        if (this->target_ || distance > 10)
78        {
79            // Multiply with 0.8 to make them a bit slower
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);
82        }
83
84        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
85            this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance
86        else
87            this->getControllableEntity()->moveFrontBack(0.8f);
88    }
89
90    void ArtificialController::moveToTargetPosition()
91    {
92        this->moveToPosition(this->targetPosition_);
93    }
94
95    int ArtificialController::getState()
96    {
97        return this->state_;
98    }
99
100    void ArtificialController::unregisterSlave() {
101        if(myMaster_)
102        {
103            myMaster_->slaves_.remove(this);
104        }
105    }
106
107    void ArtificialController::searchNewMaster()
108    {
109
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        {
119
120            //same team?
121            if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
122                continue;
123
124            //has it an ArtificialController and is it a master?
125            if (!it->getController())
126                continue;
127
128            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
129            if (!controller || controller->getState() != MASTER)
130                continue;
131
132            //is pawn oneself? && is pawn in range?
133            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000
134            {
135                if(controller->slaves_.size() > 9) continue;
136
137                this->freeAllSlaves();
138                this->slaves_.clear();
139                this->state_ = SLAVE;
140
141                this->myMaster_ = controller;
142                controller->slaves_.push_back(this);
143
144                break;
145            }
146        }//for
147
148        //hasn't encountered any masters in range? -> become a master
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
151    }
152
153    void ArtificialController::commandSlaves() {
154
155        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
156        {
157            (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
158        }
159/*
160        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
161        {
162
163            if (!it->getController())
164                continue;
165
166            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
167            if (!controller || controller->getState() != SLAVE)
168                continue;
169            //controller->setTargetPosition(this->getControllableEntity()->getPosition());
170            controller->targetPosition_ = this->getControllableEntity()->getPosition();
171            controller->bHasTargetPosition_ = true;
172        }
173*/
174    }
175
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
207    void ArtificialController::freeAllSlaves()
208    {
209
210
211        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
212        {
213            (*it)->state_ = FREE;
214        }
215/*
216        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
217        {
218            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
219            if (controller && controller->getState() != SLAVE)
220                continue;
221
222            controller->state_ = FREE;
223        }
224*/
225    }
226
227    void ArtificialController::loseMasterState()
228    {
229        this->freeAllSlaves();
230        this->state_ = FREE;
231    }
232
233    void ArtificialController::setTargetPosition(const Vector3& target)
234    {
235        this->targetPosition_ = target;
236        this->bHasTargetPosition_ = true;
237    }
238
239    void ArtificialController::searchRandomTargetPosition()
240    {
241        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
242        this->bHasTargetPosition_ = true;
243    }
244
245    void ArtificialController::setTarget(Pawn* target)
246    {
247        this->target_ = target;
248
249        if (target)
250            this->targetPosition_ = target->getPosition();
251    }
252
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        {
263            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
264                continue;
265
266            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
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
292        static const float hardcoded_projectile_speed = 1250;
293
294        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
295        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
296
297        Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity());
298        if (pawn)
299            pawn->setAimPosition(this->targetPosition_);
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
321    void ArtificialController::abandonTarget(Pawn* target)
322    {
323        if (target == this->target_)
324            this->targetDied();
325    }
326
327    void ArtificialController::targetDied()
328    {
329        this->forgetTarget();
330        this->searchRandomTargetPosition();
331    }
332
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
341        Controller* controller = 0;
342        if (entity1->getController())
343            controller = entity1->getController();
344        else
345            controller = entity1->getXMLController();
346        if (controller)
347        {
348            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
349            if (ac)
350                team1 = ac->getTeam();
351        }
352
353        if (entity1->getController())
354            controller = entity1->getController();
355        else
356            controller = entity1->getXMLController();
357        if (controller)
358        {
359            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
360            if (ac)
361                team2 = ac->getTeam();
362        }
363
364        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
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
374        TeamBaseMatchBase* base = 0;
375        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
376        if (base)
377        {
378            switch (base->getState())
379            {
380                case BaseState::ControlTeam1:
381                    team1 = 0;
382                    break;
383                case BaseState::ControlTeam2:
384                    team1 = 1;
385                    break;
386                case BaseState::Uncontrolled:
387                default:
388                    team1 = -1;
389            }
390        }
391        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
392        if (base)
393        {
394            switch (base->getState())
395            {
396                case BaseState::ControlTeam1:
397                    team2 = 0;
398                    break;
399                case BaseState::ControlTeam2:
400                    team2 = 1;
401                    break;
402                case BaseState::Uncontrolled:
403                default:
404                    team2 = -1;
405            }
406        }
407
408        return (team1 == team2 && team1 != -1);
409    }
410}
Note: See TracBrowser for help on using the repository browser.