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