| 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 "OrxonoxStableHeaders.h" |
|---|
| 30 | #include "AIController.h" |
|---|
| 31 | |
|---|
| 32 | #include "core/Core.h" |
|---|
| 33 | #include "core/CoreIncludes.h" |
|---|
| 34 | #include "core/Executor.h" |
|---|
| 35 | #include "objects/worldentities/ControllableEntity.h" |
|---|
| 36 | |
|---|
| 37 | namespace orxonox |
|---|
| 38 | { |
|---|
| 39 | static const float ACTION_INTERVAL = 1.0f; |
|---|
| 40 | |
|---|
| 41 | CreateFactory(AIController); |
|---|
| 42 | |
|---|
| 43 | AIController::AIController(BaseObject* creator) : ArtificialController(creator) |
|---|
| 44 | { |
|---|
| 45 | RegisterObject(AIController); |
|---|
| 46 | |
|---|
| 47 | if (Core::isMaster()) |
|---|
| 48 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, this, createExecutor(createFunctor(&AIController::action))); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | AIController::~AIController() |
|---|
| 52 | { |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | void AIController::action() |
|---|
| 56 | { |
|---|
| 57 | float random; |
|---|
| 58 | float maxrand = 100.0f / ACTION_INTERVAL; |
|---|
| 59 | |
|---|
| 60 | // search enemy |
|---|
| 61 | random = rnd(maxrand); |
|---|
| 62 | if (random < 15 && (!this->target_)) |
|---|
| 63 | this->searchNewTarget(); |
|---|
| 64 | |
|---|
| 65 | // forget enemy |
|---|
| 66 | random = rnd(maxrand); |
|---|
| 67 | if (random < 5 && (this->target_)) |
|---|
| 68 | this->forgetTarget(); |
|---|
| 69 | |
|---|
| 70 | // next enemy |
|---|
| 71 | random = rnd(maxrand); |
|---|
| 72 | if (random < 10 && (this->target_)) |
|---|
| 73 | this->searchNewTarget(); |
|---|
| 74 | |
|---|
| 75 | // fly somewhere |
|---|
| 76 | random = rnd(maxrand); |
|---|
| 77 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
|---|
| 78 | this->searchRandomTargetPosition(); |
|---|
| 79 | |
|---|
| 80 | // stop flying |
|---|
| 81 | random = rnd(maxrand); |
|---|
| 82 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
|---|
| 83 | this->bHasTargetPosition_ = false; |
|---|
| 84 | |
|---|
| 85 | // fly somewhere else |
|---|
| 86 | random = rnd(maxrand); |
|---|
| 87 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
|---|
| 88 | this->searchRandomTargetPosition(); |
|---|
| 89 | |
|---|
| 90 | // shoot |
|---|
| 91 | random = rnd(maxrand); |
|---|
| 92 | if (random < 75 && (this->target_ && !this->bShooting_)) |
|---|
| 93 | this->bShooting_ = true; |
|---|
| 94 | |
|---|
| 95 | // stop shooting |
|---|
| 96 | random = rnd(maxrand); |
|---|
| 97 | if (random < 25 && (this->bShooting_)) |
|---|
| 98 | this->bShooting_ = false; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void AIController::tick(float dt) |
|---|
| 102 | { |
|---|
| 103 | if (!this->isActive()) |
|---|
| 104 | return; |
|---|
| 105 | |
|---|
| 106 | if (this->target_) |
|---|
| 107 | this->aimAtTarget(); |
|---|
| 108 | |
|---|
| 109 | if (this->bHasTargetPosition_) |
|---|
| 110 | this->moveToTargetPosition(dt); |
|---|
| 111 | |
|---|
| 112 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(500) && this->isLookingAtTarget(Ogre::Math::PI / 20.0)) |
|---|
| 113 | this->getControllableEntity()->fire(WeaponMode::fire); |
|---|
| 114 | |
|---|
| 115 | SUPER(AIController, tick, dt); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | } |
|---|