Changeset 10762 for code/branches/AI_HS15/src/orxonox/controllers
- Timestamp:
- Nov 3, 2015, 3:56:41 PM (9 years ago)
- Location:
- code/branches/AI_HS15/src/orxonox/controllers
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc
r10759 r10762 242 242 } 243 243 244 245 int CommonController::getFiremode(std::string name)246 {247 for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it)248 {249 if (it->first == name)250 return it->second;251 }252 return -1;253 }254 244 bool CommonController::isCloseAtTarget(float distance) const 255 245 { … … 262 252 return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); 263 253 } 264 void CommonController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions) 265 { 266 this->bSetupWorked = false; 267 if(this->getControllableEntity()) 254 255 256 bool CommonController::canFire() 257 { 258 //check pointers 259 if (!this->getControllableEntity() || !this->target_ || !this->target_->getControllableEntity()) 260 return false; 261 262 //check if this points in the direction of target_ 263 264 Vector3 myPosition = this->getControllableEntity()->getWorldPosition(); 265 Vector3 targetPosition = this->target_->getControllableEntity()->getWorldPosition(); 266 Vector3 differenceVector = targetPosition - myPosition; 267 float scalarProduct = differenceVector * WorldEntity::FRONT; 268 Vector3 projection = scalarProduct * WorldEntity::FRONT; 269 if ((differenceVector - projection).length() > 50) 270 return false; 271 272 273 //check if there are allies on the way 274 Vector3 allyPosition, allyDifference, allyProjection; 275 float allyScalarProduct; 276 for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) 268 277 { 269 Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); 270 if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip 271 { 272 this->weaponModes_.clear(); // reset previous weapon information 273 WeaponSlot* wSlot = 0; 274 for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++) 275 { 276 WeaponMode* wMode = 0; 277 for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++) 278 { 279 std::string wName = wMode->getIdentifier()->getName(); 280 if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new" 281 weaponModes_[wName] = wMode->getMode(); 282 } 283 } 284 if(weaponModes_.size())//at least one weapon detected 285 this->bSetupWorked = true; 286 }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user); 287 } 278 if (!it->getControllableEntity()) 279 continue; 280 if ((this->getControllableEntity()->getTeam() == (it)->getControllableEntity()->getTeam())) 281 { 282 allyPosition = it->getControllableEntity()->getWorldPosition(); 283 allyDifference = allyPosition - myPosition; 284 allyScalarProduct = allyDifference * WorldEntity::FRONT; 285 allyProjection = allyScalarProduct * WorldEntity::FRONT; 286 if (allyScalarProduct < 0 || allyScalarProduct > scalarProduct) 287 continue; 288 if ((allyDifference - allyProjection).length() < 50) 289 return false; 290 } 291 } 292 293 return true; 294 288 295 } 289 296 void CommonController::doFire() 290 297 { 291 if(!this->bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ... 292 { 293 this->setupWeapons(); 294 } 295 else if(this->getControllableEntity() && weaponModes_.size()&&this->bShooting_ && 296 this->isCloseAtTarget((1 + 2)*1000) && this->isLookingAtTarget(math::pi / 20.0f)) 297 { 298 int firemode; 299 float random = rnd(1);// 300 if (this->isCloseAtTarget(130) && (firemode = getFiremode("LightningGun")) > -1 ) 301 {//LENSFLARE: short range weapon 302 this->getControllableEntity()->fire(firemode); //ai uses lens flare if they're close enough to the target 303 } 304 305 else if ((firemode = getFiremode("HsW01")) > -1 ) //LASER: default weapon 306 this->getControllableEntity()->fire(firemode); 307 308 } 309 } 310 bool CommonController::isLookingAtTarget(float angle) const 311 { 312 if (!this->getControllableEntity()) 313 return false; 314 315 return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); 316 } 317 318 void CommonController::aimAtTarget() 319 { 320 if (!this->target_ || !this->getControllableEntity()) 321 return; 322 323 static const float hardcoded_projectile_speed = 750; 324 325 Vector3 aimPosition = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), 326 hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity()); 327 328 Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); 329 if (pawn) 330 pawn->setAimPosition(aimPosition); 331 } 332 333 298 this->getControllableEntity()->fire(0); 299 } 300 334 301 335 302 } -
code/branches/AI_HS15/src/orxonox/controllers/CommonController.h
r10761 r10762 133 133 bool isLookingAtTarget(float angle) const; 134 134 135 135 //checks if spaceship points at enemy and if there are allies inbetween 136 bool canFire(); 136 137 std::map<std::string, int> weaponModes_; //<! Links each "weapon" to it's weaponmode - managed by setupWeapons() 137 138 //std::vector<int> projectiles_; //<! Displays amount of projectiles of each weapon. - managed by setupWeapons() -
code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc
r10759 r10762 95 95 } 96 96 */ 97 98 if (canFire()) 99 doFire(); 97 100 98 101 } -
code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc
r10759 r10762 58 58 return; 59 59 60 /*if (this->target_) 61 { 62 this->aimAtTarget(); 63 this->doFire(); 64 this->bShooting_ = true; 65 }*/ 60 66 61 if (this->bHasTargetPosition_) 67 62 { … … 91 86 if (this->target_ && this->myWingman_) 92 87 this->myWingman_->setTarget(this->target_); 88 89 if (canFire()) 90 doFire(); 93 91 94 92 -
code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc
r10761 r10762 111 111 void WingmanController::tick(float dt) 112 112 { 113 //------------------------------------------------------- 114 /* if (this->target_) 115 { 116 this->aimAtTarget(); 117 this->doFire(); 118 this->bShooting_ = true; 119 }*/ 113 120 114 121 115 if (!this->isActive()) 122 116 return; 123 //--------------------------Stay in formation--------------------------124 117 if (!this->target_) 125 118 { … … 128 121 else 129 122 { 130 123 131 124 } 132 125 if (this->bHasTargetPosition_) … … 135 128 } 136 129 137 //--------------------------Attack same target as the Leader-------------------------- 138 139 /*if (this->target_) 140 { 141 this->aimAtTarget(); 142 this->doFire(); 143 } 144 */ 145 146 //orxout(internal_error) << "I am " << this << endl; 147 130 148 131 149 132 SUPER(WingmanController, tick, dt); … … 169 152 170 153 } 154 155 if (canFire()) 156 doFire(); 171 157 } 172 158
Note: See TracChangeset
for help on using the changeset viewer.