-
Notifications
You must be signed in to change notification settings - Fork 0
/
Region.cpp
245 lines (196 loc) · 7.89 KB
/
Region.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
/*
Create an array of 1000 sectors.
Randomly pick a 5 to 25 sector group.
For each of the sectors in the group:
Connect it(A) to a random one(B) of the others
Connect (B) to (A)
Randomly pick 5 to 25 more sectors, but make sure they haven''t been picked before.
Do the same loop to connect those sectors.
After the final group is settled, you'll have a selection of grouped sectors. The grouped
sectors each should have their own name, like "The Marmalade Nebulae" or "Halaf Zone" or whatever.
You should have 40-200 of these groups. You can, of course, adjust the amount by changing the
amount of sectors per group.
To connect these groups to each other, do something similar.
Loop through each group and connect one to five or so of the sectors randomly to one of the other groups.
This method still leaves the chance that there are sectors that cannot ever reach other sectors.
You'd have to run through a pathfinding algorithm for each sector from the same starting point to be sure of that.
*/
#include "Region.h"
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
Region::Region()
{
connectedSectors = 0;
for (int i = 1; i <= MAX_SECTORS; i++)
{
// Sector(int num, string region, string sector, bool exp, bool link);
sectors[i] = new Sector(i, regionPrefix[rand() % 5] + " " + regionSuffix[rand() % 5], "Empty Sector", false, false);
}
linkSectors();
}
Region::~Region()
{
delete sectors;
}
// Sector Wrapper: Displays the full region info
void Region::displayRegionFull()
{
for (int i = 1; i <= MAX_SECTORS; i++)
{
sectors[i]->displaySectorFull();
}
}
// Sector Wrapper: Displays the short region info
void Region::displayRegionShort()
{
int zero, one, two, three, four, five, six = 0;
zero = one = two = three = four = five = six;
for (int i = 1; i <= MAX_SECTORS; i++)
{
sectors[i]->displaySectorShort();
}
for (int j = 1; j <= MAX_SECTORS; j++)
{
if (sectors[j]->getConnections() == 0)
zero++;
if (sectors[j]->getConnections() == 1)
one++;
if (sectors[j]->getConnections() == 2)
two++;
if (sectors[j]->getConnections() == 3)
three++;
if (sectors[j]->getConnections() == 4)
four++;
if (sectors[j]->getConnections() == 5)
five++;
if (sectors[j]->getConnections() == 6)
six++;
}
cout << "Sectors with (1) connection: " << one << endl;
cout << "Sectors with (2) connection: " << two << endl;
cout << "Sectors with (3) connection: " << three << endl;
cout << "Sectors with (4) connection: " << four << endl;
cout << "Sectors with (5) connection: " << five << endl;
cout << "Sectors with (6) connection: " << six << endl;
cout << "Sectors with (0) connection: " << zero << endl;
}
// Sector Wrapper: Displays the full sector info
void Region::displaySectorFull(int s)
{
sectors[s]->displaySectorFull();
}
// Sector Wrapper: Displays the short sector info
void Region::displaySectorShort(int s)
{
sectors[s]->displaySectorShort();
}
// Sector Wrapper: Returns true if the passed connection exists in the sector (valid movement etc)
bool Region::checkSectors(int c, int s)
{
return sectors[c]->checkSectors(s);
}
// Function runs through all of the sectors and links them together to form a region
void Region::linkSectors()
{
//int tempNum = 0;
srand(time(NULL));
vector<int> frontVec;
vector<int> backVec;
vector<int>::iterator it;
// Push the first half of the sector #'s onto the vector list
for (int i = 1; i <= MAX_SECTORS / 2; i++)
frontVec.push_back(i);
// Push the second half of the sector #'s onto the vector list
for (int i = MAX_SECTORS / 2 + 1; i <= MAX_SECTORS; i++)
backVec.push_back(i);
// Shuffle the entire vector list
random_shuffle(frontVec.begin(), frontVec.end());
random_shuffle(backVec.begin(), backVec.end());
//vector<int> vec1 (vec.begin(), vec.end());
// Randomly link half of all the sectors to half of the other sectors and back.
for (int i = 1; i <= MAX_SECTORS / 2; i++)
{
//if (sectors[i]->getConnections() == 0)
//{
sectors[frontVec.back()]->setConnections(0, backVec.back());
sectors[backVec.back()]->setConnections(0, frontVec.back());
//cout << "Front = " << setw(2) << left << frontVec.back() << " Back = " << backVec.back() << endl;
// Set each linked sector link to true
sectors[frontVec.back()]->setLinked(true);
sectors[backVec.back()]->setLinked(true);
frontVec.pop_back();
backVec.pop_back();
// }
}
// Push the first half of the sector #'s onto the vector list
for (int i = 1; i <= MAX_SECTORS / 3; i++)
frontVec.push_back(i);
// Push the second half of the sector #'s onto the vector list
for (int i = MAX_SECTORS / 3 + 1; i <= MAX_SECTORS; i++)
backVec.push_back(i);
// Shuffle the entire vector list
random_shuffle(frontVec.begin(), frontVec.end());
random_shuffle(backVec.begin(), backVec.end());
//vector<int> vec2 (vec.begin(), vec.end());
// Randomly link 1/3 of all the sectors to the remaiming 1/3
for (int i = 1; i <= MAX_SECTORS / 3; i++)
{
sectors[frontVec.back()]->setConnections(1, backVec.back());
sectors[backVec.back()]->setConnections(1, frontVec.back());
//cout << "Front = " << setw(2) << left << frontVec.back() << " Back = " << backVec.back() << endl;
// Set each linked sector link to true
sectors[frontVec.back()]->setLinked(true);
sectors[backVec.back()]->setLinked(true);
frontVec.pop_back();
backVec.pop_back();
}
// Push the first half of the sector #'s onto the vector list
for (int i = 1; i <= MAX_SECTORS / 2; i++)
frontVec.push_back(i);
// Push the second half of the sector #'s onto the vector list
for (int i = MAX_SECTORS / 2 + 1; i <= MAX_SECTORS; i++)
backVec.push_back(i);
// Shuffle the entire vector list
random_shuffle(frontVec.begin(), frontVec.end());
random_shuffle(backVec.begin(), backVec.end());
//vector<int> vec2 (vec.begin(), vec.end());
// Randomly link 1/3 of all the sectors to the remaiming 1/3
for (int i = 1; i <= MAX_SECTORS / 4; i++)
{
sectors[frontVec.back()]->setConnections(2, backVec.back());
sectors[backVec.back()]->setConnections(2, frontVec.back());
//cout << "Front = " << setw(2) << left << frontVec.back() << " Back = " << backVec.back() << endl;
// Set each linked sector link to true
sectors[frontVec.back()]->setLinked(true);
sectors[backVec.back()]->setLinked(true);
frontVec.pop_back();
backVec.pop_back();
}
// Push the first half of the sector #'s onto the vector list
for (int i = 1; i <= MAX_SECTORS / 2; i++)
frontVec.push_back(i);
// Push the second half of the sector #'s onto the vector list
for (int i = MAX_SECTORS / 2 + 1; i <= MAX_SECTORS; i++)
backVec.push_back(i);
// Shuffle the entire vector list
random_shuffle(frontVec.begin(), frontVec.end());
random_shuffle(backVec.begin(), backVec.end());
//vector<int> vec2 (vec.begin(), vec.end());
// Randomly link 1/3 of all the sectors to the remaiming 1/3
for (int i = 1; i <= MAX_SECTORS / 5; i++)
{
sectors[frontVec.back()]->setConnections(3, backVec.back());
sectors[backVec.back()]->setConnections(3, frontVec.back());
//cout << "Front = " << setw(2) << left << frontVec.back() << " Back = " << backVec.back() << endl;
// Set each linked sector link to true
sectors[frontVec.back()]->setLinked(true);
sectors[backVec.back()]->setLinked(true);
frontVec.pop_back();
backVec.pop_back();
}
}