Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/RacingBots_FS18/src/modules/gametypes/SpaceRaceController.cc @ 11913

Last change on this file since 11913 was 11913, checked in by arismu, 6 years ago

pickups, position of checkpoints are fixed

  • Property svn:eol-style set to native
File size: 13.2 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 *  Created on: Oct 8, 2012findCheck
23 *      Author: purgham
24 */
25
26#include <gametypes/SpaceRaceController.h>
27#include "core/CoreIncludes.h"
28#include "core/XMLPort.h"
29#include "gametypes/SpaceRaceManager.h"
30#include "collisionshapes/CollisionShape.h"
31#include "BulletCollision/CollisionShapes/btCollisionShape.h"
32
33
34namespace orxonox
35{
36    RegisterClass(SpaceRaceController);
37
38    const int ADJUSTDISTANCE = 500;
39    const int MINDISTANCE = 5;
40    /*
41     * Idea: Find static Point (checkpoints the spaceship has to reach)
42     */
43    SpaceRaceController::SpaceRaceController(Context* context) :
44        ArtificialController(context)
45    {
46        RegisterObject(SpaceRaceController);
47        std::vector<RaceCheckPoint*> checkpoints;
48
49        virtualCheckPointIndex = -2;
50        if (ObjectList<SpaceRaceManager>().size() != 1)
51            orxout(internal_warning) << "Expected 1 instance of SpaceRaceManager but found " << ObjectList<SpaceRaceManager>().size() << endl;
52        for (SpaceRaceManager* manager : ObjectList<SpaceRaceManager>())
53        {
54            checkpoints = manager->getAllCheckpoints();
55            nextRaceCheckpoint_ = manager->findCheckpoint(0);
56        }
57
58        OrxAssert(!checkpoints.empty(), "No Checkpoints in Level");
59        checkpoints_ = checkpoints;
60        staticRacePoints_ = findStaticCheckpoints(nextRaceCheckpoint_, checkpoints);
61        // initialisation of currentRaceCheckpoint_
62        currentRaceCheckpoint_ = nullptr;
63
64        int i;
65        for (i = -2; findCheckpoint(i) != nullptr; i--)     // WIESO?
66        {
67            continue;
68        }
69
70    }
71
72    //------------------------------
73    // functions for initialisation
74
75    void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
76    {
77        SUPER(SpaceRaceController, XMLPort, xmlelement, mode);
78        XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f);
79        XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode);
80    }
81
82    /*
83     * called from constructor 'SpaceRaceController'
84     * returns a vector of static Point (checkpoints the spaceship has to reach)
85     */
86    std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(RaceCheckPoint* currentCheckpoint, const std::vector<RaceCheckPoint*>& allCheckpoints)
87    {
88        std::map<RaceCheckPoint*, int> zaehler; // counts how many times the checkpoint was reached (for simulation)
89        for (RaceCheckPoint* checkpoint : allCheckpoints)
90        {
91            zaehler.insert(std::pair<RaceCheckPoint*, int>(checkpoint,0));
92        }
93        int maxWays = rekSimulationCheckpointsReached(currentCheckpoint, zaehler);
94
95        std::vector<RaceCheckPoint*> returnVec;
96        for (const auto& mapEntry : zaehler)
97        {
98            if (mapEntry.second == maxWays)
99            {
100                returnVec.push_back(mapEntry.first);
101            }
102        }
103        return returnVec;
104    }
105
106    /*
107     * called from 'findStaticCheckpoints'
108     * return how many ways go from the given Checkpoint to the last Checkpoint (of the Game)
109     */
110    int SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::map<RaceCheckPoint*, int>& zaehler)
111    {
112
113        if (currentCheckpoint->isLast())
114        {// last point reached
115
116            zaehler[currentCheckpoint] += 1;
117            return 1; // 1 Way form the last point to this one
118        }
119        else
120        {
121            int numberOfWays = 0; // counts number of ways from this Point to the last point
122            for (int checkpointIndex : currentCheckpoint->getNextCheckpoints())
123            {
124                if (currentCheckpoint == findCheckpoint(checkpointIndex))
125                {
126                    orxout() << currentCheckpoint->getCheckpointIndex()<<endl;
127                    continue;
128                }
129                if (findCheckpoint(checkpointIndex) == nullptr){
130                    orxout(internal_warning) << "Problematic Point: " << checkpointIndex << endl;
131                }
132                else
133                    numberOfWays += rekSimulationCheckpointsReached(findCheckpoint(checkpointIndex), zaehler);
134
135            }
136            zaehler[currentCheckpoint] += numberOfWays;
137            return numberOfWays; // returns the number of ways from this point to the last one
138        }
139    }
140
141    //-------------------------------------
142    // functions for dynamic Way-search
143
144    float SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint)
145    {
146        if (this->getControllableEntity() != nullptr)
147        {
148            return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length();
149        }
150        return -1;
151    }
152
153    /*
154     * called by: 'tick' or  'adjustNextPoint'
155     * returns the next Checkpoint which the shortest way contains
156     */
157    RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
158    {
159        float minDistance = 0;
160        RaceCheckPoint* minNextRaceCheckPoint = nullptr;
161
162        // find the next checkpoint with the minimal distance
163        for (int checkpointIndex : raceCheckpoint->getNextCheckpoints())
164        {
165            RaceCheckPoint* nextRaceCheckPoint = findCheckpoint(checkpointIndex);
166            float distance = recCalculateDistance(nextRaceCheckPoint, this->getControllableEntity()->getPosition());
167
168            if (distance < minDistance || minNextRaceCheckPoint == nullptr)
169            {
170                minDistance = distance;
171                minNextRaceCheckPoint = nextRaceCheckPoint;
172            }
173
174        }
175        if(minNextRaceCheckPoint==nullptr){orxout()<<"minNextRaceCheckPoint=nullpointer line 175 SpaceRaceController index: "<<endl;}
176        return minNextRaceCheckPoint;
177    }
178
179    /*
180     * called from 'nextPointFind'
181     * returns the distance between "currentPosition" and the next static checkpoint that can be reached from "currentCheckPoint"
182     */
183    float SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, const Vector3& currentPosition)
184    {
185        // find: looks if the currentCheckPoint is a staticCheckPoint (staticCheckPoint is the same as: static Point)
186        if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(), currentCheckPoint) != staticRacePoints_.end())
187        {
188            return (currentCheckPoint->getPosition() - currentPosition).length();
189        }
190        else
191        {
192            float minimum = std::numeric_limits<float>::max();
193            for (int checkpointIndex : currentCheckPoint->getNextCheckpoints())
194            {
195                int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length());
196
197                minimum = std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(checkpointIndex), currentCheckPoint->getPosition()));
198                // minimum of distanz from 'currentPosition' to the next static Checkpoint
199            }
200            return minimum;
201        }
202    }
203
204    /*called by 'tick'
205     *adjust chosen way of the Spaceship every "AdjustDistance" because spaceship could be displaced through an other one
206     */
207    RaceCheckPoint* SpaceRaceController::adjustNextPoint()
208    {
209        if (currentRaceCheckpoint_ == nullptr) // no Adjust possible
210
211        {
212            return nextRaceCheckpoint_;
213        }
214        if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible
215
216        {
217            return nextRaceCheckpoint_;
218        }
219
220        //Adjust possible
221
222        return nextPointFind(currentRaceCheckpoint_);
223    }
224
225    RaceCheckPoint* SpaceRaceController::findCheckpoint(int index) const
226    {
227        for (RaceCheckPoint* checkpoint : this->checkpoints_){
228            if (checkpoint->getCheckpointIndex() == index)
229                return checkpoint;
230
231        }
232
233        orxout()<<"returned nullptr @line 231 SpaceRaceController"<<endl;
234        return nullptr;
235    }
236
237    SpaceRaceController::~SpaceRaceController()
238    {
239        if (this->isInitialized())
240        {
241            for (int i =-1; i>virtualCheckPointIndex; i--)
242                delete findCheckpoint(i);
243        }
244    }
245
246    void SpaceRaceController::tick(float dt)
247    {
248        if (this->getControllableEntity() == nullptr || this->getControllableEntity()->getPlayer() == nullptr )
249        {
250            //orxout()<< this->getControllableEntity() << " in tick"<<endl;
251            return;
252        }
253        //FOR virtual Checkpoints
254        if(nextRaceCheckpoint_->getCheckpointIndex() < 0)
255        {
256            if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 200)
257            {
258                currentRaceCheckpoint_=nextRaceCheckpoint_;
259                nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
260                lastPositionSpaceship=this->getControllableEntity()->getPosition();
261                //orxout()<< "CP "<< currentRaceCheckpoint_->getCheckpointIndex()<<" chanched to: "<< nextRaceCheckpoint_->getCheckpointIndex()<<endl;
262            }
263        }
264
265        if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer()))
266        {//Checkpoint erreicht
267
268            currentRaceCheckpoint_ = nextRaceCheckpoint_;
269            OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined");
270            nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
271            lastPositionSpaceship = this->getControllableEntity()->getPosition();
272            //orxout()<< "CP "<< currentRaceCheckpoint_->getCheckpointIndex()<<" chanched to: "<< nextRaceCheckpoint_->getCheckpointIndex()<<endl;
273        }
274
275        else if ((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt > ADJUSTDISTANCE)
276        {
277            nextRaceCheckpoint_ = adjustNextPoint();
278            lastPositionSpaceship = this->getControllableEntity()->getPosition();
279        }
280
281        // Abmessung fuer MINDISTANCE gut;
282
283        else if((lastPositionSpaceship - this->getControllableEntity()->getPosition()).length()/dt < MINDISTANCE )
284        {
285            this->moveToPosition(Vector3(rnd()*100, rnd()*100, rnd()*100));
286            this->spin();
287            orxout(user_status) << "Mindistance reached" << std::endl;
288            return;
289        }
290        //orxout(user_status) << "dt= " << dt << ";  distance= " << (lastPositionSpaceship-this->getControllableEntity()->getPosition()).length() <<std::endl;
291        lastPositionSpaceship = this->getControllableEntity()->getPosition();
292       
293        this->boostControl();
294        if (nextRaceCheckpoint_ == nullptr) orxout() << "nextRaceCheckpoint_ equals to nullpointer look @line 334 SpaceRaceController.cc" << endl;
295        this->moveToPosition(nextRaceCheckpoint_->getPosition());
296        this->boostControl();
297    }
298
299    // True if a coordinate of 'pointToPoint' is smaller then the corresponding coordinate of 'groesse'
300    bool SpaceRaceController::vergleicheQuader(const Vector3& pointToPoint, const Vector3& groesse)
301    {
302        if(std::abs(pointToPoint.x) < groesse.x)
303            return true;
304        if(std::abs(pointToPoint.y) < groesse.y)
305            return true;
306        if(std::abs(pointToPoint.z) < groesse.z)
307            return true;
308        return false;
309
310    }
311
312    bool SpaceRaceController::directLinePossible(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2, const std::vector<StaticEntity*>& allObjects)
313    {
314
315        Vector3 cP1ToCP2 = (racepoint2->getPosition() - racepoint1->getPosition()) / (racepoint2->getPosition() - racepoint1->getPosition()).length(); //unit Vector
316        Vector3 centerCP1 = racepoint1->getPosition();
317        btVector3 positionObject;
318        btScalar radiusObject;
319
320        for (StaticEntity* object : allObjects)
321        {
322            for (int everyShape=0; object->getAttachedCollisionShape(everyShape) != nullptr; everyShape++)
323            {
324                btCollisionShape* currentShape = object->getAttachedCollisionShape(everyShape)->getCollisionShape();
325                if(currentShape == nullptr)
326                continue;
327
328                currentShape->getBoundingSphere(positionObject,radiusObject);
329                Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
330                if((powf((cP1ToCP2.dotProduct(centerCP1-positionObjectNonBT)),2)-(centerCP1-positionObjectNonBT).dotProduct(centerCP1-positionObjectNonBT)+powf(radiusObject, 2))>0)
331                {
332                    return false;
333                }
334
335            }
336        }
337        return true;
338
339    }
340}
Note: See TracBrowser for help on using the repository browser.