- Timestamp:
- Oct 29, 2015, 11:53:45 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AI_HS15/src/orxonox/controllers/AIController.cc
r10678 r10717 44 44 { 45 45 RegisterObject(AIController); 46 46 47 this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); 47 48 } … … 61 62 if (this->formationFlight_) 62 63 { 63 //When this is a master and was destroyed, destructor might complain that there are slaves of this, although this was removed from formation64 //race conditions?65 //destructor takes care of slaves anyway, so no need to worry about internal_error66 67 64 68 65 //changed order -> searchNewMaster MUSTN'T be called in SLAVE-state (bugfix for internal-error messages at quit) … … 72 69 73 70 // return to Master after being forced free 74 if (this->freedomCount_ == ACTION_INTERVAL)71 if (this->freedomCount_ == 1) 75 72 { 76 73 this->state_ = SLAVE; 77 this->freedomCount_ = 0; // ACTION_INTERVAL is 1 sec, freedomCount is a remaining time of temp. freedom 78 } 79 } else{ 80 //form a formation 81 if (!this->forcedFree()) 82 this->searchNewMaster(); 83 } 74 this->freedomCount_ = 0; 75 } 76 } 77 84 78 this->defaultBehaviour(maxrand); 85 79 86 80 } 87 81 88 89 82 if (this->state_ == SLAVE && this->formationMode_ == ATTACK) 90 83 { 91 84 // search enemy 92 if ((!this->target_)) 85 random = rnd(maxrand); 86 if (random < (botlevel_*100) && (!this->target_)) 93 87 this->searchNewTarget(); 94 88 95 89 // next enemy 90 random = rnd(maxrand); 91 if (random < (botlevel_*30) && (this->target_)) 92 this->searchNewTarget(); 93 96 94 // shoot 97 if ((this->target_ && !this->bShooting_)) 95 random = rnd(maxrand); 96 if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_)) 98 97 this->bShooting_ = true; 99 98 100 99 // stop shooting 101 if (this->bShooting_ && !this->target_) 100 random = rnd(maxrand); 101 if (random < (1-botlevel_)*50 && (this->bShooting_)) 102 102 this->bShooting_ = false; 103 103 … … 106 106 if (this->state_ == MASTER) 107 107 { 108 109 //------------------------------------------------------- 110 //collect data for AI behaviour 111 Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0); 112 Vector3* meanOfAlliesPtr = new Vector3(0.0,0.0,0.0); 113 Vector3 meanOfAllies = *meanOfAlliesPtr; 114 Vector3 meanOfEnemies = *meanOfEnemiesPtr; 115 116 117 for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it) 118 { 119 120 Gametype* gt=this->getGametype(); 121 if (!gt) 122 { 123 gt=it->getGametype(); 124 } 125 if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt)) 126 { 127 enemies_.push_back(*it); 128 } 129 else { 130 allies_.push_back(*it); 131 } 132 } 133 if (enemies_.size() != 0 && allies_.size() != 0){ 134 for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it) 135 meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition(); 136 137 meanOfEnemies /= enemies_.size(); 138 139 for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it) 140 meanOfAllies += (*it)->getControllableEntity()->getWorldPosition(); 141 142 meanOfAllies /= allies_.size(); 143 144 //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl; 145 orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl; 146 orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl; 147 orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl; 148 } 149 //------------------------------------------------------- 150 151 //Decide which formationMode to choose 152 this->setFormationMode(ATTACK); 153 this->setDesiredPositionOfSlaves(); 154 155 //this->commandSlaves(); 108 this->commandSlaves(); 156 109 157 110 if (this->specificMasterAction_ != NONE) … … 161 114 162 115 // make 180 degree turn - a specific Master Action 163 /*164 116 random = rnd(1000.0f); 165 117 if (random < 5) … … 171 123 this->spinInit(); 172 124 173 */174 125 /*// follow a randomly chosen human - a specific Master Action 175 126 random = rnd(1000.0f); … … 177 128 this->followRandomHumanInit(); 178 129 */ 179 /*180 130 // lose master status (only if less than 4 slaves in formation) 181 131 random = rnd(maxrand); 182 132 if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) 183 133 this->loseMasterState(); 184 */ 185 134 186 135 // look out for outher masters if formation is small 187 136 random = rnd(maxrand); … … 193 142 } 194 143 } 195 allies_.clear(); 196 enemies_.clear(); 144 197 145 } 198 146 199 147 void AIController::tick(float dt) 200 148 { 201 202 149 if (!this->isActive()) 203 150 return; 151 204 152 float random; 205 153 float maxrand = 100.0f / ACTION_INTERVAL; 206 154 ControllableEntity* controllable = this->getControllableEntity(); 207 if (this->state_ == SLAVE && controllable && this->mode_ == DEFAULT)208 {209 210 if (this->myMaster_ && this->myMaster_->getControllableEntity() && desiredRelativePosition_){211 Vector3 desiredAbsolutePosition = this->myMaster_->getControllableEntity()->getWorldPosition() + this->myMaster_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_);212 this->moveToPosition (desiredAbsolutePosition);213 //WorldEntity* waypoint = new WorldEntity(this->getContext());214 //waypoint->setPosition(desiredAbsolutePosition);215 216 //this->addWaypoint(waypoint);217 218 }219 220 }221 155 //DOES: Either move to the waypoint or search for a Point of interest 222 156 if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target … … 255 189 { 256 190 this->aimAtTarget(); 257 this->follow(); //If a bot is shooting a player, it shouldn't let him go away easily. 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. 258 194 } 259 195 } … … 316 252 }//END_OF ROCKET MODE 317 253 318 319 254 SUPER(AIController, tick, dt); 320 255 } 321 256 //**********************************************NEW 322 257 void AIController::defaultBehaviour(float maxrand) 323 { 324 if (!this->target_) 325 this->searchNewTarget(); 326 if (!(this->passive_) && (this->target_ && !this->bShooting_)) 327 this->bShooting_ = true; 328 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(); 329 323 } 330 324
Note: See TracChangeset
for help on using the changeset viewer.