-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trooper.cpp
99 lines (78 loc) · 2.63 KB
/
Trooper.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
#include "Trooper.h"
void Trooper::init(vector<warehousePtr> &wHouseLst, shared_ptr<Warehouse> first) {
this->firstVisit = first;
setMapRouts(wHouseLst);
nextDest = getNextDest();
setNext(nextDest);
Vehicle::setSpeed(90);
}
void Trooper::getStatus() {
if(getState() == "Course")
cout << " Heading to " << getHeadingTo() << ", speed " << getSpeed() << "km/h" << endl;
else if(getState() == "Position")
cout << " Heading to (" << getHeadingTo() << ") , speed " << getSpeed() << "km/h" << endl;
else
cout<< " " + getState() << endl;
}
void Trooper::setMapRouts(vector<warehousePtr> &warehouses) {
for(auto& warehouse : warehouses){
_visitedMap.insert(make_pair(warehouse, false));
}
_visitedMap.at(firstVisit) = true;
}
shared_ptr<Warehouse> Trooper::getNextDest() {
warehousePtr currClosest;
bool found = false;
getClosest(found,currClosest);
if(!found){ // start new loop again.
makeAllFalse();
setNext(firstVisit);
return firstVisit;
}
setNext(currClosest);
return currClosest;
}
void Trooper::setNext(Vehicle::warehousePtr warehouse) {
setHeadingTo(warehouse->getName());
setCourse(Point::getAngle(getLoc(),warehouse->getLoc()));
setState("Course");
}
void Trooper::makeAllFalse() {
for(auto& warehouse : _visitedMap){
warehouse.second = false;
}
}
void Trooper::getClosest(bool &found, warehousePtr &currClosest) {
double minDist = LONG_MAX;
double currDist = 0;
for(auto &wareHouse: _visitedMap){ // loop over warehouses
if(!wareHouse.second){ // find one who hasn't been visited yet.
found = true;
currDist = Point::getDistance(*getLoc(),*wareHouse.first->getLoc()); // get distance from that warehouse.
if(currDist == minDist)
if(currClosest->getName()[0] > wareHouse.first->getName()[0]){ // pick the one with lower letter.
currClosest = wareHouse.first;
}
if(currDist < minDist) {
minDist = currDist;
currClosest = wareHouse.first;
}
}
}
}
Trooper::Trooper(string &name, const Point &location, string Type) : Vehicle(name, location, Type) {
}
void Trooper::update() {
if (getState() == "Position")
if (move(headingToPoint())) {
nextDest = getNextDest();
setNext(nextDest);
return;
}
if(getState() == "Course")
if (move(*nextDest->getLoc())){
nextDest = getNextDest();
_visitedMap.at(nextDest) = true;
setNext(nextDest);
}
}