-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
121 lines (97 loc) · 2.88 KB
/
Copy pathModel.cpp
File metadata and controls
121 lines (97 loc) · 2.88 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
#include "Model.h"
#include <sstream>
void Model::addSimObj(const std::shared_ptr<Sim_obj>& obj) {
sim_objects.push_back(obj);
}
void Model::addVehicle(const std::shared_ptr<Vehicle>& v) {
vehicles.push_back(v);
sim_objects.push_back(v);
}
void Model::addWarehouse(const std::shared_ptr<Warehouse>& w) {
warehouses.push_back(w);
sim_objects.push_back(w);
}
const std::vector<std::shared_ptr<Warehouse>>& Model::getWarehouses() const {
return warehouses;
}
const std::vector<std::shared_ptr<Vehicle>>& Model::getVehicles() const {
return vehicles;
}
void Model::go() {
// First pass: warehouses and vehicles that move independently.
for (auto const& obj : sim_objects) {
if (!obj->defersUpdate()) {
obj->update();
}
}
// Second pass: objects that react to others' final positions (choppers).
for (auto const& obj : sim_objects) {
if (obj->defersUpdate()) {
obj->update();
}
}
time++;
}
void Model::status() const {
std::cout << std::endl;
for(const std::shared_ptr<Sim_obj>& sim : Model::sim_objects) {
sim->broadcast_current_state();
}
std::cout << std::endl;
}
void Model::incrementTime(double hours) {
time += hours;
}
double Model::getTime() const {
return time;
}
void Model::insert_objects() {
// insert_obj() maps world coordinates to grid cells and clips anything
// outside the visible window, so no bounds checking is needed here.
for (auto const& obj : vehicles) {
auto loc = obj->get_location();
view->insert_obj(loc->x, loc->y, obj->getName().substr(0, 2));
}
for (auto const& obj : warehouses) {
auto loc = obj->get_location();
view->insert_obj(loc->x, loc->y, obj->getName().substr(0, 2));
}
}
const shared_ptr<View> & Model::getView() const {
return view;
}
std::shared_ptr<Sim_obj> Model::findSimObjByName(const std::string &name) {
for (const auto& obj : sim_objects) {
if (obj->getName() == name) {
return obj;
}
}
return nullptr;
}
double Model::parseTime(const std::string& timeStr) {
int hours = 0, minutes = 0;
char colon;
std::istringstream iss(timeStr);
iss >> hours >> colon >> minutes;
if (!iss || colon != ':' || hours < 0 || minutes < 0 || minutes >= 60) {
std::cerr << "ERROR: Invalid time format: " << timeStr << "\n";
return 0.0;
}
return hours + minutes / 60.0;
}
std::shared_ptr<Vehicle> Model::findVehicleByName(const std::string &name) {
for (const auto& obj : vehicles) {
if (obj->getName() == name) {
return obj;
}
}
return nullptr;
}
std::shared_ptr<Warehouse> Model::findWarehouseByName(const std::string &name) {
for (const auto& obj : warehouses) {
if (obj->getName() == name) {
return obj;
}
}
return nullptr;
}