[2362] | 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 "AIController.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "util/Math.h" |
---|
[2362] | 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/Executor.h" |
---|
[5735] | 34 | #include "worldentities/ControllableEntity.h" |
---|
[2362] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
| 38 | static const float ACTION_INTERVAL = 1.0f; |
---|
| 39 | |
---|
| 40 | CreateFactory(AIController); |
---|
| 41 | |
---|
| 42 | AIController::AIController(BaseObject* creator) : ArtificialController(creator) |
---|
| 43 | { |
---|
| 44 | RegisterObject(AIController); |
---|
| 45 | |
---|
[5929] | 46 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); |
---|
[2362] | 47 | } |
---|
| 48 | |
---|
| 49 | AIController::~AIController() |
---|
| 50 | { |
---|
[6795] | 51 | COUT(0) << "~AIController 1" << std::endl; |
---|
| 52 | if (this->state_ == MASTER) setNewMasterWithinFormation(); |
---|
| 53 | COUT(0) << "~AIController 2" << std::endl; |
---|
[6695] | 54 | if (this->state_ == SLAVE) unregisterSlave(); |
---|
[6795] | 55 | COUT(0) << "~AIController 3" << std::endl; |
---|
| 56 | this->slaves_.clear(); |
---|
| 57 | COUT(0) << "~AIController 4" << std::endl; |
---|
[2362] | 58 | } |
---|
| 59 | |
---|
| 60 | void AIController::action() |
---|
| 61 | { |
---|
| 62 | float random; |
---|
| 63 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[2493] | 64 | |
---|
[6683] | 65 | if (this->state_ == FREE)//FREE |
---|
[6640] | 66 | { |
---|
[2362] | 67 | |
---|
[6695] | 68 | //this->state_ = MASTER; |
---|
[6640] | 69 | // search master |
---|
[6695] | 70 | //random = rnd(maxrand); |
---|
| 71 | //if (random < 101 && (!this->target_)) |
---|
[6640] | 72 | this->searchNewMaster(); |
---|
[2362] | 73 | |
---|
[6640] | 74 | } |
---|
[2362] | 75 | |
---|
[6683] | 76 | if (this->state_ == SLAVE)//SLAVE |
---|
[6640] | 77 | { |
---|
[6683] | 78 | // this->bShooting_ = true; |
---|
[6640] | 79 | } |
---|
[2362] | 80 | |
---|
[6683] | 81 | if (this->state_ == MASTER)//MASTER |
---|
[6640] | 82 | { |
---|
[6696] | 83 | |
---|
[6683] | 84 | // command slaves |
---|
| 85 | this->commandSlaves(); |
---|
[6695] | 86 | |
---|
[6696] | 87 | |
---|
| 88 | // lose master status (only if less than 4 slaves in formation) |
---|
| 89 | random = rnd(maxrand); |
---|
[6795] | 90 | if(random < 5/(this->slaves_.size()+1) && this->slaves_.size() < 5 ) |
---|
[6696] | 91 | this->loseMasterState(); |
---|
| 92 | |
---|
| 93 | // look out for outher masters if formation is small |
---|
[6795] | 94 | if(this->slaves_.size() < 3) |
---|
[6696] | 95 | this->searchNewMaster(); |
---|
| 96 | |
---|
[6640] | 97 | // search enemy |
---|
| 98 | random = rnd(maxrand); |
---|
| 99 | if (random < 15 && (!this->target_)) |
---|
| 100 | this->searchNewTarget(); |
---|
[2493] | 101 | |
---|
[6640] | 102 | // forget enemy |
---|
| 103 | random = rnd(maxrand); |
---|
| 104 | if (random < 5 && (this->target_)) |
---|
| 105 | this->forgetTarget(); |
---|
[2362] | 106 | |
---|
[6640] | 107 | // next enemy |
---|
| 108 | random = rnd(maxrand); |
---|
| 109 | if (random < 10 && (this->target_)) |
---|
| 110 | this->searchNewTarget(); |
---|
| 111 | |
---|
| 112 | // fly somewhere |
---|
| 113 | random = rnd(maxrand); |
---|
| 114 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 115 | this->searchRandomTargetPosition(); |
---|
| 116 | |
---|
| 117 | // stop flying |
---|
| 118 | random = rnd(maxrand); |
---|
| 119 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 120 | this->bHasTargetPosition_ = false; |
---|
| 121 | |
---|
| 122 | // fly somewhere else |
---|
| 123 | random = rnd(maxrand); |
---|
| 124 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 125 | this->searchRandomTargetPosition(); |
---|
| 126 | |
---|
| 127 | // shoot |
---|
[6683] | 128 | /*random = rnd(maxrand); |
---|
[6640] | 129 | if (random < 75 && (this->target_ && !this->bShooting_)) |
---|
| 130 | this->bShooting_ = true; |
---|
[6683] | 131 | */ |
---|
[6640] | 132 | // stop shooting |
---|
| 133 | random = rnd(maxrand); |
---|
| 134 | if (random < 25 && (this->bShooting_)) |
---|
| 135 | this->bShooting_ = false; |
---|
| 136 | } |
---|
[2362] | 137 | } |
---|
| 138 | |
---|
| 139 | void AIController::tick(float dt) |
---|
| 140 | { |
---|
| 141 | if (!this->isActive()) |
---|
| 142 | return; |
---|
| 143 | |
---|
[6683] | 144 | if (this->state_ == MASTER) |
---|
[6640] | 145 | { |
---|
| 146 | if (this->target_) |
---|
| 147 | this->aimAtTarget(); |
---|
[2362] | 148 | |
---|
[6695] | 149 | if (this->bHasTargetPosition_) |
---|
[6640] | 150 | this->moveToTargetPosition(); |
---|
[6695] | 151 | |
---|
[6640] | 152 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f)) |
---|
| 153 | this->getControllableEntity()->fire(0); |
---|
| 154 | } |
---|
[2362] | 155 | |
---|
[6695] | 156 | if (this->state_ == SLAVE) |
---|
[6640] | 157 | { |
---|
| 158 | |
---|
[6683] | 159 | if (this->bHasTargetPosition_) |
---|
| 160 | this->moveToTargetPosition(); |
---|
[6640] | 161 | |
---|
[6683] | 162 | //this->getControllableEntity()->fire(0); |
---|
| 163 | |
---|
[6640] | 164 | } |
---|
| 165 | |
---|
[2362] | 166 | SUPER(AIController, tick, dt); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | } |
---|