From faf617739f53c67f663887de34342f92056ba45d Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Tue, 6 Nov 2018 09:59:34 -0800 Subject: Assorted. --- App/Main.cpp | 53 ++++++++++++++++------- App/Version.h | 2 +- Source/Memory.h | 2 + Source/Randomizer.cpp | 106 +++++++++++++++++++++++----------------------- Source/Randomizer.h | 8 +++- Source/RandomizerCore.cpp | 39 +++++++---------- Source/RandomizerCore.h | 8 ++-- 7 files changed, 122 insertions(+), 96 deletions(-) diff --git a/App/Main.cpp b/App/Main.cpp index 48cb93d..ab3e88a 100644 --- a/App/Main.cpp +++ b/App/Main.cpp @@ -16,19 +16,26 @@ #define IDC_RANDOM 0x406 #define IDC_WRITE 0x407 #define IDC_DUMP 0x408 +#define IDT_RANDOMIZED 0x409 HWND hwndSeed, hwndRandomize; int panel = 0x18AF; // int panel = 0x33D4; std::shared_ptr _panel; +std::shared_ptr randomizer = std::make_shared(); LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - static bool wasSeedRandomlyGenerated; + static bool seedIsRNG = false; if (message == WM_DESTROY) { PostQuitMessage(0); - } else if (message == WM_COMMAND) { + } else if (message == WM_COMMAND || message == WM_TIMER) { + switch (HIWORD(wParam)) { + // Seed contents changed + case EN_CHANGE: + seedIsRNG = false; + } switch (LOWORD(wParam)) { // Speed checkbox case IDC_TOGGLESPEED: @@ -42,29 +49,41 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) // Randomize button case IDC_RANDOMIZE: { - std::wstring text(100, '\0'); + std::wstring text; + text.reserve(100); GetWindowText(hwndSeed, &text[0], 100); - int seed = 0; - if (wasSeedRandomlyGenerated || wcslen(text.c_str()) == 0) { + int seed = _wtoi(text.c_str()); + + if (seedIsRNG || text.empty()) { seed = Random::RandInt(0, 100000); - wasSeedRandomlyGenerated = true; - } else { - seed = _wtoi(text.c_str()); - wasSeedRandomlyGenerated = false; + seedIsRNG = true; } - Randomizer randomizer; - short metadata = randomizer.Randomize(seed); - if (metadata & 0x1) break; // Was already randomized - + randomizer->ClearOffsets(); + /* TODO: + if (!randomizer->GameIsRunning()) { + randomizer->StartGame(); // Try: CreateProcess(L"/path/to/TW.exe", ...); + } + */ + if (randomizer->GameIsRandomized()) break; + Random::SetSeed(seed); std::wstring seedString = std::to_wstring(seed); + SetWindowText(hwndRandomize, L"Randomizing..."); SetWindowText(hwndSeed, seedString.c_str()); - if (IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)) { - randomizer.AdjustSpeed(); + RedrawWindow(hwnd, NULL, NULL, RDW_UPDATENOW); + + randomizer->Randomize(); + if (IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)) { + randomizer->AdjustSpeed(); } SetWindowText(hwndRandomize, L"Randomized!"); + SetTimer(hwnd, IDT_RANDOMIZED, 10000, NULL); break; } + + case IDT_RANDOMIZED: + SetWindowText(hwndRandomize, L"Randomize"); + break; case IDC_READ: _panel = std::make_shared(panel); break; @@ -115,9 +134,11 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd hwndSeed = CreateWindow(MSFTEDIT_CLASS, L"", WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER, 100, 10, 50, 26, hwnd, NULL, hInstance, NULL); + SendMessage(hwndSeed, EM_SETEVENTMASK, NULL, ENM_CHANGE); // Notify on text change + hwndRandomize = CreateWindow(L"BUTTON", L"Randomize", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, - 160, 10, 100, 26, hwnd, (HMENU)IDC_RANDOMIZE, hInstance, NULL); + 160, 10, 110, 26, hwnd, (HMENU)IDC_RANDOMIZE, hInstance, NULL); #if GLOBALS == 0x5B28C0 CreateWindow(L"BUTTON", L"READ", diff --git a/App/Version.h b/App/Version.h index 05696d6..8029020 100644 --- a/App/Version.h +++ b/App/Version.h @@ -5,7 +5,7 @@ #define MAJOR 4 #define MINOR 0 -#define PATCH 0 +#define PATCH 4 #define VERSION_STR TO_STRING(MAJOR) L"." TO_STRING(MINOR) L"." TO_STRING(PATCH) #define VERSION MAJOR, MINOR, PATCH diff --git a/Source/Memory.h b/Source/Memory.h index d2ab50e..4c79d7c 100644 --- a/Source/Memory.h +++ b/Source/Memory.h @@ -64,6 +64,8 @@ public: ThrowError(); } + void ClearOffsets() {_computedAddresses = std::map();} + private: void ThrowError(); diff --git a/Source/Randomizer.cpp b/Source/Randomizer.cpp index 8833078..3cc2712 100644 --- a/Source/Randomizer.cpp +++ b/Source/Randomizer.cpp @@ -1,16 +1,13 @@ /* * BUGS: - * Shipwreck vault is solved reversed? - * Swamp <-> symmetry has non-invisible background - * Tutorial sounds don't always play + * Shipwreck vault is solved reversed? -> Not reversed, just "half", you can normally solve orange. Seems to need pattern name. + * Tutorial sounds don't always play -> Unsure. Not controlled by pattern name. * FEATURES: - * Clear "Randomized" button after short delay * Randomize audio logs -- Hard, seem to be unloaded some times? * Swap sounds in jungle (along with panels) -- maybe impossible - * Make orange 7 (all of oranges?) hard. Like big = hard. + * Make orange 7 (all of oranges?) hard. Like big = hard. (See: HARD_MODE) * Start the game if it isn't running? * Stop swapping colors in desert - * Allow users to enter seed after randomly generating seed (aka detect user input in the text box) */ #include "Memory.h" #include "Randomizer.h" @@ -29,15 +26,21 @@ int find(const std::vector &data, T search, size_t startIndex = 0) { exit(-1); } -short Randomizer::Randomize(int seed) -{ - short metadata = _core.ReadMetadata(); - if (metadata & 0x1) { - // Already randomized -- exit. - return metadata; +bool Randomizer::GameIsRandomized() { + int currentFrame = _core.GetCurrentFrame(); + if (currentFrame >= _lastRandomizedFrame) { + // Time went forwards, presumably we're still on the same save + _lastRandomizedFrame = currentFrame; + return true; } - _core.WriteMetadata(metadata | 0x1); - Random::SetSeed(seed); + // Otherwise, time has gone backwards, so assume new game + return false; +} + +void Randomizer::Randomize() +{ + if (GameIsRandomized()) return; // Nice sanity check, but should be unnecessary (since Main checks anyways) + _lastRandomizedFrame = _core.GetCurrentFrame(); // Content swaps -- must happen before squarePanels _core.Randomize(upDownPanels, SWAP_LINES); @@ -61,24 +64,23 @@ short Randomizer::Randomize(int seed) RandomizeMountain(); // RandomizeChallenge(); // RandomizeAudioLogs(); - return metadata; } void Randomizer::AdjustSpeed() { // Desert Surface Final Control - _core._memory.WritePanelData(0x09F95, OPEN_RATE, {0.04f}); // 4x + _core._memory->WritePanelData(0x09F95, OPEN_RATE, {0.04f}); // 4x // Swamp Sliding Bridge - _core._memory.WritePanelData(0x0061A, OPEN_RATE, {0.1f}); // 4x + _core._memory->WritePanelData(0x0061A, OPEN_RATE, {0.1f}); // 4x // Mountain 2 Elevator - _core._memory.WritePanelData(0x09EEC, OPEN_RATE, {0.075f}); // 3x + _core._memory->WritePanelData(0x09EEC, OPEN_RATE, {0.075f}); // 3x } void Randomizer::RandomizeTutorial() { // Disable tutorial cursor speed modifications (not working?) - _core._memory.WritePanelData(0x00295, CURSOR_SPEED_SCALE, {1.0}); - _core._memory.WritePanelData(0x0C373, CURSOR_SPEED_SCALE, {1.0}); - _core._memory.WritePanelData(0x00293, CURSOR_SPEED_SCALE, {1.0}); - _core._memory.WritePanelData(0x002C2, CURSOR_SPEED_SCALE, {1.0}); + _core._memory->WritePanelData(0x00295, CURSOR_SPEED_SCALE, {1.0}); + _core._memory->WritePanelData(0x0C373, CURSOR_SPEED_SCALE, {1.0}); + _core._memory->WritePanelData(0x00293, CURSOR_SPEED_SCALE, {1.0}); + _core._memory->WritePanelData(0x002C2, CURSOR_SPEED_SCALE, {1.0}); } void Randomizer::RandomizeSymmetry() { @@ -88,11 +90,11 @@ void Randomizer::RandomizeDesert() { _core.Randomize(desertPanels, SWAP_LINES); // Turn off desert surface 8 - _core._memory.WritePanelData(0x09F94, POWER, {0.0, 0.0}); + _core._memory->WritePanelData(0x09F94, POWER, {0.0, 0.0}); // Turn off desert flood final - _core._memory.WritePanelData(0x18076, POWER, {0.0, 0.0}); + _core._memory->WritePanelData(0x18076, POWER, {0.0, 0.0}); // Change desert floating target to desert flood final - _core._memory.WritePanelData(0x17ECA, TARGET, {0x18077}); + _core._memory->WritePanelData(0x17ECA, TARGET, {0x18077}); } void Randomizer::RandomizeQuarry() { @@ -100,14 +102,14 @@ void Randomizer::RandomizeQuarry() { void Randomizer::RandomizeTreehouse() { // Ensure that whatever pivot panels we have are flagged as "pivotable" - int panelFlags = _core._memory.ReadPanelData(0x17DD1, STYLE_FLAGS, 1)[0]; - _core._memory.WritePanelData(0x17DD1, STYLE_FLAGS, {panelFlags | 0x8000}); - panelFlags = _core._memory.ReadPanelData(0x17CE3, STYLE_FLAGS, 1)[0]; - _core._memory.WritePanelData(0x17CE3, STYLE_FLAGS, {panelFlags | 0x8000}); - panelFlags = _core._memory.ReadPanelData(0x17DB7, STYLE_FLAGS, 1)[0]; - _core._memory.WritePanelData(0x17DB7, STYLE_FLAGS, {panelFlags | 0x8000}); - panelFlags = _core._memory.ReadPanelData(0x17E52, STYLE_FLAGS, 1)[0]; - _core._memory.WritePanelData(0x17E52, STYLE_FLAGS, {panelFlags | 0x8000}); + int panelFlags = _core._memory->ReadPanelData(0x17DD1, STYLE_FLAGS, 1)[0]; + _core._memory->WritePanelData(0x17DD1, STYLE_FLAGS, {panelFlags | 0x8000}); + panelFlags = _core._memory->ReadPanelData(0x17CE3, STYLE_FLAGS, 1)[0]; + _core._memory->WritePanelData(0x17CE3, STYLE_FLAGS, {panelFlags | 0x8000}); + panelFlags = _core._memory->ReadPanelData(0x17DB7, STYLE_FLAGS, 1)[0]; + _core._memory->WritePanelData(0x17DB7, STYLE_FLAGS, {panelFlags | 0x8000}); + panelFlags = _core._memory->ReadPanelData(0x17E52, STYLE_FLAGS, 1)[0]; + _core._memory->WritePanelData(0x17E52, STYLE_FLAGS, {panelFlags | 0x8000}); } void Randomizer::RandomizeKeep() { @@ -115,11 +117,11 @@ void Randomizer::RandomizeKeep() { void Randomizer::RandomizeShadows() { // Distance-gate shadows laser to prevent sniping through the bars - _core._memory.WritePanelData(0x19650, MAX_BROADCAST_DISTANCE, {2.5}); + _core._memory->WritePanelData(0x19650, MAX_BROADCAST_DISTANCE, {2.5}); // Change the shadows tutorial cable to only activate avoid - _core._memory.WritePanelData(0x319A8, CABLE_TARGET_2, {0}); + _core._memory->WritePanelData(0x319A8, CABLE_TARGET_2, {0}); // Change shadows avoid 8 to power shadows follow - _core._memory.WritePanelData(0x1972F, TARGET, {0x1C34C}); + _core._memory->WritePanelData(0x1972F, TARGET, {0x1C34C}); std::vector randomOrder(shadowsPanels.size(), 0); std::iota(randomOrder.begin(), randomOrder.end(), 0); @@ -128,9 +130,9 @@ void Randomizer::RandomizeShadows() { _core.RandomizeRange(randomOrder, SWAP_NONE, 16, 21); // Follow _core.ReassignTargets(shadowsPanels, randomOrder); // Turn off original starting panel - _core._memory.WritePanelData(shadowsPanels[0], POWER, {0.0f, 0.0f}); + _core._memory->WritePanelData(shadowsPanels[0], POWER, {0.0f, 0.0f}); // Turn on new starting panel - _core._memory.WritePanelData(shadowsPanels[randomOrder[0]], POWER, {1.0f, 1.0f}); + _core._memory->WritePanelData(shadowsPanels[randomOrder[0]], POWER, {1.0f, 1.0f}); } void Randomizer::RandomizeTown() { @@ -167,7 +169,7 @@ void Randomizer::RandomizeJungle() { void Randomizer::RandomizeSwamp() { // Distance-gate swamp snipe 1 to prevent RNG swamp snipe - _core._memory.WritePanelData(0x17C05, MAX_BROADCAST_DISTANCE, {15.0}); + _core._memory->WritePanelData(0x17C05, MAX_BROADCAST_DISTANCE, {15.0}); } void Randomizer::RandomizeMountain() { @@ -178,7 +180,7 @@ void Randomizer::RandomizeMountain() { // Randomize final pillars order std::vector targets = {pillars[0] + 1}; for (const int pillar : pillars) { - int target = _core._memory.ReadPanelData(pillar, TARGET, 1)[0]; + int target = _core._memory->ReadPanelData(pillar, TARGET, 1)[0]; targets.push_back(target); } targets[5] = pillars[5] + 1; @@ -189,27 +191,27 @@ void Randomizer::RandomizeMountain() { _core.RandomizeRange(randomOrder, SWAP_NONE, 5, 9); // Right Pillars 1-4 _core.ReassignTargets(pillars, randomOrder, targets); // Turn off original starting panels - _core._memory.WritePanelData(pillars[0], POWER, {0.0f, 0.0f}); - _core._memory.WritePanelData(pillars[5], POWER, {0.0f, 0.0f}); + _core._memory->WritePanelData(pillars[0], POWER, {0.0f, 0.0f}); + _core._memory->WritePanelData(pillars[5], POWER, {0.0f, 0.0f}); // Turn on new starting panels - _core._memory.WritePanelData(pillars[randomOrder[0]], POWER, {1.0f, 1.0f}); - _core._memory.WritePanelData(pillars[randomOrder[5]], POWER, {1.0f, 1.0f}); + _core._memory->WritePanelData(pillars[randomOrder[0]], POWER, {1.0f, 1.0f}); + _core._memory->WritePanelData(pillars[randomOrder[5]], POWER, {1.0f, 1.0f}); // Read the target of keep front laser, and write it to keep back laser. - std::vector keepFrontLaserTarget = _core._memory.ReadPanelData(0x0360E, TARGET, 1); - _core._memory.WritePanelData(0x03317, TARGET, keepFrontLaserTarget); + std::vector keepFrontLaserTarget = _core._memory->ReadPanelData(0x0360E, TARGET, 1); + _core._memory->WritePanelData(0x03317, TARGET, keepFrontLaserTarget); } void Randomizer::RandomizeChallenge() { std::vector randomOrder(challengePanels.size(), 0); std::iota(randomOrder.begin(), randomOrder.end(), 0); _core.RandomizeRange(randomOrder, SWAP_NONE, 1, 9); // Easy maze - Triple 2 - std::vector triple1Target = _core._memory.ReadPanelData(0x00C80, TARGET, 1); - _core._memory.WritePanelData(0x00CA1, TARGET, triple1Target); - _core._memory.WritePanelData(0x00CB9, TARGET, triple1Target); - std::vector triple2Target = _core._memory.ReadPanelData(0x00C22, TARGET, 1); - _core._memory.WritePanelData(0x00C59, TARGET, triple2Target); - _core._memory.WritePanelData(0x00C68, TARGET, triple2Target); + std::vector triple1Target = _core._memory->ReadPanelData(0x00C80, TARGET, 1); + _core._memory->WritePanelData(0x00CA1, TARGET, triple1Target); + _core._memory->WritePanelData(0x00CB9, TARGET, triple1Target); + std::vector triple2Target = _core._memory->ReadPanelData(0x00C22, TARGET, 1); + _core._memory->WritePanelData(0x00C59, TARGET, triple2Target); + _core._memory->WritePanelData(0x00C68, TARGET, triple2Target); _core.ReassignTargets(challengePanels, randomOrder); } diff --git a/Source/Randomizer.h b/Source/Randomizer.h index 8c332b0..ea05975 100644 --- a/Source/Randomizer.h +++ b/Source/Randomizer.h @@ -3,10 +3,16 @@ class Randomizer { public: - short Randomize(int seed); + void Randomize(); + bool GameIsRandomized(); + void AdjustSpeed(); + void ClearOffsets() {_core.ClearOffsets();} + private: + + int _lastRandomizedFrame = 1 << 30; void RandomizeTutorial(); void RandomizeSymmetry(); void RandomizeDesert(); diff --git a/Source/RandomizerCore.cpp b/Source/RandomizerCore.cpp index d4aadef..f00dacd 100644 --- a/Source/RandomizerCore.cpp +++ b/Source/RandomizerCore.cpp @@ -3,17 +3,6 @@ #include "Random.h" #include -static int lastKnownFrame = 1 << 30; - -RandomizerCore::RandomizerCore() { - int currentFrame = _memory.ReadData({SCRIPT_FRAMES}, 1)[0]; - if (currentFrame < lastKnownFrame) { - // Time went backwards, indicates new game - WriteMetadata(0); - } - lastKnownFrame = currentFrame; -} - void RandomizerCore::Randomize(std::vector& panels, int flags) { return RandomizeRange(panels, flags, 0, panels.size()); } @@ -47,7 +36,7 @@ void RandomizerCore::SwapPanels(int panel1, int panel2, int flags) { offsets[REFLECTION_PATH_COLOR] = 16; offsets[DOT_COLOR] = 16; offsets[ACTIVE_COLOR] = 16; - offsets[BACKGROUND_REGION_COLOR] = 16; + offsets[BACKGROUND_REGION_COLOR] = 12; // Not copying alpha to preserve transparency. offsets[SUCCESS_COLOR_A] = 16; offsets[SUCCESS_COLOR_B] = 16; offsets[STROBE_COLOR_A] = 16; @@ -101,10 +90,10 @@ void RandomizerCore::SwapPanels(int panel1, int panel2, int flags) { } for (auto const& [offset, size] : offsets) { - std::vector panel1data = _memory.ReadPanelData(panel1, offset, size); - std::vector panel2data = _memory.ReadPanelData(panel2, offset, size); - _memory.WritePanelData(panel2, offset, panel1data); - _memory.WritePanelData(panel1, offset, panel2data); + std::vector panel1data = _memory->ReadPanelData(panel1, offset, size); + std::vector panel2data = _memory->ReadPanelData(panel2, offset, size); + _memory->WritePanelData(panel2, offset, panel1data); + _memory->WritePanelData(panel1, offset, panel2data); } } @@ -114,7 +103,7 @@ void RandomizerCore::ReassignTargets(const std::vector& panels, const std:: // The first panel may not have a wire to power it, so we use the panel ID itself. targets = {panels[0] + 1}; for (const int panel : panels) { - int target = _memory.ReadPanelData(panel, TARGET, 1)[0]; + int target = _memory->ReadPanelData(panel, TARGET, 1)[0]; targets.push_back(target); } } @@ -122,25 +111,29 @@ void RandomizerCore::ReassignTargets(const std::vector& panels, const std:: for (size_t i=0; i(panels[order[i]], TARGET, {panelTarget}); + _memory->WritePanelData(panels[order[i]], TARGET, {panelTarget}); } } void RandomizerCore::ReassignNames(const std::vector& panels, const std::vector& order) { std::vector names; for (const int panel : panels) { - names.push_back(_memory.ReadPanelData(panel, AUDIO_LOG_NAME, 1)[0]); + names.push_back(_memory->ReadPanelData(panel, AUDIO_LOG_NAME, 1)[0]); } for (int i=0; i(panels[i], AUDIO_LOG_NAME, {names[order[i]]}); + _memory->WritePanelData(panels[i], AUDIO_LOG_NAME, {names[order[i]]}); } } short RandomizerCore::ReadMetadata() { - return _memory.ReadData({GLOBALS + METADATA}, 1)[0]; + return _memory->ReadData({GLOBALS + METADATA}, 1)[0]; } void RandomizerCore::WriteMetadata(short metadata) { - return _memory.WriteData({GLOBALS + METADATA}, {metadata}); -} \ No newline at end of file + return _memory->WriteData({GLOBALS + METADATA}, {metadata}); +} + +int RandomizerCore::GetCurrentFrame() { + return _memory->ReadData({SCRIPT_FRAMES}, 1)[0]; +} diff --git a/Source/RandomizerCore.h b/Source/RandomizerCore.h index 4cd4b42..7555de2 100644 --- a/Source/RandomizerCore.h +++ b/Source/RandomizerCore.h @@ -1,5 +1,6 @@ #pragma once #include "Memory.h" +#include __declspec(selectany) int SWAP_NONE = 0x0; __declspec(selectany) int SWAP_TARGETS = 0x1; @@ -9,8 +10,6 @@ __declspec(selectany) int SWAP_AUDIO_NAMES = 0x4; class RandomizerCore { public: - RandomizerCore(); - void Randomize(std::vector& panels, int flags); void RandomizeRange(std::vector &panels, int flags, size_t startIndex, size_t endIndex); void SwapPanels(int panel1, int panel2, int flags); @@ -19,9 +18,12 @@ public: short ReadMetadata(); void WriteMetadata(short metadata); + int GetCurrentFrame(); + + void ClearOffsets() {_memory->ClearOffsets();} // private: - Memory _memory = Memory("witness64_d3d11.exe"); + std::shared_ptr _memory = std::make_shared("witness64_d3d11.exe"); }; #if GLOBALS == 0x5B28C0 -- cgit 1.4.1