-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
509 lines (442 loc) · 19 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
using System;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Threading;
namespace Cookie_Clicker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public int SleepTimeMillis { get; set; }
public int InitialDelay { get; set; }
public bool IsFixPosition { get; set; }
public double XPosition { get; set; }
public double YPosition { get; set; }
public int BarrelRollDelay { get; set; }
public int BarrelRollRadius { get; set; }
public bool IsBarrelRollCheck { get; set; }
public bool IsGoldenScanCheck { get; set; }
public int GoldenScanDelay { get; set; }
public bool IsAutomaticModeCheck { get; set; }
public int AutomaticModeDelay { get; set; }
bool canAutoClickerStart = false;
bool canSetGoldenCookiePosition = false;
bool canSetGoldenScan = false;
bool toggleAutoClickerState = false;
private int rotationSteps = 36 * 2;
private int rotationCliksPerStep = 4;
private int rotationClickDelay = 10;
private IntPtr _windowHandle;
private HwndSource _source;
private DispatcherTimer dispatcherTimer;
private DispatcherTimer goldenDispatcherTimer;
private DispatcherTimer automaticModeDispatcherTimer;
private const int HOTKEY_ID = 9000;
private const uint MOD_NONE = 0x0000; // (none)
private const uint MOD_ALT = 0x0001; //ALT
private const uint MOD_CONTROL = 0x0002; //CTRL
private const uint MOD_SHIFT = 0x0004; //SHIFT
private const uint MOD_WIN = 0x0008; //WINDOWS
public const int MOUSEEVENTF_LEFTDOWN = 0x02; // Mouse left click down
public const int MOUSEEVENTF_LEFTUP = 0x04; // Mouse left click up
public const int MOUSEEVENTF_WHEEL = 0x0800; // Mouse wheel
Thread Clicker;
bool isKeyToggleAllowed = false;
Key toggleKey = Key.RightCtrl; // default key Right Control
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);
[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
public Int32 X;
public Int32 Y;
};
// MAIN
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
SleepTimeMillis = 20;
InitialDelay = 200;
IsFixPosition = false;
XPosition = 290;
YPosition = 420;
BarrelRollDelay = 30;
BarrelRollRadius = 170;
IsBarrelRollCheck = false;
IsGoldenScanCheck = false;
GoldenScanDelay = 30;
IsAutomaticModeCheck = false;
AutomaticModeDelay = 10;
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
_windowHandle = new WindowInteropHelper(this).Handle;
_source = HwndSource.FromHwnd(_windowHandle);
_source.AddHook(HwndHook);
RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, (uint) KeyInterop.VirtualKeyFromKey(toggleKey));
canAutoClickerStart = true;
}
protected override void OnClosed(EventArgs e)
{
canAutoClickerStart = false;
_source.RemoveHook(HwndHook);
UnregisterHotKey(_windowHandle, HOTKEY_ID);
base.OnClosed(e);
}
// The magic function
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
switch (msg)
{
case WM_HOTKEY:
switch (wParam.ToInt32())
{
case HOTKEY_ID:
int vkey = ((int)lParam >> 16) & 0xFFFF;
// Stop clicker if Windows key press (Fail safe)
// Dont work? >>>>> Investigate <<<<<<
if (vkey == KeyInterop.VirtualKeyFromKey(Key.LWin))
{
Stop();
clearKeyToggle(null, null);
}
else if (vkey == KeyInterop.VirtualKeyFromKey(toggleKey))
{
// Key Press Logic
if (toggleAutoClickerState)
{
// if clicker status is running (true), toggle to false to stop it
Stop();
if (dispatcherTimer != null)
{
dispatcherTimer.Stop();
}
if (goldenDispatcherTimer != null)
{
goldenDispatcherTimer.Stop();
}
if (automaticModeDispatcherTimer != null)
{
automaticModeDispatcherTimer.Stop();
}
}
else
{
// if clicker is not running (false), start it
Start();
if (IsBarrelRollCheck)
{
// DispatcherTimer for Barrel Roll
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
int temp = BarrelRollDelay;
int hours = temp / 3600;
temp = temp - hours * 3600;
int minutes = temp / 60;
int seconds = temp - minutes * 60;
dispatcherTimer.Interval = new TimeSpan(hours, minutes, seconds);
dispatcherTimer.Start();
}
if (IsGoldenScanCheck)
{
// DispatcherTimer for Golden Cookie Scan
goldenDispatcherTimer = new DispatcherTimer();
goldenDispatcherTimer.Tick += new EventHandler(handleGoldenDispatcherTimer);
int temp = GoldenScanDelay;
int hours = temp / 3600;
temp = temp - hours * 3600;
int minutes = temp / 60;
int seconds = temp - minutes * 60;
goldenDispatcherTimer.Interval = new TimeSpan(hours, minutes, seconds);
goldenDispatcherTimer.Start();
}
if (IsAutomaticModeCheck)
{
// DispatcherTimer for Automatic mode
automaticModeDispatcherTimer = new DispatcherTimer();
automaticModeDispatcherTimer.Tick += new EventHandler(handleAutomaticModeDispatcherTimer);
int temp = AutomaticModeDelay;
int hours = temp / 3600;
temp = temp - hours * 3600;
int minutes = temp / 60;
int seconds = temp - minutes * 60;
automaticModeDispatcherTimer.Interval = new TimeSpan(hours, minutes, seconds);
automaticModeDispatcherTimer.Start();
}
}
}
handled = true;
break;
}
break;
}
return IntPtr.Zero;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var window = Window.GetWindow(this);
window.KeyDown += HandleKeyPress;
}
private void HandleKeyPress(object sender, KeyEventArgs e)
{
if (isKeyToggleAllowed)
{
UnregisterHotKey(_windowHandle, HOTKEY_ID);
toggleKey = e.Key;
keyNameTextBox.Text = toggleKey.ToString();
RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_NONE, (uint) KeyInterop.VirtualKeyFromKey(toggleKey));
isKeyToggleAllowed = false;
}
return;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
Stop();
doABarrelRoll(null, null);
Start();
}
private void handleGoldenDispatcherTimer(object sender, EventArgs e)
{
Stop();
doAGoldenScan(null, null);
Start();
}
private void handleAutomaticModeDispatcherTimer(object sender, EventArgs e)
{
Stop();
doAUpgradeBuy(null, null);
Start();
}
private void doAClick()
{
if (IsFixPosition)
{
LeftMouseClick((int) XPosition, (int) YPosition);
}
else
{
Point currentMousePosition = GetMousePosition();
LeftMouseClick((int)currentMousePosition.X, (int)currentMousePosition.Y);
}
}
public static Point GetMousePosition()
{
var w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
return new Point(w32Mouse.X, w32Mouse.Y);
}
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
// Simulate a wheel scroll
// Positive amount is going up, negative going down in screen
// One wheel click is defined as WHEEL_DELTA, which is 120
public static void ScrollMouse(int xpos, int ypos, int amount)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_WHEEL, xpos, ypos, amount, 0);
}
private void setKeyToggle(object sender, RoutedEventArgs e)
{
isKeyToggleAllowed = true;
}
private void clearKeyToggle(object sender, RoutedEventArgs e)
{
isKeyToggleAllowed = false;
keyNameTextBox.Text = "";
UnregisterHotKey(_windowHandle, HOTKEY_ID);
}
public void Start()
{
toggleAutoClickerState = true; // allow clicker ejecution
Thread.Sleep(InitialDelay);
// new clicker thread creation
Clicker = new Thread(MyAutoClicker);
Clicker.IsBackground = true;
Clicker.Start();
}
public void Stop()
{
toggleAutoClickerState = false; // stops clicker ejecution
if (Clicker != null)
{
Clicker.Join(); // wait for the thread to finish
}
}
public void MyAutoClicker()
{
try
{
while (toggleAutoClickerState)
{
Thread.Sleep(SleepTimeMillis);
doAClick();
}
}
catch
{
// End silently if error
}
}
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
private void resetGoldenCookiePosition(object sender, RoutedEventArgs e)
{
XPosition = 300 / 1920 * SystemParameters.PrimaryScreenWidth;
YPosition = 400 / 1080 * SystemParameters.PrimaryScreenHeight;
xPositionTextBox.Text = XPosition.ToString();
yPositionTextBox.Text = YPosition.ToString();
IsFixPosition = false;
fixPositionCheckBox.IsChecked = false;
}
private void checkFixPosition(object sender, RoutedEventArgs e)
{
IsFixPosition = true;
}
private void uncheckFixPosition(object sender, RoutedEventArgs e)
{
IsFixPosition = false;
}
private void doABarrelRoll(object sender, RoutedEventArgs e)
{
// rotationRadius = (int) XPosition / 2;
int increment = 360 / rotationSteps;
double theta, xPos, yPos;
Point originalMousePosition = GetMousePosition();
for (int i = 0; i < 360; i += increment)
{
//if (!toggleAutoClickerState) { break; }
theta = i * Math.PI / 180;
xPos = XPosition + BarrelRollRadius * Math.Cos(theta);
yPos = YPosition + BarrelRollRadius * Math.Sin(theta);
for (int j=0; j<rotationCliksPerStep; j++)
{
//if (!toggleAutoClickerState) { break; }
Thread.Sleep(rotationClickDelay);
LeftMouseClick((int) xPos, (int) yPos);
}
}
SetCursorPos((int) originalMousePosition.X, (int) originalMousePosition.Y);
}
private void doAGoldenScan(object sender, RoutedEventArgs e)
{
Point originalMousePosition = GetMousePosition();
Point goldenStartPoint = new Point(5, 180);
Point goldenEndPoint = new Point(1580, 1010);
int pixelStepHorizontal = 25;
int pixelStepVertical = 25;
for (int y = (int)goldenStartPoint.Y; y < (int)goldenEndPoint.Y; y += pixelStepVertical)
{
//if (!toggleAutoClickerState) { break; }
for (int x = (int)goldenStartPoint.X; x < (int)goldenEndPoint.X; x += pixelStepHorizontal)
{
//if (!toggleAutoClickerState) { break; }
if (x < 90 && y > 890) { continue; } // avoid Klumbor in the left bottom side
Thread.Sleep(5);
LeftMouseClick(x, y);
}
}
SetCursorPos((int)originalMousePosition.X, (int)originalMousePosition.Y);
}
private void doAUpgradeBuy(object sender, RoutedEventArgs e)
{
Point originalMousePosition = GetMousePosition();
Point automaticBuyStartPoint = new Point(1625, 1005);
int distanteBetweenBuildings = 64; // Pixels
int distanteToUpgrades = 108;
int distanceBetweenUpgradeAndSwitches = 76;
int scrollMaximumDistancePositive = 140 * 7;
int scrollMaximumDistanceNegative = - (140 * 7);
// Scroll to the top
ScrollMouse((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y, scrollMaximumDistancePositive);
Thread.Sleep(50);
// Buy upgrades first to better cookies/sec scaling
for (int k = 0; k < 10; k++)
{
Thread.Sleep(100);
LeftMouseClick((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y - 11 * distanteBetweenBuildings - distanteToUpgrades);
}
// Scroll to the end of the buildings
ScrollMouse((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y, scrollMaximumDistanceNegative);
// Buy last buildings upgrades
for (int i = 0; i < 7; i++)
{
Thread.Sleep(50);
ScrollMouse((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y, 75);
for (int i2 = 0; i2 < 10; i2++)
{
Thread.Sleep(15);
LeftMouseClick((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y);
}
}
// Buy 11 first buildings
for (int j = 1; j <= 11; j++)
{
Thread.Sleep(50);
for (int j2 = 0; j2 < 10; j2++)
{
Thread.Sleep(15);
LeftMouseClick((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y - j * distanteBetweenBuildings);
}
}
// Buy switches ?
Thread.Sleep(100);
for (int l = 0; l < 3; l++)
{
Thread.Sleep(100);
LeftMouseClick((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y - 11 * distanteBetweenBuildings - distanteToUpgrades - distanceBetweenUpgradeAndSwitches);
}
ScrollMouse((int)automaticBuyStartPoint.X, (int)automaticBuyStartPoint.Y, 140); // Set the scroll to the top
SetCursorPos((int)originalMousePosition.X, (int)originalMousePosition.Y);
}
private void checkBarrelRoll(object sender, RoutedEventArgs e)
{
IsBarrelRollCheck = true;
}
private void uncheckBarrelRoll(object sender, RoutedEventArgs e)
{
IsBarrelRollCheck = false;
}
private void checkGoldenScan(object sender, RoutedEventArgs e)
{
IsGoldenScanCheck = true;
}
private void uncheckGoldenScan(object sender, RoutedEventArgs e)
{
IsGoldenScanCheck = false;
}
private void checkAutomaticMode(object sender, RoutedEventArgs e)
{
IsAutomaticModeCheck = true;
}
private void uncheckAutomaticMode(object sender, RoutedEventArgs e)
{
IsAutomaticModeCheck = false;
}
}
}