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