[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: |
---|
[7163] | 25 | * Dominik Solenicki |
---|
[2362] | 26 | * |
---|
| 27 | */ |
---|
[7843] | 28 | |
---|
[2362] | 29 | #include "AIController.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "util/Math.h" |
---|
[2362] | 32 | #include "core/CoreIncludes.h" |
---|
[7284] | 33 | #include "core/command/Executor.h" |
---|
[5735] | 34 | #include "worldentities/ControllableEntity.h" |
---|
[7163] | 35 | #include "worldentities/pawns/Pawn.h" |
---|
[2362] | 36 | |
---|
[8701] | 37 | //Todo: Bot soll pickupspawner besuchen können, falls Pickup vorhanden --modules/weapons/ |
---|
[2362] | 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 | |
---|
[5929] | 48 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); |
---|
[2362] | 49 | } |
---|
| 50 | |
---|
| 51 | AIController::~AIController() |
---|
| 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void AIController::action() |
---|
| 56 | { |
---|
| 57 | float random; |
---|
| 58 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[2493] | 59 | |
---|
[7163] | 60 | if (this->state_ == FREE) |
---|
| 61 | { |
---|
[2362] | 62 | |
---|
[7163] | 63 | if (this->formationFlight_) |
---|
| 64 | { |
---|
| 65 | // return to Master after being forced free |
---|
| 66 | if (this->freedomCount_ == 1) |
---|
| 67 | { |
---|
[8238] | 68 | this->state_ = SLAVE; |
---|
| 69 | this->freedomCount_ = 0; |
---|
[7163] | 70 | } |
---|
[2362] | 71 | |
---|
[7163] | 72 | random = rnd(maxrand); |
---|
| 73 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
| 74 | this->searchNewMaster(); |
---|
| 75 | } |
---|
[2493] | 76 | |
---|
[7163] | 77 | // search enemy |
---|
| 78 | random = rnd(maxrand); |
---|
[7842] | 79 | if (random < (15 + botlevel_* 20) && (!this->target_)) |
---|
[7163] | 80 | this->searchNewTarget(); |
---|
[2362] | 81 | |
---|
[7163] | 82 | // forget enemy |
---|
| 83 | random = rnd(maxrand); |
---|
[8238] | 84 | if (random < ((1-botlevel_)*6) && (this->target_)) |
---|
[7163] | 85 | this->forgetTarget(); |
---|
[2362] | 86 | |
---|
[7163] | 87 | // next enemy |
---|
| 88 | random = rnd(maxrand); |
---|
[8238] | 89 | if (random < (botlevel_*20) && (this->target_)) |
---|
[7163] | 90 | this->searchNewTarget(); |
---|
[2493] | 91 | |
---|
[7163] | 92 | // fly somewhere |
---|
| 93 | random = rnd(maxrand); |
---|
| 94 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 95 | this->searchRandomTargetPosition(); |
---|
[2362] | 96 | |
---|
[7163] | 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); |
---|
[7842] | 109 | if (!(this->passive_) && random < (75 + botlevel_*25) && (this->target_ && !this->bShooting_)) |
---|
[7163] | 110 | this->bShooting_ = true; |
---|
| 111 | |
---|
| 112 | // stop shooting |
---|
| 113 | random = rnd(maxrand); |
---|
[7842] | 114 | if (random < ((1 - botlevel_)*25) && (this->bShooting_)) |
---|
[7163] | 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); |
---|
[7843] | 162 | if (random < (botlevel_)*25 && (!this->target_)) |
---|
[7163] | 163 | this->searchNewTarget(); |
---|
| 164 | |
---|
| 165 | // forget enemy |
---|
| 166 | random = rnd(maxrand); |
---|
[7843] | 167 | if (random < (1-botlevel_)*6 && (this->target_)) |
---|
[7163] | 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); |
---|
[7843] | 187 | if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_)) |
---|
[7163] | 188 | { |
---|
[7843] | 189 | this->bShooting_ = true; |
---|
| 190 | this->forceFreeSlaves(); |
---|
[7163] | 191 | } |
---|
| 192 | |
---|
| 193 | // stop shooting |
---|
| 194 | random = rnd(maxrand); |
---|
[7842] | 195 | if (random < ( (1- botlevel_)*25 ) && (this->bShooting_)) |
---|
[7163] | 196 | this->bShooting_ = false; |
---|
| 197 | |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
[2362] | 201 | } |
---|
| 202 | |
---|
| 203 | void AIController::tick(float dt) |
---|
| 204 | { |
---|
[7843] | 205 | float random; |
---|
| 206 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[8701] | 207 | |
---|
[2362] | 208 | if (!this->isActive()) |
---|
| 209 | return; |
---|
[8720] | 210 | //Vector-implementation: if(target_.size() == 0) target[0] = DEFAULT; |
---|
[8701] | 211 | if(this->mode_ == DEFAULT) |
---|
[8720] | 212 | {//Vector-implementation: mode_.back() == DEFAULT; |
---|
[8701] | 213 | if (this->state_ == MASTER) |
---|
[7163] | 214 | { |
---|
[8701] | 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 | { |
---|
[7163] | 253 | if (this->target_) |
---|
| 254 | { |
---|
[7168] | 255 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
| 256 | this->forgetTarget(); |
---|
[7843] | 257 | else |
---|
| 258 | { |
---|
| 259 | this->aimAtTarget(); |
---|
[8238] | 260 | random = rnd(maxrand); |
---|
| 261 | if(this->botlevel_*100 > random) |
---|
[7843] | 262 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
---|
[8701] | 263 | } |
---|
[7168] | 264 | } |
---|
[2362] | 265 | |
---|
[7163] | 266 | if (this->bHasTargetPosition_) |
---|
| 267 | this->moveToTargetPosition(); |
---|
[2362] | 268 | |
---|
[7832] | 269 | this->doFire(); |
---|
[7163] | 270 | } |
---|
[8701] | 271 | }//END_OF DEFAULT MODE |
---|
[8711] | 272 | else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant. |
---|
[8720] | 273 | { //Vector-implementation: mode_.back() == ROCKET; |
---|
[8701] | 274 | ControllableEntity *controllable = this->getControllableEntity(); |
---|
| 275 | if(controllable) |
---|
[7163] | 276 | { |
---|
[8711] | 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 |
---|
[7163] | 289 | } |
---|
[8701] | 290 | else |
---|
[8711] | 291 | this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode |
---|
[8701] | 292 | }//END_OF ROCKET MODE |
---|
[2362] | 293 | SUPER(AIController, tick, dt); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | } |
---|
[8701] | 297 | |
---|