summary refs log tree commit diff stats
path: root/App2/Main.cpp
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2021-01-17 16:15:37 -0800
committerjbzdarkid <jbzdarkid@gmail.com>2021-01-17 16:15:37 -0800
commitfa5c27c9b5991f2daf526eb5fdd3e41200afa63e (patch)
treef33b1822bcd756b53ddcfcccdcefe07c4654c32e /App2/Main.cpp
parent6b11e2530eaaecb1e4ab75821b12521fbb44062c (diff)
downloadwitness-tutorializer-fa5c27c9b5991f2daf526eb5fdd3e41200afa63e.tar.gz
witness-tutorializer-fa5c27c9b5991f2daf526eb5fdd3e41200afa63e.tar.bz2
witness-tutorializer-fa5c27c9b5991f2daf526eb5fdd3e41200afa63e.zip
Try adding app2
Diffstat (limited to 'App2/Main.cpp')
-rw-r--r--App2/Main.cpp313
1 files changed, 313 insertions, 0 deletions
diff --git a/App2/Main.cpp b/App2/Main.cpp new file mode 100644 index 0000000..30deff2 --- /dev/null +++ b/App2/Main.cpp
@@ -0,0 +1,313 @@
1#include "pch.h"
2#include "Richedit.h"
3#include "Version.h"
4
5#include "Memory.h"
6#include "Random.h"
7#include "Randomizer.h"
8#include "Randomizer2.h"
9#include "Panels_.h"
10
11#define HEARTBEAT 0x401
12#define RANDOMIZE_READY 0x402
13#define RANDOMIZING 0403
14#define RANDOMIZE_DONE 0x404
15#define RANDOMIZE_CHALLENGE_DONE 0x405
16#define CHALLENGE_ONLY 0x406
17#define DISABLE_SNIPES 0x407
18#define SPEED_UP_AUTOSCROLLERS 0x408
19
20/* ------- Temp ------- */
21#include "Solver.h"
22#include "PuzzleSerializer.h"
23#include <sstream>
24#include <iomanip>
25
26#define TMP1 0x501
27#define TMP2 0x502
28#define TMP3 0x503
29#define TMP4 0x504
30
31HWND g_panelId;
32Puzzle g_puzzle;
33
34HWND g_rngDebug;
35#define TMP5 0x505
36/* ------- Temp ------- */
37
38// Globals
39HWND g_hwnd;
40HWND g_seed;
41HWND g_randomizerStatus;
42HINSTANCE g_hInstance;
43auto g_witnessProc = std::make_shared<Memory>(L"witness64_d3d11.exe");
44std::shared_ptr<Randomizer> g_randomizer;
45std::shared_ptr<Randomizer2> g_randomizer2;
46void SetRandomSeed();
47
48LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
49 if (message == WM_DESTROY) {
50 PostQuitMessage(0);
51 } else if (message == WM_NOTIFY) {
52 MSGFILTER* m = (MSGFILTER *)lParam;
53 if (m->msg == WM_KEYDOWN && m->wParam == VK_RETURN) {
54 if (IsWindowEnabled(g_randomizerStatus) == TRUE) {
55 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZING, NULL);
56 return 1; // Non-zero to indicate that message was handled
57 }
58 }
59 } else if (message == WM_COMMAND || message == WM_TIMER || message == WM_NOTIFY) {
60 switch (LOWORD(wParam)) {
61 case HEARTBEAT:
62 switch ((ProcStatus)lParam) {
63 case ProcStatus::NotRunning:
64 // Shut down randomizer, wait for startup
65 if (g_randomizer) {
66 g_randomizer = nullptr;
67 g_randomizer2 = nullptr;
68 EnableWindow(g_randomizerStatus, FALSE);
69 }
70 break;
71 case ProcStatus::Running:
72 if (!g_randomizer) {
73 g_randomizer = std::make_shared<Randomizer>(g_witnessProc);
74 g_randomizer2 = std::make_shared<Randomizer2>(g_witnessProc);
75 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL);
76 }
77 break;
78 case ProcStatus::NewGame: // This status will fire only once per new game
79 SetWindowText(g_seed, L"");
80 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL);
81 break;
82 }
83 break;
84 case RANDOMIZE_READY:
85 EnableWindow(g_randomizerStatus, TRUE);
86 if (IsDlgButtonChecked(hwnd, CHALLENGE_ONLY)) {
87 SetWindowText(g_randomizerStatus, L"Randomize Challenge");
88 } else {
89 SetWindowText(g_randomizerStatus, L"Randomize");
90 }
91 break;
92 case RANDOMIZING:
93 if (!g_randomizer) break; // E.g. an enter press at the wrong time
94 EnableWindow(g_randomizerStatus, FALSE);
95
96 SetRandomSeed();
97 std::thread([]{
98 if (IsDlgButtonChecked(g_hwnd, DISABLE_SNIPES)) {
99 MEMORY_CATCH(g_randomizer->PreventSnipes());
100 }
101 if (IsDlgButtonChecked(g_hwnd, SPEED_UP_AUTOSCROLLERS)) {
102 MEMORY_CATCH(g_randomizer->AdjustSpeed());
103 }
104 if (IsDlgButtonChecked(g_hwnd, CHALLENGE_ONLY)) {
105 SetWindowText(g_randomizerStatus, L"Randomizing Challenge...");
106 MEMORY_CATCH(g_randomizer->RandomizeChallenge());
107 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_CHALLENGE_DONE, NULL);
108 } else {
109 SetWindowText(g_randomizerStatus, L"Randomizing...");
110 MEMORY_CATCH(g_randomizer->Randomize());
111 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_DONE, NULL);
112 }
113 }).detach();
114 break;
115 case RANDOMIZE_DONE:
116 EnableWindow(g_randomizerStatus, FALSE);
117 SetWindowText(g_randomizerStatus, L"Randomized!");
118 break;
119 case RANDOMIZE_CHALLENGE_DONE:
120 EnableWindow(g_randomizerStatus, FALSE);
121 SetWindowText(g_randomizerStatus, L"Randomized Challenge!");
122 std::thread([]{
123 // Allow re-randomization for challenge -- it doesn't break the rest of the game.
124 std::this_thread::sleep_for(std::chrono::seconds(10));
125 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL);
126 }).detach();
127 break;
128 case CHALLENGE_ONLY:
129 CheckDlgButton(hwnd, CHALLENGE_ONLY, !IsDlgButtonChecked(hwnd, CHALLENGE_ONLY));
130 if (IsWindowEnabled(g_randomizerStatus)) {
131 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL);
132 }
133 break;
134 case DISABLE_SNIPES:
135 CheckDlgButton(hwnd, DISABLE_SNIPES, !IsDlgButtonChecked(hwnd, DISABLE_SNIPES));
136 break;
137 case SPEED_UP_AUTOSCROLLERS:
138 CheckDlgButton(hwnd, SPEED_UP_AUTOSCROLLERS, !IsDlgButtonChecked(hwnd, SPEED_UP_AUTOSCROLLERS));
139 break;
140 case TMP1:
141 {
142 std::wstring text(128, L'\0');
143 int length = GetWindowText(g_panelId, text.data(), static_cast<int>(text.size()));
144 text.resize(length);
145 std::wstringstream s;
146 int panelId;
147 s << text;
148 s >> std::hex >> panelId;
149 g_puzzle = PuzzleSerializer(g_witnessProc).ReadPuzzle(panelId);
150 }
151 break;
152 case TMP2:
153 {
154 std::wstring text(128, L'\0');
155 int length = GetWindowText(g_panelId, text.data(), static_cast<int>(text.size()));
156 text.resize(length);
157 std::wstringstream s;
158 int panelId;
159 s << text;
160 s >> std::hex >> panelId;
161 PuzzleSerializer(g_witnessProc).WritePuzzle(g_puzzle, panelId);
162 }
163 break;
164 case TMP3:
165 {
166 for (auto [key, value] : PANELS) {
167 std::stringstream out;
168 std::string name(value);
169 out << " {'id': 0x" << std::hex << std::uppercase << std::setfill('0') << std::setw(5) << key << ", 'area':'";
170 int k;
171 for (k=0; name[k] != ' '; k++) out << name[k];
172 if (name[k+2] == ' ') {
173 out << name[k] << name[k+1];
174 k += 2;
175 }
176 out << "', 'name':'";
177 k++;
178 for (k; k < name.size(); k++) out << name[k];
179 out << "', 'data':'";
180 auto puzzle = PuzzleSerializer(g_witnessProc).ReadPuzzle(key);
181 out << puzzle.Serialize();
182 out << "'},\r\n";
183 DebugPrint(out.str());
184 }
185 }
186
187 // Solver::Solve(g_puzzle);
188 break;
189 case TMP4:
190 SetRandomSeed();
191 g_randomizer2->Randomize();
192 case TMP5:
193 {
194 std::wstring text;
195 for (int i=0; i<10; i++) {
196 Random::SetSeed(i);
197 int rng = Random::RandInt(0, 999999);
198 text += std::to_wstring(rng) + L"\n";
199 }
200 SetWindowText(g_rngDebug, text.c_str());
201 }
202 }
203 }
204 return DefWindowProc(hwnd, message, wParam, lParam);
205}
206
207void SetRandomSeed() {
208 std::wstring text(128, L'\0');
209 int length = GetWindowText(g_seed, text.data(), static_cast<int>(text.size()));
210 if (length > 0) { // Set seed
211 text.resize(length);
212 Random::SetSeed(_wtoi(text.c_str()));
213 } else { // Random seed
214 int seed = Random::RandInt(0, 999999);
215
216 text = std::to_wstring(seed);
217 SetWindowText(g_seed, text.c_str());
218 CHARRANGE range = {0, static_cast<long>(text.length())};
219 PostMessage(g_seed, EM_EXSETSEL, NULL, (LPARAM)&range);
220 SetFocus(g_seed);
221
222 Random::SetSeed(seed);
223 }
224}
225
226HWND CreateLabel(int x, int y, int width, LPCWSTR text) {
227 return CreateWindow(L"STATIC", text,
228 WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT,
229 x, y, width, 16, g_hwnd, NULL, g_hInstance, NULL);
230}
231
232HWND CreateText(int x, int y, int width, LPCWSTR defaultText = L"") {
233 return CreateWindow(MSFTEDIT_CLASS, defaultText,
234 WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER,
235 x, y, width, 26, g_hwnd, NULL, g_hInstance, NULL);
236}
237
238#pragma warning(push)
239#pragma warning(disable: 4312)
240HWND CreateButton(int x, int y, int width, LPCWSTR text, int message) {
241 return CreateWindow(L"BUTTON", text,
242 WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
243 x, y, width, 26, g_hwnd, (HMENU)message, g_hInstance, NULL);
244}
245
246HWND CreateCheckbox(int x, int y, int message) {
247 return CreateWindow(L"BUTTON", L"",
248 WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
249 x, y, 12, 12, g_hwnd, (HMENU)message, g_hInstance, NULL);
250}
251#pragma warning(pop)
252
253int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
254 LoadLibrary(L"Msftedit.dll");
255 WNDCLASSW wndClass = {
256 CS_HREDRAW | CS_VREDRAW,
257 WndProc,
258 0,
259 0,
260 hInstance,
261 NULL,
262 LoadCursor(nullptr, IDC_ARROW),
263 (HBRUSH)(COLOR_WINDOW+1),
264 WINDOW_CLASS,
265 WINDOW_CLASS,
266 };
267 RegisterClassW(&wndClass);
268
269 g_hInstance = hInstance;
270
271 RECT rect;
272 GetClientRect(GetDesktopWindow(), &rect);
273 g_hwnd = CreateWindow(WINDOW_CLASS, PRODUCT_NAME, WS_OVERLAPPEDWINDOW,
274 rect.right - 550, 200, 500, 500, nullptr, nullptr, hInstance, nullptr);
275
276 CreateLabel(390, 15, 90, L"Version: " VERSION_STR);
277 g_seed = CreateText(10, 10, 100, L"21581");
278 PostMessage(g_seed, EM_SETEVENTMASK, 0, ENM_KEYEVENTS);
279 g_randomizerStatus = CreateButton(120, 10, 180, L"Randomize", RANDOMIZING);
280 EnableWindow(g_randomizerStatus, FALSE);
281 CreateCheckbox(10, 300, CHALLENGE_ONLY);
282 CreateLabel(30, 300, 200, L"Randomize the challenge only");
283 CreateCheckbox(10, 320, DISABLE_SNIPES);
284 CheckDlgButton(g_hwnd, DISABLE_SNIPES, TRUE);
285 CreateLabel(30, 320, 240, L"Disable Swamp and Shadows snipes");
286 CreateCheckbox(10, 340, SPEED_UP_AUTOSCROLLERS);
287 CreateLabel(30, 340, 205, L"Speed up various autoscrollers");
288
289 // CreateButton(200, 50, 200, L"Test RNG", TMP5);
290 // g_rngDebug = CreateWindow(L"STATIC", L"",
291 // WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT,
292 // 200, 80, 200, 200, g_hwnd, NULL, g_hInstance, NULL);
293//#ifndef NDEBUG
294// g_panelId = CreateText(200, 100, 100, L"59");
295// CreateButton(200, 130, 100, L"Read", TMP1);
296// CreateButton(200, 160, 100, L"Write", TMP2);
297 CreateButton(200, 190, 100, L"Solve", TMP3);
298// CreateButton(200, 220, 100, L"Randomize2", TMP4);
299//#endif
300
301 g_witnessProc->StartHeartbeat(g_hwnd, HEARTBEAT);
302
303 ShowWindow(g_hwnd, nCmdShow);
304 UpdateWindow(g_hwnd);
305
306 MSG msg;
307 while (GetMessage(&msg, nullptr, 0, 0) == TRUE) {
308 TranslateMessage(&msg);
309 DispatchMessage(&msg);
310 }
311
312 return (int) msg.wParam;
313}