Skip to content

Commit

Permalink
Add progress reporting & setting seed via cli instead of compiling it…
Browse files Browse the repository at this point in the history
… in. Pretty much done for basic functionality now. See PR for possible future subprojects, but this is probably good enough and will suffice for basic use.
  • Loading branch information
DesktopFolder committed Dec 26, 2021
1 parent 44f5931 commit 140d50c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ add_custom_target(
# add the executable
add_executable(WitchHutFinder finder.cpp)

target_compile_definitions(WitchHutFinder PRIVATE -DSEED_USED=${SEED_USED})
if (OPTIMIZE_SEED)
target_compile_definitions(WitchHutFinder PRIVATE -DOPTIMIZE_SEED=${OPTIMIZE_SEED})
endif()

target_include_directories(WitchHutFinder PRIVATE "${CMAKE_SOURCE_DIR}/cubiomes")
target_link_libraries(WitchHutFinder PUBLIC -lm -fwrapv)
Expand Down
1 change: 1 addition & 0 deletions cubiomes.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
/**
* C++ wrapper for Cubiomes
* Header-only; minimal wrapper
Expand Down
51 changes: 49 additions & 2 deletions finder.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#include <fstream>
#include <iostream>
#include <optional>
#include <string>

#include "cubiomes.hpp"
#include "parser.hpp"
#include "seed.hpp"

constexpr int witchHutCount = 2;

struct OutputManager
{
std::optional<std::string> filename;
static constexpr size_t report_steps = 10;

template <typename S>
void write(S str)
Expand All @@ -25,8 +28,19 @@ struct OutputManager
write(std::forward<Ss>(strs)...);
}

template <typename... Ss>
void step(Ss... strs)
{
write(std::forward<Ss>(strs)...);
if (++steps % report_steps == 0)
{
std::cout << "Found " << steps << " witch huts...\n";
}
}

private:
std::ofstream file;
size_t steps{};

template <typename T>
void _write(const T& s)
Expand All @@ -51,9 +65,12 @@ int main(int argc, char* argv[])
ArgumentParser ap(argc, argv);
// Scoped stuff
auto output = OutputManager{};
std::string seed_str;
while (not ap.done())
{
// Parse arguments real quick.
// Wow, this is hilariously ugly. Oh well.
// Using libraries is for the weak.
const auto arg = ap.consume();
if (arg.name_clean() == "help")
{
Expand All @@ -72,7 +89,38 @@ int main(int argc, char* argv[])
output.filename = arg.values[0];
}
}
else if (arg.name_clean() == "s" || arg.name_clean() == "seed")
{
if constexpr (optimize_seed())
{
std::cout << "--seed/-s ignored (conflicts with compilation parameter)\n";
}
else
{
if (arg.values.empty())
{
std::cout << "--seed/-s requires one argument (the seed)\n";
}
else
{
seed_str = arg.values[0];
}
}
}
}

// Annnnd we end up needing macros anyways.
// Oh well.
#ifdef OPTIMIZE_SEED
constexpr seed_t seed = SEED_USED;
#else
if (seed_str == "")
{
std::cout << "Error: Seed not provided. Use [--seed SEED] to set.\n";
return 1;
}
const seed_t seed = std::stol(seed_str);
#endif

long searchRange = 500;
int offset = witchHutCount - 4;
Expand All @@ -81,7 +129,6 @@ int main(int argc, char* argv[])

SeedContext context{};

seed_t seed = SEED_USED;
for (int regPosX = -searchRange; regPosX < searchRange; ++regPosX)
{
for (int regPosZ = -searchRange; regPosZ < searchRange; ++regPosZ)
Expand Down Expand Up @@ -182,7 +229,7 @@ int main(int argc, char* argv[])
}
if (valid && maxi >= offset + 4)
{
output.write("CENTER for ", maxi, " huts: ", x, ", ", z);
output.step("CENTER for ", maxi, " huts: ", x, ", ", z);
// fprintf(fp, "CENTER for %d huts: %d,%d\n", maxi, x, z);
results[maxi - 2]++;
}
Expand Down
1 change: 1 addition & 0 deletions parser.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
/**
* A simple argument parsing toolkit.
*
Expand Down
11 changes: 11 additions & 0 deletions seed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <optional>

constexpr bool optimize_seed()
{
#ifdef OPTIMIZE_SEED
return true;
#endif
return false;
}

0 comments on commit 140d50c

Please sign in to comment.