[9399] | 1 | /* |
---|
[9412] | 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
[9399] | 4 | * |
---|
[9412] | 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 | * |
---|
[9399] | 22 | * Created on: Oct 8, 2012 |
---|
| 23 | * Author: purgham |
---|
| 24 | */ |
---|
[9459] | 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 | */ |
---|
[9399] | 38 | #include <gametypes/SpaceRaceController.h> |
---|
| 39 | #include "core/CoreIncludes.h" |
---|
| 40 | #include "core/XMLPort.h" |
---|
[9412] | 41 | #include "gametypes/SpaceRaceManager.h" |
---|
[9399] | 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
[9432] | 45 | CreateFactory(SpaceRaceController); |
---|
[9399] | 46 | |
---|
[9459] | 47 | const int AdjustDistance = 500; |
---|
[9412] | 48 | /* |
---|
| 49 | * Idea: Find static Point (checkpoints the spaceship has to reach) |
---|
| 50 | */ |
---|
[9432] | 51 | SpaceRaceController::SpaceRaceController(BaseObject* creator) : |
---|
| 52 | ArtificialController(creator) |
---|
| 53 | { |
---|
[9459] | 54 | RegisterObject(SpaceRaceController) |
---|
| 55 | ; std::vector<RaceCheckPoint*> checkpoints; |
---|
[9432] | 56 | for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it) |
---|
| 57 | { |
---|
| 58 | checkpoints = it->getAllCheckpoints(); |
---|
[9459] | 59 | nextRaceCheckpoint_=it->findCheckpoint(0); |
---|
[9432] | 60 | } |
---|
[9399] | 61 | |
---|
[9432] | 62 | OrxAssert(!checkpoints.empty(), "No Checkpoints in Level"); |
---|
| 63 | checkpoints_=checkpoints; |
---|
| 64 | staticRacePoints_ = findStaticCheckpoints(checkpoints); |
---|
[9451] | 65 | // initialisation of currentRaceCheckpoint_ |
---|
| 66 | currentRaceCheckpoint_ = NULL; |
---|
[9459] | 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); |
---|
[9441] | 114 | } |
---|
| 115 | } |
---|
[9459] | 116 | delete zaehler; |
---|
| 117 | return returnVec; |
---|
| 118 | } |
---|
[9399] | 119 | |
---|
[9459] | 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 | } |
---|
[9432] | 143 | } |
---|
[9399] | 144 | |
---|
[9459] | 145 | |
---|
| 146 | |
---|
| 147 | //------------------------------------- |
---|
| 148 | // functions for dynamic Way-search |
---|
| 149 | |
---|
[9432] | 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; |
---|
[9412] | 157 | } |
---|
| 158 | |
---|
[9459] | 159 | /* |
---|
| 160 | * called by: 'tick' or 'adjustNextPoint' |
---|
| 161 | * returns the next Checkpoint which the shortest way contains |
---|
| 162 | */ |
---|
[9432] | 163 | RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint) |
---|
| 164 | { |
---|
[9459] | 165 | int distances[] = { -1, -1, -1}; |
---|
[9432] | 166 | int temp_i = 0; |
---|
| 167 | for (std::set<int>::iterator it =raceCheckpoint->getNextCheckpoints().begin(); it!= raceCheckpoint->getNextCheckpoints().end(); ++it) |
---|
| 168 | { |
---|
[9459] | 169 | distances[temp_i] = recCalculateDistance(findCheckpoint(*it), this->getControllableEntity()->getPosition()); |
---|
[9432] | 170 | temp_i++; |
---|
| 171 | } |
---|
| 172 | if (distances[0] > distances[1] && distances[1] != -1) |
---|
| 173 | { |
---|
| 174 | if (distances[2] < distances[1] && distances[2] != -1) |
---|
| 175 | { |
---|
[9459] | 176 | return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return checkpoint with ID of raceCheckpoint->getNextCheckpoints() [2] |
---|
[9432] | 177 | } |
---|
| 178 | else |
---|
| 179 | { |
---|
| 180 | std::set<int>::iterator temp = raceCheckpoint->getNextCheckpoints().begin(); |
---|
[9459] | 181 | return findCheckpoint(*(++temp)); // return [1] |
---|
[9432] | 182 | } |
---|
| 183 | } |
---|
| 184 | else |
---|
| 185 | { |
---|
| 186 | if (distances[2] < distances[0] && distances[2] != -1) |
---|
| 187 | { |
---|
[9459] | 188 | return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return [2] |
---|
[9432] | 189 | } |
---|
| 190 | else |
---|
| 191 | { |
---|
[9459] | 192 | return findCheckpoint(*raceCheckpoint->getNextCheckpoints().begin()); // return [0] |
---|
[9432] | 193 | } |
---|
| 194 | } |
---|
| 195 | } |
---|
[9412] | 196 | |
---|
[9459] | 197 | /* |
---|
| 198 | * called from 'nextPointFind' |
---|
| 199 | * returns the distance between "currentPosition" and the next static checkpoint that can be reached from "currentCheckPoint" |
---|
| 200 | */ |
---|
[9432] | 201 | int SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, Vector3 currentPosition) |
---|
| 202 | { |
---|
[9459] | 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()) |
---|
[9432] | 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) |
---|
[9459] | 212 | { |
---|
| 213 | int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length()); |
---|
[9441] | 214 | |
---|
[9459] | 215 | minimum= std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(*it), currentCheckPoint->getPosition())); |
---|
[9441] | 216 | // minimum of distanz from 'currentPosition' to the next static Checkpoint |
---|
[9459] | 217 | } |
---|
[9432] | 218 | return minimum; |
---|
| 219 | } |
---|
| 220 | } |
---|
[9412] | 221 | |
---|
[9459] | 222 | /*called by 'tick' |
---|
| 223 | *adjust chosen way of the Spaceship every "AdjustDistance" because spaceship could be displaced through an other one |
---|
[9432] | 224 | */ |
---|
[9459] | 225 | RaceCheckPoint* SpaceRaceController::adjustNextPoint() |
---|
[9432] | 226 | { |
---|
[9459] | 227 | if (currentRaceCheckpoint_ == NULL) // no Adjust possible |
---|
| 228 | |
---|
[9432] | 229 | { |
---|
[9459] | 230 | return nextRaceCheckpoint_; |
---|
[9432] | 231 | } |
---|
[9459] | 232 | if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible |
---|
[9399] | 233 | |
---|
[9432] | 234 | { |
---|
[9459] | 235 | return nextRaceCheckpoint_; |
---|
[9432] | 236 | } |
---|
[9459] | 237 | |
---|
| 238 | //Adjust possible |
---|
| 239 | |
---|
| 240 | return nextPointFind(currentRaceCheckpoint_); |
---|
[9432] | 241 | } |
---|
[9399] | 242 | |
---|
[9459] | 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; |
---|
[9432] | 265 | } |
---|
[9459] | 266 | virtualCheckPointIndex--; |
---|
[9432] | 267 | } |
---|
[9399] | 268 | |
---|
[9459] | 269 | |
---|
| 270 | |
---|
[9432] | 271 | SpaceRaceController::~SpaceRaceController() |
---|
| 272 | { |
---|
[9459] | 273 | for (int i =-1; i>virtualCheckPointIndex ; i--){ |
---|
| 274 | delete findCheckpoint(i); |
---|
| 275 | } |
---|
[9432] | 276 | } |
---|
[9399] | 277 | |
---|
[9459] | 278 | void SpaceRaceController::tick(float dt) |
---|
[9432] | 279 | { |
---|
[9459] | 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 | } |
---|
[9399] | 290 | |
---|
[9432] | 291 | if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer())) |
---|
| 292 | {//Checkpoint erreicht |
---|
| 293 | currentRaceCheckpoint_=nextRaceCheckpoint_; |
---|
| 294 | OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined"); |
---|
| 295 | nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_); |
---|
[9459] | 296 | lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
[9432] | 297 | } |
---|
[9459] | 298 | else if ((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()> AdjustDistance) |
---|
[9432] | 299 | { |
---|
| 300 | nextRaceCheckpoint_ = adjustNextPoint(); |
---|
[9459] | 301 | lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
[9432] | 302 | } |
---|
[9459] | 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 | } |
---|
[9432] | 308 | this->moveToPosition(nextRaceCheckpoint_->getPosition()); |
---|
[9459] | 309 | |
---|
| 310 | |
---|
[9432] | 311 | } |
---|
[9399] | 312 | |
---|
| 313 | } |
---|