-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.cpp
More file actions
104 lines (87 loc) · 3.12 KB
/
Copy pathTrain.cpp
File metadata and controls
104 lines (87 loc) · 3.12 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
#include "Train.h"
#include <sstream>
#include <algorithm>
Train::Train(int id, int starting_station_id, double max_capacity)
: id(id), current_station_id(starting_station_id),
destination_station_id(starting_station_id),
current_route_index(0), status(TrainStatus::IDLE), max_capacity(max_capacity),
current_cargo_weight(0.0), speed(0.0), segment_start_time(0.0),
segment_travel_time(0.0), progress_fraction(0.0) {}
void Train::setRoute(const std::vector<int>& route) {
planned_route = route;
current_route_index = 0;
if (!route.empty()) {
destination_station_id = route.back();
}
}
void Train::setDestination(int dest_station_id) {
destination_station_id = dest_station_id;
}
void Train::setStatus(TrainStatus new_status) {
status = new_status;
}
void Train::updateLocation(const GPS& location) {
current_location = location;
}
void Train::setSpeed(double new_speed) {
speed = new_speed;
}
void Train::setCurrentStation(int station_id) {
current_station_id = station_id;
}
void Train::advanceRoute() {
current_route_index++;
}
void Train::startSegment(double current_time, double travel_time) {
segment_start_time = current_time;
segment_travel_time = travel_time;
progress_fraction = 0.0;
}
bool Train::loadPackage(std::shared_ptr<Package> package) {
if (current_cargo_weight + package->getWeight() <= max_capacity) {
cargo.push_back(package);
current_cargo_weight += package->getWeight();
package->assignToTrain(id);
return true;
}
return false;
}
std::vector<std::shared_ptr<Package>> Train::unloadPackagesForStation(int station_id) {
std::vector<std::shared_ptr<Package>> unloaded;
auto it = cargo.begin();
while (it != cargo.end()) {
if ((*it)->getDestination() == station_id) {
unloaded.push_back(*it);
current_cargo_weight -= (*it)->getWeight();
it = cargo.erase(it);
} else {
++it;
}
}
return unloaded;
}
void Train::move(double time_delta, const GPS& target_location) {
double lat_diff = target_location.latitude - current_location.latitude;
double lon_diff = target_location.longitude - current_location.longitude;
double distance = std::sqrt(lat_diff * lat_diff + lon_diff * lon_diff);
if (distance > 0.001) {
double move_fraction = std::min(1.0, (speed * time_delta) / distance);
current_location.latitude += lat_diff * move_fraction;
current_location.longitude += lon_diff * move_fraction;
}
}
std::string Train::getInfo() const {
std::ostringstream oss;
oss << "Train #" << id << " [" << trainStatusToString(status) << "] ";
oss << "Station: " << current_station_id << " -> " << destination_station_id;
oss << " | Cargo: " << cargo.size() << " packages ("
<< current_cargo_weight << "/" << max_capacity << " kg)";
return oss.str();
}
std::vector<int> Train::getPackageDestinations() const {
std::vector<int> destinations;
for (const auto& pkg : cargo) {
destinations.push_back(pkg->getDestination());
}
return destinations;
}