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 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | CreateFactory(SpaceRaceController); |
---|
46 | |
---|
47 | const int AdjustDistance = 500; |
---|
48 | /* |
---|
49 | * Idea: Find static Point (checkpoints the spaceship has to reach) |
---|
50 | */ |
---|
51 | SpaceRaceController::SpaceRaceController(BaseObject* creator) : |
---|
52 | ArtificialController(creator) |
---|
53 | { |
---|
54 | RegisterObject(SpaceRaceController) |
---|
55 | ; std::vector<RaceCheckPoint*> checkpoints; |
---|
56 | for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it) |
---|
57 | { |
---|
58 | checkpoints = it->getAllCheckpoints(); |
---|
59 | nextRaceCheckpoint_=it->findCheckpoint(0); |
---|
60 | } |
---|
61 | |
---|
62 | OrxAssert(!checkpoints.empty(), "No Checkpoints in Level"); |
---|
63 | checkpoints_=checkpoints; |
---|
64 | staticRacePoints_ = findStaticCheckpoints(checkpoints); |
---|
65 | // initialisation of currentRaceCheckpoint_ |
---|
66 | currentRaceCheckpoint_ = NULL; |
---|
67 | /* |
---|
68 | // find first Checkpoint |
---|
69 | for (int i=0; true; i++){ |
---|
70 | if(checkpoints_[i]->getCheckpointIndex()==0){ |
---|
71 | nextRaceCheckpoint_=checkpoints_[i]; |
---|
72 | break; |
---|
73 | } |
---|
74 | }*/ |
---|
75 | |
---|
76 | virtualCheckPointIndex=-1; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | //------------------------------ |
---|
81 | // functions for initialisation |
---|
82 | |
---|
83 | void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
84 | { |
---|
85 | SUPER(SpaceRaceController, XMLPort, xmlelement, mode); |
---|
86 | XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f); |
---|
87 | XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode); |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | * called from constructor 'SpaceRaceController' |
---|
93 | * returns a vector of static Point (checkpoints the spaceship has to reach) |
---|
94 | */ |
---|
95 | std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints( |
---|
96 | 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,&allCheckpoints, 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::vector<RaceCheckPoint*>* checkpoints, 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 =lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
135 | currentCheckpoint->getNextCheckpoints().begin(); it |
---|
136 | != currentCheckpoint->getNextCheckpoints().end(); ++it) |
---|
137 | { |
---|
138 | numberOfWays += rekSimulationCheckpointsReached((*checkpoints)[(*it)], checkpoints, zaehler); |
---|
139 | } |
---|
140 | (*zaehler)[currentCheckpoint] += numberOfWays; |
---|
141 | return numberOfWays; // returns the number of ways from this point to the last one |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | //------------------------------------- |
---|
148 | // functions for dynamic Way-search |
---|
149 | |
---|
150 | int SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint) |
---|
151 | { |
---|
152 | if (this->getControllableEntity() != NULL) |
---|
153 | { |
---|
154 | return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length(); |
---|
155 | } |
---|
156 | return -1; |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | * called by: 'tick' or 'adjustNextPoint' |
---|
161 | * returns the next Checkpoint which the shortest way contains |
---|
162 | */ |
---|
163 | RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint) |
---|
164 | { |
---|
165 | int distances[] = { -1, -1, -1}; |
---|
166 | int temp_i = 0; |
---|
167 | for (std::set<int>::iterator it =raceCheckpoint->getNextCheckpoints().begin(); it!= raceCheckpoint->getNextCheckpoints().end(); ++it) |
---|
168 | { |
---|
169 | distances[temp_i] = recCalculateDistance(findCheckpoint(*it), this->getControllableEntity()->getPosition()); |
---|
170 | temp_i++; |
---|
171 | } |
---|
172 | if (distances[0] > distances[1] && distances[1] != -1) |
---|
173 | { |
---|
174 | if (distances[2] < distances[1] && distances[2] != -1) |
---|
175 | { |
---|
176 | return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return checkpoint with ID of raceCheckpoint->getNextCheckpoints() [2] |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | std::set<int>::iterator temp = raceCheckpoint->getNextCheckpoints().begin(); |
---|
181 | return findCheckpoint(*(++temp)); // return [1] |
---|
182 | } |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | if (distances[2] < distances[0] && distances[2] != -1) |
---|
187 | { |
---|
188 | return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return [2] |
---|
189 | } |
---|
190 | else |
---|
191 | { |
---|
192 | return findCheckpoint(*raceCheckpoint->getNextCheckpoints().begin()); // return [0] |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | /* |
---|
198 | * called from 'nextPointFind' |
---|
199 | * returns the distance between "currentPosition" and the next static checkpoint that can be reached from "currentCheckPoint" |
---|
200 | */ |
---|
201 | int SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, Vector3 currentPosition) |
---|
202 | { |
---|
203 | // find: looks if the currentCheckPoint is a staticCheckPoint (staticCheckPoint is the same as: static Point) |
---|
204 | if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(), currentCheckPoint) != staticRacePoints_.end()) |
---|
205 | { |
---|
206 | return (currentCheckPoint->getPosition() - currentPosition).length(); |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | int minimum = std::numeric_limits<int>::max(); |
---|
211 | for (std::set<int>::iterator it = currentCheckPoint->getNextCheckpoints().begin(); it!= currentCheckPoint->getNextCheckpoints().end(); ++it) |
---|
212 | { |
---|
213 | int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length()); |
---|
214 | |
---|
215 | minimum= std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(*it), currentCheckPoint->getPosition())); |
---|
216 | // minimum of distanz from 'currentPosition' to the next static Checkpoint |
---|
217 | } |
---|
218 | return minimum; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | /*called by 'tick' |
---|
223 | *adjust chosen way of the Spaceship every "AdjustDistance" because spaceship could be displaced through an other one |
---|
224 | */ |
---|
225 | RaceCheckPoint* SpaceRaceController::adjustNextPoint() |
---|
226 | { |
---|
227 | if (currentRaceCheckpoint_ == NULL) // no Adjust possible |
---|
228 | |
---|
229 | { |
---|
230 | return nextRaceCheckpoint_; |
---|
231 | } |
---|
232 | if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible |
---|
233 | |
---|
234 | { |
---|
235 | return nextRaceCheckpoint_; |
---|
236 | } |
---|
237 | |
---|
238 | //Adjust possible |
---|
239 | |
---|
240 | return nextPointFind(currentRaceCheckpoint_); |
---|
241 | } |
---|
242 | |
---|
243 | RaceCheckPoint* SpaceRaceController::findCheckpoint(int index) const |
---|
244 | { |
---|
245 | for (size_t i = 0; i < this->checkpoints_.size(); ++i) |
---|
246 | if (this->checkpoints_[i]->getCheckpointIndex() == index) |
---|
247 | return this->checkpoints_[i]; |
---|
248 | return NULL; |
---|
249 | } |
---|
250 | |
---|
251 | bool SpaceRaceController::addVirtualCheckPoint(int positionInNextCheckPoint, RaceCheckPoint* previousCheckpoint, int indexFollowingCheckPoint , Vector3 virtualCheckPointPosition ){ |
---|
252 | |
---|
253 | RaceCheckPoint* newTempRaceCheckPoint = new RaceCheckPoint(this); |
---|
254 | newTempRaceCheckPoint->setPosition(virtualCheckPointPosition); |
---|
255 | newTempRaceCheckPoint->setCheckpointIndex(virtualCheckPointIndex); |
---|
256 | newTempRaceCheckPoint->setLast(false); |
---|
257 | newTempRaceCheckPoint->setNextCheckpointsAsVector3(Vector3(indexFollowingCheckPoint,-1,-1)); |
---|
258 | |
---|
259 | Vector3 temp = previousCheckpoint->getNextCheckpointsAsVector3(); |
---|
260 | checkpoints_.insert(checkpoints_.end(), newTempRaceCheckPoint); |
---|
261 | switch(positionInNextCheckPoint){ |
---|
262 | case 0: temp.x=virtualCheckPointIndex; break; |
---|
263 | case 1: temp.y=virtualCheckPointIndex; break; |
---|
264 | case 2: temp.z=virtualCheckPointIndex; break; |
---|
265 | } |
---|
266 | virtualCheckPointIndex--; |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | SpaceRaceController::~SpaceRaceController() |
---|
272 | { |
---|
273 | for (int i =-1; i>virtualCheckPointIndex ; i--){ |
---|
274 | delete findCheckpoint(i); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | void SpaceRaceController::tick(float dt) |
---|
279 | { |
---|
280 | if (this->getControllableEntity() == NULL || this->getControllableEntity()->getPlayer() == NULL ) |
---|
281 | { orxout()<<this->getControllableEntity()<< " in tick"<<endl; return;} |
---|
282 | //FOR virtual Checkpoints |
---|
283 | if(nextRaceCheckpoint_->getCheckpointIndex() < 0){ |
---|
284 | if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 30){ |
---|
285 | currentRaceCheckpoint_=nextRaceCheckpoint_; |
---|
286 | nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_); |
---|
287 | lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer())) |
---|
292 | {//Checkpoint erreicht |
---|
293 | currentRaceCheckpoint_=nextRaceCheckpoint_; |
---|
294 | OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined"); |
---|
295 | nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_); |
---|
296 | lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
297 | } |
---|
298 | else if ((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()> AdjustDistance) |
---|
299 | { |
---|
300 | nextRaceCheckpoint_ = adjustNextPoint(); |
---|
301 | lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
302 | } |
---|
303 | //korrigieren! |
---|
304 | else if((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()<5){\ |
---|
305 | this->moveToPosition(Vector3(rnd()*100,rnd()*100,rnd()*100)); |
---|
306 | this->spin(); |
---|
307 | } |
---|
308 | this->moveToPosition(nextRaceCheckpoint_->getPosition()); |
---|
309 | |
---|
310 | |
---|
311 | } |
---|
312 | |
---|
313 | } |
---|