Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

master now lists slaves

  • Property svn:eol-style set to native
File size: 12.5 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
54        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
55    }
56
57    ArtificialController::~ArtificialController()
58    {
59    }
60
61    void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
62    {
63        SUPER(ArtificialController, XMLPort, xmlelement, mode);
64
65        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
66    }
67
68    void ArtificialController::moveToPosition(const Vector3& target)
69    {
70        if (!this->getControllableEntity())
71            return;
72
73        Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
74        float distance = (target - this->getControllableEntity()->getPosition()).length();
75
76        if (this->target_ || distance > 10)
77        {
78            // Multiply with 0.8 to make them a bit slower
79            this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x);
80            this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y);
81        }
82
83        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
84            this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance
85        else
86            this->getControllableEntity()->moveFrontBack(0.8f);
87    }
88
89    void ArtificialController::moveToTargetPosition()
90    {
91        this->moveToPosition(this->targetPosition_);
92    }
93
94    int ArtificialController::getState()
95    {
96        return this->state_;
97    }
98
99    void ArtificialController::unregisterSlave() {
100        if(myMaster_)
101        {
102            myMaster_->slaves.remove(this);
103        }
104    }
105
106    void ArtificialController::searchNewMaster()
107    {
108
109        if (!this->getControllableEntity())
110            return;
111
112        this->targetPosition_ = this->getControllableEntity()->getPosition();
113        this->forgetTarget();
114
115        //go through all pawns
116        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
117        {
118
119            //same team?
120            if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
121                continue;
122
123            //has it an ArtificialController and is it a master?
124            if (!it->getController())
125                continue;
126
127            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
128            if (!controller || controller->getState() != MASTER)
129                continue;
130
131            //is pawn oneself? && is pawn in range?
132            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000
133            {
134                this->state_ = SLAVE;
135                this->myMaster_ = controller;
136                controller->slaves.push_back(this);
137                break;
138            }
139        }//for
140
141        //hasn't encountered any masters in range? -> become a master
142        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...
143
144    }
145
146    void ArtificialController::commandSlaves() {
147
148        for(std::list<ArtificialController*>::iterator it = slaves.begin(); it != slaves.end(); it++)
149        {
150            (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
151        }
152/*
153        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
154        {
155
156            if (!it->getController())
157                continue;
158
159            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
160            if (!controller || controller->getState() != SLAVE)
161                continue;
162            //controller->setTargetPosition(this->getControllableEntity()->getPosition());
163            controller->targetPosition_ = this->getControllableEntity()->getPosition();
164            controller->bHasTargetPosition_ = true;
165        }
166*/
167    }
168
169    void ArtificialController::freeAllSlaves()
170    {
171
172        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
173        {
174            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
175            if (controller && controller->getState() != SLAVE)
176                continue;
177
178            controller->state_ = FREE;
179        }
180
181    }
182
183
184    void ArtificialController::setTargetPosition(const Vector3& target)
185    {
186        this->targetPosition_ = target;
187        this->bHasTargetPosition_ = true;
188    }
189
190    void ArtificialController::searchRandomTargetPosition()
191    {
192        this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000));
193        this->bHasTargetPosition_ = true;
194    }
195
196    void ArtificialController::setTarget(Pawn* target)
197    {
198        this->target_ = target;
199
200        if (target)
201            this->targetPosition_ = target->getPosition();
202    }
203
204    void ArtificialController::searchNewTarget()
205    {
206        if (!this->getControllableEntity())
207            return;
208
209        this->targetPosition_ = this->getControllableEntity()->getPosition();
210        this->forgetTarget();
211
212        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
213        {
214            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
215                continue;
216
217            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
218            {
219                float speed = this->getControllableEntity()->getVelocity().length();
220                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
221                Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
222                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))
223                        < 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))
224                {
225                    this->target_ = (*it);
226                    this->targetPosition_ = it->getPosition();
227                }
228            }
229        }
230    }
231
232    void ArtificialController::forgetTarget()
233    {
234        this->target_ = 0;
235        this->bShooting_ = false;
236    }
237
238    void ArtificialController::aimAtTarget()
239    {
240        if (!this->target_ || !this->getControllableEntity())
241            return;
242
243        static const float hardcoded_projectile_speed = 1250;
244
245        this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity());
246        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
247
248        Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity());
249        if (pawn)
250            pawn->setAimPosition(this->targetPosition_);
251    }
252
253    bool ArtificialController::isCloseAtTarget(float distance) const
254    {
255        if (!this->getControllableEntity())
256            return false;
257
258        if (!this->target_)
259            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
260        else
261            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
262    }
263
264    bool ArtificialController::isLookingAtTarget(float angle) const
265    {
266        if (!this->getControllableEntity())
267            return false;
268
269        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
270    }
271
272    void ArtificialController::abandonTarget(Pawn* target)
273    {
274        if (target == this->target_)
275            this->targetDied();
276    }
277
278    void ArtificialController::targetDied()
279    {
280        this->forgetTarget();
281        this->searchRandomTargetPosition();
282    }
283
284    bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
285    {
286        if (entity1 == entity2)
287            return true;
288
289        int team1 = -1;
290        int team2 = -1;
291
292        Controller* controller = 0;
293        if (entity1->getController())
294            controller = entity1->getController();
295        else
296            controller = entity1->getXMLController();
297        if (controller)
298        {
299            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
300            if (ac)
301                team1 = ac->getTeam();
302        }
303
304        if (entity1->getController())
305            controller = entity1->getController();
306        else
307            controller = entity1->getXMLController();
308        if (controller)
309        {
310            ArtificialController* ac = orxonox_cast<ArtificialController*>(controller);
311            if (ac)
312                team2 = ac->getTeam();
313        }
314
315        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
316        if (tdm)
317        {
318            if (entity1->getPlayer())
319                team1 = tdm->getTeam(entity1->getPlayer());
320
321            if (entity2->getPlayer())
322                team2 = tdm->getTeam(entity2->getPlayer());
323        }
324
325        TeamBaseMatchBase* base = 0;
326        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
327        if (base)
328        {
329            switch (base->getState())
330            {
331                case BaseState::ControlTeam1:
332                    team1 = 0;
333                    break;
334                case BaseState::ControlTeam2:
335                    team1 = 1;
336                    break;
337                case BaseState::Uncontrolled:
338                default:
339                    team1 = -1;
340            }
341        }
342        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
343        if (base)
344        {
345            switch (base->getState())
346            {
347                case BaseState::ControlTeam1:
348                    team2 = 0;
349                    break;
350                case BaseState::ControlTeam2:
351                    team2 = 1;
352                    break;
353                case BaseState::Uncontrolled:
354                default:
355                    team2 = -1;
356            }
357        }
358
359        return (team1 == team2 && team1 != -1);
360    }
361}
Note: See TracBrowser for help on using the repository browser.