Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace AutoFish
  14. {
  15. class Program
  16. {
  17. //Key Press
  18. [DllImport("user32.dll")]
  19. public static extern int SetForegroundWindow(IntPtr hWnd);
  20. //Mouse Click
  21. [DllImport("user32.dll")]//, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  22. public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
  23. public const int MOUSEEVENTF_LEFTDOWN = 0x02;
  24. public const int MOUSEEVENTF_LEFTUP = 0x04;
  25. public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
  26. public const int MOUSEEVENTF_RIGHTUP = 0x10;
  27. private const int MOUSEEVENTF_MOVE = 0x0001;
  28.  
  29. [DllImport("user32.dll", SetLastError = true)]
  30. public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
  31. //-----
  32. private static Color splashColor;
  33. private static int splashColorThreshold = 35;
  34. private static bool isFishing = false;
  35. static void Main(string[] args)
  36. {
  37. //Set Values
  38. splashColor = Color.White;
  39. //Init
  40. Console.ForegroundColor = ConsoleColor.White;
  41. Process[] processes = Process.GetProcessesByName("Wow");
  42. if (processes.Length==0)
  43. {
  44. Console.WriteLine("WOW is not open");
  45. Console.ReadLine();
  46. return;
  47. }
  48. else
  49. {
  50. Console.WriteLine("Found World Of Warcraft!");
  51. }
  52. //Actual Fishing
  53. Console.WriteLine("----------");
  54. Console.WriteLine("STARTING TO FISH");
  55. Stopwatch timerFromStartedFishing = new Stopwatch();//30sec
  56. Stopwatch totalTimer = Stopwatch.StartNew();//30sec
  57. Stopwatch turnDownComputerTimer = Stopwatch.StartNew();
  58. float totalFishedTime = 0;
  59. int totalFishCaught = 0;
  60. while (true)
  61. {
  62. if (!isFishing)
  63. {
  64. SetForegroundWindow(processes[0].MainWindowHandle);
  65. Thread.Sleep(100);
  66. //Check if we have buff, if don't use one
  67. UseBuffIfNeeded();
  68. //Start fishing
  69. SendKeys.SendWait("{1}");
  70. isFishing = true;
  71. timerFromStartedFishing.Start();
  72. }
  73.  
  74. #region Record Screen
  75. Size screenSize = Screen.PrimaryScreen.Bounds.Size;
  76. Size offset = new Size(500, 300);//Screen.PrimaryScreen.Bounds.Size;
  77. Point splashLocation = ScanGetSplashPos(screenSize, offset);
  78.  
  79. if (splashLocation != new Point(-1, -1))
  80. {
  81. splashLocation.X += (screenSize.Width - offset.Width) / 2;
  82. splashLocation.Y += (screenSize.Height - offset.Height) / 2;
  83. }
  84.  
  85.  
  86. #endregion
  87. if (splashLocation != new Point(-1,-1))
  88. {
  89. //Console.Beep();
  90. float fishedTime = (timerFromStartedFishing.ElapsedMilliseconds / 1000f);
  91. totalFishedTime += fishedTime;
  92. totalFishCaught++;
  93.  
  94. Console.WriteLine("AVG: " + (totalFishedTime / totalFishCaught) + "s, Total Caught: " + totalFishCaught + "x");
  95. isFishing = false;
  96. timerFromStartedFishing.Stop();
  97. timerFromStartedFishing.Reset();
  98. //ClickOnPointTool.ClickOnPoint(processes[0].MainWindowHandle, splashLocation);
  99. DoMouseClick(splashLocation);
  100. //Console.Beep();
  101. Thread.Sleep(2000);
  102. }
  103. if (timerFromStartedFishing.ElapsedMilliseconds>30000)
  104. {
  105. isFishing = false;
  106. timerFromStartedFishing.Stop();
  107. timerFromStartedFishing.Reset();
  108. }
  109. //stopwatch.Stop();
  110. //Console.WriteLine(stopwatch.ElapsedMilliseconds);
  111. //Thread.Sleep(100);
  112. if (totalTimer.ElapsedMilliseconds>10000)
  113. {
  114. System.GC.Collect();
  115. totalTimer.Reset();
  116. totalTimer.Start();
  117. }
  118. //if (turnDownComputerTimer.ElapsedMilliseconds > 10800000)//(3*60 * 60 * 1000)=10800000
  119. //{
  120. // Process.Start("shutdown", "/s /t 0");
  121. // return;
  122. //}
  123. }
  124. }
  125. private static void UseBuffIfNeeded()
  126. {
  127. Bitmap image= GetDesktopImage(new Size(1, 1), new Size(3052 ,38),false);
  128. Console.WriteLine(image.GetPixel(0, 0));
  129. if (image.GetPixel(0,0) != Color.FromArgb(255,0,1,0))
  130. {
  131. UseBuff();
  132.  
  133. }
  134. }
  135. private static void UseBuff()
  136. {
  137. Console.WriteLine("Using buff");
  138. Thread.Sleep(100);
  139. DoMouseClick(new Point(3000, 965),2000);
  140. Thread.Sleep(2000);
  141. DoMouseClick(new Point(240, 840), 2000);
  142. Thread.Sleep(10000);
  143. }
  144. private static Point ScanGetSplashPos(Size screenSize, Size offset)
  145. {
  146. Bitmap image;
  147. image = GetDesktopImage(offset, screenSize,true);
  148. //image.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\AntiFishingTest\screenshot.png"); // saves the image
  149. //int splashMatches = TotalCloseColors(image, splashColor, splashColorThreshold);
  150. return GetColorLocation(image, splashColor, splashColorThreshold);
  151. }
  152. private static void DoMouseClick(Point point,int downTime=400)
  153. {
  154.  
  155. //move to coordinates
  156. //Point pt = (Point)pc.ConvertFromString(X + "," + Y);
  157. Cursor.Position = point;
  158. Thread.Sleep(100);
  159. //perform click
  160. //mouse_event(MOUSEEVENTF_LEFTDOWN,0,0, 0, 0);
  161. //mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  162. //Thread.Sleep(10);
  163. mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
  164. Thread.Sleep(downTime);
  165.  
  166. mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
  167.  
  168. //Thread.Sleep(2000);
  169. }
  170. private static int TotalCloseColors(Bitmap image,Color closeColor,int threshold)
  171. {
  172. int counter = 0;
  173. for (int x = 0; x < image.Width; x++)
  174. {
  175. for (int y = 0; y < image.Height; y++)
  176. {
  177. if (ColorsAreClose(image.GetPixel(x, y), closeColor,threshold))
  178. {
  179. counter++;
  180. }
  181. }
  182. }
  183. return counter;
  184. }
  185. private static Point GetColorLocation(Bitmap image,Color color, int threshold)
  186. {
  187. if (image==null)
  188. {
  189. return new Point(-1, -1);
  190. }
  191. int matches = 0;
  192. int totalX = 0;
  193. int totalY = 0;
  194. for (int x = 0; x < image.Width; x++)
  195. {
  196. for (int y = 0; y < image.Height; y++)
  197. {
  198. if (ColorsAreClose(image.GetPixel(x, y), color, threshold))
  199. {
  200. matches++;
  201. totalX += x;
  202. totalY += y;
  203. }
  204. }
  205. }
  206. if (matches == 0)
  207. {
  208. return new Point(-1, -1);
  209. }
  210. return new Point(totalX / matches, totalY / matches);
  211. }
  212. private static bool ColorsAreClose(Color a, Color z, int threshold = 15)
  213. {
  214. int r = (int)a.R - z.R,
  215. g = (int)a.G - z.G,
  216. b = (int)a.B - z.B;
  217. return (r * r + g * g + b * b) <= threshold * threshold;
  218. }
  219. public static Bitmap GetDesktopImage(Size inputSize, Size screenSize,bool centerMode)
  220. {
  221. WIN32_API.SIZE size;
  222.  
  223. size.cx = inputSize.Width;// WIN32_API.GetSystemMetrics(WIN32_API.SM_CXSCREEN);
  224. size.cy = inputSize.Height;// WIN32_API.GetSystemMetrics(WIN32_API.SM_CYSCREEN);
  225.  
  226. Bitmap bmpScreenshot = new Bitmap(size.cx,
  227. size.cy,
  228. PixelFormat.Format32bppArgb);
  229.  
  230. // Create a graphics object from the bitmap.
  231. var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
  232.  
  233. // Take the screenshot from the upper left corner to the right bottom corner.
  234. if (centerMode)
  235. {
  236. gfxScreenshot.CopyFromScreen(screenSize.Width / 2 - size.cx / 2, screenSize.Height / 2 - size.cy / 2, 0, 0,
  237. inputSize,
  238. CopyPixelOperation.SourceCopy);
  239. }
  240. else
  241. {
  242. gfxScreenshot.CopyFromScreen(screenSize.Width, screenSize.Height, 0, 0,
  243. inputSize,
  244. CopyPixelOperation.SourceCopy);
  245. }
  246. gfxScreenshot.Dispose();
  247. return bmpScreenshot;
  248. #region Old
  249.  
  250. IntPtr hDC = WIN32_API.GetDC(WIN32_API.GetDesktopWindow());
  251. IntPtr hMemDC = WIN32_API.CreateCompatibleDC(hDC);
  252.  
  253.  
  254.  
  255. IntPtr m_HBitmap = WIN32_API.CreateCompatibleBitmap(hDC, size.cx, size.cy);
  256.  
  257. if (m_HBitmap != IntPtr.Zero)
  258. {
  259. IntPtr hOld = (IntPtr)WIN32_API.SelectObject(hMemDC, m_HBitmap);
  260. if (centerMode)
  261. {
  262. WIN32_API.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, screenSize.Width / 2 - size.cx / 2, screenSize.Height / 2 - size.cy / 2, WIN32_API.SRCCOPY);
  263. }
  264. else
  265. {
  266. WIN32_API.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, screenSize.Width, screenSize.Height, WIN32_API.SRCCOPY);
  267. }
  268. WIN32_API.SelectObject(hMemDC, hOld);
  269. WIN32_API.DeleteDC(hMemDC);
  270. WIN32_API.ReleaseDC(WIN32_API.GetDesktopWindow(), hDC);
  271. return System.Drawing.Image.FromHbitmap(m_HBitmap);
  272. }
  273. #endregion
  274. //Application.Restart();
  275. return null;
  276. }
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement