Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/SpaceShipAI.cc @ 1772

Last change on this file since 1772 was 1772, checked in by scheusso, 16 years ago

fixed a bug in spaceship ai(segfault when client disconnects && client was target of spaceshipAI)

  • Property svn:eol-style set to native
File size: 11.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 "OrxonoxStableHeaders.h"
30#include "SpaceShipAI.h"
31
32#include <OgreMath.h>
33#include "Projectile.h"
34#include "ParticleSpawner.h"
35#include "core/CoreIncludes.h"
36#include "core/Iterator.h"
37#include "core/Executor.h"
38#include "core/ConsoleCommand.h"
39#include "core/XMLPort.h"
40#include "tools/ParticleInterface.h"
41#include "Settings.h"
42
43#define ACTION_INTERVAL 1.0f
44
45namespace orxonox
46{
47    SetConsoleCommand(SpaceShipAI, createEnemy, true).defaultValue(0, 1);
48    SetConsoleCommand(SpaceShipAI, killEnemies, true).defaultValue(0, 0);
49
50    CreateFactory(SpaceShipAI);
51
52    SpaceShipAI::SpaceShipAI()
53    {
54        RegisterObject(SpaceShipAI);
55
56        this->target_ = 0;
57        this->bShooting_ = 0;
58        this->bHasTargetPosition_ = false;
59
60        this->setTeamNr((int)rnd(NUM_AI_TEAMS) % NUM_AI_TEAMS + 1);
61
62        if (NUM_AI_TEAMS > 0)
63            this->teamColours_[1] = ColourValue(1, 0, 0, 1);
64        if (NUM_AI_TEAMS > 1)
65            this->teamColours_[2] = ColourValue(0, 1, 0, 1);
66        if (NUM_AI_TEAMS > 2)
67            this->teamColours_[3] = ColourValue(0, 0, 1, 1);
68
69        for (int i = 4; i <= NUM_AI_TEAMS; ++i)
70            this->teamColours_[i] = ColourValue(rnd(), rnd(), rnd(), 1);
71    }
72
73    SpaceShipAI::~SpaceShipAI()
74    {
75    }
76
77    void SpaceShipAI::XMLPort(Element& xmlelement, XMLPort::Mode mode)
78    {
79        SUPER(SpaceShipAI, XMLPort, xmlelement, mode);
80
81        this->actionTimer_.setTimer(ACTION_INTERVAL, true, this, createExecutor(createFunctor(&SpaceShipAI::action)));
82    }
83
84    void SpaceShipAI::createEnemy(int num)
85    {
86        for (int i = 0; i < num; ++i)
87        {
88            SpaceShipAI* newenemy = new SpaceShipAI();
89            newenemy->setMesh("assff.mesh");
90//            newenemy->setPosition(0, 0, 0);
91            newenemy->setPosition(Vector3(rnd(-3000, 3000), rnd(-3000, 3000), rnd(-3000, 3000)));
92            newenemy->setScale(10);
93            newenemy->setMaxSpeed(500);
94            newenemy->setMaxSideAndBackSpeed(50);
95            newenemy->setMaxRotation(1.0);
96            newenemy->setTransAcc(200);
97            newenemy->setRotAcc(3.0);
98            newenemy->setTransDamp(75);
99            newenemy->setRotDamp(1.0);
100            Element xmlelement;
101            newenemy->XMLPort(xmlelement, XMLPort::LoadObject);
102
103            if (Settings::showsGraphics())
104            {
105                ParticleSpawner* spawneffect = new ParticleSpawner("Orxonox/fairytwirl", LODParticle::normal, 2.0, 0, 0, newenemy->getOrth());
106                spawneffect->setPosition(newenemy->getPosition() - newenemy->getOrth() * 50);
107                spawneffect->create();
108            }
109        }
110    }
111
112    void SpaceShipAI::killEnemies(int num)
113    {
114        int i = 0;
115        for (ObjectList<SpaceShipAI>::iterator it = ObjectList<SpaceShipAI>::begin(); it; )
116        {
117            (it++)->kill();
118            if (num && i >= num)
119                break;
120        }
121    }
122
123    ColourValue SpaceShipAI::getProjectileColour() const
124    {
125        return this->teamColours_[this->getTeamNr()];
126    }
127
128    void SpaceShipAI::action()
129    {
130        float random;
131        float maxrand = 100.0f / ACTION_INTERVAL;
132
133        // search enemy
134        random = rnd(maxrand);
135        if (random < 15 && (!this->target_))
136            this->searchNewTarget();
137
138        // forget enemy
139        random = rnd(maxrand);
140        if (random < 5 && (this->target_))
141            this->forgetTarget();
142
143        // next enemy
144        random = rnd(maxrand);
145        if (random < 10 && (this->target_))
146            this->searchNewTarget();
147
148        // fly somewhere
149        random = rnd(maxrand);
150        if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
151            this->searchNewTargetPosition();
152
153        // stop flying
154        random = rnd(maxrand);
155        if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
156            this->bHasTargetPosition_ = false;
157
158        // fly somewhere else
159        random = rnd(maxrand);
160        if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
161            this->searchNewTargetPosition();
162
163        // shoot
164        random = rnd(maxrand);
165        if (random < 75 && (this->target_ && !this->bShooting_))
166            this->bShooting_ = true;
167
168        // stop shooting
169        random = rnd(maxrand);
170        if (random < 25 && (this->bShooting_))
171            this->bShooting_ = false;
172    }
173
174    void SpaceShipAI::damage(float damage)
175    {
176        this->health_ -= damage;
177        if (this->health_ <= 0)
178        {
179            this->kill();
180            SpaceShipAI::createEnemy(1);
181        }
182    }
183
184    void SpaceShipAI::kill()
185    {
186        if (Settings::showsGraphics())
187        {
188            ParticleSpawner* explosion = new ParticleSpawner("Orxonox/BigExplosion1part1", LODParticle::low, 3.0);
189            explosion->setPosition(this->getPosition());
190            explosion->getParticleInterface()->setKeepParticlesInLocalSpace(true);
191            explosion->setScale(4);
192            explosion->create();
193
194            explosion = new ParticleSpawner("Orxonox/BigExplosion1part2", LODParticle::normal, 3.0);
195            explosion->setPosition(this->getPosition());
196            explosion->getParticleInterface()->setKeepParticlesInLocalSpace(true);
197            explosion->setScale(4);
198            explosion->create();
199            explosion = new ParticleSpawner("Orxonox/BigExplosion1part2", LODParticle::high, 3.0);
200            explosion->setPosition(this->getPosition());
201            explosion->getParticleInterface()->setKeepParticlesInLocalSpace(true);
202            explosion->setScale(4);
203            explosion->create();
204
205            Vector3 ringdirection = Vector3(rnd(), rnd(), rnd());
206            ringdirection.normalise();
207            explosion = new ParticleSpawner("Orxonox/BigExplosion1part3", LODParticle::normal, 3.0, 0.5, 0, ringdirection);
208            explosion->setPosition(this->getPosition());
209            explosion->getParticleInterface()->setKeepParticlesInLocalSpace(true);
210            explosion->setScale(4);
211            explosion->create();
212            explosion = new ParticleSpawner("Orxonox/BigExplosion1part3", LODParticle::high, 3.0, 0.5, 0, ringdirection);
213            explosion->setPosition(this->getPosition());
214            explosion->getParticleInterface()->setKeepParticlesInLocalSpace(true);
215            explosion->setScale(4);
216            explosion->create();
217        }
218
219        delete this;
220    }
221
222    void SpaceShipAI::tick(float dt)
223    {
224        if (!this->isActive())
225            return;
226
227        if (this->target_)
228            this->aimAtTarget();
229
230        if (this->bHasTargetPosition_)
231            this->moveToTargetPosition(dt);
232
233        if (this->bShooting_ && this->isCloseAtTarget(2500) && this->isLookingAtTarget(Ogre::Math::PI / 20.0))
234            this->doFire();
235
236        SUPER(SpaceShipAI, tick, dt);
237    }
238
239    void SpaceShipAI::moveToTargetPosition(float dt)
240    {
241        Vector2 coord = get2DViewdirection(this->getPosition(), this->getDir(), this->getOrth(), this->targetPosition_);
242
243        float distance = (this->targetPosition_ - this->getPosition()).length();
244        if (this->target_ || distance > 50)
245        {
246            // Multiply with 0.8 to make them a bit slower
247            this->setMoveYaw(0.8 * sgn(coord.x) * coord.x*coord.x);
248            this->setMovePitch(0.8 * sgn(coord.y) * coord.y*coord.y);
249        }
250
251        if (this->target_ && distance < 1000 && this->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
252            this->setMoveLongitudinal(-0.5); // They don't brake with full power to give the player a chance
253        else if (!this->target_ && distance <= this->getVelocity().length() / (2 * this->getTransAcc()))
254            this->setMoveLongitudinal(-1.0);
255        else
256            this->setMoveLongitudinal(0.8);
257    }
258
259    void SpaceShipAI::searchNewTargetPosition()
260    {
261        this->targetPosition_ = Vector3(rnd(-5000,5000), rnd(-5000,5000), rnd(-5000,5000));
262        this->bHasTargetPosition_ = true;
263    }
264
265    void SpaceShipAI::searchNewTarget()
266    {
267        this->targetPosition_ = this->getPosition();
268        this->forgetTarget();
269
270        for (ObjectList<SpaceShip>::iterator it = ObjectList<SpaceShip>::begin(); it; ++it)
271        {
272            if (it->getTeamNr() != this->getTeamNr())
273            {
274                float speed = this->getVelocity().length();
275                Vector3 distanceCurrent = this->targetPosition_ - this->getPosition();
276                Vector3 distanceNew = it->getPosition() - this->getPosition();
277                if (!this->target_ || it->getPosition().squaredDistance(this->getPosition()) * (1.5f + acos((this->getOrientation() * Ogre::Vector3::UNIT_X).dotProduct(distanceNew) / speed / distanceNew.length()) / (2 * Ogre::Math::PI))
278                        < this->targetPosition_.squaredDistance(this->getPosition()) * (1.5f + acos((this->getOrientation() * Ogre::Vector3::UNIT_X).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)) + rnd(-250, 250))
279                {
280                    this->target_ = (*it);
281                    this->targetPosition_ = it->getPosition();
282                }
283            }
284        }
285    }
286
287    void SpaceShipAI::forgetTarget()
288    {
289        this->target_ = 0;
290        this->bShooting_ = false;
291    }
292
293    void SpaceShipAI::aimAtTarget()
294    {
295        if (!this->target_)
296            return;
297
298        this->targetPosition_ = getPredictedPosition(this->getPosition(), Projectile::getSpeed(), this->target_->getPosition(), this->target_->getOrientation() * this->target_->getVelocity());
299        this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
300    }
301
302    bool SpaceShipAI::isCloseAtTarget(float distance)
303    {
304        if (!this->target_)
305            return (this->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
306        else
307            return (this->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
308    }
309
310    bool SpaceShipAI::isLookingAtTarget(float angle)
311    {
312        return (getAngle(this->getPosition(), this->getDir(), this->targetPosition_) < angle);
313    }
314
315    void SpaceShipAI::shipDied(SpaceShip* ship)
316    {
317        if (ship == this->target_)
318        {
319            this->forgetTarget();
320            this->searchNewTargetPosition();
321        }
322    }
323}
Note: See TracBrowser for help on using the repository browser.