Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/objects/SpaceShipAI.cc @ 1929

Last change on this file since 1929 was 1694, checked in by rgrieder, 16 years ago

Added Dedicated game state (name: "dedicated", class: GSDedicated).
It doesn't work at all yet, mainly because of our very poorly designed /objects folder. I tried adding a few "if (Settings::showsGraphics())", but I ran into some serious issues.
I will simply leave this topic up to an eager orxonox developer who wants a dedicated server :D There is A LOT of work to be done…

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