Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Simulator::advanceInRoad(){
- //for each road in the set( only non empty roads):
- for (set<string>:: iterator setIt= nonEmptyRoads.begin(); setIt!=nonEmptyRoads.end(); ++setIt){
- string roadName=*setIt;
- Road* myRoad= RoadMap[roadName];
- // car with largest location will be at CarsOnRoad.begin
- stable_sort(myRoad->CarsOnRoad.begin(),myRoad->CarsOnRoad.end(),compareCar);
- double roadSpeed=ceil((myRoad->numOfCars)/(myRoad->length));
- int roudLength=myRoad->length;
- int faultCarCounter=0;
- int locationCounter=0;
- int lastLocation=-1;
- //for each car on this road(cars are in vector CarsOnRoad:
- for(vector<Car*>:: iterator vectorIt = myRoad->CarsOnRoad.begin() ; vectorIt !=myRoad->CarsOnRoad.end(); ++vectorIt){//vectorIt is a pointer to a car in the vector
- //cout<<"Simulator::advanceInRoad(): "<<"car on road"<<roadName<<": "<<(*vectorIt)->id<<endl;
- //if car is fault-dont advance
- if((*vectorIt)->isFault){
- (*vectorIt)->faultTimeLeft--;
- if((*vectorIt)->faultTimeLeft==0){
- (*vectorIt)->isFault=false;
- }
- // faultycar is in same location as last faultyCar
- if((*vectorIt)->location==lastLocation){
- locationCounter++;
- faultCarCounter++;
- }
- // faultycar is in not in same location as last faultyCar
- else{
- lastLocation=(*vectorIt)->location;
- locationCounter=1;
- faultCarCounter++;
- }
- }
- //in case car is not fault- advance
- else{
- int mySpeed;
- // car is in same location as last faultyCar
- if((*vectorIt)->location==lastLocation){
- int add=pow(0.5,faultCarCounter-locationCounter);
- mySpeed=ceil(roadSpeed*add);
- }
- // car is innot in same location as last faultyCar
- else{
- mySpeed=ceil(roadSpeed*(pow(0.5,faultCarCounter)));
- }
- // advance
- mySpeed=min(MAX_SPEED,mySpeed);
- (*vectorIt)->location=(*vectorIt)->location+mySpeed;
- //////////////////////////////////////////////////////////////////////////////////////////////////check:
- // if the car (exceded)/reached road length
- if(((*vectorIt)->location)>=roudLength){
- ((*vectorIt)->location)=roudLength;
- // if car reach end of roadPlan- out of game
- if(((*vectorIt)->roadPlanQueue).empty()){
- numOfCarsInGame--;
- myRoad->numOfCars--;
- }
- //else, continue to the roadJunction
- else{
- cout<<"car "<<(*vectorIt)->id<<" on road "<<(*vectorIt)->road->roadId<<" with len "<<(*vectorIt)->road->length<<" in location :"<<(*vectorIt)->location<<endl;
- myRoad->trafficlight.push(*vectorIt);
- cout<<"queue size :"<<myRoad->trafficlight.size()<<endl;
- cout<<"queue include car: "<<myRoad->trafficlight.front()->id<<endl;
- }
- }
- }
- //update history of all cars )
- string locationAsString = SSTR((*vectorIt)->location);
- string simulatorTimeAsString = SSTR(simulatorTime+1);
- (*vectorIt)->history.push_back('(');
- (*vectorIt)->history=(*vectorIt)->history+simulatorTimeAsString;
- (*vectorIt)->history.push_back(',');
- (*vectorIt)->history=(*vectorIt)->history+myRoad->start;
- (*vectorIt)->history.push_back(',');
- (*vectorIt)->history=(*vectorIt)->history+myRoad->end;
- (*vectorIt)->history.push_back(',');
- (*vectorIt)->history=(*vectorIt)->history+locationAsString;
- (*vectorIt)->history.push_back(')');
- // end action for each car
- }
- stable_sort(myRoad->CarsOnRoad.begin(),myRoad->CarsOnRoad.end(),compareCar);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement