/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * Dominik Solenicki * */ #include "AIController.h" #include "util/Math.h" #include "core/CoreIncludes.h" #include "core/command/Executor.h" #include "worldentities/ControllableEntity.h" #include "worldentities/pawns/Pawn.h" //Todo: Bot soll pickupspawner besuchen können, falls Pickup vorhanden --modules/weapons/ namespace orxonox { static const float ACTION_INTERVAL = 1.0f; CreateFactory(AIController); AIController::AIController(BaseObject* creator) : ArtificialController(creator) { RegisterObject(AIController); this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); } AIController::~AIController() { } void AIController::action() { float random; float maxrand = 100.0f / ACTION_INTERVAL; if (this->state_ == FREE) { if (this->formationFlight_) { // return to Master after being forced free if (this->freedomCount_ == 1) { this->state_ = SLAVE; this->freedomCount_ = 0; } random = rnd(maxrand); if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) this->searchNewMaster(); } // search enemy random = rnd(maxrand); if (random < (15 + botlevel_* 20) && (!this->target_)) this->searchNewTarget(); // forget enemy random = rnd(maxrand); if (random < ((1-botlevel_)*6) && (this->target_)) this->forgetTarget(); // next enemy random = rnd(maxrand); if (random < (botlevel_*20) && (this->target_)) this->searchNewTarget(); // fly somewhere random = rnd(maxrand); if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) this->searchRandomTargetPosition(); // stop flying random = rnd(maxrand); if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) this->bHasTargetPosition_ = false; // fly somewhere else random = rnd(maxrand); if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) this->searchRandomTargetPosition(); // shoot random = rnd(maxrand); if (!(this->passive_) && random < (75 + botlevel_*25) && (this->target_ && !this->bShooting_)) this->bShooting_ = true; // stop shooting random = rnd(maxrand); if (random < ((1 - botlevel_)*25) && (this->bShooting_)) this->bShooting_ = false; } if (this->state_ == SLAVE) { } if (this->state_ == MASTER) { this->commandSlaves(); if (this->specificMasterAction_ != NONE) this->specificMasterActionHold(); else { // make 180 degree turn - a specific Master Action random = rnd(1000.0f); if (random < 5) this->turn180Init(); // spin around - a specific Master Action random = rnd(1000.0f); if (random < 5) this->spinInit(); // follow a randomly chosen human - a specific Master Action random = rnd(1000.0f); if (random < 1) this->followRandomHumanInit(); // lose master status (only if less than 4 slaves in formation) random = rnd(maxrand); if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) this->loseMasterState(); // look out for outher masters if formation is small random = rnd(maxrand); if(this->slaves_.size() < 3 && random < 20) this->searchNewMaster(); // search enemy random = rnd(maxrand); if (random < (botlevel_)*25 && (!this->target_)) this->searchNewTarget(); // forget enemy random = rnd(maxrand); if (random < (1-botlevel_)*6 && (this->target_)) this->forgetTarget(); // next enemy random = rnd(maxrand); if (random < 10 && (this->target_)) this->searchNewTarget(); // fly somewhere random = rnd(maxrand); if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) this->searchRandomTargetPosition(); // fly somewhere else random = rnd(maxrand); if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) this->searchRandomTargetPosition(); // shoot random = rnd(maxrand); if (!(this->passive_) && random < 25*(botlevel_)+1 && (this->target_ && !this->bShooting_)) { this->bShooting_ = true; this->forceFreeSlaves(); } // stop shooting random = rnd(maxrand); if (random < ( (1- botlevel_)*25 ) && (this->bShooting_)) this->bShooting_ = false; } } } void AIController::tick(float dt) { float random; float maxrand = 100.0f / ACTION_INTERVAL; if (!this->isActive()) return; //Vector-implementation: if(target_.size() == 0) target[0] = DEFAULT; if(this->mode_ == DEFAULT) {//Vector-implementation: mode_.back() == DEFAULT; if (this->state_ == MASTER) { if (this->specificMasterAction_ == NONE) { if (this->target_) { if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ this->forgetTarget(); else { this->aimAtTarget(); random = rnd(maxrand); if(this->botlevel_*100 > random) this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. } } if (this->bHasTargetPosition_) this->moveToTargetPosition(); this->doFire(); } if (this->specificMasterAction_ == TURN180) this->turn180(); if (this->specificMasterAction_ == SPIN) this->spin(); if (this->specificMasterAction_ == FOLLOW) this->follow(); } if (this->state_ == SLAVE) { if (this->bHasTargetPosition_) this->moveToTargetPosition(); } if (this->state_ == FREE) { if (this->target_) { if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ this->forgetTarget(); else { this->aimAtTarget(); random = rnd(maxrand); if(this->botlevel_*100 > random) this->follow();//If a bot is shooting a player, it shouldn't let him go away easily. } } if (this->bHasTargetPosition_) this->moveToTargetPosition(); this->doFire(); } }//END_OF DEFAULT MODE else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant. { //Vector-implementation: mode_.back() == ROCKET; ControllableEntity *controllable = this->getControllableEntity(); if(controllable) { if(controllable->getRocket())//Check wether the bot is controlling the rocket and if the timeout is over. { this->follow(); //TODO: CHECK: does follow make the bot crash into the target_ ? this->timeout_ -= dt; if((timeout_< 0)||(!target_))//Check if the timeout is over or target died. { controllable->fire(0);//kill the rocket this->setPreviousMode();//get out of rocket mode } } else this->setPreviousMode();//no rocket entity -> get out of rocket mode } else this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode }//END_OF ROCKET MODE SUPER(AIController, tick, dt); } }