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