| 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 | const std::string __CC_boost_name = "boost"; | 
|---|
| 45 |  | 
|---|
| 46 | SetConsoleCommand("HumanController", "moveFrontBack",          &HumanController::moveFrontBack ).addShortcut().setAsInputCommand(); | 
|---|
| 47 | SetConsoleCommand("HumanController", "moveRightLeft",          &HumanController::moveRightLeft ).addShortcut().setAsInputCommand(); | 
|---|
| 48 | SetConsoleCommand("HumanController", "moveUpDown",             &HumanController::moveUpDown    ).addShortcut().setAsInputCommand(); | 
|---|
| 49 | SetConsoleCommand("HumanController", "rotateYaw",              &HumanController::rotateYaw     ).addShortcut().setAsInputCommand(); | 
|---|
| 50 | SetConsoleCommand("HumanController", "rotatePitch",            &HumanController::rotatePitch   ).addShortcut().setAsInputCommand(); | 
|---|
| 51 | SetConsoleCommand("HumanController", "rotateRoll",             &HumanController::rotateRoll    ).addShortcut().setAsInputCommand(); | 
|---|
| 52 | SetConsoleCommand("HumanController", __CC_fire_name,           &HumanController::fire          ).addShortcut().keybindMode(KeybindMode::OnHold); | 
|---|
| 53 | SetConsoleCommand("HumanController", "reload",                 &HumanController::reload        ).addShortcut(); | 
|---|
| 54 | SetConsoleCommand("HumanController", __CC_boost_name,          &HumanController::keepBoost     ).addShortcut().keybindMode(KeybindMode::OnHold); | 
|---|
| 55 | SetConsoleCommand("HumanController", "greet",                  &HumanController::greet         ).addShortcut(); | 
|---|
| 56 | SetConsoleCommand("HumanController", "switchCamera",           &HumanController::switchCamera  ).addShortcut(); | 
|---|
| 57 | SetConsoleCommand("HumanController", "mouseLook",              &HumanController::mouseLook     ).addShortcut(); | 
|---|
| 58 | SetConsoleCommand("HumanController", __CC_suicide_name,        &HumanController::suicide       ).addShortcut(); | 
|---|
| 59 | SetConsoleCommand("HumanController", "toggleGodMode",          &HumanController::toggleGodMode ).addShortcut(); | 
|---|
| 60 | SetConsoleCommand("HumanController", "addBots",                &HumanController::addBots       ).addShortcut().defaultValues(1); | 
|---|
| 61 | SetConsoleCommand("HumanController", "killBots",               &HumanController::killBots      ).addShortcut().defaultValues(0); | 
|---|
| 62 | SetConsoleCommand("HumanController", "cycleNavigationFocus",   &HumanController::cycleNavigationFocus).addShortcut(); | 
|---|
| 63 | SetConsoleCommand("HumanController", "releaseNavigationFocus", &HumanController::releaseNavigationFocus).addShortcut(); | 
|---|
| 64 | SetConsoleCommand("HumanController", "myposition",             &HumanController::myposition    ).addShortcut(); | 
|---|
| 65 |  | 
|---|
| 66 | CreateUnloadableFactory(HumanController); | 
|---|
| 67 |  | 
|---|
| 68 | HumanController* HumanController::localController_s = 0; | 
|---|
| 69 | /*static*/ const float HumanController::BOOSTING_TIME = 0.1f; | 
|---|
| 70 |  | 
|---|
| 71 | HumanController::HumanController(BaseObject* creator) : Controller(creator) | 
|---|
| 72 | { | 
|---|
| 73 | RegisterObject(HumanController); | 
|---|
| 74 |  | 
|---|
| 75 | this->controlPaused_ = false; | 
|---|
| 76 | this->boosting_ = false; | 
|---|
| 77 |  | 
|---|
| 78 | HumanController::localController_s = this; | 
|---|
| 79 | this->boostingTimeout_.setTimer(HumanController::BOOSTING_TIME, false, createExecutor(createFunctor(&HumanController::terminateBoosting, this))); | 
|---|
| 80 | this->boostingTimeout_.stopTimer(); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | HumanController::~HumanController() | 
|---|
| 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 | COUT(3) << "HumanController, Warning: Using a ControllableEntity without Camera" << std::endl; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void HumanController::moveFrontBack(const Vector2& value) | 
|---|
| 99 | { | 
|---|
| 100 | if (HumanController::localController_s) | 
|---|
| 101 | HumanController::localController_s->frontback(value); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | void HumanController::frontback(const Vector2& value) | 
|---|
| 105 | { | 
|---|
| 106 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 107 | HumanController::localController_s->controllableEntity_->moveFrontBack(value); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void HumanController::moveRightLeft(const Vector2& value) | 
|---|
| 111 | { | 
|---|
| 112 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 113 | HumanController::localController_s->controllableEntity_->moveRightLeft(value); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void HumanController::moveUpDown(const Vector2& value) | 
|---|
| 117 | { | 
|---|
| 118 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 119 | HumanController::localController_s->controllableEntity_->moveUpDown(value); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void HumanController::yaw(const Vector2& value) | 
|---|
| 123 | { | 
|---|
| 124 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 125 | HumanController::localController_s->controllableEntity_->rotateYaw(value); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void HumanController::pitch(const Vector2& value) | 
|---|
| 129 | { | 
|---|
| 130 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 131 | HumanController::localController_s->controllableEntity_->rotatePitch(value); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | void HumanController::rotateYaw(const Vector2& value) | 
|---|
| 135 | { | 
|---|
| 136 | if (HumanController::localController_s) | 
|---|
| 137 | HumanController::localController_s->yaw(value); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | void HumanController::rotatePitch(const Vector2& value) | 
|---|
| 141 | { | 
|---|
| 142 | if (HumanController::localController_s) | 
|---|
| 143 | HumanController::localController_s->pitch(value); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void HumanController::rotateRoll(const Vector2& value) | 
|---|
| 147 | { | 
|---|
| 148 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 149 | HumanController::localController_s->controllableEntity_->rotateRoll(value); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | void HumanController::fire(unsigned int firemode) | 
|---|
| 153 | { | 
|---|
| 154 | if (HumanController::localController_s) | 
|---|
| 155 | HumanController::localController_s->doFire(firemode); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void HumanController::doFire(unsigned int firemode) | 
|---|
| 159 | { | 
|---|
| 160 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 161 | HumanController::localController_s->controllableEntity_->fire(firemode); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | void HumanController::reload() | 
|---|
| 165 | { | 
|---|
| 166 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 167 | HumanController::localController_s->controllableEntity_->reload(); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | /** | 
|---|
| 171 | @brief | 
|---|
| 172 | Static method,keeps boosting. | 
|---|
| 173 | */ | 
|---|
| 174 | /*static*/ void HumanController::keepBoost() | 
|---|
| 175 | { | 
|---|
| 176 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 177 | HumanController::localController_s->keepBoosting(); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | /** | 
|---|
| 181 | @brief | 
|---|
| 182 | Starts, or keeps the boosting mode. | 
|---|
| 183 | Resets the boosting timeout and ells the ControllableEntity to boost (or not boost anymore). | 
|---|
| 184 | */ | 
|---|
| 185 | void HumanController::keepBoosting(void) | 
|---|
| 186 | { | 
|---|
| 187 | if(this->boostingTimeout_.isActive()) | 
|---|
| 188 | { | 
|---|
| 189 | this->boostingTimeout_.stopTimer(); | 
|---|
| 190 | this->boostingTimeout_.startTimer(); | 
|---|
| 191 | } | 
|---|
| 192 | else | 
|---|
| 193 | { | 
|---|
| 194 | this->boosting_ = true; | 
|---|
| 195 | this->boostingTimeout_.startTimer(); | 
|---|
| 196 |  | 
|---|
| 197 | this->controllableEntity_->boost(this->boosting_); | 
|---|
| 198 | COUT(4) << "Start boosting" << endl; | 
|---|
| 199 | } | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | /** | 
|---|
| 203 | @brief | 
|---|
| 204 | Terminates the boosting mode. | 
|---|
| 205 | */ | 
|---|
| 206 | void HumanController::terminateBoosting(void) | 
|---|
| 207 | { | 
|---|
| 208 | this->boosting_ = false; | 
|---|
| 209 | this->boostingTimeout_.stopTimer(); | 
|---|
| 210 |  | 
|---|
| 211 | this->controllableEntity_->boost(this->boosting_); | 
|---|
| 212 | COUT(4) << "Stop boosting" << endl; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | void HumanController::greet() | 
|---|
| 216 | { | 
|---|
| 217 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 218 | HumanController::localController_s->controllableEntity_->greet(); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | void HumanController::switchCamera() | 
|---|
| 222 | { | 
|---|
| 223 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 224 | HumanController::localController_s->controllableEntity_->switchCamera(); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | void HumanController::mouseLook() | 
|---|
| 228 | { | 
|---|
| 229 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 230 | HumanController::localController_s->controllableEntity_->mouseLook(); | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | void HumanController::suicide() | 
|---|
| 234 | { | 
|---|
| 235 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 236 | { | 
|---|
| 237 | Pawn* pawn = orxonox_cast<Pawn*>(HumanController::localController_s->controllableEntity_); | 
|---|
| 238 | if (pawn) | 
|---|
| 239 | pawn->kill(); | 
|---|
| 240 | else if (HumanController::localController_s->player_) | 
|---|
| 241 | HumanController::localController_s->player_->stopControl(); | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | void HumanController::toggleGodMode() | 
|---|
| 246 | { | 
|---|
| 247 | if (HumanController::localController_s) | 
|---|
| 248 | HumanController::localController_s->setGodMode(!HumanController::localController_s->getGodMode()); | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | void HumanController::myposition() | 
|---|
| 252 | { | 
|---|
| 253 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 254 | { | 
|---|
| 255 | const Vector3& position = HumanController::localController_s->controllableEntity_->getPosition(); | 
|---|
| 256 | const Quaternion& orientation = HumanController::localController_s->controllableEntity_->getOrientation(); | 
|---|
| 257 |  | 
|---|
| 258 | COUT(0) << "position=\"" << position.x << ", " << position.y << ", " << position.z << "\" "; | 
|---|
| 259 | COUT(0) << "orientation=\"" << orientation.w << ", " << orientation.x << ", " << orientation.y << ", " << orientation.z << "\"" << std::endl; | 
|---|
| 260 | } | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | void HumanController::addBots(unsigned int amount) | 
|---|
| 264 | { | 
|---|
| 265 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype()) | 
|---|
| 266 | HumanController::localController_s->controllableEntity_->getGametype()->addBots(amount); | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | void HumanController::killBots(unsigned int amount) | 
|---|
| 270 | { | 
|---|
| 271 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype()) | 
|---|
| 272 | HumanController::localController_s->controllableEntity_->getGametype()->killBots(amount); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | Pawn* HumanController::getLocalControllerEntityAsPawn() | 
|---|
| 276 | { | 
|---|
| 277 | if (HumanController::localController_s) | 
|---|
| 278 | return orxonox_cast<Pawn*>(HumanController::localController_s->getControllableEntity()); | 
|---|
| 279 | else | 
|---|
| 280 | return NULL; | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | void HumanController::cycleNavigationFocus() | 
|---|
| 284 | { | 
|---|
| 285 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 286 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->cycleFocus(); | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | void HumanController::releaseNavigationFocus() | 
|---|
| 290 | { | 
|---|
| 291 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) | 
|---|
| 292 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->releaseFocus(); | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | void HumanController::pauseControl() | 
|---|
| 296 | { | 
|---|
| 297 | if (HumanController::localController_s) | 
|---|
| 298 | HumanController::localController_s->doPauseControl(); | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | void HumanController::resumeControl() | 
|---|
| 302 | { | 
|---|
| 303 | if (HumanController::localController_s) | 
|---|
| 304 | HumanController::localController_s->doResumeControl(); | 
|---|
| 305 | } | 
|---|
| 306 | } | 
|---|