This repository has been 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
/
BusStop.cpp
148 lines (134 loc) · 6.73 KB
/
BusStop.cpp
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
#include "BusStop.h"
#include "Vehicle.h"
#include "DesignByContract.h"
#include <typeinfo>
BusStop::BusStop(int new_waitTime, int new_position, Road *new_road) : cooldown(new_waitTime) , waitTime(new_waitTime), position(new_position), road(new_road) , init(this) {
REQUIRE(*typeid(new_waitTime).name() == 'i' , "new_waitTime was not an int when calling constructor");
REQUIRE(*typeid(new_position).name() == 'i' , "new_position was not an int when calling constructor");
REQUIRE(*typeid(new_road).name() == 'P' , "new_road was not a pointer when calling constructor");
REQUIRE(new_road == NULL || new_road->properlyInitialized() , "new_road was not properly initialized when calling constructor");
ENSURE(road == new_road, "road was not correctly initialized");
ENSURE(cooldown == new_waitTime, "cooldown was not correctly initialized");
ENSURE(waitTime == new_waitTime , "waitTime was not correctly initialized");
ENSURE(position == new_position, "position was not correctly initialized");
ENSURE(properlyInitialized(), "BusStop constructor must end in properlyInitialized state");
}
BusStop::BusStop() : cooldown(0) , waitTime(0), position(0), road(NULL) , init(this) {
ENSURE(road == NULL, "road was not correctly initialized");
ENSURE(cooldown == 0, "cooldown was not correctly initialized");
ENSURE(waitTime == 0 , "waitTime was not correctly initialized");
ENSURE(position == 0, "position was not correctly initialized");
ENSURE(properlyInitialized(), "BusStop constructor must end in properlyInitialized state");
}
bool BusStop::properlyInitialized() const {
return init == this;
}
int BusStop::getCooldown() const {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling getCooldown");
return cooldown;
}
void BusStop::setCooldown(int new_cooldown) {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling setCooldown");
REQUIRE( *typeid(new_cooldown).name() == 'i' , "new_cooldown was not an int when calling setCooldown");
BusStop::cooldown = new_cooldown;
ENSURE(cooldown == new_cooldown, "cooldown was not assigned to new_cooldown, after calling setCooldown");
}
int BusStop::getWaitTime() const {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling getWaitTime");
return waitTime;
}
void BusStop::setWaitTime(int newWaitTime) {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling setWaitTime");
REQUIRE(*typeid(newWaitTime).name() == 'i' , "newWaitTime was not an int when calling setWaitTime");
BusStop::waitTime = newWaitTime;
ENSURE(waitTime == newWaitTime, "waitTime was not assigned to newWaitTime, when calling setWaitTime");
}
int BusStop::getPosition() const {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling getPosition");
return position;
}
void BusStop::setPosition(int newPosition) {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling setPosition");
REQUIRE(*typeid(newPosition).name() == 'i' , "newPosition was not an int when calling setPosition");
BusStop::position = newPosition;
ENSURE(position == newPosition, "position was not assigned to newPosition, when calling setPosition");
}
Road *BusStop::getRoad() const {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling getRoad");
return road;
}
void BusStop::setRoad(Road *newRoad) {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling setRoad");
REQUIRE(*typeid(newRoad).name() == 'P' , "newRoad was not a pointer when calling setRoad");
REQUIRE(newRoad->properlyInitialized(), "road was not properly initialized when calling setRoad");
BusStop::road = newRoad;
ENSURE(road == newRoad, "road was not assigned to newRoad, when calling setRoad");
}
Vehicle *BusStop::getNearestBus() const {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling getNearestBus");
double pos = this->getPosition();
Vehicle* nearestVehicle = NULL;
Vehicle* currentVehicle;
// Get bus type vehicles
vector<Vehicle*> buses;
for (int j = 0; j < this->road->getVehicleAmount(); ++j) {
currentVehicle = this->road->getVehicle(j);
// Check for correct type
if(currentVehicle->getType() == T_BUS && currentVehicle->getVehiclePosition() < pos){
buses.push_back(currentVehicle);
// Update nearest vehicle
if(buses.size() == 1){
nearestVehicle = currentVehicle;
}
else if(nearestVehicle->getVehiclePosition() < currentVehicle->getVehiclePosition()){
nearestVehicle = currentVehicle;
}
}
}
return nearestVehicle;
}
void BusStop::simulateBusStop() {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling simulateBusStop");
// Get nearest bus type vehicle
Vehicle* currentVehicle = getNearestBus();
// If there are no buses , or all have passed the bus stop
if(currentVehicle == NULL){
return;
}
if(currentVehicle->getType() == T_BUS) {
// If bus is in bus stop
if(currentVehicle->getStatus() == idle){
// Waiting time is over
if(cooldown < 0){
currentVehicle->setStatus(accelerate);
currentVehicle->setSlowing_bus(false);
currentVehicle->setStopping_bus(false);
currentVehicle->setLeaving_bus(true);
cooldown = waitTime;
ENSURE(cooldown == waitTime, "cooldown wasn't reset");
}
else{
cooldown--;
}
}
// If bus already left
if(currentVehicle->isLeaving_bus()){
return;
}
// Check if within stopping distance
if (position - ( currentVehicle->getVehiclePosition() + currentVehicle->getV_length() ) < STOPPING_DISTANCE && !currentVehicle->isStopping_bus() ) {
currentVehicle->setStatus(stopping);
currentVehicle->setStopping_bus(true);
currentVehicle->setSlowing_bus(false);
}
// check if within slowing distance
else if (position - ( currentVehicle->getVehiclePosition() + currentVehicle->getV_length() ) < SLOWING_DISTANCE && !( currentVehicle->isStopping_bus() || currentVehicle->isSlowing_bus() ) ) {
currentVehicle->setStatus(decelerate);
currentVehicle->setSlowing_bus(true);
currentVehicle->setStopping_bus(false);
}
}
}
BusStop::~BusStop() {
REQUIRE(this->properlyInitialized(), "BusStop was not properly initialized when calling destructor");
}