Changeset 10832 for code/branches/AI_HS15/src/orxonox/controllers
- Timestamp:
- Nov 23, 2015, 11:17:22 AM (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
r10826 r10832 51 51 CommonController::CommonController( Context* context ): Controller( context ) 52 52 { 53 this->bSetupWorked = false;54 53 55 54 56 this->executingMoveToPoint_ = false;57 55 this->action_ = Action::FLY; 58 56 this->stopLookingAtTarget(); … … 71 69 SUPER( CommonController, XMLPort, xmlelement, mode ); 72 70 XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode ); 73 71 XMLPortParam( CommonController, "action", setActionXML, getActionXML, xmlelement, mode ); 72 73 } 74 void CommonController::setActionXML( std::string val) 75 { 76 const std::string valUpper = getUppercase( val ); 77 Action::Value value; 78 79 if ( valUpper == "FIGHT" ) 80 value = Action::FIGHT; 81 else if ( valUpper == "FLY" ) 82 value = Action::FLY; 83 else if ( valUpper == "PROTECT" ) 84 value = Action::PROTECT; 85 else 86 ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ val + "'." ); 87 this->setAction( value ); 88 } 89 std::string CommonController::getActionXML() 90 { 91 switch ( this->action_ ) 92 { 93 case Action::FIGHT: 94 { 95 return "FIGHT"; 96 break; 97 } 98 case Action::FLY: 99 { 100 return "FLY"; 101 break; 102 } 103 case Action::PROTECT: 104 { 105 return "PROTECT"; 106 break; 107 } 108 default: 109 return "FIGHT"; 110 break; 111 } 74 112 } 75 113 void CommonController::setFormationModeXML( std::string val ) … … 118 156 return this->action_; 119 157 } 158 void CommonController::setAction (Action::Value action) 159 { 160 this->action_ = action; 161 } 120 162 121 163 void CommonController::setAction (Action::Value action, ControllableEntity* target) … … 156 198 } 157 199 } 200 void CommonController::setClosestTarget() 201 { 202 if (!this->getControllableEntity()) 203 return; 204 205 Pawn* closestTarget = 0; 206 float minDistance = std::numeric_limits<float>::infinity(); 207 208 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 209 { 210 if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) 211 continue; 212 213 float distance = CommonController::distance (*itP, this->getControllableEntity()); 214 if (distance < minDistance) 215 { 216 closestTarget = *itP; 217 minDistance = distance; 218 } 219 } 220 if (closestTarget) 221 { 222 (this)->setTarget(static_cast<ControllableEntity*>(closestTarget)); 223 } 224 } 158 225 void CommonController::maneuver() 159 226 { 160 counter++;161 162 if ( counter> 5)163 counter= 0;227 maneuverCounter_++; 228 229 if (maneuverCounter_ > 5) 230 maneuverCounter_ = 0; 164 231 if ( this->target_ && this->getControllableEntity()) 165 232 { … … 183 250 184 251 //bool bThisIsLookingAtTarget = this->isLooking ( getControllableEntity(), this->target_, math::pi/4 ); 185 bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/ 5.0f );252 bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/10.0f ); 186 253 187 254 … … 217 284 { 218 285 this->setTargetPosition( this->positionOfTarget_ ); 219 /* if ( counter== 0)286 /* if (maneuverCounter_ == 0) 220 287 { 221 288 this->setTargetPosition( this->positionOfTarget_ ); … … 231 298 { 232 299 233 if ( counter== 0)300 if (maneuverCounter_ == 0) 234 301 { 235 302 this->setTargetPosition( this->positionOfTarget_ ); … … 562 629 this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT, this->positionOfTarget_ ) < angle ); 563 630 } 564 bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) const631 bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) 565 632 { 566 633 if ( !entityThatLooks || !entityBeingLookedAt ) … … 587 654 float squaredDistance = squaredDistanceToTarget(); 588 655 589 if ( squaredDistance < 9000000.0f && this->isLookingAtTarget( math::pi / 10.0f)) {656 if ( squaredDistance < 9000000.0f && this->isLookingAtTarget( math::pi / 20.0f)) { 590 657 return true; 591 658 } … … 615 682 } 616 683 617 618 619 684 Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() ); 620 685 -
code/branches/AI_HS15/src/orxonox/controllers/CommonController.h
r10827 r10832 73 73 public: 74 74 static const float hardcoded_projectile_speed = 750; 75 76 75 static const float ACTION_INTERVAL = 1.0f; 77 78 76 79 77 CommonController(Context* context); 80 78 virtual ~CommonController(); 81 79 80 //----[XML data]---- 81 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 82 //----[Action data]---- 83 Action::Value getAction (); 84 void setAction (Action::Value action); 85 void setAction (Action::Value action, ControllableEntity* target); 86 void setAction (Action::Value action, const Vector3& target); 87 void setAction (Action::Value action, const Vector3& target, const Quaternion& orient ); 88 void setActionXML( std::string val); 89 std::string getActionXML(); 90 //----[/Action data]---- 91 //----[Formation data]---- 92 virtual void setFormationModeXML(std::string val); 93 virtual std::string getFormationModeXML(); 94 virtual void setFormationMode(FormationMode::Value val) 95 { this->formationMode_ = val; } 96 inline FormationMode::Value getFormationMode() const 97 { return this->formationMode_; } 98 //----[/Formation data]---- 99 //----[Rank data]---- 100 virtual void setRank(Rank::Value val) 101 { this->rank_ = val; } 102 inline Rank::Value getRank() const 103 { return this->rank_; } 104 //----[/Rank data]---- 105 //----[/XML data]---- 82 106 107 //----[Interaction with other Controllers]---- 108 virtual bool setWingman(CommonController* wingman); 109 virtual bool hasWingman(); 83 110 111 void setPositionOfTarget(const Vector3& target); 112 void setOrientationOfTarget(const Quaternion& orient); 84 113 114 void setTarget(ControllableEntity* target); 115 ControllableEntity* getTarget(); 116 bool hasTarget(); 85 117 86 virtual void setFormationMode(FormationMode::Value val) 87 { this->formationMode_ = val; } 88 inline FormationMode::Value getFormationMode() const 89 { return this->formationMode_; } 90 virtual void setFormationModeXML(std::string val); 91 virtual std::string getFormationModeXML(); 118 void setTargetPosition(const Vector3& target); 119 void setTargetOrientation(const Quaternion& orient); 120 void setTargetOrientation(ControllableEntity* target); 121 //----[/Interaction with other Controllers]---- 92 122 93 virtual void setRank(Rank::Value val) 94 { this->rank_ = val; } 95 inline Rank::Value getRank() const 96 { return this->rank_; } 123 //----[Helper functions]---- 124 float randomInRange(float a, float b); 125 static float distance(ControllableEntity* entity1, ControllableEntity* entity2); 126 static bool sameTeam (ControllableEntity* entity1, ControllableEntity* entity2); 127 static bool isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) ; 97 128 98 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 99 100 101 102 virtual bool setWingman(CommonController* wingman); 103 virtual bool hasWingman(); 104 static bool sameTeam (ControllableEntity* entity1, ControllableEntity* entity2); 105 void setTarget(ControllableEntity* target); 106 bool hasTarget(); 107 ControllableEntity* getTarget(); 108 void setTargetOrientation(const Quaternion& orient); 109 void setTargetOrientation(ControllableEntity* target); 110 void setTargetPosition(const Vector3& target); 111 112 /*void spin(); 113 void turn180();*/ 114 Action::Value getAction (); 115 void setAction (Action::Value action, ControllableEntity* target); 116 void setAction (Action::Value action, const Vector3& target); 117 void setAction (Action::Value action, const Vector3& target, const Quaternion& orient ); 129 float squaredDistanceToTarget() const; 130 bool isLookingAtTarget(float angle) const; 131 //----[/Helper functions]---- 118 132 119 133 protected: 120 void dodge(Vector3& thisPosition, Vector3& diffUnit); 121 int counter; 122 void moveToPoint(const Vector3& relativeTargetPosition, float angleRoll); 123 bool moveAndRoll(float dt); 134 //----[Flying functionality]---- 135 void stopMoving(); 124 136 125 void moveToPosition(const Vector3& target, float dt); 126 void moveToTargetPosition(float dt); 127 //enum Mode {ROCKET, ATTACK, MOVE, HOLD};//TODO; implement DEFENCE, MOVING modes 137 void moveToPoint(const Vector3& relativeTargetPosition, float angleRoll); 138 bool moveAndRoll(float dt); 128 139 129 //Mode mode_; 130 void copyOrientation(const Quaternion& orient, float dt); 131 void copyTargetOrientation(float dt); 140 void moveToPosition(const Vector3& target, float dt); 141 void moveToTargetPosition(float dt); 132 142 133 static float distance(ControllableEntity* entity1, ControllableEntity* entity2); 134 float squaredDistanceToTarget() const; 135 void doFire(); 136 void aimAtTarget(); 137 bool isLookingAtTarget(float angle) const; 138 bool isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle )const; 143 void copyOrientation(const Quaternion& orient, float dt); 144 void copyTargetOrientation(float dt); 139 145 140 //checks if spaceship points at enemy and if there are allies inbetween 141 bool canFire(); 142 std::map<std::string, int> weaponModes_; //<! Links each "weapon" to it's weaponmode - managed by setupWeapons() 143 //std::vector<int> projectiles_; //<! Displays amount of projectiles of each weapon. - managed by setupWeapons() 144 float timeout_; //<! Timeout for rocket usage. (If a rocket misses, a bot should stop using it.) 145 void setupWeapons(); //<! Defines which weapons are available for a bot. Is recalled whenever a bot was killed. 146 bool bSetupWorked; //<! If false, setupWeapons() is called. 147 int getFiremode(std::string name); 146 void lookAtTarget(float dt); 147 void stopLookingAtTarget(); 148 void startLookingAtTarget(); 148 149 149 float randomInRange(float a, float b); 150 bool bHasTargetPosition_; 151 Vector3 targetPosition_; 152 bool bHasTargetOrientation_; 153 Quaternion targetOrientation_; 150 bool bLookAtTarget_; 151 //----[/Flying functionality]---- 152 153 //----[Fighting functionality]---- 154 void maneuver(); 155 void dodge(Vector3& thisPosition, Vector3& diffUnit); 156 void aimAtTarget(); 157 bool canFire(); 158 void doFire(); 159 void setClosestTarget(); 154 160 155 Vector3 destination_; 156 bool bHasDestination; 161 bool bShooting_; 162 int maneuverCounter_; 163 //----[/Fighting functionality]---- 164 165 //----[where-to-fly information]---- 166 bool bHasTargetPosition_; 167 Vector3 targetPosition_; 168 bool bHasTargetOrientation_; 169 Quaternion targetOrientation_; 170 // Vector3 destination_; 171 // bool bHasDestination; 172 //----[/where-to-fly information]---- 173 174 //----[who-to-kill information]---- 175 WeakPtr<ControllableEntity> target_; 176 //WeakPtr<ControllableEntity> objectiveTarget_; 157 177 178 bool bHasPositionOfTarget_; 179 Vector3 positionOfTarget_; 180 bool bHasOrientationOfTarget_; 181 Quaternion orientationOfTarget_; 182 //----[/who-to-kill information]---- 158 183 159 void stopMoving(); 160 void setPositionOfTarget(const Vector3& target); 161 void setOrientationOfTarget(const Quaternion& orient); 162 bool bHasPositionOfTarget_; 163 Vector3 positionOfTarget_; 164 bool bHasOrientationOfTarget_; 165 Quaternion orientationOfTarget_; 166 167 168 WeakPtr<ControllableEntity> target_; 169 //WeakPtr<ControllableEntity> thisEntity_; 170 171 172 173 Action::Value action_; 174 bool bEngaging_; 175 bool bShooting_; 176 WeakPtr<ControllableEntity> objectiveTarget_; 177 178 void lookAtTarget(float dt); 179 void stopLookingAtTarget(); 180 void startLookingAtTarget(); 181 bool bLookAtTarget_; 182 void maneuver(); 183 void chooseManeuverType(); 184 void gunsD(); 185 void attack(); 186 void scissors(); 187 FormationMode::Value formationMode_; 188 Rank::Value rank_; 189 190 bool executingMoveToPoint_; 191 192 private: 193 194 184 //----["Private" variables]---- 185 FormationMode::Value formationMode_; 186 Rank::Value rank_; 187 Action::Value action_; 188 //----[/"Private" variables]---- 195 189 }; 196 190 } -
code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc
r10826 r10832 84 84 void DivisionController::action() 85 85 { 86 //----find a target---- 87 if ( !this->hasTarget() ) 88 { 89 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 90 { 91 if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) 92 continue; 93 94 95 if (static_cast<ControllableEntity*>(*itP) != (this)->getControllableEntity() 96 && CommonController::distance (*itP, this->getControllableEntity()) < 10000) 97 { 98 (this)->setAction(Action::FIGHT, *itP); 99 } 100 } 101 } 86 102 87 103 88 if (this->action_ == Action::FIGHT) 104 89 { 105 //----choose where to go---- 106 this->maneuver(); 107 //----fire if you can---- 108 this->bShooting_ = this->canFire(); 109 110 if (this->target_) 111 { 112 if (this->myWingman_) 113 { 114 //----wingmans shall support the fire of their leaders---- 115 this->myWingman_->setAction (Action::FIGHT, this->target_); 116 } 117 90 if (!this->hasTarget()) 91 { 92 //----find a target---- 93 this->setClosestTarget(); 94 } 95 else 96 { 118 97 //----fly in formation if far enough---- 119 98 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); … … 121 100 { 122 101 this->setTargetPositionOfWingman(); 123 //this->setTargetPositionOfFollower(); 124 } 125 } 102 this->setTargetPositionOfFollower(); 103 } 104 else 105 { 106 //----wingmans shall support the fire of their leaders---- 107 if (this->myWingman_) 108 { 109 this->myWingman_->setAction (Action::FIGHT, this->target_); 110 } 111 if (this->myFollower_) 112 { 113 this->myFollower_->setAction (Action::FIGHT); 114 } 115 116 } 117 118 } 119 if (this->hasTarget()) 120 { 121 //----choose where to go---- 122 this->maneuver(); 123 //----fire if you can---- 124 this->bShooting_ = this->canFire(); 125 } 126 126 127 } 127 128 else if (this->action_ == Action::FLY) … … 152 153 case FormationMode::FINGER4: 153 154 { 154 targetRelativePositionOfWingman = new Vector3 (400, 0, -200);155 targetRelativePositionOfWingman = new Vector3 (400, 0, 200); 155 156 break; 156 157 } … … 158 159 case FormationMode::DIAMOND: 159 160 { 160 targetRelativePositionOfWingman = new Vector3 (400, 0, -200);161 targetRelativePositionOfWingman = new Vector3 (400, 0, 200); 161 162 break; 162 163 } … … 185 186 case FormationMode::FINGER4: 186 187 { 187 targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);188 targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); 188 189 break; 189 190 } … … 191 192 case FormationMode::DIAMOND: 192 193 { 193 targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);194 targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); 194 195 break; 195 196 } -
code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc
r10826 r10832 89 89 this->myDivisionLeader_ = newDivisionLeader; 90 90 91 if (newDivisionLeader)92 {93 //orxout(internal_error) << "new DivisionLeader set" << endl;94 }95 //----If no leader found, attack someone----96 //----TODO: find closest enemy----97 else98 {99 if ( !this->hasTarget() || this->action_ != Action::FIGHT )100 {101 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)102 {103 if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )104 continue;105 106 this->setAction(Action::FIGHT, (*itP));107 break;108 }109 }110 111 }112 113 91 } 114 92 //----If have leader---- 115 93 else 116 94 { 117 this->chooseTarget();118 95 } 119 96 … … 121 98 if (this->action_ == Action::FIGHT) 122 99 { 123 //----choose where to go---- 124 this->maneuver(); 125 //----fire if you can---- 126 this->bShooting_ = this->canFire(); 127 128 if (this->target_) 129 { 130 //----wingmans shall support the fire of their leaders---- 131 if (this->myWingman_) 132 { 133 this->myWingman_->setAction (Action::FIGHT, this->target_); 134 } 100 if (!this->hasTarget()) 101 { 102 if (this->myDivisionLeader_) 103 { 104 this->chooseTarget(); 105 } 106 else 107 { 108 this->setClosestTarget(); 109 } 110 } 111 else 112 { 113 135 114 //----fly in formation if far enough---- 136 115 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); … … 139 118 this->setTargetPositionOfWingman(); 140 119 } 120 else 121 { 122 //----wingmans shall support the fire of their leaders---- 123 if (this->myWingman_) 124 { 125 this->myWingman_->setAction (Action::FIGHT, this->target_); 126 } 127 } 128 } 129 if (this->hasTarget()) 130 { 131 //----choose where to go---- 132 this->maneuver(); 133 //----fire if you can---- 134 this->bShooting_ = this->canFire(); 141 135 } 142 136 } … … 156 150 157 151 } 158 //PRE: myDivisionLeader_ != 0 152 //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT 159 153 //POST: this->target_ is set unless division leader doesn't have one 160 154 void SectionController::chooseTarget() … … 211 205 212 206 //----stay in formation---- 207 //gani-TODO: sum targetAbso... and this->predicted position 213 208 void SectionController::setTargetPositionOfWingman() 214 209 { … … 224 219 case FormationMode::FINGER4: 225 220 { 226 targetRelativePositionOfWingman = new Vector3 (-400, 0, -200);221 targetRelativePositionOfWingman = new Vector3 (-400, 0, 200); 227 222 break; 228 223 } -
code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc
r10826 r10832 85 85 CommonController* newLeader = findNewLeader(); 86 86 this->myLeader_ = newLeader; 87 if (newLeader) 88 { 89 //orxout(internal_error) << "new Leader set" << endl; 90 } 91 //----If no leader found, attack someone---- 92 //----TODO: find closest enemy---- 93 else 94 { 95 if ( !this->hasTarget() || this->action_ != Action::FIGHT ) 96 { 97 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 98 { 99 if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) 100 continue; 101 this->setAction(Action::FIGHT, (*itP)); 102 break; 103 } 104 } 105 } 87 106 88 } 107 89 //----If have leader, he will deal with logic---- 108 90 else 109 91 { 110 92 this->action_ = this->myLeader_->getAction(); 111 93 } 112 94 … … 115 97 if (this->action_ == Action::FIGHT) 116 98 { 117 //----choose where to go---- 118 this->maneuver(); 119 //----fire if you can---- 120 this->bShooting_ = this->canFire(); 99 //----If no leader found, attack someone---- 100 if (!this->hasTarget() && !this->myLeader_) 101 { 102 this->setClosestTarget(); 103 } 104 if (this->hasTarget()) 105 { 106 //----choose where to go---- 107 this->maneuver(); 108 //----fire if you can---- 109 this->bShooting_ = this->canFire(); 110 } 121 111 } 122 112 //----action was set to fly, leader handles the logic---- … … 125 115 126 116 } 127 //---- TODO: implement protect----117 //----gani-TODO: implement protect---- 128 118 else if (this->action_ == Action::PROTECT) 129 119 {
Note: See TracChangeset
for help on using the changeset viewer.