| 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 |  *      Dominik Solenicki | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 | #include "controllers/CommonController.h" | 
|---|
| 29 |  | 
|---|
| 30 | namespace orxonox | 
|---|
| 31 | { | 
|---|
| 32 |  | 
|---|
| 33 |     RegisterClass(CommonController); | 
|---|
| 34 |     static const float SPEED = 0.6f; | 
|---|
| 35 |     static const float ROTATEFACTOR = 0.2f; | 
|---|
| 36 |  | 
|---|
| 37 |     CommonController::CommonController(Context* context) : Controller(context) | 
|---|
| 38 |     { | 
|---|
| 39 |  | 
|---|
| 40 |         RegisterObject(CommonController); | 
|---|
| 41 |     } | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 |     CommonController::~CommonController() | 
|---|
| 45 |     { | 
|---|
| 46 |     } | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 |     bool CommonController::setWingman (CommonController* wingman) | 
|---|
| 52 |     { | 
|---|
| 53 |         return false; | 
|---|
| 54 |     } | 
|---|
| 55 |     | 
|---|
| 56 |     bool CommonController::hasWingman() | 
|---|
| 57 |     { | 
|---|
| 58 |         return true; | 
|---|
| 59 |     } | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 |     void CommonController::setTargetPosition(const Vector3& target) | 
|---|
| 65 |     { | 
|---|
| 66 |         this->targetPosition_ = target; | 
|---|
| 67 |         this->bHasTargetPosition_ = true; | 
|---|
| 68 |     } | 
|---|
| 69 |  | 
|---|
| 70 |     void CommonController::setTargetOrientation(const Quaternion& orient) | 
|---|
| 71 |     { | 
|---|
| 72 |         this->targetOrientation_=orient; | 
|---|
| 73 |         this->bHasTargetOrientation_=true; | 
|---|
| 74 |     } | 
|---|
| 75 |  | 
|---|
| 76 |     void CommonController::setTargetOrientation(ControllableEntity* target) | 
|---|
| 77 |     { | 
|---|
| 78 |         if (target) | 
|---|
| 79 |             setTargetOrientation(target->getOrientation()); | 
|---|
| 80 |     } | 
|---|
| 81 |  | 
|---|
| 82 |     /*void CommonController::spin() | 
|---|
| 83 |     { | 
|---|
| 84 |         this->moveToTargetPosition(); | 
|---|
| 85 |         this->getControllableEntity()->rotateRoll(8.0f); | 
|---|
| 86 |     } | 
|---|
| 87 |     void CommonController::turn180() | 
|---|
| 88 |     { | 
|---|
| 89 |         Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); | 
|---|
| 90 |  | 
|---|
| 91 |         this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); | 
|---|
| 92 |         this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); | 
|---|
| 93 |  | 
|---|
| 94 |         this->getControllableEntity()->moveFrontBack(SPEED); | 
|---|
| 95 |     }*/ | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 |     //copy the Roll orientation of given Quaternion. | 
|---|
| 100 |     void CommonController::copyOrientation(const Quaternion& orient) | 
|---|
| 101 |     { | 
|---|
| 102 |         //roll angle difference in radian | 
|---|
| 103 |         float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians()); | 
|---|
| 104 |         while(diff>math::twoPi) diff-=math::twoPi; | 
|---|
| 105 |         while(diff<-math::twoPi) diff+=math::twoPi; | 
|---|
| 106 |         this->getControllableEntity()->rotateRoll(-diff); | 
|---|
| 107 |     } | 
|---|
| 108 |     void CommonController::copyTargetOrientation() | 
|---|
| 109 |     { | 
|---|
| 110 |         if (bHasTargetOrientation_) | 
|---|
| 111 |         {    | 
|---|
| 112 |             copyOrientation(targetOrientation_); | 
|---|
| 113 |         } | 
|---|
| 114 |     } | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 |     void CommonController::moveToTargetPosition() | 
|---|
| 120 |     { | 
|---|
| 121 |         this->moveToPosition(this->targetPosition_); | 
|---|
| 122 |     } | 
|---|
| 123 |     void CommonController::moveToPosition(const Vector3& target) | 
|---|
| 124 |     { | 
|---|
| 125 |         if (!this->getControllableEntity()) | 
|---|
| 126 |             return; | 
|---|
| 127 |          | 
|---|
| 128 |         //100 is (so far) the smallest tolerance (empirically found) that can be reached,  | 
|---|
| 129 |         //with smaller distance spaceships can't reach position and go circles around it instead | 
|---|
| 130 |         int tolerance = 100; | 
|---|
| 131 |  | 
|---|
| 132 |         ControllableEntity* entity = this->getControllableEntity(); | 
|---|
| 133 |         Vector2 coord = get2DViewCoordinates | 
|---|
| 134 |             (entity->getPosition(),  | 
|---|
| 135 |             entity->getOrientation() * WorldEntity::FRONT,  | 
|---|
| 136 |             entity->getOrientation() * WorldEntity::UP,  | 
|---|
| 137 |             target); | 
|---|
| 138 |  | 
|---|
| 139 |         float distance = (target - this->getControllableEntity()->getPosition()).length(); | 
|---|
| 140 |  | 
|---|
| 141 |         //rotates should be in range [-1,+1], clamp cuts off all that is not | 
|---|
| 142 |         float rotateX = clamp(coord.x * 10, -1.0f, 1.0f); | 
|---|
| 143 |         float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); | 
|---|
| 144 |  | 
|---|
| 145 |          | 
|---|
| 146 |         if (distance > tolerance) | 
|---|
| 147 |         { | 
|---|
| 148 |             //Yaw and Pitch are enough to start facing the target | 
|---|
| 149 |             this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX); | 
|---|
| 150 |             this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY); | 
|---|
| 151 |  | 
|---|
| 152 |             //300 works, maybe less is better | 
|---|
| 153 |             if (distance < 300) | 
|---|
| 154 |             { | 
|---|
| 155 |                 //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory | 
|---|
| 156 |                 //It's important that roll is not changed in the process of changing yaw and pitch | 
|---|
| 157 |                 //Wingmen won't face same direction as Leaders, but when Leaders start moving | 
|---|
| 158 |                 //Yaw and Pitch will adapt. | 
|---|
| 159 |                 if (bHasTargetOrientation_) | 
|---|
| 160 |                 { | 
|---|
| 161 |                     copyTargetOrientation(); | 
|---|
| 162 |                 } | 
|---|
| 163 |             } | 
|---|
| 164 |             this->getControllableEntity()->moveFrontBack(1.2f*SPEED); | 
|---|
| 165 |         } | 
|---|
| 166 |         else | 
|---|
| 167 |         {       | 
|---|
| 168 |             bHasTargetPosition_ = false; | 
|---|
| 169 |             bHasTargetOrientation_ = false; | 
|---|
| 170 |         } | 
|---|
| 171 |     } | 
|---|
| 172 |     void CommonController::doFire() | 
|---|
| 173 |     { | 
|---|
| 174 |          if (this->isLookingAtTarget(math::pi / 20.0f)) | 
|---|
| 175 |             this->getControllableEntity()->fire(0); //ai uses lens flare if they're close enough to the target | 
|---|
| 176 |     } | 
|---|
| 177 |     bool CommonController::isLookingAtTarget(float angle) const | 
|---|
| 178 |     { | 
|---|
| 179 |         if (!this->getControllableEntity()) | 
|---|
| 180 |             return false; | 
|---|
| 181 |  | 
|---|
| 182 |         return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); | 
|---|
| 183 |     } | 
|---|
| 184 |  | 
|---|
| 185 |     void CommonController::aimAtTarget() | 
|---|
| 186 |     { | 
|---|
| 187 |         if (!this->target_ || !this->getControllableEntity()) | 
|---|
| 188 |             return; | 
|---|
| 189 |  | 
|---|
| 190 |         static const float hardcoded_projectile_speed = 750; | 
|---|
| 191 |  | 
|---|
| 192 |         Vector3 aimPosition = getPredictedPosition(this->getControllableEntity()->getWorldPosition(),  | 
|---|
| 193 |             hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity()); | 
|---|
| 194 |  | 
|---|
| 195 |         Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); | 
|---|
| 196 |         if (pawn) | 
|---|
| 197 |             pawn->setAimPosition(aimPosition); | 
|---|
| 198 |     } | 
|---|
| 199 |      | 
|---|
| 200 |   | 
|---|
| 201 |  | 
|---|
| 202 | } | 
|---|