Changeset 9432 for code/branches/Racingbot
- Timestamp:
- Nov 5, 2012, 3:58:03 PM (12 years ago)
- Location:
- code/branches/Racingbot/src/modules/gametypes
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt
r9399 r9432 5 5 OldSpaceRace.cc 6 6 OldRaceCheckPoint.cc 7 SpaceRaceBot.cc 7 8 SpaceRaceController.cc 8 9 ) -
code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h
r9399 r9432 67 67 class SpaceRace; 68 68 class OldSpaceRace; 69 class SpaceRaceManager; 70 class SpaceRaceBot; 69 71 class SpaceRaceController; 70 72 } -
code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc
r9412 r9432 108 108 109 109 if (checkpoints.x > -1) 110 this->nextCheckpoints_.insert(static_cast<int>(checkpoints.x + 0.5)); 110 this->nextCheckpoints_.insert(static_cast<int>(checkpoints.x + 0.5)); // the red number has the type double and for the cast (to int) is added 0.5 so that the cast works correctly 111 111 if (checkpoints.y > -1) 112 112 this->nextCheckpoints_.insert(static_cast<int>(checkpoints.y + 0.5)); -
code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc
r9348 r9432 35 35 #include "util/Convert.h" 36 36 #include "util/Math.h" 37 37 #include "SpaceRaceBot.h" 38 38 #include "items/Engine.h" 39 39 … … 46 46 RegisterObject(SpaceRace); 47 47 48 this->botclass_ = Class(SpaceRaceBot);//ClassByString("") 48 49 this->cantMove_ = false; 49 50 this->bTimeIsUp_ = false; -
code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc
r9412 r9432 28 28 #include "core/XMLPort.h" 29 29 #include "gametypes/SpaceRaceManager.h" 30 31 30 32 31 // Von SpaceRaceManager points einlesen … … 35 34 namespace orxonox 36 35 { 36 CreateFactory(SpaceRaceController); 37 37 38 38 /* 39 39 * Idea: Find static Point (checkpoints the spaceship has to reach) 40 40 */ 41 SpaceRaceController::SpaceRaceController(BaseObject* creator): ArtificialController(creator) 42 { 43 44 std::vector<RaceCheckPoint*> checkpoints; 45 // TODO Auto-generated constructor stub 46 for (ObjectList<SpaceRaceManager >::iterator it = ObjectList<SpaceRaceManager>::begin(); it != ObjectList<SpaceRaceManager>::end(); ++it) 47 checkpoints=it->getAllCheckpoints(); 48 //OrxAssert(checkpoints, "No Checkpoints in Level"); 49 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 } 50 217 51 218 } 52 /*53 RaceCheckPoint* SpaceRaceController::nextPoint(){54 return NULL;55 }*/56 57 std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(std::vector<RaceCheckPoint*> allCheckpoints){58 std::map< RaceCheckPoint*, int> * zaehler= new std::map< RaceCheckPoint*, int>(); // counts how many times the checkpoit was reached for simulation59 for (unsigned int i=0; i<allCheckpoints.size() ; i++){60 zaehler->insert ( std::pair<RaceCheckPoint*,int>(allCheckpoints[i], 0));61 }62 rekSimulationCheckpointsReached(zaehler->begin()->first, & allCheckpoints, zaehler);63 //Werte auslesen und statische Checkpoints bestimmen64 delete zaehler;65 66 }67 68 void SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::vector<RaceCheckPoint*>* checkpoints, std::map< RaceCheckPoint*, int>* zaehler){69 zaehler->at(currentCheckpoint)++;70 if(!currentCheckpoint->isLast()){71 for( std::set<int>::iterator it=currentCheckpoint->getNextCheckpoints().begin(); it != currentCheckpoint->getNextCheckpoints().end(); ++it){72 rekSimulationCheckpointsReached( (*checkpoints)[(*it)], checkpoints, zaehler);73 }74 }75 }76 77 78 SpaceRaceController::~SpaceRaceController()79 {80 // TODO Auto-generated destructor stub81 }82 83 void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode){84 SUPER(SpaceRaceController, XMLPort, xmlelement, mode);85 86 XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f);87 XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode);88 89 }90 void SpaceRaceController::tick(float dt){91 92 }93 94 95 96 } -
code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h
r9412 r9432 38 38 class _GametypesExport SpaceRaceController : public ArtificialController, public Tickable 39 39 { 40 private:/* 41 vector<RaceCheckPoint*> lastRaceCheckpoint; 42 vector<RaceCheckPoint*> nextRaceCheckpoint;*/ 43 //RaceCheckPoint* nextPoint(); 40 private: 41 std::vector<RaceCheckPoint*> staticRacePoints_; 42 RaceCheckPoint* nextRaceCheckpoint_; 43 RaceCheckPoint* currentRaceCheckpoint_; 44 std::vector<RaceCheckPoint*> checkpoints_; 45 int lastDistance; 46 47 int recCalculateDistance(RaceCheckPoint*, Vector3 currentPosition); 48 int distanceSpaceshipToCheckPoint(RaceCheckPoint*); 49 RaceCheckPoint* nextPointFind(RaceCheckPoint*); 50 RaceCheckPoint* adjustNextPoint(); 44 51 std::vector<RaceCheckPoint*> findStaticCheckpoints(std::vector<RaceCheckPoint*>); 45 52 std::vector<RaceCheckPoint*> staticCheckpoints(); 46 void rekSimulationCheckpointsReached(RaceCheckPoint* , std::vector<RaceCheckPoint*>* checkpoints, std::map< RaceCheckPoint*, int>*); 53 int rekSimulationCheckpointsReached(RaceCheckPoint* , std::vector<RaceCheckPoint*>* checkpoints, std::map< RaceCheckPoint*, int>*); 54 47 55 public: 48 56 SpaceRaceController(BaseObject* creator); -
code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc
r9412 r9432 45 45 BaseObject(creator) 46 46 { 47 RegisterObject(SpaceRaceManager) 48 ;this->race_ = orxonox_cast<SpaceRace*>(this->getGametype().get());47 RegisterObject(SpaceRaceManager); 48 this->race_ = orxonox_cast<SpaceRace*>(this->getGametype().get()); 49 49 assert(race_); 50 50 //amountOfPlayers=(race_->getPlayers()).size(); 51 51 this->firstcheckpointvisible_ = false; 52 this->players_ = this->race_->getPlayers();53 52 } 54 53 … … 70 69 SUPER(SpaceRaceManager,tick,dt); 71 70 71 this->players_ = this->race_->getPlayers(); 72 72 73 if (this->checkpoints_[0] != NULL && !this->firstcheckpointvisible_) 73 74 { … … 78 79 for ( std::map< PlayerInfo*, Player>::iterator it = players_.begin(); it != players_.end(); ++it) 79 80 { 81 80 82 for (size_t i = 0; i < this->checkpoints_.size(); ++i) 81 83 { 82 if (this->checkpoints_[i]->playerWasHere(it->first)) 84 if (this->checkpoints_[i]->playerWasHere(it->first)){ 83 85 this->checkpointReached(this->checkpoints_[i], it->first /*this->checkpoints_[i]->getPlayer()*/); 86 } 84 87 } 85 88 } … … 142 145 143 146 RaceCheckPoint* oldCheckpoint = gametype->getCheckpointReached(player); // returns the last from player reached checkpoint 144 147 // % fixing 148 orxout() << "SpaceRaceManager.checkpointReached( "<<newCheckpoint->getCheckpointIndex() 149 <<", "<< player->isHumanPlayer() << endl; 150 // % end fixing 145 151 if (this->reachedValidCheckpoint(oldCheckpoint, newCheckpoint, player)) 146 152 {
Note: See TracChangeset
for help on using the changeset viewer.