-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cpp
253 lines (224 loc) · 7.27 KB
/
Model.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// Created by Admin on 6/18/2021.
//
#include "Model.h"
#include "Freighter.h"
#include "Boat_patrol.h"
#include "Cruiser.h"
std::unique_ptr<Model> Model::_ptr = nullptr; // initialized during or immediately after loading
Model& Model::get_Instance() {
if (_ptr == nullptr)
_ptr = std::unique_ptr<Model>(new Model());
return *(_ptr.get());
}
Model::Model() {
}
void Model::update() {
Port *closest = nullptr;
Boat_patrol *b_p = nullptr;
for (unsigned int i = 0; i < _Sim_object_vector.size(); i++) {
/* if the object is a patrol boat then do the next
* time step of the 3 ( refuel -> dock -> set next port destination) */
if (_Sim_object_vector[i]->getType() == patrol) {
switch ((dynamic_cast<Boat_patrol*>(_ship_vector[getShipByName(
_Sim_object_vector[i]->getName())].get()))->getStep()) {
case -1:
closest = findClosestPort(_Sim_object_vector[i]->getPosition());
destination(_Sim_object_vector[i]->getName(),
closest->getName(),
_ship_vector[getShipByName(
_Sim_object_vector[i]->getName())]->getSpeed());
_Sim_object_vector[i]->update();
break;
case 0:
_Sim_object_vector[i]->update();
break;
case 1:
_Sim_object_vector[i]->update();
break;
case 2:
break;
case 3:
// this is the step the patrol boat chooses the next close port
closest = findClosestPort(_Sim_object_vector[i]->getPosition());
b_p = dynamic_cast<Boat_patrol*>(_ship_vector[getShipByName(
_Sim_object_vector[i]->getName())].get());
destination(_Sim_object_vector[i]->getName(),
closest->getName(), b_p->getSpeed());
b_p->setStep(0);
break;
case 4:
// after we got in the port , now refuel (in other words add the ship to the queue of the
// ships the port will refuel
closest = findClosestPort(_Sim_object_vector[i]->getPosition());
closest->addShipToFuelQueue(
dynamic_cast<Boat_patrol*>(_ship_vector[getShipByName(
_Sim_object_vector[i]->getName())].get()));
break;
default:
break;
}
return;
} else {
_Sim_object_vector[i]->update();
}
}
}
void Model::addShip(Ship *newShip) {
_Sim_object_vector.emplace_back(newShip);
_ship_vector.emplace_back(newShip);
}
void Model::addCruiser(std::string name, Point position, int power,
double captureRange) {
if (name.length() > 2)
name = name.substr(0, 2);
std::unique_ptr<Cruiser> p = make_unique<Cruiser>(name, position, power,
captureRange);
auto a = new Cruiser(name, position, power, captureRange);
_Sim_object_vector.emplace_back(a);
_ship_vector.emplace_back(a);
}
void Model::addPort(Port *a) {
_Sim_object_vector.emplace_back(a);
_port_vector.emplace_back(a);
}
void Model::addPort(std::string name, double x, double y, double Fuel,
int containers, int FPH) {
if (name.length() > 2)
name = name.substr(0, 2);
auto p = new Port(name, x, y, Fuel, containers, FPH);
_Sim_object_vector.emplace_back(p);
_port_vector.emplace_back(p);
}
void Model::addView(View *newView) {
_view_vector.emplace_back(newView);
}
void Model::Attatch(Ship &newShip, Port &newPort) {
}
void Model::Dettatch(Ship &newShip, Port &newPort) {
}
const vector<std::unique_ptr<Sim_object>>& Model::getSimObjectVector() {
return _Sim_object_vector;
}
const vector<std::unique_ptr<Ship>>& Model::getShipVector() const {
return _ship_vector;
}
const vector<std::unique_ptr<Port>>& Model::getPortVector() const {
return _port_vector;
}
const vector<std::unique_ptr<View>>& Model::getViewVector() const {
return _view_vector;
}
int Model::getShipByName(const std::string &name) {
for (unsigned int i = 0; i < _ship_vector.size(); ++i) {
auto &k = _ship_vector.at(i);
if (k != nullptr && k.get()->getName() == name) {
return i;
}
}
throw "Ship not found !";
}
Port* Model::getPortByName(std::string name) {
int q = 0;
for (auto &i : _port_vector) {
q++;
if (i->getName() == name) {
auto temp = i.get();
// _port_vector.erase(_port_vector.cbegin()+q);
return temp;
}
}
throw "Port not found !";
}
void Model::destination(std::string shipName, std::string portName,
double speed) {
int q = getShipByName(shipName);
if (_ship_vector[q]->getType() == cruiser)
throw "ERROR Cruiser only moves in a direction";
_ship_vector[q]->destination(speed, portName);
}
void Model::course(std::string shipName, double angle, double speed) {
auto a = getShipByName(shipName);
auto b = _ship_vector[a].get();
b->Course(angle, speed);
}
void Model::position(std::string shipName, double x, double y, double speed) {
auto a = getShipByName(shipName);
if (_ship_vector[a]->getType() == cruiser)
throw "ERROR Cruiser only moves in a direction";
else
_ship_vector[getShipByName(shipName)].get()->Position(x, y, speed);
}
void Model::load_at(std::string shipName, std::string portName) {
auto a = getShipByName(shipName);
auto port = getPortByName(portName);
if (_ship_vector[a]->getType() != freighter) {
throw "ERROR only freighter can load ";
}
if (inProximity(_ship_vector[a]->getPosition(), port->getPosition(), 0.1)) {
int needed = MAX_CONTAINERS_IN_FREIGHTER
- dynamic_cast<Freighter&>(*_ship_vector[a]).getContainers();
int final =
(port->getContainers() > needed) ?
needed : port->getContainers();
dynamic_cast<Freighter&>(*_ship_vector[a]).load(final);
port->setContainers(port->getContainers() - final);
}
}
void Model::unload_at(std::string shipName, std::string portName,
double numberOfShipmentsToUnload) {
auto a = getShipByName(shipName);
auto port = getPortByName(portName);
if (_ship_vector[a]->getType() != freighter)
throw "ERROR only freighter can load ";
if (inProximity(_ship_vector[a]->getPosition(), port->getPosition(), 0.1)) {
int needed = MAX_CONTAINERS_IN_PORT - port->getContainers();
int final =
(dynamic_cast<Freighter&>(*_ship_vector[a]).getContainers()
> needed) ? needed : port->getContainers();
port->setContainers(port->getContainers() + final);
dynamic_cast<Freighter&>(*_ship_vector[a]).unload(final);
}
}
void Model::addBoatPatrol(string &name, Point position, const int &resistance) {
if (name.length() > 2)
name = name.substr(0, 2);
std::unique_ptr<Boat_patrol> p = make_unique<Boat_patrol>(name, position,
resistance);
auto a = new Boat_patrol(name, position, resistance);
_Sim_object_vector.emplace_back(a);
_ship_vector.emplace_back(a);
}
void Model::addFreighter(string &name, const Point &position,
const int &resistance, int container) {
if (name.length() > 2) {
name = name.substr(0, 2);
}
std::unique_ptr<Freighter> p = make_unique<Freighter>(name, position,
resistance, container);
auto a = new Freighter(name, position, resistance, container);
_Sim_object_vector.emplace_back(a);
_ship_vector.emplace_back(a);
}
Port* Model::findClosestPort(const Point &point) {
if (_port_vector.empty() == true) {
throw "there is no ports yet !";
}
Port *found = nullptr;
Point temp = _port_vector[0]->getPosition();
double min_dist = sqrt(
pow(point.x - temp.x, 2) + pow(point.y - temp.y, 2) * 1.0), dist;
for (unsigned int i = 0; i < _port_vector.size(); i++) {
temp = _port_vector[i]->getPosition();
dist = sqrt(pow(point.x - temp.x, 2) + pow(point.y - temp.y, 2) * 1.0);
if (dist <= min_dist) {
found = _port_vector[i].get();
min_dist = dist;
}
}
return found;
}
void Model::status() {
for (auto &i : _Sim_object_vector)
i->status();
}