[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 | { |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void AIController::action() |
---|
| 54 | { |
---|
| 55 | float random; |
---|
| 56 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[2493] | 57 | |
---|
[6850] | 58 | if (this->state_ == FREE) |
---|
[6640] | 59 | { |
---|
[2362] | 60 | |
---|
[6850] | 61 | |
---|
| 62 | // return to Master after being forced free |
---|
| 63 | if (this->freedomCount_ == 1) |
---|
| 64 | { |
---|
| 65 | this->state_ = SLAVE; |
---|
| 66 | this->freedomCount_ = 0; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | random = rnd(maxrand); |
---|
| 70 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
[6640] | 71 | this->searchNewMaster(); |
---|
[2362] | 72 | |
---|
[6850] | 73 | |
---|
| 74 | // search enemy |
---|
| 75 | random = rnd(maxrand); |
---|
| 76 | if (random < 15 && (!this->target_)) |
---|
| 77 | this->searchNewTarget(); |
---|
| 78 | |
---|
| 79 | // forget enemy |
---|
| 80 | random = rnd(maxrand); |
---|
| 81 | if (random < 5 && (this->target_)) |
---|
| 82 | this->forgetTarget(); |
---|
| 83 | |
---|
| 84 | // next enemy |
---|
| 85 | random = rnd(maxrand); |
---|
| 86 | if (random < 10 && (this->target_)) |
---|
| 87 | this->searchNewTarget(); |
---|
| 88 | |
---|
| 89 | // fly somewhere |
---|
| 90 | random = rnd(maxrand); |
---|
| 91 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 92 | this->searchRandomTargetPosition(); |
---|
| 93 | |
---|
| 94 | // stop flying |
---|
| 95 | random = rnd(maxrand); |
---|
| 96 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 97 | this->bHasTargetPosition_ = false; |
---|
| 98 | |
---|
| 99 | // fly somewhere else |
---|
| 100 | random = rnd(maxrand); |
---|
| 101 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 102 | this->searchRandomTargetPosition(); |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | random = rnd(maxrand); |
---|
| 106 | if (random < 75 && (this->target_ && !this->bShooting_)) |
---|
| 107 | this->bShooting_ = true; |
---|
| 108 | |
---|
| 109 | // stop shooting |
---|
| 110 | random = rnd(maxrand); |
---|
| 111 | if (random < 25 && (this->bShooting_)) |
---|
| 112 | this->bShooting_ = false; |
---|
| 113 | |
---|
[6640] | 114 | } |
---|
[2362] | 115 | |
---|
[6850] | 116 | if (this->state_ == SLAVE) |
---|
[6640] | 117 | { |
---|
[6683] | 118 | // this->bShooting_ = true; |
---|
[6640] | 119 | } |
---|
[2362] | 120 | |
---|
[6683] | 121 | if (this->state_ == MASTER)//MASTER |
---|
[6640] | 122 | { |
---|
[6696] | 123 | |
---|
[6850] | 124 | |
---|
[6683] | 125 | this->commandSlaves(); |
---|
[6695] | 126 | |
---|
[6696] | 127 | |
---|
| 128 | // lose master status (only if less than 4 slaves in formation) |
---|
| 129 | random = rnd(maxrand); |
---|
[6850] | 130 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 5 ) |
---|
[6696] | 131 | this->loseMasterState(); |
---|
| 132 | |
---|
| 133 | // look out for outher masters if formation is small |
---|
[6850] | 134 | random = rnd(maxrand); |
---|
| 135 | if(this->slaves_.size() < 3 && random < 20) |
---|
[6696] | 136 | this->searchNewMaster(); |
---|
| 137 | |
---|
[6640] | 138 | // search enemy |
---|
| 139 | random = rnd(maxrand); |
---|
| 140 | if (random < 15 && (!this->target_)) |
---|
| 141 | this->searchNewTarget(); |
---|
[2493] | 142 | |
---|
[6640] | 143 | // forget enemy |
---|
| 144 | random = rnd(maxrand); |
---|
| 145 | if (random < 5 && (this->target_)) |
---|
| 146 | this->forgetTarget(); |
---|
[2362] | 147 | |
---|
[6640] | 148 | // next enemy |
---|
| 149 | random = rnd(maxrand); |
---|
| 150 | if (random < 10 && (this->target_)) |
---|
| 151 | this->searchNewTarget(); |
---|
| 152 | |
---|
| 153 | // fly somewhere |
---|
| 154 | random = rnd(maxrand); |
---|
| 155 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 156 | this->searchRandomTargetPosition(); |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | // fly somewhere else |
---|
| 160 | random = rnd(maxrand); |
---|
| 161 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 162 | this->searchRandomTargetPosition(); |
---|
| 163 | |
---|
| 164 | // shoot |
---|
[6850] | 165 | random = rnd(maxrand); |
---|
| 166 | if (random < 5 && (this->target_ && !this->bShooting_)) |
---|
| 167 | { |
---|
[6640] | 168 | this->bShooting_ = true; |
---|
[6850] | 169 | this->forceFreeSlaves(); |
---|
| 170 | } |
---|
[6640] | 171 | // stop shooting |
---|
| 172 | random = rnd(maxrand); |
---|
| 173 | if (random < 25 && (this->bShooting_)) |
---|
| 174 | this->bShooting_ = false; |
---|
| 175 | } |
---|
[6850] | 176 | |
---|
[2362] | 177 | } |
---|
| 178 | |
---|
| 179 | void AIController::tick(float dt) |
---|
| 180 | { |
---|
| 181 | if (!this->isActive()) |
---|
| 182 | return; |
---|
| 183 | |
---|
[6683] | 184 | if (this->state_ == MASTER) |
---|
[6640] | 185 | { |
---|
| 186 | if (this->target_) |
---|
| 187 | this->aimAtTarget(); |
---|
[2362] | 188 | |
---|
[6695] | 189 | if (this->bHasTargetPosition_) |
---|
[6640] | 190 | this->moveToTargetPosition(); |
---|
[6695] | 191 | |
---|
[6640] | 192 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f)) |
---|
| 193 | this->getControllableEntity()->fire(0); |
---|
| 194 | } |
---|
[2362] | 195 | |
---|
[6695] | 196 | if (this->state_ == SLAVE) |
---|
[6640] | 197 | { |
---|
| 198 | |
---|
[6683] | 199 | if (this->bHasTargetPosition_) |
---|
| 200 | this->moveToTargetPosition(); |
---|
[6640] | 201 | |
---|
[6850] | 202 | } |
---|
[6683] | 203 | |
---|
[6850] | 204 | if (this->state_ == FREE) |
---|
| 205 | { |
---|
| 206 | if (this->target_) |
---|
| 207 | this->aimAtTarget(); |
---|
| 208 | |
---|
| 209 | if (this->bHasTargetPosition_) |
---|
| 210 | this->moveToTargetPosition(); |
---|
| 211 | |
---|
| 212 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f)) |
---|
| 213 | this->getControllableEntity()->fire(0); |
---|
[6640] | 214 | } |
---|
| 215 | |
---|
[2362] | 216 | SUPER(AIController, tick, dt); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | } |
---|