From e66a408e38849107a3d35a317ff6f2b23dcaeead Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Tue, 30 Oct 2018 20:00:32 -0700 Subject: RM static cast, lower warning level, add homebrew RNG --- Source/Main.cpp | 16 ++++++++++------ Source/Memory.cpp | 4 ++-- Source/Random.h | 17 +++++++++++++++++ Source/Randomizer.cpp | 2 +- Source/RandomizerCore.cpp | 3 ++- Source/Source.vcxproj | 3 ++- Source/Source.vcxproj.filters | 3 +++ WitnessRandomizer.sln | 1 - 8 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 Source/Random.h diff --git a/Source/Main.cpp b/Source/Main.cpp index e6b149f..28bc472 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp @@ -1,10 +1,13 @@ +#include "windows.h" #include "resource.h" #include + #include #include + #include "Randomizer.h" -#include "windows.h" #include "Version.h" +#include "Random.h" #define IDC_RANDOMIZE 0x401 #define IDC_TOGGLESPEED 0x402 @@ -37,9 +40,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) GetWindowText(hwndSeed, &text[0], 100); int seed = 0; if (wasSeedRandomlyGenerated || wcslen(text.c_str()) == 0) { - srand(static_cast(time(nullptr))); // Seed from the time in milliseconds - rand(); // Increment RNG so that RNG isn't still using the time - seed = rand() % 100000; + Random::SetSeed(time(nullptr)); // Seed from the time in milliseconds + seed = Random::RandInt(0, 100000); std::wstring seedString = std::to_wstring(seed); SetWindowText(hwndSeed, seedString.c_str()); wasSeedRandomlyGenerated = true; @@ -47,7 +49,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) seed = _wtoi(text.c_str()); wasSeedRandomlyGenerated = false; } - srand(seed); + Random::SetSeed(seed); Randomizer randomizer; randomizer.Randomize(); if (IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)) { @@ -79,8 +81,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd }; RegisterClassW(&wndClass); + RECT rect; + GetClientRect(GetDesktopWindow(), &rect); HWND hwnd = CreateWindow(WINDOW_CLASS, PRODUCT_NAME, WS_OVERLAPPEDWINDOW, - 400, 200, 500, 500, nullptr, nullptr, hInstance, nullptr); + rect.right - 550, 200, 500, 500, nullptr, nullptr, hInstance, nullptr); CreateWindow(L"STATIC", L"Version: " VERSION_STR, WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 0afeded..168b4e2 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp @@ -25,7 +25,7 @@ Memory::Memory(const std::string& processName) { // Next, get the process base address DWORD numModules; std::vector moduleList(1024); - EnumProcessModulesEx(_handle, &moduleList[0], static_cast(moduleList.size()), &numModules, 3); + EnumProcessModulesEx(_handle, &moduleList[0], moduleList.size(), &numModules, 3); std::string name(64, 0); for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { @@ -49,7 +49,7 @@ Memory::~Memory() { void Memory::ThrowError() { std::string message(256, '\0'); - FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], static_cast(message.length()), nullptr); + FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], message.length(), nullptr); std::cout << message.c_str() << std::endl; exit(EXIT_FAILURE); } diff --git a/Source/Random.h b/Source/Random.h new file mode 100644 index 0000000..e4700f3 --- /dev/null +++ b/Source/Random.h @@ -0,0 +1,17 @@ +#pragma once + +static int s_seed; + +class Random +{ +public: + static void SetSeed(int seed) { + s_seed = seed; + } + + static int RandInt(int min, int max) { + s_seed = (214013 * s_seed + 2531011) % 2147483648; + if (min == max) return min; + return (s_seed % (max - (min - 1))) + min; + } +}; diff --git a/Source/Randomizer.cpp b/Source/Randomizer.cpp index c3c084c..83f5571 100644 --- a/Source/Randomizer.cpp +++ b/Source/Randomizer.cpp @@ -23,7 +23,7 @@ template int find(const std::vector &data, T search, size_t startIndex = 0) { for (size_t i=startIndex ; i(i); + if (data[i] == search) return i; } std::cout << "Couldn't find " << search << " in data!" << std::endl; exit(-1); diff --git a/Source/RandomizerCore.cpp b/Source/RandomizerCore.cpp index 77977c6..684e19d 100644 --- a/Source/RandomizerCore.cpp +++ b/Source/RandomizerCore.cpp @@ -1,5 +1,6 @@ #include "RandomizerCore.h" #include "Memory.h" +#include "Random.h" #include void RandomizerCore::Randomize(std::vector& panels, int flags) { @@ -12,7 +13,7 @@ void RandomizerCore::RandomizeRange(std::vector &panels, int flags, size_t if (startIndex >= endIndex) return; if (endIndex >= panels.size()) endIndex = panels.size(); for (size_t i = endIndex-1; i > startIndex; i--) { - const size_t target = rand() % (i - startIndex) + startIndex; + const size_t target = Random::RandInt(startIndex, i); if (i != target) { // std::cout << "Swapping panels " << std::hex << panels[i] << " and " << std::hex << panels[target] << std::endl; SwapPanels(panels[i], panels[target], flags); diff --git a/Source/Source.vcxproj b/Source/Source.vcxproj index e46475b..afd4fd3 100644 --- a/Source/Source.vcxproj +++ b/Source/Source.vcxproj @@ -142,7 +142,7 @@ NotUsing - Level3 + Level1 MaxSpeed true true @@ -164,6 +164,7 @@ + diff --git a/Source/Source.vcxproj.filters b/Source/Source.vcxproj.filters index 665fc05..dbb50f9 100644 --- a/Source/Source.vcxproj.filters +++ b/Source/Source.vcxproj.filters @@ -33,6 +33,9 @@ Header Files + + Header Files + diff --git a/WitnessRandomizer.sln b/WitnessRandomizer.sln index 1e614e8..bc19048 100644 --- a/WitnessRandomizer.sln +++ b/WitnessRandomizer.sln @@ -28,7 +28,6 @@ Global {90113AEC-8765-4A8D-B7A1-6C9BE730E5D5}.Debug|x64.ActiveCfg = Debug {90113AEC-8765-4A8D-B7A1-6C9BE730E5D5}.Debug|x86.ActiveCfg = Release {90113AEC-8765-4A8D-B7A1-6C9BE730E5D5}.Release|x64.ActiveCfg = Release - {90113AEC-8765-4A8D-B7A1-6C9BE730E5D5}.Release|x64.Build.0 = Release {90113AEC-8765-4A8D-B7A1-6C9BE730E5D5}.Release|x86.ActiveCfg = Release {98BC35B9-EE1A-4D77-85F2-ADAA72DB16F7}.Debug|x64.ActiveCfg = Debug|x64 {98BC35B9-EE1A-4D77-85F2-ADAA72DB16F7}.Debug|x64.Build.0 = Debug|x64 -- cgit 1.4.1