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