blob: 2c4e96e682799a60af5c93dd38d619bdef9a30e7 (
plain) (
blame)
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
|
#include "SpaceAgency.h"
void SpaceAgency::free() {
delete[] hangars;
delete[] sites;
}
void SpaceAgency::copyFrom(const SpaceAgency& other) {
hangarsCount = other.hangarsCount;
hangarsAlloc = other.hangarsAlloc;
hangars = new Hangar[hangarsAlloc];
for (unsigned i = 0; i < hangarsCount; ++i)
hangars[i] = other.hangars[i];
sitesCount = other.sitesCount;
sitesAlloc = other.sitesAlloc;
sites = new LaunchSite[sitesAlloc];
for (unsigned i = 0; i < sitesCount; ++i)
sites[i] = other.sites[i];
}
void SpaceAgency::resizeHangars() {
hangarsAlloc = hangarsAlloc == 0 ? 8 : hangarsAlloc * 2;
Hangar* hangarsResized = new Hangar[hangarsAlloc];
for (unsigned i = 0; i < hangarsCount; ++i)
hangarsResized[i] = hangars[i];
delete[] hangars;
hangars = hangarsResized;
}
void SpaceAgency::resizeSites() {
sitesAlloc = sitesAlloc == 0 ? 8 : sitesAlloc * 2;
LaunchSite* sitesResized = new LaunchSite[sitesAlloc];
for (unsigned i = 0; i < sitesCount; ++i)
sitesResized[i] = sites[i];
delete[] sites;
sites = sitesResized;
}
SpaceAgency::SpaceAgency() {
hangars = nullptr;
sites = nullptr;
hangarsCount = hangarsAlloc = sitesCount = sitesAlloc = 0;
}
SpaceAgency::~SpaceAgency() {
free();
}
SpaceAgency::SpaceAgency(const SpaceAgency& other) {
copyFrom(other);
}
SpaceAgency& SpaceAgency::operator=(const SpaceAgency& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
SpaceAgency::SpaceAgency(SpaceAgency&& other) {
hangars = other.hangars;
hangarsAlloc = other.hangarsAlloc;
hangarsCount = other.hangarsCount;
sites = other.sites;
sitesAlloc = other.sitesAlloc;
sitesCount = other.sitesCount;
other.hangars = nullptr;
other.hangarsAlloc = other.hangarsCount = 0;
other.sites = nullptr;
other.sitesAlloc = other.sitesCount = 0;
}
SpaceAgency& SpaceAgency::operator=(SpaceAgency&& other) {
if (this != &other) {
free();
hangars = other.hangars;
hangarsAlloc = other.hangarsAlloc;
hangarsCount = other.hangarsCount;
sites = other.sites;
sitesAlloc = other.sitesAlloc;
sitesCount = other.sitesCount;
other.hangars = nullptr;
other.hangarsAlloc = other.hangarsCount = 0;
other.sites = nullptr;
other.sitesAlloc = other.sitesCount = 0;
}
return *this;
}
void SpaceAgency::AddHangar(const Hangar& hangar) {
if (hangarsCount >= hangarsAlloc)
resizeHangars();
hangars[hangarsCount++] = hangar;
}
Hangar* SpaceAgency::GetHangar(unsigned hangarIndex) {
if (hangarIndex >= hangarsCount)
return nullptr;
return &hangars[hangarIndex];
}
const Hangar* SpaceAgency::GetHangar(unsigned hangarIndex) const {
if (hangarIndex >= hangarsCount)
return nullptr;
return &hangars[hangarIndex];
}
void SpaceAgency::AddLaunchSite(const LaunchSite& site) {
if (sitesCount >= sitesAlloc)
resizeSites();
sites[sitesCount++] = site;
}
LaunchSite* SpaceAgency::GetLaunchSite(unsigned launchSiteIndex) {
if (launchSiteIndex >= sitesCount)
return nullptr;
return &sites[launchSiteIndex];
}
const LaunchSite* SpaceAgency::GetLaunchSite(unsigned launchSiteIndex) const {
if (launchSiteIndex >= sitesCount)
return nullptr;
return &sites[launchSiteIndex];
}
void SpaceAgency::AddRocketStage(const RocketStage& stage, unsigned hangarIndex) {
if (hangarIndex >= hangarsCount)
return;
hangars[hangarIndex].AddStage(stage);
}
unsigned SpaceAgency::AddRocket(const Rocket& rocket, unsigned hangarIndex) {
if (hangarIndex >= hangarsCount)
return -1; // Заради 2s complement това е само единици битове и резултата е най-голямото unsigned число
return hangars[hangarIndex].AddRocket(rocket);
}
void SpaceAgency::MoveRocketStageToRocket(unsigned stageHangar, unsigned stageIndex, unsigned rocketHangar, unsigned rocketIndex) {
if (stageHangar >= hangarsCount || rocketHangar >= hangarsCount)
return;
if (stageHangar != rocketHangar) {
stageIndex = hangars[rocketIndex].AddStage(hangars[stageIndex].RemoveStage(stageIndex));
}
hangars[rocketIndex].MoveStageToRocket(rocketIndex, stageIndex);
}
bool SpaceAgency::CanLaunch(unsigned siteIndex, unsigned hangarIndex, unsigned rocketIndex) const {
if (siteIndex >= sitesCount || hangarIndex >= hangarsCount)
return false;
const Rocket* rock = hangars[hangarIndex].GetRocket(rocketIndex);
if (rock == nullptr)
return false;
return rock->TotalHeight() <= sites[siteIndex].GetMaxHeight() && rock->TotalWeight() <= sites[siteIndex].GetMaxWeight();
}
|