Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1.  
  2. void Simulator::advanceInJunction(){
  3.  
  4. for (map<string,Junction*>:: iterator it= JunctionMap.begin(); it!=JunctionMap.end(); ++it){
  5.  
  6.  
  7. Road* currRoad = (*it).second->incomingRoads[(*it).second->index];
  8. cout<<"time is: "<<simulatorTime<<endl;
  9. cout<<" this IR had green light: "<<currRoad->roadId<<endl;
  10. if(it->second->incomingRoads.empty()==false){
  11.  
  12. //cout<<"im junction: "+namejunc+" incoming road: "+currRoad->roadId+" and im empty"<<endl;
  13. if(currRoad->trafficlight.empty()==false){
  14.  
  15. // cout<<"im junction: "+namejunc+" incoming road: "+currRoad->roadId+" and im not empty"<<endl;
  16. bool firstIsFault=false;
  17.  
  18. Car& PoppedCar=(*currRoad->trafficlight.front());
  19. if(PoppedCar.isFault==true){
  20. firstIsFault=true;
  21. stack <Car*> faultyStack;
  22.  
  23.  
  24.  
  25. while(currRoad->trafficlight.empty()==false&&PoppedCar.isFault==true){
  26. currRoad->trafficlight.pop();
  27. PoppedCar.faultTimeLeft--;
  28.  
  29. if(PoppedCar.faultTimeLeft==0){
  30. PoppedCar.isFault=false;
  31. currRoad->numOfFaultyCarsInQueue--;
  32. }
  33.  
  34. faultyStack.push(&PoppedCar);
  35. if(currRoad->trafficlight.empty()==false){
  36. PoppedCar =(*currRoad->trafficlight.front());
  37. }
  38. }
  39.  
  40. currRoad->trafficlight.pop();
  41.  
  42.  
  43. while(!faultyStack.empty()){
  44. currRoad->trafficlight.push(faultyStack.top());
  45. faultyStack.pop();
  46. }
  47. }
  48. //if(firstIsFault==false){
  49. else{
  50.  
  51. currRoad->trafficlight.pop();
  52. }
  53.  
  54.  
  55. if (currRoad->trafficlight.empty()==true && currRoad->CarsOnRoad.empty()==true){
  56. nonEmptyRoads.erase(currRoad->roadId);
  57. }
  58.  
  59. currRoad->numOfCarsPerGreenSlice++;
  60. Road& NextRoad=*PoppedCar.roadPlanQueue.front();
  61. PoppedCar.roadPlanQueue.pop();
  62. NextRoad.CarsOnRoad.push_back(&PoppedCar);
  63. nonEmptyRoads.insert(NextRoad.roadId);
  64. NextRoad.numOfCars++;
  65. PoppedCar.road->numOfCars--;
  66. PoppedCar.road=&NextRoad;
  67. PoppedCar.location=0;
  68. string timeAsString = SSTR(simulatorTime);
  69. string start=NextRoad.start;
  70. string end=NextRoad.end;
  71.  
  72. PoppedCar.history.push_back('(');
  73. PoppedCar.history=PoppedCar.history+timeAsString;
  74. PoppedCar.history.push_back(',');
  75. PoppedCar.history=PoppedCar.history+start;
  76. PoppedCar.history.push_back(',');
  77. PoppedCar.history=PoppedCar.history+end;
  78. PoppedCar.history.push_back(',');
  79. PoppedCar.history.push_back('0');
  80. PoppedCar.history.push_back(')');
  81.  
  82.  
  83. }
  84.  
  85. currRoad->leftGreenTime--;
  86. if(currRoad->leftGreenTime==0){
  87.  
  88. if(it->second->index==(it->second->incomingRoads.size())-1){
  89. it->second->index=0;
  90. }
  91. else{
  92.  
  93. it->second->index++;
  94. }
  95.  
  96. int gap=(currRoad->greenSliceTime)-(currRoad->numOfCarsPerGreenSlice);
  97. if (gap==0){
  98. currRoad->greenSliceTime=min(currRoad->greenSliceTime+1,MAX_TIME_SLICE);
  99. }
  100. if (gap==currRoad->greenSliceTime){
  101. currRoad->greenSliceTime=max(currRoad->greenSliceTime-1,MIN_TIME_SLICE);
  102. }
  103. currRoad->numOfCarsPerGreenSlice=0;
  104.  
  105.  
  106. }
  107.  
  108.  
  109.  
  110. }
  111. for(vector<Road*>:: iterator vectorIt = (*it).second->incomingRoads.begin() ; vectorIt !=(*it).second->incomingRoads.end(); ++vectorIt){
  112.  
  113. bool start = true;
  114. if(!(*vectorIt)->trafficlight.empty()){
  115. Car* topCar=(*vectorIt)->trafficlight.front();
  116. while((*vectorIt)->trafficlight.front()==topCar||start){
  117. start=false;
  118. (*topCar).history.push_back('(');
  119. (*topCar).history=(*topCar).history+SSTR(simulatorTime);
  120. (*topCar).history.push_back(',');
  121. (*topCar).history=(*topCar).history+(*topCar).road->start;
  122. (*topCar).history.push_back(',');
  123. (*topCar).history=(*topCar).history+(*topCar).road->end;
  124. (*topCar).history.push_back(',');
  125. (*topCar).history=(*topCar).history+SSTR((*topCar).road->length);
  126. (*topCar).history.push_back(')');
  127. (*vectorIt)->trafficlight.pop();
  128. (*vectorIt)->trafficlight.push(topCar);
  129. topCar=(*vectorIt)->trafficlight.front();
  130.  
  131. }
  132.  
  133.  
  134. }
  135.  
  136. }
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement