| 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 "ArtificialController.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "worldentities/ControllableEntity.h" | 
|---|
| 33 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 34 | #include "worldentities/pawns/TeamBaseMatchBase.h" | 
|---|
| 35 | #include "gametypes/TeamDeathmatch.h" | 
|---|
| 36 | #include "controllers/WaypointPatrolController.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace orxonox | 
|---|
| 39 | { | 
|---|
| 40 |     ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) | 
|---|
| 41 |     { | 
|---|
| 42 |         RegisterObject(ArtificialController); | 
|---|
| 43 |  | 
|---|
| 44 |         this->target_ = 0; | 
|---|
| 45 |         this->bShooting_ = false; | 
|---|
| 46 |         this->bHasTargetPosition_ = false; | 
|---|
| 47 |         this->targetPosition_ = Vector3::ZERO; | 
|---|
| 48 |  | 
|---|
| 49 |         this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); | 
|---|
| 50 |     } | 
|---|
| 51 |  | 
|---|
| 52 |     ArtificialController::~ArtificialController() | 
|---|
| 53 |     { | 
|---|
| 54 |     } | 
|---|
| 55 |  | 
|---|
| 56 |     void ArtificialController::moveToPosition(const Vector3& target) | 
|---|
| 57 |     { | 
|---|
| 58 |         if (!this->getControllableEntity()) | 
|---|
| 59 |             return; | 
|---|
| 60 |  | 
|---|
| 61 |         Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); | 
|---|
| 62 |         float distance = (target - this->getControllableEntity()->getPosition()).length(); | 
|---|
| 63 |  | 
|---|
| 64 |         if (this->target_ || distance > 10) | 
|---|
| 65 |         { | 
|---|
| 66 |             // Multiply with 0.8 to make them a bit slower | 
|---|
| 67 |             this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); | 
|---|
| 68 |             this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); | 
|---|
| 69 |         } | 
|---|
| 70 |  | 
|---|
| 71 |         if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) | 
|---|
| 72 |             this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance | 
|---|
| 73 |         else | 
|---|
| 74 |             this->getControllableEntity()->moveFrontBack(0.8f); | 
|---|
| 75 |     } | 
|---|
| 76 |  | 
|---|
| 77 |     void ArtificialController::moveToTargetPosition() | 
|---|
| 78 |     { | 
|---|
| 79 |         this->moveToPosition(this->targetPosition_); | 
|---|
| 80 |     } | 
|---|
| 81 |  | 
|---|
| 82 |     void ArtificialController::setTargetPosition(const Vector3& target) | 
|---|
| 83 |     { | 
|---|
| 84 |         this->targetPosition_ = target; | 
|---|
| 85 |         this->bHasTargetPosition_ = true; | 
|---|
| 86 |     } | 
|---|
| 87 |  | 
|---|
| 88 |     void ArtificialController::searchRandomTargetPosition() | 
|---|
| 89 |     { | 
|---|
| 90 |         this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); | 
|---|
| 91 |         this->bHasTargetPosition_ = true; | 
|---|
| 92 |     } | 
|---|
| 93 |  | 
|---|
| 94 |     void ArtificialController::setTarget(Pawn* target) | 
|---|
| 95 |     { | 
|---|
| 96 |         this->target_ = target; | 
|---|
| 97 |  | 
|---|
| 98 |         if (target) | 
|---|
| 99 |             this->targetPosition_ = target->getPosition(); | 
|---|
| 100 |     } | 
|---|
| 101 |  | 
|---|
| 102 |     void ArtificialController::searchNewTarget() | 
|---|
| 103 |     { | 
|---|
| 104 |         if (!this->getControllableEntity()) | 
|---|
| 105 |             return; | 
|---|
| 106 |  | 
|---|
| 107 |         this->targetPosition_ = this->getControllableEntity()->getPosition(); | 
|---|
| 108 |         this->forgetTarget(); | 
|---|
| 109 |  | 
|---|
| 110 |         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) | 
|---|
| 111 |         { | 
|---|
| 112 |             if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) | 
|---|
| 113 |                 continue; | 
|---|
| 114 |  | 
|---|
| 115 |             /* So AI won't choose invisible Spaceships as target */ | 
|---|
| 116 |             if (!it->getRadarVisibility()) | 
|---|
| 117 |                 continue; | 
|---|
| 118 |  | 
|---|
| 119 |             if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) | 
|---|
| 120 |             { | 
|---|
| 121 |                 float speed = this->getControllableEntity()->getVelocity().length(); | 
|---|
| 122 |                 Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); | 
|---|
| 123 |                 Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); | 
|---|
| 124 |                 if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / (2 * Ogre::Math::PI)) | 
|---|
| 125 |                         < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)) + rnd(-250, 250)) | 
|---|
| 126 |                 { | 
|---|
| 127 |                     this->target_ = (*it); | 
|---|
| 128 |                     this->targetPosition_ = it->getPosition(); | 
|---|
| 129 |                 } | 
|---|
| 130 |             } | 
|---|
| 131 |         } | 
|---|
| 132 |     } | 
|---|
| 133 |  | 
|---|
| 134 |     void ArtificialController::forgetTarget() | 
|---|
| 135 |     { | 
|---|
| 136 |         this->target_ = 0; | 
|---|
| 137 |         this->bShooting_ = false; | 
|---|
| 138 |     } | 
|---|
| 139 |  | 
|---|
| 140 |     void ArtificialController::aimAtTarget() | 
|---|
| 141 |     { | 
|---|
| 142 |         if (!this->target_ || !this->getControllableEntity()) | 
|---|
| 143 |             return; | 
|---|
| 144 |  | 
|---|
| 145 |         static const float hardcoded_projectile_speed = 1250; | 
|---|
| 146 |  | 
|---|
| 147 |         this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); | 
|---|
| 148 |         this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); | 
|---|
| 149 |  | 
|---|
| 150 |         Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity()); | 
|---|
| 151 |         if (pawn) | 
|---|
| 152 |             pawn->setAimPosition(this->targetPosition_); | 
|---|
| 153 |     } | 
|---|
| 154 |  | 
|---|
| 155 |     bool ArtificialController::isCloseAtTarget(float distance) const | 
|---|
| 156 |     { | 
|---|
| 157 |         if (!this->getControllableEntity()) | 
|---|
| 158 |             return false; | 
|---|
| 159 |  | 
|---|
| 160 |         if (!this->target_) | 
|---|
| 161 |             return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); | 
|---|
| 162 |         else | 
|---|
| 163 |             return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); | 
|---|
| 164 |     } | 
|---|
| 165 |  | 
|---|
| 166 |     bool ArtificialController::isLookingAtTarget(float angle) const | 
|---|
| 167 |     { | 
|---|
| 168 |         if (!this->getControllableEntity()) | 
|---|
| 169 |             return false; | 
|---|
| 170 |  | 
|---|
| 171 |         return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); | 
|---|
| 172 |     } | 
|---|
| 173 |  | 
|---|
| 174 |     void ArtificialController::abandonTarget(Pawn* target) | 
|---|
| 175 |     { | 
|---|
| 176 |         if (target == this->target_) | 
|---|
| 177 |             this->targetDied(); | 
|---|
| 178 |     } | 
|---|
| 179 |  | 
|---|
| 180 |     void ArtificialController::targetDied() | 
|---|
| 181 |     { | 
|---|
| 182 |         this->forgetTarget(); | 
|---|
| 183 |         this->searchRandomTargetPosition(); | 
|---|
| 184 |     } | 
|---|
| 185 |  | 
|---|
| 186 |     bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) | 
|---|
| 187 |     { | 
|---|
| 188 |         if (entity1 == entity2) | 
|---|
| 189 |             return true; | 
|---|
| 190 |  | 
|---|
| 191 |         int team1 = -1; | 
|---|
| 192 |         int team2 = -1; | 
|---|
| 193 |  | 
|---|
| 194 |         if (entity1->getXMLController()) | 
|---|
| 195 |         { | 
|---|
| 196 |             WaypointPatrolController* wpc = orxonox_cast<WaypointPatrolController*>(entity1->getXMLController()); | 
|---|
| 197 |             if (wpc) | 
|---|
| 198 |                 team1 = wpc->getTeam(); | 
|---|
| 199 |         } | 
|---|
| 200 |         if (entity2->getXMLController()) | 
|---|
| 201 |         { | 
|---|
| 202 |             WaypointPatrolController* wpc = orxonox_cast<WaypointPatrolController*>(entity2->getXMLController()); | 
|---|
| 203 |             if (wpc) | 
|---|
| 204 |                 team2 = wpc->getTeam(); | 
|---|
| 205 |         } | 
|---|
| 206 |  | 
|---|
| 207 |         TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); | 
|---|
| 208 |         if (tdm) | 
|---|
| 209 |         { | 
|---|
| 210 |             if (entity1->getPlayer()) | 
|---|
| 211 |                 team1 = tdm->getTeam(entity1->getPlayer()); | 
|---|
| 212 |  | 
|---|
| 213 |             if (entity2->getPlayer()) | 
|---|
| 214 |                 team2 = tdm->getTeam(entity2->getPlayer()); | 
|---|
| 215 |         } | 
|---|
| 216 |  | 
|---|
| 217 |         TeamBaseMatchBase* base = 0; | 
|---|
| 218 |         base = orxonox_cast<TeamBaseMatchBase*>(entity1); | 
|---|
| 219 |         if (base) | 
|---|
| 220 |         { | 
|---|
| 221 |             switch (base->getState()) | 
|---|
| 222 |             { | 
|---|
| 223 |                 case BaseState::ControlTeam1: | 
|---|
| 224 |                     team1 = 0; | 
|---|
| 225 |                     break; | 
|---|
| 226 |                 case BaseState::ControlTeam2: | 
|---|
| 227 |                     team1 = 1; | 
|---|
| 228 |                     break; | 
|---|
| 229 |                 case BaseState::Uncontrolled: | 
|---|
| 230 |                 default: | 
|---|
| 231 |                     team1 = -1; | 
|---|
| 232 |             } | 
|---|
| 233 |         } | 
|---|
| 234 |         base = orxonox_cast<TeamBaseMatchBase*>(entity2); | 
|---|
| 235 |         if (base) | 
|---|
| 236 |         { | 
|---|
| 237 |             switch (base->getState()) | 
|---|
| 238 |             { | 
|---|
| 239 |                 case BaseState::ControlTeam1: | 
|---|
| 240 |                     team2 = 0; | 
|---|
| 241 |                     break; | 
|---|
| 242 |                 case BaseState::ControlTeam2: | 
|---|
| 243 |                     team2 = 1; | 
|---|
| 244 |                     break; | 
|---|
| 245 |                 case BaseState::Uncontrolled: | 
|---|
| 246 |                 default: | 
|---|
| 247 |                     team2 = -1; | 
|---|
| 248 |             } | 
|---|
| 249 |         } | 
|---|
| 250 |  | 
|---|
| 251 |         return (team1 == team2 && team1 != -1); | 
|---|
| 252 |     } | 
|---|
| 253 | } | 
|---|