Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Simulator::advanceInJunction(){
- for (map<string,Junction*>:: iterator it= JunctionMap.begin(); it!=JunctionMap.end(); ++it){
- Road* currRoad = (*it).second->incomingRoads[(*it).second->index];
- cout<<"time is: "<<simulatorTime<<endl;
- cout<<" this IR had green light: "<<currRoad->roadId<<endl;
- if(it->second->incomingRoads.empty()==false){
- //cout<<"im junction: "+namejunc+" incoming road: "+currRoad->roadId+" and im empty"<<endl;
- if(currRoad->trafficlight.empty()==false){
- // cout<<"im junction: "+namejunc+" incoming road: "+currRoad->roadId+" and im not empty"<<endl;
- bool firstIsFault=false;
- Car& PoppedCar=(*currRoad->trafficlight.front());
- if(PoppedCar.isFault==true){
- firstIsFault=true;
- stack <Car*> faultyStack;
- while(currRoad->trafficlight.empty()==false&&PoppedCar.isFault==true){
- currRoad->trafficlight.pop();
- PoppedCar.faultTimeLeft--;
- if(PoppedCar.faultTimeLeft==0){
- PoppedCar.isFault=false;
- currRoad->numOfFaultyCarsInQueue--;
- }
- faultyStack.push(&PoppedCar);
- if(currRoad->trafficlight.empty()==false){
- PoppedCar =(*currRoad->trafficlight.front());
- }
- }
- currRoad->trafficlight.pop();
- while(!faultyStack.empty()){
- currRoad->trafficlight.push(faultyStack.top());
- faultyStack.pop();
- }
- }
- //if(firstIsFault==false){
- else{
- currRoad->trafficlight.pop();
- }
- if (currRoad->trafficlight.empty()==true && currRoad->CarsOnRoad.empty()==true){
- nonEmptyRoads.erase(currRoad->roadId);
- }
- currRoad->numOfCarsPerGreenSlice++;
- Road& NextRoad=*PoppedCar.roadPlanQueue.front();
- PoppedCar.roadPlanQueue.pop();
- NextRoad.CarsOnRoad.push_back(&PoppedCar);
- nonEmptyRoads.insert(NextRoad.roadId);
- NextRoad.numOfCars++;
- PoppedCar.road->numOfCars--;
- PoppedCar.road=&NextRoad;
- PoppedCar.location=0;
- string timeAsString = SSTR(simulatorTime);
- string start=NextRoad.start;
- string end=NextRoad.end;
- PoppedCar.history.push_back('(');
- PoppedCar.history=PoppedCar.history+timeAsString;
- PoppedCar.history.push_back(',');
- PoppedCar.history=PoppedCar.history+start;
- PoppedCar.history.push_back(',');
- PoppedCar.history=PoppedCar.history+end;
- PoppedCar.history.push_back(',');
- PoppedCar.history.push_back('0');
- PoppedCar.history.push_back(')');
- }
- currRoad->leftGreenTime--;
- if(currRoad->leftGreenTime==0){
- if(it->second->index==(it->second->incomingRoads.size())-1){
- it->second->index=0;
- }
- else{
- it->second->index++;
- }
- int gap=(currRoad->greenSliceTime)-(currRoad->numOfCarsPerGreenSlice);
- if (gap==0){
- currRoad->greenSliceTime=min(currRoad->greenSliceTime+1,MAX_TIME_SLICE);
- }
- if (gap==currRoad->greenSliceTime){
- currRoad->greenSliceTime=max(currRoad->greenSliceTime-1,MIN_TIME_SLICE);
- }
- currRoad->numOfCarsPerGreenSlice=0;
- }
- }
- for(vector<Road*>:: iterator vectorIt = (*it).second->incomingRoads.begin() ; vectorIt !=(*it).second->incomingRoads.end(); ++vectorIt){
- bool start = true;
- if(!(*vectorIt)->trafficlight.empty()){
- Car* topCar=(*vectorIt)->trafficlight.front();
- while((*vectorIt)->trafficlight.front()==topCar||start){
- start=false;
- (*topCar).history.push_back('(');
- (*topCar).history=(*topCar).history+SSTR(simulatorTime);
- (*topCar).history.push_back(',');
- (*topCar).history=(*topCar).history+(*topCar).road->start;
- (*topCar).history.push_back(',');
- (*topCar).history=(*topCar).history+(*topCar).road->end;
- (*topCar).history.push_back(',');
- (*topCar).history=(*topCar).history+SSTR((*topCar).road->length);
- (*topCar).history.push_back(')');
- (*vectorIt)->trafficlight.pop();
- (*vectorIt)->trafficlight.push(topCar);
- topCar=(*vectorIt)->trafficlight.front();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement