-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrewDragonRocketship.cpp
239 lines (216 loc) · 6.42 KB
/
CrewDragonRocketship.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
#include "CrewDragonRocketship.h"
using namespace std;
CrewDragonRocketship::CrewDragonRocketship(string n) : Rocketship(n, 'c')
{
cargo = nullptr;
spacecraft = nullptr;
rocket = nullptr;
}
CrewDragonRocketship::~CrewDragonRocketship()
{
if (cargo != nullptr)
{
if (spacecraft != nullptr)
for (int i = 0; i < spacecraft->getCapacity(); i++)
cargo[i] = nullptr;
delete[] cargo;
}
}
void CrewDragonRocketship::Launch(Station *ss)
{
this->countdown();
rocket->turnOn();
this->attachToStation(ss);
this->dropCargo();
}
Rocketship *CrewDragonRocketship::clone()
{
CrewDragonRocketship *temp = new CrewDragonRocketship(this->getName());
temp->attachSpacecraft((this->getSpacecraft())->clone());
temp->attachRocket((this->getRocket())->clone());
if (cargo != nullptr)
{
vector<Cargo *> c;
for (int i = 0; i < spacecraft->getCapacity() - 1; i++)
{
if (cargo[i] != nullptr)
c.push_back(cargo[i]->clone());
}
temp->attachCargo(c);
}
return temp;
}
void CrewDragonRocketship::attachSpacecraft(Spacecraft *s)
{
spacecraft = s;
}
void CrewDragonRocketship::attachRocket(FalconRocket *r)
{
rocket = r;
}
void CrewDragonRocketship::attachCargo(vector<Cargo *> cvect)
{
cargo = new Cargo *[spacecraft->getCapacity()];
int toFill = cvect.size();
if (toFill > spacecraft->getCapacity())
{
cout << "There is not enough space on the " << this->getName() << " for all the cargo" << endl;
toFill = spacecraft->getCapacity(); // only fill the available space
}
for (int i = 0; i < toFill; i++)
cargo[i] = cvect[i];
}
void CrewDragonRocketship::dropCargo()
{
Station *station = this->getStation();
if (cargo[0] == nullptr)
cout << "There is no Cargo to unload" << endl;
for (int i = 0; i < spacecraft->getCapacity(); i++)
{
if (cargo[i] != nullptr)
{
station->receiveCargo(cargo[i]);
cargo[i] = nullptr;
}
}
}
int CrewDragonRocketship::getRockets()
{
return rocket->getEngine()->EngineCount();
}
bool CrewDragonRocketship::testFire()
{
return rocket->getEngine()->testFire();
}
bool CrewDragonRocketship::testLoading()
{
return cargo == NULL ? false : true;
}
void CrewDragonRocketship::startLanding(Station *base)
{
Station *s = this->getStation();
bool loopBack;
loopBack = false;
string response;
string addOn;
vector<Cargo *> temp;
cout << "Landing for " << this->getName() << " initialised" << endl;
// cargo loop
do
{
loopBack = false;
cout << "Do you want to load " << addOn << "equipment from the space station? (Y/N): ";
cin >> response;
if (response == "Y")
{
if (!s->equipment.empty())
{
int index;
int amount;
cout << "Choose an index from the list: " << endl;
s->printEquipment();
cin >> index;
while (!cin.good() || index <= 0)
{
cin.clear();
cin.ignore(15, '\n');
cout << "Please enter a valid index: ";
cin >> index;
}
cout << "How many " << s->equipment.at(index - 1).first->getName() << "s do you want to load";
cin >> amount;
while (!cin.good() || amount <= 0)
{
cin.clear();
cin.ignore(15, '\n');
cout << "Please enter a valid amount: ";
cin >> amount;
}
pair<Cargo *, int> p = s->loadEquipment(index - 1, amount);
if (p.second < 0)
cout << "Please enter a valid index." << endl;
else
{
for (int i = 0; i < p.second - 1; i++)
{
temp.push_back(p.first->clone());
}
temp.push_back(p.first);
}
loopBack = true;
addOn = "more ";
}
else
{
cout << "No equipment to load" << endl;
loopBack = false;
}
}
else if (response != "N")
{
cout << "Please input only Y or N." << endl;
loopBack = true;
}
else
loopBack = false;
} while (loopBack);
// human loop
addOn = "";
do
{
cout << "Do you want to board " << addOn << "humans from the space station? (Y/N): ";
cin >> response;
if (response == "Y")
{
if (!s->humans.empty())
{
int index;
int amount;
cout << "Choose an index from the list: " << endl;
s->printHumans();
cin >> index;
while (!cin.good() || index <= 0)
{
cin.clear();
cin.ignore(15, '\n');
cout << "Please enter a valid index: ";
cin >> index;
}
Cargo *c = s->loadHumans(index - 1);
if (c == nullptr)
cout << "Please enter a valid index." << endl;
else
temp.push_back(c);
loopBack = true;
addOn = "more ";
}
else
{
cout << "No humans to board" << endl;
loopBack = false;
}
}
else if (response != "N")
{
cout << "Please input only Y or N. " << endl;
loopBack = true;
}
else
loopBack = false;
} while (loopBack);
this->attachCargo(temp);
cout << this->getName() << " begins its journey back to earth" << endl;
this->attachToStation(base);
}
Spacecraft *CrewDragonRocketship::getSpacecraft()
{
return spacecraft;
}
FalconRocket *CrewDragonRocketship::getRocket()
{
return rocket;
}
Cargo **CrewDragonRocketship::getCargo()
{
return cargo;
}