Repository navigation
Expand file tree
/
Copy pathMain.cpp
More file actions
363 lines (304 loc) · 11.2 KB
/
Copy pathMain.cpp
File metadata and controls
363 lines (304 loc) · 11.2 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "SlimeArea.cpp"
#include "BiomeAreaEvaluator.cpp"
#include <iostream>
#include <cstdint>
#include <vector>
#include <future>
#include <mutex>
#include <thread>
#include <optional>
#include <chrono>
auto getTaskAreasFromUserInput(const uint32_t radius)
{
struct Area
{
uint32_t beginX;
uint32_t beginZ;
uint32_t endX;
uint32_t endZ;
};
std::vector<Area> areas;
const uint32_t ConstantArea = 3749998*2;
const uint32_t ZStep = ConstantArea/radius;
uint32_t currentZ = 0;
do
{
Area area;
area.beginX = 0;
area.beginZ = currentZ;
currentZ+=ZStep;
if(currentZ > radius*2-15)
currentZ = radius*2-15;
area.endX = radius*2-15;
area.endZ = currentZ;
areas.push_back(area);
}
while(currentZ < radius*2-15);
return areas;
}
class SlimeChunkPrinter
{
public:
static void print(const GlobalSeedMatrix& matrix, const uint32_t matrixX, const uint32_t matrixZ, const uint32_t farmHeight)
{
uint32_t chunkCount = 0;
for(uint32_t j = 0; j < 16; j++)
{
for(uint32_t i = 0; i < 16; i++)
{
const bool isSlimeChunk = Util::isSlimeChunk(matrix, matrixX+i, matrixZ+j);
const bool isWithinSphere = Util::isChunkWithinSphere(i, j, farmHeight);
chunkCount+=isSlimeChunk&isWithinSphere;
std::cout << (isSlimeChunk&isWithinSphere ? 'S' : ' ');
}
std::cout << std::endl;
}
std::cout << chunkCount << " chunks in use" << std::endl;
}
};
template<uint32_t i = 1>
void findBestSlimePlace(const uint32_t farmHeight,
const int minecraftVersion,
const uint64_t seed,
const uint32_t radius,
const auto& matrix,
const auto& area,
auto& sharedVolume,
auto& toReturn)
{
if(i == farmHeight)
_findBestSlimePlace<i>(minecraftVersion, seed, radius, matrix, area, sharedVolume, toReturn);
else if constexpr(i <= 32)
findBestSlimePlace<i+1>(farmHeight, minecraftVersion, seed, radius, matrix, area, sharedVolume, toReturn);
}
template<uint32_t farmHeight>
void _findBestSlimePlace(const int minecraftVersion,
const uint64_t seed,
const uint32_t radius,
const auto& matrix,
const auto& area,
auto& sharedVolume,
auto& toReturn)
{
uint32_t localVolumeWorthChecking = sharedVolume.getVolumeWorthChecking();
for(uint32_t j = area.beginZ; j < area.endZ; j++)
{
SlimeArea<farmHeight> slimeArea(matrix, area.beginX, j);
uint32_t i = area.beginX;
do
{
const auto getDistanceSquared = [](const uint32_t x, const uint32_t z)->uint64_t
{
const uint64_t X = static_cast<uint64_t>(x);
const uint64_t Z = static_cast<uint64_t>(z);
return X*X + Z*Z;
};
const uint32_t volume = slimeArea.getTotalVolume();
const auto queryCheckVolume = [&sharedVolume, &localVolumeWorthChecking, &volume]()->bool
{
const uint32_t queriedVolume = sharedVolume.getVolumeWorthChecking();
if(volume > queriedVolume)
return true;
localVolumeWorthChecking = queriedVolume;
return false;
};
if(volume > localVolumeWorthChecking
&& queryCheckVolume())
{
const BiomeAreaEvaluatorParams biomeEvaluatorParams {
minecraftVersion,
seed,
radius,
farmHeight,
i, j,
matrix
};
const uint64_t biomeScore = BiomeAreaEvaluator::evaluate(biomeEvaluatorParams);
if(biomeScore > toReturn.score
|| (biomeScore == toReturn.score && getDistanceSquared(i, j) < getDistanceSquared(toReturn.x, toReturn.z)))
{
toReturn.score = biomeScore;
toReturn.x = i;
toReturn.z = j;
localVolumeWorthChecking = biomeScore/g_OneFullBlockPackSpawnScore;
sharedVolume.setPotentiallyBiggerVolume(localVolumeWorthChecking);
}
}
i++;
if(i >= area.endX) break;
slimeArea.shiftToPositiveX();
}
while(true);
}
}
class ProgressUpdate
{
public:
ProgressUpdate(const std::size_t totalTasks):
m_TotalTasks(totalTasks){}
void reportTaskDone()
{
{
std::lock_guard lock(m);
currentTasks++;
reported = true;
}
cv.notify_one();
}
void printWorker() noexcept try
{
const auto timeOnStart = std::chrono::steady_clock::now();
while(true)
{
std::unique_lock lock(m);
cv.wait(lock, [&]{ return reported; });
const auto timePassedSeconds = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now()-timeOnStart);
const auto speed = static_cast<float>(timePassedSeconds.count() == 0 ? 0.0f : 1.0f*currentTasks/timePassedSeconds.count());
using NumberType = decltype(timePassedSeconds/timePassedSeconds);
const NumberType secondsLeft = (speed == 0.0f ? 0 : (m_TotalTasks-currentTasks)/speed);
std::cout << '[' << currentTasks << '/' << m_TotalTasks << "] "
<< std::setw(2) << std::setfill('0') << secondsLeft/3600 << ':'
<< std::setw(2) << std::setfill('0') << secondsLeft%3600/60 << ':'
<< std::setw(2) << std::setfill('0') << secondsLeft%60 << " left" << std::endl;
reported = false;
if(currentTasks >= m_TotalTasks) break;
}
}
catch(...){}
private:
const std::size_t m_TotalTasks;
std::size_t currentTasks = 0;
bool reported = false;
std::mutex m{};
std::condition_variable cv{};
};
int32_t main()
{
//Seed input
uint64_t seed = 0;
std::cout << "Enter seed\n: ";
std::cin >> seed;
//Radius input
uint32_t radius = 0;
std::cout << "Enter Radius\n(min=8, max=1874999): ";
std::cin >> radius;
if(radius < 8 || radius > 1874999)
{
std::cout << "Please enter proper radius value" << std::endl;
return 1;
}
//farm height input
uint32_t farmHeight = 0;
std::cout << "Enter farm height\n(min=1, max=32): ";
std::cin >> farmHeight;
if(farmHeight < 1 || farmHeight > 32)
{
std::cout << "Please enter proper farm height value" << std::endl;
return 1;
}
//Thread number input
const uint32_t hardwareThreadNum = std::thread::hardware_concurrency();
uint32_t threadNum = 0;
std::cout << "Enter thread number\n(suggestion=" << hardwareThreadNum << "): ";
std::cin >> threadNum;
if(threadNum == 0)
{
std::cout << "Please enter proper thread number" << std::endl;
return 1;
}
//minecraft version
const auto minecraftVersion = Version::getMinecraftVersionFromUserInput();
std::cout << "It might be a bit slow at first, but it will eventually get faster..." << std::endl;
//vector of tasks
auto areas = getTaskAreasFromUserInput(radius);
std::mutex areaMutex; //tasks are used by multiple threads
const GlobalSeedMatrix matrix(seed, radius*2);
//progress output
ProgressUpdate update(areas.size());
//time measurement
const auto timeStarted = std::chrono::steady_clock::now();
struct SlimePosition
{
uint32_t x;
uint32_t z;
uint64_t score;
};
std::vector<std::future<SlimePosition>> finderFutures; //threads return slime positions
class SharedVolume
{
public:
uint32_t getVolumeWorthChecking() const
{
return m_Volume.load();
}
//only sets the volume if it's actually bigger than the current volume
//locks the thread
void setPotentiallyBiggerVolume(const uint32_t newVolume)
{
std::lock_guard lock(m_VolumeMutex);
if(newVolume > m_Volume.load())
m_Volume.store(newVolume);
}
private:
std::atomic_uint32_t m_Volume{0};
std::mutex m_VolumeMutex{};
};
SharedVolume sharedVolume;
for(uint32_t i = 0; i < threadNum; i++)
finderFutures.emplace_back(std::async([&]()->SlimePosition
{
SlimePosition toReturn{0, 0, 0};
std::unique_lock lock(areaMutex);
while(!areas.empty())
{
//getting area task
const auto area = areas.back();
areas.pop_back();
lock.unlock();
//searching
findBestSlimePlace(farmHeight, minecraftVersion, seed, radius, matrix, area, sharedVolume, toReturn);
//reporting
update.reportTaskDone();
lock.lock(); //the next access is areas.empty(), that's why locking
}
return toReturn;
}));
auto updateFuture = std::async(&ProgressUpdate::printWorker, &update);
//processing threads output
std::vector<SlimePosition> positions;
for(auto& fut : finderFutures)
positions.emplace_back(fut.get());
//wait for progress update finishing printing
updateFuture.wait();
//time measurement
const auto timeEnded = std::chrono::steady_clock::now();
const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(timeEnded-timeStarted);
std::cout << "It took " << seconds.count() << " seconds" << std::endl;
const auto bestPosition = std::max_element(positions.cbegin(), positions.cend(), [](const auto& a, const auto& b)->bool
{
return a.score < b.score;
});
if(bestPosition->score == 0)
{
std::cout << "Sorry, no slime chunks for you today :/" << std::endl;
return 0;
}
SlimeChunkPrinter::print(matrix, bestPosition->x, bestPosition->z, farmHeight);
struct thousandsSeparated : std::numpunct<char>
{
char_type do_thousands_sep() const override{ return ' '; }
string_type do_grouping() const override { return "\03"; }
};
std::stringstream scoreStringStream;
scoreStringStream.imbue(std::locale(std::cout.getloc(), new thousandsSeparated));
scoreStringStream << bestPosition->score;
std::cout << "AFK position at X:"
<< (static_cast<int32_t>(bestPosition->x)-static_cast<int32_t>(radius))*16+128 << "; Z:"
<< (static_cast<int32_t>(bestPosition->z)-static_cast<int32_t>(radius))*16+128 << '\n'
<< std::setprecision(4) << bestPosition->score/256.0f/farmHeight/g_OneFullBlockPackSpawnScore << " \"full\" chunks, score="
<< scoreStringStream.str() << " (this is a number)\n"
<< "Higher score means better" << std::endl;
//do not close right after we found it
char ch;
std::cin >> ch;
}