[2839] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[8108] | 29 | /** |
---|
| 30 | @file PongAI.cc |
---|
| 31 | @brief Implementation of the PongAI class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2839] | 34 | #include "PongAI.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
[2857] | 37 | #include "core/ConfigValueIncludes.h" |
---|
[3196] | 38 | #include "tools/Timer.h" |
---|
[8108] | 39 | |
---|
[5735] | 40 | #include "worldentities/ControllableEntity.h" |
---|
[8108] | 41 | |
---|
[5725] | 42 | #include "PongBall.h" |
---|
[2839] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | CreateUnloadableFactory(PongAI); |
---|
| 47 | |
---|
[3196] | 48 | const static float MAX_REACTION_TIME = 0.4f; |
---|
[2860] | 49 | |
---|
[8108] | 50 | /** |
---|
| 51 | @brief |
---|
| 52 | Constructor. Registers and initializes the object. |
---|
| 53 | */ |
---|
[2839] | 54 | PongAI::PongAI(BaseObject* creator) : Controller(creator) |
---|
| 55 | { |
---|
| 56 | RegisterObject(PongAI); |
---|
| 57 | |
---|
| 58 | this->ball_ = 0; |
---|
[2860] | 59 | this->ballDirection_ = Vector2::ZERO; |
---|
| 60 | this->ballEndPosition_ = 0; |
---|
[2839] | 61 | this->randomOffset_ = 0; |
---|
[5929] | 62 | this->bChangedRandomOffset_ = false; |
---|
[3196] | 63 | this->relHysteresisOffset_ = 0.02f; |
---|
| 64 | this->strength_ = 0.5f; |
---|
[2860] | 65 | this->movement_ = 0; |
---|
[2885] | 66 | this->oldMove_ = 0; |
---|
| 67 | this->bOscillationAvoidanceActive_ = false; |
---|
[2857] | 68 | |
---|
| 69 | this->setConfigValues(); |
---|
[2839] | 70 | } |
---|
| 71 | |
---|
[8108] | 72 | /** |
---|
| 73 | @brief |
---|
| 74 | Destructor. Cleans up the list fo reaction timers. |
---|
| 75 | */ |
---|
[2860] | 76 | PongAI::~PongAI() |
---|
| 77 | { |
---|
[5929] | 78 | for (std::list<std::pair<Timer*, char> >::iterator it = this->reactionTimers_.begin(); it != this->reactionTimers_.end(); ++it) |
---|
[6417] | 79 | it->first->destroy(); |
---|
[2860] | 80 | } |
---|
| 81 | |
---|
[8108] | 82 | /** |
---|
| 83 | @brief |
---|
| 84 | Sets config values. |
---|
| 85 | */ |
---|
[2857] | 86 | void PongAI::setConfigValues() |
---|
| 87 | { |
---|
[8108] | 88 | // Sets the strength of the PongAi as a config value. |
---|
[2857] | 89 | SetConfigValue(strength_, 0.5).description("A value from 0 to 1 where 0 is weak and 1 is strong."); |
---|
| 90 | } |
---|
| 91 | |
---|
[8108] | 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 | */ |
---|
[2839] | 99 | void PongAI::tick(float dt) |
---|
| 100 | { |
---|
[8108] | 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) |
---|
[2839] | 103 | return; |
---|
| 104 | |
---|
[2860] | 105 | Vector3 mypos = this->getControllableEntity()->getPosition(); |
---|
[2839] | 106 | Vector3 ballpos = this->ball_->getPosition(); |
---|
| 107 | Vector3 ballvel = this->ball_->getVelocity(); |
---|
| 108 | float hysteresisOffset = this->relHysteresisOffset_ * this->ball_->getFieldDimension().y; |
---|
| 109 | |
---|
[2860] | 110 | char move = 0; |
---|
[2885] | 111 | bool delay = false; |
---|
[2860] | 112 | |
---|
[2839] | 113 | // Check in which direction the ball is flying |
---|
| 114 | if ((mypos.x > 0 && ballvel.x < 0) || (mypos.x < 0 && ballvel.x > 0)) |
---|
| 115 | { |
---|
[2872] | 116 | // The ball is flying away |
---|
[2860] | 117 | this->ballDirection_.x = -1; |
---|
| 118 | this->ballDirection_.y = 0; |
---|
[2885] | 119 | this->bOscillationAvoidanceActive_ = false; |
---|
[2839] | 120 | |
---|
[2872] | 121 | // Move to the middle |
---|
[2839] | 122 | if (mypos.z > hysteresisOffset) |
---|
[2860] | 123 | move = 1; |
---|
[2839] | 124 | else if (mypos.z < -hysteresisOffset) |
---|
[2860] | 125 | move = -1; |
---|
[2839] | 126 | } |
---|
| 127 | else if (ballvel.x == 0) |
---|
| 128 | { |
---|
[2872] | 129 | // The ball is standing still |
---|
[2860] | 130 | this->ballDirection_.x = 0; |
---|
| 131 | this->ballDirection_.y = 0; |
---|
[2885] | 132 | this->bOscillationAvoidanceActive_ = false; |
---|
[2839] | 133 | } |
---|
| 134 | else |
---|
| 135 | { |
---|
[2872] | 136 | // The ball is approaching |
---|
[2860] | 137 | if (this->ballDirection_.x != 1) |
---|
| 138 | { |
---|
[8108] | 139 | // The ball just started to approach, initialize all values |
---|
[2860] | 140 | this->ballDirection_.x = 1; |
---|
| 141 | this->ballDirection_.y = sgn(ballvel.z); |
---|
| 142 | this->ballEndPosition_ = 0; |
---|
| 143 | this->randomOffset_ = 0; |
---|
[5929] | 144 | this->bChangedRandomOffset_ = false; |
---|
[2839] | 145 | |
---|
[2860] | 146 | this->calculateRandomOffset(); |
---|
| 147 | this->calculateBallEndPosition(); |
---|
[2885] | 148 | delay = true; |
---|
| 149 | this->bOscillationAvoidanceActive_ = false; |
---|
[2860] | 150 | } |
---|
| 151 | |
---|
| 152 | if (this->ballDirection_.y != sgn(ballvel.z)) |
---|
| 153 | { |
---|
[2872] | 154 | // The ball just bounced from a bound, recalculate the predicted end position |
---|
[2860] | 155 | this->ballDirection_.y = sgn(ballvel.z); |
---|
| 156 | |
---|
| 157 | this->calculateBallEndPosition(); |
---|
[2885] | 158 | delay = true; |
---|
| 159 | this->bOscillationAvoidanceActive_ = false; |
---|
[2860] | 160 | } |
---|
[6417] | 161 | |
---|
[5929] | 162 | // If the ball is close enough, calculate another random offset to accelerate the ball |
---|
| 163 | if (!this->bChangedRandomOffset_) |
---|
| 164 | { |
---|
| 165 | float timetohit = (-this->ball_->getPosition().x + this->ball_->getFieldDimension().x / 2 * sgn(this->ball_->getVelocity().x)) / this->ball_->getVelocity().x; |
---|
| 166 | if (timetohit < 0.05) |
---|
| 167 | { |
---|
| 168 | this->bChangedRandomOffset_ = true; |
---|
| 169 | if (rnd() < this->strength_) |
---|
| 170 | this->calculateRandomOffset(); |
---|
| 171 | } |
---|
| 172 | } |
---|
[2860] | 173 | |
---|
[2872] | 174 | // Move to the predicted end position with an additional offset (to hit the ball with the side of the bat) |
---|
[2885] | 175 | if (!this->bOscillationAvoidanceActive_) |
---|
| 176 | { |
---|
| 177 | float desiredZValue = this->ballEndPosition_ + this->randomOffset_; |
---|
[2860] | 178 | |
---|
[2885] | 179 | if (mypos.z > desiredZValue + hysteresisOffset * (this->randomOffset_ < 0)) |
---|
| 180 | move = 1; |
---|
| 181 | else if (mypos.z < desiredZValue - hysteresisOffset * (this->randomOffset_ > 0)) |
---|
| 182 | move = -1; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if (move != 0 && this->oldMove_ != 0 && move != this->oldMove_ && !delay) |
---|
| 186 | { |
---|
| 187 | // We had to correct our position because we moved too far |
---|
[8108] | 188 | // (and delay is false, so we're not in the wrong place because of a new end-position prediction) |
---|
[2885] | 189 | if (fabs(mypos.z - this->ballEndPosition_) < 0.5 * this->ball_->getBatLength() * this->ball_->getFieldDimension().y) |
---|
| 190 | { |
---|
| 191 | // We're not at the right position, but we still hit the ball, so just stay there to avoid oscillation |
---|
| 192 | move = 0; |
---|
| 193 | this->bOscillationAvoidanceActive_ = true; |
---|
| 194 | } |
---|
| 195 | } |
---|
[2839] | 196 | } |
---|
[2860] | 197 | |
---|
[2885] | 198 | this->oldMove_ = move; |
---|
| 199 | this->move(move, delay); |
---|
[2860] | 200 | this->getControllableEntity()->moveFrontBack(this->movement_); |
---|
[2839] | 201 | } |
---|
| 202 | |
---|
[8108] | 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 | */ |
---|
[2839] | 209 | void PongAI::calculateRandomOffset() |
---|
| 210 | { |
---|
[2857] | 211 | // Calculate the exponent for the position-formula |
---|
| 212 | float exp = pow(10, 1 - 2*this->strength_); // strength: 0 -> exp = 10 |
---|
| 213 | // strength: 0.5 -> exp = 1 |
---|
| 214 | // strength: 1 -> exp = 0.1 |
---|
| 215 | |
---|
| 216 | // Calculate the relative position where to hit the ball with the bat |
---|
| 217 | float position = pow(rnd(), exp); // exp > 1 -> position is more likely a small number |
---|
| 218 | // exp < 1 -> position is more likely a large number |
---|
| 219 | |
---|
[8108] | 220 | // The position shouldn't be larger than 0.5 (50% of the bat-length from the middle is the end) |
---|
[3196] | 221 | position *= 0.48f; |
---|
[2857] | 222 | |
---|
| 223 | // Both sides are equally probable |
---|
[2872] | 224 | position *= rndsgn(); |
---|
[2857] | 225 | |
---|
| 226 | // Calculate the offset in world units |
---|
| 227 | this->randomOffset_ = position * this->ball_->getBatLength() * this->ball_->getFieldDimension().y; |
---|
[2839] | 228 | } |
---|
[2860] | 229 | |
---|
[8108] | 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 | */ |
---|
[2860] | 235 | void PongAI::calculateBallEndPosition() |
---|
| 236 | { |
---|
| 237 | Vector3 position = this->ball_->getPosition(); |
---|
| 238 | Vector3 velocity = this->ball_->getVelocity(); |
---|
[5929] | 239 | Vector3 acceleration = this->ball_->getAcceleration(); |
---|
[2860] | 240 | Vector2 dimension = this->ball_->getFieldDimension(); |
---|
| 241 | |
---|
[5929] | 242 | // Calculate bounces. The number of predicted bounces is limited by the AIs strength |
---|
| 243 | for (float limit = -0.05f; limit < this->strength_ || this->strength_ > 0.99f; limit += 0.4f) |
---|
[2860] | 244 | { |
---|
[5929] | 245 | // calculate the time until the ball reaches the other side |
---|
| 246 | float totaltime = (-position.x + dimension.x / 2 * sgn(velocity.x)) / velocity.x; |
---|
[6417] | 247 | |
---|
[5929] | 248 | // calculate wall bounce position (four possible solutions of the equation: pos.z + vel.z*t + acc.z/2*t^2 = +/- dim.z/2) |
---|
| 249 | float bouncetime = totaltime; |
---|
| 250 | bool bUpperWall = false; |
---|
[6417] | 251 | |
---|
[5929] | 252 | if (acceleration.z == 0) |
---|
| 253 | { |
---|
| 254 | if (velocity.z > 0) |
---|
| 255 | { |
---|
| 256 | bUpperWall = true; |
---|
| 257 | bouncetime = (dimension.y/2 - position.z) / velocity.z; |
---|
| 258 | } |
---|
| 259 | else if (velocity.z < 0) |
---|
| 260 | { |
---|
| 261 | bUpperWall = false; |
---|
| 262 | bouncetime = (-dimension.y/2 - position.z) / velocity.z; |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
| 267 | // upper wall |
---|
| 268 | float temp = velocity.z*velocity.z + 2*acceleration.z*(dimension.y/2 - position.z); |
---|
| 269 | if (temp >= 0) |
---|
| 270 | { |
---|
| 271 | float t1 = (sqrt(temp) - velocity.z) / acceleration.z; |
---|
| 272 | float t2 = (sqrt(temp) + velocity.z) / acceleration.z * (-1); |
---|
| 273 | if (t1 > 0 && t1 < bouncetime) |
---|
| 274 | { |
---|
| 275 | bouncetime = t1; |
---|
| 276 | bUpperWall = true; |
---|
| 277 | } |
---|
| 278 | if (t2 > 0 && t2 < bouncetime) |
---|
| 279 | { |
---|
| 280 | bouncetime = t2; |
---|
| 281 | bUpperWall = true; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | // lower wall |
---|
| 285 | temp = velocity.z*velocity.z - 2*acceleration.z*(dimension.y/2 + position.z); |
---|
| 286 | if (temp >= 0) |
---|
| 287 | { |
---|
| 288 | float t1 = (sqrt(temp) - velocity.z) / acceleration.z; |
---|
| 289 | float t2 = (sqrt(temp) + velocity.z) / acceleration.z * (-1); |
---|
| 290 | if (t1 > 0 && t1 < bouncetime) |
---|
| 291 | { |
---|
| 292 | bouncetime = t1; |
---|
| 293 | bUpperWall = false; |
---|
| 294 | } |
---|
| 295 | if (t2 > 0 && t2 < bouncetime) |
---|
| 296 | { |
---|
| 297 | bouncetime = t2; |
---|
| 298 | bUpperWall = false; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | } |
---|
[2885] | 302 | |
---|
[5929] | 303 | if (bouncetime < totaltime) |
---|
[2860] | 304 | { |
---|
[5929] | 305 | // Calculate a random prediction error, based on the vertical speed of the ball and the strength of the AI |
---|
| 306 | float randomErrorX = rnd(-1, 1) * dimension.y * (velocity.z / velocity.x / PongBall::MAX_REL_Z_VELOCITY) * (1 - this->strength_); |
---|
| 307 | float randomErrorZ = rnd(-1, 1) * dimension.y * (velocity.z / velocity.x / PongBall::MAX_REL_Z_VELOCITY) * (1 - this->strength_); |
---|
| 308 | |
---|
| 309 | // ball bounces after <bouncetime> seconds, update the position and continue |
---|
| 310 | velocity.z = velocity.z + acceleration.z * bouncetime; |
---|
[6417] | 311 | |
---|
[5929] | 312 | if (bUpperWall) |
---|
| 313 | { |
---|
| 314 | position.z = dimension.y / 2; |
---|
| 315 | velocity.z = -fabs(velocity.z) + fabs(randomErrorZ); |
---|
| 316 | } |
---|
| 317 | else |
---|
| 318 | { |
---|
| 319 | position.z = -dimension.y / 2; |
---|
| 320 | velocity.z = fabs(velocity.z) - fabs(randomErrorZ); |
---|
| 321 | } |
---|
[6417] | 322 | |
---|
[5929] | 323 | position.x = position.x + velocity.x * bouncetime + randomErrorX; |
---|
| 324 | this->ballEndPosition_ = position.z; |
---|
[2860] | 325 | } |
---|
[5929] | 326 | else |
---|
[2860] | 327 | { |
---|
[5929] | 328 | // ball doesn't bounce, calculate the end position and return |
---|
| 329 | // calculate end-height: current height + slope * distance incl. acceleration |
---|
| 330 | this->ballEndPosition_ = position.z + velocity.z * totaltime + acceleration.z / 2 * totaltime * totaltime; |
---|
| 331 | return; |
---|
[2860] | 332 | } |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
[8108] | 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 | */ |
---|
[2885] | 345 | void PongAI::move(char direction, bool bUseDelay) |
---|
[2860] | 346 | { |
---|
| 347 | // The current direction is either what we're doing right now (movement_) or what is last in the queue |
---|
| 348 | char currentDirection = this->movement_; |
---|
| 349 | if (this->reactionTimers_.size() > 0) |
---|
| 350 | currentDirection = this->reactionTimers_.back().second; |
---|
| 351 | |
---|
| 352 | // Only add changes of direction |
---|
| 353 | if (direction == currentDirection) |
---|
| 354 | return; |
---|
| 355 | |
---|
[2885] | 356 | if (bUseDelay) |
---|
[2860] | 357 | { |
---|
[2885] | 358 | // Calculate delay |
---|
[2860] | 359 | float delay = MAX_REACTION_TIME * (1 - this->strength_); |
---|
| 360 | |
---|
| 361 | // Add a new Timer |
---|
[5929] | 362 | Timer* timer = new Timer(delay, false, createExecutor(createFunctor(&PongAI::delayedMove, this))); |
---|
| 363 | this->reactionTimers_.push_back(std::pair<Timer*, char>(timer, direction)); |
---|
[2860] | 364 | } |
---|
| 365 | else |
---|
| 366 | { |
---|
[2885] | 367 | this->movement_ = direction; |
---|
[2860] | 368 | } |
---|
| 369 | } |
---|
| 370 | |
---|
[8108] | 371 | /** |
---|
| 372 | @brief |
---|
| 373 | Is called, when a delayed move takes effect. |
---|
| 374 | */ |
---|
[2860] | 375 | void PongAI::delayedMove() |
---|
| 376 | { |
---|
[2872] | 377 | // Get the new movement direction from the timer list |
---|
[2860] | 378 | this->movement_ = this->reactionTimers_.front().second; |
---|
| 379 | |
---|
[2872] | 380 | // Destroy the timer and remove it from the list |
---|
[5929] | 381 | Timer* timer = this->reactionTimers_.front().first; |
---|
| 382 | timer->destroy(); |
---|
[2860] | 383 | |
---|
| 384 | this->reactionTimers_.pop_front(); |
---|
| 385 | } |
---|
[2839] | 386 | } |
---|