- Timestamp:
- May 3, 2010, 6:41:43 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ai/src/orxonox/controllers/ArtificialController.cc
r6795 r6850 45 45 this->target_ = 0; 46 46 this->myMaster_ = 0; 47 //this->slaveNumber_ = -1;48 this->team_ = -1; //new49 this->state_ = FREE; //new47 this->freedomCount_ = 0; 48 this->team_ = -1; 49 this->state_ = FREE; 50 50 this->bShooting_ = false; 51 51 this->bHasTargetPosition_ = false; 52 52 this->targetPosition_ = Vector3::ZERO; 53 //this->slaves_ = new std::list<ArtificialController*>;54 53 55 54 this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); … … 65 64 66 65 XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0); 66 } 67 68 // gets called when Bot dies 69 void ArtificialController::changedControllableEntity() 70 { 71 COUT(0) << "~changedControllableEntity 0" << std::endl; 72 if(!getControllableEntity()) 73 { 74 COUT(0) << "~changedControllableEntity 1" << std::endl; 75 if (this->state_ == SLAVE) unregisterSlave(); 76 COUT(0) << "~changedControllableEntity 2" << std::endl; 77 if (this->state_ == MASTER) setNewMasterWithinFormation(); 78 COUT(0) << "~changedControllableEntity 3" << std::endl; 79 this->slaves_.clear(); 80 COUT(0) << "~changedControllableEntity 4" << std::endl; 81 this->state_ = FREE; 82 COUT(0) << "~changedControllableEntity 5" << std::endl; 83 } 67 84 } 68 85 … … 77 94 if (this->target_ || distance > 10) 78 95 { 79 // Multiply with 0.8 to make them a bit slower 80 this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); 81 this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); 96 // Multiply with rotateFactor to make them a bit slower 97 float rotateFactor; 98 if(this->state_ == SLAVE) rotateFactor = 1.0f; 99 if(this->state_ == MASTER) rotateFactor = 0.4f; 100 else rotateFactor = 0.8f; 101 102 this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x); 103 this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y); 104 105 106 82 107 } 83 108 84 109 if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) 85 this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance 86 else 87 this->getControllableEntity()->moveFrontBack(0.8f); 110 { 111 if(this->state_ == SLAVE) this->getControllableEntity()->moveFrontBack(-0.8f); 112 else this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance 113 } else { 114 if(this->state_ == SLAVE) this->getControllableEntity()->moveFrontBack(2.5f); 115 if(this->state_ == MASTER) this->getControllableEntity()->moveFrontBack(0.4f); 116 else this->getControllableEntity()->moveFrontBack(0.8f); 117 } 88 118 } 89 119 … … 102 132 { 103 133 myMaster_->slaves_.remove(this); 134 COUT(0) << "~unregister slave" << std::endl; 104 135 } 105 136 } … … 126 157 continue; 127 158 128 ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); 129 if (!controller || controller->getState() != MASTER) 159 ArtificialController *newMaster = static_cast<ArtificialController*>(it->getController()); 160 161 if (!newMaster || newMaster->getState() != MASTER) 130 162 continue; 131 163 … … 133 165 if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000 134 166 { 135 if(controller->slaves_.size() > 9) continue; 136 137 this->freeAllSlaves(); 167 if(newMaster->slaves_.size() > 6) continue; 168 169 //this->freeSlaves(); 170 171 for(std::list<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++) 172 { 173 (*itSlave)->myMaster_ = newMaster; 174 newMaster->slaves_.push_back(*itSlave); 175 } 138 176 this->slaves_.clear(); 139 177 this->state_ = SLAVE; 140 178 141 this->myMaster_ = controller;142 controller->slaves_.push_back(this);179 this->myMaster_ = newMaster; 180 newMaster->slaves_.push_back(this); 143 181 144 182 break; … … 148 186 //hasn't encountered any masters in range? -> become a master 149 187 if (state_!=SLAVE) state_ = MASTER; // keep in mind: what happens when two masters encounter eache other? -> has to be evaluated in the for loop within master mode in AIcontroller... 150 188 COUT(0) << "~searcheNewMaster" << std::endl; 151 189 } 152 190 … … 157 195 (*it)->setTargetPosition(this->getControllableEntity()->getPosition()); 158 196 } 159 /*160 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)161 {162 163 if (!it->getController())164 continue;165 166 ArtificialController *controller = static_cast<ArtificialController*>(it->getController());167 if (!controller || controller->getState() != SLAVE)168 continue;169 //controller->setTargetPosition(this->getControllableEntity()->getPosition());170 controller->targetPosition_ = this->getControllableEntity()->getPosition();171 controller->bHasTargetPosition_ = true;172 }173 */174 197 } 175 198 … … 190 213 newMaster->state_ = MASTER; 191 214 newMaster->slaves_ = this->slaves_; 192 //this->slaves_.clear();215 this->slaves_.clear(); 193 216 194 217 this->state_ = SLAVE; … … 205 228 } 206 229 207 void ArtificialController::freeAllSlaves() 208 { 209 210 230 void ArtificialController::freeSlaves() 231 { 211 232 for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) 212 233 { 213 234 (*it)->state_ = FREE; 214 235 } 215 /* 216 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) 217 { 218 ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); 219 if (controller && controller->getState() != SLAVE) 220 continue; 221 222 controller->state_ = FREE; 223 } 224 */ 236 this->slaves_.clear(); 237 } 238 239 void ArtificialController::forceFreeSlaves() 240 { 241 for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) 242 { 243 (*it)->state_ = FREE; 244 (*it)->forceFreedom(); 245 (*it)->targetPosition_ = this->targetPosition_; 246 (*it)->bShooting_ = true; 247 (*it)->getControllableEntity()->fire(0); 248 } 225 249 } 226 250 227 251 void ArtificialController::loseMasterState() 228 252 { 229 this->free AllSlaves();253 this->freeSlaves(); 230 254 this->state_ = FREE; 255 } 256 257 void ArtificialController::forceFreedom() 258 { 259 this->freedomCount_ = 5; 260 } 261 262 bool ArtificialController::forcedFree() 263 { 264 if(this->freedomCount_ > 0) 265 { 266 this->freedomCount_--; 267 return true; 268 } else return false; 231 269 } 232 270
Note: See TracChangeset
for help on using the changeset viewer.