| [1495] | 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 |  | 
|---|
 | 42 | namespace 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 |  | 
|---|
 | 72 |     void SpaceShipAI::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 73 |     { | 
|---|
 | 74 |         SpaceShip::XMLPort(xmlelement, mode); | 
|---|
 | 75 |         myShip_=true; | 
|---|
 | 76 |  | 
|---|
 | 77 |         this->actionTimer_.setTimer(ACTION_INTERVAL, true, this, createExecutor(createFunctor(&SpaceShipAI::action))); | 
|---|
 | 78 |     } | 
|---|
 | 79 |  | 
|---|
 | 80 |     void SpaceShipAI::createEnemy(int num) | 
|---|
 | 81 |     { | 
|---|
 | 82 |         for (int i = 0; i < num; ++i) | 
|---|
 | 83 |         { | 
|---|
 | 84 |             SpaceShipAI* newenemy = new SpaceShipAI(); | 
|---|
 | 85 |             newenemy->setMesh("assff.mesh"); | 
|---|
 | 86 | //            newenemy->setPosition(0, 0, 0); | 
|---|
 | 87 |             newenemy->setScale(10); | 
|---|
 | 88 |             newenemy->setMaxSpeed(500); | 
|---|
 | 89 |             newenemy->setMaxSideAndBackSpeed(50); | 
|---|
 | 90 |             newenemy->setMaxRotation(1.0); | 
|---|
 | 91 |             newenemy->setTransAcc(200); | 
|---|
 | 92 |             newenemy->setRotAcc(3.0); | 
|---|
 | 93 |             newenemy->setTransDamp(75); | 
|---|
 | 94 |             newenemy->setRotDamp(1.0); | 
|---|
 | 95 |             Element xmlelement; | 
|---|
 | 96 |             newenemy->XMLPort(xmlelement, XMLPort::LoadObject); | 
|---|
 | 97 |         } | 
|---|
 | 98 |     } | 
|---|
 | 99 |  | 
|---|
 | 100 |     void SpaceShipAI::killEnemies(int num) | 
|---|
 | 101 |     { | 
|---|
 | 102 |         int i = 0; | 
|---|
 | 103 |         for (Iterator<SpaceShipAI> it = ObjectList<SpaceShipAI>::begin(); it; ) | 
|---|
 | 104 |         { | 
|---|
 | 105 |             delete *(it++); | 
|---|
 | 106 |             ++i; | 
|---|
 | 107 |             if (num && i >= num) | 
|---|
 | 108 |                 break; | 
|---|
 | 109 |         } | 
|---|
 | 110 |     } | 
|---|
 | 111 |  | 
|---|
 | 112 |     ColourValue SpaceShipAI::getProjectileColour() const | 
|---|
 | 113 |     { | 
|---|
 | 114 |         return this->teamColours_[this->getTeamNr()]; | 
|---|
 | 115 |     } | 
|---|
 | 116 |  | 
|---|
 | 117 |     void SpaceShipAI::action() | 
|---|
 | 118 |     { | 
|---|
 | 119 |         float random; | 
|---|
 | 120 |         float maxrand = 100.0f / ACTION_INTERVAL; | 
|---|
 | 121 |  | 
|---|
 | 122 |         // search enemy | 
|---|
 | 123 |         random = rnd(maxrand); | 
|---|
 | 124 | //std::cout << "search enemy: " << random << std::endl; | 
|---|
 | 125 |         if (random < 20 && (!this->target_)) | 
|---|
 | 126 |         { | 
|---|
 | 127 |             this->searchNewTarget(); | 
|---|
 | 128 |         } | 
|---|
 | 129 |  | 
|---|
 | 130 |         // forget enemy | 
|---|
 | 131 |         random = rnd(maxrand); | 
|---|
 | 132 | //std::cout << "forget enemy: " << random << std::endl; | 
|---|
 | 133 |         if (random < 5 && (this->target_)) | 
|---|
 | 134 |         { | 
|---|
 | 135 |             this->forgetTarget(); | 
|---|
 | 136 |         } | 
|---|
 | 137 |  | 
|---|
 | 138 |         // next enemy | 
|---|
 | 139 |         random = rnd(maxrand); | 
|---|
 | 140 | //std::cout << "next enemy: " << random << std::endl; | 
|---|
 | 141 |         if (random < 10 && (this->target_)) | 
|---|
 | 142 |         { | 
|---|
 | 143 |             this->searchNewTarget(); | 
|---|
 | 144 |         } | 
|---|
 | 145 |  | 
|---|
 | 146 |         // fly somewhere | 
|---|
 | 147 |         random = rnd(maxrand); | 
|---|
 | 148 | //std::cout << "fly somewhere: " << random << std::endl; | 
|---|
 | 149 |         if (random < 40 && (!this->bHasTargetPosition_ && !this->target_)) | 
|---|
 | 150 |         { | 
|---|
 | 151 |             this->searchNewTargetPosition(); | 
|---|
 | 152 |         } | 
|---|
 | 153 |  | 
|---|
 | 154 |         // stop flying | 
|---|
 | 155 |         random = rnd(maxrand); | 
|---|
 | 156 | //std::cout << "stop flying: " << random << std::endl; | 
|---|
 | 157 |         if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) | 
