[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 | { |
---|
[8729] | 39 | const float AIController::ACTION_INTERVAL = 1.0f; |
---|
[2362] | 40 | |
---|
[9667] | 41 | RegisterClass(AIController); |
---|
[2362] | 42 | |
---|
[9667] | 43 | AIController::AIController(Context* context) : ArtificialController(context) |
---|
[2362] | 44 | { |
---|
| 45 | RegisterObject(AIController); |
---|
[10717] | 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 | { |
---|
[9016] | 61 | |
---|
[7163] | 62 | if (this->formationFlight_) |
---|
| 63 | { |
---|
[9016] | 64 | |
---|
| 65 | //changed order -> searchNewMaster MUSTN'T be called in SLAVE-state (bugfix for internal-error messages at quit) |
---|
| 66 | random = rnd(maxrand); |
---|
| 67 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
| 68 | this->searchNewMaster(); |
---|
| 69 | |
---|
[7163] | 70 | // return to Master after being forced free |
---|
[10717] | 71 | if (this->freedomCount_ == 1) |
---|
[7163] | 72 | { |
---|
[8891] | 73 | this->state_ = SLAVE; |
---|
[10717] | 74 | this->freedomCount_ = 0; |
---|
[7163] | 75 | } |
---|
| 76 | } |
---|
[10717] | 77 | |
---|
[9016] | 78 | this->defaultBehaviour(maxrand); |
---|
| 79 | |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if (this->state_ == SLAVE && this->formationMode_ == ATTACK) |
---|
| 83 | { |
---|
[7163] | 84 | // search enemy |
---|
[10717] | 85 | random = rnd(maxrand); |
---|
| 86 | if (random < (botlevel_*100) && (!this->target_)) |
---|
[7163] | 87 | this->searchNewTarget(); |
---|
[2362] | 88 | |
---|
[10717] | 89 | // next enemy |
---|
| 90 | random = rnd(maxrand); |
---|
| 91 | if (random < (botlevel_*30) && (this->target_)) |
---|
| 92 | this->searchNewTarget(); |
---|
| 93 | |
---|
[7163] | 94 | // shoot |
---|
[10717] | 95 | random = rnd(maxrand); |
---|
| 96 | if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_)) |
---|
[7163] | 97 | this->bShooting_ = true; |
---|
| 98 | |
---|
| 99 | // stop shooting |
---|
[10717] | 100 | random = rnd(maxrand); |
---|
| 101 | if (random < (1-botlevel_)*50 && (this->bShooting_)) |
---|
[7163] | 102 | this->bShooting_ = false; |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if (this->state_ == MASTER) |
---|
| 107 | { |
---|
[10717] | 108 | this->commandSlaves(); |
---|
[10652] | 109 | |
---|
[7163] | 110 | if (this->specificMasterAction_ != NONE) |
---|
| 111 | this->specificMasterActionHold(); |
---|
| 112 | |
---|
| 113 | else { |
---|
| 114 | |
---|
| 115 | // make 180 degree turn - a specific Master Action |
---|
| 116 | random = rnd(1000.0f); |
---|
| 117 | if (random < 5) |
---|
| 118 | this->turn180Init(); |
---|
| 119 | |
---|
| 120 | // spin around - a specific Master Action |
---|
| 121 | random = rnd(1000.0f); |
---|
| 122 | if (random < 5) |
---|
| 123 | this->spinInit(); |
---|
| 124 | |
---|
[9016] | 125 | /*// follow a randomly chosen human - a specific Master Action |
---|
[7163] | 126 | random = rnd(1000.0f); |
---|
| 127 | if (random < 1) |
---|
| 128 | this->followRandomHumanInit(); |
---|
[9016] | 129 | */ |
---|
[7163] | 130 | // lose master status (only if less than 4 slaves in formation) |
---|
| 131 | random = rnd(maxrand); |
---|
| 132 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) |
---|
| 133 | this->loseMasterState(); |
---|
[10717] | 134 | |
---|
[7163] | 135 | // look out for outher masters if formation is small |
---|
| 136 | random = rnd(maxrand); |
---|
| 137 | if(this->slaves_.size() < 3 && random < 20) |
---|
| 138 | this->searchNewMaster(); |
---|
| 139 | |
---|
[9016] | 140 | this->defaultBehaviour(maxrand); |
---|
[7163] | 141 | |
---|
| 142 | } |
---|
| 143 | } |
---|
[10717] | 144 | |
---|
[2362] | 145 | } |
---|
| 146 | |
---|
| 147 | void AIController::tick(float dt) |
---|
| 148 | { |
---|
| 149 | if (!this->isActive()) |
---|
| 150 | return; |
---|
[10717] | 151 | |
---|
[8891] | 152 | float random; |
---|
| 153 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
| 154 | ControllableEntity* controllable = this->getControllableEntity(); |
---|
[9016] | 155 | //DOES: Either move to the waypoint or search for a Point of interest |
---|
[8891] | 156 | if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target |
---|
[7163] | 157 | { |
---|
[8891] | 158 | if (this->waypoints_.size() > 0 ) //Waypoint functionality. |
---|
[7163] | 159 | { |
---|
[8891] | 160 | WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1]; |
---|
| 161 | if(wPoint) |
---|
[7163] | 162 | { |
---|
[8891] | 163 | this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash |
---|
| 164 | if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_) |
---|
| 165 | this->waypoints_.pop_back(); // if goal is reached, remove it from the list |
---|
[7168] | 166 | } |
---|
[8891] | 167 | else |
---|
| 168 | this->waypoints_.pop_back(); // remove invalid waypoints |
---|
[2362] | 169 | |
---|
[7163] | 170 | } |
---|
[8891] | 171 | else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length() > 200.0f)) |
---|
| 172 | { |
---|
| 173 | this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_ |
---|
| 174 | random = rnd(maxrand); |
---|
| 175 | } |
---|
| 176 | } |
---|
[9016] | 177 | |
---|
| 178 | if (this->mode_ == DEFAULT) |
---|
| 179 | { |
---|
[8891] | 180 | if (this->state_ == MASTER) |
---|
| 181 | { |
---|
| 182 | if (this->specificMasterAction_ == NONE) |
---|
| 183 | { |
---|
| 184 | if (this->target_) |
---|
| 185 | { |
---|
| 186 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
| 187 | this->forgetTarget(); |
---|
| 188 | else |
---|
| 189 | { |
---|
| 190 | this->aimAtTarget(); |
---|
[10717] | 191 | random = rnd(maxrand); |
---|
| 192 | if(this->botlevel_*70 > random && !this->isCloseAtTarget(100)) |
---|
| 193 | this->follow(); //If a bot is shooting a player, it shouldn't let him go away easily. |
---|
[8891] | 194 | } |
---|
| 195 | } |
---|
[2362] | 196 | |
---|
[8891] | 197 | if (this->bHasTargetPosition_) |
---|
| 198 | this->moveToTargetPosition(); |
---|
| 199 | this->doFire(); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | if (this->specificMasterAction_ == TURN180) |
---|
[7163] | 203 | this->turn180(); |
---|
| 204 | |
---|
[8891] | 205 | if (this->specificMasterAction_ == SPIN) |
---|
[7163] | 206 | this->spin(); |
---|
[8891] | 207 | if (this->specificMasterAction_ == FOLLOW) |
---|
[7163] | 208 | this->follow(); |
---|
[8891] | 209 | } |
---|
[7163] | 210 | |
---|
[9016] | 211 | if (this->state_ == SLAVE && this->formationMode_ != ATTACK) |
---|
[8891] | 212 | { |
---|
| 213 | if (this->bHasTargetPosition_) |
---|
| 214 | this->moveToTargetPosition(); |
---|
| 215 | } |
---|
[7163] | 216 | |
---|
[9016] | 217 | if (this->state_ == FREE || (this->state_==SLAVE && this->formationMode_ == ATTACK) ) |
---|
[8891] | 218 | { |
---|
| 219 | if (this->target_) |
---|
| 220 | { |
---|
| 221 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
| 222 | this->forgetTarget(); |
---|
[9016] | 223 | else this->aimAtTarget(); |
---|
[8891] | 224 | } |
---|
[7163] | 225 | |
---|
[8891] | 226 | if (this->bHasTargetPosition_) |
---|
| 227 | this->moveToTargetPosition(); |
---|
| 228 | |
---|
[9016] | 229 | this->doFire(); |
---|
[8891] | 230 | } |
---|
[9016] | 231 | } |
---|
[8891] | 232 | else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant. |
---|
| 233 | { //Vector-implementation: mode_.back() == ROCKET; |
---|
| 234 | if(controllable) |
---|
[9016] | 235 | {//Check wether the bot is controlling the rocket and if the timeout is over. |
---|
| 236 | if(controllable->getIdentifier() == ClassByString("Rocket")) |
---|
| 237 | |
---|
[8891] | 238 | { |
---|
| 239 | this->follow(); |
---|
| 240 | this->timeout_ -= dt; |
---|
| 241 | if((timeout_< 0)||(!target_))//Check if the timeout is over or target died. |
---|
| 242 | { |
---|
| 243 | controllable->fire(0);//kill the rocket |
---|
| 244 | this->setPreviousMode();//get out of rocket mode |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | else |
---|
| 248 | this->setPreviousMode();//no rocket entity -> get out of rocket mode |
---|
[7163] | 249 | } |
---|
[8891] | 250 | else |
---|
| 251 | this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode |
---|
| 252 | }//END_OF ROCKET MODE |
---|
[7163] | 253 | |
---|
[2362] | 254 | SUPER(AIController, tick, dt); |
---|
| 255 | } |
---|
[9016] | 256 | //**********************************************NEW |
---|
| 257 | void AIController::defaultBehaviour(float maxrand) |
---|
[10717] | 258 | { float random; |
---|
| 259 | // search enemy |
---|
| 260 | random = rnd(maxrand); |
---|
| 261 | if (random < (botlevel_* 100) && (!this->target_)) |
---|
| 262 | this->searchNewTarget(); |
---|
| 263 | |
---|
| 264 | // forget enemy |
---|
| 265 | random = rnd(maxrand); |
---|
| 266 | if (random < ((1-botlevel_)*20) && (this->target_)) |
---|
| 267 | this->forgetTarget(); |
---|
| 268 | |
---|
| 269 | // next enemy |
---|
| 270 | random = rnd(maxrand); |
---|
| 271 | if (random < (botlevel_*30) && (this->target_)) |
---|
| 272 | this->searchNewTarget(); |
---|
| 273 | |
---|
| 274 | // fly somewhere |
---|
| 275 | random = rnd(maxrand); |
---|
| 276 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
| 277 | this->searchRandomTargetPosition(); |
---|
| 278 | |
---|
| 279 | // stop flying |
---|
| 280 | random = rnd(maxrand); |
---|
| 281 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 282 | this->bHasTargetPosition_ = false; |
---|
| 283 | |
---|
| 284 | // fly somewhere else |
---|
| 285 | random = rnd(maxrand); |
---|
| 286 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
| 287 | this->searchRandomTargetPosition(); |
---|
| 288 | |
---|
| 289 | if (this->state_ == MASTER) // master: shoot |
---|
| 290 | { |
---|
| 291 | random = rnd(maxrand); |
---|
| 292 | if (!(this->passive_) && random < (100*botlevel_) && (this->target_ && !this->bShooting_)) |
---|
| 293 | { |
---|
| 294 | this->bShooting_ = true; |
---|
| 295 | this->forceFreeSlaves(); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | else |
---|
| 299 | { |
---|
| 300 | // shoot |
---|
| 301 | random = rnd(maxrand); |
---|
| 302 | if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_)) |
---|
| 303 | this->bShooting_ = true; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | // stop shooting |
---|
| 307 | random = rnd(maxrand); |
---|
| 308 | if (random < ((1 - botlevel_)*50) && (this->bShooting_)) |
---|
| 309 | this->bShooting_ = false; |
---|
| 310 | |
---|
| 311 | // boost |
---|
| 312 | random = rnd(maxrand); |
---|
| 313 | if (random < botlevel_*50 ) |
---|
| 314 | this->boostControl(); |
---|
| 315 | |
---|
| 316 | // update Checkpoints |
---|
| 317 | /*random = rnd(maxrand); |
---|
| 318 | if (this->defaultWaypoint_ && random > (maxrand-10)) |
---|
| 319 | this->manageWaypoints(); |
---|
| 320 | else //if(random > maxrand-10) //CHECK USABILITY!!*/ |
---|
| 321 | if (this->waypoints_.size() == 0 ) |
---|
| 322 | this->manageWaypoints(); |
---|
[9016] | 323 | } |
---|
| 324 | |
---|
[10717] | 325 | } |
---|