[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 | |
---|
[8786] | 116 | // boost |
---|
| 117 | random = rnd(maxrand); |
---|
| 118 | if (random < botlevel_*100 ) |
---|
| 119 | this->boostControl(); |
---|
| 120 | |
---|
| 121 | // update Checkpoints |
---|
[8791] | 122 | /*random = rnd(maxrand); |
---|
[8786] | 123 | if (this->defaultWaypoint_ && random > (maxrand-10)) |
---|
| 124 | this->manageWaypoints(); |
---|
[8791] | 125 | else //if(random > maxrand-10) //CHECK USABILITY!!*/ |
---|
| 126 | if (this->waypoints_.size() == 0 ) |
---|
[8786] | 127 | this->manageWaypoints(); |
---|
| 128 | |
---|
[7163] | 129 | } |
---|
| 130 | |
---|
| 131 | if (this->state_ == SLAVE) |
---|
| 132 | { |
---|
| 133 | |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (this->state_ == MASTER) |
---|
| 137 | { |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | this->commandSlaves(); |
---|
| 141 | |
---|
| 142 | if (this->specificMasterAction_ != NONE) |
---|
| 143 | this->specificMasterActionHold(); |
---|
| 144 | |
---|
| 145 | else { |
---|
| 146 | |
---|
| 147 | // make 180 degree turn - a specific Master Action |
---|
| 148 | random = rnd(1000.0f); |
---|
| 149 | if (random < 5) |
---|
| 150 | this->turn180Init(); |
---|
| 151 | |
---|
| 152 | // spin around - a specific Master Action |
---|
| 153 | random = rnd(1000.0f); |
---|
| 154 | if (random < 5) |
---|
| 155 | this->spinInit(); |
---|
| 156 | |
---|
| 157 | // follow a randomly chosen human - a specific Master Action |
---|
| 158 | random = rnd(1000.0f); |
---|
| 159 | if (random < 1) |
---|
| 160 | this->followRandomHumanInit(); |
---|
| 161 | |
---|
| 162 | // lose master status (only if less than 4 slaves in formation) |
---|
| 163 | random = rnd(maxrand); |
---|
| 164 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) |
---|
| 165 | this->loseMasterState(); |
---|
| 166 | |
---|
| 167 | // look out for outher masters if formation is small |
---|
| 168 | random = rnd(maxrand); |
---|
| 169 | if(this->slaves_.size() < 3 && random < 20) |
---|
| 170 | this->searchNewMaster(); |
---|
| 171 | |
---|
| 172 | // search enemy |
---|
| 173 | random = rnd(maxrand); |
---|
[8732] | 174 | if (random < (botlevel_)*25 && (!this->target_)) |
---|
[7163] | 175 | this->searchNewTarget(); |
---|
| 176 | |
---|
| 177 | // forget enemy |
---|
| 178 | random = rnd(maxrand); |
---|
[8732] | 179 | if (random < (1-botlevel_)*6 && (this->target_)) |
---|
[7163] | 180 | this->forgetTarget(); |
---|
| 181 | |
---|
| 182 | // next enemy |
---|
| 183 | random = rnd(maxrand); |
---|
| 184 | if (random < 10 && (this->target_)) |
---|
| 185 | this->searchNewTarget(); |
---|
| 186 | |
---|
| 187 | // fly somewhere |
---|
| 188 | random = rnd(maxrand); |
---|
| 189 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 190 | this->searchRandomTargetPosition(); |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | // fly somewhere else |
---|
| 194 | random = rnd(maxrand); |
---|
| 195 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 196 | this->searchRandomTargetPosition(); |
---|
| 197 | |
---|
| 198 | // shoot |
---|
| 199 | random = rnd(maxrand); |
---|
[8732] | 200 | if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_)) |
---|
[7163] | 201 | { |
---|
[8732] | 202 | this->bShooting_ = true; |
---|
| 203 | this->forceFreeSlaves(); |
---|
[7163] | 204 | } |
---|
| 205 | |
---|
| 206 | // stop shooting |
---|
| 207 | random = rnd(maxrand); |
---|
[8732] | 208 | if (random < ( (1- botlevel_)*25 ) && (this->bShooting_)) |
---|
[7163] | 209 | this->bShooting_ = false; |
---|
| 210 | |
---|
[8786] | 211 | // boost |
---|
[8758] | 212 | random = rnd(maxrand); |
---|
| 213 | if (random < botlevel_*100 ) |
---|
[8786] | 214 | this->boostControl(); |
---|
| 215 | |
---|
| 216 | // update Checkpoints |
---|
[8791] | 217 | /*random = rnd(maxrand); |
---|
[8786] | 218 | if (this->defaultWaypoint_ && random > (maxrand-10)) |
---|
| 219 | this->manageWaypoints(); |
---|
[8791] | 220 | else //if(random > maxrand-10) //CHECK USABILITY!!*/ |
---|
| 221 | if (this->waypoints_.size() == 0 ) |
---|
[8786] | 222 | this->manageWaypoints(); |
---|
[7163] | 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
[2362] | 226 | } |
---|
| 227 | |
---|
| 228 | void AIController::tick(float dt) |
---|
| 229 | { |
---|
[8730] | 230 | if (!this->isActive()) |
---|
| 231 | return; |
---|
[8758] | 232 | |
---|
[8724] | 233 | float random; |
---|
| 234 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[8786] | 235 | ControllableEntity* controllable = this->getControllableEntity(); |
---|
| 236 | |
---|
| 237 | if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target |
---|
[8769] | 238 | { |
---|
[8786] | 239 | if (this->waypoints_.size() > 0 ) //Waypoint functionality. |
---|
| 240 | { |
---|
[8791] | 241 | WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1]; |
---|
| 242 | if(wPoint) |
---|
[8792] | 243 | { |
---|
[8791] | 244 | this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash |
---|
[8792] | 245 | if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_) |
---|
| 246 | this->waypoints_.pop_back(); // if goal is reached, remove it from the list |
---|
| 247 | } |
---|
| 248 | else |
---|
| 249 | this->waypoints_.pop_back(); // remove invalid waypoints |
---|
[8791] | 250 | |
---|
[8786] | 251 | } |
---|
| 252 | else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length() > 200.0f)) |
---|
| 253 | { |
---|
| 254 | this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_ |
---|
| 255 | random = rnd(maxrand); |
---|
| 256 | } |
---|
[8769] | 257 | } |
---|
[8731] | 258 | if(this->mode_ == DEFAULT) |
---|
| 259 | { |
---|
| 260 | if (this->state_ == MASTER) |
---|
| 261 | { |
---|
| 262 | if (this->specificMasterAction_ == NONE) |
---|
| 263 | { |
---|
| 264 | if (this->target_) |
---|
| 265 | { |
---|
| 266 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
| 267 | this->forgetTarget(); |
---|
[8732] | 268 | else |
---|
| 269 | { |
---|
| 270 | this->aimAtTarget(); |
---|
| 271 | random = rnd(maxrand); |
---|
[8766] | 272 | if(this->botlevel_*100 > random && !this->isCloseAtTarget(20)) |
---|
[8758] | 273 | this->follow(); //If a bot is shooting a player, it shouldn't let him go away easily. |
---|
[8732] | 274 | } |
---|
[8731] | 275 | } |
---|
[8724] | 276 | |
---|
[8731] | 277 | if (this->bHasTargetPosition_) |
---|
| 278 | this->moveToTargetPosition(); |
---|
| 279 | |
---|
[8733] | 280 | this->doFire(); |
---|
[8731] | 281 | } |
---|
| 282 | |
---|
| 283 | if (this->specificMasterAction_ == TURN180) |
---|
| 284 | this->turn180(); |
---|
| 285 | |
---|
| 286 | if (this->specificMasterAction_ == SPIN) |
---|
| 287 | this->spin(); |
---|
| 288 | if (this->specificMasterAction_ == FOLLOW) |
---|
| 289 | this->follow(); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | if (this->state_ == SLAVE) |
---|
[7163] | 293 | { |
---|
[8731] | 294 | if (this->bHasTargetPosition_) |
---|
| 295 | this->moveToTargetPosition(); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | if (this->state_ == FREE) |
---|
| 299 | { |
---|
[7163] | 300 | if (this->target_) |
---|
| 301 | { |
---|
[7168] | 302 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
| 303 | this->forgetTarget(); |
---|
[8732] | 304 | else |
---|
| 305 | { |
---|
| 306 | this->aimAtTarget(); |
---|
| 307 | random = rnd(maxrand); |
---|
[8766] | 308 | |
---|
| 309 | if(this->botlevel_*100 > random && !this->isCloseAtTarget(20)) |
---|
[8732] | 310 | this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. |
---|
| 311 | } |
---|
[7168] | 312 | } |
---|
[2362] | 313 | |
---|
[7163] | 314 | if (this->bHasTargetPosition_) |
---|
| 315 | this->moveToTargetPosition(); |
---|
[2362] | 316 | |
---|
[8733] | 317 | this->doFire(); |
---|
[7163] | 318 | } |
---|
[8731] | 319 | }//END_OF DEFAULT MODE |
---|
| 320 | else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant. |
---|
| 321 | { //Vector-implementation: mode_.back() == ROCKET; |
---|
| 322 | if(controllable) |
---|
[7163] | 323 | { |
---|
[8731] | 324 | if(controllable->getRocket())//Check wether the bot is controlling the rocket and if the timeout is over. |
---|
| 325 | { |
---|
[8736] | 326 | this->follow(); |
---|
[8731] | 327 | this->timeout_ -= dt; |
---|
| 328 | if((timeout_< 0)||(!target_))//Check if the timeout is over or target died. |
---|
| 329 | { |
---|
| 330 | controllable->fire(0);//kill the rocket |
---|
| 331 | this->setPreviousMode();//get out of rocket mode |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | else |
---|
| 335 | this->setPreviousMode();//no rocket entity -> get out of rocket mode |
---|
[7163] | 336 | } |
---|
[8731] | 337 | else |
---|
| 338 | this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode |
---|
| 339 | }//END_OF ROCKET MODE |
---|
[7163] | 340 | |
---|
[2362] | 341 | SUPER(AIController, tick, dt); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | } |
---|