[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: |
---|
[6978] | 25 | * Dominik Solenicki |
---|
[2362] | 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 | |
---|
[6919] | 61 | if (this->formationFlight_) |
---|
| 62 | { |
---|
[6978] | 63 | // return to Master after being forced free |
---|
[6919] | 64 | if (this->freedomCount_ == 1) |
---|
| 65 | { |
---|
[6850] | 66 | this->state_ = SLAVE; |
---|
| 67 | this->freedomCount_ = 0; |
---|
[6919] | 68 | } |
---|
| 69 | |
---|
| 70 | random = rnd(maxrand); |
---|
| 71 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
| 72 | this->searchNewMaster(); |
---|
[6850] | 73 | } |
---|
| 74 | |
---|
| 75 | // search enemy |
---|
| 76 | random = rnd(maxrand); |
---|
| 77 | if (random < 15 && (!this->target_)) |
---|
| 78 | this->searchNewTarget(); |
---|
| 79 | |
---|
| 80 | // forget enemy |
---|
| 81 | random = rnd(maxrand); |
---|
| 82 | if (random < 5 && (this->target_)) |
---|
| 83 | this->forgetTarget(); |
---|
| 84 | |
---|
| 85 | // next enemy |
---|
| 86 | random = rnd(maxrand); |
---|
| 87 | if (random < 10 && (this->target_)) |
---|
| 88 | this->searchNewTarget(); |
---|
| 89 | |
---|
| 90 | // fly somewhere |
---|
| 91 | random = rnd(maxrand); |
---|
| 92 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 93 | this->searchRandomTargetPosition(); |
---|
| 94 | |
---|
| 95 | // stop flying |
---|
| 96 | random = rnd(maxrand); |
---|
| 97 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 98 | this->bHasTargetPosition_ = false; |
---|
| 99 | |
---|
| 100 | // fly somewhere else |
---|
| 101 | random = rnd(maxrand); |
---|
| 102 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 103 | this->searchRandomTargetPosition(); |
---|
| 104 | |
---|
[6978] | 105 | // shoot |
---|
[6850] | 106 | random = rnd(maxrand); |
---|
[6978] | 107 | if (!(this->passive_) && random < 75 && (this->target_ && !this->bShooting_)) |
---|
[6850] | 108 | this->bShooting_ = true; |
---|
| 109 | |
---|
| 110 | // stop shooting |
---|
| 111 | random = rnd(maxrand); |
---|
| 112 | if (random < 25 && (this->bShooting_)) |
---|
[6978] | 113 | this->bShooting_ = false; |
---|
[6850] | 114 | |
---|
[6640] | 115 | } |
---|
[2362] | 116 | |
---|
[6850] | 117 | if (this->state_ == SLAVE) |
---|
[6640] | 118 | { |
---|
[6919] | 119 | |
---|
[6640] | 120 | } |
---|
[2362] | 121 | |
---|
[6978] | 122 | if (this->state_ == MASTER) |
---|
[6640] | 123 | { |
---|
[6696] | 124 | |
---|
[6850] | 125 | |
---|
[6683] | 126 | this->commandSlaves(); |
---|
[6695] | 127 | |
---|
[6919] | 128 | if (this->specificMasterAction_ != NONE) |
---|
| 129 | { |
---|
| 130 | this->specificMasterActionHold(); |
---|
[6696] | 131 | |
---|
[6997] | 132 | // if (this->specificMasterAction_ == TURN180) |
---|
| 133 | // this->turn180Init(); |
---|
[6696] | 134 | |
---|
[6997] | 135 | // if (this->specificMasterAction_ == SPIN) |
---|
| 136 | // this->spinInit(); |
---|
[6978] | 137 | |
---|
| 138 | // if (this->specificMasterAction_ == FOLLOWHUMAN) |
---|
| 139 | // this->followHuman(this->HumanToFollow_, false); |
---|
[6919] | 140 | } |
---|
[6696] | 141 | |
---|
[6919] | 142 | else { |
---|
[2493] | 143 | |
---|
[6919] | 144 | // make 180 degree turn - a specific Master Action |
---|
[6997] | 145 | random = rnd(1000.0f); |
---|
[6919] | 146 | if (random < 5) |
---|
[6997] | 147 | this->turn180Init(); |
---|
[2362] | 148 | |
---|
[6919] | 149 | // spin around - a specific Master Action |
---|
[6997] | 150 | random = rnd(1000.0f); |
---|
[6919] | 151 | if (random < 5) |
---|
[6997] | 152 | this->spinInit(); |
---|
[6640] | 153 | |
---|
[6919] | 154 | // lose master status (only if less than 4 slaves in formation) |
---|
| 155 | random = rnd(maxrand); |
---|
| 156 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) |
---|
| 157 | this->loseMasterState(); |
---|
[6640] | 158 | |
---|
[6919] | 159 | // look out for outher masters if formation is small |
---|
| 160 | random = rnd(maxrand); |
---|
| 161 | if(this->slaves_.size() < 3 && random < 20) |
---|
| 162 | this->searchNewMaster(); |
---|
[6640] | 163 | |
---|
[6919] | 164 | // search enemy |
---|
| 165 | random = rnd(maxrand); |
---|
| 166 | if (random < 15 && (!this->target_)) |
---|
| 167 | this->searchNewTarget(); |
---|
[6640] | 168 | |
---|
[6919] | 169 | // forget enemy |
---|
| 170 | random = rnd(maxrand); |
---|
| 171 | if (random < 5 && (this->target_)) |
---|
| 172 | this->forgetTarget(); |
---|
| 173 | |
---|
| 174 | // next enemy |
---|
| 175 | random = rnd(maxrand); |
---|
| 176 | if (random < 10 && (this->target_)) |
---|
| 177 | this->searchNewTarget(); |
---|
| 178 | |
---|
| 179 | // fly somewhere |
---|
| 180 | random = rnd(maxrand); |
---|
| 181 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 182 | this->searchRandomTargetPosition(); |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | // fly somewhere else |
---|
| 186 | random = rnd(maxrand); |
---|
| 187 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 188 | this->searchRandomTargetPosition(); |
---|
| 189 | |
---|
| 190 | // shoot |
---|
| 191 | random = rnd(maxrand); |
---|
[6997] | 192 | if (!(this->passive_) && random < 9 && (this->target_ && !this->bShooting_)) |
---|
[6919] | 193 | { |
---|
[6640] | 194 | this->bShooting_ = true; |
---|
[6919] | 195 | this->forceFreeSlaves(); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | // stop shooting |
---|
| 199 | random = rnd(maxrand); |
---|
| 200 | if (random < 25 && (this->bShooting_)) |
---|
| 201 | this->bShooting_ = false; |
---|
| 202 | |
---|
[6850] | 203 | } |
---|
[6640] | 204 | } |
---|
[6850] | 205 | |
---|
[2362] | 206 | } |
---|
| 207 | |
---|
| 208 | void AIController::tick(float dt) |
---|
| 209 | { |
---|
| 210 | if (!this->isActive()) |
---|
| 211 | return; |
---|
| 212 | |
---|
[6683] | 213 | if (this->state_ == MASTER) |
---|
[6640] | 214 | { |
---|
[6919] | 215 | if (this->specificMasterAction_ == NONE) |
---|
| 216 | { |
---|
[6997] | 217 | if (this->target_) |
---|
| 218 | this->aimAtTarget(); |
---|
| 219 | |
---|
| 220 | if (this->bHasTargetPosition_) |
---|
| 221 | this->moveToTargetPosition(); |
---|
| 222 | |
---|
[6919] | 223 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f)) |
---|
| 224 | this->getControllableEntity()->fire(0); |
---|
| 225 | } |
---|
[6997] | 226 | |
---|
| 227 | if (this->specificMasterAction_ == TURN180) |
---|
| 228 | this->turn180(); |
---|
| 229 | |
---|
| 230 | if (this->specificMasterAction_ == SPIN) |
---|
| 231 | this->spin(); |
---|
[7029] | 232 | if (this->specificMasterAction_ == FOLLOWHUMAN) |
---|
| 233 | this->follow(); |
---|
[6640] | 234 | } |
---|
[2362] | 235 | |
---|
[6695] | 236 | if (this->state_ == SLAVE) |
---|
[6640] | 237 | { |
---|
| 238 | |
---|
[6683] | 239 | if (this->bHasTargetPosition_) |
---|
| 240 | this->moveToTargetPosition(); |
---|
[6640] | 241 | |
---|
[6850] | 242 | } |
---|
[6683] | 243 | |
---|
[6850] | 244 | if (this->state_ == FREE) |
---|
| 245 | { |
---|
| 246 | if (this->target_) |
---|
| 247 | this->aimAtTarget(); |
---|
| 248 | |
---|
| 249 | if (this->bHasTargetPosition_) |
---|
| 250 | this->moveToTargetPosition(); |
---|
| 251 | |
---|
| 252 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f)) |
---|
| 253 | this->getControllableEntity()->fire(0); |
---|
[6640] | 254 | } |
---|
| 255 | |
---|
[2362] | 256 | SUPER(AIController, tick, dt); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | } |
---|