This repository was archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrafficLight.cpp
More file actions
154 lines (138 loc) · 6.92 KB
/
Copy pathTrafficLight.cpp
File metadata and controls
154 lines (138 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "TrafficLight.h"
#include "Vehicle.h"
#include "Standard_Values.h"
#include "DesignByContract.h"
#include <typeinfo>
TrafficLight::TrafficLight(unsigned int cyclus, Road *road) : cyclus(cyclus), road(road) , currentColor(red) , init(this) {
REQUIRE(*typeid(cyclus).name() == 'j' && cyclus >= 0 , "constructor was called with invalid cyclus");
REQUIRE(*typeid(road).name() == 'P' , "constructor was called with invalid road");
REQUIRE(road->properlyInitialized() , "constructor was called with invalid road");
ENSURE(TrafficLight::cyclus == cyclus , "cyclus was not properly initialized");
ENSURE(TrafficLight::road == road , "road was not properly initialized");
ENSURE(TrafficLight::currentColor == red , "currentColor was not properly initialized");
ENSURE(properlyInitialized(), "constructor must end in properlyInitialized state");
}
TrafficLight::TrafficLight() : road(NULL), currentColor(red) , init(this){
ENSURE(TrafficLight::currentColor == red , "currentColor was not properly initialized");
ENSURE(TrafficLight::road == NULL , "road was not properly initialized");
ENSURE(properlyInitialized(), "constructor must end in properlyInitialized state");
}
bool TrafficLight::properlyInitialized() {
return init == this;
}
int TrafficLight::getCyclus() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling getCyclus");
return cyclus;
}
void TrafficLight::setCyclus(unsigned int newCyclus) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling setCyclus");
REQUIRE(*typeid(newCyclus).name() == 'j' , "setCyclus was called with invalid parameter");
TrafficLight::cyclus = newCyclus;
ENSURE(TrafficLight::cyclus == newCyclus , "setCyclus failed");
}
lightColor TrafficLight::getCurrentColor() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling getCurrentColor");
return currentColor;
}
void TrafficLight::setCurrentColor(lightColor newColor) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling setCurrentColor");
REQUIRE( newColor == red || newColor == green , "setCurrentColor was called with invalid parameter");
TrafficLight::currentColor = newColor;
ENSURE(TrafficLight::currentColor == newColor , "setCurrentColor failed");
}
Road* TrafficLight::getRoad(){
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling getRoad");
return road;
}
void TrafficLight::setRoad(Road* newRoad) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling setRoad");
REQUIRE(*typeid(newRoad).name() == 'P' , "setRoad was called with invalid parameter");
REQUIRE(newRoad->properlyInitialized() , "setRoad was called with uninitialized parameter");
TrafficLight::road = newRoad;
ENSURE(TrafficLight::road == newRoad , "setRoad failed");
}
unsigned int TrafficLight::getPosition() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling getPosition");
return position;
}
void TrafficLight::setPosition(unsigned int newPosition) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling setPosition");
REQUIRE(*typeid(newPosition).name() == 'j' , "setPosition was called with invalid parameter");
TrafficLight::position = newPosition;
ENSURE(TrafficLight::position == newPosition , "setPosition failed");
}
Vehicle* TrafficLight::getNearestVehicle() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling getNearestVehicle");
double pos = this->getPosition();
Vehicle* nearestVehicle = this->road->getVehicle(0);
Vehicle* currentVehicle;
for (int i = 0; i < this->road->getVehicleAmount(); ++i) {
currentVehicle = this->road->getVehicle(i);
if (currentVehicle->getVehiclePosition() < pos && currentVehicle->getVehiclePosition() > nearestVehicle->getVehiclePosition()){
nearestVehicle = currentVehicle;
}
}
return nearestVehicle;
}
void TrafficLight::simulate(int &count) {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling simulate");
REQUIRE(*typeid(count).name() == 'i' , "simulate was called with invalid parameter");
// Check if the cyclus has been completed
if (count % this->getCyclus() == 0){
// Change light color
this->setCurrentColor(this->getCurrentColor() == green ? red : green);
}
Vehicle* currentVehicle;
if (this->getCurrentColor() == green){
// Vehicles before the light may accelerate again
for (int i = 0; i < this->road->getVehicleAmount(); ++i) {
currentVehicle = this->road->getVehicle(i);
// Check for bus slowing down or stopping
if(currentVehicle->isSlowing_bus() || currentVehicle->isStopping_bus()){
continue;
}
if(this->road->getVehicleAmount() > 0 && currentVehicle->getVehiclePosition() + currentVehicle->getV_length() < this->getPosition()){
this->road->getVehicle(i)->setStatus(accelerate);
// currentVehicle->simulate();
}
}
}
if (this->getCurrentColor() == red){
currentVehicle = this->getNearestVehicle();
if ( currentVehicle->getType() != T_AUTO && currentVehicle->getType() != T_BUS){
return;
}
if(currentVehicle->isSlowing_bus() || currentVehicle->isStopping_bus()){
return;
}
if ( (this->position - ( currentVehicle->getVehiclePosition() + currentVehicle->getV_length() ) ) <= STOPPING_DISTANCE){
if( (this->position - ( currentVehicle->getVehiclePosition() + currentVehicle->getV_length() ) ) >= STOPPING_DISTANCE / 2 ){
// Stop
currentVehicle->setStatus(stopping);
}
if ( currentVehicle->getStatus() == idle){
return;
}
// currentVehicle->simulate();
}
else if ( (this->position - ( currentVehicle->getVehiclePosition() + currentVehicle->getV_length() ) ) <= SLOWING_DISTANCE && currentVehicle->getStatus() != stopping ){
// Slow down
currentVehicle->setStatus(decelerate);
// currentVehicle->simulate();
}
else{
// Clear to accelerate
currentVehicle->setStatus(accelerate);
// currentVehicle->simulate();
}
}
}
void TrafficLight::print() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling print");
cout << '\t' << "-> Road: " << this->road->getRoadName() << endl;
cout << '\t' << "-> Position: " << this->position << endl;
cout << '\t' << "-> Cycle: " << this->cyclus << endl;
}
TrafficLight::~TrafficLight() {
REQUIRE(this->properlyInitialized(), "TrafficSimulation was not initialized when calling destructor");
}