|---|
 | 158 |         { | 
|---|
 | 159 |             this->bHasTargetPosition_ = false; | 
|---|
 | 160 |         } | 
|---|
 | 161 |  | 
|---|
 | 162 |         // fly somewhere else | 
|---|
 | 163 |         random = rnd(maxrand); | 
|---|
 | 164 | //std::cout << "fly somewhere else: " << random << std::endl; | 
|---|
 | 165 |         if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) | 
|---|
 | 166 |         { | 
|---|
 | 167 |             this->searchNewTargetPosition(); | 
|---|
 | 168 |         } | 
|---|
 | 169 |  | 
|---|
 | 170 |         // shoot | 
|---|
 | 171 |         random = rnd(maxrand); | 
|---|
 | 172 | //std::cout << "shoot: " << random << std::endl; | 
|---|
 | 173 |         if (random < 75 && (this->target_ && !this->bShooting_)) | 
|---|
 | 174 |         { | 
|---|
 | 175 |             this->bShooting_ = true; | 
|---|
 | 176 |         } | 
|---|
 | 177 |  | 
|---|
 | 178 |         // stop shooting | 
|---|
 | 179 |         random = rnd(maxrand); | 
|---|
 | 180 | //std::cout << "stop shooting: " << random << std::endl; | 
|---|
 | 181 |         if (random < 25 && (this->bShooting_)) | 
|---|
 | 182 |         { | 
|---|
 | 183 |             this->bShooting_ = false; | 
|---|
 | 184 |         } | 
|---|
 | 185 |     } | 
|---|
 | 186 |  | 
|---|
 | 187 |     void SpaceShipAI::tick(float dt) | 
|---|
 | 188 |     { | 
|---|
 | 189 |         if (this->target_) | 
|---|
 | 190 |             this->aimAtTarget(); | 
|---|
 | 191 |  | 
|---|
 | 192 |         if (this->bHasTargetPosition_) | 
|---|
 | 193 |             this->moveToTargetPosition(dt); | 
|---|
 | 194 |  | 
|---|
 | 195 |         if (this->bShooting_ && this->isCloseAtTarget(2000) && this->isLookingAtTarget(Ogre::Math::PI / 10.0f)) | 
|---|
 | 196 |             this->doFire(); | 
|---|
 | 197 |  | 
|---|
 | 198 |         SpaceShip::tick(dt); | 
|---|
 | 199 |     } | 
|---|
 | 200 |  | 
|---|
 | 201 |     void SpaceShipAI::moveToTargetPosition(float dt) | 
|---|
 | 202 |     { | 
|---|
 | 203 |         static Radian RadianZERO(0); | 
|---|
 | 204 |  | 
|---|
 | 205 | //        float dotprod = (this->getOrientation() * Ogre::Vector3::UNIT_X).dotProduct(this->targetPosition_ - this->getPosition()); | 
|---|
 | 206 |         Quaternion rotation = (this->getOrientation() * Ogre::Vector3::UNIT_X).getRotationTo(this->targetPosition_ - this->getPosition()); | 
|---|
 | 207 | /* | 
|---|
 | 208 | std::cout << "scalprod: " << dotprod << std::endl; | 
|---|
 | 209 | std::cout << "dist: " << this->targetPosition_ - this->getPosition() << std::endl; | 
|---|
 | 210 | std::cout << "yaw: " << rotation.getYaw().valueRadians() << std::endl; | 
|---|
 | 211 | std::cout << "pitch: " << rotation.getPitch().valueRadians() << std::endl; | 
|---|
 | 212 | std::cout << "roll: " << rotation.getRoll().valueRadians() << std::endl; | 
|---|
 | 213 | */ | 
|---|
 | 214 |         this->setMoveYaw(-rotation.getRoll().valueRadians()); | 
|---|
 | 215 |         this->setMovePitch(rotation.getYaw().valueRadians()); | 
|---|
 | 216 |  | 
|---|
 | 217 |         if ((this->targetPosition_ - this->getPosition()).length() > 100) | 
|---|
 | 218 |         { | 
|---|
 | 219 |             this->setMoveLongitudinal(1); | 
|---|
 | 220 |         } | 
|---|
 | 221 |  | 
|---|
 | 222 |     } | 
|---|
 | 223 |  | 
