Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/objects/SpaceShipAI.cc @ 1481

Last change on this file since 1481 was 1480, checked in by rgrieder, 16 years ago
  • updated msvc files and OrxonoxStableHeaders.h
File size: 9.6 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 "core/CoreIncludes.h"
35#include "core/Iterator.h"
36#include "core/Executor.h"
37#include "core/ConsoleCommand.h"
38#include "core/XMLPort.h"
39
40#define ACTION_INTERVAL 1.0f
41
42namespace orxonox
43{
44    SetConsoleCommand(SpaceShipAI, createEnemy, true).setDefaultValue(0, 1);
45    SetConsoleCommand(SpaceShipAI, killEnemies, true).setDefaultValue(0, 0);
46
47    CreateFactory(SpaceShipAI);
48
49    SpaceShipAI::SpaceShipAI()
50    {
51        RegisterObject(SpaceShipAI);
52
53        this->alive_ = true;
54        this->setPosition(Vector3(rnd(-1000, 1000), rnd(-1000, 1000), rnd(-1000, 0000)));
55        this->target_ = 0;
56        this->bShooting_ = 0;
57        this->bHasTargetPosition_ = false;
58
59        this->setTeamNr((int)rnd(NUM_AI_TEAMS) % NUM_AI_TEAMS + 1);
60
61        if (NUM_AI_TEAMS > 0)
62            this->teamColours_[1] = ColourValue(1, 0, 0, 1);
63        if (NUM_AI_TEAMS > 1)
64            this->teamColours_[2] = ColourValue(0, 1, 0, 1);
65        if (NUM_AI_TEAMS > 2)
66            this->teamColours_[3] = ColourValue(0, 0, 1, 1);
67
68        for (int i = 4; i <= NUM_AI_TEAMS; ++i)
69            this->teamColours_[i] = ColourValue(rnd(), rnd(), rnd(), 1);
70
71        actionTimer_.setTimer(ACTION_INTERVAL, true, this, createExecutor(createFunctor(&SpaceShipAI::action)));
72    }
73
74    void SpaceShipAI::createEnemy(int num)
75    {
76        for (int i = 0; i < num; ++i)
77        {
78            SpaceShipAI* newenemy = new SpaceShipAI();
79            newenemy->setMesh("assff.mesh");
80//            newenemy->setPosition(0, 0, 0);
81            newenemy->setScale(10);
82            newenemy->setMaxSpeed(500);
83            newenemy->setMaxSideAndBackSpeed(50);
84            newenemy->setMaxRotation(1.0);
85            newenemy->setTransAcc(200);
86            newenemy->setRotAcc(3.0);
87            newenemy->setTransDamp(75);
88            newenemy->setRotDamp(1.0);
89            Element xmlelement;
90            newenemy->create();
91        }
92    }
93
94    void SpaceShipAI::killEnemies(int num)
95    {
96        int i = 0;
97        for (Iterator<SpaceShipAI> it = ObjectList<SpaceShipAI>::begin(); it; )
98        {
99            delete *(it++);
100            ++i;
101            if (num && i >= num)
102                break;
103        }
104    }
105
106    ColourValue SpaceShipAI::getProjectileColour() const
107    {
108        return this->teamColours_[this->getTeamNr()];
109    }
110
111    void SpaceShipAI::action()
112    {
113        float random;
114        float maxrand = 100.0f / ACTION_INTERVAL;
115
116        // search enemy
117        random = rnd(maxrand);
118//std::cout << "search enemy: " << random << std::endl;
119        if (random < 20 && (!this->target_))
120        {
121            this->searchNewTarget();
122        }
123
124        // forget enemy
125        random = rnd(maxrand);
126//std::cout << "forget enemy: " << random << std::endl;
127        if (random < 5 && (this->target_))
128        {
129            this->forgetTarget();
130        }
131
132        // next enemy
133        random = rnd(maxrand);
134//std::cout << "next enemy: " << random << std::endl;
135        if (random < 10 && (this->target_))
136        {
137            this->searchNewTarget();
138        }
139
140        // fly somewhere
141        random = rnd(maxrand);
142//std::cout << "fly somewhere: " << random << std::endl;
143        if (random < 40 && (!this->bHasTargetPosition_ && !this->target_))
144        {
145            this->searchNewTargetPosition();
146        }
147
148        // stop flying
149        random = rnd(maxrand);
150//std::cout << "stop flying: " << random << std::endl;
151        if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
152        {
153            this->bHasTargetPosition_ = false;
154        }
155
156        // fly somewhere else
157        random = rnd(maxrand);
158//std::cout << "fly somewhere else: " << random << std::endl;
159        if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
160        {
161            this->searchNewTargetPosition();
162        }
163
164        // shoot
165        random = rnd(maxrand);
166//std::cout << "shoot: " << random << std::endl;
167        if (random < 75 && (this->target_ && !this->bShooting_))
168        {
169            this->bShooting_ = true;
170        }
171
172        // stop shooting
173        random = rnd(maxrand);
174//std::cout << "stop shooting: " << random << std::endl;
175        if (random < 25 && (this->bShooting_))
176        {
177            this->bShooting_ = false;
178        }
179    }
180
181    void SpaceShipAI::tick(float dt)
182    {
183        if (this->target_)
184            this->aimAtTarget();
185
186        if (this->bHasTargetPosition_)
187            this->moveToTargetPosition(dt);
188
189        if (this->bShooting_ && this->isCloseAtTarget(2000) && this->isLookingAtTarget(Ogre::Math::PI / 10.0f))
190            this->doFire();
191
192        SpaceShip::tick(dt);
193    }
194
195    void SpaceShipAI::moveToTargetPosition(float dt)
196    {
197        static Radian RadianZERO(0);
198
199//        float dotprod = (this->getOrientation() * Ogre::Vector3::UNIT_X).dotProduct(this->targetPosition_ - this->getPosition());
200        Quaternion rotation = (this->getOrientation() * Ogre::Vector3::UNIT_X).getRotationTo(this->targetPosition_ - this->getPosition());
201/*
202std::cout << "scalprod: " << dotprod << std::endl;
203std::cout << "dist: " << this->targetPosition_ - this->getPosition() << std::endl;
204std::cout << "yaw: " << rotation.getYaw().valueRadians() << std::endl;
205std::cout << "pitch: " << rotation.getPitch().valueRadians() << std::endl;
206std::cout << "roll: " << rotation.getRoll().valueRadians() << std::endl;
207*/
208        this->setMoveYaw(-rotation.getRoll().valueRadians());
209        this->setMovePitch(rotation.getYaw().valueRadians());
210
211        if ((this->targetPosition_ - this->getPosition()).length() > 100)
212        {
213            this->setMoveLongitudinal(1);
214        }
215
216    }
217
218    void SpaceShipAI::searchNewTargetPosition()
219    {
220        this->targetPosition_ += Vector3(rnd(-10000,10000), rnd(-10000,10000), rnd(-10000,10000));
221        this->bHasTargetPosition_ = true;
222    }
223
224    void SpaceShipAI::searchNewTarget()
225    {
226        this->targetPosition_ = this->getPosition();
227        this->forgetTarget();
228
229        for (Iterator<SpaceShip> it = ObjectList<SpaceShip>::begin(); it; ++it)
230        {
231            if (it->getTeamNr() != this->getTeamNr())
232            {
233                float speed = this->getVelocity().length();
234                Vector3 distanceCurrent = this->targetPosition_ - this->getPosition();
235                Vector3 distanceNew = it->getPosition() - this->getPosition();
236                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))
237                        < this->targetPosition_.squaredDistance(this->getPosition()) * (1.5f + acos((this->getOrientation() * Ogre::Vector3::UNIT_X).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)))
238                {
239                    this->target_ = (*it);
240                    this->targetPosition_ = it->getPosition();
241                }
242            }
243        }
244   }
245
246    void SpaceShipAI::forgetTarget()
247    {
248        this->target_ = 0;
249        this->bShooting_ = false;
250    }
251
252    void SpaceShipAI::aimAtTarget()
253    {
254        if (!this->target_)
255            return;
256/*
257        Vector3 enemymovement = this->target_->getVelocity();
258        Vector3 distance_normalised = this->target_->getPosition() - this->getPosition();
259        distance_normalised.normalise();
260
261        float scalarprod = enemymovement.dotProduct(distance_normalised);
262        float aimoffset = scalarprod*scalarprod + Projectile::getSpeed() * Projectile::getSpeed() - this->target_->getVelocity().squaredLength();
263        if (aimoffset < 0)
264        {
265            this->bHasTargetPosition_ = false;
266            return;
267        }
268        aimoffset = -scalarprod + sqrt(aimoffset);
269        this->targetPosition_ = enemymovement + distance_normalised * aimoffset;
270        this->bHasTargetPosition_ = true;
271
272        std::cout << "targetpos: " << this->targetPosition_ << std::endl;
273*/
274        this->targetPosition_ = this->target_->getPosition();
275        this->bHasTargetPosition_ = true;
276    }
277
278    bool SpaceShipAI::isCloseAtTarget(float distance)
279    {
280        return (this->getPosition().squaredDistance(this->targetPosition_) < distance*distance);
281    }
282
283    bool SpaceShipAI::isLookingAtTarget(float angle)
284    {
285        return (this->getOrientation() * Ogre::Vector3::UNIT_X).directionEquals(this->targetPosition_ - this->getPosition(), Radian(angle));
286    }
287}
Note: See TracBrowser for help on using the repository browser.