| 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 | * Dominik Solenicki |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "AIController.h" |
|---|
| 30 | |
|---|
| 31 | #include "util/Math.h" |
|---|
| 32 | #include "core/CoreIncludes.h" |
|---|
| 33 | #include "core/command/Executor.h" |
|---|
| 34 | #include "worldentities/ControllableEntity.h" |
|---|
| 35 | #include "worldentities/pawns/Pawn.h" |
|---|
| 36 | |
|---|
| 37 | //Todo: Bot soll pickupspawner besuchen können, falls Pickup vorhanden --modules/weapons/ |
|---|
| 38 | namespace orxonox |
|---|
| 39 | { |
|---|
| 40 | static const float ACTION_INTERVAL = 1.0f; |
|---|
| 41 | |
|---|
| 42 | CreateFactory(AIController); |
|---|
| 43 | |
|---|
| 44 | AIController::AIController(BaseObject* creator) : ArtificialController(creator) |
|---|
| 45 | { |
|---|
| 46 | RegisterObject(AIController); |
|---|
| 47 | |
|---|
| 48 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); |
|---|
| 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 | if (this->state_ == FREE) |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | if (this->formationFlight_) |
|---|
| 64 | { |
|---|
| 65 | // return to Master after being forced free |
|---|
| 66 | if (this->freedomCount_ == 1) |
|---|
| 67 | { |
|---|
| 68 | this->state_ = SLAVE; |
|---|
| 69 | this->freedomCount_ = 0; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | random = rnd(maxrand); |
|---|
| 73 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
|---|
| 74 | this->searchNewMaster(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // search enemy |
|---|
| 78 | random = rnd(maxrand); |
|---|
| 79 | if (random < (15 + botlevel_* 20) && (!this->target_)) |
|---|
| 80 | this->searchNewTarget(); |
|---|
| 81 | |
|---|
| 82 | // forget enemy |
|---|
| 83 | random = rnd(maxrand); |
|---|
| 84 | if (random < ((1-botlevel_)*6) && (this->target_)) |
|---|
| 85 | this->forgetTarget(); |
|---|
| 86 | |
|---|
| 87 | // next enemy |
|---|
| 88 | random = rnd(maxrand); |
|---|
| 89 | if (random < (botlevel_*20) && (this->target_)) |
|---|
| 90 | this->searchNewTarget(); |
|---|
| 91 | |
|---|
| 92 | // fly somewhere |
|---|
| 93 | random = rnd(maxrand); |
|---|
| 94 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
|---|
| 95 | this->searchRandomTargetPosition(); |
|---|
| 96 | |
|---|
| 97 | // stop flying |
|---|
| 98 | random = rnd(maxrand); |
|---|
| 99 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
|---|
| 100 | this->bHasTargetPosition_ = false; |
|---|
| 101 | |
|---|
| 102 | // fly somewhere else |
|---|
| 103 | random = rnd(maxrand); |
|---|
| 104 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
|---|
| 105 | this->searchRandomTargetPosition(); |
|---|
| 106 | |
|---|
| 107 | // shoot |
|---|
| 108 | random = rnd(maxrand); |
|---|
| 109 | if (!(this->passive_) && random < (75 + botlevel_*25) && (this->target_ && !this->bShooting_)) |
|---|
| 110 | this->bShooting_ = true; |
|---|
| 111 | |
|---|
| 112 | // stop shooting |
|---|
| 113 | random = rnd(maxrand); |
|---|
| 114 | if (random < ((1 - botlevel_)*25) && (this->bShooting_)) |
|---|
| 115 | this->bShooting_ = false; |
|---|
| 116 | |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if (this->state_ == SLAVE) |
|---|
| 120 | { |
|---|
| 121 | |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | if (this->state_ == MASTER) |
|---|
| 125 | { |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | this->commandSlaves(); |
|---|
| 129 | |
|---|
| 130 | if (this->specificMasterAction_ != NONE) |
|---|
| 131 | this->specificMasterActionHold(); |
|---|
| 132 | |
|---|
| 133 | else { |
|---|
| 134 | |
|---|
| 135 | // make 180 degree turn - a specific Master Action |
|---|
| 136 | random = rnd(1000.0f); |
|---|
| 137 | if (random < 5) |
|---|
| 138 | this->turn180Init(); |
|---|
| 139 | |
|---|
| 140 | // spin around - a specific Master Action |
|---|
| 141 | random = rnd(1000.0f); |
|---|
| 142 | if (random < 5) |
|---|
| 143 | this->spinInit(); |
|---|
| 144 | |
|---|
| 145 | // follow a randomly chosen human - a specific Master Action |
|---|
| 146 | random = rnd(1000.0f); |
|---|
| 147 | if (random < 1) |
|---|
| 148 | this->followRandomHumanInit(); |
|---|
| 149 | |
|---|
| 150 | // lose master status (only if less than 4 slaves in formation) |
|---|
| 151 | random = rnd(maxrand); |
|---|
| 152 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) |
|---|
| 153 | this->loseMasterState(); |
|---|
| 154 | |
|---|
| 155 | // look out for outher masters if formation is small |
|---|
| 156 | random = rnd(maxrand); |
|---|
| 157 | if(this->slaves_.size() < 3 && random < 20) |
|---|
| 158 | this->searchNewMaster(); |
|---|
| 159 | |
|---|
| 160 | // search enemy |
|---|
| 161 | random = rnd(maxrand); |
|---|
| 162 | if (random < (botlevel_)*25 && (!this->target_)) |
|---|
| 163 | this->searchNewTarget(); |
|---|
| 164 | |
|---|
| 165 | // forget enemy |
|---|
| 166 | random = rnd(maxrand); |
|---|
| 167 | if (random < (1-botlevel_)*6 && (this->target_)) |
|---|
| 168 | this->forgetTarget(); |
|---|
| 169 | |
|---|
| 170 | // next enemy |
|---|
| 171 | random = rnd(maxrand); |
|---|
| 172 | if (random < 10 && (this->target_)) |
|---|
| 173 | this->searchNewTarget(); |
|---|
| 174 | |
|---|
| 175 | // fly somewhere |
|---|
| 176 | random = rnd(maxrand); |
|---|
| 177 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
|---|
| 178 | this->searchRandomTargetPosition(); |
|---|
| 179 | |
|---|
| 180 | // fly somewhere else |
|---|
| 181 | random = rnd(maxrand); |
|---|
| 182 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
|---|
| 183 | this->searchRandomTargetPosition(); |
|---|
| 184 | |
|---|
| 185 | // shoot |
|---|
| 186 | random = rnd(maxrand); |
|---|
| 187 | if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_)) |
|---|
| 188 | { |
|---|
| 189 | this->bShooting_ = true; |
|---|
| 190 | this->forceFreeSlaves(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | // stop shooting |
|---|
| 194 | random = rnd(maxrand); |
|---|
| 195 | if (random < ( (1- botlevel_)*25 ) && (this->bShooting_)) |
|---|
| 196 | this->bShooting_ = false; |
|---|
| 197 | |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | void AIController::tick(float dt) |
|---|
| 204 | { |
|---|
| 205 | float random; |
|---|
| 206 | float maxrand = 100.0f / ACTION_INTERVAL; |
|---|
| 207 | |
|---|
| 208 | if (!this->isActive()) |
|---|
| 209 | return; |
|---|
| 210 | //Vector-implementation: if(target_.size() == 0) target[0] = DEFAULT; |
|---|
| 211 | if(this->mode_ == DEFAULT) |
|---|
| 212 | {//Vector-implementation: mode_.back() == DEFAULT; |
|---|
| 213 | if (this->state_ == MASTER) |
|---|
| 214 | { |
|---|
| 215 | if (this->specificMasterAction_ == NONE) |
|---|
| 216 | { |
|---|
| 217 | if (this->target_) |
|---|
| 218 | { |
|---|
| 219 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
|---|
| 220 | this->forgetTarget(); |
|---|
| 221 | else |
|---|
| 222 | { |
|---|
| 223 | this->aimAtTarget(); |
|---|
| 224 | random = rnd(maxrand); |
|---|
| 225 | if(this->botlevel_*100 > random) |
|---|
| 226 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | if (this->bHasTargetPosition_) |
|---|
| 231 | this->moveToTargetPosition(); |
|---|
| 232 | |
|---|
| 233 | this->doFire(); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | if (this->specificMasterAction_ == TURN180) |
|---|
| 237 | this->turn180(); |
|---|
| 238 | |
|---|
| 239 | if (this->specificMasterAction_ == SPIN) |
|---|
| 240 | this->spin(); |
|---|
| 241 | if (this->specificMasterAction_ == FOLLOW) |
|---|
| 242 | this->follow(); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | if (this->state_ == SLAVE) |
|---|
| 246 | { |
|---|
| 247 | if (this->bHasTargetPosition_) |
|---|
| 248 | this->moveToTargetPosition(); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | if (this->state_ == FREE) |
|---|
| 252 | { |
|---|
| 253 | if (this->target_) |
|---|
| 254 | { |
|---|
| 255 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
|---|
| 256 | this->forgetTarget(); |
|---|
| 257 | else |
|---|
| 258 | { |
|---|
| 259 | this->aimAtTarget(); |
|---|
| 260 | random = rnd(maxrand); |
|---|
| 261 | if(this->botlevel_*100 > random) |
|---|
| 262 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | if (this->bHasTargetPosition_) |
|---|
| 267 | this->moveToTargetPosition(); |
|---|
| 268 | |
|---|
| 269 | this->doFire(); |
|---|
| 270 | } |
|---|
| 271 | }//END_OF DEFAULT MODE |
|---|
| 272 | else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant. |
|---|
| 273 | { //Vector-implementation: mode_.back() == ROCKET; |
|---|
| 274 | ControllableEntity *controllable = this->getControllableEntity(); |
|---|
| 275 | if(controllable) |
|---|
| 276 | { |
|---|
| 277 | if(controllable->getRocket())//Check wether the bot is controlling the rocket and if the timeout is over. |
|---|
| 278 | { |
|---|
| 279 | this->follow(); //TODO: CHECK: does follow make the bot crash into the target_ ? |
|---|
| 280 | this->timeout_ -= dt; |
|---|
| 281 | if((timeout_< 0)||(!target_))//Check if the timeout is over or target died. |
|---|
| 282 | { |
|---|
| 283 | controllable->fire(0);//kill the rocket |
|---|
| 284 | this->setPreviousMode();//get out of rocket mode |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | else |
|---|
| 288 | this->setPreviousMode();//no rocket entity -> get out of rocket mode |
|---|
| 289 | } |
|---|
| 290 | else |
|---|
| 291 | this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode |
|---|
| 292 | }//END_OF ROCKET MODE |
|---|
| 293 | SUPER(AIController, tick, dt); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | } |
|---|
| 297 | |
|---|