Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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