|---|
 | 224 |     void SpaceShipAI::searchNewTargetPosition() | 
|---|
 | 225 |     { | 
|---|
 | 226 |         this->targetPosition_ = Vector3(rnd(-5000,5000), rnd(-5000,5000), rnd(-5000,5000)); | 
|---|
 | 227 |         this->bHasTargetPosition_ = true; | 
|---|
 | 228 |     } | 
|---|
 | 229 |  | 
|---|
 | 230 |     void SpaceShipAI::searchNewTarget() | 
|---|
 | 231 |     { | 
|---|
 | 232 |         this->targetPosition_ = this->getPosition(); | 
|---|
 | 233 |         this->forgetTarget(); | 
|---|
 | 234 |  | 
|---|
 | 235 |         for (Iterator<SpaceShip> it = ObjectList<SpaceShip>::begin(); it; ++it) | 
|---|
 | 236 |         { | 
|---|
 | 237 |             if (it->getTeamNr() != this->getTeamNr()) | 
|---|
 | 238 |             { | 
|---|
 | 239 |                 float speed = this->getVelocity().length(); | 
|---|
 | 240 |                 Vector3 distanceCurrent = this->targetPosition_ - this->getPosition(); | 
|---|
 | 241 |                 Vector3 distanceNew = it->getPosition() - this->getPosition(); | 
|---|
 | 242 |                 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)) | 
|---|
 | 243 |                         < this->targetPosition_.squaredDistance(this->getPosition()) * (1.5f + acos((this->getOrientation() * Ogre::Vector3::UNIT_X).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI))) | 
|---|
 | 244 |                 { | 
|---|
 | 245 |                     this->target_ = (*it); | 
|---|
 | 246 |                     this->targetPosition_ = it->getPosition(); | 
|---|
 | 247 |                 } | 
|---|
 | 248 |             } | 
|---|
 | 249 |         } | 
|---|
 | 250 |    } | 
|---|
 | 251 |  | 
|---|
 | 252 |     void SpaceShipAI::forgetTarget() | 
|---|
 | 253 |     { | 
|---|
 | 254 |         this->target_ = 0; | 
|---|
 | 255 |         this->bShooting_ = false; | 
|---|
 | 256 |     } | 
|---|
 | 257 |  | 
|---|
 | 258 |     void SpaceShipAI::aimAtTarget() | 
|---|
 | 259 |     { | 
|---|
 | 260 |         if (!this->target_) | 
|---|
 | 261 |             return; | 
|---|
 | 262 | /* | 
|---|
 | 263 |         Vector3 enemymovement = this->target_->getVelocity(); | 
|---|
 | 264 |         Vector3 distance_normalised = this->target_->getPosition() - this->getPosition(); | 
|---|
 | 265 |         distance_normalised.normalise(); | 
|---|
 | 266 |  | 
|---|
 | 267 |         float scalarprod = enemymovement.dotProduct(distance_normalised); | 
|---|
 | 268 |         float aimoffset = scalarprod*scalarprod + Projectile::getSpeed() * Projectile::getSpeed() - this->target_->getVelocity().squaredLength(); | 
|---|
 | 269 |         if (aimoffset < 0) | 
|---|
 | 270 |         { | 
|---|
 | 271 |             this->bHasTargetPosition_ = false; | 
|---|
 | 272 |             return; | 
|---|
 | 273 |         } | 
|---|
 | 274 |         aimoffset = -scalarprod + sqrt(aimoffset); | 
|---|
 | 275 |         this->targetPosition_ = enemymovement + distance_normalised * aimoffset; | 
|---|
 | 276 |         this->bHasTargetPosition_ = true; | 
|---|
 | 277 |  | 
|---|
 | 278 |         std::cout << "targetpos: " << this->targetPosition_ << std::endl; | 
|---|
 | 279 | */ | 
|---|
 | 280 |         this->targetPosition_ = this->target_->getPosition(); | 
|---|
 | 281 |         this->bHasTargetPosition_ = true; | 
|---|
 | 282 |     } | 
|---|
 | 283 |  | 
|---|
 | 284 |     bool SpaceShipAI::isCloseAtTarget(float distance) | 
|---|
 | 285 |     { | 
|---|
 | 286 |         return (this->getPosition().squaredDistance(this->targetPosition_) < distance*distance); | 
|---|
 | 287 |     } | 
|---|
 | 288 |  | 
|---|
 | 289 |     bool SpaceShipAI::isLookingAtTarget(float angle) | 
|---|
 | 290 |     { | 
|---|
 | 291 |         return (this->getOrientation() * Ogre::Vector3::UNIT_X).directionEquals(this->targetPosition_ - this->getPosition(), Radian(angle)); | 
|---|
 | 292 |     } | 
|---|
 | 293 | } | 
|---|