-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
281 lines (224 loc) · 8.14 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using AForge.Imaging;
using AForge.Imaging.Filters;
using CoreAudioApi;
using Point = System.Drawing.Point;
namespace Horgaszbot
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool fStopRequested;
private bool fFishing;
private IntPtr hwnd;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (fFishing)
{
fStopRequested = true;
return;
}
fFishing = true;
Button1.Content = "Stop";
var bmpAncor = BmpFromRes("Horgaszbot.ancor.bmp");
fStopRequested = false;
while (!fStopRequested)
{
try
{
hwnd = WowLocator.HwndFind();
DoEvents();
var cursorHandleNotAncor = CursorHandleGet(0, 0);
var bmp1 = new ScreenCapture().CaptureWindow(hwnd);
CastFishingLine();
var bmp2 = new ScreenCapture().CaptureWindow(hwnd);
var rgrect = RgrectBobberCandidate(bmp1, bmp2);
var bmp = Tsto(bmp2, rgrect, null);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
Image1.Source = bi;
foreach (var rect in rgrect)
{
DoEvents();
var pt = new Point((rect.Left + rect.Right)/2, (rect.Top + rect.Bottom)/2);
if (FBobbler(pt, bmpAncor, cursorHandleNotAncor))
{
if (FWaitForFish())
CatchFish(pt);
break;
}
}
}
catch (StopReq)
{
break;
}
catch (PauseReq)
{
continue;
}
}
fFishing = false;
Button1.Content = "Start";
}
private class PauseReq : Exception
{
}
private class StopReq : Exception
{
}
void DoEvents()
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
if (fStopRequested)
throw new StopReq();
if (User32.GetForegroundWindow() != hwnd)
{
Thread.Sleep(100);
throw new PauseReq();
}
}
private bool FBobbler(Point pt, Bitmap bmpAncor, IntPtr? cursorHandleNotAncor)
{
var cursorHandle = CursorHandleGet(pt.X, pt.Y);
if (cursorHandle == cursorHandleNotAncor)
return false;
BitmapSource biIcon;
User32.aICONINFO icInfo;
var hicon = User32.CopyIcon(cursorHandle.Value);
if (User32.GetIconInfo(hicon, out icInfo))
{
Icon ic = System.Drawing.Icon.FromHandle(hicon);
var bmpCursor = ic.ToBitmap();
User32.DestroyIcon(hicon);
for (int x = 0; x < 32; x++)
for (int y = 0; y < 32; y++)
if (bmpAncor.GetPixel(x, y).GetHue() != bmpCursor.GetPixel(x, y).GetHue())
return false;
}
return true;
}
private List<Rectangle> RgrectBobberCandidate(Bitmap imgBefore, Bitmap imgAfter)
{
var img = XXX(imgBefore, imgAfter);
var blobCounter = new BlobCounter();
blobCounter.ProcessImage(img);
return blobCounter.GetObjectsRectangles().ToList();
}
private Bitmap Tsto(Bitmap bmp, IEnumerable<Rectangle> rgrect, Point? ptBobber)
{
var bmpDst = new Bitmap(bmp);
using (var g = Graphics.FromImage(bmpDst))
{
foreach (var rect in rgrect)
g.DrawRectangle(Pens.White, rect);
if (ptBobber != null)
g.FillEllipse(Brushes.Red, ptBobber.Value.X - 5, ptBobber.Value.Y - 5, 10, 10);
}
return bmpDst;
}
private Bitmap XXX(Bitmap bmpBefore, Bitmap bmpAfter)
{
var filter = new Grayscale(0.2125, 0.7154, 0.0721);
bmpBefore = filter.Apply(bmpBefore);
bmpAfter = filter.Apply(bmpAfter);
// create filters
var differenceFilter = new Difference();
IFilter thresholdFilter = new Threshold(15);
// set backgroud frame as an overlay for difference filter
differenceFilter.OverlayImage = bmpBefore;
// apply the filters
Bitmap tmp1 = differenceFilter.Apply(bmpAfter);
Bitmap tmp2 = thresholdFilter.Apply(tmp1);
IFilter erosionFilter = new Erosion();
// apply the filter
Bitmap tmp3 = erosionFilter.Apply(tmp2);
IFilter pixellateFilter = new Pixellate();
// apply the filter
Bitmap tmp4 = pixellateFilter.Apply(tmp3);
return tmp4;
}
public void Jump()
{
Console.WriteLine("jump");
SendKey(" ");
Thread.Sleep(1000);
}
public void CastFishingLine()
{
SendKey("1");
Thread.Sleep(2000);
}
public void SendKey(string st)
{
User32.PostMessage(hwnd, User32.WM_KEYDOWN, new IntPtr(st[0]), IntPtr.Zero);
User32.PostMessage(hwnd, User32.WM_KEYUP, new IntPtr(st[0]), IntPtr.Zero);
}
public IntPtr? CursorHandleGet(int x, int y)
{
var pt = new User32.POINT { x = x, y = y };
User32.ClientToScreen(hwnd, ref pt);
User32.SetCursorPos(pt.x, pt.y);
Thread.Sleep(20);
var info = new User32.CursorInfo();
info.Size = Marshal.SizeOf(info.GetType());
if (User32.GetCursorInfo(out info))
return info.Handle;
return null;
}
public void CatchFish(Point pt)
{
User32.PostMessage(hwnd, User32.WM_RBUTTONDOWN, IntPtr.Zero, new IntPtr((pt.Y << 16) + pt.X));
Thread.Sleep(50);
User32.PostMessage(hwnd, User32.WM_RBUTTONUP, IntPtr.Zero, new IntPtr((pt.Y << 16) + pt.X));
Thread.Sleep(3000);
}
private Bitmap BmpFromRes(string resid)
{
return new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream(resid));
}
private bool FWaitForFish()
{
var devEnum = new MMDeviceEnumerator();
var defaultDevice = devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
var dtStart = DateTime.Now;
var qMpv = new Queue<float>();
while ((DateTime.Now - dtStart).TotalSeconds < 30)
{
Thread.Sleep(100);
qMpv.Enqueue(defaultDevice.AudioMeterInformation.MasterPeakValue);
if (qMpv.Count > 5)
qMpv.Dequeue();
if (qMpv.Average() > 0.15)
return true;
DoEvents();
}
return false;
}
}
}