Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1.  
  2. void Simulator::advanceInRoad(){
  3.  
  4. //for each road in the set( only non empty roads):
  5. for (set<string>:: iterator setIt= nonEmptyRoads.begin(); setIt!=nonEmptyRoads.end(); ++setIt){
  6. string roadName=*setIt;
  7. Road* myRoad= RoadMap[roadName];
  8. // car with largest location will be at CarsOnRoad.begin
  9. stable_sort(myRoad->CarsOnRoad.begin(),myRoad->CarsOnRoad.end(),compareCar);
  10.  
  11. double roadSpeed=ceil((myRoad->numOfCars)/(myRoad->length));
  12. int roudLength=myRoad->length;
  13. int faultCarCounter=0;
  14. int locationCounter=0;
  15. int lastLocation=-1;
  16.  
  17. //for each car on this road(cars are in vector CarsOnRoad:
  18. for(vector<Car*>:: iterator vectorIt = myRoad->CarsOnRoad.begin() ; vectorIt !=myRoad->CarsOnRoad.end(); ++vectorIt){//vectorIt is a pointer to a car in the vector
  19. //cout<<"Simulator::advanceInRoad(): "<<"car on road"<<roadName<<": "<<(*vectorIt)->id<<endl;
  20.  
  21. //if car is fault-dont advance
  22. if((*vectorIt)->isFault){
  23.  
  24. (*vectorIt)->faultTimeLeft--;
  25. if((*vectorIt)->faultTimeLeft==0){
  26. (*vectorIt)->isFault=false;
  27. }
  28.  
  29. // faultycar is in same location as last faultyCar
  30. if((*vectorIt)->location==lastLocation){
  31. locationCounter++;
  32. faultCarCounter++;
  33. }
  34.  
  35. // faultycar is in not in same location as last faultyCar
  36. else{
  37. lastLocation=(*vectorIt)->location;
  38. locationCounter=1;
  39. faultCarCounter++;
  40. }
  41. }
  42.  
  43. //in case car is not fault- advance
  44. else{
  45. int mySpeed;
  46.  
  47. // car is in same location as last faultyCar
  48. if((*vectorIt)->location==lastLocation){
  49.  
  50. int add=pow(0.5,faultCarCounter-locationCounter);
  51. mySpeed=ceil(roadSpeed*add);
  52. }
  53.  
  54. // car is innot in same location as last faultyCar
  55. else{
  56.  
  57. mySpeed=ceil(roadSpeed*(pow(0.5,faultCarCounter)));
  58. }
  59. // advance
  60. mySpeed=min(MAX_SPEED,mySpeed);
  61. (*vectorIt)->location=(*vectorIt)->location+mySpeed;
  62.  
  63.  
  64.  
  65. //////////////////////////////////////////////////////////////////////////////////////////////////check:
  66. // if the car (exceded)/reached road length
  67. if(((*vectorIt)->location)>=roudLength){
  68.  
  69. ((*vectorIt)->location)=roudLength;
  70.  
  71. // if car reach end of roadPlan- out of game
  72. if(((*vectorIt)->roadPlanQueue).empty()){
  73. numOfCarsInGame--;
  74. myRoad->numOfCars--;
  75.  
  76. }
  77. //else, continue to the roadJunction
  78. else{
  79. cout<<"car "<<(*vectorIt)->id<<" on road "<<(*vectorIt)->road->roadId<<" with len "<<(*vectorIt)->road->length<<" in location :"<<(*vectorIt)->location<<endl;
  80. myRoad->trafficlight.push(*vectorIt);
  81. cout<<"queue size :"<<myRoad->trafficlight.size()<<endl;
  82. cout<<"queue include car: "<<myRoad->trafficlight.front()->id<<endl;
  83.  
  84. }
  85. }
  86. }
  87.  
  88. //update history of all cars )
  89. string locationAsString = SSTR((*vectorIt)->location);
  90. string simulatorTimeAsString = SSTR(simulatorTime+1);
  91.  
  92. (*vectorIt)->history.push_back('(');
  93. (*vectorIt)->history=(*vectorIt)->history+simulatorTimeAsString;
  94. (*vectorIt)->history.push_back(',');
  95. (*vectorIt)->history=(*vectorIt)->history+myRoad->start;
  96. (*vectorIt)->history.push_back(',');
  97. (*vectorIt)->history=(*vectorIt)->history+myRoad->end;
  98. (*vectorIt)->history.push_back(',');
  99. (*vectorIt)->history=(*vectorIt)->history+locationAsString;
  100. (*vectorIt)->history.push_back(')');
  101.  
  102. // end action for each car
  103. }
  104. stable_sort(myRoad->CarsOnRoad.begin(),myRoad->CarsOnRoad.end(),compareCar);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement