Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/controllers/PongAI.cc @ 2857

Last change on this file since 2857 was 2857, checked in by landauf, 16 years ago

added configurable PongAI-strength

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
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
29#include "OrxonoxStableHeaders.h"
30#include "PongAI.h"
31
32#include "core/CoreIncludes.h"
33#include "core/ConfigValueIncludes.h"
34#include "objects/worldentities/ControllableEntity.h"
35#include "objects/worldentities/PongBall.h"
36
37namespace orxonox
38{
39    CreateUnloadableFactory(PongAI);
40
41    PongAI::PongAI(BaseObject* creator) : Controller(creator)
42    {
43        RegisterObject(PongAI);
44
45        this->ball_ = 0;
46        this->randomOffset_ = 0;
47        this->relHysteresisOffset_ = 0.02;
48        this->strength_ = 0.5;
49
50        this->setConfigValues();
51    }
52
53    void PongAI::setConfigValues()
54    {
55        SetConfigValue(strength_, 0.5).description("A value from 0 to 1 where 0 is weak and 1 is strong.");
56    }
57
58    void PongAI::tick(float dt)
59    {
60        if (!this->ball_ || !this->getControllableEntity())
61            return;
62
63        ControllableEntity* bat = this->getControllableEntity();
64
65        Vector3 mypos = bat->getPosition();
66        Vector3 ballpos = this->ball_->getPosition();
67        Vector3 ballvel = this->ball_->getVelocity();
68        float hysteresisOffset = this->relHysteresisOffset_ * this->ball_->getFieldDimension().y;
69
70        // Check in which direction the ball is flying
71        if ((mypos.x > 0 && ballvel.x < 0) || (mypos.x < 0 && ballvel.x > 0))
72        {
73            // Ball is flying away
74            this->calculateRandomOffset();
75
76            if (mypos.z > hysteresisOffset)
77                bat->moveFrontBack(1);
78            else if (mypos.z < -hysteresisOffset)
79                bat->moveFrontBack(-1);
80        }
81        else if (ballvel.x == 0)
82        {
83            // Ball is standing still
84            this->calculateRandomOffset();
85        }
86        else
87        {
88            // Ball is approaching
89            float desiredZValue = ballpos.z + this->randomOffset_;
90
91            if (mypos.z > desiredZValue + hysteresisOffset)
92                bat->moveFrontBack(1);
93            else if (mypos.z < desiredZValue - hysteresisOffset)
94                bat->moveFrontBack(-1);
95        }
96    }
97
98    void PongAI::calculateRandomOffset()
99    {
100        // Calculate the exponent for the position-formula
101        float exp = pow(10, 1 - 2*this->strength_); // strength: 0   -> exp = 10
102                                                    // strength: 0.5 -> exp = 1
103                                                    // strength: 1   -> exp = 0.1
104
105        // Calculate the relative position where to hit the ball with the bat
106        float position = pow(rnd(), exp); // exp > 1 -> position is more likely a small number
107                                          // exp < 1 -> position is more likely a large number
108
109        // The position shouln't be larger than 0.5 (50% of the bat-length from the middle is the end)
110        position *= 0.45;
111
112        // Both sides are equally probable
113        position *= sgn(rnd(-1,1));
114
115        // Calculate the offset in world units
116        this->randomOffset_ = position * this->ball_->getBatLength() * this->ball_->getFieldDimension().y;
117    }
118}
Note: See TracBrowser for help on using the repository browser.