Skip to content

Commit

Permalink
RM static cast, lower warning level, add homebrew RNG
Browse files Browse the repository at this point in the history
  • Loading branch information
jbzdarkid committed Oct 31, 2018
1 parent 2ec4950 commit e66a408
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
16 changes: 10 additions & 6 deletions Source/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "windows.h"
#include "resource.h"
#include <Richedit.h>

#include <ctime>
#include <string>

#include "Randomizer.h"
#include "windows.h"
#include "Version.h"
#include "Random.h"

#define IDC_RANDOMIZE 0x401
#define IDC_TOGGLESPEED 0x402
Expand Down Expand Up @@ -37,17 +40,16 @@ 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<unsigned int>(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;
} else {
seed = _wtoi(text.c_str());
wasSeedRandomlyGenerated = false;
}
srand(seed);
Random::SetSeed(seed);
Randomizer randomizer;
randomizer.Randomize();
if (IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)) {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Source/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Memory::Memory(const std::string& processName) {
// Next, get the process base address
DWORD numModules;
std::vector<HMODULE> moduleList(1024);
EnumProcessModulesEx(_handle, &moduleList[0], static_cast<DWORD>(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++) {
Expand All @@ -49,7 +49,7 @@ Memory::~Memory() {

void Memory::ThrowError() {
std::string message(256, '\0');
FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], static_cast<DWORD>(message.length()), nullptr);
FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], message.length(), nullptr);
std::cout << message.c_str() << std::endl;
exit(EXIT_FAILURE);
}
Expand Down
17 changes: 17 additions & 0 deletions Source/Random.h
Original file line number Diff line number Diff line change
@@ -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;
}
};
2 changes: 1 addition & 1 deletion Source/Randomizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
template <class T>
int find(const std::vector<T> &data, T search, size_t startIndex = 0) {
for (size_t i=startIndex ; i<data.size(); i++) {
if (data[i] == search) return static_cast<int>(i);
if (data[i] == search) return i;
}
std::cout << "Couldn't find " << search << " in data!" << std::endl;
exit(-1);
Expand Down
3 changes: 2 additions & 1 deletion Source/RandomizerCore.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "RandomizerCore.h"
#include "Memory.h"
#include "Random.h"
#include <sstream>

void RandomizerCore::Randomize(std::vector<int>& panels, int flags) {
Expand All @@ -12,7 +13,7 @@ void RandomizerCore::RandomizeRange(std::vector<int> &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);
Expand Down
3 changes: 2 additions & 1 deletion Source/Source.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level1</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
Expand All @@ -164,6 +164,7 @@
<ItemGroup>
<ClInclude Include="Memory.h" />
<ClInclude Include="Panels.h" />
<ClInclude Include="Random.h" />
<ClInclude Include="Randomizer.h" />
<ClInclude Include="RandomizerCore.h" />
<ClInclude Include="Resource.h" />
Expand Down
3 changes: 3 additions & 0 deletions Source/Source.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<ClInclude Include="Version.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Random.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down
1 change: 0 additions & 1 deletion WitnessRandomizer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e66a408

Please sign in to comment.