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