Changeset 8107 for code/branches/tetris/src/modules/pong
- Timestamp:
- Mar 23, 2011, 9:41:21 PM (14 years ago)
- Location:
- code/branches/tetris/src/modules/pong
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/tetris/src/modules/pong/PongAI.cc
r6417 r8107 27 27 */ 28 28 29 /** 30 @file PongAI.cc 31 @brief Implementation of the PongAI class. 32 */ 33 29 34 #include "PongAI.h" 30 35 … … 32 37 #include "core/ConfigValueIncludes.h" 33 38 #include "tools/Timer.h" 39 34 40 #include "worldentities/ControllableEntity.h" 41 35 42 #include "PongBall.h" 36 43 … … 41 48 const static float MAX_REACTION_TIME = 0.4f; 42 49 50 /** 51 @brief 52 Constructor. Registers and initializes the object. 53 */ 43 54 PongAI::PongAI(BaseObject* creator) : Controller(creator) 44 55 { … … 59 70 } 60 71 72 /** 73 @brief 74 Destructor. Cleans up the list fo reaction timers. 75 */ 61 76 PongAI::~PongAI() 62 77 { … … 65 80 } 66 81 82 /** 83 @brief 84 Sets config values. 85 */ 67 86 void PongAI::setConfigValues() 68 87 { 88 // Sets the strength of the PongAi as a config value. 69 89 SetConfigValue(strength_, 0.5).description("A value from 0 to 1 where 0 is weak and 1 is strong."); 70 90 } 71 91 92 /** 93 @brief 94 Is called each tick. 95 Implements the behavior of the PongAI (i.e. its intelligence). 96 @param dt 97 The time that has elapsed since the last tick. 98 */ 72 99 void PongAI::tick(float dt) 73 100 { 74 if (!this->ball_ || !this->getControllableEntity()) 101 // If either the ball, or the controllable entity (i.e. the bat) don't exist (or aren't set). 102 if (this->ball_ == NULL || this->getControllableEntity() == NULL) 75 103 return; 76 104 … … 109 137 if (this->ballDirection_.x != 1) 110 138 { 111 // The ball just starte tto approach, initialize all values139 // The ball just started to approach, initialize all values 112 140 this->ballDirection_.x = 1; 113 141 this->ballDirection_.y = sgn(ballvel.z); … … 158 186 { 159 187 // We had to correct our position because we moved too far 160 // (and delay is tfalse, so we're not in the wrong place because of a new end-position prediction)188 // (and delay is false, so we're not in the wrong place because of a new end-position prediction) 161 189 if (fabs(mypos.z - this->ballEndPosition_) < 0.5 * this->ball_->getBatLength() * this->ball_->getFieldDimension().y) 162 190 { … … 173 201 } 174 202 203 /** 204 @brief 205 Calculates the random offset, that accounts for random errors the AI makes in order to be beatable. 206 The higher the strength of the AI, the smaller the (expected value of the) error. 207 The result of this method is stored in this->randomOffset_. 208 */ 175 209 void PongAI::calculateRandomOffset() 176 210 { … … 184 218 // exp < 1 -> position is more likely a large number 185 219 186 // The position shoul n't be larger than 0.5 (50% of the bat-length from the middle is the end)220 // The position shouldn't be larger than 0.5 (50% of the bat-length from the middle is the end) 187 221 position *= 0.48f; 188 222 … … 194 228 } 195 229 230 /** 231 @brief 232 Calculate the end position the ball will be in. 233 The result of this calculation is stored in this->ballEndPosition_. 234 */ 196 235 void PongAI::calculateBallEndPosition() 197 236 { … … 295 334 } 296 335 336 /** 337 @brief 338 Determine the movement the AI will undertake. (Either -1, 0 or 1) 339 The result of this calculation is stored in this->movement_; 340 @param direction 341 The current direction of movement. 342 @param bUseDelay 343 The time by which this move is delayed. (Again, to make the AI less efficient) 344 */ 297 345 void PongAI::move(char direction, bool bUseDelay) 298 346 { … … 321 369 } 322 370 371 /** 372 @brief 373 Is called, when a delayed move takes effect. 374 */ 323 375 void PongAI::delayedMove() 324 376 { -
code/branches/tetris/src/modules/pong/PongAI.h
r5929 r8107 27 27 */ 28 28 29 /** 30 @file PongAI.h 31 @brief Declaration of the PongAI class. 32 @ingroup Pong 33 */ 34 29 35 #ifndef _PongAI_H__ 30 36 #define _PongAI_H__ … … 33 39 34 40 #include <list> 41 35 42 #include "util/Math.h" 36 43 #include "tools/interfaces/Tickable.h" 44 37 45 #include "controllers/Controller.h" 38 46 39 47 namespace orxonox 40 48 { 49 50 /** 51 @brief 52 The PongAI is an artificial intelligence for the @ref orxonox::Pong "Pong" gametype. 53 54 @author 55 Fabian 'x3n' Landau 56 57 @ingroup Pong 58 */ 41 59 class _PongExport PongAI : public Controller, public Tickable 42 60 { 43 61 public: 44 PongAI(BaseObject* creator); 62 PongAI(BaseObject* creator); //!< Constructor. Registers and initializes the object. 45 63 virtual ~PongAI(); 46 64 47 65 void setConfigValues(); 48 66 49 virtual void tick(float dt); 67 virtual void tick(float dt); //!< Implements the behavior of the PongAI (i.e. its intelligence). 50 68 69 /** 70 @brief Set the ball for the AI. 71 @param ball A pointer to the ball. 72 */ 51 73 void setPongBall(PongBall* ball) 52 74 { this->ball_ = ball; } 53 75 54 76 protected: 55 void calculateRandomOffset(); 56 void calculateBallEndPosition(); 57 void move(char direction, bool bUseDelay); 58 void delayedMove(); 77 void calculateRandomOffset(); //!< Calculates the random offset, that accounts for random errors the AI makes in order to be beatable. 78 void calculateBallEndPosition(); //!< Calculate the end position the ball will be in. 79 void move(char direction, bool bUseDelay); //!< Determine the movement the AI will undertake. 80 void delayedMove(); //!< Is called, when a delayed move takes effect. 59 81 60 PongBall* ball_; 61 Vector2 ballDirection_; 62 float ballEndPosition_; 63 float randomOffset_; 64 bool bChangedRandomOffset_; 65 float relHysteresisOffset_; 66 float strength_; 82 PongBall* ball_; //!< A pointer to the ball. 83 Vector2 ballDirection_; //!< Vector to store the (x,z) direction in which the ball is flying. 84 float ballEndPosition_; //!< The calculated end position of the ball. 85 float randomOffset_; //!< A random offset to introduce random errors (weighted by the strength of the AI) into the AI's behavior. 86 bool bChangedRandomOffset_; //!< Helper boolean, to change the random offset more often. 87 float relHysteresisOffset_; //!< A hysteresis offset. 88 float strength_; //!< The strength of the AI. Ranging from 0 to 1. 67 89 68 std::list<std::pair<Timer*, char> > reactionTimers_; 69 char movement_; 70 char oldMove_; 71 bool bOscillationAvoidanceActive_; 90 std::list<std::pair<Timer*, char> > reactionTimers_; //!< A list of reaction timers and the directions that take effect when their timer expires. 91 char movement_; //!< The planned movement. 92 char oldMove_; //!< The previous movement. 93 bool bOscillationAvoidanceActive_; //!< Boolean, to avoid oscillations. 72 94 }; 73 95 } -
code/branches/tetris/src/modules/pong/PongBat.cc
r8106 r8107 87 87 this->movement_ = clamp(this->movement_, -1.0f, 1.0f) * this->speed_; 88 88 89 // TODO: Why needed?89 // If moveRightLeft() is used the movement is dependento on wehther it is the right or the left bat, so, it is i.e. dependent on the orientation of the bat. 90 90 if (this->bMoveLocal_) 91 91 this->setVelocity(this->getOrientation() * Vector3(this->movement_, 0, 0)); -
code/branches/tetris/src/modules/pong/PongBot.cc
r5781 r8107 27 27 */ 28 28 29 /** 30 @file PongBot.cc 31 @brief Implementation of the PongBot class. 32 */ 33 29 34 #include "PongBot.h" 30 35 … … 36 41 CreateFactory(PongBot); 37 42 43 /** 44 @brief 45 Constructor. Registers the object and creates a PongAI controller. 46 */ 38 47 PongBot::PongBot(BaseObject* creator) : Bot(creator) 39 48 { -
code/branches/tetris/src/modules/pong/PongBot.h
r5781 r8107 27 27 */ 28 28 29 /** 30 @file PongBot.h 31 @brief Declaration of the PongBot class. 32 @ingroup Pong 33 */ 34 29 35 #ifndef _PongBot_H__ 30 36 #define _PongBot_H__ … … 35 41 namespace orxonox 36 42 { 43 44 /** 45 @brief 46 A bot especially for @ref orxonox::Pong "Pong". 47 48 Uses the @ref orxonox::PongAI "PongAI". 49 50 @author 51 Fabian 'x3n' Landau 52 53 @ingroup Pong 54 */ 37 55 class _PongExport PongBot : public Bot 38 56 { -
code/branches/tetris/src/modules/pong/PongScore.cc
r6417 r8107 27 27 */ 28 28 29 /** 30 @file PongScore.cc 31 @brief Implementation of the PongScore class. 32 */ 33 29 34 #include "PongScore.h" 30 35 31 #include "util/Convert.h"32 36 #include "core/CoreIncludes.h" 33 37 #include "core/XMLPort.h" 38 #include "util/Convert.h" 39 40 #include "infos/PlayerInfo.h" 41 34 42 #include "Pong.h" 35 #include "infos/PlayerInfo.h"36 43 37 44 namespace orxonox … … 39 46 CreateFactory(PongScore); 40 47 48 /** 49 @brief 50 Constructor. Registers and initializes the object. 51 */ 41 52 PongScore::PongScore(BaseObject* creator) : OverlayText(creator) 42 53 { … … 51 62 } 52 63 64 /** 65 @brief 66 Destructor. 67 */ 53 68 PongScore::~PongScore() 54 69 { 55 70 } 56 71 72 /** 73 @brief 74 Method to create a PongScore through XML. 75 */ 57 76 void PongScore::XMLPort(Element& xmlelement, XMLPort::Mode mode) 58 77 { … … 65 84 } 66 85 86 /** 87 @brief 88 Is called each tick. 89 Creates and sets the caption to be displayed by the PongScore. 90 @param dt 91 The time that has elapsed since the last tick. 92 */ 67 93 void PongScore::tick(float dt) 68 94 { 69 95 SUPER(PongScore, tick, dt); 70 96 71 if (this->owner_) 97 // If the owner is set. The owner being a Pong game. 98 if (this->owner_ != NULL) 72 99 { 100 // Get the two players. 73 101 PlayerInfo* player1 = this->owner_->getLeftPlayer(); 74 102 PlayerInfo* player2 = this->owner_->getRightPlayer(); … … 80 108 std::string score2("0"); 81 109 82 if (player1) 110 // Save the name and score of each player as a string. 111 if (player1 != NULL) 83 112 { 84 113 name1 = player1->getName(); 85 114 score1 = multi_cast<std::string>(this->owner_->getScore(player1)); 86 115 } 87 88 if (player2) 116 if (player2 != NULL) 89 117 { 90 118 name2 = player2->getName(); … … 92 120 } 93 121 122 // Assemble the strings, depending on what should all be displayed. 94 123 std::string output1; 95 124 if (this->bShowLeftPlayer_) 96 125 { 97 if (this->bShowName_ && this->bShowScore_ && player1 )126 if (this->bShowName_ && this->bShowScore_ && player1 != NULL) 98 127 output1 = name1 + " - " + score1; 99 128 else if (this->bShowScore_) … … 106 135 if (this->bShowRightPlayer_) 107 136 { 108 if (this->bShowName_ && this->bShowScore_ && player2 )137 if (this->bShowName_ && this->bShowScore_ && player2 != NULL) 109 138 output2 = score2 + " - " + name2; 110 139 else if (this->bShowScore_) … … 127 156 } 128 157 129 158 /** 159 @brief 160 Is called when the owner changes. 161 Sets the owner to NULL, if it is not a pointer to a Pong game. 162 */ 130 163 void PongScore::changedOwner() 131 164 { 132 165 SUPER(PongScore, changedOwner); 133 166 134 if (this->getOwner() && this->getOwner()->getGametype())167 if (this->getOwner() != NULL && this->getOwner()->getGametype()) 135 168 this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype().get()); 136 169 else -
code/branches/tetris/src/modules/pong/PongScore.h
r8106 r8107 39 39 40 40 #include "tools/interfaces/Tickable.h" 41 41 42 #include "overlays/OverlayText.h" 42 43 … … 45 46 46 47 /** 47 48 @brief 49 The PongScore class displays the score for a game of @ref orxonox::Pong "Pong". 50 51 @author 52 Fabian 'x3n' Landau 53 54 @ingroup Pong 48 55 */ 49 56 class _PongExport PongScore : public OverlayText, public Tickable … … 53 60 virtual ~PongScore(); 54 61 55 virtual void tick(float dt); 62 virtual void tick(float dt); //!< Creates and sets the caption to be displayed by the PongScore. 56 63 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 57 virtual void changedOwner(); 64 virtual void changedOwner(); //!< Is called when the owner changes. 58 65 66 /** 67 @brief Set whether the PongScore displays the players' names. 68 @param value If true the players' names are displayed. 69 */ 59 70 inline void setShowName(bool value) 60 71 { this->bShowName_ = value; } 72 /** 73 @brief Get whether the PongScore displays the players' names. 74 @return Returns true if the players' names are displayed, false otherwise. 75 */ 61 76 inline bool getShowName() const 62 77 { return this->bShowName_; } 63 78 79 /** 80 @brief Set whether the PongScore displays the players' scores. 81 @param value If true the players' scores are displayed. 82 */ 64 83 inline void setShowScore(bool value) 65 84 { this->bShowScore_ = value; } 85 /** 86 @brief Get whether the PongScore displays the players' scores. 87 @return Returns true if the players' scores are displayed, false otherwise. 88 */ 66 89 inline bool getShowScore() const 67 90 { return this->bShowScore_; } 68 91 92 /** 93 @brief Set whether the PongScore displays the left player. 94 @param value If true the left player is displayed. 95 */ 69 96 inline void setShowLeftPlayer(bool value) 70 97 { this->bShowLeftPlayer_ = value; } 98 /** 99 @brief Get whether the PongScore displays the left player. 100 @return Returns true if the left player is displayed, false otherwise. 101 */ 71 102 inline bool getShowLeftPlayer() const 72 103 { return this->bShowLeftPlayer_; } 73 104 105 /** 106 @brief Set whether the PongScore displays the right player. 107 @param value If true the right player is displayed. 108 */ 74 109 inline void setShowRightPlayer(bool value) 75 110 { this->bShowRightPlayer_ = value; } 111 /** 112 @brief Get whether the PongScore displays the right player. 113 @return Returns true if the right player is displayed, false otherwise. 114 */ 76 115 inline bool getShowRightPlayer() const 77 116 { return this->bShowRightPlayer_; } 78 117 79 118 private: 80 Pong* owner_; 81 bool bShowName_; 82 bool bShowScore_; 83 bool bShowLeftPlayer_; 84 bool bShowRightPlayer_; 119 Pong* owner_; //!< The Pong game that owns this PongScore. 120 bool bShowName_; //!< Whether the names of the players are shown. 121 bool bShowScore_; //!< Whether the score of the players is shown. 122 bool bShowLeftPlayer_; //!< Whether the left player is shown. 123 bool bShowRightPlayer_; //!< Whether the right player is shown. 85 124 }; 86 125 }
Note: See TracChangeset
for help on using the changeset viewer.