| [10719] | 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" |
|---|
| [10759] | 29 | #include "core/XMLPort.h" |
|---|
| 30 | |
|---|
| [10737] | 31 | #include "weaponsystem/WeaponMode.h" |
|---|
| 32 | #include "weaponsystem/WeaponPack.h" |
|---|
| 33 | #include "weaponsystem/Weapon.h" |
|---|
| 34 | #include "weaponsystem/WeaponSlot.h" |
|---|
| 35 | #include "weaponsystem/WeaponSlot.h" |
|---|
| 36 | #include "worldentities/pawns/SpaceShip.h" |
|---|
| [10719] | 37 | |
|---|
| [10780] | 38 | #include "Scene.h" |
|---|
| 39 | #include <OgreRay.h> |
|---|
| 40 | #include <OgreSceneQuery.h> |
|---|
| 41 | #include <OgreCamera.h> |
|---|
| 42 | #include <OgreSceneManager.h> |
|---|
| [10719] | 43 | namespace orxonox |
|---|
| 44 | { |
|---|
| 45 | |
|---|
| 46 | RegisterClass(CommonController); |
|---|
| [10759] | 47 | float SPEED = 0.7f; |
|---|
| 48 | float ROTATEFACTOR = 0.3f; |
|---|
| [10719] | 49 | |
|---|
| [10731] | 50 | CommonController::CommonController(Context* context) : Controller(context) |
|---|
| [10719] | 51 | { |
|---|
| [10759] | 52 | this->bSetupWorked = false; |
|---|
| [10731] | 53 | |
|---|
| [10780] | 54 | this->targetMask_.exclude(ClassByString("BaseObject")); |
|---|
| 55 | this->targetMask_.include(ClassByString("WorldEntity")); |
|---|
| 56 | this->targetMask_.exclude(ClassByString("Projectile")); |
|---|
| 57 | |
|---|
| [10731] | 58 | RegisterObject(CommonController); |
|---|
| [10719] | 59 | } |
|---|
| [10731] | 60 | |
|---|
| 61 | |
|---|
| 62 | CommonController::~CommonController() |
|---|
| [10719] | 63 | { |
|---|
| [10731] | 64 | } |
|---|
| 65 | |
|---|
| [10759] | 66 | void CommonController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 67 | { |
|---|
| 68 | SUPER(CommonController, XMLPort, xmlelement, mode); |
|---|
| 69 | XMLPortParam(CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode); |
|---|
| [10731] | 70 | |
|---|
| [10759] | 71 | } |
|---|
| 72 | void CommonController::setFormationModeXML(std::string val) |
|---|
| 73 | { |
|---|
| 74 | const std::string valUpper = getUppercase(val); |
|---|
| 75 | FormationMode::Value value; |
|---|
| 76 | if (valUpper == "VEE") |
|---|
| 77 | value = FormationMode::VEE; |
|---|
| 78 | else if (valUpper == "WALL") |
|---|
| 79 | value = FormationMode::WALL; |
|---|
| 80 | else if (valUpper == "FINGER4") |
|---|
| 81 | value = FormationMode::FINGER4; |
|---|
| 82 | else if (valUpper == "DIAMOND") |
|---|
| 83 | value = FormationMode::DIAMOND; |
|---|
| 84 | else |
|---|
| 85 | ThrowException(ParseError, std::string("Attempting to set an unknown FormationMode: '") + val + "'."); |
|---|
| 86 | this->setFormationMode(value); |
|---|
| 87 | |
|---|
| 88 | } |
|---|
| 89 | std::string CommonController::getFormationModeXML() |
|---|
| 90 | { |
|---|
| 91 | switch (this->formationMode_) |
|---|
| 92 | { |
|---|
| 93 | case FormationMode::VEE: |
|---|
| 94 | { |
|---|
| 95 | return "VEE"; |
|---|
| 96 | break; |
|---|
| 97 | } |
|---|
| 98 | case FormationMode::WALL: |
|---|
| 99 | { |
|---|
| 100 | return "WALL"; |
|---|
| 101 | break; |
|---|
| 102 | } |
|---|
| 103 | case FormationMode::FINGER4: |
|---|
| 104 | { |
|---|
| 105 | return "FINGER4"; |
|---|
| 106 | break; |
|---|
| 107 | } |
|---|
| 108 | case FormationMode::DIAMOND: |
|---|
| 109 | { |
|---|
| 110 | return "DIAMOND"; |
|---|
| 111 | break; |
|---|
| 112 | } |
|---|
| 113 | default: |
|---|
| 114 | return "DIAMOND"; |
|---|
| 115 | break; |
|---|
| [10731] | 116 | |
|---|
| [10759] | 117 | } |
|---|
| 118 | } |
|---|
| [10731] | 119 | |
|---|
| 120 | bool CommonController::setWingman (CommonController* wingman) |
|---|
| 121 | { |
|---|
| [10719] | 122 | return false; |
|---|
| 123 | } |
|---|
| [10731] | 124 | |
|---|
| [10722] | 125 | bool CommonController::hasWingman() |
|---|
| 126 | { |
|---|
| 127 | return true; |
|---|
| 128 | } |
|---|
| [10759] | 129 | void CommonController::setTarget(ControllableEntity* target) |
|---|
| 130 | { |
|---|
| 131 | this->target_ = target; |
|---|
| 132 | orxout (internal_error) << " TARGET SET " << endl; |
|---|
| 133 | if (target) |
|---|
| 134 | this->targetPosition_ = target->getPosition(); |
|---|
| 135 | } |
|---|
| [10722] | 136 | |
|---|
| [10731] | 137 | |
|---|
| 138 | |
|---|
| 139 | void CommonController::setTargetPosition(const Vector3& target) |
|---|
| [10719] | 140 | { |
|---|
| [10731] | 141 | this->targetPosition_ = target; |
|---|
| 142 | this->bHasTargetPosition_ = true; |
|---|
| 143 | } |
|---|
| [10719] | 144 | |
|---|
| [10731] | 145 | void CommonController::setTargetOrientation(const Quaternion& orient) |
|---|
| 146 | { |
|---|
| 147 | this->targetOrientation_=orient; |
|---|
| 148 | this->bHasTargetOrientation_=true; |
|---|
| [10719] | 149 | } |
|---|
| 150 | |
|---|
| [10731] | 151 | void CommonController::setTargetOrientation(ControllableEntity* target) |
|---|
| 152 | { |
|---|
| 153 | if (target) |
|---|
| 154 | setTargetOrientation(target->getOrientation()); |
|---|
| 155 | } |
|---|
| [10719] | 156 | |
|---|
| [10731] | 157 | /*void CommonController::spin() |
|---|
| [10719] | 158 | { |
|---|
| [10731] | 159 | this->moveToTargetPosition(); |
|---|
| 160 | this->getControllableEntity()->rotateRoll(8.0f); |
|---|
| [10719] | 161 | } |
|---|
| [10731] | 162 | void CommonController::turn180() |
|---|
| 163 | { |
|---|
| 164 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); |
|---|
| 165 | |
|---|
| 166 | this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); |
|---|
| 167 | this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); |
|---|
| 168 | |
|---|
| 169 | this->getControllableEntity()->moveFrontBack(SPEED); |
|---|
| 170 | }*/ |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| [10729] | 174 | //copy the Roll orientation of given Quaternion. |
|---|
| 175 | void CommonController::copyOrientation(const Quaternion& orient) |
|---|
| 176 | { |
|---|
| 177 | //roll angle difference in radian |
|---|
| 178 | float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians()); |
|---|
| 179 | while(diff>math::twoPi) diff-=math::twoPi; |
|---|
| 180 | while(diff<-math::twoPi) diff+=math::twoPi; |
|---|
| [10759] | 181 | this->getControllableEntity()->rotateRoll(diff*ROTATEFACTOR); |
|---|
| [10729] | 182 | } |
|---|
| 183 | void CommonController::copyTargetOrientation() |
|---|
| 184 | { |
|---|
| 185 | if (bHasTargetOrientation_) |
|---|
| 186 | { |
|---|
| 187 | copyOrientation(targetOrientation_); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| [10731] | 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | void CommonController::moveToTargetPosition() |
|---|
| [10729] | 195 | { |
|---|
| [10731] | 196 | this->moveToPosition(this->targetPosition_); |
|---|
| [10729] | 197 | } |
|---|
| [10725] | 198 | void CommonController::moveToPosition(const Vector3& target) |
|---|
| 199 | { |
|---|
| [10763] | 200 | float factor = 1; |
|---|
| [10725] | 201 | if (!this->getControllableEntity()) |
|---|
| 202 | return; |
|---|
| [10759] | 203 | if (this->rank_ == Rank::DIVISIONLEADER) |
|---|
| [10763] | 204 | factor = 0.8; |
|---|
| [10759] | 205 | if (this->rank_ == Rank::SECTIONLEADER) |
|---|
| [10763] | 206 | factor = 0.9; |
|---|
| [10729] | 207 | |
|---|
| 208 | //100 is (so far) the smallest tolerance (empirically found) that can be reached, |
|---|
| 209 | //with smaller distance spaceships can't reach position and go circles around it instead |
|---|
| [10759] | 210 | int tolerance = 60; |
|---|
| [10725] | 211 | |
|---|
| [10729] | 212 | ControllableEntity* entity = this->getControllableEntity(); |
|---|
| 213 | Vector2 coord = get2DViewCoordinates |
|---|
| 214 | (entity->getPosition(), |
|---|
| 215 | entity->getOrientation() * WorldEntity::FRONT, |
|---|
| 216 | entity->getOrientation() * WorldEntity::UP, |
|---|
| 217 | target); |
|---|
| 218 | |
|---|
| [10725] | 219 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
|---|
| [10729] | 220 | |
|---|
| 221 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
|---|
| [10725] | 222 | float rotateX = clamp(coord.x * 10, -1.0f, 1.0f); |
|---|
| 223 | float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); |
|---|
| 224 | |
|---|
| 225 | |
|---|
| [10729] | 226 | if (distance > tolerance) |
|---|
| [10725] | 227 | { |
|---|
| [10729] | 228 | //Yaw and Pitch are enough to start facing the target |
|---|
| 229 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX); |
|---|
| 230 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY); |
|---|
| 231 | |
|---|
| 232 | //300 works, maybe less is better |
|---|
| [10759] | 233 | if (distance < 400) |
|---|
| [10729] | 234 | { |
|---|
| 235 | //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory |
|---|
| 236 | //It's important that roll is not changed in the process of changing yaw and pitch |
|---|
| 237 | //Wingmen won't face same direction as Leaders, but when Leaders start moving |
|---|
| 238 | //Yaw and Pitch will adapt. |
|---|
| 239 | if (bHasTargetOrientation_) |
|---|
| 240 | { |
|---|
| 241 | copyTargetOrientation(); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| [10763] | 244 | |
|---|
| 245 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED*factor); |
|---|
| [10725] | 246 | } |
|---|
| [10729] | 247 | else |
|---|
| 248 | { |
|---|
| 249 | bHasTargetPosition_ = false; |
|---|
| 250 | bHasTargetOrientation_ = false; |
|---|
| 251 | } |
|---|
| [10731] | 252 | } |
|---|
| [10737] | 253 | |
|---|
| 254 | bool CommonController::isCloseAtTarget(float distance) const |
|---|
| 255 | { |
|---|
| 256 | if (!this->getControllableEntity()) |
|---|
| 257 | return false; |
|---|
| 258 | |
|---|
| 259 | if (!this->target_) |
|---|
| 260 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
|---|
| 261 | else |
|---|
| 262 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
|---|
| 263 | } |
|---|
| [10780] | 264 | bool CommonController::isLookingAtTarget(float angle) const |
|---|
| [10737] | 265 | { |
|---|
| [10780] | 266 | if (!this->getControllableEntity()) |
|---|
| [10762] | 267 | return false; |
|---|
| 268 | |
|---|
| [10780] | 269 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
|---|
| 270 | } |
|---|
| [10764] | 271 | |
|---|
| [10780] | 272 | bool CommonController::canFire() |
|---|
| 273 | { |
|---|
| 274 | if ( this->bShooting_ && this->isCloseAtTarget(3000) && this->isLookingAtTarget(math::pi / 20.0f) ) |
|---|
| 275 | { |
|---|
| 276 | return true; |
|---|
| 277 | } |
|---|
| 278 | else |
|---|
| 279 | { |
|---|
| [10762] | 280 | return false; |
|---|
| [10780] | 281 | } |
|---|
| [10762] | 282 | |
|---|
| [10780] | 283 | } |
|---|
| 284 | void CommonController::doFire() |
|---|
| 285 | { |
|---|
| 286 | if (!this->target_ || !this->getControllableEntity()) |
|---|
| 287 | return; |
|---|
| 288 | static const float hardcoded_projectile_speed = 750; |
|---|
| [10764] | 289 | |
|---|
| [10780] | 290 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity()); |
|---|
| 291 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
|---|
| [10764] | 292 | |
|---|
| [10763] | 293 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
|---|
| 294 | if (pawn) |
|---|
| [10780] | 295 | pawn->setAimPosition(this->getControllableEntity()->getWorldPosition() + 4000*(this->getControllableEntity()->getOrientation() * WorldEntity::FRONT)); |
|---|
| [10763] | 296 | |
|---|
| [10762] | 297 | this->getControllableEntity()->fire(0); |
|---|
| [10731] | 298 | } |
|---|
| [10762] | 299 | |
|---|
| [10725] | 300 | |
|---|
| [10719] | 301 | } |
|---|