Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9432 was 9432, checked in by purgham, 11 years ago

first working Version

File size: 8.1 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#include <gametypes/SpaceRaceController.h>
27#include "core/CoreIncludes.h"
28#include "core/XMLPort.h"
29#include "gametypes/SpaceRaceManager.h"
30
31// Von SpaceRaceManager points einlesen
32// Berechnungsklasse nextPoint zur verfuegung stellen
33// ^- aufrufen ueber tick (if ob noetig)
34namespace orxonox
35{
36    CreateFactory(SpaceRaceController);
37
38    /*
39     * Idea: Find static Point (checkpoints the spaceship has to reach)
40     */
41    SpaceRaceController::SpaceRaceController(BaseObject* creator) :
42        ArtificialController(creator)
43    {
44        RegisterObject(SpaceRaceController);
45
46        std::vector<RaceCheckPoint*> checkpoints;
47        for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it)
48        {
49            checkpoints = it->getAllCheckpoints();
50            nextRaceCheckpoint_=it->findCheckpoint(1);
51        }
52
53        OrxAssert(!checkpoints.empty(), "No Checkpoints in Level");
54        checkpoints_=checkpoints;
55        staticRacePoints_ = findStaticCheckpoints(checkpoints);
56
57    }
58
59    int SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint)
60    {
61        if (this->getControllableEntity() != NULL)
62        {
63            return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length();
64        }
65        return -1;
66    }
67
68    RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
69    {
70        int distances[] = { -1, -1, -1 };
71        int temp_i = 0;
72        for (std::set<int>::iterator it =raceCheckpoint->getNextCheckpoints().begin(); it!= raceCheckpoint->getNextCheckpoints().end(); ++it)
73        {
74            distances[temp_i] = recCalculateDistance(raceCheckpoint,this->getControllableEntity()->getPosition());
75            temp_i++;
76        }
77        if (distances[0] > distances[1] && distances[1] != -1)
78        {
79            if (distances[2] < distances[1] && distances[2] != -1)
80            {
81                return checkpoints_[*raceCheckpoint->getNextCheckpoints().end()]; // return [2]
82            }
83            else
84            {
85                std::set<int>::iterator temp = raceCheckpoint->getNextCheckpoints().begin();
86                return checkpoints_[*(++temp)];
87            }
88        }
89        else
90        {
91            if (distances[2] < distances[0] && distances[2] != -1)
92            {
93                return checkpoints_[*raceCheckpoint->getNextCheckpoints().end()]; // return [2]
94            }
95            else
96            {
97                return checkpoints_[*raceCheckpoint->getNextCheckpoints().begin()]; // return [2]
98            }
99        }
100    }
101
102    RaceCheckPoint* SpaceRaceController::adjustNextPoint()
103    {
104        if (currentRaceCheckpoint_ == NULL) // no Adjust possible
105        {
106            return nextRaceCheckpoint_;
107        }
108        if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible
109        {
110            return nextRaceCheckpoint_;
111        }
112
113        //Adjust possible
114
115        return nextPointFind(currentRaceCheckpoint_);
116    }
117
118    int SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, Vector3 currentPosition)
119    {
120        // if ( staticCheckPoint was reached)
121        if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(),currentCheckPoint) != staticRacePoints_.end())
122        {
123            return (currentCheckPoint->getPosition() - currentPosition).length();
124        }
125        else
126        {
127            int minimum = std::numeric_limits<int>::max();
128            for (std::set<int>::iterator it = currentCheckPoint->getNextCheckpoints().begin(); it!= currentCheckPoint->getNextCheckpoints().end(); ++it)
129            {
130                minimum= std::min(minimum,(int) (currentPosition- currentCheckPoint->getPosition()).length() + recCalculateDistance(checkpoints_[(*it)], currentCheckPoint->getPosition()));
131            }//TODO: fix cast
132            return minimum;
133        }
134    }
135
136    /*
137     * returns a vector of static Point (checkpoints the spaceship has to reach)
138     */
139    std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(
140            std::vector<RaceCheckPoint*> allCheckpoints)
141    {
142        std::map<RaceCheckPoint*, int> * zaehler = new std::map<
143                RaceCheckPoint*, int>(); // counts how many times the checkpoit was reached (for simulation)
144        for (unsigned int i = 0; i < allCheckpoints.size(); i++)
145        {
146            zaehler->insert(std::pair<RaceCheckPoint*, int>(allCheckpoints[i],0));
147        }
148        int maxWays = rekSimulationCheckpointsReached(zaehler->begin()->first,&allCheckpoints, zaehler);
149
150        std::vector<RaceCheckPoint*> returnVec;
151        returnVec.clear();
152        for (std::map<RaceCheckPoint*, int>::iterator iter = zaehler->begin(); iter!= zaehler->end(); iter++)
153        {
154            if (iter->second == maxWays)
155            {
156                //returnVec.insert(allCheckpoints[1]);
157                returnVec.insert(returnVec.end(), iter->first);
158            }
159        }
160        delete zaehler;
161        return returnVec;
162    }
163
164    /*
165     *
166     * return how many ways go from the given checkpoint to the last one
167     */
168    int SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::vector<RaceCheckPoint*>* checkpoints, std::map<RaceCheckPoint*, int>* zaehler)
169    {
170        if (currentCheckpoint->isLast())
171        {// last point reached
172            (*zaehler)[currentCheckpoint] += 1;
173            return 1; // 1 Way form the last point to this one
174        }
175        else
176        {
177            int numberOfWays = 0; // counts number of ways from this Point to the last point
178            for (std::set<int>::iterator it =
179                    currentCheckpoint->getNextCheckpoints().begin(); it
180                    != currentCheckpoint->getNextCheckpoints().end(); ++it)
181            {
182                numberOfWays += rekSimulationCheckpointsReached((*checkpoints)[(*it)], checkpoints, zaehler);
183            }
184            (*zaehler)[currentCheckpoint] += numberOfWays;
185            return numberOfWays; // returns the number of ways from this point to the last one
186        }
187    }
188
189    SpaceRaceController::~SpaceRaceController()
190    {
191        // TODO Auto-generated destructor stub
192    }
193
194    void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
195    {
196        SUPER(SpaceRaceController, XMLPort, xmlelement, mode);
197        XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(
198                100.0f);
199        XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode)
200        ;
201
202    }
203    void SpaceRaceController::tick(float dt)
204    {
205        if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer()))
206        {//Checkpoint erreicht
207            currentRaceCheckpoint_=nextRaceCheckpoint_;
208            OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined");
209            nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
210        }
211        else if (std::abs(lastDistance - distanceSpaceshipToCheckPoint(nextRaceCheckpoint_)) < 500)
212        {
213            nextRaceCheckpoint_ = adjustNextPoint();
214        }
215        this->moveToPosition(nextRaceCheckpoint_->getPosition());
216    }
217
218}
Note: See TracBrowser for help on using the repository browser.