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