| 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 "HumanController.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "core/command/ConsoleCommand.h" | 
|---|
| 33 | #include "worldentities/ControllableEntity.h" | 
|---|
| 34 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 35 | #include "gametypes/Gametype.h" | 
|---|
| 36 | #include "infos/PlayerInfo.h" | 
|---|
| 37 | #include "Radar.h" | 
|---|
| 38 | #include "Scene.h" | 
|---|
| 39 |  | 
|---|
| 40 | namespace orxonox | 
|---|
| 41 | { | 
|---|
| 42 | extern const std::string __CC_fire_name = "fire"; | 
|---|
| 43 | extern const std::string __CC_suicide_name = "suicide"; | 
|---|
| 44 |  | 
|---|
| 45 | SetConsoleCommand("HumanController", "moveFrontBack",          &HumanController::moveFrontBack ).addShortcut().setAsInputCommand(); | 
|---|
| 46 | SetConsoleCommand("HumanController", "moveRightLeft",          &HumanController::moveRightLeft ).addShortcut().setAsInputCommand(); | 
|---|
| 47 | SetConsoleCommand("HumanController", "moveUpDown",             &HumanController::moveUpDown    ).addShortcut().setAsInputCommand(); | 
|---|
| 48 | SetConsoleCommand("HumanController", "rotateYaw",              &HumanController::rotateYaw     ).addShortcut().setAsInputCommand(); | 
|---|
| 49 | SetConsoleCommand("HumanController", "rotatePitch",            &HumanController::rotatePitch   ).addShortcut().setAsInputCommand(); | 
|---|
| 50 | SetConsoleCommand("HumanController", "rotateRoll",             &HumanController::rotateRoll    ).addShortcut().setAsInputCommand(); | 
|---|
| 51 | SetConsoleCommand("HumanController", "toggleFormationFlight",  &HumanController::toggleFormationFlight).addShortcut().keybindMode(KeybindMode::OnPress); | 
|---|
| 52 | SetConsoleCommand("HumanController", "FFChangeMode",           &HumanController::FFChangeMode).addShortcut().keybindMode(KeybindMode::OnPress); | 
|---|
| 53 | SetConsoleCommand("HumanController", __CC_fire_name,           &HumanController::fire          ).addShortcut().keybindMode(KeybindMode::OnHold); | 
|---|
| 54 | SetConsoleCommand("HumanController", "reload",                 &HumanController::reload        ).addShortcut(); | 
|---|
| 55 | SetConsoleCommand("HumanController", "boost",                  &HumanController::boost         ).addShortcut().setAsInputCommand().keybindMode(KeybindMode::OnPressAndRelease); | 
|---|
| 56 | SetConsoleCommand("HumanController", "greet",                  &HumanController::greet         ).addShortcut(); | 
|---|
| 57 | SetConsoleCommand("HumanController", "switchCamera",           &HumanController::switchCamera  ).addShortcut(); | 
|---|
| 58 | SetConsoleCommand("HumanController", "mouseLook",              &HumanController::mouseLook     ).addShortcut(); | 
|---|
| 59 | SetConsoleCommand("HumanController", __CC_suicide_name,        &HumanController::suicide       ).addShortcut(); | 
|---|
| 60 | SetConsoleCommand("HumanController", "toggleGodMode",          &HumanController::toggleGodMode ).addShortcut(); | 
|---|
| 61 | SetConsoleCommand("HumanController", "addBots",                &HumanController::addBots       ).addShortcut().defaultValues(1); | 
|---|
| 62 | SetConsoleCommand("HumanController", "killBots",               &HumanController::killBots      ).addShortcut().defaultValues(0); | 
|---|
| 63 | SetConsoleCommand("HumanController", "cycleNavigationFocus",   &HumanController::cycleNavigationFocus).addShortcut(); | 
|---|
| 64 | SetConsoleCommand("HumanController", "releaseNavigationFocus", &HumanController::releaseNavigationFocus).addShortcut(); | 
|---|
| 65 | SetConsoleCommand("HumanController", "myposition",             &HumanController::myposition    ).addShortcut(); | 
|---|
| 66 |  | 
|---|
| 67 | RegisterUnloadableClass(HumanController); | 
|---|
| 68 |  | 
|---|
| 69 | HumanController* HumanController::localController_s = 0; | 
|---|
| 70 |  | 
|---|
| 71 | HumanController::HumanController(Context* context) : FormationController(context) | 
|---|
| 72 | { | 
|---|
| 73 | RegisterObject(HumanController); | 
|---|
| 74 |  | 
|---|
| 75 | this->controlPaused_ = false; | 
|---|
| 76 | HumanController::localController_s = this; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | HumanController::~HumanController() | 
|---|
| 80 | { | 
|---|
| 81 | if (HumanController::localController_s) | 
|---|
| 82 | { | 
|---|
| 83 | HumanController::localController_s->removeFromFormation(); | 
|---|
| 84 | } | 
|---|
| 85 | HumanController::localController_s = 0; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void HumanController::tick(float dt) | 
|---|
| 89 | { | 
|---|
| 90 | if (GameMode::playsSound() && HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 91 | { | 
|---|
| 92 | Camera* camera = HumanController::localController_s->controllableEntity_->getCamera(); | 
|---|
| 93 | if (!camera) | 
|---|
| 94 | orxout(internal_warning) << "HumanController, Warning: Using a ControllableEntity without Camera" << endl; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | // commandslaves when Master of a formation | 
|---|
| 98 | if (HumanController::localController_s && HumanController::localController_s->state_==MASTER && FormationController::slaves_.size() > 0) | 
|---|
| 99 | { | 
|---|
| 100 | if (HumanController::localController_s->formationMode_ != ATTACK) | 
|---|
| 101 | HumanController::localController_s->commandSlaves(); | 
|---|
| 102 | } | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void HumanController::moveFrontBack(const Vector2& value) | 
|---|
| 106 | { | 
|---|
| 107 | if (HumanController::localController_s) | 
|---|
| 108 | HumanController::localController_s->frontback(value); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | void HumanController::frontback(const Vector2& value) | 
|---|
| 112 | { | 
|---|
| 113 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 114 | HumanController::localController_s->controllableEntity_->moveFrontBack(value); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void HumanController::moveRightLeft(const Vector2& value) | 
|---|
| 118 | { | 
|---|
| 119 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 120 | HumanController::localController_s->controllableEntity_->moveRightLeft(value); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | void HumanController::moveUpDown(const Vector2& value) | 
|---|
| 124 | { | 
|---|
| 125 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 126 | HumanController::localController_s->controllableEntity_->moveUpDown(value); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | void HumanController::yaw(const Vector2& value) | 
|---|
| 130 | { | 
|---|
| 131 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 132 | HumanController::localController_s->controllableEntity_->rotateYaw(value); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void HumanController::pitch(const Vector2& value) | 
|---|
| 136 | { | 
|---|
| 137 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 138 | HumanController::localController_s->controllableEntity_->rotatePitch(value); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void HumanController::rotateYaw(const Vector2& value) | 
|---|
| 142 | { | 
|---|
| 143 | if (HumanController::localController_s) | 
|---|
| 144 | HumanController::localController_s->yaw(value); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | void HumanController::rotatePitch(const Vector2& value) | 
|---|
| 148 | { | 
|---|
| 149 | if (HumanController::localController_s) | 
|---|
| 150 | HumanController::localController_s->pitch(value); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | void HumanController::rotateRoll(const Vector2& value) | 
|---|
| 154 | { | 
|---|
| 155 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 156 | HumanController::localController_s->controllableEntity_->rotateRoll(value); | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | void HumanController::fire(unsigned int firemode) | 
|---|
| 160 | { | 
|---|
| 161 | if (HumanController::localController_s) | 
|---|
| 162 | HumanController::localController_s->doFire(firemode); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | void HumanController::doFire(unsigned int firemode) | 
|---|
| 166 | { | 
|---|
| 167 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 168 | { | 
|---|
| 169 | HumanController::localController_s->controllableEntity_->fire(firemode); | 
|---|
| 170 | //if human fires, set slaves free. See FormationController::forceFreeSlaves() | 
|---|
| 171 | if (HumanController::localController_s->state_==MASTER && HumanController::localController_s->formationMode_ == NORMAL) | 
|---|
| 172 | { | 
|---|
| 173 | HumanController::localController_s->forceFreeSlaves(); | 
|---|
| 174 | } | 
|---|
| 175 | } | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | void HumanController::reload() | 
|---|
| 179 | { | 
|---|
| 180 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 181 | HumanController::localController_s->controllableEntity_->reload(); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | /** | 
|---|
| 185 | @brief | 
|---|
| 186 | Static method, controls boosting. | 
|---|
| 187 | */ | 
|---|
| 188 | /*static*/ void HumanController::boost(const Vector2& value) | 
|---|
| 189 | { | 
|---|
| 190 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 191 | { | 
|---|
| 192 | float abs = value.x; | 
|---|
| 193 | if (abs > 0) | 
|---|
| 194 | HumanController::localController_s->startBoosting(); | 
|---|
| 195 | else | 
|---|
| 196 | HumanController::localController_s->stopBoosting(); | 
|---|
| 197 | } | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | /** | 
|---|
| 201 | @brief | 
|---|
| 202 | Starts the boosting mode. | 
|---|
| 203 | Resets the boosting timeout and ells the ControllableEntity to boost (or not boost anymore). | 
|---|
| 204 | */ | 
|---|
| 205 | void HumanController::startBoosting(void) | 
|---|
| 206 | { | 
|---|
| 207 | if(this->controllableEntity_) | 
|---|
| 208 | this->controllableEntity_->boost(true); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | /** | 
|---|
| 212 | @brief | 
|---|
| 213 | Stops the boosting mode. | 
|---|
| 214 | */ | 
|---|
| 215 | void HumanController::stopBoosting(void) | 
|---|
| 216 | { | 
|---|
| 217 | if(this->controllableEntity_) | 
|---|
| 218 | this->controllableEntity_->boost(false); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | void HumanController::greet() | 
|---|
| 222 | { | 
|---|
| 223 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 224 | HumanController::localController_s->controllableEntity_->greet(); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | void HumanController::switchCamera() | 
|---|
| 228 | { | 
|---|
| 229 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 230 | HumanController::localController_s->controllableEntity_->switchCamera(); | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | void HumanController::mouseLook() | 
|---|
| 234 | { | 
|---|
| 235 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 236 | HumanController::localController_s->controllableEntity_->mouseLook(); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | void HumanController::suicide() | 
|---|
| 240 | { | 
|---|
| 241 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 242 | { | 
|---|
| 243 | Pawn* pawn = orxonox_cast<Pawn*>(HumanController::localController_s->controllableEntity_); | 
|---|
| 244 | if (pawn) | 
|---|
| 245 | pawn->kill(); | 
|---|
| 246 | else if (HumanController::localController_s->player_) | 
|---|
| 247 | HumanController::localController_s->player_->stopControl(); | 
|---|
| 248 | } | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | void HumanController::toggleGodMode() | 
|---|
| 252 | { | 
|---|
| 253 | if (HumanController::localController_s) | 
|---|
| 254 | HumanController::localController_s->setGodMode(!HumanController::localController_s->getGodMode()); | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | void HumanController::myposition() | 
|---|
| 258 | { | 
|---|
| 259 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 260 | { | 
|---|
| 261 | const Vector3& position = HumanController::localController_s->controllableEntity_->getPosition(); | 
|---|
| 262 | const Quaternion& orientation = HumanController::localController_s->controllableEntity_->getOrientation(); | 
|---|
| 263 |  | 
|---|
| 264 | orxout(message) << "position=\"" << position.x << ", " << position.y << ", " << position.z << "\" " | 
|---|
| 265 | << "orientation=\"" << orientation.w << ", " << orientation.x << ", " << orientation.y << ", " << orientation.z << "\"" << endl; | 
|---|
| 266 | } | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | /** | 
|---|
| 270 | @brief | 
|---|
| 271 | toggle the formation. Not usable, if formationflight is disabled generally (formationFlight_) | 
|---|
| 272 | */ | 
|---|
| 273 | void HumanController::toggleFormationFlight() | 
|---|
| 274 | { | 
|---|
| 275 | if (HumanController::localController_s) | 
|---|
| 276 | { | 
|---|
| 277 | if (!HumanController::localController_s->formationFlight_) | 
|---|
| 278 | { | 
|---|
| 279 | return; //dont use when formationFlight is disabled | 
|---|
| 280 | } | 
|---|
| 281 | if (HumanController::localController_s->state_==MASTER) | 
|---|
| 282 | { | 
|---|
| 283 | HumanController::localController_s->loseMasterState(); | 
|---|
| 284 | orxout(message) <<"FormationFlight disabled "<< endl; | 
|---|
| 285 | } else //SLAVE or FREE | 
|---|
| 286 | { | 
|---|
| 287 | HumanController::localController_s->takeLeadOfFormation(); | 
|---|
| 288 | orxout(message) <<"FormationFlight enabled "<< endl; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | /** | 
|---|
| 296 | @brief | 
|---|
| 297 | Switch through the different Modes of formationflight. You must be a master of a formation to use. | 
|---|
| 298 | */ | 
|---|
| 299 | void HumanController::FFChangeMode() | 
|---|
| 300 | { | 
|---|
| 301 | if (HumanController::localController_s && HumanController::localController_s->state_==MASTER) | 
|---|
| 302 | { | 
|---|
| 303 | switch (HumanController::localController_s->getFormationMode()) { | 
|---|
| 304 | case NORMAL: | 
|---|
| 305 | HumanController::localController_s->setFormationMode(DEFEND); | 
|---|
| 306 | orxout(message) <<"Mode: DEFEND "<< endl; | 
|---|
| 307 | break; | 
|---|
| 308 | case DEFEND: | 
|---|
| 309 | HumanController::localController_s->setFormationMode(ATTACK); | 
|---|
| 310 | orxout(message) <<"Mode: ATTACK "<< endl; | 
|---|
| 311 | break; | 
|---|
| 312 | case ATTACK: | 
|---|
| 313 | HumanController::localController_s->setFormationMode(NORMAL); | 
|---|
| 314 | orxout(message) <<"Mode: NORMAL "<< endl; | 
|---|
| 315 | break; | 
|---|
| 316 | } | 
|---|
| 317 | } | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | void HumanController::addBots(unsigned int amount) | 
|---|
| 321 | { | 
|---|
| 322 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype()) | 
|---|
| 323 | HumanController::localController_s->controllableEntity_->getGametype()->addBots(amount); | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | void HumanController::killBots(unsigned int amount) | 
|---|
| 327 | { | 
|---|
| 328 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype()) | 
|---|
| 329 | HumanController::localController_s->controllableEntity_->getGametype()->killBots(amount); | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | Pawn* HumanController::getLocalControllerEntityAsPawn() | 
|---|
| 333 | { | 
|---|
| 334 | if (HumanController::localController_s) | 
|---|
| 335 | return orxonox_cast<Pawn*>(HumanController::localController_s->getControllableEntity()); | 
|---|
| 336 | else | 
|---|
| 337 | return NULL; | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | void HumanController::cycleNavigationFocus() | 
|---|
| 341 | { | 
|---|
| 342 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 343 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->cycleFocus(); | 
|---|
| 344 | } | 
|---|
| 345 |  | 
|---|
| 346 | void HumanController::releaseNavigationFocus() | 
|---|
| 347 | { | 
|---|
| 348 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 349 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->releaseFocus(); | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | void HumanController::pauseControl() | 
|---|
| 353 | { | 
|---|
| 354 | if (HumanController::localController_s) | 
|---|
| 355 | HumanController::localController_s->doPauseControl(); | 
|---|
| 356 | } | 
|---|
| 357 |  | 
|---|
| 358 | void HumanController::resumeControl() | 
|---|
| 359 | { | 
|---|
| 360 | if (HumanController::localController_s) | 
|---|
| 361 | HumanController::localController_s->doResumeControl(); | 
|---|
| 362 | } | 
|---|
| 363 | } | 
|---|