Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc @ 9470

Last change on this file since 9470 was 9470, checked in by purgham, 12 years ago

26.11.12 not really working but usefull stuff for later

File size: 13.8 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, 2012
23 *      Author: purgham
24 */
25
26/**
27 * Conventions:
28 * -first Checkpoint has index 0
29 * -staticCheckPoint= static Point (see def over = constructor)
30 */
31
32
33/*TODO:
34 * tICK KORRIGIEREN
35 *
36 *
37 */
38#include <gametypes/SpaceRaceController.h>
39#include "core/CoreIncludes.h"
40#include "core/XMLPort.h"
41#include "gametypes/SpaceRaceManager.h"
42
43namespace orxonox
44{
45    CreateFactory(SpaceRaceController);
46
47    const int ADJUSTDISTANCE = 500;
48    const int MINDISTANCE=5;
49    /*
50     * Idea: Find static Point (checkpoints the spaceship has to reach)
51     */
52    SpaceRaceController::SpaceRaceController(BaseObject* creator) :
53        ArtificialController(creator)
54    {
55        RegisterObject(SpaceRaceController)
56;        std::vector<RaceCheckPoint*> checkpoints;
57        for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it)
58        {
59            checkpoints = it->getAllCheckpoints();
60            nextRaceCheckpoint_=it->findCheckpoint(0);
61        }
62
63        OrxAssert(!checkpoints.empty(), "No Checkpoints in Level");
64        checkpoints_=checkpoints;
65        staticRacePoints_ = findStaticCheckpoints(checkpoints);
66        // initialisation of currentRaceCheckpoint_
67        currentRaceCheckpoint_ = NULL;
68        /*
69         // find first Checkpoint
70         for (int i=0; true; i++){
71         if(checkpoints_[i]->getCheckpointIndex()==0){
72         nextRaceCheckpoint_=checkpoints_[i];
73         break;
74         }
75         }*/
76
77        virtualCheckPointIndex=-1;
78    }
79
80
81    //------------------------------
82    // functions for initialisation
83
84    void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
85    {
86        SUPER(SpaceRaceController, XMLPort, xmlelement, mode);
87        XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f);
88        XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode);
89
90    }
91
92    /*
93     * called from constructor 'SpaceRaceController'
94     * returns a vector of static Point (checkpoints the spaceship has to reach)
95     */
96    std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(std::vector<RaceCheckPoint*> allCheckpoints)
97    {
98        std::map<RaceCheckPoint*, int> * zaehler = new std::map<
99        RaceCheckPoint*, int>(); // counts how many times the checkpoit was reached (for simulation)
100        for (unsigned int i = 0; i < allCheckpoints.size(); i++)
101        {
102            zaehler->insert(std::pair<RaceCheckPoint*, int>(allCheckpoints[i],0));
103        }
104        int maxWays = rekSimulationCheckpointsReached(zaehler->begin()->first, zaehler);
105
106        std::vector<RaceCheckPoint*> returnVec;
107        returnVec.clear();
108        for (std::map<RaceCheckPoint*, int>::iterator iter = zaehler->begin(); iter!= zaehler->end(); iter++)
109        {
110            if (iter->second == maxWays)
111            {
112                //returnVec.insert(allCheckpoints[1]);
113                returnVec.insert(returnVec.end(), iter->first);
114            }
115        }
116        delete zaehler;
117        return returnVec;
118    }
119
120    /*
121     * called from 'findStaticCheckpoints'
122     * return how many ways go from the given Checkpoint to the last Checkpoint (of the Game)
123     */
124    int SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::map<RaceCheckPoint*, int>* zaehler)
125    {
126        if (currentCheckpoint->isLast())
127        {// last point reached
128            (*zaehler)[currentCheckpoint] += 1;
129            return 1; // 1 Way form the last point to this one
130        }
131        else
132        {
133            int numberOfWays = 0; // counts number of ways from this Point to the last point
134            for (std::set<int>::iterator it = currentCheckpoint->getNextCheckpoints().begin(); it!= currentCheckpoint->getNextCheckpoints().end(); ++it)
135            {
136                numberOfWays += rekSimulationCheckpointsReached(findCheckpoint(*it), zaehler);
137            }
138            (*zaehler)[currentCheckpoint] += numberOfWays;
139            return numberOfWays; // returns the number of ways from this point to the last one
140        }
141    }
142
143
144
145    //-------------------------------------
146    // functions for dynamic Way-search
147
148    int SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint)
149    {
150        if (this->getControllableEntity() != NULL)
151        {
152            return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length();
153        }
154        return -1;
155    }
156
157    /*
158     * called by: 'tick' or  'adjustNextPoint'
159     * returns the next Checkpoint which the shortest way contains
160     */
161    RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
162    {
163        int distances[] = {   -1, -1, -1};
164        int temp_i = 0;
165        for (std::set<int>::iterator it =raceCheckpoint->getNextCheckpoints().begin(); it!= raceCheckpoint->getNextCheckpoints().end(); ++it)
166        {
167            distances[temp_i] = recCalculateDistance(findCheckpoint(*it), this->getControllableEntity()->getPosition());
168            temp_i++;
169        }
170        if (distances[0] > distances[1] && distances[1] != -1)
171        {
172            if (distances[2] < distances[1] && distances[2] != -1)
173            {
174                return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return checkpoint with ID of raceCheckpoint->getNextCheckpoints() [2]
175            }
176            else
177            {
178                std::set<int>::iterator temp = raceCheckpoint->getNextCheckpoints().begin();
179                return findCheckpoint(*(++temp)); // return [1]
180            }
181        }
182        else
183        {
184            if (distances[2] < distances[0] && distances[2] != -1)
185            {
186                return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return [2]
187            }
188            else
189            {
190                return findCheckpoint(*raceCheckpoint->getNextCheckpoints().begin()); // return [0]
191            }
192        }
193    }
194
195    /*
196     * called from 'nextPointFind'
197     * returns the distance between "currentPosition" and the next static checkpoint that can be reached from "currentCheckPoint"
198     */
199    int SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, Vector3 currentPosition)
200    {
201        // find: looks if the currentCheckPoint is a staticCheckPoint (staticCheckPoint is the same as: static Point)
202        if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(), currentCheckPoint) != staticRacePoints_.end())
203        {
204            return (currentCheckPoint->getPosition() - currentPosition).length();
205        }
206        else
207        {
208            int minimum = std::numeric_limits<int>::max();
209            for (std::set<int>::iterator it = currentCheckPoint->getNextCheckpoints().begin(); it!= currentCheckPoint->getNextCheckpoints().end(); ++it)
210            {
211                int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length());
212
213                minimum= std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(*it), currentCheckPoint->getPosition()));
214                // minimum of distanz from 'currentPosition' to the next static Checkpoint
215            }
216            return minimum;
217        }
218    }
219
220    /*called by 'tick'
221     *adjust chosen way of the Spaceship every "AdjustDistance" because spaceship could be displaced through an other one
222     */
223    RaceCheckPoint* SpaceRaceController::adjustNextPoint()
224    {
225        if (currentRaceCheckpoint_ == NULL) // no Adjust possible
226
227        {
228            return nextRaceCheckpoint_;
229        }
230        if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible
231
232        {
233            return nextRaceCheckpoint_;
234        }
235
236        //Adjust possible
237
238        return nextPointFind(currentRaceCheckpoint_);
239    }
240
241    RaceCheckPoint* SpaceRaceController::findCheckpoint(int index) const
242       {
243           for (size_t i = 0; i < this->checkpoints_.size(); ++i)
244           if (this->checkpoints_[i]->getCheckpointIndex() == index)
245           return this->checkpoints_[i];
246           return NULL;
247       }
248
249    bool SpaceRaceController::addVirtualCheckPoint(int positionInNextCheckPoint, RaceCheckPoint* previousCheckpoint, int indexFollowingCheckPoint , Vector3 virtualCheckPointPosition ){
250
251        RaceCheckPoint* newTempRaceCheckPoint = new RaceCheckPoint(this);
252        newTempRaceCheckPoint->setPosition(virtualCheckPointPosition);
253        newTempRaceCheckPoint->setCheckpointIndex(virtualCheckPointIndex);
254        newTempRaceCheckPoint->setLast(false);
255        newTempRaceCheckPoint->setNextCheckpointsAsVector3(Vector3(indexFollowingCheckPoint,-1,-1));
256
257        Vector3 temp = previousCheckpoint->getNextCheckpointsAsVector3();
258        checkpoints_.insert(checkpoints_.end(), newTempRaceCheckPoint);
259        switch(positionInNextCheckPoint){
260            case 0: temp.x=virtualCheckPointIndex; break;
261            case 1: temp.y=virtualCheckPointIndex; break;
262            case 2: temp.z=virtualCheckPointIndex; break;
263        }
264        virtualCheckPointIndex--;
265    }
266
267
268
269    SpaceRaceController::~SpaceRaceController()
270    {
271        for (int i =-1; i>virtualCheckPointIndex ; i--){
272            delete findCheckpoint(i);
273        }
274    }
275
276    void SpaceRaceController::tick(float dt)
277    {
278        if (this->getControllableEntity() == NULL || this->getControllableEntity()->getPlayer() == NULL )
279        {   orxout()<<this->getControllableEntity()<< " in tick"<<endl; return;}
280        //FOR virtual Checkpoints
281        if(nextRaceCheckpoint_->getCheckpointIndex() < 0){
282            if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 30){
283                currentRaceCheckpoint_=nextRaceCheckpoint_;
284                nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
285                lastPositionSpaceship=this->getControllableEntity()->getPosition();
286            }
287        }
288
289        if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer()))
290        {//Checkpoint erreicht
291            currentRaceCheckpoint_=nextRaceCheckpoint_;
292            OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined");
293            nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
294            lastPositionSpaceship=this->getControllableEntity()->getPosition();
295        }
296        else if ((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt > ADJUSTDISTANCE)
297        {
298            nextRaceCheckpoint_ = adjustNextPoint();
299            lastPositionSpaceship=this->getControllableEntity()->getPosition();
300        }
301        //korrigieren! done
302        else if((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt< MINDISTANCE  ){
303            this->moveToPosition(Vector3(rnd()*100,rnd()*100,rnd()*100));
304            this->spin();
305            //orxout(user_status) << "Mindistance reached" << std::endl;
306            return;
307        }
308        //orxout(user_status) << "dt= " << dt << ";  distance= " << (lastPositionSpaceship-this->getControllableEntity()->getPosition()).length() <<std::endl;
309        lastPositionSpaceship=this->getControllableEntity()->getPosition();
310        this->moveToPosition(nextRaceCheckpoint_->getPosition());
311
312
313        ObjectList<StaticEntity>::iterator it = ObjectList<StaticEntity>::begin();
314        /*if ( it->isA(RaceCheckPoint)){
315            orxout(user_status) << "works" << std::endl;
316        }
317        /*
318            for (ObjectList<StaticEntity>::iterator it = ObjectList<StaticEntity>::begin(); it!= ObjectList<StaticEntity>::end(); ++it)
319                {
320                    if ((*it).isA(RaceCheckPoint))
321                    btVector3 temppos;
322                    it->
323                    btScalar temppos;
324                    btCollisionShape* temp= it->getCollisionShape(temppos, temppos);
325                    it->get
326
327                 }
328*/
329    }
330
331    int SpaceRaceController::pointToPointDistance(Vector3 point1, Vector3 point2){
332        for (ObjectList<StaticEntity>::iterator it = ObjectList<StaticEntity>::begin(); it!= ObjectList<StaticEntity>::end(); ++it)
333                        {
334                            if ((*it).isA(RaceCheckPoint)){break;} // does not work jet
335
336                            for (int i=0; it->getAttachedCollisionShape(i)!=0; i++){
337                                btVector3 temppos;
338                                btScalar tempradius;
339                                it->getAttachedCollisionShape(i)->getCollisionShape()->getCollisionShape(temppos,tempradius);
340                                //http://bulletphysics.com/Bullet/BulletFull/btCollisionShape_8cpp_source.html#l00032
341                                //ueber shape moegliche Hindernisse bestimmen
342                            }
343                            it->getAttachedCollisionShape(i);
344
345
346                         }
347        const int DeltaStrecke = 5;
348        for( int i=0; i*DeltaStrecke-5 < (point1-point2).length(); i++){
349            return 0;
350        }
351    }
352}
Note: See TracBrowser for help on using the repository browser.