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